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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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>
51 #include <gst/video/video.h>
54 * Creates a new AtomsContext for the given flavor.
57 atoms_context_new (AtomsTreeFlavor flavor)
59 AtomsContext *context = g_new0 (AtomsContext, 1);
60 context->flavor = flavor;
65 * Frees an AtomsContext and all memory associated with it
68 atoms_context_free (AtomsContext * context)
73 /* -- creation, initialization, clear and free functions -- */
75 #define SECS_PER_DAY (24 * 60 * 60)
76 #define LEAP_YEARS_FROM_1904_TO_1970 17
79 atoms_get_current_qt_time (void)
83 g_get_current_time (&timeval);
84 /* FIXME this should use UTC coordinated time */
85 return timeval.tv_sec + (((1970 - 1904) * (guint64) 365) +
86 LEAP_YEARS_FROM_1904_TO_1970) * SECS_PER_DAY;
90 common_time_info_init (TimeInfo * ti)
92 ti->creation_time = ti->modification_time = atoms_get_current_qt_time ();
98 atom_header_set (Atom * header, guint32 fourcc, gint32 size, gint64 ext_size)
100 header->type = fourcc;
102 header->extended_size = ext_size;
106 atom_clear (Atom * atom)
111 atom_full_init (AtomFull * full, guint32 fourcc, gint32 size, gint64 ext_size,
112 guint8 version, guint8 flags[3])
114 atom_header_set (&(full->header), fourcc, size, ext_size);
115 full->version = version;
116 full->flags[0] = flags[0];
117 full->flags[1] = flags[1];
118 full->flags[2] = flags[2];
122 atom_full_clear (AtomFull * full)
124 atom_clear (&full->header);
128 atom_full_free (AtomFull * full)
130 atom_full_clear (full);
135 atom_full_get_flags_as_uint (AtomFull * full)
137 return full->flags[0] << 16 | full->flags[1] << 8 | full->flags[2];
141 atom_full_set_flags_as_uint (AtomFull * full, guint32 flags_as_uint)
143 full->flags[2] = flags_as_uint & 0xFF;
144 full->flags[1] = (flags_as_uint & 0xFF00) >> 8;
145 full->flags[0] = (flags_as_uint & 0xFF0000) >> 16;
149 build_atom_info_wrapper (Atom * atom, gpointer copy_func, gpointer free_func)
151 AtomInfo *info = NULL;
154 info = g_new0 (AtomInfo, 1);
157 info->copy_data_func = copy_func;
158 info->free_func = free_func;
165 atom_info_list_prepend_atom (GList * ai, Atom * atom,
166 AtomCopyDataFunc copy_func, AtomFreeFunc free_func)
169 return g_list_prepend (ai,
170 build_atom_info_wrapper (atom, copy_func, free_func));
176 atom_info_list_free (GList * ai)
179 AtomInfo *info = (AtomInfo *) ai->data;
181 info->free_func (info->atom);
183 ai = g_list_delete_link (ai, ai);
188 atom_data_new (guint32 fourcc)
190 AtomData *data = g_new0 (AtomData, 1);
192 atom_header_set (&data->header, fourcc, 0, 0);
197 atom_data_alloc_mem (AtomData * data, guint32 size)
200 data->data = g_new0 (guint8, size);
201 data->datalen = size;
205 atom_data_new_from_data (guint32 fourcc, const guint8 * mem, gsize size)
207 AtomData *data = atom_data_new (fourcc);
209 atom_data_alloc_mem (data, size);
210 memcpy (data->data, mem, size);
215 atom_data_new_from_gst_buffer (guint32 fourcc, const GstBuffer * buf)
217 AtomData *data = atom_data_new (fourcc);
218 gsize size = gst_buffer_get_size ((GstBuffer *) buf);
220 atom_data_alloc_mem (data, size);
221 gst_buffer_extract ((GstBuffer *) buf, 0, data->data, size);
226 atom_data_free (AtomData * data)
228 atom_clear (&data->header);
236 AtomUUID *uuid = g_new0 (AtomUUID, 1);
238 atom_header_set (&uuid->header, FOURCC_uuid, 0, 0);
243 atom_uuid_free (AtomUUID * data)
245 atom_clear (&data->header);
251 atom_ftyp_init (AtomFTYP * ftyp, guint32 major, guint32 version, GList * brands)
256 atom_header_set (&ftyp->header, FOURCC_ftyp, 16, 0);
257 ftyp->major_brand = major;
258 ftyp->version = version;
260 /* always include major brand as compatible brand */
261 ftyp->compatible_brands_size = g_list_length (brands) + 1;
262 ftyp->compatible_brands = g_new (guint32, ftyp->compatible_brands_size);
264 ftyp->compatible_brands[0] = major;
266 for (it = brands; it != NULL; it = g_list_next (it)) {
267 ftyp->compatible_brands[index++] = GPOINTER_TO_UINT (it->data);
272 atom_ftyp_new (AtomsContext * context, guint32 major, guint32 version,
275 AtomFTYP *ftyp = g_new0 (AtomFTYP, 1);
277 atom_ftyp_init (ftyp, major, version, brands);
282 atom_ftyp_free (AtomFTYP * ftyp)
284 atom_clear (&ftyp->header);
285 g_free (ftyp->compatible_brands);
286 ftyp->compatible_brands = NULL;
291 atom_esds_init (AtomESDS * esds)
293 guint8 flags[3] = { 0, 0, 0 };
295 atom_full_init (&esds->header, FOURCC_esds, 0, 0, 0, flags);
296 desc_es_init (&esds->es);
302 AtomESDS *esds = g_new0 (AtomESDS, 1);
304 atom_esds_init (esds);
309 atom_esds_free (AtomESDS * esds)
311 atom_full_clear (&esds->header);
312 desc_es_descriptor_clear (&esds->es);
319 AtomFRMA *frma = g_new0 (AtomFRMA, 1);
321 atom_header_set (&frma->header, FOURCC_frma, 0, 0);
326 atom_frma_free (AtomFRMA * frma)
328 atom_clear (&frma->header);
335 AtomWAVE *wave = g_new0 (AtomWAVE, 1);
337 atom_header_set (&wave->header, FOURCC_wave, 0, 0);
342 atom_wave_free (AtomWAVE * wave)
344 atom_clear (&wave->header);
345 atom_info_list_free (wave->extension_atoms);
350 atom_elst_init (AtomELST * elst)
352 guint8 flags[3] = { 0, 0, 0 };
353 atom_full_init (&elst->header, FOURCC_elst, 0, 0, 0, flags);
358 atom_elst_clear (AtomELST * elst)
362 atom_full_clear (&elst->header);
363 walker = elst->entries;
365 g_free ((EditListEntry *) walker->data);
366 walker = g_slist_next (walker);
368 g_slist_free (elst->entries);
372 atom_edts_init (AtomEDTS * edts)
374 atom_header_set (&edts->header, FOURCC_edts, 0, 0);
375 atom_elst_init (&edts->elst);
379 atom_edts_clear (AtomEDTS * edts)
381 atom_clear (&edts->header);
382 atom_elst_clear (&edts->elst);
388 AtomEDTS *edts = g_new0 (AtomEDTS, 1);
389 atom_edts_init (edts);
394 atom_edts_free (AtomEDTS * edts)
396 atom_edts_clear (edts);
401 atom_tcmi_init (AtomTCMI * tcmi)
403 guint8 flags[3] = { 0, 0, 0 };
405 atom_full_init (&tcmi->header, FOURCC_tcmi, 0, 0, 0, flags);
409 atom_tcmi_clear (AtomTCMI * tcmi)
411 atom_full_clear (&tcmi->header);
415 tcmi->text_color[0] = 0;
416 tcmi->text_color[1] = 0;
417 tcmi->text_color[2] = 0;
418 tcmi->bg_color[0] = 0;
419 tcmi->bg_color[1] = 0;
420 tcmi->bg_color[2] = 0;
421 g_free (tcmi->font_name);
422 tcmi->font_name = NULL;
426 atom_tmcd_init (AtomTMCD * tmcd)
428 atom_header_set (&tmcd->header, FOURCC_tmcd, 0, 0);
429 atom_tcmi_init (&tmcd->tcmi);
433 atom_tmcd_clear (AtomTMCD * tmcd)
435 atom_clear (&tmcd->header);
436 atom_tcmi_clear (&tmcd->tcmi);
440 atom_gmin_init (AtomGMIN * gmin)
442 guint8 flags[3] = { 0, 0, 0 };
444 atom_full_init (&gmin->header, FOURCC_gmin, 0, 0, 0, flags);
448 atom_gmin_clear (AtomGMIN * gmin)
450 atom_full_clear (&gmin->header);
451 gmin->graphics_mode = 0;
452 gmin->opcolor[0] = 0;
453 gmin->opcolor[1] = 0;
454 gmin->opcolor[2] = 0;
460 atom_gmhd_init (AtomGMHD * gmhd)
462 atom_header_set (&gmhd->header, FOURCC_gmhd, 0, 0);
463 atom_gmin_init (&gmhd->gmin);
464 atom_tmcd_init (&gmhd->tmcd);
468 atom_gmhd_clear (AtomGMHD * gmhd)
470 atom_clear (&gmhd->header);
471 atom_gmin_clear (&gmhd->gmin);
472 atom_tmcd_clear (&gmhd->tmcd);
478 AtomGMHD *gmhd = g_new0 (AtomGMHD, 1);
479 atom_gmhd_init (gmhd);
484 atom_gmhd_free (AtomGMHD * gmhd)
486 atom_gmhd_clear (gmhd);
491 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
493 atom_header_set (&se->header, type, 0, 0);
495 memset (se->reserved, 0, sizeof (guint8) * 6);
496 se->data_reference_index = 0;
500 atom_sample_entry_free (SampleTableEntry * se)
502 atom_clear (&se->header);
506 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
508 atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
511 mp4a->revision_level = 0;
514 mp4a->sample_size = 16;
515 mp4a->compression_id = 0;
516 mp4a->packet_size = 0;
517 mp4a->sample_rate = 0;
518 /* following only used if version is 1 */
519 mp4a->samples_per_packet = 0;
520 mp4a->bytes_per_packet = 0;
521 mp4a->bytes_per_frame = 0;
522 mp4a->bytes_per_sample = 0;
524 mp4a->extension_atoms = NULL;
527 static SampleTableEntryMP4A *
528 sample_entry_mp4a_new (void)
530 SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
532 sample_entry_mp4a_init (mp4a);
537 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
539 atom_sample_entry_free (&mp4a->se);
540 atom_info_list_free (mp4a->extension_atoms);
545 sample_entry_tmcd_init (SampleTableEntryTMCD * tmcd)
547 atom_sample_entry_init (&tmcd->se, FOURCC_tmcd);
551 tmcd->frame_duration = 0;
554 tmcd->name.language_code = 0;
555 g_free (tmcd->name.name);
556 tmcd->name.name = NULL;
559 static SampleTableEntryTMCD *
560 sample_entry_tmcd_new (void)
562 SampleTableEntryTMCD *tmcd = g_new0 (SampleTableEntryTMCD, 1);
564 sample_entry_tmcd_init (tmcd);
569 sample_entry_tmcd_free (SampleTableEntryTMCD * tmcd)
571 atom_sample_entry_free (&tmcd->se);
572 g_free (tmcd->name.name);
577 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
579 atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
582 mp4v->revision_level = 0;
585 mp4v->temporal_quality = 0;
586 mp4v->spatial_quality = 0;
588 /* qt and ISO base media do not contradict, and examples agree */
589 mp4v->horizontal_resolution = 0x00480000;
590 mp4v->vertical_resolution = 0x00480000;
593 mp4v->frame_count = 1;
595 memset (mp4v->compressor, 0, sizeof (guint8) * 32);
598 mp4v->color_table_id = 0;
600 mp4v->extension_atoms = NULL;
604 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
606 atom_sample_entry_free (&mp4v->se);
607 atom_info_list_free (mp4v->extension_atoms);
611 static SampleTableEntryMP4V *
612 sample_entry_mp4v_new (AtomsContext * context)
614 SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
616 sample_entry_mp4v_init (mp4v, context);
621 sample_entry_tx3g_init (SampleTableEntryTX3G * tx3g)
623 atom_sample_entry_init (&tx3g->se, FOURCC_tx3g);
625 tx3g->display_flags = 0;
626 tx3g->font_id = 1; /* must be 1 as there is a single font */
628 tx3g->foreground_color_rgba = 0xFFFFFFFF; /* white, opaque */
630 /* can't set this now */
631 tx3g->default_text_box = 0;
636 sample_entry_tx3g_free (SampleTableEntryTX3G * tx3g)
638 atom_sample_entry_free (&tx3g->se);
642 static SampleTableEntryTX3G *
643 sample_entry_tx3g_new (void)
645 SampleTableEntryTX3G *tx3g = g_new0 (SampleTableEntryTX3G, 1);
647 sample_entry_tx3g_init (tx3g);
653 atom_stsd_init (AtomSTSD * stsd)
655 guint8 flags[3] = { 0, 0, 0 };
657 atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
658 stsd->entries = NULL;
663 atom_stsd_remove_entries (AtomSTSD * stsd)
667 walker = stsd->entries;
670 SampleTableEntry *se = (SampleTableEntry *) aux->data;
672 walker = g_list_next (walker);
673 stsd->entries = g_list_remove_link (stsd->entries, aux);
677 sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
680 sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
683 sample_entry_tx3g_free ((SampleTableEntryTX3G *) se);
686 sample_entry_tmcd_free ((SampleTableEntryTMCD *) se);
689 /* best possible cleanup */
690 atom_sample_entry_free (se);
698 atom_stsd_clear (AtomSTSD * stsd)
700 atom_stsd_remove_entries (stsd);
701 atom_full_clear (&stsd->header);
705 atom_ctts_init (AtomCTTS * ctts)
707 guint8 flags[3] = { 0, 0, 0 };
709 atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
710 atom_array_init (&ctts->entries, 128);
711 ctts->do_pts = FALSE;
717 AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
719 atom_ctts_init (ctts);
724 atom_ctts_free (AtomCTTS * ctts)
726 atom_full_clear (&ctts->header);
727 atom_array_clear (&ctts->entries);
732 atom_svmi_init (AtomSVMI * svmi)
734 guint8 flags[3] = { 0, 0, 0 };
736 atom_full_init (&svmi->header, FOURCC_svmi, 0, 0, 0, flags);
737 svmi->stereoscopic_composition_type = 0x00;
738 svmi->is_left_first = FALSE;
742 atom_svmi_new (guint8 stereoscopic_composition_type, gboolean is_left_first)
744 AtomSVMI *svmi = g_new0 (AtomSVMI, 1);
746 atom_svmi_init (svmi);
747 svmi->stereoscopic_composition_type = stereoscopic_composition_type;
748 svmi->is_left_first = is_left_first;
753 atom_svmi_free (AtomSVMI * svmi)
759 atom_stts_init (AtomSTTS * stts)
761 guint8 flags[3] = { 0, 0, 0 };
763 atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
764 atom_array_init (&stts->entries, 512);
768 atom_stts_clear (AtomSTTS * stts)
770 atom_full_clear (&stts->header);
771 atom_array_clear (&stts->entries);
775 atom_stsz_init (AtomSTSZ * stsz)
777 guint8 flags[3] = { 0, 0, 0 };
779 atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
780 atom_array_init (&stsz->entries, 1024);
781 stsz->sample_size = 0;
782 stsz->table_size = 0;
786 atom_stsz_clear (AtomSTSZ * stsz)
788 atom_full_clear (&stsz->header);
789 atom_array_clear (&stsz->entries);
790 stsz->table_size = 0;
794 atom_stsc_init (AtomSTSC * stsc)
796 guint8 flags[3] = { 0, 0, 0 };
798 atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
799 atom_array_init (&stsc->entries, 128);
803 atom_stsc_clear (AtomSTSC * stsc)
805 atom_full_clear (&stsc->header);
806 atom_array_clear (&stsc->entries);
810 atom_co64_init (AtomSTCO64 * co64)
812 guint8 flags[3] = { 0, 0, 0 };
814 atom_full_init (&co64->header, FOURCC_stco, 0, 0, 0, flags);
815 atom_array_init (&co64->entries, 256);
819 atom_stco64_clear (AtomSTCO64 * stco64)
821 atom_full_clear (&stco64->header);
822 atom_array_clear (&stco64->entries);
826 atom_stss_init (AtomSTSS * stss)
828 guint8 flags[3] = { 0, 0, 0 };
830 atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
831 atom_array_init (&stss->entries, 128);
835 atom_stss_clear (AtomSTSS * stss)
837 atom_full_clear (&stss->header);
838 atom_array_clear (&stss->entries);
842 atom_stbl_init (AtomSTBL * stbl)
844 atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
846 atom_stts_init (&stbl->stts);
847 atom_stss_init (&stbl->stss);
848 atom_stsd_init (&stbl->stsd);
849 atom_stsz_init (&stbl->stsz);
850 atom_stsc_init (&stbl->stsc);
854 atom_co64_init (&stbl->stco64);
858 atom_stbl_clear (AtomSTBL * stbl)
860 atom_clear (&stbl->header);
861 atom_stsd_clear (&stbl->stsd);
862 atom_stts_clear (&stbl->stts);
863 atom_stss_clear (&stbl->stss);
864 atom_stsc_clear (&stbl->stsc);
865 atom_stsz_clear (&stbl->stsz);
867 atom_ctts_free (stbl->ctts);
870 atom_svmi_free (stbl->svmi);
872 atom_stco64_clear (&stbl->stco64);
876 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
878 guint8 flags[3] = { 0, 0, 1 };
880 atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
881 vmhd->graphics_mode = 0x0;
882 memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
884 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
885 vmhd->graphics_mode = 0x40;
886 vmhd->opcolor[0] = 32768;
887 vmhd->opcolor[1] = 32768;
888 vmhd->opcolor[2] = 32768;
893 atom_vmhd_new (AtomsContext * context)
895 AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
897 atom_vmhd_init (vmhd, context);
902 atom_vmhd_free (AtomVMHD * vmhd)
904 atom_full_clear (&vmhd->header);
909 atom_smhd_init (AtomSMHD * smhd)
911 guint8 flags[3] = { 0, 0, 0 };
913 atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
921 AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
923 atom_smhd_init (smhd);
928 atom_smhd_free (AtomSMHD * smhd)
930 atom_full_clear (&smhd->header);
935 atom_hmhd_free (AtomHMHD * hmhd)
937 atom_full_clear (&hmhd->header);
942 atom_hdlr_init (AtomHDLR * hdlr, AtomsContext * context)
944 guint8 flags[3] = { 0, 0, 0 };
946 atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
948 hdlr->component_type = 0;
949 hdlr->handler_type = 0;
950 hdlr->manufacturer = 0;
952 hdlr->flags_mask = 0;
953 hdlr->name = g_strdup ("");
955 /* Store the flavor to know how to serialize the 'name' string */
956 hdlr->flavor = context->flavor;
960 atom_hdlr_new (AtomsContext * context)
962 AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
964 atom_hdlr_init (hdlr, context);
969 atom_hdlr_clear (AtomHDLR * hdlr)
971 atom_full_clear (&hdlr->header);
979 atom_hdlr_free (AtomHDLR * hdlr)
981 atom_hdlr_clear (hdlr);
986 atom_url_init (AtomURL * url)
988 guint8 flags[3] = { 0, 0, 1 };
990 atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
991 url->location = NULL;
995 atom_url_free (AtomURL * url)
997 atom_full_clear (&url->header);
999 g_free (url->location);
1000 url->location = NULL;
1008 AtomURL *url = g_new0 (AtomURL, 1);
1010 atom_url_init (url);
1015 atom_alis_new (void)
1017 guint8 flags[3] = { 0, 0, 1 };
1018 AtomFull *alis = g_new0 (AtomFull, 1);
1020 atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
1025 atom_dref_init (AtomDREF * dref, AtomsContext * context)
1027 guint8 flags[3] = { 0, 0, 0 };
1029 atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
1031 /* in either case, alis or url init arranges to set self-contained flag */
1032 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
1033 /* alis dref for qt */
1034 AtomFull *alis = atom_alis_new ();
1035 dref->entries = g_list_append (dref->entries, alis);
1037 /* url for iso spec, as 'alis' not specified there */
1038 AtomURL *url = atom_url_new ();
1039 dref->entries = g_list_append (dref->entries, url);
1044 atom_dref_clear (AtomDREF * dref)
1048 atom_full_clear (&dref->header);
1049 walker = dref->entries;
1051 GList *aux = walker;
1052 Atom *atom = (Atom *) aux->data;
1054 walker = g_list_next (walker);
1055 dref->entries = g_list_remove_link (dref->entries, aux);
1056 switch (atom->type) {
1058 atom_full_free ((AtomFull *) atom);
1061 atom_url_free ((AtomURL *) atom);
1064 /* we do nothing, better leak than crash */
1072 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
1074 atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
1075 atom_dref_init (&dinf->dref, context);
1079 atom_dinf_clear (AtomDINF * dinf)
1081 atom_clear (&dinf->header);
1082 atom_dref_clear (&dinf->dref);
1086 atom_minf_init (AtomMINF * minf, AtomsContext * context)
1088 atom_header_set (&minf->header, FOURCC_minf, 0, 0);
1095 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
1096 minf->hdlr = atom_hdlr_new (context);
1097 minf->hdlr->component_type = FOURCC_dhlr;
1098 minf->hdlr->handler_type = FOURCC_alis;
1102 atom_dinf_init (&minf->dinf, context);
1103 atom_stbl_init (&minf->stbl);
1107 atom_minf_clear_handlers (AtomMINF * minf)
1110 atom_vmhd_free (minf->vmhd);
1114 atom_smhd_free (minf->smhd);
1118 atom_hmhd_free (minf->hmhd);
1122 atom_gmhd_free (minf->gmhd);
1128 atom_minf_clear (AtomMINF * minf)
1130 atom_clear (&minf->header);
1131 atom_minf_clear_handlers (minf);
1133 atom_hdlr_free (minf->hdlr);
1135 atom_dinf_clear (&minf->dinf);
1136 atom_stbl_clear (&minf->stbl);
1140 atom_mdhd_init (AtomMDHD * mdhd)
1142 guint8 flags[3] = { 0, 0, 0 };
1144 atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
1145 common_time_info_init (&mdhd->time_info);
1146 /* tempting as it may be to simply 0-initialize,
1147 * that will have the demuxer (correctly) come up with 'eng' as language
1148 * so explicitly specify undefined instead */
1149 mdhd->language_code =
1150 ('u' - 0x60) * 0x400 + ('n' - 0x60) * 0x20 + ('d' - 0x60);
1155 atom_mdhd_clear (AtomMDHD * mdhd)
1157 atom_full_clear (&mdhd->header);
1161 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
1163 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
1165 atom_mdhd_init (&mdia->mdhd);
1166 atom_hdlr_init (&mdia->hdlr, context);
1167 atom_minf_init (&mdia->minf, context);
1171 atom_mdia_clear (AtomMDIA * mdia)
1173 atom_clear (&mdia->header);
1174 atom_mdhd_clear (&mdia->mdhd);
1175 atom_hdlr_clear (&mdia->hdlr);
1176 atom_minf_clear (&mdia->minf);
1180 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
1184 * 1 -> track enabled
1185 * 2 -> track in movie
1186 * 4 -> track in preview
1188 guint8 flags[3] = { 0, 0, 7 };
1190 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
1192 tkhd->creation_time = tkhd->modification_time = atoms_get_current_qt_time ();
1197 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
1199 tkhd->alternate_group = 0;
1201 tkhd->reserved3 = 0;
1202 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
1203 tkhd->matrix[0] = 1 << 16;
1204 tkhd->matrix[4] = 1 << 16;
1205 tkhd->matrix[8] = 16384 << 16;
1211 atom_tkhd_clear (AtomTKHD * tkhd)
1213 atom_full_clear (&tkhd->header);
1217 atom_ilst_init (AtomILST * ilst)
1219 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
1220 ilst->entries = NULL;
1224 atom_ilst_new (void)
1226 AtomILST *ilst = g_new0 (AtomILST, 1);
1228 atom_ilst_init (ilst);
1233 atom_ilst_free (AtomILST * ilst)
1236 atom_info_list_free (ilst->entries);
1237 atom_clear (&ilst->header);
1242 atom_meta_init (AtomMETA * meta, AtomsContext * context)
1244 guint8 flags[3] = { 0, 0, 0 };
1246 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1247 atom_hdlr_init (&meta->hdlr, context);
1248 /* FIXME (ISOM says this is always 0) */
1249 meta->hdlr.component_type = FOURCC_mhlr;
1250 meta->hdlr.handler_type = FOURCC_mdir;
1255 atom_meta_new (AtomsContext * context)
1257 AtomMETA *meta = g_new0 (AtomMETA, 1);
1259 atom_meta_init (meta, context);
1264 atom_meta_free (AtomMETA * meta)
1266 atom_full_clear (&meta->header);
1267 atom_hdlr_clear (&meta->hdlr);
1269 atom_ilst_free (meta->ilst);
1275 atom_udta_init_metatags (AtomUDTA * udta, AtomsContext * context)
1277 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
1279 udta->meta = atom_meta_new (context);
1281 if (!udta->meta->ilst) {
1282 udta->meta->ilst = atom_ilst_new ();
1288 atom_udta_init (AtomUDTA * udta, AtomsContext * context)
1290 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1292 udta->context = context;
1294 atom_udta_init_metatags (udta, context);
1298 atom_udta_clear (AtomUDTA * udta)
1300 atom_clear (&udta->header);
1302 atom_meta_free (udta->meta);
1305 atom_info_list_free (udta->entries);
1309 atom_tref_init (AtomTREF * tref, guint32 reftype)
1311 atom_header_set (&tref->header, FOURCC_tref, 0, 0);
1312 tref->reftype = reftype;
1313 atom_array_init (&tref->entries, 128);
1317 atom_tref_clear (AtomTREF * tref)
1319 atom_clear (&tref->header);
1321 atom_array_clear (&tref->entries);
1325 atom_tref_new (guint32 reftype)
1329 tref = g_new0 (AtomTREF, 1);
1330 atom_tref_init (tref, reftype);
1336 atom_tref_free (AtomTREF * tref)
1338 atom_tref_clear (tref);
1342 /* Clear added tags, but keep the context/flavor the same */
1344 atom_udta_clear_tags (AtomUDTA * udta)
1346 if (udta->entries) {
1347 atom_info_list_free (udta->entries);
1348 udta->entries = NULL;
1350 if (udta->meta && udta->meta->ilst->entries) {
1351 atom_info_list_free (udta->meta->ilst->entries);
1352 udta->meta->ilst->entries = NULL;
1357 atom_tag_data_init (AtomTagData * data)
1359 guint8 flags[] = { 0, 0, 0 };
1361 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1365 atom_tag_data_clear (AtomTagData * data)
1367 atom_full_clear (&data->header);
1368 g_free (data->data);
1373 * Fourcc is the tag fourcc
1374 * flags will be truncated to 24bits
1377 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1379 AtomTag *tag = g_new0 (AtomTag, 1);
1381 tag->header.type = fourcc;
1382 atom_tag_data_init (&tag->data);
1383 atom_full_set_flags_as_uint (&tag->data.header, flags_as_uint);
1388 atom_tag_free (AtomTag * tag)
1390 atom_clear (&tag->header);
1391 atom_tag_data_clear (&tag->data);
1396 atom_mvhd_init (AtomMVHD * mvhd)
1398 guint8 flags[3] = { 0, 0, 0 };
1400 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1402 common_time_info_init (&mvhd->time_info);
1404 mvhd->prefered_rate = 1 << 16;
1405 mvhd->volume = 1 << 8;
1406 mvhd->reserved3 = 0;
1407 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1409 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1410 mvhd->matrix[0] = 1 << 16;
1411 mvhd->matrix[4] = 1 << 16;
1412 mvhd->matrix[8] = 16384 << 16;
1414 mvhd->preview_time = 0;
1415 mvhd->preview_duration = 0;
1416 mvhd->poster_time = 0;
1417 mvhd->selection_time = 0;
1418 mvhd->selection_duration = 0;
1419 mvhd->current_time = 0;
1421 mvhd->next_track_id = 1;
1425 atom_mvhd_clear (AtomMVHD * mvhd)
1427 atom_full_clear (&mvhd->header);
1431 atom_mehd_init (AtomMEHD * mehd)
1433 guint8 flags[3] = { 0, 0, 0 };
1435 atom_full_init (&mehd->header, FOURCC_mehd, 0, 0, 1, flags);
1436 mehd->fragment_duration = 0;
1440 atom_mvex_init (AtomMVEX * mvex)
1442 atom_header_set (&mvex->header, FOURCC_mvex, 0, 0);
1443 atom_mehd_init (&mvex->mehd);
1448 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
1450 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
1452 atom_tkhd_init (&trak->tkhd, context);
1453 trak->context = context;
1454 atom_udta_init (&trak->udta, context);
1456 atom_mdia_init (&trak->mdia, context);
1461 atom_trak_new (AtomsContext * context)
1463 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
1465 atom_trak_init (trak, context);
1470 atom_trak_clear (AtomTRAK * trak)
1472 atom_clear (&trak->header);
1473 atom_tkhd_clear (&trak->tkhd);
1475 atom_edts_free (trak->edts);
1476 atom_udta_clear (&trak->udta);
1477 atom_mdia_clear (&trak->mdia);
1479 atom_tref_free (trak->tref);
1483 atom_trak_free (AtomTRAK * trak)
1485 atom_trak_clear (trak);
1491 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1493 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1494 atom_mvhd_init (&(moov->mvhd));
1495 atom_mvex_init (&(moov->mvex));
1496 atom_udta_init (&moov->udta, context);
1498 moov->context = *context;
1502 atom_moov_new (AtomsContext * context)
1504 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1506 atom_moov_init (moov, context);
1511 atom_trex_free (AtomTREX * trex)
1513 atom_full_clear (&trex->header);
1518 atom_mvex_clear (AtomMVEX * mvex)
1522 atom_clear (&mvex->header);
1523 walker = mvex->trexs;
1525 atom_trex_free ((AtomTREX *) walker->data);
1526 walker = g_list_next (walker);
1528 g_list_free (mvex->trexs);
1533 atom_moov_free (AtomMOOV * moov)
1537 atom_clear (&moov->header);
1538 atom_mvhd_clear (&moov->mvhd);
1540 walker = moov->traks;
1542 atom_trak_free ((AtomTRAK *) walker->data);
1543 walker = g_list_next (walker);
1545 g_list_free (moov->traks);
1548 atom_udta_clear (&moov->udta);
1549 atom_mvex_clear (&moov->mvex);
1554 /* -- end of init / free -- */
1556 /* -- copy data functions -- */
1559 atom_full_get_version (AtomFull * full)
1561 return full->version;
1565 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1566 guint8 ** buffer, guint64 * size, guint64 * offset)
1568 guint64 original_offset = *offset;
1571 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1572 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1573 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1574 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1576 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1577 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1578 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1579 prop_copy_uint64 (ti->duration, buffer, size, offset);
1581 return *offset - original_offset;
1585 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1588 /* this only works for non-extended atom size, which is OK
1589 * (though it could be made to do mem_move, etc and write extended size) */
1590 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1594 atom_copy_empty (Atom * atom, guint8 ** buffer, guint64 * size,
1597 guint64 original_offset = *offset;
1599 prop_copy_uint32 (0, buffer, size, offset);
1601 return *offset - original_offset;
1605 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1607 guint64 original_offset = *offset;
1609 /* copies type and size */
1610 prop_copy_uint32 (atom->size, buffer, size, offset);
1611 prop_copy_fourcc (atom->type, buffer, size, offset);
1613 /* extended size needed */
1614 if (atom->size == 1) {
1615 /* really should not happen other than with mdat atom;
1616 * would be a problem for size (re)write code, not to mention memory */
1617 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1618 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1621 return *offset - original_offset;
1625 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1628 guint64 original_offset = *offset;
1630 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1634 prop_copy_uint8 (atom->version, buffer, size, offset);
1635 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1637 atom_write_size (buffer, size, offset, original_offset);
1638 return *offset - original_offset;
1642 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1645 guint64 original_offset = *offset;
1648 AtomInfo *info = (AtomInfo *) ai->data;
1650 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1653 ai = g_list_next (ai);
1656 return *offset - original_offset;
1660 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1663 guint64 original_offset = *offset;
1665 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1669 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1671 atom_write_size (buffer, size, offset, original_offset);
1672 return *offset - original_offset;
1676 atom_uuid_copy_data (AtomUUID * uuid, guint8 ** buffer, guint64 * size,
1679 guint64 original_offset = *offset;
1681 if (!atom_copy_data (&uuid->header, buffer, size, offset)) {
1684 prop_copy_uint8_array (uuid->uuid, 16, buffer, size, offset);
1686 prop_copy_uint8_array (uuid->data, uuid->datalen, buffer, size, offset);
1688 atom_write_size (buffer, size, offset, original_offset);
1689 return *offset - original_offset;
1693 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1696 guint64 original_offset = *offset;
1698 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1701 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1702 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1704 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1705 buffer, size, offset);
1707 atom_write_size (buffer, size, offset, original_offset);
1708 return *offset - original_offset;
1712 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1716 guint64 original_offset = *offset;
1718 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1722 version = atom_full_get_version (&(atom->header));
1724 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1725 } else if (version == 1) {
1726 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1728 *offset = original_offset;
1732 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1733 prop_copy_uint16 (atom->volume, buffer, size, offset);
1734 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1735 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1736 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1737 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1738 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1739 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1740 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1741 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1742 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1744 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1746 atom_write_size (buffer, size, offset, original_offset);
1747 return *offset - original_offset;
1751 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1754 guint64 original_offset = *offset;
1756 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1760 if (atom_full_get_version (&tkhd->header) == 0) {
1761 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1762 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1763 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1764 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1765 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1767 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1768 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1769 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1770 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1771 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1774 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1775 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1776 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1777 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1778 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1779 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1781 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1782 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1784 atom_write_size (buffer, size, offset, original_offset);
1785 return *offset - original_offset;
1789 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1792 guint64 original_offset = *offset;
1794 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1798 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1799 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1800 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1801 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1802 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1804 if (hdlr->flavor == ATOMS_TREE_FLAVOR_MOV) {
1805 prop_copy_size_string ((guint8 *) hdlr->name, strlen (hdlr->name), buffer,
1808 /* assume isomedia base is more generic and use null terminated */
1809 prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
1812 atom_write_size (buffer, size, offset, original_offset);
1813 return *offset - original_offset;
1817 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1820 guint64 original_offset = *offset;
1822 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1825 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1826 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1828 atom_write_size (buffer, size, offset, original_offset);
1829 return original_offset - *offset;
1833 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1836 guint64 original_offset = *offset;
1838 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1841 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1842 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1844 atom_write_size (buffer, size, offset, original_offset);
1845 return original_offset - *offset;
1849 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1852 guint64 original_offset = *offset;
1854 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1857 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1858 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1859 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1860 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1861 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1863 atom_write_size (buffer, size, offset, original_offset);
1864 return original_offset - *offset;
1868 atom_tcmi_copy_data (AtomTCMI * tcmi, guint8 ** buffer, guint64 * size,
1871 guint64 original_offset = *offset;
1873 if (!atom_full_copy_data (&tcmi->header, buffer, size, offset)) {
1876 prop_copy_uint16 (tcmi->text_font, buffer, size, offset);
1877 prop_copy_uint16 (tcmi->text_face, buffer, size, offset);
1878 prop_copy_uint16 (tcmi->text_size, buffer, size, offset);
1879 prop_copy_uint16 (tcmi->text_color[0], buffer, size, offset);
1880 prop_copy_uint16 (tcmi->text_color[1], buffer, size, offset);
1881 prop_copy_uint16 (tcmi->text_color[2], buffer, size, offset);
1882 prop_copy_uint16 (tcmi->bg_color[0], buffer, size, offset);
1883 prop_copy_uint16 (tcmi->bg_color[1], buffer, size, offset);
1884 prop_copy_uint16 (tcmi->bg_color[2], buffer, size, offset);
1886 prop_copy_uint16 (0, buffer, size, offset);
1887 prop_copy_size_string ((guint8 *) tcmi->font_name, strlen (tcmi->font_name),
1888 buffer, size, offset);
1890 atom_write_size (buffer, size, offset, original_offset);
1891 return original_offset - *offset;
1895 atom_tmcd_copy_data (AtomTMCD * tmcd, guint8 ** buffer, guint64 * size,
1898 guint64 original_offset = *offset;
1900 if (!atom_copy_data (&tmcd->header, buffer, size, offset)) {
1903 if (!atom_tcmi_copy_data (&tmcd->tcmi, buffer, size, offset)) {
1907 atom_write_size (buffer, size, offset, original_offset);
1908 return original_offset - *offset;
1912 atom_gmin_copy_data (AtomGMIN * gmin, guint8 ** buffer, guint64 * size,
1915 guint64 original_offset = *offset;
1917 if (!atom_full_copy_data (&gmin->header, buffer, size, offset)) {
1920 prop_copy_uint16 (gmin->graphics_mode, buffer, size, offset);
1921 prop_copy_uint16 (gmin->opcolor[0], buffer, size, offset);
1922 prop_copy_uint16 (gmin->opcolor[1], buffer, size, offset);
1923 prop_copy_uint16 (gmin->opcolor[2], buffer, size, offset);
1924 prop_copy_uint8 (gmin->balance, buffer, size, offset);
1926 prop_copy_uint8 (0, buffer, size, offset);
1928 atom_write_size (buffer, size, offset, original_offset);
1929 return original_offset - *offset;
1933 atom_gmhd_copy_data (AtomGMHD * gmhd, guint8 ** buffer, guint64 * size,
1936 guint64 original_offset = *offset;
1938 if (!atom_copy_data (&gmhd->header, buffer, size, offset)) {
1941 if (!atom_gmin_copy_data (&gmhd->gmin, buffer, size, offset)) {
1944 if (!atom_tmcd_copy_data (&gmhd->tmcd, buffer, size, offset)) {
1948 atom_write_size (buffer, size, offset, original_offset);
1949 return original_offset - *offset;
1953 atom_url_same_file_flag (AtomURL * url)
1955 return (url->header.flags[2] & 0x1) == 1;
1959 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1962 guint64 original_offset = *offset;
1964 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1968 if (!atom_url_same_file_flag (url)) {
1969 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1972 atom_write_size (buffer, size, offset, original_offset);
1973 return original_offset - *offset;
1977 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1980 guint64 original_offset = *offset;
1983 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1987 prop_copy_uint32 (atom_array_get_len (&stts->entries), buffer, size, offset);
1988 /* minimize realloc */
1989 prop_copy_ensure_buffer (buffer, size, offset,
1990 8 * atom_array_get_len (&stts->entries));
1991 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
1992 STTSEntry *entry = &atom_array_index (&stts->entries, i);
1994 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
1995 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
1998 atom_write_size (buffer, size, offset, original_offset);
1999 return *offset - original_offset;
2003 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
2004 guint64 * size, guint64 * offset)
2006 guint64 original_offset = *offset;
2008 if (!atom_copy_data (&se->header, buffer, size, offset)) {
2012 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
2013 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
2015 return *offset - original_offset;
2019 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
2022 guint64 original_offset = *offset;
2024 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
2027 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
2031 atom_write_size (buffer, size, offset, original_offset);
2032 return *offset - original_offset;
2036 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
2037 guint64 * size, guint64 * offset)
2039 guint64 original_offset = *offset;
2041 if (!atom_copy_data (&(frma->header), buffer, size, offset))
2044 prop_copy_fourcc (frma->media_type, buffer, size, offset);
2046 atom_write_size (buffer, size, offset, original_offset);
2047 return *offset - original_offset;
2051 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
2052 guint64 * size, guint64 * offset)
2054 guint64 original_offset = *offset;
2056 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
2060 prop_copy_uint32 (hse->size, buffer, size, offset);
2061 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
2063 atom_write_size (buffer, size, offset, original_offset);
2064 return *offset - original_offset;
2068 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
2069 guint64 * size, guint64 * offset)
2071 guint64 original_offset = *offset;
2073 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
2077 prop_copy_uint16 (mp4a->version, buffer, size, offset);
2078 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
2079 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
2080 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
2081 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
2082 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
2083 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
2084 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
2086 /* this should always be 0 for mp4 flavor */
2087 if (mp4a->version == 1) {
2088 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
2089 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
2090 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
2091 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
2094 if (mp4a->extension_atoms) {
2095 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
2099 atom_write_size (buffer, size, offset, original_offset);
2100 return *offset - original_offset;
2104 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
2105 guint64 * size, guint64 * offset)
2107 guint64 original_offset = *offset;
2109 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
2113 prop_copy_uint16 (mp4v->version, buffer, size, offset);
2114 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
2115 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
2116 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
2117 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
2119 prop_copy_uint16 (mp4v->width, buffer, size, offset);
2120 prop_copy_uint16 (mp4v->height, buffer, size, offset);
2122 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
2123 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
2124 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
2126 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
2128 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
2131 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
2132 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
2135 if (mp4v->extension_atoms &&
2136 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
2139 atom_write_size (buffer, size, offset, original_offset);
2140 return *offset - original_offset;
2144 sample_entry_tx3g_copy_data (SampleTableEntryTX3G * tx3g, guint8 ** buffer,
2145 guint64 * size, guint64 * offset)
2147 guint64 original_offset = *offset;
2149 if (!atom_sample_entry_copy_data (&tx3g->se, buffer, size, offset)) {
2153 prop_copy_uint32 (tx3g->display_flags, buffer, size, offset);
2156 prop_copy_uint8 (1, buffer, size, offset);
2157 prop_copy_uint8 (-1, buffer, size, offset);
2158 prop_copy_uint32 (0, buffer, size, offset);
2160 prop_copy_uint64 (tx3g->default_text_box, buffer, size, offset);
2163 prop_copy_uint32 (0, buffer, size, offset);
2165 prop_copy_uint16 (tx3g->font_id, buffer, size, offset);
2166 prop_copy_uint8 (tx3g->font_face, buffer, size, offset);
2167 prop_copy_uint8 (tx3g->font_size, buffer, size, offset);
2168 prop_copy_uint32 (tx3g->foreground_color_rgba, buffer, size, offset);
2170 /* it must have a fonttable atom */
2174 atom_header_set (&atom, FOURCC_ftab, 18, 0);
2175 if (!atom_copy_data (&atom, buffer, size, offset))
2177 prop_copy_uint16 (1, buffer, size, offset); /* Count must be 1 */
2178 prop_copy_uint16 (1, buffer, size, offset); /* Font id: 1 */
2179 prop_copy_size_string ((guint8 *) "Serif", 5, buffer, size, offset);
2182 atom_write_size (buffer, size, offset, original_offset);
2183 return *offset - original_offset;
2187 sample_entry_tmcd_copy_data (SampleTableEntryTMCD * tmcd, guint8 ** buffer,
2188 guint64 * size, guint64 * offset)
2190 guint64 original_offset = *offset;
2192 if (!atom_sample_entry_copy_data (&tmcd->se, buffer, size, offset)) {
2197 prop_copy_uint32 (0, buffer, size, offset);
2199 prop_copy_uint32 (tmcd->tc_flags, buffer, size, offset);
2200 prop_copy_uint32 (tmcd->timescale, buffer, size, offset);
2201 prop_copy_uint32 (tmcd->frame_duration, buffer, size, offset);
2202 prop_copy_uint8 (tmcd->n_frames, buffer, size, offset);
2205 prop_copy_uint8 (0, buffer, size, offset);
2208 guint64 name_offset = *offset;
2210 atom_header_set (&atom, FOURCC_name, 0, 0);
2211 if (!atom_copy_data (&atom, buffer, size, offset))
2213 prop_copy_uint16 (strlen (tmcd->name.name), buffer, size, offset);
2214 prop_copy_uint16 (tmcd->name.language_code, buffer, size, offset);
2215 prop_copy_fixed_size_string ((guint8 *) tmcd->name.name,
2216 strlen (tmcd->name.name), buffer, size, offset);
2218 atom_write_size (buffer, size, offset, name_offset);
2221 atom_write_size (buffer, size, offset, original_offset);
2222 return *offset - original_offset;
2226 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
2229 guint64 original_offset = *offset;
2232 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
2236 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
2237 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
2238 if (stsz->sample_size == 0) {
2239 /* minimize realloc */
2240 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
2241 /* entry count must match sample count */
2242 g_assert (atom_array_get_len (&stsz->entries) == stsz->table_size);
2243 for (i = 0; i < atom_array_get_len (&stsz->entries); i++) {
2244 prop_copy_uint32 (atom_array_index (&stsz->entries, i), buffer, size,
2249 atom_write_size (buffer, size, offset, original_offset);
2250 return *offset - original_offset;
2254 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
2257 guint64 original_offset = *offset;
2259 gboolean last_entries_merged = FALSE;
2261 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
2265 /* Last two entries might be the same size here as we only merge once the
2266 * next chunk is started */
2267 if ((len = atom_array_get_len (&stsc->entries)) > 1 &&
2268 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
2269 (atom_array_index (&stsc->entries, len - 2)).samples_per_chunk)) {
2270 stsc->entries.len--;
2271 last_entries_merged = TRUE;
2274 prop_copy_uint32 (atom_array_get_len (&stsc->entries), buffer, size, offset);
2275 /* minimize realloc */
2276 prop_copy_ensure_buffer (buffer, size, offset,
2277 12 * atom_array_get_len (&stsc->entries));
2279 for (i = 0; i < atom_array_get_len (&stsc->entries); i++) {
2280 STSCEntry *entry = &atom_array_index (&stsc->entries, i);
2282 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
2283 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
2284 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
2287 atom_write_size (buffer, size, offset, original_offset);
2289 /* Need to add the last entry again as in "robust" muxing mode we will most
2290 * likely add new samples to the last chunk, thus making the
2291 * samples_per_chunk in the last one different to the second to last one,
2292 * and thus making it wrong to keep them merged
2294 if (last_entries_merged)
2295 stsc->entries.len++;
2297 return *offset - original_offset;
2301 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
2304 guint64 original_offset = *offset;
2307 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
2311 prop_copy_uint32 (atom_array_get_len (&ctts->entries), buffer, size, offset);
2312 /* minimize realloc */
2313 prop_copy_ensure_buffer (buffer, size, offset,
2314 8 * atom_array_get_len (&ctts->entries));
2315 for (i = 0; i < atom_array_get_len (&ctts->entries); i++) {
2316 CTTSEntry *entry = &atom_array_index (&ctts->entries, i);
2318 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
2319 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
2322 atom_write_size (buffer, size, offset, original_offset);
2323 return *offset - original_offset;
2327 atom_svmi_copy_data (AtomSVMI * svmi, guint8 ** buffer, guint64 * size,
2330 guint64 original_offset = *offset;
2332 if (!atom_full_copy_data (&svmi->header, buffer, size, offset)) {
2336 prop_copy_uint8 (svmi->stereoscopic_composition_type, buffer, size, offset);
2337 prop_copy_uint8 (svmi->is_left_first ? 1 : 0, buffer, size, offset);
2338 /* stereo-mono change count */
2339 prop_copy_uint32 (0, buffer, size, offset);
2341 atom_write_size (buffer, size, offset, original_offset);
2342 return *offset - original_offset;
2346 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
2349 guint64 original_offset = *offset;
2351 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
2353 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
2357 prop_copy_uint32 (atom_array_get_len (&stco64->entries), buffer, size,
2360 /* minimize realloc */
2361 prop_copy_ensure_buffer (buffer, size, offset,
2362 8 * atom_array_get_len (&stco64->entries));
2363 for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
2365 atom_array_index (&stco64->entries, i) + stco64->chunk_offset;
2368 prop_copy_uint32 ((guint32) value, buffer, size, offset);
2370 prop_copy_uint64 (value, buffer, size, offset);
2374 atom_write_size (buffer, size, offset, original_offset);
2375 return *offset - original_offset;
2379 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
2382 guint64 original_offset = *offset;
2385 if (atom_array_get_len (&stss->entries) == 0) {
2386 /* FIXME not needing this atom might be confused with error while copying */
2390 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
2394 prop_copy_uint32 (atom_array_get_len (&stss->entries), buffer, size, offset);
2395 /* minimize realloc */
2396 prop_copy_ensure_buffer (buffer, size, offset,
2397 4 * atom_array_get_len (&stss->entries));
2398 for (i = 0; i < atom_array_get_len (&stss->entries); i++) {
2399 prop_copy_uint32 (atom_array_index (&stss->entries, i), buffer, size,
2403 atom_write_size (buffer, size, offset, original_offset);
2404 return *offset - original_offset;
2408 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
2411 guint64 original_offset = *offset;
2414 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
2418 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
2420 for (walker = g_list_last (stsd->entries); walker != NULL;
2421 walker = g_list_previous (walker)) {
2422 SampleTableEntry *se = (SampleTableEntry *) walker->data;
2424 switch (((Atom *) walker->data)->type) {
2426 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
2427 buffer, size, offset)) {
2432 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
2433 buffer, size, offset)) {
2438 if (se->kind == VIDEO) {
2439 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
2440 walker->data, buffer, size, offset)) {
2443 } else if (se->kind == AUDIO) {
2444 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
2445 walker->data, buffer, size, offset)) {
2448 } else if (se->kind == SUBTITLE) {
2449 if (!sample_entry_tx3g_copy_data ((SampleTableEntryTX3G *)
2450 walker->data, buffer, size, offset)) {
2453 } else if (se->kind == TIMECODE) {
2454 if (!sample_entry_tmcd_copy_data ((SampleTableEntryTMCD *)
2455 walker->data, buffer, size, offset)) {
2459 if (!atom_hint_sample_entry_copy_data (
2460 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
2468 atom_write_size (buffer, size, offset, original_offset);
2469 return *offset - original_offset;
2473 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
2476 guint64 original_offset = *offset;
2478 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
2482 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
2485 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
2488 /* this atom is optional, so let's check if we need it
2489 * (to avoid false error) */
2490 if (atom_array_get_len (&stbl->stss.entries)) {
2491 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
2496 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
2499 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
2502 if (stbl->ctts && stbl->ctts->do_pts) {
2503 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
2508 if (!atom_svmi_copy_data (stbl->svmi, buffer, size, offset)) {
2512 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
2516 atom_write_size (buffer, size, offset, original_offset);
2517 return original_offset - *offset;
2522 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
2525 guint64 original_offset = *offset;
2528 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
2532 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
2534 walker = dref->entries;
2535 while (walker != NULL) {
2536 Atom *atom = (Atom *) walker->data;
2538 if (atom->type == FOURCC_url_) {
2539 if (!atom_url_copy_data ((AtomURL *) atom, buffer, size, offset))
2541 } else if (atom->type == FOURCC_alis) {
2542 if (!atom_full_copy_data ((AtomFull *) atom, buffer, size, offset))
2545 g_error ("Unsupported atom used inside dref atom");
2547 walker = g_list_next (walker);
2550 atom_write_size (buffer, size, offset, original_offset);
2551 return *offset - original_offset;
2555 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
2558 guint64 original_offset = *offset;
2560 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
2564 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
2568 atom_write_size (buffer, size, offset, original_offset);
2569 return original_offset - *offset;
2573 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
2576 guint64 original_offset = *offset;
2578 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
2583 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
2586 } else if (minf->smhd) {
2587 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
2590 } else if (minf->hmhd) {
2591 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
2594 } else if (minf->gmhd) {
2595 if (!atom_gmhd_copy_data (minf->gmhd, buffer, size, offset)) {
2601 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2606 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2609 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2613 atom_write_size (buffer, size, offset, original_offset);
2614 return *offset - original_offset;
2618 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2621 guint64 original_offset = *offset;
2623 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2627 if (!common_time_info_copy_data (&mdhd->time_info,
2628 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2632 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2633 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2635 atom_write_size (buffer, size, offset, original_offset);
2636 return *offset - original_offset;
2640 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2643 guint64 original_offset = *offset;
2645 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2648 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2651 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2655 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2659 atom_write_size (buffer, size, offset, original_offset);
2660 return *offset - original_offset;
2664 atom_elst_copy_data (AtomELST * elst, guint8 ** buffer, guint64 * size,
2667 guint64 original_offset = *offset;
2670 if (!atom_full_copy_data (&elst->header, buffer, size, offset)) {
2674 prop_copy_uint32 (g_slist_length (elst->entries), buffer, size, offset);
2676 for (walker = elst->entries; walker != NULL; walker = g_slist_next (walker)) {
2677 EditListEntry *entry = (EditListEntry *) walker->data;
2678 prop_copy_uint32 (entry->duration, buffer, size, offset);
2679 prop_copy_uint32 (entry->media_time, buffer, size, offset);
2680 prop_copy_uint32 (entry->media_rate, buffer, size, offset);
2682 atom_write_size (buffer, size, offset, original_offset);
2683 return *offset - original_offset;
2687 atom_tref_copy_data (AtomTREF * tref, guint8 ** buffer, guint64 * size,
2690 guint64 original_offset = *offset;
2693 g_assert (atom_array_get_len (&tref->entries) > 0);
2695 if (!atom_copy_data (&tref->header, buffer, size, offset)) {
2699 prop_copy_uint32 (8 + 4 * atom_array_get_len (&tref->entries), buffer, size,
2701 prop_copy_fourcc (tref->reftype, buffer, size, offset);
2702 /* minimize realloc */
2703 prop_copy_ensure_buffer (buffer, size, offset,
2704 4 * atom_array_get_len (&tref->entries));
2705 for (i = 0; i < atom_array_get_len (&tref->entries); i++) {
2706 prop_copy_uint32 (atom_array_index (&tref->entries, i), buffer, size,
2710 atom_write_size (buffer, size, offset, original_offset);
2711 return *offset - original_offset;
2715 atom_edts_copy_data (AtomEDTS * edts, guint8 ** buffer, guint64 * size,
2718 guint64 original_offset = *offset;
2720 if (!atom_copy_data (&(edts->header), buffer, size, offset))
2723 if (!atom_elst_copy_data (&(edts->elst), buffer, size, offset))
2726 atom_write_size (buffer, size, offset, original_offset);
2727 return *offset - original_offset;
2731 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2734 guint64 original_offset = *offset;
2736 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2740 prop_copy_uint32 (data->reserved, buffer, size, offset);
2741 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2743 atom_write_size (buffer, size, offset, original_offset);
2744 return *offset - original_offset;
2748 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2751 guint64 original_offset = *offset;
2753 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2757 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2761 atom_write_size (buffer, size, offset, original_offset);
2762 return *offset - original_offset;
2766 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2769 guint64 original_offset = *offset;
2771 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2775 if (ilst->entries &&
2776 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2779 atom_write_size (buffer, size, offset, original_offset);
2780 return *offset - original_offset;
2784 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2787 guint64 original_offset = *offset;
2789 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2792 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2796 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2801 atom_write_size (buffer, size, offset, original_offset);
2802 return *offset - original_offset;
2806 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2809 guint64 original_offset = *offset;
2811 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2815 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2819 if (udta->entries) {
2821 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2825 atom_write_size (buffer, size, offset, original_offset);
2826 return *offset - original_offset;
2830 atom_mehd_copy_data (AtomMEHD * mehd, guint8 ** buffer, guint64 * size,
2833 guint64 original_offset = *offset;
2835 if (!atom_full_copy_data (&mehd->header, buffer, size, offset)) {
2839 prop_copy_uint64 (mehd->fragment_duration, buffer, size, offset);
2841 atom_write_size (buffer, size, offset, original_offset);
2842 return *offset - original_offset;
2846 atom_trex_copy_data (AtomTREX * trex, guint8 ** buffer, guint64 * size,
2849 guint64 original_offset = *offset;
2851 if (!atom_full_copy_data (&trex->header, buffer, size, offset)) {
2855 prop_copy_uint32 (trex->track_ID, buffer, size, offset);
2856 prop_copy_uint32 (trex->default_sample_description_index, buffer, size,
2858 prop_copy_uint32 (trex->default_sample_duration, buffer, size, offset);
2859 prop_copy_uint32 (trex->default_sample_size, buffer, size, offset);
2860 prop_copy_uint32 (trex->default_sample_flags, buffer, size, offset);
2862 atom_write_size (buffer, size, offset, original_offset);
2863 return *offset - original_offset;
2867 atom_mvex_copy_data (AtomMVEX * mvex, guint8 ** buffer, guint64 * size,
2870 guint64 original_offset = *offset;
2873 if (!atom_copy_data (&mvex->header, buffer, size, offset)) {
2877 if (!atom_mehd_copy_data (&mvex->mehd, buffer, size, offset)) {
2881 walker = g_list_first (mvex->trexs);
2882 while (walker != NULL) {
2883 if (!atom_trex_copy_data ((AtomTREX *) walker->data, buffer, size, offset)) {
2886 walker = g_list_next (walker);
2889 atom_write_size (buffer, size, offset, original_offset);
2890 return *offset - original_offset;
2894 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2897 guint64 original_offset = *offset;
2899 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2902 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2906 if (!trak->tapt->copy_data_func (trak->tapt->atom, buffer, size, offset)) {
2911 if (!atom_edts_copy_data (trak->edts, buffer, size, offset)) {
2916 /* Make sure we need this atom (there is a referenced track */
2917 if (atom_array_get_len (&trak->tref->entries) > 0) {
2918 if (!atom_tref_copy_data (trak->tref, buffer, size, offset)) {
2924 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2928 if (!atom_udta_copy_data (&trak->udta, buffer, size, offset)) {
2932 atom_write_size (buffer, size, offset, original_offset);
2933 return *offset - original_offset;
2938 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2941 guint64 original_offset = *offset;
2944 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2947 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2950 walker = g_list_first (atom->traks);
2951 while (walker != NULL) {
2952 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2955 walker = g_list_next (walker);
2958 if (!atom_udta_copy_data (&atom->udta, buffer, size, offset)) {
2962 if (atom->fragmented) {
2963 if (!atom_mvex_copy_data (&atom->mvex, buffer, size, offset)) {
2968 atom_write_size (buffer, size, offset, original_offset);
2969 return *offset - original_offset;
2973 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
2974 guint64 * size, guint64 * offset)
2976 guint64 original_offset = *offset;
2978 if (!atom_copy_data (&(wave->header), buffer, size, offset))
2981 if (wave->extension_atoms) {
2982 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
2986 atom_write_size (buffer, size, offset, original_offset);
2987 return *offset - original_offset;
2990 /* -- end of copy data functions -- */
2992 /* -- general functions, API and support functions */
2994 /* add samples to tables */
2997 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
3001 if ((len = atom_array_get_len (&stsc->entries)) > 1 &&
3002 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
3003 (atom_array_index (&stsc->entries, len - 2)).samples_per_chunk)) {
3006 /* Merge last two entries as they have the same number of samples per chunk */
3007 nentry = &atom_array_index (&stsc->entries, len - 1);
3008 nentry->first_chunk = first_chunk;
3009 nentry->samples_per_chunk = nsamples;
3010 nentry->sample_description_index = 1;
3014 nentry.first_chunk = first_chunk;
3015 nentry.samples_per_chunk = nsamples;
3016 nentry.sample_description_index = 1;
3017 atom_array_append (&stsc->entries, nentry, 128);
3022 atom_stsc_update_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
3026 len = atom_array_get_len (&stsc->entries);
3027 g_assert (len != 0);
3028 g_assert (atom_array_index (&stsc->entries,
3029 len - 1).first_chunk == first_chunk);
3031 atom_array_index (&stsc->entries, len - 1).samples_per_chunk += nsamples;
3035 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
3037 STTSEntry *entry = NULL;
3039 if (G_LIKELY (atom_array_get_len (&stts->entries) != 0))
3040 entry = &atom_array_index (&stts->entries,
3041 atom_array_get_len (&stts->entries) - 1);
3043 if (entry && entry->sample_delta == sample_delta) {
3044 entry->sample_count += sample_count;
3048 nentry.sample_count = sample_count;
3049 nentry.sample_delta = sample_delta;
3050 atom_array_append (&stts->entries, nentry, 256);
3055 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
3059 stsz->table_size += nsamples;
3060 if (stsz->sample_size != 0) {
3061 /* it is constant size, we don't need entries */
3064 for (i = 0; i < nsamples; i++) {
3065 atom_array_append (&stsz->entries, size, 1024);
3070 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
3072 return atom_array_get_len (&stco64->entries);
3075 /* returns TRUE if a new entry was added */
3077 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
3081 /* Only add a new entry if the chunk offset changed */
3082 if ((len = atom_array_get_len (&stco64->entries)) &&
3083 ((atom_array_index (&stco64->entries, len - 1)) == entry))
3086 atom_array_append (&stco64->entries, entry, 256);
3087 if (entry > G_MAXUINT32)
3088 stco64->header.header.type = FOURCC_co64;
3094 atom_tref_add_entry (AtomTREF * tref, guint32 sample)
3096 atom_array_append (&tref->entries, sample, 512);
3100 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
3102 atom_array_append (&stss->entries, sample, 512);
3106 atom_stbl_add_stss_entry (AtomSTBL * stbl)
3108 guint32 sample_index = stbl->stsz.table_size;
3110 atom_stss_add_entry (&stbl->stss, sample_index);
3114 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
3116 CTTSEntry *entry = NULL;
3118 if (G_LIKELY (atom_array_get_len (&ctts->entries) != 0))
3119 entry = &atom_array_index (&ctts->entries,
3120 atom_array_get_len (&ctts->entries) - 1);
3122 if (entry == NULL || entry->sampleoffset != offset) {
3125 nentry.samplecount = nsamples;
3126 nentry.sampleoffset = offset;
3127 atom_array_append (&ctts->entries, nentry, 256);
3129 ctts->do_pts = TRUE;
3131 entry->samplecount += nsamples;
3136 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
3138 if (stbl->ctts == NULL) {
3139 stbl->ctts = atom_ctts_new ();
3141 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
3145 atom_stbl_add_samples (AtomSTBL * stbl, guint32 nsamples, guint32 delta,
3146 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3148 atom_stts_add_entry (&stbl->stts, nsamples, delta);
3149 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
3150 if (atom_stco64_add_entry (&stbl->stco64, chunk_offset)) {
3151 atom_stsc_add_new_entry (&stbl->stsc,
3152 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
3154 atom_stsc_update_entry (&stbl->stsc,
3155 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
3159 atom_stbl_add_stss_entry (stbl);
3160 /* always store to arrange for consistent content */
3161 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
3165 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
3166 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3168 AtomSTBL *stbl = &trak->mdia.minf.stbl;
3169 atom_stbl_add_samples (stbl, nsamples, delta, size, chunk_offset, sync,
3173 /* trak and moov molding */
3176 atom_trak_get_timescale (AtomTRAK * trak)
3178 return trak->mdia.mdhd.time_info.timescale;
3182 atom_trak_get_id (AtomTRAK * trak)
3184 return trak->tkhd.track_ID;
3188 atom_trak_set_id (AtomTRAK * trak, guint32 id)
3190 trak->tkhd.track_ID = id;
3194 atom_moov_add_trex (AtomMOOV * moov, AtomTREX * trex)
3196 moov->mvex.trexs = g_list_append (moov->mvex.trexs, trex);
3200 atom_trex_new (AtomTRAK * trak)
3202 guint8 flags[3] = { 0, 0, 0 };
3203 AtomTREX *trex = g_new0 (AtomTREX, 1);
3205 atom_full_init (&trex->header, FOURCC_trex, 0, 0, 0, flags);
3207 trex->track_ID = trak->tkhd.track_ID;
3208 trex->default_sample_description_index = 1;
3209 trex->default_sample_duration = 0;
3210 trex->default_sample_size = 0;
3211 trex->default_sample_flags = 0;
3217 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
3219 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
3220 moov->traks = g_list_append (moov->traks, trak);
3221 /* additional trak means also new trex */
3222 atom_moov_add_trex (moov, atom_trex_new (trak));
3226 atom_moov_get_trak_count (AtomMOOV * moov)
3228 return g_list_length (moov->traks);
3232 atom_trak_get_duration (AtomTRAK * trak)
3234 return trak->tkhd.duration;
3238 atom_stts_get_total_duration (AtomSTTS * stts)
3243 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
3244 STTSEntry *entry = &atom_array_index (&stts->entries, i);
3246 sum += (guint64) (entry->sample_count) * entry->sample_delta;
3252 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
3254 trak->mdia.mdhd.time_info.duration =
3255 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
3256 if (trak->mdia.mdhd.time_info.timescale != 0) {
3257 trak->tkhd.duration =
3258 gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration,
3259 moov_timescale, trak->mdia.mdhd.time_info.timescale);
3261 trak->tkhd.duration = 0;
3266 timecode_atom_trak_set_duration (AtomTRAK * trak, guint64 duration,
3272 /* Sanity checks to ensure we have a timecode */
3273 g_assert (trak->mdia.minf.gmhd != NULL);
3274 g_assert (atom_array_get_len (&trak->mdia.minf.stbl.stts.entries) == 1);
3276 for (iter = trak->mdia.minf.stbl.stsd.entries; iter;
3277 iter = g_list_next (iter)) {
3278 SampleTableEntry *entry = iter->data;
3279 if (entry->kind == TIMECODE) {
3280 SampleTableEntryTMCD *tmcd = (SampleTableEntryTMCD *) entry;
3282 duration = duration * tmcd->timescale / timescale;
3283 timescale = tmcd->timescale;
3288 trak->tkhd.duration = duration;
3289 trak->mdia.mdhd.time_info.duration = duration;
3290 trak->mdia.mdhd.time_info.timescale = timescale;
3292 entry = &atom_array_index (&trak->mdia.minf.stbl.stts.entries, 0);
3293 entry->sample_delta = duration;
3297 atom_moov_get_timescale (AtomMOOV * moov)
3299 return moov->mvhd.time_info.timescale;
3303 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
3305 moov->mvhd.time_info.timescale = timescale;
3309 atom_moov_update_duration (AtomMOOV * moov)
3311 GList *traks = moov->traks;
3312 guint64 dur, duration = 0;
3315 AtomTRAK *trak = (AtomTRAK *) traks->data;
3317 /* Skip timecodes for now: they have a placeholder duration */
3318 if (trak->mdia.minf.gmhd == NULL) {
3319 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
3320 dur = atom_trak_get_duration (trak);
3324 traks = g_list_next (traks);
3326 /* Now update the duration of the timecodes */
3327 traks = moov->traks;
3329 AtomTRAK *trak = (AtomTRAK *) traks->data;
3331 if (trak->mdia.minf.gmhd != NULL)
3332 timecode_atom_trak_set_duration (trak, duration,
3333 atom_moov_get_timescale (moov));
3334 traks = g_list_next (traks);
3336 moov->mvhd.time_info.duration = duration;
3337 moov->mvex.mehd.fragment_duration = duration;
3341 atom_moov_set_fragmented (AtomMOOV * moov, gboolean fragmented)
3343 moov->fragmented = fragmented;
3347 atom_stco64_chunks_set_offset (AtomSTCO64 * stco64, guint32 offset)
3349 stco64->chunk_offset = offset;
3353 atom_moov_chunks_set_offset (AtomMOOV * moov, guint32 offset)
3355 GList *traks = moov->traks;
3357 if (offset == moov->chunks_offset)
3358 return; /* Nothing to do */
3361 AtomTRAK *trak = (AtomTRAK *) traks->data;
3363 atom_stco64_chunks_set_offset (&trak->mdia.minf.stbl.stco64, offset);
3364 traks = g_list_next (traks);
3367 moov->chunks_offset = offset;
3371 atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
3372 guint32 max_bitrate)
3374 AtomESDS *esds = NULL;
3375 AtomData *btrt = NULL;
3376 AtomWAVE *wave = NULL;
3379 GList *extensioniter = NULL;
3381 g_return_if_fail (trak != NULL);
3383 if (avg_bitrate == 0 && max_bitrate == 0)
3386 stsd = &trak->mdia.minf.stbl.stsd;
3387 for (iter = stsd->entries; iter; iter = g_list_next (iter)) {
3388 SampleTableEntry *entry = iter->data;
3390 switch (entry->kind) {
3392 SampleTableEntryMP4A *audioentry = (SampleTableEntryMP4A *) entry;
3393 extensioniter = audioentry->extension_atoms;
3397 SampleTableEntryMP4V *videoentry = (SampleTableEntryMP4V *) entry;
3398 extensioniter = videoentry->extension_atoms;
3406 for (; extensioniter; extensioniter = g_list_next (extensioniter)) {
3407 AtomInfo *atominfo = extensioniter->data;
3408 if (atominfo->atom->type == FOURCC_esds) {
3409 esds = (AtomESDS *) atominfo->atom;
3410 } else if (atominfo->atom->type == FOURCC_btrt) {
3411 btrt = (AtomData *) atominfo->atom;
3412 } else if (atominfo->atom->type == FOURCC_wave) {
3413 wave = (AtomWAVE *) atominfo->atom;
3417 /* wave might have an esds internally */
3419 for (extensioniter = wave->extension_atoms; extensioniter;
3420 extensioniter = g_list_next (extensioniter)) {
3421 AtomInfo *atominfo = extensioniter->data;
3422 if (atominfo->atom->type == FOURCC_esds) {
3423 esds = (AtomESDS *) atominfo->atom;
3430 if (avg_bitrate && esds->es.dec_conf_desc.avg_bitrate == 0)
3431 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
3432 if (max_bitrate && esds->es.dec_conf_desc.max_bitrate == 0)
3433 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
3436 /* type(4bytes) + size(4bytes) + buffersize(4bytes) +
3437 * maxbitrate(bytes) + avgbitrate(bytes) */
3438 if (max_bitrate && GST_READ_UINT32_BE (btrt->data + 4) == 0)
3439 GST_WRITE_UINT32_BE (btrt->data + 4, max_bitrate);
3440 if (avg_bitrate && GST_READ_UINT32_BE (btrt->data + 8) == 0)
3441 GST_WRITE_UINT32_BE (btrt->data + 8, avg_bitrate);
3446 atom_trak_tx3g_update_dimension (AtomTRAK * trak, guint32 width, guint32 height)
3450 SampleTableEntryTX3G *tx3g = NULL;
3452 stsd = &trak->mdia.minf.stbl.stsd;
3453 for (iter = stsd->entries; iter && tx3g == NULL; iter = g_list_next (iter)) {
3454 SampleTableEntry *entry = iter->data;
3456 switch (entry->kind) {
3458 tx3g = (SampleTableEntryTX3G *) entry;
3466 /* Currently we never set the vertical placement flag, so we don't
3467 * check for it to set the dimensions differently as the spec says.
3468 * Always do it for the not set case */
3470 tx3g->font_size = 0.05 * height;
3472 height = 0.15 * height;
3473 trak->tkhd.width = width << 16;
3474 trak->tkhd.height = height << 16;
3475 tx3g->default_text_box = width | (height << 16);
3480 * Meta tags functions
3483 atom_tag_data_alloc_data (AtomTagData * data, guint size)
3485 g_free (data->data);
3486 data->data = g_new0 (guint8, size);
3487 data->datalen = size;
3491 atom_udta_append_tag (AtomUDTA * udta, AtomInfo * tag)
3496 entries = &udta->meta->ilst->entries;
3498 entries = &udta->entries;
3499 *entries = g_list_append (*entries, tag);
3503 atom_udta_add_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3504 const guint8 * data, guint size)
3509 tag = atom_tag_new (fourcc, flags);
3511 atom_tag_data_alloc_data (tdata, size);
3512 memmove (tdata->data, data, size);
3514 atom_udta_append_tag (udta,
3515 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
3520 atom_udta_add_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3522 gint len = strlen (value);
3525 atom_udta_add_tag (udta, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
3529 atom_udta_add_uint_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3532 guint8 data[8] = { 0, };
3535 GST_WRITE_UINT16_BE (data, value);
3536 atom_udta_add_tag (udta, fourcc, flags, data, 2);
3538 GST_WRITE_UINT32_BE (data + 2, value);
3539 atom_udta_add_tag (udta, fourcc, flags, data, 8);
3543 #define GST_BUFFER_NEW_READONLY(mem, size) \
3544 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, mem, size, \
3548 atom_udta_add_blob_tag (AtomUDTA * udta, guint8 * data, guint size)
3550 AtomData *data_atom;
3557 /* blob is unparsed atom;
3558 * extract size and fourcc, and wrap remainder in data atom */
3559 len = GST_READ_UINT32_BE (data);
3560 fourcc = GST_READ_UINT32_LE (data + 4);
3564 data_atom = atom_data_new_from_data (fourcc, data + 8, len - 8);
3566 atom_udta_append_tag (udta,
3567 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3572 atom_udta_add_3gp_tag (AtomUDTA * udta, guint32 fourcc, guint8 * data,
3575 AtomData *data_atom;
3577 data_atom = atom_data_new (fourcc);
3579 /* need full atom */
3580 atom_data_alloc_mem (data_atom, size + 4);
3582 /* full atom: version and flags */
3583 GST_WRITE_UINT32_BE (data_atom->data, 0);
3584 memcpy (data_atom->data + 4, data, size);
3586 atom_udta_append_tag (udta,
3587 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3592 language_code (const char *lang)
3594 g_return_val_if_fail (lang != NULL, 0);
3595 g_return_val_if_fail (strlen (lang) == 3, 0);
3597 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
3598 ((lang[2] - 0x60) & 0x1F);
3602 atom_udta_add_3gp_str_int_tag (AtomUDTA * udta, guint32 fourcc,
3603 const gchar * value, gint16 ivalue)
3605 gint len = 0, size = 0;
3609 len = strlen (value);
3616 data = g_malloc (size + 3);
3617 /* language tag and null-terminated UTF-8 string */
3619 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
3620 /* include 0 terminator */
3621 memcpy (data + 2, value, len + 1);
3623 /* 16-bit unsigned int if standalone, otherwise 8-bit */
3626 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
3628 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
3633 atom_udta_add_3gp_tag (udta, fourcc, data, size);
3638 atom_udta_add_3gp_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3640 atom_udta_add_3gp_str_int_tag (udta, fourcc, value, -1);
3644 atom_udta_add_3gp_uint_tag (AtomUDTA * udta, guint32 fourcc, guint16 value)
3646 atom_udta_add_3gp_str_int_tag (udta, fourcc, NULL, value);
3650 atom_udta_add_xmp_tags (AtomUDTA * udta, GstBuffer * xmpbuffer)
3652 AtomData *data_atom = NULL;
3654 if (udta->context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3656 data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
3657 udta->entries = g_list_append (udta->entries,
3658 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3662 GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
3667 * Functions for specifying media types
3671 atom_minf_set_audio (AtomMINF * minf)
3673 atom_minf_clear_handlers (minf);
3674 minf->smhd = atom_smhd_new ();
3678 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
3680 atom_minf_clear_handlers (minf);
3681 minf->vmhd = atom_vmhd_new (context);
3685 atom_minf_set_subtitle (AtomMINF * minf)
3687 atom_minf_clear_handlers (minf);
3691 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
3694 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3695 hdlr->component_type = comp_type;
3697 hdlr->handler_type = hdlr_type;
3701 atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
3703 g_free (hdlr->name);
3704 hdlr->name = g_strdup (name);
3708 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
3710 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3711 /* Some players (low-end hardware) check for this name, which is what
3712 * QuickTime itself sets */
3713 atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3717 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3719 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3720 /* Some players (low-end hardware) check for this name, which is what
3721 * QuickTime itself sets */
3722 atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3726 atom_mdia_set_hdlr_type_subtitle (AtomMDIA * mdia, AtomsContext * context)
3728 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_sbtl);
3730 /* Just follows the pattern from video and audio above */
3731 atom_hdlr_set_name (&mdia->hdlr, "SubtitleHandler");
3735 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3737 atom_mdia_set_hdlr_type_audio (mdia, context);
3738 atom_minf_set_audio (&mdia->minf);
3742 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3744 atom_mdia_set_hdlr_type_video (mdia, context);
3745 atom_minf_set_video (&mdia->minf, context);
3749 atom_mdia_set_subtitle (AtomMDIA * mdia, AtomsContext * context)
3751 atom_mdia_set_hdlr_type_subtitle (mdia, context);
3752 atom_minf_set_subtitle (&mdia->minf);
3756 atom_tkhd_set_audio (AtomTKHD * tkhd)
3758 tkhd->volume = 0x0100;
3759 tkhd->width = tkhd->height = 0;
3763 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3768 /* qt and ISO base media do not contradict, and examples agree */
3769 tkhd->width = width;
3770 tkhd->height = height;
3774 atom_tkhd_set_subtitle (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3779 /* qt and ISO base media do not contradict, and examples agree */
3780 tkhd->width = width;
3781 tkhd->height = height;
3786 atom_edts_add_entry (AtomEDTS * edts, gint index, EditListEntry * entry)
3789 (EditListEntry *) g_slist_nth_data (edts->elst.entries, index);
3790 /* Create a new entry if missing (appends to the list if index is larger) */
3792 e = g_new (EditListEntry, 1);
3793 edts->elst.entries = g_slist_insert (edts->elst.entries, e, index);
3796 /* Update the entry */
3801 atom_trak_edts_clear (AtomTRAK * trak)
3804 atom_edts_free (trak->edts);
3810 * Update an entry in this trak edits list, creating it if needed.
3811 * index is the index of the entry to update, or create if it's past the end.
3812 * duration is in the moov's timescale
3813 * media_time is the offset in the media time to start from (media's timescale)
3814 * rate is a 32 bits fixed-point
3817 atom_trak_set_elst_entry (AtomTRAK * trak, gint index,
3818 guint32 duration, guint32 media_time, guint32 rate)
3820 EditListEntry entry;
3822 entry.duration = duration;
3823 entry.media_time = media_time;
3824 entry.media_rate = rate;
3826 if (trak->edts == NULL)
3827 trak->edts = atom_edts_new ();
3829 atom_edts_add_entry (trak->edts, index, &entry);
3832 /* re-negotiation is prevented at top-level, so only 1 entry expected.
3833 * Quite some more care here and elsewhere may be needed to
3834 * support several entries */
3835 static SampleTableEntryMP4A *
3836 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
3839 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3840 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
3842 mp4a->se.header.type = type;
3843 mp4a->se.kind = AUDIO;
3844 mp4a->compression_id = -1;
3845 mp4a->se.data_reference_index = 1;
3847 stsd->entries = g_list_prepend (stsd->entries, mp4a);
3852 /* return number of centiframes per second */
3854 atom_framerate_to_timescale (gint n, gint d)
3859 if (d != 1 && d != 1001) {
3860 /* otherwise there are probably rounding errors and we should rather guess
3861 * if it's close enough to a well known framerate */
3862 gst_video_guess_framerate (gst_util_uint64_scale (d, GST_SECOND, n), &n,
3866 return gst_util_uint64_scale (n, 100, d);
3869 static SampleTableEntryTMCD *
3870 atom_trak_add_timecode_entry (AtomTRAK * trak, AtomsContext * context,
3871 guint32 trak_timescale, GstVideoTimeCode * tc)
3873 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3874 SampleTableEntryTMCD *tmcd = sample_entry_tmcd_new ();
3876 g_assert (trak_timescale != 0);
3878 trak->mdia.hdlr.component_type = FOURCC_mhlr;
3879 trak->mdia.hdlr.handler_type = FOURCC_tmcd;
3880 g_free (trak->mdia.hdlr.name);
3881 trak->mdia.hdlr.name = g_strdup ("Time Code Media Handler");
3882 trak->mdia.mdhd.time_info.timescale = trak_timescale;
3884 tmcd->se.kind = TIMECODE;
3885 tmcd->se.data_reference_index = 1;
3886 tmcd->tc_flags = TC_24H_MAX;
3887 if (tc->config.flags &= GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME)
3888 tmcd->tc_flags |= TC_DROP_FRAME;
3889 tmcd->name.language_code = 0;
3890 tmcd->name.name = g_strdup ("Tape");
3891 tmcd->timescale = trak_timescale;
3892 tmcd->frame_duration =
3893 gst_util_uint64_scale (tmcd->timescale, tc->config.fps_d,
3895 if (tc->config.fps_d == 1001)
3896 tmcd->n_frames = tc->config.fps_n / 1000;
3898 tmcd->n_frames = tc->config.fps_n / tc->config.fps_d;
3900 stsd->entries = g_list_prepend (stsd->entries, tmcd);
3905 static SampleTableEntryMP4V *
3906 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
3909 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
3910 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3912 mp4v->se.header.type = type;
3913 mp4v->se.kind = VIDEO;
3914 mp4v->se.data_reference_index = 1;
3915 mp4v->horizontal_resolution = 72 << 16;
3916 mp4v->vertical_resolution = 72 << 16;
3917 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3918 mp4v->spatial_quality = 512;
3919 mp4v->temporal_quality = 512;
3922 stsd->entries = g_list_prepend (stsd->entries, mp4v);
3927 static SampleTableEntryTX3G *
3928 atom_trak_add_subtitle_entry (AtomTRAK * trak, AtomsContext * context,
3931 SampleTableEntryTX3G *tx3g = sample_entry_tx3g_new ();
3932 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3934 tx3g->se.header.type = type;
3935 tx3g->se.kind = SUBTITLE;
3936 tx3g->se.data_reference_index = 1;
3938 stsd->entries = g_list_prepend (stsd->entries, tx3g);
3945 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
3947 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
3951 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
3953 atom_tkhd_set_audio (&trak->tkhd);
3954 atom_mdia_set_audio (&trak->mdia, context);
3958 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
3961 atom_tkhd_set_video (&trak->tkhd, context, width, height);
3962 atom_mdia_set_video (&trak->mdia, context);
3966 atom_trak_set_subtitle (AtomTRAK * trak, AtomsContext * context)
3968 atom_tkhd_set_subtitle (&trak->tkhd, context, 0, 0);
3969 atom_mdia_set_subtitle (&trak->mdia, context);
3973 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
3976 atom_trak_set_audio (trak, context);
3977 trak->mdia.mdhd.time_info.timescale = rate;
3981 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
3982 guint32 rate, guint32 width, guint32 height)
3984 atom_trak_set_video (trak, context, width, height);
3985 trak->mdia.mdhd.time_info.timescale = rate;
3986 trak->tkhd.width = width << 16;
3987 trak->tkhd.height = height << 16;
3991 atom_trak_set_subtitle_commons (AtomTRAK * trak, AtomsContext * context)
3993 atom_trak_set_subtitle (trak, context);
3994 trak->mdia.mdhd.time_info.timescale = 1000;
3996 trak->tkhd.alternate_group = 2; /* same for all subtitles */
3997 trak->tkhd.layer = -1; /* above video (layer 0) */
4001 sample_table_entry_add_ext_atom (SampleTableEntry * ste, AtomInfo * ext)
4003 GList **list = NULL;
4004 if (ste->kind == AUDIO) {
4005 list = &(((SampleTableEntryMP4A *) ste)->extension_atoms);
4006 } else if (ste->kind == VIDEO) {
4007 list = &(((SampleTableEntryMP4V *) ste)->extension_atoms);
4009 g_assert_not_reached ();
4013 *list = g_list_prepend (*list, ext);
4016 SampleTableEntryMP4A *
4017 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
4018 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
4020 SampleTableEntryMP4A *ste;
4022 atom_trak_set_audio_commons (trak, context, scale);
4023 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4024 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
4026 trak->is_video = FALSE;
4027 trak->is_h264 = FALSE;
4029 ste->version = entry->version;
4030 ste->compression_id = entry->compression_id;
4031 ste->sample_size = entry->sample_size;
4032 ste->sample_rate = entry->sample_rate << 16;
4033 ste->channels = entry->channels;
4035 ste->samples_per_packet = entry->samples_per_packet;
4036 ste->bytes_per_sample = entry->bytes_per_sample;
4037 ste->bytes_per_packet = entry->bytes_per_packet;
4038 ste->bytes_per_frame = entry->bytes_per_frame;
4041 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
4043 /* 0 size means variable size */
4044 atom_trak_set_constant_size_samples (trak, sample_size);
4049 SampleTableEntryTMCD *
4050 atom_trak_set_timecode_type (AtomTRAK * trak, AtomsContext * context,
4051 guint32 trak_timescale, GstVideoTimeCode * tc)
4053 SampleTableEntryTMCD *ste;
4054 AtomGMHD *gmhd = trak->mdia.minf.gmhd;
4056 if (context->flavor != ATOMS_TREE_FLAVOR_MOV) {
4060 ste = atom_trak_add_timecode_entry (trak, context, trak_timescale, tc);
4062 gmhd = atom_gmhd_new ();
4063 gmhd->gmin.graphics_mode = 0x0040;
4064 gmhd->gmin.opcolor[0] = 0x8000;
4065 gmhd->gmin.opcolor[1] = 0x8000;
4066 gmhd->gmin.opcolor[2] = 0x8000;
4067 gmhd->tmcd.tcmi.text_size = 12;
4068 gmhd->tmcd.tcmi.font_name = g_strdup ("Chicago"); /* Pascal string */
4070 trak->mdia.minf.gmhd = gmhd;
4071 trak->is_video = FALSE;
4072 trak->is_h264 = FALSE;
4078 build_pasp_extension (gint par_width, gint par_height)
4080 AtomData *atom_data = atom_data_new (FOURCC_pasp);
4083 atom_data_alloc_mem (atom_data, 8);
4084 data = atom_data->data;
4086 /* ihdr = image header box */
4087 GST_WRITE_UINT32_BE (data, par_width);
4088 GST_WRITE_UINT32_BE (data + 4, par_height);
4090 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4095 build_fiel_extension (GstVideoInterlaceMode mode, GstVideoFieldOrder order)
4097 AtomData *atom_data = atom_data_new (FOURCC_fiel);
4102 atom_data_alloc_mem (atom_data, 2);
4103 data = atom_data->data;
4105 if (mode == GST_VIDEO_INTERLACE_MODE_PROGRESSIVE) {
4108 } else if (mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED) {
4110 field_order = order == GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST ? 9 : 14;
4116 GST_WRITE_UINT8 (data, interlace);
4117 GST_WRITE_UINT8 (data + 1, field_order);
4119 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4124 build_colr_extension (const GstVideoColorimetry * colorimetry, gboolean is_mp4)
4126 AtomData *atom_data = atom_data_new (FOURCC_colr);
4129 guint16 transfer_function;
4132 switch (colorimetry->primaries) {
4133 case GST_VIDEO_COLOR_PRIMARIES_BT709:
4136 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
4139 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
4140 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
4143 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
4146 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
4152 switch (colorimetry->transfer) {
4153 case GST_VIDEO_TRANSFER_BT709:
4154 transfer_function = 1;
4156 case GST_VIDEO_TRANSFER_SMPTE240M:
4157 transfer_function = 7;
4159 case GST_VIDEO_TRANSFER_UNKNOWN:
4161 transfer_function = 2;
4165 switch (colorimetry->matrix) {
4166 case GST_VIDEO_COLOR_MATRIX_BT709:
4169 case GST_VIDEO_COLOR_MATRIX_BT601:
4172 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
4175 case GST_VIDEO_COLOR_MATRIX_BT2020:
4178 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
4184 atom_data_alloc_mem (atom_data, 10 + (is_mp4 ? 1 : 0));
4185 data = atom_data->data;
4187 /* colour specification box */
4189 GST_WRITE_UINT32_LE (data, FOURCC_nclx);
4191 GST_WRITE_UINT32_LE (data, FOURCC_nclc);
4193 GST_WRITE_UINT16_BE (data + 4, primaries);
4194 GST_WRITE_UINT16_BE (data + 6, transfer_function);
4195 GST_WRITE_UINT16_BE (data + 8, matrix);
4198 GST_WRITE_UINT8 (data + 10,
4199 colorimetry->range == GST_VIDEO_COLOR_RANGE_0_255 ? 0x80 : 0x00);
4202 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4207 build_clap_extension (gint width_n, gint width_d, gint height_n, gint height_d,
4208 gint h_off_n, gint h_off_d, gint v_off_n, gint v_off_d)
4210 AtomData *atom_data = atom_data_new (FOURCC_clap);
4213 atom_data_alloc_mem (atom_data, 32);
4214 data = atom_data->data;
4216 GST_WRITE_UINT32_BE (data, width_n);
4217 GST_WRITE_UINT32_BE (data + 4, width_d);
4218 GST_WRITE_UINT32_BE (data + 8, height_n);
4219 GST_WRITE_UINT32_BE (data + 12, height_d);
4220 GST_WRITE_UINT32_BE (data + 16, h_off_n);
4221 GST_WRITE_UINT32_BE (data + 20, h_off_d);
4222 GST_WRITE_UINT32_BE (data + 24, v_off_n);
4223 GST_WRITE_UINT32_BE (data + 28, v_off_d);
4225 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4230 build_tapt_extension (gint clef_width, gint clef_height, gint prof_width,
4231 gint prof_height, gint enof_width, gint enof_height)
4233 AtomData *atom_data = atom_data_new (FOURCC_tapt);
4236 atom_data_alloc_mem (atom_data, 60);
4237 data = atom_data->data;
4239 GST_WRITE_UINT32_BE (data, 20);
4240 GST_WRITE_UINT32_LE (data + 4, FOURCC_clef);
4241 GST_WRITE_UINT32_BE (data + 8, 0);
4242 GST_WRITE_UINT32_BE (data + 12, clef_width);
4243 GST_WRITE_UINT32_BE (data + 16, clef_height);
4245 GST_WRITE_UINT32_BE (data + 20, 20);
4246 GST_WRITE_UINT32_LE (data + 24, FOURCC_prof);
4247 GST_WRITE_UINT32_BE (data + 28, 0);
4248 GST_WRITE_UINT32_BE (data + 32, prof_width);
4249 GST_WRITE_UINT32_BE (data + 36, prof_height);
4251 GST_WRITE_UINT32_BE (data + 40, 20);
4252 GST_WRITE_UINT32_LE (data + 44, FOURCC_enof);
4253 GST_WRITE_UINT32_BE (data + 48, 0);
4254 GST_WRITE_UINT32_BE (data + 52, enof_width);
4255 GST_WRITE_UINT32_BE (data + 56, enof_height);
4258 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4263 build_mov_video_sample_description_padding_extension (void)
4265 AtomData *atom_data = atom_data_new (FOURCC_clap);
4267 return build_atom_info_wrapper ((Atom *) atom_data, atom_copy_empty,
4271 SampleTableEntryMP4V *
4272 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
4273 VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
4275 SampleTableEntryMP4V *ste;
4276 guint dwidth, dheight;
4277 gint par_n = 0, par_d = 0;
4279 par_n = entry->par_n;
4280 par_d = entry->par_d;
4282 dwidth = entry->width;
4283 dheight = entry->height;
4284 /* ISO file spec says track header w/h indicates track's visual presentation
4285 * (so this together with pixels w/h implicitly defines PAR) */
4286 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
4287 /* Assumes square pixels display */
4288 if (!gst_video_calculate_display_ratio (&dwidth, &dheight, entry->width,
4289 entry->height, par_n, par_d, 1, 1)) {
4290 GST_WARNING ("Could not calculate display ratio");
4294 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
4295 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4296 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
4298 trak->is_video = TRUE;
4299 trak->is_h264 = (entry->fourcc == FOURCC_avc1
4300 || entry->fourcc == FOURCC_avc3);
4302 ste->version = entry->version;
4303 ste->width = entry->width;
4304 ste->height = entry->height;
4305 ste->depth = entry->depth;
4306 ste->color_table_id = entry->color_table_id;
4307 ste->frame_count = entry->frame_count;
4310 ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
4312 ste->extension_atoms = g_list_append (ste->extension_atoms,
4313 build_pasp_extension (par_n, par_d));
4315 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
4316 /* append 0 as a terminator "length" to work around some broken software */
4317 ste->extension_atoms =
4318 g_list_append (ste->extension_atoms,
4319 build_mov_video_sample_description_padding_extension ());
4326 subtitle_sample_entry_init (SubtitleSampleEntry * entry)
4328 entry->font_size = 0;
4329 entry->font_face = 0;
4330 entry->foreground_color_rgba = 0xFFFFFFFF; /* all white, opaque */
4333 SampleTableEntryTX3G *
4334 atom_trak_set_subtitle_type (AtomTRAK * trak, AtomsContext * context,
4335 SubtitleSampleEntry * entry)
4337 SampleTableEntryTX3G *tx3g;
4339 atom_trak_set_subtitle_commons (trak, context);
4340 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4341 tx3g = atom_trak_add_subtitle_entry (trak, context, entry->fourcc);
4343 tx3g->font_face = entry->font_face;
4344 tx3g->font_size = entry->font_size;
4345 tx3g->foreground_color_rgba = entry->foreground_color_rgba;
4347 trak->is_video = FALSE;
4348 trak->is_h264 = FALSE;
4354 atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
4356 guint8 flags[3] = { 0, 0, 0 };
4358 atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
4359 mfhd->sequence_number = sequence_number;
4363 atom_moof_init (AtomMOOF * moof, AtomsContext * context,
4364 guint32 sequence_number)
4366 atom_header_set (&moof->header, FOURCC_moof, 0, 0);
4367 atom_mfhd_init (&moof->mfhd, sequence_number);
4372 atom_moof_new (AtomsContext * context, guint32 sequence_number)
4374 AtomMOOF *moof = g_new0 (AtomMOOF, 1);
4376 atom_moof_init (moof, context, sequence_number);
4381 atom_trun_free (AtomTRUN * trun)
4383 atom_full_clear (&trun->header);
4384 atom_array_clear (&trun->entries);
4389 atom_sdtp_free (AtomSDTP * sdtp)
4391 atom_full_clear (&sdtp->header);
4392 atom_array_clear (&sdtp->entries);
4397 atom_traf_free (AtomTRAF * traf)
4401 walker = traf->truns;
4403 atom_trun_free ((AtomTRUN *) walker->data);
4404 walker = g_list_next (walker);
4406 g_list_free (traf->truns);
4409 walker = traf->sdtps;
4411 atom_sdtp_free ((AtomSDTP *) walker->data);
4412 walker = g_list_next (walker);
4414 g_list_free (traf->sdtps);
4421 atom_moof_free (AtomMOOF * moof)
4425 walker = moof->trafs;
4427 atom_traf_free ((AtomTRAF *) walker->data);
4428 walker = g_list_next (walker);
4430 g_list_free (moof->trafs);
4437 atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
4440 guint64 original_offset = *offset;
4442 if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
4446 prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
4448 atom_write_size (buffer, size, offset, original_offset);
4449 return *offset - original_offset;
4453 atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
4456 guint64 original_offset = *offset;
4459 if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
4463 prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
4465 flags = atom_full_get_flags_as_uint (&tfhd->header);
4467 if (flags & TF_BASE_DATA_OFFSET)
4468 prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
4469 if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
4470 prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
4471 if (flags & TF_DEFAULT_SAMPLE_DURATION)
4472 prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
4473 if (flags & TF_DEFAULT_SAMPLE_SIZE)
4474 prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
4475 if (flags & TF_DEFAULT_SAMPLE_FLAGS)
4476 prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
4478 atom_write_size (buffer, size, offset, original_offset);
4479 return *offset - original_offset;
4483 atom_tfdt_copy_data (AtomTFDT * tfdt, guint8 ** buffer, guint64 * size,
4486 guint64 original_offset = *offset;
4488 if (!atom_full_copy_data (&tfdt->header, buffer, size, offset)) {
4492 /* 32-bit time if version == 0 else 64-bit: */
4493 if (tfdt->header.version == 0)
4494 prop_copy_uint32 (tfdt->base_media_decode_time, buffer, size, offset);
4496 prop_copy_uint64 (tfdt->base_media_decode_time, buffer, size, offset);
4498 atom_write_size (buffer, size, offset, original_offset);
4499 return *offset - original_offset;
4503 atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
4504 guint64 * offset, guint32 * data_offset)
4506 guint64 original_offset = *offset;
4509 flags = atom_full_get_flags_as_uint (&trun->header);
4511 /* if first trun in moof, forcibly add data_offset and record
4512 * where it must be written later on */
4513 if (data_offset && !*data_offset) {
4514 flags |= TR_DATA_OFFSET;
4516 flags &= ~TR_DATA_OFFSET;
4519 atom_full_set_flags_as_uint (&trun->header, flags);
4521 if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
4525 prop_copy_uint32 (trun->sample_count, buffer, size, offset);
4527 if (flags & TR_DATA_OFFSET) {
4528 *data_offset = *offset;
4529 prop_copy_int32 (trun->data_offset, buffer, size, offset);
4531 if (flags & TR_FIRST_SAMPLE_FLAGS)
4532 prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
4534 for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
4535 TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
4537 if (flags & TR_SAMPLE_DURATION)
4538 prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
4539 if (flags & TR_SAMPLE_SIZE)
4540 prop_copy_uint32 (entry->sample_size, buffer, size, offset);
4541 if (flags & TR_SAMPLE_FLAGS)
4542 prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
4543 if (flags & TR_COMPOSITION_TIME_OFFSETS)
4544 prop_copy_uint32 (entry->sample_composition_time_offset,
4545 buffer, size, offset);
4548 atom_write_size (buffer, size, offset, original_offset);
4549 return *offset - original_offset;
4553 atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
4556 guint64 original_offset = *offset;
4558 if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
4562 /* all entries at once */
4563 prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
4564 atom_array_get_len (&sdtp->entries), buffer, size, offset);
4566 atom_write_size (buffer, size, offset, original_offset);
4567 return *offset - original_offset;
4571 atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
4572 guint64 * offset, guint32 * data_offset)
4574 guint64 original_offset = *offset;
4577 if (!atom_copy_data (&traf->header, buffer, size, offset)) {
4580 if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
4583 if (!atom_tfdt_copy_data (&traf->tfdt, buffer, size, offset)) {
4587 walker = g_list_first (traf->truns);
4588 while (walker != NULL) {
4589 if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset,
4593 walker = g_list_next (walker);
4596 walker = g_list_first (traf->sdtps);
4597 while (walker != NULL) {
4598 if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
4601 walker = g_list_next (walker);
4604 atom_write_size (buffer, size, offset, original_offset);
4605 return *offset - original_offset;
4608 /* creates moof atom; metadata is written expecting actual buffer data
4609 * is in mdata directly after moof, and is consecutively written per trak */
4611 atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
4612 guint64 * size, guint64 * offset)
4614 guint64 original_offset = *offset;
4616 guint32 data_offset = 0;
4618 if (!atom_copy_data (&moof->header, buffer, size, offset))
4621 if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
4624 walker = g_list_first (moof->trafs);
4625 while (walker != NULL) {
4626 if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset,
4630 walker = g_list_next (walker);
4633 atom_write_size (buffer, size, offset, original_offset);
4635 if (*buffer && data_offset) {
4636 /* first trun needs a data-offset relative to moof start
4637 * = moof size + mdat prefix */
4638 GST_WRITE_UINT32_BE (*buffer + data_offset, *offset - original_offset + 8);
4641 return *offset - original_offset;
4645 atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
4647 guint8 flags[3] = { 0, 0, 0 };
4649 atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
4650 tfhd->track_ID = track_ID;
4651 tfhd->base_data_offset = 0;
4652 tfhd->sample_description_index = 1;
4653 tfhd->default_sample_duration = 0;
4654 tfhd->default_sample_size = 0;
4655 tfhd->default_sample_flags = 0;
4659 atom_tfdt_init (AtomTFDT * tfdt)
4661 guint8 flags[3] = { 0, 0, 0 };
4662 atom_full_init (&tfdt->header, FOURCC_tfdt, 0, 0, 0, flags);
4664 tfdt->base_media_decode_time = 0;
4668 atom_trun_init (AtomTRUN * trun)
4670 guint8 flags[3] = { 0, 0, 0 };
4672 atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
4673 trun->sample_count = 0;
4674 trun->data_offset = 0;
4675 trun->first_sample_flags = 0;
4676 atom_array_init (&trun->entries, 512);
4680 atom_trun_new (void)
4682 AtomTRUN *trun = g_new0 (AtomTRUN, 1);
4684 atom_trun_init (trun);
4689 atom_sdtp_init (AtomSDTP * sdtp)
4691 guint8 flags[3] = { 0, 0, 0 };
4693 atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
4694 atom_array_init (&sdtp->entries, 512);
4698 atom_sdtp_new (AtomsContext * context)
4700 AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
4702 atom_sdtp_init (sdtp);
4707 atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
4709 traf->sdtps = g_list_append (traf->sdtps, sdtp);
4713 atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
4715 /* it does not make much/any sense according to specs,
4716 * but that's how MS isml samples seem to do it */
4717 atom_array_append (&sdtp->entries, val, 256);
4721 atom_trun_add_samples (AtomTRUN * trun, guint32 delta, guint32 size,
4722 guint32 flags, gint64 pts_offset)
4724 TRUNSampleEntry nentry;
4726 if (pts_offset != 0)
4727 trun->header.flags[1] |= (TR_COMPOSITION_TIME_OFFSETS >> 8);
4729 nentry.sample_duration = delta;
4730 nentry.sample_size = size;
4731 nentry.sample_flags = flags;
4732 nentry.sample_composition_time_offset = pts_offset;
4733 atom_array_append (&trun->entries, nentry, 256);
4734 trun->sample_count++;
4738 atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
4740 atom_header_set (&traf->header, FOURCC_traf, 0, 0);
4741 atom_tfdt_init (&traf->tfdt);
4742 atom_tfhd_init (&traf->tfhd, track_ID);
4745 if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
4746 atom_traf_add_sdtp (traf, atom_sdtp_new (context));
4750 atom_traf_new (AtomsContext * context, guint32 track_ID)
4752 AtomTRAF *traf = g_new0 (AtomTRAF, 1);
4754 atom_traf_init (traf, context, track_ID);
4759 atom_traf_set_base_decode_time (AtomTRAF * traf, guint64 base_decode_time)
4761 traf->tfdt.base_media_decode_time = base_decode_time;
4762 /* If we need to write a 64-bit tfdt, set the atom version */
4763 if (base_decode_time > G_MAXUINT32)
4764 traf->tfdt.header.version = 1;
4768 atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
4770 traf->truns = g_list_append (traf->truns, trun);
4774 atom_traf_add_samples (AtomTRAF * traf, guint32 delta, guint32 size,
4775 gboolean sync, gint64 pts_offset, gboolean sdtp_sync)
4780 /* 0x10000 is sample-is-difference-sample flag
4781 * low byte stuff is what ismv uses */
4782 flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
4784 if (G_UNLIKELY (!traf->truns)) {
4785 trun = atom_trun_new ();
4786 atom_traf_add_trun (traf, trun);
4787 /* optimistic; indicate all defaults present in tfhd */
4788 traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
4789 TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
4790 traf->tfhd.default_sample_duration = delta;
4791 traf->tfhd.default_sample_size = size;
4792 traf->tfhd.default_sample_flags = flags;
4793 trun->first_sample_flags = flags;
4796 trun = traf->truns->data;
4798 /* check if still matching defaults,
4799 * if not, abandon default and need entry for each sample */
4800 if (traf->tfhd.default_sample_duration != delta) {
4801 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
4802 trun->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
4804 if (traf->tfhd.default_sample_size != size) {
4805 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
4806 trun->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
4808 if (traf->tfhd.default_sample_flags != flags) {
4809 if (trun->sample_count == 1) {
4810 /* at least will need first sample flag */
4811 traf->tfhd.default_sample_flags = flags;
4812 trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
4814 /* now we need sample flags for each sample */
4815 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
4816 trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
4817 trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
4821 atom_trun_add_samples (traf->truns->data, delta, size, flags, pts_offset);
4824 atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
4828 atom_traf_get_sample_num (AtomTRAF * traf)
4832 if (G_UNLIKELY (!traf->truns))
4835 trun = traf->truns->data;
4836 return atom_array_get_len (&trun->entries);
4840 atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
4842 moof->trafs = g_list_append (moof->trafs, traf);
4846 atom_tfra_free (AtomTFRA * tfra)
4848 atom_full_clear (&tfra->header);
4849 atom_array_clear (&tfra->entries);
4854 atom_mfra_new (AtomsContext * context)
4856 AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
4858 atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
4863 atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
4865 mfra->tfras = g_list_append (mfra->tfras, tfra);
4869 atom_mfra_free (AtomMFRA * mfra)
4873 walker = mfra->tfras;
4875 atom_tfra_free ((AtomTFRA *) walker->data);
4876 walker = g_list_next (walker);
4878 g_list_free (mfra->tfras);
4881 atom_clear (&mfra->header);
4886 atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
4888 guint8 flags[3] = { 0, 0, 0 };
4890 atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
4891 tfra->track_ID = track_ID;
4892 atom_array_init (&tfra->entries, 512);
4896 atom_tfra_new (AtomsContext * context, guint32 track_ID)
4898 AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
4900 atom_tfra_init (tfra, track_ID);
4906 need_bytes (guint32 num)
4917 atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
4923 entry.moof_offset = 0;
4924 /* always write a single trun in a single traf */
4925 entry.traf_number = 1;
4926 entry.trun_number = 1;
4927 entry.sample_number = sample_num;
4929 /* auto-use 64 bits if needed */
4930 if (dts > G_MAXUINT32)
4931 tfra->header.version = 1;
4933 /* 1 byte will always do for traf and trun number,
4934 * check how much sample_num needs */
4935 tfra->lengths = (tfra->lengths & 0xfc) ||
4936 MAX (tfra->lengths, need_bytes (sample_num));
4938 atom_array_append (&tfra->entries, entry, 256);
4942 atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
4946 /* auto-use 64 bits if needed */
4947 if (offset > G_MAXUINT32)
4948 tfra->header.version = 1;
4950 for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
4951 TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
4953 if (entry->moof_offset)
4955 entry->moof_offset = offset;
4960 atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
4963 guint64 original_offset = *offset;
4970 if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
4974 prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
4975 prop_copy_uint32 (tfra->lengths, buffer, size, offset);
4976 prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
4978 version = tfra->header.version;
4979 for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
4980 entry = &atom_array_index (&tfra->entries, i);
4982 prop_copy_uint64 (entry->time, buffer, size, offset);
4983 prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
4985 prop_copy_uint32 (entry->time, buffer, size, offset);
4986 prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
4989 bytes = (tfra->lengths & (0x3 << 4)) + 1;
4990 data = GUINT32_TO_BE (entry->traf_number);
4991 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
4992 buffer, size, offset);
4994 bytes = (tfra->lengths & (0x3 << 2)) + 1;
4995 data = GUINT32_TO_BE (entry->trun_number);
4996 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
4997 buffer, size, offset);
4999 bytes = (tfra->lengths & (0x3)) + 1;
5000 data = GUINT32_TO_BE (entry->sample_number);
5001 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5002 buffer, size, offset);
5006 atom_write_size (buffer, size, offset, original_offset);
5007 return *offset - original_offset;
5011 atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
5014 guint64 original_offset = *offset;
5015 guint8 flags[3] = { 0, 0, 0 };
5018 atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
5020 if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
5024 prop_copy_uint32 (s, buffer, size, offset);
5026 atom_write_size (buffer, size, offset, original_offset);
5028 return *offset - original_offset;
5033 atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
5036 guint64 original_offset = *offset;
5039 if (!atom_copy_data (&mfra->header, buffer, size, offset))
5042 walker = g_list_first (mfra->tfras);
5043 while (walker != NULL) {
5044 if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
5047 walker = g_list_next (walker);
5050 /* 16 is the size of the mfro atom */
5051 if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
5055 atom_write_size (buffer, size, offset, original_offset);
5056 return *offset - original_offset;
5059 /* some sample description construction helpers */
5062 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
5063 const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
5068 track_id = trak->tkhd.track_ID;
5070 esds = atom_esds_new ();
5071 esds->es.id = track_id & 0xFFFF;
5072 esds->es.dec_conf_desc.object_type = object_type;
5073 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
5075 if (avg_bitrate > 0)
5076 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
5077 if (max_bitrate > 0)
5078 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
5080 /* optional DecoderSpecificInfo */
5082 DecoderSpecificInfoDescriptor *desc;
5085 esds->es.dec_conf_desc.dec_specific_info = desc =
5086 desc_dec_specific_info_new ();
5087 size = gst_buffer_get_size ((GstBuffer *) codec_data);
5088 desc_dec_specific_info_alloc_data (desc, size);
5089 gst_buffer_extract ((GstBuffer *) codec_data, 0, desc->data, size);
5092 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
5097 build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
5098 guint32 max_bitrate)
5100 AtomData *atom_data = atom_data_new (FOURCC_btrt);
5103 atom_data_alloc_mem (atom_data, 12);
5104 data = atom_data->data;
5106 GST_WRITE_UINT32_BE (data, buffer_size_db);
5107 GST_WRITE_UINT32_BE (data + 4, max_bitrate);
5108 GST_WRITE_UINT32_BE (data + 8, avg_bitrate);
5110 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5115 build_mov_wave_extension (guint32 fourcc, AtomInfo * atom1, AtomInfo * atom2,
5116 gboolean terminator)
5122 /* Build WAVE atom for sample table entry */
5123 wave = atom_wave_new ();
5125 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5127 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5128 wave->extension_atoms =
5129 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5130 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5133 /* Add supplied atoms to WAVE */
5135 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
5137 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
5139 /* Add FRMA to the WAVE */
5140 frma = atom_frma_new ();
5141 frma->media_type = fourcc;
5143 wave->extension_atoms =
5144 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5145 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5147 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5152 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
5153 guint32 avg_bitrate, guint32 max_bitrate)
5155 AtomInfo *esds, *mp4a;
5159 /* Add ESDS atom to WAVE */
5160 esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
5161 ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
5163 /* Add MP4A atom to the WAVE:
5164 * not really in spec, but makes offset based players happy */
5165 buf = GST_BUFFER_NEW_READONLY (&tmp, 4);
5166 mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
5167 gst_buffer_unref (buf);
5169 return build_mov_wave_extension (FOURCC_mp4a, mp4a, esds, TRUE);
5173 build_mov_alac_extension (const GstBuffer * codec_data)
5177 alac = build_codec_data_extension (FOURCC_alac, codec_data);
5179 return build_mov_wave_extension (FOURCC_alac, NULL, alac, TRUE);
5183 build_jp2x_extension (const GstBuffer * prefix)
5185 AtomData *atom_data;
5191 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2x, prefix);
5193 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5198 build_jp2h_extension (gint width, gint height, const gchar * colorspace,
5199 gint ncomp, const GValue * cmap_array, const GValue * cdef_array)
5201 AtomData *atom_data;
5205 gint idhr_size = 22;
5206 gint colr_size = 15;
5207 gint cmap_size = 0, cdef_size = 0;
5208 gint cmap_array_size = 0;
5209 gint cdef_array_size = 0;
5210 GstByteWriter writer;
5212 g_return_val_if_fail (cmap_array == NULL ||
5213 GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
5214 g_return_val_if_fail (cdef_array == NULL ||
5215 GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
5217 if (g_str_equal (colorspace, "sRGB")) {
5221 } else if (g_str_equal (colorspace, "GRAY")) {
5225 } else if (g_str_equal (colorspace, "sYUV")) {
5233 cmap_array_size = gst_value_array_get_size (cmap_array);
5234 cmap_size = 8 + cmap_array_size * 4;
5237 cdef_array_size = gst_value_array_get_size (cdef_array);
5238 cdef_size = 8 + 2 + cdef_array_size * 6;
5241 gst_byte_writer_init_with_size (&writer,
5242 idhr_size + colr_size + cmap_size + cdef_size, TRUE);
5244 /* ihdr = image header box */
5245 gst_byte_writer_put_uint32_be_unchecked (&writer, 22);
5246 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_ihdr);
5247 gst_byte_writer_put_uint32_be_unchecked (&writer, height);
5248 gst_byte_writer_put_uint32_be_unchecked (&writer, width);
5249 gst_byte_writer_put_uint16_be_unchecked (&writer, ncomp);
5250 /* 8 bits per component, unsigned */
5251 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5252 /* compression type; reserved */
5253 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5254 /* colour space (un)known */
5255 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5256 /* intellectual property right (box present) */
5257 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5259 /* colour specification box */
5260 gst_byte_writer_put_uint32_be_unchecked (&writer, 15);
5261 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_colr);
5263 /* specification method: enumerated */
5264 gst_byte_writer_put_uint8_unchecked (&writer, 0x1);
5265 /* precedence; reserved */
5266 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5267 /* approximation; reserved */
5268 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5269 /* enumerated colourspace */
5270 gst_byte_writer_put_uint32_be_unchecked (&writer, cenum);
5273 gst_byte_writer_put_uint32_be_unchecked (&writer, cmap_size);
5274 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cmap);
5275 for (i = 0; i < cmap_array_size; i++) {
5281 item = gst_value_array_get_value (cmap_array, i);
5282 value = g_value_get_int (item);
5284 /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
5285 cmp = value & 0xFFFF;
5287 pcol = (value >> 16) & 0xFF;
5290 GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
5291 "handle Pallete mapping atoms yet");
5293 gst_byte_writer_put_uint16_be_unchecked (&writer, cmp);
5294 gst_byte_writer_put_uint8_unchecked (&writer, mtyp);
5295 gst_byte_writer_put_uint8_unchecked (&writer, pcol);
5300 gst_byte_writer_put_uint32_be_unchecked (&writer, cdef_size);
5301 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cdef);
5302 gst_byte_writer_put_uint16_be_unchecked (&writer, cdef_array_size);
5303 for (i = 0; i < cdef_array_size; i++) {
5306 item = gst_value_array_get_value (cdef_array, i);
5307 value = g_value_get_int (item);
5309 gst_byte_writer_put_uint16_be_unchecked (&writer, i);
5311 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5312 gst_byte_writer_put_uint16_be_unchecked (&writer, value);
5313 } else if (value < 0) {
5314 gst_byte_writer_put_uint16_be_unchecked (&writer, -value);
5315 gst_byte_writer_put_uint16_be_unchecked (&writer, 0); /* TODO what here? */
5317 gst_byte_writer_put_uint16_be_unchecked (&writer, 1);
5318 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5323 g_assert (gst_byte_writer_get_remaining (&writer) == 0);
5324 buf = gst_byte_writer_reset_and_get_buffer (&writer);
5326 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
5327 gst_buffer_unref (buf);
5329 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5334 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
5337 AtomInfo *result = NULL;
5340 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
5341 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
5349 build_amr_extension (void)
5356 GST_WRITE_UINT32_LE (ext, 0);
5357 /* decoder version */
5358 GST_WRITE_UINT8 (ext + 4, 0);
5359 /* mode set (all modes) */
5360 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
5361 /* mode change period (no restriction) */
5362 GST_WRITE_UINT8 (ext + 7, 0);
5363 /* frames per sample */
5364 GST_WRITE_UINT8 (ext + 8, 1);
5366 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5367 res = build_codec_data_extension (FOURCC_damr, buf);
5368 gst_buffer_unref (buf);
5373 build_h263_extension (void)
5380 GST_WRITE_UINT32_LE (ext, 0);
5381 /* decoder version */
5382 GST_WRITE_UINT8 (ext + 4, 0);
5383 /* level / profile */
5384 /* FIXME ? maybe ? obtain somewhere; baseline for now */
5385 GST_WRITE_UINT8 (ext + 5, 10);
5386 GST_WRITE_UINT8 (ext + 6, 0);
5388 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5389 res = build_codec_data_extension (FOURCC_d263, buf);
5390 gst_buffer_unref (buf);
5395 build_gama_atom (gdouble gamma)
5401 /* convert to uint32 from fixed point */
5402 gamma_fp = (guint32) 65536 *gamma;
5404 gamma_fp = GUINT32_TO_BE (gamma_fp);
5405 buf = GST_BUFFER_NEW_READONLY (&gamma_fp, 4);
5406 res = build_codec_data_extension (FOURCC_gama, buf);
5407 gst_buffer_unref (buf);
5412 build_SMI_atom (const GstBuffer * seqh)
5419 /* the seqh plus its size and fourcc */
5420 size = gst_buffer_get_size ((GstBuffer *) seqh);
5421 data = g_malloc (size + 8);
5423 GST_WRITE_UINT32_LE (data, FOURCC_SEQH);
5424 GST_WRITE_UINT32_BE (data + 4, size + 8);
5425 gst_buffer_extract ((GstBuffer *) seqh, 0, data + 8, size);
5426 buf = gst_buffer_new_wrapped (data, size + 8);
5427 res = build_codec_data_extension (FOURCC_SMI_, buf);
5428 gst_buffer_unref (buf);
5433 build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
5435 #define IMA_ADPCM_ATOM_SIZE 20
5436 AtomData *atom_data;
5439 gint samplesperblock;
5442 /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
5443 identifier. Note that the identifier here is big-endian, but when used
5444 within the WAVE header (below), it's little endian. */
5445 fourcc = MS_WAVE_FOURCC (0x11);
5447 atom_data = atom_data_new (fourcc);
5448 atom_data_alloc_mem (atom_data, IMA_ADPCM_ATOM_SIZE);
5449 data = atom_data->data;
5451 /* This atom's content is a WAVE header, including 2 bytes of extra data.
5452 Note that all of this is little-endian, unlike most stuff in qt. */
5453 /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
5454 for the rest. Simplifies to this. */
5455 samplesperblock = 2 * blocksize / channels - 7;
5456 bytespersec = rate * blocksize / samplesperblock;
5457 GST_WRITE_UINT16_LE (data, 0x11);
5458 GST_WRITE_UINT16_LE (data + 2, channels);
5459 GST_WRITE_UINT32_LE (data + 4, rate);
5460 GST_WRITE_UINT32_LE (data + 8, bytespersec);
5461 GST_WRITE_UINT16_LE (data + 12, blocksize);
5462 GST_WRITE_UINT16_LE (data + 14, 4);
5463 GST_WRITE_UINT16_LE (data + 16, 2); /* Two extra bytes */
5464 GST_WRITE_UINT16_LE (data + 18, samplesperblock);
5466 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5471 build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
5478 wave = atom_wave_new ();
5480 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5481 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5482 wave->extension_atoms =
5483 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5484 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5486 /* Add wave ima adpcm atom to WAVE */
5487 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
5488 build_ima_adpcm_atom (channels, rate, blocksize));
5490 /* Add FRMA to the WAVE */
5491 frma = atom_frma_new ();
5492 frma->media_type = MS_WAVE_FOURCC (0x11);
5494 wave->extension_atoms =
5495 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5496 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5498 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5503 build_ac3_extension (guint8 fscod, guint8 bsid, guint8 bsmod, guint8 acmod,
5504 guint8 lfe_on, guint8 bitrate_code)
5506 AtomData *atom_data = atom_data_new (FOURCC_dac3);
5509 atom_data_alloc_mem (atom_data, 3);
5510 data = atom_data->data;
5512 /* Bits from the spec
5522 /* Some bit manipulation magic. Need bitwriter */
5523 data[0] = (fscod << 6) | (bsid << 1) | ((bsmod >> 2) & 1);
5525 ((bsmod & 0x3) << 6) | (acmod << 3) | ((lfe_on & 1) << 2) | ((bitrate_code
5527 data[2] = ((bitrate_code & 0x7) << 5);
5529 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5534 build_opus_extension (guint32 rate, guint8 channels, guint8 mapping_family,
5535 guint8 stream_count, guint8 coupled_count, guint8 channel_mapping[256],
5536 guint16 pre_skip, guint16 output_gain)
5538 AtomData *atom_data;
5541 gboolean hdl = TRUE;
5542 guint data_block_len;
5544 gst_byte_writer_init (&bw);
5545 hdl &= gst_byte_writer_put_uint8 (&bw, 0x00); /* version number */
5546 hdl &= gst_byte_writer_put_uint8 (&bw, channels);
5547 hdl &= gst_byte_writer_put_uint16_le (&bw, pre_skip);
5548 hdl &= gst_byte_writer_put_uint32_le (&bw, rate);
5549 hdl &= gst_byte_writer_put_uint16_le (&bw, output_gain);
5550 hdl &= gst_byte_writer_put_uint8 (&bw, mapping_family);
5551 if (mapping_family > 0) {
5552 hdl &= gst_byte_writer_put_uint8 (&bw, stream_count);
5553 hdl &= gst_byte_writer_put_uint8 (&bw, coupled_count);
5554 hdl &= gst_byte_writer_put_data (&bw, channel_mapping, channels);
5558 GST_WARNING ("Error creating header");
5562 data_block_len = gst_byte_writer_get_size (&bw);
5563 data_block = gst_byte_writer_reset_and_get_data (&bw);
5564 atom_data = atom_data_new_from_data (FOURCC_dops, data_block, data_block_len);
5565 g_free (data_block);
5567 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5572 build_uuid_xmp_atom (GstBuffer * xmp_data)
5576 static const guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
5577 0x97, 0xA9, 0x42, 0xE8,
5578 0x9C, 0x71, 0x99, 0x94,
5579 0x91, 0xE3, 0xAF, 0xAC
5582 if (xmp_data == NULL)
5585 uuid = atom_uuid_new ();
5586 memcpy (uuid->uuid, xmp_uuid, 16);
5588 size = gst_buffer_get_size (xmp_data);
5589 uuid->data = g_malloc (size);
5590 uuid->datalen = size;
5591 gst_buffer_extract (xmp_data, 0, uuid->data, size);
5593 return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,