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 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 = get_current_qt_time ();
98 atom_header_set (Atom * header, guint32 fourcc, gint32 size, gint64 ext_size)
100 header->type = fourcc;
102 header->extended_size = ext_size;
106 atom_clear (Atom * atom)
111 atom_full_init (AtomFull * full, guint32 fourcc, gint32 size, gint64 ext_size,
112 guint8 version, guint8 flags[3])
114 atom_header_set (&(full->header), fourcc, size, ext_size);
115 full->version = version;
116 full->flags[0] = flags[0];
117 full->flags[1] = flags[1];
118 full->flags[2] = flags[2];
122 atom_full_clear (AtomFull * full)
124 atom_clear (&full->header);
128 atom_full_free (AtomFull * full)
130 atom_full_clear (full);
135 atom_full_get_flags_as_uint (AtomFull * full)
137 return full->flags[0] << 16 | full->flags[1] << 8 | full->flags[2];
141 atom_full_set_flags_as_uint (AtomFull * full, guint32 flags_as_uint)
143 full->flags[2] = flags_as_uint & 0xFF;
144 full->flags[1] = (flags_as_uint & 0xFF00) >> 8;
145 full->flags[0] = (flags_as_uint & 0xFF0000) >> 16;
149 build_atom_info_wrapper (Atom * atom, gpointer copy_func, gpointer free_func)
151 AtomInfo *info = NULL;
154 info = g_new0 (AtomInfo, 1);
157 info->copy_data_func = copy_func;
158 info->free_func = free_func;
165 atom_info_list_prepend_atom (GList * ai, Atom * atom,
166 AtomCopyDataFunc copy_func, AtomFreeFunc free_func)
169 return g_list_prepend (ai,
170 build_atom_info_wrapper (atom, copy_func, free_func));
176 atom_info_list_free (GList * ai)
179 AtomInfo *info = (AtomInfo *) ai->data;
181 info->free_func (info->atom);
183 ai = g_list_delete_link (ai, ai);
188 atom_data_new (guint32 fourcc)
190 AtomData *data = g_new0 (AtomData, 1);
192 atom_header_set (&data->header, fourcc, 0, 0);
197 atom_data_alloc_mem (AtomData * data, guint32 size)
200 data->data = g_new0 (guint8, size);
201 data->datalen = size;
205 atom_data_new_from_data (guint32 fourcc, const guint8 * mem, gsize size)
207 AtomData *data = atom_data_new (fourcc);
209 atom_data_alloc_mem (data, size);
210 memcpy (data->data, mem, size);
215 atom_data_new_from_gst_buffer (guint32 fourcc, const GstBuffer * buf)
217 AtomData *data = atom_data_new (fourcc);
218 gsize size = gst_buffer_get_size ((GstBuffer *) buf);
220 atom_data_alloc_mem (data, size);
221 gst_buffer_extract ((GstBuffer *) buf, 0, data->data, size);
226 atom_data_free (AtomData * data)
228 atom_clear (&data->header);
236 AtomUUID *uuid = g_new0 (AtomUUID, 1);
238 atom_header_set (&uuid->header, FOURCC_uuid, 0, 0);
243 atom_uuid_free (AtomUUID * data)
245 atom_clear (&data->header);
251 atom_ftyp_init (AtomFTYP * ftyp, guint32 major, guint32 version, GList * brands)
256 atom_header_set (&ftyp->header, FOURCC_ftyp, 16, 0);
257 ftyp->major_brand = major;
258 ftyp->version = version;
260 /* always include major brand as compatible brand */
261 ftyp->compatible_brands_size = g_list_length (brands) + 1;
262 ftyp->compatible_brands = g_new (guint32, ftyp->compatible_brands_size);
264 ftyp->compatible_brands[0] = major;
266 for (it = brands; it != NULL; it = g_list_next (it)) {
267 ftyp->compatible_brands[index++] = GPOINTER_TO_UINT (it->data);
272 atom_ftyp_new (AtomsContext * context, guint32 major, guint32 version,
275 AtomFTYP *ftyp = g_new0 (AtomFTYP, 1);
277 atom_ftyp_init (ftyp, major, version, brands);
282 atom_ftyp_free (AtomFTYP * ftyp)
284 atom_clear (&ftyp->header);
285 g_free (ftyp->compatible_brands);
286 ftyp->compatible_brands = NULL;
291 atom_esds_init (AtomESDS * esds)
293 guint8 flags[3] = { 0, 0, 0 };
295 atom_full_init (&esds->header, FOURCC_esds, 0, 0, 0, flags);
296 desc_es_init (&esds->es);
302 AtomESDS *esds = g_new0 (AtomESDS, 1);
304 atom_esds_init (esds);
309 atom_esds_free (AtomESDS * esds)
311 atom_full_clear (&esds->header);
312 desc_es_descriptor_clear (&esds->es);
319 AtomFRMA *frma = g_new0 (AtomFRMA, 1);
321 atom_header_set (&frma->header, FOURCC_frma, 0, 0);
326 atom_frma_free (AtomFRMA * frma)
328 atom_clear (&frma->header);
335 AtomWAVE *wave = g_new0 (AtomWAVE, 1);
337 atom_header_set (&wave->header, FOURCC_wave, 0, 0);
342 atom_wave_free (AtomWAVE * wave)
344 atom_clear (&wave->header);
345 atom_info_list_free (wave->extension_atoms);
350 atom_elst_init (AtomELST * elst)
352 guint8 flags[3] = { 0, 0, 0 };
353 atom_full_init (&elst->header, FOURCC_elst, 0, 0, 0, flags);
358 atom_elst_clear (AtomELST * elst)
362 atom_full_clear (&elst->header);
363 walker = elst->entries;
365 g_free ((EditListEntry *) walker->data);
366 walker = g_slist_next (walker);
368 g_slist_free (elst->entries);
372 atom_edts_init (AtomEDTS * edts)
374 atom_header_set (&edts->header, FOURCC_edts, 0, 0);
375 atom_elst_init (&edts->elst);
379 atom_edts_clear (AtomEDTS * edts)
381 atom_clear (&edts->header);
382 atom_elst_clear (&edts->elst);
388 AtomEDTS *edts = g_new0 (AtomEDTS, 1);
389 atom_edts_init (edts);
394 atom_edts_free (AtomEDTS * edts)
396 atom_edts_clear (edts);
401 atom_tcmi_init (AtomTCMI * tcmi)
403 guint8 flags[3] = { 0, 0, 0 };
405 atom_full_init (&tcmi->header, FOURCC_tcmi, 0, 0, 0, flags);
409 atom_tcmi_clear (AtomTCMI * tcmi)
411 atom_full_clear (&tcmi->header);
415 tcmi->text_color[0] = 0;
416 tcmi->text_color[1] = 0;
417 tcmi->text_color[2] = 0;
418 tcmi->bg_color[0] = 0;
419 tcmi->bg_color[1] = 0;
420 tcmi->bg_color[2] = 0;
421 g_free (tcmi->font_name);
422 tcmi->font_name = NULL;
426 atom_tmcd_init (AtomTMCD * tmcd)
428 atom_header_set (&tmcd->header, FOURCC_tmcd, 0, 0);
429 atom_tcmi_init (&tmcd->tcmi);
433 atom_tmcd_clear (AtomTMCD * tmcd)
435 atom_clear (&tmcd->header);
436 atom_tcmi_clear (&tmcd->tcmi);
440 atom_gmin_init (AtomGMIN * gmin)
442 guint8 flags[3] = { 0, 0, 0 };
444 atom_full_init (&gmin->header, FOURCC_gmin, 0, 0, 0, flags);
448 atom_gmin_clear (AtomGMIN * gmin)
450 atom_full_clear (&gmin->header);
451 gmin->graphics_mode = 0;
452 gmin->opcolor[0] = 0;
453 gmin->opcolor[1] = 0;
454 gmin->opcolor[2] = 0;
460 atom_gmhd_init (AtomGMHD * gmhd)
462 atom_header_set (&gmhd->header, FOURCC_gmhd, 0, 0);
463 atom_gmin_init (&gmhd->gmin);
464 atom_tmcd_init (&gmhd->tmcd);
468 atom_gmhd_clear (AtomGMHD * gmhd)
470 atom_clear (&gmhd->header);
471 atom_gmin_clear (&gmhd->gmin);
472 atom_tmcd_clear (&gmhd->tmcd);
478 AtomGMHD *gmhd = g_new0 (AtomGMHD, 1);
479 atom_gmhd_init (gmhd);
484 atom_gmhd_free (AtomGMHD * gmhd)
486 atom_gmhd_clear (gmhd);
491 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
493 atom_header_set (&se->header, type, 0, 0);
495 memset (se->reserved, 0, sizeof (guint8) * 6);
496 se->data_reference_index = 0;
500 atom_sample_entry_free (SampleTableEntry * se)
502 atom_clear (&se->header);
506 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
508 atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
511 mp4a->revision_level = 0;
514 mp4a->sample_size = 16;
515 mp4a->compression_id = 0;
516 mp4a->packet_size = 0;
517 mp4a->sample_rate = 0;
518 /* following only used if version is 1 */
519 mp4a->samples_per_packet = 0;
520 mp4a->bytes_per_packet = 0;
521 mp4a->bytes_per_frame = 0;
522 mp4a->bytes_per_sample = 0;
524 mp4a->extension_atoms = NULL;
527 static SampleTableEntryMP4A *
528 sample_entry_mp4a_new (void)
530 SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
532 sample_entry_mp4a_init (mp4a);
537 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
539 atom_sample_entry_free (&mp4a->se);
540 atom_info_list_free (mp4a->extension_atoms);
545 sample_entry_tmcd_init (SampleTableEntryTMCD * tmcd)
547 atom_sample_entry_init (&tmcd->se, FOURCC_tmcd);
551 tmcd->frame_duration = 0;
554 tmcd->name.language_code = 0;
555 g_free (tmcd->name.name);
556 tmcd->name.name = NULL;
559 static SampleTableEntryTMCD *
560 sample_entry_tmcd_new (void)
562 SampleTableEntryTMCD *tmcd = g_new0 (SampleTableEntryTMCD, 1);
564 sample_entry_tmcd_init (tmcd);
569 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
571 atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
574 mp4v->revision_level = 0;
577 mp4v->temporal_quality = 0;
578 mp4v->spatial_quality = 0;
580 /* qt and ISO base media do not contradict, and examples agree */
581 mp4v->horizontal_resolution = 0x00480000;
582 mp4v->vertical_resolution = 0x00480000;
585 mp4v->frame_count = 1;
587 memset (mp4v->compressor, 0, sizeof (guint8) * 32);
590 mp4v->color_table_id = 0;
592 mp4v->extension_atoms = NULL;
596 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
598 atom_sample_entry_free (&mp4v->se);
599 atom_info_list_free (mp4v->extension_atoms);
603 static SampleTableEntryMP4V *
604 sample_entry_mp4v_new (AtomsContext * context)
606 SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
608 sample_entry_mp4v_init (mp4v, context);
613 sample_entry_tx3g_init (SampleTableEntryTX3G * tx3g)
615 atom_sample_entry_init (&tx3g->se, FOURCC_tx3g);
617 tx3g->display_flags = 0;
618 tx3g->font_id = 1; /* must be 1 as there is a single font */
620 tx3g->foreground_color_rgba = 0xFFFFFFFF; /* white, opaque */
622 /* can't set this now */
623 tx3g->default_text_box = 0;
628 sample_entry_tx3g_free (SampleTableEntryTX3G * tx3g)
630 atom_sample_entry_free (&tx3g->se);
634 static SampleTableEntryTX3G *
635 sample_entry_tx3g_new (void)
637 SampleTableEntryTX3G *tx3g = g_new0 (SampleTableEntryTX3G, 1);
639 sample_entry_tx3g_init (tx3g);
645 atom_stsd_init (AtomSTSD * stsd)
647 guint8 flags[3] = { 0, 0, 0 };
649 atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
650 stsd->entries = NULL;
655 atom_stsd_remove_entries (AtomSTSD * stsd)
659 walker = stsd->entries;
662 SampleTableEntry *se = (SampleTableEntry *) aux->data;
664 walker = g_list_next (walker);
665 stsd->entries = g_list_remove_link (stsd->entries, aux);
669 sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
672 sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
675 sample_entry_tx3g_free ((SampleTableEntryTX3G *) se);
678 /* best possible cleanup */
679 atom_sample_entry_free (se);
687 atom_stsd_clear (AtomSTSD * stsd)
689 atom_stsd_remove_entries (stsd);
690 atom_full_clear (&stsd->header);
694 atom_ctts_init (AtomCTTS * ctts)
696 guint8 flags[3] = { 0, 0, 0 };
698 atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
699 atom_array_init (&ctts->entries, 128);
700 ctts->do_pts = FALSE;
706 AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
708 atom_ctts_init (ctts);
713 atom_ctts_free (AtomCTTS * ctts)
715 atom_full_clear (&ctts->header);
716 atom_array_clear (&ctts->entries);
721 atom_stts_init (AtomSTTS * stts)
723 guint8 flags[3] = { 0, 0, 0 };
725 atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
726 atom_array_init (&stts->entries, 512);
730 atom_stts_clear (AtomSTTS * stts)
732 atom_full_clear (&stts->header);
733 atom_array_clear (&stts->entries);
737 atom_stsz_init (AtomSTSZ * stsz)
739 guint8 flags[3] = { 0, 0, 0 };
741 atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
742 atom_array_init (&stsz->entries, 1024);
743 stsz->sample_size = 0;
744 stsz->table_size = 0;
748 atom_stsz_clear (AtomSTSZ * stsz)
750 atom_full_clear (&stsz->header);
751 atom_array_clear (&stsz->entries);
752 stsz->table_size = 0;
756 atom_stsc_init (AtomSTSC * stsc)
758 guint8 flags[3] = { 0, 0, 0 };
760 atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
761 atom_array_init (&stsc->entries, 128);
765 atom_stsc_clear (AtomSTSC * stsc)
767 atom_full_clear (&stsc->header);
768 atom_array_clear (&stsc->entries);
772 atom_co64_init (AtomSTCO64 * co64)
774 guint8 flags[3] = { 0, 0, 0 };
776 atom_full_init (&co64->header, FOURCC_stco, 0, 0, 0, flags);
777 atom_array_init (&co64->entries, 256);
781 atom_stco64_clear (AtomSTCO64 * stco64)
783 atom_full_clear (&stco64->header);
784 atom_array_clear (&stco64->entries);
788 atom_stss_init (AtomSTSS * stss)
790 guint8 flags[3] = { 0, 0, 0 };
792 atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
793 atom_array_init (&stss->entries, 128);
797 atom_stss_clear (AtomSTSS * stss)
799 atom_full_clear (&stss->header);
800 atom_array_clear (&stss->entries);
804 atom_stbl_init (AtomSTBL * stbl)
806 atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
808 atom_stts_init (&stbl->stts);
809 atom_stss_init (&stbl->stss);
810 atom_stsd_init (&stbl->stsd);
811 atom_stsz_init (&stbl->stsz);
812 atom_stsc_init (&stbl->stsc);
815 atom_co64_init (&stbl->stco64);
819 atom_stbl_clear (AtomSTBL * stbl)
821 atom_clear (&stbl->header);
822 atom_stsd_clear (&stbl->stsd);
823 atom_stts_clear (&stbl->stts);
824 atom_stss_clear (&stbl->stss);
825 atom_stsc_clear (&stbl->stsc);
826 atom_stsz_clear (&stbl->stsz);
828 atom_ctts_free (stbl->ctts);
830 atom_stco64_clear (&stbl->stco64);
834 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
836 guint8 flags[3] = { 0, 0, 1 };
838 atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
839 vmhd->graphics_mode = 0x0;
840 memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
842 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
843 vmhd->graphics_mode = 0x40;
844 vmhd->opcolor[0] = 32768;
845 vmhd->opcolor[1] = 32768;
846 vmhd->opcolor[2] = 32768;
851 atom_vmhd_new (AtomsContext * context)
853 AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
855 atom_vmhd_init (vmhd, context);
860 atom_vmhd_free (AtomVMHD * vmhd)
862 atom_full_clear (&vmhd->header);
867 atom_smhd_init (AtomSMHD * smhd)
869 guint8 flags[3] = { 0, 0, 0 };
871 atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
879 AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
881 atom_smhd_init (smhd);
886 atom_smhd_free (AtomSMHD * smhd)
888 atom_full_clear (&smhd->header);
893 atom_hmhd_free (AtomHMHD * hmhd)
895 atom_full_clear (&hmhd->header);
900 atom_hdlr_init (AtomHDLR * hdlr, AtomsContext * context)
902 guint8 flags[3] = { 0, 0, 0 };
904 atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
906 hdlr->component_type = 0;
907 hdlr->handler_type = 0;
908 hdlr->manufacturer = 0;
910 hdlr->flags_mask = 0;
911 hdlr->name = g_strdup ("");
913 /* Store the flavor to know how to serialize the 'name' string */
914 hdlr->flavor = context->flavor;
918 atom_hdlr_new (AtomsContext * context)
920 AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
922 atom_hdlr_init (hdlr, context);
927 atom_hdlr_clear (AtomHDLR * hdlr)
929 atom_full_clear (&hdlr->header);
937 atom_hdlr_free (AtomHDLR * hdlr)
939 atom_hdlr_clear (hdlr);
944 atom_url_init (AtomURL * url)
946 guint8 flags[3] = { 0, 0, 1 };
948 atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
949 url->location = NULL;
953 atom_url_free (AtomURL * url)
955 atom_full_clear (&url->header);
957 g_free (url->location);
958 url->location = NULL;
966 AtomURL *url = g_new0 (AtomURL, 1);
975 guint8 flags[3] = { 0, 0, 1 };
976 AtomFull *alis = g_new0 (AtomFull, 1);
978 atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
983 atom_dref_init (AtomDREF * dref, AtomsContext * context)
985 guint8 flags[3] = { 0, 0, 0 };
987 atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
989 /* in either case, alis or url init arranges to set self-contained flag */
990 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
991 /* alis dref for qt */
992 AtomFull *alis = atom_alis_new ();
993 dref->entries = g_list_append (dref->entries, alis);
995 /* url for iso spec, as 'alis' not specified there */
996 AtomURL *url = atom_url_new ();
997 dref->entries = g_list_append (dref->entries, url);
1002 atom_dref_clear (AtomDREF * dref)
1006 atom_full_clear (&dref->header);
1007 walker = dref->entries;
1009 GList *aux = walker;
1010 Atom *atom = (Atom *) aux->data;
1012 walker = g_list_next (walker);
1013 dref->entries = g_list_remove_link (dref->entries, aux);
1014 switch (atom->type) {
1016 atom_full_free ((AtomFull *) atom);
1019 atom_url_free ((AtomURL *) atom);
1022 /* we do nothing, better leak than crash */
1030 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
1032 atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
1033 atom_dref_init (&dinf->dref, context);
1037 atom_dinf_clear (AtomDINF * dinf)
1039 atom_clear (&dinf->header);
1040 atom_dref_clear (&dinf->dref);
1044 atom_minf_init (AtomMINF * minf, AtomsContext * context)
1046 atom_header_set (&minf->header, FOURCC_minf, 0, 0);
1053 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
1054 minf->hdlr = atom_hdlr_new (context);
1055 minf->hdlr->component_type = FOURCC_dhlr;
1056 minf->hdlr->handler_type = FOURCC_alis;
1060 atom_dinf_init (&minf->dinf, context);
1061 atom_stbl_init (&minf->stbl);
1065 atom_minf_clear_handlers (AtomMINF * minf)
1068 atom_vmhd_free (minf->vmhd);
1072 atom_smhd_free (minf->smhd);
1076 atom_hmhd_free (minf->hmhd);
1080 atom_gmhd_free (minf->gmhd);
1086 atom_minf_clear (AtomMINF * minf)
1088 atom_clear (&minf->header);
1089 atom_minf_clear_handlers (minf);
1091 atom_hdlr_free (minf->hdlr);
1093 atom_dinf_clear (&minf->dinf);
1094 atom_stbl_clear (&minf->stbl);
1098 atom_mdhd_init (AtomMDHD * mdhd)
1100 guint8 flags[3] = { 0, 0, 0 };
1102 atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
1103 common_time_info_init (&mdhd->time_info);
1104 mdhd->language_code = 0;
1109 atom_mdhd_clear (AtomMDHD * mdhd)
1111 atom_full_clear (&mdhd->header);
1115 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
1117 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
1119 atom_mdhd_init (&mdia->mdhd);
1120 atom_hdlr_init (&mdia->hdlr, context);
1121 atom_minf_init (&mdia->minf, context);
1125 atom_mdia_clear (AtomMDIA * mdia)
1127 atom_clear (&mdia->header);
1128 atom_mdhd_clear (&mdia->mdhd);
1129 atom_hdlr_clear (&mdia->hdlr);
1130 atom_minf_clear (&mdia->minf);
1134 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
1138 * 1 -> track enabled
1139 * 2 -> track in movie
1140 * 4 -> track in preview
1142 guint8 flags[3] = { 0, 0, 7 };
1144 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
1146 tkhd->creation_time = tkhd->modification_time = get_current_qt_time ();
1151 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
1153 tkhd->alternate_group = 0;
1155 tkhd->reserved3 = 0;
1156 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
1157 tkhd->matrix[0] = 1 << 16;
1158 tkhd->matrix[4] = 1 << 16;
1159 tkhd->matrix[8] = 16384 << 16;
1165 atom_tkhd_clear (AtomTKHD * tkhd)
1167 atom_full_clear (&tkhd->header);
1171 atom_ilst_init (AtomILST * ilst)
1173 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
1174 ilst->entries = NULL;
1178 atom_ilst_new (void)
1180 AtomILST *ilst = g_new0 (AtomILST, 1);
1182 atom_ilst_init (ilst);
1187 atom_ilst_free (AtomILST * ilst)
1190 atom_info_list_free (ilst->entries);
1191 atom_clear (&ilst->header);
1196 atom_meta_init (AtomMETA * meta, AtomsContext * context)
1198 guint8 flags[3] = { 0, 0, 0 };
1200 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1201 atom_hdlr_init (&meta->hdlr, context);
1202 /* FIXME (ISOM says this is always 0) */
1203 meta->hdlr.component_type = FOURCC_mhlr;
1204 meta->hdlr.handler_type = FOURCC_mdir;
1209 atom_meta_new (AtomsContext * context)
1211 AtomMETA *meta = g_new0 (AtomMETA, 1);
1213 atom_meta_init (meta, context);
1218 atom_meta_free (AtomMETA * meta)
1220 atom_full_clear (&meta->header);
1221 atom_hdlr_clear (&meta->hdlr);
1223 atom_ilst_free (meta->ilst);
1229 atom_udta_init_metatags (AtomUDTA * udta, AtomsContext * context)
1231 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
1233 udta->meta = atom_meta_new (context);
1235 if (!udta->meta->ilst) {
1236 udta->meta->ilst = atom_ilst_new ();
1242 atom_udta_init (AtomUDTA * udta, AtomsContext * context)
1244 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1246 udta->context = context;
1248 atom_udta_init_metatags (udta, context);
1252 atom_udta_clear (AtomUDTA * udta)
1254 atom_clear (&udta->header);
1256 atom_meta_free (udta->meta);
1259 atom_info_list_free (udta->entries);
1263 atom_tref_init (AtomTREF * tref, guint32 reftype)
1265 atom_header_set (&tref->header, FOURCC_tref, 0, 0);
1266 tref->reftype = reftype;
1267 atom_array_init (&tref->entries, 128);
1271 atom_tref_clear (AtomTREF * tref)
1273 atom_clear (&tref->header);
1275 atom_array_clear (&tref->entries);
1279 atom_tref_new (guint32 reftype)
1283 tref = g_new0 (AtomTREF, 1);
1284 atom_tref_init (tref, reftype);
1290 atom_tref_free (AtomTREF * tref)
1292 atom_tref_clear (tref);
1296 /* Clear added tags, but keep the context/flavor the same */
1298 atom_udta_clear_tags (AtomUDTA * udta)
1300 if (udta->entries) {
1301 atom_info_list_free (udta->entries);
1302 udta->entries = NULL;
1304 if (udta->meta && udta->meta->ilst->entries) {
1305 atom_info_list_free (udta->meta->ilst->entries);
1306 udta->meta->ilst->entries = NULL;
1311 atom_tag_data_init (AtomTagData * data)
1313 guint8 flags[] = { 0, 0, 0 };
1315 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1319 atom_tag_data_clear (AtomTagData * data)
1321 atom_full_clear (&data->header);
1322 g_free (data->data);
1327 * Fourcc is the tag fourcc
1328 * flags will be truncated to 24bits
1331 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1333 AtomTag *tag = g_new0 (AtomTag, 1);
1335 tag->header.type = fourcc;
1336 atom_tag_data_init (&tag->data);
1337 atom_full_set_flags_as_uint (&tag->data.header, flags_as_uint);
1342 atom_tag_free (AtomTag * tag)
1344 atom_clear (&tag->header);
1345 atom_tag_data_clear (&tag->data);
1350 atom_mvhd_init (AtomMVHD * mvhd)
1352 guint8 flags[3] = { 0, 0, 0 };
1354 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1356 common_time_info_init (&mvhd->time_info);
1358 mvhd->prefered_rate = 1 << 16;
1359 mvhd->volume = 1 << 8;
1360 mvhd->reserved3 = 0;
1361 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1363 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1364 mvhd->matrix[0] = 1 << 16;
1365 mvhd->matrix[4] = 1 << 16;
1366 mvhd->matrix[8] = 16384 << 16;
1368 mvhd->preview_time = 0;
1369 mvhd->preview_duration = 0;
1370 mvhd->poster_time = 0;
1371 mvhd->selection_time = 0;
1372 mvhd->selection_duration = 0;
1373 mvhd->current_time = 0;
1375 mvhd->next_track_id = 1;
1379 atom_mvhd_clear (AtomMVHD * mvhd)
1381 atom_full_clear (&mvhd->header);
1385 atom_mehd_init (AtomMEHD * mehd)
1387 guint8 flags[3] = { 0, 0, 0 };
1389 atom_full_init (&mehd->header, FOURCC_mehd, 0, 0, 1, flags);
1390 mehd->fragment_duration = 0;
1394 atom_mvex_init (AtomMVEX * mvex)
1396 atom_header_set (&mvex->header, FOURCC_mvex, 0, 0);
1397 atom_mehd_init (&mvex->mehd);
1402 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
1404 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
1406 atom_tkhd_init (&trak->tkhd, context);
1407 trak->context = context;
1408 atom_udta_init (&trak->udta, context);
1410 atom_mdia_init (&trak->mdia, context);
1415 atom_trak_new (AtomsContext * context)
1417 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
1419 atom_trak_init (trak, context);
1424 atom_trak_clear (AtomTRAK * trak)
1426 atom_clear (&trak->header);
1427 atom_tkhd_clear (&trak->tkhd);
1429 atom_edts_free (trak->edts);
1430 atom_udta_clear (&trak->udta);
1431 atom_mdia_clear (&trak->mdia);
1433 atom_tref_free (trak->tref);
1437 atom_trak_free (AtomTRAK * trak)
1439 atom_trak_clear (trak);
1445 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1447 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1448 atom_mvhd_init (&(moov->mvhd));
1449 atom_mvex_init (&(moov->mvex));
1450 atom_udta_init (&moov->udta, context);
1452 moov->context = *context;
1456 atom_moov_new (AtomsContext * context)
1458 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1460 atom_moov_init (moov, context);
1465 atom_trex_free (AtomTREX * trex)
1467 atom_full_clear (&trex->header);
1472 atom_mvex_clear (AtomMVEX * mvex)
1476 atom_clear (&mvex->header);
1477 walker = mvex->trexs;
1479 atom_trex_free ((AtomTREX *) walker->data);
1480 walker = g_list_next (walker);
1482 g_list_free (mvex->trexs);
1487 atom_moov_free (AtomMOOV * moov)
1491 atom_clear (&moov->header);
1492 atom_mvhd_clear (&moov->mvhd);
1494 walker = moov->traks;
1496 atom_trak_free ((AtomTRAK *) walker->data);
1497 walker = g_list_next (walker);
1499 g_list_free (moov->traks);
1502 atom_udta_clear (&moov->udta);
1503 atom_mvex_clear (&moov->mvex);
1508 /* -- end of init / free -- */
1510 /* -- copy data functions -- */
1513 atom_full_get_version (AtomFull * full)
1515 return full->version;
1519 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1520 guint8 ** buffer, guint64 * size, guint64 * offset)
1522 guint64 original_offset = *offset;
1525 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1526 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1527 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1528 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1530 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1531 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1532 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1533 prop_copy_uint64 (ti->duration, buffer, size, offset);
1535 return *offset - original_offset;
1539 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1542 /* this only works for non-extended atom size, which is OK
1543 * (though it could be made to do mem_move, etc and write extended size) */
1544 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1548 atom_copy_empty (Atom * atom, guint8 ** buffer, guint64 * size,
1551 guint64 original_offset = *offset;
1553 prop_copy_uint32 (0, buffer, size, offset);
1555 return *offset - original_offset;
1559 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1561 guint64 original_offset = *offset;
1563 /* copies type and size */
1564 prop_copy_uint32 (atom->size, buffer, size, offset);
1565 prop_copy_fourcc (atom->type, buffer, size, offset);
1567 /* extended size needed */
1568 if (atom->size == 1) {
1569 /* really should not happen other than with mdat atom;
1570 * would be a problem for size (re)write code, not to mention memory */
1571 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1572 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1575 return *offset - original_offset;
1579 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1582 guint64 original_offset = *offset;
1584 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1588 prop_copy_uint8 (atom->version, buffer, size, offset);
1589 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1591 atom_write_size (buffer, size, offset, original_offset);
1592 return *offset - original_offset;
1596 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1599 guint64 original_offset = *offset;
1602 AtomInfo *info = (AtomInfo *) ai->data;
1604 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1607 ai = g_list_next (ai);
1610 return *offset - original_offset;
1614 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1617 guint64 original_offset = *offset;
1619 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1623 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1625 atom_write_size (buffer, size, offset, original_offset);
1626 return *offset - original_offset;
1630 atom_uuid_copy_data (AtomUUID * uuid, guint8 ** buffer, guint64 * size,
1633 guint64 original_offset = *offset;
1635 if (!atom_copy_data (&uuid->header, buffer, size, offset)) {
1638 prop_copy_uint8_array (uuid->uuid, 16, buffer, size, offset);
1640 prop_copy_uint8_array (uuid->data, uuid->datalen, buffer, size, offset);
1642 atom_write_size (buffer, size, offset, original_offset);
1643 return *offset - original_offset;
1647 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1650 guint64 original_offset = *offset;
1652 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1655 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1656 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1658 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1659 buffer, size, offset);
1661 atom_write_size (buffer, size, offset, original_offset);
1662 return *offset - original_offset;
1666 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1670 guint64 original_offset = *offset;
1672 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1676 version = atom_full_get_version (&(atom->header));
1678 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1679 } else if (version == 1) {
1680 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1682 *offset = original_offset;
1686 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1687 prop_copy_uint16 (atom->volume, buffer, size, offset);
1688 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1689 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1690 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1691 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1692 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1693 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1694 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1695 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1696 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1698 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1700 atom_write_size (buffer, size, offset, original_offset);
1701 return *offset - original_offset;
1705 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1708 guint64 original_offset = *offset;
1710 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1714 if (atom_full_get_version (&tkhd->header) == 0) {
1715 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1716 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1717 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1718 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1719 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1721 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1722 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1723 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1724 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1725 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1728 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1729 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1730 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1731 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1732 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1733 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1735 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1736 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1738 atom_write_size (buffer, size, offset, original_offset);
1739 return *offset - original_offset;
1743 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1746 guint64 original_offset = *offset;
1748 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1752 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1753 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1754 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1755 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1756 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1758 if (hdlr->flavor == ATOMS_TREE_FLAVOR_MOV) {
1759 prop_copy_size_string ((guint8 *) hdlr->name, strlen (hdlr->name), buffer,
1762 /* assume isomedia base is more generic and use null terminated */
1763 prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
1766 atom_write_size (buffer, size, offset, original_offset);
1767 return *offset - original_offset;
1771 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1774 guint64 original_offset = *offset;
1776 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1779 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1780 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1782 atom_write_size (buffer, size, offset, original_offset);
1783 return original_offset - *offset;
1787 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1790 guint64 original_offset = *offset;
1792 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1795 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1796 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1798 atom_write_size (buffer, size, offset, original_offset);
1799 return original_offset - *offset;
1803 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1806 guint64 original_offset = *offset;
1808 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1811 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1812 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1813 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1814 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1815 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1817 atom_write_size (buffer, size, offset, original_offset);
1818 return original_offset - *offset;
1822 atom_tcmi_copy_data (AtomTCMI * tcmi, guint8 ** buffer, guint64 * size,
1825 guint64 original_offset = *offset;
1827 if (!atom_full_copy_data (&tcmi->header, buffer, size, offset)) {
1830 prop_copy_uint16 (tcmi->text_font, buffer, size, offset);
1831 prop_copy_uint16 (tcmi->text_face, buffer, size, offset);
1832 prop_copy_uint16 (tcmi->text_size, buffer, size, offset);
1833 prop_copy_uint16 (tcmi->text_color[0], buffer, size, offset);
1834 prop_copy_uint16 (tcmi->text_color[1], buffer, size, offset);
1835 prop_copy_uint16 (tcmi->text_color[2], buffer, size, offset);
1836 prop_copy_uint16 (tcmi->bg_color[0], buffer, size, offset);
1837 prop_copy_uint16 (tcmi->bg_color[1], buffer, size, offset);
1838 prop_copy_uint16 (tcmi->bg_color[2], buffer, size, offset);
1840 prop_copy_uint16 (0, buffer, size, offset);
1841 prop_copy_size_string ((guint8 *) tcmi->font_name, strlen (tcmi->font_name),
1842 buffer, size, offset);
1844 atom_write_size (buffer, size, offset, original_offset);
1845 return original_offset - *offset;
1849 atom_tmcd_copy_data (AtomTMCD * tmcd, guint8 ** buffer, guint64 * size,
1852 guint64 original_offset = *offset;
1854 if (!atom_copy_data (&tmcd->header, buffer, size, offset)) {
1857 if (!atom_tcmi_copy_data (&tmcd->tcmi, buffer, size, offset)) {
1861 atom_write_size (buffer, size, offset, original_offset);
1862 return original_offset - *offset;
1866 atom_gmin_copy_data (AtomGMIN * gmin, guint8 ** buffer, guint64 * size,
1869 guint64 original_offset = *offset;
1871 if (!atom_full_copy_data (&gmin->header, buffer, size, offset)) {
1874 prop_copy_uint16 (gmin->graphics_mode, buffer, size, offset);
1875 prop_copy_uint16 (gmin->opcolor[0], buffer, size, offset);
1876 prop_copy_uint16 (gmin->opcolor[1], buffer, size, offset);
1877 prop_copy_uint16 (gmin->opcolor[2], buffer, size, offset);
1878 prop_copy_uint8 (gmin->balance, buffer, size, offset);
1880 prop_copy_uint8 (0, buffer, size, offset);
1882 atom_write_size (buffer, size, offset, original_offset);
1883 return original_offset - *offset;
1887 atom_gmhd_copy_data (AtomGMHD * gmhd, guint8 ** buffer, guint64 * size,
1890 guint64 original_offset = *offset;
1892 if (!atom_copy_data (&gmhd->header, buffer, size, offset)) {
1895 if (!atom_gmin_copy_data (&gmhd->gmin, buffer, size, offset)) {
1898 if (!atom_tmcd_copy_data (&gmhd->tmcd, buffer, size, offset)) {
1902 atom_write_size (buffer, size, offset, original_offset);
1903 return original_offset - *offset;
1907 atom_url_same_file_flag (AtomURL * url)
1909 return (url->header.flags[2] & 0x1) == 1;
1913 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1916 guint64 original_offset = *offset;
1918 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1922 if (!atom_url_same_file_flag (url)) {
1923 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1926 atom_write_size (buffer, size, offset, original_offset);
1927 return original_offset - *offset;
1931 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1934 guint64 original_offset = *offset;
1937 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1941 prop_copy_uint32 (atom_array_get_len (&stts->entries), buffer, size, offset);
1942 /* minimize realloc */
1943 prop_copy_ensure_buffer (buffer, size, offset,
1944 8 * atom_array_get_len (&stts->entries));
1945 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
1946 STTSEntry *entry = &atom_array_index (&stts->entries, i);
1948 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
1949 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
1952 atom_write_size (buffer, size, offset, original_offset);
1953 return *offset - original_offset;
1957 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
1958 guint64 * size, guint64 * offset)
1960 guint64 original_offset = *offset;
1962 if (!atom_copy_data (&se->header, buffer, size, offset)) {
1966 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
1967 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
1969 return *offset - original_offset;
1973 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
1976 guint64 original_offset = *offset;
1978 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
1981 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
1985 atom_write_size (buffer, size, offset, original_offset);
1986 return *offset - original_offset;
1990 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
1991 guint64 * size, guint64 * offset)
1993 guint64 original_offset = *offset;
1995 if (!atom_copy_data (&(frma->header), buffer, size, offset))
1998 prop_copy_fourcc (frma->media_type, buffer, size, offset);
2000 atom_write_size (buffer, size, offset, original_offset);
2001 return *offset - original_offset;
2005 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
2006 guint64 * size, guint64 * offset)
2008 guint64 original_offset = *offset;
2010 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
2014 prop_copy_uint32 (hse->size, buffer, size, offset);
2015 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
2017 atom_write_size (buffer, size, offset, original_offset);
2018 return *offset - original_offset;
2022 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
2023 guint64 * size, guint64 * offset)
2025 guint64 original_offset = *offset;
2027 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
2031 prop_copy_uint16 (mp4a->version, buffer, size, offset);
2032 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
2033 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
2034 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
2035 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
2036 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
2037 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
2038 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
2040 /* this should always be 0 for mp4 flavor */
2041 if (mp4a->version == 1) {
2042 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
2043 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
2044 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
2045 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
2048 if (mp4a->extension_atoms) {
2049 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
2053 atom_write_size (buffer, size, offset, original_offset);
2054 return *offset - original_offset;
2058 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
2059 guint64 * size, guint64 * offset)
2061 guint64 original_offset = *offset;
2063 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
2067 prop_copy_uint16 (mp4v->version, buffer, size, offset);
2068 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
2069 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
2070 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
2071 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
2073 prop_copy_uint16 (mp4v->width, buffer, size, offset);
2074 prop_copy_uint16 (mp4v->height, buffer, size, offset);
2076 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
2077 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
2078 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
2080 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
2082 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
2085 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
2086 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
2089 if (mp4v->extension_atoms &&
2090 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
2093 atom_write_size (buffer, size, offset, original_offset);
2094 return *offset - original_offset;
2098 sample_entry_tx3g_copy_data (SampleTableEntryTX3G * tx3g, guint8 ** buffer,
2099 guint64 * size, guint64 * offset)
2101 guint64 original_offset = *offset;
2103 if (!atom_sample_entry_copy_data (&tx3g->se, buffer, size, offset)) {
2107 prop_copy_uint32 (tx3g->display_flags, buffer, size, offset);
2110 prop_copy_uint8 (1, buffer, size, offset);
2111 prop_copy_uint8 (-1, buffer, size, offset);
2112 prop_copy_uint32 (0, buffer, size, offset);
2114 prop_copy_uint64 (tx3g->default_text_box, buffer, size, offset);
2117 prop_copy_uint32 (0, buffer, size, offset);
2119 prop_copy_uint16 (tx3g->font_id, buffer, size, offset);
2120 prop_copy_uint8 (tx3g->font_face, buffer, size, offset);
2121 prop_copy_uint8 (tx3g->font_size, buffer, size, offset);
2122 prop_copy_uint32 (tx3g->foreground_color_rgba, buffer, size, offset);
2124 /* it must have a fonttable atom */
2128 atom_header_set (&atom, FOURCC_ftab, 18, 0);
2129 if (!atom_copy_data (&atom, buffer, size, offset))
2131 prop_copy_uint16 (1, buffer, size, offset); /* Count must be 1 */
2132 prop_copy_uint16 (1, buffer, size, offset); /* Font id: 1 */
2133 prop_copy_size_string ((guint8 *) "Serif", 5, buffer, size, offset);
2136 atom_write_size (buffer, size, offset, original_offset);
2137 return *offset - original_offset;
2141 sample_entry_tmcd_copy_data (SampleTableEntryTMCD * tmcd, guint8 ** buffer,
2142 guint64 * size, guint64 * offset)
2144 guint64 original_offset = *offset;
2146 if (!atom_sample_entry_copy_data (&tmcd->se, buffer, size, offset)) {
2151 prop_copy_uint32 (0, buffer, size, offset);
2153 prop_copy_uint32 (tmcd->tc_flags, buffer, size, offset);
2154 prop_copy_uint32 (tmcd->timescale, buffer, size, offset);
2155 prop_copy_uint32 (tmcd->frame_duration, buffer, size, offset);
2156 prop_copy_uint8 (tmcd->n_frames, buffer, size, offset);
2159 prop_copy_uint8 (0, buffer, size, offset);
2162 guint64 name_offset = *offset;
2164 atom_header_set (&atom, FOURCC_name, 0, 0);
2165 if (!atom_copy_data (&atom, buffer, size, offset))
2167 prop_copy_uint16 (strlen (tmcd->name.name), buffer, size, offset);
2168 prop_copy_uint16 (tmcd->name.language_code, buffer, size, offset);
2169 prop_copy_fixed_size_string ((guint8 *) tmcd->name.name,
2170 strlen (tmcd->name.name), buffer, size, offset);
2172 atom_write_size (buffer, size, offset, name_offset);
2175 atom_write_size (buffer, size, offset, original_offset);
2176 return *offset - original_offset;
2180 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
2183 guint64 original_offset = *offset;
2186 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
2190 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
2191 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
2192 if (stsz->sample_size == 0) {
2193 /* minimize realloc */
2194 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
2195 /* entry count must match sample count */
2196 g_assert (atom_array_get_len (&stsz->entries) == stsz->table_size);
2197 for (i = 0; i < atom_array_get_len (&stsz->entries); i++) {
2198 prop_copy_uint32 (atom_array_index (&stsz->entries, i), buffer, size,
2203 atom_write_size (buffer, size, offset, original_offset);
2204 return *offset - original_offset;
2208 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
2211 guint64 original_offset = *offset;
2214 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
2218 /* Last two entries might be the same size here as we only merge once the
2219 * next chunk is started */
2220 if ((len = atom_array_get_len (&stsc->entries)) > 1 &&
2221 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
2222 (atom_array_index (&stsc->entries, len - 2)).samples_per_chunk)) {
2223 stsc->entries.len--;
2226 prop_copy_uint32 (atom_array_get_len (&stsc->entries), buffer, size, offset);
2227 /* minimize realloc */
2228 prop_copy_ensure_buffer (buffer, size, offset,
2229 12 * atom_array_get_len (&stsc->entries));
2231 for (i = 0; i < atom_array_get_len (&stsc->entries); i++) {
2232 STSCEntry *entry = &atom_array_index (&stsc->entries, i);
2234 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
2235 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
2236 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
2239 atom_write_size (buffer, size, offset, original_offset);
2240 return *offset - original_offset;
2244 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
2247 guint64 original_offset = *offset;
2250 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
2254 prop_copy_uint32 (atom_array_get_len (&ctts->entries), buffer, size, offset);
2255 /* minimize realloc */
2256 prop_copy_ensure_buffer (buffer, size, offset,
2257 8 * atom_array_get_len (&ctts->entries));
2258 for (i = 0; i < atom_array_get_len (&ctts->entries); i++) {
2259 CTTSEntry *entry = &atom_array_index (&ctts->entries, i);
2261 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
2262 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
2265 atom_write_size (buffer, size, offset, original_offset);
2266 return *offset - original_offset;
2270 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
2273 guint64 original_offset = *offset;
2275 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
2277 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
2281 prop_copy_uint32 (atom_array_get_len (&stco64->entries), buffer, size,
2284 /* minimize realloc */
2285 prop_copy_ensure_buffer (buffer, size, offset,
2286 8 * atom_array_get_len (&stco64->entries));
2287 for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
2289 atom_array_index (&stco64->entries, i) + stco64->chunk_offset;
2292 prop_copy_uint32 ((guint32) value, buffer, size, offset);
2294 prop_copy_uint64 (value, buffer, size, offset);
2298 atom_write_size (buffer, size, offset, original_offset);
2299 return *offset - original_offset;
2303 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
2306 guint64 original_offset = *offset;
2309 if (atom_array_get_len (&stss->entries) == 0) {
2310 /* FIXME not needing this atom might be confused with error while copying */
2314 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
2318 prop_copy_uint32 (atom_array_get_len (&stss->entries), buffer, size, offset);
2319 /* minimize realloc */
2320 prop_copy_ensure_buffer (buffer, size, offset,
2321 4 * atom_array_get_len (&stss->entries));
2322 for (i = 0; i < atom_array_get_len (&stss->entries); i++) {
2323 prop_copy_uint32 (atom_array_index (&stss->entries, i), buffer, size,
2327 atom_write_size (buffer, size, offset, original_offset);
2328 return *offset - original_offset;
2332 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
2335 guint64 original_offset = *offset;
2338 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
2342 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
2344 for (walker = g_list_last (stsd->entries); walker != NULL;
2345 walker = g_list_previous (walker)) {
2346 SampleTableEntry *se = (SampleTableEntry *) walker->data;
2348 switch (((Atom *) walker->data)->type) {
2350 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
2351 buffer, size, offset)) {
2356 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
2357 buffer, size, offset)) {
2362 if (se->kind == VIDEO) {
2363 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
2364 walker->data, buffer, size, offset)) {
2367 } else if (se->kind == AUDIO) {
2368 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
2369 walker->data, buffer, size, offset)) {
2372 } else if (se->kind == SUBTITLE) {
2373 if (!sample_entry_tx3g_copy_data ((SampleTableEntryTX3G *)
2374 walker->data, buffer, size, offset)) {
2377 } else if (se->kind == TIMECODE) {
2378 if (!sample_entry_tmcd_copy_data ((SampleTableEntryTMCD *)
2379 walker->data, buffer, size, offset)) {
2383 if (!atom_hint_sample_entry_copy_data (
2384 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
2392 atom_write_size (buffer, size, offset, original_offset);
2393 return *offset - original_offset;
2397 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
2400 guint64 original_offset = *offset;
2402 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
2406 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
2409 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
2412 /* this atom is optional, so let's check if we need it
2413 * (to avoid false error) */
2414 if (atom_array_get_len (&stbl->stss.entries)) {
2415 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
2420 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
2423 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
2426 if (stbl->ctts && stbl->ctts->do_pts) {
2427 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
2431 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
2435 atom_write_size (buffer, size, offset, original_offset);
2436 return original_offset - *offset;
2441 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
2444 guint64 original_offset = *offset;
2447 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
2451 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
2453 walker = dref->entries;
2454 while (walker != NULL) {
2455 Atom *atom = (Atom *) walker->data;
2457 if (atom->type == FOURCC_url_) {
2458 if (!atom_url_copy_data ((AtomURL *) atom, buffer, size, offset))
2460 } else if (atom->type == FOURCC_alis) {
2461 if (!atom_full_copy_data ((AtomFull *) atom, buffer, size, offset))
2464 g_error ("Unsupported atom used inside dref atom");
2466 walker = g_list_next (walker);
2469 atom_write_size (buffer, size, offset, original_offset);
2470 return *offset - original_offset;
2474 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
2477 guint64 original_offset = *offset;
2479 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
2483 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
2487 atom_write_size (buffer, size, offset, original_offset);
2488 return original_offset - *offset;
2492 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
2495 guint64 original_offset = *offset;
2497 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
2502 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
2505 } else if (minf->smhd) {
2506 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
2509 } else if (minf->hmhd) {
2510 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
2513 } else if (minf->gmhd) {
2514 if (!atom_gmhd_copy_data (minf->gmhd, buffer, size, offset)) {
2520 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2525 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2528 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2532 atom_write_size (buffer, size, offset, original_offset);
2533 return *offset - original_offset;
2537 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2540 guint64 original_offset = *offset;
2542 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2546 if (!common_time_info_copy_data (&mdhd->time_info,
2547 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2551 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2552 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2554 atom_write_size (buffer, size, offset, original_offset);
2555 return *offset - original_offset;
2559 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2562 guint64 original_offset = *offset;
2564 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2567 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2570 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2574 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2578 atom_write_size (buffer, size, offset, original_offset);
2579 return *offset - original_offset;
2583 atom_elst_copy_data (AtomELST * elst, guint8 ** buffer, guint64 * size,
2586 guint64 original_offset = *offset;
2589 if (!atom_full_copy_data (&elst->header, buffer, size, offset)) {
2593 prop_copy_uint32 (g_slist_length (elst->entries), buffer, size, offset);
2595 for (walker = elst->entries; walker != NULL; walker = g_slist_next (walker)) {
2596 EditListEntry *entry = (EditListEntry *) walker->data;
2597 prop_copy_uint32 (entry->duration, buffer, size, offset);
2598 prop_copy_uint32 (entry->media_time, buffer, size, offset);
2599 prop_copy_uint32 (entry->media_rate, buffer, size, offset);
2601 atom_write_size (buffer, size, offset, original_offset);
2602 return *offset - original_offset;
2606 atom_tref_copy_data (AtomTREF * tref, guint8 ** buffer, guint64 * size,
2609 guint64 original_offset = *offset;
2612 g_assert (atom_array_get_len (&tref->entries) > 0);
2614 if (!atom_copy_data (&tref->header, buffer, size, offset)) {
2618 prop_copy_uint32 (8 + 4 * atom_array_get_len (&tref->entries), buffer, size,
2620 prop_copy_fourcc (tref->reftype, buffer, size, offset);
2621 /* minimize realloc */
2622 prop_copy_ensure_buffer (buffer, size, offset,
2623 4 * atom_array_get_len (&tref->entries));
2624 for (i = 0; i < atom_array_get_len (&tref->entries); i++) {
2625 prop_copy_uint32 (atom_array_index (&tref->entries, i), buffer, size,
2629 atom_write_size (buffer, size, offset, original_offset);
2630 return *offset - original_offset;
2634 atom_edts_copy_data (AtomEDTS * edts, guint8 ** buffer, guint64 * size,
2637 guint64 original_offset = *offset;
2639 if (!atom_copy_data (&(edts->header), buffer, size, offset))
2642 if (!atom_elst_copy_data (&(edts->elst), buffer, size, offset))
2645 atom_write_size (buffer, size, offset, original_offset);
2646 return *offset - original_offset;
2650 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2653 guint64 original_offset = *offset;
2655 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2659 prop_copy_uint32 (data->reserved, buffer, size, offset);
2660 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2662 atom_write_size (buffer, size, offset, original_offset);
2663 return *offset - original_offset;
2667 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2670 guint64 original_offset = *offset;
2672 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2676 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2680 atom_write_size (buffer, size, offset, original_offset);
2681 return *offset - original_offset;
2685 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2688 guint64 original_offset = *offset;
2690 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2694 if (ilst->entries &&
2695 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2698 atom_write_size (buffer, size, offset, original_offset);
2699 return *offset - original_offset;
2703 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2706 guint64 original_offset = *offset;
2708 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2711 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2715 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2720 atom_write_size (buffer, size, offset, original_offset);
2721 return *offset - original_offset;
2725 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2728 guint64 original_offset = *offset;
2730 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2734 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2738 if (udta->entries) {
2740 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2744 atom_write_size (buffer, size, offset, original_offset);
2745 return *offset - original_offset;
2749 atom_mehd_copy_data (AtomMEHD * mehd, guint8 ** buffer, guint64 * size,
2752 guint64 original_offset = *offset;
2754 if (!atom_full_copy_data (&mehd->header, buffer, size, offset)) {
2758 prop_copy_uint64 (mehd->fragment_duration, buffer, size, offset);
2760 atom_write_size (buffer, size, offset, original_offset);
2761 return *offset - original_offset;
2765 atom_trex_copy_data (AtomTREX * trex, guint8 ** buffer, guint64 * size,
2768 guint64 original_offset = *offset;
2770 if (!atom_full_copy_data (&trex->header, buffer, size, offset)) {
2774 prop_copy_uint32 (trex->track_ID, buffer, size, offset);
2775 prop_copy_uint32 (trex->default_sample_description_index, buffer, size,
2777 prop_copy_uint32 (trex->default_sample_duration, buffer, size, offset);
2778 prop_copy_uint32 (trex->default_sample_size, buffer, size, offset);
2779 prop_copy_uint32 (trex->default_sample_flags, buffer, size, offset);
2781 atom_write_size (buffer, size, offset, original_offset);
2782 return *offset - original_offset;
2786 atom_mvex_copy_data (AtomMVEX * mvex, guint8 ** buffer, guint64 * size,
2789 guint64 original_offset = *offset;
2792 if (!atom_copy_data (&mvex->header, buffer, size, offset)) {
2796 if (!atom_mehd_copy_data (&mvex->mehd, buffer, size, offset)) {
2800 walker = g_list_first (mvex->trexs);
2801 while (walker != NULL) {
2802 if (!atom_trex_copy_data ((AtomTREX *) walker->data, buffer, size, offset)) {
2805 walker = g_list_next (walker);
2808 atom_write_size (buffer, size, offset, original_offset);
2809 return *offset - original_offset;
2813 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2816 guint64 original_offset = *offset;
2818 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2821 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2825 if (!trak->tapt->copy_data_func (trak->tapt->atom, buffer, size, offset)) {
2830 if (!atom_edts_copy_data (trak->edts, buffer, size, offset)) {
2835 /* Make sure we need this atom (there is a referenced track */
2836 if (atom_array_get_len (&trak->tref->entries) > 0) {
2837 if (!atom_tref_copy_data (trak->tref, buffer, size, offset)) {
2843 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2847 if (!atom_udta_copy_data (&trak->udta, buffer, size, offset)) {
2851 atom_write_size (buffer, size, offset, original_offset);
2852 return *offset - original_offset;
2857 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2860 guint64 original_offset = *offset;
2863 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2866 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2869 walker = g_list_first (atom->traks);
2870 while (walker != NULL) {
2871 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2874 walker = g_list_next (walker);
2877 if (!atom_udta_copy_data (&atom->udta, buffer, size, offset)) {
2881 if (atom->fragmented) {
2882 if (!atom_mvex_copy_data (&atom->mvex, buffer, size, offset)) {
2887 atom_write_size (buffer, size, offset, original_offset);
2888 return *offset - original_offset;
2892 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
2893 guint64 * size, guint64 * offset)
2895 guint64 original_offset = *offset;
2897 if (!atom_copy_data (&(wave->header), buffer, size, offset))
2900 if (wave->extension_atoms) {
2901 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
2905 atom_write_size (buffer, size, offset, original_offset);
2906 return *offset - original_offset;
2909 /* -- end of copy data functions -- */
2911 /* -- general functions, API and support functions */
2913 /* add samples to tables */
2916 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
2920 if ((len = atom_array_get_len (&stsc->entries)) > 1 &&
2921 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
2922 (atom_array_index (&stsc->entries, len - 2)).samples_per_chunk)) {
2925 /* Merge last two entries as they have the same number of samples per chunk */
2926 nentry = &atom_array_index (&stsc->entries, len - 1);
2927 nentry->first_chunk = first_chunk;
2928 nentry->samples_per_chunk = nsamples;
2929 nentry->sample_description_index = 1;
2933 nentry.first_chunk = first_chunk;
2934 nentry.samples_per_chunk = nsamples;
2935 nentry.sample_description_index = 1;
2936 atom_array_append (&stsc->entries, nentry, 128);
2941 atom_stsc_update_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
2945 len = atom_array_get_len (&stsc->entries);
2946 g_assert (len != 0);
2947 g_assert (atom_array_index (&stsc->entries,
2948 len - 1).first_chunk == first_chunk);
2950 atom_array_index (&stsc->entries, len - 1).samples_per_chunk += nsamples;
2954 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
2956 STTSEntry *entry = NULL;
2958 if (G_LIKELY (atom_array_get_len (&stts->entries) != 0))
2959 entry = &atom_array_index (&stts->entries,
2960 atom_array_get_len (&stts->entries) - 1);
2962 if (entry && entry->sample_delta == sample_delta) {
2963 entry->sample_count += sample_count;
2967 nentry.sample_count = sample_count;
2968 nentry.sample_delta = sample_delta;
2969 atom_array_append (&stts->entries, nentry, 256);
2974 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
2978 stsz->table_size += nsamples;
2979 if (stsz->sample_size != 0) {
2980 /* it is constant size, we don't need entries */
2983 for (i = 0; i < nsamples; i++) {
2984 atom_array_append (&stsz->entries, size, 1024);
2989 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
2991 return atom_array_get_len (&stco64->entries);
2994 /* returns TRUE if a new entry was added */
2996 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
3000 /* Only add a new entry if the chunk offset changed */
3001 if ((len = atom_array_get_len (&stco64->entries)) &&
3002 ((atom_array_index (&stco64->entries, len - 1)) == entry))
3005 atom_array_append (&stco64->entries, entry, 256);
3006 if (entry > G_MAXUINT32)
3007 stco64->header.header.type = FOURCC_co64;
3013 atom_tref_add_entry (AtomTREF * tref, guint32 sample)
3015 atom_array_append (&tref->entries, sample, 512);
3019 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
3021 atom_array_append (&stss->entries, sample, 512);
3025 atom_stbl_add_stss_entry (AtomSTBL * stbl)
3027 guint32 sample_index = stbl->stsz.table_size;
3029 atom_stss_add_entry (&stbl->stss, sample_index);
3033 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
3035 CTTSEntry *entry = NULL;
3037 if (G_LIKELY (atom_array_get_len (&ctts->entries) != 0))
3038 entry = &atom_array_index (&ctts->entries,
3039 atom_array_get_len (&ctts->entries) - 1);
3041 if (entry == NULL || entry->sampleoffset != offset) {
3044 nentry.samplecount = nsamples;
3045 nentry.sampleoffset = offset;
3046 atom_array_append (&ctts->entries, nentry, 256);
3048 ctts->do_pts = TRUE;
3050 entry->samplecount += nsamples;
3055 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
3057 if (stbl->ctts == NULL) {
3058 stbl->ctts = atom_ctts_new ();
3060 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
3064 atom_stbl_add_samples (AtomSTBL * stbl, guint32 nsamples, guint32 delta,
3065 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3067 atom_stts_add_entry (&stbl->stts, nsamples, delta);
3068 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
3069 if (atom_stco64_add_entry (&stbl->stco64, chunk_offset)) {
3070 atom_stsc_add_new_entry (&stbl->stsc,
3071 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
3073 atom_stsc_update_entry (&stbl->stsc,
3074 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
3078 atom_stbl_add_stss_entry (stbl);
3079 /* always store to arrange for consistent content */
3080 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
3084 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
3085 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3087 AtomSTBL *stbl = &trak->mdia.minf.stbl;
3088 atom_stbl_add_samples (stbl, nsamples, delta, size, chunk_offset, sync,
3092 /* trak and moov molding */
3095 atom_trak_get_timescale (AtomTRAK * trak)
3097 return trak->mdia.mdhd.time_info.timescale;
3101 atom_trak_get_id (AtomTRAK * trak)
3103 return trak->tkhd.track_ID;
3107 atom_trak_set_id (AtomTRAK * trak, guint32 id)
3109 trak->tkhd.track_ID = id;
3113 atom_moov_add_trex (AtomMOOV * moov, AtomTREX * trex)
3115 moov->mvex.trexs = g_list_append (moov->mvex.trexs, trex);
3119 atom_trex_new (AtomTRAK * trak)
3121 guint8 flags[3] = { 0, 0, 0 };
3122 AtomTREX *trex = g_new0 (AtomTREX, 1);
3124 atom_full_init (&trex->header, FOURCC_trex, 0, 0, 0, flags);
3126 trex->track_ID = trak->tkhd.track_ID;
3127 trex->default_sample_description_index = 1;
3128 trex->default_sample_duration = 0;
3129 trex->default_sample_size = 0;
3130 trex->default_sample_flags = 0;
3136 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
3138 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
3139 moov->traks = g_list_append (moov->traks, trak);
3140 /* additional trak means also new trex */
3141 atom_moov_add_trex (moov, atom_trex_new (trak));
3145 atom_moov_get_trak_count (AtomMOOV * moov)
3147 return g_list_length (moov->traks);
3151 atom_trak_get_duration (AtomTRAK * trak)
3153 return trak->tkhd.duration;
3157 atom_stts_get_total_duration (AtomSTTS * stts)
3162 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
3163 STTSEntry *entry = &atom_array_index (&stts->entries, i);
3165 sum += (guint64) (entry->sample_count) * entry->sample_delta;
3171 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
3173 trak->mdia.mdhd.time_info.duration =
3174 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
3175 if (trak->mdia.mdhd.time_info.timescale != 0) {
3176 trak->tkhd.duration =
3177 gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration,
3178 moov_timescale, trak->mdia.mdhd.time_info.timescale);
3180 trak->tkhd.duration = 0;
3185 timecode_atom_trak_set_duration (AtomTRAK * trak, guint64 duration,
3191 /* Sanity checks to ensure we have a timecode */
3192 g_assert (trak->mdia.minf.gmhd != NULL);
3193 g_assert (atom_array_get_len (&trak->mdia.minf.stbl.stts.entries) == 1);
3195 for (iter = trak->mdia.minf.stbl.stsd.entries; iter;
3196 iter = g_list_next (iter)) {
3197 SampleTableEntry *entry = iter->data;
3198 if (entry->kind == TIMECODE) {
3199 SampleTableEntryTMCD *tmcd = (SampleTableEntryTMCD *) entry;
3201 duration = duration * tmcd->timescale / timescale;
3202 timescale = tmcd->timescale;
3207 trak->tkhd.duration = duration;
3208 trak->mdia.mdhd.time_info.duration = duration;
3209 trak->mdia.mdhd.time_info.timescale = timescale;
3211 entry = &atom_array_index (&trak->mdia.minf.stbl.stts.entries, 0);
3212 entry->sample_delta = duration;
3216 atom_moov_get_timescale (AtomMOOV * moov)
3218 return moov->mvhd.time_info.timescale;
3222 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
3224 moov->mvhd.time_info.timescale = timescale;
3228 atom_moov_update_duration (AtomMOOV * moov)
3230 GList *traks = moov->traks;
3231 guint64 dur, duration = 0;
3234 AtomTRAK *trak = (AtomTRAK *) traks->data;
3236 /* Skip timecodes for now: they have a placeholder duration */
3237 if (trak->mdia.minf.gmhd == NULL) {
3238 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
3239 dur = atom_trak_get_duration (trak);
3243 traks = g_list_next (traks);
3245 /* Now update the duration of the timecodes */
3246 traks = moov->traks;
3248 AtomTRAK *trak = (AtomTRAK *) traks->data;
3250 if (trak->mdia.minf.gmhd != NULL)
3251 timecode_atom_trak_set_duration (trak, duration,
3252 atom_moov_get_timescale (moov));
3253 traks = g_list_next (traks);
3255 moov->mvhd.time_info.duration = duration;
3256 moov->mvex.mehd.fragment_duration = duration;
3260 atom_moov_set_fragmented (AtomMOOV * moov, gboolean fragmented)
3262 moov->fragmented = fragmented;
3266 atom_stco64_chunks_set_offset (AtomSTCO64 * stco64, guint32 offset)
3268 stco64->chunk_offset = offset;
3272 atom_moov_chunks_set_offset (AtomMOOV * moov, guint32 offset)
3274 GList *traks = moov->traks;
3276 if (offset == moov->chunks_offset)
3277 return; /* Nothing to do */
3280 AtomTRAK *trak = (AtomTRAK *) traks->data;
3282 atom_stco64_chunks_set_offset (&trak->mdia.minf.stbl.stco64, offset);
3283 traks = g_list_next (traks);
3286 moov->chunks_offset = offset;
3290 atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
3291 guint32 max_bitrate)
3293 AtomESDS *esds = NULL;
3294 AtomData *btrt = NULL;
3295 AtomWAVE *wave = NULL;
3298 GList *extensioniter = NULL;
3300 g_return_if_fail (trak != NULL);
3302 if (avg_bitrate == 0 && max_bitrate == 0)
3305 stsd = &trak->mdia.minf.stbl.stsd;
3306 for (iter = stsd->entries; iter; iter = g_list_next (iter)) {
3307 SampleTableEntry *entry = iter->data;
3309 switch (entry->kind) {
3311 SampleTableEntryMP4A *audioentry = (SampleTableEntryMP4A *) entry;
3312 extensioniter = audioentry->extension_atoms;
3316 SampleTableEntryMP4V *videoentry = (SampleTableEntryMP4V *) entry;
3317 extensioniter = videoentry->extension_atoms;
3325 for (; extensioniter; extensioniter = g_list_next (extensioniter)) {
3326 AtomInfo *atominfo = extensioniter->data;
3327 if (atominfo->atom->type == FOURCC_esds) {
3328 esds = (AtomESDS *) atominfo->atom;
3329 } else if (atominfo->atom->type == FOURCC_btrt) {
3330 btrt = (AtomData *) atominfo->atom;
3331 } else if (atominfo->atom->type == FOURCC_wave) {
3332 wave = (AtomWAVE *) atominfo->atom;
3336 /* wave might have an esds internally */
3338 for (extensioniter = wave->extension_atoms; extensioniter;
3339 extensioniter = g_list_next (extensioniter)) {
3340 AtomInfo *atominfo = extensioniter->data;
3341 if (atominfo->atom->type == FOURCC_esds) {
3342 esds = (AtomESDS *) atominfo->atom;
3349 if (avg_bitrate && esds->es.dec_conf_desc.avg_bitrate == 0)
3350 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
3351 if (max_bitrate && esds->es.dec_conf_desc.max_bitrate == 0)
3352 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
3355 /* type(4bytes) + size(4bytes) + buffersize(4bytes) +
3356 * maxbitrate(bytes) + avgbitrate(bytes) */
3357 if (max_bitrate && GST_READ_UINT32_BE (btrt->data + 4) == 0)
3358 GST_WRITE_UINT32_BE (btrt->data + 4, max_bitrate);
3359 if (avg_bitrate && GST_READ_UINT32_BE (btrt->data + 8) == 0)
3360 GST_WRITE_UINT32_BE (btrt->data + 8, avg_bitrate);
3365 atom_trak_tx3g_update_dimension (AtomTRAK * trak, guint32 width, guint32 height)
3369 SampleTableEntryTX3G *tx3g = NULL;
3371 stsd = &trak->mdia.minf.stbl.stsd;
3372 for (iter = stsd->entries; iter && tx3g == NULL; iter = g_list_next (iter)) {
3373 SampleTableEntry *entry = iter->data;
3375 switch (entry->kind) {
3377 tx3g = (SampleTableEntryTX3G *) entry;
3385 /* Currently we never set the vertical placement flag, so we don't
3386 * check for it to set the dimensions differently as the spec says.
3387 * Always do it for the not set case */
3389 tx3g->font_size = 0.05 * height;
3391 height = 0.15 * height;
3392 trak->tkhd.width = width << 16;
3393 trak->tkhd.height = height << 16;
3394 tx3g->default_text_box = width | (height << 16);
3399 * Meta tags functions
3402 atom_tag_data_alloc_data (AtomTagData * data, guint size)
3404 g_free (data->data);
3405 data->data = g_new0 (guint8, size);
3406 data->datalen = size;
3410 atom_udta_append_tag (AtomUDTA * udta, AtomInfo * tag)
3415 entries = &udta->meta->ilst->entries;
3417 entries = &udta->entries;
3418 *entries = g_list_append (*entries, tag);
3422 atom_udta_add_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3423 const guint8 * data, guint size)
3428 tag = atom_tag_new (fourcc, flags);
3430 atom_tag_data_alloc_data (tdata, size);
3431 memmove (tdata->data, data, size);
3433 atom_udta_append_tag (udta,
3434 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
3439 atom_udta_add_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3441 gint len = strlen (value);
3444 atom_udta_add_tag (udta, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
3448 atom_udta_add_uint_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3451 guint8 data[8] = { 0, };
3454 GST_WRITE_UINT16_BE (data, value);
3455 atom_udta_add_tag (udta, fourcc, flags, data, 2);
3457 GST_WRITE_UINT32_BE (data + 2, value);
3458 atom_udta_add_tag (udta, fourcc, flags, data, 8);
3462 #define GST_BUFFER_NEW_READONLY(mem, size) \
3463 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, mem, size, \
3467 atom_udta_add_blob_tag (AtomUDTA * udta, guint8 * data, guint size)
3469 AtomData *data_atom;
3476 /* blob is unparsed atom;
3477 * extract size and fourcc, and wrap remainder in data atom */
3478 len = GST_READ_UINT32_BE (data);
3479 fourcc = GST_READ_UINT32_LE (data + 4);
3483 data_atom = atom_data_new_from_data (fourcc, data + 8, len - 8);
3485 atom_udta_append_tag (udta,
3486 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3491 atom_udta_add_3gp_tag (AtomUDTA * udta, guint32 fourcc, guint8 * data,
3494 AtomData *data_atom;
3496 data_atom = atom_data_new (fourcc);
3498 /* need full atom */
3499 atom_data_alloc_mem (data_atom, size + 4);
3501 /* full atom: version and flags */
3502 GST_WRITE_UINT32_BE (data_atom->data, 0);
3503 memcpy (data_atom->data + 4, data, size);
3505 atom_udta_append_tag (udta,
3506 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3511 language_code (const char *lang)
3513 g_return_val_if_fail (lang != NULL, 0);
3514 g_return_val_if_fail (strlen (lang) == 3, 0);
3516 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
3517 ((lang[2] - 0x60) & 0x1F);
3521 atom_udta_add_3gp_str_int_tag (AtomUDTA * udta, guint32 fourcc,
3522 const gchar * value, gint16 ivalue)
3524 gint len = 0, size = 0;
3528 len = strlen (value);
3535 data = g_malloc (size + 3);
3536 /* language tag and null-terminated UTF-8 string */
3538 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
3539 /* include 0 terminator */
3540 memcpy (data + 2, value, len + 1);
3542 /* 16-bit unsigned int if standalone, otherwise 8-bit */
3545 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
3547 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
3552 atom_udta_add_3gp_tag (udta, fourcc, data, size);
3557 atom_udta_add_3gp_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3559 atom_udta_add_3gp_str_int_tag (udta, fourcc, value, -1);
3563 atom_udta_add_3gp_uint_tag (AtomUDTA * udta, guint32 fourcc, guint16 value)
3565 atom_udta_add_3gp_str_int_tag (udta, fourcc, NULL, value);
3569 atom_udta_add_xmp_tags (AtomUDTA * udta, GstBuffer * xmpbuffer)
3571 AtomData *data_atom = NULL;
3573 if (udta->context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3575 data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
3576 udta->entries = g_list_append (udta->entries,
3577 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3581 GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
3586 * Functions for specifying media types
3590 atom_minf_set_audio (AtomMINF * minf)
3592 atom_minf_clear_handlers (minf);
3593 minf->smhd = atom_smhd_new ();
3597 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
3599 atom_minf_clear_handlers (minf);
3600 minf->vmhd = atom_vmhd_new (context);
3604 atom_minf_set_subtitle (AtomMINF * minf)
3606 atom_minf_clear_handlers (minf);
3610 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
3613 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3614 hdlr->component_type = comp_type;
3616 hdlr->handler_type = hdlr_type;
3620 atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
3622 g_free (hdlr->name);
3623 hdlr->name = g_strdup (name);
3627 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
3629 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3630 /* Some players (low-end hardware) check for this name, which is what
3631 * QuickTime itself sets */
3632 atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3636 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3638 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3639 /* Some players (low-end hardware) check for this name, which is what
3640 * QuickTime itself sets */
3641 atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3645 atom_mdia_set_hdlr_type_subtitle (AtomMDIA * mdia, AtomsContext * context)
3647 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_sbtl);
3649 /* Just follows the pattern from video and audio above */
3650 atom_hdlr_set_name (&mdia->hdlr, "SubtitleHandler");
3654 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3656 atom_mdia_set_hdlr_type_audio (mdia, context);
3657 atom_minf_set_audio (&mdia->minf);
3661 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3663 atom_mdia_set_hdlr_type_video (mdia, context);
3664 atom_minf_set_video (&mdia->minf, context);
3668 atom_mdia_set_subtitle (AtomMDIA * mdia, AtomsContext * context)
3670 atom_mdia_set_hdlr_type_subtitle (mdia, context);
3671 atom_minf_set_subtitle (&mdia->minf);
3675 atom_tkhd_set_audio (AtomTKHD * tkhd)
3677 tkhd->volume = 0x0100;
3678 tkhd->width = tkhd->height = 0;
3682 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3687 /* qt and ISO base media do not contradict, and examples agree */
3688 tkhd->width = width;
3689 tkhd->height = height;
3693 atom_tkhd_set_subtitle (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3698 /* qt and ISO base media do not contradict, and examples agree */
3699 tkhd->width = width;
3700 tkhd->height = height;
3705 atom_edts_add_entry (AtomEDTS * edts, gint index, EditListEntry * entry)
3708 (EditListEntry *) g_slist_nth_data (edts->elst.entries, index);
3709 /* Create a new entry if missing (appends to the list if index is larger) */
3711 e = g_new (EditListEntry, 1);
3712 edts->elst.entries = g_slist_insert (edts->elst.entries, e, index);
3715 /* Update the entry */
3720 atom_trak_edts_clear (AtomTRAK * trak)
3723 atom_edts_clear (trak->edts);
3729 * Update an entry in this trak edits list, creating it if needed.
3730 * index is the index of the entry to update, or create if it's past the end.
3731 * duration is in the moov's timescale
3732 * media_time is the offset in the media time to start from (media's timescale)
3733 * rate is a 32 bits fixed-point
3736 atom_trak_set_elst_entry (AtomTRAK * trak, gint index,
3737 guint32 duration, guint32 media_time, guint32 rate)
3739 EditListEntry entry;
3741 entry.duration = duration;
3742 entry.media_time = media_time;
3743 entry.media_rate = rate;
3745 if (trak->edts == NULL)
3746 trak->edts = atom_edts_new ();
3748 atom_edts_add_entry (trak->edts, index, &entry);
3751 /* re-negotiation is prevented at top-level, so only 1 entry expected.
3752 * Quite some more care here and elsewhere may be needed to
3753 * support several entries */
3754 static SampleTableEntryMP4A *
3755 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
3758 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3759 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
3761 mp4a->se.header.type = type;
3762 mp4a->se.kind = AUDIO;
3763 mp4a->compression_id = -1;
3764 mp4a->se.data_reference_index = 1;
3766 stsd->entries = g_list_prepend (stsd->entries, mp4a);
3771 static SampleTableEntryTMCD *
3772 atom_trak_add_timecode_entry (AtomTRAK * trak, AtomsContext * context,
3773 GstVideoTimeCode * tc)
3775 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3776 SampleTableEntryTMCD *tmcd = sample_entry_tmcd_new ();
3778 trak->mdia.hdlr.component_type = FOURCC_mhlr;
3779 trak->mdia.hdlr.handler_type = FOURCC_tmcd;
3780 trak->mdia.hdlr.name = g_strdup ("Time Code Media Handler");
3781 trak->mdia.mdhd.time_info.timescale = tc->config.fps_n / tc->config.fps_d;
3783 tmcd->se.kind = TIMECODE;
3784 tmcd->se.data_reference_index = 1;
3785 tmcd->tc_flags = TC_24H_MAX;
3786 if (tc->config.flags &= GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME)
3787 tmcd->tc_flags |= TC_DROP_FRAME;
3788 tmcd->name.language_code = 0;
3789 tmcd->name.name = g_strdup ("Tape");
3790 tmcd->timescale = tc->config.fps_n;
3791 tmcd->frame_duration = tc->config.fps_d;
3792 if (tc->config.fps_d == 1001)
3793 tmcd->n_frames = tc->config.fps_n / 1000;
3795 tmcd->n_frames = tc->config.fps_n / tc->config.fps_d;
3797 stsd->entries = g_list_prepend (stsd->entries, tmcd);
3802 static SampleTableEntryMP4V *
3803 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
3806 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
3807 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3809 mp4v->se.header.type = type;
3810 mp4v->se.kind = VIDEO;
3811 mp4v->se.data_reference_index = 1;
3812 mp4v->horizontal_resolution = 72 << 16;
3813 mp4v->vertical_resolution = 72 << 16;
3814 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3815 mp4v->spatial_quality = 512;
3816 mp4v->temporal_quality = 512;
3819 stsd->entries = g_list_prepend (stsd->entries, mp4v);
3824 static SampleTableEntryTX3G *
3825 atom_trak_add_subtitle_entry (AtomTRAK * trak, AtomsContext * context,
3828 SampleTableEntryTX3G *tx3g = sample_entry_tx3g_new ();
3829 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3831 tx3g->se.header.type = type;
3832 tx3g->se.kind = SUBTITLE;
3833 tx3g->se.data_reference_index = 1;
3835 stsd->entries = g_list_prepend (stsd->entries, tx3g);
3842 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
3844 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
3848 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
3850 atom_tkhd_set_audio (&trak->tkhd);
3851 atom_mdia_set_audio (&trak->mdia, context);
3855 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
3858 atom_tkhd_set_video (&trak->tkhd, context, width, height);
3859 atom_mdia_set_video (&trak->mdia, context);
3863 atom_trak_set_subtitle (AtomTRAK * trak, AtomsContext * context)
3865 atom_tkhd_set_subtitle (&trak->tkhd, context, 0, 0);
3866 atom_mdia_set_subtitle (&trak->mdia, context);
3870 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
3873 atom_trak_set_audio (trak, context);
3874 trak->mdia.mdhd.time_info.timescale = rate;
3878 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
3879 guint32 rate, guint32 width, guint32 height)
3881 atom_trak_set_video (trak, context, width, height);
3882 trak->mdia.mdhd.time_info.timescale = rate;
3883 trak->tkhd.width = width << 16;
3884 trak->tkhd.height = height << 16;
3888 atom_trak_set_subtitle_commons (AtomTRAK * trak, AtomsContext * context)
3890 atom_trak_set_subtitle (trak, context);
3891 trak->mdia.mdhd.time_info.timescale = 1000;
3893 trak->tkhd.alternate_group = 2; /* same for all subtitles */
3894 trak->tkhd.layer = -1; /* above video (layer 0) */
3898 sample_table_entry_add_ext_atom (SampleTableEntry * ste, AtomInfo * ext)
3900 GList **list = NULL;
3901 if (ste->kind == AUDIO) {
3902 list = &(((SampleTableEntryMP4A *) ste)->extension_atoms);
3903 } else if (ste->kind == VIDEO) {
3904 list = &(((SampleTableEntryMP4V *) ste)->extension_atoms);
3906 g_assert_not_reached ();
3910 *list = g_list_prepend (*list, ext);
3913 SampleTableEntryMP4A *
3914 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
3915 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
3917 SampleTableEntryMP4A *ste;
3919 atom_trak_set_audio_commons (trak, context, scale);
3920 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3921 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
3923 trak->is_video = FALSE;
3924 trak->is_h264 = FALSE;
3926 ste->version = entry->version;
3927 ste->compression_id = entry->compression_id;
3928 ste->sample_size = entry->sample_size;
3929 ste->sample_rate = entry->sample_rate << 16;
3930 ste->channels = entry->channels;
3932 ste->samples_per_packet = entry->samples_per_packet;
3933 ste->bytes_per_sample = entry->bytes_per_sample;
3934 ste->bytes_per_packet = entry->bytes_per_packet;
3935 ste->bytes_per_frame = entry->bytes_per_frame;
3938 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
3940 /* 0 size means variable size */
3941 atom_trak_set_constant_size_samples (trak, sample_size);
3946 SampleTableEntryTMCD *
3947 atom_trak_set_timecode_type (AtomTRAK * trak, AtomsContext * context,
3948 GstVideoTimeCode * tc)
3950 SampleTableEntryTMCD *ste;
3951 AtomGMHD *gmhd = trak->mdia.minf.gmhd;
3953 if (context->flavor != ATOMS_TREE_FLAVOR_MOV) {
3957 ste = atom_trak_add_timecode_entry (trak, context, tc);
3959 gmhd = atom_gmhd_new ();
3960 gmhd->gmin.graphics_mode = 0x0040;
3961 gmhd->gmin.opcolor[0] = 0x8000;
3962 gmhd->gmin.opcolor[1] = 0x8000;
3963 gmhd->gmin.opcolor[2] = 0x8000;
3964 gmhd->tmcd.tcmi.text_size = 12;
3965 gmhd->tmcd.tcmi.font_name = g_strdup ("Chicago"); /* Pascal string */
3967 trak->mdia.minf.gmhd = gmhd;
3968 trak->is_video = FALSE;
3969 trak->is_h264 = FALSE;
3975 build_pasp_extension (gint par_width, gint par_height)
3977 AtomData *atom_data = atom_data_new (FOURCC_pasp);
3980 atom_data_alloc_mem (atom_data, 8);
3981 data = atom_data->data;
3983 /* ihdr = image header box */
3984 GST_WRITE_UINT32_BE (data, par_width);
3985 GST_WRITE_UINT32_BE (data + 4, par_height);
3987 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3992 build_fiel_extension (GstVideoInterlaceMode mode, GstVideoFieldOrder order)
3994 AtomData *atom_data = atom_data_new (FOURCC_fiel);
3999 atom_data_alloc_mem (atom_data, 2);
4000 data = atom_data->data;
4002 if (mode == GST_VIDEO_INTERLACE_MODE_PROGRESSIVE) {
4005 } else if (mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED) {
4007 field_order = order == GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST ? 9 : 14;
4013 GST_WRITE_UINT8 (data, interlace);
4014 GST_WRITE_UINT8 (data + 1, field_order);
4016 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4021 build_colr_extension (const GstVideoColorimetry * colorimetry, gboolean is_mp4)
4023 AtomData *atom_data = atom_data_new (FOURCC_colr);
4026 guint16 transfer_function;
4029 switch (colorimetry->primaries) {
4030 case GST_VIDEO_COLOR_PRIMARIES_BT709:
4033 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
4036 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
4037 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
4040 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
4043 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
4049 switch (colorimetry->transfer) {
4050 case GST_VIDEO_TRANSFER_BT709:
4051 transfer_function = 1;
4053 case GST_VIDEO_TRANSFER_SMPTE240M:
4054 transfer_function = 7;
4056 case GST_VIDEO_TRANSFER_UNKNOWN:
4058 transfer_function = 2;
4062 switch (colorimetry->matrix) {
4063 case GST_VIDEO_COLOR_MATRIX_BT709:
4066 case GST_VIDEO_COLOR_MATRIX_BT601:
4069 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
4072 case GST_VIDEO_COLOR_MATRIX_BT2020:
4075 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
4081 atom_data_alloc_mem (atom_data, 10 + (is_mp4 ? 1 : 0));
4082 data = atom_data->data;
4084 /* colour specification box */
4086 GST_WRITE_UINT32_LE (data, FOURCC_nclx);
4088 GST_WRITE_UINT32_LE (data, FOURCC_nclc);
4090 GST_WRITE_UINT16_BE (data + 4, primaries);
4091 GST_WRITE_UINT16_BE (data + 6, transfer_function);
4092 GST_WRITE_UINT16_BE (data + 8, matrix);
4095 GST_WRITE_UINT8 (data + 10,
4096 colorimetry->range == GST_VIDEO_COLOR_RANGE_0_255 ? 0x80 : 0x00);
4099 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4104 build_clap_extension (gint width_n, gint width_d, gint height_n, gint height_d,
4105 gint h_off_n, gint h_off_d, gint v_off_n, gint v_off_d)
4107 AtomData *atom_data = atom_data_new (FOURCC_clap);
4110 atom_data_alloc_mem (atom_data, 32);
4111 data = atom_data->data;
4113 GST_WRITE_UINT32_BE (data, width_n);
4114 GST_WRITE_UINT32_BE (data + 4, width_d);
4115 GST_WRITE_UINT32_BE (data + 8, height_n);
4116 GST_WRITE_UINT32_BE (data + 12, height_d);
4117 GST_WRITE_UINT32_BE (data + 16, h_off_n);
4118 GST_WRITE_UINT32_BE (data + 20, h_off_d);
4119 GST_WRITE_UINT32_BE (data + 24, v_off_n);
4120 GST_WRITE_UINT32_BE (data + 28, v_off_d);
4122 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4127 build_tapt_extension (gint clef_width, gint clef_height, gint prof_width,
4128 gint prof_height, gint enof_width, gint enof_height)
4130 AtomData *atom_data = atom_data_new (FOURCC_tapt);
4133 atom_data_alloc_mem (atom_data, 60);
4134 data = atom_data->data;
4136 GST_WRITE_UINT32_BE (data, 20);
4137 GST_WRITE_UINT32_LE (data + 4, FOURCC_clef);
4138 GST_WRITE_UINT32_BE (data + 8, 0);
4139 GST_WRITE_UINT32_BE (data + 12, clef_width);
4140 GST_WRITE_UINT32_BE (data + 16, clef_height);
4142 GST_WRITE_UINT32_BE (data + 20, 20);
4143 GST_WRITE_UINT32_LE (data + 24, FOURCC_prof);
4144 GST_WRITE_UINT32_BE (data + 28, 0);
4145 GST_WRITE_UINT32_BE (data + 32, prof_width);
4146 GST_WRITE_UINT32_BE (data + 36, prof_height);
4148 GST_WRITE_UINT32_BE (data + 40, 20);
4149 GST_WRITE_UINT32_LE (data + 44, FOURCC_enof);
4150 GST_WRITE_UINT32_BE (data + 48, 0);
4151 GST_WRITE_UINT32_BE (data + 52, enof_width);
4152 GST_WRITE_UINT32_BE (data + 56, enof_height);
4155 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4160 build_mov_video_sample_description_padding_extension (void)
4162 AtomData *atom_data = atom_data_new (FOURCC_clap);
4164 return build_atom_info_wrapper ((Atom *) atom_data, atom_copy_empty,
4168 SampleTableEntryMP4V *
4169 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
4170 VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
4172 SampleTableEntryMP4V *ste;
4173 guint dwidth, dheight;
4174 gint par_n = 0, par_d = 0;
4176 par_n = entry->par_n;
4177 par_d = entry->par_d;
4179 dwidth = entry->width;
4180 dheight = entry->height;
4181 /* ISO file spec says track header w/h indicates track's visual presentation
4182 * (so this together with pixels w/h implicitly defines PAR) */
4183 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
4184 /* Assumes square pixels display */
4185 if (!gst_video_calculate_display_ratio (&dwidth, &dheight, entry->width,
4186 entry->height, par_n, par_d, 1, 1)) {
4187 GST_WARNING ("Could not calculate display ratio");
4191 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
4192 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4193 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
4195 trak->is_video = TRUE;
4196 trak->is_h264 = (entry->fourcc == FOURCC_avc1
4197 || entry->fourcc == FOURCC_avc3);
4199 ste->version = entry->version;
4200 ste->width = entry->width;
4201 ste->height = entry->height;
4202 ste->depth = entry->depth;
4203 ste->color_table_id = entry->color_table_id;
4204 ste->frame_count = entry->frame_count;
4207 ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
4209 ste->extension_atoms = g_list_append (ste->extension_atoms,
4210 build_pasp_extension (par_n, par_d));
4212 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
4213 /* append 0 as a terminator "length" to work around some broken software */
4214 ste->extension_atoms =
4215 g_list_append (ste->extension_atoms,
4216 build_mov_video_sample_description_padding_extension ());
4223 subtitle_sample_entry_init (SubtitleSampleEntry * entry)
4225 entry->font_size = 0;
4226 entry->font_face = 0;
4227 entry->foreground_color_rgba = 0xFFFFFFFF; /* all white, opaque */
4230 SampleTableEntryTX3G *
4231 atom_trak_set_subtitle_type (AtomTRAK * trak, AtomsContext * context,
4232 SubtitleSampleEntry * entry)
4234 SampleTableEntryTX3G *tx3g;
4236 atom_trak_set_subtitle_commons (trak, context);
4237 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4238 tx3g = atom_trak_add_subtitle_entry (trak, context, entry->fourcc);
4240 tx3g->font_face = entry->font_face;
4241 tx3g->font_size = entry->font_size;
4242 tx3g->foreground_color_rgba = entry->foreground_color_rgba;
4244 trak->is_video = FALSE;
4245 trak->is_h264 = FALSE;
4251 atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
4253 guint8 flags[3] = { 0, 0, 0 };
4255 atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
4256 mfhd->sequence_number = sequence_number;
4260 atom_moof_init (AtomMOOF * moof, AtomsContext * context,
4261 guint32 sequence_number)
4263 atom_header_set (&moof->header, FOURCC_moof, 0, 0);
4264 atom_mfhd_init (&moof->mfhd, sequence_number);
4269 atom_moof_new (AtomsContext * context, guint32 sequence_number)
4271 AtomMOOF *moof = g_new0 (AtomMOOF, 1);
4273 atom_moof_init (moof, context, sequence_number);
4278 atom_trun_free (AtomTRUN * trun)
4280 atom_full_clear (&trun->header);
4281 atom_array_clear (&trun->entries);
4286 atom_sdtp_free (AtomSDTP * sdtp)
4288 atom_full_clear (&sdtp->header);
4289 atom_array_clear (&sdtp->entries);
4294 atom_traf_free (AtomTRAF * traf)
4298 walker = traf->truns;
4300 atom_trun_free ((AtomTRUN *) walker->data);
4301 walker = g_list_next (walker);
4303 g_list_free (traf->truns);
4306 walker = traf->sdtps;
4308 atom_sdtp_free ((AtomSDTP *) walker->data);
4309 walker = g_list_next (walker);
4311 g_list_free (traf->sdtps);
4318 atom_moof_free (AtomMOOF * moof)
4322 walker = moof->trafs;
4324 atom_traf_free ((AtomTRAF *) walker->data);
4325 walker = g_list_next (walker);
4327 g_list_free (moof->trafs);
4334 atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
4337 guint64 original_offset = *offset;
4339 if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
4343 prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
4345 atom_write_size (buffer, size, offset, original_offset);
4346 return *offset - original_offset;
4350 atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
4353 guint64 original_offset = *offset;
4356 if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
4360 prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
4362 flags = atom_full_get_flags_as_uint (&tfhd->header);
4364 if (flags & TF_BASE_DATA_OFFSET)
4365 prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
4366 if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
4367 prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
4368 if (flags & TF_DEFAULT_SAMPLE_DURATION)
4369 prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
4370 if (flags & TF_DEFAULT_SAMPLE_SIZE)
4371 prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
4372 if (flags & TF_DEFAULT_SAMPLE_FLAGS)
4373 prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
4375 atom_write_size (buffer, size, offset, original_offset);
4376 return *offset - original_offset;
4380 atom_tfdt_copy_data (AtomTFDT * tfdt, guint8 ** buffer, guint64 * size,
4383 guint64 original_offset = *offset;
4385 if (!atom_full_copy_data (&tfdt->header, buffer, size, offset)) {
4389 /* 32-bit time if version == 0 else 64-bit: */
4390 if (tfdt->header.version == 0)
4391 prop_copy_uint32 (tfdt->base_media_decode_time, buffer, size, offset);
4393 prop_copy_uint64 (tfdt->base_media_decode_time, buffer, size, offset);
4395 atom_write_size (buffer, size, offset, original_offset);
4396 return *offset - original_offset;
4400 atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
4401 guint64 * offset, guint32 * data_offset)
4403 guint64 original_offset = *offset;
4406 flags = atom_full_get_flags_as_uint (&trun->header);
4408 /* if first trun in moof, forcibly add data_offset and record
4409 * where it must be written later on */
4410 if (data_offset && !*data_offset) {
4411 flags |= TR_DATA_OFFSET;
4413 flags &= ~TR_DATA_OFFSET;
4416 atom_full_set_flags_as_uint (&trun->header, flags);
4418 if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
4422 prop_copy_uint32 (trun->sample_count, buffer, size, offset);
4424 if (flags & TR_DATA_OFFSET) {
4425 *data_offset = *offset;
4426 prop_copy_int32 (trun->data_offset, buffer, size, offset);
4428 if (flags & TR_FIRST_SAMPLE_FLAGS)
4429 prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
4431 for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
4432 TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
4434 if (flags & TR_SAMPLE_DURATION)
4435 prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
4436 if (flags & TR_SAMPLE_SIZE)
4437 prop_copy_uint32 (entry->sample_size, buffer, size, offset);
4438 if (flags & TR_SAMPLE_FLAGS)
4439 prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
4440 if (flags & TR_COMPOSITION_TIME_OFFSETS)
4441 prop_copy_uint32 (entry->sample_composition_time_offset,
4442 buffer, size, offset);
4445 atom_write_size (buffer, size, offset, original_offset);
4446 return *offset - original_offset;
4450 atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
4453 guint64 original_offset = *offset;
4455 if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
4459 /* all entries at once */
4460 prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
4461 atom_array_get_len (&sdtp->entries), buffer, size, offset);
4463 atom_write_size (buffer, size, offset, original_offset);
4464 return *offset - original_offset;
4468 atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
4469 guint64 * offset, guint32 * data_offset)
4471 guint64 original_offset = *offset;
4474 if (!atom_copy_data (&traf->header, buffer, size, offset)) {
4477 if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
4480 if (!atom_tfdt_copy_data (&traf->tfdt, buffer, size, offset)) {
4484 walker = g_list_first (traf->truns);
4485 while (walker != NULL) {
4486 if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset,
4490 walker = g_list_next (walker);
4493 walker = g_list_first (traf->sdtps);
4494 while (walker != NULL) {
4495 if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
4498 walker = g_list_next (walker);
4501 atom_write_size (buffer, size, offset, original_offset);
4502 return *offset - original_offset;
4505 /* creates moof atom; metadata is written expecting actual buffer data
4506 * is in mdata directly after moof, and is consecutively written per trak */
4508 atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
4509 guint64 * size, guint64 * offset)
4511 guint64 original_offset = *offset;
4513 guint32 data_offset = 0;
4515 if (!atom_copy_data (&moof->header, buffer, size, offset))
4518 if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
4521 walker = g_list_first (moof->trafs);
4522 while (walker != NULL) {
4523 if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset,
4527 walker = g_list_next (walker);
4530 atom_write_size (buffer, size, offset, original_offset);
4532 if (*buffer && data_offset) {
4533 /* first trun needs a data-offset relative to moof start
4534 * = moof size + mdat prefix */
4535 GST_WRITE_UINT32_BE (*buffer + data_offset, *offset - original_offset + 8);
4538 return *offset - original_offset;
4542 atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
4544 guint8 flags[3] = { 0, 0, 0 };
4546 atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
4547 tfhd->track_ID = track_ID;
4548 tfhd->base_data_offset = 0;
4549 tfhd->sample_description_index = 1;
4550 tfhd->default_sample_duration = 0;
4551 tfhd->default_sample_size = 0;
4552 tfhd->default_sample_flags = 0;
4556 atom_tfdt_init (AtomTFDT * tfdt)
4558 guint8 flags[3] = { 0, 0, 0 };
4559 atom_full_init (&tfdt->header, FOURCC_tfdt, 0, 0, 0, flags);
4561 tfdt->base_media_decode_time = 0;
4565 atom_trun_init (AtomTRUN * trun)
4567 guint8 flags[3] = { 0, 0, 0 };
4569 atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
4570 trun->sample_count = 0;
4571 trun->data_offset = 0;
4572 trun->first_sample_flags = 0;
4573 atom_array_init (&trun->entries, 512);
4577 atom_trun_new (void)
4579 AtomTRUN *trun = g_new0 (AtomTRUN, 1);
4581 atom_trun_init (trun);
4586 atom_sdtp_init (AtomSDTP * sdtp)
4588 guint8 flags[3] = { 0, 0, 0 };
4590 atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
4591 atom_array_init (&sdtp->entries, 512);
4595 atom_sdtp_new (AtomsContext * context)
4597 AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
4599 atom_sdtp_init (sdtp);
4604 atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
4606 traf->sdtps = g_list_append (traf->sdtps, sdtp);
4610 atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
4612 /* it does not make much/any sense according to specs,
4613 * but that's how MS isml samples seem to do it */
4614 atom_array_append (&sdtp->entries, val, 256);
4618 atom_trun_add_samples (AtomTRUN * trun, guint32 delta, guint32 size,
4619 guint32 flags, gint64 pts_offset)
4621 TRUNSampleEntry nentry;
4623 if (pts_offset != 0)
4624 trun->header.flags[1] |= (TR_COMPOSITION_TIME_OFFSETS >> 8);
4626 nentry.sample_duration = delta;
4627 nentry.sample_size = size;
4628 nentry.sample_flags = flags;
4629 nentry.sample_composition_time_offset = pts_offset;
4630 atom_array_append (&trun->entries, nentry, 256);
4631 trun->sample_count++;
4635 atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
4637 atom_header_set (&traf->header, FOURCC_traf, 0, 0);
4638 atom_tfdt_init (&traf->tfdt);
4639 atom_tfhd_init (&traf->tfhd, track_ID);
4642 if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
4643 atom_traf_add_sdtp (traf, atom_sdtp_new (context));
4647 atom_traf_new (AtomsContext * context, guint32 track_ID)
4649 AtomTRAF *traf = g_new0 (AtomTRAF, 1);
4651 atom_traf_init (traf, context, track_ID);
4656 atom_traf_set_base_decode_time (AtomTRAF * traf, guint64 base_decode_time)
4658 traf->tfdt.base_media_decode_time = base_decode_time;
4659 /* If we need to write a 64-bit tfdt, set the atom version */
4660 if (base_decode_time > G_MAXUINT32)
4661 traf->tfdt.header.version = 1;
4665 atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
4667 traf->truns = g_list_append (traf->truns, trun);
4671 atom_traf_add_samples (AtomTRAF * traf, guint32 delta, guint32 size,
4672 gboolean sync, gint64 pts_offset, gboolean sdtp_sync)
4677 /* 0x10000 is sample-is-difference-sample flag
4678 * low byte stuff is what ismv uses */
4679 flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
4681 if (G_UNLIKELY (!traf->truns)) {
4682 trun = atom_trun_new ();
4683 atom_traf_add_trun (traf, trun);
4684 /* optimistic; indicate all defaults present in tfhd */
4685 traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
4686 TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
4687 traf->tfhd.default_sample_duration = delta;
4688 traf->tfhd.default_sample_size = size;
4689 traf->tfhd.default_sample_flags = flags;
4690 trun->first_sample_flags = flags;
4693 trun = traf->truns->data;
4695 /* check if still matching defaults,
4696 * if not, abandon default and need entry for each sample */
4697 if (traf->tfhd.default_sample_duration != delta) {
4698 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
4699 trun->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
4701 if (traf->tfhd.default_sample_size != size) {
4702 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
4703 trun->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
4705 if (traf->tfhd.default_sample_flags != flags) {
4706 if (trun->sample_count == 1) {
4707 /* at least will need first sample flag */
4708 traf->tfhd.default_sample_flags = flags;
4709 trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
4711 /* now we need sample flags for each sample */
4712 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
4713 trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
4714 trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
4718 atom_trun_add_samples (traf->truns->data, delta, size, flags, pts_offset);
4721 atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
4725 atom_traf_get_sample_num (AtomTRAF * traf)
4729 if (G_UNLIKELY (!traf->truns))
4732 trun = traf->truns->data;
4733 return atom_array_get_len (&trun->entries);
4737 atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
4739 moof->trafs = g_list_append (moof->trafs, traf);
4743 atom_tfra_free (AtomTFRA * tfra)
4745 atom_full_clear (&tfra->header);
4746 atom_array_clear (&tfra->entries);
4751 atom_mfra_new (AtomsContext * context)
4753 AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
4755 atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
4760 atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
4762 mfra->tfras = g_list_append (mfra->tfras, tfra);
4766 atom_mfra_free (AtomMFRA * mfra)
4770 walker = mfra->tfras;
4772 atom_tfra_free ((AtomTFRA *) walker->data);
4773 walker = g_list_next (walker);
4775 g_list_free (mfra->tfras);
4778 atom_clear (&mfra->header);
4783 atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
4785 guint8 flags[3] = { 0, 0, 0 };
4787 atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
4788 tfra->track_ID = track_ID;
4789 atom_array_init (&tfra->entries, 512);
4793 atom_tfra_new (AtomsContext * context, guint32 track_ID)
4795 AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
4797 atom_tfra_init (tfra, track_ID);
4803 need_bytes (guint32 num)
4814 atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
4820 entry.moof_offset = 0;
4821 /* always write a single trun in a single traf */
4822 entry.traf_number = 1;
4823 entry.trun_number = 1;
4824 entry.sample_number = sample_num;
4826 /* auto-use 64 bits if needed */
4827 if (dts > G_MAXUINT32)
4828 tfra->header.version = 1;
4830 /* 1 byte will always do for traf and trun number,
4831 * check how much sample_num needs */
4832 tfra->lengths = (tfra->lengths & 0xfc) ||
4833 MAX (tfra->lengths, need_bytes (sample_num));
4835 atom_array_append (&tfra->entries, entry, 256);
4839 atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
4843 /* auto-use 64 bits if needed */
4844 if (offset > G_MAXUINT32)
4845 tfra->header.version = 1;
4847 for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
4848 TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
4850 if (entry->moof_offset)
4852 entry->moof_offset = offset;
4857 atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
4860 guint64 original_offset = *offset;
4867 if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
4871 prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
4872 prop_copy_uint32 (tfra->lengths, buffer, size, offset);
4873 prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
4875 version = tfra->header.version;
4876 for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
4877 entry = &atom_array_index (&tfra->entries, i);
4879 prop_copy_uint64 (entry->time, buffer, size, offset);
4880 prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
4882 prop_copy_uint32 (entry->time, buffer, size, offset);
4883 prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
4886 bytes = (tfra->lengths & (0x3 << 4)) + 1;
4887 data = GUINT32_TO_BE (entry->traf_number);
4888 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
4889 buffer, size, offset);
4891 bytes = (tfra->lengths & (0x3 << 2)) + 1;
4892 data = GUINT32_TO_BE (entry->trun_number);
4893 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
4894 buffer, size, offset);
4896 bytes = (tfra->lengths & (0x3)) + 1;
4897 data = GUINT32_TO_BE (entry->sample_number);
4898 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
4899 buffer, size, offset);
4903 atom_write_size (buffer, size, offset, original_offset);
4904 return *offset - original_offset;
4908 atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
4911 guint64 original_offset = *offset;
4912 guint8 flags[3] = { 0, 0, 0 };
4915 atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
4917 if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
4921 prop_copy_uint32 (s, buffer, size, offset);
4923 atom_write_size (buffer, size, offset, original_offset);
4925 return *offset - original_offset;
4930 atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
4933 guint64 original_offset = *offset;
4936 if (!atom_copy_data (&mfra->header, buffer, size, offset))
4939 walker = g_list_first (mfra->tfras);
4940 while (walker != NULL) {
4941 if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
4944 walker = g_list_next (walker);
4947 /* 16 is the size of the mfro atom */
4948 if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
4952 atom_write_size (buffer, size, offset, original_offset);
4953 return *offset - original_offset;
4956 /* some sample description construction helpers */
4959 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
4960 const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
4965 track_id = trak->tkhd.track_ID;
4967 esds = atom_esds_new ();
4968 esds->es.id = track_id & 0xFFFF;
4969 esds->es.dec_conf_desc.object_type = object_type;
4970 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
4972 if (avg_bitrate > 0)
4973 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
4974 if (max_bitrate > 0)
4975 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
4977 /* optional DecoderSpecificInfo */
4979 DecoderSpecificInfoDescriptor *desc;
4982 esds->es.dec_conf_desc.dec_specific_info = desc =
4983 desc_dec_specific_info_new ();
4984 size = gst_buffer_get_size ((GstBuffer *) codec_data);
4985 desc_dec_specific_info_alloc_data (desc, size);
4986 gst_buffer_extract ((GstBuffer *) codec_data, 0, desc->data, size);
4989 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
4994 build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
4995 guint32 max_bitrate)
4997 AtomData *atom_data = atom_data_new (FOURCC_btrt);
5000 atom_data_alloc_mem (atom_data, 12);
5001 data = atom_data->data;
5003 GST_WRITE_UINT32_BE (data, buffer_size_db);
5004 GST_WRITE_UINT32_BE (data + 4, max_bitrate);
5005 GST_WRITE_UINT32_BE (data + 8, avg_bitrate);
5007 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5012 build_mov_wave_extension (guint32 fourcc, AtomInfo * atom1, AtomInfo * atom2,
5013 gboolean terminator)
5019 /* Build WAVE atom for sample table entry */
5020 wave = atom_wave_new ();
5022 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5024 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5025 wave->extension_atoms =
5026 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5027 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5030 /* Add supplied atoms to WAVE */
5032 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
5034 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
5036 /* Add FRMA to the WAVE */
5037 frma = atom_frma_new ();
5038 frma->media_type = fourcc;
5040 wave->extension_atoms =
5041 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5042 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5044 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5049 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
5050 guint32 avg_bitrate, guint32 max_bitrate)
5052 AtomInfo *esds, *mp4a;
5056 /* Add ESDS atom to WAVE */
5057 esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
5058 ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
5060 /* Add MP4A atom to the WAVE:
5061 * not really in spec, but makes offset based players happy */
5062 buf = GST_BUFFER_NEW_READONLY (&tmp, 4);
5063 mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
5064 gst_buffer_unref (buf);
5066 return build_mov_wave_extension (FOURCC_mp4a, mp4a, esds, TRUE);
5070 build_mov_alac_extension (const GstBuffer * codec_data)
5074 alac = build_codec_data_extension (FOURCC_alac, codec_data);
5076 return build_mov_wave_extension (FOURCC_alac, NULL, alac, TRUE);
5080 build_jp2x_extension (const GstBuffer * prefix)
5082 AtomData *atom_data;
5088 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2x, prefix);
5090 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5095 build_jp2h_extension (gint width, gint height, const gchar * colorspace,
5096 gint ncomp, const GValue * cmap_array, const GValue * cdef_array)
5098 AtomData *atom_data;
5102 gint idhr_size = 22;
5103 gint colr_size = 15;
5104 gint cmap_size = 0, cdef_size = 0;
5105 gint cmap_array_size = 0;
5106 gint cdef_array_size = 0;
5107 GstByteWriter writer;
5109 g_return_val_if_fail (cmap_array == NULL ||
5110 GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
5111 g_return_val_if_fail (cdef_array == NULL ||
5112 GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
5114 if (g_str_equal (colorspace, "sRGB")) {
5118 } else if (g_str_equal (colorspace, "GRAY")) {
5122 } else if (g_str_equal (colorspace, "sYUV")) {
5130 cmap_array_size = gst_value_array_get_size (cmap_array);
5131 cmap_size = 8 + cmap_array_size * 4;
5134 cdef_array_size = gst_value_array_get_size (cdef_array);
5135 cdef_size = 8 + 2 + cdef_array_size * 6;
5138 gst_byte_writer_init_with_size (&writer,
5139 idhr_size + colr_size + cmap_size + cdef_size, TRUE);
5141 /* ihdr = image header box */
5142 gst_byte_writer_put_uint32_be_unchecked (&writer, 22);
5143 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_ihdr);
5144 gst_byte_writer_put_uint32_be_unchecked (&writer, height);
5145 gst_byte_writer_put_uint32_be_unchecked (&writer, width);
5146 gst_byte_writer_put_uint16_be_unchecked (&writer, ncomp);
5147 /* 8 bits per component, unsigned */
5148 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5149 /* compression type; reserved */
5150 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5151 /* colour space (un)known */
5152 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5153 /* intellectual property right (box present) */
5154 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5156 /* colour specification box */
5157 gst_byte_writer_put_uint32_be_unchecked (&writer, 15);
5158 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_colr);
5160 /* specification method: enumerated */
5161 gst_byte_writer_put_uint8_unchecked (&writer, 0x1);
5162 /* precedence; reserved */
5163 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5164 /* approximation; reserved */
5165 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5166 /* enumerated colourspace */
5167 gst_byte_writer_put_uint32_be_unchecked (&writer, cenum);
5170 gst_byte_writer_put_uint32_be_unchecked (&writer, cmap_size);
5171 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cmap);
5172 for (i = 0; i < cmap_array_size; i++) {
5178 item = gst_value_array_get_value (cmap_array, i);
5179 value = g_value_get_int (item);
5181 /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
5182 cmp = value & 0xFFFF;
5184 pcol = (value >> 16) & 0xFF;
5187 GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
5188 "handle Pallete mapping atoms yet");
5190 gst_byte_writer_put_uint16_be_unchecked (&writer, cmp);
5191 gst_byte_writer_put_uint8_unchecked (&writer, mtyp);
5192 gst_byte_writer_put_uint8_unchecked (&writer, pcol);
5197 gst_byte_writer_put_uint32_be_unchecked (&writer, cdef_size);
5198 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cdef);
5199 gst_byte_writer_put_uint16_be_unchecked (&writer, cdef_array_size);
5200 for (i = 0; i < cdef_array_size; i++) {
5203 item = gst_value_array_get_value (cdef_array, i);
5204 value = g_value_get_int (item);
5206 gst_byte_writer_put_uint16_be_unchecked (&writer, i);
5208 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5209 gst_byte_writer_put_uint16_be_unchecked (&writer, value);
5210 } else if (value < 0) {
5211 gst_byte_writer_put_uint16_be_unchecked (&writer, -value);
5212 gst_byte_writer_put_uint16_be_unchecked (&writer, 0); /* TODO what here? */
5214 gst_byte_writer_put_uint16_be_unchecked (&writer, 1);
5215 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5220 g_assert (gst_byte_writer_get_remaining (&writer) == 0);
5221 buf = gst_byte_writer_reset_and_get_buffer (&writer);
5223 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
5224 gst_buffer_unref (buf);
5226 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5231 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
5234 AtomInfo *result = NULL;
5237 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
5238 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
5246 build_amr_extension (void)
5253 GST_WRITE_UINT32_LE (ext, 0);
5254 /* decoder version */
5255 GST_WRITE_UINT8 (ext + 4, 0);
5256 /* mode set (all modes) */
5257 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
5258 /* mode change period (no restriction) */
5259 GST_WRITE_UINT8 (ext + 7, 0);
5260 /* frames per sample */
5261 GST_WRITE_UINT8 (ext + 8, 1);
5263 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5264 res = build_codec_data_extension (FOURCC_damr, buf);
5265 gst_buffer_unref (buf);
5270 build_h263_extension (void)
5277 GST_WRITE_UINT32_LE (ext, 0);
5278 /* decoder version */
5279 GST_WRITE_UINT8 (ext + 4, 0);
5280 /* level / profile */
5281 /* FIXME ? maybe ? obtain somewhere; baseline for now */
5282 GST_WRITE_UINT8 (ext + 5, 10);
5283 GST_WRITE_UINT8 (ext + 6, 0);
5285 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5286 res = build_codec_data_extension (FOURCC_d263, buf);
5287 gst_buffer_unref (buf);
5292 build_gama_atom (gdouble gamma)
5298 /* convert to uint32 from fixed point */
5299 gamma_fp = (guint32) 65536 *gamma;
5301 gamma_fp = GUINT32_TO_BE (gamma_fp);
5302 buf = GST_BUFFER_NEW_READONLY (&gamma_fp, 4);
5303 res = build_codec_data_extension (FOURCC_gama, buf);
5304 gst_buffer_unref (buf);
5309 build_SMI_atom (const GstBuffer * seqh)
5316 /* the seqh plus its size and fourcc */
5317 size = gst_buffer_get_size ((GstBuffer *) seqh);
5318 data = g_malloc (size + 8);
5320 GST_WRITE_UINT32_LE (data, FOURCC_SEQH);
5321 GST_WRITE_UINT32_BE (data + 4, size + 8);
5322 gst_buffer_extract ((GstBuffer *) seqh, 0, data + 8, size);
5323 buf = gst_buffer_new_wrapped (data, size + 8);
5324 res = build_codec_data_extension (FOURCC_SMI_, buf);
5325 gst_buffer_unref (buf);
5330 build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
5332 #define IMA_ADPCM_ATOM_SIZE 20
5333 AtomData *atom_data;
5336 gint samplesperblock;
5339 /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
5340 identifier. Note that the identifier here is big-endian, but when used
5341 within the WAVE header (below), it's little endian. */
5342 fourcc = MS_WAVE_FOURCC (0x11);
5344 atom_data = atom_data_new (fourcc);
5345 atom_data_alloc_mem (atom_data, IMA_ADPCM_ATOM_SIZE);
5346 data = atom_data->data;
5348 /* This atom's content is a WAVE header, including 2 bytes of extra data.
5349 Note that all of this is little-endian, unlike most stuff in qt. */
5350 /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
5351 for the rest. Simplifies to this. */
5352 samplesperblock = 2 * blocksize / channels - 7;
5353 bytespersec = rate * blocksize / samplesperblock;
5354 GST_WRITE_UINT16_LE (data, 0x11);
5355 GST_WRITE_UINT16_LE (data + 2, channels);
5356 GST_WRITE_UINT32_LE (data + 4, rate);
5357 GST_WRITE_UINT32_LE (data + 8, bytespersec);
5358 GST_WRITE_UINT16_LE (data + 12, blocksize);
5359 GST_WRITE_UINT16_LE (data + 14, 4);
5360 GST_WRITE_UINT16_LE (data + 16, 2); /* Two extra bytes */
5361 GST_WRITE_UINT16_LE (data + 18, samplesperblock);
5363 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5368 build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
5375 wave = atom_wave_new ();
5377 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5378 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5379 wave->extension_atoms =
5380 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5381 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5383 /* Add wave ima adpcm atom to WAVE */
5384 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
5385 build_ima_adpcm_atom (channels, rate, blocksize));
5387 /* Add FRMA to the WAVE */
5388 frma = atom_frma_new ();
5389 frma->media_type = MS_WAVE_FOURCC (0x11);
5391 wave->extension_atoms =
5392 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5393 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5395 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5400 build_ac3_extension (guint8 fscod, guint8 bsid, guint8 bsmod, guint8 acmod,
5401 guint8 lfe_on, guint8 bitrate_code)
5403 AtomData *atom_data = atom_data_new (FOURCC_dac3);
5406 atom_data_alloc_mem (atom_data, 3);
5407 data = atom_data->data;
5409 /* Bits from the spec
5419 /* Some bit manipulation magic. Need bitwriter */
5420 data[0] = (fscod << 6) | (bsid << 1) | ((bsmod >> 2) & 1);
5422 ((bsmod & 0x3) << 6) | (acmod << 3) | ((lfe_on & 1) << 2) | ((bitrate_code
5424 data[2] = ((bitrate_code & 0x7) << 5);
5426 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5431 build_opus_extension (guint32 rate, guint8 channels, guint8 mapping_family,
5432 guint8 stream_count, guint8 coupled_count, guint8 channel_mapping[256],
5433 guint16 pre_skip, guint16 output_gain)
5435 AtomData *atom_data;
5438 gboolean hdl = TRUE;
5439 guint data_block_len;
5441 gst_byte_writer_init (&bw);
5442 hdl &= gst_byte_writer_put_uint8 (&bw, 0x00); /* version number */
5443 hdl &= gst_byte_writer_put_uint8 (&bw, channels);
5444 hdl &= gst_byte_writer_put_uint16_le (&bw, pre_skip);
5445 hdl &= gst_byte_writer_put_uint32_le (&bw, rate);
5446 hdl &= gst_byte_writer_put_uint16_le (&bw, output_gain);
5447 hdl &= gst_byte_writer_put_uint8 (&bw, mapping_family);
5448 if (mapping_family > 0) {
5449 hdl &= gst_byte_writer_put_uint8 (&bw, stream_count);
5450 hdl &= gst_byte_writer_put_uint8 (&bw, coupled_count);
5451 hdl &= gst_byte_writer_put_data (&bw, channel_mapping, channels);
5455 GST_WARNING ("Error creating header");
5459 data_block_len = gst_byte_writer_get_size (&bw);
5460 data_block = gst_byte_writer_reset_and_get_data (&bw);
5461 atom_data = atom_data_new_from_data (FOURCC_dops, data_block, data_block_len);
5462 g_free (data_block);
5464 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5469 build_uuid_xmp_atom (GstBuffer * xmp_data)
5473 static const guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
5474 0x97, 0xA9, 0x42, 0xE8,
5475 0x9C, 0x71, 0x99, 0x94,
5476 0x91, 0xE3, 0xAF, 0xAC
5479 if (xmp_data == NULL)
5482 uuid = atom_uuid_new ();
5483 memcpy (uuid->uuid, xmp_uuid, 16);
5485 size = gst_buffer_get_size (xmp_data);
5486 uuid->data = g_malloc (size);
5487 uuid->datalen = size;
5488 gst_buffer_extract (xmp_data, 0, uuid->data, size);
5490 return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,