1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2008-2010 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
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.
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.
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
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:
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
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
49 #include "descriptors.h"
50 #include "properties.h"
54 /* helper storage struct */
55 #define ATOM_ARRAY(struct_type) \
64 #define atom_array_init(array, reserve) \
67 (array)->size = reserve; \
68 (array)->data = g_malloc (sizeof (*(array)->data) * reserve); \
71 #define atom_array_append(array, elmt, inc) \
73 g_assert ((array)->data); \
75 if (G_UNLIKELY ((array)->len == (array)->size)) { \
76 (array)->size += inc; \
78 g_realloc ((array)->data, sizeof (*((array)->data)) * (array)->size); \
80 (array)->data[(array)->len] = elmt; \
84 #define atom_array_get_len(array) ((array)->len)
85 #define atom_array_index(array, index) ((array)->data[index])
87 #define atom_array_clear(array) \
89 (array)->size = (array)->len = 0; \
90 g_free ((array)->data); \
91 (array)->data = NULL; \
94 /* light-weight context that may influence header atom tree construction */
95 typedef enum _AtomsTreeFlavor
97 ATOMS_TREE_FLAVOR_MOV,
98 ATOMS_TREE_FLAVOR_ISOM,
99 ATOMS_TREE_FLAVOR_3GP,
100 ATOMS_TREE_FLAVOR_ISML
103 typedef struct _AtomsContext
105 AtomsTreeFlavor flavor;
108 AtomsContext* atoms_context_new (AtomsTreeFlavor flavor);
109 void atoms_context_free (AtomsContext *context);
111 #define METADATA_DATA_FLAG 0x0
112 #define METADATA_TEXT_FLAG 0x1
114 /* atom defs and functions */
117 * Used for storing time related values for some atoms.
119 typedef struct _TimeInfo
121 guint64 creation_time;
122 guint64 modification_time;
131 guint64 extended_size;
134 typedef struct _AtomFull
143 * Generic extension atom
145 typedef struct _AtomData
155 typedef struct _AtomUUID
167 typedef struct _AtomFTYP
172 guint32 *compatible_brands;
175 guint32 compatible_brands_size;
178 typedef struct _AtomMVHD
182 /* version 0: 32 bits */
185 guint32 prefered_rate; /* ISO: 0x00010000 */
186 guint16 volume; /* ISO: 0x0100 */
187 guint16 reserved3; /* ISO: 0x0 */
188 guint32 reserved4[2]; /* ISO: 0, 0 */
189 /* ISO: identity matrix =
190 * { 0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000 } */
194 guint32 preview_time;
195 guint32 preview_duration;
197 guint32 selection_time;
198 guint32 selection_duration;
199 guint32 current_time;
201 guint32 next_track_id;
204 typedef struct _AtomTKHD
208 /* version 0: 32 bits */
209 /* like the TimeInfo struct, but it has this track_ID inside */
210 guint64 creation_time;
211 guint64 modification_time;
216 guint32 reserved2[2];
218 guint16 alternate_group;
222 /* ISO: identity matrix =
223 * { 0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000 } */
229 typedef struct _AtomMDHD
233 /* version 0: 32 bits */
236 /* ISO: packed ISO-639-2/T language code (first bit must be 0) */
237 guint16 language_code;
242 typedef struct _AtomHDLR
247 guint32 component_type;
248 guint32 handler_type;
249 guint32 manufacturer;
255 typedef struct _AtomVMHD
257 AtomFull header; /* ISO: flags = 1 */
259 guint16 graphics_mode;
264 typedef struct _AtomSMHD
272 typedef struct _AtomHMHD
276 guint16 max_pdu_size;
277 guint16 avg_pdu_size;
280 guint32 sliding_avg_bitrate;
283 typedef struct _AtomURL
290 typedef struct _AtomDREF
297 typedef struct _AtomDINF
304 typedef struct _STTSEntry
306 guint32 sample_count;
310 typedef struct _AtomSTTS
314 ATOM_ARRAY (STTSEntry) entries;
317 typedef struct _AtomSTSS
321 ATOM_ARRAY (guint32) entries;
324 typedef struct _AtomESDS
331 typedef struct _AtomFRMA
338 typedef enum _SampleEntryKind
345 typedef struct _SampleTableEntry
350 guint16 data_reference_index;
353 SampleEntryKind kind;
356 typedef struct _AtomHintSampleEntry
361 } AtomHintSampleEntry;
363 typedef struct _SampleTableEntryMP4V
368 guint16 revision_level;
370 guint32 vendor; /* fourcc code */
371 guint32 temporal_quality;
372 guint32 spatial_quality;
377 guint32 horizontal_resolution;
378 guint32 vertical_resolution;
381 guint16 frame_count; /* usually 1 */
383 guint8 compressor[32]; /* pascal string, i.e. first byte = length */
386 guint16 color_table_id;
388 /* (optional) list of AtomInfo */
389 GList *extension_atoms;
390 } SampleTableEntryMP4V;
392 typedef struct _SampleTableEntryMP4A
397 guint16 revision_level;
402 guint16 compression_id;
405 guint32 sample_rate; /* fixed point 16.16 */
407 guint32 samples_per_packet;
408 guint32 bytes_per_packet;
409 guint32 bytes_per_frame;
410 guint32 bytes_per_sample;
412 /* (optional) list of AtomInfo */
413 GList *extension_atoms;
414 } SampleTableEntryMP4A;
416 typedef struct _SampleTableEntryMP4S
421 } SampleTableEntryMP4S;
423 typedef struct _AtomSTSD
428 /* list of subclasses of SampleTableEntry */
432 typedef struct _AtomSTSZ
438 /* need the size here because when sample_size is constant,
439 * the list is empty */
441 ATOM_ARRAY (guint32) entries;
444 typedef struct _STSCEntry
447 guint32 samples_per_chunk;
448 guint32 sample_description_index;
451 typedef struct _AtomSTSC
455 ATOM_ARRAY (STSCEntry) entries;
460 * used for both STCO and CO64
461 * if used as STCO, entries should be truncated to use only 32bits
463 typedef struct _AtomSTCO64
467 ATOM_ARRAY (guint64) entries;
470 typedef struct _CTTSEntry
473 guint32 sampleoffset;
476 typedef struct _AtomCTTS
480 /* also entry count here */
481 ATOM_ARRAY (CTTSEntry) entries;
485 typedef struct _AtomSTBL
494 /* NULL if not present */
500 typedef struct _AtomMINF
504 /* only (exactly) one of those must be present */
514 typedef struct _EditListEntry
516 /* duration in movie's timescale */
518 /* start time in media's timescale, -1 for empty */
520 guint32 media_rate; /* fixed point 32 bit */
523 typedef struct _AtomELST
527 /* number of entries is implicit */
531 typedef struct _AtomEDTS
537 typedef struct _AtomMDIA
546 typedef struct _AtomILST
550 /* list of AtomInfo */
554 typedef struct _AtomTagData
563 typedef struct _AtomTag
570 typedef struct _AtomMETA
577 typedef struct _AtomUDTA
581 /* list of AtomInfo */
583 /* or list is further down */
589 TR_DATA_OFFSET = 0x01, /* data-offset-present */
590 TR_FIRST_SAMPLE_FLAGS = 0x04, /* first-sample-flags-present */
591 TR_SAMPLE_DURATION = 0x0100, /* sample-duration-present */
592 TR_SAMPLE_SIZE = 0x0200, /* sample-size-present */
593 TR_SAMPLE_FLAGS = 0x0400, /* sample-flags-present */
594 TR_COMPOSITION_TIME_OFFSETS = 0x0800 /* sample-composition-time-offsets-presents */
599 TF_BASE_DATA_OFFSET = 0x01, /* base-data-offset-present */
600 TF_SAMPLE_DESCRIPTION_INDEX = 0x02, /* sample-description-index-present */
601 TF_DEFAULT_SAMPLE_DURATION = 0x08, /* default-sample-duration-present */
602 TF_DEFAULT_SAMPLE_SIZE = 0x010, /* default-sample-size-present */
603 TF_DEFAULT_SAMPLE_FLAGS = 0x020, /* default-sample-flags-present */
604 TF_DURATION_IS_EMPTY = 0x010000 /* sample-composition-time-offsets-presents */
607 typedef struct _AtomTRAK
615 /* some helper info for structural conformity checks */
620 typedef struct _AtomTREX
625 guint32 default_sample_description_index;
626 guint32 default_sample_duration;
627 guint32 default_sample_size;
628 guint32 default_sample_flags;
631 typedef struct _AtomMEHD
635 guint64 fragment_duration;
639 typedef struct _AtomMVEX
645 /* list of AtomTREX */
649 typedef struct _AtomMFHD
653 guint32 sequence_number;
656 typedef struct _AtomTFHD
661 guint64 base_data_offset;
662 guint32 sample_description_index;
663 guint32 default_sample_duration;
664 guint32 default_sample_size;
665 guint32 default_sample_flags;
668 typedef struct _TRUNSampleEntry
670 guint32 sample_duration;
672 guint32 sample_flags;
673 guint32 sample_composition_time_offset;
676 typedef struct _AtomTRUN
680 guint32 sample_count;
682 guint32 first_sample_flags;
684 /* array of fields */
685 ATOM_ARRAY (TRUNSampleEntry) entries;
688 typedef struct _AtomSDTP
693 guint32 sample_count;
695 /* array of fields */
696 ATOM_ARRAY (guint8) entries;
699 typedef struct _AtomTRAF
705 /* list of AtomTRUN */
707 /* list of AtomSDTP */
711 typedef struct _AtomMOOF
717 /* list of AtomTRAF */
722 typedef struct _AtomMOOV
725 AtomsContext context;
732 /* list of AtomTRAK */
739 typedef struct _AtomWAVE
743 /* list of AtomInfo */
744 GList *extension_atoms;
747 typedef struct _TFRAEntry
753 guint32 sample_number;
756 typedef struct _AtomTFRA
762 /* array of entries */
763 ATOM_ARRAY (TFRAEntry) entries;
766 typedef struct _AtomMFRA
775 * Function to serialize an atom
777 typedef guint64 (*AtomCopyDataFunc) (Atom *atom, guint8 **buffer, guint64 *size, guint64 *offset);
780 * Releases memory allocated by an atom
782 typedef guint64 (*AtomFreeFunc) (Atom *atom);
785 * Some atoms might have many optional different kinds of child atoms, so this
786 * is useful for enabling generic handling of any atom.
787 * All we need are the two functions (copying it to an array
788 * for serialization and the memory releasing function).
790 typedef struct _AtomInfo
793 AtomCopyDataFunc copy_data_func;
794 AtomFreeFunc free_func;
798 guint64 atom_copy_data (Atom *atom, guint8 **buffer,
799 guint64 *size, guint64* offset);
801 AtomFTYP* atom_ftyp_new (AtomsContext *context, guint32 major,
802 guint32 version, GList *brands);
803 guint64 atom_ftyp_copy_data (AtomFTYP *ftyp, guint8 **buffer,
804 guint64 *size, guint64 *offset);
805 void atom_ftyp_free (AtomFTYP *ftyp);
807 AtomTRAK* atom_trak_new (AtomsContext *context);
808 void atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
809 guint32 size, guint64 chunk_offset, gboolean sync,
811 void atom_trak_add_elst_entry (AtomTRAK * trak, guint32 duration,
812 guint32 media_time, guint32 rate);
813 guint32 atom_trak_get_timescale (AtomTRAK *trak);
814 guint32 atom_trak_get_id (AtomTRAK * trak);
815 void atom_stbl_add_samples (AtomSTBL * stbl, guint32 nsamples,
816 guint32 delta, guint32 size,
817 guint64 chunk_offset, gboolean sync,
820 AtomMOOV* atom_moov_new (AtomsContext *context);
821 void atom_moov_free (AtomMOOV *moov);
822 guint64 atom_moov_copy_data (AtomMOOV *atom, guint8 **buffer, guint64 *size, guint64* offset);
823 void atom_moov_update_timescale (AtomMOOV *moov, guint32 timescale);
824 void atom_moov_update_duration (AtomMOOV *moov);
825 void atom_moov_set_fragmented (AtomMOOV *moov, gboolean fragmented);
826 void atom_moov_chunks_add_offset (AtomMOOV *moov, guint32 offset);
827 void atom_moov_add_trak (AtomMOOV *moov, AtomTRAK *trak);
829 guint64 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer,
830 guint64 * size, guint64 * offset);
831 void atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset);
832 guint64 atom_trak_copy_data (AtomTRAK * atom, guint8 ** buffer,
833 guint64 * size, guint64 * offset);
834 void atom_stbl_clear (AtomSTBL * stbl);
835 void atom_stbl_init (AtomSTBL * stbl);
836 guint64 atom_stss_copy_data (AtomSTSS *atom, guint8 **buffer,
837 guint64 *size, guint64* offset);
838 guint64 atom_stts_copy_data (AtomSTTS *atom, guint8 **buffer,
839 guint64 *size, guint64* offset);
840 guint64 atom_stsc_copy_data (AtomSTSC *atom, guint8 **buffer,
841 guint64 *size, guint64* offset);
842 guint64 atom_stsz_copy_data (AtomSTSZ *atom, guint8 **buffer,
843 guint64 *size, guint64* offset);
844 guint64 atom_ctts_copy_data (AtomCTTS *atom, guint8 **buffer,
845 guint64 *size, guint64* offset);
846 guint64 atom_stco64_copy_data (AtomSTCO64 *atom, guint8 **buffer,
847 guint64 *size, guint64* offset);
848 AtomMOOF* atom_moof_new (AtomsContext *context, guint32 sequence_number);
849 void atom_moof_free (AtomMOOF *moof);
850 guint64 atom_moof_copy_data (AtomMOOF *moof, guint8 **buffer, guint64 *size, guint64* offset);
851 AtomTRAF * atom_traf_new (AtomsContext * context, guint32 track_ID);
852 void atom_traf_free (AtomTRAF * traf);
853 void atom_traf_add_samples (AtomTRAF * traf, guint32 delta,
854 guint32 size, gboolean sync, gint64 pts_offset,
856 guint32 atom_traf_get_sample_num (AtomTRAF * traf);
857 void atom_moof_add_traf (AtomMOOF *moof, AtomTRAF *traf);
859 AtomMFRA* atom_mfra_new (AtomsContext *context);
860 void atom_mfra_free (AtomMFRA *mfra);
861 AtomTFRA* atom_tfra_new (AtomsContext *context, guint32 track_ID);
862 void atom_tfra_add_entry (AtomTFRA *tfra, guint64 dts, guint32 sample_num);
863 void atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset);
864 void atom_mfra_add_tfra (AtomMFRA *mfra, AtomTFRA *tfra);
865 guint64 atom_mfra_copy_data (AtomMFRA *mfra, guint8 **buffer, guint64 *size, guint64* offset);
868 /* media sample description related helpers */
882 GstBuffer *codec_data;
893 guint bytes_per_packet;
894 guint samples_per_packet;
895 guint bytes_per_sample;
896 guint bytes_per_frame;
898 GstBuffer *codec_data;
901 void atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
902 AudioSampleEntry * entry, guint32 scale,
903 AtomInfo * ext, gint sample_size);
905 void atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
906 VisualSampleEntry * entry, guint32 rate,
907 GList * ext_atoms_list);
909 AtomInfo * build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data);
910 AtomInfo * build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
911 guint32 avg_bitrate, guint32 max_bitrate);
912 AtomInfo * build_mov_alac_extension (AtomTRAK * trak, const GstBuffer * codec_data);
913 AtomInfo * build_esds_extension (AtomTRAK * trak, guint8 object_type,
914 guint8 stream_type, const GstBuffer * codec_data,
915 guint32 avg_bitrate, guint32 max_bitrate);
916 AtomInfo * build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
917 guint32 max_bitrate);
918 AtomInfo * build_jp2h_extension (AtomTRAK * trak, gint width, gint height,
919 const gchar *colorspace, gint ncomp,
920 const GValue * cmap_array,
921 const GValue * cdef_array);
923 AtomInfo * build_jp2x_extension (const GstBuffer * prefix);
924 AtomInfo * build_fiel_extension (gint fields);
925 AtomInfo * build_amr_extension (void);
926 AtomInfo * build_h263_extension (void);
927 AtomInfo * build_gama_atom (gdouble gamma);
928 AtomInfo * build_SMI_atom (const GstBuffer *seqh);
929 AtomInfo * build_ima_adpcm_extension (gint channels, gint rate,
931 AtomInfo * build_uuid_xmp_atom (GstBuffer * xmp);
935 * Meta tags functions
937 void atom_moov_add_str_tag (AtomMOOV *moov, guint32 fourcc, const gchar *value);
938 void atom_moov_add_uint_tag (AtomMOOV *moov, guint32 fourcc, guint32 flags,
940 void atom_moov_add_tag (AtomMOOV *moov, guint32 fourcc, guint32 flags,
941 const guint8 * data, guint size);
942 void atom_moov_add_blob_tag (AtomMOOV *moov, guint8 *data, guint size);
944 void atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value);
945 void atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value);
946 void atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value,
948 void atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
951 void atom_moov_add_xmp_tags (AtomMOOV * moov, GstBuffer * xmp);
953 #define GST_QT_MUX_DEFAULT_TAG_LANGUAGE "eng"
954 guint16 language_code (const char * lang);
956 #endif /* __ATOMS_H__ */