1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
/* Quicktime muxer plugin for GStreamer
* Copyright (C) 2008 Thiago Sousa Santos <thiagoss@embedded.ufcg.edu.br>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*
* Unless otherwise indicated, Source Code is licensed under MIT license.
* See further explanation attached in License Statement (distributed in the file
* LICENSE).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "descriptors.h"
/*
* Some mp4 structures (descriptors) use a coding scheme for
* representing its size.
* It is grouped in bytes. The 1st bit set to 1 means we need another byte,
* 0 otherwise. The remaining 7 bits are the useful values.
*
* The next set of functions handle those values
*/
/*
* Gets an unsigned integer and packs it into a 'expandable size' format
* (as used by mp4 descriptors)
* @size: the integer to be parsed
* @ptr: the array to place the result
* @array_size: the size of ptr array
*/
static void
expandable_size_parse (guint64 size, guint8 * ptr, guint32 array_size)
{
int index = 0;
memset (ptr, 0, sizeof (array_size));
while (size > 0 && index < array_size) {
ptr[index++] = (size > 0x7F ? 0x80 : 0x0) | (size & 0x7F);
size = size >> 7;
}
}
/*
* Gets how many positions in an array holding an 'expandable size'
* are really used
*
* @ptr: the array with the 'expandable size'
* @array_size: the size of ptr array
*
* Returns: the number of really used positions
*/
static guint64
expandable_size_get_length (guint8 * ptr, guint32 array_size)
{
gboolean next = TRUE;
guint32 index = 0;
while (next && index < array_size) {
next = (ptr[index] & 0x80);
index++;
}
return index;
}
/*
* Initializers below
*/
static void
desc_base_descriptor_init (BaseDescriptor * bd, guint8 tag, guint32 size)
{
bd->tag = tag;
expandable_size_parse (size, bd->size, 4);
}
static void
desc_dec_specific_info_init (DecoderSpecificInfoDescriptor * dsid)
{
desc_base_descriptor_init (&dsid->base, DECODER_SPECIFIC_INFO_TAG, 0);
dsid->length = 0;
dsid->data = NULL;
}
DecoderSpecificInfoDescriptor *
desc_dec_specific_info_new (void)
{
DecoderSpecificInfoDescriptor *desc =
g_new0 (DecoderSpecificInfoDescriptor, 1);
desc_dec_specific_info_init (desc);
return desc;
}
static void
desc_dec_conf_desc_init (DecoderConfigDescriptor * dcd)
{
desc_base_descriptor_init (&dcd->base, DECODER_CONFIG_DESC_TAG, 0);
dcd->dec_specific_info = NULL;
}
static void
desc_sl_conf_desc_init (SLConfigDescriptor * sl)
{
desc_base_descriptor_init (&sl->base, SL_CONFIG_DESC_TAG, 0);
sl->predefined = 0x2;
}
void
desc_es_init (ESDescriptor * es)
{
desc_base_descriptor_init (&es->base, ES_DESCRIPTOR_TAG, 0);
es->id = 0;
es->flags = 0;
es->depends_on_es_id = 0;
es->ocr_es_id = 0;
es->url_length = 0;
es->url_string = NULL;
desc_dec_conf_desc_init (&es->dec_conf_desc);
desc_sl_conf_desc_init (&es->sl_conf_desc);
}
ESDescriptor *
desc_es_descriptor_new (void)
{
ESDescriptor *es = g_new0 (ESDescriptor, 1);
desc_es_init (es);
return es;
}
/*
* Deinitializers/Destructors below
*/
static void
desc_base_descriptor_clear (BaseDescriptor * base)
{
}
void
desc_dec_specific_info_free (DecoderSpecificInfoDescriptor * dsid)
{
desc_base_descriptor_clear (&dsid->base);
if (dsid->data) {
g_free (dsid->data);
dsid->data = NULL;
}
g_free (dsid);
}
static void
desc_dec_conf_desc_clear (DecoderConfigDescriptor * dec)
{
desc_base_descriptor_clear (&dec->base);
if (dec->dec_specific_info) {
desc_dec_specific_info_free (dec->dec_specific_info);
}
}
static void
desc_sl_config_descriptor_clear (SLConfigDescriptor * sl)
{
desc_base_descriptor_clear (&sl->base);
}
void
desc_es_descriptor_clear (ESDescriptor * es)
{
desc_base_descriptor_clear (&es->base);
if (es->url_string) {
g_free (es->url_string);
es->url_string = NULL;
}
desc_dec_conf_desc_clear (&es->dec_conf_desc);
desc_sl_config_descriptor_clear (&es->sl_conf_desc);
}
/*
* Size handling functions below
*/
void
desc_dec_specific_info_alloc_data (DecoderSpecificInfoDescriptor * dsid,
guint32 size)
{
if (dsid->data) {
g_free (dsid->data);
}
dsid->data = g_new0 (guint8, size);
dsid->length = size;
}
static void
desc_base_descriptor_set_size (BaseDescriptor * bd, guint32 size)
{
expandable_size_parse (size, bd->size, 4);
}
static guint64
desc_base_descriptor_get_size (BaseDescriptor * bd)
{
guint64 size = 0;
size += sizeof (guint8);
size += expandable_size_get_length (bd->size, 4) * sizeof (guint8);
return size;
}
static guint64
desc_sl_config_descriptor_get_size (SLConfigDescriptor * sl_desc)
{
guint64 size = 0;
guint64 extra_size = 0;
size += desc_base_descriptor_get_size (&sl_desc->base);
/* predefined */
extra_size += sizeof (guint8);
desc_base_descriptor_set_size (&sl_desc->base, extra_size);
return size + extra_size;
}
static guint64
desc_dec_specific_info_get_size (DecoderSpecificInfoDescriptor * dsid)
{
guint64 size = 0;
guint64 extra_size = 0;
size += desc_base_descriptor_get_size (&dsid->base);
extra_size += sizeof (guint8) * dsid->length;
desc_base_descriptor_set_size (&dsid->base, extra_size);
return size + extra_size;
}
static guint64
desc_dec_config_descriptor_get_size (DecoderConfigDescriptor * dec_desc)
{
guint64 size = 0;
guint64 extra_size = 0;
size += desc_base_descriptor_get_size (&dec_desc->base);
/* object type */
extra_size += sizeof (guint8);
/* stream type */
extra_size += sizeof (guint8);
/* buffer size */
extra_size += sizeof (guint8) * 3;
/* max bitrate */
extra_size += sizeof (guint32);
/* avg bitrate */
extra_size += sizeof (guint32);
if (dec_desc->dec_specific_info) {
extra_size += desc_dec_specific_info_get_size (dec_desc->dec_specific_info);
}
desc_base_descriptor_set_size (&dec_desc->base, extra_size);
return size + extra_size;
}
static guint64
desc_es_descriptor_get_size (ESDescriptor * es)
{
guint64 size = 0;
guint64 extra_size = 0;
size += desc_base_descriptor_get_size (&es->base);
/* id */
extra_size += sizeof (guint16);
/* flags */
extra_size += sizeof (guint8);
/* depends_on_es_id */
if (es->flags & 0x80) {
extra_size += sizeof (guint16);
}
if (es->flags & 0x40) {
/* url_length */
extra_size += sizeof (guint8);
/* url */
extra_size += sizeof (gchar) * es->url_length;
}
if (es->flags & 0x20) {
/* ocr_es_id */
extra_size += sizeof (guint16);
}
extra_size += desc_dec_config_descriptor_get_size (&es->dec_conf_desc);
extra_size += desc_sl_config_descriptor_get_size (&es->sl_conf_desc);
desc_base_descriptor_set_size (&es->base, extra_size);
return size + extra_size;
}
static gboolean
desc_es_descriptor_check_stream_dependency (ESDescriptor * es)
{
return es->flags & 0x80;
}
static gboolean
desc_es_descriptor_check_url_flag (ESDescriptor * es)
{
return es->flags & 0x40;
}
static gboolean
desc_es_descriptor_check_ocr (ESDescriptor * es)
{
return es->flags & 0x20;
}
/* Copy/Serializations Functions below */
static guint64
desc_base_descriptor_copy_data (BaseDescriptor * desc, guint8 ** buffer,
guint64 * size, guint64 * offset)
{
guint64 original_offset = *offset;
prop_copy_uint8 (desc->tag, buffer, size, offset);
prop_copy_uint8_array (desc->size, expandable_size_get_length (desc->size, 4),
buffer, size, offset);
return original_offset - *offset;
}
static guint64
desc_sl_config_descriptor_copy_data (SLConfigDescriptor * desc,
guint8 ** buffer, guint64 * size, guint64 * offset)
{
guint64 original_offset = *offset;
if (!desc_base_descriptor_copy_data (&desc->base, buffer, size, offset)) {
return 0;
}
/* predefined attribute */
prop_copy_uint8 (desc->predefined, buffer, size, offset);
return *offset - original_offset;
}
static guint64
desc_dec_specific_info_copy_data (DecoderSpecificInfoDescriptor * desc,
guint8 ** buffer, guint64 * size, guint64 * offset)
{
guint64 original_offset = *offset;
if (!desc_base_descriptor_copy_data (&desc->base, buffer, size, offset)) {
return 0;
}
prop_copy_uint8_array (desc->data, desc->length, buffer, size, offset);
return *offset - original_offset;
}
static guint64
desc_dec_config_descriptor_copy_data (DecoderConfigDescriptor * desc,
guint8 ** buffer, guint64 * size, guint64 * offset)
{
guint64 original_offset = *offset;
if (!desc_base_descriptor_copy_data (&desc->base, buffer, size, offset)) {
return 0;
}
prop_copy_uint8 (desc->object_type, buffer, size, offset);
prop_copy_uint8 (desc->stream_type, buffer, size, offset);
prop_copy_uint8_array (desc->buffer_size_DB, 3, buffer, size, offset);
prop_copy_uint32 (desc->max_bitrate, buffer, size, offset);
prop_copy_uint32 (desc->avg_bitrate, buffer, size, offset);
if (desc->dec_specific_info) {
if (!desc_dec_specific_info_copy_data (desc->dec_specific_info, buffer,
size, offset)) {
return 0;
}
}
return *offset - original_offset;
}
guint64
desc_es_descriptor_copy_data (ESDescriptor * desc, guint8 ** buffer,
guint64 * size, guint64 * offset)
{
guint64 original_offset = *offset;
/* must call this twice to have size fields of all contained descriptors set
* correctly, and to have the size of the size fields taken into account */
desc_es_descriptor_get_size (desc);
desc_es_descriptor_get_size (desc);
if (!desc_base_descriptor_copy_data (&desc->base, buffer, size, offset)) {
return 0;
}
/* id and flags */
prop_copy_uint16 (desc->id, buffer, size, offset);
prop_copy_uint8 (desc->flags, buffer, size, offset);
if (desc_es_descriptor_check_stream_dependency (desc)) {
prop_copy_uint16 (desc->depends_on_es_id, buffer, size, offset);
}
if (desc_es_descriptor_check_url_flag (desc)) {
prop_copy_size_string (desc->url_string, desc->url_length, buffer, size,
offset);
}
if (desc_es_descriptor_check_ocr (desc)) {
prop_copy_uint16 (desc->ocr_es_id, buffer, size, offset);
}
if (!desc_dec_config_descriptor_copy_data (&desc->dec_conf_desc, buffer, size,
offset)) {
return 0;
}
if (!desc_sl_config_descriptor_copy_data (&desc->sl_conf_desc, buffer, size,
offset)) {
return 0;
}
return *offset - original_offset;
}
|