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 * Meta tags functions
2750 atom_moov_init_metatags (AtomMOOV * moov, AtomsContext * context)
2753 moov->udta = atom_udta_new ();
2755 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
2756 if (!moov->udta->meta) {
2757 moov->udta->meta = atom_meta_new ();
2759 if (!moov->udta->meta->ilst) {
2760 moov->udta->meta->ilst = atom_ilst_new ();
2766 atom_tag_data_alloc_data (AtomTagData * data, guint size)
2768 if (data->data != NULL) {
2769 g_free (data->data);
2771 data->data = g_new0 (guint8, size);
2772 data->datalen = size;
2776 atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
2780 atom_moov_init_metatags (moov, &moov->context);
2781 if (moov->udta->meta)
2782 entries = &moov->udta->meta->ilst->entries;
2784 entries = &moov->udta->entries;
2785 *entries = g_list_append (*entries, tag);
2789 atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2790 const guint8 * data, guint size)
2795 tag = atom_tag_new (fourcc, flags);
2797 atom_tag_data_alloc_data (tdata, size);
2798 g_memmove (tdata->data, data, size);
2800 atom_moov_append_tag (moov,
2801 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
2806 atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2808 gint len = strlen (value);
2811 atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
2815 atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2818 guint8 data[8] = { 0, };
2821 GST_WRITE_UINT16_BE (data, value);
2822 atom_moov_add_tag (moov, fourcc, flags, data, 2);
2824 GST_WRITE_UINT32_BE (data + 2, value);
2825 atom_moov_add_tag (moov, fourcc, flags, data, 8);
2830 _gst_buffer_new_wrapped (gpointer mem, gsize size, GFreeFunc free_func)
2834 buf = gst_buffer_new ();
2835 gst_buffer_take_memory (buf, -1,
2836 gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
2837 mem, free_func, size, 0, size));
2843 atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
2845 AtomData *data_atom;
2853 /* blob is unparsed atom;
2854 * extract size and fourcc, and wrap remainder in data atom */
2855 len = GST_READ_UINT32_BE (data);
2856 fourcc = GST_READ_UINT32_LE (data + 4);
2860 buf = _gst_buffer_new_wrapped (data + 8, len - 8, NULL);
2861 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2862 gst_buffer_unref (buf);
2864 atom_moov_append_tag (moov,
2865 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2870 atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
2873 AtomData *data_atom;
2877 /* need full atom */
2878 bdata = g_malloc (size + 4);
2879 /* full atom: version and flags */
2880 GST_WRITE_UINT32_BE (bdata, 0);
2881 memcpy (bdata + 4, data, size);
2883 buf = _gst_buffer_new_wrapped (bdata, size + 4, g_free);
2884 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2885 gst_buffer_unref (buf);
2887 atom_moov_append_tag (moov,
2888 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2893 language_code (const char *lang)
2895 g_return_val_if_fail (lang != NULL, 0);
2896 g_return_val_if_fail (strlen (lang) == 3, 0);
2898 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
2899 ((lang[2] - 0x60) & 0x1F);
2903 atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc,
2904 const gchar * value, gint16 ivalue)
2906 gint len = 0, size = 0;
2910 len = strlen (value);
2917 data = g_malloc (size + 3);
2918 /* language tag and null-terminated UTF-8 string */
2920 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
2921 /* include 0 terminator */
2922 memcpy (data + 2, value, len + 1);
2924 /* 16-bit unsigned int if standalone, otherwise 8-bit */
2927 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
2929 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
2934 atom_moov_add_3gp_tag (moov, fourcc, data, size);
2939 atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2941 atom_moov_add_3gp_str_int_tag (moov, fourcc, value, -1);
2945 atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value)
2947 atom_moov_add_3gp_str_int_tag (moov, fourcc, NULL, value);
2951 atom_moov_add_xmp_tags (AtomMOOV * moov, GstBuffer * xmpbuffer)
2953 AtomData *data_atom = NULL;
2955 if (moov->context.flavor == ATOMS_TREE_FLAVOR_MOV) {
2957 data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
2958 atom_moov_init_metatags (moov, &moov->context);
2959 moov->udta->entries = g_list_append (moov->udta->entries,
2960 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2964 GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
2970 * Functions for specifying media types
2974 atom_minf_set_audio (AtomMINF * minf)
2976 atom_minf_clear_handlers (minf);
2977 minf->smhd = atom_smhd_new ();
2981 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
2983 atom_minf_clear_handlers (minf);
2984 minf->vmhd = atom_vmhd_new (context);
2988 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
2991 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2992 hdlr->component_type = comp_type;
2994 hdlr->handler_type = hdlr_type;
2998 atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
3001 g_free (hdlr->name);
3002 hdlr->name = g_strdup (name);
3006 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
3008 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3009 /* Some players (low-end hardware) check for this name, which is what
3010 * QuickTime itself sets */
3011 atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3015 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3017 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3018 /* Some players (low-end hardware) check for this name, which is what
3019 * QuickTime itself sets */
3020 atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3024 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3026 atom_mdia_set_hdlr_type_audio (mdia, context);
3027 atom_minf_set_audio (&mdia->minf);
3031 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3033 atom_mdia_set_hdlr_type_video (mdia, context);
3034 atom_minf_set_video (&mdia->minf, context);
3038 atom_tkhd_set_audio (AtomTKHD * tkhd)
3040 tkhd->volume = 0x0100;
3041 tkhd->width = tkhd->height = 0;
3045 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3050 /* qt and ISO base media do not contradict, and examples agree */
3051 tkhd->width = width;
3052 tkhd->height = height;
3056 atom_edts_add_entry (AtomEDTS * edts, EditListEntry * entry)
3058 edts->elst.entries = g_slist_append (edts->elst.entries, entry);
3062 * Adds a new entry to this trak edits list
3063 * duration is in the moov's timescale
3064 * media_time is the offset in the media time to start from (media's timescale)
3065 * rate is a 32 bits fixed-point
3068 atom_trak_add_elst_entry (AtomTRAK * trak, guint32 duration, guint32 media_time,
3071 EditListEntry *entry = g_new (EditListEntry, 1);
3073 entry->duration = duration;
3074 entry->media_time = media_time;
3075 entry->media_rate = rate;
3077 if (trak->edts == NULL) {
3078 trak->edts = atom_edts_new ();
3080 atom_edts_add_entry (trak->edts, entry);
3083 /* re-negotiation is prevented at top-level, so only 1 entry expected.
3084 * Quite some more care here and elsewhere may be needed to
3085 * support several entries */
3086 static SampleTableEntryMP4A *
3087 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
3090 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3091 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
3093 mp4a->se.header.type = type;
3094 mp4a->se.kind = AUDIO;
3095 mp4a->compression_id = -1;
3096 mp4a->se.data_reference_index = 1;
3098 stsd->entries = g_list_prepend (stsd->entries, mp4a);
3103 static SampleTableEntryMP4V *
3104 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
3107 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
3108 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3110 mp4v->se.header.type = type;
3111 mp4v->se.kind = VIDEO;
3112 mp4v->se.data_reference_index = 1;
3113 mp4v->horizontal_resolution = 72 << 16;
3114 mp4v->vertical_resolution = 72 << 16;
3115 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3116 mp4v->spatial_quality = 512;
3117 mp4v->temporal_quality = 512;
3120 stsd->entries = g_list_prepend (stsd->entries, mp4v);
3126 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
3128 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
3132 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
3134 atom_tkhd_set_audio (&trak->tkhd);
3135 atom_mdia_set_audio (&trak->mdia, context);
3139 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
3142 atom_tkhd_set_video (&trak->tkhd, context, width, height);
3143 atom_mdia_set_video (&trak->mdia, context);
3147 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
3150 atom_trak_set_audio (trak, context);
3151 trak->mdia.mdhd.time_info.timescale = rate;
3155 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
3156 guint32 rate, guint32 width, guint32 height)
3158 atom_trak_set_video (trak, context, width, height);
3159 trak->mdia.mdhd.time_info.timescale = rate;
3160 trak->tkhd.width = width << 16;
3161 trak->tkhd.height = height << 16;
3165 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
3166 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
3168 SampleTableEntryMP4A *ste;
3170 atom_trak_set_audio_commons (trak, context, scale);
3171 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3172 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
3174 trak->is_video = FALSE;
3175 trak->is_h264 = FALSE;
3177 ste->version = entry->version;
3178 ste->compression_id = entry->compression_id;
3179 ste->sample_size = entry->sample_size;
3180 ste->sample_rate = entry->sample_rate << 16;
3181 ste->channels = entry->channels;
3183 ste->samples_per_packet = entry->samples_per_packet;
3184 ste->bytes_per_sample = entry->bytes_per_sample;
3185 ste->bytes_per_packet = entry->bytes_per_packet;
3186 ste->bytes_per_frame = entry->bytes_per_frame;
3189 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
3191 /* 0 size means variable size */
3192 atom_trak_set_constant_size_samples (trak, sample_size);
3196 build_pasp_extension (AtomTRAK * trak, gint par_width, gint par_height)
3198 AtomData *atom_data;
3202 data = g_malloc (8);
3203 /* ihdr = image header box */
3204 GST_WRITE_UINT32_BE (data, par_width);
3205 GST_WRITE_UINT32_BE (data + 4, par_height);
3207 buf = _gst_buffer_new_wrapped (data, 8, g_free);
3208 atom_data = atom_data_new_from_gst_buffer (FOURCC_pasp, buf);
3209 gst_buffer_unref (buf);
3211 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3216 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
3217 VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
3219 SampleTableEntryMP4V *ste;
3220 gint dwidth, dheight;
3221 gint par_n = 0, par_d = 0;
3223 if ((entry->par_n != 1 || entry->par_d != 1) &&
3224 (entry->par_n != entry->par_d)) {
3225 par_n = entry->par_n;
3226 par_d = entry->par_d;
3229 dwidth = entry->width;
3230 dheight = entry->height;
3231 /* ISO file spec says track header w/h indicates track's visual presentation
3232 * (so this together with pixels w/h implicitly defines PAR) */
3233 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
3234 if (par_n > par_d) {
3235 dwidth = entry->width * par_n / par_d;
3236 dheight = entry->height;
3238 dwidth = entry->width * par_n / par_d;
3239 dheight = entry->height;
3243 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
3244 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3245 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
3247 trak->is_video = TRUE;
3248 trak->is_h264 = (entry->fourcc == FOURCC_avc1);
3250 ste->version = entry->version;
3251 ste->width = entry->width;
3252 ste->height = entry->height;
3253 ste->depth = entry->depth;
3254 ste->color_table_id = entry->color_table_id;
3255 ste->frame_count = entry->frame_count;
3258 ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
3260 /* QT spec has a pasp extension atom in stsd that can hold PAR */
3261 if (par_n && (context->flavor == ATOMS_TREE_FLAVOR_MOV)) {
3262 ste->extension_atoms = g_list_append (ste->extension_atoms,
3263 build_pasp_extension (trak, par_n, par_d));
3268 atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
3270 guint8 flags[3] = { 0, 0, 0 };
3272 atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
3273 mfhd->sequence_number = sequence_number;
3277 atom_moof_init (AtomMOOF * moof, AtomsContext * context,
3278 guint32 sequence_number)
3280 atom_header_set (&moof->header, FOURCC_moof, 0, 0);
3281 atom_mfhd_init (&moof->mfhd, sequence_number);
3286 atom_moof_new (AtomsContext * context, guint32 sequence_number)
3288 AtomMOOF *moof = g_new0 (AtomMOOF, 1);
3290 atom_moof_init (moof, context, sequence_number);
3295 atom_trun_free (AtomTRUN * trun)
3297 atom_full_clear (&trun->header);
3298 atom_array_clear (&trun->entries);
3303 atom_sdtp_free (AtomSDTP * sdtp)
3305 atom_full_clear (&sdtp->header);
3306 atom_array_clear (&sdtp->entries);
3311 atom_traf_free (AtomTRAF * traf)
3315 walker = traf->truns;
3317 atom_trun_free ((AtomTRUN *) walker->data);
3318 walker = g_list_next (walker);
3320 g_list_free (traf->truns);
3323 walker = traf->sdtps;
3325 atom_sdtp_free ((AtomSDTP *) walker->data);
3326 walker = g_list_next (walker);
3328 g_list_free (traf->sdtps);
3335 atom_moof_free (AtomMOOF * moof)
3339 walker = moof->trafs;
3341 atom_traf_free ((AtomTRAF *) walker->data);
3342 walker = g_list_next (walker);
3344 g_list_free (moof->trafs);
3351 atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
3354 guint64 original_offset = *offset;
3356 if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
3360 prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
3362 atom_write_size (buffer, size, offset, original_offset);
3363 return *offset - original_offset;
3367 atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
3370 guint64 original_offset = *offset;
3373 if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
3377 prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
3379 flags = atom_full_get_flags_as_uint (&tfhd->header);
3381 if (flags & TF_BASE_DATA_OFFSET)
3382 prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
3383 if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
3384 prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
3385 if (flags & TF_DEFAULT_SAMPLE_DURATION)
3386 prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
3387 if (flags & TF_DEFAULT_SAMPLE_SIZE)
3388 prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
3389 if (flags & TF_DEFAULT_SAMPLE_FLAGS)
3390 prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
3392 atom_write_size (buffer, size, offset, original_offset);
3393 return *offset - original_offset;
3397 atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
3398 guint64 * offset, guint32 * data_offset)
3400 guint64 original_offset = *offset;
3403 flags = atom_full_get_flags_as_uint (&trun->header);
3405 /* if first trun in moof, forcibly add data_offset and record
3406 * where it must be written later on */
3407 if (data_offset && !*data_offset) {
3408 flags |= TR_DATA_OFFSET;
3410 flags &= ~TR_DATA_OFFSET;
3413 atom_full_set_flags_as_uint (&trun->header, flags);
3415 if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
3419 prop_copy_uint32 (trun->sample_count, buffer, size, offset);
3421 if (flags & TR_DATA_OFFSET) {
3422 *data_offset = *offset;
3423 prop_copy_int32 (trun->data_offset, buffer, size, offset);
3425 if (flags & TR_FIRST_SAMPLE_FLAGS)
3426 prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
3428 for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
3429 TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
3431 if (flags & TR_SAMPLE_DURATION)
3432 prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
3433 if (flags & TR_SAMPLE_SIZE)
3434 prop_copy_uint32 (entry->sample_size, buffer, size, offset);
3435 if (flags & TR_SAMPLE_FLAGS)
3436 prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
3437 if (flags & TR_COMPOSITION_TIME_OFFSETS)
3438 prop_copy_uint32 (entry->sample_composition_time_offset,
3439 buffer, size, offset);
3442 atom_write_size (buffer, size, offset, original_offset);
3443 return *offset - original_offset;
3447 atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
3450 guint64 original_offset = *offset;
3452 if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
3456 /* all entries at once */
3457 prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
3458 atom_array_get_len (&sdtp->entries), buffer, size, offset);
3460 atom_write_size (buffer, size, offset, original_offset);
3461 return *offset - original_offset;
3465 atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
3466 guint64 * offset, guint32 * data_offset)
3468 guint64 original_offset = *offset;
3471 if (!atom_copy_data (&traf->header, buffer, size, offset)) {
3474 if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
3478 walker = g_list_first (traf->truns);
3479 while (walker != NULL) {
3480 if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset,
3484 walker = g_list_next (walker);
3487 walker = g_list_first (traf->sdtps);
3488 while (walker != NULL) {
3489 if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
3492 walker = g_list_next (walker);
3495 atom_write_size (buffer, size, offset, original_offset);
3496 return *offset - original_offset;
3499 /* creates moof atom; metadata is written expecting actual buffer data
3500 * is in mdata directly after moof, and is consecutively written per trak */
3502 atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
3503 guint64 * size, guint64 * offset)
3505 guint64 original_offset = *offset;
3507 guint32 data_offset = 0;
3509 if (!atom_copy_data (&moof->header, buffer, size, offset))
3512 if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
3515 walker = g_list_first (moof->trafs);
3516 while (walker != NULL) {
3517 if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset,
3521 walker = g_list_next (walker);
3524 atom_write_size (buffer, size, offset, original_offset);
3526 if (*buffer && data_offset) {
3527 /* first trun needs a data-offset relative to moof start
3528 * = moof size + mdat prefix */
3529 GST_WRITE_UINT32_BE (*buffer + data_offset, *offset - original_offset + 8);
3532 return *offset - original_offset;
3536 atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
3538 guint8 flags[3] = { 0, 0, 0 };
3540 atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
3541 tfhd->track_ID = track_ID;
3542 tfhd->base_data_offset = 0;
3543 tfhd->sample_description_index = 1;
3544 tfhd->default_sample_duration = 0;
3545 tfhd->default_sample_size = 0;
3546 tfhd->default_sample_flags = 0;
3550 atom_trun_init (AtomTRUN * trun)
3552 guint8 flags[3] = { 0, 0, 0 };
3554 atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
3555 trun->sample_count = 0;
3556 trun->data_offset = 0;
3557 trun->first_sample_flags = 0;
3558 atom_array_init (&trun->entries, 512);
3562 atom_trun_new (void)
3564 AtomTRUN *trun = g_new0 (AtomTRUN, 1);
3566 atom_trun_init (trun);
3571 atom_sdtp_init (AtomSDTP * sdtp)
3573 guint8 flags[3] = { 0, 0, 0 };
3575 atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
3576 atom_array_init (&sdtp->entries, 512);
3580 atom_sdtp_new (AtomsContext * context)
3582 AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
3584 atom_sdtp_init (sdtp);
3589 atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
3591 traf->sdtps = g_list_append (traf->sdtps, sdtp);
3595 atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
3597 /* it does not make much/any sense according to specs,
3598 * but that's how MS isml samples seem to do it */
3599 atom_array_append (&sdtp->entries, val, 256);
3603 atom_trun_add_samples (AtomTRUN * trun, guint32 delta, guint32 size,
3604 guint32 flags, gint64 pts_offset)
3606 TRUNSampleEntry nentry;
3608 if (pts_offset != 0)
3609 trun->header.flags[1] |= TR_COMPOSITION_TIME_OFFSETS;
3611 nentry.sample_duration = delta;
3612 nentry.sample_size = size;
3613 nentry.sample_flags = flags;
3614 nentry.sample_composition_time_offset = pts_offset;
3615 atom_array_append (&trun->entries, nentry, 256);
3616 trun->sample_count++;
3620 atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
3622 atom_header_set (&traf->header, FOURCC_traf, 0, 0);
3623 atom_tfhd_init (&traf->tfhd, track_ID);
3626 if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
3627 atom_traf_add_sdtp (traf, atom_sdtp_new (context));
3631 atom_traf_new (AtomsContext * context, guint32 track_ID)
3633 AtomTRAF *traf = g_new0 (AtomTRAF, 1);
3635 atom_traf_init (traf, context, track_ID);
3640 atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
3642 traf->truns = g_list_append (traf->truns, trun);
3646 atom_traf_add_samples (AtomTRAF * traf, guint32 delta, guint32 size,
3647 gboolean sync, gint64 pts_offset, gboolean sdtp_sync)
3652 /* 0x10000 is sample-is-difference-sample flag
3653 * low byte stuff is what ismv uses */
3654 flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
3656 if (G_UNLIKELY (!traf->truns)) {
3657 trun = atom_trun_new ();
3658 atom_traf_add_trun (traf, trun);
3659 /* optimistic; indicate all defaults present in tfhd */
3660 traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
3661 TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
3662 traf->tfhd.default_sample_duration = delta;
3663 traf->tfhd.default_sample_size = size;
3664 traf->tfhd.default_sample_flags = flags;
3665 trun->first_sample_flags = flags;
3668 trun = traf->truns->data;
3670 /* check if still matching defaults,
3671 * if not, abandon default and need entry for each sample */
3672 if (traf->tfhd.default_sample_duration != delta) {
3673 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
3674 trun->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
3676 if (traf->tfhd.default_sample_size != size) {
3677 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
3678 trun->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
3680 if (traf->tfhd.default_sample_flags != flags) {
3681 if (trun->sample_count == 1) {
3682 /* at least will need first sample flag */
3683 traf->tfhd.default_sample_flags = flags;
3684 trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
3686 /* now we need sample flags for each sample */
3687 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
3688 trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
3689 trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
3693 atom_trun_add_samples (traf->truns->data, delta, size, flags, pts_offset);
3696 atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
3700 atom_traf_get_sample_num (AtomTRAF * traf)
3704 if (G_UNLIKELY (!traf->truns))
3707 trun = traf->truns->data;
3708 return atom_array_get_len (&trun->entries);
3712 atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
3714 moof->trafs = g_list_append (moof->trafs, traf);
3718 atom_tfra_free (AtomTFRA * tfra)
3720 atom_full_clear (&tfra->header);
3721 atom_array_clear (&tfra->entries);
3726 atom_mfra_new (AtomsContext * context)
3728 AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
3730 atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
3735 atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
3737 mfra->tfras = g_list_append (mfra->tfras, tfra);
3741 atom_mfra_free (AtomMFRA * mfra)
3745 walker = mfra->tfras;
3747 atom_tfra_free ((AtomTFRA *) walker->data);
3748 walker = g_list_next (walker);
3750 g_list_free (mfra->tfras);
3753 atom_clear (&mfra->header);
3758 atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
3760 guint8 flags[3] = { 0, 0, 0 };
3762 atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
3763 tfra->track_ID = track_ID;
3764 atom_array_init (&tfra->entries, 512);
3768 atom_tfra_new (AtomsContext * context, guint32 track_ID)
3770 AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
3772 atom_tfra_init (tfra, track_ID);
3778 need_bytes (guint32 num)
3789 atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
3795 entry.moof_offset = 0;
3796 /* always write a single trun in a single traf */
3797 entry.traf_number = 1;
3798 entry.trun_number = 1;
3799 entry.sample_number = sample_num;
3801 /* auto-use 64 bits if needed */
3802 if (dts > G_MAXUINT32)
3803 tfra->header.version = 1;
3805 /* 1 byte will always do for traf and trun number,
3806 * check how much sample_num needs */
3807 tfra->lengths = (tfra->lengths & 0xfc) ||
3808 MAX (tfra->lengths, need_bytes (sample_num));
3810 atom_array_append (&tfra->entries, entry, 256);
3814 atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
3818 /* auto-use 64 bits if needed */
3819 if (offset > G_MAXUINT32)
3820 tfra->header.version = 1;
3822 for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
3823 TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
3825 if (entry->moof_offset)
3827 entry->moof_offset = offset;
3832 atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
3835 guint64 original_offset = *offset;
3842 if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
3846 prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
3847 prop_copy_uint32 (tfra->lengths, buffer, size, offset);
3848 prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
3850 version = tfra->header.version;
3851 for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
3852 entry = &atom_array_index (&tfra->entries, i);
3854 prop_copy_uint64 (entry->time, buffer, size, offset);
3855 prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
3857 prop_copy_uint32 (entry->time, buffer, size, offset);
3858 prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
3861 bytes = (tfra->lengths & (0x3 << 4)) + 1;
3862 data = GUINT32_TO_BE (entry->traf_number);
3863 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3864 buffer, size, offset);
3866 bytes = (tfra->lengths & (0x3 << 2)) + 1;
3867 data = GUINT32_TO_BE (entry->trun_number);
3868 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3869 buffer, size, offset);
3871 bytes = (tfra->lengths & (0x3)) + 1;
3872 data = GUINT32_TO_BE (entry->sample_number);
3873 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3874 buffer, size, offset);
3878 atom_write_size (buffer, size, offset, original_offset);
3879 return *offset - original_offset;
3883 atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
3886 guint64 original_offset = *offset;
3887 guint8 flags[3] = { 0, 0, 0 };
3890 atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
3892 if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
3896 prop_copy_uint32 (s, buffer, size, offset);
3898 atom_write_size (buffer, size, offset, original_offset);
3900 return *offset - original_offset;
3905 atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
3908 guint64 original_offset = *offset;
3911 if (!atom_copy_data (&mfra->header, buffer, size, offset))
3914 walker = g_list_first (mfra->tfras);
3915 while (walker != NULL) {
3916 if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
3919 walker = g_list_next (walker);
3922 /* 16 is the size of the mfro atom */
3923 if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
3927 atom_write_size (buffer, size, offset, original_offset);
3928 return *offset - original_offset;
3931 /* some sample description construction helpers */
3934 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
3935 const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
3940 track_id = trak->tkhd.track_ID;
3942 esds = atom_esds_new ();
3943 esds->es.id = track_id & 0xFFFF;
3944 esds->es.dec_conf_desc.object_type = object_type;
3945 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
3947 if (avg_bitrate > 0)
3948 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
3949 if (max_bitrate > 0)
3950 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
3952 /* optional DecoderSpecificInfo */
3954 DecoderSpecificInfoDescriptor *desc;
3957 esds->es.dec_conf_desc.dec_specific_info = desc =
3958 desc_dec_specific_info_new ();
3959 size = gst_buffer_get_size ((GstBuffer *) codec_data);
3960 desc_dec_specific_info_alloc_data (desc, size);
3961 gst_buffer_extract ((GstBuffer *) codec_data, 0, desc->data, size);
3964 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
3969 build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
3970 guint32 max_bitrate)
3972 AtomData *atom_data;
3976 if (buffer_size_db == 0 && avg_bitrate == 0 && max_bitrate == 0)
3979 data = g_malloc (12);
3980 GST_WRITE_UINT32_BE (data, buffer_size_db);
3981 GST_WRITE_UINT32_BE (data + 4, max_bitrate);
3982 GST_WRITE_UINT32_BE (data + 8, avg_bitrate);
3984 buf = _gst_buffer_new_wrapped (data, 12, g_free);
3986 atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('b', 't', 'r', 't'), buf);
3987 gst_buffer_unref (buf);
3989 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3994 build_mov_wave_extension (AtomTRAK * trak, guint32 fourcc, AtomInfo * atom1,
3995 AtomInfo * atom2, gboolean terminator)
4001 /* Build WAVE atom for sample table entry */
4002 wave = atom_wave_new ();
4004 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
4006 ext_atom = (Atom *) atom_data_new (FOURCC_null);
4007 wave->extension_atoms =
4008 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4009 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4012 /* Add supplied atoms to WAVE */
4014 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
4016 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
4018 /* Add FRMA to the WAVE */
4019 frma = atom_frma_new ();
4020 frma->media_type = fourcc;
4022 wave->extension_atoms =
4023 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4024 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4026 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4031 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
4032 guint32 avg_bitrate, guint32 max_bitrate)
4034 AtomInfo *esds, *mp4a;
4038 /* Add ESDS atom to WAVE */
4039 esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
4040 ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
4042 /* Add MP4A atom to the WAVE:
4043 * not really in spec, but makes offset based players happy */
4044 buf = _gst_buffer_new_wrapped (&tmp, 4, NULL);
4045 mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
4046 gst_buffer_unref (buf);
4048 return build_mov_wave_extension (trak, FOURCC_mp4a, mp4a, esds, TRUE);
4052 build_mov_alac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
4056 alac = build_codec_data_extension (FOURCC_alac, codec_data);
4058 return build_mov_wave_extension (trak, FOURCC_alac, NULL, alac, TRUE);
4062 build_fiel_extension (gint fields)
4064 AtomData *atom_data;
4072 buf = _gst_buffer_new_wrapped (&f, 1, NULL);
4074 atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('f', 'i', 'e', 'l'), buf);
4075 gst_buffer_unref (buf);
4077 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4082 build_jp2x_extension (const GstBuffer * prefix)
4084 AtomData *atom_data;
4091 atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('j', 'p', '2', 'x'),
4094 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4099 build_jp2h_extension (AtomTRAK * trak, gint width, gint height,
4100 const gchar * colorspace, gint ncomp, const GValue * cmap_array,
4101 const GValue * cdef_array)
4103 AtomData *atom_data;
4107 gint idhr_size = 22;
4108 gint colr_size = 15;
4109 gint cmap_size = 0, cdef_size = 0;
4110 gint cmap_array_size = 0;
4111 gint cdef_array_size = 0;
4112 GstByteWriter writer;
4114 g_return_val_if_fail (cmap_array == NULL ||
4115 GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
4116 g_return_val_if_fail (cdef_array == NULL ||
4117 GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
4119 if (g_str_equal (colorspace, "sRGB")) {
4123 } else if (g_str_equal (colorspace, "GRAY")) {
4127 } else if (g_str_equal (colorspace, "sYUV")) {
4135 cmap_array_size = gst_value_array_get_size (cmap_array);
4136 cmap_size = 8 + cmap_array_size * 4;
4139 cdef_array_size = gst_value_array_get_size (cdef_array);
4140 cdef_size = 8 + 2 + cdef_array_size * 6;
4143 gst_byte_writer_init_with_size (&writer,
4144 idhr_size + colr_size + cmap_size + cdef_size, TRUE);
4146 /* ihdr = image header box */
4147 gst_byte_writer_put_uint32_be (&writer, 22);
4148 gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
4149 gst_byte_writer_put_uint32_be (&writer, height);
4150 gst_byte_writer_put_uint32_be (&writer, width);
4151 gst_byte_writer_put_uint16_be (&writer, ncomp);
4152 /* 8 bits per component, unsigned */
4153 gst_byte_writer_put_uint8 (&writer, 0x7);
4154 /* compression type; reserved */
4155 gst_byte_writer_put_uint8 (&writer, 0x7);
4156 /* colour space (un)known */
4157 gst_byte_writer_put_uint8 (&writer, 0x0);
4158 /* intellectual property right (box present) */
4159 gst_byte_writer_put_uint8 (&writer, 0x0);
4161 /* colour specification box */
4162 gst_byte_writer_put_uint32_be (&writer, 15);
4163 gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
4165 /* specification method: enumerated */
4166 gst_byte_writer_put_uint8 (&writer, 0x1);
4167 /* precedence; reserved */
4168 gst_byte_writer_put_uint8 (&writer, 0x0);
4169 /* approximation; reserved */
4170 gst_byte_writer_put_uint8 (&writer, 0x0);
4171 /* enumerated colourspace */
4172 gst_byte_writer_put_uint32_be (&writer, cenum);
4175 gst_byte_writer_put_uint32_be (&writer, cmap_size);
4176 gst_byte_writer_put_uint32_le (&writer,
4177 GST_MAKE_FOURCC ('c', 'm', 'a', 'p'));
4178 for (i = 0; i < cmap_array_size; i++) {
4184 item = gst_value_array_get_value (cmap_array, i);
4185 value = g_value_get_int (item);
4187 /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
4188 cmp = value & 0xFFFF;
4190 pcol = (value >> 16) & 0xFF;
4193 GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
4194 "handle Pallete mapping atoms yet");
4196 gst_byte_writer_put_uint16_be (&writer, cmp);
4197 gst_byte_writer_put_uint8 (&writer, mtyp);
4198 gst_byte_writer_put_uint8 (&writer, pcol);
4203 gst_byte_writer_put_uint32_be (&writer, cdef_size);
4204 gst_byte_writer_put_uint32_le (&writer,
4205 GST_MAKE_FOURCC ('c', 'd', 'e', 'f'));
4206 gst_byte_writer_put_uint16_be (&writer, cdef_array_size);
4207 for (i = 0; i < cdef_array_size; i++) {
4210 item = gst_value_array_get_value (cdef_array, i);
4211 value = g_value_get_int (item);
4213 gst_byte_writer_put_uint16_be (&writer, i);
4215 gst_byte_writer_put_uint16_be (&writer, 0);
4216 gst_byte_writer_put_uint16_be (&writer, value);
4217 } else if (value < 0) {
4218 gst_byte_writer_put_uint16_be (&writer, -value);
4219 gst_byte_writer_put_uint16_be (&writer, 0); /* TODO what here? */
4221 gst_byte_writer_put_uint16_be (&writer, 1);
4222 gst_byte_writer_put_uint16_be (&writer, 0);
4227 g_assert (gst_byte_writer_get_remaining (&writer) == 0);
4228 buf = gst_byte_writer_reset_and_get_buffer (&writer);
4230 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
4231 gst_buffer_unref (buf);
4233 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4238 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
4241 AtomInfo *result = NULL;
4244 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
4245 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
4253 build_amr_extension (void)
4260 GST_WRITE_UINT32_LE (ext, 0);
4261 /* decoder version */
4262 GST_WRITE_UINT8 (ext + 4, 0);
4263 /* mode set (all modes) */
4264 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
4265 /* mode change period (no restriction) */
4266 GST_WRITE_UINT8 (ext + 7, 0);
4267 /* frames per sample */
4268 GST_WRITE_UINT8 (ext + 8, 1);
4270 buf = _gst_buffer_new_wrapped (ext, sizeof (ext), NULL);
4271 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', 'a', 'm', 'r'), buf);
4272 gst_buffer_unref (buf);
4277 build_h263_extension (void)
4284 GST_WRITE_UINT32_LE (ext, 0);
4285 /* decoder version */
4286 GST_WRITE_UINT8 (ext + 4, 0);
4287 /* level / profile */
4288 /* FIXME ? maybe ? obtain somewhere; baseline for now */
4289 GST_WRITE_UINT8 (ext + 5, 10);
4290 GST_WRITE_UINT8 (ext + 6, 0);
4292 buf = _gst_buffer_new_wrapped (ext, sizeof (ext), NULL);
4293 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', '2', '6', '3'), buf);
4294 gst_buffer_unref (buf);
4299 build_gama_atom (gdouble gamma)
4305 /* convert to uint32 from fixed point */
4306 gamma_fp = (guint32) 65536 *gamma;
4308 gamma_fp = GUINT32_TO_BE (gamma_fp);
4309 buf = _gst_buffer_new_wrapped (&gamma_fp, 4, NULL);
4310 res = build_codec_data_extension (FOURCC_gama, buf);
4311 gst_buffer_unref (buf);
4316 build_SMI_atom (const GstBuffer * seqh)
4323 /* the seqh plus its size and fourcc */
4324 size = gst_buffer_get_size ((GstBuffer *) seqh);
4325 data = g_malloc (size + 8);
4327 GST_WRITE_UINT32_LE (data, FOURCC_SEQH);
4328 GST_WRITE_UINT32_BE (data + 4, size + 8);
4329 gst_buffer_extract ((GstBuffer *) seqh, 0, data + 8, size);
4330 buf = _gst_buffer_new_wrapped (data, size + 8, g_free);
4331 res = build_codec_data_extension (FOURCC_SMI_, buf);
4332 gst_buffer_unref (buf);
4337 build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
4339 AtomData *atom_data;
4342 const gint ima_adpcm_atom_size = 20;
4344 gint samplesperblock;
4347 /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
4348 identifier. Note that the identifier here is big-endian, but when used
4349 within the WAVE header (below), it's little endian. */
4350 fourcc = MS_WAVE_FOURCC (0x11);
4352 data = g_malloc (ima_adpcm_atom_size);
4354 /* This atom's content is a WAVE header, including 2 bytes of extra data.
4355 Note that all of this is little-endian, unlike most stuff in qt. */
4356 /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
4357 for the rest. Simplifies to this. */
4358 samplesperblock = 2 * blocksize / channels - 7;
4359 bytespersec = rate * blocksize / samplesperblock;
4360 GST_WRITE_UINT16_LE (data, 0x11);
4361 GST_WRITE_UINT16_LE (data + 2, channels);
4362 GST_WRITE_UINT32_LE (data + 4, rate);
4363 GST_WRITE_UINT32_LE (data + 8, bytespersec);
4364 GST_WRITE_UINT16_LE (data + 12, blocksize);
4365 GST_WRITE_UINT16_LE (data + 14, 4);
4366 GST_WRITE_UINT16_LE (data + 16, 2); /* Two extra bytes */
4367 GST_WRITE_UINT16_LE (data + 18, samplesperblock);
4369 buf = _gst_buffer_new_wrapped (data, ima_adpcm_atom_size, g_free);
4370 atom_data = atom_data_new_from_gst_buffer (fourcc, buf);
4371 gst_buffer_unref (buf);
4373 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4378 build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
4385 wave = atom_wave_new ();
4387 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
4388 ext_atom = (Atom *) atom_data_new (FOURCC_null);
4389 wave->extension_atoms =
4390 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4391 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4393 /* Add wave ima adpcm atom to WAVE */
4394 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
4395 build_ima_adpcm_atom (channels, rate, blocksize));
4397 /* Add FRMA to the WAVE */
4398 frma = atom_frma_new ();
4399 frma->media_type = MS_WAVE_FOURCC (0x11);
4401 wave->extension_atoms =
4402 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4403 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4405 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4410 build_uuid_xmp_atom (GstBuffer * xmp_data)
4414 static guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
4415 0x97, 0xA9, 0x42, 0xE8,
4416 0x9C, 0x71, 0x99, 0x94,
4417 0x91, 0xE3, 0xAF, 0xAC
4420 if (xmp_data == NULL)
4423 uuid = atom_uuid_new ();
4424 memcpy (uuid->uuid, xmp_uuid, 16);
4426 size = gst_buffer_get_size (xmp_data);
4427 uuid->data = g_malloc (size);
4428 uuid->datalen = size;
4429 gst_buffer_extract (xmp_data, 0, uuid->data, size);
4431 return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,