summaryrefslogtreecommitdiff
path: root/gst/isomp4/atoms.h
blob: 09637df7d9b64d09bcd3e02ca57df554c09c9019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/* Quicktime muxer plugin for GStreamer
 * Copyright (C) 2008-2010 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
/*
 * Unless otherwise indicated, Source Code is licensed under MIT license.
 * See further explanation attached in License Statement (distributed in the file
 * LICENSE).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __ATOMS_H__
#define __ATOMS_H__

#include <glib.h>
#include <string.h>

#include "descriptors.h"
#include "properties.h"
#include "fourcc.h"

/* helper storage struct */
#define ATOM_ARRAY(struct_type) \
struct { \
  guint size; \
  guint len; \
  struct_type *data; \
}

/* storage helpers */

#define atom_array_init(array, reserve)                                       \
G_STMT_START {                                                                \
  (array)->len = 0;                                                           \
  (array)->size = reserve;                                                    \
  (array)->data = g_malloc (sizeof (*(array)->data) * reserve);               \
} G_STMT_END

#define atom_array_append(array, elmt, inc)                                   \
G_STMT_START {                                                                \
  g_assert ((array)->data);                                                   \
  g_assert (inc > 0);                                                         \
  if (G_UNLIKELY ((array)->len == (array)->size)) {                           \
    (array)->size += inc;                                                     \
    (array)->data =                                                           \
        g_realloc ((array)->data, sizeof (*((array)->data)) * (array)->size); \
  }                                                                           \
  (array)->data[(array)->len] = elmt;                                         \
  (array)->len++;                                                             \
} G_STMT_END

#define atom_array_get_len(array)                  ((array)->len)
#define atom_array_index(array, index)             ((array)->data[index])

#define atom_array_clear(array)                                               \
G_STMT_START {                                                                \
  (array)->size = (array)->len = 0;                                           \
  g_free ((array)->data);                                                     \
  (array)->data = NULL;                                                       \
} G_STMT_END

/* light-weight context that may influence header atom tree construction */
typedef enum _AtomsTreeFlavor
{
  ATOMS_TREE_FLAVOR_MOV,
  ATOMS_TREE_FLAVOR_ISOM,
  ATOMS_TREE_FLAVOR_3GP,
  ATOMS_TREE_FLAVOR_ISML
} AtomsTreeFlavor;

typedef struct _AtomsContext
{
  AtomsTreeFlavor flavor;
} AtomsContext;

AtomsContext* atoms_context_new  (AtomsTreeFlavor flavor);
void          atoms_context_free (AtomsContext *context);

#define METADATA_DATA_FLAG 0x0
#define METADATA_TEXT_FLAG 0x1

/* atom defs and functions */

/**
 * Used for storing time related values for some atoms.
 */
typedef struct _TimeInfo
{
  guint64 creation_time;
  guint64 modification_time;
  guint32 timescale;
  guint64 duration;
} TimeInfo;

typedef struct _Atom
{
  guint32 size;
  guint32 type;
  guint64 extended_size;
} Atom;

typedef struct _AtomFull
{
  Atom header;

  guint8 version;
  guint8 flags[3];
} AtomFull;

/*
 * Generic extension atom
 */
typedef struct _AtomData
{
  Atom header;

  /* not written */
  guint32 datalen;

  guint8 *data;
} AtomData;

typedef struct _AtomUUID
{
  Atom header;

  guint8 uuid[16];

  /* not written */
  guint32 datalen;

  guint8 *data;
} AtomUUID;

typedef struct _AtomFTYP
{
  Atom header;
  guint32 major_brand;
  guint32 version;
  guint32 *compatible_brands;

  /* not written */
  guint32 compatible_brands_size;
} AtomFTYP;

typedef struct _AtomMVHD
{
  AtomFull header;

  /* version 0: 32 bits */
  TimeInfo time_info;

  guint32 prefered_rate;      /* ISO: 0x00010000 */
  guint16 volume;             /* ISO: 0x0100 */
  guint16 reserved3;          /* ISO: 0x0 */
  guint32 reserved4[2];       /* ISO: 0, 0 */
  /* ISO: identity matrix =
   * { 0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000 } */
  guint32 matrix[9];

  /* ISO: all 0 */
  guint32 preview_time;
  guint32 preview_duration;
  guint32 poster_time;
  guint32 selection_time;
  guint32 selection_duration;
  guint32 current_time;

  guint32 next_track_id;
} AtomMVHD;

