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;
428 AtomTMCD *tmcd = g_new0 (AtomTMCD, 1);
430 atom_header_set (&tmcd->header, FOURCC_tmcd, 0, 0);
431 atom_tcmi_init (&tmcd->tcmi);
437 atom_tmcd_free (AtomTMCD * tmcd)
439 atom_clear (&tmcd->header);
440 atom_tcmi_clear (&tmcd->tcmi);
445 atom_gmin_init (AtomGMIN * gmin)
447 guint8 flags[3] = { 0, 0, 0 };
449 atom_full_init (&gmin->header, FOURCC_gmin, 0, 0, 0, flags);
453 atom_gmin_clear (AtomGMIN * gmin)
455 atom_full_clear (&gmin->header);
456 gmin->graphics_mode = 0;
457 gmin->opcolor[0] = 0;
458 gmin->opcolor[1] = 0;
459 gmin->opcolor[2] = 0;
465 atom_gmhd_init (AtomGMHD * gmhd)
467 atom_header_set (&gmhd->header, FOURCC_gmhd, 0, 0);
468 atom_gmin_init (&gmhd->gmin);
472 atom_gmhd_clear (AtomGMHD * gmhd)
474 atom_clear (&gmhd->header);
475 atom_gmin_clear (&gmhd->gmin);
477 atom_tmcd_free (gmhd->tmcd);
485 AtomGMHD *gmhd = g_new0 (AtomGMHD, 1);
486 atom_gmhd_init (gmhd);
491 atom_gmhd_free (AtomGMHD * gmhd)
493 atom_gmhd_clear (gmhd);
498 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
500 atom_header_set (&se->header, type, 0, 0);
502 memset (se->reserved, 0, sizeof (guint8) * 6);
503 se->data_reference_index = 0;
507 atom_sample_entry_free (SampleTableEntry * se)
509 atom_clear (&se->header);
513 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
515 atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
518 mp4a->revision_level = 0;
521 mp4a->sample_size = 16;
522 mp4a->compression_id = 0;
523 mp4a->packet_size = 0;
524 mp4a->sample_rate = 0;
525 /* following only used if version is 1 */
526 mp4a->samples_per_packet = 0;
527 mp4a->bytes_per_packet = 0;
528 mp4a->bytes_per_frame = 0;
529 mp4a->bytes_per_sample = 0;
531 mp4a->extension_atoms = NULL;
534 static SampleTableEntryMP4A *
535 sample_entry_mp4a_new (void)
537 SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
539 sample_entry_mp4a_init (mp4a);
544 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
546 atom_sample_entry_free (&mp4a->se);
547 atom_info_list_free (mp4a->extension_atoms);
552 sample_entry_tmcd_init (SampleTableEntryTMCD * tmcd)
554 atom_sample_entry_init (&tmcd->se, FOURCC_tmcd);
558 tmcd->frame_duration = 0;
561 tmcd->name.language_code = 0;
562 g_free (tmcd->name.name);
563 tmcd->name.name = NULL;
566 static SampleTableEntryTMCD *
567 sample_entry_tmcd_new (void)
569 SampleTableEntryTMCD *tmcd = g_new0 (SampleTableEntryTMCD, 1);
571 sample_entry_tmcd_init (tmcd);
576 sample_entry_tmcd_free (SampleTableEntryTMCD * tmcd)
578 atom_sample_entry_free (&tmcd->se);
579 g_free (tmcd->name.name);
584 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
586 atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
589 mp4v->revision_level = 0;
592 mp4v->temporal_quality = 0;
593 mp4v->spatial_quality = 0;
595 /* qt and ISO base media do not contradict, and examples agree */
596 mp4v->horizontal_resolution = 0x00480000;
597 mp4v->vertical_resolution = 0x00480000;
600 mp4v->frame_count = 1;
602 memset (mp4v->compressor, 0, sizeof (guint8) * 32);
605 mp4v->color_table_id = 0;
607 mp4v->extension_atoms = NULL;
611 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
613 atom_sample_entry_free (&mp4v->se);
614 atom_info_list_free (mp4v->extension_atoms);
618 static SampleTableEntryMP4V *
619 sample_entry_mp4v_new (AtomsContext * context)
621 SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
623 sample_entry_mp4v_init (mp4v, context);
628 sample_entry_tx3g_init (SampleTableEntryTX3G * tx3g)
630 atom_sample_entry_init (&tx3g->se, FOURCC_tx3g);
632 tx3g->display_flags = 0;
633 tx3g->font_id = 1; /* must be 1 as there is a single font */
635 tx3g->foreground_color_rgba = 0xFFFFFFFF; /* white, opaque */
637 /* can't set this now */
638 tx3g->default_text_box = 0;
643 sample_entry_tx3g_free (SampleTableEntryTX3G * tx3g)
645 atom_sample_entry_free (&tx3g->se);
649 static SampleTableEntryTX3G *
650 sample_entry_tx3g_new (void)
652 SampleTableEntryTX3G *tx3g = g_new0 (SampleTableEntryTX3G, 1);
654 sample_entry_tx3g_init (tx3g);
660 atom_stsd_init (AtomSTSD * stsd)
662 guint8 flags[3] = { 0, 0, 0 };
664 atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
665 stsd->entries = NULL;
670 atom_stsd_remove_entries (AtomSTSD * stsd)
674 walker = stsd->entries;
677 SampleTableEntry *se = (SampleTableEntry *) aux->data;
679 walker = g_list_next (walker);
680 stsd->entries = g_list_remove_link (stsd->entries, aux);
684 sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
687 sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
690 sample_entry_tx3g_free ((SampleTableEntryTX3G *) se);
693 sample_entry_tmcd_free ((SampleTableEntryTMCD *) se);
697 /* best possible cleanup */
698 atom_sample_entry_free (se);
706 atom_stsd_clear (AtomSTSD * stsd)
708 atom_stsd_remove_entries (stsd);
709 atom_full_clear (&stsd->header);
713 atom_ctts_init (AtomCTTS * ctts)
715 guint8 flags[3] = { 0, 0, 0 };
717 atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
718 atom_array_init (&ctts->entries, 128);
719 ctts->do_pts = FALSE;
725 AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
727 atom_ctts_init (ctts);
732 atom_ctts_free (AtomCTTS * ctts)
734 atom_full_clear (&ctts->header);
735 atom_array_clear (&ctts->entries);
740 atom_svmi_init (AtomSVMI * svmi)
742 guint8 flags[3] = { 0, 0, 0 };
744 atom_full_init (&svmi->header, FOURCC_svmi, 0, 0, 0, flags);
745 svmi->stereoscopic_composition_type = 0x00;
746 svmi->is_left_first = FALSE;
750 atom_svmi_new (guint8 stereoscopic_composition_type, gboolean is_left_first)
752 AtomSVMI *svmi = g_new0 (AtomSVMI, 1);
754 atom_svmi_init (svmi);
755 svmi->stereoscopic_composition_type = stereoscopic_composition_type;
756 svmi->is_left_first = is_left_first;
761 atom_svmi_free (AtomSVMI * svmi)
767 atom_stts_init (AtomSTTS * stts)
769 guint8 flags[3] = { 0, 0, 0 };
771 atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
772 atom_array_init (&stts->entries, 512);
776 atom_stts_clear (AtomSTTS * stts)
778 atom_full_clear (&stts->header);
779 atom_array_clear (&stts->entries);
783 atom_stsz_init (AtomSTSZ * stsz)
785 guint8 flags[3] = { 0, 0, 0 };
787 atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
788 atom_array_init (&stsz->entries, 1024);
789 stsz->sample_size = 0;
790 stsz->table_size = 0;
794 atom_stsz_clear (AtomSTSZ * stsz)
796 atom_full_clear (&stsz->header);
797 atom_array_clear (&stsz->entries);
798 stsz->table_size = 0;
802 atom_stsc_init (AtomSTSC * stsc)
804 guint8 flags[3] = { 0, 0, 0 };
806 atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
807 atom_array_init (&stsc->entries, 128);
811 atom_stsc_clear (AtomSTSC * stsc)
813 atom_full_clear (&stsc->header);
814 atom_array_clear (&stsc->entries);
818 atom_co64_init (AtomSTCO64 * co64)
820 guint8 flags[3] = { 0, 0, 0 };
822 atom_full_init (&co64->header, FOURCC_stco, 0, 0, 0, flags);
823 atom_array_init (&co64->entries, 256);
827 atom_stco64_clear (AtomSTCO64 * stco64)
829 atom_full_clear (&stco64->header);
830 atom_array_clear (&stco64->entries);
834 atom_stss_init (AtomSTSS * stss)
836 guint8 flags[3] = { 0, 0, 0 };
838 atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
839 atom_array_init (&stss->entries, 128);
843 atom_stss_clear (AtomSTSS * stss)
845 atom_full_clear (&stss->header);
846 atom_array_clear (&stss->entries);
850 atom_stbl_init (AtomSTBL * stbl)
852 atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
854 atom_stts_init (&stbl->stts);
855 atom_stss_init (&stbl->stss);
856 atom_stsd_init (&stbl->stsd);
857 atom_stsz_init (&stbl->stsz);
858 atom_stsc_init (&stbl->stsc);
862 atom_co64_init (&stbl->stco64);
866 atom_stbl_clear (AtomSTBL * stbl)
868 atom_clear (&stbl->header);
869 atom_stsd_clear (&stbl->stsd);
870 atom_stts_clear (&stbl->stts);
871 atom_stss_clear (&stbl->stss);
872 atom_stsc_clear (&stbl->stsc);
873 atom_stsz_clear (&stbl->stsz);
875 atom_ctts_free (stbl->ctts);
878 atom_svmi_free (stbl->svmi);
880 atom_stco64_clear (&stbl->stco64);
884 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
886 guint8 flags[3] = { 0, 0, 1 };
888 atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
889 vmhd->graphics_mode = 0x0;
890 memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
892 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
893 vmhd->graphics_mode = 0x40;
894 vmhd->opcolor[0] = 32768;
895 vmhd->opcolor[1] = 32768;
896 vmhd->opcolor[2] = 32768;
901 atom_vmhd_new (AtomsContext * context)
903 AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
905 atom_vmhd_init (vmhd, context);
910 atom_vmhd_free (AtomVMHD * vmhd)
912 atom_full_clear (&vmhd->header);
917 atom_smhd_init (AtomSMHD * smhd)
919 guint8 flags[3] = { 0, 0, 0 };
921 atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
929 AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
931 atom_smhd_init (smhd);
936 atom_smhd_free (AtomSMHD * smhd)
938 atom_full_clear (&smhd->header);
943 atom_hmhd_free (AtomHMHD * hmhd)
945 atom_full_clear (&hmhd->header);
950 atom_hdlr_init (AtomHDLR * hdlr, AtomsContext * context)
952 guint8 flags[3] = { 0, 0, 0 };
954 atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
956 hdlr->component_type = 0;
957 hdlr->handler_type = 0;
958 hdlr->manufacturer = 0;
960 hdlr->flags_mask = 0;
961 hdlr->name = g_strdup ("");
963 /* Store the flavor to know how to serialize the 'name' string */
964 hdlr->flavor = context->flavor;
968 atom_hdlr_new (AtomsContext * context)
970 AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
972 atom_hdlr_init (hdlr, context);
977 atom_hdlr_clear (AtomHDLR * hdlr)
979 atom_full_clear (&hdlr->header);
987 atom_hdlr_free (AtomHDLR * hdlr)
989 atom_hdlr_clear (hdlr);
994 atom_url_init (AtomURL * url)
996 guint8 flags[3] = { 0, 0, 1 };
998 atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
999 url->location = NULL;
1003 atom_url_free (AtomURL * url)
1005 atom_full_clear (&url->header);
1006 if (url->location) {
1007 g_free (url->location);
1008 url->location = NULL;
1016 AtomURL *url = g_new0 (AtomURL, 1);
1018 atom_url_init (url);
1023 atom_alis_new (void)
1025 guint8 flags[3] = { 0, 0, 1 };
1026 AtomFull *alis = g_new0 (AtomFull, 1);
1028 atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
1033 atom_dref_init (AtomDREF * dref, AtomsContext * context)
1035 guint8 flags[3] = { 0, 0, 0 };
1037 atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
1039 /* in either case, alis or url init arranges to set self-contained flag */
1040 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
1041 /* alis dref for qt */
1042 AtomFull *alis = atom_alis_new ();
1043 dref->entries = g_list_append (dref->entries, alis);
1045 /* url for iso spec, as 'alis' not specified there */
1046 AtomURL *url = atom_url_new ();
1047 dref->entries = g_list_append (dref->entries, url);
1052 atom_dref_clear (AtomDREF * dref)
1056 atom_full_clear (&dref->header);
1057 walker = dref->entries;
1059 GList *aux = walker;
1060 Atom *atom = (Atom *) aux->data;
1062 walker = g_list_next (walker);
1063 dref->entries = g_list_remove_link (dref->entries, aux);
1064 switch (atom->type) {
1066 atom_full_free ((AtomFull *) atom);
1069 atom_url_free ((AtomURL *) atom);
1072 /* we do nothing, better leak than crash */
1080 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
1082 atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
1083 atom_dref_init (&dinf->dref, context);
1087 atom_dinf_clear (AtomDINF * dinf)
1089 atom_clear (&dinf->header);
1090 atom_dref_clear (&dinf->dref);
1094 atom_minf_init (AtomMINF * minf, AtomsContext * context)
1096 atom_header_set (&minf->header, FOURCC_minf, 0, 0);
1103 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
1104 minf->hdlr = atom_hdlr_new (context);
1105 minf->hdlr->component_type = FOURCC_dhlr;
1106 minf->hdlr->handler_type = FOURCC_alis;
1110 atom_dinf_init (&minf->dinf, context);
1111 atom_stbl_init (&minf->stbl);
1115 atom_minf_clear_handlers (AtomMINF * minf)
1118 atom_vmhd_free (minf->vmhd);
1122 atom_smhd_free (minf->smhd);
1126 atom_hmhd_free (minf->hmhd);
1130 atom_gmhd_free (minf->gmhd);
1136 atom_minf_clear (AtomMINF * minf)
1138 atom_clear (&minf->header);
1139 atom_minf_clear_handlers (minf);
1141 atom_hdlr_free (minf->hdlr);
1143 atom_dinf_clear (&minf->dinf);
1144 atom_stbl_clear (&minf->stbl);
1148 atom_mdhd_init (AtomMDHD * mdhd)
1150 guint8 flags[3] = { 0, 0, 0 };
1152 atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
1153 common_time_info_init (&mdhd->time_info);
1154 /* tempting as it may be to simply 0-initialize,
1155 * that will have the demuxer (correctly) come up with 'eng' as language
1156 * so explicitly specify undefined instead */
1157 mdhd->language_code = language_code ("und");
1162 atom_mdhd_clear (AtomMDHD * mdhd)
1164 atom_full_clear (&mdhd->header);
1168 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
1170 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
1172 atom_mdhd_init (&mdia->mdhd);
1173 atom_hdlr_init (&mdia->hdlr, context);
1174 atom_minf_init (&mdia->minf, context);
1178 atom_mdia_clear (AtomMDIA * mdia)
1180 atom_clear (&mdia->header);
1181 atom_mdhd_clear (&mdia->mdhd);
1182 atom_hdlr_clear (&mdia->hdlr);
1183 atom_minf_clear (&mdia->minf);
1187 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
1191 * 1 -> track enabled
1192 * 2 -> track in movie
1193 * 4 -> track in preview
1195 guint8 flags[3] = { 0, 0, 7 };
1197 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
1199 tkhd->creation_time = tkhd->modification_time = atoms_get_current_qt_time ();
1204 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
1206 tkhd->alternate_group = 0;
1208 tkhd->reserved3 = 0;
1209 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
1210 tkhd->matrix[0] = 1 << 16;
1211 tkhd->matrix[4] = 1 << 16;
1212 tkhd->matrix[8] = 16384 << 16;
1218 atom_tkhd_clear (AtomTKHD * tkhd)
1220 atom_full_clear (&tkhd->header);
1224 atom_ilst_init (AtomILST * ilst)
1226 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
1227 ilst->entries = NULL;
1231 atom_ilst_new (void)
1233 AtomILST *ilst = g_new0 (AtomILST, 1);
1235 atom_ilst_init (ilst);
1240 atom_ilst_free (AtomILST * ilst)
1243 atom_info_list_free (ilst->entries);
1244 atom_clear (&ilst->header);
1249 atom_meta_init (AtomMETA * meta, AtomsContext * context)
1251 guint8 flags[3] = { 0, 0, 0 };
1253 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1254 atom_hdlr_init (&meta->hdlr, context);
1255 /* FIXME (ISOM says this is always 0) */
1256 meta->hdlr.component_type = FOURCC_mhlr;
1257 meta->hdlr.handler_type = FOURCC_mdir;
1262 atom_meta_new (AtomsContext * context)
1264 AtomMETA *meta = g_new0 (AtomMETA, 1);
1266 atom_meta_init (meta, context);
1271 atom_meta_free (AtomMETA * meta)
1273 atom_full_clear (&meta->header);
1274 atom_hdlr_clear (&meta->hdlr);
1276 atom_ilst_free (meta->ilst);
1282 atom_udta_init_metatags (AtomUDTA * udta, AtomsContext * context)
1284 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
1286 udta->meta = atom_meta_new (context);
1288 if (!udta->meta->ilst) {
1289 udta->meta->ilst = atom_ilst_new ();
1295 atom_udta_init (AtomUDTA * udta, AtomsContext * context)
1297 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1299 udta->context = context;
1301 atom_udta_init_metatags (udta, context);
1305 atom_udta_clear (AtomUDTA * udta)
1307 atom_clear (&udta->header);
1309 atom_meta_free (udta->meta);
1312 atom_info_list_free (udta->entries);
1316 atom_tref_init (AtomTREF * tref, guint32 reftype)
1318 atom_header_set (&tref->header, FOURCC_tref, 0, 0);
1319 tref->reftype = reftype;
1320 atom_array_init (&tref->entries, 128);
1324 atom_tref_clear (AtomTREF * tref)
1326 atom_clear (&tref->header);
1328 atom_array_clear (&tref->entries);
1332 atom_tref_new (guint32 reftype)
1336 tref = g_new0 (AtomTREF, 1);
1337 atom_tref_init (tref, reftype);
1343 atom_tref_free (AtomTREF * tref)
1345 atom_tref_clear (tref);
1349 /* Clear added tags, but keep the context/flavor the same */
1351 atom_udta_clear_tags (AtomUDTA * udta)
1353 if (udta->entries) {
1354 atom_info_list_free (udta->entries);
1355 udta->entries = NULL;
1357 if (udta->meta && udta->meta->ilst->entries) {
1358 atom_info_list_free (udta->meta->ilst->entries);
1359 udta->meta->ilst->entries = NULL;
1364 atom_tag_data_init (AtomTagData * data)
1366 guint8 flags[] = { 0, 0, 0 };
1368 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1372 atom_tag_data_clear (AtomTagData * data)
1374 atom_full_clear (&data->header);
1375 g_free (data->data);
1380 * Fourcc is the tag fourcc
1381 * flags will be truncated to 24bits
1384 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1386 AtomTag *tag = g_new0 (AtomTag, 1);
1388 tag->header.type = fourcc;
1389 atom_tag_data_init (&tag->data);
1390 atom_full_set_flags_as_uint (&tag->data.header, flags_as_uint);
1395 atom_tag_free (AtomTag * tag)
1397 atom_clear (&tag->header);
1398 atom_tag_data_clear (&tag->data);
1403 atom_mvhd_init (AtomMVHD * mvhd)
1405 guint8 flags[3] = { 0, 0, 0 };
1407 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1409 common_time_info_init (&mvhd->time_info);
1411 mvhd->prefered_rate = 1 << 16;
1412 mvhd->volume = 1 << 8;
1413 mvhd->reserved3 = 0;
1414 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1416 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1417 mvhd->matrix[0] = 1 << 16;
1418 mvhd->matrix[4] = 1 << 16;
1419 mvhd->matrix[8] = 16384 << 16;
1421 mvhd->preview_time = 0;
1422 mvhd->preview_duration = 0;
1423 mvhd->poster_time = 0;
1424 mvhd->selection_time = 0;
1425 mvhd->selection_duration = 0;
1426 mvhd->current_time = 0;
1428 mvhd->next_track_id = 1;
1432 atom_mvhd_clear (AtomMVHD * mvhd)
1434 atom_full_clear (&mvhd->header);
1438 atom_mehd_init (AtomMEHD * mehd)
1440 guint8 flags[3] = { 0, 0, 0 };
1442 atom_full_init (&mehd->header, FOURCC_mehd, 0, 0, 1, flags);
1443 mehd->fragment_duration = 0;
1447 atom_mvex_init (AtomMVEX * mvex)
1449 atom_header_set (&mvex->header, FOURCC_mvex, 0, 0);
1450 atom_mehd_init (&mvex->mehd);
1455 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
1457 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
1459 atom_tkhd_init (&trak->tkhd, context);
1460 trak->context = context;
1461 atom_udta_init (&trak->udta, context);
1463 atom_mdia_init (&trak->mdia, context);
1468 atom_trak_new (AtomsContext * context)
1470 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
1472 atom_trak_init (trak, context);
1477 atom_trak_clear (AtomTRAK * trak)
1479 atom_clear (&trak->header);
1480 atom_tkhd_clear (&trak->tkhd);
1482 atom_edts_free (trak->edts);
1483 atom_udta_clear (&trak->udta);
1484 atom_mdia_clear (&trak->mdia);
1486 atom_tref_free (trak->tref);
1490 atom_trak_free (AtomTRAK * trak)
1492 atom_trak_clear (trak);
1498 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1500 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1501 atom_mvhd_init (&(moov->mvhd));
1502 atom_mvex_init (&(moov->mvex));
1503 atom_udta_init (&moov->udta, context);
1505 moov->context = *context;
1509 atom_moov_new (AtomsContext * context)
1511 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1513 atom_moov_init (moov, context);
1518 atom_trex_free (AtomTREX * trex)
1520 atom_full_clear (&trex->header);
1525 atom_mvex_clear (AtomMVEX * mvex)
1529 atom_clear (&mvex->header);
1530 walker = mvex->trexs;
1532 atom_trex_free ((AtomTREX *) walker->data);
1533 walker = g_list_next (walker);
1535 g_list_free (mvex->trexs);
1540 atom_moov_free (AtomMOOV * moov)
1544 atom_clear (&moov->header);
1545 atom_mvhd_clear (&moov->mvhd);
1547 walker = moov->traks;
1549 atom_trak_free ((AtomTRAK *) walker->data);
1550 walker = g_list_next (walker);
1552 g_list_free (moov->traks);
1555 atom_udta_clear (&moov->udta);
1556 atom_mvex_clear (&moov->mvex);
1561 /* -- end of init / free -- */
1563 /* -- copy data functions -- */
1566 atom_full_get_version (AtomFull * full)
1568 return full->version;
1572 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1573 guint8 ** buffer, guint64 * size, guint64 * offset)
1575 guint64 original_offset = *offset;
1578 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1579 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1580 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1581 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1583 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1584 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1585 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1586 prop_copy_uint64 (ti->duration, buffer, size, offset);
1588 return *offset - original_offset;
1592 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1595 /* this only works for non-extended atom size, which is OK
1596 * (though it could be made to do mem_move, etc and write extended size) */
1597 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1601 atom_copy_empty (Atom * atom, guint8 ** buffer, guint64 * size,
1604 guint64 original_offset = *offset;
1606 prop_copy_uint32 (0, buffer, size, offset);
1608 return *offset - original_offset;
1612 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1614 guint64 original_offset = *offset;
1616 /* copies type and size */
1617 prop_copy_uint32 (atom->size, buffer, size, offset);
1618 prop_copy_fourcc (atom->type, buffer, size, offset);
1620 /* extended size needed */
1621 if (atom->size == 1) {
1622 /* really should not happen other than with mdat atom;
1623 * would be a problem for size (re)write code, not to mention memory */
1624 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1625 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1628 return *offset - original_offset;
1632 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1635 guint64 original_offset = *offset;
1637 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1641 prop_copy_uint8 (atom->version, buffer, size, offset);
1642 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1644 atom_write_size (buffer, size, offset, original_offset);
1645 return *offset - original_offset;
1649 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1652 guint64 original_offset = *offset;
1655 AtomInfo *info = (AtomInfo *) ai->data;
1657 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1660 ai = g_list_next (ai);
1663 return *offset - original_offset;
1667 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1670 guint64 original_offset = *offset;
1672 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1676 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1678 atom_write_size (buffer, size, offset, original_offset);
1679 return *offset - original_offset;
1683 atom_uuid_copy_data (AtomUUID * uuid, guint8 ** buffer, guint64 * size,
1686 guint64 original_offset = *offset;
1688 if (!atom_copy_data (&uuid->header, buffer, size, offset)) {
1691 prop_copy_uint8_array (uuid->uuid, 16, buffer, size, offset);
1693 prop_copy_uint8_array (uuid->data, uuid->datalen, buffer, size, offset);
1695 atom_write_size (buffer, size, offset, original_offset);
1696 return *offset - original_offset;
1700 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1703 guint64 original_offset = *offset;
1705 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1708 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1709 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1711 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1712 buffer, size, offset);
1714 atom_write_size (buffer, size, offset, original_offset);
1715 return *offset - original_offset;
1719 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1723 guint64 original_offset = *offset;
1725 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1729 version = atom_full_get_version (&(atom->header));
1731 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1732 } else if (version == 1) {
1733 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1735 *offset = original_offset;
1739 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1740 prop_copy_uint16 (atom->volume, buffer, size, offset);
1741 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1742 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1743 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1744 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1745 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1746 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1747 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1748 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1749 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1751 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1753 atom_write_size (buffer, size, offset, original_offset);
1754 return *offset - original_offset;
1758 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1761 guint64 original_offset = *offset;
1763 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1767 if (atom_full_get_version (&tkhd->header) == 0) {
1768 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1769 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1770 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1771 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1772 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1774 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1775 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1776 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1777 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1778 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1781 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1782 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1783 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1784 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1785 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1786 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1788 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1789 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1791 atom_write_size (buffer, size, offset, original_offset);
1792 return *offset - original_offset;
1796 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1799 guint64 original_offset = *offset;
1801 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1805 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1806 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1807 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1808 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1809 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1811 if (hdlr->flavor == ATOMS_TREE_FLAVOR_MOV) {
1812 prop_copy_size_string ((guint8 *) hdlr->name, strlen (hdlr->name), buffer,
1815 /* assume isomedia base is more generic and use null terminated */
1816 prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
1819 atom_write_size (buffer, size, offset, original_offset);
1820 return *offset - original_offset;
1824 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1827 guint64 original_offset = *offset;
1829 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1832 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1833 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1835 atom_write_size (buffer, size, offset, original_offset);
1836 return original_offset - *offset;
1840 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1843 guint64 original_offset = *offset;
1845 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1848 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1849 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1851 atom_write_size (buffer, size, offset, original_offset);
1852 return original_offset - *offset;
1856 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1859 guint64 original_offset = *offset;
1861 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1864 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1865 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1866 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1867 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1868 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1870 atom_write_size (buffer, size, offset, original_offset);
1871 return original_offset - *offset;
1875 atom_tcmi_copy_data (AtomTCMI * tcmi, guint8 ** buffer, guint64 * size,
1878 guint64 original_offset = *offset;
1880 if (!atom_full_copy_data (&tcmi->header, buffer, size, offset)) {
1883 prop_copy_uint16 (tcmi->text_font, buffer, size, offset);
1884 prop_copy_uint16 (tcmi->text_face, buffer, size, offset);
1885 prop_copy_uint16 (tcmi->text_size, buffer, size, offset);
1886 prop_copy_uint16 (tcmi->text_color[0], buffer, size, offset);
1887 prop_copy_uint16 (tcmi->text_color[1], buffer, size, offset);
1888 prop_copy_uint16 (tcmi->text_color[2], buffer, size, offset);
1889 prop_copy_uint16 (tcmi->bg_color[0], buffer, size, offset);
1890 prop_copy_uint16 (tcmi->bg_color[1], buffer, size, offset);
1891 prop_copy_uint16 (tcmi->bg_color[2], buffer, size, offset);
1893 prop_copy_uint16 (0, buffer, size, offset);
1894 prop_copy_size_string ((guint8 *) tcmi->font_name, strlen (tcmi->font_name),
1895 buffer, size, offset);
1897 atom_write_size (buffer, size, offset, original_offset);
1898 return original_offset - *offset;
1902 atom_tmcd_copy_data (AtomTMCD * tmcd, guint8 ** buffer, guint64 * size,
1905 guint64 original_offset = *offset;
1907 if (!atom_copy_data (&tmcd->header, buffer, size, offset)) {
1910 if (!atom_tcmi_copy_data (&tmcd->tcmi, buffer, size, offset)) {
1914 atom_write_size (buffer, size, offset, original_offset);
1915 return original_offset - *offset;
1919 atom_gmin_copy_data (AtomGMIN * gmin, guint8 ** buffer, guint64 * size,
1922 guint64 original_offset = *offset;
1924 if (!atom_full_copy_data (&gmin->header, buffer, size, offset)) {
1927 prop_copy_uint16 (gmin->graphics_mode, buffer, size, offset);
1928 prop_copy_uint16 (gmin->opcolor[0], buffer, size, offset);
1929 prop_copy_uint16 (gmin->opcolor[1], buffer, size, offset);
1930 prop_copy_uint16 (gmin->opcolor[2], buffer, size, offset);
1931 prop_copy_uint8 (gmin->balance, buffer, size, offset);
1933 prop_copy_uint8 (0, buffer, size, offset);
1935 atom_write_size (buffer, size, offset, original_offset);
1936 return original_offset - *offset;
1940 atom_gmhd_copy_data (AtomGMHD * gmhd, guint8 ** buffer, guint64 * size,
1943 guint64 original_offset = *offset;
1945 if (!atom_copy_data (&gmhd->header, buffer, size, offset)) {
1948 if (!atom_gmin_copy_data (&gmhd->gmin, buffer, size, offset)) {
1951 if (gmhd->tmcd && !atom_tmcd_copy_data (gmhd->tmcd, buffer, size, offset)) {
1955 atom_write_size (buffer, size, offset, original_offset);
1956 return original_offset - *offset;
1960 atom_url_same_file_flag (AtomURL * url)
1962 return (url->header.flags[2] & 0x1) == 1;
1966 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1969 guint64 original_offset = *offset;
1971 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1975 if (!atom_url_same_file_flag (url)) {
1976 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1979 atom_write_size (buffer, size, offset, original_offset);
1980 return original_offset - *offset;
1984 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1987 guint64 original_offset = *offset;
1990 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1994 prop_copy_uint32 (atom_array_get_len (&stts->entries), buffer, size, offset);
1995 /* minimize realloc */
1996 prop_copy_ensure_buffer (buffer, size, offset,
1997 8 * atom_array_get_len (&stts->entries));
1998 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
1999 STTSEntry *entry = &atom_array_index (&stts->entries, i);
2001 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
2002 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
2005 atom_write_size (buffer, size, offset, original_offset);
2006 return *offset - original_offset;
2010 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
2011 guint64 * size, guint64 * offset)
2013 guint64 original_offset = *offset;
2015 if (!atom_copy_data (&se->header, buffer, size, offset)) {
2019 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
2020 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
2022 return *offset - original_offset;
2026 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
2029 guint64 original_offset = *offset;
2031 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
2034 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
2038 atom_write_size (buffer, size, offset, original_offset);
2039 return *offset - original_offset;
2043 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
2044 guint64 * size, guint64 * offset)
2046 guint64 original_offset = *offset;
2048 if (!atom_copy_data (&(frma->header), buffer, size, offset))
2051 prop_copy_fourcc (frma->media_type, buffer, size, offset);
2053 atom_write_size (buffer, size, offset, original_offset);
2054 return *offset - original_offset;
2058 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
2059 guint64 * size, guint64 * offset)
2061 guint64 original_offset = *offset;
2063 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
2067 prop_copy_uint32 (hse->size, buffer, size, offset);
2068 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
2070 atom_write_size (buffer, size, offset, original_offset);
2071 return *offset - original_offset;
2075 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
2076 guint64 * size, guint64 * offset)
2078 guint64 original_offset = *offset;
2080 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
2084 prop_copy_uint16 (mp4a->version, buffer, size, offset);
2085 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
2086 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
2087 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
2088 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
2089 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
2090 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
2091 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
2093 /* this should always be 0 for mp4 flavor */
2094 if (mp4a->version == 1) {
2095 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
2096 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
2097 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
2098 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
2101 if (mp4a->extension_atoms) {
2102 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
2106 atom_write_size (buffer, size, offset, original_offset);
2107 return *offset - original_offset;
2111 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
2112 guint64 * size, guint64 * offset)
2114 guint64 original_offset = *offset;
2116 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
2120 prop_copy_uint16 (mp4v->version, buffer, size, offset);
2121 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
2122 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
2123 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
2124 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
2126 prop_copy_uint16 (mp4v->width, buffer, size, offset);
2127 prop_copy_uint16 (mp4v->height, buffer, size, offset);
2129 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
2130 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
2131 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
2133 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
2135 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
2138 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
2139 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
2142 if (mp4v->extension_atoms &&
2143 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
2146 atom_write_size (buffer, size, offset, original_offset);
2147 return *offset - original_offset;
2151 sample_entry_tx3g_copy_data (SampleTableEntryTX3G * tx3g, guint8 ** buffer,
2152 guint64 * size, guint64 * offset)
2154 guint64 original_offset = *offset;
2156 if (!atom_sample_entry_copy_data (&tx3g->se, buffer, size, offset)) {
2160 prop_copy_uint32 (tx3g->display_flags, buffer, size, offset);
2163 prop_copy_uint8 (1, buffer, size, offset);
2164 prop_copy_uint8 (-1, buffer, size, offset);
2165 prop_copy_uint32 (0, buffer, size, offset);
2167 prop_copy_uint64 (tx3g->default_text_box, buffer, size, offset);
2170 prop_copy_uint32 (0, buffer, size, offset);
2172 prop_copy_uint16 (tx3g->font_id, buffer, size, offset);
2173 prop_copy_uint8 (tx3g->font_face, buffer, size, offset);
2174 prop_copy_uint8 (tx3g->font_size, buffer, size, offset);
2175 prop_copy_uint32 (tx3g->foreground_color_rgba, buffer, size, offset);
2177 /* it must have a fonttable atom */
2181 atom_header_set (&atom, FOURCC_ftab, 18, 0);
2182 if (!atom_copy_data (&atom, buffer, size, offset))
2184 prop_copy_uint16 (1, buffer, size, offset); /* Count must be 1 */
2185 prop_copy_uint16 (1, buffer, size, offset); /* Font id: 1 */
2186 prop_copy_size_string ((guint8 *) "Serif", 5, buffer, size, offset);
2189 atom_write_size (buffer, size, offset, original_offset);
2190 return *offset - original_offset;
2194 sample_entry_tmcd_copy_data (SampleTableEntryTMCD * tmcd, guint8 ** buffer,
2195 guint64 * size, guint64 * offset)
2197 guint64 original_offset = *offset;
2199 if (!atom_sample_entry_copy_data (&tmcd->se, buffer, size, offset)) {
2204 prop_copy_uint32 (0, buffer, size, offset);
2206 prop_copy_uint32 (tmcd->tc_flags, buffer, size, offset);
2207 prop_copy_uint32 (tmcd->timescale, buffer, size, offset);
2208 prop_copy_uint32 (tmcd->frame_duration, buffer, size, offset);
2209 prop_copy_uint8 (tmcd->n_frames, buffer, size, offset);
2212 prop_copy_uint8 (0, buffer, size, offset);
2215 guint64 name_offset = *offset;
2217 atom_header_set (&atom, FOURCC_name, 0, 0);
2218 if (!atom_copy_data (&atom, buffer, size, offset))
2220 prop_copy_uint16 (strlen (tmcd->name.name), buffer, size, offset);
2221 prop_copy_uint16 (tmcd->name.language_code, buffer, size, offset);
2222 prop_copy_fixed_size_string ((guint8 *) tmcd->name.name,
2223 strlen (tmcd->name.name), buffer, size, offset);
2225 atom_write_size (buffer, size, offset, name_offset);
2228 atom_write_size (buffer, size, offset, original_offset);
2229 return *offset - original_offset;
2233 sample_entry_generic_copy_data (SampleTableEntry * entry, guint8 ** buffer,
2234 guint64 * size, guint64 * offset)
2236 guint64 original_offset = *offset;
2238 if (!atom_sample_entry_copy_data (entry, buffer, size, offset)) {
2242 atom_write_size (buffer, size, offset, original_offset);
2243 return *offset - original_offset;
2247 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
2250 guint64 original_offset = *offset;
2253 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
2257 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
2258 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
2259 if (stsz->sample_size == 0) {
2260 /* minimize realloc */
2261 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
2262 /* entry count must match sample count */
2263 g_assert (atom_array_get_len (&stsz->entries) == stsz->table_size);
2264 for (i = 0; i < atom_array_get_len (&stsz->entries); i++) {
2265 prop_copy_uint32 (atom_array_index (&stsz->entries, i), buffer, size,
2270 atom_write_size (buffer, size, offset, original_offset);
2271 return *offset - original_offset;
2275 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
2278 guint64 original_offset = *offset;
2280 gboolean last_entries_merged = FALSE;
2282 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
2286 /* Last two entries might be the same size here as we only merge once the
2287 * next chunk is started */
2288 if ((len = atom_array_get_len (&stsc->entries)) > 1 &&
2289 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
2290 (atom_array_index (&stsc->entries, len - 2)).samples_per_chunk)) {
2291 stsc->entries.len--;
2292 last_entries_merged = TRUE;
2295 prop_copy_uint32 (atom_array_get_len (&stsc->entries), buffer, size, offset);
2296 /* minimize realloc */
2297 prop_copy_ensure_buffer (buffer, size, offset,
2298 12 * atom_array_get_len (&stsc->entries));
2300 for (i = 0; i < atom_array_get_len (&stsc->entries); i++) {
2301 STSCEntry *entry = &atom_array_index (&stsc->entries, i);
2303 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
2304 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
2305 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
2308 atom_write_size (buffer, size, offset, original_offset);
2310 /* Need to add the last entry again as in "robust" muxing mode we will most
2311 * likely add new samples to the last chunk, thus making the
2312 * samples_per_chunk in the last one different to the second to last one,
2313 * and thus making it wrong to keep them merged
2315 if (last_entries_merged)
2316 stsc->entries.len++;
2318 return *offset - original_offset;
2322 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
2325 guint64 original_offset = *offset;
2328 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
2332 prop_copy_uint32 (atom_array_get_len (&ctts->entries), buffer, size, offset);
2333 /* minimize realloc */
2334 prop_copy_ensure_buffer (buffer, size, offset,
2335 8 * atom_array_get_len (&ctts->entries));
2336 for (i = 0; i < atom_array_get_len (&ctts->entries); i++) {
2337 CTTSEntry *entry = &atom_array_index (&ctts->entries, i);
2339 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
2340 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
2343 atom_write_size (buffer, size, offset, original_offset);
2344 return *offset - original_offset;
2348 atom_svmi_copy_data (AtomSVMI * svmi, guint8 ** buffer, guint64 * size,
2351 guint64 original_offset = *offset;
2353 if (!atom_full_copy_data (&svmi->header, buffer, size, offset)) {
2357 prop_copy_uint8 (svmi->stereoscopic_composition_type, buffer, size, offset);
2358 prop_copy_uint8 (svmi->is_left_first ? 1 : 0, buffer, size, offset);
2359 /* stereo-mono change count */
2360 prop_copy_uint32 (0, buffer, size, offset);
2362 atom_write_size (buffer, size, offset, original_offset);
2363 return *offset - original_offset;
2367 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
2370 guint64 original_offset = *offset;
2372 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
2374 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
2378 prop_copy_uint32 (atom_array_get_len (&stco64->entries), buffer, size,
2381 /* minimize realloc */
2382 prop_copy_ensure_buffer (buffer, size, offset,
2383 8 * atom_array_get_len (&stco64->entries));
2384 for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
2386 atom_array_index (&stco64->entries, i) + stco64->chunk_offset;
2389 prop_copy_uint32 ((guint32) value, buffer, size, offset);
2391 prop_copy_uint64 (value, buffer, size, offset);
2395 atom_write_size (buffer, size, offset, original_offset);
2396 return *offset - original_offset;
2400 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
2403 guint64 original_offset = *offset;
2406 if (atom_array_get_len (&stss->entries) == 0) {
2407 /* FIXME not needing this atom might be confused with error while copying */
2411 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
2415 prop_copy_uint32 (atom_array_get_len (&stss->entries), buffer, size, offset);
2416 /* minimize realloc */
2417 prop_copy_ensure_buffer (buffer, size, offset,
2418 4 * atom_array_get_len (&stss->entries));
2419 for (i = 0; i < atom_array_get_len (&stss->entries); i++) {
2420 prop_copy_uint32 (atom_array_index (&stss->entries, i), buffer, size,
2424 atom_write_size (buffer, size, offset, original_offset);
2425 return *offset - original_offset;
2429 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
2432 guint64 original_offset = *offset;
2435 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
2439 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
2441 for (walker = g_list_last (stsd->entries); walker != NULL;
2442 walker = g_list_previous (walker)) {
2443 SampleTableEntry *se = (SampleTableEntry *) walker->data;
2445 switch (((Atom *) walker->data)->type) {
2447 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
2448 buffer, size, offset)) {
2453 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
2454 buffer, size, offset)) {
2459 if (se->kind == VIDEO) {
2460 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
2461 walker->data, buffer, size, offset)) {
2464 } else if (se->kind == AUDIO) {
2465 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
2466 walker->data, buffer, size, offset)) {
2469 } else if (se->kind == SUBTITLE) {
2470 if (!sample_entry_tx3g_copy_data ((SampleTableEntryTX3G *)
2471 walker->data, buffer, size, offset)) {
2474 } else if (se->kind == TIMECODE) {
2475 if (!sample_entry_tmcd_copy_data ((SampleTableEntryTMCD *)
2476 walker->data, buffer, size, offset)) {
2479 } else if (se->kind == CLOSEDCAPTION) {
2480 if (!sample_entry_generic_copy_data ((SampleTableEntry *)
2481 walker->data, buffer, size, offset)) {
2485 if (!atom_hint_sample_entry_copy_data (
2486 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
2494 atom_write_size (buffer, size, offset, original_offset);
2495 return *offset - original_offset;
2499 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
2502 guint64 original_offset = *offset;
2504 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
2508 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
2511 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
2514 /* this atom is optional, so let's check if we need it
2515 * (to avoid false error) */
2516 if (atom_array_get_len (&stbl->stss.entries)) {
2517 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
2522 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
2525 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
2528 if (stbl->ctts && stbl->ctts->do_pts) {
2529 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
2534 if (!atom_svmi_copy_data (stbl->svmi, buffer, size, offset)) {
2538 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
2542 atom_write_size (buffer, size, offset, original_offset);
2543 return original_offset - *offset;
2548 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
2551 guint64 original_offset = *offset;
2554 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
2558 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
2560 walker = dref->entries;
2561 while (walker != NULL) {
2562 Atom *atom = (Atom *) walker->data;
2564 if (atom->type == FOURCC_url_) {
2565 if (!atom_url_copy_data ((AtomURL *) atom, buffer, size, offset))
2567 } else if (atom->type == FOURCC_alis) {
2568 if (!atom_full_copy_data ((AtomFull *) atom, buffer, size, offset))
2571 g_error ("Unsupported atom used inside dref atom");
2573 walker = g_list_next (walker);
2576 atom_write_size (buffer, size, offset, original_offset);
2577 return *offset - original_offset;
2581 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
2584 guint64 original_offset = *offset;
2586 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
2590 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
2594 atom_write_size (buffer, size, offset, original_offset);
2595 return original_offset - *offset;
2599 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
2602 guint64 original_offset = *offset;
2604 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
2609 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
2612 } else if (minf->smhd) {
2613 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
2616 } else if (minf->hmhd) {
2617 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
2620 } else if (minf->gmhd) {
2621 if (!atom_gmhd_copy_data (minf->gmhd, buffer, size, offset)) {
2627 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2632 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2635 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2639 atom_write_size (buffer, size, offset, original_offset);
2640 return *offset - original_offset;
2644 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2647 guint64 original_offset = *offset;
2649 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2653 if (!common_time_info_copy_data (&mdhd->time_info,
2654 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2658 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2659 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2661 atom_write_size (buffer, size, offset, original_offset);
2662 return *offset - original_offset;
2666 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2669 guint64 original_offset = *offset;
2671 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2674 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2677 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2681 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2685 atom_write_size (buffer, size, offset, original_offset);
2686 return *offset - original_offset;
2690 atom_elst_copy_data (AtomELST * elst, guint8 ** buffer, guint64 * size,
2693 guint64 original_offset = *offset;
2696 if (!atom_full_copy_data (&elst->header, buffer, size, offset)) {
2700 prop_copy_uint32 (g_slist_length (elst->entries), buffer, size, offset);
2702 for (walker = elst->entries; walker != NULL; walker = g_slist_next (walker)) {
2703 EditListEntry *entry = (EditListEntry *) walker->data;
2704 prop_copy_uint32 (entry->duration, buffer, size, offset);
2705 prop_copy_uint32 (entry->media_time, buffer, size, offset);
2706 prop_copy_uint32 (entry->media_rate, buffer, size, offset);
2708 atom_write_size (buffer, size, offset, original_offset);
2709 return *offset - original_offset;
2713 atom_tref_copy_data (AtomTREF * tref, guint8 ** buffer, guint64 * size,
2716 guint64 original_offset = *offset;
2719 g_assert (atom_array_get_len (&tref->entries) > 0);
2721 if (!atom_copy_data (&tref->header, buffer, size, offset)) {
2725 prop_copy_uint32 (8 + 4 * atom_array_get_len (&tref->entries), buffer, size,
2727 prop_copy_fourcc (tref->reftype, buffer, size, offset);
2728 /* minimize realloc */
2729 prop_copy_ensure_buffer (buffer, size, offset,
2730 4 * atom_array_get_len (&tref->entries));
2731 for (i = 0; i < atom_array_get_len (&tref->entries); i++) {
2732 prop_copy_uint32 (atom_array_index (&tref->entries, i), buffer, size,
2736 atom_write_size (buffer, size, offset, original_offset);
2737 return *offset - original_offset;
2741 atom_edts_copy_data (AtomEDTS * edts, guint8 ** buffer, guint64 * size,
2744 guint64 original_offset = *offset;
2746 if (!atom_copy_data (&(edts->header), buffer, size, offset))
2749 if (!atom_elst_copy_data (&(edts->elst), buffer, size, offset))
2752 atom_write_size (buffer, size, offset, original_offset);
2753 return *offset - original_offset;
2757 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2760 guint64 original_offset = *offset;
2762 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2766 prop_copy_uint32 (data->reserved, buffer, size, offset);
2767 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2769 atom_write_size (buffer, size, offset, original_offset);
2770 return *offset - original_offset;
2774 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2777 guint64 original_offset = *offset;
2779 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2783 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2787 atom_write_size (buffer, size, offset, original_offset);
2788 return *offset - original_offset;
2792 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2795 guint64 original_offset = *offset;
2797 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2801 if (ilst->entries &&
2802 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2805 atom_write_size (buffer, size, offset, original_offset);
2806 return *offset - original_offset;
2810 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2813 guint64 original_offset = *offset;
2815 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2818 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2822 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2827 atom_write_size (buffer, size, offset, original_offset);
2828 return *offset - original_offset;
2832 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2835 guint64 original_offset = *offset;
2837 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2841 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2845 if (udta->entries) {
2847 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2851 atom_write_size (buffer, size, offset, original_offset);
2852 return *offset - original_offset;
2856 atom_mehd_copy_data (AtomMEHD * mehd, guint8 ** buffer, guint64 * size,
2859 guint64 original_offset = *offset;
2861 if (!atom_full_copy_data (&mehd->header, buffer, size, offset)) {
2865 prop_copy_uint64 (mehd->fragment_duration, buffer, size, offset);
2867 atom_write_size (buffer, size, offset, original_offset);
2868 return *offset - original_offset;
2872 atom_trex_copy_data (AtomTREX * trex, guint8 ** buffer, guint64 * size,
2875 guint64 original_offset = *offset;
2877 if (!atom_full_copy_data (&trex->header, buffer, size, offset)) {
2881 prop_copy_uint32 (trex->track_ID, buffer, size, offset);
2882 prop_copy_uint32 (trex->default_sample_description_index, buffer, size,
2884 prop_copy_uint32 (trex->default_sample_duration, buffer, size, offset);
2885 prop_copy_uint32 (trex->default_sample_size, buffer, size, offset);
2886 prop_copy_uint32 (trex->default_sample_flags, buffer, size, offset);
2888 atom_write_size (buffer, size, offset, original_offset);
2889 return *offset - original_offset;
2893 atom_mvex_copy_data (AtomMVEX * mvex, guint8 ** buffer, guint64 * size,
2896 guint64 original_offset = *offset;
2899 if (!atom_copy_data (&mvex->header, buffer, size, offset)) {
2903 if (!atom_mehd_copy_data (&mvex->mehd, buffer, size, offset)) {
2907 walker = g_list_first (mvex->trexs);
2908 while (walker != NULL) {
2909 if (!atom_trex_copy_data ((AtomTREX *) walker->data, buffer, size, offset)) {
2912 walker = g_list_next (walker);
2915 atom_write_size (buffer, size, offset, original_offset);
2916 return *offset - original_offset;
2920 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2923 guint64 original_offset = *offset;
2925 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2928 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2932 if (!trak->tapt->copy_data_func (trak->tapt->atom, buffer, size, offset)) {
2937 if (!atom_edts_copy_data (trak->edts, buffer, size, offset)) {
2942 /* Make sure we need this atom (there is a referenced track */
2943 if (atom_array_get_len (&trak->tref->entries) > 0) {
2944 if (!atom_tref_copy_data (trak->tref, buffer, size, offset)) {
2950 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2954 if (!atom_udta_copy_data (&trak->udta, buffer, size, offset)) {
2958 atom_write_size (buffer, size, offset, original_offset);
2959 return *offset - original_offset;
2964 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2967 guint64 original_offset = *offset;
2970 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2973 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2976 walker = g_list_first (atom->traks);
2977 while (walker != NULL) {
2978 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2981 walker = g_list_next (walker);
2984 if (!atom_udta_copy_data (&atom->udta, buffer, size, offset)) {
2988 if (atom->fragmented) {
2989 if (!atom_mvex_copy_data (&atom->mvex, buffer, size, offset)) {
2994 atom_write_size (buffer, size, offset, original_offset);
2995 return *offset - original_offset;
2999 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
3000 guint64 * size, guint64 * offset)
3002 guint64 original_offset = *offset;
3004 if (!atom_copy_data (&(wave->header), buffer, size, offset))
3007 if (wave->extension_atoms) {
3008 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
3012 atom_write_size (buffer, size, offset, original_offset);
3013 return *offset - original_offset;
3016 /* -- end of copy data functions -- */
3018 /* -- general functions, API and support functions */
3020 /* add samples to tables */
3023 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
3027 if ((len = atom_array_get_len (&stsc->entries)) > 1 &&
3028 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
3029 (atom_array_index (&stsc->entries, len - 2)).samples_per_chunk)) {
3032 /* Merge last two entries as they have the same number of samples per chunk */
3033 nentry = &atom_array_index (&stsc->entries, len - 1);
3034 nentry->first_chunk = first_chunk;
3035 nentry->samples_per_chunk = nsamples;
3036 nentry->sample_description_index = 1;
3040 nentry.first_chunk = first_chunk;
3041 nentry.samples_per_chunk = nsamples;
3042 nentry.sample_description_index = 1;
3043 atom_array_append (&stsc->entries, nentry, 128);
3048 atom_stsc_update_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
3052 len = atom_array_get_len (&stsc->entries);
3053 g_assert (len != 0);
3054 g_assert (atom_array_index (&stsc->entries,
3055 len - 1).first_chunk == first_chunk);
3057 atom_array_index (&stsc->entries, len - 1).samples_per_chunk += nsamples;
3061 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
3063 STTSEntry *entry = NULL;
3065 if (G_LIKELY (atom_array_get_len (&stts->entries) != 0))
3066 entry = &atom_array_index (&stts->entries,
3067 atom_array_get_len (&stts->entries) - 1);
3069 if (entry && entry->sample_delta == sample_delta) {
3070 entry->sample_count += sample_count;
3074 nentry.sample_count = sample_count;
3075 nentry.sample_delta = sample_delta;
3076 atom_array_append (&stts->entries, nentry, 256);
3081 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
3085 stsz->table_size += nsamples;
3086 if (stsz->sample_size != 0) {
3087 /* it is constant size, we don't need entries */
3090 for (i = 0; i < nsamples; i++) {
3091 atom_array_append (&stsz->entries, size, 1024);
3096 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
3098 return atom_array_get_len (&stco64->entries);
3101 /* returns TRUE if a new entry was added */
3103 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
3107 /* Only add a new entry if the chunk offset changed */
3108 if ((len = atom_array_get_len (&stco64->entries)) &&
3109 ((atom_array_index (&stco64->entries, len - 1)) == entry))
3112 atom_array_append (&stco64->entries, entry, 256);
3113 if (entry > G_MAXUINT32)
3114 stco64->header.header.type = FOURCC_co64;
3120 atom_tref_add_entry (AtomTREF * tref, guint32 sample)
3122 atom_array_append (&tref->entries, sample, 512);
3126 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
3128 atom_array_append (&stss->entries, sample, 512);
3132 atom_stbl_add_stss_entry (AtomSTBL * stbl)
3134 guint32 sample_index = stbl->stsz.table_size;
3136 atom_stss_add_entry (&stbl->stss, sample_index);
3140 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
3142 CTTSEntry *entry = NULL;
3144 if (G_LIKELY (atom_array_get_len (&ctts->entries) != 0))
3145 entry = &atom_array_index (&ctts->entries,
3146 atom_array_get_len (&ctts->entries) - 1);
3148 if (entry == NULL || entry->sampleoffset != offset) {
3151 nentry.samplecount = nsamples;
3152 nentry.sampleoffset = offset;
3153 atom_array_append (&ctts->entries, nentry, 256);
3155 ctts->do_pts = TRUE;
3157 entry->samplecount += nsamples;
3162 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
3164 if (stbl->ctts == NULL) {
3165 stbl->ctts = atom_ctts_new ();
3167 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
3171 atom_stbl_add_samples (AtomSTBL * stbl, guint32 nsamples, guint32 delta,
3172 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3174 atom_stts_add_entry (&stbl->stts, nsamples, delta);
3175 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
3176 if (atom_stco64_add_entry (&stbl->stco64, chunk_offset)) {
3177 atom_stsc_add_new_entry (&stbl->stsc,
3178 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
3180 atom_stsc_update_entry (&stbl->stsc,
3181 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
3185 atom_stbl_add_stss_entry (stbl);
3186 /* always store to arrange for consistent content */
3187 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
3191 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
3192 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3194 AtomSTBL *stbl = &trak->mdia.minf.stbl;
3195 atom_stbl_add_samples (stbl, nsamples, delta, size, chunk_offset, sync,
3199 /* trak and moov molding */
3202 atom_trak_get_timescale (AtomTRAK * trak)
3204 return trak->mdia.mdhd.time_info.timescale;
3208 atom_trak_get_id (AtomTRAK * trak)
3210 return trak->tkhd.track_ID;
3214 atom_trak_set_id (AtomTRAK * trak, guint32 id)
3216 trak->tkhd.track_ID = id;
3220 atom_moov_add_trex (AtomMOOV * moov, AtomTREX * trex)
3222 moov->mvex.trexs = g_list_append (moov->mvex.trexs, trex);
3226 atom_trex_new (AtomTRAK * trak)
3228 guint8 flags[3] = { 0, 0, 0 };
3229 AtomTREX *trex = g_new0 (AtomTREX, 1);
3231 atom_full_init (&trex->header, FOURCC_trex, 0, 0, 0, flags);
3233 trex->track_ID = trak->tkhd.track_ID;
3234 trex->default_sample_description_index = 1;
3235 trex->default_sample_duration = 0;
3236 trex->default_sample_size = 0;
3237 trex->default_sample_flags = 0;
3243 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
3245 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
3246 moov->traks = g_list_append (moov->traks, trak);
3247 /* additional trak means also new trex */
3248 atom_moov_add_trex (moov, atom_trex_new (trak));
3252 atom_moov_get_trak_count (AtomMOOV * moov)
3254 return g_list_length (moov->traks);
3258 atom_trak_get_duration (AtomTRAK * trak)
3260 return trak->tkhd.duration;
3264 atom_stts_get_total_duration (AtomSTTS * stts)
3269 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
3270 STTSEntry *entry = &atom_array_index (&stts->entries, i);
3272 sum += (guint64) (entry->sample_count) * entry->sample_delta;
3278 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
3280 trak->mdia.mdhd.time_info.duration =
3281 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
3282 if (trak->mdia.mdhd.time_info.timescale != 0) {
3283 trak->tkhd.duration =
3284 gst_util_uint64_scale_round (trak->mdia.mdhd.time_info.duration,
3285 moov_timescale, trak->mdia.mdhd.time_info.timescale);
3287 trak->tkhd.duration = 0;
3292 timecode_atom_trak_set_duration (AtomTRAK * trak, guint64 duration,
3298 /* Sanity checks to ensure we have a timecode */
3299 g_assert (trak->mdia.minf.gmhd != NULL);
3300 g_assert (atom_array_get_len (&trak->mdia.minf.stbl.stts.entries) == 1);
3302 for (iter = trak->mdia.minf.stbl.stsd.entries; iter;
3303 iter = g_list_next (iter)) {
3304 SampleTableEntry *entry = iter->data;
3305 if (entry->kind == TIMECODE) {
3306 SampleTableEntryTMCD *tmcd = (SampleTableEntryTMCD *) entry;
3308 duration = duration * tmcd->timescale / timescale;
3309 timescale = tmcd->timescale;
3314 trak->tkhd.duration = duration;
3315 trak->mdia.mdhd.time_info.duration = duration;
3316 trak->mdia.mdhd.time_info.timescale = timescale;
3318 entry = &atom_array_index (&trak->mdia.minf.stbl.stts.entries, 0);
3319 entry->sample_delta = duration;
3323 atom_moov_get_timescale (AtomMOOV * moov)
3325 return moov->mvhd.time_info.timescale;
3329 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
3331 moov->mvhd.time_info.timescale = timescale;
3335 atom_moov_update_duration (AtomMOOV * moov)
3337 GList *traks = moov->traks;
3338 guint64 dur, duration = 0;
3341 AtomTRAK *trak = (AtomTRAK *) traks->data;
3343 /* Skip timecodes for now: they have a placeholder duration */
3344 if (trak->mdia.minf.gmhd == NULL || trak->mdia.minf.gmhd->tmcd == NULL) {
3345 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
3346 dur = atom_trak_get_duration (trak);
3350 traks = g_list_next (traks);
3352 /* Now update the duration of the timecodes */
3353 traks = moov->traks;
3355 AtomTRAK *trak = (AtomTRAK *) traks->data;
3357 if (trak->mdia.minf.gmhd != NULL && trak->mdia.minf.gmhd->tmcd != NULL)
3358 timecode_atom_trak_set_duration (trak, duration,
3359 atom_moov_get_timescale (moov));
3360 traks = g_list_next (traks);
3362 moov->mvhd.time_info.duration = duration;
3363 moov->mvex.mehd.fragment_duration = duration;
3367 atom_moov_set_fragmented (AtomMOOV * moov, gboolean fragmented)
3369 moov->fragmented = fragmented;
3373 atom_stco64_chunks_set_offset (AtomSTCO64 * stco64, guint32 offset)
3375 stco64->chunk_offset = offset;
3379 atom_moov_chunks_set_offset (AtomMOOV * moov, guint32 offset)
3381 GList *traks = moov->traks;
3383 if (offset == moov->chunks_offset)
3384 return; /* Nothing to do */
3387 AtomTRAK *trak = (AtomTRAK *) traks->data;
3389 atom_stco64_chunks_set_offset (&trak->mdia.minf.stbl.stco64, offset);
3390 traks = g_list_next (traks);
3393 moov->chunks_offset = offset;
3397 atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
3398 guint32 max_bitrate)
3400 AtomESDS *esds = NULL;
3401 AtomData *btrt = NULL;
3402 AtomWAVE *wave = NULL;
3405 GList *extensioniter = NULL;
3407 g_return_if_fail (trak != NULL);
3409 if (avg_bitrate == 0 && max_bitrate == 0)
3412 stsd = &trak->mdia.minf.stbl.stsd;
3413 for (iter = stsd->entries; iter; iter = g_list_next (iter)) {
3414 SampleTableEntry *entry = iter->data;
3416 switch (entry->kind) {
3418 SampleTableEntryMP4A *audioentry = (SampleTableEntryMP4A *) entry;
3419 extensioniter = audioentry->extension_atoms;
3423 SampleTableEntryMP4V *videoentry = (SampleTableEntryMP4V *) entry;
3424 extensioniter = videoentry->extension_atoms;
3432 for (; extensioniter; extensioniter = g_list_next (extensioniter)) {
3433 AtomInfo *atominfo = extensioniter->data;
3434 if (atominfo->atom->type == FOURCC_esds) {
3435 esds = (AtomESDS *) atominfo->atom;
3436 } else if (atominfo->atom->type == FOURCC_btrt) {
3437 btrt = (AtomData *) atominfo->atom;
3438 } else if (atominfo->atom->type == FOURCC_wave) {
3439 wave = (AtomWAVE *) atominfo->atom;
3443 /* wave might have an esds internally */
3445 for (extensioniter = wave->extension_atoms; extensioniter;
3446 extensioniter = g_list_next (extensioniter)) {
3447 AtomInfo *atominfo = extensioniter->data;
3448 if (atominfo->atom->type == FOURCC_esds) {
3449 esds = (AtomESDS *) atominfo->atom;
3456 if (avg_bitrate && esds->es.dec_conf_desc.avg_bitrate == 0)
3457 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
3458 if (max_bitrate && esds->es.dec_conf_desc.max_bitrate == 0)
3459 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
3462 /* type(4bytes) + size(4bytes) + buffersize(4bytes) +
3463 * maxbitrate(bytes) + avgbitrate(bytes) */
3464 if (max_bitrate && GST_READ_UINT32_BE (btrt->data + 4) == 0)
3465 GST_WRITE_UINT32_BE (btrt->data + 4, max_bitrate);
3466 if (avg_bitrate && GST_READ_UINT32_BE (btrt->data + 8) == 0)
3467 GST_WRITE_UINT32_BE (btrt->data + 8, avg_bitrate);
3472 atom_trak_tx3g_update_dimension (AtomTRAK * trak, guint32 width, guint32 height)
3476 SampleTableEntryTX3G *tx3g = NULL;
3478 stsd = &trak->mdia.minf.stbl.stsd;
3479 for (iter = stsd->entries; iter && tx3g == NULL; iter = g_list_next (iter)) {
3480 SampleTableEntry *entry = iter->data;
3482 switch (entry->kind) {
3484 tx3g = (SampleTableEntryTX3G *) entry;
3492 /* Currently we never set the vertical placement flag, so we don't
3493 * check for it to set the dimensions differently as the spec says.
3494 * Always do it for the not set case */
3496 tx3g->font_size = 0.05 * height;
3498 height = 0.15 * height;
3499 trak->tkhd.width = width << 16;
3500 trak->tkhd.height = height << 16;
3501 tx3g->default_text_box = width | (height << 16);
3506 * Meta tags functions
3509 atom_tag_data_alloc_data (AtomTagData * data, guint size)
3511 g_free (data->data);
3512 data->data = g_new0 (guint8, size);
3513 data->datalen = size;
3517 atom_udta_append_tag (AtomUDTA * udta, AtomInfo * tag)
3522 entries = &udta->meta->ilst->entries;
3524 entries = &udta->entries;
3525 *entries = g_list_append (*entries, tag);
3529 atom_udta_add_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3530 const guint8 * data, guint size)
3535 tag = atom_tag_new (fourcc, flags);
3537 atom_tag_data_alloc_data (tdata, size);
3538 memmove (tdata->data, data, size);
3540 atom_udta_append_tag (udta,
3541 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
3546 atom_udta_add_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3548 gint len = strlen (value);
3551 atom_udta_add_tag (udta, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
3555 atom_udta_add_uint_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3558 guint8 data[8] = { 0, };
3561 GST_WRITE_UINT16_BE (data, value);
3562 atom_udta_add_tag (udta, fourcc, flags, data, 2);
3564 GST_WRITE_UINT32_BE (data + 2, value);
3565 atom_udta_add_tag (udta, fourcc, flags, data, 8);
3569 #define GST_BUFFER_NEW_READONLY(mem, size) \
3570 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, mem, size, \
3574 atom_udta_add_blob_tag (AtomUDTA * udta, guint8 * data, guint size)
3576 AtomData *data_atom;
3583 /* blob is unparsed atom;
3584 * extract size and fourcc, and wrap remainder in data atom */
3585 len = GST_READ_UINT32_BE (data);
3586 fourcc = GST_READ_UINT32_LE (data + 4);
3590 data_atom = atom_data_new_from_data (fourcc, data + 8, len - 8);
3592 atom_udta_append_tag (udta,
3593 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3598 atom_udta_add_3gp_tag (AtomUDTA * udta, guint32 fourcc, guint8 * data,
3601 AtomData *data_atom;
3603 data_atom = atom_data_new (fourcc);
3605 /* need full atom */
3606 atom_data_alloc_mem (data_atom, size + 4);
3608 /* full atom: version and flags */
3609 GST_WRITE_UINT32_BE (data_atom->data, 0);
3610 memcpy (data_atom->data + 4, data, size);
3612 atom_udta_append_tag (udta,
3613 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3618 language_code (const char *lang)
3620 g_return_val_if_fail (lang != NULL, 0);
3621 g_return_val_if_fail (strlen (lang) == 3, 0);
3623 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
3624 ((lang[2] - 0x60) & 0x1F);
3628 atom_udta_add_3gp_str_int_tag (AtomUDTA * udta, guint32 fourcc,
3629 const gchar * value, gint16 ivalue)
3631 gint len = 0, size = 0;
3635 len = strlen (value);
3642 data = g_malloc (size + 3);
3643 /* language tag and null-terminated UTF-8 string */
3645 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
3646 /* include 0 terminator */
3647 memcpy (data + 2, value, len + 1);
3649 /* 16-bit unsigned int if standalone, otherwise 8-bit */
3652 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
3654 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
3659 atom_udta_add_3gp_tag (udta, fourcc, data, size);
3664 atom_udta_add_3gp_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3666 atom_udta_add_3gp_str_int_tag (udta, fourcc, value, -1);
3670 atom_udta_add_3gp_uint_tag (AtomUDTA * udta, guint32 fourcc, guint16 value)
3672 atom_udta_add_3gp_str_int_tag (udta, fourcc, NULL, value);
3676 atom_udta_add_xmp_tags (AtomUDTA * udta, GstBuffer * xmpbuffer)
3678 AtomData *data_atom = NULL;
3680 if (udta->context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3682 data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
3683 udta->entries = g_list_append (udta->entries,
3684 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3688 GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
3693 * Functions for specifying media types
3697 atom_minf_set_audio (AtomMINF * minf)
3699 atom_minf_clear_handlers (minf);
3700 minf->smhd = atom_smhd_new ();
3704 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
3706 atom_minf_clear_handlers (minf);
3707 minf->vmhd = atom_vmhd_new (context);
3711 atom_minf_set_subtitle (AtomMINF * minf)
3713 atom_minf_clear_handlers (minf);
3717 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
3720 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3721 hdlr->component_type = comp_type;
3723 hdlr->handler_type = hdlr_type;
3727 atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
3729 g_free (hdlr->name);
3730 hdlr->name = g_strdup (name);
3734 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
3736 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3737 /* Some players (low-end hardware) check for this name, which is what
3738 * QuickTime itself sets */
3739 atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3743 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3745 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3746 /* Some players (low-end hardware) check for this name, which is what
3747 * QuickTime itself sets */
3748 atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3752 atom_mdia_set_hdlr_type_subtitle (AtomMDIA * mdia, AtomsContext * context)
3754 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_sbtl);
3756 /* Just follows the pattern from video and audio above */
3757 atom_hdlr_set_name (&mdia->hdlr, "SubtitleHandler");
3761 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3763 atom_mdia_set_hdlr_type_audio (mdia, context);
3764 atom_minf_set_audio (&mdia->minf);
3768 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3770 atom_mdia_set_hdlr_type_video (mdia, context);
3771 atom_minf_set_video (&mdia->minf, context);
3775 atom_mdia_set_subtitle (AtomMDIA * mdia, AtomsContext * context)
3777 atom_mdia_set_hdlr_type_subtitle (mdia, context);
3778 atom_minf_set_subtitle (&mdia->minf);
3782 atom_tkhd_set_audio (AtomTKHD * tkhd)
3784 tkhd->volume = 0x0100;
3785 tkhd->width = tkhd->height = 0;
3789 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3794 /* qt and ISO base media do not contradict, and examples agree */
3795 tkhd->width = width;
3796 tkhd->height = height;
3800 atom_tkhd_set_subtitle (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3805 /* qt and ISO base media do not contradict, and examples agree */
3806 tkhd->width = width;
3807 tkhd->height = height;
3812 atom_edts_add_entry (AtomEDTS * edts, gint index, EditListEntry * entry)
3815 (EditListEntry *) g_slist_nth_data (edts->elst.entries, index);
3816 /* Create a new entry if missing (appends to the list if index is larger) */
3818 e = g_new (EditListEntry, 1);
3819 edts->elst.entries = g_slist_insert (edts->elst.entries, e, index);
3822 /* Update the entry */
3827 atom_trak_edts_clear (AtomTRAK * trak)
3830 atom_edts_free (trak->edts);
3836 * Update an entry in this trak edits list, creating it if needed.
3837 * index is the index of the entry to update, or create if it's past the end.
3838 * duration is in the moov's timescale
3839 * media_time is the offset in the media time to start from (media's timescale)
3840 * rate is a 32 bits fixed-point
3843 atom_trak_set_elst_entry (AtomTRAK * trak, gint index,
3844 guint32 duration, guint32 media_time, guint32 rate)
3846 EditListEntry entry;
3848 entry.duration = duration;
3849 entry.media_time = media_time;
3850 entry.media_rate = rate;
3852 if (trak->edts == NULL)
3853 trak->edts = atom_edts_new ();
3855 atom_edts_add_entry (trak->edts, index, &entry);
3858 /* re-negotiation is prevented at top-level, so only 1 entry expected.
3859 * Quite some more care here and elsewhere may be needed to
3860 * support several entries */
3861 static SampleTableEntryMP4A *
3862 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
3865 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3866 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
3868 mp4a->se.header.type = type;
3869 mp4a->se.kind = AUDIO;
3870 mp4a->compression_id = -1;
3871 mp4a->se.data_reference_index = 1;
3873 stsd->entries = g_list_prepend (stsd->entries, mp4a);
3878 /* return number of centiframes per second */
3880 atom_framerate_to_timescale (gint n, gint d)
3885 if (d != 1 && d != 1001) {
3886 /* otherwise there are probably rounding errors and we should rather guess
3887 * if it's close enough to a well known framerate */
3888 gst_video_guess_framerate (gst_util_uint64_scale (d, GST_SECOND, n), &n,
3892 return gst_util_uint64_scale (n, 100, d);
3895 static SampleTableEntryTMCD *
3896 atom_trak_add_timecode_entry (AtomTRAK * trak, AtomsContext * context,
3897 guint32 trak_timescale, GstVideoTimeCode * tc)
3899 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3900 SampleTableEntryTMCD *tmcd = sample_entry_tmcd_new ();
3902 g_assert (trak_timescale != 0);
3904 trak->mdia.hdlr.component_type = FOURCC_mhlr;
3905 trak->mdia.hdlr.handler_type = FOURCC_tmcd;
3906 g_free (trak->mdia.hdlr.name);
3907 trak->mdia.hdlr.name = g_strdup ("Time Code Media Handler");
3908 trak->mdia.mdhd.time_info.timescale = trak_timescale;
3910 tmcd->se.kind = TIMECODE;
3911 tmcd->se.data_reference_index = 1;
3912 tmcd->tc_flags = TC_24H_MAX;
3913 if (tc->config.flags &= GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME)
3914 tmcd->tc_flags |= TC_DROP_FRAME;
3915 tmcd->name.language_code = 0;
3916 tmcd->name.name = g_strdup ("Tape");
3917 tmcd->timescale = trak_timescale;
3918 tmcd->frame_duration =
3919 gst_util_uint64_scale (tmcd->timescale, tc->config.fps_d,
3921 if (tc->config.fps_d == 1001)
3922 tmcd->n_frames = tc->config.fps_n / 1000;
3924 tmcd->n_frames = tc->config.fps_n / tc->config.fps_d;
3926 stsd->entries = g_list_prepend (stsd->entries, tmcd);
3931 static SampleTableEntryMP4V *
3932 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
3935 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
3936 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3938 mp4v->se.header.type = type;
3939 mp4v->se.kind = VIDEO;
3940 mp4v->se.data_reference_index = 1;
3941 mp4v->horizontal_resolution = 72 << 16;
3942 mp4v->vertical_resolution = 72 << 16;
3943 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3944 mp4v->spatial_quality = 512;
3945 mp4v->temporal_quality = 512;
3948 stsd->entries = g_list_prepend (stsd->entries, mp4v);
3953 static SampleTableEntryTX3G *
3954 atom_trak_add_subtitle_entry (AtomTRAK * trak, AtomsContext * context,
3957 SampleTableEntryTX3G *tx3g = sample_entry_tx3g_new ();
3958 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3960 tx3g->se.header.type = type;
3961 tx3g->se.kind = SUBTITLE;
3962 tx3g->se.data_reference_index = 1;
3964 stsd->entries = g_list_prepend (stsd->entries, tx3g);
3971 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
3973 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
3977 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
3979 atom_tkhd_set_audio (&trak->tkhd);
3980 atom_mdia_set_audio (&trak->mdia, context);
3984 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
3987 atom_tkhd_set_video (&trak->tkhd, context, width, height);
3988 atom_mdia_set_video (&trak->mdia, context);
3992 atom_trak_set_subtitle (AtomTRAK * trak, AtomsContext * context)
3994 atom_tkhd_set_subtitle (&trak->tkhd, context, 0, 0);
3995 atom_mdia_set_subtitle (&trak->mdia, context);
3999 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
4002 atom_trak_set_audio (trak, context);
4003 trak->mdia.mdhd.time_info.timescale = rate;
4007 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
4008 guint32 rate, guint32 width, guint32 height)
4010 atom_trak_set_video (trak, context, width, height);
4011 trak->mdia.mdhd.time_info.timescale = rate;
4012 trak->tkhd.width = width << 16;
4013 trak->tkhd.height = height << 16;
4017 atom_trak_set_subtitle_commons (AtomTRAK * trak, AtomsContext * context)
4019 atom_trak_set_subtitle (trak, context);
4020 trak->mdia.mdhd.time_info.timescale = 1000;
4022 trak->tkhd.alternate_group = 2; /* same for all subtitles */
4023 trak->tkhd.layer = -1; /* above video (layer 0) */
4027 sample_table_entry_add_ext_atom (SampleTableEntry * ste, AtomInfo * ext)
4029 GList **list = NULL;
4030 if (ste->kind == AUDIO) {
4031 list = &(((SampleTableEntryMP4A *) ste)->extension_atoms);
4032 } else if (ste->kind == VIDEO) {
4033 list = &(((SampleTableEntryMP4V *) ste)->extension_atoms);
4035 g_assert_not_reached ();
4039 *list = g_list_prepend (*list, ext);
4042 SampleTableEntryMP4A *
4043 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
4044 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
4046 SampleTableEntryMP4A *ste;
4048 atom_trak_set_audio_commons (trak, context, scale);
4049 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4050 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
4052 trak->is_video = FALSE;
4053 trak->is_h264 = FALSE;
4055 ste->version = entry->version;
4056 ste->compression_id = entry->compression_id;
4057 ste->sample_size = entry->sample_size;
4058 ste->sample_rate = entry->sample_rate << 16;
4059 ste->channels = entry->channels;
4061 ste->samples_per_packet = entry->samples_per_packet;
4062 ste->bytes_per_sample = entry->bytes_per_sample;
4063 ste->bytes_per_packet = entry->bytes_per_packet;
4064 ste->bytes_per_frame = entry->bytes_per_frame;
4067 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
4069 /* 0 size means variable size */
4070 atom_trak_set_constant_size_samples (trak, sample_size);
4075 SampleTableEntryTMCD *
4076 atom_trak_set_timecode_type (AtomTRAK * trak, AtomsContext * context,
4077 guint32 trak_timescale, GstVideoTimeCode * tc)
4079 SampleTableEntryTMCD *ste;
4080 AtomGMHD *gmhd = trak->mdia.minf.gmhd;
4082 if (context->flavor != ATOMS_TREE_FLAVOR_MOV) {
4086 ste = atom_trak_add_timecode_entry (trak, context, trak_timescale, tc);
4088 gmhd = atom_gmhd_new ();
4089 gmhd->gmin.graphics_mode = 0x0040;
4090 gmhd->gmin.opcolor[0] = 0x8000;
4091 gmhd->gmin.opcolor[1] = 0x8000;
4092 gmhd->gmin.opcolor[2] = 0x8000;
4093 gmhd->tmcd = atom_tmcd_new ();
4094 gmhd->tmcd->tcmi.text_size = 12;
4095 gmhd->tmcd->tcmi.font_name = g_strdup ("Chicago"); /* Pascal string */
4097 trak->mdia.minf.gmhd = gmhd;
4098 trak->is_video = FALSE;
4099 trak->is_h264 = FALSE;
4105 atom_trak_set_caption_type (AtomTRAK * trak, AtomsContext * context,
4106 guint32 trak_timescale, guint32 caption_type)
4108 SampleTableEntry *ste;
4109 AtomGMHD *gmhd = trak->mdia.minf.gmhd;
4110 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
4112 if (context->flavor != ATOMS_TREE_FLAVOR_MOV) {
4116 trak->mdia.mdhd.time_info.timescale = trak_timescale;
4117 trak->mdia.hdlr.component_type = FOURCC_mhlr;
4118 trak->mdia.hdlr.handler_type = FOURCC_clcp;
4119 g_free (trak->mdia.hdlr.name);
4120 trak->mdia.hdlr.name = g_strdup ("Closed Caption Media Handler");
4122 ste = g_new0 (SampleTableEntry, 1);
4123 atom_sample_entry_init (ste, caption_type);
4124 ste->kind = CLOSEDCAPTION;
4125 ste->data_reference_index = 1;
4126 stsd->entries = g_list_prepend (stsd->entries, ste);
4129 gmhd = atom_gmhd_new ();
4130 gmhd->gmin.graphics_mode = 0x0040;
4131 gmhd->gmin.opcolor[0] = 0x8000;
4132 gmhd->gmin.opcolor[1] = 0x8000;
4133 gmhd->gmin.opcolor[2] = 0x8000;
4135 trak->mdia.minf.gmhd = gmhd;
4136 trak->is_video = FALSE;
4137 trak->is_h264 = FALSE;
4143 build_pasp_extension (gint par_width, gint par_height)
4145 AtomData *atom_data = atom_data_new (FOURCC_pasp);
4148 atom_data_alloc_mem (atom_data, 8);
4149 data = atom_data->data;
4151 /* ihdr = image header box */
4152 GST_WRITE_UINT32_BE (data, par_width);
4153 GST_WRITE_UINT32_BE (data + 4, par_height);
4155 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4160 build_fiel_extension (GstVideoInterlaceMode mode, GstVideoFieldOrder order)
4162 AtomData *atom_data = atom_data_new (FOURCC_fiel);
4167 atom_data_alloc_mem (atom_data, 2);
4168 data = atom_data->data;
4170 if (mode == GST_VIDEO_INTERLACE_MODE_PROGRESSIVE) {
4173 } else if (mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED) {
4175 field_order = order == GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST ? 9 : 14;
4181 GST_WRITE_UINT8 (data, interlace);
4182 GST_WRITE_UINT8 (data + 1, field_order);
4184 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4189 build_colr_extension (const GstVideoColorimetry * colorimetry, gboolean is_mp4)
4191 AtomData *atom_data = atom_data_new (FOURCC_colr);
4194 guint16 transfer_function;
4197 switch (colorimetry->primaries) {
4198 case GST_VIDEO_COLOR_PRIMARIES_BT709:
4201 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
4204 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
4205 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
4208 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
4211 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
4217 switch (colorimetry->transfer) {
4218 case GST_VIDEO_TRANSFER_BT709:
4219 transfer_function = 1;
4221 case GST_VIDEO_TRANSFER_SMPTE240M:
4222 transfer_function = 7;
4224 case GST_VIDEO_TRANSFER_UNKNOWN:
4226 transfer_function = 2;
4230 switch (colorimetry->matrix) {
4231 case GST_VIDEO_COLOR_MATRIX_BT709:
4234 case GST_VIDEO_COLOR_MATRIX_BT601:
4237 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
4240 case GST_VIDEO_COLOR_MATRIX_BT2020:
4243 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
4249 atom_data_alloc_mem (atom_data, 10 + (is_mp4 ? 1 : 0));
4250 data = atom_data->data;
4252 /* colour specification box */
4254 GST_WRITE_UINT32_LE (data, FOURCC_nclx);
4256 GST_WRITE_UINT32_LE (data, FOURCC_nclc);
4258 GST_WRITE_UINT16_BE (data + 4, primaries);
4259 GST_WRITE_UINT16_BE (data + 6, transfer_function);
4260 GST_WRITE_UINT16_BE (data + 8, matrix);
4263 GST_WRITE_UINT8 (data + 10,
4264 colorimetry->range == GST_VIDEO_COLOR_RANGE_0_255 ? 0x80 : 0x00);
4267 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4272 build_clap_extension (gint width_n, gint width_d, gint height_n, gint height_d,
4273 gint h_off_n, gint h_off_d, gint v_off_n, gint v_off_d)
4275 AtomData *atom_data = atom_data_new (FOURCC_clap);
4278 atom_data_alloc_mem (atom_data, 32);
4279 data = atom_data->data;
4281 GST_WRITE_UINT32_BE (data, width_n);
4282 GST_WRITE_UINT32_BE (data + 4, width_d);
4283 GST_WRITE_UINT32_BE (data + 8, height_n);
4284 GST_WRITE_UINT32_BE (data + 12, height_d);
4285 GST_WRITE_UINT32_BE (data + 16, h_off_n);
4286 GST_WRITE_UINT32_BE (data + 20, h_off_d);
4287 GST_WRITE_UINT32_BE (data + 24, v_off_n);
4288 GST_WRITE_UINT32_BE (data + 28, v_off_d);
4290 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4295 build_tapt_extension (gint clef_width, gint clef_height, gint prof_width,
4296 gint prof_height, gint enof_width, gint enof_height)
4298 AtomData *atom_data = atom_data_new (FOURCC_tapt);
4301 atom_data_alloc_mem (atom_data, 60);
4302 data = atom_data->data;
4304 GST_WRITE_UINT32_BE (data, 20);
4305 GST_WRITE_UINT32_LE (data + 4, FOURCC_clef);
4306 GST_WRITE_UINT32_BE (data + 8, 0);
4307 GST_WRITE_UINT32_BE (data + 12, clef_width);
4308 GST_WRITE_UINT32_BE (data + 16, clef_height);
4310 GST_WRITE_UINT32_BE (data + 20, 20);
4311 GST_WRITE_UINT32_LE (data + 24, FOURCC_prof);
4312 GST_WRITE_UINT32_BE (data + 28, 0);
4313 GST_WRITE_UINT32_BE (data + 32, prof_width);
4314 GST_WRITE_UINT32_BE (data + 36, prof_height);
4316 GST_WRITE_UINT32_BE (data + 40, 20);
4317 GST_WRITE_UINT32_LE (data + 44, FOURCC_enof);
4318 GST_WRITE_UINT32_BE (data + 48, 0);
4319 GST_WRITE_UINT32_BE (data + 52, enof_width);
4320 GST_WRITE_UINT32_BE (data + 56, enof_height);
4323 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4328 build_mov_video_sample_description_padding_extension (void)
4330 AtomData *atom_data = atom_data_new (FOURCC_clap);
4332 return build_atom_info_wrapper ((Atom *) atom_data, atom_copy_empty,
4336 SampleTableEntryMP4V *
4337 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
4338 VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
4340 SampleTableEntryMP4V *ste;
4341 guint dwidth, dheight;
4342 gint par_n = 0, par_d = 0;
4344 par_n = entry->par_n;
4345 par_d = entry->par_d;
4347 dwidth = entry->width;
4348 dheight = entry->height;
4349 /* ISO file spec says track header w/h indicates track's visual presentation
4350 * (so this together with pixels w/h implicitly defines PAR) */
4351 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
4352 dwidth = entry->width * par_n / par_d;
4353 dheight = entry->height;
4356 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
4357 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4358 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
4360 trak->is_video = TRUE;
4361 trak->is_h264 = (entry->fourcc == FOURCC_avc1
4362 || entry->fourcc == FOURCC_avc3);
4364 ste->version = entry->version;
4365 ste->width = entry->width;
4366 ste->height = entry->height;
4367 ste->depth = entry->depth;
4368 ste->color_table_id = entry->color_table_id;
4369 ste->frame_count = entry->frame_count;
4372 ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
4374 ste->extension_atoms = g_list_append (ste->extension_atoms,
4375 build_pasp_extension (par_n, par_d));
4377 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
4378 /* append 0 as a terminator "length" to work around some broken software */
4379 ste->extension_atoms =
4380 g_list_append (ste->extension_atoms,
4381 build_mov_video_sample_description_padding_extension ());
4388 subtitle_sample_entry_init (SubtitleSampleEntry * entry)
4390 entry->font_size = 0;
4391 entry->font_face = 0;
4392 entry->foreground_color_rgba = 0xFFFFFFFF; /* all white, opaque */
4395 SampleTableEntryTX3G *
4396 atom_trak_set_subtitle_type (AtomTRAK * trak, AtomsContext * context,
4397 SubtitleSampleEntry * entry)
4399 SampleTableEntryTX3G *tx3g;
4401 atom_trak_set_subtitle_commons (trak, context);
4402 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4403 tx3g = atom_trak_add_subtitle_entry (trak, context, entry->fourcc);
4405 tx3g->font_face = entry->font_face;
4406 tx3g->font_size = entry->font_size;
4407 tx3g->foreground_color_rgba = entry->foreground_color_rgba;
4409 trak->is_video = FALSE;
4410 trak->is_h264 = FALSE;
4416 atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
4418 guint8 flags[3] = { 0, 0, 0 };
4420 atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
4421 mfhd->sequence_number = sequence_number;
4425 atom_moof_init (AtomMOOF * moof, AtomsContext * context,
4426 guint32 sequence_number)
4428 atom_header_set (&moof->header, FOURCC_moof, 0, 0);
4429 atom_mfhd_init (&moof->mfhd, sequence_number);
4434 atom_moof_new (AtomsContext * context, guint32 sequence_number)
4436 AtomMOOF *moof = g_new0 (AtomMOOF, 1);
4438 atom_moof_init (moof, context, sequence_number);
4443 atom_trun_free (AtomTRUN * trun)
4445 atom_full_clear (&trun->header);
4446 atom_array_clear (&trun->entries);
4451 atom_sdtp_free (AtomSDTP * sdtp)
4453 atom_full_clear (&sdtp->header);
4454 atom_array_clear (&sdtp->entries);
4459 atom_traf_free (AtomTRAF * traf)
4463 walker = traf->truns;
4465 atom_trun_free ((AtomTRUN *) walker->data);
4466 walker = g_list_next (walker);
4468 g_list_free (traf->truns);
4471 walker = traf->sdtps;
4473 atom_sdtp_free ((AtomSDTP *) walker->data);
4474 walker = g_list_next (walker);
4476 g_list_free (traf->sdtps);
4483 atom_moof_free (AtomMOOF * moof)
4487 walker = moof->trafs;
4489 atom_traf_free ((AtomTRAF *) walker->data);
4490 walker = g_list_next (walker);
4492 g_list_free (moof->trafs);
4499 atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
4502 guint64 original_offset = *offset;
4504 if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
4508 prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
4510 atom_write_size (buffer, size, offset, original_offset);
4511 return *offset - original_offset;
4515 atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
4518 guint64 original_offset = *offset;
4521 if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
4525 prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
4527 flags = atom_full_get_flags_as_uint (&tfhd->header);
4529 if (flags & TF_BASE_DATA_OFFSET)
4530 prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
4531 if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
4532 prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
4533 if (flags & TF_DEFAULT_SAMPLE_DURATION)
4534 prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
4535 if (flags & TF_DEFAULT_SAMPLE_SIZE)
4536 prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
4537 if (flags & TF_DEFAULT_SAMPLE_FLAGS)
4538 prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
4540 atom_write_size (buffer, size, offset, original_offset);
4541 return *offset - original_offset;
4545 atom_tfdt_copy_data (AtomTFDT * tfdt, guint8 ** buffer, guint64 * size,
4548 guint64 original_offset = *offset;
4550 if (!atom_full_copy_data (&tfdt->header, buffer, size, offset)) {
4554 /* 32-bit time if version == 0 else 64-bit: */
4555 if (tfdt->header.version == 0)
4556 prop_copy_uint32 (tfdt->base_media_decode_time, buffer, size, offset);
4558 prop_copy_uint64 (tfdt->base_media_decode_time, buffer, size, offset);
4560 atom_write_size (buffer, size, offset, original_offset);
4561 return *offset - original_offset;
4565 atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
4566 guint64 * offset, guint32 * data_offset)
4568 guint64 original_offset = *offset;
4571 flags = atom_full_get_flags_as_uint (&trun->header);
4573 /* if first trun in moof, forcibly add data_offset and record
4574 * where it must be written later on */
4575 if (data_offset && !*data_offset) {
4576 flags |= TR_DATA_OFFSET;
4578 flags &= ~TR_DATA_OFFSET;
4581 atom_full_set_flags_as_uint (&trun->header, flags);
4583 if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
4587 prop_copy_uint32 (trun->sample_count, buffer, size, offset);
4589 if (flags & TR_DATA_OFFSET) {
4590 *data_offset = *offset;
4591 prop_copy_int32 (trun->data_offset, buffer, size, offset);
4593 if (flags & TR_FIRST_SAMPLE_FLAGS)
4594 prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
4596 for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
4597 TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
4599 if (flags & TR_SAMPLE_DURATION)
4600 prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
4601 if (flags & TR_SAMPLE_SIZE)
4602 prop_copy_uint32 (entry->sample_size, buffer, size, offset);
4603 if (flags & TR_SAMPLE_FLAGS)
4604 prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
4605 if (flags & TR_COMPOSITION_TIME_OFFSETS)
4606 prop_copy_uint32 (entry->sample_composition_time_offset,
4607 buffer, size, offset);
4610 atom_write_size (buffer, size, offset, original_offset);
4611 return *offset - original_offset;
4615 atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
4618 guint64 original_offset = *offset;
4620 if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
4624 /* all entries at once */
4625 prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
4626 atom_array_get_len (&sdtp->entries), buffer, size, offset);
4628 atom_write_size (buffer, size, offset, original_offset);
4629 return *offset - original_offset;
4633 atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
4634 guint64 * offset, guint32 * data_offset)
4636 guint64 original_offset = *offset;
4639 if (!atom_copy_data (&traf->header, buffer, size, offset)) {
4642 if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
4645 if (!atom_tfdt_copy_data (&traf->tfdt, buffer, size, offset)) {
4649 walker = g_list_first (traf->truns);
4650 while (walker != NULL) {
4651 if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset,
4655 walker = g_list_next (walker);
4658 walker = g_list_first (traf->sdtps);
4659 while (walker != NULL) {
4660 if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
4663 walker = g_list_next (walker);
4666 atom_write_size (buffer, size, offset, original_offset);
4667 return *offset - original_offset;
4670 /* creates moof atom; metadata is written expecting actual buffer data
4671 * is in mdata directly after moof, and is consecutively written per trak */
4673 atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
4674 guint64 * size, guint64 * offset)
4676 guint64 original_offset = *offset;
4678 guint32 data_offset = 0;
4680 if (!atom_copy_data (&moof->header, buffer, size, offset))
4683 if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
4686 walker = g_list_first (moof->trafs);
4687 while (walker != NULL) {
4688 if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset,
4692 walker = g_list_next (walker);
4695 atom_write_size (buffer, size, offset, original_offset);
4697 if (*buffer && data_offset) {
4698 /* first trun needs a data-offset relative to moof start
4699 * = moof size + mdat prefix */
4700 GST_WRITE_UINT32_BE (*buffer + data_offset, *offset - original_offset + 8);
4703 return *offset - original_offset;
4707 atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
4709 guint8 flags[3] = { 0, 0, 0 };
4711 atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
4712 tfhd->track_ID = track_ID;
4713 tfhd->base_data_offset = 0;
4714 tfhd->sample_description_index = 1;
4715 tfhd->default_sample_duration = 0;
4716 tfhd->default_sample_size = 0;
4717 tfhd->default_sample_flags = 0;
4721 atom_tfdt_init (AtomTFDT * tfdt)
4723 guint8 flags[3] = { 0, 0, 0 };
4724 atom_full_init (&tfdt->header, FOURCC_tfdt, 0, 0, 0, flags);
4726 tfdt->base_media_decode_time = 0;
4730 atom_trun_init (AtomTRUN * trun)
4732 guint8 flags[3] = { 0, 0, 0 };
4734 atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
4735 trun->sample_count = 0;
4736 trun->data_offset = 0;
4737 trun->first_sample_flags = 0;
4738 atom_array_init (&trun->entries, 512);
4742 atom_trun_new (void)
4744 AtomTRUN *trun = g_new0 (AtomTRUN, 1);
4746 atom_trun_init (trun);
4751 atom_sdtp_init (AtomSDTP * sdtp)
4753 guint8 flags[3] = { 0, 0, 0 };
4755 atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
4756 atom_array_init (&sdtp->entries, 512);
4760 atom_sdtp_new (AtomsContext * context)
4762 AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
4764 atom_sdtp_init (sdtp);
4769 atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
4771 traf->sdtps = g_list_append (traf->sdtps, sdtp);
4775 atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
4777 /* it does not make much/any sense according to specs,
4778 * but that's how MS isml samples seem to do it */
4779 atom_array_append (&sdtp->entries, val, 256);
4783 atom_trun_add_samples (AtomTRUN * trun, guint32 delta, guint32 size,
4784 guint32 flags, gint64 pts_offset)
4786 TRUNSampleEntry nentry;
4788 if (pts_offset != 0)
4789 trun->header.flags[1] |= (TR_COMPOSITION_TIME_OFFSETS >> 8);
4791 nentry.sample_duration = delta;
4792 nentry.sample_size = size;
4793 nentry.sample_flags = flags;
4794 nentry.sample_composition_time_offset = pts_offset;
4795 atom_array_append (&trun->entries, nentry, 256);
4796 trun->sample_count++;
4800 atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
4802 atom_header_set (&traf->header, FOURCC_traf, 0, 0);
4803 atom_tfdt_init (&traf->tfdt);
4804 atom_tfhd_init (&traf->tfhd, track_ID);
4807 if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
4808 atom_traf_add_sdtp (traf, atom_sdtp_new (context));
4812 atom_traf_new (AtomsContext * context, guint32 track_ID)
4814 AtomTRAF *traf = g_new0 (AtomTRAF, 1);
4816 atom_traf_init (traf, context, track_ID);
4821 atom_traf_set_base_decode_time (AtomTRAF * traf, guint64 base_decode_time)
4823 traf->tfdt.base_media_decode_time = base_decode_time;
4824 /* If we need to write a 64-bit tfdt, set the atom version */
4825 if (base_decode_time > G_MAXUINT32)
4826 traf->tfdt.header.version = 1;
4830 atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
4832 traf->truns = g_list_append (traf->truns, trun);
4836 atom_traf_add_samples (AtomTRAF * traf, guint32 delta, guint32 size,
4837 gboolean sync, gint64 pts_offset, gboolean sdtp_sync)
4842 /* 0x10000 is sample-is-difference-sample flag
4843 * low byte stuff is what ismv uses */
4844 flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
4846 if (G_UNLIKELY (!traf->truns)) {
4847 trun = atom_trun_new ();
4848 atom_traf_add_trun (traf, trun);
4849 /* optimistic; indicate all defaults present in tfhd */
4850 traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
4851 TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
4852 traf->tfhd.default_sample_duration = delta;
4853 traf->tfhd.default_sample_size = size;
4854 traf->tfhd.default_sample_flags = flags;
4855 trun->first_sample_flags = flags;
4858 trun = traf->truns->data;
4860 /* check if still matching defaults,
4861 * if not, abandon default and need entry for each sample */
4862 if (traf->tfhd.default_sample_duration != delta) {
4863 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
4864 trun->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
4866 if (traf->tfhd.default_sample_size != size) {
4867 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
4868 trun->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
4870 if (traf->tfhd.default_sample_flags != flags) {
4871 if (trun->sample_count == 1) {
4872 /* at least will need first sample flag */
4873 traf->tfhd.default_sample_flags = flags;
4874 trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
4876 /* now we need sample flags for each sample */
4877 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
4878 trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
4879 trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
4883 atom_trun_add_samples (traf->truns->data, delta, size, flags, pts_offset);
4886 atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
4890 atom_traf_get_sample_num (AtomTRAF * traf)
4894 if (G_UNLIKELY (!traf->truns))
4897 trun = traf->truns->data;
4898 return atom_array_get_len (&trun->entries);
4902 atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
4904 moof->trafs = g_list_append (moof->trafs, traf);
4908 atom_tfra_free (AtomTFRA * tfra)
4910 atom_full_clear (&tfra->header);
4911 atom_array_clear (&tfra->entries);
4916 atom_mfra_new (AtomsContext * context)
4918 AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
4920 atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
4925 atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
4927 mfra->tfras = g_list_append (mfra->tfras, tfra);
4931 atom_mfra_free (AtomMFRA * mfra)
4935 walker = mfra->tfras;
4937 atom_tfra_free ((AtomTFRA *) walker->data);
4938 walker = g_list_next (walker);
4940 g_list_free (mfra->tfras);
4943 atom_clear (&mfra->header);
4948 atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
4950 guint8 flags[3] = { 0, 0, 0 };
4952 atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
4953 tfra->track_ID = track_ID;
4954 atom_array_init (&tfra->entries, 512);
4958 atom_tfra_new (AtomsContext * context, guint32 track_ID)
4960 AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
4962 atom_tfra_init (tfra, track_ID);
4968 need_bytes (guint32 num)
4979 atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
4985 entry.moof_offset = 0;
4986 /* always write a single trun in a single traf */
4987 entry.traf_number = 1;
4988 entry.trun_number = 1;
4989 entry.sample_number = sample_num;
4991 /* auto-use 64 bits if needed */
4992 if (dts > G_MAXUINT32)
4993 tfra->header.version = 1;
4995 /* 1 byte will always do for traf and trun number,
4996 * check how much sample_num needs */
4997 tfra->lengths = (tfra->lengths & 0xfc) ||
4998 MAX (tfra->lengths, need_bytes (sample_num));
5000 atom_array_append (&tfra->entries, entry, 256);
5004 atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
5008 /* auto-use 64 bits if needed */
5009 if (offset > G_MAXUINT32)
5010 tfra->header.version = 1;
5012 for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
5013 TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
5015 if (entry->moof_offset)
5017 entry->moof_offset = offset;
5022 atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
5025 guint64 original_offset = *offset;
5032 if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
5036 prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
5037 prop_copy_uint32 (tfra->lengths, buffer, size, offset);
5038 prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
5040 version = tfra->header.version;
5041 for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
5042 entry = &atom_array_index (&tfra->entries, i);
5044 prop_copy_uint64 (entry->time, buffer, size, offset);
5045 prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
5047 prop_copy_uint32 (entry->time, buffer, size, offset);
5048 prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
5051 bytes = (tfra->lengths & (0x3 << 4)) + 1;
5052 data = GUINT32_TO_BE (entry->traf_number);
5053 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5054 buffer, size, offset);
5056 bytes = (tfra->lengths & (0x3 << 2)) + 1;
5057 data = GUINT32_TO_BE (entry->trun_number);
5058 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5059 buffer, size, offset);
5061 bytes = (tfra->lengths & (0x3)) + 1;
5062 data = GUINT32_TO_BE (entry->sample_number);
5063 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5064 buffer, size, offset);
5068 atom_write_size (buffer, size, offset, original_offset);
5069 return *offset - original_offset;
5073 atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
5076 guint64 original_offset = *offset;
5077 guint8 flags[3] = { 0, 0, 0 };
5080 atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
5082 if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
5086 prop_copy_uint32 (s, buffer, size, offset);
5088 atom_write_size (buffer, size, offset, original_offset);
5090 return *offset - original_offset;
5095 atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
5098 guint64 original_offset = *offset;
5101 if (!atom_copy_data (&mfra->header, buffer, size, offset))
5104 walker = g_list_first (mfra->tfras);
5105 while (walker != NULL) {
5106 if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
5109 walker = g_list_next (walker);
5112 /* 16 is the size of the mfro atom */
5113 if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
5117 atom_write_size (buffer, size, offset, original_offset);
5118 return *offset - original_offset;
5121 /* some sample description construction helpers */
5124 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
5125 const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
5130 track_id = trak->tkhd.track_ID;
5132 esds = atom_esds_new ();
5133 esds->es.id = track_id & 0xFFFF;
5134 esds->es.dec_conf_desc.object_type = object_type;
5135 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
5137 if (avg_bitrate > 0)
5138 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
5139 if (max_bitrate > 0)
5140 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
5142 /* optional DecoderSpecificInfo */
5144 DecoderSpecificInfoDescriptor *desc;
5147 esds->es.dec_conf_desc.dec_specific_info = desc =
5148 desc_dec_specific_info_new ();
5149 size = gst_buffer_get_size ((GstBuffer *) codec_data);
5150 desc_dec_specific_info_alloc_data (desc, size);
5151 gst_buffer_extract ((GstBuffer *) codec_data, 0, desc->data, size);
5154 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
5159 build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
5160 guint32 max_bitrate)
5162 AtomData *atom_data = atom_data_new (FOURCC_btrt);
5165 atom_data_alloc_mem (atom_data, 12);
5166 data = atom_data->data;
5168 GST_WRITE_UINT32_BE (data, buffer_size_db);
5169 GST_WRITE_UINT32_BE (data + 4, max_bitrate);
5170 GST_WRITE_UINT32_BE (data + 8, avg_bitrate);
5172 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5177 build_mov_wave_extension (guint32 fourcc, AtomInfo * atom1, AtomInfo * atom2,
5178 gboolean terminator)
5184 /* Build WAVE atom for sample table entry */
5185 wave = atom_wave_new ();
5187 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5189 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5190 wave->extension_atoms =
5191 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5192 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5195 /* Add supplied atoms to WAVE */
5197 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
5199 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
5201 /* Add FRMA to the WAVE */
5202 frma = atom_frma_new ();
5203 frma->media_type = fourcc;
5205 wave->extension_atoms =
5206 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5207 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5209 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5214 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
5215 guint32 avg_bitrate, guint32 max_bitrate)
5217 AtomInfo *esds, *mp4a;
5221 /* Add ESDS atom to WAVE */
5222 esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
5223 ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
5225 /* Add MP4A atom to the WAVE:
5226 * not really in spec, but makes offset based players happy */
5227 buf = GST_BUFFER_NEW_READONLY (&tmp, 4);
5228 mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
5229 gst_buffer_unref (buf);
5231 return build_mov_wave_extension (FOURCC_mp4a, mp4a, esds, TRUE);
5235 build_mov_alac_extension (const GstBuffer * codec_data)
5239 alac = build_codec_data_extension (FOURCC_alac, codec_data);
5241 return build_mov_wave_extension (FOURCC_alac, NULL, alac, TRUE);
5245 build_jp2x_extension (const GstBuffer * prefix)
5247 AtomData *atom_data;
5253 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2x, prefix);
5255 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5260 build_jp2h_extension (gint width, gint height, const gchar * colorspace,
5261 gint ncomp, const GValue * cmap_array, const GValue * cdef_array)
5263 AtomData *atom_data;
5267 gint idhr_size = 22;
5268 gint colr_size = 15;
5269 gint cmap_size = 0, cdef_size = 0;
5270 gint cmap_array_size = 0;
5271 gint cdef_array_size = 0;
5272 GstByteWriter writer;
5274 g_return_val_if_fail (cmap_array == NULL ||
5275 GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
5276 g_return_val_if_fail (cdef_array == NULL ||
5277 GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
5279 if (g_str_equal (colorspace, "sRGB")) {
5283 } else if (g_str_equal (colorspace, "GRAY")) {
5287 } else if (g_str_equal (colorspace, "sYUV")) {
5295 cmap_array_size = gst_value_array_get_size (cmap_array);
5296 cmap_size = 8 + cmap_array_size * 4;
5299 cdef_array_size = gst_value_array_get_size (cdef_array);
5300 cdef_size = 8 + 2 + cdef_array_size * 6;
5303 gst_byte_writer_init_with_size (&writer,
5304 idhr_size + colr_size + cmap_size + cdef_size, TRUE);
5306 /* ihdr = image header box */
5307 gst_byte_writer_put_uint32_be_unchecked (&writer, 22);
5308 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_ihdr);
5309 gst_byte_writer_put_uint32_be_unchecked (&writer, height);
5310 gst_byte_writer_put_uint32_be_unchecked (&writer, width);
5311 gst_byte_writer_put_uint16_be_unchecked (&writer, ncomp);
5312 /* 8 bits per component, unsigned */
5313 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5314 /* compression type; reserved */
5315 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5316 /* colour space (un)known */
5317 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5318 /* intellectual property right (box present) */
5319 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5321 /* colour specification box */
5322 gst_byte_writer_put_uint32_be_unchecked (&writer, 15);
5323 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_colr);
5325 /* specification method: enumerated */
5326 gst_byte_writer_put_uint8_unchecked (&writer, 0x1);
5327 /* precedence; reserved */
5328 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5329 /* approximation; reserved */
5330 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5331 /* enumerated colourspace */
5332 gst_byte_writer_put_uint32_be_unchecked (&writer, cenum);
5335 gst_byte_writer_put_uint32_be_unchecked (&writer, cmap_size);
5336 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cmap);
5337 for (i = 0; i < cmap_array_size; i++) {
5343 item = gst_value_array_get_value (cmap_array, i);
5344 value = g_value_get_int (item);
5346 /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
5347 cmp = value & 0xFFFF;
5349 pcol = (value >> 16) & 0xFF;
5352 GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
5353 "handle Pallete mapping atoms yet");
5355 gst_byte_writer_put_uint16_be_unchecked (&writer, cmp);
5356 gst_byte_writer_put_uint8_unchecked (&writer, mtyp);
5357 gst_byte_writer_put_uint8_unchecked (&writer, pcol);
5362 gst_byte_writer_put_uint32_be_unchecked (&writer, cdef_size);
5363 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cdef);
5364 gst_byte_writer_put_uint16_be_unchecked (&writer, cdef_array_size);
5365 for (i = 0; i < cdef_array_size; i++) {
5368 item = gst_value_array_get_value (cdef_array, i);
5369 value = g_value_get_int (item);
5371 gst_byte_writer_put_uint16_be_unchecked (&writer, i);
5373 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5374 gst_byte_writer_put_uint16_be_unchecked (&writer, value);
5375 } else if (value < 0) {
5376 gst_byte_writer_put_uint16_be_unchecked (&writer, -value);
5377 gst_byte_writer_put_uint16_be_unchecked (&writer, 0); /* TODO what here? */
5379 gst_byte_writer_put_uint16_be_unchecked (&writer, 1);
5380 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5385 g_assert (gst_byte_writer_get_remaining (&writer) == 0);
5386 buf = gst_byte_writer_reset_and_get_buffer (&writer);
5388 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
5389 gst_buffer_unref (buf);
5391 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5396 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
5399 AtomInfo *result = NULL;
5402 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
5403 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
5411 build_amr_extension (void)
5418 GST_WRITE_UINT32_LE (ext, 0);
5419 /* decoder version */
5420 GST_WRITE_UINT8 (ext + 4, 0);
5421 /* mode set (all modes) */
5422 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
5423 /* mode change period (no restriction) */
5424 GST_WRITE_UINT8 (ext + 7, 0);
5425 /* frames per sample */
5426 GST_WRITE_UINT8 (ext + 8, 1);
5428 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5429 res = build_codec_data_extension (FOURCC_damr, buf);
5430 gst_buffer_unref (buf);
5435 build_h263_extension (void)
5442 GST_WRITE_UINT32_LE (ext, 0);
5443 /* decoder version */
5444 GST_WRITE_UINT8 (ext + 4, 0);
5445 /* level / profile */
5446 /* FIXME ? maybe ? obtain somewhere; baseline for now */
5447 GST_WRITE_UINT8 (ext + 5, 10);
5448 GST_WRITE_UINT8 (ext + 6, 0);
5450 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5451 res = build_codec_data_extension (FOURCC_d263, buf);
5452 gst_buffer_unref (buf);
5457 build_gama_atom (gdouble gamma)
5463 /* convert to uint32 from fixed point */
5464 gamma_fp = (guint32) 65536 *gamma;
5466 gamma_fp = GUINT32_TO_BE (gamma_fp);
5467 buf = GST_BUFFER_NEW_READONLY (&gamma_fp, 4);
5468 res = build_codec_data_extension (FOURCC_gama, buf);
5469 gst_buffer_unref (buf);
5474 build_SMI_atom (const GstBuffer * seqh)
5481 /* the seqh plus its size and fourcc */
5482 size = gst_buffer_get_size ((GstBuffer *) seqh);
5483 data = g_malloc (size + 8);
5485 GST_WRITE_UINT32_LE (data, FOURCC_SEQH);
5486 GST_WRITE_UINT32_BE (data + 4, size + 8);
5487 gst_buffer_extract ((GstBuffer *) seqh, 0, data + 8, size);
5488 buf = gst_buffer_new_wrapped (data, size + 8);
5489 res = build_codec_data_extension (FOURCC_SMI_, buf);
5490 gst_buffer_unref (buf);
5495 build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
5497 #define IMA_ADPCM_ATOM_SIZE 20
5498 AtomData *atom_data;
5501 gint samplesperblock;
5504 /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
5505 identifier. Note that the identifier here is big-endian, but when used
5506 within the WAVE header (below), it's little endian. */
5507 fourcc = MS_WAVE_FOURCC (0x11);
5509 atom_data = atom_data_new (fourcc);
5510 atom_data_alloc_mem (atom_data, IMA_ADPCM_ATOM_SIZE);
5511 data = atom_data->data;
5513 /* This atom's content is a WAVE header, including 2 bytes of extra data.
5514 Note that all of this is little-endian, unlike most stuff in qt. */
5515 /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
5516 for the rest. Simplifies to this. */
5517 samplesperblock = 2 * blocksize / channels - 7;
5518 bytespersec = rate * blocksize / samplesperblock;
5519 GST_WRITE_UINT16_LE (data, 0x11);
5520 GST_WRITE_UINT16_LE (data + 2, channels);
5521 GST_WRITE_UINT32_LE (data + 4, rate);
5522 GST_WRITE_UINT32_LE (data + 8, bytespersec);
5523 GST_WRITE_UINT16_LE (data + 12, blocksize);
5524 GST_WRITE_UINT16_LE (data + 14, 4);
5525 GST_WRITE_UINT16_LE (data + 16, 2); /* Two extra bytes */
5526 GST_WRITE_UINT16_LE (data + 18, samplesperblock);
5528 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5533 build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
5540 wave = atom_wave_new ();
5542 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5543 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5544 wave->extension_atoms =
5545 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5546 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5548 /* Add wave ima adpcm atom to WAVE */
5549 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
5550 build_ima_adpcm_atom (channels, rate, blocksize));
5552 /* Add FRMA to the WAVE */
5553 frma = atom_frma_new ();
5554 frma->media_type = MS_WAVE_FOURCC (0x11);
5556 wave->extension_atoms =
5557 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5558 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5560 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5565 build_ac3_extension (guint8 fscod, guint8 bsid, guint8 bsmod, guint8 acmod,
5566 guint8 lfe_on, guint8 bitrate_code)
5568 AtomData *atom_data = atom_data_new (FOURCC_dac3);
5571 atom_data_alloc_mem (atom_data, 3);
5572 data = atom_data->data;
5574 /* Bits from the spec
5584 /* Some bit manipulation magic. Need bitwriter */
5585 data[0] = (fscod << 6) | (bsid << 1) | ((bsmod >> 2) & 1);
5587 ((bsmod & 0x3) << 6) | (acmod << 3) | ((lfe_on & 1) << 2) | ((bitrate_code
5589 data[2] = ((bitrate_code & 0x7) << 5);
5591 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5596 build_opus_extension (guint32 rate, guint8 channels, guint8 mapping_family,
5597 guint8 stream_count, guint8 coupled_count, guint8 channel_mapping[256],
5598 guint16 pre_skip, guint16 output_gain)
5600 AtomData *atom_data;
5603 gboolean hdl = TRUE;
5604 guint data_block_len;
5606 gst_byte_writer_init (&bw);
5607 hdl &= gst_byte_writer_put_uint8 (&bw, 0x00); /* version number */
5608 hdl &= gst_byte_writer_put_uint8 (&bw, channels);
5609 hdl &= gst_byte_writer_put_uint16_le (&bw, pre_skip);
5610 hdl &= gst_byte_writer_put_uint32_le (&bw, rate);
5611 hdl &= gst_byte_writer_put_uint16_le (&bw, output_gain);
5612 hdl &= gst_byte_writer_put_uint8 (&bw, mapping_family);
5613 if (mapping_family > 0) {
5614 hdl &= gst_byte_writer_put_uint8 (&bw, stream_count);
5615 hdl &= gst_byte_writer_put_uint8 (&bw, coupled_count);
5616 hdl &= gst_byte_writer_put_data (&bw, channel_mapping, channels);
5620 GST_WARNING ("Error creating header");
5624 data_block_len = gst_byte_writer_get_size (&bw);
5625 data_block = gst_byte_writer_reset_and_get_data (&bw);
5626 atom_data = atom_data_new_from_data (FOURCC_dops, data_block, data_block_len);
5627 g_free (data_block);
5629 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5634 build_uuid_xmp_atom (GstBuffer * xmp_data)
5638 static const guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
5639 0x97, 0xA9, 0x42, 0xE8,
5640 0x9C, 0x71, 0x99, 0x94,
5641 0x91, 0xE3, 0xAF, 0xAC
5644 if (xmp_data == NULL)
5647 uuid = atom_uuid_new ();
5648 memcpy (uuid->uuid, xmp_uuid, 16);
5650 size = gst_buffer_get_size (xmp_data);
5651 uuid->data = g_malloc (size);
5652 uuid->datalen = size;
5653 gst_buffer_extract (xmp_data, 0, uuid->data, size);
5655 return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,