• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Quicktime muxer plugin for GStreamer
2  * Copyright (C) 2008-2010 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /*
20  * Unless otherwise indicated, Source Code is licensed under MIT license.
21  * See further explanation attached in License Statement (distributed in the file
22  * LICENSE).
23  *
24  * Permission is hereby granted, free of charge, to any person obtaining a copy of
25  * this software and associated documentation files (the "Software"), to deal in
26  * the Software without restriction, including without limitation the rights to
27  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28  * of the Software, and to permit persons to whom the Software is furnished to do
29  * so, subject to the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be included in all
32  * copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40  * SOFTWARE.
41  */
42 
43 #ifndef __ATOMS_H__
44 #define __ATOMS_H__
45 
46 #include <glib.h>
47 #include <string.h>
48 #include <gst/video/video.h>
49 
50 #include "descriptors.h"
51 #include "properties.h"
52 #include "fourcc.h"
53 
54 /* helper storage struct */
55 #define ATOM_ARRAY(struct_type) \
56 struct { \
57   guint size; \
58   guint len; \
59   struct_type *data; \
60 }
61 
62 /* storage helpers */
63 
64 #define atom_array_init(array, reserve)                                       \
65 G_STMT_START {                                                                \
66   (array)->len = 0;                                                           \
67   (array)->size = reserve;                                                    \
68   (array)->data = g_malloc (sizeof (*(array)->data) * reserve);               \
69 } G_STMT_END
70 
71 #define atom_array_append(array, elmt, inc)                                   \
72 G_STMT_START {                                                                \
73   g_assert ((array)->data);                                                   \
74   g_assert (inc > 0);                                                         \
75   if (G_UNLIKELY ((array)->len == (array)->size)) {                           \
76     (array)->size += inc;                                                     \
77     (array)->data =                                                           \
78         g_realloc ((array)->data, sizeof (*((array)->data)) * (array)->size); \
79   }                                                                           \
80   (array)->data[(array)->len] = elmt;                                         \
81   (array)->len++;                                                             \
82 } G_STMT_END
83 
84 #define atom_array_get_len(array)                  ((array)->len)
85 #define atom_array_index(array, index)             ((array)->data[index])
86 
87 #define atom_array_clear(array)                                               \
88 G_STMT_START {                                                                \
89   (array)->size = (array)->len = 0;                                           \
90   g_free ((array)->data);                                                     \
91   (array)->data = NULL;                                                       \
92 } G_STMT_END
93 
94 /* light-weight context that may influence header atom tree construction */
95 typedef enum _AtomsTreeFlavor
96 {
97   ATOMS_TREE_FLAVOR_MOV,
98   ATOMS_TREE_FLAVOR_ISOM,
99   ATOMS_TREE_FLAVOR_3GP,
100   ATOMS_TREE_FLAVOR_ISML
101 } AtomsTreeFlavor;
102 
103 typedef struct _AtomsContext
104 {
105   AtomsTreeFlavor flavor;
106 } AtomsContext;
107 
108 AtomsContext* atoms_context_new  (AtomsTreeFlavor flavor);
109 void          atoms_context_free (AtomsContext *context);
110 
111 #define METADATA_DATA_FLAG 0x0
112 #define METADATA_TEXT_FLAG 0x1
113 
114 /* atom defs and functions */
115 
116 typedef struct _AtomInfo AtomInfo;
117 
118 /*
119  * Used for storing time related values for some atoms.
120  */
121 typedef struct _TimeInfo
122 {
123   guint64 creation_time;
124   guint64 modification_time;
125   guint32 timescale;
126   guint64 duration;
127 } TimeInfo;
128 
129 typedef struct _Atom
130 {
131   guint32 size;
132   guint32 type;
133   guint64 extended_size;
134 } Atom;
135 
136 typedef struct _AtomFull
137 {
138   Atom header;
139 
140   guint8 version;
141   guint8 flags[3];
142 } AtomFull;
143 
144 /*
145  * Generic extension atom
146  */
147 typedef struct _AtomData
148 {
149   Atom header;
150 
151   /* not written */
152   guint32 datalen;
153 
154   guint8 *data;
155 } AtomData;
156 
157 typedef struct _AtomUUID
158 {
159   Atom header;
160 
161   guint8 uuid[16];
162 
163   /* not written */
164   guint32 datalen;
165 
166   guint8 *data;
167 } AtomUUID;
168 
169 typedef struct _AtomFTYP
170 {
171   Atom header;
172   guint32 major_brand;
173   guint32 version;
174   guint32 *compatible_brands;
175 
176   /* not written */
177   guint32 compatible_brands_size;
178 } AtomFTYP;
179 
180 typedef struct _AtomMVHD
181 {
182   AtomFull header;
183 
184   /* version 0: 32 bits */
185   TimeInfo time_info;
186 
187   guint32 prefered_rate;      /* ISO: 0x00010000 */
188   guint16 volume;             /* ISO: 0x0100 */
189   guint16 reserved3;          /* ISO: 0x0 */
190   guint32 reserved4[2];       /* ISO: 0, 0 */
191   /* ISO: identity matrix =
192    * { 0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000 } */
193   guint32 matrix[9];
194 
195   /* ISO: all 0 */
196   guint32 preview_time;
197   guint32 preview_duration;
198   guint32 poster_time;
199   guint32 selection_time;
200   guint32 selection_duration;
201   guint32 current_time;
202 
203   guint32 next_track_id;
204 } AtomMVHD;
205 
206 typedef struct _AtomTKHD
207 {
208   AtomFull header;
209 
210   /* version 0: 32 bits */
211   /* like the TimeInfo struct, but it has this track_ID inside */
212   guint64 creation_time;
213   guint64 modification_time;
214   guint32 track_ID;
215   guint32 reserved;
216   guint64 duration;
217 
218   guint32 reserved2[2];
219   guint16 layer;
220   guint16 alternate_group;
221   guint16 volume;
222   guint16 reserved3;
223 
224   /* ISO: identity matrix =
225    * { 0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000 } */
226   guint32 matrix[9];
227   guint32 width;
228   guint32 height;
229 } AtomTKHD;
230 
231 typedef struct _AtomMDHD
232 {
233   AtomFull header;
234 
235   /* version 0: 32 bits */
236   TimeInfo time_info;
237 
238   /* ISO: packed ISO-639-2/T language code (first bit must be 0) */
239   guint16 language_code;
240   /* ISO: 0 */
241   guint16 quality;
242 } AtomMDHD;
243 
244 typedef struct _AtomHDLR
245 {
246   AtomFull header;
247 
248   /* ISO: 0 */
249   guint32 component_type;
250   guint32 handler_type;
251   guint32 manufacturer;
252   guint32 flags;
253   guint32 flags_mask;
254   gchar *name;
255 
256   AtomsTreeFlavor flavor;
257 } AtomHDLR;
258 
259 typedef struct _AtomVMHD
260 {
261   AtomFull header;          /* ISO: flags = 1 */
262 
263   guint16 graphics_mode;
264   /* RGB */
265   guint16 opcolor[3];
266 } AtomVMHD;
267 
268 typedef struct _AtomSMHD
269 {
270   AtomFull header;
271 
272   guint16 balance;
273   guint16 reserved;
274 } AtomSMHD;
275 
276 typedef struct _AtomHMHD
277 {
278   AtomFull header;
279 
280   guint16 max_pdu_size;
281   guint16 avg_pdu_size;
282   guint32 max_bitrate;
283   guint32 avg_bitrate;
284   guint32 sliding_avg_bitrate;
285 } AtomHMHD;
286 
287 typedef struct _AtomTCMI
288 {
289   AtomFull header;
290 
291   guint16 text_font;
292   guint16 text_face;
293   guint16 text_size;
294   guint16 text_color[3];
295   guint16 bg_color[3];
296   gchar *font_name;
297 } AtomTCMI;
298 
299 typedef struct _AtomTMCD
300 {
301   Atom header;
302 
303   AtomTCMI tcmi;
304 } AtomTMCD;
305 
306 typedef struct _AtomGMIN
307 {
308   AtomFull header;
309 
310   guint16 graphics_mode;
311   guint16 opcolor[3];
312   guint8 balance;
313   guint8 reserved;
314 
315 } AtomGMIN;
316 
317 typedef struct _AtomGMHD
318 {
319   Atom header;
320 
321   /* Only gmin is required in a gmhd atom
322    * The other fields are optional */
323   AtomGMIN gmin;
324   AtomTMCD *tmcd;
325 
326 } AtomGMHD;
327 
328 typedef struct _AtomURL
329 {
330   AtomFull header;
331 
332   gchar *location;
333 } AtomURL;
334 
335 typedef struct _AtomDREF
336 {
337   AtomFull header;
338 
339   GList *entries;
340 } AtomDREF;
341 
342 typedef struct _AtomDINF
343 {
344   Atom header;
345 
346   AtomDREF dref;
347 } AtomDINF;
348 
349 typedef struct _STTSEntry
350 {
351   guint32 sample_count;
352   gint32 sample_delta;
353 } STTSEntry;
354 
355 typedef struct _AtomSTTS
356 {
357   AtomFull header;
358 
359   ATOM_ARRAY (STTSEntry) entries;
360 } AtomSTTS;
361 
362 typedef struct _AtomSTSS
363 {
364   AtomFull header;
365 
366   ATOM_ARRAY (guint32) entries;
367 } AtomSTSS;
368 
369 typedef struct _AtomESDS
370 {
371   AtomFull header;
372 
373   ESDescriptor es;
374 } AtomESDS;
375 
376 typedef struct _AtomFRMA
377 {
378   Atom header;
379 
380   guint32 media_type;
381 } AtomFRMA;
382 
383 typedef enum _SampleEntryKind
384 {
385   UNKNOWN,
386   AUDIO,
387   VIDEO,
388   SUBTITLE,
389   TIMECODE,
390   CLOSEDCAPTION
391 } SampleEntryKind;
392 
393 typedef struct _SampleTableEntry
394 {
395   Atom header;
396 
397   guint8 reserved[6];
398   guint16 data_reference_index;
399 
400   /* type of entry */
401   SampleEntryKind kind;
402 } SampleTableEntry;
403 
404 typedef struct _AtomHintSampleEntry
405 {
406   SampleTableEntry se;
407   guint32 size;
408   guint8 *data;
409 } AtomHintSampleEntry;
410 
411 typedef struct _SampleTableEntryMP4V
412 {
413   SampleTableEntry se;
414 
415   guint16 version;
416   guint16 revision_level;
417 
418   guint32 vendor;                 /* fourcc code */
419   guint32 temporal_quality;
420   guint32 spatial_quality;
421 
422   guint16 width;
423   guint16 height;
424 
425   guint32 horizontal_resolution;
426   guint32 vertical_resolution;
427   guint32 datasize;
428 
429   guint16 frame_count;            /* usually 1 */
430 
431   guint8 compressor[32];         /* pascal string, i.e. first byte = length */
432 
433   guint16 depth;
434   guint16 color_table_id;
435 
436   /* (optional) list of AtomInfo */
437   GList *extension_atoms;
438 } SampleTableEntryMP4V;
439 
440 typedef struct _SampleTableEntryMP4A
441 {
442   SampleTableEntry se;
443 
444   guint16 version;
445   guint16 revision_level;
446   guint32 vendor;
447 
448   guint16 channels;
449   guint16 sample_size;
450   guint16 compression_id;
451   guint16 packet_size;
452 
453   guint32 sample_rate;            /* fixed point 16.16 */
454 
455   guint32 samples_per_packet;
456   guint32 bytes_per_packet;
457   guint32 bytes_per_frame;
458   guint32 bytes_per_sample;
459 
460   /* (optional) list of AtomInfo */
461   GList *extension_atoms;
462 } SampleTableEntryMP4A;
463 
464 typedef struct _AtomNAME
465 {
466   Atom header;
467 
468   guint8 language_code;
469   gchar *name;
470 } AtomNAME;
471 
472 typedef struct _SampleTableEntryTMCD
473 {
474   SampleTableEntry se;
475 
476   guint32 tc_flags;
477   guint32 timescale;
478   guint32 frame_duration;
479   guint8 n_frames;
480 
481   AtomNAME name;
482 
483 } SampleTableEntryTMCD;
484 
485 typedef struct _SampleTableEntryTX3G
486 {
487   SampleTableEntry se;
488 
489   guint32 display_flags;
490   guint64 default_text_box;
491   guint16 font_id;
492   guint8  font_face; /* bold=0x1, italic=0x2, underline=0x4 */
493   guint8  font_size; /* should always be 0.05 multiplied by the video track header height */
494   guint32 foreground_color_rgba;
495 
496 } SampleTableEntryTX3G;
497 
498 typedef struct _AtomSTSD
499 {
500   AtomFull header;
501 
502   guint n_entries;
503   /* list of subclasses of SampleTableEntry */
504   GList *entries;
505 } AtomSTSD;
506 
507 typedef struct _AtomSTSZ
508 {
509   AtomFull header;
510 
511   guint32 sample_size;
512 
513   /* need the size here because when sample_size is constant,
514    * the list is empty */
515   guint32 table_size;
516   ATOM_ARRAY (guint32) entries;
517 } AtomSTSZ;
518 
519 typedef struct _STSCEntry
520 {
521   guint32 first_chunk;
522   guint32 samples_per_chunk;
523   guint32 sample_description_index;
524 } STSCEntry;
525 
526 typedef struct _AtomSTSC
527 {
528   AtomFull header;
529 
530   ATOM_ARRAY (STSCEntry) entries;
531 } AtomSTSC;
532 
533 /* FIXME: this can support multiple tracks */
534 typedef struct _AtomTREF
535 {
536   Atom header;
537 
538   guint32 reftype;
539   ATOM_ARRAY (guint32) entries;
540 } AtomTREF;
541 
542 /*
543  * used for both STCO and CO64
544  * if used as STCO, entries should be truncated to use only 32bits
545  */
546 typedef struct _AtomSTCO64
547 {
548   AtomFull header;
549   /* Global offset to add to entries when serialising */
550   guint32 chunk_offset;
551   ATOM_ARRAY (guint64) entries;
552 } AtomSTCO64;
553 
554 typedef struct _CTTSEntry
555 {
556   guint32 samplecount;
557   guint32 sampleoffset;
558 } CTTSEntry;
559 
560 typedef struct _AtomCTTS
561 {
562   AtomFull header;
563 
564   /* also entry count here */
565   ATOM_ARRAY (CTTSEntry) entries;
566   gboolean do_pts;
567 } AtomCTTS;
568 
569 typedef struct _AtomSVMI
570 {
571   AtomFull header;
572 
573   guint8 stereoscopic_composition_type;
574   gboolean is_left_first;
575 } AtomSVMI;
576 
577 typedef struct _AtomSTBL
578 {
579   Atom header;
580 
581   AtomSTSD stsd;
582   AtomSTTS stts;
583   AtomSTSS stss;
584   AtomSTSC stsc;
585   AtomSTSZ stsz;
586   /* NULL if not present */
587   AtomCTTS *ctts;
588   /* NULL if not present */
589   AtomSVMI *svmi;
590 
591   AtomSTCO64 stco64;
592 } AtomSTBL;
593 
594 typedef struct _AtomMINF
595 {
596   Atom header;
597 
598   /* only (exactly) one of those must be present */
599   AtomVMHD *vmhd;
600   AtomSMHD *smhd;
601   AtomHMHD *hmhd;
602   AtomGMHD *gmhd;
603 
604   AtomHDLR *hdlr;
605   AtomDINF dinf;
606   AtomSTBL stbl;
607 } AtomMINF;
608 
609 typedef struct _EditListEntry
610 {
611   /* duration in movie's timescale */
612   guint32 duration;
613   /* start time in media's timescale, -1 for empty */
614   guint32 media_time;
615   guint32 media_rate;  /* fixed point 32 bit */
616 } EditListEntry;
617 
618 typedef struct _AtomELST
619 {
620   AtomFull header;
621 
622   /* number of entries is implicit */
623   GSList *entries;
624 } AtomELST;
625 
626 typedef struct _AtomEDTS
627 {
628   Atom header;
629   AtomELST elst;
630 } AtomEDTS;
631 
632 typedef struct _AtomMDIA
633 {
634   Atom header;
635 
636   AtomMDHD mdhd;
637   AtomHDLR hdlr;
638   AtomMINF minf;
639 } AtomMDIA;
640 
641 typedef struct _AtomILST
642 {
643   Atom header;
644 
645   /* list of AtomInfo */
646   GList* entries;
647 } AtomILST;
648 
649 typedef struct _AtomTagData
650 {
651   AtomFull header;
652   guint32 reserved;
653 
654   guint32 datalen;
655   guint8* data;
656 } AtomTagData;
657 
658 typedef struct _AtomTag
659 {
660   Atom header;
661 
662   AtomTagData data;
663 } AtomTag;
664 
665 typedef struct _AtomMETA
666 {
667   AtomFull header;
668   AtomHDLR hdlr;
669   AtomILST *ilst;
670 } AtomMETA;
671 
672 typedef struct _AtomUDTA
673 {
674   Atom header;
675 
676   /* list of AtomInfo */
677   GList* entries;
678   /* or list is further down */
679   AtomMETA *meta;
680 
681   AtomsContext *context;
682 
683 /* ohos.ext.func.0016
684  * add additional features to set geographic location information in mp4 file
685  * set_location is the flag to enable this feature
686  * latitude is the latitude to set, multiply 10000 for the convenience of calculation.
687  * longitude is the longitude to set, multiply 10000 for the convenience of calculation.
688  */
689 #ifdef OHOS_EXT_FUNC
690   gboolean set_location;
691   gint latitude;
692   gint longitude;
693 #endif
694 } AtomUDTA;
695 
696 enum TrFlags
697 {
698   TR_DATA_OFFSET              = 0x01,     /* data-offset-present */
699   TR_FIRST_SAMPLE_FLAGS       = 0x04,     /* first-sample-flags-present */
700   TR_SAMPLE_DURATION          = 0x0100,   /* sample-duration-present */
701   TR_SAMPLE_SIZE              = 0x0200,   /* sample-size-present */
702   TR_SAMPLE_FLAGS             = 0x0400,   /* sample-flags-present */
703   TR_COMPOSITION_TIME_OFFSETS = 0x0800    /* sample-composition-time-offsets-presents */
704 };
705 
706 enum TfFlags
707 {
708   TF_BASE_DATA_OFFSET         = 0x01,     /* base-data-offset-present */
709   TF_SAMPLE_DESCRIPTION_INDEX = 0x02,     /* sample-description-index-present */
710   TF_DEFAULT_SAMPLE_DURATION  = 0x08,     /* default-sample-duration-present */
711   TF_DEFAULT_SAMPLE_SIZE      = 0x010,    /* default-sample-size-present */
712   TF_DEFAULT_SAMPLE_FLAGS     = 0x020,    /* default-sample-flags-present */
713   TF_DURATION_IS_EMPTY        = 0x010000, /* sample-composition-time-offsets-presents */
714   TF_DEFAULT_BASE_IS_MOOF     = 0x020000  /* default-base-is-moof */
715 };
716 
717 /* Timecode flags */
718 enum TcFlags
719 {
720   TC_DROP_FRAME = 0x0001,   /* Drop-frame timecode */
721   TC_24H_MAX = 0x0002,      /* Whether the timecode wraps after 24 hours */
722   TC_NEGATIVE_OK = 0x0004,  /* Whether negative time values are OK */
723   TC_COUNTER = 0x0008       /* Whether the time value corresponds to a tape counter value */
724 };
725 
726 typedef struct _AtomTRAK
727 {
728   Atom header;
729 
730   AtomTKHD tkhd;
731   AtomInfo *tapt;
732   AtomEDTS *edts;
733   AtomMDIA mdia;
734   AtomUDTA udta;
735   AtomTREF *tref;
736 
737   /* some helper info for structural conformity checks */
738   gboolean is_video;
739   gboolean is_h264;
740 
741   AtomsContext *context;
742 } AtomTRAK;
743 
744 typedef struct _AtomTREX
745 {
746   AtomFull header;
747 
748   guint32 track_ID;
749   guint32 default_sample_description_index;
750   guint32 default_sample_duration;
751   guint32 default_sample_size;
752   guint32 default_sample_flags;
753 } AtomTREX;
754 
755 typedef struct _AtomMEHD
756 {
757   AtomFull header;
758 
759   guint64 fragment_duration;
760 } AtomMEHD;
761 
762 
763 typedef struct _AtomMVEX
764 {
765   Atom header;
766 
767   AtomMEHD mehd;
768 
769   /* list of AtomTREX */
770   GList *trexs;
771 } AtomMVEX;
772 
773 typedef struct _AtomMFHD
774 {
775   AtomFull header;
776 
777   guint32 sequence_number;
778 } AtomMFHD;
779 
780 typedef struct _AtomTFHD
781 {
782   AtomFull header;
783 
784   guint32 track_ID;
785   guint64 base_data_offset;
786   guint32 sample_description_index;
787   guint32 default_sample_duration;
788   guint32 default_sample_size;
789   guint32 default_sample_flags;
790 } AtomTFHD;
791 
792 typedef struct _AtomTFDT
793 {
794   AtomFull header;
795 
796   guint64 base_media_decode_time;
797 } AtomTFDT;
798 
799 typedef struct _TRUNSampleEntry
800 {
801   guint32 sample_duration;
802   guint32 sample_size;
803   guint32 sample_flags;
804   guint32 sample_composition_time_offset;
805 } TRUNSampleEntry;
806 
807 typedef struct _AtomTRUN
808 {
809   AtomFull header;
810 
811   guint32 sample_count;
812   gint32 data_offset;
813   guint32 first_sample_flags;
814 
815   /* array of fields */
816   ATOM_ARRAY (TRUNSampleEntry) entries;
817 } AtomTRUN;
818 
819 typedef struct _AtomSDTP
820 {
821   AtomFull header;
822 
823   /* not serialized */
824   guint32 sample_count;
825 
826   /* array of fields */
827   ATOM_ARRAY (guint8) entries;
828 } AtomSDTP;
829 
830 typedef struct _AtomTRAF
831 {
832   Atom header;
833 
834   AtomTFHD tfhd;
835 
836   AtomTFDT tfdt;
837 
838   /* list of AtomTRUN */
839   GList *truns;
840   /* list of AtomSDTP */
841   GList *sdtps;
842 } AtomTRAF;
843 
844 typedef struct _AtomMOOF
845 {
846   Atom header;
847 
848   AtomMFHD mfhd;
849 
850   /* list of AtomTRAF */
851   GList *trafs;
852 } AtomMOOF;
853 
854 
855 typedef struct _AtomMOOV
856 {
857   /* style */
858   AtomsContext context;
859 
860   Atom header;
861 
862   AtomMVHD mvhd;
863   AtomMVEX mvex;
864 
865   /* list of AtomTRAK */
866   GList *traks;
867   AtomUDTA udta;
868 
869   gboolean fragmented;
870   guint32 chunks_offset;
871 } AtomMOOV;
872 
873 typedef struct _AtomWAVE
874 {
875   Atom header;
876 
877   /* list of AtomInfo */
878   GList *extension_atoms;
879 } AtomWAVE;
880 
881 typedef struct _TFRAEntry
882 {
883   guint64 time;
884   guint64 moof_offset;
885   guint32 traf_number;
886   guint32 trun_number;
887   guint32 sample_number;
888 } TFRAEntry;
889 
890 typedef struct _AtomTFRA
891 {
892   AtomFull header;
893 
894   guint32 track_ID;
895   guint32 lengths;
896   /* array of entries */
897   ATOM_ARRAY (TFRAEntry) entries;
898 } AtomTFRA;
899 
900 typedef struct _AtomMFRA
901 {
902   Atom header;
903 
904   /* list of tfra */
905   GList *tfras;
906 } AtomMFRA;
907 
908 /*
909  * Function to serialize an atom
910  */
911 typedef guint64 (*AtomCopyDataFunc) (Atom *atom, guint8 **buffer, guint64 *size, guint64 *offset);
912 
913 /*
914  * Releases memory allocated by an atom
915  */
916 typedef guint64 (*AtomFreeFunc) (Atom *atom);
917 
918 /*
919  * Some atoms might have many optional different kinds of child atoms, so this
920  * is useful for enabling generic handling of any atom.
921  * All we need are the two functions (copying it to an array
922  * for serialization and the memory releasing function).
923  */
924 struct _AtomInfo
925 {
926   Atom *atom;
927   AtomCopyDataFunc copy_data_func;
928   AtomFreeFunc free_func;
929 };
930 
931 guint64    atoms_get_current_qt_time   (void);
932 
933 guint64    atom_copy_data              (Atom *atom, guint8 **buffer,
934                                         guint64 *size, guint64* offset);
935 
936 AtomFTYP*  atom_ftyp_new               (AtomsContext *context, guint32 major,
937                                         guint32 version, GList *brands);
938 guint64    atom_ftyp_copy_data         (AtomFTYP *ftyp, guint8 **buffer,
939                                         guint64 *size, guint64 *offset);
940 void       atom_ftyp_free              (AtomFTYP *ftyp);
941 
942 AtomTRAK*  atom_trak_new               (AtomsContext *context);
943 void       atom_trak_add_samples       (AtomTRAK * trak, guint32 nsamples, guint32 delta,
944                                         guint32 size, guint64 chunk_offset, gboolean sync,
945                                         gint64 pts_offset);
946 void       atom_trak_set_elst_entry    (AtomTRAK * trak, gint index, guint32 duration,
947                                         guint32 media_time, guint32 rate);
948 void       atom_trak_edts_clear        (AtomTRAK * trak);
949 guint32    atom_trak_get_timescale     (AtomTRAK *trak);
950 guint32    atom_trak_get_id            (AtomTRAK * trak);
951 void       atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size);
952 void       atom_stbl_add_samples       (AtomSTBL * stbl, guint32 nsamples,
953                                         guint32 delta, guint32 size,
954                                         guint64 chunk_offset, gboolean sync,
955                                         gint64 pts_offset);
956 void       atom_stsc_add_new_entry     (AtomSTSC * stsc,
957                                         guint32 first_chunk, guint32 nsamples);
958 
959 AtomMOOV*  atom_moov_new               (AtomsContext *context);
960 void       atom_moov_free              (AtomMOOV *moov);
961 guint64    atom_moov_copy_data         (AtomMOOV *atom, guint8 **buffer, guint64 *size, guint64* offset);
962 void       atom_moov_update_timescale  (AtomMOOV *moov, guint32 timescale);
963 void       atom_moov_update_duration   (AtomMOOV *moov);
964 void       atom_moov_set_fragmented    (AtomMOOV *moov, gboolean fragmented);
965 void       atom_moov_chunks_set_offset (AtomMOOV *moov, guint32 offset);
966 void       atom_moov_add_trak          (AtomMOOV *moov, AtomTRAK *trak);
967 guint      atom_moov_get_trak_count    (AtomMOOV *moov);
968 
969 guint      atom_framerate_to_timescale (gint fps_n, gint fps_d);
970 
971 guint64    atom_mvhd_copy_data         (AtomMVHD * atom, guint8 ** buffer,
972                                         guint64 * size, guint64 * offset);
973 void       atom_stco64_chunks_set_offset (AtomSTCO64 * stco64, guint32 offset);
974 guint64    atom_trak_copy_data         (AtomTRAK * atom, guint8 ** buffer,
975                                         guint64 * size, guint64 * offset);
976 void       atom_stbl_clear             (AtomSTBL * stbl);
977 void       atom_stbl_init              (AtomSTBL * stbl);
978 guint64    atom_stss_copy_data         (AtomSTSS *atom, guint8 **buffer,
979                                         guint64 *size, guint64* offset);
980 guint64    atom_stts_copy_data         (AtomSTTS *atom, guint8 **buffer,
981                                         guint64 *size, guint64* offset);
982 guint64    atom_stsc_copy_data         (AtomSTSC *atom, guint8 **buffer,
983                                         guint64 *size, guint64* offset);
984 guint64    atom_stsz_copy_data         (AtomSTSZ *atom, guint8 **buffer,
985                                         guint64 *size, guint64* offset);
986 guint64    atom_ctts_copy_data         (AtomCTTS *atom, guint8 **buffer,
987                                         guint64 *size, guint64* offset);
988 guint64    atom_svmi_copy_data         (AtomSVMI *atom, guint8 **buffer,
989                                         guint64 *size, guint64* offset);
990 AtomSVMI * atom_svmi_new (guint8 stereoscopic_composition_type, gboolean is_left_first);
991 guint64    atom_stco64_copy_data       (AtomSTCO64 *atom, guint8 **buffer,
992                                         guint64 *size, guint64* offset);
993 AtomMOOF*  atom_moof_new               (AtomsContext *context, guint32 sequence_number);
994 void       atom_moof_free              (AtomMOOF *moof);
995 guint64    atom_moof_copy_data         (AtomMOOF *moof, guint8 **buffer, guint64 *size, guint64* offset);
996 AtomTRAF * atom_traf_new               (AtomsContext * context, guint32 track_ID);
997 void       atom_traf_free              (AtomTRAF * traf);
998 void       atom_traf_set_base_decode_time (AtomTRAF * traf, guint64 base_decode_time);
999 void       atom_traf_add_samples       (AtomTRAF * traf, guint32 delta,
1000                                         guint32 size, gboolean sync, gint64 pts_offset,
1001                                         gboolean sdtp_sync);
1002 guint32    atom_traf_get_sample_num    (AtomTRAF * traf);
1003 void       atom_moof_add_traf          (AtomMOOF *moof, AtomTRAF *traf);
1004 
1005 AtomMFRA*  atom_mfra_new               (AtomsContext *context);
1006 void       atom_mfra_free              (AtomMFRA *mfra);
1007 AtomTFRA*  atom_tfra_new               (AtomsContext *context, guint32 track_ID);
1008 void       atom_tfra_add_entry         (AtomTFRA *tfra, guint64 dts, guint32 sample_num);
1009 void       atom_tfra_update_offset     (AtomTFRA * tfra, guint64 offset);
1010 void       atom_mfra_add_tfra          (AtomMFRA *mfra, AtomTFRA *tfra);
1011 guint64    atom_mfra_copy_data         (AtomMFRA *mfra, guint8 **buffer, guint64 *size, guint64* offset);
1012 
1013 
1014 /* media sample description related helpers */
1015 typedef struct
1016 {
1017   guint16 version;
1018   guint32 fourcc;
1019   guint width;
1020   guint height;
1021   guint depth;
1022   guint frame_count;
1023   gint color_table_id;
1024   guint par_n;
1025   guint par_d;
1026 
1027   GstBuffer *codec_data;
1028 } VisualSampleEntry;
1029 
1030 typedef struct
1031 {
1032   guint32 fourcc;
1033   guint version;
1034   gint compression_id;
1035   guint sample_rate;
1036   guint channels;
1037   guint sample_size;
1038   guint bytes_per_packet;
1039   guint samples_per_packet;
1040   guint bytes_per_sample;
1041   guint bytes_per_frame;
1042 
1043   GstBuffer *codec_data;
1044 } AudioSampleEntry;
1045 
1046 typedef struct
1047 {
1048   guint32 fourcc;
1049 
1050   guint8  font_face; /* bold=0x1, italic=0x2, underline=0x4 */
1051   guint8  font_size;
1052   guint32 foreground_color_rgba;
1053 } SubtitleSampleEntry;
1054 
1055 void subtitle_sample_entry_init (SubtitleSampleEntry * entry);
1056 
1057 SampleTableEntryMP4A * atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
1058                                AudioSampleEntry * entry, guint32 scale,
1059                                AtomInfo * ext, gint sample_size);
1060 
1061 SampleTableEntryMP4V * atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
1062                                VisualSampleEntry * entry, guint32 rate,
1063                                GList * ext_atoms_list);
1064 
1065 SampleTableEntryTX3G * atom_trak_set_subtitle_type (AtomTRAK * trak, AtomsContext * context,
1066                                SubtitleSampleEntry * entry);
1067 
1068 SampleTableEntryTMCD *
1069 atom_trak_set_timecode_type (AtomTRAK * trak, AtomsContext * context, guint trak_timescale, GstVideoTimeCode * tc);
1070 
1071 SampleTableEntry * atom_trak_set_caption_type (AtomTRAK *trak, AtomsContext *context,
1072 					       guint32 trak_timescale, guint32 caption_type);
1073 
1074 void atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
1075                                 guint32 max_bitrate);
1076 
1077 void atom_trak_tx3g_update_dimension (AtomTRAK * trak, guint32 width,
1078                                       guint32 height);
1079 
1080 void sample_table_entry_add_ext_atom (SampleTableEntry * ste, AtomInfo * ext);
1081 
1082 AtomInfo *   build_codec_data_extension  (guint32 fourcc, const GstBuffer * codec_data);
1083 AtomInfo *   build_mov_aac_extension     (AtomTRAK * trak, const GstBuffer * codec_data,
1084                                           guint32 avg_bitrate, guint32 max_bitrate);
1085 AtomInfo *   build_mov_alac_extension    (const GstBuffer * codec_data);
1086 AtomInfo *   build_esds_extension        (AtomTRAK * trak, guint8 object_type,
1087                                           guint8 stream_type, const GstBuffer * codec_data,
1088                                           guint32 avg_bitrate, guint32 max_bitrate);
1089 AtomInfo *   build_btrt_extension        (guint32 buffer_size_db, guint32 avg_bitrate,
1090                                           guint32 max_bitrate);
1091 AtomInfo *   build_jp2h_extension        (gint width, gint height, const gchar *colorspace,
1092                                           gint ncomp, const GValue * cmap_array,
1093                                           const GValue * cdef_array);
1094 
1095 AtomInfo *   build_jp2x_extension        (const GstBuffer * prefix);
1096 AtomInfo *   build_fiel_extension        (GstVideoInterlaceMode mode, GstVideoFieldOrder order);
1097 AtomInfo *   build_colr_extension        (const GstVideoColorimetry *colorimetry, gboolean is_mp4);
1098 AtomInfo *   build_clap_extension        (gint width_n, gint width_d, gint height_n, gint height_d, gint h_off_n, gint h_off_d, gint v_off_n, gint v_off_d);
1099 AtomInfo *   build_tapt_extension        (gint clef_width, gint clef_height, gint prof_width, gint prof_height, gint enof_width, gint enof_height);
1100 
1101 
1102 AtomInfo *   build_ac3_extension         (guint8 fscod, guint8 bsid,
1103                                           guint8 bsmod, guint8 acmod,
1104                                           guint8 lfe_on, guint8 bitrate_code);
1105 AtomInfo *   build_opus_extension        (guint32 rate, guint8 channels, guint8 mapping_family,
1106                                           guint8 stream_count, guint8 coupled_count,
1107                                           guint8 channel_mapping[256], guint16 pre_skip,
1108                                           guint16 output_gain);
1109 
1110 AtomInfo *   build_amr_extension         (void);
1111 AtomInfo *   build_h263_extension        (void);
1112 AtomInfo *   build_gama_atom             (gdouble gamma);
1113 AtomInfo *   build_SMI_atom              (const GstBuffer *seqh);
1114 AtomInfo *   build_ima_adpcm_extension   (gint channels, gint rate,
1115                                           gint blocksize);
1116 AtomInfo *   build_uuid_xmp_atom         (GstBuffer * xmp);
1117 
1118 
1119 /*
1120  * Meta tags functions
1121  */
1122 void atom_udta_clear_tags (AtomUDTA *udta);
1123 void atom_udta_add_str_tag    (AtomUDTA *udta, guint32 fourcc, const gchar *value);
1124 void atom_udta_add_uint_tag   (AtomUDTA *udta, guint32 fourcc, guint32 flags,
1125                                guint32 value);
1126 void atom_udta_add_tag        (AtomUDTA *udta, guint32 fourcc, guint32 flags,
1127                                const guint8 * data, guint size);
1128 void atom_udta_add_blob_tag   (AtomUDTA *udta, guint8 *data, guint size);
1129 
1130 void atom_udta_add_3gp_str_tag       (AtomUDTA *udta, guint32 fourcc, const gchar * value);
1131 void atom_udta_add_3gp_uint_tag      (AtomUDTA *udta, guint32 fourcc, guint16 value);
1132 void atom_udta_add_3gp_str_int_tag   (AtomUDTA *udta, guint32 fourcc, const gchar * value,
1133                                       gint16 ivalue);
1134 void atom_udta_add_3gp_tag           (AtomUDTA *udta, guint32 fourcc, guint8 * data,
1135                                       guint size);
1136 
1137 void atom_udta_add_xmp_tags          (AtomUDTA *udta, GstBuffer * xmp);
1138 
1139 AtomTREF * atom_tref_new (guint32 reftype);
1140 void atom_tref_add_entry (AtomTREF * tref, guint32 sample);
1141 
1142 #define GST_QT_MUX_DEFAULT_TAG_LANGUAGE   "und" /* undefined/unknown */
1143 guint16  language_code               (const char * lang);
1144 
1145 #endif /* __ATOMS_H__ */
1146