typedef struct _AtomTKHD
{
  AtomFull header;

  /* version 0: 32 bits */
  /* like the TimeInfo struct, but it has this track_ID inside */
  guint64 creation_time;
  guint64 modification_time;
  guint32 track_ID;
  guint32 reserved;
  guint64 duration;

  guint32 reserved2[2];
  guint16 layer;
  guint16 alternate_group;
  guint16 volume;
  guint16 reserved3;

  /* ISO: identity matrix =
   * { 0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000 } */
  guint32 matrix[9];
  guint32 width;
  guint32 height;
} AtomTKHD;

typedef struct _AtomMDHD
{
  AtomFull header;

  /* version 0: 32 bits */
  TimeInfo time_info;

  /* ISO: packed ISO-639-2/T language code (first bit must be 0) */
  guint16 language_code;
  /* ISO: 0 */
  guint16 quality;
} AtomMDHD;

typedef struct _AtomHDLR
{
  AtomFull header;

  /* ISO: 0 */
  guint32 component_type;
  guint32 handler_type;
  guint32 manufacturer;
  guint32 flags;
  guint32 flags_mask;
  gchar *name;
} AtomHDLR;

typedef struct _AtomVMHD
{
  AtomFull header;          /* ISO: flags = 1 */

  guint16 graphics_mode;
  /* RGB */
  guint16 opcolor[3];
} AtomVMHD;

typedef struct _AtomSMHD
{
  AtomFull header;

  guint16 balance;
  guint16 reserved;
} AtomSMHD;

typedef struct _AtomHMHD
{
  AtomFull header;

  guint16 max_pdu_size;
  guint16 avg_pdu_size;
  guint32 max_bitrate;
  guint32 avg_bitrate;
  guint32 sliding_avg_bitrate;
} AtomHMHD;

typedef struct _AtomURL
{
  AtomFull header;

  gchar *location;
} AtomURL;

typedef struct _AtomDREF
{
  AtomFull header;

  GList *entries;
} AtomDREF;

typedef struct _AtomDINF
{
  Atom header;

  AtomDREF dref;
} AtomDINF;

typedef struct _STTSEntry
{
  guint32 sample_count;
  gint32 sample_delta;
} STTSEntry;

typedef struct _AtomSTTS
{
  AtomFull header;

  ATOM_ARRAY (STTSEntry) entries;
} AtomSTTS;

typedef struct _AtomSTSS
{
  AtomFull header;

  ATOM_ARRAY (guint32) entries;
} AtomSTSS;

typedef struct _AtomESDS
{
  AtomFull header;

  ESDescriptor es;
} AtomESDS;

typedef struct _AtomFRMA
{
  Atom header;

  guint32 media_type;
} AtomFRMA;

typedef enum _SampleEntryKind
{
  UNKNOWN,
  AUDIO,
  VIDEO
} SampleEntryKind;

typedef struct _SampleTableEntry
{
  Atom header;

  guint8 reserved[6];
  guint16 data_reference_index;

  /* sort of entry */
  SampleEntryKind kind;
} SampleTableEntry;

typedef struct _AtomHintSampleEntry
{
  SampleTableEntry se;
  guint32 size;
  guint8 *data;
} AtomHintSampleEntry;

typedef struct _SampleTableEntryMP4V
{
  SampleTableEntry se;

  guint16 version;
  guint16 revision_level;

  guint32 vendor;                 /* fourcc code */
  guint32 temporal_quality;
  guint32 spatial_quality;

  guint16 width;
  guint16 height;

  guint32 horizontal_resolution;
  guint32 vertical_resolution;
  guint32 datasize;

  guint16 frame_count;            /* usually 1 */

  guint8 compressor[32];         /* pascal string, i.e. first byte = length */

  guint16 depth;
  guint16 color_table_id;

  /* (optional) list of AtomInfo */
  GList *extension_atoms;
} SampleTableEntryMP4V;

typedef struct _SampleTableEntryMP4A
{
  SampleTableEntry se;

  guint16 version;
  guint16 revision_level;
  guint32 vendor;

  guint16 channels;
  guint16 sample_size;
  guint16 compression_id;
  guint16 packet_size;

  guint32 sample_rate;            /* fixed point 16.16 */

  guint32 samples_per_packet;
  guint32 bytes_per_packet;
  guint32 bytes_per_frame;
  guint32 bytes_per_sample;

  /* (optional) list of AtomInfo */
  GList *extension_atoms;
} SampleTableEntryMP4A;

