1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2008 Thiago Sousa Santos <thiagoss@embedded.ufcg.edu.br>
3 * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sf.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
48 /* only needed for gst_util_uint64_scale */
52 * Creates a new AtomsContext for the given flavor.
55 atoms_context_new (AtomsTreeFlavor flavor)
57 AtomsContext *context = g_new0 (AtomsContext, 1);
58 context->flavor = flavor;
63 * Frees an AtomsContext and all memory associated with it
66 atoms_context_free (AtomsContext * context)
71 /* -- creation, initialization, clear and free functions -- */
73 #define SECS_PER_DAY (24 * 60 * 60)
74 #define LEAP_YEARS_FROM_1904_TO_1970 17
77 get_current_qt_time ()
81 g_get_current_time (&timeval);
82 /* FIXME this should use UTC coordinated time */
83 return timeval.tv_sec + (((1970 - 1904) * (guint64) 365) +
84 LEAP_YEARS_FROM_1904_TO_1970) * SECS_PER_DAY;
88 common_time_info_init (TimeInfo * ti)
90 ti->creation_time = ti->modification_time = get_current_qt_time ();
96 atom_header_set (Atom * header, guint32 fourcc, gint32 size, gint64 ext_size)
98 header->type = fourcc;
100 header->extended_size = ext_size;
104 atom_clear (Atom * atom)
109 atom_full_init (AtomFull * full, guint32 fourcc, gint32 size, gint64 ext_size,
110 guint8 version, guint8 flags[3])
112 atom_header_set (&(full->header), fourcc, size, ext_size);
113 full->version = version;
114 full->flags[0] = flags[0];
115 full->flags[1] = flags[1];
116 full->flags[2] = flags[2];
120 atom_full_clear (AtomFull * full)
122 atom_clear (&full->header);
126 atom_full_free (AtomFull * full)
128 atom_full_clear (full);
133 build_atom_info_wrapper (Atom * atom, gpointer copy_func, gpointer free_func)
135 AtomInfo *info = NULL;
138 info = g_new0 (AtomInfo, 1);
141 info->copy_data_func = copy_func;
142 info->free_func = free_func;
149 atom_info_list_prepend_atom (GList * ai, Atom * atom,
150 AtomCopyDataFunc copy_func, AtomFreeFunc free_func)
153 return g_list_prepend (ai,
154 build_atom_info_wrapper (atom, copy_func, free_func));
160 atom_info_list_free (GList * ai)
163 AtomInfo *info = (AtomInfo *) ai->data;
165 info->free_func (info->atom);
167 ai = g_list_delete_link (ai, ai);
172 atom_data_new (guint32 fourcc)
174 AtomData *data = g_new0 (AtomData, 1);
176 atom_header_set (&data->header, fourcc, 0, 0);
181 atom_data_alloc_mem (AtomData * data, guint32 size)
186 data->data = g_new0 (guint8, size);
187 data->datalen = size;
191 atom_data_new_from_gst_buffer (guint32 fourcc, const GstBuffer * buf)
193 AtomData *data = atom_data_new (fourcc);
195 atom_data_alloc_mem (data, GST_BUFFER_SIZE (buf));
196 g_memmove (data->data, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
201 atom_data_free (AtomData * data)
203 atom_clear (&data->header);
209 atom_ftyp_init (AtomFTYP * ftyp, guint32 major, guint32 version, GList * brands)
214 atom_header_set (&ftyp->header, FOURCC_ftyp, 16, 0);
215 ftyp->major_brand = major;
216 ftyp->version = version;
218 /* always include major brand as compatible brand */
219 ftyp->compatible_brands_size = g_list_length (brands) + 1;
220 ftyp->compatible_brands = g_new (guint32, ftyp->compatible_brands_size);
222 ftyp->compatible_brands[0] = major;
224 for (it = brands; it != NULL; it = g_list_next (it)) {
225 ftyp->compatible_brands[index++] = GPOINTER_TO_UINT (it->data);
230 atom_ftyp_new (AtomsContext * context, guint32 major, guint32 version,
233 AtomFTYP *ftyp = g_new0 (AtomFTYP, 1);
235 atom_ftyp_init (ftyp, major, version, brands);
240 atom_ftyp_free (AtomFTYP * ftyp)
242 atom_clear (&ftyp->header);
243 g_free (ftyp->compatible_brands);
244 ftyp->compatible_brands = NULL;
249 atom_esds_init (AtomESDS * esds)
251 guint8 flags[3] = { 0, 0, 0 };
253 atom_full_init (&esds->header, FOURCC_esds, 0, 0, 0, flags);
254 desc_es_init (&esds->es);
260 AtomESDS *esds = g_new0 (AtomESDS, 1);
262 atom_esds_init (esds);
267 atom_esds_free (AtomESDS * esds)
269 atom_full_clear (&esds->header);
270 desc_es_descriptor_clear (&esds->es);
277 AtomFRMA *frma = g_new0 (AtomFRMA, 1);
279 atom_header_set (&frma->header, FOURCC_frma, 0, 0);
284 atom_frma_free (AtomFRMA * frma)
286 atom_clear (&frma->header);
293 AtomWAVE *wave = g_new0 (AtomWAVE, 1);
295 atom_header_set (&wave->header, FOURCC_wave, 0, 0);
300 atom_wave_free (AtomWAVE * wave)
302 atom_clear (&wave->header);
303 atom_info_list_free (wave->extension_atoms);
308 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
310 atom_header_set (&se->header, type, 0, 0);
312 memset (se->reserved, 0, sizeof (guint8) * 6);
313 se->data_reference_index = 0;
317 atom_sample_entry_free (SampleTableEntry * se)
319 atom_clear (&se->header);
323 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
325 atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
328 mp4a->revision_level = 0;
331 mp4a->sample_size = 16;
332 mp4a->compression_id = 0;
333 mp4a->packet_size = 0;
334 mp4a->sample_rate = 0;
335 /* following only used if version is 1 */
336 mp4a->samples_per_packet = 0;
337 mp4a->bytes_per_packet = 0;
338 mp4a->bytes_per_frame = 0;
339 mp4a->bytes_per_sample = 0;
341 mp4a->extension_atoms = NULL;
344 static SampleTableEntryMP4A *
345 sample_entry_mp4a_new ()
347 SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
349 sample_entry_mp4a_init (mp4a);
354 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
356 atom_sample_entry_free (&mp4a->se);
357 atom_info_list_free (mp4a->extension_atoms);
362 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
364 atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
367 mp4v->revision_level = 0;
370 mp4v->temporal_quality = 0;
371 mp4v->spatial_quality = 0;
373 /* qt and ISO base media do not contradict, and examples agree */
374 mp4v->horizontal_resolution = 0x00480000;
375 mp4v->vertical_resolution = 0x00480000;
378 mp4v->frame_count = 1;
380 memset (mp4v->compressor, 0, sizeof (guint8) * 32);
383 mp4v->color_table_id = 0;
385 mp4v->extension_atoms = NULL;
389 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
391 atom_sample_entry_free (&mp4v->se);
392 atom_info_list_free (mp4v->extension_atoms);
396 static SampleTableEntryMP4V *
397 sample_entry_mp4v_new (AtomsContext * context)
399 SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
401 sample_entry_mp4v_init (mp4v, context);
406 atom_stsd_init (AtomSTSD * stsd)
408 guint8 flags[3] = { 0, 0, 0 };
410 atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
411 stsd->entries = NULL;
415 atom_stsd_clear (AtomSTSD * stsd)
419 atom_full_clear (&stsd->header);
420 walker = stsd->entries;
423 SampleTableEntry *se = (SampleTableEntry *) aux->data;
425 walker = g_list_next (walker);
426 stsd->entries = g_list_remove_link (stsd->entries, aux);
430 sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
433 sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
436 /* best possible cleanup */
437 atom_sample_entry_free (se);
444 atom_ctts_init (AtomCTTS * ctts)
446 guint8 flags[3] = { 0, 0, 0 };
448 atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
449 ctts->entries = NULL;
455 AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
457 atom_ctts_init (ctts);
462 atom_ctts_free (AtomCTTS * ctts)
466 atom_full_clear (&ctts->header);
467 walker = ctts->entries;
471 walker = g_list_next (walker);
472 ctts->entries = g_list_remove_link (ctts->entries, aux);
473 g_free ((CTTSEntry *) aux->data);
480 atom_stts_init (AtomSTTS * stts)
482 guint8 flags[3] = { 0, 0, 0 };
484 atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
485 stts->entries = NULL;
489 atom_stts_clear (AtomSTTS * stts)
493 atom_full_clear (&stts->header);
494 walker = stts->entries;
498 walker = g_list_next (walker);
499 stts->entries = g_list_remove_link (stts->entries, aux);
500 g_free ((STTSEntry *) aux->data);
507 atom_stsz_init (AtomSTSZ * stsz)
509 guint8 flags[3] = { 0, 0, 0 };
511 atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
512 stsz->sample_size = 0;
513 stsz->table_size = 0;
514 stsz->entries = NULL;
518 atom_stsz_clear (AtomSTSZ * stsz)
520 atom_full_clear (&stsz->header);
521 g_list_free (stsz->entries);
522 stsz->entries = NULL;
523 stsz->table_size = 0;
527 atom_stsc_init (AtomSTSC * stsc)
529 guint8 flags[3] = { 0, 0, 0 };
531 atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
532 stsc->entries = NULL;
537 atom_stsc_clear (AtomSTSC * stsc)
541 atom_full_clear (&stsc->header);
542 walker = stsc->entries;
546 walker = g_list_next (walker);
547 stsc->entries = g_list_remove_link (stsc->entries, aux);
548 g_free ((STSCEntry *) aux->data);
555 atom_co64_init (AtomSTCO64 * co64)
557 guint8 flags[3] = { 0, 0, 0 };
559 atom_full_init (&co64->header, FOURCC_co64, 0, 0, 0, flags);
560 co64->entries = NULL;
565 atom_stco64_clear (AtomSTCO64 * stco64)
569 atom_full_clear (&stco64->header);
570 walker = stco64->entries;
574 walker = g_list_next (walker);
575 stco64->entries = g_list_remove_link (stco64->entries, aux);
576 g_free ((guint64 *) aux->data);
579 stco64->n_entries = 0;
583 atom_stss_init (AtomSTSS * stss)
585 guint8 flags[3] = { 0, 0, 0 };
587 atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
588 stss->entries = NULL;
593 atom_stss_clear (AtomSTSS * stss)
595 atom_full_clear (&stss->header);
596 g_list_free (stss->entries);
597 stss->entries = NULL;
602 atom_stbl_init (AtomSTBL * stbl)
604 atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
606 atom_stts_init (&stbl->stts);
607 atom_stss_init (&stbl->stss);
608 atom_stsd_init (&stbl->stsd);
609 atom_stsz_init (&stbl->stsz);
610 atom_stsc_init (&stbl->stsc);
613 atom_co64_init (&stbl->stco64);
617 atom_stbl_clear (AtomSTBL * stbl)
619 atom_clear (&stbl->header);
620 atom_stsd_clear (&stbl->stsd);
621 atom_stts_clear (&stbl->stts);
622 atom_stss_clear (&stbl->stss);
623 atom_stsc_clear (&stbl->stsc);
624 atom_stsz_clear (&stbl->stsz);
626 atom_ctts_free (stbl->ctts);
628 atom_stco64_clear (&stbl->stco64);
632 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
634 guint8 flags[3] = { 0, 0, 1 };
636 atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
637 vmhd->graphics_mode = 0x0;
638 memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
640 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
641 vmhd->graphics_mode = 0x40;
642 vmhd->opcolor[0] = 32768;
643 vmhd->opcolor[1] = 32768;
644 vmhd->opcolor[2] = 32768;
649 atom_vmhd_new (AtomsContext * context)
651 AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
653 atom_vmhd_init (vmhd, context);
658 atom_vmhd_free (AtomVMHD * vmhd)
660 atom_full_clear (&vmhd->header);
665 atom_smhd_init (AtomSMHD * smhd)
667 guint8 flags[3] = { 0, 0, 0 };
669 atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
677 AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
679 atom_smhd_init (smhd);
684 atom_smhd_free (AtomSMHD * smhd)
686 atom_full_clear (&smhd->header);
691 atom_hmhd_free (AtomHMHD * hmhd)
693 atom_full_clear (&hmhd->header);
698 atom_hdlr_init (AtomHDLR * hdlr)
700 guint8 flags[3] = { 0, 0, 0 };
702 atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
704 hdlr->component_type = 0;
705 hdlr->handler_type = 0;
706 hdlr->manufacturer = 0;
708 hdlr->flags_mask = 0;
709 hdlr->name = g_strdup ("");
715 AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
717 atom_hdlr_init (hdlr);
722 atom_hdlr_clear (AtomHDLR * hdlr)
724 atom_full_clear (&hdlr->header);
732 atom_hdlr_free (AtomHDLR * hdlr)
734 atom_hdlr_clear (hdlr);
739 atom_url_init (AtomURL * url)
741 guint8 flags[3] = { 0, 0, 1 };
743 atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
744 url->location = NULL;
748 atom_url_free (AtomURL * url)
750 atom_full_clear (&url->header);
752 g_free (url->location);
753 url->location = NULL;
761 AtomURL *url = g_new0 (AtomURL, 1);
770 guint8 flags[3] = { 0, 0, 1 };
771 AtomFull *alis = g_new0 (AtomFull, 1);
773 atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
778 atom_dref_init (AtomDREF * dref, AtomsContext * context)
780 guint8 flags[3] = { 0, 0, 0 };
782 atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
784 /* in either case, alis or url init arranges to set self-contained flag */
785 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
786 /* alis dref for qt */
787 AtomFull *alis = atom_alis_new ();
788 dref->entries = g_list_append (dref->entries, alis);
790 /* url for iso spec, as 'alis' not specified there */
791 AtomURL *url = atom_url_new ();
792 dref->entries = g_list_append (dref->entries, url);
797 atom_dref_clear (AtomDREF * dref)
801 atom_full_clear (&dref->header);
802 walker = dref->entries;
805 Atom *atom = (Atom *) aux->data;
807 walker = g_list_next (walker);
808 dref->entries = g_list_remove_link (dref->entries, aux);
809 switch (atom->type) {
811 atom_full_free ((AtomFull *) atom);
814 atom_url_free ((AtomURL *) atom);
817 /* we do nothing, better leak than crash */
825 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
827 atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
828 atom_dref_init (&dinf->dref, context);
832 atom_dinf_clear (AtomDINF * dinf)
834 atom_clear (&dinf->header);
835 atom_dref_clear (&dinf->dref);
839 atom_minf_init (AtomMINF * minf, AtomsContext * context)
841 atom_header_set (&minf->header, FOURCC_minf, 0, 0);
847 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
848 minf->hdlr = atom_hdlr_new ();
849 minf->hdlr->component_type = FOURCC_dhlr;
850 minf->hdlr->handler_type = FOURCC_alis;
854 atom_dinf_init (&minf->dinf, context);
855 atom_stbl_init (&minf->stbl);
859 atom_minf_clear_handlers (AtomMINF * minf)
862 atom_vmhd_free (minf->vmhd);
866 atom_smhd_free (minf->smhd);
870 atom_hmhd_free (minf->hmhd);
876 atom_minf_clear (AtomMINF * minf)
878 atom_clear (&minf->header);
879 atom_minf_clear_handlers (minf);
881 atom_hdlr_free (minf->hdlr);
883 atom_dinf_clear (&minf->dinf);
884 atom_stbl_clear (&minf->stbl);
888 atom_mdhd_init (AtomMDHD * mdhd)
890 guint8 flags[3] = { 0, 0, 0 };
892 atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
893 common_time_info_init (&mdhd->time_info);
894 mdhd->language_code = 0;
899 atom_mdhd_clear (AtomMDHD * mdhd)
901 atom_full_clear (&mdhd->header);
905 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
907 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
909 atom_mdhd_init (&mdia->mdhd);
910 atom_hdlr_init (&mdia->hdlr);
911 atom_minf_init (&mdia->minf, context);
915 atom_mdia_clear (AtomMDIA * mdia)
917 atom_clear (&mdia->header);
918 atom_mdhd_clear (&mdia->mdhd);
919 atom_hdlr_clear (&mdia->hdlr);
920 atom_minf_clear (&mdia->minf);
924 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
929 * 2 -> track in movie
930 * 4 -> track in preview
932 guint8 flags[3] = { 0, 0, 7 };
934 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
936 tkhd->creation_time = tkhd->modification_time = get_current_qt_time ();
941 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
943 tkhd->alternate_group = 0;
946 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
947 tkhd->matrix[0] = 1 << 16;
948 tkhd->matrix[4] = 1 << 16;
949 tkhd->matrix[8] = 16384 << 16;
955 atom_tkhd_clear (AtomTKHD * tkhd)
957 atom_full_clear (&tkhd->header);
961 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
963 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
965 atom_tkhd_init (&trak->tkhd, context);
966 atom_mdia_init (&trak->mdia, context);
970 atom_trak_new (AtomsContext * context)
972 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
974 atom_trak_init (trak, context);
979 atom_trak_free (AtomTRAK * trak)
981 atom_clear (&trak->header);
982 atom_tkhd_clear (&trak->tkhd);
983 atom_mdia_clear (&trak->mdia);
988 atom_ilst_init (AtomILST * ilst)
990 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
991 ilst->entries = NULL;
997 AtomILST *ilst = g_new0 (AtomILST, 1);
999 atom_ilst_init (ilst);
1004 atom_ilst_free (AtomILST * ilst)
1007 atom_info_list_free (ilst->entries);
1008 atom_clear (&ilst->header);
1013 atom_meta_init (AtomMETA * meta)
1015 guint8 flags[3] = { 0, 0, 0 };
1017 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1018 atom_hdlr_init (&meta->hdlr);
1019 /* FIXME (ISOM says this is always 0) */
1020 meta->hdlr.component_type = FOURCC_mhlr;
1021 meta->hdlr.handler_type = FOURCC_mdir;
1028 AtomMETA *meta = g_new0 (AtomMETA, 1);
1030 atom_meta_init (meta);
1035 atom_meta_free (AtomMETA * meta)
1037 atom_full_clear (&meta->header);
1038 atom_hdlr_clear (&meta->hdlr);
1040 atom_ilst_free (meta->ilst);
1046 atom_udta_init (AtomUDTA * udta)
1048 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1055 AtomUDTA *udta = g_new0 (AtomUDTA, 1);
1057 atom_udta_init (udta);
1062 atom_udta_free (AtomUDTA * udta)
1064 atom_clear (&udta->header);
1066 atom_meta_free (udta->meta);
1069 atom_info_list_free (udta->entries);
1074 atom_tag_data_init (AtomTagData * data)
1076 guint8 flags[] = { 0, 0, 0 };
1078 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1082 atom_tag_data_clear (AtomTagData * data)
1084 atom_full_clear (&data->header);
1085 g_free (data->data);
1090 * Fourcc is the tag fourcc
1091 * flags will be truncated to 24bits
1094 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1096 AtomTag *tag = g_new0 (AtomTag, 1);
1098 tag->header.type = fourcc;
1099 atom_tag_data_init (&tag->data);
1100 tag->data.header.flags[2] = flags_as_uint & 0xFF;
1101 tag->data.header.flags[1] = flags_as_uint & 0xFF00;
1102 tag->data.header.flags[0] = flags_as_uint & 0xFF0000;
1107 atom_tag_free (AtomTag * tag)
1109 atom_clear (&tag->header);
1110 atom_tag_data_clear (&tag->data);
1115 atom_mvhd_init (AtomMVHD * mvhd)
1117 guint8 flags[3] = { 0, 0, 0 };
1119 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1121 common_time_info_init (&mvhd->time_info);
1123 mvhd->prefered_rate = 1 << 16;
1124 mvhd->volume = 1 << 8;
1125 mvhd->reserved3 = 0;
1126 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1128 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1129 mvhd->matrix[0] = 1 << 16;
1130 mvhd->matrix[4] = 1 << 16;
1131 mvhd->matrix[8] = 16384 << 16;
1133 mvhd->preview_time = 0;
1134 mvhd->preview_duration = 0;
1135 mvhd->poster_time = 0;
1136 mvhd->selection_time = 0;
1137 mvhd->selection_duration = 0;
1138 mvhd->current_time = 0;
1140 mvhd->next_track_id = 1;
1144 atom_mvhd_clear (AtomMVHD * mvhd)
1146 atom_full_clear (&mvhd->header);
1150 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1152 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1153 atom_mvhd_init (&(moov->mvhd));
1156 moov->context = *context;
1160 atom_moov_new (AtomsContext * context)
1162 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1164 atom_moov_init (moov, context);
1169 atom_moov_free (AtomMOOV * moov)
1173 atom_clear (&moov->header);
1174 atom_mvhd_clear (&moov->mvhd);
1176 walker = moov->traks;
1178 atom_trak_free ((AtomTRAK *) walker->data);
1179 walker = g_list_next (walker);
1181 g_list_free (moov->traks);
1185 atom_udta_free (moov->udta);
1192 /* -- end of init / free -- */
1194 /* -- copy data functions -- */
1197 atom_full_get_version (AtomFull * full)
1199 return full->version;
1203 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1204 guint8 ** buffer, guint64 * size, guint64 * offset)
1206 guint64 original_offset = *offset;
1209 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1210 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1211 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1212 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1214 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1215 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1216 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1217 prop_copy_uint64 (ti->duration, buffer, size, offset);
1219 return *offset - original_offset;
1223 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1226 /* this only works for non-extended atom size, which is OK
1227 * (though it could be made to do mem_move, etc and write extended size) */
1228 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1232 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1234 guint64 original_offset = *offset;
1236 /* copies type and size */
1237 prop_copy_uint32 (atom->size, buffer, size, offset);
1238 prop_copy_fourcc (atom->type, buffer, size, offset);
1240 /* extended size needed */
1241 if (atom->size == 1) {
1242 /* really should not happen other than with mdat atom;
1243 * would be a problem for size (re)write code, not to mention memory */
1244 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1245 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1247 /* just in case some trivially derived atom does not do so */
1248 atom_write_size (buffer, size, offset, original_offset);
1251 return *offset - original_offset;
1255 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1258 guint64 original_offset = *offset;
1260 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1264 prop_copy_uint8 (atom->version, buffer, size, offset);
1265 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1267 atom_write_size (buffer, size, offset, original_offset);
1268 return *offset - original_offset;
1272 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1275 guint64 original_offset = *offset;
1278 AtomInfo *info = (AtomInfo *) ai->data;
1280 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1283 ai = g_list_next (ai);
1286 return *offset - original_offset;
1290 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1293 guint64 original_offset = *offset;
1295 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1299 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1301 atom_write_size (buffer, size, offset, original_offset);
1302 return *offset - original_offset;
1306 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1309 guint64 original_offset = *offset;
1311 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1314 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1315 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1317 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1318 buffer, size, offset);
1320 atom_write_size (buffer, size, offset, original_offset);
1321 return *offset - original_offset;
1325 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1329 guint64 original_offset = *offset;
1331 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1335 version = atom_full_get_version (&(atom->header));
1337 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1338 } else if (version == 1) {
1339 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1341 *offset = original_offset;
1345 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1346 prop_copy_uint16 (atom->volume, buffer, size, offset);
1347 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1348 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1349 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1350 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1351 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1352 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1353 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1354 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1355 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1357 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1359 atom_write_size (buffer, size, offset, original_offset);
1360 return *offset - original_offset;
1364 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1367 guint64 original_offset = *offset;
1369 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1373 if (atom_full_get_version (&tkhd->header) == 0) {
1374 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1375 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1376 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1377 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1378 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1380 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1381 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1382 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1383 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1384 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1387 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1388 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1389 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1390 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1391 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1392 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1394 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1395 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1397 atom_write_size (buffer, size, offset, original_offset);
1398 return *offset - original_offset;
1402 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1405 guint64 original_offset = *offset;
1407 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1411 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1412 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1413 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1414 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1415 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1417 prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
1419 atom_write_size (buffer, size, offset, original_offset);
1420 return *offset - original_offset;
1424 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1427 guint64 original_offset = *offset;
1429 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1432 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1433 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1435 atom_write_size (buffer, size, offset, original_offset);
1436 return original_offset - *offset;
1440 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1443 guint64 original_offset = *offset;
1445 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1448 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1449 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1451 atom_write_size (buffer, size, offset, original_offset);
1452 return original_offset - *offset;
1456 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1459 guint64 original_offset = *offset;
1461 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1464 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1465 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1466 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1467 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1468 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1470 atom_write_size (buffer, size, offset, original_offset);
1471 return original_offset - *offset;
1475 atom_url_same_file_flag (AtomURL * url)
1477 return (url->header.flags[2] & 0x1) == 1;
1481 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1484 guint64 original_offset = *offset;
1486 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1490 if (!atom_url_same_file_flag (url)) {
1491 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1494 atom_write_size (buffer, size, offset, original_offset);
1495 return original_offset - *offset;
1499 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1502 guint64 original_offset = *offset;
1505 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1509 prop_copy_uint32 (stts->n_entries, buffer, size, offset);
1510 /* minimize realloc */
1511 prop_copy_ensure_buffer (buffer, size, offset, 8 * stts->n_entries);
1512 for (walker = g_list_last (stts->entries); walker != NULL;
1513 walker = g_list_previous (walker)) {
1514 STTSEntry *entry = (STTSEntry *) walker->data;
1516 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
1517 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
1520 atom_write_size (buffer, size, offset, original_offset);
1521 return *offset - original_offset;
1525 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
1526 guint64 * size, guint64 * offset)
1528 guint64 original_offset = *offset;
1530 if (!atom_copy_data (&se->header, buffer, size, offset)) {
1534 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
1535 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
1537 return *offset - original_offset;
1541 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
1544 guint64 original_offset = *offset;
1546 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
1549 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
1553 atom_write_size (buffer, size, offset, original_offset);
1554 return *offset - original_offset;
1558 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
1559 guint64 * size, guint64 * offset)
1561 guint64 original_offset = *offset;
1563 if (!atom_copy_data (&(frma->header), buffer, size, offset))
1566 prop_copy_fourcc (frma->media_type, buffer, size, offset);
1568 atom_write_size (buffer, size, offset, original_offset);
1569 return *offset - original_offset;
1573 atom_mp4s_copy_data (SampleTableEntryMP4S * mp4s, guint8 ** buffer,
1574 guint64 * size, guint64 * offset)
1576 guint64 original_offset = *offset;
1578 if (!atom_sample_entry_copy_data (&mp4s->se, buffer, size, offset)) {
1581 if (!atom_esds_copy_data (&mp4s->es, buffer, size, offset)) {
1585 atom_write_size (buffer, size, offset, original_offset);
1586 return *offset - original_offset;
1590 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
1591 guint64 * size, guint64 * offset)
1593 guint64 original_offset = *offset;
1595 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
1599 prop_copy_uint32 (hse->size, buffer, size, offset);
1600 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
1602 atom_write_size (buffer, size, offset, original_offset);
1603 return *offset - original_offset;
1607 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
1608 guint64 * size, guint64 * offset)
1610 guint64 original_offset = *offset;
1612 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
1616 prop_copy_uint16 (mp4a->version, buffer, size, offset);
1617 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
1618 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
1619 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
1620 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
1621 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
1622 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
1623 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
1625 /* this should always be 0 for mp4 flavor */
1626 if (mp4a->version == 1) {
1627 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
1628 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
1629 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
1630 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
1633 if (mp4a->extension_atoms) {
1634 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
1638 atom_write_size (buffer, size, offset, original_offset);
1639 return *offset - original_offset;
1643 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
1644 guint64 * size, guint64 * offset)
1646 guint64 original_offset = *offset;
1648 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
1652 prop_copy_uint16 (mp4v->version, buffer, size, offset);
1653 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
1654 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
1655 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
1656 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
1658 prop_copy_uint16 (mp4v->width, buffer, size, offset);
1659 prop_copy_uint16 (mp4v->height, buffer, size, offset);
1661 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
1662 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
1663 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
1665 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
1667 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
1670 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
1671 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
1674 if (mp4v->extension_atoms &&
1675 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
1678 atom_write_size (buffer, size, offset, original_offset);
1679 return *offset - original_offset;
1683 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
1686 guint64 original_offset = *offset;
1689 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
1693 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
1694 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
1695 /* minimize realloc */
1696 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
1697 if (stsz->sample_size == 0) {
1698 for (walker = g_list_last (stsz->entries); walker != NULL;
1699 walker = g_list_previous (walker)) {
1700 prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
1705 atom_write_size (buffer, size, offset, original_offset);
1706 return *offset - original_offset;
1710 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
1713 guint64 original_offset = *offset;
1716 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
1720 prop_copy_uint32 (stsc->n_entries, buffer, size, offset);
1721 /* minimize realloc */
1722 prop_copy_ensure_buffer (buffer, size, offset, 12 * stsc->n_entries);
1724 for (walker = g_list_last (stsc->entries); walker != NULL;
1725 walker = g_list_previous (walker)) {
1726 STSCEntry *entry = (STSCEntry *) walker->data;
1728 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
1729 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
1730 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
1733 atom_write_size (buffer, size, offset, original_offset);
1734 return *offset - original_offset;
1738 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
1741 guint64 original_offset = *offset;
1744 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
1748 prop_copy_uint32 (ctts->n_entries, buffer, size, offset);
1749 /* minimize realloc */
1750 prop_copy_ensure_buffer (buffer, size, offset, 8 * ctts->n_entries);
1751 for (walker = g_list_last (ctts->entries); walker != NULL;
1752 walker = g_list_previous (walker)) {
1753 CTTSEntry *entry = (CTTSEntry *) walker->data;
1755 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
1756 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
1759 atom_write_size (buffer, size, offset, original_offset);
1760 return *offset - original_offset;
1764 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
1767 guint64 original_offset = *offset;
1769 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
1771 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
1775 prop_copy_uint32 (stco64->n_entries, buffer, size, offset);
1777 /* minimize realloc */
1778 prop_copy_ensure_buffer (buffer, size, offset, 8 * stco64->n_entries);
1779 for (walker = g_list_last (stco64->entries); walker != NULL;
1780 walker = g_list_previous (walker)) {
1781 guint64 *value = (guint64 *) walker->data;
1784 prop_copy_uint32 ((guint32) * value, buffer, size, offset);
1786 prop_copy_uint64 (*value, buffer, size, offset);
1790 atom_write_size (buffer, size, offset, original_offset);
1791 return *offset - original_offset;
1795 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
1798 guint64 original_offset = *offset;
1801 if (stss->entries == NULL) {
1802 /* FIXME not needing this atom might be confused with error while copying */
1806 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
1810 prop_copy_uint32 (stss->n_entries, buffer, size, offset);
1811 /* minimize realloc */
1812 prop_copy_ensure_buffer (buffer, size, offset, 4 * stss->n_entries);
1813 for (walker = g_list_last (stss->entries); walker != NULL;
1814 walker = g_list_previous (walker)) {
1815 prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
1819 atom_write_size (buffer, size, offset, original_offset);
1820 return *offset - original_offset;
1824 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
1827 guint64 original_offset = *offset;
1830 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
1834 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
1836 for (walker = g_list_last (stsd->entries); walker != NULL;
1837 walker = g_list_previous (walker)) {
1838 SampleTableEntry *se = (SampleTableEntry *) walker->data;
1840 switch (((Atom *) walker->data)->type) {
1842 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
1843 buffer, size, offset)) {
1848 if (!atom_mp4s_copy_data ((SampleTableEntryMP4S *) walker->data,
1849 buffer, size, offset)) {
1854 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
1855 buffer, size, offset)) {
1860 if (se->kind == VIDEO) {
1861 size += sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
1862 walker->data, buffer, size, offset);
1863 } else if (se->kind == AUDIO) {
1864 size += sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
1865 walker->data, buffer, size, offset);
1867 if (!atom_hint_sample_entry_copy_data (
1868 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
1876 atom_write_size (buffer, size, offset, original_offset);
1877 return *offset - original_offset;
1881 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
1884 guint64 original_offset = *offset;
1886 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
1890 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
1893 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
1896 /* this atom is optional, so let's check if we need it
1897 * (to avoid false error) */
1898 if (stbl->stss.entries) {
1899 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
1904 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
1907 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
1911 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
1915 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
1919 atom_write_size (buffer, size, offset, original_offset);
1920 return original_offset - *offset;
1925 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
1928 guint64 original_offset = *offset;
1931 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
1935 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
1937 walker = dref->entries;
1938 while (walker != NULL) {
1939 Atom *atom = (Atom *) walker->data;
1941 if (atom->type == FOURCC_url_) {
1942 atom_url_copy_data ((AtomURL *) atom, buffer, size, offset);
1943 } else if (atom->type == FOURCC_alis) {
1944 atom_full_copy_data ((AtomFull *) atom, buffer, size, offset);
1946 g_error ("Unsupported atom used inside dref atom");
1948 walker = g_list_next (walker);
1951 atom_write_size (buffer, size, offset, original_offset);
1952 return *offset - original_offset;
1956 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
1959 guint64 original_offset = *offset;
1961 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
1965 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
1969 atom_write_size (buffer, size, offset, original_offset);
1970 return original_offset - *offset;
1974 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
1977 guint64 original_offset = *offset;
1979 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
1984 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
1987 } else if (minf->smhd) {
1988 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
1991 } else if (minf->hmhd) {
1992 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
1998 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2003 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2006 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2010 atom_write_size (buffer, size, offset, original_offset);
2011 return *offset - original_offset;
2015 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2018 guint64 original_offset = *offset;
2020 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2024 if (!common_time_info_copy_data (&mdhd->time_info,
2025 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2029 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2030 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2032 atom_write_size (buffer, size, offset, original_offset);
2033 return *offset - original_offset;
2037 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2040 guint64 original_offset = *offset;
2042 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2045 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2048 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2052 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2056 atom_write_size (buffer, size, offset, original_offset);
2057 return *offset - original_offset;
2061 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2064 guint64 original_offset = *offset;
2066 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2069 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2073 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2077 atom_write_size (buffer, size, offset, original_offset);
2078 return *offset - original_offset;
2082 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2085 guint64 original_offset = *offset;
2087 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2091 prop_copy_uint32 (data->reserved, buffer, size, offset);
2092 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2094 atom_write_size (buffer, size, offset, original_offset);
2095 return *offset - original_offset;
2099 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2102 guint64 original_offset = *offset;
2104 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2108 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2112 atom_write_size (buffer, size, offset, original_offset);
2113 return *offset - original_offset;
2117 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2120 guint64 original_offset = *offset;
2122 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2126 if (ilst->entries &&
2127 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2130 atom_write_size (buffer, size, offset, original_offset);
2131 return *offset - original_offset;
2135 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2138 guint64 original_offset = *offset;
2140 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2143 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2147 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2152 atom_write_size (buffer, size, offset, original_offset);
2153 return *offset - original_offset;
2157 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2160 guint64 original_offset = *offset;
2162 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2166 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2169 } else if (udta->entries) {
2171 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2175 atom_write_size (buffer, size, offset, original_offset);
2176 return *offset - original_offset;
2180 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2183 guint64 original_offset = *offset;
2186 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2189 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2192 walker = g_list_first (atom->traks);
2193 while (walker != NULL) {
2194 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2197 walker = g_list_next (walker);
2201 if (!atom_udta_copy_data (atom->udta, buffer, size, offset)) {
2206 atom_write_size (buffer, size, offset, original_offset);
2207 return *offset - original_offset;
2211 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
2212 guint64 * size, guint64 * offset)
2214 guint64 original_offset = *offset;
2216 if (!atom_copy_data (&(wave->header), buffer, size, offset))
2219 if (wave->extension_atoms) {
2220 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
2224 atom_write_size (buffer, size, offset, original_offset);
2225 return *offset - original_offset;
2228 /* -- end of copy data functions -- */
2230 /* -- general functions, API and support functions */
2232 /* add samples to tables */
2235 stsc_entry_new (guint32 first_chunk, guint32 samples, guint32 desc_index)
2237 STSCEntry *entry = g_new0 (STSCEntry, 1);
2239 entry->first_chunk = first_chunk;
2240 entry->samples_per_chunk = samples;
2241 entry->sample_description_index = desc_index;
2246 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
2248 if (stsc->entries &&
2249 ((STSCEntry *) stsc->entries->data)->samples_per_chunk == nsamples)
2253 g_list_prepend (stsc->entries, stsc_entry_new (first_chunk, nsamples, 1));
2258 stts_entry_new (guint32 sample_count, gint32 sample_delta)
2260 STTSEntry *entry = g_new0 (STTSEntry, 1);
2262 entry->sample_count = sample_count;
2263 entry->sample_delta = sample_delta;
2268 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
2272 if (stts->entries == NULL) {
2274 g_list_prepend (stts->entries, stts_entry_new (sample_count,
2279 entry = (STTSEntry *) g_list_first (stts->entries)->data;
2280 if (entry->sample_delta == sample_delta) {
2281 entry->sample_count += sample_count;
2284 g_list_prepend (stts->entries, stts_entry_new (sample_count,
2291 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
2295 stsz->table_size += nsamples;
2296 if (stsz->sample_size != 0) {
2297 /* it is constant size, we don't need entries */
2300 for (i = 0; i < nsamples; i++) {
2301 stsz->entries = g_list_prepend (stsz->entries, GUINT_TO_POINTER (size));
2306 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
2308 return stco64->n_entries;
2312 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
2314 guint64 *pont = g_new (guint64, 1);
2317 stco64->entries = g_list_prepend (stco64->entries, pont);
2318 stco64->n_entries++;
2322 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
2324 stss->entries = g_list_prepend (stss->entries, GUINT_TO_POINTER (sample));
2329 atom_stbl_add_stss_entry (AtomSTBL * stbl)
2331 guint32 sample_index = stbl->stsz.table_size;
2333 atom_stss_add_entry (&stbl->stss, sample_index);
2337 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
2342 walker = g_list_first (ctts->entries);
2343 entry = (walker == NULL) ? NULL : (CTTSEntry *) walker->data;
2345 if (entry == NULL || entry->sampleoffset != offset) {
2346 CTTSEntry *entry = g_new0 (CTTSEntry, 1);
2348 entry->samplecount = nsamples;
2349 entry->sampleoffset = offset;
2350 ctts->entries = g_list_prepend (ctts->entries, entry);
2353 entry->samplecount += nsamples;
2358 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
2360 if (stbl->ctts == NULL) {
2361 stbl->ctts = atom_ctts_new ();
2363 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
2367 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
2368 guint32 size, guint64 chunk_offset, gboolean sync,
2369 gboolean do_pts, gint64 pts_offset)
2371 AtomSTBL *stbl = &trak->mdia.minf.stbl;
2373 atom_stts_add_entry (&stbl->stts, nsamples, delta);
2374 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
2375 atom_stco64_add_entry (&stbl->stco64, chunk_offset);
2376 atom_stsc_add_new_entry (&stbl->stsc,
2377 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
2379 atom_stbl_add_stss_entry (stbl);
2381 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
2384 /* trak and moov molding */
2387 atom_trak_get_timescale (AtomTRAK * trak)
2389 return trak->mdia.mdhd.time_info.timescale;
2393 atom_trak_set_id (AtomTRAK * trak, guint32 id)
2395 trak->tkhd.track_ID = id;
2399 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
2401 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
2402 moov->traks = g_list_append (moov->traks, trak);
2406 atom_trak_get_duration (AtomTRAK * trak)
2408 return trak->tkhd.duration;
2412 atom_stts_get_total_duration (AtomSTTS * stts)
2414 GList *walker = stts->entries;
2418 STTSEntry *entry = (STTSEntry *) walker->data;
2420 sum += (guint64) (entry->sample_count) * entry->sample_delta;
2421 walker = g_list_next (walker);
2427 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
2429 trak->mdia.mdhd.time_info.duration =
2430 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
2431 trak->tkhd.duration =
2432 gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration, moov_timescale,
2433 trak->mdia.mdhd.time_info.timescale);
2437 atom_moov_get_timescale (AtomMOOV * moov)
2439 return moov->mvhd.time_info.timescale;
2443 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
2445 moov->mvhd.time_info.timescale = timescale;
2449 atom_moov_update_duration (AtomMOOV * moov)
2451 GList *traks = moov->traks;
2452 guint64 dur, duration = 0;
2455 AtomTRAK *trak = (AtomTRAK *) traks->data;
2457 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
2458 dur = atom_trak_get_duration (trak);
2461 traks = g_list_next (traks);
2463 moov->mvhd.time_info.duration = duration;
2467 atom_set_type (Atom * atom, guint32 fourcc)
2469 atom->type = fourcc;
2473 atom_stbl_set_64bits (AtomSTBL * stbl, gboolean use)
2476 atom_set_type (&stbl->stco64.header.header, FOURCC_co64);
2478 atom_set_type (&stbl->stco64.header.header, FOURCC_stco);
2483 atom_trak_set_64bits (AtomTRAK * trak, gboolean use)
2485 atom_stbl_set_64bits (&trak->mdia.minf.stbl, use);
2489 atom_moov_set_64bits (AtomMOOV * moov, gboolean large_file)
2491 GList *traks = moov->traks;
2494 AtomTRAK *trak = (AtomTRAK *) traks->data;
2496 atom_trak_set_64bits (trak, large_file);
2497 traks = g_list_next (traks);
2502 atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset)
2504 GList *entries = stco64->entries;
2507 guint64 *value = (guint64 *) entries->data;
2510 entries = g_list_next (entries);
2515 atom_moov_chunks_add_offset (AtomMOOV * moov, guint32 offset)
2517 GList *traks = moov->traks;
2520 AtomTRAK *trak = (AtomTRAK *) traks->data;
2522 atom_stco64_chunks_add_offset (&trak->mdia.minf.stbl.stco64, offset);
2523 traks = g_list_next (traks);
2528 * Meta tags functions
2531 atom_moov_init_metatags (AtomMOOV * moov, AtomsContext * context)
2534 moov->udta = atom_udta_new ();
2536 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
2537 if (!moov->udta->meta) {
2538 moov->udta->meta = atom_meta_new ();
2540 if (!moov->udta->meta->ilst) {
2541 moov->udta->meta->ilst = atom_ilst_new ();
2547 atom_tag_data_alloc_data (AtomTagData * data, guint size)
2549 if (data->data != NULL) {
2550 g_free (data->data);
2552 data->data = g_new0 (guint8, size);
2553 data->datalen = size;
2557 atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
2561 atom_moov_init_metatags (moov, &moov->context);
2562 if (moov->udta->meta)
2563 entries = &moov->udta->meta->ilst->entries;
2565 entries = &moov->udta->entries;
2566 *entries = g_list_append (*entries, tag);
2570 atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2571 const guint8 * data, guint size)
2576 tag = atom_tag_new (fourcc, flags);
2578 atom_tag_data_alloc_data (tdata, size);
2579 g_memmove (tdata->data, data, size);
2581 atom_moov_append_tag (moov,
2582 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
2587 atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2589 gint len = strlen (value);
2592 atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
2596 atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2599 guint8 data[8] = { 0, };
2602 GST_WRITE_UINT16_BE (data, value);
2603 atom_moov_add_tag (moov, fourcc, flags, data, 2);
2605 GST_WRITE_UINT32_BE (data + 2, value);
2606 atom_moov_add_tag (moov, fourcc, flags, data, 8);
2611 atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
2613 AtomData *data_atom;
2621 /* blob is unparsed atom;
2622 * extract size and fourcc, and wrap remainder in data atom */
2623 len = GST_READ_UINT32_BE (data);
2624 fourcc = GST_READ_UINT32_LE (data + 4);
2628 buf = gst_buffer_new ();
2629 GST_BUFFER_SIZE (buf) = len - 8;
2630 GST_BUFFER_DATA (buf) = data + 8;
2632 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2633 gst_buffer_unref (buf);
2635 atom_moov_append_tag (moov,
2636 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2641 atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
2644 AtomData *data_atom;
2648 /* need full atom */
2649 buf = gst_buffer_new_and_alloc (size + 4);
2650 bdata = GST_BUFFER_DATA (buf);
2651 /* full atom: version and flags */
2652 GST_WRITE_UINT32_BE (bdata, 0);
2653 memcpy (bdata + 4, data, size);
2655 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2656 gst_buffer_unref (buf);
2658 atom_moov_append_tag (moov,
2659 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2664 language_code (const char *lang)
2666 g_return_val_if_fail (lang != NULL, 0);
2667 g_return_val_if_fail (strlen (lang) == 3, 0);
2669 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
2670 ((lang[2] - 0x60) & 0x1F);
2674 atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc,
2675 const gchar * value, gint16 ivalue)
2677 gint len = 0, size = 0;
2681 len = strlen (value);
2688 data = g_malloc (size + 3);
2689 /* language tag and null-terminated UTF-8 string */
2691 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
2692 /* include 0 terminator */
2693 memcpy (data + 2, value, len + 1);
2695 /* 16-bit unsigned int if standalone, otherwise 8-bit */
2698 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
2700 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
2705 atom_moov_add_3gp_tag (moov, fourcc, data, size);
2710 atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2712 atom_moov_add_3gp_str_int_tag (moov, fourcc, value, -1);
2716 atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value)
2718 atom_moov_add_3gp_str_int_tag (moov, fourcc, NULL, value);
2722 * Functions for specifying media types
2726 atom_minf_set_audio (AtomMINF * minf)
2728 atom_minf_clear_handlers (minf);
2729 minf->smhd = atom_smhd_new ();
2733 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
2735 atom_minf_clear_handlers (minf);
2736 minf->vmhd = atom_vmhd_new (context);
2740 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
2743 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2744 hdlr->component_type = comp_type;
2746 hdlr->handler_type = hdlr_type;
2750 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
2752 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
2756 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
2758 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
2762 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
2764 atom_mdia_set_hdlr_type_audio (mdia, context);
2765 atom_minf_set_audio (&mdia->minf);
2769 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
2771 atom_mdia_set_hdlr_type_video (mdia, context);
2772 atom_minf_set_video (&mdia->minf, context);
2776 atom_tkhd_set_audio (AtomTKHD * tkhd)
2778 tkhd->volume = 0x0100;
2779 tkhd->width = tkhd->height = 0;
2783 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
2788 /* qt and ISO base media do not contradict, and examples agree */
2789 tkhd->width = width;
2790 tkhd->height = height;
2793 /* re-negotiation is prevented at top-level, so only 1 entry expected.
2794 * Quite some more care here and elsewhere may be needed to
2795 * support several entries */
2796 static SampleTableEntryMP4A *
2797 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
2800 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
2801 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
2803 mp4a->se.header.type = type;
2804 mp4a->se.kind = AUDIO;
2805 mp4a->compression_id = -1;
2806 mp4a->se.data_reference_index = 1;
2808 stsd->entries = g_list_prepend (stsd->entries, mp4a);
2813 static SampleTableEntryMP4V *
2814 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
2817 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
2818 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
2820 mp4v->se.header.type = type;
2821 mp4v->se.kind = VIDEO;
2822 mp4v->se.data_reference_index = 1;
2823 mp4v->horizontal_resolution = 72 << 16;
2824 mp4v->vertical_resolution = 72 << 16;
2825 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2826 mp4v->spatial_quality = 512;
2827 mp4v->temporal_quality = 512;
2830 stsd->entries = g_list_prepend (stsd->entries, mp4v);
2836 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
2838 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
2842 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
2844 atom_tkhd_set_audio (&trak->tkhd);
2845 atom_mdia_set_audio (&trak->mdia, context);
2849 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
2852 atom_tkhd_set_video (&trak->tkhd, context, width, height);
2853 atom_mdia_set_video (&trak->mdia, context);
2857 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
2860 atom_trak_set_audio (trak, context);
2861 trak->mdia.mdhd.time_info.timescale = rate;
2865 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
2866 guint32 rate, guint32 width, guint32 height)
2868 atom_trak_set_video (trak, context, width, height);
2869 trak->mdia.mdhd.time_info.timescale = rate;
2870 trak->tkhd.width = width << 16;
2871 trak->tkhd.height = height << 16;
2875 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
2876 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
2878 SampleTableEntryMP4A *ste;
2880 atom_trak_set_audio_commons (trak, context, scale);
2881 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
2883 trak->is_video = FALSE;
2884 trak->is_h264 = FALSE;
2886 ste->version = entry->version;
2887 ste->compression_id = entry->compression_id;
2888 ste->sample_size = entry->sample_size;
2889 ste->sample_rate = entry->sample_rate << 16;
2890 ste->channels = entry->channels;
2892 ste->samples_per_packet = entry->samples_per_packet;
2893 ste->bytes_per_sample = entry->bytes_per_sample;
2894 ste->bytes_per_packet = entry->bytes_per_packet;
2895 ste->bytes_per_frame = entry->bytes_per_frame;
2898 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
2900 /* 0 size means variable size */
2901 atom_trak_set_constant_size_samples (trak, sample_size);
2905 build_pasp_extension (AtomTRAK * trak, gint par_width, gint par_height)
2907 AtomData *atom_data;
2911 buf = gst_buffer_new_and_alloc (8);
2912 data = GST_BUFFER_DATA (buf);
2914 /* ihdr = image header box */
2915 GST_WRITE_UINT32_BE (data, par_width);
2916 GST_WRITE_UINT32_BE (data + 4, par_height);
2918 atom_data = atom_data_new_from_gst_buffer (FOURCC_pasp, buf);
2919 gst_buffer_unref (buf);
2921 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
2926 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
2927 VisualSampleEntry * entry, guint32 scale, AtomInfo * ext)
2929 SampleTableEntryMP4V *ste;
2930 gint dwidth, dheight;
2931 gint par_n = 0, par_d = 0;
2933 if ((entry->par_n != 1 || entry->par_d != 1) &&
2934 (entry->par_n != entry->par_d)) {
2935 par_n = entry->par_n;
2936 par_d = entry->par_d;
2939 dwidth = entry->width;
2940 dheight = entry->height;
2941 /* ISO file spec says track header w/h indicates track's visual presentation
2942 * (so this together with pixels w/h implicitly defines PAR) */
2943 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
2944 if (par_n > par_d) {
2945 dwidth = entry->width * par_n / par_d;
2946 dheight = entry->height;
2948 dwidth = entry->width * par_n / par_d;
2949 dheight = entry->height;
2953 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
2954 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
2956 trak->is_video = TRUE;
2957 trak->is_h264 = (entry->fourcc == FOURCC_avc1);
2959 ste->width = entry->width;
2960 ste->height = entry->height;
2961 ste->depth = entry->depth;
2962 ste->color_table_id = entry->color_table_id;
2963 ste->frame_count = entry->frame_count;
2966 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
2968 /* QT spec has a pasp extension atom in stsd that can hold PAR */
2969 if (par_n && (context->flavor == ATOMS_TREE_FLAVOR_MOV)) {
2970 ste->extension_atoms = g_list_append (ste->extension_atoms,
2971 build_pasp_extension (trak, par_n, par_d));
2975 /* some sample description construction helpers */
2978 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
2979 const GstBuffer * codec_data)
2984 track_id = trak->tkhd.track_ID;
2986 esds = atom_esds_new ();
2987 esds->es.id = track_id & 0xFFFF;
2988 esds->es.dec_conf_desc.object_type = object_type;
2989 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
2991 /* optional DecoderSpecificInfo */
2993 DecoderSpecificInfoDescriptor *desc;
2995 esds->es.dec_conf_desc.dec_specific_info = desc =
2996 desc_dec_specific_info_new ();
2997 desc_dec_specific_info_alloc_data (desc, GST_BUFFER_SIZE (codec_data));
2999 memcpy (desc->data, GST_BUFFER_DATA (codec_data),
3000 GST_BUFFER_SIZE (codec_data));
3003 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
3008 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
3016 track_id = trak->tkhd.track_ID;
3018 /* Add WAVE atom to the MP4A sample table entry */
3019 wave = atom_wave_new ();
3021 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
3022 ext_atom = (Atom *) atom_data_new (FOURCC_null);
3023 wave->extension_atoms =
3024 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
3025 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
3027 /* Add ESDS atom to WAVE */
3028 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
3029 build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
3030 ESDS_STREAM_TYPE_AUDIO, codec_data));
3032 /* Add MP4A atom to the WAVE:
3033 * not really in spec, but makes offset based players happy */
3034 buf = gst_buffer_new_and_alloc (4);
3035 *((guint32 *) GST_BUFFER_DATA (buf)) = 0;
3036 ext_atom = (Atom *) atom_data_new_from_gst_buffer (FOURCC_mp4a, buf);
3037 gst_buffer_unref (buf);
3039 wave->extension_atoms =
3040 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
3041 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
3043 /* Add FRMA to the WAVE */
3044 frma = atom_frma_new ();
3045 frma->media_type = FOURCC_mp4a;
3047 wave->extension_atoms =
3048 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
3049 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
3051 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
3056 build_jp2h_extension (AtomTRAK * trak, gint width, gint height, guint32 fourcc)
3058 AtomData *atom_data;
3063 if (fourcc == GST_MAKE_FOURCC ('s', 'R', 'G', 'B')) {
3065 } else if (fourcc == GST_MAKE_FOURCC ('s', 'Y', 'U', 'V')) {
3070 buf = gst_buffer_new_and_alloc (22 + 15);
3071 data = GST_BUFFER_DATA (buf);
3073 /* ihdr = image header box */
3074 GST_WRITE_UINT32_BE (data, 22);
3075 GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
3076 GST_WRITE_UINT32_BE (data + 8, height);
3077 GST_WRITE_UINT32_BE (data + 12, width);
3078 /* FIXME perhaps parse from stream,
3079 * though exactly 3 in any respectable colourspace */
3080 GST_WRITE_UINT16_BE (data + 16, 3);
3081 /* 8 bits per component, unsigned */
3082 GST_WRITE_UINT8 (data + 18, 0x7);
3083 /* compression type; reserved */
3084 GST_WRITE_UINT8 (data + 19, 0x7);
3085 /* colour space (un)known */
3086 GST_WRITE_UINT8 (data + 20, 0x0);
3087 /* intellectual property right (box present) */
3088 GST_WRITE_UINT8 (data + 21, 0x0);
3090 /* colour specification box */
3092 GST_WRITE_UINT32_BE (data, 15);
3093 GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
3094 /* specification method: enumerated */
3095 GST_WRITE_UINT8 (data + 8, 0x1);
3096 /* precedence; reserved */
3097 GST_WRITE_UINT8 (data + 9, 0x0);
3098 /* approximation; reserved */
3099 GST_WRITE_UINT8 (data + 10, 0x0);
3100 /* enumerated colourspace */
3101 GST_WRITE_UINT32_BE (data + 11, cenum);
3103 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
3104 gst_buffer_unref (buf);
3106 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3111 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
3114 AtomInfo *result = NULL;
3117 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
3118 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
3126 build_amr_extension ()
3132 buf = gst_buffer_new ();
3133 GST_BUFFER_DATA (buf) = ext;
3134 GST_BUFFER_SIZE (buf) = sizeof (ext);
3137 GST_WRITE_UINT32_LE (ext, 0);
3138 /* decoder version */
3139 GST_WRITE_UINT8 (ext + 4, 0);
3140 /* mode set (all modes) */
3141 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
3142 /* mode change period (no restriction) */
3143 GST_WRITE_UINT8 (ext + 7, 0);
3144 /* frames per sample */
3145 GST_WRITE_UINT8 (ext + 8, 1);
3147 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', 'a', 'm', 'r'), buf);
3148 gst_buffer_unref (buf);
3153 build_h263_extension ()
3159 buf = gst_buffer_new ();
3160 GST_BUFFER_DATA (buf) = ext;
3161 GST_BUFFER_SIZE (buf) = sizeof (ext);
3164 GST_WRITE_UINT32_LE (ext, 0);
3165 /* decoder version */
3166 GST_WRITE_UINT8 (ext + 4, 0);
3167 /* level / profile */
3168 /* FIXME ? maybe ? obtain somewhere; baseline for now */
3169 GST_WRITE_UINT8 (ext + 5, 10);
3170 GST_WRITE_UINT8 (ext + 6, 0);
3172 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', '2', '6', '3'), buf);
3173 gst_buffer_unref (buf);