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);
1248 return *offset - original_offset;
1252 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1255 guint64 original_offset = *offset;
1257 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1261 prop_copy_uint8 (atom->version, buffer, size, offset);
1262 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1264 atom_write_size (buffer, size, offset, original_offset);
1265 return *offset - original_offset;
1269 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1272 guint64 original_offset = *offset;
1275 AtomInfo *info = (AtomInfo *) ai->data;
1277 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1280 ai = g_list_next (ai);
1283 return *offset - original_offset;
1287 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1290 guint64 original_offset = *offset;
1292 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1296 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1298 atom_write_size (buffer, size, offset, original_offset);
1299 return *offset - original_offset;
1303 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1306 guint64 original_offset = *offset;
1308 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1311 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1312 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1314 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1315 buffer, size, offset);
1317 atom_write_size (buffer, size, offset, original_offset);
1318 return *offset - original_offset;
1322 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1326 guint64 original_offset = *offset;
1328 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1332 version = atom_full_get_version (&(atom->header));
1334 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1335 } else if (version == 1) {
1336 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1338 *offset = original_offset;
1342 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1343 prop_copy_uint16 (atom->volume, buffer, size, offset);
1344 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1345 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1346 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1347 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1348 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1349 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1350 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1351 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1352 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1354 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1356 atom_write_size (buffer, size, offset, original_offset);
1357 return *offset - original_offset;
1361 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1364 guint64 original_offset = *offset;
1366 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1370 if (atom_full_get_version (&tkhd->header) == 0) {
1371 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1372 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1373 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1374 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1375 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1377 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1378 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1379 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1380 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1381 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1384 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1385 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1386 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1387 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1388 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1389 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1391 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1392 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1394 atom_write_size (buffer, size, offset, original_offset);
1395 return *offset - original_offset;
1399 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1402 guint64 original_offset = *offset;
1404 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1408 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1409 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1410 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1411 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1412 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1414 prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
1416 atom_write_size (buffer, size, offset, original_offset);
1417 return *offset - original_offset;
1421 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1424 guint64 original_offset = *offset;
1426 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1429 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1430 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1432 atom_write_size (buffer, size, offset, original_offset);
1433 return original_offset - *offset;
1437 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1440 guint64 original_offset = *offset;
1442 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1445 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1446 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1448 atom_write_size (buffer, size, offset, original_offset);
1449 return original_offset - *offset;
1453 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1456 guint64 original_offset = *offset;
1458 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1461 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1462 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1463 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1464 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1465 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1467 atom_write_size (buffer, size, offset, original_offset);
1468 return original_offset - *offset;
1472 atom_url_same_file_flag (AtomURL * url)
1474 return (url->header.flags[2] & 0x1) == 1;
1478 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1481 guint64 original_offset = *offset;
1483 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1487 if (!atom_url_same_file_flag (url)) {
1488 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1491 atom_write_size (buffer, size, offset, original_offset);
1492 return original_offset - *offset;
1496 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1499 guint64 original_offset = *offset;
1502 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1506 prop_copy_uint32 (stts->n_entries, buffer, size, offset);
1507 /* minimize realloc */
1508 prop_copy_ensure_buffer (buffer, size, offset, 8 * stts->n_entries);
1509 for (walker = g_list_last (stts->entries); walker != NULL;
1510 walker = g_list_previous (walker)) {
1511 STTSEntry *entry = (STTSEntry *) walker->data;
1513 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
1514 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
1517 atom_write_size (buffer, size, offset, original_offset);
1518 return *offset - original_offset;
1522 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
1523 guint64 * size, guint64 * offset)
1525 guint64 original_offset = *offset;
1527 if (!atom_copy_data (&se->header, buffer, size, offset)) {
1531 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
1532 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
1534 return *offset - original_offset;
1538 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
1541 guint64 original_offset = *offset;
1543 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
1546 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
1550 atom_write_size (buffer, size, offset, original_offset);
1551 return *offset - original_offset;
1555 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
1556 guint64 * size, guint64 * offset)
1558 guint64 original_offset = *offset;
1560 if (!atom_copy_data (&(frma->header), buffer, size, offset))
1563 prop_copy_fourcc (frma->media_type, buffer, size, offset);
1565 atom_write_size (buffer, size, offset, original_offset);
1566 return *offset - original_offset;
1570 atom_mp4s_copy_data (SampleTableEntryMP4S * mp4s, guint8 ** buffer,
1571 guint64 * size, guint64 * offset)
1573 guint64 original_offset = *offset;
1575 if (!atom_sample_entry_copy_data (&mp4s->se, buffer, size, offset)) {
1578 if (!atom_esds_copy_data (&mp4s->es, buffer, size, offset)) {
1582 atom_write_size (buffer, size, offset, original_offset);
1583 return *offset - original_offset;
1587 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
1588 guint64 * size, guint64 * offset)
1590 guint64 original_offset = *offset;
1592 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
1596 prop_copy_uint32 (hse->size, buffer, size, offset);
1597 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
1599 atom_write_size (buffer, size, offset, original_offset);
1600 return *offset - original_offset;
1604 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
1605 guint64 * size, guint64 * offset)
1607 guint64 original_offset = *offset;
1609 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
1613 prop_copy_uint16 (mp4a->version, buffer, size, offset);
1614 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
1615 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
1616 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
1617 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
1618 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
1619 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
1620 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
1622 /* this should always be 0 for mp4 flavor */
1623 if (mp4a->version == 1) {
1624 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
1625 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
1626 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
1627 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
1630 if (mp4a->extension_atoms) {
1631 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
1635 atom_write_size (buffer, size, offset, original_offset);
1636 return *offset - original_offset;
1640 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
1641 guint64 * size, guint64 * offset)
1643 guint64 original_offset = *offset;
1645 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
1649 prop_copy_uint16 (mp4v->version, buffer, size, offset);
1650 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
1651 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
1652 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
1653 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
1655 prop_copy_uint16 (mp4v->width, buffer, size, offset);
1656 prop_copy_uint16 (mp4v->height, buffer, size, offset);
1658 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
1659 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
1660 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
1662 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
1664 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
1667 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
1668 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
1671 if (mp4v->extension_atoms &&
1672 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
1675 atom_write_size (buffer, size, offset, original_offset);
1676 return *offset - original_offset;
1680 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
1683 guint64 original_offset = *offset;
1686 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
1690 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
1691 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
1692 /* minimize realloc */
1693 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
1694 if (stsz->sample_size == 0) {
1695 for (walker = g_list_last (stsz->entries); walker != NULL;
1696 walker = g_list_previous (walker)) {
1697 prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
1702 atom_write_size (buffer, size, offset, original_offset);
1703 return *offset - original_offset;
1707 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
1710 guint64 original_offset = *offset;
1713 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
1717 prop_copy_uint32 (stsc->n_entries, buffer, size, offset);
1718 /* minimize realloc */
1719 prop_copy_ensure_buffer (buffer, size, offset, 12 * stsc->n_entries);
1721 for (walker = g_list_last (stsc->entries); walker != NULL;
1722 walker = g_list_previous (walker)) {
1723 STSCEntry *entry = (STSCEntry *) walker->data;
1725 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
1726 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
1727 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
1730 atom_write_size (buffer, size, offset, original_offset);
1731 return *offset - original_offset;
1735 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
1738 guint64 original_offset = *offset;
1741 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
1745 prop_copy_uint32 (ctts->n_entries, buffer, size, offset);
1746 /* minimize realloc */
1747 prop_copy_ensure_buffer (buffer, size, offset, 8 * ctts->n_entries);
1748 for (walker = g_list_last (ctts->entries); walker != NULL;
1749 walker = g_list_previous (walker)) {
1750 CTTSEntry *entry = (CTTSEntry *) walker->data;
1752 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
1753 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
1756 atom_write_size (buffer, size, offset, original_offset);
1757 return *offset - original_offset;
1761 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
1764 guint64 original_offset = *offset;
1766 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
1768 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
1772 prop_copy_uint32 (stco64->n_entries, buffer, size, offset);
1774 /* minimize realloc */
1775 prop_copy_ensure_buffer (buffer, size, offset, 8 * stco64->n_entries);
1776 for (walker = g_list_last (stco64->entries); walker != NULL;
1777 walker = g_list_previous (walker)) {
1778 guint64 *value = (guint64 *) walker->data;
1781 prop_copy_uint32 ((guint32) * value, buffer, size, offset);
1783 prop_copy_uint64 (*value, buffer, size, offset);
1787 atom_write_size (buffer, size, offset, original_offset);
1788 return *offset - original_offset;
1792 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
1795 guint64 original_offset = *offset;
1798 if (stss->entries == NULL) {
1799 /* FIXME not needing this atom might be confused with error while copying */
1803 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
1807 prop_copy_uint32 (stss->n_entries, buffer, size, offset);
1808 /* minimize realloc */
1809 prop_copy_ensure_buffer (buffer, size, offset, 4 * stss->n_entries);
1810 for (walker = g_list_last (stss->entries); walker != NULL;
1811 walker = g_list_previous (walker)) {
1812 prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
1816 atom_write_size (buffer, size, offset, original_offset);
1817 return *offset - original_offset;
1821 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
1824 guint64 original_offset = *offset;
1827 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
1831 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
1833 for (walker = g_list_last (stsd->entries); walker != NULL;
1834 walker = g_list_previous (walker)) {
1835 SampleTableEntry *se = (SampleTableEntry *) walker->data;
1837 switch (((Atom *) walker->data)->type) {
1839 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
1840 buffer, size, offset)) {
1845 if (!atom_mp4s_copy_data ((SampleTableEntryMP4S *) walker->data,
1846 buffer, size, offset)) {
1851 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
1852 buffer, size, offset)) {
1857 if (se->kind == VIDEO) {
1858 size += sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
1859 walker->data, buffer, size, offset);
1860 } else if (se->kind == AUDIO) {
1861 size += sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
1862 walker->data, buffer, size, offset);
1864 if (!atom_hint_sample_entry_copy_data (
1865 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
1873 atom_write_size (buffer, size, offset, original_offset);
1874 return *offset - original_offset;
1878 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
1881 guint64 original_offset = *offset;
1883 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
1887 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
1890 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
1893 /* this atom is optional, so let's check if we need it
1894 * (to avoid false error) */
1895 if (stbl->stss.entries) {
1896 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
1901 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
1904 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
1908 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
1912 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
1916 atom_write_size (buffer, size, offset, original_offset);
1917 return original_offset - *offset;
1922 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
1925 guint64 original_offset = *offset;
1928 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
1932 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
1934 walker = dref->entries;
1935 while (walker != NULL) {
1936 Atom *atom = (Atom *) walker->data;
1938 if (atom->type == FOURCC_url_) {
1939 atom_url_copy_data ((AtomURL *) atom, buffer, size, offset);
1940 } else if (atom->type == FOURCC_alis) {
1941 atom_full_copy_data ((AtomFull *) atom, buffer, size, offset);
1943 g_error ("Unsupported atom used inside dref atom");
1945 walker = g_list_next (walker);
1948 atom_write_size (buffer, size, offset, original_offset);
1949 return *offset - original_offset;
1953 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
1956 guint64 original_offset = *offset;
1958 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
1962 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
1966 atom_write_size (buffer, size, offset, original_offset);
1967 return original_offset - *offset;
1971 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
1974 guint64 original_offset = *offset;
1976 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
1981 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
1984 } else if (minf->smhd) {
1985 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
1988 } else if (minf->hmhd) {
1989 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
1995 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2000 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2003 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2007 atom_write_size (buffer, size, offset, original_offset);
2008 return *offset - original_offset;
2012 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2015 guint64 original_offset = *offset;
2017 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2021 if (!common_time_info_copy_data (&mdhd->time_info,
2022 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2026 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2027 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2029 atom_write_size (buffer, size, offset, original_offset);
2030 return *offset - original_offset;
2034 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2037 guint64 original_offset = *offset;
2039 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2042 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2045 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2049 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2053 atom_write_size (buffer, size, offset, original_offset);
2054 return *offset - original_offset;
2058 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2061 guint64 original_offset = *offset;
2063 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2066 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2070 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2074 atom_write_size (buffer, size, offset, original_offset);
2075 return *offset - original_offset;
2079 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2082 guint64 original_offset = *offset;
2084 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2088 prop_copy_uint32 (data->reserved, buffer, size, offset);
2089 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2091 atom_write_size (buffer, size, offset, original_offset);
2092 return *offset - original_offset;
2096 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2099 guint64 original_offset = *offset;
2101 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2105 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2109 atom_write_size (buffer, size, offset, original_offset);
2110 return *offset - original_offset;
2114 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2117 guint64 original_offset = *offset;
2119 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2123 if (ilst->entries &&
2124 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2127 atom_write_size (buffer, size, offset, original_offset);
2128 return *offset - original_offset;
2132 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2135 guint64 original_offset = *offset;
2137 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2140 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2144 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2149 atom_write_size (buffer, size, offset, original_offset);
2150 return *offset - original_offset;
2154 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2157 guint64 original_offset = *offset;
2159 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2163 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2166 } else if (udta->entries) {
2168 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2172 atom_write_size (buffer, size, offset, original_offset);
2173 return *offset - original_offset;
2177 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2180 guint64 original_offset = *offset;
2183 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2186 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2189 walker = g_list_first (atom->traks);
2190 while (walker != NULL) {
2191 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2194 walker = g_list_next (walker);
2198 if (!atom_udta_copy_data (atom->udta, buffer, size, offset)) {
2203 atom_write_size (buffer, size, offset, original_offset);
2204 return *offset - original_offset;
2208 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
2209 guint64 * size, guint64 * offset)
2211 guint64 original_offset = *offset;
2213 if (!atom_copy_data (&(wave->header), buffer, size, offset))
2216 if (wave->extension_atoms) {
2217 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
2221 atom_write_size (buffer, size, offset, original_offset);
2222 return *offset - original_offset;
2225 /* -- end of copy data functions -- */
2227 /* -- general functions, API and support functions */
2229 /* add samples to tables */
2232 stsc_entry_new (guint32 first_chunk, guint32 samples, guint32 desc_index)
2234 STSCEntry *entry = g_new0 (STSCEntry, 1);
2236 entry->first_chunk = first_chunk;
2237 entry->samples_per_chunk = samples;
2238 entry->sample_description_index = desc_index;
2243 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
2245 if (stsc->entries &&
2246 ((STSCEntry *) stsc->entries->data)->samples_per_chunk == nsamples)
2250 g_list_prepend (stsc->entries, stsc_entry_new (first_chunk, nsamples, 1));
2255 stts_entry_new (guint32 sample_count, gint32 sample_delta)
2257 STTSEntry *entry = g_new0 (STTSEntry, 1);
2259 entry->sample_count = sample_count;
2260 entry->sample_delta = sample_delta;
2265 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
2269 if (stts->entries == NULL) {
2271 g_list_prepend (stts->entries, stts_entry_new (sample_count,
2276 entry = (STTSEntry *) g_list_first (stts->entries)->data;
2277 if (entry->sample_delta == sample_delta) {
2278 entry->sample_count += sample_count;
2281 g_list_prepend (stts->entries, stts_entry_new (sample_count,
2288 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
2292 stsz->table_size += nsamples;
2293 if (stsz->sample_size != 0) {
2294 /* it is constant size, we don't need entries */
2297 for (i = 0; i < nsamples; i++) {
2298 stsz->entries = g_list_prepend (stsz->entries, GUINT_TO_POINTER (size));
2303 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
2305 return stco64->n_entries;
2309 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
2311 guint64 *pont = g_new (guint64, 1);
2314 stco64->entries = g_list_prepend (stco64->entries, pont);
2315 stco64->n_entries++;
2319 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
2321 stss->entries = g_list_prepend (stss->entries, GUINT_TO_POINTER (sample));
2326 atom_stbl_add_stss_entry (AtomSTBL * stbl)
2328 guint32 sample_index = stbl->stsz.table_size;
2330 atom_stss_add_entry (&stbl->stss, sample_index);
2334 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
2339 walker = g_list_first (ctts->entries);
2340 entry = (walker == NULL) ? NULL : (CTTSEntry *) walker->data;
2342 if (entry == NULL || entry->sampleoffset != offset) {
2343 CTTSEntry *entry = g_new0 (CTTSEntry, 1);
2345 entry->samplecount = nsamples;
2346 entry->sampleoffset = offset;
2347 ctts->entries = g_list_prepend (ctts->entries, entry);
2350 entry->samplecount += nsamples;
2355 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
2357 if (stbl->ctts == NULL) {
2358 stbl->ctts = atom_ctts_new ();
2360 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
2364 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
2365 guint32 size, guint64 chunk_offset, gboolean sync,
2366 gboolean do_pts, gint64 pts_offset)
2368 AtomSTBL *stbl = &trak->mdia.minf.stbl;
2370 atom_stts_add_entry (&stbl->stts, nsamples, delta);
2371 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
2372 atom_stco64_add_entry (&stbl->stco64, chunk_offset);
2373 atom_stsc_add_new_entry (&stbl->stsc,
2374 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
2376 atom_stbl_add_stss_entry (stbl);
2378 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
2381 /* trak and moov molding */
2384 atom_trak_get_timescale (AtomTRAK * trak)
2386 return trak->mdia.mdhd.time_info.timescale;
2390 atom_trak_set_id (AtomTRAK * trak, guint32 id)
2392 trak->tkhd.track_ID = id;
2396 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
2398 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
2399 moov->traks = g_list_append (moov->traks, trak);
2403 atom_trak_get_duration (AtomTRAK * trak)
2405 return trak->tkhd.duration;
2409 atom_stts_get_total_duration (AtomSTTS * stts)
2411 GList *walker = stts->entries;
2415 STTSEntry *entry = (STTSEntry *) walker->data;
2417 sum += (guint64) (entry->sample_count) * entry->sample_delta;
2418 walker = g_list_next (walker);
2424 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
2426 trak->mdia.mdhd.time_info.duration =
2427 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
2428 trak->tkhd.duration =
2429 gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration, moov_timescale,
2430 trak->mdia.mdhd.time_info.timescale);
2434 atom_moov_get_timescale (AtomMOOV * moov)
2436 return moov->mvhd.time_info.timescale;
2440 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
2442 moov->mvhd.time_info.timescale = timescale;
2446 atom_moov_update_duration (AtomMOOV * moov)
2448 GList *traks = moov->traks;
2449 guint64 dur, duration = 0;
2452 AtomTRAK *trak = (AtomTRAK *) traks->data;
2454 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
2455 dur = atom_trak_get_duration (trak);
2458 traks = g_list_next (traks);
2460 moov->mvhd.time_info.duration = duration;
2464 atom_set_type (Atom * atom, guint32 fourcc)
2466 atom->type = fourcc;
2470 atom_stbl_set_64bits (AtomSTBL * stbl, gboolean use)
2473 atom_set_type (&stbl->stco64.header.header, FOURCC_co64);
2475 atom_set_type (&stbl->stco64.header.header, FOURCC_stco);
2480 atom_trak_set_64bits (AtomTRAK * trak, gboolean use)
2482 atom_stbl_set_64bits (&trak->mdia.minf.stbl, use);
2486 atom_moov_set_64bits (AtomMOOV * moov, gboolean large_file)
2488 GList *traks = moov->traks;
2491 AtomTRAK *trak = (AtomTRAK *) traks->data;
2493 atom_trak_set_64bits (trak, large_file);
2494 traks = g_list_next (traks);
2499 atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset)
2501 GList *entries = stco64->entries;
2504 guint64 *value = (guint64 *) entries->data;
2507 entries = g_list_next (entries);
2512 atom_moov_chunks_add_offset (AtomMOOV * moov, guint32 offset)
2514 GList *traks = moov->traks;
2517 AtomTRAK *trak = (AtomTRAK *) traks->data;
2519 atom_stco64_chunks_add_offset (&trak->mdia.minf.stbl.stco64, offset);
2520 traks = g_list_next (traks);
2525 * Meta tags functions
2528 atom_moov_init_metatags (AtomMOOV * moov, AtomsContext * context)
2531 moov->udta = atom_udta_new ();
2533 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
2534 if (!moov->udta->meta) {
2535 moov->udta->meta = atom_meta_new ();
2537 if (!moov->udta->meta->ilst) {
2538 moov->udta->meta->ilst = atom_ilst_new ();
2544 atom_tag_data_alloc_data (AtomTagData * data, guint size)
2546 if (data->data != NULL) {
2547 g_free (data->data);
2549 data->data = g_new0 (guint8, size);
2550 data->datalen = size;
2554 atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
2558 atom_moov_init_metatags (moov, &moov->context);
2559 if (moov->udta->meta)
2560 entries = &moov->udta->meta->ilst->entries;
2562 entries = &moov->udta->entries;
2563 *entries = g_list_append (*entries, tag);
2567 atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2568 const guint8 * data, guint size)
2573 tag = atom_tag_new (fourcc, flags);
2575 atom_tag_data_alloc_data (tdata, size);
2576 g_memmove (tdata->data, data, size);
2578 atom_moov_append_tag (moov,
2579 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
2584 atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2586 gint len = strlen (value);
2589 atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
2593 atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2596 guint8 data[8] = { 0, };
2599 GST_WRITE_UINT16_BE (data, value);
2600 atom_moov_add_tag (moov, fourcc, flags, data, 2);
2602 GST_WRITE_UINT32_BE (data + 2, value);
2603 atom_moov_add_tag (moov, fourcc, flags, data, 8);
2608 atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
2610 AtomData *data_atom;
2618 /* blob is unparsed atom;
2619 * extract size and fourcc, and wrap remainder in data atom */
2620 len = GST_READ_UINT32_BE (data);
2621 fourcc = GST_READ_UINT32_LE (data + 4);
2625 buf = gst_buffer_new ();
2626 GST_BUFFER_SIZE (buf) = len - 8;
2627 GST_BUFFER_DATA (buf) = data + 8;
2629 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2630 gst_buffer_unref (buf);
2632 atom_moov_append_tag (moov,
2633 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2638 atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
2641 AtomData *data_atom;
2645 /* need full atom */
2646 buf = gst_buffer_new_and_alloc (size + 4);
2647 bdata = GST_BUFFER_DATA (buf);
2648 /* full atom: version and flags */
2649 GST_WRITE_UINT32_BE (bdata, 0);
2650 memcpy (bdata + 4, data, size);
2652 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2653 gst_buffer_unref (buf);
2655 atom_moov_append_tag (moov,
2656 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2661 language_code (const char *lang)
2663 g_return_val_if_fail (lang != NULL, 0);
2664 g_return_val_if_fail (strlen (lang) == 3, 0);
2666 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
2667 ((lang[2] - 0x60) & 0x1F);
2671 atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc,
2672 const gchar * value, gint16 ivalue)
2674 gint len = 0, size = 0;
2678 len = strlen (value);
2685 data = g_malloc (size + 3);
2686 /* language tag and null-terminated UTF-8 string */
2688 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
2689 /* include 0 terminator */
2690 memcpy (data + 2, value, len + 1);
2692 /* 16-bit unsigned int if standalone, otherwise 8-bit */
2695 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
2697 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
2702 atom_moov_add_3gp_tag (moov, fourcc, data, size);
2707 atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2709 atom_moov_add_3gp_str_int_tag (moov, fourcc, value, -1);
2713 atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value)
2715 atom_moov_add_3gp_str_int_tag (moov, fourcc, NULL, value);
2719 * Functions for specifying media types
2723 atom_minf_set_audio (AtomMINF * minf)
2725 atom_minf_clear_handlers (minf);
2726 minf->smhd = atom_smhd_new ();
2730 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
2732 atom_minf_clear_handlers (minf);
2733 minf->vmhd = atom_vmhd_new (context);
2737 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
2740 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2741 hdlr->component_type = comp_type;
2743 hdlr->handler_type = hdlr_type;
2747 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
2749 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
2753 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
2755 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
2759 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
2761 atom_mdia_set_hdlr_type_audio (mdia, context);
2762 atom_minf_set_audio (&mdia->minf);
2766 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
2768 atom_mdia_set_hdlr_type_video (mdia, context);
2769 atom_minf_set_video (&mdia->minf, context);
2773 atom_tkhd_set_audio (AtomTKHD * tkhd)
2775 tkhd->volume = 0x0100;
2776 tkhd->width = tkhd->height = 0;
2780 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
2785 /* qt and ISO base media do not contradict, and examples agree */
2786 tkhd->width = width;
2787 tkhd->height = height;
2790 /* re-negotiation is prevented at top-level, so only 1 entry expected.
2791 * Quite some more care here and elsewhere may be needed to
2792 * support several entries */
2793 static SampleTableEntryMP4A *
2794 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
2797 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
2798 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
2800 mp4a->se.header.type = type;
2801 mp4a->se.kind = AUDIO;
2802 mp4a->compression_id = -1;
2803 mp4a->se.data_reference_index = 1;
2805 stsd->entries = g_list_prepend (stsd->entries, mp4a);
2810 static SampleTableEntryMP4V *
2811 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
2814 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
2815 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
2817 mp4v->se.header.type = type;
2818 mp4v->se.kind = VIDEO;
2819 mp4v->se.data_reference_index = 1;
2820 mp4v->horizontal_resolution = 72 << 16;
2821 mp4v->vertical_resolution = 72 << 16;
2822 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2823 mp4v->spatial_quality = 512;
2824 mp4v->temporal_quality = 512;
2827 stsd->entries = g_list_prepend (stsd->entries, mp4v);
2833 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
2835 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
2839 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
2841 atom_tkhd_set_audio (&trak->tkhd);
2842 atom_mdia_set_audio (&trak->mdia, context);
2846 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
2849 atom_tkhd_set_video (&trak->tkhd, context, width, height);
2850 atom_mdia_set_video (&trak->mdia, context);
2854 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
2857 atom_trak_set_audio (trak, context);
2858 trak->mdia.mdhd.time_info.timescale = rate;
2862 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
2863 guint32 rate, guint32 width, guint32 height)
2865 atom_trak_set_video (trak, context, width, height);
2866 trak->mdia.mdhd.time_info.timescale = rate;
2867 trak->tkhd.width = width << 16;
2868 trak->tkhd.height = height << 16;
2872 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
2873 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
2875 SampleTableEntryMP4A *ste;
2877 atom_trak_set_audio_commons (trak, context, scale);
2878 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
2880 trak->is_video = FALSE;
2881 trak->is_h264 = FALSE;
2883 ste->version = entry->version;
2884 ste->compression_id = entry->compression_id;
2885 ste->sample_size = entry->sample_size;
2886 ste->sample_rate = entry->sample_rate << 16;
2887 ste->channels = entry->channels;
2889 ste->samples_per_packet = entry->samples_per_packet;
2890 ste->bytes_per_sample = entry->bytes_per_sample;
2891 ste->bytes_per_packet = entry->bytes_per_packet;
2892 ste->bytes_per_frame = entry->bytes_per_frame;
2895 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
2897 /* 0 size means variable size */
2898 atom_trak_set_constant_size_samples (trak, sample_size);
2902 build_pasp_extension (AtomTRAK * trak, gint par_width, gint par_height)
2904 AtomData *atom_data;
2908 buf = gst_buffer_new_and_alloc (8);
2909 data = GST_BUFFER_DATA (buf);
2911 /* ihdr = image header box */
2912 GST_WRITE_UINT32_BE (data, par_width);
2913 GST_WRITE_UINT32_BE (data + 4, par_height);
2915 atom_data = atom_data_new_from_gst_buffer (FOURCC_pasp, buf);
2916 gst_buffer_unref (buf);
2918 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
2923 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
2924 VisualSampleEntry * entry, guint32 scale, AtomInfo * ext)
2926 SampleTableEntryMP4V *ste;
2927 gint dwidth, dheight;
2928 gint par_n = 0, par_d = 0;
2930 if ((entry->par_n != 1 || entry->par_d != 1) &&
2931 (entry->par_n != entry->par_d)) {
2932 par_n = entry->par_n;
2933 par_d = entry->par_d;
2936 dwidth = entry->width;
2937 dheight = entry->height;
2938 /* ISO file spec says track header w/h indicates track's visual presentation
2939 * (so this together with pixels w/h implicitly defines PAR) */
2940 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
2941 if (par_n > par_d) {
2942 dwidth = entry->width * par_n / par_d;
2943 dheight = entry->height;
2945 dwidth = entry->width * par_n / par_d;
2946 dheight = entry->height;
2950 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
2951 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
2953 trak->is_video = TRUE;
2954 trak->is_h264 = (entry->fourcc == FOURCC_avc1);
2956 ste->width = entry->width;
2957 ste->height = entry->height;
2958 ste->depth = entry->depth;
2959 ste->color_table_id = entry->color_table_id;
2960 ste->frame_count = entry->frame_count;
2963 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
2965 /* QT spec has a pasp extension atom in stsd that can hold PAR */
2966 if (par_n && (context->flavor == ATOMS_TREE_FLAVOR_MOV)) {
2967 ste->extension_atoms = g_list_append (ste->extension_atoms,
2968 build_pasp_extension (trak, par_n, par_d));
2972 /* some sample description construction helpers */
2975 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
2976 const GstBuffer * codec_data)
2981 track_id = trak->tkhd.track_ID;
2983 esds = atom_esds_new ();
2984 esds->es.id = track_id & 0xFFFF;
2985 esds->es.dec_conf_desc.object_type = object_type;
2986 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
2988 /* optional DecoderSpecificInfo */
2990 DecoderSpecificInfoDescriptor *desc;
2992 esds->es.dec_conf_desc.dec_specific_info = desc =
2993 desc_dec_specific_info_new ();
2994 desc_dec_specific_info_alloc_data (desc, GST_BUFFER_SIZE (codec_data));
2996 memcpy (desc->data, GST_BUFFER_DATA (codec_data),
2997 GST_BUFFER_SIZE (codec_data));
3000 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
3005 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
3012 /* Add WAVE atom to the MP4A sample table entry */
3013 wave = atom_wave_new ();
3015 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
3016 ext_atom = (Atom *) atom_data_new (FOURCC_null);
3017 wave->extension_atoms =
3018 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
3019 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
3021 /* Add ESDS atom to WAVE */
3022 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
3023 build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
3024 ESDS_STREAM_TYPE_AUDIO, codec_data));
3026 /* Add MP4A atom to the WAVE:
3027 * not really in spec, but makes offset based players happy */
3028 buf = gst_buffer_new_and_alloc (4);
3029 *((guint32 *) GST_BUFFER_DATA (buf)) = 0;
3030 ext_atom = (Atom *) atom_data_new_from_gst_buffer (FOURCC_mp4a, buf);
3031 gst_buffer_unref (buf);
3033 wave->extension_atoms =
3034 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
3035 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
3037 /* Add FRMA to the WAVE */
3038 frma = atom_frma_new ();
3039 frma->media_type = FOURCC_mp4a;
3041 wave->extension_atoms =
3042 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
3043 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
3045 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
3050 build_jp2h_extension (AtomTRAK * trak, gint width, gint height, guint32 fourcc)
3052 AtomData *atom_data;
3057 if (fourcc == GST_MAKE_FOURCC ('s', 'R', 'G', 'B')) {
3059 } else if (fourcc == GST_MAKE_FOURCC ('s', 'Y', 'U', 'V')) {
3064 buf = gst_buffer_new_and_alloc (22 + 15);
3065 data = GST_BUFFER_DATA (buf);
3067 /* ihdr = image header box */
3068 GST_WRITE_UINT32_BE (data, 22);
3069 GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
3070 GST_WRITE_UINT32_BE (data + 8, height);
3071 GST_WRITE_UINT32_BE (data + 12, width);
3072 /* FIXME perhaps parse from stream,
3073 * though exactly 3 in any respectable colourspace */
3074 GST_WRITE_UINT16_BE (data + 16, 3);
3075 /* 8 bits per component, unsigned */
3076 GST_WRITE_UINT8 (data + 18, 0x7);
3077 /* compression type; reserved */
3078 GST_WRITE_UINT8 (data + 19, 0x7);
3079 /* colour space (un)known */
3080 GST_WRITE_UINT8 (data + 20, 0x0);
3081 /* intellectual property right (box present) */
3082 GST_WRITE_UINT8 (data + 21, 0x0);
3084 /* colour specification box */
3086 GST_WRITE_UINT32_BE (data, 15);
3087 GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
3088 /* specification method: enumerated */
3089 GST_WRITE_UINT8 (data + 8, 0x1);
3090 /* precedence; reserved */
3091 GST_WRITE_UINT8 (data + 9, 0x0);
3092 /* approximation; reserved */
3093 GST_WRITE_UINT8 (data + 10, 0x0);
3094 /* enumerated colourspace */
3095 GST_WRITE_UINT32_BE (data + 11, cenum);
3097 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
3098 gst_buffer_unref (buf);
3100 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3105 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
3108 AtomInfo *result = NULL;
3111 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
3112 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
3120 build_amr_extension ()
3126 buf = gst_buffer_new ();
3127 GST_BUFFER_DATA (buf) = ext;
3128 GST_BUFFER_SIZE (buf) = sizeof (ext);
3131 GST_WRITE_UINT32_LE (ext, 0);
3132 /* decoder version */
3133 GST_WRITE_UINT8 (ext + 4, 0);
3134 /* mode set (all modes) */
3135 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
3136 /* mode change period (no restriction) */
3137 GST_WRITE_UINT8 (ext + 7, 0);
3138 /* frames per sample */
3139 GST_WRITE_UINT8 (ext + 8, 1);
3141 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', 'a', 'm', 'r'), buf);
3142 gst_buffer_unref (buf);
3147 build_h263_extension ()
3153 buf = gst_buffer_new ();
3154 GST_BUFFER_DATA (buf) = ext;
3155 GST_BUFFER_SIZE (buf) = sizeof (ext);
3158 GST_WRITE_UINT32_LE (ext, 0);
3159 /* decoder version */
3160 GST_WRITE_UINT8 (ext + 4, 0);
3161 /* level / profile */
3162 /* FIXME ? maybe ? obtain somewhere; baseline for now */
3163 GST_WRITE_UINT8 (ext + 5, 10);
3164 GST_WRITE_UINT8 (ext + 6, 0);
3166 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', '2', '6', '3'), buf);
3167 gst_buffer_unref (buf);