typedef struct _SampleTableEntryMP4S
{
  SampleTableEntry se;

  AtomESDS es;
} SampleTableEntryMP4S;

typedef struct _AtomSTSD
{
  AtomFull header;

  guint n_entries;
  /* list of subclasses of SampleTableEntry */
  GList *entries;
} AtomSTSD;

typedef struct _AtomSTSZ
{
  AtomFull header;

  guint32 sample_size;

  /* need the size here because when sample_size is constant,
   * the list is empty */
  guint32 table_size;
  ATOM_ARRAY (guint32) entries;
} AtomSTSZ;

typedef struct _STSCEntry
{
  guint32 first_chunk;
  guint32 samples_per_chunk;
  guint32 sample_description_index;
} STSCEntry;

typedef struct _AtomSTSC
{
  AtomFull header;

  ATOM_ARRAY (STSCEntry) entries;
} AtomSTSC;


/*
 * used for both STCO and CO64
 * if used as STCO, entries should be truncated to use only 32bits
 */
typedef struct _AtomSTCO64
{
  AtomFull header;

  ATOM_ARRAY (guint64) entries;
} AtomSTCO64;

typedef struct _CTTSEntry
{
  guint32 samplecount;
  guint32 sampleoffset;
} CTTSEntry;

typedef struct _AtomCTTS
{
  AtomFull header;

  /* also entry count here */
  ATOM_ARRAY (CTTSEntry) entries;
  gboolean do_pts;
} AtomCTTS;

typedef struct _AtomSTBL
{
  Atom header;

  AtomSTSD stsd;
  AtomSTTS stts;
  AtomSTSS stss;
  AtomSTSC stsc;
  AtomSTSZ stsz;
  /* NULL if not present */
  AtomCTTS *ctts;

  AtomSTCO64 stco64;
} AtomSTBL;

typedef struct _AtomMINF
{
  Atom header;

  /* only (exactly) one of those must be present */
  AtomVMHD *vmhd;
  AtomSMHD *smhd;
  AtomHMHD *hmhd;

  AtomHDLR *hdlr;
  AtomDINF dinf;
  AtomSTBL stbl;
} AtomMINF;

typedef struct _EditListEntry
{
  /* duration in movie's timescale */
  guint32 duration;
  /* start time in media's timescale, -1 for empty */
  guint32 media_time;
  guint32 media_rate;  /* fixed point 32 bit */
} EditListEntry;

typedef struct _AtomELST
{
  AtomFull header;

  /* number of entries is implicit */
  GSList *entries;
} AtomELST;

typedef struct _AtomEDTS
{
  Atom header;
  AtomELST elst;
} AtomEDTS;

typedef struct _AtomMDIA
{
  Atom header;

  AtomMDHD mdhd;
  AtomHDLR hdlr;
  AtomMINF minf;
} AtomMDIA;

typedef struct _AtomILST
{
  Atom header;

  /* list of AtomInfo */
  GList* entries;
} AtomILST;

typedef struct _AtomTagData
{
  AtomFull header;
  guint32 reserved;

  guint32 datalen;
  guint8* data;
} AtomTagData;

typedef struct _AtomTag
{
  Atom header;

  AtomTagData data;
} AtomTag;

typedef struct _AtomMETA
{
  AtomFull header;
  AtomHDLR hdlr;
  AtomILST *ilst;
} AtomMETA;

typedef struct _AtomUDTA
{
  Atom header;

  /* list of AtomInfo */
  GList* entries;
  /* or list is further down */
  AtomMETA *meta;
} AtomUDTA;

enum TrFlags
{
  TR_DATA_OFFSET              = 0x01,     /* data-offset-present */
  TR_FIRST_SAMPLE_FLAGS       = 0x04,     /* first-sample-flags-present */
  TR_SAMPLE_DURATION          = 0x0100,   /* sample-duration-present */
  TR_SAMPLE_SIZE              = 0x0200,   /* sample-size-present */
  TR_SAMPLE_FLAGS             = 0x0400,   /* sample-flags-present */
  TR_COMPOSITION_TIME_OFFSETS = 0x0800    /* sample-composition-time-offsets-presents */
};

