1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2008-2010 Thiago 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
49 #include <gst/base/gstbytewriter.h>
50 #include <gst/tag/tag.h>
53 * Creates a new AtomsContext for the given flavor.
56 atoms_context_new (AtomsTreeFlavor flavor)
58 AtomsContext *context = g_new0 (AtomsContext, 1);
59 context->flavor = flavor;
64 * Frees an AtomsContext and all memory associated with it
67 atoms_context_free (AtomsContext * context)
72 /* -- creation, initialization, clear and free functions -- */
74 #define SECS_PER_DAY (24 * 60 * 60)
75 #define LEAP_YEARS_FROM_1904_TO_1970 17
78 get_current_qt_time (void)
82 g_get_current_time (&timeval);
83 /* FIXME this should use UTC coordinated time */
84 return timeval.tv_sec + (((1970 - 1904) * (guint64) 365) +
85 LEAP_YEARS_FROM_1904_TO_1970) * SECS_PER_DAY;
89 common_time_info_init (TimeInfo * ti)
91 ti->creation_time = ti->modification_time = get_current_qt_time ();
97 atom_header_set (Atom * header, guint32 fourcc, gint32 size, gint64 ext_size)
99 header->type = fourcc;
101 header->extended_size = ext_size;
105 atom_clear (Atom * atom)
110 atom_full_init (AtomFull * full, guint32 fourcc, gint32 size, gint64 ext_size,
111 guint8 version, guint8 flags[3])
113 atom_header_set (&(full->header), fourcc, size, ext_size);
114 full->version = version;
115 full->flags[0] = flags[0];
116 full->flags[1] = flags[1];
117 full->flags[2] = flags[2];
121 atom_full_clear (AtomFull * full)
123 atom_clear (&full->header);
127 atom_full_free (AtomFull * full)
129 atom_full_clear (full);
134 atom_full_get_flags_as_uint (AtomFull * full)
136 return full->flags[0] << 16 | full->flags[1] << 8 | full->flags[2];
140 atom_full_set_flags_as_uint (AtomFull * full, guint32 flags_as_uint)
142 full->flags[2] = flags_as_uint & 0xFF;
143 full->flags[1] = (flags_as_uint & 0xFF00) >> 8;
144 full->flags[0] = (flags_as_uint & 0xFF0000) >> 16;
148 build_atom_info_wrapper (Atom * atom, gpointer copy_func, gpointer free_func)
150 AtomInfo *info = NULL;
153 info = g_new0 (AtomInfo, 1);
156 info->copy_data_func = copy_func;
157 info->free_func = free_func;
164 atom_info_list_prepend_atom (GList * ai, Atom * atom,
165 AtomCopyDataFunc copy_func, AtomFreeFunc free_func)
168 return g_list_prepend (ai,
169 build_atom_info_wrapper (atom, copy_func, free_func));
175 atom_info_list_free (GList * ai)
178 AtomInfo *info = (AtomInfo *) ai->data;
180 info->free_func (info->atom);
182 ai = g_list_delete_link (ai, ai);
187 atom_data_new (guint32 fourcc)
189 AtomData *data = g_new0 (AtomData, 1);
191 atom_header_set (&data->header, fourcc, 0, 0);
196 atom_data_alloc_mem (AtomData * data, guint32 size)
201 data->data = g_new0 (guint8, size);
202 data->datalen = size;
206 atom_data_new_from_gst_buffer (guint32 fourcc, const GstBuffer * buf)
208 AtomData *data = atom_data_new (fourcc);
209 gsize size = gst_buffer_get_size ((GstBuffer *) buf);
211 atom_data_alloc_mem (data, size);
212 gst_buffer_extract ((GstBuffer *) buf, 0, data->data, size);
217 atom_data_free (AtomData * data)
219 atom_clear (&data->header);
227 AtomUUID *uuid = g_new0 (AtomUUID, 1);
229 atom_header_set (&uuid->header, FOURCC_uuid, 0, 0);
234 atom_uuid_free (AtomUUID * data)
236 atom_clear (&data->header);
242 atom_ftyp_init (AtomFTYP * ftyp, guint32 major, guint32 version, GList * brands)
247 atom_header_set (&ftyp->header, FOURCC_ftyp, 16, 0);
248 ftyp->major_brand = major;
249 ftyp->version = version;
251 /* always include major brand as compatible brand */
252 ftyp->compatible_brands_size = g_list_length (brands) + 1;
253 ftyp->compatible_brands = g_new (guint32, ftyp->compatible_brands_size);
255 ftyp->compatible_brands[0] = major;
257 for (it = brands; it != NULL; it = g_list_next (it)) {
258 ftyp->compatible_brands[index++] = GPOINTER_TO_UINT (it->data);
263 atom_ftyp_new (AtomsContext * context, guint32 major, guint32 version,
266 AtomFTYP *ftyp = g_new0 (AtomFTYP, 1);
268 atom_ftyp_init (ftyp, major, version, brands);
273 atom_ftyp_free (AtomFTYP * ftyp)
275 atom_clear (&ftyp->header);
276 g_free (ftyp->compatible_brands);
277 ftyp->compatible_brands = NULL;
282 atom_esds_init (AtomESDS * esds)
284 guint8 flags[3] = { 0, 0, 0 };
286 atom_full_init (&esds->header, FOURCC_esds, 0, 0, 0, flags);
287 desc_es_init (&esds->es);
293 AtomESDS *esds = g_new0 (AtomESDS, 1);
295 atom_esds_init (esds);
300 atom_esds_free (AtomESDS * esds)
302 atom_full_clear (&esds->header);
303 desc_es_descriptor_clear (&esds->es);
310 AtomFRMA *frma = g_new0 (AtomFRMA, 1);
312 atom_header_set (&frma->header, FOURCC_frma, 0, 0);
317 atom_frma_free (AtomFRMA * frma)
319 atom_clear (&frma->header);
326 AtomWAVE *wave = g_new0 (AtomWAVE, 1);
328 atom_header_set (&wave->header, FOURCC_wave, 0, 0);
333 atom_wave_free (AtomWAVE * wave)
335 atom_clear (&wave->header);
336 atom_info_list_free (wave->extension_atoms);
341 atom_elst_init (AtomELST * elst)
343 guint8 flags[3] = { 0, 0, 0 };
344 atom_full_init (&elst->header, FOURCC_elst, 0, 0, 0, flags);
349 atom_elst_clear (AtomELST * elst)
353 atom_full_clear (&elst->header);
354 walker = elst->entries;
356 g_free ((EditListEntry *) walker->data);
357 walker = g_slist_next (walker);
359 g_slist_free (elst->entries);
363 atom_edts_init (AtomEDTS * edts)
365 atom_header_set (&edts->header, FOURCC_edts, 0, 0);
366 atom_elst_init (&edts->elst);
370 atom_edts_clear (AtomEDTS * edts)
372 atom_clear (&edts->header);
373 atom_elst_clear (&edts->elst);
379 AtomEDTS *edts = g_new0 (AtomEDTS, 1);
380 atom_edts_init (edts);
385 atom_edts_free (AtomEDTS * edts)
387 atom_edts_clear (edts);
392 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
394 atom_header_set (&se->header, type, 0, 0);
396 memset (se->reserved, 0, sizeof (guint8) * 6);
397 se->data_reference_index = 0;
401 atom_sample_entry_free (SampleTableEntry * se)
403 atom_clear (&se->header);
407 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
409 atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
412 mp4a->revision_level = 0;
415 mp4a->sample_size = 16;
416 mp4a->compression_id = 0;
417 mp4a->packet_size = 0;
418 mp4a->sample_rate = 0;
419 /* following only used if version is 1 */
420 mp4a->samples_per_packet = 0;
421 mp4a->bytes_per_packet = 0;
422 mp4a->bytes_per_frame = 0;
423 mp4a->bytes_per_sample = 0;
425 mp4a->extension_atoms = NULL;
428 static SampleTableEntryMP4A *
429 sample_entry_mp4a_new (void)
431 SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
433 sample_entry_mp4a_init (mp4a);
438 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
440 atom_sample_entry_free (&mp4a->se);
441 atom_info_list_free (mp4a->extension_atoms);
446 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
448 atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
451 mp4v->revision_level = 0;
454 mp4v->temporal_quality = 0;
455 mp4v->spatial_quality = 0;
457 /* qt and ISO base media do not contradict, and examples agree */
458 mp4v->horizontal_resolution = 0x00480000;
459 mp4v->vertical_resolution = 0x00480000;
462 mp4v->frame_count = 1;
464 memset (mp4v->compressor, 0, sizeof (guint8) * 32);
467 mp4v->color_table_id = 0;
469 mp4v->extension_atoms = NULL;
473 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
475 atom_sample_entry_free (&mp4v->se);
476 atom_info_list_free (mp4v->extension_atoms);
480 static SampleTableEntryMP4V *
481 sample_entry_mp4v_new (AtomsContext * context)
483 SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
485 sample_entry_mp4v_init (mp4v, context);
490 atom_stsd_init (AtomSTSD * stsd)
492 guint8 flags[3] = { 0, 0, 0 };
494 atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
495 stsd->entries = NULL;
500 atom_stsd_remove_entries (AtomSTSD * stsd)
504 walker = stsd->entries;
507 SampleTableEntry *se = (SampleTableEntry *) aux->data;
509 walker = g_list_next (walker);
510 stsd->entries = g_list_remove_link (stsd->entries, aux);
514 sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
517 sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
520 /* best possible cleanup */
521 atom_sample_entry_free (se);
529 atom_stsd_clear (AtomSTSD * stsd)
531 atom_stsd_remove_entries (stsd);
532 atom_full_clear (&stsd->header);
536 atom_ctts_init (AtomCTTS * ctts)
538 guint8 flags[3] = { 0, 0, 0 };
540 atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
541 atom_array_init (&ctts->entries, 128);
542 ctts->do_pts = FALSE;
548 AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
550 atom_ctts_init (ctts);
555 atom_ctts_free (AtomCTTS * ctts)
557 atom_full_clear (&ctts->header);
558 atom_array_clear (&ctts->entries);
563 atom_stts_init (AtomSTTS * stts)
565 guint8 flags[3] = { 0, 0, 0 };
567 atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
568 atom_array_init (&stts->entries, 512);
572 atom_stts_clear (AtomSTTS * stts)
574 atom_full_clear (&stts->header);
575 atom_array_clear (&stts->entries);
579 atom_stsz_init (AtomSTSZ * stsz)
581 guint8 flags[3] = { 0, 0, 0 };
583 atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
584 atom_array_init (&stsz->entries, 1024);
585 stsz->sample_size = 0;
586 stsz->table_size = 0;
590 atom_stsz_clear (AtomSTSZ * stsz)
592 atom_full_clear (&stsz->header);
593 atom_array_clear (&stsz->entries);
594 stsz->table_size = 0;
598 atom_stsc_init (AtomSTSC * stsc)
600 guint8 flags[3] = { 0, 0, 0 };
602 atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
603 atom_array_init (&stsc->entries, 128);
607 atom_stsc_clear (AtomSTSC * stsc)
609 atom_full_clear (&stsc->header);
610 atom_array_clear (&stsc->entries);
614 atom_co64_init (AtomSTCO64 * co64)
616 guint8 flags[3] = { 0, 0, 0 };
618 atom_full_init (&co64->header, FOURCC_stco, 0, 0, 0, flags);
619 atom_array_init (&co64->entries, 256);
623 atom_stco64_clear (AtomSTCO64 * stco64)
625 atom_full_clear (&stco64->header);
626 atom_array_clear (&stco64->entries);
630 atom_stss_init (AtomSTSS * stss)
632 guint8 flags[3] = { 0, 0, 0 };
634 atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
635 atom_array_init (&stss->entries, 128);
639 atom_stss_clear (AtomSTSS * stss)
641 atom_full_clear (&stss->header);
642 atom_array_clear (&stss->entries);
646 atom_stbl_init (AtomSTBL * stbl)
648 atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
650 atom_stts_init (&stbl->stts);
651 atom_stss_init (&stbl->stss);
652 atom_stsd_init (&stbl->stsd);
653 atom_stsz_init (&stbl->stsz);
654 atom_stsc_init (&stbl->stsc);
657 atom_co64_init (&stbl->stco64);
661 atom_stbl_clear (AtomSTBL * stbl)
663 atom_clear (&stbl->header);
664 atom_stsd_clear (&stbl->stsd);
665 atom_stts_clear (&stbl->stts);
666 atom_stss_clear (&stbl->stss);
667 atom_stsc_clear (&stbl->stsc);
668 atom_stsz_clear (&stbl->stsz);
670 atom_ctts_free (stbl->ctts);
672 atom_stco64_clear (&stbl->stco64);
676 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
678 guint8 flags[3] = { 0, 0, 1 };
680 atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
681 vmhd->graphics_mode = 0x0;
682 memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
684 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
685 vmhd->graphics_mode = 0x40;
686 vmhd->opcolor[0] = 32768;
687 vmhd->opcolor[1] = 32768;
688 vmhd->opcolor[2] = 32768;
693 atom_vmhd_new (AtomsContext * context)
695 AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
697 atom_vmhd_init (vmhd, context);
702 atom_vmhd_free (AtomVMHD * vmhd)
704 atom_full_clear (&vmhd->header);
709 atom_smhd_init (AtomSMHD * smhd)
711 guint8 flags[3] = { 0, 0, 0 };
713 atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
721 AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
723 atom_smhd_init (smhd);
728 atom_smhd_free (AtomSMHD * smhd)
730 atom_full_clear (&smhd->header);
735 atom_hmhd_free (AtomHMHD * hmhd)
737 atom_full_clear (&hmhd->header);
742 atom_hdlr_init (AtomHDLR * hdlr)
744 guint8 flags[3] = { 0, 0, 0 };
746 atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
748 hdlr->component_type = 0;
749 hdlr->handler_type = 0;
750 hdlr->manufacturer = 0;
752 hdlr->flags_mask = 0;
753 hdlr->name = g_strdup ("");
759 AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
761 atom_hdlr_init (hdlr);
766 atom_hdlr_clear (AtomHDLR * hdlr)
768 atom_full_clear (&hdlr->header);
776 atom_hdlr_free (AtomHDLR * hdlr)
778 atom_hdlr_clear (hdlr);
783 atom_url_init (AtomURL * url)
785 guint8 flags[3] = { 0, 0, 1 };
787 atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
788 url->location = NULL;
792 atom_url_free (AtomURL * url)
794 atom_full_clear (&url->header);
796 g_free (url->location);
797 url->location = NULL;
805 AtomURL *url = g_new0 (AtomURL, 1);
814 guint8 flags[3] = { 0, 0, 1 };
815 AtomFull *alis = g_new0 (AtomFull, 1);
817 atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
822 atom_dref_init (AtomDREF * dref, AtomsContext * context)
824 guint8 flags[3] = { 0, 0, 0 };
826 atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
828 /* in either case, alis or url init arranges to set self-contained flag */
829 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
830 /* alis dref for qt */
831 AtomFull *alis = atom_alis_new ();
832 dref->entries = g_list_append (dref->entries, alis);
834 /* url for iso spec, as 'alis' not specified there */
835 AtomURL *url = atom_url_new ();
836 dref->entries = g_list_append (dref->entries, url);
841 atom_dref_clear (AtomDREF * dref)
845 atom_full_clear (&dref->header);
846 walker = dref->entries;
849 Atom *atom = (Atom *) aux->data;
851 walker = g_list_next (walker);
852 dref->entries = g_list_remove_link (dref->entries, aux);
853 switch (atom->type) {
855 atom_full_free ((AtomFull *) atom);
858 atom_url_free ((AtomURL *) atom);
861 /* we do nothing, better leak than crash */
869 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
871 atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
872 atom_dref_init (&dinf->dref, context);
876 atom_dinf_clear (AtomDINF * dinf)
878 atom_clear (&dinf->header);
879 atom_dref_clear (&dinf->dref);
883 atom_minf_init (AtomMINF * minf, AtomsContext * context)
885 atom_header_set (&minf->header, FOURCC_minf, 0, 0);
891 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
892 minf->hdlr = atom_hdlr_new ();
893 minf->hdlr->component_type = FOURCC_dhlr;
894 minf->hdlr->handler_type = FOURCC_alis;
898 atom_dinf_init (&minf->dinf, context);
899 atom_stbl_init (&minf->stbl);
903 atom_minf_clear_handlers (AtomMINF * minf)
906 atom_vmhd_free (minf->vmhd);
910 atom_smhd_free (minf->smhd);
914 atom_hmhd_free (minf->hmhd);
920 atom_minf_clear (AtomMINF * minf)
922 atom_clear (&minf->header);
923 atom_minf_clear_handlers (minf);
925 atom_hdlr_free (minf->hdlr);
927 atom_dinf_clear (&minf->dinf);
928 atom_stbl_clear (&minf->stbl);
932 atom_mdhd_init (AtomMDHD * mdhd)
934 guint8 flags[3] = { 0, 0, 0 };
936 atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
937 common_time_info_init (&mdhd->time_info);
938 mdhd->language_code = 0;
943 atom_mdhd_clear (AtomMDHD * mdhd)
945 atom_full_clear (&mdhd->header);
949 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
951 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
953 atom_mdhd_init (&mdia->mdhd);
954 atom_hdlr_init (&mdia->hdlr);
955 atom_minf_init (&mdia->minf, context);
959 atom_mdia_clear (AtomMDIA * mdia)
961 atom_clear (&mdia->header);
962 atom_mdhd_clear (&mdia->mdhd);
963 atom_hdlr_clear (&mdia->hdlr);
964 atom_minf_clear (&mdia->minf);
968 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
973 * 2 -> track in movie
974 * 4 -> track in preview
976 guint8 flags[3] = { 0, 0, 7 };
978 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
980 tkhd->creation_time = tkhd->modification_time = get_current_qt_time ();
985 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
987 tkhd->alternate_group = 0;
990 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
991 tkhd->matrix[0] = 1 << 16;
992 tkhd->matrix[4] = 1 << 16;
993 tkhd->matrix[8] = 16384 << 16;
999 atom_tkhd_clear (AtomTKHD * tkhd)
1001 atom_full_clear (&tkhd->header);
1005 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
1007 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
1009 atom_tkhd_init (&trak->tkhd, context);
1011 atom_mdia_init (&trak->mdia, context);
1015 atom_trak_new (AtomsContext * context)
1017 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
1019 atom_trak_init (trak, context);
1024 atom_trak_clear (AtomTRAK * trak)
1026 atom_clear (&trak->header);
1027 atom_tkhd_clear (&trak->tkhd);
1029 atom_edts_free (trak->edts);
1030 atom_mdia_clear (&trak->mdia);
1034 atom_trak_free (AtomTRAK * trak)
1036 atom_trak_clear (trak);
1041 atom_ilst_init (AtomILST * ilst)
1043 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
1044 ilst->entries = NULL;
1048 atom_ilst_new (void)
1050 AtomILST *ilst = g_new0 (AtomILST, 1);
1052 atom_ilst_init (ilst);
1057 atom_ilst_free (AtomILST * ilst)
1060 atom_info_list_free (ilst->entries);
1061 atom_clear (&ilst->header);
1066 atom_meta_init (AtomMETA * meta)
1068 guint8 flags[3] = { 0, 0, 0 };
1070 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1071 atom_hdlr_init (&meta->hdlr);
1072 /* FIXME (ISOM says this is always 0) */
1073 meta->hdlr.component_type = FOURCC_mhlr;
1074 meta->hdlr.handler_type = FOURCC_mdir;
1079 atom_meta_new (void)
1081 AtomMETA *meta = g_new0 (AtomMETA, 1);
1083 atom_meta_init (meta);
1088 atom_meta_free (AtomMETA * meta)
1090 atom_full_clear (&meta->header);
1091 atom_hdlr_clear (&meta->hdlr);
1093 atom_ilst_free (meta->ilst);
1099 atom_udta_init (AtomUDTA * udta)
1101 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1106 atom_udta_new (void)
1108 AtomUDTA *udta = g_new0 (AtomUDTA, 1);
1110 atom_udta_init (udta);
1115 atom_udta_free (AtomUDTA * udta)
1117 atom_clear (&udta->header);
1119 atom_meta_free (udta->meta);
1122 atom_info_list_free (udta->entries);
1127 atom_tag_data_init (AtomTagData * data)
1129 guint8 flags[] = { 0, 0, 0 };
1131 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1135 atom_tag_data_clear (AtomTagData * data)
1137 atom_full_clear (&data->header);
1138 g_free (data->data);
1143 * Fourcc is the tag fourcc
1144 * flags will be truncated to 24bits
1147 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1149 AtomTag *tag = g_new0 (AtomTag, 1);
1151 tag->header.type = fourcc;
1152 atom_tag_data_init (&tag->data);
1153 atom_full_set_flags_as_uint (&tag->data.header, flags_as_uint);
1158 atom_tag_free (AtomTag * tag)
1160 atom_clear (&tag->header);
1161 atom_tag_data_clear (&tag->data);
1166 atom_mvhd_init (AtomMVHD * mvhd)
1168 guint8 flags[3] = { 0, 0, 0 };
1170 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1172 common_time_info_init (&mvhd->time_info);
1174 mvhd->prefered_rate = 1 << 16;
1175 mvhd->volume = 1 << 8;
1176 mvhd->reserved3 = 0;
1177 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1179 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1180 mvhd->matrix[0] = 1 << 16;
1181 mvhd->matrix[4] = 1 << 16;
1182 mvhd->matrix[8] = 16384 << 16;
1184 mvhd->preview_time = 0;
1185 mvhd->preview_duration = 0;
1186 mvhd->poster_time = 0;
1187 mvhd->selection_time = 0;
1188 mvhd->selection_duration = 0;
1189 mvhd->current_time = 0;
1191 mvhd->next_track_id = 1;
1195 atom_mvhd_clear (AtomMVHD * mvhd)
1197 atom_full_clear (&mvhd->header);
1201 atom_mehd_init (AtomMEHD * mehd)
1203 guint8 flags[3] = { 0, 0, 0 };
1205 atom_full_init (&mehd->header, FOURCC_mehd, 0, 0, 1, flags);
1206 mehd->fragment_duration = 0;
1210 atom_mvex_init (AtomMVEX * mvex)
1212 atom_header_set (&mvex->header, FOURCC_mvex, 0, 0);
1213 atom_mehd_init (&mvex->mehd);
1218 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1220 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1221 atom_mvhd_init (&(moov->mvhd));
1222 atom_mvex_init (&(moov->mvex));
1225 moov->context = *context;
1229 atom_moov_new (AtomsContext * context)
1231 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1233 atom_moov_init (moov, context);
1238 atom_trex_free (AtomTREX * trex)
1240 atom_full_clear (&trex->header);
1245 atom_mvex_clear (AtomMVEX * mvex)
1249 atom_clear (&mvex->header);
1250 walker = mvex->trexs;
1252 atom_trex_free ((AtomTREX *) walker->data);
1253 walker = g_list_next (walker);
1255 g_list_free (mvex->trexs);
1260 atom_moov_free (AtomMOOV * moov)
1264 atom_clear (&moov->header);
1265 atom_mvhd_clear (&moov->mvhd);
1267 walker = moov->traks;
1269 atom_trak_free ((AtomTRAK *) walker->data);
1270 walker = g_list_next (walker);
1272 g_list_free (moov->traks);
1276 atom_udta_free (moov->udta);
1280 atom_mvex_clear (&moov->mvex);
1285 /* -- end of init / free -- */
1287 /* -- copy data functions -- */
1290 atom_full_get_version (AtomFull * full)
1292 return full->version;
1296 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1297 guint8 ** buffer, guint64 * size, guint64 * offset)
1299 guint64 original_offset = *offset;
1302 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1303 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1304 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1305 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1307 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1308 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1309 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1310 prop_copy_uint64 (ti->duration, buffer, size, offset);
1312 return *offset - original_offset;
1316 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1319 /* this only works for non-extended atom size, which is OK
1320 * (though it could be made to do mem_move, etc and write extended size) */
1321 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1325 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1327 guint64 original_offset = *offset;
1329 /* copies type and size */
1330 prop_copy_uint32 (atom->size, buffer, size, offset);
1331 prop_copy_fourcc (atom->type, buffer, size, offset);
1333 /* extended size needed */
1334 if (atom->size == 1) {
1335 /* really should not happen other than with mdat atom;
1336 * would be a problem for size (re)write code, not to mention memory */
1337 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1338 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1341 return *offset - original_offset;
1345 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1348 guint64 original_offset = *offset;
1350 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1354 prop_copy_uint8 (atom->version, buffer, size, offset);
1355 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1357 atom_write_size (buffer, size, offset, original_offset);
1358 return *offset - original_offset;
1362 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1365 guint64 original_offset = *offset;
1368 AtomInfo *info = (AtomInfo *) ai->data;
1370 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1373 ai = g_list_next (ai);
1376 return *offset - original_offset;
1380 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1383 guint64 original_offset = *offset;
1385 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1389 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1391 atom_write_size (buffer, size, offset, original_offset);
1392 return *offset - original_offset;
1396 atom_uuid_copy_data (AtomUUID * uuid, guint8 ** buffer, guint64 * size,
1399 guint64 original_offset = *offset;
1401 if (!atom_copy_data (&uuid->header, buffer, size, offset)) {
1404 prop_copy_uint8_array (uuid->uuid, 16, buffer, size, offset);
1406 prop_copy_uint8_array (uuid->data, uuid->datalen, buffer, size, offset);
1408 atom_write_size (buffer, size, offset, original_offset);
1409 return *offset - original_offset;
1413 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1416 guint64 original_offset = *offset;
1418 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1421 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1422 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1424 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1425 buffer, size, offset);
1427 atom_write_size (buffer, size, offset, original_offset);
1428 return *offset - original_offset;
1432 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1436 guint64 original_offset = *offset;
1438 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1442 version = atom_full_get_version (&(atom->header));
1444 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1445 } else if (version == 1) {
1446 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1448 *offset = original_offset;
1452 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1453 prop_copy_uint16 (atom->volume, buffer, size, offset);
1454 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1455 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1456 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1457 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1458 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1459 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1460 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1461 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1462 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1464 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1466 atom_write_size (buffer, size, offset, original_offset);
1467 return *offset - original_offset;
1471 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1474 guint64 original_offset = *offset;
1476 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1480 if (atom_full_get_version (&tkhd->header) == 0) {
1481 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1482 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1483 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1484 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1485 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1487 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1488 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1489 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1490 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1491 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1494 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1495 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1496 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1497 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1498 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1499 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1501 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1502 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1504 atom_write_size (buffer, size, offset, original_offset);
1505 return *offset - original_offset;
1509 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1512 guint64 original_offset = *offset;
1514 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1518 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1519 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1520 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1521 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1522 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1524 prop_copy_size_string ((guint8 *) hdlr->name, strlen (hdlr->name), buffer,
1527 atom_write_size (buffer, size, offset, original_offset);
1528 return *offset - original_offset;
1532 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1535 guint64 original_offset = *offset;
1537 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1540 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1541 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1543 atom_write_size (buffer, size, offset, original_offset);
1544 return original_offset - *offset;
1548 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1551 guint64 original_offset = *offset;
1553 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1556 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1557 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1559 atom_write_size (buffer, size, offset, original_offset);
1560 return original_offset - *offset;
1564 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1567 guint64 original_offset = *offset;
1569 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1572 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1573 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1574 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1575 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1576 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1578 atom_write_size (buffer, size, offset, original_offset);
1579 return original_offset - *offset;
1583 atom_url_same_file_flag (AtomURL * url)
1585 return (url->header.flags[2] & 0x1) == 1;
1589 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1592 guint64 original_offset = *offset;
1594 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1598 if (!atom_url_same_file_flag (url)) {
1599 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1602 atom_write_size (buffer, size, offset, original_offset);
1603 return original_offset - *offset;
1607 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1610 guint64 original_offset = *offset;
1613 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1617 prop_copy_uint32 (atom_array_get_len (&stts->entries), buffer, size, offset);
1618 /* minimize realloc */
1619 prop_copy_ensure_buffer (buffer, size, offset,
1620 8 * atom_array_get_len (&stts->entries));
1621 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
1622 STTSEntry *entry = &atom_array_index (&stts->entries, i);
1624 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
1625 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
1628 atom_write_size (buffer, size, offset, original_offset);
1629 return *offset - original_offset;
1633 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
1634 guint64 * size, guint64 * offset)
1636 guint64 original_offset = *offset;
1638 if (!atom_copy_data (&se->header, buffer, size, offset)) {
1642 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
1643 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
1645 return *offset - original_offset;
1649 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
1652 guint64 original_offset = *offset;
1654 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
1657 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
1661 atom_write_size (buffer, size, offset, original_offset);
1662 return *offset - original_offset;
1666 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
1667 guint64 * size, guint64 * offset)
1669 guint64 original_offset = *offset;
1671 if (!atom_copy_data (&(frma->header), buffer, size, offset))
1674 prop_copy_fourcc (frma->media_type, buffer, size, offset);
1676 atom_write_size (buffer, size, offset, original_offset);
1677 return *offset - original_offset;
1681 atom_mp4s_copy_data (SampleTableEntryMP4S * mp4s, guint8 ** buffer,
1682 guint64 * size, guint64 * offset)
1684 guint64 original_offset = *offset;
1686 if (!atom_sample_entry_copy_data (&mp4s->se, buffer, size, offset)) {
1689 if (!atom_esds_copy_data (&mp4s->es, buffer, size, offset)) {
1693 atom_write_size (buffer, size, offset, original_offset);
1694 return *offset - original_offset;
1698 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
1699 guint64 * size, guint64 * offset)
1701 guint64 original_offset = *offset;
1703 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
1707 prop_copy_uint32 (hse->size, buffer, size, offset);
1708 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
1710 atom_write_size (buffer, size, offset, original_offset);
1711 return *offset - original_offset;
1715 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
1716 guint64 * size, guint64 * offset)
1718 guint64 original_offset = *offset;
1720 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
1724 prop_copy_uint16 (mp4a->version, buffer, size, offset);
1725 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
1726 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
1727 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
1728 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
1729 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
1730 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
1731 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
1733 /* this should always be 0 for mp4 flavor */
1734 if (mp4a->version == 1) {
1735 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
1736 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
1737 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
1738 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
1741 if (mp4a->extension_atoms) {
1742 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
1746 atom_write_size (buffer, size, offset, original_offset);
1747 return *offset - original_offset;
1751 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
1752 guint64 * size, guint64 * offset)
1754 guint64 original_offset = *offset;
1756 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
1760 prop_copy_uint16 (mp4v->version, buffer, size, offset);
1761 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
1762 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
1763 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
1764 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
1766 prop_copy_uint16 (mp4v->width, buffer, size, offset);
1767 prop_copy_uint16 (mp4v->height, buffer, size, offset);
1769 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
1770 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
1771 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
1773 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
1775 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
1778 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
1779 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
1782 if (mp4v->extension_atoms &&
1783 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
1786 atom_write_size (buffer, size, offset, original_offset);
1787 return *offset - original_offset;
1791 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
1794 guint64 original_offset = *offset;
1797 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
1801 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
1802 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
1803 if (stsz->sample_size == 0) {
1804 /* minimize realloc */
1805 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
1806 /* entry count must match sample count */
1807 g_assert (atom_array_get_len (&stsz->entries) == stsz->table_size);
1808 for (i = 0; i < atom_array_get_len (&stsz->entries); i++) {
1809 prop_copy_uint32 (atom_array_index (&stsz->entries, i), buffer, size,
1814 atom_write_size (buffer, size, offset, original_offset);
1815 return *offset - original_offset;
1819 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
1822 guint64 original_offset = *offset;
1825 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
1829 prop_copy_uint32 (atom_array_get_len (&stsc->entries), buffer, size, offset);
1830 /* minimize realloc */
1831 prop_copy_ensure_buffer (buffer, size, offset,
1832 12 * atom_array_get_len (&stsc->entries));
1834 for (i = 0; i < atom_array_get_len (&stsc->entries); i++) {
1835 STSCEntry *entry = &atom_array_index (&stsc->entries, i);
1837 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
1838 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
1839 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
1842 atom_write_size (buffer, size, offset, original_offset);
1843 return *offset - original_offset;
1847 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
1850 guint64 original_offset = *offset;
1853 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
1857 prop_copy_uint32 (atom_array_get_len (&ctts->entries), buffer, size, offset);
1858 /* minimize realloc */
1859 prop_copy_ensure_buffer (buffer, size, offset,
1860 8 * atom_array_get_len (&ctts->entries));
1861 for (i = 0; i < atom_array_get_len (&ctts->entries); i++) {
1862 CTTSEntry *entry = &atom_array_index (&ctts->entries, i);
1864 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
1865 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
1868 atom_write_size (buffer, size, offset, original_offset);
1869 return *offset - original_offset;
1873 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
1876 guint64 original_offset = *offset;
1878 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
1880 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
1884 prop_copy_uint32 (atom_array_get_len (&stco64->entries), buffer, size,
1887 /* minimize realloc */
1888 prop_copy_ensure_buffer (buffer, size, offset,
1889 8 * atom_array_get_len (&stco64->entries));
1890 for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
1891 guint64 *value = &atom_array_index (&stco64->entries, i);
1894 prop_copy_uint32 ((guint32) * value, buffer, size, offset);
1896 prop_copy_uint64 (*value, buffer, size, offset);
1900 atom_write_size (buffer, size, offset, original_offset);
1901 return *offset - original_offset;
1905 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
1908 guint64 original_offset = *offset;
1911 if (atom_array_get_len (&stss->entries) == 0) {
1912 /* FIXME not needing this atom might be confused with error while copying */
1916 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
1920 prop_copy_uint32 (atom_array_get_len (&stss->entries), buffer, size, offset);
1921 /* minimize realloc */
1922 prop_copy_ensure_buffer (buffer, size, offset,
1923 4 * atom_array_get_len (&stss->entries));
1924 for (i = 0; i < atom_array_get_len (&stss->entries); i++) {
1925 prop_copy_uint32 (atom_array_index (&stss->entries, i), buffer, size,
1929 atom_write_size (buffer, size, offset, original_offset);
1930 return *offset - original_offset;
1934 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
1937 guint64 original_offset = *offset;
1940 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
1944 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
1946 for (walker = g_list_last (stsd->entries); walker != NULL;
1947 walker = g_list_previous (walker)) {
1948 SampleTableEntry *se = (SampleTableEntry *) walker->data;
1950 switch (((Atom *) walker->data)->type) {
1952 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
1953 buffer, size, offset)) {
1958 if (!atom_mp4s_copy_data ((SampleTableEntryMP4S *) walker->data,
1959 buffer, size, offset)) {
1964 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
1965 buffer, size, offset)) {
1970 if (se->kind == VIDEO) {
1971 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
1972 walker->data, buffer, size, offset)) {
1975 } else if (se->kind == AUDIO) {
1976 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
1977 walker->data, buffer, size, offset)) {
1981 if (!atom_hint_sample_entry_copy_data (
1982 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
1990 atom_write_size (buffer, size, offset, original_offset);
1991 return *offset - original_offset;
1995 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
1998 guint64 original_offset = *offset;
2000 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
2004 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
2007 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
2010 /* this atom is optional, so let's check if we need it
2011 * (to avoid false error) */
2012 if (atom_array_get_len (&stbl->stss.entries)) {
2013 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
2018 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
2021 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
2024 if (stbl->ctts && stbl->ctts->do_pts) {
2025 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
2029 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
2033 atom_write_size (buffer, size, offset, original_offset);
2034 return original_offset - *offset;
2039 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
2042 guint64 original_offset = *offset;
2045 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
2049 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
2051 walker = dref->entries;
2052 while (walker != NULL) {
2053 Atom *atom = (Atom *) walker->data;
2055 if (atom->type == FOURCC_url_) {
2056 atom_url_copy_data ((AtomURL *) atom, buffer, size, offset);
2057 } else if (atom->type == FOURCC_alis) {
2058 atom_full_copy_data ((AtomFull *) atom, buffer, size, offset);
2060 g_error ("Unsupported atom used inside dref atom");
2062 walker = g_list_next (walker);
2065 atom_write_size (buffer, size, offset, original_offset);
2066 return *offset - original_offset;
2070 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
2073 guint64 original_offset = *offset;
2075 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
2079 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
2083 atom_write_size (buffer, size, offset, original_offset);
2084 return original_offset - *offset;
2088 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
2091 guint64 original_offset = *offset;
2093 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
2098 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
2101 } else if (minf->smhd) {
2102 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
2105 } else if (minf->hmhd) {
2106 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
2112 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2117 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2120 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2124 atom_write_size (buffer, size, offset, original_offset);
2125 return *offset - original_offset;
2129 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2132 guint64 original_offset = *offset;
2134 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2138 if (!common_time_info_copy_data (&mdhd->time_info,
2139 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2143 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2144 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2146 atom_write_size (buffer, size, offset, original_offset);
2147 return *offset - original_offset;
2151 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2154 guint64 original_offset = *offset;
2156 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2159 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2162 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2166 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2170 atom_write_size (buffer, size, offset, original_offset);
2171 return *offset - original_offset;
2175 atom_elst_copy_data (AtomELST * elst, guint8 ** buffer, guint64 * size,
2178 guint64 original_offset = *offset;
2181 if (!atom_full_copy_data (&elst->header, buffer, size, offset)) {
2185 prop_copy_uint32 (g_slist_length (elst->entries), buffer, size, offset);
2187 for (walker = elst->entries; walker != NULL; walker = g_slist_next (walker)) {
2188 EditListEntry *entry = (EditListEntry *) walker->data;
2189 prop_copy_uint32 (entry->duration, buffer, size, offset);
2190 prop_copy_uint32 (entry->media_time, buffer, size, offset);
2191 prop_copy_uint32 (entry->media_rate, buffer, size, offset);
2193 atom_write_size (buffer, size, offset, original_offset);
2194 return *offset - original_offset;
2198 atom_edts_copy_data (AtomEDTS * edts, guint8 ** buffer, guint64 * size,
2201 guint64 original_offset = *offset;
2203 if (!atom_copy_data (&(edts->header), buffer, size, offset))
2206 if (!atom_elst_copy_data (&(edts->elst), buffer, size, offset))
2209 atom_write_size (buffer, size, offset, original_offset);
2210 return *offset - original_offset;
2214 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2217 guint64 original_offset = *offset;
2219 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2222 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2226 if (!atom_edts_copy_data (trak->edts, buffer, size, offset)) {
2231 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2235 atom_write_size (buffer, size, offset, original_offset);
2236 return *offset - original_offset;
2240 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2243 guint64 original_offset = *offset;
2245 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2249 prop_copy_uint32 (data->reserved, buffer, size, offset);
2250 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2252 atom_write_size (buffer, size, offset, original_offset);
2253 return *offset - original_offset;
2257 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2260 guint64 original_offset = *offset;
2262 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2266 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2270 atom_write_size (buffer, size, offset, original_offset);
2271 return *offset - original_offset;
2275 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2278 guint64 original_offset = *offset;
2280 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2284 if (ilst->entries &&
2285 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2288 atom_write_size (buffer, size, offset, original_offset);
2289 return *offset - original_offset;
2293 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2296 guint64 original_offset = *offset;
2298 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2301 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2305 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2310 atom_write_size (buffer, size, offset, original_offset);
2311 return *offset - original_offset;
2315 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2318 guint64 original_offset = *offset;
2320 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2324 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2328 if (udta->entries) {
2330 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2334 atom_write_size (buffer, size, offset, original_offset);
2335 return *offset - original_offset;
2339 atom_mehd_copy_data (AtomMEHD * mehd, guint8 ** buffer, guint64 * size,
2342 guint64 original_offset = *offset;
2344 if (!atom_full_copy_data (&mehd->header, buffer, size, offset)) {
2348 prop_copy_uint64 (mehd->fragment_duration, buffer, size, offset);
2350 atom_write_size (buffer, size, offset, original_offset);
2351 return *offset - original_offset;
2355 atom_trex_copy_data (AtomTREX * trex, guint8 ** buffer, guint64 * size,
2358 guint64 original_offset = *offset;
2360 if (!atom_full_copy_data (&trex->header, buffer, size, offset)) {
2364 prop_copy_uint32 (trex->track_ID, buffer, size, offset);
2365 prop_copy_uint32 (trex->default_sample_description_index, buffer, size,
2367 prop_copy_uint32 (trex->default_sample_duration, buffer, size, offset);
2368 prop_copy_uint32 (trex->default_sample_size, buffer, size, offset);
2369 prop_copy_uint32 (trex->default_sample_flags, buffer, size, offset);
2371 atom_write_size (buffer, size, offset, original_offset);
2372 return *offset - original_offset;
2376 atom_mvex_copy_data (AtomMVEX * mvex, guint8 ** buffer, guint64 * size,
2379 guint64 original_offset = *offset;
2382 if (!atom_copy_data (&mvex->header, buffer, size, offset)) {
2386 if (!atom_mehd_copy_data (&mvex->mehd, buffer, size, offset)) {
2390 walker = g_list_first (mvex->trexs);
2391 while (walker != NULL) {
2392 if (!atom_trex_copy_data ((AtomTREX *) walker->data, buffer, size, offset)) {
2395 walker = g_list_next (walker);
2398 atom_write_size (buffer, size, offset, original_offset);
2399 return *offset - original_offset;
2403 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2406 guint64 original_offset = *offset;
2409 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2412 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2415 walker = g_list_first (atom->traks);
2416 while (walker != NULL) {
2417 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2420 walker = g_list_next (walker);
2424 if (!atom_udta_copy_data (atom->udta, buffer, size, offset)) {
2429 if (atom->fragmented) {
2430 if (!atom_mvex_copy_data (&atom->mvex, buffer, size, offset)) {
2435 atom_write_size (buffer, size, offset, original_offset);
2436 return *offset - original_offset;
2440 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
2441 guint64 * size, guint64 * offset)
2443 guint64 original_offset = *offset;
2445 if (!atom_copy_data (&(wave->header), buffer, size, offset))
2448 if (wave->extension_atoms) {
2449 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
2453 atom_write_size (buffer, size, offset, original_offset);
2454 return *offset - original_offset;
2457 /* -- end of copy data functions -- */
2459 /* -- general functions, API and support functions */
2461 /* add samples to tables */
2464 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
2469 if ((len = atom_array_get_len (&stsc->entries)) &&
2470 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
2474 nentry.first_chunk = first_chunk;
2475 nentry.samples_per_chunk = nsamples;
2476 nentry.sample_description_index = 1;
2477 atom_array_append (&stsc->entries, nentry, 128);
2481 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
2483 STTSEntry *entry = NULL;
2485 if (G_LIKELY (atom_array_get_len (&stts->entries) != 0))
2486 entry = &atom_array_index (&stts->entries,
2487 atom_array_get_len (&stts->entries) - 1);
2489 if (entry && entry->sample_delta == sample_delta) {
2490 entry->sample_count += sample_count;
2494 nentry.sample_count = sample_count;
2495 nentry.sample_delta = sample_delta;
2496 atom_array_append (&stts->entries, nentry, 256);
2501 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
2505 stsz->table_size += nsamples;
2506 if (stsz->sample_size != 0) {
2507 /* it is constant size, we don't need entries */
2510 for (i = 0; i < nsamples; i++) {
2511 atom_array_append (&stsz->entries, size, 1024);
2516 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
2518 return atom_array_get_len (&stco64->entries);
2522 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
2524 atom_array_append (&stco64->entries, entry, 256);
2525 if (entry > G_MAXUINT32)
2526 stco64->header.header.type = FOURCC_co64;
2530 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
2532 atom_array_append (&stss->entries, sample, 512);
2536 atom_stbl_add_stss_entry (AtomSTBL * stbl)
2538 guint32 sample_index = stbl->stsz.table_size;
2540 atom_stss_add_entry (&stbl->stss, sample_index);
2544 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
2546 CTTSEntry *entry = NULL;
2548 if (G_LIKELY (atom_array_get_len (&ctts->entries) != 0))
2549 entry = &atom_array_index (&ctts->entries,
2550 atom_array_get_len (&ctts->entries) - 1);
2552 if (entry == NULL || entry->sampleoffset != offset) {
2555 nentry.samplecount = nsamples;
2556 nentry.sampleoffset = offset;
2557 atom_array_append (&ctts->entries, nentry, 256);
2559 ctts->do_pts = TRUE;
2561 entry->samplecount += nsamples;
2566 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
2568 if (stbl->ctts == NULL) {
2569 stbl->ctts = atom_ctts_new ();
2571 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
2575 atom_stbl_add_samples (AtomSTBL * stbl, guint32 nsamples, guint32 delta,
2576 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
2578 atom_stts_add_entry (&stbl->stts, nsamples, delta);
2579 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
2580 atom_stco64_add_entry (&stbl->stco64, chunk_offset);
2581 atom_stsc_add_new_entry (&stbl->stsc,
2582 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
2584 atom_stbl_add_stss_entry (stbl);
2585 /* always store to arrange for consistent content */
2586 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
2590 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
2591 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
2593 AtomSTBL *stbl = &trak->mdia.minf.stbl;
2594 atom_stbl_add_samples (stbl, nsamples, delta, size, chunk_offset, sync,
2598 /* trak and moov molding */
2601 atom_trak_get_timescale (AtomTRAK * trak)
2603 return trak->mdia.mdhd.time_info.timescale;
2607 atom_trak_get_id (AtomTRAK * trak)
2609 return trak->tkhd.track_ID;
2613 atom_trak_set_id (AtomTRAK * trak, guint32 id)
2615 trak->tkhd.track_ID = id;
2619 atom_moov_add_trex (AtomMOOV * moov, AtomTREX * trex)
2621 moov->mvex.trexs = g_list_append (moov->mvex.trexs, trex);
2625 atom_trex_new (AtomTRAK * trak)
2627 guint8 flags[3] = { 0, 0, 0 };
2628 AtomTREX *trex = g_new0 (AtomTREX, 1);
2630 atom_full_init (&trex->header, FOURCC_trex, 0, 0, 0, flags);
2632 trex->track_ID = trak->tkhd.track_ID;
2633 trex->default_sample_description_index = 1;
2634 trex->default_sample_duration = 0;
2635 trex->default_sample_size = 0;
2636 trex->default_sample_flags = 0;
2642 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
2644 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
2645 moov->traks = g_list_append (moov->traks, trak);
2646 /* additional trak means also new trex */
2647 atom_moov_add_trex (moov, atom_trex_new (trak));
2651 atom_trak_get_duration (AtomTRAK * trak)
2653 return trak->tkhd.duration;
2657 atom_stts_get_total_duration (AtomSTTS * stts)
2662 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
2663 STTSEntry *entry = &atom_array_index (&stts->entries, i);
2665 sum += (guint64) (entry->sample_count) * entry->sample_delta;
2671 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
2673 trak->mdia.mdhd.time_info.duration =
2674 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
2675 if (trak->mdia.mdhd.time_info.timescale != 0) {
2676 trak->tkhd.duration =
2677 gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration,
2678 moov_timescale, trak->mdia.mdhd.time_info.timescale);
2680 trak->tkhd.duration = 0;
2685 atom_moov_get_timescale (AtomMOOV * moov)
2687 return moov->mvhd.time_info.timescale;
2691 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
2693 moov->mvhd.time_info.timescale = timescale;
2697 atom_moov_update_duration (AtomMOOV * moov)
2699 GList *traks = moov->traks;
2700 guint64 dur, duration = 0;
2703 AtomTRAK *trak = (AtomTRAK *) traks->data;
2705 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
2706 dur = atom_trak_get_duration (trak);
2709 traks = g_list_next (traks);
2711 moov->mvhd.time_info.duration = duration;
2712 moov->mvex.mehd.fragment_duration = duration;
2716 atom_moov_set_fragmented (AtomMOOV * moov, gboolean fragmented)
2718 moov->fragmented = fragmented;
2722 atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset)
2726 for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
2727 guint64 *value = &atom_array_index (&stco64->entries, i);
2734 atom_moov_chunks_add_offset (AtomMOOV * moov, guint32 offset)
2736 GList *traks = moov->traks;
2739 AtomTRAK *trak = (AtomTRAK *) traks->data;
2741 atom_stco64_chunks_add_offset (&trak->mdia.minf.stbl.stco64, offset);
2742 traks = g_list_next (traks);
2747 atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
2748 guint32 max_bitrate)
2750 AtomESDS *esds = NULL;
2751 AtomData *btrt = NULL;
2752 AtomWAVE *wave = NULL;
2755 GList *extensioniter = NULL;
2757 g_return_if_fail (trak != NULL);
2759 if (avg_bitrate == 0 && max_bitrate == 0)
2762 stsd = &trak->mdia.minf.stbl.stsd;
2763 for (iter = stsd->entries; iter; iter = g_list_next (iter)) {
2764 SampleTableEntry *entry = iter->data;
2766 switch (entry->kind) {
2768 SampleTableEntryMP4A *audioentry = (SampleTableEntryMP4A *) entry;
2769 extensioniter = audioentry->extension_atoms;
2773 SampleTableEntryMP4V *videoentry = (SampleTableEntryMP4V *) entry;
2774 extensioniter = videoentry->extension_atoms;
2782 for (; extensioniter; extensioniter = g_list_next (extensioniter)) {
2783 AtomInfo *atominfo = extensioniter->data;
2784 if (atominfo->atom->type == FOURCC_esds) {
2785 esds = (AtomESDS *) atominfo->atom;
2786 } else if (atominfo->atom->type == FOURCC_btrt) {
2787 btrt = (AtomData *) atominfo->atom;
2788 } else if (atominfo->atom->type == FOURCC_wave) {
2789 wave = (AtomWAVE *) atominfo->atom;
2793 /* wave might have an esds internally */
2795 for (extensioniter = wave->extension_atoms; extensioniter;
2796 extensioniter = g_list_next (extensioniter)) {
2797 AtomInfo *atominfo = extensioniter->data;
2798 if (atominfo->atom->type == FOURCC_esds) {
2799 esds = (AtomESDS *) atominfo->atom;
2806 if (avg_bitrate && esds->es.dec_conf_desc.avg_bitrate == 0)
2807 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
2808 if (max_bitrate && esds->es.dec_conf_desc.max_bitrate == 0)
2809 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
2812 /* type(4bytes) + size(4bytes) + buffersize(4bytes) +
2813 * maxbitrate(bytes) + avgbitrate(bytes) */
2814 if (max_bitrate && GST_READ_UINT32_BE (btrt->data + 4) == 0)
2815 GST_WRITE_UINT32_BE (btrt->data + 4, max_bitrate);
2816 if (avg_bitrate && GST_READ_UINT32_BE (btrt->data + 8) == 0)
2817 GST_WRITE_UINT32_BE (btrt->data + 8, avg_bitrate);
2822 * Meta tags functions
2825 atom_moov_init_metatags (AtomMOOV * moov, AtomsContext * context)
2828 moov->udta = atom_udta_new ();
2830 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
2831 if (!moov->udta->meta) {
2832 moov->udta->meta = atom_meta_new ();
2834 if (!moov->udta->meta->ilst) {
2835 moov->udta->meta->ilst = atom_ilst_new ();
2841 atom_tag_data_alloc_data (AtomTagData * data, guint size)
2843 if (data->data != NULL) {
2844 g_free (data->data);
2846 data->data = g_new0 (guint8, size);
2847 data->datalen = size;
2851 atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
2855 atom_moov_init_metatags (moov, &moov->context);
2856 if (moov->udta->meta)
2857 entries = &moov->udta->meta->ilst->entries;
2859 entries = &moov->udta->entries;
2860 *entries = g_list_append (*entries, tag);
2864 atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2865 const guint8 * data, guint size)
2870 tag = atom_tag_new (fourcc, flags);
2872 atom_tag_data_alloc_data (tdata, size);
2873 g_memmove (tdata->data, data, size);
2875 atom_moov_append_tag (moov,
2876 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
2881 atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2883 gint len = strlen (value);
2886 atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
2890 atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2893 guint8 data[8] = { 0, };
2896 GST_WRITE_UINT16_BE (data, value);
2897 atom_moov_add_tag (moov, fourcc, flags, data, 2);
2899 GST_WRITE_UINT32_BE (data + 2, value);
2900 atom_moov_add_tag (moov, fourcc, flags, data, 8);
2905 _gst_buffer_new_wrapped (gpointer mem, gsize size, GFreeFunc free_func)
2909 buf = gst_buffer_new ();
2910 gst_buffer_take_memory (buf, -1,
2911 gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
2912 mem, free_func, size, 0, size));
2918 atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
2920 AtomData *data_atom;
2928 /* blob is unparsed atom;
2929 * extract size and fourcc, and wrap remainder in data atom */
2930 len = GST_READ_UINT32_BE (data);
2931 fourcc = GST_READ_UINT32_LE (data + 4);
2935 buf = _gst_buffer_new_wrapped (data + 8, len - 8, NULL);
2936 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2937 gst_buffer_unref (buf);
2939 atom_moov_append_tag (moov,
2940 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2945 atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
2948 AtomData *data_atom;
2952 /* need full atom */
2953 bdata = g_malloc (size + 4);
2954 /* full atom: version and flags */
2955 GST_WRITE_UINT32_BE (bdata, 0);
2956 memcpy (bdata + 4, data, size);
2958 buf = _gst_buffer_new_wrapped (bdata, size + 4, g_free);
2959 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2960 gst_buffer_unref (buf);
2962 atom_moov_append_tag (moov,
2963 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2968 language_code (const char *lang)
2970 g_return_val_if_fail (lang != NULL, 0);
2971 g_return_val_if_fail (strlen (lang) == 3, 0);
2973 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
2974 ((lang[2] - 0x60) & 0x1F);
2978 atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc,
2979 const gchar * value, gint16 ivalue)
2981 gint len = 0, size = 0;
2985 len = strlen (value);
2992 data = g_malloc (size + 3);
2993 /* language tag and null-terminated UTF-8 string */
2995 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
2996 /* include 0 terminator */
2997 memcpy (data + 2, value, len + 1);
2999 /* 16-bit unsigned int if standalone, otherwise 8-bit */
3002 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
3004 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
3009 atom_moov_add_3gp_tag (moov, fourcc, data, size);
3014 atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
3016 atom_moov_add_3gp_str_int_tag (moov, fourcc, value, -1);
3020 atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value)
3022 atom_moov_add_3gp_str_int_tag (moov, fourcc, NULL, value);
3026 atom_moov_add_xmp_tags (AtomMOOV * moov, GstBuffer * xmpbuffer)
3028 AtomData *data_atom = NULL;
3030 if (moov->context.flavor == ATOMS_TREE_FLAVOR_MOV) {
3032 data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
3033 atom_moov_init_metatags (moov, &moov->context);
3034 moov->udta->entries = g_list_append (moov->udta->entries,
3035 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3039 GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
3045 * Functions for specifying media types
3049 atom_minf_set_audio (AtomMINF * minf)
3051 atom_minf_clear_handlers (minf);
3052 minf->smhd = atom_smhd_new ();
3056 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
3058 atom_minf_clear_handlers (minf);
3059 minf->vmhd = atom_vmhd_new (context);
3063 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
3066 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3067 hdlr->component_type = comp_type;
3069 hdlr->handler_type = hdlr_type;
3073 atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
3076 g_free (hdlr->name);
3077 hdlr->name = g_strdup (name);
3081 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
3083 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3084 /* Some players (low-end hardware) check for this name, which is what
3085 * QuickTime itself sets */
3086 atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3090 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3092 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3093 /* Some players (low-end hardware) check for this name, which is what
3094 * QuickTime itself sets */
3095 atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3099 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3101 atom_mdia_set_hdlr_type_audio (mdia, context);
3102 atom_minf_set_audio (&mdia->minf);
3106 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3108 atom_mdia_set_hdlr_type_video (mdia, context);
3109 atom_minf_set_video (&mdia->minf, context);
3113 atom_tkhd_set_audio (AtomTKHD * tkhd)
3115 tkhd->volume = 0x0100;
3116 tkhd->width = tkhd->height = 0;
3120 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3125 /* qt and ISO base media do not contradict, and examples agree */
3126 tkhd->width = width;
3127 tkhd->height = height;
3131 atom_edts_add_entry (AtomEDTS * edts, EditListEntry * entry)
3133 edts->elst.entries = g_slist_append (edts->elst.entries, entry);
3137 * Adds a new entry to this trak edits list
3138 * duration is in the moov's timescale
3139 * media_time is the offset in the media time to start from (media's timescale)
3140 * rate is a 32 bits fixed-point
3143 atom_trak_add_elst_entry (AtomTRAK * trak, guint32 duration, guint32 media_time,
3146 EditListEntry *entry = g_new (EditListEntry, 1);
3148 entry->duration = duration;
3149 entry->media_time = media_time;
3150 entry->media_rate = rate;
3152 if (trak->edts == NULL) {
3153 trak->edts = atom_edts_new ();
3155 atom_edts_add_entry (trak->edts, entry);
3158 /* re-negotiation is prevented at top-level, so only 1 entry expected.
3159 * Quite some more care here and elsewhere may be needed to
3160 * support several entries */
3161 static SampleTableEntryMP4A *
3162 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
3165 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3166 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
3168 mp4a->se.header.type = type;
3169 mp4a->se.kind = AUDIO;
3170 mp4a->compression_id = -1;
3171 mp4a->se.data_reference_index = 1;
3173 stsd->entries = g_list_prepend (stsd->entries, mp4a);
3178 static SampleTableEntryMP4V *
3179 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
3182 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
3183 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3185 mp4v->se.header.type = type;
3186 mp4v->se.kind = VIDEO;
3187 mp4v->se.data_reference_index = 1;
3188 mp4v->horizontal_resolution = 72 << 16;
3189 mp4v->vertical_resolution = 72 << 16;
3190 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3191 mp4v->spatial_quality = 512;
3192 mp4v->temporal_quality = 512;
3195 stsd->entries = g_list_prepend (stsd->entries, mp4v);
3201 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
3203 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
3207 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
3209 atom_tkhd_set_audio (&trak->tkhd);
3210 atom_mdia_set_audio (&trak->mdia, context);
3214 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
3217 atom_tkhd_set_video (&trak->tkhd, context, width, height);
3218 atom_mdia_set_video (&trak->mdia, context);
3222 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
3225 atom_trak_set_audio (trak, context);
3226 trak->mdia.mdhd.time_info.timescale = rate;
3230 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
3231 guint32 rate, guint32 width, guint32 height)
3233 atom_trak_set_video (trak, context, width, height);
3234 trak->mdia.mdhd.time_info.timescale = rate;
3235 trak->tkhd.width = width << 16;
3236 trak->tkhd.height = height << 16;
3240 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
3241 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
3243 SampleTableEntryMP4A *ste;
3245 atom_trak_set_audio_commons (trak, context, scale);
3246 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3247 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
3249 trak->is_video = FALSE;
3250 trak->is_h264 = FALSE;
3252 ste->version = entry->version;
3253 ste->compression_id = entry->compression_id;
3254 ste->sample_size = entry->sample_size;
3255 ste->sample_rate = entry->sample_rate << 16;
3256 ste->channels = entry->channels;
3258 ste->samples_per_packet = entry->samples_per_packet;
3259 ste->bytes_per_sample = entry->bytes_per_sample;
3260 ste->bytes_per_packet = entry->bytes_per_packet;
3261 ste->bytes_per_frame = entry->bytes_per_frame;
3264 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
3266 /* 0 size means variable size */
3267 atom_trak_set_constant_size_samples (trak, sample_size);
3271 build_pasp_extension (AtomTRAK * trak, gint par_width, gint par_height)
3273 AtomData *atom_data;
3277 data = g_malloc (8);
3278 /* ihdr = image header box */
3279 GST_WRITE_UINT32_BE (data, par_width);
3280 GST_WRITE_UINT32_BE (data + 4, par_height);
3282 buf = _gst_buffer_new_wrapped (data, 8, g_free);
3283 atom_data = atom_data_new_from_gst_buffer (FOURCC_pasp, buf);
3284 gst_buffer_unref (buf);
3286 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3291 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
3292 VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
3294 SampleTableEntryMP4V *ste;
3295 gint dwidth, dheight;
3296 gint par_n = 0, par_d = 0;
3298 if ((entry->par_n != 1 || entry->par_d != 1) &&
3299 (entry->par_n != entry->par_d)) {
3300 par_n = entry->par_n;
3301 par_d = entry->par_d;
3304 dwidth = entry->width;
3305 dheight = entry->height;
3306 /* ISO file spec says track header w/h indicates track's visual presentation
3307 * (so this together with pixels w/h implicitly defines PAR) */
3308 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
3309 if (par_n > par_d) {
3310 dwidth = entry->width * par_n / par_d;
3311 dheight = entry->height;
3313 dwidth = entry->width * par_n / par_d;
3314 dheight = entry->height;
3318 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
3319 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3320 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
3322 trak->is_video = TRUE;
3323 trak->is_h264 = (entry->fourcc == FOURCC_avc1);
3325 ste->version = entry->version;
3326 ste->width = entry->width;
3327 ste->height = entry->height;
3328 ste->depth = entry->depth;
3329 ste->color_table_id = entry->color_table_id;
3330 ste->frame_count = entry->frame_count;
3333 ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
3335 /* QT spec has a pasp extension atom in stsd that can hold PAR */
3336 if (par_n && (context->flavor == ATOMS_TREE_FLAVOR_MOV)) {
3337 ste->extension_atoms = g_list_append (ste->extension_atoms,
3338 build_pasp_extension (trak, par_n, par_d));
3343 atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
3345 guint8 flags[3] = { 0, 0, 0 };
3347 atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
3348 mfhd->sequence_number = sequence_number;
3352 atom_moof_init (AtomMOOF * moof, AtomsContext * context,
3353 guint32 sequence_number)
3355 atom_header_set (&moof->header, FOURCC_moof, 0, 0);
3356 atom_mfhd_init (&moof->mfhd, sequence_number);
3361 atom_moof_new (AtomsContext * context, guint32 sequence_number)
3363 AtomMOOF *moof = g_new0 (AtomMOOF, 1);
3365 atom_moof_init (moof, context, sequence_number);
3370 atom_trun_free (AtomTRUN * trun)
3372 atom_full_clear (&trun->header);
3373 atom_array_clear (&trun->entries);
3378 atom_sdtp_free (AtomSDTP * sdtp)
3380 atom_full_clear (&sdtp->header);
3381 atom_array_clear (&sdtp->entries);
3386 atom_traf_free (AtomTRAF * traf)
3390 walker = traf->truns;
3392 atom_trun_free ((AtomTRUN *) walker->data);
3393 walker = g_list_next (walker);
3395 g_list_free (traf->truns);
3398 walker = traf->sdtps;
3400 atom_sdtp_free ((AtomSDTP *) walker->data);
3401 walker = g_list_next (walker);
3403 g_list_free (traf->sdtps);
3410 atom_moof_free (AtomMOOF * moof)
3414 walker = moof->trafs;
3416 atom_traf_free ((AtomTRAF *) walker->data);
3417 walker = g_list_next (walker);
3419 g_list_free (moof->trafs);
3426 atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
3429 guint64 original_offset = *offset;
3431 if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
3435 prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
3437 atom_write_size (buffer, size, offset, original_offset);
3438 return *offset - original_offset;
3442 atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
3445 guint64 original_offset = *offset;
3448 if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
3452 prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
3454 flags = atom_full_get_flags_as_uint (&tfhd->header);
3456 if (flags & TF_BASE_DATA_OFFSET)
3457 prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
3458 if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
3459 prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
3460 if (flags & TF_DEFAULT_SAMPLE_DURATION)
3461 prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
3462 if (flags & TF_DEFAULT_SAMPLE_SIZE)
3463 prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
3464 if (flags & TF_DEFAULT_SAMPLE_FLAGS)
3465 prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
3467 atom_write_size (buffer, size, offset, original_offset);
3468 return *offset - original_offset;
3472 atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
3473 guint64 * offset, guint32 * data_offset)
3475 guint64 original_offset = *offset;
3478 flags = atom_full_get_flags_as_uint (&trun->header);
3480 /* if first trun in moof, forcibly add data_offset and record
3481 * where it must be written later on */
3482 if (data_offset && !*data_offset) {
3483 flags |= TR_DATA_OFFSET;
3485 flags &= ~TR_DATA_OFFSET;
3488 atom_full_set_flags_as_uint (&trun->header, flags);
3490 if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
3494 prop_copy_uint32 (trun->sample_count, buffer, size, offset);
3496 if (flags & TR_DATA_OFFSET) {
3497 *data_offset = *offset;
3498 prop_copy_int32 (trun->data_offset, buffer, size, offset);
3500 if (flags & TR_FIRST_SAMPLE_FLAGS)
3501 prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
3503 for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
3504 TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
3506 if (flags & TR_SAMPLE_DURATION)
3507 prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
3508 if (flags & TR_SAMPLE_SIZE)
3509 prop_copy_uint32 (entry->sample_size, buffer, size, offset);
3510 if (flags & TR_SAMPLE_FLAGS)
3511 prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
3512 if (flags & TR_COMPOSITION_TIME_OFFSETS)
3513 prop_copy_uint32 (entry->sample_composition_time_offset,
3514 buffer, size, offset);
3517 atom_write_size (buffer, size, offset, original_offset);
3518 return *offset - original_offset;
3522 atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
3525 guint64 original_offset = *offset;
3527 if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
3531 /* all entries at once */
3532 prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
3533 atom_array_get_len (&sdtp->entries), buffer, size, offset);
3535 atom_write_size (buffer, size, offset, original_offset);
3536 return *offset - original_offset;
3540 atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
3541 guint64 * offset, guint32 * data_offset)
3543 guint64 original_offset = *offset;
3546 if (!atom_copy_data (&traf->header, buffer, size, offset)) {
3549 if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
3553 walker = g_list_first (traf->truns);
3554 while (walker != NULL) {
3555 if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset,
3559 walker = g_list_next (walker);
3562 walker = g_list_first (traf->sdtps);
3563 while (walker != NULL) {
3564 if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
3567 walker = g_list_next (walker);
3570 atom_write_size (buffer, size, offset, original_offset);
3571 return *offset - original_offset;
3574 /* creates moof atom; metadata is written expecting actual buffer data
3575 * is in mdata directly after moof, and is consecutively written per trak */
3577 atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
3578 guint64 * size, guint64 * offset)
3580 guint64 original_offset = *offset;
3582 guint32 data_offset = 0;
3584 if (!atom_copy_data (&moof->header, buffer, size, offset))
3587 if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
3590 walker = g_list_first (moof->trafs);
3591 while (walker != NULL) {
3592 if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset,
3596 walker = g_list_next (walker);
3599 atom_write_size (buffer, size, offset, original_offset);
3601 if (*buffer && data_offset) {
3602 /* first trun needs a data-offset relative to moof start
3603 * = moof size + mdat prefix */
3604 GST_WRITE_UINT32_BE (*buffer + data_offset, *offset - original_offset + 8);
3607 return *offset - original_offset;
3611 atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
3613 guint8 flags[3] = { 0, 0, 0 };
3615 atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
3616 tfhd->track_ID = track_ID;
3617 tfhd->base_data_offset = 0;
3618 tfhd->sample_description_index = 1;
3619 tfhd->default_sample_duration = 0;
3620 tfhd->default_sample_size = 0;
3621 tfhd->default_sample_flags = 0;
3625 atom_trun_init (AtomTRUN * trun)
3627 guint8 flags[3] = { 0, 0, 0 };
3629 atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
3630 trun->sample_count = 0;
3631 trun->data_offset = 0;
3632 trun->first_sample_flags = 0;
3633 atom_array_init (&trun->entries, 512);
3637 atom_trun_new (void)
3639 AtomTRUN *trun = g_new0 (AtomTRUN, 1);
3641 atom_trun_init (trun);
3646 atom_sdtp_init (AtomSDTP * sdtp)
3648 guint8 flags[3] = { 0, 0, 0 };
3650 atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
3651 atom_array_init (&sdtp->entries, 512);
3655 atom_sdtp_new (AtomsContext * context)
3657 AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
3659 atom_sdtp_init (sdtp);
3664 atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
3666 traf->sdtps = g_list_append (traf->sdtps, sdtp);
3670 atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
3672 /* it does not make much/any sense according to specs,
3673 * but that's how MS isml samples seem to do it */
3674 atom_array_append (&sdtp->entries, val, 256);
3678 atom_trun_add_samples (AtomTRUN * trun, guint32 delta, guint32 size,
3679 guint32 flags, gint64 pts_offset)
3681 TRUNSampleEntry nentry;
3683 if (pts_offset != 0)
3684 trun->header.flags[1] |= TR_COMPOSITION_TIME_OFFSETS;
3686 nentry.sample_duration = delta;
3687 nentry.sample_size = size;
3688 nentry.sample_flags = flags;
3689 nentry.sample_composition_time_offset = pts_offset;
3690 atom_array_append (&trun->entries, nentry, 256);
3691 trun->sample_count++;
3695 atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
3697 atom_header_set (&traf->header, FOURCC_traf, 0, 0);
3698 atom_tfhd_init (&traf->tfhd, track_ID);
3701 if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
3702 atom_traf_add_sdtp (traf, atom_sdtp_new (context));
3706 atom_traf_new (AtomsContext * context, guint32 track_ID)
3708 AtomTRAF *traf = g_new0 (AtomTRAF, 1);
3710 atom_traf_init (traf, context, track_ID);
3715 atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
3717 traf->truns = g_list_append (traf->truns, trun);
3721 atom_traf_add_samples (AtomTRAF * traf, guint32 delta, guint32 size,
3722 gboolean sync, gint64 pts_offset, gboolean sdtp_sync)
3727 /* 0x10000 is sample-is-difference-sample flag
3728 * low byte stuff is what ismv uses */
3729 flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
3731 if (G_UNLIKELY (!traf->truns)) {
3732 trun = atom_trun_new ();
3733 atom_traf_add_trun (traf, trun);
3734 /* optimistic; indicate all defaults present in tfhd */
3735 traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
3736 TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
3737 traf->tfhd.default_sample_duration = delta;
3738 traf->tfhd.default_sample_size = size;
3739 traf->tfhd.default_sample_flags = flags;
3740 trun->first_sample_flags = flags;
3743 trun = traf->truns->data;
3745 /* check if still matching defaults,
3746 * if not, abandon default and need entry for each sample */
3747 if (traf->tfhd.default_sample_duration != delta) {
3748 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
3749 trun->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
3751 if (traf->tfhd.default_sample_size != size) {
3752 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
3753 trun->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
3755 if (traf->tfhd.default_sample_flags != flags) {
3756 if (trun->sample_count == 1) {
3757 /* at least will need first sample flag */
3758 traf->tfhd.default_sample_flags = flags;
3759 trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
3761 /* now we need sample flags for each sample */
3762 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
3763 trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
3764 trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
3768 atom_trun_add_samples (traf->truns->data, delta, size, flags, pts_offset);
3771 atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
3775 atom_traf_get_sample_num (AtomTRAF * traf)
3779 if (G_UNLIKELY (!traf->truns))
3782 trun = traf->truns->data;
3783 return atom_array_get_len (&trun->entries);
3787 atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
3789 moof->trafs = g_list_append (moof->trafs, traf);
3793 atom_tfra_free (AtomTFRA * tfra)
3795 atom_full_clear (&tfra->header);
3796 atom_array_clear (&tfra->entries);
3801 atom_mfra_new (AtomsContext * context)
3803 AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
3805 atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
3810 atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
3812 mfra->tfras = g_list_append (mfra->tfras, tfra);
3816 atom_mfra_free (AtomMFRA * mfra)
3820 walker = mfra->tfras;
3822 atom_tfra_free ((AtomTFRA *) walker->data);
3823 walker = g_list_next (walker);
3825 g_list_free (mfra->tfras);
3828 atom_clear (&mfra->header);
3833 atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
3835 guint8 flags[3] = { 0, 0, 0 };
3837 atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
3838 tfra->track_ID = track_ID;
3839 atom_array_init (&tfra->entries, 512);
3843 atom_tfra_new (AtomsContext * context, guint32 track_ID)
3845 AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
3847 atom_tfra_init (tfra, track_ID);
3853 need_bytes (guint32 num)
3864 atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
3870 entry.moof_offset = 0;
3871 /* always write a single trun in a single traf */
3872 entry.traf_number = 1;
3873 entry.trun_number = 1;
3874 entry.sample_number = sample_num;
3876 /* auto-use 64 bits if needed */
3877 if (dts > G_MAXUINT32)
3878 tfra->header.version = 1;
3880 /* 1 byte will always do for traf and trun number,
3881 * check how much sample_num needs */
3882 tfra->lengths = (tfra->lengths & 0xfc) ||
3883 MAX (tfra->lengths, need_bytes (sample_num));
3885 atom_array_append (&tfra->entries, entry, 256);
3889 atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
3893 /* auto-use 64 bits if needed */
3894 if (offset > G_MAXUINT32)
3895 tfra->header.version = 1;
3897 for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
3898 TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
3900 if (entry->moof_offset)
3902 entry->moof_offset = offset;
3907 atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
3910 guint64 original_offset = *offset;
3917 if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
3921 prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
3922 prop_copy_uint32 (tfra->lengths, buffer, size, offset);
3923 prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
3925 version = tfra->header.version;
3926 for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
3927 entry = &atom_array_index (&tfra->entries, i);
3929 prop_copy_uint64 (entry->time, buffer, size, offset);
3930 prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
3932 prop_copy_uint32 (entry->time, buffer, size, offset);
3933 prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
3936 bytes = (tfra->lengths & (0x3 << 4)) + 1;
3937 data = GUINT32_TO_BE (entry->traf_number);
3938 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3939 buffer, size, offset);
3941 bytes = (tfra->lengths & (0x3 << 2)) + 1;
3942 data = GUINT32_TO_BE (entry->trun_number);
3943 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3944 buffer, size, offset);
3946 bytes = (tfra->lengths & (0x3)) + 1;
3947 data = GUINT32_TO_BE (entry->sample_number);
3948 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3949 buffer, size, offset);
3953 atom_write_size (buffer, size, offset, original_offset);
3954 return *offset - original_offset;
3958 atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
3961 guint64 original_offset = *offset;
3962 guint8 flags[3] = { 0, 0, 0 };
3965 atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
3967 if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
3971 prop_copy_uint32 (s, buffer, size, offset);
3973 atom_write_size (buffer, size, offset, original_offset);
3975 return *offset - original_offset;
3980 atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
3983 guint64 original_offset = *offset;
3986 if (!atom_copy_data (&mfra->header, buffer, size, offset))
3989 walker = g_list_first (mfra->tfras);
3990 while (walker != NULL) {
3991 if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
3994 walker = g_list_next (walker);
3997 /* 16 is the size of the mfro atom */
3998 if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
4002 atom_write_size (buffer, size, offset, original_offset);
4003 return *offset - original_offset;
4006 /* some sample description construction helpers */
4009 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
4010 const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
4015 track_id = trak->tkhd.track_ID;
4017 esds = atom_esds_new ();
4018 esds->es.id = track_id & 0xFFFF;
4019 esds->es.dec_conf_desc.object_type = object_type;
4020 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
4022 if (avg_bitrate > 0)
4023 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
4024 if (max_bitrate > 0)
4025 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
4027 /* optional DecoderSpecificInfo */
4029 DecoderSpecificInfoDescriptor *desc;
4032 esds->es.dec_conf_desc.dec_specific_info = desc =
4033 desc_dec_specific_info_new ();
4034 size = gst_buffer_get_size ((GstBuffer *) codec_data);
4035 desc_dec_specific_info_alloc_data (desc, size);
4036 gst_buffer_extract ((GstBuffer *) codec_data, 0, desc->data, size);
4039 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
4044 build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
4045 guint32 max_bitrate)
4047 AtomData *atom_data;
4051 data = g_malloc (12);
4052 GST_WRITE_UINT32_BE (data, buffer_size_db);
4053 GST_WRITE_UINT32_BE (data + 4, max_bitrate);
4054 GST_WRITE_UINT32_BE (data + 8, avg_bitrate);
4056 buf = _gst_buffer_new_wrapped (data, 12, g_free);
4057 atom_data = atom_data_new_from_gst_buffer (FOURCC_btrt, buf);
4058 gst_buffer_unref (buf);
4060 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4065 build_mov_wave_extension (AtomTRAK * trak, guint32 fourcc, AtomInfo * atom1,
4066 AtomInfo * atom2, gboolean terminator)
4072 /* Build WAVE atom for sample table entry */
4073 wave = atom_wave_new ();
4075 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
4077 ext_atom = (Atom *) atom_data_new (FOURCC_null);
4078 wave->extension_atoms =
4079 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4080 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4083 /* Add supplied atoms to WAVE */
4085 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
4087 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
4089 /* Add FRMA to the WAVE */
4090 frma = atom_frma_new ();
4091 frma->media_type = fourcc;
4093 wave->extension_atoms =
4094 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4095 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4097 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4102 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
4103 guint32 avg_bitrate, guint32 max_bitrate)
4105 AtomInfo *esds, *mp4a;
4109 /* Add ESDS atom to WAVE */
4110 esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
4111 ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
4113 /* Add MP4A atom to the WAVE:
4114 * not really in spec, but makes offset based players happy */
4115 buf = _gst_buffer_new_wrapped (&tmp, 4, NULL);
4116 mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
4117 gst_buffer_unref (buf);
4119 return build_mov_wave_extension (trak, FOURCC_mp4a, mp4a, esds, TRUE);
4123 build_mov_alac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
4127 alac = build_codec_data_extension (FOURCC_alac, codec_data);
4129 return build_mov_wave_extension (trak, FOURCC_alac, NULL, alac, TRUE);
4133 build_fiel_extension (gint fields)
4135 AtomData *atom_data;
4143 buf = _gst_buffer_new_wrapped (&f, 1, NULL);
4145 atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('f', 'i', 'e', 'l'), buf);
4146 gst_buffer_unref (buf);
4148 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4153 build_jp2x_extension (const GstBuffer * prefix)
4155 AtomData *atom_data;
4162 atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('j', 'p', '2', 'x'),
4165 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4170 build_jp2h_extension (AtomTRAK * trak, gint width, gint height,
4171 const gchar * colorspace, gint ncomp, const GValue * cmap_array,
4172 const GValue * cdef_array)
4174 AtomData *atom_data;
4178 gint idhr_size = 22;
4179 gint colr_size = 15;
4180 gint cmap_size = 0, cdef_size = 0;
4181 gint cmap_array_size = 0;
4182 gint cdef_array_size = 0;
4183 GstByteWriter writer;
4185 g_return_val_if_fail (cmap_array == NULL ||
4186 GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
4187 g_return_val_if_fail (cdef_array == NULL ||
4188 GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
4190 if (g_str_equal (colorspace, "sRGB")) {
4194 } else if (g_str_equal (colorspace, "GRAY")) {
4198 } else if (g_str_equal (colorspace, "sYUV")) {
4206 cmap_array_size = gst_value_array_get_size (cmap_array);
4207 cmap_size = 8 + cmap_array_size * 4;
4210 cdef_array_size = gst_value_array_get_size (cdef_array);
4211 cdef_size = 8 + 2 + cdef_array_size * 6;
4214 gst_byte_writer_init_with_size (&writer,
4215 idhr_size + colr_size + cmap_size + cdef_size, TRUE);
4217 /* ihdr = image header box */
4218 gst_byte_writer_put_uint32_be (&writer, 22);
4219 gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
4220 gst_byte_writer_put_uint32_be (&writer, height);
4221 gst_byte_writer_put_uint32_be (&writer, width);
4222 gst_byte_writer_put_uint16_be (&writer, ncomp);
4223 /* 8 bits per component, unsigned */
4224 gst_byte_writer_put_uint8 (&writer, 0x7);
4225 /* compression type; reserved */
4226 gst_byte_writer_put_uint8 (&writer, 0x7);
4227 /* colour space (un)known */
4228 gst_byte_writer_put_uint8 (&writer, 0x0);
4229 /* intellectual property right (box present) */
4230 gst_byte_writer_put_uint8 (&writer, 0x0);
4232 /* colour specification box */
4233 gst_byte_writer_put_uint32_be (&writer, 15);
4234 gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
4236 /* specification method: enumerated */
4237 gst_byte_writer_put_uint8 (&writer, 0x1);
4238 /* precedence; reserved */
4239 gst_byte_writer_put_uint8 (&writer, 0x0);
4240 /* approximation; reserved */
4241 gst_byte_writer_put_uint8 (&writer, 0x0);
4242 /* enumerated colourspace */
4243 gst_byte_writer_put_uint32_be (&writer, cenum);
4246 gst_byte_writer_put_uint32_be (&writer, cmap_size);
4247 gst_byte_writer_put_uint32_le (&writer,
4248 GST_MAKE_FOURCC ('c', 'm', 'a', 'p'));
4249 for (i = 0; i < cmap_array_size; i++) {
4255 item = gst_value_array_get_value (cmap_array, i);
4256 value = g_value_get_int (item);
4258 /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
4259 cmp = value & 0xFFFF;
4261 pcol = (value >> 16) & 0xFF;
4264 GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
4265 "handle Pallete mapping atoms yet");
4267 gst_byte_writer_put_uint16_be (&writer, cmp);
4268 gst_byte_writer_put_uint8 (&writer, mtyp);
4269 gst_byte_writer_put_uint8 (&writer, pcol);
4274 gst_byte_writer_put_uint32_be (&writer, cdef_size);
4275 gst_byte_writer_put_uint32_le (&writer,
4276 GST_MAKE_FOURCC ('c', 'd', 'e', 'f'));
4277 gst_byte_writer_put_uint16_be (&writer, cdef_array_size);
4278 for (i = 0; i < cdef_array_size; i++) {
4281 item = gst_value_array_get_value (cdef_array, i);
4282 value = g_value_get_int (item);
4284 gst_byte_writer_put_uint16_be (&writer, i);
4286 gst_byte_writer_put_uint16_be (&writer, 0);
4287 gst_byte_writer_put_uint16_be (&writer, value);
4288 } else if (value < 0) {
4289 gst_byte_writer_put_uint16_be (&writer, -value);
4290 gst_byte_writer_put_uint16_be (&writer, 0); /* TODO what here? */
4292 gst_byte_writer_put_uint16_be (&writer, 1);
4293 gst_byte_writer_put_uint16_be (&writer, 0);
4298 g_assert (gst_byte_writer_get_remaining (&writer) == 0);
4299 buf = gst_byte_writer_reset_and_get_buffer (&writer);
4301 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
4302 gst_buffer_unref (buf);
4304 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4309 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
4312 AtomInfo *result = NULL;
4315 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
4316 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
4324 build_amr_extension (void)
4331 GST_WRITE_UINT32_LE (ext, 0);
4332 /* decoder version */
4333 GST_WRITE_UINT8 (ext + 4, 0);
4334 /* mode set (all modes) */
4335 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
4336 /* mode change period (no restriction) */
4337 GST_WRITE_UINT8 (ext + 7, 0);
4338 /* frames per sample */
4339 GST_WRITE_UINT8 (ext + 8, 1);
4341 buf = _gst_buffer_new_wrapped (ext, sizeof (ext), NULL);
4342 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', 'a', 'm', 'r'), buf);
4343 gst_buffer_unref (buf);
4348 build_h263_extension (void)
4355 GST_WRITE_UINT32_LE (ext, 0);
4356 /* decoder version */
4357 GST_WRITE_UINT8 (ext + 4, 0);
4358 /* level / profile */
4359 /* FIXME ? maybe ? obtain somewhere; baseline for now */
4360 GST_WRITE_UINT8 (ext + 5, 10);
4361 GST_WRITE_UINT8 (ext + 6, 0);
4363 buf = _gst_buffer_new_wrapped (ext, sizeof (ext), NULL);
4364 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', '2', '6', '3'), buf);
4365 gst_buffer_unref (buf);
4370 build_gama_atom (gdouble gamma)
4376 /* convert to uint32 from fixed point */
4377 gamma_fp = (guint32) 65536 *gamma;
4379 gamma_fp = GUINT32_TO_BE (gamma_fp);
4380 buf = _gst_buffer_new_wrapped (&gamma_fp, 4, NULL);
4381 res = build_codec_data_extension (FOURCC_gama, buf);
4382 gst_buffer_unref (buf);
4387 build_SMI_atom (const GstBuffer * seqh)
4394 /* the seqh plus its size and fourcc */
4395 size = gst_buffer_get_size ((GstBuffer *) seqh);
4396 data = g_malloc (size + 8);
4398 GST_WRITE_UINT32_LE (data, FOURCC_SEQH);
4399 GST_WRITE_UINT32_BE (data + 4, size + 8);
4400 gst_buffer_extract ((GstBuffer *) seqh, 0, data + 8, size);
4401 buf = _gst_buffer_new_wrapped (data, size + 8, g_free);
4402 res = build_codec_data_extension (FOURCC_SMI_, buf);
4403 gst_buffer_unref (buf);
4408 build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
4410 AtomData *atom_data;
4413 const gint ima_adpcm_atom_size = 20;
4415 gint samplesperblock;
4418 /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
4419 identifier. Note that the identifier here is big-endian, but when used
4420 within the WAVE header (below), it's little endian. */
4421 fourcc = MS_WAVE_FOURCC (0x11);
4423 data = g_malloc (ima_adpcm_atom_size);
4425 /* This atom's content is a WAVE header, including 2 bytes of extra data.
4426 Note that all of this is little-endian, unlike most stuff in qt. */
4427 /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
4428 for the rest. Simplifies to this. */
4429 samplesperblock = 2 * blocksize / channels - 7;
4430 bytespersec = rate * blocksize / samplesperblock;
4431 GST_WRITE_UINT16_LE (data, 0x11);
4432 GST_WRITE_UINT16_LE (data + 2, channels);
4433 GST_WRITE_UINT32_LE (data + 4, rate);
4434 GST_WRITE_UINT32_LE (data + 8, bytespersec);
4435 GST_WRITE_UINT16_LE (data + 12, blocksize);
4436 GST_WRITE_UINT16_LE (data + 14, 4);
4437 GST_WRITE_UINT16_LE (data + 16, 2); /* Two extra bytes */
4438 GST_WRITE_UINT16_LE (data + 18, samplesperblock);
4440 buf = _gst_buffer_new_wrapped (data, ima_adpcm_atom_size, g_free);
4441 atom_data = atom_data_new_from_gst_buffer (fourcc, buf);
4442 gst_buffer_unref (buf);
4444 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4449 build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
4456 wave = atom_wave_new ();
4458 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
4459 ext_atom = (Atom *) atom_data_new (FOURCC_null);
4460 wave->extension_atoms =
4461 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4462 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4464 /* Add wave ima adpcm atom to WAVE */
4465 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
4466 build_ima_adpcm_atom (channels, rate, blocksize));
4468 /* Add FRMA to the WAVE */
4469 frma = atom_frma_new ();
4470 frma->media_type = MS_WAVE_FOURCC (0x11);
4472 wave->extension_atoms =
4473 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4474 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4476 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4481 build_uuid_xmp_atom (GstBuffer * xmp_data)
4485 static guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
4486 0x97, 0xA9, 0x42, 0xE8,
4487 0x9C, 0x71, 0x99, 0x94,
4488 0x91, 0xE3, 0xAF, 0xAC
4491 if (xmp_data == NULL)
4494 uuid = atom_uuid_new ();
4495 memcpy (uuid->uuid, xmp_uuid, 16);
4497 size = gst_buffer_get_size (xmp_data);
4498 uuid->data = g_malloc (size);
4499 uuid->datalen = size;
4500 gst_buffer_extract (xmp_data, 0, uuid->data, size);
4502 return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,