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 =
1158 ('u' - 0x60) * 0x400 + ('n' - 0x60) * 0x20 + ('d' - 0x60);
1163 atom_mdhd_clear (AtomMDHD * mdhd)
1165 atom_full_clear (&mdhd->header);
1169 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
1171 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
1173 atom_mdhd_init (&mdia->mdhd);
1174 atom_hdlr_init (&mdia->hdlr, context);
1175 atom_minf_init (&mdia->minf, context);
1179 atom_mdia_clear (AtomMDIA * mdia)
1181 atom_clear (&mdia->header);
1182 atom_mdhd_clear (&mdia->mdhd);
1183 atom_hdlr_clear (&mdia->hdlr);
1184 atom_minf_clear (&mdia->minf);
1188 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
1192 * 1 -> track enabled
1193 * 2 -> track in movie
1194 * 4 -> track in preview
1196 guint8 flags[3] = { 0, 0, 7 };
1198 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
1200 tkhd->creation_time = tkhd->modification_time = atoms_get_current_qt_time ();
1205 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
1207 tkhd->alternate_group = 0;
1209 tkhd->reserved3 = 0;
1210 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
1211 tkhd->matrix[0] = 1 << 16;
1212 tkhd->matrix[4] = 1 << 16;
1213 tkhd->matrix[8] = 16384 << 16;
1219 atom_tkhd_clear (AtomTKHD * tkhd)
1221 atom_full_clear (&tkhd->header);
1225 atom_ilst_init (AtomILST * ilst)
1227 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
1228 ilst->entries = NULL;
1232 atom_ilst_new (void)
1234 AtomILST *ilst = g_new0 (AtomILST, 1);
1236 atom_ilst_init (ilst);
1241 atom_ilst_free (AtomILST * ilst)
1244 atom_info_list_free (ilst->entries);
1245 atom_clear (&ilst->header);
1250 atom_meta_init (AtomMETA * meta, AtomsContext * context)
1252 guint8 flags[3] = { 0, 0, 0 };
1254 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1255 atom_hdlr_init (&meta->hdlr, context);
1256 /* FIXME (ISOM says this is always 0) */
1257 meta->hdlr.component_type = FOURCC_mhlr;
1258 meta->hdlr.handler_type = FOURCC_mdir;
1263 atom_meta_new (AtomsContext * context)
1265 AtomMETA *meta = g_new0 (AtomMETA, 1);
1267 atom_meta_init (meta, context);
1272 atom_meta_free (AtomMETA * meta)
1274 atom_full_clear (&meta->header);
1275 atom_hdlr_clear (&meta->hdlr);
1277 atom_ilst_free (meta->ilst);
1283 atom_udta_init_metatags (AtomUDTA * udta, AtomsContext * context)
1285 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
1287 udta->meta = atom_meta_new (context);
1289 if (!udta->meta->ilst) {
1290 udta->meta->ilst = atom_ilst_new ();
1296 atom_udta_init (AtomUDTA * udta, AtomsContext * context)
1298 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1300 udta->context = context;
1302 atom_udta_init_metatags (udta, context);
1306 atom_udta_clear (AtomUDTA * udta)
1308 atom_clear (&udta->header);
1310 atom_meta_free (udta->meta);
1313 atom_info_list_free (udta->entries);
1317 atom_tref_init (AtomTREF * tref, guint32 reftype)
1319 atom_header_set (&tref->header, FOURCC_tref, 0, 0);
1320 tref->reftype = reftype;
1321 atom_array_init (&tref->entries, 128);
1325 atom_tref_clear (AtomTREF * tref)
1327 atom_clear (&tref->header);
1329 atom_array_clear (&tref->entries);
1333 atom_tref_new (guint32 reftype)
1337 tref = g_new0 (AtomTREF, 1);
1338 atom_tref_init (tref, reftype);
1344 atom_tref_free (AtomTREF * tref)
1346 atom_tref_clear (tref);
1350 /* Clear added tags, but keep the context/flavor the same */
1352 atom_udta_clear_tags (AtomUDTA * udta)
1354 if (udta->entries) {
1355 atom_info_list_free (udta->entries);
1356 udta->entries = NULL;
1358 if (udta->meta && udta->meta->ilst->entries) {
1359 atom_info_list_free (udta->meta->ilst->entries);
1360 udta->meta->ilst->entries = NULL;
1365 atom_tag_data_init (AtomTagData * data)
1367 guint8 flags[] = { 0, 0, 0 };
1369 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1373 atom_tag_data_clear (AtomTagData * data)
1375 atom_full_clear (&data->header);
1376 g_free (data->data);
1381 * Fourcc is the tag fourcc
1382 * flags will be truncated to 24bits
1385 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1387 AtomTag *tag = g_new0 (AtomTag, 1);
1389 tag->header.type = fourcc;
1390 atom_tag_data_init (&tag->data);
1391 atom_full_set_flags_as_uint (&tag->data.header, flags_as_uint);
1396 atom_tag_free (AtomTag * tag)
1398 atom_clear (&tag->header);
1399 atom_tag_data_clear (&tag->data);
1404 atom_mvhd_init (AtomMVHD * mvhd)
1406 guint8 flags[3] = { 0, 0, 0 };
1408 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1410 common_time_info_init (&mvhd->time_info);
1412 mvhd->prefered_rate = 1 << 16;
1413 mvhd->volume = 1 << 8;
1414 mvhd->reserved3 = 0;
1415 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1417 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1418 mvhd->matrix[0] = 1 << 16;
1419 mvhd->matrix[4] = 1 << 16;
1420 mvhd->matrix[8] = 16384 << 16;
1422 mvhd->preview_time = 0;
1423 mvhd->preview_duration = 0;
1424 mvhd->poster_time = 0;
1425 mvhd->selection_time = 0;
1426 mvhd->selection_duration = 0;
1427 mvhd->current_time = 0;
1429 mvhd->next_track_id = 1;
1433 atom_mvhd_clear (AtomMVHD * mvhd)
1435 atom_full_clear (&mvhd->header);
1439 atom_mehd_init (AtomMEHD * mehd)
1441 guint8 flags[3] = { 0, 0, 0 };
1443 atom_full_init (&mehd->header, FOURCC_mehd, 0, 0, 1, flags);
1444 mehd->fragment_duration = 0;
1448 atom_mvex_init (AtomMVEX * mvex)
1450 atom_header_set (&mvex->header, FOURCC_mvex, 0, 0);
1451 atom_mehd_init (&mvex->mehd);
1456 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
1458 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
1460 atom_tkhd_init (&trak->tkhd, context);
1461 trak->context = context;
1462 atom_udta_init (&trak->udta, context);
1464 atom_mdia_init (&trak->mdia, context);
1469 atom_trak_new (AtomsContext * context)
1471 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
1473 atom_trak_init (trak, context);
1478 atom_trak_clear (AtomTRAK * trak)
1480 atom_clear (&trak->header);
1481 atom_tkhd_clear (&trak->tkhd);
1483 atom_edts_free (trak->edts);
1484 atom_udta_clear (&trak->udta);
1485 atom_mdia_clear (&trak->mdia);
1487 atom_tref_free (trak->tref);
1491 atom_trak_free (AtomTRAK * trak)
1493 atom_trak_clear (trak);
1499 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1501 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1502 atom_mvhd_init (&(moov->mvhd));
1503 atom_mvex_init (&(moov->mvex));
1504 atom_udta_init (&moov->udta, context);
1506 moov->context = *context;
1510 atom_moov_new (AtomsContext * context)
1512 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1514 atom_moov_init (moov, context);
1519 atom_trex_free (AtomTREX * trex)
1521 atom_full_clear (&trex->header);
1526 atom_mvex_clear (AtomMVEX * mvex)
1530 atom_clear (&mvex->header);
1531 walker = mvex->trexs;
1533 atom_trex_free ((AtomTREX *) walker->data);
1534 walker = g_list_next (walker);
1536 g_list_free (mvex->trexs);
1541 atom_moov_free (AtomMOOV * moov)
1545 atom_clear (&moov->header);
1546 atom_mvhd_clear (&moov->mvhd);
1548 walker = moov->traks;
1550 atom_trak_free ((AtomTRAK *) walker->data);
1551 walker = g_list_next (walker);
1553 g_list_free (moov->traks);
1556 atom_udta_clear (&moov->udta);
1557 atom_mvex_clear (&moov->mvex);
1562 /* -- end of init / free -- */
1564 /* -- copy data functions -- */
1567 atom_full_get_version (AtomFull * full)
1569 return full->version;
1573 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1574 guint8 ** buffer, guint64 * size, guint64 * offset)
1576 guint64 original_offset = *offset;
1579 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1580 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1581 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1582 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1584 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1585 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1586 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1587 prop_copy_uint64 (ti->duration, buffer, size, offset);
1589 return *offset - original_offset;
1593 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1596 /* this only works for non-extended atom size, which is OK
1597 * (though it could be made to do mem_move, etc and write extended size) */
1598 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1602 atom_copy_empty (Atom * atom, guint8 ** buffer, guint64 * size,
1605 guint64 original_offset = *offset;
1607 prop_copy_uint32 (0, buffer, size, offset);
1609 return *offset - original_offset;
1613 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1615 guint64 original_offset = *offset;
1617 /* copies type and size */
1618 prop_copy_uint32 (atom->size, buffer, size, offset);
1619 prop_copy_fourcc (atom->type, buffer, size, offset);
1621 /* extended size needed */
1622 if (atom->size == 1) {
1623 /* really should not happen other than with mdat atom;
1624 * would be a problem for size (re)write code, not to mention memory */
1625 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1626 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1629 return *offset - original_offset;
1633 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1636 guint64 original_offset = *offset;
1638 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1642 prop_copy_uint8 (atom->version, buffer, size, offset);
1643 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1645 atom_write_size (buffer, size, offset, original_offset);
1646 return *offset - original_offset;
1650 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1653 guint64 original_offset = *offset;
1656 AtomInfo *info = (AtomInfo *) ai->data;
1658 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1661 ai = g_list_next (ai);
1664 return *offset - original_offset;
1668 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1671 guint64 original_offset = *offset;
1673 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1677 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1679 atom_write_size (buffer, size, offset, original_offset);
1680 return *offset - original_offset;
1684 atom_uuid_copy_data (AtomUUID * uuid, guint8 ** buffer, guint64 * size,
1687 guint64 original_offset = *offset;
1689 if (!atom_copy_data (&uuid->header, buffer, size, offset)) {
1692 prop_copy_uint8_array (uuid->uuid, 16, buffer, size, offset);
1694 prop_copy_uint8_array (uuid->data, uuid->datalen, buffer, size, offset);
1696 atom_write_size (buffer, size, offset, original_offset);
1697 return *offset - original_offset;
1701 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1704 guint64 original_offset = *offset;
1706 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1709 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1710 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1712 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1713 buffer, size, offset);
1715 atom_write_size (buffer, size, offset, original_offset);
1716 return *offset - original_offset;
1720 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1724 guint64 original_offset = *offset;
1726 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1730 version = atom_full_get_version (&(atom->header));
1732 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1733 } else if (version == 1) {
1734 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1736 *offset = original_offset;
1740 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1741 prop_copy_uint16 (atom->volume, buffer, size, offset);
1742 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1743 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1744 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1745 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1746 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1747 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1748 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1749 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1750 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1752 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1754 atom_write_size (buffer, size, offset, original_offset);
1755 return *offset - original_offset;
1759 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1762 guint64 original_offset = *offset;
1764 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1768 if (atom_full_get_version (&tkhd->header) == 0) {
1769 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1770 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1771 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1772 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1773 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1775 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1776 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1777 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1778 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1779 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1782 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1783 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1784 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1785 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1786 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1787 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1789 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1790 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1792 atom_write_size (buffer, size, offset, original_offset);
1793 return *offset - original_offset;
1797 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1800 guint64 original_offset = *offset;
1802 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1806 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1807 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1808 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1809 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1810 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1812 if (hdlr->flavor == ATOMS_TREE_FLAVOR_MOV) {
1813 prop_copy_size_string ((guint8 *) hdlr->name, strlen (hdlr->name), buffer,
1816 /* assume isomedia base is more generic and use null terminated */
1817 prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
1820 atom_write_size (buffer, size, offset, original_offset);
1821 return *offset - original_offset;
1825 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1828 guint64 original_offset = *offset;
1830 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1833 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1834 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1836 atom_write_size (buffer, size, offset, original_offset);
1837 return original_offset - *offset;
1841 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1844 guint64 original_offset = *offset;
1846 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1849 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1850 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1852 atom_write_size (buffer, size, offset, original_offset);
1853 return original_offset - *offset;
1857 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1860 guint64 original_offset = *offset;
1862 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1865 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1866 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1867 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1868 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1869 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1871 atom_write_size (buffer, size, offset, original_offset);
1872 return original_offset - *offset;
1876 atom_tcmi_copy_data (AtomTCMI * tcmi, guint8 ** buffer, guint64 * size,
1879 guint64 original_offset = *offset;
1881 if (!atom_full_copy_data (&tcmi->header, buffer, size, offset)) {
1884 prop_copy_uint16 (tcmi->text_font, buffer, size, offset);
1885 prop_copy_uint16 (tcmi->text_face, buffer, size, offset);
1886 prop_copy_uint16 (tcmi->text_size, buffer, size, offset);
1887 prop_copy_uint16 (tcmi->text_color[0], buffer, size, offset);
1888 prop_copy_uint16 (tcmi->text_color[1], buffer, size, offset);
1889 prop_copy_uint16 (tcmi->text_color[2], buffer, size, offset);
1890 prop_copy_uint16 (tcmi->bg_color[0], buffer, size, offset);
1891 prop_copy_uint16 (tcmi->bg_color[1], buffer, size, offset);
1892 prop_copy_uint16 (tcmi->bg_color[2], buffer, size, offset);
1894 prop_copy_uint16 (0, buffer, size, offset);
1895 prop_copy_size_string ((guint8 *) tcmi->font_name, strlen (tcmi->font_name),
1896 buffer, size, offset);
1898 atom_write_size (buffer, size, offset, original_offset);
1899 return original_offset - *offset;
1903 atom_tmcd_copy_data (AtomTMCD * tmcd, guint8 ** buffer, guint64 * size,
1906 guint64 original_offset = *offset;
1908 if (!atom_copy_data (&tmcd->header, buffer, size, offset)) {
1911 if (!atom_tcmi_copy_data (&tmcd->tcmi, buffer, size, offset)) {
1915 atom_write_size (buffer, size, offset, original_offset);
1916 return original_offset - *offset;
1920 atom_gmin_copy_data (AtomGMIN * gmin, guint8 ** buffer, guint64 * size,
1923 guint64 original_offset = *offset;
1925 if (!atom_full_copy_data (&gmin->header, buffer, size, offset)) {
1928 prop_copy_uint16 (gmin->graphics_mode, buffer, size, offset);
1929 prop_copy_uint16 (gmin->opcolor[0], buffer, size, offset);
1930 prop_copy_uint16 (gmin->opcolor[1], buffer, size, offset);
1931 prop_copy_uint16 (gmin->opcolor[2], buffer, size, offset);
1932 prop_copy_uint8 (gmin->balance, buffer, size, offset);
1934 prop_copy_uint8 (0, buffer, size, offset);
1936 atom_write_size (buffer, size, offset, original_offset);
1937 return original_offset - *offset;
1941 atom_gmhd_copy_data (AtomGMHD * gmhd, guint8 ** buffer, guint64 * size,
1944 guint64 original_offset = *offset;
1946 if (!atom_copy_data (&gmhd->header, buffer, size, offset)) {
1949 if (!atom_gmin_copy_data (&gmhd->gmin, buffer, size, offset)) {
1952 if (gmhd->tmcd && !atom_tmcd_copy_data (gmhd->tmcd, buffer, size, offset)) {
1956 atom_write_size (buffer, size, offset, original_offset);
1957 return original_offset - *offset;
1961 atom_url_same_file_flag (AtomURL * url)
1963 return (url->header.flags[2] & 0x1) == 1;
1967 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1970 guint64 original_offset = *offset;
1972 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1976 if (!atom_url_same_file_flag (url)) {
1977 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1980 atom_write_size (buffer, size, offset, original_offset);
1981 return original_offset - *offset;
1985 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1988 guint64 original_offset = *offset;
1991 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1995 prop_copy_uint32 (atom_array_get_len (&stts->entries), buffer, size, offset);
1996 /* minimize realloc */
1997 prop_copy_ensure_buffer (buffer, size, offset,
1998 8 * atom_array_get_len (&stts->entries));
1999 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
2000 STTSEntry *entry = &atom_array_index (&stts->entries, i);
2002 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
2003 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
2006 atom_write_size (buffer, size, offset, original_offset);
2007 return *offset - original_offset;
2011 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
2012 guint64 * size, guint64 * offset)
2014 guint64 original_offset = *offset;
2016 if (!atom_copy_data (&se->header, buffer, size, offset)) {
2020 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
2021 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
2023 return *offset - original_offset;
2027 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
2030 guint64 original_offset = *offset;
2032 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
2035 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
2039 atom_write_size (buffer, size, offset, original_offset);
2040 return *offset - original_offset;
2044 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
2045 guint64 * size, guint64 * offset)
2047 guint64 original_offset = *offset;
2049 if (!atom_copy_data (&(frma->header), buffer, size, offset))
2052 prop_copy_fourcc (frma->media_type, buffer, size, offset);
2054 atom_write_size (buffer, size, offset, original_offset);
2055 return *offset - original_offset;
2059 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
2060 guint64 * size, guint64 * offset)
2062 guint64 original_offset = *offset;
2064 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
2068 prop_copy_uint32 (hse->size, buffer, size, offset);
2069 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
2071 atom_write_size (buffer, size, offset, original_offset);
2072 return *offset - original_offset;
2076 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
2077 guint64 * size, guint64 * offset)
2079 guint64 original_offset = *offset;
2081 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
2085 prop_copy_uint16 (mp4a->version, buffer, size, offset);
2086 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
2087 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
2088 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
2089 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
2090 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
2091 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
2092 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
2094 /* this should always be 0 for mp4 flavor */
2095 if (mp4a->version == 1) {
2096 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
2097 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
2098 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
2099 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
2102 if (mp4a->extension_atoms) {
2103 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
2107 atom_write_size (buffer, size, offset, original_offset);
2108 return *offset - original_offset;
2112 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
2113 guint64 * size, guint64 * offset)
2115 guint64 original_offset = *offset;
2117 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
2121 prop_copy_uint16 (mp4v->version, buffer, size, offset);
2122 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
2123 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
2124 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
2125 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
2127 prop_copy_uint16 (mp4v->width, buffer, size, offset);
2128 prop_copy_uint16 (mp4v->height, buffer, size, offset);
2130 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
2131 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
2132 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
2134 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
2136 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
2139 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
2140 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
2143 if (mp4v->extension_atoms &&
2144 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
2147 atom_write_size (buffer, size, offset, original_offset);
2148 return *offset - original_offset;
2152 sample_entry_tx3g_copy_data (SampleTableEntryTX3G * tx3g, guint8 ** buffer,
2153 guint64 * size, guint64 * offset)
2155 guint64 original_offset = *offset;
2157 if (!atom_sample_entry_copy_data (&tx3g->se, buffer, size, offset)) {
2161 prop_copy_uint32 (tx3g->display_flags, buffer, size, offset);
2164 prop_copy_uint8 (1, buffer, size, offset);
2165 prop_copy_uint8 (-1, buffer, size, offset);
2166 prop_copy_uint32 (0, buffer, size, offset);
2168 prop_copy_uint64 (tx3g->default_text_box, buffer, size, offset);
2171 prop_copy_uint32 (0, buffer, size, offset);
2173 prop_copy_uint16 (tx3g->font_id, buffer, size, offset);
2174 prop_copy_uint8 (tx3g->font_face, buffer, size, offset);
2175 prop_copy_uint8 (tx3g->font_size, buffer, size, offset);
2176 prop_copy_uint32 (tx3g->foreground_color_rgba, buffer, size, offset);
2178 /* it must have a fonttable atom */
2182 atom_header_set (&atom, FOURCC_ftab, 18, 0);
2183 if (!atom_copy_data (&atom, buffer, size, offset))
2185 prop_copy_uint16 (1, buffer, size, offset); /* Count must be 1 */
2186 prop_copy_uint16 (1, buffer, size, offset); /* Font id: 1 */
2187 prop_copy_size_string ((guint8 *) "Serif", 5, buffer, size, offset);
2190 atom_write_size (buffer, size, offset, original_offset);
2191 return *offset - original_offset;
2195 sample_entry_tmcd_copy_data (SampleTableEntryTMCD * tmcd, guint8 ** buffer,
2196 guint64 * size, guint64 * offset)
2198 guint64 original_offset = *offset;
2200 if (!atom_sample_entry_copy_data (&tmcd->se, buffer, size, offset)) {
2205 prop_copy_uint32 (0, buffer, size, offset);
2207 prop_copy_uint32 (tmcd->tc_flags, buffer, size, offset);
2208 prop_copy_uint32 (tmcd->timescale, buffer, size, offset);
2209 prop_copy_uint32 (tmcd->frame_duration, buffer, size, offset);
2210 prop_copy_uint8 (tmcd->n_frames, buffer, size, offset);
2213 prop_copy_uint8 (0, buffer, size, offset);
2216 guint64 name_offset = *offset;
2218 atom_header_set (&atom, FOURCC_name, 0, 0);
2219 if (!atom_copy_data (&atom, buffer, size, offset))
2221 prop_copy_uint16 (strlen (tmcd->name.name), buffer, size, offset);
2222 prop_copy_uint16 (tmcd->name.language_code, buffer, size, offset);
2223 prop_copy_fixed_size_string ((guint8 *) tmcd->name.name,
2224 strlen (tmcd->name.name), buffer, size, offset);
2226 atom_write_size (buffer, size, offset, name_offset);
2229 atom_write_size (buffer, size, offset, original_offset);
2230 return *offset - original_offset;
2234 sample_entry_generic_copy_data (SampleTableEntry * entry, guint8 ** buffer,
2235 guint64 * size, guint64 * offset)
2237 guint64 original_offset = *offset;
2239 if (!atom_sample_entry_copy_data (entry, buffer, size, offset)) {
2243 atom_write_size (buffer, size, offset, original_offset);
2244 return *offset - original_offset;
2248 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
2251 guint64 original_offset = *offset;
2254 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
2258 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
2259 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
2260 if (stsz->sample_size == 0) {
2261 /* minimize realloc */
2262 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
2263 /* entry count must match sample count */
2264 g_assert (atom_array_get_len (&stsz->entries) == stsz->table_size);
2265 for (i = 0; i < atom_array_get_len (&stsz->entries); i++) {
2266 prop_copy_uint32 (atom_array_index (&stsz->entries, i), buffer, size,
2271 atom_write_size (buffer, size, offset, original_offset);
2272 return *offset - original_offset;
2276 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
2279 guint64 original_offset = *offset;
2281 gboolean last_entries_merged = FALSE;
2283 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
2287 /* Last two entries might be the same size here as we only merge once the
2288 * next chunk is started */
2289 if ((len = atom_array_get_len (&stsc->entries)) > 1 &&
2290 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
2291 (atom_array_index (&stsc->entries, len - 2)).samples_per_chunk)) {
2292 stsc->entries.len--;
2293 last_entries_merged = TRUE;
2296 prop_copy_uint32 (atom_array_get_len (&stsc->entries), buffer, size, offset);
2297 /* minimize realloc */
2298 prop_copy_ensure_buffer (buffer, size, offset,
2299 12 * atom_array_get_len (&stsc->entries));
2301 for (i = 0; i < atom_array_get_len (&stsc->entries); i++) {
2302 STSCEntry *entry = &atom_array_index (&stsc->entries, i);
2304 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
2305 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
2306 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
2309 atom_write_size (buffer, size, offset, original_offset);
2311 /* Need to add the last entry again as in "robust" muxing mode we will most
2312 * likely add new samples to the last chunk, thus making the
2313 * samples_per_chunk in the last one different to the second to last one,
2314 * and thus making it wrong to keep them merged
2316 if (last_entries_merged)
2317 stsc->entries.len++;
2319 return *offset - original_offset;
2323 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
2326 guint64 original_offset = *offset;
2329 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
2333 prop_copy_uint32 (atom_array_get_len (&ctts->entries), buffer, size, offset);
2334 /* minimize realloc */
2335 prop_copy_ensure_buffer (buffer, size, offset,
2336 8 * atom_array_get_len (&ctts->entries));
2337 for (i = 0; i < atom_array_get_len (&ctts->entries); i++) {
2338 CTTSEntry *entry = &atom_array_index (&ctts->entries, i);
2340 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
2341 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
2344 atom_write_size (buffer, size, offset, original_offset);
2345 return *offset - original_offset;
2349 atom_svmi_copy_data (AtomSVMI * svmi, guint8 ** buffer, guint64 * size,
2352 guint64 original_offset = *offset;
2354 if (!atom_full_copy_data (&svmi->header, buffer, size, offset)) {
2358 prop_copy_uint8 (svmi->stereoscopic_composition_type, buffer, size, offset);
2359 prop_copy_uint8 (svmi->is_left_first ? 1 : 0, buffer, size, offset);
2360 /* stereo-mono change count */
2361 prop_copy_uint32 (0, buffer, size, offset);
2363 atom_write_size (buffer, size, offset, original_offset);
2364 return *offset - original_offset;
2368 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
2371 guint64 original_offset = *offset;
2373 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
2375 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
2379 prop_copy_uint32 (atom_array_get_len (&stco64->entries), buffer, size,
2382 /* minimize realloc */
2383 prop_copy_ensure_buffer (buffer, size, offset,
2384 8 * atom_array_get_len (&stco64->entries));
2385 for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
2387 atom_array_index (&stco64->entries, i) + stco64->chunk_offset;
2390 prop_copy_uint32 ((guint32) value, buffer, size, offset);
2392 prop_copy_uint64 (value, buffer, size, offset);
2396 atom_write_size (buffer, size, offset, original_offset);
2397 return *offset - original_offset;
2401 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
2404 guint64 original_offset = *offset;
2407 if (atom_array_get_len (&stss->entries) == 0) {
2408 /* FIXME not needing this atom might be confused with error while copying */
2412 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
2416 prop_copy_uint32 (atom_array_get_len (&stss->entries), buffer, size, offset);
2417 /* minimize realloc */
2418 prop_copy_ensure_buffer (buffer, size, offset,
2419 4 * atom_array_get_len (&stss->entries));
2420 for (i = 0; i < atom_array_get_len (&stss->entries); i++) {
2421 prop_copy_uint32 (atom_array_index (&stss->entries, i), buffer, size,
2425 atom_write_size (buffer, size, offset, original_offset);
2426 return *offset - original_offset;
2430 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
2433 guint64 original_offset = *offset;
2436 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
2440 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
2442 for (walker = g_list_last (stsd->entries); walker != NULL;
2443 walker = g_list_previous (walker)) {
2444 SampleTableEntry *se = (SampleTableEntry *) walker->data;
2446 switch (((Atom *) walker->data)->type) {
2448 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
2449 buffer, size, offset)) {
2454 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
2455 buffer, size, offset)) {
2460 if (se->kind == VIDEO) {
2461 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
2462 walker->data, buffer, size, offset)) {
2465 } else if (se->kind == AUDIO) {
2466 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
2467 walker->data, buffer, size, offset)) {
2470 } else if (se->kind == SUBTITLE) {
2471 if (!sample_entry_tx3g_copy_data ((SampleTableEntryTX3G *)
2472 walker->data, buffer, size, offset)) {
2475 } else if (se->kind == TIMECODE) {
2476 if (!sample_entry_tmcd_copy_data ((SampleTableEntryTMCD *)
2477 walker->data, buffer, size, offset)) {
2480 } else if (se->kind == CLOSEDCAPTION) {
2481 if (!sample_entry_generic_copy_data ((SampleTableEntry *)
2482 walker->data, buffer, size, offset)) {
2486 if (!atom_hint_sample_entry_copy_data (
2487 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
2495 atom_write_size (buffer, size, offset, original_offset);
2496 return *offset - original_offset;
2500 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
2503 guint64 original_offset = *offset;
2505 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
2509 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
2512 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
2515 /* this atom is optional, so let's check if we need it
2516 * (to avoid false error) */
2517 if (atom_array_get_len (&stbl->stss.entries)) {
2518 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
2523 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
2526 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
2529 if (stbl->ctts && stbl->ctts->do_pts) {
2530 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
2535 if (!atom_svmi_copy_data (stbl->svmi, buffer, size, offset)) {
2539 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
2543 atom_write_size (buffer, size, offset, original_offset);
2544 return original_offset - *offset;
2549 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
2552 guint64 original_offset = *offset;
2555 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
2559 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
2561 walker = dref->entries;
2562 while (walker != NULL) {
2563 Atom *atom = (Atom *) walker->data;
2565 if (atom->type == FOURCC_url_) {
2566 if (!atom_url_copy_data ((AtomURL *) atom, buffer, size, offset))
2568 } else if (atom->type == FOURCC_alis) {
2569 if (!atom_full_copy_data ((AtomFull *) atom, buffer, size, offset))
2572 g_error ("Unsupported atom used inside dref atom");
2574 walker = g_list_next (walker);
2577 atom_write_size (buffer, size, offset, original_offset);
2578 return *offset - original_offset;
2582 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
2585 guint64 original_offset = *offset;
2587 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
2591 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
2595 atom_write_size (buffer, size, offset, original_offset);
2596 return original_offset - *offset;
2600 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
2603 guint64 original_offset = *offset;
2605 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
2610 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
2613 } else if (minf->smhd) {
2614 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
2617 } else if (minf->hmhd) {
2618 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
2621 } else if (minf->gmhd) {
2622 if (!atom_gmhd_copy_data (minf->gmhd, buffer, size, offset)) {
2628 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2633 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2636 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2640 atom_write_size (buffer, size, offset, original_offset);
2641 return *offset - original_offset;
2645 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2648 guint64 original_offset = *offset;
2650 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2654 if (!common_time_info_copy_data (&mdhd->time_info,
2655 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2659 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2660 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2662 atom_write_size (buffer, size, offset, original_offset);
2663 return *offset - original_offset;
2667 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2670 guint64 original_offset = *offset;
2672 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2675 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2678 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2682 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2686 atom_write_size (buffer, size, offset, original_offset);
2687 return *offset - original_offset;
2691 atom_elst_copy_data (AtomELST * elst, guint8 ** buffer, guint64 * size,
2694 guint64 original_offset = *offset;
2697 if (!atom_full_copy_data (&elst->header, buffer, size, offset)) {
2701 prop_copy_uint32 (g_slist_length (elst->entries), buffer, size, offset);
2703 for (walker = elst->entries; walker != NULL; walker = g_slist_next (walker)) {
2704 EditListEntry *entry = (EditListEntry *) walker->data;
2705 prop_copy_uint32 (entry->duration, buffer, size, offset);
2706 prop_copy_uint32 (entry->media_time, buffer, size, offset);
2707 prop_copy_uint32 (entry->media_rate, buffer, size, offset);
2709 atom_write_size (buffer, size, offset, original_offset);
2710 return *offset - original_offset;
2714 atom_tref_copy_data (AtomTREF * tref, guint8 ** buffer, guint64 * size,
2717 guint64 original_offset = *offset;
2720 g_assert (atom_array_get_len (&tref->entries) > 0);
2722 if (!atom_copy_data (&tref->header, buffer, size, offset)) {
2726 prop_copy_uint32 (8 + 4 * atom_array_get_len (&tref->entries), buffer, size,
2728 prop_copy_fourcc (tref->reftype, buffer, size, offset);
2729 /* minimize realloc */
2730 prop_copy_ensure_buffer (buffer, size, offset,
2731 4 * atom_array_get_len (&tref->entries));
2732 for (i = 0; i < atom_array_get_len (&tref->entries); i++) {
2733 prop_copy_uint32 (atom_array_index (&tref->entries, i), buffer, size,
2737 atom_write_size (buffer, size, offset, original_offset);
2738 return *offset - original_offset;
2742 atom_edts_copy_data (AtomEDTS * edts, guint8 ** buffer, guint64 * size,
2745 guint64 original_offset = *offset;
2747 if (!atom_copy_data (&(edts->header), buffer, size, offset))
2750 if (!atom_elst_copy_data (&(edts->elst), buffer, size, offset))
2753 atom_write_size (buffer, size, offset, original_offset);
2754 return *offset - original_offset;
2758 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2761 guint64 original_offset = *offset;
2763 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2767 prop_copy_uint32 (data->reserved, buffer, size, offset);
2768 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2770 atom_write_size (buffer, size, offset, original_offset);
2771 return *offset - original_offset;
2775 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2778 guint64 original_offset = *offset;
2780 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2784 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2788 atom_write_size (buffer, size, offset, original_offset);
2789 return *offset - original_offset;
2793 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2796 guint64 original_offset = *offset;
2798 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2802 if (ilst->entries &&
2803 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2806 atom_write_size (buffer, size, offset, original_offset);
2807 return *offset - original_offset;
2811 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2814 guint64 original_offset = *offset;
2816 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2819 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2823 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2828 atom_write_size (buffer, size, offset, original_offset);
2829 return *offset - original_offset;
2833 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2836 guint64 original_offset = *offset;
2838 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2842 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2846 if (udta->entries) {
2848 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2852 atom_write_size (buffer, size, offset, original_offset);
2853 return *offset - original_offset;
2857 atom_mehd_copy_data (AtomMEHD * mehd, guint8 ** buffer, guint64 * size,
2860 guint64 original_offset = *offset;
2862 if (!atom_full_copy_data (&mehd->header, buffer, size, offset)) {
2866 prop_copy_uint64 (mehd->fragment_duration, buffer, size, offset);
2868 atom_write_size (buffer, size, offset, original_offset);
2869 return *offset - original_offset;
2873 atom_trex_copy_data (AtomTREX * trex, guint8 ** buffer, guint64 * size,
2876 guint64 original_offset = *offset;
2878 if (!atom_full_copy_data (&trex->header, buffer, size, offset)) {
2882 prop_copy_uint32 (trex->track_ID, buffer, size, offset);
2883 prop_copy_uint32 (trex->default_sample_description_index, buffer, size,
2885 prop_copy_uint32 (trex->default_sample_duration, buffer, size, offset);
2886 prop_copy_uint32 (trex->default_sample_size, buffer, size, offset);
2887 prop_copy_uint32 (trex->default_sample_flags, buffer, size, offset);
2889 atom_write_size (buffer, size, offset, original_offset);
2890 return *offset - original_offset;
2894 atom_mvex_copy_data (AtomMVEX * mvex, guint8 ** buffer, guint64 * size,
2897 guint64 original_offset = *offset;
2900 if (!atom_copy_data (&mvex->header, buffer, size, offset)) {
2904 if (!atom_mehd_copy_data (&mvex->mehd, buffer, size, offset)) {
2908 walker = g_list_first (mvex->trexs);
2909 while (walker != NULL) {
2910 if (!atom_trex_copy_data ((AtomTREX *) walker->data, buffer, size, offset)) {
2913 walker = g_list_next (walker);
2916 atom_write_size (buffer, size, offset, original_offset);
2917 return *offset - original_offset;
2921 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2924 guint64 original_offset = *offset;
2926 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2929 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2933 if (!trak->tapt->copy_data_func (trak->tapt->atom, buffer, size, offset)) {
2938 if (!atom_edts_copy_data (trak->edts, buffer, size, offset)) {
2943 /* Make sure we need this atom (there is a referenced track */
2944 if (atom_array_get_len (&trak->tref->entries) > 0) {
2945 if (!atom_tref_copy_data (trak->tref, buffer, size, offset)) {
2951 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2955 if (!atom_udta_copy_data (&trak->udta, buffer, size, offset)) {
2959 atom_write_size (buffer, size, offset, original_offset);
2960 return *offset - original_offset;
2965 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2968 guint64 original_offset = *offset;
2971 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2974 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2977 walker = g_list_first (atom->traks);
2978 while (walker != NULL) {
2979 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2982 walker = g_list_next (walker);
2985 if (!atom_udta_copy_data (&atom->udta, buffer, size, offset)) {
2989 if (atom->fragmented) {
2990 if (!atom_mvex_copy_data (&atom->mvex, buffer, size, offset)) {
2995 atom_write_size (buffer, size, offset, original_offset);
2996 return *offset - original_offset;
3000 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
3001 guint64 * size, guint64 * offset)
3003 guint64 original_offset = *offset;
3005 if (!atom_copy_data (&(wave->header), buffer, size, offset))
3008 if (wave->extension_atoms) {
3009 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
3013 atom_write_size (buffer, size, offset, original_offset);
3014 return *offset - original_offset;
3017 /* -- end of copy data functions -- */
3019 /* -- general functions, API and support functions */
3021 /* add samples to tables */
3024 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
3028 if ((len = atom_array_get_len (&stsc->entries)) > 1 &&
3029 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
3030 (atom_array_index (&stsc->entries, len - 2)).samples_per_chunk)) {
3033 /* Merge last two entries as they have the same number of samples per chunk */
3034 nentry = &atom_array_index (&stsc->entries, len - 1);
3035 nentry->first_chunk = first_chunk;
3036 nentry->samples_per_chunk = nsamples;
3037 nentry->sample_description_index = 1;
3041 nentry.first_chunk = first_chunk;
3042 nentry.samples_per_chunk = nsamples;
3043 nentry.sample_description_index = 1;
3044 atom_array_append (&stsc->entries, nentry, 128);
3049 atom_stsc_update_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
3053 len = atom_array_get_len (&stsc->entries);
3054 g_assert (len != 0);
3055 g_assert (atom_array_index (&stsc->entries,
3056 len - 1).first_chunk == first_chunk);
3058 atom_array_index (&stsc->entries, len - 1).samples_per_chunk += nsamples;
3062 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
3064 STTSEntry *entry = NULL;
3066 if (G_LIKELY (atom_array_get_len (&stts->entries) != 0))
3067 entry = &atom_array_index (&stts->entries,
3068 atom_array_get_len (&stts->entries) - 1);
3070 if (entry && entry->sample_delta == sample_delta) {
3071 entry->sample_count += sample_count;
3075 nentry.sample_count = sample_count;
3076 nentry.sample_delta = sample_delta;
3077 atom_array_append (&stts->entries, nentry, 256);
3082 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
3086 stsz->table_size += nsamples;
3087 if (stsz->sample_size != 0) {
3088 /* it is constant size, we don't need entries */
3091 for (i = 0; i < nsamples; i++) {
3092 atom_array_append (&stsz->entries, size, 1024);
3097 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
3099 return atom_array_get_len (&stco64->entries);
3102 /* returns TRUE if a new entry was added */
3104 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
3108 /* Only add a new entry if the chunk offset changed */
3109 if ((len = atom_array_get_len (&stco64->entries)) &&
3110 ((atom_array_index (&stco64->entries, len - 1)) == entry))
3113 atom_array_append (&stco64->entries, entry, 256);
3114 if (entry > G_MAXUINT32)
3115 stco64->header.header.type = FOURCC_co64;
3121 atom_tref_add_entry (AtomTREF * tref, guint32 sample)
3123 atom_array_append (&tref->entries, sample, 512);
3127 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
3129 atom_array_append (&stss->entries, sample, 512);
3133 atom_stbl_add_stss_entry (AtomSTBL * stbl)
3135 guint32 sample_index = stbl->stsz.table_size;
3137 atom_stss_add_entry (&stbl->stss, sample_index);
3141 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
3143 CTTSEntry *entry = NULL;
3145 if (G_LIKELY (atom_array_get_len (&ctts->entries) != 0))
3146 entry = &atom_array_index (&ctts->entries,
3147 atom_array_get_len (&ctts->entries) - 1);
3149 if (entry == NULL || entry->sampleoffset != offset) {
3152 nentry.samplecount = nsamples;
3153 nentry.sampleoffset = offset;
3154 atom_array_append (&ctts->entries, nentry, 256);
3156 ctts->do_pts = TRUE;
3158 entry->samplecount += nsamples;
3163 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
3165 if (stbl->ctts == NULL) {
3166 stbl->ctts = atom_ctts_new ();
3168 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
3172 atom_stbl_add_samples (AtomSTBL * stbl, guint32 nsamples, guint32 delta,
3173 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3175 atom_stts_add_entry (&stbl->stts, nsamples, delta);
3176 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
3177 if (atom_stco64_add_entry (&stbl->stco64, chunk_offset)) {
3178 atom_stsc_add_new_entry (&stbl->stsc,
3179 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
3181 atom_stsc_update_entry (&stbl->stsc,
3182 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
3186 atom_stbl_add_stss_entry (stbl);
3187 /* always store to arrange for consistent content */
3188 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
3192 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
3193 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3195 AtomSTBL *stbl = &trak->mdia.minf.stbl;
3196 atom_stbl_add_samples (stbl, nsamples, delta, size, chunk_offset, sync,
3200 /* trak and moov molding */
3203 atom_trak_get_timescale (AtomTRAK * trak)
3205 return trak->mdia.mdhd.time_info.timescale;
3209 atom_trak_get_id (AtomTRAK * trak)
3211 return trak->tkhd.track_ID;
3215 atom_trak_set_id (AtomTRAK * trak, guint32 id)
3217 trak->tkhd.track_ID = id;
3221 atom_moov_add_trex (AtomMOOV * moov, AtomTREX * trex)
3223 moov->mvex.trexs = g_list_append (moov->mvex.trexs, trex);
3227 atom_trex_new (AtomTRAK * trak)
3229 guint8 flags[3] = { 0, 0, 0 };
3230 AtomTREX *trex = g_new0 (AtomTREX, 1);
3232 atom_full_init (&trex->header, FOURCC_trex, 0, 0, 0, flags);
3234 trex->track_ID = trak->tkhd.track_ID;
3235 trex->default_sample_description_index = 1;
3236 trex->default_sample_duration = 0;
3237 trex->default_sample_size = 0;
3238 trex->default_sample_flags = 0;
3244 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
3246 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
3247 moov->traks = g_list_append (moov->traks, trak);
3248 /* additional trak means also new trex */
3249 atom_moov_add_trex (moov, atom_trex_new (trak));
3253 atom_moov_get_trak_count (AtomMOOV * moov)
3255 return g_list_length (moov->traks);
3259 atom_trak_get_duration (AtomTRAK * trak)
3261 return trak->tkhd.duration;
3265 atom_stts_get_total_duration (AtomSTTS * stts)
3270 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
3271 STTSEntry *entry = &atom_array_index (&stts->entries, i);
3273 sum += (guint64) (entry->sample_count) * entry->sample_delta;
3279 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
3281 trak->mdia.mdhd.time_info.duration =
3282 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
3283 if (trak->mdia.mdhd.time_info.timescale != 0) {
3284 trak->tkhd.duration =
3285 gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration,
3286 moov_timescale, trak->mdia.mdhd.time_info.timescale);
3288 trak->tkhd.duration = 0;
3293 timecode_atom_trak_set_duration (AtomTRAK * trak, guint64 duration,
3299 /* Sanity checks to ensure we have a timecode */
3300 g_assert (trak->mdia.minf.gmhd != NULL);
3301 g_assert (atom_array_get_len (&trak->mdia.minf.stbl.stts.entries) == 1);
3303 for (iter = trak->mdia.minf.stbl.stsd.entries; iter;
3304 iter = g_list_next (iter)) {
3305 SampleTableEntry *entry = iter->data;
3306 if (entry->kind == TIMECODE) {
3307 SampleTableEntryTMCD *tmcd = (SampleTableEntryTMCD *) entry;
3309 duration = duration * tmcd->timescale / timescale;
3310 timescale = tmcd->timescale;
3315 trak->tkhd.duration = duration;
3316 trak->mdia.mdhd.time_info.duration = duration;
3317 trak->mdia.mdhd.time_info.timescale = timescale;
3319 entry = &atom_array_index (&trak->mdia.minf.stbl.stts.entries, 0);
3320 entry->sample_delta = duration;
3324 atom_moov_get_timescale (AtomMOOV * moov)
3326 return moov->mvhd.time_info.timescale;
3330 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
3332 moov->mvhd.time_info.timescale = timescale;
3336 atom_moov_update_duration (AtomMOOV * moov)
3338 GList *traks = moov->traks;
3339 guint64 dur, duration = 0;
3342 AtomTRAK *trak = (AtomTRAK *) traks->data;
3344 /* Skip timecodes for now: they have a placeholder duration */
3345 if (trak->mdia.minf.gmhd == NULL || trak->mdia.minf.gmhd->tmcd == NULL) {
3346 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
3347 dur = atom_trak_get_duration (trak);
3351 traks = g_list_next (traks);
3353 /* Now update the duration of the timecodes */
3354 traks = moov->traks;
3356 AtomTRAK *trak = (AtomTRAK *) traks->data;
3358 if (trak->mdia.minf.gmhd != NULL && trak->mdia.minf.gmhd->tmcd != NULL)
3359 timecode_atom_trak_set_duration (trak, duration,
3360 atom_moov_get_timescale (moov));
3361 traks = g_list_next (traks);
3363 moov->mvhd.time_info.duration = duration;
3364 moov->mvex.mehd.fragment_duration = duration;
3368 atom_moov_set_fragmented (AtomMOOV * moov, gboolean fragmented)
3370 moov->fragmented = fragmented;
3374 atom_stco64_chunks_set_offset (AtomSTCO64 * stco64, guint32 offset)
3376 stco64->chunk_offset = offset;
3380 atom_moov_chunks_set_offset (AtomMOOV * moov, guint32 offset)
3382 GList *traks = moov->traks;
3384 if (offset == moov->chunks_offset)
3385 return; /* Nothing to do */
3388 AtomTRAK *trak = (AtomTRAK *) traks->data;
3390 atom_stco64_chunks_set_offset (&trak->mdia.minf.stbl.stco64, offset);
3391 traks = g_list_next (traks);
3394 moov->chunks_offset = offset;
3398 atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
3399 guint32 max_bitrate)
3401 AtomESDS *esds = NULL;
3402 AtomData *btrt = NULL;
3403 AtomWAVE *wave = NULL;
3406 GList *extensioniter = NULL;
3408 g_return_if_fail (trak != NULL);
3410 if (avg_bitrate == 0 && max_bitrate == 0)
3413 stsd = &trak->mdia.minf.stbl.stsd;
3414 for (iter = stsd->entries; iter; iter = g_list_next (iter)) {
3415 SampleTableEntry *entry = iter->data;
3417 switch (entry->kind) {
3419 SampleTableEntryMP4A *audioentry = (SampleTableEntryMP4A *) entry;
3420 extensioniter = audioentry->extension_atoms;
3424 SampleTableEntryMP4V *videoentry = (SampleTableEntryMP4V *) entry;
3425 extensioniter = videoentry->extension_atoms;
3433 for (; extensioniter; extensioniter = g_list_next (extensioniter)) {
3434 AtomInfo *atominfo = extensioniter->data;
3435 if (atominfo->atom->type == FOURCC_esds) {
3436 esds = (AtomESDS *) atominfo->atom;
3437 } else if (atominfo->atom->type == FOURCC_btrt) {
3438 btrt = (AtomData *) atominfo->atom;
3439 } else if (atominfo->atom->type == FOURCC_wave) {
3440 wave = (AtomWAVE *) atominfo->atom;
3444 /* wave might have an esds internally */
3446 for (extensioniter = wave->extension_atoms; extensioniter;
3447 extensioniter = g_list_next (extensioniter)) {
3448 AtomInfo *atominfo = extensioniter->data;
3449 if (atominfo->atom->type == FOURCC_esds) {
3450 esds = (AtomESDS *) atominfo->atom;
3457 if (avg_bitrate && esds->es.dec_conf_desc.avg_bitrate == 0)
3458 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
3459 if (max_bitrate && esds->es.dec_conf_desc.max_bitrate == 0)
3460 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
3463 /* type(4bytes) + size(4bytes) + buffersize(4bytes) +
3464 * maxbitrate(bytes) + avgbitrate(bytes) */
3465 if (max_bitrate && GST_READ_UINT32_BE (btrt->data + 4) == 0)
3466 GST_WRITE_UINT32_BE (btrt->data + 4, max_bitrate);
3467 if (avg_bitrate && GST_READ_UINT32_BE (btrt->data + 8) == 0)
3468 GST_WRITE_UINT32_BE (btrt->data + 8, avg_bitrate);
3473 atom_trak_tx3g_update_dimension (AtomTRAK * trak, guint32 width, guint32 height)
3477 SampleTableEntryTX3G *tx3g = NULL;
3479 stsd = &trak->mdia.minf.stbl.stsd;
3480 for (iter = stsd->entries; iter && tx3g == NULL; iter = g_list_next (iter)) {
3481 SampleTableEntry *entry = iter->data;
3483 switch (entry->kind) {
3485 tx3g = (SampleTableEntryTX3G *) entry;
3493 /* Currently we never set the vertical placement flag, so we don't
3494 * check for it to set the dimensions differently as the spec says.
3495 * Always do it for the not set case */
3497 tx3g->font_size = 0.05 * height;
3499 height = 0.15 * height;
3500 trak->tkhd.width = width << 16;
3501 trak->tkhd.height = height << 16;
3502 tx3g->default_text_box = width | (height << 16);
3507 * Meta tags functions
3510 atom_tag_data_alloc_data (AtomTagData * data, guint size)
3512 g_free (data->data);
3513 data->data = g_new0 (guint8, size);
3514 data->datalen = size;
3518 atom_udta_append_tag (AtomUDTA * udta, AtomInfo * tag)
3523 entries = &udta->meta->ilst->entries;
3525 entries = &udta->entries;
3526 *entries = g_list_append (*entries, tag);
3530 atom_udta_add_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3531 const guint8 * data, guint size)
3536 tag = atom_tag_new (fourcc, flags);
3538 atom_tag_data_alloc_data (tdata, size);
3539 memmove (tdata->data, data, size);
3541 atom_udta_append_tag (udta,
3542 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
3547 atom_udta_add_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3549 gint len = strlen (value);
3552 atom_udta_add_tag (udta, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
3556 atom_udta_add_uint_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3559 guint8 data[8] = { 0, };
3562 GST_WRITE_UINT16_BE (data, value);
3563 atom_udta_add_tag (udta, fourcc, flags, data, 2);
3565 GST_WRITE_UINT32_BE (data + 2, value);
3566 atom_udta_add_tag (udta, fourcc, flags, data, 8);
3570 #define GST_BUFFER_NEW_READONLY(mem, size) \
3571 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, mem, size, \
3575 atom_udta_add_blob_tag (AtomUDTA * udta, guint8 * data, guint size)
3577 AtomData *data_atom;
3584 /* blob is unparsed atom;
3585 * extract size and fourcc, and wrap remainder in data atom */
3586 len = GST_READ_UINT32_BE (data);
3587 fourcc = GST_READ_UINT32_LE (data + 4);
3591 data_atom = atom_data_new_from_data (fourcc, data + 8, len - 8);
3593 atom_udta_append_tag (udta,
3594 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3599 atom_udta_add_3gp_tag (AtomUDTA * udta, guint32 fourcc, guint8 * data,
3602 AtomData *data_atom;
3604 data_atom = atom_data_new (fourcc);
3606 /* need full atom */
3607 atom_data_alloc_mem (data_atom, size + 4);
3609 /* full atom: version and flags */
3610 GST_WRITE_UINT32_BE (data_atom->data, 0);
3611 memcpy (data_atom->data + 4, data, size);
3613 atom_udta_append_tag (udta,
3614 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3619 language_code (const char *lang)
3621 g_return_val_if_fail (lang != NULL, 0);
3622 g_return_val_if_fail (strlen (lang) == 3, 0);
3624 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
3625 ((lang[2] - 0x60) & 0x1F);
3629 atom_udta_add_3gp_str_int_tag (AtomUDTA * udta, guint32 fourcc,
3630 const gchar * value, gint16 ivalue)
3632 gint len = 0, size = 0;
3636 len = strlen (value);
3643 data = g_malloc (size + 3);
3644 /* language tag and null-terminated UTF-8 string */
3646 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
3647 /* include 0 terminator */
3648 memcpy (data + 2, value, len + 1);
3650 /* 16-bit unsigned int if standalone, otherwise 8-bit */
3653 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
3655 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
3660 atom_udta_add_3gp_tag (udta, fourcc, data, size);
3665 atom_udta_add_3gp_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3667 atom_udta_add_3gp_str_int_tag (udta, fourcc, value, -1);
3671 atom_udta_add_3gp_uint_tag (AtomUDTA * udta, guint32 fourcc, guint16 value)
3673 atom_udta_add_3gp_str_int_tag (udta, fourcc, NULL, value);
3677 atom_udta_add_xmp_tags (AtomUDTA * udta, GstBuffer * xmpbuffer)
3679 AtomData *data_atom = NULL;
3681 if (udta->context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3683 data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
3684 udta->entries = g_list_append (udta->entries,
3685 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3689 GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
3694 * Functions for specifying media types
3698 atom_minf_set_audio (AtomMINF * minf)
3700 atom_minf_clear_handlers (minf);
3701 minf->smhd = atom_smhd_new ();
3705 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
3707 atom_minf_clear_handlers (minf);
3708 minf->vmhd = atom_vmhd_new (context);
3712 atom_minf_set_subtitle (AtomMINF * minf)
3714 atom_minf_clear_handlers (minf);
3718 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
3721 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3722 hdlr->component_type = comp_type;
3724 hdlr->handler_type = hdlr_type;
3728 atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
3730 g_free (hdlr->name);
3731 hdlr->name = g_strdup (name);
3735 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
3737 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3738 /* Some players (low-end hardware) check for this name, which is what
3739 * QuickTime itself sets */
3740 atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3744 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3746 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3747 /* Some players (low-end hardware) check for this name, which is what
3748 * QuickTime itself sets */
3749 atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3753 atom_mdia_set_hdlr_type_subtitle (AtomMDIA * mdia, AtomsContext * context)
3755 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_sbtl);
3757 /* Just follows the pattern from video and audio above */
3758 atom_hdlr_set_name (&mdia->hdlr, "SubtitleHandler");
3762 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3764 atom_mdia_set_hdlr_type_audio (mdia, context);
3765 atom_minf_set_audio (&mdia->minf);
3769 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3771 atom_mdia_set_hdlr_type_video (mdia, context);
3772 atom_minf_set_video (&mdia->minf, context);
3776 atom_mdia_set_subtitle (AtomMDIA * mdia, AtomsContext * context)
3778 atom_mdia_set_hdlr_type_subtitle (mdia, context);
3779 atom_minf_set_subtitle (&mdia->minf);
3783 atom_tkhd_set_audio (AtomTKHD * tkhd)
3785 tkhd->volume = 0x0100;
3786 tkhd->width = tkhd->height = 0;
3790 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3795 /* qt and ISO base media do not contradict, and examples agree */
3796 tkhd->width = width;
3797 tkhd->height = height;
3801 atom_tkhd_set_subtitle (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3806 /* qt and ISO base media do not contradict, and examples agree */
3807 tkhd->width = width;
3808 tkhd->height = height;
3813 atom_edts_add_entry (AtomEDTS * edts, gint index, EditListEntry * entry)
3816 (EditListEntry *) g_slist_nth_data (edts->elst.entries, index);
3817 /* Create a new entry if missing (appends to the list if index is larger) */
3819 e = g_new (EditListEntry, 1);
3820 edts->elst.entries = g_slist_insert (edts->elst.entries, e, index);
3823 /* Update the entry */
3828 atom_trak_edts_clear (AtomTRAK * trak)
3831 atom_edts_free (trak->edts);
3837 * Update an entry in this trak edits list, creating it if needed.
3838 * index is the index of the entry to update, or create if it's past the end.
3839 * duration is in the moov's timescale
3840 * media_time is the offset in the media time to start from (media's timescale)
3841 * rate is a 32 bits fixed-point
3844 atom_trak_set_elst_entry (AtomTRAK * trak, gint index,
3845 guint32 duration, guint32 media_time, guint32 rate)
3847 EditListEntry entry;
3849 entry.duration = duration;
3850 entry.media_time = media_time;
3851 entry.media_rate = rate;
3853 if (trak->edts == NULL)
3854 trak->edts = atom_edts_new ();
3856 atom_edts_add_entry (trak->edts, index, &entry);
3859 /* re-negotiation is prevented at top-level, so only 1 entry expected.
3860 * Quite some more care here and elsewhere may be needed to
3861 * support several entries */
3862 static SampleTableEntryMP4A *
3863 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
3866 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3867 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
3869 mp4a->se.header.type = type;
3870 mp4a->se.kind = AUDIO;
3871 mp4a->compression_id = -1;
3872 mp4a->se.data_reference_index = 1;
3874 stsd->entries = g_list_prepend (stsd->entries, mp4a);
3879 /* return number of centiframes per second */
3881 atom_framerate_to_timescale (gint n, gint d)
3886 if (d != 1 && d != 1001) {
3887 /* otherwise there are probably rounding errors and we should rather guess
3888 * if it's close enough to a well known framerate */
3889 gst_video_guess_framerate (gst_util_uint64_scale (d, GST_SECOND, n), &n,
3893 return gst_util_uint64_scale (n, 100, d);
3896 static SampleTableEntryTMCD *
3897 atom_trak_add_timecode_entry (AtomTRAK * trak, AtomsContext * context,
3898 guint32 trak_timescale, GstVideoTimeCode * tc)
3900 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3901 SampleTableEntryTMCD *tmcd = sample_entry_tmcd_new ();
3903 g_assert (trak_timescale != 0);
3905 trak->mdia.hdlr.component_type = FOURCC_mhlr;
3906 trak->mdia.hdlr.handler_type = FOURCC_tmcd;
3907 g_free (trak->mdia.hdlr.name);
3908 trak->mdia.hdlr.name = g_strdup ("Time Code Media Handler");
3909 trak->mdia.mdhd.time_info.timescale = trak_timescale;
3911 tmcd->se.kind = TIMECODE;
3912 tmcd->se.data_reference_index = 1;
3913 tmcd->tc_flags = TC_24H_MAX;
3914 if (tc->config.flags &= GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME)
3915 tmcd->tc_flags |= TC_DROP_FRAME;
3916 tmcd->name.language_code = 0;
3917 tmcd->name.name = g_strdup ("Tape");
3918 tmcd->timescale = trak_timescale;
3919 tmcd->frame_duration =
3920 gst_util_uint64_scale (tmcd->timescale, tc->config.fps_d,
3922 if (tc->config.fps_d == 1001)
3923 tmcd->n_frames = tc->config.fps_n / 1000;
3925 tmcd->n_frames = tc->config.fps_n / tc->config.fps_d;
3927 stsd->entries = g_list_prepend (stsd->entries, tmcd);
3932 static SampleTableEntryMP4V *
3933 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
3936 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
3937 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3939 mp4v->se.header.type = type;
3940 mp4v->se.kind = VIDEO;
3941 mp4v->se.data_reference_index = 1;
3942 mp4v->horizontal_resolution = 72 << 16;
3943 mp4v->vertical_resolution = 72 << 16;
3944 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3945 mp4v->spatial_quality = 512;
3946 mp4v->temporal_quality = 512;
3949 stsd->entries = g_list_prepend (stsd->entries, mp4v);
3954 static SampleTableEntryTX3G *
3955 atom_trak_add_subtitle_entry (AtomTRAK * trak, AtomsContext * context,
3958 SampleTableEntryTX3G *tx3g = sample_entry_tx3g_new ();
3959 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3961 tx3g->se.header.type = type;
3962 tx3g->se.kind = SUBTITLE;
3963 tx3g->se.data_reference_index = 1;
3965 stsd->entries = g_list_prepend (stsd->entries, tx3g);
3972 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
3974 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
3978 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
3980 atom_tkhd_set_audio (&trak->tkhd);
3981 atom_mdia_set_audio (&trak->mdia, context);
3985 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
3988 atom_tkhd_set_video (&trak->tkhd, context, width, height);
3989 atom_mdia_set_video (&trak->mdia, context);
3993 atom_trak_set_subtitle (AtomTRAK * trak, AtomsContext * context)
3995 atom_tkhd_set_subtitle (&trak->tkhd, context, 0, 0);
3996 atom_mdia_set_subtitle (&trak->mdia, context);
4000 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
4003 atom_trak_set_audio (trak, context);
4004 trak->mdia.mdhd.time_info.timescale = rate;
4008 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
4009 guint32 rate, guint32 width, guint32 height)
4011 atom_trak_set_video (trak, context, width, height);
4012 trak->mdia.mdhd.time_info.timescale = rate;
4013 trak->tkhd.width = width << 16;
4014 trak->tkhd.height = height << 16;
4018 atom_trak_set_subtitle_commons (AtomTRAK * trak, AtomsContext * context)
4020 atom_trak_set_subtitle (trak, context);
4021 trak->mdia.mdhd.time_info.timescale = 1000;
4023 trak->tkhd.alternate_group = 2; /* same for all subtitles */
4024 trak->tkhd.layer = -1; /* above video (layer 0) */
4028 sample_table_entry_add_ext_atom (SampleTableEntry * ste, AtomInfo * ext)
4030 GList **list = NULL;
4031 if (ste->kind == AUDIO) {
4032 list = &(((SampleTableEntryMP4A *) ste)->extension_atoms);
4033 } else if (ste->kind == VIDEO) {
4034 list = &(((SampleTableEntryMP4V *) ste)->extension_atoms);
4036 g_assert_not_reached ();
4040 *list = g_list_prepend (*list, ext);
4043 SampleTableEntryMP4A *
4044 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
4045 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
4047 SampleTableEntryMP4A *ste;
4049 atom_trak_set_audio_commons (trak, context, scale);
4050 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4051 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
4053 trak->is_video = FALSE;
4054 trak->is_h264 = FALSE;
4056 ste->version = entry->version;
4057 ste->compression_id = entry->compression_id;
4058 ste->sample_size = entry->sample_size;
4059 ste->sample_rate = entry->sample_rate << 16;
4060 ste->channels = entry->channels;
4062 ste->samples_per_packet = entry->samples_per_packet;
4063 ste->bytes_per_sample = entry->bytes_per_sample;
4064 ste->bytes_per_packet = entry->bytes_per_packet;
4065 ste->bytes_per_frame = entry->bytes_per_frame;
4068 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
4070 /* 0 size means variable size */
4071 atom_trak_set_constant_size_samples (trak, sample_size);
4076 SampleTableEntryTMCD *
4077 atom_trak_set_timecode_type (AtomTRAK * trak, AtomsContext * context,
4078 guint32 trak_timescale, GstVideoTimeCode * tc)
4080 SampleTableEntryTMCD *ste;
4081 AtomGMHD *gmhd = trak->mdia.minf.gmhd;
4083 if (context->flavor != ATOMS_TREE_FLAVOR_MOV) {
4087 ste = atom_trak_add_timecode_entry (trak, context, trak_timescale, tc);
4089 gmhd = atom_gmhd_new ();
4090 gmhd->gmin.graphics_mode = 0x0040;
4091 gmhd->gmin.opcolor[0] = 0x8000;
4092 gmhd->gmin.opcolor[1] = 0x8000;
4093 gmhd->gmin.opcolor[2] = 0x8000;
4094 gmhd->tmcd = atom_tmcd_new ();
4095 gmhd->tmcd->tcmi.text_size = 12;
4096 gmhd->tmcd->tcmi.font_name = g_strdup ("Chicago"); /* Pascal string */
4098 trak->mdia.minf.gmhd = gmhd;
4099 trak->is_video = FALSE;
4100 trak->is_h264 = FALSE;
4106 atom_trak_set_caption_type (AtomTRAK * trak, AtomsContext * context,
4107 guint32 trak_timescale, guint32 caption_type)
4109 SampleTableEntry *ste;
4110 AtomGMHD *gmhd = trak->mdia.minf.gmhd;
4111 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
4113 if (context->flavor != ATOMS_TREE_FLAVOR_MOV) {
4117 trak->mdia.mdhd.time_info.timescale = trak_timescale;
4118 trak->mdia.hdlr.component_type = FOURCC_mhlr;
4119 trak->mdia.hdlr.handler_type = FOURCC_clcp;
4120 g_free (trak->mdia.hdlr.name);
4121 trak->mdia.hdlr.name = g_strdup ("Closed Caption Media Handler");
4123 ste = g_new0 (SampleTableEntry, 1);
4124 atom_sample_entry_init (ste, caption_type);
4125 ste->kind = CLOSEDCAPTION;
4126 ste->data_reference_index = 1;
4127 stsd->entries = g_list_prepend (stsd->entries, ste);
4130 gmhd = atom_gmhd_new ();
4131 gmhd->gmin.graphics_mode = 0x0040;
4132 gmhd->gmin.opcolor[0] = 0x8000;
4133 gmhd->gmin.opcolor[1] = 0x8000;
4134 gmhd->gmin.opcolor[2] = 0x8000;
4136 trak->mdia.minf.gmhd = gmhd;
4137 trak->is_video = FALSE;
4138 trak->is_h264 = FALSE;
4144 build_pasp_extension (gint par_width, gint par_height)
4146 AtomData *atom_data = atom_data_new (FOURCC_pasp);
4149 atom_data_alloc_mem (atom_data, 8);
4150 data = atom_data->data;
4152 /* ihdr = image header box */
4153 GST_WRITE_UINT32_BE (data, par_width);
4154 GST_WRITE_UINT32_BE (data + 4, par_height);
4156 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4161 build_fiel_extension (GstVideoInterlaceMode mode, GstVideoFieldOrder order)
4163 AtomData *atom_data = atom_data_new (FOURCC_fiel);
4168 atom_data_alloc_mem (atom_data, 2);
4169 data = atom_data->data;
4171 if (mode == GST_VIDEO_INTERLACE_MODE_PROGRESSIVE) {
4174 } else if (mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED) {
4176 field_order = order == GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST ? 9 : 14;
4182 GST_WRITE_UINT8 (data, interlace);
4183 GST_WRITE_UINT8 (data + 1, field_order);
4185 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4190 build_colr_extension (const GstVideoColorimetry * colorimetry, gboolean is_mp4)
4192 AtomData *atom_data = atom_data_new (FOURCC_colr);
4195 guint16 transfer_function;
4198 switch (colorimetry->primaries) {
4199 case GST_VIDEO_COLOR_PRIMARIES_BT709:
4202 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
4205 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
4206 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
4209 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
4212 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
4218 switch (colorimetry->transfer) {
4219 case GST_VIDEO_TRANSFER_BT709:
4220 transfer_function = 1;
4222 case GST_VIDEO_TRANSFER_SMPTE240M:
4223 transfer_function = 7;
4225 case GST_VIDEO_TRANSFER_UNKNOWN:
4227 transfer_function = 2;
4231 switch (colorimetry->matrix) {
4232 case GST_VIDEO_COLOR_MATRIX_BT709:
4235 case GST_VIDEO_COLOR_MATRIX_BT601:
4238 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
4241 case GST_VIDEO_COLOR_MATRIX_BT2020:
4244 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
4250 atom_data_alloc_mem (atom_data, 10 + (is_mp4 ? 1 : 0));
4251 data = atom_data->data;
4253 /* colour specification box */
4255 GST_WRITE_UINT32_LE (data, FOURCC_nclx);
4257 GST_WRITE_UINT32_LE (data, FOURCC_nclc);
4259 GST_WRITE_UINT16_BE (data + 4, primaries);
4260 GST_WRITE_UINT16_BE (data + 6, transfer_function);
4261 GST_WRITE_UINT16_BE (data + 8, matrix);
4264 GST_WRITE_UINT8 (data + 10,
4265 colorimetry->range == GST_VIDEO_COLOR_RANGE_0_255 ? 0x80 : 0x00);
4268 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4273 build_clap_extension (gint width_n, gint width_d, gint height_n, gint height_d,
4274 gint h_off_n, gint h_off_d, gint v_off_n, gint v_off_d)
4276 AtomData *atom_data = atom_data_new (FOURCC_clap);
4279 atom_data_alloc_mem (atom_data, 32);
4280 data = atom_data->data;
4282 GST_WRITE_UINT32_BE (data, width_n);
4283 GST_WRITE_UINT32_BE (data + 4, width_d);
4284 GST_WRITE_UINT32_BE (data + 8, height_n);
4285 GST_WRITE_UINT32_BE (data + 12, height_d);
4286 GST_WRITE_UINT32_BE (data + 16, h_off_n);
4287 GST_WRITE_UINT32_BE (data + 20, h_off_d);
4288 GST_WRITE_UINT32_BE (data + 24, v_off_n);
4289 GST_WRITE_UINT32_BE (data + 28, v_off_d);
4291 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4296 build_tapt_extension (gint clef_width, gint clef_height, gint prof_width,
4297 gint prof_height, gint enof_width, gint enof_height)
4299 AtomData *atom_data = atom_data_new (FOURCC_tapt);
4302 atom_data_alloc_mem (atom_data, 60);
4303 data = atom_data->data;
4305 GST_WRITE_UINT32_BE (data, 20);
4306 GST_WRITE_UINT32_LE (data + 4, FOURCC_clef);
4307 GST_WRITE_UINT32_BE (data + 8, 0);
4308 GST_WRITE_UINT32_BE (data + 12, clef_width);
4309 GST_WRITE_UINT32_BE (data + 16, clef_height);
4311 GST_WRITE_UINT32_BE (data + 20, 20);
4312 GST_WRITE_UINT32_LE (data + 24, FOURCC_prof);
4313 GST_WRITE_UINT32_BE (data + 28, 0);
4314 GST_WRITE_UINT32_BE (data + 32, prof_width);
4315 GST_WRITE_UINT32_BE (data + 36, prof_height);
4317 GST_WRITE_UINT32_BE (data + 40, 20);
4318 GST_WRITE_UINT32_LE (data + 44, FOURCC_enof);
4319 GST_WRITE_UINT32_BE (data + 48, 0);
4320 GST_WRITE_UINT32_BE (data + 52, enof_width);
4321 GST_WRITE_UINT32_BE (data + 56, enof_height);
4324 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4329 build_mov_video_sample_description_padding_extension (void)
4331 AtomData *atom_data = atom_data_new (FOURCC_clap);
4333 return build_atom_info_wrapper ((Atom *) atom_data, atom_copy_empty,
4337 SampleTableEntryMP4V *
4338 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
4339 VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
4341 SampleTableEntryMP4V *ste;
4342 guint dwidth, dheight;
4343 gint par_n = 0, par_d = 0;
4345 par_n = entry->par_n;
4346 par_d = entry->par_d;
4348 dwidth = entry->width;
4349 dheight = entry->height;
4350 /* ISO file spec says track header w/h indicates track's visual presentation
4351 * (so this together with pixels w/h implicitly defines PAR) */
4352 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
4353 /* Assumes square pixels display */
4354 if (!gst_video_calculate_display_ratio (&dwidth, &dheight, entry->width,
4355 entry->height, par_n, par_d, 1, 1)) {
4356 GST_WARNING ("Could not calculate display ratio");
4360 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
4361 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4362 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
4364 trak->is_video = TRUE;
4365 trak->is_h264 = (entry->fourcc == FOURCC_avc1
4366 || entry->fourcc == FOURCC_avc3);
4368 ste->version = entry->version;
4369 ste->width = entry->width;
4370 ste->height = entry->height;
4371 ste->depth = entry->depth;
4372 ste->color_table_id = entry->color_table_id;
4373 ste->frame_count = entry->frame_count;
4376 ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
4378 ste->extension_atoms = g_list_append (ste->extension_atoms,
4379 build_pasp_extension (par_n, par_d));
4381 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
4382 /* append 0 as a terminator "length" to work around some broken software */
4383 ste->extension_atoms =
4384 g_list_append (ste->extension_atoms,
4385 build_mov_video_sample_description_padding_extension ());
4392 subtitle_sample_entry_init (SubtitleSampleEntry * entry)
4394 entry->font_size = 0;
4395 entry->font_face = 0;
4396 entry->foreground_color_rgba = 0xFFFFFFFF; /* all white, opaque */
4399 SampleTableEntryTX3G *
4400 atom_trak_set_subtitle_type (AtomTRAK * trak, AtomsContext * context,
4401 SubtitleSampleEntry * entry)
4403 SampleTableEntryTX3G *tx3g;
4405 atom_trak_set_subtitle_commons (trak, context);
4406 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4407 tx3g = atom_trak_add_subtitle_entry (trak, context, entry->fourcc);
4409 tx3g->font_face = entry->font_face;
4410 tx3g->font_size = entry->font_size;
4411 tx3g->foreground_color_rgba = entry->foreground_color_rgba;
4413 trak->is_video = FALSE;
4414 trak->is_h264 = FALSE;
4420 atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
4422 guint8 flags[3] = { 0, 0, 0 };
4424 atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
4425 mfhd->sequence_number = sequence_number;
4429 atom_moof_init (AtomMOOF * moof, AtomsContext * context,
4430 guint32 sequence_number)
4432 atom_header_set (&moof->header, FOURCC_moof, 0, 0);
4433 atom_mfhd_init (&moof->mfhd, sequence_number);
4438 atom_moof_new (AtomsContext * context, guint32 sequence_number)
4440 AtomMOOF *moof = g_new0 (AtomMOOF, 1);
4442 atom_moof_init (moof, context, sequence_number);
4447 atom_trun_free (AtomTRUN * trun)
4449 atom_full_clear (&trun->header);
4450 atom_array_clear (&trun->entries);
4455 atom_sdtp_free (AtomSDTP * sdtp)
4457 atom_full_clear (&sdtp->header);
4458 atom_array_clear (&sdtp->entries);
4463 atom_traf_free (AtomTRAF * traf)
4467 walker = traf->truns;
4469 atom_trun_free ((AtomTRUN *) walker->data);
4470 walker = g_list_next (walker);
4472 g_list_free (traf->truns);
4475 walker = traf->sdtps;
4477 atom_sdtp_free ((AtomSDTP *) walker->data);
4478 walker = g_list_next (walker);
4480 g_list_free (traf->sdtps);
4487 atom_moof_free (AtomMOOF * moof)
4491 walker = moof->trafs;
4493 atom_traf_free ((AtomTRAF *) walker->data);
4494 walker = g_list_next (walker);
4496 g_list_free (moof->trafs);
4503 atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
4506 guint64 original_offset = *offset;
4508 if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
4512 prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
4514 atom_write_size (buffer, size, offset, original_offset);
4515 return *offset - original_offset;
4519 atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
4522 guint64 original_offset = *offset;
4525 if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
4529 prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
4531 flags = atom_full_get_flags_as_uint (&tfhd->header);
4533 if (flags & TF_BASE_DATA_OFFSET)
4534 prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
4535 if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
4536 prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
4537 if (flags & TF_DEFAULT_SAMPLE_DURATION)
4538 prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
4539 if (flags & TF_DEFAULT_SAMPLE_SIZE)
4540 prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
4541 if (flags & TF_DEFAULT_SAMPLE_FLAGS)
4542 prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
4544 atom_write_size (buffer, size, offset, original_offset);
4545 return *offset - original_offset;
4549 atom_tfdt_copy_data (AtomTFDT * tfdt, guint8 ** buffer, guint64 * size,
4552 guint64 original_offset = *offset;
4554 if (!atom_full_copy_data (&tfdt->header, buffer, size, offset)) {
4558 /* 32-bit time if version == 0 else 64-bit: */
4559 if (tfdt->header.version == 0)
4560 prop_copy_uint32 (tfdt->base_media_decode_time, buffer, size, offset);
4562 prop_copy_uint64 (tfdt->base_media_decode_time, buffer, size, offset);
4564 atom_write_size (buffer, size, offset, original_offset);
4565 return *offset - original_offset;
4569 atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
4570 guint64 * offset, guint32 * data_offset)
4572 guint64 original_offset = *offset;
4575 flags = atom_full_get_flags_as_uint (&trun->header);
4577 /* if first trun in moof, forcibly add data_offset and record
4578 * where it must be written later on */
4579 if (data_offset && !*data_offset) {
4580 flags |= TR_DATA_OFFSET;
4582 flags &= ~TR_DATA_OFFSET;
4585 atom_full_set_flags_as_uint (&trun->header, flags);
4587 if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
4591 prop_copy_uint32 (trun->sample_count, buffer, size, offset);
4593 if (flags & TR_DATA_OFFSET) {
4594 *data_offset = *offset;
4595 prop_copy_int32 (trun->data_offset, buffer, size, offset);
4597 if (flags & TR_FIRST_SAMPLE_FLAGS)
4598 prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
4600 for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
4601 TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
4603 if (flags & TR_SAMPLE_DURATION)
4604 prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
4605 if (flags & TR_SAMPLE_SIZE)
4606 prop_copy_uint32 (entry->sample_size, buffer, size, offset);
4607 if (flags & TR_SAMPLE_FLAGS)
4608 prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
4609 if (flags & TR_COMPOSITION_TIME_OFFSETS)
4610 prop_copy_uint32 (entry->sample_composition_time_offset,
4611 buffer, size, offset);
4614 atom_write_size (buffer, size, offset, original_offset);
4615 return *offset - original_offset;
4619 atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
4622 guint64 original_offset = *offset;
4624 if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
4628 /* all entries at once */
4629 prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
4630 atom_array_get_len (&sdtp->entries), buffer, size, offset);
4632 atom_write_size (buffer, size, offset, original_offset);
4633 return *offset - original_offset;
4637 atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
4638 guint64 * offset, guint32 * data_offset)
4640 guint64 original_offset = *offset;
4643 if (!atom_copy_data (&traf->header, buffer, size, offset)) {
4646 if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
4649 if (!atom_tfdt_copy_data (&traf->tfdt, buffer, size, offset)) {
4653 walker = g_list_first (traf->truns);
4654 while (walker != NULL) {
4655 if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset,
4659 walker = g_list_next (walker);
4662 walker = g_list_first (traf->sdtps);
4663 while (walker != NULL) {
4664 if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
4667 walker = g_list_next (walker);
4670 atom_write_size (buffer, size, offset, original_offset);
4671 return *offset - original_offset;
4674 /* creates moof atom; metadata is written expecting actual buffer data
4675 * is in mdata directly after moof, and is consecutively written per trak */
4677 atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
4678 guint64 * size, guint64 * offset)
4680 guint64 original_offset = *offset;
4682 guint32 data_offset = 0;
4684 if (!atom_copy_data (&moof->header, buffer, size, offset))
4687 if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
4690 walker = g_list_first (moof->trafs);
4691 while (walker != NULL) {
4692 if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset,
4696 walker = g_list_next (walker);
4699 atom_write_size (buffer, size, offset, original_offset);
4701 if (*buffer && data_offset) {
4702 /* first trun needs a data-offset relative to moof start
4703 * = moof size + mdat prefix */
4704 GST_WRITE_UINT32_BE (*buffer + data_offset, *offset - original_offset + 8);
4707 return *offset - original_offset;
4711 atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
4713 guint8 flags[3] = { 0, 0, 0 };
4715 atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
4716 tfhd->track_ID = track_ID;
4717 tfhd->base_data_offset = 0;
4718 tfhd->sample_description_index = 1;
4719 tfhd->default_sample_duration = 0;
4720 tfhd->default_sample_size = 0;
4721 tfhd->default_sample_flags = 0;
4725 atom_tfdt_init (AtomTFDT * tfdt)
4727 guint8 flags[3] = { 0, 0, 0 };
4728 atom_full_init (&tfdt->header, FOURCC_tfdt, 0, 0, 0, flags);
4730 tfdt->base_media_decode_time = 0;
4734 atom_trun_init (AtomTRUN * trun)
4736 guint8 flags[3] = { 0, 0, 0 };
4738 atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
4739 trun->sample_count = 0;
4740 trun->data_offset = 0;
4741 trun->first_sample_flags = 0;
4742 atom_array_init (&trun->entries, 512);
4746 atom_trun_new (void)
4748 AtomTRUN *trun = g_new0 (AtomTRUN, 1);
4750 atom_trun_init (trun);
4755 atom_sdtp_init (AtomSDTP * sdtp)
4757 guint8 flags[3] = { 0, 0, 0 };
4759 atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
4760 atom_array_init (&sdtp->entries, 512);
4764 atom_sdtp_new (AtomsContext * context)
4766 AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
4768 atom_sdtp_init (sdtp);
4773 atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
4775 traf->sdtps = g_list_append (traf->sdtps, sdtp);
4779 atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
4781 /* it does not make much/any sense according to specs,
4782 * but that's how MS isml samples seem to do it */
4783 atom_array_append (&sdtp->entries, val, 256);
4787 atom_trun_add_samples (AtomTRUN * trun, guint32 delta, guint32 size,
4788 guint32 flags, gint64 pts_offset)
4790 TRUNSampleEntry nentry;
4792 if (pts_offset != 0)
4793 trun->header.flags[1] |= (TR_COMPOSITION_TIME_OFFSETS >> 8);
4795 nentry.sample_duration = delta;
4796 nentry.sample_size = size;
4797 nentry.sample_flags = flags;
4798 nentry.sample_composition_time_offset = pts_offset;
4799 atom_array_append (&trun->entries, nentry, 256);
4800 trun->sample_count++;
4804 atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
4806 atom_header_set (&traf->header, FOURCC_traf, 0, 0);
4807 atom_tfdt_init (&traf->tfdt);
4808 atom_tfhd_init (&traf->tfhd, track_ID);
4811 if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
4812 atom_traf_add_sdtp (traf, atom_sdtp_new (context));
4816 atom_traf_new (AtomsContext * context, guint32 track_ID)
4818 AtomTRAF *traf = g_new0 (AtomTRAF, 1);
4820 atom_traf_init (traf, context, track_ID);
4825 atom_traf_set_base_decode_time (AtomTRAF * traf, guint64 base_decode_time)
4827 traf->tfdt.base_media_decode_time = base_decode_time;
4828 /* If we need to write a 64-bit tfdt, set the atom version */
4829 if (base_decode_time > G_MAXUINT32)
4830 traf->tfdt.header.version = 1;
4834 atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
4836 traf->truns = g_list_append (traf->truns, trun);
4840 atom_traf_add_samples (AtomTRAF * traf, guint32 delta, guint32 size,
4841 gboolean sync, gint64 pts_offset, gboolean sdtp_sync)
4846 /* 0x10000 is sample-is-difference-sample flag
4847 * low byte stuff is what ismv uses */
4848 flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
4850 if (G_UNLIKELY (!traf->truns)) {
4851 trun = atom_trun_new ();
4852 atom_traf_add_trun (traf, trun);
4853 /* optimistic; indicate all defaults present in tfhd */
4854 traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
4855 TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
4856 traf->tfhd.default_sample_duration = delta;
4857 traf->tfhd.default_sample_size = size;
4858 traf->tfhd.default_sample_flags = flags;
4859 trun->first_sample_flags = flags;
4862 trun = traf->truns->data;
4864 /* check if still matching defaults,
4865 * if not, abandon default and need entry for each sample */
4866 if (traf->tfhd.default_sample_duration != delta) {
4867 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
4868 trun->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
4870 if (traf->tfhd.default_sample_size != size) {
4871 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
4872 trun->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
4874 if (traf->tfhd.default_sample_flags != flags) {
4875 if (trun->sample_count == 1) {
4876 /* at least will need first sample flag */
4877 traf->tfhd.default_sample_flags = flags;
4878 trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
4880 /* now we need sample flags for each sample */
4881 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
4882 trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
4883 trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
4887 atom_trun_add_samples (traf->truns->data, delta, size, flags, pts_offset);
4890 atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
4894 atom_traf_get_sample_num (AtomTRAF * traf)
4898 if (G_UNLIKELY (!traf->truns))
4901 trun = traf->truns->data;
4902 return atom_array_get_len (&trun->entries);
4906 atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
4908 moof->trafs = g_list_append (moof->trafs, traf);
4912 atom_tfra_free (AtomTFRA * tfra)
4914 atom_full_clear (&tfra->header);
4915 atom_array_clear (&tfra->entries);
4920 atom_mfra_new (AtomsContext * context)
4922 AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
4924 atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
4929 atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
4931 mfra->tfras = g_list_append (mfra->tfras, tfra);
4935 atom_mfra_free (AtomMFRA * mfra)
4939 walker = mfra->tfras;
4941 atom_tfra_free ((AtomTFRA *) walker->data);
4942 walker = g_list_next (walker);
4944 g_list_free (mfra->tfras);
4947 atom_clear (&mfra->header);
4952 atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
4954 guint8 flags[3] = { 0, 0, 0 };
4956 atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
4957 tfra->track_ID = track_ID;
4958 atom_array_init (&tfra->entries, 512);
4962 atom_tfra_new (AtomsContext * context, guint32 track_ID)
4964 AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
4966 atom_tfra_init (tfra, track_ID);
4972 need_bytes (guint32 num)
4983 atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
4989 entry.moof_offset = 0;
4990 /* always write a single trun in a single traf */
4991 entry.traf_number = 1;
4992 entry.trun_number = 1;
4993 entry.sample_number = sample_num;
4995 /* auto-use 64 bits if needed */
4996 if (dts > G_MAXUINT32)
4997 tfra->header.version = 1;
4999 /* 1 byte will always do for traf and trun number,
5000 * check how much sample_num needs */
5001 tfra->lengths = (tfra->lengths & 0xfc) ||
5002 MAX (tfra->lengths, need_bytes (sample_num));
5004 atom_array_append (&tfra->entries, entry, 256);
5008 atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
5012 /* auto-use 64 bits if needed */
5013 if (offset > G_MAXUINT32)
5014 tfra->header.version = 1;
5016 for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
5017 TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
5019 if (entry->moof_offset)
5021 entry->moof_offset = offset;
5026 atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
5029 guint64 original_offset = *offset;
5036 if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
5040 prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
5041 prop_copy_uint32 (tfra->lengths, buffer, size, offset);
5042 prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
5044 version = tfra->header.version;
5045 for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
5046 entry = &atom_array_index (&tfra->entries, i);
5048 prop_copy_uint64 (entry->time, buffer, size, offset);
5049 prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
5051 prop_copy_uint32 (entry->time, buffer, size, offset);
5052 prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
5055 bytes = (tfra->lengths & (0x3 << 4)) + 1;
5056 data = GUINT32_TO_BE (entry->traf_number);
5057 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5058 buffer, size, offset);
5060 bytes = (tfra->lengths & (0x3 << 2)) + 1;
5061 data = GUINT32_TO_BE (entry->trun_number);
5062 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5063 buffer, size, offset);
5065 bytes = (tfra->lengths & (0x3)) + 1;
5066 data = GUINT32_TO_BE (entry->sample_number);
5067 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5068 buffer, size, offset);
5072 atom_write_size (buffer, size, offset, original_offset);
5073 return *offset - original_offset;
5077 atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
5080 guint64 original_offset = *offset;
5081 guint8 flags[3] = { 0, 0, 0 };
5084 atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
5086 if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
5090 prop_copy_uint32 (s, buffer, size, offset);
5092 atom_write_size (buffer, size, offset, original_offset);
5094 return *offset - original_offset;
5099 atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
5102 guint64 original_offset = *offset;
5105 if (!atom_copy_data (&mfra->header, buffer, size, offset))
5108 walker = g_list_first (mfra->tfras);
5109 while (walker != NULL) {
5110 if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
5113 walker = g_list_next (walker);
5116 /* 16 is the size of the mfro atom */
5117 if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
5121 atom_write_size (buffer, size, offset, original_offset);
5122 return *offset - original_offset;
5125 /* some sample description construction helpers */
5128 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
5129 const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
5134 track_id = trak->tkhd.track_ID;
5136 esds = atom_esds_new ();
5137 esds->es.id = track_id & 0xFFFF;
5138 esds->es.dec_conf_desc.object_type = object_type;
5139 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
5141 if (avg_bitrate > 0)
5142 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
5143 if (max_bitrate > 0)
5144 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
5146 /* optional DecoderSpecificInfo */
5148 DecoderSpecificInfoDescriptor *desc;
5151 esds->es.dec_conf_desc.dec_specific_info = desc =
5152 desc_dec_specific_info_new ();
5153 size = gst_buffer_get_size ((GstBuffer *) codec_data);
5154 desc_dec_specific_info_alloc_data (desc, size);
5155 gst_buffer_extract ((GstBuffer *) codec_data, 0, desc->data, size);
5158 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
5163 build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
5164 guint32 max_bitrate)
5166 AtomData *atom_data = atom_data_new (FOURCC_btrt);
5169 atom_data_alloc_mem (atom_data, 12);
5170 data = atom_data->data;
5172 GST_WRITE_UINT32_BE (data, buffer_size_db);
5173 GST_WRITE_UINT32_BE (data + 4, max_bitrate);
5174 GST_WRITE_UINT32_BE (data + 8, avg_bitrate);
5176 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5181 build_mov_wave_extension (guint32 fourcc, AtomInfo * atom1, AtomInfo * atom2,
5182 gboolean terminator)
5188 /* Build WAVE atom for sample table entry */
5189 wave = atom_wave_new ();
5191 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5193 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5194 wave->extension_atoms =
5195 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5196 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5199 /* Add supplied atoms to WAVE */
5201 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
5203 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
5205 /* Add FRMA to the WAVE */
5206 frma = atom_frma_new ();
5207 frma->media_type = fourcc;
5209 wave->extension_atoms =
5210 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5211 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5213 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5218 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
5219 guint32 avg_bitrate, guint32 max_bitrate)
5221 AtomInfo *esds, *mp4a;
5225 /* Add ESDS atom to WAVE */
5226 esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
5227 ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
5229 /* Add MP4A atom to the WAVE:
5230 * not really in spec, but makes offset based players happy */
5231 buf = GST_BUFFER_NEW_READONLY (&tmp, 4);
5232 mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
5233 gst_buffer_unref (buf);
5235 return build_mov_wave_extension (FOURCC_mp4a, mp4a, esds, TRUE);
5239 build_mov_alac_extension (const GstBuffer * codec_data)
5243 alac = build_codec_data_extension (FOURCC_alac, codec_data);
5245 return build_mov_wave_extension (FOURCC_alac, NULL, alac, TRUE);
5249 build_jp2x_extension (const GstBuffer * prefix)
5251 AtomData *atom_data;
5257 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2x, prefix);
5259 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5264 build_jp2h_extension (gint width, gint height, const gchar * colorspace,
5265 gint ncomp, const GValue * cmap_array, const GValue * cdef_array)
5267 AtomData *atom_data;
5271 gint idhr_size = 22;
5272 gint colr_size = 15;
5273 gint cmap_size = 0, cdef_size = 0;
5274 gint cmap_array_size = 0;
5275 gint cdef_array_size = 0;
5276 GstByteWriter writer;
5278 g_return_val_if_fail (cmap_array == NULL ||
5279 GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
5280 g_return_val_if_fail (cdef_array == NULL ||
5281 GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
5283 if (g_str_equal (colorspace, "sRGB")) {
5287 } else if (g_str_equal (colorspace, "GRAY")) {
5291 } else if (g_str_equal (colorspace, "sYUV")) {
5299 cmap_array_size = gst_value_array_get_size (cmap_array);
5300 cmap_size = 8 + cmap_array_size * 4;
5303 cdef_array_size = gst_value_array_get_size (cdef_array);
5304 cdef_size = 8 + 2 + cdef_array_size * 6;
5307 gst_byte_writer_init_with_size (&writer,
5308 idhr_size + colr_size + cmap_size + cdef_size, TRUE);
5310 /* ihdr = image header box */
5311 gst_byte_writer_put_uint32_be_unchecked (&writer, 22);
5312 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_ihdr);
5313 gst_byte_writer_put_uint32_be_unchecked (&writer, height);
5314 gst_byte_writer_put_uint32_be_unchecked (&writer, width);
5315 gst_byte_writer_put_uint16_be_unchecked (&writer, ncomp);
5316 /* 8 bits per component, unsigned */
5317 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5318 /* compression type; reserved */
5319 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5320 /* colour space (un)known */
5321 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5322 /* intellectual property right (box present) */
5323 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5325 /* colour specification box */
5326 gst_byte_writer_put_uint32_be_unchecked (&writer, 15);
5327 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_colr);
5329 /* specification method: enumerated */
5330 gst_byte_writer_put_uint8_unchecked (&writer, 0x1);
5331 /* precedence; reserved */
5332 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5333 /* approximation; reserved */
5334 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5335 /* enumerated colourspace */
5336 gst_byte_writer_put_uint32_be_unchecked (&writer, cenum);
5339 gst_byte_writer_put_uint32_be_unchecked (&writer, cmap_size);
5340 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cmap);
5341 for (i = 0; i < cmap_array_size; i++) {
5347 item = gst_value_array_get_value (cmap_array, i);
5348 value = g_value_get_int (item);
5350 /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
5351 cmp = value & 0xFFFF;
5353 pcol = (value >> 16) & 0xFF;
5356 GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
5357 "handle Pallete mapping atoms yet");
5359 gst_byte_writer_put_uint16_be_unchecked (&writer, cmp);
5360 gst_byte_writer_put_uint8_unchecked (&writer, mtyp);
5361 gst_byte_writer_put_uint8_unchecked (&writer, pcol);
5366 gst_byte_writer_put_uint32_be_unchecked (&writer, cdef_size);
5367 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cdef);
5368 gst_byte_writer_put_uint16_be_unchecked (&writer, cdef_array_size);
5369 for (i = 0; i < cdef_array_size; i++) {
5372 item = gst_value_array_get_value (cdef_array, i);
5373 value = g_value_get_int (item);
5375 gst_byte_writer_put_uint16_be_unchecked (&writer, i);
5377 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5378 gst_byte_writer_put_uint16_be_unchecked (&writer, value);
5379 } else if (value < 0) {
5380 gst_byte_writer_put_uint16_be_unchecked (&writer, -value);
5381 gst_byte_writer_put_uint16_be_unchecked (&writer, 0); /* TODO what here? */
5383 gst_byte_writer_put_uint16_be_unchecked (&writer, 1);
5384 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5389 g_assert (gst_byte_writer_get_remaining (&writer) == 0);
5390 buf = gst_byte_writer_reset_and_get_buffer (&writer);
5392 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
5393 gst_buffer_unref (buf);
5395 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5400 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
5403 AtomInfo *result = NULL;
5406 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
5407 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
5415 build_amr_extension (void)
5422 GST_WRITE_UINT32_LE (ext, 0);
5423 /* decoder version */
5424 GST_WRITE_UINT8 (ext + 4, 0);
5425 /* mode set (all modes) */
5426 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
5427 /* mode change period (no restriction) */
5428 GST_WRITE_UINT8 (ext + 7, 0);
5429 /* frames per sample */
5430 GST_WRITE_UINT8 (ext + 8, 1);
5432 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5433 res = build_codec_data_extension (FOURCC_damr, buf);
5434 gst_buffer_unref (buf);
5439 build_h263_extension (void)
5446 GST_WRITE_UINT32_LE (ext, 0);
5447 /* decoder version */
5448 GST_WRITE_UINT8 (ext + 4, 0);
5449 /* level / profile */
5450 /* FIXME ? maybe ? obtain somewhere; baseline for now */
5451 GST_WRITE_UINT8 (ext + 5, 10);
5452 GST_WRITE_UINT8 (ext + 6, 0);
5454 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5455 res = build_codec_data_extension (FOURCC_d263, buf);
5456 gst_buffer_unref (buf);
5461 build_gama_atom (gdouble gamma)
5467 /* convert to uint32 from fixed point */
5468 gamma_fp = (guint32) 65536 *gamma;
5470 gamma_fp = GUINT32_TO_BE (gamma_fp);
5471 buf = GST_BUFFER_NEW_READONLY (&gamma_fp, 4);
5472 res = build_codec_data_extension (FOURCC_gama, buf);
5473 gst_buffer_unref (buf);
5478 build_SMI_atom (const GstBuffer * seqh)
5485 /* the seqh plus its size and fourcc */
5486 size = gst_buffer_get_size ((GstBuffer *) seqh);
5487 data = g_malloc (size + 8);
5489 GST_WRITE_UINT32_LE (data, FOURCC_SEQH);
5490 GST_WRITE_UINT32_BE (data + 4, size + 8);
5491 gst_buffer_extract ((GstBuffer *) seqh, 0, data + 8, size);
5492 buf = gst_buffer_new_wrapped (data, size + 8);
5493 res = build_codec_data_extension (FOURCC_SMI_, buf);
5494 gst_buffer_unref (buf);
5499 build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
5501 #define IMA_ADPCM_ATOM_SIZE 20
5502 AtomData *atom_data;
5505 gint samplesperblock;
5508 /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
5509 identifier. Note that the identifier here is big-endian, but when used
5510 within the WAVE header (below), it's little endian. */
5511 fourcc = MS_WAVE_FOURCC (0x11);
5513 atom_data = atom_data_new (fourcc);
5514 atom_data_alloc_mem (atom_data, IMA_ADPCM_ATOM_SIZE);
5515 data = atom_data->data;
5517 /* This atom's content is a WAVE header, including 2 bytes of extra data.
5518 Note that all of this is little-endian, unlike most stuff in qt. */
5519 /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
5520 for the rest. Simplifies to this. */
5521 samplesperblock = 2 * blocksize / channels - 7;
5522 bytespersec = rate * blocksize / samplesperblock;
5523 GST_WRITE_UINT16_LE (data, 0x11);
5524 GST_WRITE_UINT16_LE (data + 2, channels);
5525 GST_WRITE_UINT32_LE (data + 4, rate);
5526 GST_WRITE_UINT32_LE (data + 8, bytespersec);
5527 GST_WRITE_UINT16_LE (data + 12, blocksize);
5528 GST_WRITE_UINT16_LE (data + 14, 4);
5529 GST_WRITE_UINT16_LE (data + 16, 2); /* Two extra bytes */
5530 GST_WRITE_UINT16_LE (data + 18, samplesperblock);
5532 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5537 build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
5544 wave = atom_wave_new ();
5546 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5547 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5548 wave->extension_atoms =
5549 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5550 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5552 /* Add wave ima adpcm atom to WAVE */
5553 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
5554 build_ima_adpcm_atom (channels, rate, blocksize));
5556 /* Add FRMA to the WAVE */
5557 frma = atom_frma_new ();
5558 frma->media_type = MS_WAVE_FOURCC (0x11);
5560 wave->extension_atoms =
5561 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5562 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5564 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5569 build_ac3_extension (guint8 fscod, guint8 bsid, guint8 bsmod, guint8 acmod,
5570 guint8 lfe_on, guint8 bitrate_code)
5572 AtomData *atom_data = atom_data_new (FOURCC_dac3);
5575 atom_data_alloc_mem (atom_data, 3);
5576 data = atom_data->data;
5578 /* Bits from the spec
5588 /* Some bit manipulation magic. Need bitwriter */
5589 data[0] = (fscod << 6) | (bsid << 1) | ((bsmod >> 2) & 1);
5591 ((bsmod & 0x3) << 6) | (acmod << 3) | ((lfe_on & 1) << 2) | ((bitrate_code
5593 data[2] = ((bitrate_code & 0x7) << 5);
5595 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5600 build_opus_extension (guint32 rate, guint8 channels, guint8 mapping_family,
5601 guint8 stream_count, guint8 coupled_count, guint8 channel_mapping[256],
5602 guint16 pre_skip, guint16 output_gain)
5604 AtomData *atom_data;
5607 gboolean hdl = TRUE;
5608 guint data_block_len;
5610 gst_byte_writer_init (&bw);
5611 hdl &= gst_byte_writer_put_uint8 (&bw, 0x00); /* version number */
5612 hdl &= gst_byte_writer_put_uint8 (&bw, channels);
5613 hdl &= gst_byte_writer_put_uint16_le (&bw, pre_skip);
5614 hdl &= gst_byte_writer_put_uint32_le (&bw, rate);
5615 hdl &= gst_byte_writer_put_uint16_le (&bw, output_gain);
5616 hdl &= gst_byte_writer_put_uint8 (&bw, mapping_family);
5617 if (mapping_family > 0) {
5618 hdl &= gst_byte_writer_put_uint8 (&bw, stream_count);
5619 hdl &= gst_byte_writer_put_uint8 (&bw, coupled_count);
5620 hdl &= gst_byte_writer_put_data (&bw, channel_mapping, channels);
5624 GST_WARNING ("Error creating header");
5628 data_block_len = gst_byte_writer_get_size (&bw);
5629 data_block = gst_byte_writer_reset_and_get_data (&bw);
5630 atom_data = atom_data_new_from_data (FOURCC_dops, data_block, data_block_len);
5631 g_free (data_block);
5633 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5638 build_uuid_xmp_atom (GstBuffer * xmp_data)
5642 static const guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
5643 0x97, 0xA9, 0x42, 0xE8,
5644 0x9C, 0x71, 0x99, 0x94,
5645 0x91, 0xE3, 0xAF, 0xAC
5648 if (xmp_data == NULL)
5651 uuid = atom_uuid_new ();
5652 memcpy (uuid->uuid, xmp_uuid, 16);
5654 size = gst_buffer_get_size (xmp_data);
5655 uuid->data = g_malloc (size);
5656 uuid->datalen = size;
5657 gst_buffer_extract (xmp_data, 0, uuid->data, size);
5659 return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,