enum TfFlags
{
  TF_BASE_DATA_OFFSET         = 0x01,     /* base-data-offset-present */
  TF_SAMPLE_DESCRIPTION_INDEX = 0x02,     /* sample-description-index-present */
  TF_DEFAULT_SAMPLE_DURATION  = 0x08,     /* default-sample-duration-present */
  TF_DEFAULT_SAMPLE_SIZE      = 0x010,    /* default-sample-size-present */
  TF_DEFAULT_SAMPLE_FLAGS     = 0x020,    /* default-sample-flags-present */
  TF_DURATION_IS_EMPTY        = 0x010000  /* sample-composition-time-offsets-presents */
};

typedef struct _AtomTRAK
{
  Atom header;

  AtomTKHD tkhd;
  AtomEDTS *edts;
  AtomMDIA mdia;

  /* some helper info for structural conformity checks */
  gboolean is_video;
  gboolean is_h264;
} AtomTRAK;

typedef struct _AtomTREX
{
  AtomFull header;

  guint32 track_ID;
  guint32 default_sample_description_index;
  guint32 default_sample_duration;
  guint32 default_sample_size;
  guint32 default_sample_flags;
} AtomTREX;

typedef struct _AtomMEHD
{
  AtomFull header;

  guint64 fragment_duration;
} AtomMEHD;


typedef struct _AtomMVEX
{
  Atom header;

  AtomMEHD mehd;

  /* list of AtomTREX */
  GList *trexs;
} AtomMVEX;

typedef struct _AtomMFHD
{
  AtomFull header;

  guint32 sequence_number;
} AtomMFHD;

typedef struct _AtomTFHD
{
  AtomFull header;

  guint32 track_ID;
  guint64 base_data_offset;
  guint32 sample_description_index;
  guint32 default_sample_duration;
  guint32 default_sample_size;
  guint32 default_sample_flags;
} AtomTFHD;

typedef struct _TRUNSampleEntry
{
  guint32 sample_duration;
  guint32 sample_size;
  guint32 sample_flags;
  guint32 sample_composition_time_offset;
} TRUNSampleEntry;

typedef struct _AtomTRUN
{
  AtomFull header;

  guint32 sample_count;
  gint32 data_offset;
  guint32 first_sample_flags;

  /* array of fields */
  ATOM_ARRAY (TRUNSampleEntry) entries;
} AtomTRUN;

typedef struct _AtomSDTP
{
  AtomFull header;

  /* not serialized */
  guint32 sample_count;

  /* array of fields */
  ATOM_ARRAY (guint8) entries;
} AtomSDTP;

typedef struct _AtomTRAF
{
  Atom header;

  AtomTFHD tfhd;

  /* list of AtomTRUN */
  GList *truns;
  /* list of AtomSDTP */
  GList *sdtps;
} AtomTRAF;

typedef struct _AtomMOOF
{
  Atom header;

  AtomMFHD mfhd;

  /* list of AtomTRAF */
  GList *trafs;
} AtomMOOF;


typedef struct _AtomMOOV
{
  /* style */
  AtomsContext context;

  Atom header;

  AtomMVHD mvhd;
  AtomMVEX mvex;

  /* list of AtomTRAK */
  GList *traks;
  AtomUDTA *udta;

  gboolean fragmented;
} AtomMOOV;

typedef struct _AtomWAVE
{
  Atom header;

  /* list of AtomInfo */
  GList *extension_atoms;
} AtomWAVE;

typedef struct _TFRAEntry
{
  guint64 time;
  guint64 moof_offset;
  guint32 traf_number;
  guint32 trun_number;
  guint32 sample_number;
} TFRAEntry;

typedef struct _AtomTFRA
{
  AtomFull header;

  guint32 track_ID;
  guint32 lengths;
  /* array of entries */
  ATOM_ARRAY (TFRAEntry) entries;
} AtomTFRA;

typedef struct _AtomMFRA
{
  Atom header;

  /* list of tfra */
  GList *tfras;
} AtomMFRA;

/*
 * Function to serialize an atom
 */
typedef guint64 (*AtomCopyDataFunc) (Atom *atom, guint8 **buffer, guint64 *size, guint64 *offset);

/*
 * Releases memory allocated by an atom
 */
typedef guint64 (*AtomFreeFunc) (Atom *atom);

/*
 * Some atoms might have many optional different kinds of child atoms, so this
 * is useful for enabling generic handling of any atom.
 * All we need are the two functions (copying it to an array
 * for serialization and the memory releasing function).
 */
