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