1 /*****************************************************************************
2  * box.h
3  *****************************************************************************
4  * Copyright (C) 2010-2017 L-SMASH project
5  *
6  * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  *****************************************************************************/
20 
21 /* This file is available under an ISC license. */
22 
23 #ifndef LSMASH_BOX_H
24 #define LSMASH_BOX_H
25 
26 /* For generating creation_time and modification_time.
27  * According to ISO/IEC-14496-5-2001, the difference between Unix time and Mac OS time is 2082758400.
28  * However this is wrong and 2082844800 is correct. */
29 #include <time.h>
30 #define ISOM_MAC_EPOCH_OFFSET 2082844800
31 
32 #include "box_type.h"
33 
34 /* aliases internally used only for convenience */
35 typedef struct lsmash_file_tag isom_file_abstract_t;
36 typedef struct lsmash_root_tag isom_root_abstract_t;
37 typedef struct isom_unknown_box_tag isom_unknown_t;
38 typedef struct lsmash_box_tag isom_dummy_t; /* for dummy usage */
39 
40 typedef struct lsmash_box_tag isom_box_t;
41 typedef struct isom_unknown_box_tag isom_unknown_box_t;
42 typedef struct isom_mdhd_tag isom_mdhd_t;
43 typedef struct isom_stbl_tag isom_stbl_t;
44 
45 typedef void (*isom_extension_destructor_t)( void *extension_data );
46 typedef int (*isom_extension_writer_t)( lsmash_bs_t *bs, isom_box_t *box );
47 
48 typedef int (*isom_bitrate_updater_t)( isom_stbl_t *stbl, isom_mdhd_t *mdhd, uint32_t sample_description_index );
49 
50 /* If size is 1, then largesize is actual size.
51  * If size is 0, then this box is the last one in the file. */
52 #define ISOM_BASEBOX_COMMON                                                                             \
53         const lsmash_class_t       *class;                                                              \
54         lsmash_root_t              *root;               /* pointer to root */                           \
55         lsmash_file_t              *file;               /* pointer to file */                           \
56         isom_box_t                 *parent;             /* pointer to the parent box of this box */     \
57         void                       *nonexist_ptr;       /* pointer to non-existing box constant */      \
58         uint8_t                    *binary;             /* used only when LSMASH_BINARY_CODED_BOX */    \
59         isom_extension_destructor_t destruct;           /* box specific destructor */                   \
60         isom_extension_writer_t     write;              /* box specific writer */                       \
61         size_t                      offset_in_parent;   /* offset of this box in parent box struct */   \
62         uint32_t                    manager;            /* flags for L-SMASH */                         \
63         uint64_t                    precedence;         /* precedence of the box position */            \
64         uint64_t                    pos;                /* starting position of this box in the file */ \
65         lsmash_entry_list_t         extensions;         /* extension boxes */                           \
66     uint64_t          size;                             /* the number of bytes in this box */           \
67     lsmash_box_type_t type
68 
69 #define ISOM_FULLBOX_COMMON                                         \
70     ISOM_BASEBOX_COMMON;                                            \
71     uint8_t  version;   /* Basically, version is either 0 or 1 */   \
72     uint32_t flags      /* In the actual structure of box, flags is 24 bits. */
73 
74 #define ISOM_BASEBOX_COMMON_SIZE       8
75 #define ISOM_FULLBOX_COMMON_SIZE      12
76 #define ISOM_LIST_FULLBOX_COMMON_SIZE 16
77 
78 /* flags for L-SMASH */
79 #define LSMASH_UNKNOWN_BOX       0x001
80 #define LSMASH_ABSENT_IN_FILE    0x002
81 #define LSMASH_QTFF_BASE         0x004
82 #define LSMASH_VIDEO_DESCRIPTION 0x008
83 #define LSMASH_AUDIO_DESCRIPTION 0x010
84 #define LSMASH_FULLBOX           0x020
85 #define LSMASH_LAST_BOX          0x040
86 #define LSMASH_INCOMPLETE_BOX    0x080
87 #define LSMASH_BINARY_CODED_BOX  0x100
88 #define LSMASH_PLACEHOLDER       0x200
89 #define LSMASH_WRITTEN_BOX       0x400
90 #define LSMASH_NON_EXISTING_BOX  0x800  /* This flag indicates a read only non-existing box constant.
91                                          * Don't use for wild boxes other than non-existing box constants
92                                          * because this flags prevents attempting to freeing its box. */
93 
94 /* Use these macros for checking existences of boxes.
95  * If the result of LSMASH_IS_EXISTING_BOX is 0, the evaluated box is read only.
96  * If the result of LSMASH_IS_NON_EXISTING_BOX is 1, the evaluated box is read only. */
97 #define LSMASH_IS_EXISTING_BOX( box_ptr ) \
98     ((box_ptr) && !((box_ptr)->manager & LSMASH_NON_EXISTING_BOX))
99 #define LSMASH_IS_NON_EXISTING_BOX( box_ptr ) \
100     (!(box_ptr) || ((box_ptr)->manager & LSMASH_NON_EXISTING_BOX))
101 
102 #define LSMASH_IS_BOX_ADDITION_SUCCESS( box_ptr ) \
103     (!((box_ptr)->manager & LSMASH_NON_EXISTING_BOX))
104 #define LSMASH_IS_BOX_ADDITION_FAILURE( box_ptr ) \
105     (!!((box_ptr)->manager & LSMASH_NON_EXISTING_BOX))
106 
107 /* Use this macro for disabling a predefined child box in struct.
108  * Predefined childs must not be NULL for safety. */
109 #define LSMASH_MAKE_BOX_NON_EXISTING( box_ptr ) \
110     (box_ptr) = (void *)(box_ptr)->nonexist_ptr
111 
112 /* 12-byte ISO reserved value:
113  * 0xXXXXXXXX-0011-0010-8000-00AA00389B71 */
114 static const uint8_t static_lsmash_iso_12_bytes[12]
115     = { 0x00, 0x11, 0x00, 0x10, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
116 #define LSMASH_ISO_12_BYTES static_lsmash_iso_12_bytes
117 
118 /* L-SMASH original 12-byte QuickTime file format value for CODEC discrimination mainly:
119  * 0xXXXXXXXX-0F11-4DA5-BF4E-F2C48C6AA11E */
120 static const uint8_t static_lsmash_qtff_12_bytes[12]
121     = { 0x0F, 0x11, 0x4D, 0xA5, 0xBF, 0x4E, 0xF2, 0xC4, 0x8C, 0x6A, 0xA1, 0x1E };
122 #define LSMASH_QTFF_12_BYTES static_lsmash_qtff_12_bytes
123 
124 struct lsmash_box_tag
125 {
126     ISOM_FULLBOX_COMMON;
127 };
128 
129 /* Unknown Box
130  * This structure is for boxes we don't know or define yet.
131  * This box must be always appended as an extension box. */
132 struct isom_unknown_box_tag
133 {
134     ISOM_BASEBOX_COMMON;
135     uint32_t unknown_size;
136     uint8_t *unknown_field;
137 };
138 
139 /* File Type Box
140  * This box identifies the specifications to which this file complies.
141  * This box shall occur before any variable-length box.
142  * In the absence of this box, the file is QuickTime file format or MP4 version 1 file format.
143  * In MP4 version 1 file format, Object Descriptor Box is mandatory.
144  * In QuickTime file format, Object Descriptor Box isn't defined.
145  * Therefore, if this box and an Object Descriptor Box are absent in the file, the file shall be QuickTime file format. */
146 typedef struct
147 {
148     ISOM_BASEBOX_COMMON;
149     uint32_t major_brand;           /* brand identifier */
150     uint32_t minor_version;         /* the minor version of the major brand */
151     uint32_t *compatible_brands;    /* a list, to the end of the box, of brands */
152 
153         uint32_t brand_count;       /* the number of factors in compatible_brands array */
154 } isom_ftyp_t;
155 
156 /* Color Table Box
157  * This box defines a list of preferred colors for displaying the movie on devices that support only 256 colors.
158  * The list may contain up to 256 colors. This box contains a Macintosh color table data structure.
159  * This box is defined in QuickTime File Format Specification.
160  * The color table structure is also defined in struct ColorTable defined in Quickdraw.h. */
161 typedef struct
162 {
163     /* An array of colors.
164      * Each color is made of four unsigned 16-bit integers. */
165     uint16_t value;     /* index or other value
166                          * Must be set to 0. */
167     /* true color */
168     uint16_t r;         /* magnitude of red component */
169     uint16_t g;         /* magnitude of green component */
170     uint16_t b;         /* magnitude of blue component */
171 } isom_qt_color_array_t;
172 
173 typedef struct
174 {
175     uint32_t seed;          /* unique identifier for table
176                              * Must be set to 0. */
177     uint16_t flags;         /* high bit: 0 = PixMap; 1 = device
178                              * Must be set to 0x8000. */
179     uint16_t size;          /* the number of colors in the following color array
180                              * This is a zero-relative value;
181                              * setting this field to 0 means that there is one color in the array. */
182     isom_qt_color_array_t *array;
183 } isom_qt_color_table_t;
184 
185 typedef struct
186 {
187     ISOM_BASEBOX_COMMON;
188     isom_qt_color_table_t color_table;
189 } isom_ctab_t;
190 
191 /* Track Header Box
192  * This box specifies the characteristics of a single track. */
193 typedef struct
194 {
195     /* version is either 0 or 1
196      * flags
197      *      0x000001: Indicates that the track is enabled.
198      *                A disabled track is treated as if it were not present.
199      *      0x000002: Indicates that the track is used in the presentation.
200      *      0x000004: Indicates that the track is used when previewing the presentation.
201      *      0x000008: Indicates that the track is used in the movie's poster. (only defined in QuickTime file format)
202      * ISOM: If in a presentation all tracks have neither track_in_movie nor track_in_preview set,
203      *       then all tracks shall be treated as if both flags were set on all tracks. */
204     ISOM_FULLBOX_COMMON;
205     /* version == 0: uint64_t -> uint32_t */
206     uint64_t creation_time;         /* the creation time of this track (in seconds since midnight, Jan. 1, 1904, in UTC time) */
207     uint64_t modification_time;     /* the most recent time the track was modified (in seconds since midnight, Jan. 1, 1904, in UTC time) */
208     uint32_t track_ID;              /* an integer that uniquely identifies the track
209                                      * Track IDs are never re-used and cannot be zero. */
210     uint32_t reserved1;
211     uint64_t duration;              /* the duration of this track expressed in the movie timescale units */
212     /* The following fields are treated as
213      * ISOM: template fields.
214      * MP41: reserved fields.
215      * MP42: ignored fileds since compositions are done using BIFS system.
216      * 3GPP: ignored fields except for alternate_group.
217      * QTFF: usable fields. */
218     uint32_t reserved2[2];
219     int16_t  layer;                 /* the front-to-back ordering of video tracks; tracks with lower numbers are closer to the viewer. */
220     int16_t  alternate_group;       /* an integer that specifies a group or collection of tracks
221                                      * If this field is not 0, it should be the same for tracks that contain alternate data for one another
222                                      * and different for tracks belonging to different such groups.
223                                      * Only one track within an alternate group should be played or streamed at any one time. */
224     int16_t  volume;                /* fixed point 8.8 number. 0x0100 is full volume. */
225     uint16_t reserved3;
226     int32_t  matrix[9];             /* transformation matrix for the video */
227     /* track's visual presentation size
228      * All images in the sequence are scaled to this size, before any overall transformation of the track represented by the matrix.
229      * Note: these fields are treated as reserved in MP4 version 1. */
230     uint32_t width;                 /* fixed point 16.16 number */
231     uint32_t height;                /* fixed point 16.16 number */
232     /* */
233 } isom_tkhd_t;
234 
235 /* Track Clean Aperture Dimensions Box
236  * A presentation mode where clap and pasp are reflected. */
237 typedef struct
238 {
239     ISOM_FULLBOX_COMMON;
240     uint32_t width;     /* fixed point 16.16 number */
241     uint32_t height;    /* fixed point 16.16 number */
242 } isom_clef_t;
243 
244 /* Track Production Aperture Dimensions Box
245  * A presentation mode where pasp is reflected. */
246 typedef struct
247 {
248     ISOM_FULLBOX_COMMON;
249     uint32_t width;     /* fixed point 16.16 number */
250     uint32_t height;    /* fixed point 16.16 number */
251 } isom_prof_t;
252 
253 /* Track Encoded Pixels Dimensions Box
254  * A presentation mode where clap and pasp are not reflected. */
255 typedef struct
256 {
257     ISOM_FULLBOX_COMMON;
258     uint32_t width;     /* fixed point 16.16 number */
259     uint32_t height;    /* fixed point 16.16 number */
260 } isom_enof_t;
261 
262 /* Track Aperture Mode Dimensions Box */
263 typedef struct
264 {
265     ISOM_BASEBOX_COMMON;
266     isom_clef_t *clef;      /* Track Clean Aperture Dimensions Box */
267     isom_prof_t *prof;      /* Track Production Aperture Dimensions Box */
268     isom_enof_t *enof;      /* Track Encoded Pixels Dimensions Box */
269 } isom_tapt_t;
270 
271 /* Edit List Box
272  * This box contains an explicit timeline map.
273  * Each entry defines part of the track timeline: by mapping part of the media timeline, or by indicating 'empty' time,
274  * or by defining a 'dwell', where a single time-point in the media is held for a period.
275  * The last edit in a track shall never be an empty edit.
276  * Any difference between the duration in the Movie Header Box, and the track's duration is expressed as an implicit empty edit at the end.
277  * It is recommended that any edits, explicit or implied, not select any portion of the composition timeline that doesn't map to a sample.
278  * Therefore, if the first sample in the track has non-zero CTS, then this track should have at least one edit and the start time in it should
279  * correspond to the value of the CTS the first sample has or more not to exceed the largest CTS in this track. */
280 typedef struct
281 {
282     /* This entry is called Timeline Mapping Edit (TME) entry in UltraViolet Common File Format.
283      * version == 0: 64bits -> 32bits */
284     uint64_t segment_duration;  /* the duration of this edit expressed in the movie timescale units */
285     int64_t  media_time;        /* the starting composition time within the media of this edit segment
286                                  * If this field is set to -1, it is an empty edit. */
287     int32_t  media_rate;        /* the relative rate at which to play the media corresponding to this edit segment
288                                  * If this value is 0, then the edit is specifying a 'dwell':
289                                  * the media at media_time is presented for the segment_duration.
290                                  * This field is expressed as 16.16 fixed-point number. */
291 } isom_elst_entry_t;
292 
293 typedef struct
294 {
295     ISOM_FULLBOX_COMMON;        /* version is either 0 or 1 */
296     lsmash_entry_list_t *list;
297 } isom_elst_t;
298 
299 /* Edit Box
300  * This optional box maps the presentation time-line to the media time-line as it is stored in the file.
301  * In the absence of this box, there is an implicit one-to-one mapping of these time-lines,
302  * and the presentation of a track starts at the beginning of the presentation. */
303 typedef struct
304 {
305     ISOM_BASEBOX_COMMON;
306     isom_elst_t *elst;     /* Edit List Box */
307 } isom_edts_t;
308 
309 /* Track Reference Box
310  * The Track Reference Box contains Track Reference Type Boxes.
311  * Track Reference Type Boxes define relationships between tracks.
312  * They allow one track to specify how it is related to other tracks. */
313 typedef struct
314 {
315     ISOM_BASEBOX_COMMON;
316     uint32_t *track_ID;         /* track_IDs of reference tracks / Zero value must not be used */
317 
318         uint32_t ref_count;     /* number of reference tracks */
319 } isom_tref_type_t;
320 
321 typedef struct
322 {
323     ISOM_BASEBOX_COMMON;
324     lsmash_entry_list_t ref_list;   /* Track Reference Type Boxes */
325 } isom_tref_t;
326 
327 /* Media Header Box
328  * This box declares overall information that is media-independent, and relevant to characteristics of the media in a track.*/
329 struct isom_mdhd_tag
330 {
331     ISOM_FULLBOX_COMMON;            /* version is either 0 or 1 */
332     /* version == 0: uint64_t -> uint32_t */
333     uint64_t creation_time;         /* the creation time of the media in this track (in seconds since midnight, Jan. 1, 1904, in UTC time) */
334     uint64_t modification_time;     /* the most recent time the media in this track was modified (in seconds since midnight, Jan. 1, 1904, in UTC time) */
335     uint32_t timescale;             /* media timescale: timescale for this media */
336     uint64_t duration;              /* the duration of this media expressed in the timescale indicated in this box */
337     /* */
338     uint16_t language;              /* ISOM: ISO-639-2/T language codes. Most significant 1-bit is 0.
339                                      *       Each character is packed as the difference between its ASCII value and 0x60.
340                                      * QTFF: Macintosh language codes is usually used.
341                                      *       Mac's value is less than 0x800 while ISO's value is 0x800 or greater. */
342     int16_t quality;                /* ISOM: pre_defined / QTFF: the media's playback quality */
343 };
344 
345 /* Handler Reference Box
346  * In Media Box, this box is mandatory and (ISOM: should/QTFF: must) come before Media Information Box.
347  * ISOM: this box might be also in Meta Box.
348  * QTFF: this box might be also in Media Information Box. If this box is present there, it must come before Data Information Box. */
349 typedef struct
350 {
351     ISOM_FULLBOX_COMMON;
352     uint32_t componentType;             /* ISOM: pre_difined = 0
353                                          * QTFF: 'mhlr' for Media Handler Reference Box and 'dhlr' for Data Handler Reference Box  */
354     uint32_t componentSubtype;          /* Both ISOM and QT: when present in Media Handler Reference Box, this field defines the type of media data.
355                                          * ISOM: when present in Metadata Handler Reference Box, this field defines the format of the meta box contents.
356                                          * QTFF: when present in Data Handler Reference Box, this field defines the data reference type. */
357     /* The following fields are defined in QTFF however these fields aren't mentioned in QuickTime SDK and are reserved in the specification.
358      * In ISOM, these fields are still defined as reserved. */
359     uint32_t componentManufacturer;     /* vendor indentification / A value of 0 matches any manufacturer. */
360     uint32_t componentFlags;            /* flags describing required component capabilities
361                                          * The high-order 8 bits should be set to 0.
362                                          * The low-order 24 bits are specific to each component type. */
363     uint32_t componentFlagsMask;        /* This field indicates which flags in the componentFlags field are relevant to this operation. */
364     /* */
365     uint8_t *componentName;             /* ISOM: a null-terminated string in UTF-8 characters
366                                          * QTFF: Pascal string */
367 
368         uint32_t componentName_length;
369 } isom_hdlr_t;
370 
371 
372 /** Media Information Header Boxes
373  ** There is a different media information header for each track type
374  ** (corresponding to the media handler-type); the matching header shall be present. **/
375 /* Video Media Header Box
376  * This box contains general presentation information, independent of the coding, for video media. */
377 typedef struct
378 {
379     ISOM_FULLBOX_COMMON;        /* flags is 1 */
380     uint16_t graphicsmode;      /* template: graphicsmode = 0 */
381     uint16_t opcolor[3];        /* template: opcolor = { 0, 0, 0 } */
382 } isom_vmhd_t;
383 
384 /* Sound Media Header Box
385  * This box contains general presentation information, independent of the coding, for audio media. */
386 typedef struct
387 {
388     ISOM_FULLBOX_COMMON;
389     int16_t balance;        /* a fixed-point 8.8 number that places mono audio tracks in a stereo space. template: balance = 0 */
390     uint16_t reserved;
391 } isom_smhd_t;
392 
393 /* Hint Media Header Box
394  * This box contains general information, independent of the protocol, for hint tracks. (A PDU is a Protocol Data Unit.) */
395 typedef struct
396 {
397     ISOM_FULLBOX_COMMON;
398     uint16_t maxPDUsize;        /* the size in bytes of the largest PDU in this (hint) stream */
399     uint16_t avgPDUsize;        /* the average size of a PDU over the entire presentation */
400     uint32_t maxbitrate;        /* the maximum rate in bits/second over any window of one second */
401     uint32_t avgbitrate;        /* the average rate in bits/second over the entire presentation */
402     uint32_t reserved;
403 } isom_hmhd_t;
404 
405 /* Null Media Header Box
406  * This box may be used for streams other than visual and audio (e.g., timed metadata streams). */
407 typedef struct
408 {
409     /* Streams other than visual and audio may use a Null Media Header Box */
410     ISOM_FULLBOX_COMMON;    /* flags is currently all zero */
411 } isom_nmhd_t;
412 
413 /* Generic Media Information Box */
414 typedef struct
415 {
416     ISOM_FULLBOX_COMMON;
417     uint16_t graphicsmode;
418     uint16_t opcolor[3];
419     int16_t balance;        /* This field is nomally set to 0. */
420     uint16_t reserved;      /* Reserved for use by Apple. Set this field to 0. */
421 } isom_gmin_t;
422 
423 /* Text Media Information Box */
424 typedef struct
425 {
426     ISOM_BASEBOX_COMMON;
427     int32_t matrix[9];      /* Unkown fields. Default values are probably:
428                              * { 0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000 } */
429 } isom_text_t;
430 
431 /* Generic Media Information Header Box */
432 typedef struct
433 {
434     ISOM_BASEBOX_COMMON;
435     isom_gmin_t *gmin;      /* Generic Media Information Box */
436     isom_text_t *text;      /* Text Media Information Box */
437 } isom_gmhd_t;
438 /** **/
439 
440 /* Data Reference Box
441  * name and location fields are expressed in null-terminated string using UTF-8 characters. */
442 typedef struct
443 {
444     /* This box is DataEntryUrlBox or DataEntryUrnBox */
445     ISOM_FULLBOX_COMMON;    /* flags == 0x000001 means that the media data is in the same file
446                              * as the Movie Box containing this data reference. */
447     char *name;             /* only for DataEntryUrnBox */
448     char *location;         /* a location to find the resource with the given name */
449 
450         uint32_t       name_length;
451         uint32_t       location_length;
452         lsmash_file_t *ref_file;    /* pointer to the handle of the referenced file */
453 } isom_dref_entry_t;
454 
455 typedef struct
456 {
457     ISOM_FULLBOX_COMMON;
458     lsmash_entry_list_t list;
459 } isom_dref_t;
460 
461 /* Data Information Box */
462 typedef struct
463 {
464     /* This box is in Media Information Box or Meta Box */
465     ISOM_BASEBOX_COMMON;
466     isom_dref_t *dref;      /* Data Reference Box */
467 } isom_dinf_t;
468 
469 /** Sample Description **/
470 /* ES Descriptor Box */
471 struct mp4sys_ES_Descriptor_t; /* FIXME: I think these structs using mp4sys should be placed in isom.c */
472 typedef struct
473 {
474     ISOM_FULLBOX_COMMON;
475     struct mp4sys_ES_Descriptor_t *ES;
476 } isom_esds_t;
477 
478 /* MPEG-4 Bit Rate Box
479  * This box signals the bit rate information of the AVC video stream. */
480 typedef struct
481 {
482     ISOM_BASEBOX_COMMON;
483     uint32_t bufferSizeDB;  /* the size of the decoding buffer for the elementary stream in bytes */
484     uint32_t maxBitrate;    /* the maximum rate in bits/second over any window of one second */
485     uint32_t avgBitrate;    /* the average rate in bits/second over the entire presentation */
486 } isom_btrt_t;
487 
488 /* Global Header Box */
489 typedef struct
490 {
491     ISOM_BASEBOX_COMMON;
492     uint32_t header_size;
493     uint8_t *header_data;
494 } isom_glbl_t;
495 
496 /* Clean Aperture Box
497  * There are notionally four values in this box and these parameters are represented as a fraction N/D.
498  * Here, we refer to the pair of parameters fooN and fooD as foo.
499  * Considering the pixel dimensions as defined by the VisualSampleEntry width and height.
500  * If picture centre of the image is at pcX and pcY, then horizOff and vertOff are defined as follows:
501  *  pcX = horizOff + (width - 1)/2;
502  *  pcY = vertOff + (height - 1)/2;
503  * The leftmost/rightmost pixel and the topmost/bottommost line of the clean aperture fall at:
504  *  pcX +/- (cleanApertureWidth - 1)/2;
505  *  pcY +/- (cleanApertureHeight - 1)/2;
506  * QTFF: this box is a mandatory extension for all uncompressed Y'CbCr data formats. */
507 typedef struct
508 {
509     ISOM_BASEBOX_COMMON;
510     uint32_t cleanApertureWidthN;
511     uint32_t cleanApertureWidthD;
512     uint32_t cleanApertureHeightN;
513     uint32_t cleanApertureHeightD;
514     int32_t  horizOffN;
515     uint32_t horizOffD;
516     int32_t  vertOffN;
517     uint32_t vertOffD;
518 } isom_clap_t;
519 
520 /* Pixel Aspect Ratio Box
521  * This box specifies the aspect ratio of a pixel, in arbitrary units.
522  * If a pixel appears H wide and V tall, then hSpacing/vSpacing is equal to H/V.
523  * When adjusting pixel aspect ratio, normally, the horizontal dimension of the video is scaled, if needed. */
524 typedef struct
525 {
526     ISOM_BASEBOX_COMMON;
527     uint32_t hSpacing;      /* horizontal spacing */
528     uint32_t vSpacing;      /* vertical spacing */
529 } isom_pasp_t;
530 
531 /* ISOM: Colour Information Box / QTFF: Color Parameter Box
532  * This box is used to map the numerical values of pixels in the file to a common representation of color
533  * in which images can be correctly compared, combined, and displayed.
534  * If colour information is supplied in both this box, and also in the video bitstream,
535  * this box takes precedence, and over-rides the information in the bitstream.
536  * For QuickTime file format:
537  *   This box ('colr') supersedes the Gamma Level Box ('gama').
538  *   Writers of QTFF should never write both into an Image Description, and readers of QTFF should ignore 'gama' if 'colr' is present.
539  *   Note: this box is a mandatory extension for all uncompressed Y'CbCr data formats.
540  * For ISO Base Media file format:
541  *   Colour information may be supplied in one or more Colour Information Boxes placed in a VisualSampleEntry.
542  *   These should be placed in order in the sample entry starting with the most accurate (and potentially the most difficult to process), in progression to the least.
543  *   These are advisory and concern rendering and colour conversion, and there is no normative behaviour associated with them; a reader may choose to use the most suitable. */
544 typedef struct
545 {
546     ISOM_BASEBOX_COMMON;
547     uint32_t color_parameter_type;          /* QTFF: 'nclc' or 'prof'
548                                              * ISOM: 'nclx', 'rICC' or 'prof' */
549     /* for 'nclc' and 'nclx' */
550     uint16_t primaries_index;               /* CIE 1931 xy chromaticity coordinates */
551     uint16_t transfer_function_index;       /* nonlinear transfer function from RGB to ErEgEb */
552     uint16_t matrix_index;                  /* matrix from ErEgEb to EyEcbEcr */
553     /* for 'nclx' */
554     unsigned full_range_flag : 1;
555     unsigned reserved        : 7;
556 } isom_colr_t;
557 
558 /* Gamma Level Box
559  * This box is used to indicate that the decompressor corrects gamma level at display time.
560  * This box is defined in QuickTime File Format Specification and ImageCompression.h. */
561 typedef struct
562 {
563     ISOM_BASEBOX_COMMON;
564     uint32_t level;     /* A fixed-point 16.16 number indicating the gamma level at which the image was captured.
565                          * Zero value indicates platform's standard gamma. */
566 } isom_gama_t;
567 
568 /* Field/Frame Information Box
569  * This box is used by applications to modify decompressed image data or by decompressor components to determine field display order.
570  * This box is defined in QuickTime File Format Specification, dispatch019 and ImageCodec.h.
571  * Note: this box is a mandatory extension for all uncompressed Y'CbCr data formats. */
572 typedef struct
573 {
574     ISOM_BASEBOX_COMMON;
575     uint8_t fields;     /* the number of fields per frame
576                          * 1: progressive scan
577                          * 2: 2:1 interlaced */
578     uint8_t detail;     /* field ordering */
579 } isom_fiel_t;
580 
581 /* Colorspace Box
582  * This box is defined in ImageCompression.h. */
583 typedef struct
584 {
585     ISOM_BASEBOX_COMMON;
586     uint32_t pixel_format;      /* the native pixel format of an image */
587 } isom_cspc_t;
588 
589 /* Significant Bits Box
590  * This box is defined in Letters from the Ice Floe dispatch019.
591  * Note: this box is a mandatory extension for 'v216' (Uncompressed Y'CbCr, 10, 12, 14, or 16-bit-per-component 4:2:2). */
592 typedef struct
593 {
594     ISOM_BASEBOX_COMMON;
595     uint8_t significantBits;    /* the number of significant bits per component */
596 } isom_sgbt_t;
597 
598 /* Sample Scale Box
599  * If this box is present and can be interpreted by the decoder,
600  * all samples shall be displayed according to the scaling behaviour that is specified in this box.
601  * Otherwise, all samples are scaled to the size that is indicated by the width and height field in the Track Header Box.
602  * This box is defined in ISO Base Media file format. */
603 typedef struct
604 {
605     ISOM_FULLBOX_COMMON;
606     uint8_t constraint_flag;    /* Upper 7-bits are reserved.
607                                  * If this flag is set, all samples described by this sample entry shall be scaled
608                                  * according to the method specified by the field 'scale_method'. */
609     uint8_t scale_method;       /* The semantics of the values for scale_method are as specified for the 'fit' attribute of regions in SMIL 1.0. */
610     int16_t display_center_x;
611     int16_t display_center_y;
612 } isom_stsl_t;
613 
614 /* Sample Entry */
615 #define ISOM_SAMPLE_ENTRY           \
616     ISOM_BASEBOX_COMMON;            \
617     uint8_t reserved[6];            \
618     uint16_t data_reference_index
619 
620 typedef struct
621 {
622     ISOM_SAMPLE_ENTRY;
623 } isom_sample_entry_t;
624 
625 /* Mpeg Sample Entry */
626 typedef struct
627 {
628     ISOM_SAMPLE_ENTRY;
629 } isom_mp4s_entry_t;
630 
631 /* ISOM: Visual Sample Entry / QTFF: Image Description
632  * For maximum compatibility, the following extension boxes should follow, not precede,
633  * any extension boxes defined in or required by derived specifications.
634  *   Clean Aperture Box
635  *   Pixel Aspect Ratio Box */
636 typedef struct
637 {
638     ISOM_SAMPLE_ENTRY;
639     int16_t  version;           /* ISOM: pre_defined / QTFF: sample description version */
640     int16_t  revision_level;    /* ISOM: reserved / QTFF: version of the CODEC */
641     int32_t  vendor;            /* ISOM: pre_defined / QTFF: whose CODEC */
642     uint32_t temporalQuality;   /* ISOM: pre_defined / QTFF: the temporal quality factor */
643     uint32_t spatialQuality;    /* ISOM: pre_defined / QTFF: the spatial quality factor */
644     /* The width and height are the maximum pixel counts that the codec will deliver.
645      * Since these are counts they do not take into account pixel aspect ratio. */
646     uint16_t width;
647     uint16_t height;
648     /* */
649     uint32_t horizresolution;   /* 16.16 fixed-point / template: horizresolution = 0x00480000 / 72 dpi */
650     uint32_t vertresolution;    /* 16.16 fixed-point / template: vertresolution = 0x00480000 / 72 dpi */
651     uint32_t dataSize;          /* ISOM: reserved / QTFF: if known, the size of data for this descriptor */
652     uint16_t frame_count;       /* frame per sample / template: frame_count = 1 */
653     char compressorname[33];    /* a fixed 32-byte field, with the first byte set to the number of bytes to be displayed */
654     uint16_t depth;             /* ISOM: template: depth = 0x0018
655                                  * AVC : 0x0018: colour with no alpha
656                                  *       0x0028: grayscale with no alpha
657                                  *       0x0020: gray or colour with alpha
658                                  * QTFF: depth of this data (1-32) or (33-40 grayscale) */
659     int16_t color_table_ID;     /* ISOM: template: pre_defined = -1
660                                  * QTFF: color table ID
661                                  *       If this field is set to -1, the default color table should be used for the specified depth
662                                  *       If the color table ID is set to 0, a color table is contained within the sample description itself.
663                                  *       The color table immediately follows the color table ID field. */
664     /* Color table follows color_table_ID only when color_table_ID is set to 0. */
665     isom_qt_color_table_t color_table;  /* a list of preferred colors for displaying the movie on devices that support only 256 colors */
666 } isom_visual_entry_t;
667 
668 /* Format Box
669  * This box shows the data format of the stored sound media.
670  * ISO base media file format also defines the same four-character-code for the type field,
671  * however, that is used to indicate original sample description of the media when a protected sample entry is used. */
672 typedef struct
673 {
674     ISOM_BASEBOX_COMMON;
675     uint32_t data_format;       /* copy of sample description type */
676 } isom_frma_t;
677 
678 /* Audio Endian Box */
679 typedef struct
680 {
681     ISOM_BASEBOX_COMMON;
682     int16_t littleEndian;
683 } isom_enda_t;
684 
685 /* MPEG-4 Audio Box */
686 typedef struct
687 {
688     ISOM_BASEBOX_COMMON;
689     uint32_t unknown;           /* always 0? */
690 } isom_mp4a_t;
691 
692 /* Terminator Box
693  * This box is present to indicate the end of the sound description. It contains no data. */
694 typedef struct
695 {
696     ISOM_BASEBOX_COMMON;    /* size = 8, type = 0x00000000 */
697 } isom_terminator_t;
698 
699 /* Sound Information Decompression Parameters Box
700  * This box is defined in QuickTime file format.
701  * This box provides the ability to store data specific to a given audio decompressor in the sound description.
702  * The contents of this box are dependent on the audio decompressor. */
703 typedef struct
704 {
705     ISOM_BASEBOX_COMMON;
706     isom_frma_t       *frma;            /* Format Box */
707     isom_enda_t       *enda;            /* Audio Endian Box */
708     isom_mp4a_t       *mp4a;            /* MPEG-4 Audio Box */
709     isom_terminator_t *terminator;      /* Terminator Box */
710 } isom_wave_t;
711 
712 /* Audio Channel Layout Box
713  * This box is defined in QuickTime file format or Apple Lossless Audio inside ISO Base Media. */
714 typedef struct
715 {
716     uint32_t channelLabel;          /* the channelLabel that describes the channel */
717     uint32_t channelFlags;          /* flags that control the interpretation of coordinates */
718     uint32_t coordinates[3];        /* an ordered triple that specifies a precise speaker location / 32-bit floating point */
719 } isom_channel_description_t;
720 
721 typedef struct
722 {
723     ISOM_FULLBOX_COMMON;
724     uint32_t channelLayoutTag;              /* the channelLayoutTag indicates the layout */
725     uint32_t channelBitmap;                 /* If channelLayoutTag is set to 0x00010000, this field is the channel usage bitmap. */
726     uint32_t numberChannelDescriptions;     /* the number of items in the Channel Descriptions array */
727     /* Channel Descriptions array */
728     isom_channel_description_t *channelDescriptions;
729 } isom_chan_t;
730 
731 /* Sampling Rate Box
732  * This box may be present only in an AudioSampleEntryV1, and when present,
733  * it overrides the samplerate field and documents the actual sampling rate.
734  * When this box is present, the media timescale should be the same as the
735  * sampling rate, or an integer division or multiple of it. */
736 typedef struct
737 {
738     ISOM_FULLBOX_COMMON;
739     uint32_t sampling_rate;     /* the actual sampling rate of the audio media, expressed as a 32-bit integer
740                                  * The value of this field overrides the samplerate field in the AudioSampleEntryV1
741                                  * and documents the actual sampling rate. */
742 } isom_srat_t;
743 
744 /* ISOM: Audio Sample Entry / QTFF: Sound Description */
745 typedef struct
746 {
747     ISOM_SAMPLE_ENTRY;
748     int16_t  version;           /* ISOM: version = 0 is used to support non-high samplerate audio format.
749                                  *       version = 1, called AudioSampleEntryV1, is used to support high samplerate audio format.
750                                  *       An AudioSampleEntryV1 requires that the enclosing Sample Description Box also takes the version 1.
751                                  *       For maximum compatibility, an AudioSampleEntryV1 should only be used when needed.
752                                  * QTFF: version = 0 supports only 'raw ' or 'twos' audio format.
753                                  *       version = 1 is used to support out-of-band configuration settings for decompression.
754                                  *       version = 2 is used to support high samplerate, or 3 or more multichannel audio format. */
755     int16_t  revision_level;    /* ISOM: reserved / QTFF: version of the CODEC */
756     int32_t  vendor;            /* ISOM: reserved / QTFF: whose CODEC */
757     uint16_t channelcount;      /* ISOM: template: channelcount = 2
758                                  *       channelcount is a value greater than zero that indicates the maximum number of channels that the
759                                  *       audio could deliver.
760                                  *       A channelcount of 1 indicates mono audio, and 2 indicates stereo (left/right).
761                                  *       When values greater than 2 are used, the codec configuration should identify the channel assignment.
762                                  * QTFF: the number of audio channels
763                                  *       Allowable values are 1 (mono) or 2 (stereo).
764                                  *       For more than 2, set this field to 3 and use numAudioChannels instead of this field. */
765     uint16_t samplesize;        /* ISOM: template: samplesize = 16
766                                  * QTFF: the number of bits in each uncompressed sample for a single channel
767                                  *       Allowable values are 8 or 16.
768                                  *       For non-mod8, set this field to 16 and use constBitsPerChannel instead of this field.
769                                  *       For more than 16, set this field to 16 and use bytesPerPacket instead of this field. */
770     int16_t  compression_ID;    /* ISOM: pre_defined
771                                  * QTFF: version = 0 -> must be set to 0.
772                                  *       version = 2 -> must be set to -2. */
773     uint16_t packet_size;       /* ISOM: reserved / QTFF: must be set to 0. */
774     uint32_t samplerate;        /* the sampling rate expressed as a 16.16 fixed-point number
775                                  * ISOM: template: samplerate = {default samplerate of media}<<16
776                                  *       When it is desired to indicate an audio sampling rate greater than the value that can be represented in
777                                  *       this field, this field should contain a value left-shifted 16 bits that matches the media timescale,
778                                  *       or be an integer division or multiple of it.
779                                  * QTFF: the integer portion should match the media's timescale.
780                                  *       If this field is invalid because of higher samplerate,
781                                  *       then set this field to 0x00010000 and use audioSampleRate instead of this field. */
782     /* QTFF-based version 1 fields
783      * These fields are for description of the compression ratio of fixed ratio audio compression algorithms.
784      * If these fields are not used, they are set to 0. */
785     uint32_t samplesPerPacket;      /* For compressed audio, be set to the number of uncompressed frames generated by a compressed frame.
786                                      * For uncompressed audio, shall be set to 1. */
787     uint32_t bytesPerPacket;        /* the number of bytes in a sample for a single channel */
788     uint32_t bytesPerFrame;         /* the number of bytes in a frame */
789     uint32_t bytesPerSample;        /* 8-bit audio: 1, other audio: 2 */
790     /* QTFF-based version 2 fields
791      * LPCMFrame: one sample from each channel.
792      * AudioPacket: For uncompressed audio, an AudioPacket is simply one LPCMFrame.
793      *              For compressed audio, an AudioPacket is the natural compressed access unit of that format. */
794     uint32_t sizeOfStructOnly;                  /* offset to extensions */
795     uint64_t audioSampleRate;                   /* 64-bit floating point */
796     uint32_t numAudioChannels;                  /* any channel assignment info will be in Audio Channel Layout Box. */
797     int32_t  always7F000000;                    /* always 0x7F000000 */
798     uint32_t constBitsPerChannel;               /* only set if constant (and only for uncompressed audio) */
799     uint32_t formatSpecificFlags;
800     uint32_t constBytesPerAudioPacket;          /* only set if constant */
801     uint32_t constLPCMFramesPerAudioPacket;     /* only set if constant */
802 } isom_audio_entry_t;
803 
804 /* Hint Sample Entry */
805 #define ISOM_HINT_SAMPLE_ENTRY \
806     ISOM_SAMPLE_ENTRY; \
807     uint8_t *data
808 
809 typedef struct
810 {
811     ISOM_HINT_SAMPLE_ENTRY;
812     uint32_t data_length;
813 } isom_hint_entry_t;
814 
815 /* Metadata Sample Entry */
816 #define ISOM_METADATA_SAMPLE_ENTRY \
817     ISOM_SAMPLE_ENTRY
818 
819 typedef struct
820 {
821     ISOM_METADATA_SAMPLE_ENTRY;
822 } isom_metadata_entry_t;
823 
824 /* QuickTime Text Sample Description */
825 typedef struct
826 {
827     ISOM_SAMPLE_ENTRY;
828     int32_t displayFlags;
829     int32_t textJustification;
830     uint16_t bgColor[3];            /* background RGB color */
831     /* defaultTextBox */
832     int16_t top;
833     int16_t left;
834     int16_t bottom;
835     int16_t right;
836     /* defaultStyle */
837     int32_t scrpStartChar;          /* starting character position */
838     int16_t scrpHeight;
839     int16_t scrpAscent;
840     int16_t scrpFont;
841     uint16_t scrpFace;              /* only first 8-bits are used */
842     int16_t scrpSize;
843     uint16_t scrpColor[3];          /* foreground RGB color */
844     /* defaultFontName is Pascal string */
845     uint8_t font_name_length;
846     char *font_name;
847 } isom_qt_text_entry_t;
848 
849 /* FontRecord */
850 typedef struct
851 {
852     uint16_t font_ID;
853     /* Pascal string */
854     uint8_t font_name_length;
855     char   *font_name;
856 } isom_font_record_t;
857 
858 /* Font Table Box */
859 typedef struct
860 {
861     ISOM_BASEBOX_COMMON;
862     /* FontRecord
863      * entry_count is uint16_t. */
864     lsmash_entry_list_t *list;
865 } isom_ftab_t;
866 
867 /* 3GPP Timed Text Sample Entry */
868 typedef struct
869 {
870     ISOM_SAMPLE_ENTRY;
871     uint32_t displayFlags;
872     int8_t horizontal_justification;
873     int8_t vertical_justification;
874     uint8_t background_color_rgba[4];
875     /* BoxRecord default_text_box */
876     int16_t top;
877     int16_t left;
878     int16_t bottom;
879     int16_t right;
880     /* StyleRecord default_style */
881     uint16_t startChar;     /* always 0 */
882     uint16_t endChar;       /* always 0 */
883     uint16_t font_ID;
884     uint8_t face_style_flags;
885     uint8_t font_size;
886     uint8_t text_color_rgba[4];
887     /* Font Table Box font_table */
888     isom_ftab_t *ftab;
889 } isom_tx3g_entry_t;
890 
891 /* Sample Description Box */
892 typedef struct
893 {
894     ISOM_FULLBOX_COMMON;
895     uint32_t entry_count;   /* print only */
896     lsmash_entry_list_t list;
897 } isom_stsd_t;
898 /** **/
899 
900 /* Decoding Time to Sample Box
901  * This box contains a compact version of a table that allows indexing from decoding time to sample number.
902  * Each entry in the table gives the number of consecutive samples with the same time delta, and the delta of those samples.
903  * By adding the deltas a complete time-to-sample map may be built.
904  * All samples must have non-zero durations except for the last one.
905  * The sum of all deltas gives the media duration in the track (not mapped to the movie timescale, and not considering any edit list).
906  * DTS is an abbreviation of 'decoding time stamp'. */
907 typedef struct
908 {
909     uint32_t sample_count;      /* number of consecutive samples that have the given sample_delta */
910     uint32_t sample_delta;      /* DTS[0] = 0; DTS[n+1] = DTS[n] + sample_delta[n]; */
911 } isom_stts_entry_t;
912 
913 typedef struct
914 {
915     ISOM_FULLBOX_COMMON;
916     lsmash_entry_list_t *list;
917 } isom_stts_t;
918 
919 /* Composition Time to Sample Box
920  * This box provides the offset between decoding time and composition time.
921  * CTS is an abbreviation of 'composition time stamp'.
922  * This box is optional and must only be present if DTS and CTS differ for any samples. */
923 typedef struct
924 {
925 #define ISOM_NON_OUTPUT_SAMPLE_OFFSET 0x80000000
926     uint32_t sample_count;      /* number of consecutive samples that have the given sample_offset */
927     uint32_t sample_offset;     /* CTS[n] = DTS[n] + sample_offset[n];
928                                  * ISOM: if version is set to 1, sample_offset is signed 32-bit integer.
929                                  * QTFF: sample_offset is always signed 32-bit integer. */
930 } isom_ctts_entry_t;
931 
932 typedef struct
933 {
934     ISOM_FULLBOX_COMMON;
935     lsmash_entry_list_t *list;
936 } isom_ctts_t;
937 
938 /* Composition to Decode Box (Composition Shift Least Greatest Box)
939  * This box may be used to relate the composition and decoding timelines,
940  * and deal with some of the ambiguities that signed composition offsets introduce. */
941 typedef struct
942 {
943     ISOM_FULLBOX_COMMON;
944     int32_t compositionToDTSShift;          /* If this value is added to the composition times (as calculated by the CTS offsets from the DTS),
945                                              * then for all samples, their CTS is guaranteed to be greater than or equal to their DTS,
946                                              * and the buffer model implied by the indicated profile/level will be honoured;
947                                              * if leastDecodeToDisplayDelta is positive or zero, this field can be 0;
948                                              * otherwise it should be at least (- leastDecodeToDisplayDelta). */
949     int32_t leastDecodeToDisplayDelta;      /* the smallest sample_offset in this track */
950     int32_t greatestDecodeToDisplayDelta;   /* the largest sample_offset in this track */
951     int32_t compositionStartTime;           /* the smallest CTS for any sample */
952     int32_t compositionEndTime;             /* the CTS plus the composition duration, of the sample with the largest CTS in this track */
953 } isom_cslg_t;
954 
955 /* Sample Size Box / Compact Sample Size Box
956  * This box contains the sample count and a table giving the size in bytes of each sample.
957  * The total number of samples in the media within the initial movie is always indicated in the sample_count.
958  * Note: a sample size of zero is not prohibited in general, but it must be valid and defined for the coding system,
959  *       as defined by the sample entry, that the sample belongs to. */
960 typedef struct
961 {
962     uint32_t entry_size;        /* the size of a sample */
963 } isom_stsz_entry_t;
964 
965 typedef struct
966 {
967     ISOM_FULLBOX_COMMON;
968     uint32_t sample_size;           /* the default sample size
969                                      * If this field is set to 0, then the samples have different sizes. */
970     uint32_t sample_count;          /* the number of samples in the media within the initial movie */
971     lsmash_entry_list_t *list;      /* available if sample_size == 0 */
972 } isom_stsz_t;
973 
974 typedef struct
975 {
976     ISOM_FULLBOX_COMMON;
977     unsigned int reserved   : 24;   /* 0 */
978     unsigned int field_size : 8;    /* the size in bits of the entries in the following table
979                                      * It shall take the value 4, 8 or 16. If the value 4 is used, then each byte contains two values
980                                      * entry[i]<<4 + entry[i+1]; if the sizes do not fill an integral number of bytes, the last byte is
981                                      * padded with zero. */
982     uint32_t     sample_count;      /* the number of entries in the following table */
983     lsmash_entry_list_t *list;      /* L-SMASH uses isom_stsz_entry_t for its internal processes. */
984 } isom_stz2_t;
985 
986 /* Sync Sample Box
987  * If this box is not present, every sample is a random access point.
988  * In AVC streams, this box cannot point non-IDR samples.
989  * The table is arranged in strictly increasing order of sample number. */
990 typedef struct
991 {
992     uint32_t sample_number;     /* the numbers of the samples that are random access points in the stream. */
993 } isom_stss_entry_t;
994 
995 typedef struct
996 {
997     ISOM_FULLBOX_COMMON;
998     lsmash_entry_list_t *list;
999 } isom_stss_t;
1000 
1001 /* Partial Sync Sample Box
1002  * Tip from QT engineering - Open-GOP intra frames need to be marked as "partial sync samples".
1003  * Partial sync frames perform a partial reset of inter-frame dependencies;
1004  * decoding two partial sync frames and the non-droppable difference frames between them is
1005  * sufficient to prepare a decompressor for correctly decoding the difference frames that follow. */
1006 typedef struct
1007 {
1008     uint32_t sample_number;     /* the numbers of the samples that are partial sync samples in the stream. */
1009 } isom_stps_entry_t;
1010 
1011 typedef struct
1012 {
1013     ISOM_FULLBOX_COMMON;
1014     lsmash_entry_list_t *list;
1015 } isom_stps_t;
1016 
1017 /* Independent and Disposable Samples Box */
1018 typedef struct
1019 {
1020     unsigned is_leading            : 2;     /* ISOM: leading / QTFF: samples later in decode order may have earlier display times */
1021     unsigned sample_depends_on     : 2;     /* independency */
1022     unsigned sample_is_depended_on : 2;     /* disposable */
1023     unsigned sample_has_redundancy : 2;     /* redundancy */
1024 } isom_sdtp_entry_t;
1025 
1026 typedef struct
1027 {
1028     ISOM_FULLBOX_COMMON;
1029     /* According to the specification, the size of the table, sample_count, doesn't exist in this box.
1030      * Instead of this, it is taken from the sample_count in the stsz or the stz2 box. */
1031     lsmash_entry_list_t *list;
1032 } isom_sdtp_t;
1033 
1034 /* Sample To Chunk Box
1035  * This box can be used to find the chunk that contains a sample, its position, and the associated sample description.
1036  * The table is compactly coded. Each entry gives the index of the first chunk of a run of chunks with the same characteristics.
1037  * By subtracting one entry here from the previous one, you can compute how many chunks are in this run.
1038  * You can convert this to a sample count by multiplying by the appropriate samples_per_chunk. */
1039 typedef struct
1040 {
1041     uint32_t first_chunk;                   /* the index of the first chunk in this run of chunks that share the same samples_per_chunk and sample_description_index */
1042     uint32_t samples_per_chunk;             /* the number of samples in each of these chunks */
1043     uint32_t sample_description_index;      /* the index of the sample entry that describes the samples in this chunk */
1044 } isom_stsc_entry_t;
1045 
1046 typedef struct
1047 {
1048     ISOM_FULLBOX_COMMON;
1049     lsmash_entry_list_t *list;
1050 } isom_stsc_t;
1051 
1052 /* Chunk Offset Box
1053  * chunk_offset is the offset of the start of a chunk into its containing media file.
1054  * Offsets are file offsets, not the offset into any box within the file. */
1055 typedef struct
1056 {
1057     uint32_t chunk_offset;
1058 } isom_stco_entry_t;
1059 
1060 typedef struct
1061 {
1062     /* for large presentations */
1063     uint64_t chunk_offset;
1064 } isom_co64_entry_t;
1065 
1066 typedef struct
1067 {
1068     ISOM_FULLBOX_COMMON;        /* type = 'stco': 32-bit chunk offsets / type = 'co64': 64-bit chunk offsets */
1069     lsmash_entry_list_t *list;
1070 
1071         uint8_t large_presentation;     /* Set 1 to this if 64-bit chunk-offset are needed. */
1072 } isom_stco_t;      /* share with co64 box */
1073 
1074 /* Sample Group Description Box
1075  * This box gives information about the characteristics of sample groups. */
1076 typedef struct
1077 {
1078     ISOM_FULLBOX_COMMON;            /* Use of version 0 entries is deprecated. */
1079     uint32_t grouping_type;         /* an integer that identifies the sbgp that is associated with this sample group description */
1080     uint32_t default_length;        /* the length of every group entry (if the length is constant), or zero (if it is variable)
1081                                      * This field is available only if version == 1. */
1082     lsmash_entry_list_t *list;
1083 } isom_sgpd_t;
1084 
1085 /* Random Access Entry
1086  * Samples marked by this group must be random access points, and may also be sync points. */
1087 typedef struct
1088 {
1089     /* grouping_type is 'rap ' */
1090     uint32_t description_length;                /* This field is available only if version == 1 and default_length == 0. */
1091     unsigned num_leading_samples_known : 1;     /* the value of one indicates that the number of leading samples is known for each sample in this group,
1092                                                  * and the number is specified by num_leading_samples. */
1093     unsigned num_leading_samples       : 7;     /* the number of leading samples for each sample in this group
1094                                                  * Note: when num_leading_samples_known is equal to 0, this field should be ignored. */
1095 } isom_rap_entry_t;
1096 
1097 /* Roll Recovery Entry
1098  * This grouping type is defined as that group of samples having the same roll distance. */
1099 typedef struct
1100 {
1101     /* grouping_type is 'roll' */
1102     uint32_t description_length;        /* This field is available only if version == 1 and default_length == 0. */
1103     int16_t  roll_distance;             /* the number of samples that must be decoded in order for a sample to be decoded correctly
1104                                          * A positive value indicates post-roll, and a negative value indicates pre-roll.
1105                                          * The value zero must not be used. */
1106 } isom_roll_entry_t;
1107 
1108 /* Sample to Group Box
1109  * This box is used to find the group that a sample belongs to and the associated description of that sample group. */
1110 typedef struct
1111 {
1112     ISOM_FULLBOX_COMMON;
1113     uint32_t grouping_type;             /* Links it to its sample group description table with the same value for grouping type. */
1114     uint32_t grouping_type_parameter;   /* an indication of the sub-type of the grouping
1115                                          * This field is available only if version == 1. */
1116     lsmash_entry_list_t *list;
1117 } isom_sbgp_t;
1118 
1119 typedef struct
1120 {
1121     uint32_t sample_count;                  /* the number of consecutive samples with the same sample group descriptor */
1122     uint32_t group_description_index;       /* the index of the sample group entry which describes the samples in this group
1123                                              * The index ranges from 1 to the number of sample group entries in the Sample Group Description Box,
1124                                              * or takes the value 0 to indicate that this sample is a member of no group of this type.
1125                                              * Within the Sample to Group Box in movie fragment, the group description indexes for groups defined
1126                                              * within the same fragment start at 0x10001, i.e. the index value 1, with the value 1 in the top 16 bits. */
1127 } isom_group_assignment_entry_t;
1128 
1129 /* Sample Table Box */
1130 struct isom_stbl_tag
1131 {
1132     ISOM_BASEBOX_COMMON;
1133     isom_stsd_t *stsd;      /* Sample Description Box */
1134     isom_stts_t *stts;      /* Decoding Time to Sample Box */
1135     isom_ctts_t *ctts;      /* Composition Time to Sample Box */
1136     isom_cslg_t *cslg;      /* ISOM: Composition to Decode Box / QTFF: Composition Shift Least Greatest Box */
1137     isom_stss_t *stss;      /* Sync Sample Box */
1138     isom_stps_t *stps;      /* ISOM: null / QTFF: Partial Sync Sample Box */
1139     isom_sdtp_t *sdtp;      /* Independent and Disposable Samples Box */
1140     isom_stsc_t *stsc;      /* Sample To Chunk Box */
1141     isom_stsz_t *stsz;      /* Sample Size Box */
1142     isom_stz2_t *stz2;      /* Compact Sample Size Box */
1143     isom_stco_t *stco;      /* Chunk Offset Box */
1144     lsmash_entry_list_t sgpd_list;  /* Sample Group Description Boxes */
1145     lsmash_entry_list_t sbgp_list;  /* Sample To Group Boxes */
1146 
1147         /* Use 'stz2' instead of 'stsz' if possible. (write mode only) */
1148         int (*compress_sample_size_table)( isom_stbl_t *stbl );
1149         /* Add independent and disposable info for each sample if possible. (write mode only) */
1150         int (*add_dependency_type)( isom_stbl_t *stbl, lsmash_file_t *file, lsmash_sample_property_t *prop );
1151 };
1152 
1153 /* Media Information Box */
1154 typedef struct
1155 {
1156     ISOM_BASEBOX_COMMON;
1157     /* Media Information Header Boxes */
1158     isom_vmhd_t *vmhd;      /* Video Media Header Box */
1159     isom_smhd_t *smhd;      /* Sound Media Header Box */
1160     isom_hmhd_t *hmhd;      /* ISOM: Hint Media Header Box / QTFF: null */
1161     isom_nmhd_t *nmhd;      /* ISOM: Null Media Header Box / QTFF: null */
1162     isom_gmhd_t *gmhd;      /* ISOM: null / QTFF: Generic Media Information Header Box */
1163     /* */
1164     isom_hdlr_t *hdlr;      /* ISOM: null / QTFF: Data Handler Reference Box
1165                              * Note: this box must come before Data Information Box. */
1166     isom_dinf_t *dinf;      /* Data Information Box */
1167     isom_stbl_t *stbl;      /* Sample Table Box */
1168 } isom_minf_t;
1169 
1170 /* Media Box */
1171 typedef struct
1172 {
1173     ISOM_BASEBOX_COMMON;
1174     isom_mdhd_t *mdhd;      /* Media Header Box */
1175     isom_hdlr_t *hdlr;      /* ISOM: Handler Reference Box / QTFF: Media Handler Reference Box
1176                              * Note: this box must come before Media Information Box. */
1177     isom_minf_t *minf;      /* Media Information Box */
1178 } isom_mdia_t;
1179 
1180 /* Movie Header Box
1181  * This box defines overall information which is media-independent, and relevant to the entire presentation considered as a whole. */
1182 typedef struct
1183 {
1184     ISOM_FULLBOX_COMMON;            /* version is either 0 or 1 */
1185     /* version == 0: uint64_t -> uint32_t */
1186     uint64_t creation_time;         /* the creation time of the presentation (in seconds since midnight, Jan. 1, 1904, in UTC time) */
1187     uint64_t modification_time;     /* the most recent time the presentation was modified (in seconds since midnight, Jan. 1, 1904, in UTC time) */
1188     uint32_t timescale;             /* movie timescale: timescale for the entire presentation */
1189     uint64_t duration;              /* the duration, expressed in movie timescale, of the longest track */
1190     /* The following fields are treated as
1191      * ISOM: template fields.
1192      * MP41: reserved fields.
1193      * MP42: ignored fileds since compositions are done using BIFS system.
1194      * 3GPP: ignored fields.
1195      * QTFF: usable fields. */
1196     int32_t  rate;                  /* fixed point 16.16 number. 0x00010000 is normal forward playback. */
1197     int16_t  volume;                /* fixed point 8.8 number. 0x0100 is full volume. */
1198     int16_t  reserved;
1199     int32_t  preferredLong[2];      /* ISOM: reserved / QTFF: unknown */
1200     int32_t  matrix[9];             /* transformation matrix for the video */
1201     /* The following fields are defined in QuickTime file format.
1202      * In ISO Base Media file format, these fields are treated as pre_defined. */
1203     int32_t  previewTime;           /* the time value in the movie at which the preview begins */
1204     int32_t  previewDuration;       /* the duration of the movie preview in movie timescale units */
1205     int32_t  posterTime;            /* the time value of the time of the movie poster */
1206     int32_t  selectionTime;         /* the time value for the start time of the current selection */
1207     int32_t  selectionDuration;     /* the duration of the current selection in movie timescale units */
1208     int32_t  currentTime;           /* the time value for current time position within the movie */
1209     /* */
1210     uint32_t next_track_ID;         /* larger than the largest track-ID in use */
1211 } isom_mvhd_t;
1212 
1213 /* Object Descriptor Box
1214  * Note that this box is mandatory under 14496-1:2001 (mp41) while not mandatory under 14496-14:2003 (mp42). */
1215 struct mp4sys_ObjectDescriptor_t; /* FIXME: I think these structs using mp4sys should be placed in isom.c */
1216 typedef struct
1217 {
1218     ISOM_FULLBOX_COMMON;
1219     struct mp4sys_ObjectDescriptor_t *OD;
1220 } isom_iods_t;
1221 
1222 /* Media Data Box
1223  * This box contains the media data.
1224  * A presentation may contain zero or more Media Data Boxes.*/
1225 typedef struct
1226 {
1227     ISOM_BASEBOX_COMMON;    /* If size is 0, then this box is the last box. */
1228 
1229         uint64_t media_size;    /* the total media size already written in this box */
1230         uint64_t reserved_size; /* the reserved total media size in this box
1231                                  * If 'media_size' > 'reserved_size' occurs when finishing a non-fragmented movie,
1232                                  * rewrite the size of this box. */
1233 } isom_mdat_t;
1234 
1235 /* Free Space Box
1236  * The contents of a free-space box are irrelevant and may be ignored without affecting the presentation. */
1237 typedef struct
1238 {
1239     ISOM_BASEBOX_COMMON;    /* type is 'free' or 'skip' */
1240     uint32_t length;
1241     uint8_t *data;
1242 } isom_free_t;
1243 
1244 typedef isom_free_t isom_skip_t;
1245 
1246 /* Chapter List Box
1247  * This box is NOT defined in the ISO/MPEG-4 specs.
1248  * Basically, this box exists in User Data Box inside Movie Box if present. */
1249 typedef struct
1250 {
1251     uint64_t start_time;    /* version = 0: expressed in movie timescale units
1252                              * version = 1: expressed in 100 nanoseconds */
1253     /* Chapter name is Pascal string */
1254     uint8_t chapter_name_length;
1255     char *chapter_name;
1256 } isom_chpl_entry_t;
1257 
1258 typedef struct
1259 {
1260     ISOM_FULLBOX_COMMON;            /* version = 0 is defined in F4V file format. */
1261     uint8_t unknown;                /* only available under version = 1 */
1262     lsmash_entry_list_t *list;      /* if version is set to 0, entry_count is uint8_t. */
1263 } isom_chpl_t;
1264 
1265 typedef struct
1266 {
1267     char *chapter_name;
1268     uint64_t start_time;
1269 } isom_chapter_entry_t;
1270 
1271 /* Metadata Item Keys Box */
1272 typedef struct
1273 {
1274     ISOM_FULLBOX_COMMON;
1275     lsmash_entry_list_t *list;
1276 } isom_keys_t;
1277 
1278 typedef struct
1279 {
1280     uint32_t key_size;          /* the size of the entire structure containing a key definition
1281                                  * key_size = sizeof(key_size) + sizeof(key_namespace) + sizeof(key_value) */
1282     uint32_t key_namespace;     /* a naming scheme used for metadata keys
1283                                  * Location metadata keys, for example, use the 'mdta' key namespace. */
1284     uint8_t *key_value;         /* the actual name of the metadata key
1285                                  * Keys with the 'mdta' namespace use a reverse DNS naming convention. */
1286 } isom_keys_entry_t;
1287 
1288 /* Meaning Box */
1289 typedef struct
1290 {
1291     ISOM_FULLBOX_COMMON;
1292     uint8_t *meaning_string;        /* to fill the box */
1293 
1294         uint32_t meaning_string_length;
1295 } isom_mean_t;
1296 
1297 /* Name Box */
1298 typedef struct
1299 {
1300     ISOM_FULLBOX_COMMON;
1301     uint8_t *name;      /* to fill the box */
1302 
1303         uint32_t name_length;
1304 } isom_name_t;
1305 
1306 /* Data Box */
1307 typedef struct
1308 {
1309     ISOM_BASEBOX_COMMON;
1310     /* type indicator */
1311     uint16_t reserved;              /* always 0 */
1312     uint8_t  type_set_identifier;   /* 0: type set of the common basic data types */
1313     uint8_t  type_code;             /* type of data code */
1314     /* */
1315     uint32_t the_locale;            /* reserved to be 0 */
1316     uint8_t *value;                 /* to fill the box */
1317 
1318         uint32_t value_length;
1319 } isom_data_t;
1320 
1321 /* Metadata Item Box */
1322 typedef struct
1323 {
1324     ISOM_BASEBOX_COMMON;
1325     isom_mean_t *mean;      /* Meaning Box */
1326     isom_name_t *name;      /* Name Box */
1327     isom_data_t *data;      /* Data Box */
1328 } isom_metaitem_t;
1329 
1330 /* Metadata Item List Box */
1331 typedef struct
1332 {
1333     ISOM_BASEBOX_COMMON;
1334     lsmash_entry_list_t metaitem_list;  /* Metadata Item Box List
1335                                          * There is no entry_count field. */
1336 } isom_ilst_t;
1337 
1338 /* Meta Box */
1339 typedef struct
1340 {
1341     ISOM_FULLBOX_COMMON;    /* ISOM: FullBox / QTFF: BaseBox */
1342     isom_hdlr_t *hdlr;      /* Metadata Handler Reference Box */
1343     isom_dinf_t *dinf;      /* ISOM: Data Information Box / QTFF: null */
1344     isom_keys_t *keys;      /* ISOM: null / QTFF: Metadata Item Keys Box */
1345     isom_ilst_t *ilst;      /* Metadata Item List Box only defined in Apple MPEG-4 and QTFF */
1346 } isom_meta_t;
1347 
1348 /* Window Location Box */
1349 typedef struct
1350 {
1351     ISOM_BASEBOX_COMMON;
1352     /* default window location for movie */
1353     uint16_t x;
1354     uint16_t y;
1355 } isom_WLOC_t;
1356 
1357 /* Looping Box */
1358 typedef struct
1359 {
1360     ISOM_BASEBOX_COMMON;
1361     uint32_t looping_mode;      /* 0 for none, 1 for looping, 2 for palindromic looping */
1362 } isom_LOOP_t;
1363 
1364 /* Play Selection Only Box */
1365 typedef struct
1366 {
1367     ISOM_BASEBOX_COMMON;
1368     uint8_t selection_only;     /* whether only the selected area of the movie should be played */
1369 } isom_SelO_t;
1370 
1371 /* Play All Frames Box */
1372 typedef struct
1373 {
1374     ISOM_BASEBOX_COMMON;
1375     uint8_t play_all_frames;    /* whether all frames of video should be played, regardless of timing */
1376 } isom_AllF_t;
1377 
1378 /* Copyright Box
1379  * The Copyright box contains a copyright declaration which applies to the entire presentation,
1380  * when contained within the Movie Box, or, when contained in a track, to that entire track.
1381  * There may be multiple copyright boxes using different language codes. */
1382 typedef struct
1383 {
1384     ISOM_FULLBOX_COMMON;
1385     uint16_t language;              /* ISO-639-2/T language codes. Most significant 1-bit is 0.
1386                                      * Each character is packed as the difference between its ASCII value and 0x60. */
1387     uint8_t *notice;                /* a null-terminated string in either UTF-8 or UTF-16 characters, giving a copyright notice.
1388                                      * If UTF-16 is used, the string shall start with the BYTE ORDER MARK (0xFEFF), to distinguish it from a UTF-8 string.
1389                                      * This mark does not form part of the final string. */
1390         uint32_t notice_length;
1391 } isom_cprt_t;
1392 
1393 /* User Data Box
1394  * This box is a container box for informative user-data.
1395  * This user data is formatted as a set of boxes with more specific box types, which declare more precisely their content.
1396  * QTFF: for historical reasons, this box is optionally terminated by a 32-bit integer set to 0. */
1397 typedef struct
1398 {
1399     ISOM_BASEBOX_COMMON;
1400     isom_chpl_t *chpl;      /* Chapter List Box */
1401     isom_meta_t *meta;      /* Meta Box extended by Apple for iTunes movie */
1402     /* QuickTime user data */
1403     isom_WLOC_t *WLOC;      /* Window Location Box */
1404     isom_LOOP_t *LOOP;      /* Looping Box */
1405     isom_SelO_t *SelO;      /* Play Selection Only Box */
1406     isom_AllF_t *AllF;      /* Play All Frames Box */
1407     /* Copyright Box List */
1408     lsmash_entry_list_t cprt_list;  /* Copyright Boxes is defined in ISO Base Media and 3GPP file format */
1409 } isom_udta_t;
1410 
1411 /** Caches for handling tracks **/
1412 typedef struct
1413 {
1414     uint64_t alloc;             /* total buffer size for the pool */
1415     uint64_t size;              /* total size of samples in the pool */
1416     uint32_t sample_count;      /* number of samples in the pool */
1417     uint8_t *data;              /* actual data of samples in the pool */
1418 } isom_sample_pool_t;
1419 
1420 typedef struct
1421 {
1422     uint32_t chunk_number;                  /* chunk number */
1423     uint32_t sample_description_index;      /* sample description index */
1424     uint64_t first_dts;                     /* the first DTS in chunk */
1425     isom_sample_pool_t *pool;               /* samples pooled to interleave */
1426 } isom_chunk_t;
1427 
1428 typedef struct
1429 {
1430     uint64_t dts;
1431     uint64_t cts;
1432     int32_t  ctd_shift;
1433 } isom_timestamp_t;
1434 
1435 typedef struct
1436 {
1437     isom_group_assignment_entry_t *assignment;          /* the address corresponding to the entry in Sample to Group Box */
1438     isom_group_assignment_entry_t *prev_assignment;     /* the address of the previous assignment */
1439     isom_rap_entry_t              *random_access;       /* the address corresponding to the random access entry in Sample Group Description Box */
1440     uint8_t                        is_prev_rap;         /* whether the previous sample is a random access point or not */
1441 } isom_rap_group_t;
1442 
1443 typedef struct
1444 {
1445     isom_group_assignment_entry_t *assignment;      /* the address corresponding to the entry in Sample to Group Box */
1446     isom_sgpd_t                   *sgpd;            /* the address to the active Sample Group Description Box */
1447     uint32_t first_sample;                          /* the number of the first sample of the group */
1448     uint32_t recovery_point;                        /* the identifier necessary for the recovery from its starting point to be completed */
1449     uint64_t rp_cts;                                /* the CTS of the recovery point */
1450     int16_t  roll_distance;                         /* the current roll_distance
1451                                                      * The value may be updated when 'described' is set to ROLL_DISTANCE_INITIALIZED. */
1452 #define MAX_ROLL_WAIT_AND_SEE_COUNT 64
1453     uint8_t  wait_and_see_count;                    /* Wait-and-see after initialization of roll_distance until reaching MAX_ROLL_WAIT_AND_SEE. */
1454     uint8_t  is_fragment;                           /* the flag if the current group is in fragment */
1455     uint8_t  prev_is_recovery_start;                /* whether the previous sample is a starting point of recovery or not */
1456     uint8_t  delimited;                             /* the flag if the sample_count is determined */
1457 #define ROLL_DISTANCE_INITIALIZED 1
1458 #define ROLL_DISTANCE_DETERMINED  2
1459     uint8_t  described;                             /* the status of the group description */
1460 } isom_roll_group_t;
1461 
1462 typedef struct
1463 {
1464     lsmash_entry_list_t *pool;  /* grouping pooled to delimit and describe */
1465 } isom_grouping_t;
1466 
1467 typedef struct
1468 {
1469     uint64_t segment_duration;     /* the sum of the subsegment_duration of preceeding subsegments */
1470     uint64_t largest_cts;          /* the largest CTS of a subsegment of the reference stream */
1471     uint64_t smallest_cts;         /* the smallest CTS of a subsegment of the reference stream */
1472     uint64_t first_sample_cts;     /* the CTS of the first sample of a subsegment of the reference stream  */
1473     /* SAP related info within the active subsegment of the reference stream */
1474     uint64_t                  first_ed_cts;     /* the earliest CTS of decodable samples after the first recovery point */
1475     uint64_t                  first_rp_cts;     /* the CTS of the first recovery point */
1476     uint32_t                  first_rp_number;  /* the number of the first recovery point */
1477     uint32_t                  first_ra_number;  /* the number of the first random accessible sample */
1478     lsmash_random_access_flag first_ra_flags;   /* the flags of the first random accessible sample */
1479     int                       is_first_recovery_point;
1480     int                       decodable;
1481 } isom_subsegment_t;
1482 
1483 typedef struct
1484 {
1485     uint8_t           has_samples;          /* Whether whole movie has any sample or not. */
1486     uint8_t           roll_grouping;
1487     uint8_t           rap_grouping;
1488     uint32_t          traf_number;
1489     uint32_t          last_duration;        /* the last sample duration in this track fragment */
1490     uint64_t          largest_cts;          /* the largest CTS in this track fragment */
1491     uint32_t          sample_count;         /* the number of samples in this track fragment */
1492     uint32_t          output_sample_count;  /* the number of output samples in this track fragment */
1493     isom_subsegment_t subsegment;
1494 } isom_fragment_t;
1495 
1496 typedef struct
1497 {
1498     uint8_t           all_sync;     /* if all samples are sync sample */
1499     uint8_t           is_audio;
1500     isom_chunk_t      chunk;
1501     isom_timestamp_t  timestamp;    /* Each field stores the last valid value. */
1502     isom_grouping_t   roll;
1503     isom_rap_group_t *rap;
1504     isom_fragment_t  *fragment;
1505 } isom_cache_t;
1506 
1507 /** Movie Fragments Boxes **/
1508 /* Track Fragments Flags ('tf_flags') */
1509 typedef enum
1510 {
1511     ISOM_TF_FLAGS_BASE_DATA_OFFSET_PRESENT               = 0x000001,    /* base-data-offset-present:
1512                                                                          * This flag indicates the presence of the base_data_offset field.
1513                                                                          * The base_data_offset is the base offset to use when calculating data offsets.
1514                                                                          * Offsets are file offsets as like as chunk_offset in Chunk Offset Box.
1515                                                                          * If this flag is set and default-base-is-moof is not set, the base_data_offset
1516                                                                          * for the first track in the movie fragment is the position of the first byte
1517                                                                          * of the enclosing Movie Fragment Box, and for second and subsequent track
1518                                                                          * fragments, the default is the end of the data defined by the preceding fragment. */
1519     ISOM_TF_FLAGS_SAMPLE_DESCRIPTION_INDEX_PRESENT       = 0x000002,    /* sample-description-index-present
1520                                                                          * This flag indicates the presence of the sample_description_index field. */
1521     ISOM_TF_FLAGS_DEFAULT_SAMPLE_DURATION_PRESENT        = 0x000008,    /* default-sample-duration-present:
1522                                                                          * This flag indicates the presence of the default_sample_duration field. */
1523     ISOM_TF_FLAGS_DEFAULT_SAMPLE_SIZE_PRESENT            = 0x000010,    /* default-sample-size-present:
1524                                                                          * This flag indicates the presence of the default_sample_size field. */
1525     ISOM_TF_FLAGS_DEFAULT_SAMPLE_FLAGS_PRESENT           = 0x000020,    /* default-sample-flags-present:
1526                                                                          * This flag indicates the presence of the default_sample_flags field. */
1527     ISOM_TF_FLAGS_DURATION_IS_EMPTY                      = 0x010000,    /* duration-is-empty:
1528                                                                          * This flag indicates there are no samples for this time interval. */
1529     ISOM_TF_FLAGS_DEFAULT_BASE_IS_MOOF                   = 0x020000,    /* default-base-is-moof:
1530                                                                          * If base-data-offset-present is not set, this flag indicates the implicit
1531                                                                          * base_data_offset is always equal to the position of the first byte of the
1532                                                                          * enclosing Movie Fragment BOX.
1533                                                                          * This flag is only available under the 'iso5' or later brands and cannot be set
1534                                                                          * when earlier brands are included in the File Type box. */
1535 } isom_tf_flags_code;
1536 
1537 /* Track Run Flags ('tr_flags') */
1538 typedef enum
1539 {
1540     ISOM_TR_FLAGS_DATA_OFFSET_PRESENT                    = 0x000001,    /* data-offset-present:
1541                                                                          * This flag indicates the presence of the data_offset field. */
1542     ISOM_TR_FLAGS_FIRST_SAMPLE_FLAGS_PRESENT             = 0x000004,    /* first-sample-flags-present:
1543                                                                          * This flag indicates the presence of the first_sample_flags field. */
1544     ISOM_TR_FLAGS_SAMPLE_DURATION_PRESENT                = 0x000100,    /* sample-duration-present:
1545                                                                          * This flag indicates the presence of the sample_duration field. */
1546     ISOM_TR_FLAGS_SAMPLE_SIZE_PRESENT                    = 0x000200,    /* sample-size-present:
1547                                                                          * This flag indicates the presence of the sample_size field. */
1548     ISOM_TR_FLAGS_SAMPLE_FLAGS_PRESENT                   = 0x000400,    /* sample-flags-present:
1549                                                                          * This flag indicates the presence of the sample_flags field. */
1550     ISOM_TR_FLAGS_SAMPLE_COMPOSITION_TIME_OFFSET_PRESENT = 0x000800,    /* sample-composition-time-offsets-present:
1551                                                                          * This flag indicates the presence of the sample_composition_time_offset field. */
1552 } isom_tr_flags_code;
1553 
1554 /* Sample Flags */
1555 typedef struct
1556 {
1557     unsigned reserved                  : 4;
1558     /* The definition of the following fields is quite the same as Independent and Disposable Samples Box. */
1559     unsigned is_leading                : 2;
1560     unsigned sample_depends_on         : 2;
1561     unsigned sample_is_depended_on     : 2;
1562     unsigned sample_has_redundancy     : 2;
1563     /* */
1564     unsigned sample_padding_value      : 3;     /* the number of bits at the end of this sample */
1565     unsigned sample_is_non_sync_sample : 1;     /* 0 value means this sample is sync sample. */
1566     uint16_t sample_degradation_priority;
1567 } isom_sample_flags_t;
1568 
1569 /* Movie Extends Header Box
1570  * This box is omitted when used in live streaming.
1571  * If this box is not present, the overall duration must be computed by examining each fragment. */
1572 typedef struct
1573 {
1574     ISOM_FULLBOX_COMMON;
1575     /* version == 0: uint64_t -> uint32_t */
1576     uint64_t fragment_duration;     /* the duration of the longest track, in the timescale indicated in the Movie Header Box, including movie fragments. */
1577 } isom_mehd_t;
1578 
1579 /* Track Extends Box
1580  * This box sets up default values used by the movie fragments. */
1581 typedef struct
1582 {
1583     ISOM_FULLBOX_COMMON;
1584     uint32_t            track_ID;       /* identifier of the track; this shall be the track ID of a track in the Movie Box */
1585     uint32_t            default_sample_description_index;
1586     uint32_t            default_sample_duration;
1587     uint32_t            default_sample_size;
1588     isom_sample_flags_t default_sample_flags;
1589 } isom_trex_t;
1590 
1591 /* Movie Extends Box
1592  * This box warns readers that there might be Movie Fragment Boxes in this file. */
1593 typedef struct
1594 {
1595     ISOM_BASEBOX_COMMON;
1596     isom_mehd_t         *mehd;          /* Movie Extends Header Box / omitted when used in live streaming */
1597     lsmash_entry_list_t  trex_list;     /* Track Extends Box */
1598 } isom_mvex_t;
1599 
1600 /* Movie Fragment Header Box
1601  * This box contains a sequence number, as a safety check.
1602  * The sequence number 'usually' starts at 1 and must increase for each movie fragment in the file, in the order in which they occur. */
1603 typedef struct
1604 {
1605     ISOM_FULLBOX_COMMON;
1606     uint32_t sequence_number;       /* the ordinal number of this fragment, in increasing order */
1607 } isom_mfhd_t;
1608 
1609 /* Track Fragment Header Box
1610  * Each movie fragment can contain zero or more fragments for each track;
1611  * and a track fragment can contain zero or more contiguous runs of samples.
1612  * This box sets up information and defaults used for those runs of samples. */
1613 typedef struct
1614 {
1615     ISOM_FULLBOX_COMMON;                            /* flags field is used for 'tf_flags'. */
1616     uint32_t            track_ID;
1617     /* all the following are optional fields */
1618     uint64_t            base_data_offset;           /* an explicit anchor for the data offsets in each track run
1619                                                      * To avoid the case this field might overflow, e.g. semi-permanent live streaming and broadcasting,
1620                                                      * you shall not use this optional field. */
1621     uint32_t            sample_description_index;   /* override default_sample_description_index in Track Extends Box */
1622     uint32_t            default_sample_duration;    /* override default_sample_duration in Track Extends Box */
1623     uint32_t            default_sample_size;        /* override default_sample_size in Track Extends Box */
1624     isom_sample_flags_t default_sample_flags;       /* override default_sample_flags in Track Extends Box */
1625 } isom_tfhd_t;
1626 
1627 /* Track Fragment Base Media Decode Time Box
1628  * This box provides the absolute decode time, measured on the media timeline, of the first sample in decode order in the track fragment.
1629  * This can be useful, for example, when performing random access in a file;
1630  * it is not necessary to sum the sample durations of all preceding samples in previous fragments to find this value
1631  * (where the sample durations are the deltas in the Decoding Time to Sample Box and the sample_durations in the preceding track runs).
1632  * This box, if present, shall be positioned after the Track Fragment Header Box and before the first Track Fragment Run box. */
1633 typedef struct
1634 {
1635     ISOM_FULLBOX_COMMON;    /* version is either 0 or 1 */
1636     /* version == 0: 64bits -> 32bits */
1637     uint64_t baseMediaDecodeTime;   /* an integer equal to the sum of the decode durations of all earlier samples in the media, expressed in the media's timescale
1638                                      * It does not include the samples added in the enclosing track fragment.
1639                                      * NOTE: the decode timeline is a media timeline, established before any explicit or implied mapping of media time to presentation time,
1640                                      *       for example by an edit list or similar structure. */
1641 } isom_tfdt_t;
1642 
1643 /* Track Fragment Run Box
1644  * Within the Track Fragment Box, there are zero or more Track Fragment Run Boxes.
1645  * If the duration-is-empty flag is set in the tf_flags, there are no track runs.
1646  * A track run documents a contiguous set of samples for a track. */
1647 typedef struct
1648 {
1649     ISOM_FULLBOX_COMMON;                        /* flags field is used for 'tr_flags'. */
1650     uint32_t            sample_count;           /* the number of samples being added in this run; also the number of rows in the following table */
1651     /* The following are optional fields. */
1652     int32_t             data_offset;            /* This value is added to the implicit or explicit data_offset established in the Track Fragment Header Box.
1653                                                  * If this field is not present, then the data for this run starts immediately after the data of the previous run,
1654                                                  * or at the base_data_offset defined by the Track Fragment Header Box if this is the first run in a track fragment. */
1655     isom_sample_flags_t first_sample_flags;     /* a set of flags for the first sample only of this run */
1656     lsmash_entry_list_t *optional;              /* all fields in this array are optional. */
1657 } isom_trun_t;
1658 
1659 typedef struct
1660 {
1661     /* If the following fields is present, each field overrides default value described in Track Fragment Header Box or Track Extends Box. */
1662     uint32_t            sample_duration;                    /* override default_sample_duration */
1663     uint32_t            sample_size;                        /* override default_sample_size */
1664     isom_sample_flags_t sample_flags;                       /* override default_sample_flags */
1665     /* */
1666     uint32_t            sample_composition_time_offset;     /* composition time offset
1667                                                              *   If version == 0, unsigned 32-bit integer.
1668                                                              *   Otherwise, signed 32-bit integer. */
1669 } isom_trun_optional_row_t;
1670 
1671 /* Track Fragment Box */
1672 typedef struct
1673 {
1674     ISOM_BASEBOX_COMMON;
1675     isom_tfhd_t         *tfhd;          /* Track Fragment Header Box */
1676     isom_tfdt_t         *tfdt;          /* Track Fragment Base Media Decode Time Box */
1677     lsmash_entry_list_t  trun_list;     /* Track Fragment Run Box List
1678                                          * If the duration-is-empty flag is set in the tf_flags, there are no track runs. */
1679     isom_sdtp_t         *sdtp;          /* Independent and Disposable Samples Box (available under Protected Interoperable File Format) */
1680     lsmash_entry_list_t  sgpd_list;     /* Sample Group Description Boxes (available under ISO Base Media version 6 or later) */
1681     lsmash_entry_list_t  sbgp_list;     /* Sample To Group Boxes */
1682 
1683         isom_cache_t *cache;            /* taken over from corresponding 'trak' */
1684 } isom_traf_t;
1685 
1686 /* Movie Fragment Box */
1687 typedef struct
1688 {
1689     ISOM_BASEBOX_COMMON;
1690     isom_mfhd_t         *mfhd;          /* Movie Fragment Header Box */
1691     lsmash_entry_list_t  traf_list;     /* Track Fragment Box List */
1692 } isom_moof_t;
1693 
1694 /* Track Fragment Random Access Box
1695  * Each entry in this box contains the location and the presentation time of the sync sample.
1696  * Note that not every sync sample in the track needs to be listed in the table.
1697  * The absence of this box does not mean that all the samples are sync samples. */
1698 typedef struct
1699 {
1700     ISOM_FULLBOX_COMMON;
1701     uint32_t track_ID;
1702     unsigned int reserved                  : 26;
1703     unsigned int length_size_of_traf_num   : 2;     /* the length in byte of the traf_number field minus one */
1704     unsigned int length_size_of_trun_num   : 2;     /* the length in byte of the trun_number field minus one */
1705     unsigned int length_size_of_sample_num : 2;     /* the length in byte of the sample_number field minus one */
1706     uint32_t number_of_entry;                       /* the number of the entries for this track
1707                                                      * Value zero indicates that every sample is a sync sample and no table entry follows. */
1708     lsmash_entry_list_t *list;                      /* entry_count corresponds to number_of_entry. */
1709 } isom_tfra_t;
1710 
1711 typedef struct
1712 {
1713     /* version == 0: 64bits -> 32bits */
1714     uint64_t time;              /* the presentation time of the sync sample in units defined in the Media Header Box of the associated track
1715                                  * For segments based on movie sample tables or movie fragments, presentation times are in the movie timeline,
1716                                  * that is they are composition times after the application of any edit list for the track.
1717                                  * Note: the definition of segment is portion of an ISO base media file format file, consisting of either
1718                                  *       (a) a movie box, with its associated media data (if any) and other associated boxes
1719                                  *        or
1720                                  *       (b) one or more movie fragment boxes, with their associated media data, and other associated boxes. */
1721     uint64_t moof_offset;       /* the offset of the Movie Fragment Box used in this entry
1722                                  * Offset is the byte-offset between the beginning of the file and the beginning of the Movie Fragment Box. */
1723     /* */
1724     uint32_t traf_number;       /* the Track Fragment Box ('traf') number that contains the sync sample
1725                                  * The number ranges from 1 in each Movie Fragment Box ('moof'). */
1726     uint32_t trun_number;       /* the Track Fragment Run Box ('trun') number that contains the sync sample
1727                                  * The number ranges from 1 in each Track Fragment Box ('traf'). */
1728     uint32_t sample_number;     /* the sample number that contains the sync sample
1729                                  * The number ranges from 1 in each Track Fragment Run Box ('trun'). */
1730 } isom_tfra_location_time_entry_t;
1731 
1732 /* Movie Fragment Random Access Offset Box
1733  * This box provides a copy of the length field from the enclosing Movie Fragment Random Access Box. */
1734 typedef struct
1735 {
1736     ISOM_FULLBOX_COMMON;
1737     uint32_t length;        /* an integer gives the number of bytes of the enclosing Movie Fragment Random Access Box
1738                              * This field is placed at the last of the enclosing box to assist readers scanning
1739                              * from the end of the file in finding the Movie Fragment Random Access Box. */
1740 } isom_mfro_t;
1741 
1742 /* Movie Fragment Random Access Box
1743  * This box provides a table which may assist readers in finding sync samples in a file using movie fragments,
1744  * and is usually placed at or near the end of the file.
1745  * The last box within the Movie Fragment Random Access Box, which is called Movie Fragment Random Access Offset Box,
1746  * provides a copy of the length field from the Movie Fragment Random Access Box. */
1747 typedef struct
1748 {
1749     ISOM_BASEBOX_COMMON;
1750     lsmash_entry_list_t  tfra_list;     /* Track Fragment Random Access Box */
1751     isom_mfro_t         *mfro;          /* Movie Fragment Random Access Offset Box */
1752 } isom_mfra_t;
1753 
1754 /* Movie fragment manager
1755  * The presence of this means we use the structure of movie fragments. */
1756 typedef struct
1757 {
1758 #define FIRST_MOOF_POS_UNDETERMINED UINT64_MAX
1759     isom_moof_t         *movie;             /* the address corresponding to the current Movie Fragment Box */
1760     uint64_t             first_moof_pos;
1761     uint64_t             pool_size;         /* the total sample size in the current movie fragment */
1762     uint64_t             sample_count;      /* the number of samples within the current movie fragment */
1763     lsmash_entry_list_t *pool;              /* samples pooled to interleave for the current movie fragment */
1764 } isom_fragment_manager_t;
1765 
1766 /** **/
1767 
1768 /* Track Box */
1769 typedef struct
1770 {
1771     ISOM_BASEBOX_COMMON;
1772     isom_tkhd_t *tkhd;          /* Track Header Box */
1773     isom_tapt_t *tapt;          /* ISOM: null / QTFF: Track Aperture Mode Dimensions Box */
1774     isom_edts_t *edts;          /* Edit Box */
1775     isom_tref_t *tref;          /* Track Reference Box */
1776     isom_mdia_t *mdia;          /* Media Box */
1777     isom_udta_t *udta;          /* User Data Box */
1778     isom_meta_t *meta;          /* Meta Box */
1779 
1780         isom_cache_t *cache;
1781         uint32_t      related_track_ID;
1782         uint8_t       is_chapter;
1783 } isom_trak_t;
1784 
1785 /* Movie Box */
1786 typedef struct
1787 {
1788     ISOM_BASEBOX_COMMON;
1789     isom_mvhd_t         *mvhd;          /* Movie Header Box */
1790     isom_iods_t         *iods;          /* MP4: Object Descriptor Box */
1791     lsmash_entry_list_t  trak_list;     /* Track Box List */
1792     isom_udta_t         *udta;          /* User Data Box */
1793     isom_ctab_t         *ctab;          /* ISOM: null / QTFF: Color Table Box */
1794     isom_meta_t         *meta;          /* Meta Box */
1795     isom_mvex_t         *mvex;          /* Movie Extends Box */
1796 } isom_moov_t;
1797 
1798 /** Segments
1799  * segment
1800  *   portion of an ISO base media file format file, consisting of either (a) a movie box, with its associated media data
1801  *   (if any) and other associated boxes or (b) one or more movie fragment boxes, with their associated media data, and
1802  *   and other associated boxes
1803  * subsegment
1804  *   time interval of a segment formed from movie fragment boxes, that is also a valid segment
1805  *     A subsegment is defined as a time interval of the containing (sub)segment, and corresponds to a single range of
1806  *     bytes of the containing (sub)segment. The durations of all the subsegments sum to the duration of the containing
1807  *     (sub)segment.
1808  **/
1809 /* Segment Type Box
1810  * Media presentations may be divided into segments for delivery, for example, it is possible (e.g. in HTTP streaming) to
1811  * form files that contain a segment ? or concatenated segments ? which would not necessarily form ISO Base Media file
1812  * format compliant files (e.g. they do not contain a Movie Box).
1813  * If segments are stored in separate files (e.g. on a standard HTTP server) it is recommended that these 'segment files'
1814  * contain a Segment Type Box, which must be first if present, to enable identification of those files, and declaration of
1815  * the specifications with which they are compliant.
1816  * Segment Type Boxes that are not first in a file may be ignored.
1817  * Valid Segment Type Boxes shall be the first box in a segment.
1818  * Note:
1819  *   The 'valid' here does not always mean that any brand of that segment has compatibility against other brands of it.
1820  *   After concatenations of segments, the result file might contain incompatibilities among brands. */
1821 typedef isom_ftyp_t isom_styp_t;
1822 
1823 /* Segment Index Box
1824  * This box provides a compact index of one media stream within the media segment to which it applies.
1825  *
1826  * Each Segment Index Box documents how a (sub)segment is divided into one or more subsegments (which may themselves be
1827  * further subdivided using Segment Index boxes).
1828  *
1829  * Each entry in the Segment Index Box contains a reference type that indicates whether the reference points directly to
1830  * the media bytes of a referenced leaf subsegment, which is a subsegment that does not contain any indexing information
1831  * that would enable its further division into subsegments, or to a Segment Index box that describes how the referenced
1832  * subsegment is further subdivided; as a result, the segment may be indexed in a 'hierarchical' or 'daisy-chain' or
1833  * other form by documenting time and byte offset information for other Segment Index Boxes applying to portions of the
1834  * same (sub)segment.
1835  *
1836  * For segments based on ISO Base Media file format (i.e. based on movie sample tables or movie fragments):
1837  *   ! an access unit is a sample;
1838  *   ! a subsegment is a self-contained set of one or more consecutive movie fragments; a self-contained set contains
1839  *     one or more Movie Fragment Boxes with the corresponding Media Data Box(es), and a Media Data Box containing data
1840  *     referenced by a Movie Fragment Box must follow that Movie Fragment Box and precede the next Movie Fragment box
1841  *     containing information about the same track;
1842  *   ! Segment Index Boxes shall be placed before subsegment material they document, that is, before any Movie Fragment
1843  *     Box of the documented material of the subsegment;
1844  *   ! streams are tracks in the file format, and stream IDs are track IDs;
1845  *   ! a subsegment contains a stream access point if a track fragment within the subsegment for the track with track_ID
1846  *     equal to reference_ID contains a stream access point;
1847  *   ! initialisation data for SAPs consists of the Movie Box;
1848  *   ! presentation times are in the movie timeline, that is they are composition times after the application of any edit
1849  *     list for the track;
1850  *   ! the ISAP is a position exactly pointing to the start of a top-level box, such as a Movie Fragment Box;
1851  *   ! a SAP of type 1 or type 2 is indicated as a sync sample;
1852  *   ! a SAP of type 3 is marked as a member of a sample group of type 'rap ';
1853  *   ! a SAP of type 4 is marked as a member of a sample group of type 'roll' where the value of the roll_distance field
1854  *     is greater than 0.
1855  *   For SAPs of type 5 and 6, no specific signalling in the ISO Base Media file format is supported. */
1856 typedef struct
1857 {
1858     unsigned int reference_type  : 1;   /* 1: the reference is to a Segment Index Box
1859                                          * 0: the reference is to media content
1860                                          *      For files based on the ISO Base Media file format, the reference is to a
1861                                          *      Movie Fragment Box.
1862                                          *   If a separate index segment is used, then entries with reference type 1 are
1863                                          *   in the index segment, and entries with reference type 0 are in the media file. */
1864     unsigned int reference_size  : 31;  /* the distance in bytes from the first byte of the referenced item to the first
1865                                          * byte of the next referenced item, or in the case of the last entry, the end of
1866                                          * the referenced material */
1867     uint32_t     subsegment_duration;   /* when the reference is to Segment Index Box, i.e. reference_type is equal to 1:
1868                                          *   this field carries the sum of the subsegment_duration fields in that box;
1869                                          * when the reference is to a subsegment:
1870                                          *   this field carries the difference between the earliest presentation time of
1871                                          *   any access unit of the reference stream in the next subsegment (or the first
1872                                          *   subsegment of the next segment, if this is the last subsegment of the segment,
1873                                          *   or the end presentation time of the reference stream if this is the last
1874                                          *   subsegment of the stream) and the earliest presentation time of any access
1875                                          *   unit of the reference stream in the referenced subsegment;
1876                                          * The duration is expressed in the timescale of the enclosing Segment Index Box. */
1877     unsigned int starts_with_SAP : 1;   /* whether the referenced subsegments start with a SAP */
1878     unsigned int SAP_type        : 3;   /* a SAP type or the value 0
1879                                          *   When starting with a SAP, the value 0 means a SAP may be of an unknown type.
1880                                          *   Otherwise, the value 0 means no information of SAPs is provided. */
1881     unsigned int SAP_delta_time  : 28;  /* TSAP of the first SAP, in decoding order, in the referenced subsegment for
1882                                          * the reference stream
1883                                          *   If the referenced subsegments do not contain a SAP, SAP_delta_time is
1884                                          *   reserved with the value 0, otherwise SAP_delta_time is the difference between
1885                                          *   the earliest presentation time of the subsegment, and the TSAP.
1886                                          *   Note that this difference may be zero, in the case that the subsegment starts
1887                                          *   with a SAP. */
1888 } isom_sidx_referenced_item_t;
1889 
1890 typedef struct
1891 {
1892     ISOM_FULLBOX_COMMON;
1893     uint32_t reference_ID;      /* the stream ID for the reference stream
1894                                  *   If this Segment Index box is referenced from a "parent" Segment Index box, the value
1895                                  *   of the value of reference_ID shall be the same as the value of reference_ID of the
1896                                  *   "parent" Segment Index Box. */
1897     uint32_t timescale;         /* the timescale, in ticks per second, for the time and duration fields within this box
1898                                  *   It is recommended that this match the timescale of the reference stream or track.
1899                                  *   For files based on the ISO Base Media file format, that is the timescale field of
1900                                  *   the Media Header Box of the track. */
1901     /* version == 0: 64bits -> 32bits */
1902     uint64_t earliest_presentation_time;    /* the earliest presentation time of any access unit in the reference stream
1903                                              * in the first subsegment, in the timescale indicated in the timescale field */
1904     uint64_t first_offset;                  /* the distance in bytes, in the file containing media, from the anchor point,
1905                                              * to the first byte of the indexed material */
1906     /* */
1907     uint16_t reserved;          /* 0 */
1908     uint16_t reference_count;   /* the number of referenced items */
1909     lsmash_entry_list_t *list;  /* entry_count corresponds to reference_count. */
1910 } isom_sidx_t;
1911 
1912 /** **/
1913 
1914 /* File */
1915 struct lsmash_file_tag
1916 {
1917     ISOM_FULLBOX_COMMON;                /* The 'size' field indicates total file size.
1918                                          * The 'flags' field indicates file mode. */
1919     isom_ftyp_t         *ftyp;          /* File Type Box */
1920     lsmash_entry_list_t  styp_list;     /* Segment Type Box List */
1921     isom_moov_t         *moov;          /* Movie Box */
1922     lsmash_entry_list_t  sidx_list;     /* Segment Index Box List */
1923     lsmash_entry_list_t  moof_list;     /* Movie Fragment Box List */
1924     isom_mdat_t         *mdat;          /* Media Data Box */
1925     isom_meta_t         *meta;          /* Meta Box */
1926     isom_mfra_t         *mfra;          /* Movie Fragment Random Access Box */
1927 
1928         lsmash_bs_t             *bs;        /* bytestream manager */
1929         isom_fragment_manager_t *fragment;  /* movie fragment manager */
1930         lsmash_entry_list_t     *print;
1931         lsmash_entry_list_t     *timeline;
1932         lsmash_file_t           *initializer;   /* A file containing the initialization information of whole movie including subsequent segments
1933                                                  * For ISOBMFF, an initializer corresponds to a file containing the 'moov' box.
1934                                                  * ROOT-to-initializer is designed to be a one-to-one relationship while initializer-to-file
1935                                                  * is designed to be a one-to-many relationship. */
1936         struct importer_tag     *importer;      /* An importer of this file
1937                                                  * Importer-to-file is designed to be a one-to-one relationship. */
1938         uint64_t  fragment_count;           /* the number of movie fragments we created */
1939         double    max_chunk_duration;       /* max duration per chunk in seconds */
1940         double    max_async_tolerance;      /* max tolerance, in seconds, for amount of interleaving asynchronization between tracks */
1941         uint64_t  max_chunk_size;           /* max size per chunk in bytes. */
1942         uint32_t  brand_count;
1943         uint32_t *compatible_brands;        /* the backup of the compatible brands in the File Type Box or the valid Segment Type Box */
1944         uint8_t   fake_file_mode;           /* If set to 1, the bytestream manager handles fake-file stream. */
1945         /* flags for compatibility */
1946 #define COMPAT_FLAGS_OFFSET offsetof( lsmash_file_t, qt_compatible )
1947         uint8_t qt_compatible;              /* compatibility with QuickTime file format */
1948         uint8_t isom_compatible;            /* compatibility with ISO Base Media file format */
1949         uint8_t avc_extensions;             /* compatibility with AVC extensions */
1950         uint8_t mp4_version1;               /* compatibility with MP4 ver.1 file format */
1951         uint8_t mp4_version2;               /* compatibility with MP4 ver.2 file format */
1952         uint8_t itunes_movie;               /* compatibility with iTunes Movie */
1953         uint8_t max_3gpp_version;           /* maximum 3GPP version */
1954         uint8_t max_isom_version;           /* maximum ISO Base Media file format version */
1955         uint8_t min_isom_version;           /* minimum ISO Base Media file format version */
1956         uint8_t forbid_tref;                /* If set to 1, track reference is forbidden. */
1957         uint8_t undefined_64_ver;           /* If set to 1, 64-bit version fields, e.g. duration, are undefined. */
1958         uint8_t allow_moof_base;            /* If set to 1, default-base-is-moof is available for muxing. */
1959         uint8_t media_segment;              /* If set to 1, this file is a media segment. */
1960 };
1961 
1962 /* fake-file stream */
1963 typedef struct
1964 {
1965     uint32_t size;
1966     uint8_t *data;
1967     uint32_t pos;
1968 } fake_file_stream_t;
1969 
1970 /* ROOT */
1971 struct lsmash_root_tag
1972 {
1973     ISOM_FULLBOX_COMMON;                    /* The 'file' field contains the address of the current active file. */
1974     lsmash_entry_list_t file_abstract_list; /* the list of all files the ROOT contains */
1975 };
1976 
1977 /** **/
1978 
1979 /* Pre-defined precedence */
1980 #define LSMASH_BOX_PRECEDENCE_ISOM_FTYP (LSMASH_BOX_PRECEDENCE_H  -  0 * LSMASH_BOX_PRECEDENCE_S)
1981 #define LSMASH_BOX_PRECEDENCE_ISOM_STYP (LSMASH_BOX_PRECEDENCE_H  -  0 * LSMASH_BOX_PRECEDENCE_S)
1982 #define LSMASH_BOX_PRECEDENCE_ISOM_SIDX (LSMASH_BOX_PRECEDENCE_N  +  1 * LSMASH_BOX_PRECEDENCE_S)   /* shall be placed before any 'moof' of the documented subsegments */
1983 #define LSMASH_BOX_PRECEDENCE_ISOM_MOOV (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
1984 #define LSMASH_BOX_PRECEDENCE_ISOM_MVHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
1985 #define LSMASH_BOX_PRECEDENCE_ISOM_IODS (LSMASH_BOX_PRECEDENCE_HM -  2 * LSMASH_BOX_PRECEDENCE_S)
1986 #define LSMASH_BOX_PRECEDENCE_ISOM_TRAK (LSMASH_BOX_PRECEDENCE_N  -  2 * LSMASH_BOX_PRECEDENCE_S)
1987 #define LSMASH_BOX_PRECEDENCE_ISOM_TKHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
1988 #define LSMASH_BOX_PRECEDENCE_QTFF_TAPT (LSMASH_BOX_PRECEDENCE_N  -  1 * LSMASH_BOX_PRECEDENCE_S)
1989 #define LSMASH_BOX_PRECEDENCE_QTFF_CLEF (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
1990 #define LSMASH_BOX_PRECEDENCE_QTFF_PROF (LSMASH_BOX_PRECEDENCE_N  -  1 * LSMASH_BOX_PRECEDENCE_S)
1991 #define LSMASH_BOX_PRECEDENCE_QTFF_ENOF (LSMASH_BOX_PRECEDENCE_N  -  2 * LSMASH_BOX_PRECEDENCE_S)
1992 #define LSMASH_BOX_PRECEDENCE_ISOM_EDTS (LSMASH_BOX_PRECEDENCE_N  -  2 * LSMASH_BOX_PRECEDENCE_S)
1993 #define LSMASH_BOX_PRECEDENCE_ISOM_ELST (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
1994 #define LSMASH_BOX_PRECEDENCE_ISOM_TREF (LSMASH_BOX_PRECEDENCE_N  -  3 * LSMASH_BOX_PRECEDENCE_S)
1995 #define LSMASH_BOX_PRECEDENCE_ISOM_TREF_TYPE (LSMASH_BOX_PRECEDENCE_N - 0 * LSMASH_BOX_PRECEDENCE_S)
1996 #define LSMASH_BOX_PRECEDENCE_ISOM_MDIA (LSMASH_BOX_PRECEDENCE_N  -  4 * LSMASH_BOX_PRECEDENCE_S)
1997 #define LSMASH_BOX_PRECEDENCE_ISOM_MDHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
1998 #define LSMASH_BOX_PRECEDENCE_ISOM_HDLR (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
1999 #define LSMASH_BOX_PRECEDENCE_ISOM_MINF (LSMASH_BOX_PRECEDENCE_N  -  1 * LSMASH_BOX_PRECEDENCE_S)
2000 #define LSMASH_BOX_PRECEDENCE_ISOM_VMHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2001 #define LSMASH_BOX_PRECEDENCE_ISOM_SMHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2002 #define LSMASH_BOX_PRECEDENCE_ISOM_HMHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2003 #define LSMASH_BOX_PRECEDENCE_ISOM_NMHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2004 #define LSMASH_BOX_PRECEDENCE_QTFF_GMHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2005 #define LSMASH_BOX_PRECEDENCE_QTFF_GMIN (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2006 #define LSMASH_BOX_PRECEDENCE_QTFF_TEXT (LSMASH_BOX_PRECEDENCE_N  -  1 * LSMASH_BOX_PRECEDENCE_S)
2007 #define LSMASH_BOX_PRECEDENCE_ISOM_DINF (LSMASH_BOX_PRECEDENCE_N  -  1 * LSMASH_BOX_PRECEDENCE_S)
2008 #define LSMASH_BOX_PRECEDENCE_ISOM_DREF (LSMASH_BOX_PRECEDENCE_N  -  1 * LSMASH_BOX_PRECEDENCE_S)
2009 #define LSMASH_BOX_PRECEDENCE_ISOM_DREF_ENTRY (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2010 #define LSMASH_BOX_PRECEDENCE_ISOM_STBL (LSMASH_BOX_PRECEDENCE_N  -  2 * LSMASH_BOX_PRECEDENCE_S)
2011 #define LSMASH_BOX_PRECEDENCE_ISOM_STSD (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2012 #define LSMASH_BOX_PRECEDENCE_QTFF_GLBL (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2013 #define LSMASH_BOX_PRECEDENCE_ISOM_ESDS (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2014 #define LSMASH_BOX_PRECEDENCE_QTFF_ESDS (LSMASH_BOX_PRECEDENCE_HM -  1 * LSMASH_BOX_PRECEDENCE_S)   /* preceded by 'frma' and 'mp4a' */
2015 #define LSMASH_BOX_PRECEDENCE_ISOM_BTRT (LSMASH_BOX_PRECEDENCE_HM -  1 * LSMASH_BOX_PRECEDENCE_S)
2016 #define LSMASH_BOX_PRECEDENCE_ISOM_COLR (LSMASH_BOX_PRECEDENCE_LP +  2 * LSMASH_BOX_PRECEDENCE_S)
2017 #define LSMASH_BOX_PRECEDENCE_QTFF_COLR (LSMASH_BOX_PRECEDENCE_LP +  2 * LSMASH_BOX_PRECEDENCE_S)
2018 #define LSMASH_BOX_PRECEDENCE_QTFF_GAMA (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2019 #define LSMASH_BOX_PRECEDENCE_QTFF_FIEL (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2020 #define LSMASH_BOX_PRECEDENCE_QTFF_CSPC (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2021 #define LSMASH_BOX_PRECEDENCE_QTFF_SGBT (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)   /* 'v216' specific */
2022 #define LSMASH_BOX_PRECEDENCE_ISOM_CLAP (LSMASH_BOX_PRECEDENCE_LP +  1 * LSMASH_BOX_PRECEDENCE_S)
2023 #define LSMASH_BOX_PRECEDENCE_ISOM_PASP (LSMASH_BOX_PRECEDENCE_LP -  0 * LSMASH_BOX_PRECEDENCE_S)
2024 #define LSMASH_BOX_PRECEDENCE_ISOM_STSL (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2025 #define LSMASH_BOX_PRECEDENCE_ISOM_CHAN (LSMASH_BOX_PRECEDENCE_LP -  0 * LSMASH_BOX_PRECEDENCE_S)
2026 #define LSMASH_BOX_PRECEDENCE_QTFF_CHAN (LSMASH_BOX_PRECEDENCE_LP -  0 * LSMASH_BOX_PRECEDENCE_S)
2027 #define LSMASH_BOX_PRECEDENCE_QTFF_WAVE (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2028 #define LSMASH_BOX_PRECEDENCE_QTFF_FRMA (LSMASH_BOX_PRECEDENCE_HM +  1 * LSMASH_BOX_PRECEDENCE_S)   /* precede any as much as possible */
2029 #define LSMASH_BOX_PRECEDENCE_QTFF_ENDA (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2030 #define LSMASH_BOX_PRECEDENCE_QTFF_MP4A (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2031 #define LSMASH_BOX_PRECEDENCE_QTFF_TERMINATOR (LSMASH_BOX_PRECEDENCE_L - 0 * LSMASH_BOX_PRECEDENCE_S)
2032 #define LSMASH_BOX_PRECEDENCE_ISOM_SRAT (LSMASH_BOX_PRECEDENCE_LP -  1 * LSMASH_BOX_PRECEDENCE_S)   /* place at the end for maximum compatibility */
2033 #define LSMASH_BOX_PRECEDENCE_ISOM_FTAB (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2034 #define LSMASH_BOX_PRECEDENCE_ISOM_STTS (LSMASH_BOX_PRECEDENCE_N  -  2 * LSMASH_BOX_PRECEDENCE_S)
2035 #define LSMASH_BOX_PRECEDENCE_ISOM_CTTS (LSMASH_BOX_PRECEDENCE_N  -  4 * LSMASH_BOX_PRECEDENCE_S)
2036 #define LSMASH_BOX_PRECEDENCE_ISOM_CSLG (LSMASH_BOX_PRECEDENCE_N  -  6 * LSMASH_BOX_PRECEDENCE_S)
2037 #define LSMASH_BOX_PRECEDENCE_ISOM_STSS (LSMASH_BOX_PRECEDENCE_N  -  8 * LSMASH_BOX_PRECEDENCE_S)
2038 #define LSMASH_BOX_PRECEDENCE_QTFF_STPS (LSMASH_BOX_PRECEDENCE_N  - 10 * LSMASH_BOX_PRECEDENCE_S)
2039 #define LSMASH_BOX_PRECEDENCE_ISOM_SDTP (LSMASH_BOX_PRECEDENCE_N  - 12 * LSMASH_BOX_PRECEDENCE_S)
2040 #define LSMASH_BOX_PRECEDENCE_ISOM_STSC (LSMASH_BOX_PRECEDENCE_N  - 14 * LSMASH_BOX_PRECEDENCE_S)
2041 #define LSMASH_BOX_PRECEDENCE_ISOM_STSZ (LSMASH_BOX_PRECEDENCE_N  - 16 * LSMASH_BOX_PRECEDENCE_S)
2042 #define LSMASH_BOX_PRECEDENCE_ISOM_STZ2 (LSMASH_BOX_PRECEDENCE_N  - 16 * LSMASH_BOX_PRECEDENCE_S)
2043 #define LSMASH_BOX_PRECEDENCE_ISOM_STCO (LSMASH_BOX_PRECEDENCE_N  - 18 * LSMASH_BOX_PRECEDENCE_S)
2044 #define LSMASH_BOX_PRECEDENCE_ISOM_CO64 (LSMASH_BOX_PRECEDENCE_N  - 18 * LSMASH_BOX_PRECEDENCE_S)
2045 #define LSMASH_BOX_PRECEDENCE_ISOM_SGPD (LSMASH_BOX_PRECEDENCE_N  - 20 * LSMASH_BOX_PRECEDENCE_S)
2046 #define LSMASH_BOX_PRECEDENCE_ISOM_SBGP (LSMASH_BOX_PRECEDENCE_N  - 22 * LSMASH_BOX_PRECEDENCE_S)
2047 #define LSMASH_BOX_PRECEDENCE_ISOM_UDTA (LSMASH_BOX_PRECEDENCE_N  -  5 * LSMASH_BOX_PRECEDENCE_S)
2048 #define LSMASH_BOX_PRECEDENCE_ISOM_MEAN (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2049 #define LSMASH_BOX_PRECEDENCE_ISOM_NAME (LSMASH_BOX_PRECEDENCE_N  -  1 * LSMASH_BOX_PRECEDENCE_S)
2050 #define LSMASH_BOX_PRECEDENCE_ISOM_DATA (LSMASH_BOX_PRECEDENCE_N  -  2 * LSMASH_BOX_PRECEDENCE_S)
2051 #define LSMASH_BOX_PRECEDENCE_QTFF_KEYS (LSMASH_BOX_PRECEDENCE_N  -  1 * LSMASH_BOX_PRECEDENCE_S)
2052 #define LSMASH_BOX_PRECEDENCE_ISOM_ILST (LSMASH_BOX_PRECEDENCE_N  -  2 * LSMASH_BOX_PRECEDENCE_S)
2053 #define LSMASH_BOX_PRECEDENCE_ISOM_METAITEM (LSMASH_BOX_PRECEDENCE_N - 0 * LSMASH_BOX_PRECEDENCE_S)
2054 #define LSMASH_BOX_PRECEDENCE_ISOM_CHPL (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2055 #define LSMASH_BOX_PRECEDENCE_ISOM_META (LSMASH_BOX_PRECEDENCE_N  -  7 * LSMASH_BOX_PRECEDENCE_S)
2056 #define LSMASH_BOX_PRECEDENCE_QTFF_WLOC (LSMASH_BOX_PRECEDENCE_N  -  8 * LSMASH_BOX_PRECEDENCE_S)
2057 #define LSMASH_BOX_PRECEDENCE_QTFF_LOOP (LSMASH_BOX_PRECEDENCE_N  -  9 * LSMASH_BOX_PRECEDENCE_S)
2058 #define LSMASH_BOX_PRECEDENCE_QTFF_SELO (LSMASH_BOX_PRECEDENCE_N  - 10 * LSMASH_BOX_PRECEDENCE_S)
2059 #define LSMASH_BOX_PRECEDENCE_QTFF_ALLF (LSMASH_BOX_PRECEDENCE_N  - 11 * LSMASH_BOX_PRECEDENCE_S)
2060 #define LSMASH_BOX_PRECEDENCE_ISOM_CPRT (LSMASH_BOX_PRECEDENCE_N  - 12 * LSMASH_BOX_PRECEDENCE_S)
2061 #define LSMASH_BOX_PRECEDENCE_QTFF_CTAB (LSMASH_BOX_PRECEDENCE_N  -  6 * LSMASH_BOX_PRECEDENCE_S)
2062 #define LSMASH_BOX_PRECEDENCE_ISOM_MVEX (LSMASH_BOX_PRECEDENCE_N  -  8 * LSMASH_BOX_PRECEDENCE_S)
2063 #define LSMASH_BOX_PRECEDENCE_ISOM_MEHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2064 #define LSMASH_BOX_PRECEDENCE_ISOM_TREX (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2065 #define LSMASH_BOX_PRECEDENCE_ISOM_MOOF (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2066 #define LSMASH_BOX_PRECEDENCE_ISOM_MFHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2067 #define LSMASH_BOX_PRECEDENCE_ISOM_TRAF (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2068 #define LSMASH_BOX_PRECEDENCE_ISOM_TFHD (LSMASH_BOX_PRECEDENCE_HM -  0 * LSMASH_BOX_PRECEDENCE_S)
2069 #define LSMASH_BOX_PRECEDENCE_ISOM_TFDT (LSMASH_BOX_PRECEDENCE_HM -  1 * LSMASH_BOX_PRECEDENCE_S)   /* shall be positioned after 'tfhd' and before 'trun' */
2070 #define LSMASH_BOX_PRECEDENCE_ISOM_TRUN (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2071 #define LSMASH_BOX_PRECEDENCE_ISOM_MFRA (LSMASH_BOX_PRECEDENCE_L  -  0 * LSMASH_BOX_PRECEDENCE_S)
2072 #define LSMASH_BOX_PRECEDENCE_ISOM_TFRA (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2073 #define LSMASH_BOX_PRECEDENCE_ISOM_MFRO (LSMASH_BOX_PRECEDENCE_L  -  0 * LSMASH_BOX_PRECEDENCE_S)
2074 #define LSMASH_BOX_PRECEDENCE_ISOM_MDAT (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2075 #define LSMASH_BOX_PRECEDENCE_ISOM_FREE (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2076 #define LSMASH_BOX_PRECEDENCE_ISOM_SKIP (LSMASH_BOX_PRECEDENCE_N  -  0 * LSMASH_BOX_PRECEDENCE_S)
2077 
2078 /* Track reference types */
2079 typedef enum
2080 {
2081     ISOM_TREF_TYPE_AVCP = LSMASH_4CC( 'a', 'v', 'c', 'p' ),   /* AVC parameter set stream link */
2082     ISOM_TREF_TYPE_CDSC = LSMASH_4CC( 'c', 'd', 's', 'c' ),   /* This track describes the referenced track. */
2083     ISOM_TREF_TYPE_DPND = LSMASH_4CC( 'd', 'p', 'n', 'd' ),   /* This track has an MPEG-4 dependency on the referenced track. */
2084     ISOM_TREF_TYPE_HIND = LSMASH_4CC( 'h', 'i', 'n', 'd' ),   /* Hint dependency */
2085     ISOM_TREF_TYPE_HINT = LSMASH_4CC( 'h', 'i', 'n', 't' ),   /* Links hint track to original media track */
2086     ISOM_TREF_TYPE_IPIR = LSMASH_4CC( 'i', 'p', 'i', 'r' ),   /* This track contains IPI declarations for the referenced track. */
2087     ISOM_TREF_TYPE_MPOD = LSMASH_4CC( 'm', 'p', 'o', 'd' ),   /* This track is an OD track which uses the referenced track as an included elementary stream track. */
2088     ISOM_TREF_TYPE_SBAS = LSMASH_4CC( 's', 'b', 'a', 's' ),   /* Scalable base */
2089     ISOM_TREF_TYPE_SCAL = LSMASH_4CC( 's', 'c', 'a', 'l' ),   /* Scalable extraction */
2090     ISOM_TREF_TYPE_SWFR = LSMASH_4CC( 's', 'w', 'f', 'r' ),   /* AVC Switch from */
2091     ISOM_TREF_TYPE_SWTO = LSMASH_4CC( 's', 'w', 't', 'o' ),   /* AVC Switch to */
2092     ISOM_TREF_TYPE_SYNC = LSMASH_4CC( 's', 'y', 'n', 'c' ),   /* This track uses the referenced track as its synchronization source. */
2093     ISOM_TREF_TYPE_VDEP = LSMASH_4CC( 'v', 'd', 'e', 'p' ),   /* Auxiliary video depth */
2094     ISOM_TREF_TYPE_VPLX = LSMASH_4CC( 'v', 'p', 'l', 'x' ),   /* Auxiliary video parallax */
2095 
2096     QT_TREF_TYPE_CHAP   = LSMASH_4CC( 'c', 'h', 'a', 'p' ),   /* Chapter or scene list. Usually references a text track. */
2097     QT_TREF_TYPE_SCPT   = LSMASH_4CC( 's', 'c', 'p', 't' ),   /* Transcript. Usually references a text track. */
2098     QT_TREF_TYPE_SSRC   = LSMASH_4CC( 's', 's', 'r', 'c' ),   /* Nonprimary source. Indicates that the referenced track should send its data to this track, rather than presenting it. */
2099     QT_TREF_TYPE_TMCD   = LSMASH_4CC( 't', 'm', 'c', 'd' ),   /* Time code. Usually references a time code track. */
2100 } isom_track_reference_type;
2101 
2102 /* Handler types */
2103 enum isom_handler_type
2104 {
2105     QT_HANDLER_TYPE_DATA    = LSMASH_4CC( 'd', 'h', 'l', 'r' ),
2106     QT_HANDLER_TYPE_MEDIA   = LSMASH_4CC( 'm', 'h', 'l', 'r' ),
2107 };
2108 
2109 enum isom_meta_type
2110 {
2111     ISOM_META_HANDLER_TYPE_ITUNES_METADATA = LSMASH_4CC( 'm', 'd', 'i', 'r' ),
2112 };
2113 
2114 /* Data reference types */
2115 enum isom_data_reference_type
2116 {
2117     ISOM_REFERENCE_HANDLER_TYPE_URL     = LSMASH_4CC( 'u', 'r', 'l', ' ' ),
2118     ISOM_REFERENCE_HANDLER_TYPE_URN     = LSMASH_4CC( 'u', 'r', 'n', ' ' ),
2119 
2120     QT_REFERENCE_HANDLER_TYPE_ALIAS     = LSMASH_4CC( 'a', 'l', 'i', 's' ),
2121     QT_REFERENCE_HANDLER_TYPE_RESOURCE  = LSMASH_4CC( 'r', 's', 'r', 'c' ),
2122     QT_REFERENCE_HANDLER_TYPE_URL       = LSMASH_4CC( 'u', 'r', 'l', ' ' ),
2123 };
2124 
2125 /* Lanuage codes */
2126 typedef struct
2127 {
2128     uint16_t mac_value;
2129     uint16_t iso_name;
2130 } isom_language_t;
2131 
2132 static const isom_language_t isom_languages[] =
2133 {
2134     {   0, ISOM_LANGUAGE_CODE_ENGLISH },
2135     {   1, ISOM_LANGUAGE_CODE_FRENCH },
2136     {   2, ISOM_LANGUAGE_CODE_GERMAN },
2137     {   3, ISOM_LANGUAGE_CODE_ITALIAN },
2138     {   4, ISOM_LANGUAGE_CODE_DUTCH_M },
2139     {   5, ISOM_LANGUAGE_CODE_SWEDISH },
2140     {   6, ISOM_LANGUAGE_CODE_SPANISH },
2141     {   7, ISOM_LANGUAGE_CODE_DANISH },
2142     {   8, ISOM_LANGUAGE_CODE_PORTUGUESE },
2143     {   9, ISOM_LANGUAGE_CODE_NORWEGIAN },
2144     {  10, ISOM_LANGUAGE_CODE_HEBREW },
2145     {  11, ISOM_LANGUAGE_CODE_JAPANESE },
2146     {  12, ISOM_LANGUAGE_CODE_ARABIC },
2147     {  13, ISOM_LANGUAGE_CODE_FINNISH },
2148     {  14, ISOM_LANGUAGE_CODE_GREEK },
2149     {  15, ISOM_LANGUAGE_CODE_ICELANDIC },
2150     {  16, ISOM_LANGUAGE_CODE_MALTESE },
2151     {  17, ISOM_LANGUAGE_CODE_TURKISH },
2152     {  18, ISOM_LANGUAGE_CODE_CROATIAN },
2153     {  19, ISOM_LANGUAGE_CODE_CHINESE },
2154     {  20, ISOM_LANGUAGE_CODE_URDU },
2155     {  21, ISOM_LANGUAGE_CODE_HINDI },
2156     {  22, ISOM_LANGUAGE_CODE_THAI },
2157     {  23, ISOM_LANGUAGE_CODE_KOREAN },
2158     {  24, ISOM_LANGUAGE_CODE_LITHUANIAN },
2159     {  25, ISOM_LANGUAGE_CODE_POLISH },
2160     {  26, ISOM_LANGUAGE_CODE_HUNGARIAN },
2161     {  27, ISOM_LANGUAGE_CODE_ESTONIAN },
2162     {  28, ISOM_LANGUAGE_CODE_LATVIAN },
2163     {  29, ISOM_LANGUAGE_CODE_SAMI },
2164     {  30, ISOM_LANGUAGE_CODE_FAROESE },
2165     {  32, ISOM_LANGUAGE_CODE_RUSSIAN },
2166     {  33, ISOM_LANGUAGE_CODE_CHINESE },
2167     {  34, ISOM_LANGUAGE_CODE_DUTCH },
2168     {  35, ISOM_LANGUAGE_CODE_IRISH },
2169     {  36, ISOM_LANGUAGE_CODE_ALBANIAN },
2170     {  37, ISOM_LANGUAGE_CODE_ROMANIAN },
2171     {  38, ISOM_LANGUAGE_CODE_CZECH },
2172     {  39, ISOM_LANGUAGE_CODE_SLOVAK },
2173     {  40, ISOM_LANGUAGE_CODE_SLOVENIA },
2174     {  41, ISOM_LANGUAGE_CODE_YIDDISH },
2175     {  42, ISOM_LANGUAGE_CODE_SERBIAN },
2176     {  43, ISOM_LANGUAGE_CODE_MACEDONIAN },
2177     {  44, ISOM_LANGUAGE_CODE_BULGARIAN },
2178     {  45, ISOM_LANGUAGE_CODE_UKRAINIAN },
2179     {  46, ISOM_LANGUAGE_CODE_BELARUSIAN },
2180     {  47, ISOM_LANGUAGE_CODE_UZBEK },
2181     {  48, ISOM_LANGUAGE_CODE_KAZAKH },
2182     {  49, ISOM_LANGUAGE_CODE_AZERBAIJANI },
2183     {  51, ISOM_LANGUAGE_CODE_ARMENIAN },
2184     {  52, ISOM_LANGUAGE_CODE_GEORGIAN },
2185     {  53, ISOM_LANGUAGE_CODE_MOLDAVIAN },
2186     {  54, ISOM_LANGUAGE_CODE_KIRGHIZ },
2187     {  55, ISOM_LANGUAGE_CODE_TAJIK },
2188     {  56, ISOM_LANGUAGE_CODE_TURKMEN },
2189     {  57, ISOM_LANGUAGE_CODE_MONGOLIAN },
2190     {  59, ISOM_LANGUAGE_CODE_PASHTO },
2191     {  60, ISOM_LANGUAGE_CODE_KURDISH },
2192     {  61, ISOM_LANGUAGE_CODE_KASHMIRI },
2193     {  62, ISOM_LANGUAGE_CODE_SINDHI },
2194     {  63, ISOM_LANGUAGE_CODE_TIBETAN },
2195     {  64, ISOM_LANGUAGE_CODE_NEPALI },
2196     {  65, ISOM_LANGUAGE_CODE_SANSKRIT },
2197     {  66, ISOM_LANGUAGE_CODE_MARATHI },
2198     {  67, ISOM_LANGUAGE_CODE_BENGALI },
2199     {  68, ISOM_LANGUAGE_CODE_ASSAMESE },
2200     {  69, ISOM_LANGUAGE_CODE_GUJARATI },
2201     {  70, ISOM_LANGUAGE_CODE_PUNJABI },
2202     {  71, ISOM_LANGUAGE_CODE_ORIYA },
2203     {  72, ISOM_LANGUAGE_CODE_MALAYALAM },
2204     {  73, ISOM_LANGUAGE_CODE_KANNADA },
2205     {  74, ISOM_LANGUAGE_CODE_TAMIL },
2206     {  75, ISOM_LANGUAGE_CODE_TELUGU },
2207     {  76, ISOM_LANGUAGE_CODE_SINHALESE },
2208     {  77, ISOM_LANGUAGE_CODE_BURMESE },
2209     {  78, ISOM_LANGUAGE_CODE_KHMER },
2210     {  79, ISOM_LANGUAGE_CODE_LAO },
2211     {  80, ISOM_LANGUAGE_CODE_VIETNAMESE },
2212     {  81, ISOM_LANGUAGE_CODE_INDONESIAN },
2213     {  82, ISOM_LANGUAGE_CODE_TAGALOG },
2214     {  83, ISOM_LANGUAGE_CODE_MALAY_ROMAN },
2215     {  84, ISOM_LANGUAGE_CODE_MAYAY_ARABIC },
2216     {  85, ISOM_LANGUAGE_CODE_AMHARIC },
2217     {  87, ISOM_LANGUAGE_CODE_OROMO },
2218     {  88, ISOM_LANGUAGE_CODE_SOMALI },
2219     {  89, ISOM_LANGUAGE_CODE_SWAHILI },
2220     {  90, ISOM_LANGUAGE_CODE_KINYARWANDA },
2221     {  91, ISOM_LANGUAGE_CODE_RUNDI },
2222     {  92, ISOM_LANGUAGE_CODE_CHEWA },
2223     {  93, ISOM_LANGUAGE_CODE_MALAGASY },
2224     {  94, ISOM_LANGUAGE_CODE_ESPERANTO },
2225     { 128, ISOM_LANGUAGE_CODE_WELSH },
2226     { 129, ISOM_LANGUAGE_CODE_BASQUE },
2227     { 130, ISOM_LANGUAGE_CODE_CATALAN },
2228     { 131, ISOM_LANGUAGE_CODE_LATIN },
2229     { 132, ISOM_LANGUAGE_CODE_QUECHUA },
2230     { 133, ISOM_LANGUAGE_CODE_GUARANI },
2231     { 134, ISOM_LANGUAGE_CODE_AYMARA },
2232     { 135, ISOM_LANGUAGE_CODE_TATAR },
2233     { 136, ISOM_LANGUAGE_CODE_UIGHUR },
2234     { 137, ISOM_LANGUAGE_CODE_DZONGKHA },
2235     { 138, ISOM_LANGUAGE_CODE_JAVANESE },
2236     { UINT16_MAX, 0 }
2237 };
2238 
2239 /* Color parameters */
2240 enum isom_color_patameter_type
2241 {
2242     ISOM_COLOR_PARAMETER_TYPE_NCLX = LSMASH_4CC( 'n', 'c', 'l', 'x' ),      /* on-screen colours */
2243     ISOM_COLOR_PARAMETER_TYPE_RICC = LSMASH_4CC( 'r', 'I', 'C', 'C' ),      /* restricted ICC profile */
2244     ISOM_COLOR_PARAMETER_TYPE_PROF = LSMASH_4CC( 'p', 'r', 'o', 'f' ),      /* unrestricted ICC profile */
2245 
2246     QT_COLOR_PARAMETER_TYPE_NCLC   = LSMASH_4CC( 'n', 'c', 'l', 'c' ),      /* NonConstant Luminance Coding */
2247     QT_COLOR_PARAMETER_TYPE_PROF   = LSMASH_4CC( 'p', 'r', 'o', 'f' ),      /* ICC profile */
2248 };
2249 
2250 /* Sample grouping types */
2251 typedef enum
2252 {
2253     ISOM_GROUP_TYPE_3GAG = LSMASH_4CC( '3', 'g', 'a', 'g' ),      /* Text track3GPP PSS Annex G video buffer parameters */
2254     ISOM_GROUP_TYPE_ALST = LSMASH_4CC( 'a', 'l', 's', 't' ),      /* Alternative startup sequence */
2255     ISOM_GROUP_TYPE_AVCB = LSMASH_4CC( 'a', 'v', 'c', 'b' ),      /* AVC HRD parameters */
2256     ISOM_GROUP_TYPE_AVLL = LSMASH_4CC( 'a', 'v', 'l', 'l' ),      /* AVC Layer */
2257     ISOM_GROUP_TYPE_AVSS = LSMASH_4CC( 'a', 'v', 's', 's' ),      /* AVC Sub Sequence */
2258     ISOM_GROUP_TYPE_DTRT = LSMASH_4CC( 'd', 't', 'r', 't' ),      /* Decode re-timing */
2259     ISOM_GROUP_TYPE_MVIF = LSMASH_4CC( 'm', 'v', 'i', 'f' ),      /* MVC Scalability Information */
2260     ISOM_GROUP_TYPE_PROL = LSMASH_4CC( 'p', 'r', 'o', 'l' ),      /* Pre-roll */
2261     ISOM_GROUP_TYPE_RAP  = LSMASH_4CC( 'r', 'a', 'p', ' ' ),      /* Random Access Point */
2262     ISOM_GROUP_TYPE_RASH = LSMASH_4CC( 'r', 'a', 's', 'h' ),      /* Rate Share */
2263     ISOM_GROUP_TYPE_ROLL = LSMASH_4CC( 'r', 'o', 'l', 'l' ),      /* Pre-roll/Post-roll */
2264     ISOM_GROUP_TYPE_SCIF = LSMASH_4CC( 's', 'c', 'i', 'f' ),      /* SVC Scalability Information */
2265     ISOM_GROUP_TYPE_SCNM = LSMASH_4CC( 's', 'c', 'n', 'm' ),      /* AVC/SVC/MVC map groups */
2266     ISOM_GROUP_TYPE_VIPR = LSMASH_4CC( 'v', 'i', 'p', 'r' ),      /* View priority */
2267 } isom_grouping_type;
2268 
2269 /* wrapper to avoid boring cast */
2270 #define isom_init_box_common( box, parent, box_type, precedence, destructor ) \
2271         isom_init_box_common_orig( box, parent, box_type, precedence, (isom_extension_destructor_t)(destructor) )
2272 
2273 void isom_init_box_common_orig
2274 (
2275     void                       *box,
2276     void                       *parent,
2277     lsmash_box_type_t           box_type,
2278     uint64_t                    precedence,
2279     isom_extension_destructor_t destructor
2280 );
2281 
2282 int isom_is_fullbox( const void *box );
2283 int isom_is_lpcm_audio( const void *box );
2284 int isom_is_qt_audio( lsmash_codec_type_t type );
2285 int isom_is_uncompressed_ycbcr( lsmash_codec_type_t type );
2286 int isom_is_waveform_audio( lsmash_box_type_t type );
2287 
2288 size_t isom_skip_box_common
2289 (
2290     uint8_t **p_data
2291 );
2292 
2293 uint8_t *isom_get_child_box_position
2294 (
2295     uint8_t           *parent_data,
2296     uint32_t          parent_size,
2297     lsmash_box_type_t child_type,
2298     uint32_t         *child_size
2299 );
2300 
2301 void isom_bs_put_basebox_common( lsmash_bs_t *bs, isom_box_t *box );
2302 void isom_bs_put_fullbox_common( lsmash_bs_t *bs, isom_box_t *box );
2303 void isom_bs_put_box_common( lsmash_bs_t *bs, void *box );
2304 
2305 #define isom_is_printable_char( c ) ((c) >= 32 && (c) < 128)
2306 #define isom_is_printable_4cc( fourcc )                \
2307     (isom_is_printable_char( ((fourcc) >> 24) & 0xff ) \
2308   && isom_is_printable_char( ((fourcc) >> 16) & 0xff ) \
2309   && isom_is_printable_char( ((fourcc) >>  8) & 0xff ) \
2310   && isom_is_printable_char(  (fourcc)        & 0xff ))
2311 
2312 #define isom_4cc2str( fourcc ) (const char [5]){ (fourcc) >> 24, (fourcc) >> 16, (fourcc) >> 8, (fourcc), 0 }
2313 
2314 int isom_check_initializer_present( lsmash_root_t *root );
2315 
2316 isom_trak_t *isom_get_trak( lsmash_file_t *file, uint32_t track_ID );
2317 isom_trex_t *isom_get_trex( isom_mvex_t *mvex, uint32_t track_ID );
2318 isom_traf_t *isom_get_traf( isom_moof_t *moof, uint32_t track_ID );
2319 isom_tfra_t *isom_get_tfra( isom_mfra_t *mfra, uint32_t track_ID );
2320 isom_sgpd_t *isom_get_sample_group_description( isom_stbl_t *stbl, uint32_t grouping_type );
2321 isom_sbgp_t *isom_get_sample_to_group( isom_stbl_t *stbl, uint32_t grouping_type );
2322 isom_sgpd_t *isom_get_roll_recovery_sample_group_description( lsmash_entry_list_t *list );
2323 isom_sbgp_t *isom_get_roll_recovery_sample_to_group( lsmash_entry_list_t *list );
2324 isom_sgpd_t *isom_get_fragment_sample_group_description( isom_traf_t *traf, uint32_t grouping_type );
2325 isom_sbgp_t *isom_get_fragment_sample_to_group( isom_traf_t *traf, uint32_t grouping_type );
2326 
2327 isom_trak_t *isom_track_create( lsmash_file_t *file, lsmash_media_type media_type );
2328 isom_moov_t *isom_movie_create( lsmash_file_t *file );
2329 
2330 int isom_setup_handler_reference( isom_hdlr_t *hdlr, uint32_t media_type );
2331 int isom_setup_iods( isom_moov_t *moov );
2332 
2333 uint32_t isom_get_sample_count
2334 (
2335     isom_trak_t *trak
2336 );
2337 
2338 isom_sample_pool_t *isom_create_sample_pool
2339 (
2340     uint64_t size
2341 );
2342 
2343 int isom_update_sample_tables
2344 (
2345     isom_trak_t         *trak,
2346     lsmash_sample_t     *sample,
2347     uint32_t            *samples_per_packet,
2348     isom_sample_entry_t *sample_entry
2349 );
2350 
2351 int isom_pool_sample
2352 (
2353     isom_sample_pool_t *pool,
2354     lsmash_sample_t    *sample,
2355     uint32_t            samples_per_packet
2356 );
2357 
2358 int isom_append_sample_by_type
2359 (
2360     void                *track,
2361     lsmash_sample_t     *sample,
2362     isom_sample_entry_t *sample_entry,
2363     int (*func_append_sample)( void *, lsmash_sample_t *, isom_sample_entry_t * )
2364 );
2365 
2366 int isom_calculate_bitrate_description
2367 (
2368     isom_stbl_t *stbl,
2369     isom_mdhd_t *mdhd,
2370     uint32_t    *bufferSizeDB,
2371     uint32_t    *maxBitrate,
2372     uint32_t    *avgBitrate,
2373     uint32_t     sample_description_index
2374 );
2375 
2376 int isom_is_variable_size
2377 (
2378     isom_stbl_t *stbl
2379 );
2380 
2381 uint32_t isom_get_first_sample_size
2382 (
2383     isom_stbl_t *stbl
2384 );
2385 
2386 
2387 void isom_update_cache_timestamp
2388 (
2389     isom_cache_t *cache,
2390     uint64_t      dts,
2391     uint64_t      cts,
2392     int32_t       ctd_shift,
2393     uint32_t      sample_duration,
2394     int           non_output_sample
2395 );
2396 
2397 /* Make CTS from DTS and sample_offset.
2398  * This function does NOT add the value of composition to decode timeline shift to the result. */
isom_make_cts(uint64_t dts,uint32_t sample_offset,int32_t ctd_shift)2399 static inline uint64_t isom_make_cts
2400 (
2401     uint64_t dts,
2402     uint32_t sample_offset,
2403     int32_t  ctd_shift
2404 )
2405 {
2406     if( sample_offset != ISOM_NON_OUTPUT_SAMPLE_OFFSET )
2407         return ctd_shift ? (dts + (int32_t)sample_offset) : (dts + sample_offset);
2408     else
2409         return LSMASH_TIMESTAMP_UNDEFINED;
2410 }
2411 
2412 /* Make CTS from DTS and sample_offset.
2413  * This function adds the value of composition to decode timeline shift to the result. */
isom_make_cts_adjust(uint64_t dts,uint32_t sample_offset,int32_t ctd_shift)2414 static inline uint64_t isom_make_cts_adjust
2415 (
2416     uint64_t dts,
2417     uint32_t sample_offset,
2418     int32_t  ctd_shift
2419 )
2420 {
2421     if( sample_offset != ISOM_NON_OUTPUT_SAMPLE_OFFSET )
2422         return ctd_shift ? (dts + (int32_t)sample_offset + ctd_shift) : (dts + sample_offset);
2423     else
2424         return LSMASH_TIMESTAMP_UNDEFINED;
2425 }
2426 
2427 /* Utilities for sample entry type decision
2428  * NOTE: This implementation does not work when 'mdia' and/or 'hdlr' is stored as binary string. */
isom_check_media_hdlr_from_stsd(isom_stsd_t * stsd)2429 static inline int isom_check_media_hdlr_from_stsd( isom_stsd_t *stsd )
2430 {
2431     return ((isom_stbl_t *)stsd->parent
2432         &&  (isom_minf_t *)stsd->parent->parent
2433         &&  (isom_mdia_t *)stsd->parent->parent->parent
2434         && ((isom_mdia_t *)stsd->parent->parent->parent)->hdlr);
2435 }
isom_get_media_type_from_stsd(isom_stsd_t * stsd)2436 static inline lsmash_media_type isom_get_media_type_from_stsd( isom_stsd_t *stsd )
2437 {
2438     assert( isom_check_media_hdlr_from_stsd( stsd ) );
2439     return ((isom_mdia_t *)stsd->parent->parent->parent)->hdlr->componentSubtype;
2440 }
2441 
2442 int isom_add_sample_grouping( isom_box_t *parent, isom_grouping_type grouping_type );
2443 int isom_group_random_access( isom_box_t *parent, isom_cache_t *cache, lsmash_sample_t *sample );
2444 int isom_group_roll_recovery( isom_box_t *parent, isom_cache_t *cache, lsmash_sample_t *sample );
2445 
2446 int isom_update_tkhd_duration( isom_trak_t *trak );
2447 int isom_update_bitrate_description( isom_mdia_t *mdia );
2448 int isom_complement_data_reference( isom_minf_t *minf );
2449 int isom_check_large_offset_requirement( isom_moov_t *moov, uint64_t meta_size );
2450 void isom_add_preceding_box_size( isom_moov_t *moov, uint64_t preceding_size );
2451 int isom_establish_movie( lsmash_file_t *file );
2452 int isom_rap_grouping_established( isom_rap_group_t *group, int num_leading_samples_known, isom_sgpd_t *sgpd, int is_fragment );
2453 int isom_all_recovery_completed( isom_sbgp_t *sbgp, lsmash_entry_list_t *pool );
2454 
2455 lsmash_file_t *isom_add_file_abstract( lsmash_root_t *root );
2456 isom_ftyp_t *isom_add_ftyp( lsmash_file_t *file );
2457 isom_moov_t *isom_add_moov( lsmash_file_t *file );
2458 isom_mvhd_t *isom_add_mvhd( isom_moov_t *moov );
2459 isom_iods_t *isom_add_iods( isom_moov_t *moov );
2460 isom_ctab_t *isom_add_ctab( void *parent_box );
2461 isom_trak_t *isom_add_trak( isom_moov_t *moov );
2462 isom_tkhd_t *isom_add_tkhd( isom_trak_t *trak );
2463 isom_tapt_t *isom_add_tapt( isom_trak_t *trak );
2464 isom_clef_t *isom_add_clef( isom_tapt_t *tapt );
2465 isom_prof_t *isom_add_prof( isom_tapt_t *tapt );
2466 isom_enof_t *isom_add_enof( isom_tapt_t *tapt );
2467 isom_edts_t *isom_add_edts( isom_trak_t *trak );
2468 isom_elst_t *isom_add_elst( isom_edts_t *edts );
2469 isom_tref_t *isom_add_tref( isom_trak_t *trak );
2470 isom_tref_type_t *isom_add_track_reference_type( isom_tref_t *tref, isom_track_reference_type type );
2471 isom_mdia_t *isom_add_mdia( isom_trak_t *trak );
2472 isom_mdhd_t *isom_add_mdhd( isom_mdia_t *mdia );
2473 isom_hdlr_t *isom_add_hdlr( void *parent_box );
2474 isom_minf_t *isom_add_minf( isom_mdia_t *mdia );
2475 isom_vmhd_t *isom_add_vmhd( isom_minf_t *minf );
2476 isom_smhd_t *isom_add_smhd( isom_minf_t *minf );
2477 isom_hmhd_t *isom_add_hmhd( isom_minf_t *minf );
2478 isom_nmhd_t *isom_add_nmhd( isom_minf_t *minf );
2479 isom_gmhd_t *isom_add_gmhd( isom_minf_t *minf );
2480 isom_gmin_t *isom_add_gmin( isom_gmhd_t *gmhd );
2481 isom_text_t *isom_add_text( isom_gmhd_t *gmhd );
2482 isom_dinf_t *isom_add_dinf( void *parent_box );
2483 isom_dref_t *isom_add_dref( isom_dinf_t *dinf );
2484 isom_dref_entry_t *isom_add_dref_entry( isom_dref_t *dref, lsmash_box_type_t type );
2485 isom_stbl_t *isom_add_stbl( isom_minf_t *minf );
2486 isom_stsd_t *isom_add_stsd( isom_stbl_t *stbl );
2487 isom_visual_entry_t *isom_add_visual_description( isom_stsd_t *stsd, lsmash_codec_type_t sample_type );
2488 isom_audio_entry_t *isom_add_audio_description( isom_stsd_t *stsd, lsmash_codec_type_t sample_type );
2489 isom_qt_text_entry_t *isom_add_qt_text_description( isom_stsd_t *stsd );
2490 isom_tx3g_entry_t *isom_add_tx3g_description( isom_stsd_t *stsd );
2491 isom_esds_t *isom_add_esds( void *parent_box );
2492 isom_glbl_t *isom_add_glbl( void *parent_box );
2493 isom_clap_t *isom_add_clap( isom_visual_entry_t *visual );
2494 isom_pasp_t *isom_add_pasp( isom_visual_entry_t *visual );
2495 isom_colr_t *isom_add_colr( isom_visual_entry_t *visual );
2496 isom_gama_t *isom_add_gama( isom_visual_entry_t *visual );
2497 isom_fiel_t *isom_add_fiel( isom_visual_entry_t *visual );
2498 isom_cspc_t *isom_add_cspc( isom_visual_entry_t *visual );
2499 isom_sgbt_t *isom_add_sgbt( isom_visual_entry_t *visual );
2500 isom_stsl_t *isom_add_stsl( isom_visual_entry_t *visual );
2501 isom_btrt_t *isom_add_btrt( isom_visual_entry_t *visual );
2502 isom_wave_t *isom_add_wave( isom_audio_entry_t *audio );
2503 isom_frma_t *isom_add_frma( isom_wave_t *wave );
2504 isom_enda_t *isom_add_enda( isom_wave_t *wave );
2505 isom_mp4a_t *isom_add_mp4a( isom_wave_t *wave );
2506 isom_terminator_t *isom_add_terminator( isom_wave_t *wave );
2507 isom_chan_t *isom_add_chan( isom_audio_entry_t *audio );
2508 isom_srat_t *isom_add_srat( isom_audio_entry_t *audio );
2509 isom_ftab_t *isom_add_ftab( isom_tx3g_entry_t *tx3g );
2510 isom_stts_t *isom_add_stts( isom_stbl_t *stbl );
2511 isom_ctts_t *isom_add_ctts( isom_stbl_t *stbl );
2512 isom_cslg_t *isom_add_cslg( isom_stbl_t *stbl );
2513 isom_stsc_t *isom_add_stsc( isom_stbl_t *stbl );
2514 isom_stsz_t *isom_add_stsz( isom_stbl_t *stbl );
2515 isom_stz2_t *isom_add_stz2( isom_stbl_t *stbl );
2516 isom_stss_t *isom_add_stss( isom_stbl_t *stbl );
2517 isom_stps_t *isom_add_stps( isom_stbl_t *stbl );
2518 isom_sdtp_t *isom_add_sdtp( isom_box_t *parent );
2519 isom_sgpd_t *isom_add_sgpd( void *parent_box );
2520 isom_sbgp_t *isom_add_sbgp( void *parent_box );
2521 isom_stco_t *isom_add_stco( isom_stbl_t *stbl );
2522 isom_stco_t *isom_add_co64( isom_stbl_t *stbl );
2523 isom_udta_t *isom_add_udta( void *parent_box );
2524 isom_cprt_t *isom_add_cprt( isom_udta_t *udta );
2525 isom_WLOC_t *isom_add_WLOC( isom_udta_t *udta );
2526 isom_LOOP_t *isom_add_LOOP( isom_udta_t *udta );
2527 isom_SelO_t *isom_add_SelO( isom_udta_t *udta );
2528 isom_AllF_t *isom_add_AllF( isom_udta_t *udta );
2529 isom_chpl_t *isom_add_chpl( isom_udta_t *udta );
2530 isom_meta_t *isom_add_meta( void *parent_box );
2531 isom_keys_t *isom_add_keys( isom_meta_t *meta );
2532 isom_ilst_t *isom_add_ilst( isom_meta_t *meta );
2533 isom_metaitem_t *isom_add_metaitem( isom_ilst_t *ilst, lsmash_itunes_metadata_item item );
2534 isom_mean_t *isom_add_mean( isom_metaitem_t *metaitem );
2535 isom_name_t *isom_add_name( isom_metaitem_t *metaitem );
2536 isom_data_t *isom_add_data( isom_metaitem_t *metaitem );
2537 isom_mvex_t *isom_add_mvex( isom_moov_t *moov );
2538 isom_mehd_t *isom_add_mehd( isom_mvex_t *mvex );
2539 isom_trex_t *isom_add_trex( isom_mvex_t *mvex );
2540 isom_moof_t *isom_add_moof( lsmash_file_t *file );
2541 isom_mfhd_t *isom_add_mfhd( isom_moof_t *moof );
2542 isom_traf_t *isom_add_traf( isom_moof_t *moof );
2543 isom_tfhd_t *isom_add_tfhd( isom_traf_t *traf );
2544 isom_tfdt_t *isom_add_tfdt( isom_traf_t *traf );
2545 isom_trun_t *isom_add_trun( isom_traf_t *traf );
2546 isom_mfra_t *isom_add_mfra( lsmash_file_t *file );
2547 isom_tfra_t *isom_add_tfra( isom_mfra_t *mfra );
2548 isom_mfro_t *isom_add_mfro( isom_mfra_t *mfra );
2549 isom_mdat_t *isom_add_mdat( lsmash_file_t *file );
2550 isom_free_t *isom_add_free( void *parent_box );
2551 isom_styp_t *isom_add_styp( lsmash_file_t *file );
2552 isom_sidx_t *isom_add_sidx( lsmash_file_t *file );
2553 
2554 void isom_remove_extension_box( isom_box_t *ext );
2555 void isom_remove_sample_description( isom_sample_entry_t *sample );
2556 void isom_remove_unknown_box( isom_unknown_box_t *unknown_box );
2557 void isom_remove_sample_pool( isom_sample_pool_t *pool );
2558 
2559 uint64_t isom_update_box_size( void *box );
2560 
2561 int isom_add_extension_binary( void *parent_box, lsmash_box_type_t box_type, uint64_t precedence, uint8_t *box_data, uint32_t box_size );
2562 void isom_remove_all_extension_boxes( lsmash_entry_list_t *extensions );
2563 isom_box_t *isom_get_extension_box( lsmash_entry_list_t *extensions, lsmash_box_type_t box_type );
2564 void *isom_get_extension_box_format( lsmash_entry_list_t *extensions, lsmash_box_type_t box_type );
2565 void isom_remove_box_by_itself( void *opaque_box );
2566 
2567 #endif
2568