typedef struct _AtomInfo
{
  Atom *atom;
  AtomCopyDataFunc copy_data_func;
  AtomFreeFunc free_func;
} AtomInfo;


guint64    atom_copy_data              (Atom *atom, guint8 **buffer,
                                        guint64 *size, guint64* offset);

AtomFTYP*  atom_ftyp_new               (AtomsContext *context, guint32 major,
                                        guint32 version, GList *brands);
guint64    atom_ftyp_copy_data         (AtomFTYP *ftyp, guint8 **buffer,
                                        guint64 *size, guint64 *offset);
void       atom_ftyp_free              (AtomFTYP *ftyp);

AtomTRAK*  atom_trak_new               (AtomsContext *context);
void       atom_trak_add_samples       (AtomTRAK * trak, guint32 nsamples, guint32 delta,
                                        guint32 size, guint64 chunk_offset, gboolean sync,
                                        gint64 pts_offset);
void       atom_trak_add_elst_entry    (AtomTRAK * trak, guint32 duration,
                                        guint32 media_time, guint32 rate);
guint32    atom_trak_get_timescale     (AtomTRAK *trak);
guint32    atom_trak_get_id            (AtomTRAK * trak);
void       atom_stbl_add_samples       (AtomSTBL * stbl, guint32 nsamples,
                                        guint32 delta, guint32 size,
                                        guint64 chunk_offset, gboolean sync,
                                        gint64 pts_offset);

AtomMOOV*  atom_moov_new               (AtomsContext *context);
void       atom_moov_free              (AtomMOOV *moov);
guint64    atom_moov_copy_data         (AtomMOOV *atom, guint8 **buffer, guint64 *size, guint64* offset);
void       atom_moov_update_timescale  (AtomMOOV *moov, guint32 timescale);
void       atom_moov_update_duration   (AtomMOOV *moov);
void       atom_moov_set_fragmented    (AtomMOOV *moov, gboolean fragmented);
void       atom_moov_chunks_add_offset (AtomMOOV *moov, guint32 offset);
void       atom_moov_add_trak          (AtomMOOV *moov, AtomTRAK *trak);

guint64    atom_mvhd_copy_data         (AtomMVHD * atom, guint8 ** buffer,
                                        guint64 * size, guint64 * offset);
void       atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset);
guint64    atom_trak_copy_data         (AtomTRAK * atom, guint8 ** buffer,
                                        guint64 * size, guint64 * offset);
void       atom_stbl_clear             (AtomSTBL * stbl);
void       atom_stbl_init              (AtomSTBL * stbl);
guint64    atom_stss_copy_data         (AtomSTSS *atom, guint8 **buffer,
                                        guint64 *size, guint64* offset);
guint64    atom_stts_copy_data         (AtomSTTS *atom, guint8 **buffer,
                                        guint64 *size, guint64* offset);
guint64    atom_stsc_copy_data         (AtomSTSC *atom, guint8 **buffer,
                                        guint64 *size, guint64* offset);
guint64    atom_stsz_copy_data         (AtomSTSZ *atom, guint8 **buffer,
                                        guint64 *size, guint64* offset);
guint64    atom_ctts_copy_data         (AtomCTTS *atom, guint8 **buffer,
                                        guint64 *size, guint64* offset);
guint64    atom_stco64_copy_data       (AtomSTCO64 *atom, guint8 **buffer,
                                        guint64 *size, guint64* offset);
AtomMOOF*  atom_moof_new               (AtomsContext *context, guint32 sequence_number);
void       atom_moof_free              (AtomMOOF *moof);
guint64    atom_moof_copy_data         (AtomMOOF *moof, guint8 **buffer, guint64 *size, guint64* offset);
AtomTRAF * atom_traf_new               (AtomsContext * context, guint32 track_ID);
void       atom_traf_free              (AtomTRAF * traf);
void       atom_traf_add_samples       (AtomTRAF * traf, guint32 delta,
                                        guint32 size, gboolean sync, gint64 pts_offset,
                                        gboolean sdtp_sync);
guint32    atom_traf_get_sample_num    (AtomTRAF * traf);
void       atom_moof_add_traf          (AtomMOOF *moof, AtomTRAF *traf);

AtomMFRA*  atom_mfra_new               (AtomsContext *context);
void       atom_mfra_free              (AtomMFRA *mfra);
AtomTFRA*  atom_tfra_new               (AtomsContext *context, guint32 track_ID);
void       atom_tfra_add_entry         (AtomTFRA *tfra, guint64 dts, guint32 sample_num);
void       atom_tfra_update_offset     (AtomTFRA * tfra, guint64 offset);
void       atom_mfra_add_tfra          (AtomMFRA *mfra, AtomTFRA *tfra);
guint64    atom_mfra_copy_data         (AtomMFRA *mfra, guint8 **buffer, guint64 *size, guint64* offset);


/* media sample description related helpers */

typedef struct
{
  guint16 version;
  guint32 fourcc;
  guint width;
  guint height;
  guint depth;
  guint frame_count;
  gint color_table_id;
  guint par_n;
  guint par_d;

  GstBuffer *codec_data;
} VisualSampleEntry;

typedef struct
{
  guint32 fourcc;
  guint version;
  gint compression_id;
  guint sample_rate;
  guint channels;
  guint sample_size;
  guint bytes_per_packet;
  guint samples_per_packet;
  guint bytes_per_sample;
  guint bytes_per_frame;

  GstBuffer *codec_data;
} AudioSampleEntry;

void atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
                               AudioSampleEntry * entry, guint32 scale,
                               AtomInfo * ext, gint sample_size);

void atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
                               VisualSampleEntry * entry, guint32 rate,
                               GList * ext_atoms_list);

void atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
                                guint32 max_bitrate);

AtomInfo *   build_codec_data_extension  (guint32 fourcc, const GstBuffer * codec_data);
AtomInfo *   build_mov_aac_extension     (AtomTRAK * trak, const GstBuffer * codec_data,
                                          guint32 avg_bitrate, guint32 max_bitrate);
AtomInfo *   build_mov_alac_extension    (AtomTRAK * trak, const GstBuffer * codec_data);
AtomInfo *   build_esds_extension        (AtomTRAK * trak, guint8 object_type,
                                          guint8 stream_type, const GstBuffer * codec_data,
                                          guint32 avg_bitrate, guint32 max_bitrate);
AtomInfo *   build_btrt_extension        (guint32 buffer_size_db, guint32 avg_bitrate,
                                          guint32 max_bitrate);
AtomInfo *   build_jp2h_extension        (AtomTRAK * trak, gint width, gint height,
                                          const gchar *colorspace, gint ncomp,
                                          const GValue * cmap_array,
                                          const GValue * cdef_array);

AtomInfo *   build_jp2x_extension        (const GstBuffer * prefix);
AtomInfo *   build_fiel_extension        (gint fields);
AtomInfo *   build_amr_extension         (void);
AtomInfo *   build_h263_extension        (void);
AtomInfo *   build_gama_atom             (gdouble gamma);
AtomInfo *   build_SMI_atom              (const GstBuffer *seqh);
AtomInfo *   build_ima_adpcm_extension   (gint channels, gint rate,
                                          gint blocksize);
AtomInfo *   build_uuid_xmp_atom         (GstBuffer * xmp);


/*
 * Meta tags functions
 */
void atom_moov_add_str_tag    (AtomMOOV *moov, guint32 fourcc, const gchar *value);
void atom_moov_add_uint_tag   (AtomMOOV *moov, guint32 fourcc, guint32 flags,
                               guint32 value);
void atom_moov_add_tag        (AtomMOOV *moov, guint32 fourcc, guint32 flags,
                               const guint8 * data, guint size);
void atom_moov_add_blob_tag   (AtomMOOV *moov, guint8 *data, guint size);

void atom_moov_add_3gp_str_tag       (AtomMOOV * moov, guint32 fourcc, const gchar * value);
void atom_moov_add_3gp_uint_tag      (AtomMOOV * moov, guint32 fourcc, guint16 value);
void atom_moov_add_3gp_str_int_tag   (AtomMOOV * moov, guint32 fourcc, const gchar * value,
                                      gint16 ivalue);
void atom_moov_add_3gp_tag           (AtomMOOV * moov, guint32 fourcc, guint8 * data,
                                      guint size);

void atom_moov_add_xmp_tags          (AtomMOOV * moov, GstBuffer * xmp);

#define GST_QT_MUX_DEFAULT_TAG_LANGUAGE   "und" /* undefined/unknown */
guint16  language_code               (const char * lang);

#endif /* __ATOMS_H__ */