1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2008-2010 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
3 * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sf.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
49 #include <gst/base/gstbytewriter.h>
50 #include <gst/tag/tag.h>
53 * Creates a new AtomsContext for the given flavor.
56 atoms_context_new (AtomsTreeFlavor flavor)
58 AtomsContext *context = g_new0 (AtomsContext, 1);
59 context->flavor = flavor;
64 * Frees an AtomsContext and all memory associated with it
67 atoms_context_free (AtomsContext * context)
72 /* -- creation, initialization, clear and free functions -- */
74 #define SECS_PER_DAY (24 * 60 * 60)
75 #define LEAP_YEARS_FROM_1904_TO_1970 17
78 get_current_qt_time (void)
82 g_get_current_time (&timeval);
83 /* FIXME this should use UTC coordinated time */
84 return timeval.tv_sec + (((1970 - 1904) * (guint64) 365) +
85 LEAP_YEARS_FROM_1904_TO_1970) * SECS_PER_DAY;
89 common_time_info_init (TimeInfo * ti)
91 ti->creation_time = ti->modification_time = get_current_qt_time ();
97 atom_header_set (Atom * header, guint32 fourcc, gint32 size, gint64 ext_size)
99 header->type = fourcc;
101 header->extended_size = ext_size;
105 atom_clear (Atom * atom)
110 atom_full_init (AtomFull * full, guint32 fourcc, gint32 size, gint64 ext_size,
111 guint8 version, guint8 flags[3])
113 atom_header_set (&(full->header), fourcc, size, ext_size);
114 full->version = version;
115 full->flags[0] = flags[0];
116 full->flags[1] = flags[1];
117 full->flags[2] = flags[2];
121 atom_full_clear (AtomFull * full)
123 atom_clear (&full->header);
127 atom_full_free (AtomFull * full)
129 atom_full_clear (full);
134 atom_full_get_flags_as_uint (AtomFull * full)
136 return full->flags[0] << 16 | full->flags[1] << 8 | full->flags[2];
140 atom_full_set_flags_as_uint (AtomFull * full, guint32 flags_as_uint)
142 full->flags[2] = flags_as_uint & 0xFF;
143 full->flags[1] = (flags_as_uint & 0xFF00) >> 8;
144 full->flags[0] = (flags_as_uint & 0xFF0000) >> 16;
148 build_atom_info_wrapper (Atom * atom, gpointer copy_func, gpointer free_func)
150 AtomInfo *info = NULL;
153 info = g_new0 (AtomInfo, 1);
156 info->copy_data_func = copy_func;
157 info->free_func = free_func;
164 atom_info_list_prepend_atom (GList * ai, Atom * atom,
165 AtomCopyDataFunc copy_func, AtomFreeFunc free_func)
168 return g_list_prepend (ai,
169 build_atom_info_wrapper (atom, copy_func, free_func));
175 atom_info_list_free (GList * ai)
178 AtomInfo *info = (AtomInfo *) ai->data;
180 info->free_func (info->atom);
182 ai = g_list_delete_link (ai, ai);
187 atom_data_new (guint32 fourcc)
189 AtomData *data = g_new0 (AtomData, 1);
191 atom_header_set (&data->header, fourcc, 0, 0);
196 atom_data_alloc_mem (AtomData * data, guint32 size)
201 data->data = g_new0 (guint8, size);
202 data->datalen = size;
206 atom_data_new_from_gst_buffer (guint32 fourcc, const GstBuffer * buf)
208 AtomData *data = atom_data_new (fourcc);
210 atom_data_alloc_mem (data, GST_BUFFER_SIZE (buf));
211 g_memmove (data->data, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
216 atom_data_free (AtomData * data)
218 atom_clear (&data->header);
226 AtomUUID *uuid = g_new0 (AtomUUID, 1);
228 atom_header_set (&uuid->header, FOURCC_uuid, 0, 0);
233 atom_uuid_free (AtomUUID * data)
235 atom_clear (&data->header);
241 atom_ftyp_init (AtomFTYP * ftyp, guint32 major, guint32 version, GList * brands)
246 atom_header_set (&ftyp->header, FOURCC_ftyp, 16, 0);
247 ftyp->major_brand = major;
248 ftyp->version = version;
250 /* always include major brand as compatible brand */
251 ftyp->compatible_brands_size = g_list_length (brands) + 1;
252 ftyp->compatible_brands = g_new (guint32, ftyp->compatible_brands_size);
254 ftyp->compatible_brands[0] = major;
256 for (it = brands; it != NULL; it = g_list_next (it)) {
257 ftyp->compatible_brands[index++] = GPOINTER_TO_UINT (it->data);
262 atom_ftyp_new (AtomsContext * context, guint32 major, guint32 version,
265 AtomFTYP *ftyp = g_new0 (AtomFTYP, 1);
267 atom_ftyp_init (ftyp, major, version, brands);
272 atom_ftyp_free (AtomFTYP * ftyp)
274 atom_clear (&ftyp->header);
275 g_free (ftyp->compatible_brands);
276 ftyp->compatible_brands = NULL;
281 atom_esds_init (AtomESDS * esds)
283 guint8 flags[3] = { 0, 0, 0 };
285 atom_full_init (&esds->header, FOURCC_esds, 0, 0, 0, flags);
286 desc_es_init (&esds->es);
292 AtomESDS *esds = g_new0 (AtomESDS, 1);
294 atom_esds_init (esds);
299 atom_esds_free (AtomESDS * esds)
301 atom_full_clear (&esds->header);
302 desc_es_descriptor_clear (&esds->es);
309 AtomFRMA *frma = g_new0 (AtomFRMA, 1);
311 atom_header_set (&frma->header, FOURCC_frma, 0, 0);
316 atom_frma_free (AtomFRMA * frma)
318 atom_clear (&frma->header);
325 AtomWAVE *wave = g_new0 (AtomWAVE, 1);
327 atom_header_set (&wave->header, FOURCC_wave, 0, 0);
332 atom_wave_free (AtomWAVE * wave)
334 atom_clear (&wave->header);
335 atom_info_list_free (wave->extension_atoms);
340 atom_elst_init (AtomELST * elst)
342 guint8 flags[3] = { 0, 0, 0 };
343 atom_full_init (&elst->header, FOURCC_elst, 0, 0, 0, flags);
348 atom_elst_clear (AtomELST * elst)
352 atom_full_clear (&elst->header);
353 walker = elst->entries;
355 g_free ((EditListEntry *) walker->data);
356 walker = g_slist_next (walker);
358 g_slist_free (elst->entries);
362 atom_edts_init (AtomEDTS * edts)
364 atom_header_set (&edts->header, FOURCC_edts, 0, 0);
365 atom_elst_init (&edts->elst);
369 atom_edts_clear (AtomEDTS * edts)
371 atom_clear (&edts->header);
372 atom_elst_clear (&edts->elst);
378 AtomEDTS *edts = g_new0 (AtomEDTS, 1);
379 atom_edts_init (edts);
384 atom_edts_free (AtomEDTS * edts)
386 atom_edts_clear (edts);
391 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
393 atom_header_set (&se->header, type, 0, 0);
395 memset (se->reserved, 0, sizeof (guint8) * 6);
396 se->data_reference_index = 0;
400 atom_sample_entry_free (SampleTableEntry * se)
402 atom_clear (&se->header);
406 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
408 atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
411 mp4a->revision_level = 0;
414 mp4a->sample_size = 16;
415 mp4a->compression_id = 0;
416 mp4a->packet_size = 0;
417 mp4a->sample_rate = 0;
418 /* following only used if version is 1 */
419 mp4a->samples_per_packet = 0;
420 mp4a->bytes_per_packet = 0;
421 mp4a->bytes_per_frame = 0;
422 mp4a->bytes_per_sample = 0;
424 mp4a->extension_atoms = NULL;
427 static SampleTableEntryMP4A *
428 sample_entry_mp4a_new (void)
430 SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
432 sample_entry_mp4a_init (mp4a);
437 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
439 atom_sample_entry_free (&mp4a->se);
440 atom_info_list_free (mp4a->extension_atoms);
445 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
447 atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
450 mp4v->revision_level = 0;
453 mp4v->temporal_quality = 0;
454 mp4v->spatial_quality = 0;
456 /* qt and ISO base media do not contradict, and examples agree */
457 mp4v->horizontal_resolution = 0x00480000;
458 mp4v->vertical_resolution = 0x00480000;
461 mp4v->frame_count = 1;
463 memset (mp4v->compressor, 0, sizeof (guint8) * 32);
466 mp4v->color_table_id = 0;
468 mp4v->extension_atoms = NULL;
472 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
474 atom_sample_entry_free (&mp4v->se);
475 atom_info_list_free (mp4v->extension_atoms);
479 static SampleTableEntryMP4V *
480 sample_entry_mp4v_new (AtomsContext * context)
482 SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
484 sample_entry_mp4v_init (mp4v, context);
489 atom_stsd_init (AtomSTSD * stsd)
491 guint8 flags[3] = { 0, 0, 0 };
493 atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
494 stsd->entries = NULL;
499 atom_stsd_remove_entries (AtomSTSD * stsd)
503 walker = stsd->entries;
506 SampleTableEntry *se = (SampleTableEntry *) aux->data;
508 walker = g_list_next (walker);
509 stsd->entries = g_list_remove_link (stsd->entries, aux);
513 sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
516 sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
519 /* best possible cleanup */
520 atom_sample_entry_free (se);
528 atom_stsd_clear (AtomSTSD * stsd)
530 atom_stsd_remove_entries (stsd);
531 atom_full_clear (&stsd->header);
535 atom_ctts_init (AtomCTTS * ctts)
537 guint8 flags[3] = { 0, 0, 0 };
539 atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
540 atom_array_init (&ctts->entries, 128);
541 ctts->do_pts = FALSE;
547 AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
549 atom_ctts_init (ctts);
554 atom_ctts_free (AtomCTTS * ctts)
556 atom_full_clear (&ctts->header);
557 atom_array_clear (&ctts->entries);
562 atom_stts_init (AtomSTTS * stts)
564 guint8 flags[3] = { 0, 0, 0 };
566 atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
567 atom_array_init (&stts->entries, 512);
571 atom_stts_clear (AtomSTTS * stts)
573 atom_full_clear (&stts->header);
574 atom_array_clear (&stts->entries);
578 atom_stsz_init (AtomSTSZ * stsz)
580 guint8 flags[3] = { 0, 0, 0 };
582 atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
583 atom_array_init (&stsz->entries, 1024);
584 stsz->sample_size = 0;
585 stsz->table_size = 0;
589 atom_stsz_clear (AtomSTSZ * stsz)
591 atom_full_clear (&stsz->header);
592 atom_array_clear (&stsz->entries);
593 stsz->table_size = 0;
597 atom_stsc_init (AtomSTSC * stsc)
599 guint8 flags[3] = { 0, 0, 0 };
601 atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
602 atom_array_init (&stsc->entries, 128);
606 atom_stsc_clear (AtomSTSC * stsc)
608 atom_full_clear (&stsc->header);
609 atom_array_clear (&stsc->entries);
613 atom_co64_init (AtomSTCO64 * co64)
615 guint8 flags[3] = { 0, 0, 0 };
617 atom_full_init (&co64->header, FOURCC_stco, 0, 0, 0, flags);
618 atom_array_init (&co64->entries, 256);
622 atom_stco64_clear (AtomSTCO64 * stco64)
624 atom_full_clear (&stco64->header);
625 atom_array_clear (&stco64->entries);
629 atom_stss_init (AtomSTSS * stss)
631 guint8 flags[3] = { 0, 0, 0 };
633 atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
634 atom_array_init (&stss->entries, 128);
638 atom_stss_clear (AtomSTSS * stss)
640 atom_full_clear (&stss->header);
641 atom_array_clear (&stss->entries);
645 atom_stbl_init (AtomSTBL * stbl)
647 atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
649 atom_stts_init (&stbl->stts);
650 atom_stss_init (&stbl->stss);
651 atom_stsd_init (&stbl->stsd);
652 atom_stsz_init (&stbl->stsz);
653 atom_stsc_init (&stbl->stsc);
656 atom_co64_init (&stbl->stco64);
660 atom_stbl_clear (AtomSTBL * stbl)
662 atom_clear (&stbl->header);
663 atom_stsd_clear (&stbl->stsd);
664 atom_stts_clear (&stbl->stts);
665 atom_stss_clear (&stbl->stss);
666 atom_stsc_clear (&stbl->stsc);
667 atom_stsz_clear (&stbl->stsz);
669 atom_ctts_free (stbl->ctts);
671 atom_stco64_clear (&stbl->stco64);
675 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
677 guint8 flags[3] = { 0, 0, 1 };
679 atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
680 vmhd->graphics_mode = 0x0;
681 memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
683 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
684 vmhd->graphics_mode = 0x40;
685 vmhd->opcolor[0] = 32768;
686 vmhd->opcolor[1] = 32768;
687 vmhd->opcolor[2] = 32768;
692 atom_vmhd_new (AtomsContext * context)
694 AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
696 atom_vmhd_init (vmhd, context);
701 atom_vmhd_free (AtomVMHD * vmhd)
703 atom_full_clear (&vmhd->header);
708 atom_smhd_init (AtomSMHD * smhd)
710 guint8 flags[3] = { 0, 0, 0 };
712 atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
720 AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
722 atom_smhd_init (smhd);
727 atom_smhd_free (AtomSMHD * smhd)
729 atom_full_clear (&smhd->header);
734 atom_hmhd_free (AtomHMHD * hmhd)
736 atom_full_clear (&hmhd->header);
741 atom_hdlr_init (AtomHDLR * hdlr)
743 guint8 flags[3] = { 0, 0, 0 };
745 atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
747 hdlr->component_type = 0;
748 hdlr->handler_type = 0;
749 hdlr->manufacturer = 0;
751 hdlr->flags_mask = 0;
752 hdlr->name = g_strdup ("");
758 AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
760 atom_hdlr_init (hdlr);
765 atom_hdlr_clear (AtomHDLR * hdlr)
767 atom_full_clear (&hdlr->header);
775 atom_hdlr_free (AtomHDLR * hdlr)
777 atom_hdlr_clear (hdlr);
782 atom_url_init (AtomURL * url)
784 guint8 flags[3] = { 0, 0, 1 };
786 atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
787 url->location = NULL;
791 atom_url_free (AtomURL * url)
793 atom_full_clear (&url->header);
795 g_free (url->location);
796 url->location = NULL;
804 AtomURL *url = g_new0 (AtomURL, 1);
813 guint8 flags[3] = { 0, 0, 1 };
814 AtomFull *alis = g_new0 (AtomFull, 1);
816 atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
821 atom_dref_init (AtomDREF * dref, AtomsContext * context)
823 guint8 flags[3] = { 0, 0, 0 };
825 atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
827 /* in either case, alis or url init arranges to set self-contained flag */
828 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
829 /* alis dref for qt */
830 AtomFull *alis = atom_alis_new ();
831 dref->entries = g_list_append (dref->entries, alis);
833 /* url for iso spec, as 'alis' not specified there */
834 AtomURL *url = atom_url_new ();
835 dref->entries = g_list_append (dref->entries, url);
840 atom_dref_clear (AtomDREF * dref)
844 atom_full_clear (&dref->header);
845 walker = dref->entries;
848 Atom *atom = (Atom *) aux->data;
850 walker = g_list_next (walker);
851 dref->entries = g_list_remove_link (dref->entries, aux);
852 switch (atom->type) {
854 atom_full_free ((AtomFull *) atom);
857 atom_url_free ((AtomURL *) atom);
860 /* we do nothing, better leak than crash */
868 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
870 atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
871 atom_dref_init (&dinf->dref, context);
875 atom_dinf_clear (AtomDINF * dinf)
877 atom_clear (&dinf->header);
878 atom_dref_clear (&dinf->dref);
882 atom_minf_init (AtomMINF * minf, AtomsContext * context)
884 atom_header_set (&minf->header, FOURCC_minf, 0, 0);
890 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
891 minf->hdlr = atom_hdlr_new ();
892 minf->hdlr->component_type = FOURCC_dhlr;
893 minf->hdlr->handler_type = FOURCC_alis;
897 atom_dinf_init (&minf->dinf, context);
898 atom_stbl_init (&minf->stbl);
902 atom_minf_clear_handlers (AtomMINF * minf)
905 atom_vmhd_free (minf->vmhd);
909 atom_smhd_free (minf->smhd);
913 atom_hmhd_free (minf->hmhd);
919 atom_minf_clear (AtomMINF * minf)
921 atom_clear (&minf->header);
922 atom_minf_clear_handlers (minf);
924 atom_hdlr_free (minf->hdlr);
926 atom_dinf_clear (&minf->dinf);
927 atom_stbl_clear (&minf->stbl);
931 atom_mdhd_init (AtomMDHD * mdhd)
933 guint8 flags[3] = { 0, 0, 0 };
935 atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
936 common_time_info_init (&mdhd->time_info);
937 mdhd->language_code = 0;
942 atom_mdhd_clear (AtomMDHD * mdhd)
944 atom_full_clear (&mdhd->header);
948 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
950 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
952 atom_mdhd_init (&mdia->mdhd);
953 atom_hdlr_init (&mdia->hdlr);
954 atom_minf_init (&mdia->minf, context);
958 atom_mdia_clear (AtomMDIA * mdia)
960 atom_clear (&mdia->header);
961 atom_mdhd_clear (&mdia->mdhd);
962 atom_hdlr_clear (&mdia->hdlr);
963 atom_minf_clear (&mdia->minf);
967 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
972 * 2 -> track in movie
973 * 4 -> track in preview
975 guint8 flags[3] = { 0, 0, 7 };
977 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
979 tkhd->creation_time = tkhd->modification_time = get_current_qt_time ();
984 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
986 tkhd->alternate_group = 0;
989 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
990 tkhd->matrix[0] = 1 << 16;
991 tkhd->matrix[4] = 1 << 16;
992 tkhd->matrix[8] = 16384 << 16;
998 atom_tkhd_clear (AtomTKHD * tkhd)
1000 atom_full_clear (&tkhd->header);
1004 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
1006 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
1008 atom_tkhd_init (&trak->tkhd, context);
1010 atom_mdia_init (&trak->mdia, context);
1014 atom_trak_new (AtomsContext * context)
1016 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
1018 atom_trak_init (trak, context);
1023 atom_trak_clear (AtomTRAK * trak)
1025 atom_clear (&trak->header);
1026 atom_tkhd_clear (&trak->tkhd);
1028 atom_edts_free (trak->edts);
1029 atom_mdia_clear (&trak->mdia);
1033 atom_trak_free (AtomTRAK * trak)
1035 atom_trak_clear (trak);
1040 atom_ilst_init (AtomILST * ilst)
1042 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
1043 ilst->entries = NULL;
1047 atom_ilst_new (void)
1049 AtomILST *ilst = g_new0 (AtomILST, 1);
1051 atom_ilst_init (ilst);
1056 atom_ilst_free (AtomILST * ilst)
1059 atom_info_list_free (ilst->entries);
1060 atom_clear (&ilst->header);
1065 atom_meta_init (AtomMETA * meta)
1067 guint8 flags[3] = { 0, 0, 0 };
1069 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1070 atom_hdlr_init (&meta->hdlr);
1071 /* FIXME (ISOM says this is always 0) */
1072 meta->hdlr.component_type = FOURCC_mhlr;
1073 meta->hdlr.handler_type = FOURCC_mdir;
1078 atom_meta_new (void)
1080 AtomMETA *meta = g_new0 (AtomMETA, 1);
1082 atom_meta_init (meta);
1087 atom_meta_free (AtomMETA * meta)
1089 atom_full_clear (&meta->header);
1090 atom_hdlr_clear (&meta->hdlr);
1092 atom_ilst_free (meta->ilst);
1098 atom_udta_init (AtomUDTA * udta)
1100 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1105 atom_udta_new (void)
1107 AtomUDTA *udta = g_new0 (AtomUDTA, 1);
1109 atom_udta_init (udta);
1114 atom_udta_free (AtomUDTA * udta)
1116 atom_clear (&udta->header);
1118 atom_meta_free (udta->meta);
1121 atom_info_list_free (udta->entries);
1126 atom_tag_data_init (AtomTagData * data)
1128 guint8 flags[] = { 0, 0, 0 };
1130 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1134 atom_tag_data_clear (AtomTagData * data)
1136 atom_full_clear (&data->header);
1137 g_free (data->data);
1142 * Fourcc is the tag fourcc
1143 * flags will be truncated to 24bits
1146 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1148 AtomTag *tag = g_new0 (AtomTag, 1);
1150 tag->header.type = fourcc;
1151 atom_tag_data_init (&tag->data);
1152 atom_full_set_flags_as_uint (&tag->data.header, flags_as_uint);
1157 atom_tag_free (AtomTag * tag)
1159 atom_clear (&tag->header);
1160 atom_tag_data_clear (&tag->data);
1165 atom_mvhd_init (AtomMVHD * mvhd)
1167 guint8 flags[3] = { 0, 0, 0 };
1169 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1171 common_time_info_init (&mvhd->time_info);
1173 mvhd->prefered_rate = 1 << 16;
1174 mvhd->volume = 1 << 8;
1175 mvhd->reserved3 = 0;
1176 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1178 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1179 mvhd->matrix[0] = 1 << 16;
1180 mvhd->matrix[4] = 1 << 16;
1181 mvhd->matrix[8] = 16384 << 16;
1183 mvhd->preview_time = 0;
1184 mvhd->preview_duration = 0;
1185 mvhd->poster_time = 0;
1186 mvhd->selection_time = 0;
1187 mvhd->selection_duration = 0;
1188 mvhd->current_time = 0;
1190 mvhd->next_track_id = 1;
1194 atom_mvhd_clear (AtomMVHD * mvhd)
1196 atom_full_clear (&mvhd->header);
1200 atom_mehd_init (AtomMEHD * mehd)
1202 guint8 flags[3] = { 0, 0, 0 };
1204 atom_full_init (&mehd->header, FOURCC_mehd, 0, 0, 1, flags);
1205 mehd->fragment_duration = 0;
1209 atom_mvex_init (AtomMVEX * mvex)
1211 atom_header_set (&mvex->header, FOURCC_mvex, 0, 0);
1212 atom_mehd_init (&mvex->mehd);
1217 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1219 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1220 atom_mvhd_init (&(moov->mvhd));
1221 atom_mvex_init (&(moov->mvex));
1224 moov->context = *context;
1228 atom_moov_new (AtomsContext * context)
1230 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1232 atom_moov_init (moov, context);
1237 atom_trex_free (AtomTREX * trex)
1239 atom_full_clear (&trex->header);
1244 atom_mvex_clear (AtomMVEX * mvex)
1248 atom_clear (&mvex->header);
1249 walker = mvex->trexs;
1251 atom_trex_free ((AtomTREX *) walker->data);
1252 walker = g_list_next (walker);
1254 g_list_free (mvex->trexs);
1259 atom_moov_free (AtomMOOV * moov)
1263 atom_clear (&moov->header);
1264 atom_mvhd_clear (&moov->mvhd);
1266 walker = moov->traks;
1268 atom_trak_free ((AtomTRAK *) walker->data);
1269 walker = g_list_next (walker);
1271 g_list_free (moov->traks);
1275 atom_udta_free (moov->udta);
1279 atom_mvex_clear (&moov->mvex);
1284 /* -- end of init / free -- */
1286 /* -- copy data functions -- */
1289 atom_full_get_version (AtomFull * full)
1291 return full->version;
1295 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1296 guint8 ** buffer, guint64 * size, guint64 * offset)
1298 guint64 original_offset = *offset;
1301 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1302 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1303 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1304 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1306 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1307 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1308 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1309 prop_copy_uint64 (ti->duration, buffer, size, offset);
1311 return *offset - original_offset;
1315 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1318 /* this only works for non-extended atom size, which is OK
1319 * (though it could be made to do mem_move, etc and write extended size) */
1320 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1324 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1326 guint64 original_offset = *offset;
1328 /* copies type and size */
1329 prop_copy_uint32 (atom->size, buffer, size, offset);
1330 prop_copy_fourcc (atom->type, buffer, size, offset);
1332 /* extended size needed */
1333 if (atom->size == 1) {
1334 /* really should not happen other than with mdat atom;
1335 * would be a problem for size (re)write code, not to mention memory */
1336 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1337 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1340 return *offset - original_offset;
1344 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1347 guint64 original_offset = *offset;
1349 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1353 prop_copy_uint8 (atom->version, buffer, size, offset);
1354 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1356 atom_write_size (buffer, size, offset, original_offset);
1357 return *offset - original_offset;
1361 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1364 guint64 original_offset = *offset;
1367 AtomInfo *info = (AtomInfo *) ai->data;
1369 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1372 ai = g_list_next (ai);
1375 return *offset - original_offset;
1379 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1382 guint64 original_offset = *offset;
1384 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1388 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1390 atom_write_size (buffer, size, offset, original_offset);
1391 return *offset - original_offset;
1395 atom_uuid_copy_data (AtomUUID * uuid, guint8 ** buffer, guint64 * size,
1398 guint64 original_offset = *offset;
1400 if (!atom_copy_data (&uuid->header, buffer, size, offset)) {
1403 prop_copy_uint8_array (uuid->uuid, 16, buffer, size, offset);
1405 prop_copy_uint8_array (uuid->data, uuid->datalen, buffer, size, offset);
1407 atom_write_size (buffer, size, offset, original_offset);
1408 return *offset - original_offset;
1412 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1415 guint64 original_offset = *offset;
1417 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1420 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1421 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1423 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1424 buffer, size, offset);
1426 atom_write_size (buffer, size, offset, original_offset);
1427 return *offset - original_offset;
1431 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1435 guint64 original_offset = *offset;
1437 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1441 version = atom_full_get_version (&(atom->header));
1443 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1444 } else if (version == 1) {
1445 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1447 *offset = original_offset;
1451 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1452 prop_copy_uint16 (atom->volume, buffer, size, offset);
1453 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1454 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1455 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1456 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1457 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1458 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1459 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1460 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1461 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1463 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1465 atom_write_size (buffer, size, offset, original_offset);
1466 return *offset - original_offset;
1470 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1473 guint64 original_offset = *offset;
1475 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1479 if (atom_full_get_version (&tkhd->header) == 0) {
1480 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1481 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1482 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1483 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1484 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1486 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1487 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1488 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1489 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1490 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1493 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1494 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1495 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1496 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1497 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1498 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1500 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1501 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1503 atom_write_size (buffer, size, offset, original_offset);
1504 return *offset - original_offset;
1508 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1511 guint64 original_offset = *offset;
1513 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1517 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1518 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1519 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1520 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1521 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1523 prop_copy_size_string ((guint8 *) hdlr->name, strlen (hdlr->name), buffer,
1526 atom_write_size (buffer, size, offset, original_offset);
1527 return *offset - original_offset;
1531 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1534 guint64 original_offset = *offset;
1536 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1539 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1540 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1542 atom_write_size (buffer, size, offset, original_offset);
1543 return original_offset - *offset;
1547 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1550 guint64 original_offset = *offset;
1552 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1555 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1556 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1558 atom_write_size (buffer, size, offset, original_offset);
1559 return original_offset - *offset;
1563 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1566 guint64 original_offset = *offset;
1568 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1571 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1572 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1573 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1574 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1575 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1577 atom_write_size (buffer, size, offset, original_offset);
1578 return original_offset - *offset;
1582 atom_url_same_file_flag (AtomURL * url)
1584 return (url->header.flags[2] & 0x1) == 1;
1588 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1591 guint64 original_offset = *offset;
1593 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1597 if (!atom_url_same_file_flag (url)) {
1598 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1601 atom_write_size (buffer, size, offset, original_offset);
1602 return original_offset - *offset;
1606 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1609 guint64 original_offset = *offset;
1612 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1616 prop_copy_uint32 (atom_array_get_len (&stts->entries), buffer, size, offset);
1617 /* minimize realloc */
1618 prop_copy_ensure_buffer (buffer, size, offset,
1619 8 * atom_array_get_len (&stts->entries));
1620 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
1621 STTSEntry *entry = &atom_array_index (&stts->entries, i);
1623 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
1624 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
1627 atom_write_size (buffer, size, offset, original_offset);
1628 return *offset - original_offset;
1632 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
1633 guint64 * size, guint64 * offset)
1635 guint64 original_offset = *offset;
1637 if (!atom_copy_data (&se->header, buffer, size, offset)) {
1641 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
1642 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
1644 return *offset - original_offset;
1648 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
1651 guint64 original_offset = *offset;
1653 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
1656 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
1660 atom_write_size (buffer, size, offset, original_offset);
1661 return *offset - original_offset;
1665 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
1666 guint64 * size, guint64 * offset)
1668 guint64 original_offset = *offset;
1670 if (!atom_copy_data (&(frma->header), buffer, size, offset))
1673 prop_copy_fourcc (frma->media_type, buffer, size, offset);
1675 atom_write_size (buffer, size, offset, original_offset);
1676 return *offset - original_offset;
1680 atom_mp4s_copy_data (SampleTableEntryMP4S * mp4s, guint8 ** buffer,
1681 guint64 * size, guint64 * offset)
1683 guint64 original_offset = *offset;
1685 if (!atom_sample_entry_copy_data (&mp4s->se, buffer, size, offset)) {
1688 if (!atom_esds_copy_data (&mp4s->es, buffer, size, offset)) {
1692 atom_write_size (buffer, size, offset, original_offset);
1693 return *offset - original_offset;
1697 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
1698 guint64 * size, guint64 * offset)
1700 guint64 original_offset = *offset;
1702 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
1706 prop_copy_uint32 (hse->size, buffer, size, offset);
1707 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
1709 atom_write_size (buffer, size, offset, original_offset);
1710 return *offset - original_offset;
1714 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
1715 guint64 * size, guint64 * offset)
1717 guint64 original_offset = *offset;
1719 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
1723 prop_copy_uint16 (mp4a->version, buffer, size, offset);
1724 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
1725 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
1726 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
1727 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
1728 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
1729 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
1730 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
1732 /* this should always be 0 for mp4 flavor */
1733 if (mp4a->version == 1) {
1734 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
1735 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
1736 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
1737 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
1740 if (mp4a->extension_atoms) {
1741 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
1745 atom_write_size (buffer, size, offset, original_offset);
1746 return *offset - original_offset;
1750 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
1751 guint64 * size, guint64 * offset)
1753 guint64 original_offset = *offset;
1755 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
1759 prop_copy_uint16 (mp4v->version, buffer, size, offset);
1760 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
1761 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
1762 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
1763 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
1765 prop_copy_uint16 (mp4v->width, buffer, size, offset);
1766 prop_copy_uint16 (mp4v->height, buffer, size, offset);
1768 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
1769 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
1770 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
1772 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
1774 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
1777 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
1778 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
1781 if (mp4v->extension_atoms &&
1782 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
1785 atom_write_size (buffer, size, offset, original_offset);
1786 return *offset - original_offset;
1790 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
1793 guint64 original_offset = *offset;
1796 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
1800 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
1801 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
1802 if (stsz->sample_size == 0) {
1803 /* minimize realloc */
1804 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
1805 /* entry count must match sample count */
1806 g_assert (atom_array_get_len (&stsz->entries) == stsz->table_size);
1807 for (i = 0; i < atom_array_get_len (&stsz->entries); i++) {
1808 prop_copy_uint32 (atom_array_index (&stsz->entries, i), buffer, size,
1813 atom_write_size (buffer, size, offset, original_offset);
1814 return *offset - original_offset;
1818 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
1821 guint64 original_offset = *offset;
1824 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
1828 prop_copy_uint32 (atom_array_get_len (&stsc->entries), buffer, size, offset);
1829 /* minimize realloc */
1830 prop_copy_ensure_buffer (buffer, size, offset,
1831 12 * atom_array_get_len (&stsc->entries));
1833 for (i = 0; i < atom_array_get_len (&stsc->entries); i++) {
1834 STSCEntry *entry = &atom_array_index (&stsc->entries, i);
1836 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
1837 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
1838 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
1841 atom_write_size (buffer, size, offset, original_offset);
1842 return *offset - original_offset;
1846 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
1849 guint64 original_offset = *offset;
1852 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
1856 prop_copy_uint32 (atom_array_get_len (&ctts->entries), buffer, size, offset);
1857 /* minimize realloc */
1858 prop_copy_ensure_buffer (buffer, size, offset,
1859 8 * atom_array_get_len (&ctts->entries));
1860 for (i = 0; i < atom_array_get_len (&ctts->entries); i++) {
1861 CTTSEntry *entry = &atom_array_index (&ctts->entries, i);
1863 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
1864 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
1867 atom_write_size (buffer, size, offset, original_offset);
1868 return *offset - original_offset;
1872 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
1875 guint64 original_offset = *offset;
1877 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
1879 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
1883 prop_copy_uint32 (atom_array_get_len (&stco64->entries), buffer, size,
1886 /* minimize realloc */
1887 prop_copy_ensure_buffer (buffer, size, offset,
1888 8 * atom_array_get_len (&stco64->entries));
1889 for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
1890 guint64 *value = &atom_array_index (&stco64->entries, i);
1893 prop_copy_uint32 ((guint32) * value, buffer, size, offset);
1895 prop_copy_uint64 (*value, buffer, size, offset);
1899 atom_write_size (buffer, size, offset, original_offset);
1900 return *offset - original_offset;
1904 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
1907 guint64 original_offset = *offset;
1910 if (atom_array_get_len (&stss->entries) == 0) {
1911 /* FIXME not needing this atom might be confused with error while copying */
1915 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
1919 prop_copy_uint32 (atom_array_get_len (&stss->entries), buffer, size, offset);
1920 /* minimize realloc */
1921 prop_copy_ensure_buffer (buffer, size, offset,
1922 4 * atom_array_get_len (&stss->entries));
1923 for (i = 0; i < atom_array_get_len (&stss->entries); i++) {
1924 prop_copy_uint32 (atom_array_index (&stss->entries, i), buffer, size,
1928 atom_write_size (buffer, size, offset, original_offset);
1929 return *offset - original_offset;
1933 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
1936 guint64 original_offset = *offset;
1939 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
1943 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
1945 for (walker = g_list_last (stsd->entries); walker != NULL;
1946 walker = g_list_previous (walker)) {
1947 SampleTableEntry *se = (SampleTableEntry *) walker->data;
1949 switch (((Atom *) walker->data)->type) {
1951 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
1952 buffer, size, offset)) {
1957 if (!atom_mp4s_copy_data ((SampleTableEntryMP4S *) walker->data,
1958 buffer, size, offset)) {
1963 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
1964 buffer, size, offset)) {
1969 if (se->kind == VIDEO) {
1970 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
1971 walker->data, buffer, size, offset)) {
1974 } else if (se->kind == AUDIO) {
1975 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
1976 walker->data, buffer, size, offset)) {
1980 if (!atom_hint_sample_entry_copy_data (
1981 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
1989 atom_write_size (buffer, size, offset, original_offset);
1990 return *offset - original_offset;
1994 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
1997 guint64 original_offset = *offset;
1999 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
2003 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
2006 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
2009 /* this atom is optional, so let's check if we need it
2010 * (to avoid false error) */
2011 if (atom_array_get_len (&stbl->stss.entries)) {
2012 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
2017 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
2020 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
2023 if (stbl->ctts && stbl->ctts->do_pts) {
2024 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
2028 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
2032 atom_write_size (buffer, size, offset, original_offset);
2033 return original_offset - *offset;
2038 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
2041 guint64 original_offset = *offset;
2044 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
2048 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
2050 walker = dref->entries;
2051 while (walker != NULL) {
2052 Atom *atom = (Atom *) walker->data;
2054 if (atom->type == FOURCC_url_) {
2055 atom_url_copy_data ((AtomURL *) atom, buffer, size, offset);
2056 } else if (atom->type == FOURCC_alis) {
2057 atom_full_copy_data ((AtomFull *) atom, buffer, size, offset);
2059 g_error ("Unsupported atom used inside dref atom");
2061 walker = g_list_next (walker);
2064 atom_write_size (buffer, size, offset, original_offset);
2065 return *offset - original_offset;
2069 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
2072 guint64 original_offset = *offset;
2074 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
2078 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
2082 atom_write_size (buffer, size, offset, original_offset);
2083 return original_offset - *offset;
2087 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
2090 guint64 original_offset = *offset;
2092 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
2097 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
2100 } else if (minf->smhd) {
2101 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
2104 } else if (minf->hmhd) {
2105 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
2111 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2116 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2119 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2123 atom_write_size (buffer, size, offset, original_offset);
2124 return *offset - original_offset;
2128 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2131 guint64 original_offset = *offset;
2133 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2137 if (!common_time_info_copy_data (&mdhd->time_info,
2138 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2142 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2143 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2145 atom_write_size (buffer, size, offset, original_offset);
2146 return *offset - original_offset;
2150 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2153 guint64 original_offset = *offset;
2155 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2158 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2161 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2165 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2169 atom_write_size (buffer, size, offset, original_offset);
2170 return *offset - original_offset;
2174 atom_elst_copy_data (AtomELST * elst, guint8 ** buffer, guint64 * size,
2177 guint64 original_offset = *offset;
2180 if (!atom_full_copy_data (&elst->header, buffer, size, offset)) {
2184 prop_copy_uint32 (g_slist_length (elst->entries), buffer, size, offset);
2186 for (walker = elst->entries; walker != NULL; walker = g_slist_next (walker)) {
2187 EditListEntry *entry = (EditListEntry *) walker->data;
2188 prop_copy_uint32 (entry->duration, buffer, size, offset);
2189 prop_copy_uint32 (entry->media_time, buffer, size, offset);
2190 prop_copy_uint32 (entry->media_rate, buffer, size, offset);
2192 atom_write_size (buffer, size, offset, original_offset);
2193 return *offset - original_offset;
2197 atom_edts_copy_data (AtomEDTS * edts, guint8 ** buffer, guint64 * size,
2200 guint64 original_offset = *offset;
2202 if (!atom_copy_data (&(edts->header), buffer, size, offset))
2205 if (!atom_elst_copy_data (&(edts->elst), buffer, size, offset))
2208 atom_write_size (buffer, size, offset, original_offset);
2209 return *offset - original_offset;
2213 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2216 guint64 original_offset = *offset;
2218 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2221 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2225 if (!atom_edts_copy_data (trak->edts, buffer, size, offset)) {
2230 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2234 atom_write_size (buffer, size, offset, original_offset);
2235 return *offset - original_offset;
2239 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2242 guint64 original_offset = *offset;
2244 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2248 prop_copy_uint32 (data->reserved, buffer, size, offset);
2249 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2251 atom_write_size (buffer, size, offset, original_offset);
2252 return *offset - original_offset;
2256 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2259 guint64 original_offset = *offset;
2261 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2265 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2269 atom_write_size (buffer, size, offset, original_offset);
2270 return *offset - original_offset;
2274 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2277 guint64 original_offset = *offset;
2279 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2283 if (ilst->entries &&
2284 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2287 atom_write_size (buffer, size, offset, original_offset);
2288 return *offset - original_offset;
2292 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2295 guint64 original_offset = *offset;
2297 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2300 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2304 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2309 atom_write_size (buffer, size, offset, original_offset);
2310 return *offset - original_offset;
2314 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2317 guint64 original_offset = *offset;
2319 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2323 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2327 if (udta->entries) {
2329 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2333 atom_write_size (buffer, size, offset, original_offset);
2334 return *offset - original_offset;
2338 atom_mehd_copy_data (AtomMEHD * mehd, guint8 ** buffer, guint64 * size,
2341 guint64 original_offset = *offset;
2343 if (!atom_full_copy_data (&mehd->header, buffer, size, offset)) {
2347 prop_copy_uint64 (mehd->fragment_duration, buffer, size, offset);
2349 atom_write_size (buffer, size, offset, original_offset);
2350 return *offset - original_offset;
2354 atom_trex_copy_data (AtomTREX * trex, guint8 ** buffer, guint64 * size,
2357 guint64 original_offset = *offset;
2359 if (!atom_full_copy_data (&trex->header, buffer, size, offset)) {
2363 prop_copy_uint32 (trex->track_ID, buffer, size, offset);
2364 prop_copy_uint32 (trex->default_sample_description_index, buffer, size,
2366 prop_copy_uint32 (trex->default_sample_duration, buffer, size, offset);
2367 prop_copy_uint32 (trex->default_sample_size, buffer, size, offset);
2368 prop_copy_uint32 (trex->default_sample_flags, buffer, size, offset);
2370 atom_write_size (buffer, size, offset, original_offset);
2371 return *offset - original_offset;
2375 atom_mvex_copy_data (AtomMVEX * mvex, guint8 ** buffer, guint64 * size,
2378 guint64 original_offset = *offset;
2381 if (!atom_copy_data (&mvex->header, buffer, size, offset)) {
2385 if (!atom_mehd_copy_data (&mvex->mehd, buffer, size, offset)) {
2389 walker = g_list_first (mvex->trexs);
2390 while (walker != NULL) {
2391 if (!atom_trex_copy_data ((AtomTREX *) walker->data, buffer, size, offset)) {
2394 walker = g_list_next (walker);
2397 atom_write_size (buffer, size, offset, original_offset);
2398 return *offset - original_offset;
2402 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2405 guint64 original_offset = *offset;
2408 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2411 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2414 walker = g_list_first (atom->traks);
2415 while (walker != NULL) {
2416 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2419 walker = g_list_next (walker);
2423 if (!atom_udta_copy_data (atom->udta, buffer, size, offset)) {
2428 if (atom->fragmented) {
2429 if (!atom_mvex_copy_data (&atom->mvex, buffer, size, offset)) {
2434 atom_write_size (buffer, size, offset, original_offset);
2435 return *offset - original_offset;
2439 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
2440 guint64 * size, guint64 * offset)
2442 guint64 original_offset = *offset;
2444 if (!atom_copy_data (&(wave->header), buffer, size, offset))
2447 if (wave->extension_atoms) {
2448 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
2452 atom_write_size (buffer, size, offset, original_offset);
2453 return *offset - original_offset;
2456 /* -- end of copy data functions -- */
2458 /* -- general functions, API and support functions */
2460 /* add samples to tables */
2463 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
2468 if ((len = atom_array_get_len (&stsc->entries)) &&
2469 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
2473 nentry.first_chunk = first_chunk;
2474 nentry.samples_per_chunk = nsamples;
2475 nentry.sample_description_index = 1;
2476 atom_array_append (&stsc->entries, nentry, 128);
2480 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
2482 STTSEntry *entry = NULL;
2484 if (G_LIKELY (atom_array_get_len (&stts->entries) != 0))
2485 entry = &atom_array_index (&stts->entries,
2486 atom_array_get_len (&stts->entries) - 1);
2488 if (entry && entry->sample_delta == sample_delta) {
2489 entry->sample_count += sample_count;
2493 nentry.sample_count = sample_count;
2494 nentry.sample_delta = sample_delta;
2495 atom_array_append (&stts->entries, nentry, 256);
2500 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
2504 stsz->table_size += nsamples;
2505 if (stsz->sample_size != 0) {
2506 /* it is constant size, we don't need entries */
2509 for (i = 0; i < nsamples; i++) {
2510 atom_array_append (&stsz->entries, size, 1024);
2515 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
2517 return atom_array_get_len (&stco64->entries);
2521 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
2523 atom_array_append (&stco64->entries, entry, 256);
2524 if (entry > G_MAXUINT32)
2525 stco64->header.header.type = FOURCC_co64;
2529 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
2531 atom_array_append (&stss->entries, sample, 512);
2535 atom_stbl_add_stss_entry (AtomSTBL * stbl)
2537 guint32 sample_index = stbl->stsz.table_size;
2539 atom_stss_add_entry (&stbl->stss, sample_index);
2543 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
2545 CTTSEntry *entry = NULL;
2547 if (G_LIKELY (atom_array_get_len (&ctts->entries) != 0))
2548 entry = &atom_array_index (&ctts->entries,
2549 atom_array_get_len (&ctts->entries) - 1);
2551 if (entry == NULL || entry->sampleoffset != offset) {
2554 nentry.samplecount = nsamples;
2555 nentry.sampleoffset = offset;
2556 atom_array_append (&ctts->entries, nentry, 256);
2558 ctts->do_pts = TRUE;
2560 entry->samplecount += nsamples;
2565 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
2567 if (stbl->ctts == NULL) {
2568 stbl->ctts = atom_ctts_new ();
2570 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
2574 atom_stbl_add_samples (AtomSTBL * stbl, guint32 nsamples, guint32 delta,
2575 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
2577 atom_stts_add_entry (&stbl->stts, nsamples, delta);
2578 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
2579 atom_stco64_add_entry (&stbl->stco64, chunk_offset);
2580 atom_stsc_add_new_entry (&stbl->stsc,
2581 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
2583 atom_stbl_add_stss_entry (stbl);
2584 /* always store to arrange for consistent content */
2585 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
2589 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
2590 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
2592 AtomSTBL *stbl = &trak->mdia.minf.stbl;
2593 atom_stbl_add_samples (stbl, nsamples, delta, size, chunk_offset, sync,
2597 /* trak and moov molding */
2600 atom_trak_get_timescale (AtomTRAK * trak)
2602 return trak->mdia.mdhd.time_info.timescale;
2606 atom_trak_get_id (AtomTRAK * trak)
2608 return trak->tkhd.track_ID;
2612 atom_trak_set_id (AtomTRAK * trak, guint32 id)
2614 trak->tkhd.track_ID = id;
2618 atom_moov_add_trex (AtomMOOV * moov, AtomTREX * trex)
2620 moov->mvex.trexs = g_list_append (moov->mvex.trexs, trex);
2624 atom_trex_new (AtomTRAK * trak)
2626 guint8 flags[3] = { 0, 0, 0 };
2627 AtomTREX *trex = g_new0 (AtomTREX, 1);
2629 atom_full_init (&trex->header, FOURCC_trex, 0, 0, 0, flags);
2631 trex->track_ID = trak->tkhd.track_ID;
2632 trex->default_sample_description_index = 1;
2633 trex->default_sample_duration = 0;
2634 trex->default_sample_size = 0;
2635 trex->default_sample_flags = 0;
2641 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
2643 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
2644 moov->traks = g_list_append (moov->traks, trak);
2645 /* additional trak means also new trex */
2646 atom_moov_add_trex (moov, atom_trex_new (trak));
2650 atom_trak_get_duration (AtomTRAK * trak)
2652 return trak->tkhd.duration;
2656 atom_stts_get_total_duration (AtomSTTS * stts)
2661 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
2662 STTSEntry *entry = &atom_array_index (&stts->entries, i);
2664 sum += (guint64) (entry->sample_count) * entry->sample_delta;
2670 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
2672 trak->mdia.mdhd.time_info.duration =
2673 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
2674 if (trak->mdia.mdhd.time_info.timescale != 0) {
2675 trak->tkhd.duration =
2676 gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration,
2677 moov_timescale, trak->mdia.mdhd.time_info.timescale);
2679 trak->tkhd.duration = 0;
2684 atom_moov_get_timescale (AtomMOOV * moov)
2686 return moov->mvhd.time_info.timescale;
2690 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
2692 moov->mvhd.time_info.timescale = timescale;
2696 atom_moov_update_duration (AtomMOOV * moov)
2698 GList *traks = moov->traks;
2699 guint64 dur, duration = 0;
2702 AtomTRAK *trak = (AtomTRAK *) traks->data;
2704 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
2705 dur = atom_trak_get_duration (trak);
2708 traks = g_list_next (traks);
2710 moov->mvhd.time_info.duration = duration;
2711 moov->mvex.mehd.fragment_duration = duration;
2715 atom_moov_set_fragmented (AtomMOOV * moov, gboolean fragmented)
2717 moov->fragmented = fragmented;
2721 atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset)
2725 for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
2726 guint64 *value = &atom_array_index (&stco64->entries, i);
2733 atom_moov_chunks_add_offset (AtomMOOV * moov, guint32 offset)
2735 GList *traks = moov->traks;
2738 AtomTRAK *trak = (AtomTRAK *) traks->data;
2740 atom_stco64_chunks_add_offset (&trak->mdia.minf.stbl.stco64, offset);
2741 traks = g_list_next (traks);
2746 atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
2747 guint32 max_bitrate)
2749 AtomESDS *esds = NULL;
2750 AtomData *btrt = NULL;
2751 AtomWAVE *wave = NULL;
2754 GList *extensioniter = NULL;
2756 g_return_if_fail (trak != NULL);
2758 if (avg_bitrate == 0 && max_bitrate == 0)
2761 stsd = &trak->mdia.minf.stbl.stsd;
2762 for (iter = stsd->entries; iter; iter = g_list_next (iter)) {
2763 SampleTableEntry *entry = iter->data;
2765 switch (entry->kind) {
2767 SampleTableEntryMP4A *audioentry = (SampleTableEntryMP4A *) entry;
2768 extensioniter = audioentry->extension_atoms;
2772 SampleTableEntryMP4V *videoentry = (SampleTableEntryMP4V *) entry;
2773 extensioniter = videoentry->extension_atoms;
2781 for (; extensioniter; extensioniter = g_list_next (extensioniter)) {
2782 AtomInfo *atominfo = extensioniter->data;
2783 if (atominfo->atom->type == FOURCC_esds) {
2784 esds = (AtomESDS *) atominfo->atom;
2785 } else if (atominfo->atom->type == FOURCC_btrt) {
2786 btrt = (AtomData *) atominfo->atom;
2787 } else if (atominfo->atom->type == FOURCC_wave) {
2788 wave = (AtomWAVE *) atominfo->atom;
2792 /* wave might have an esds internally */
2794 for (extensioniter = wave->extension_atoms; extensioniter;
2795 extensioniter = g_list_next (extensioniter)) {
2796 AtomInfo *atominfo = extensioniter->data;
2797 if (atominfo->atom->type == FOURCC_esds) {
2798 esds = (AtomESDS *) atominfo->atom;
2805 if (avg_bitrate && esds->es.dec_conf_desc.avg_bitrate == 0)
2806 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
2807 if (max_bitrate && esds->es.dec_conf_desc.max_bitrate == 0)
2808 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
2811 /* type(4bytes) + size(4bytes) + buffersize(4bytes) +
2812 * maxbitrate(bytes) + avgbitrate(bytes) */
2813 if (max_bitrate && GST_READ_UINT32_BE (btrt->data + 4) == 0)
2814 GST_WRITE_UINT32_BE (btrt->data + 4, max_bitrate);
2815 if (avg_bitrate && GST_READ_UINT32_BE (btrt->data + 8) == 0)
2816 GST_WRITE_UINT32_BE (btrt->data + 8, avg_bitrate);
2821 * Meta tags functions
2824 atom_moov_init_metatags (AtomMOOV * moov, AtomsContext * context)
2827 moov->udta = atom_udta_new ();
2829 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
2830 if (!moov->udta->meta) {
2831 moov->udta->meta = atom_meta_new ();
2833 if (!moov->udta->meta->ilst) {
2834 moov->udta->meta->ilst = atom_ilst_new ();
2840 atom_tag_data_alloc_data (AtomTagData * data, guint size)
2842 if (data->data != NULL) {
2843 g_free (data->data);
2845 data->data = g_new0 (guint8, size);
2846 data->datalen = size;
2850 atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
2854 atom_moov_init_metatags (moov, &moov->context);
2855 if (moov->udta->meta)
2856 entries = &moov->udta->meta->ilst->entries;
2858 entries = &moov->udta->entries;
2859 *entries = g_list_append (*entries, tag);
2863 atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2864 const guint8 * data, guint size)
2869 tag = atom_tag_new (fourcc, flags);
2871 atom_tag_data_alloc_data (tdata, size);
2872 g_memmove (tdata->data, data, size);
2874 atom_moov_append_tag (moov,
2875 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
2880 atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2882 gint len = strlen (value);
2885 atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
2889 atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2892 guint8 data[8] = { 0, };
2895 GST_WRITE_UINT16_BE (data, value);
2896 atom_moov_add_tag (moov, fourcc, flags, data, 2);
2898 GST_WRITE_UINT32_BE (data + 2, value);
2899 atom_moov_add_tag (moov, fourcc, flags, data, 8);
2904 atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
2906 AtomData *data_atom;
2914 /* blob is unparsed atom;
2915 * extract size and fourcc, and wrap remainder in data atom */
2916 len = GST_READ_UINT32_BE (data);
2917 fourcc = GST_READ_UINT32_LE (data + 4);
2921 buf = gst_buffer_new ();
2922 GST_BUFFER_SIZE (buf) = len - 8;
2923 GST_BUFFER_DATA (buf) = data + 8;
2925 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2926 gst_buffer_unref (buf);
2928 atom_moov_append_tag (moov,
2929 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2934 atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
2937 AtomData *data_atom;
2941 /* need full atom */
2942 buf = gst_buffer_new_and_alloc (size + 4);
2943 bdata = GST_BUFFER_DATA (buf);
2944 /* full atom: version and flags */
2945 GST_WRITE_UINT32_BE (bdata, 0);
2946 memcpy (bdata + 4, data, size);
2948 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2949 gst_buffer_unref (buf);
2951 atom_moov_append_tag (moov,
2952 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2957 language_code (const char *lang)
2959 g_return_val_if_fail (lang != NULL, 0);
2960 g_return_val_if_fail (strlen (lang) == 3, 0);
2962 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
2963 ((lang[2] - 0x60) & 0x1F);
2967 atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc,
2968 const gchar * value, gint16 ivalue)
2970 gint len = 0, size = 0;
2974 len = strlen (value);
2981 data = g_malloc (size + 3);
2982 /* language tag and null-terminated UTF-8 string */
2984 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
2985 /* include 0 terminator */
2986 memcpy (data + 2, value, len + 1);
2988 /* 16-bit unsigned int if standalone, otherwise 8-bit */
2991 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
2993 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
2998 atom_moov_add_3gp_tag (moov, fourcc, data, size);
3003 atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
3005 atom_moov_add_3gp_str_int_tag (moov, fourcc, value, -1);
3009 atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value)
3011 atom_moov_add_3gp_str_int_tag (moov, fourcc, NULL, value);
3015 atom_moov_add_xmp_tags (AtomMOOV * moov, GstBuffer * xmpbuffer)
3017 AtomData *data_atom = NULL;
3019 if (moov->context.flavor == ATOMS_TREE_FLAVOR_MOV) {
3021 data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
3022 atom_moov_init_metatags (moov, &moov->context);
3023 moov->udta->entries = g_list_append (moov->udta->entries,
3024 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3028 GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
3034 * Functions for specifying media types
3038 atom_minf_set_audio (AtomMINF * minf)
3040 atom_minf_clear_handlers (minf);
3041 minf->smhd = atom_smhd_new ();
3045 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
3047 atom_minf_clear_handlers (minf);
3048 minf->vmhd = atom_vmhd_new (context);
3052 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
3055 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3056 hdlr->component_type = comp_type;
3058 hdlr->handler_type = hdlr_type;
3062 atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
3065 g_free (hdlr->name);
3066 hdlr->name = g_strdup (name);
3070 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
3072 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3073 /* Some players (low-end hardware) check for this name, which is what
3074 * QuickTime itself sets */
3075 atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3079 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3081 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3082 /* Some players (low-end hardware) check for this name, which is what
3083 * QuickTime itself sets */
3084 atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3088 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3090 atom_mdia_set_hdlr_type_audio (mdia, context);
3091 atom_minf_set_audio (&mdia->minf);
3095 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3097 atom_mdia_set_hdlr_type_video (mdia, context);
3098 atom_minf_set_video (&mdia->minf, context);
3102 atom_tkhd_set_audio (AtomTKHD * tkhd)
3104 tkhd->volume = 0x0100;
3105 tkhd->width = tkhd->height = 0;
3109 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3114 /* qt and ISO base media do not contradict, and examples agree */
3115 tkhd->width = width;
3116 tkhd->height = height;
3120 atom_edts_add_entry (AtomEDTS * edts, EditListEntry * entry)
3122 edts->elst.entries = g_slist_append (edts->elst.entries, entry);
3126 * Adds a new entry to this trak edits list
3127 * duration is in the moov's timescale
3128 * media_time is the offset in the media time to start from (media's timescale)
3129 * rate is a 32 bits fixed-point
3132 atom_trak_add_elst_entry (AtomTRAK * trak, guint32 duration, guint32 media_time,
3135 EditListEntry *entry = g_new (EditListEntry, 1);
3137 entry->duration = duration;
3138 entry->media_time = media_time;
3139 entry->media_rate = rate;
3141 if (trak->edts == NULL) {
3142 trak->edts = atom_edts_new ();
3144 atom_edts_add_entry (trak->edts, entry);
3147 /* re-negotiation is prevented at top-level, so only 1 entry expected.
3148 * Quite some more care here and elsewhere may be needed to
3149 * support several entries */
3150 static SampleTableEntryMP4A *
3151 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
3154 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3155 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
3157 mp4a->se.header.type = type;
3158 mp4a->se.kind = AUDIO;
3159 mp4a->compression_id = -1;
3160 mp4a->se.data_reference_index = 1;
3162 stsd->entries = g_list_prepend (stsd->entries, mp4a);
3167 static SampleTableEntryMP4V *
3168 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
3171 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
3172 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3174 mp4v->se.header.type = type;
3175 mp4v->se.kind = VIDEO;
3176 mp4v->se.data_reference_index = 1;
3177 mp4v->horizontal_resolution = 72 << 16;
3178 mp4v->vertical_resolution = 72 << 16;
3179 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3180 mp4v->spatial_quality = 512;
3181 mp4v->temporal_quality = 512;
3184 stsd->entries = g_list_prepend (stsd->entries, mp4v);
3190 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
3192 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
3196 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
3198 atom_tkhd_set_audio (&trak->tkhd);
3199 atom_mdia_set_audio (&trak->mdia, context);
3203 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
3206 atom_tkhd_set_video (&trak->tkhd, context, width, height);
3207 atom_mdia_set_video (&trak->mdia, context);
3211 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
3214 atom_trak_set_audio (trak, context);
3215 trak->mdia.mdhd.time_info.timescale = rate;
3219 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
3220 guint32 rate, guint32 width, guint32 height)
3222 atom_trak_set_video (trak, context, width, height);
3223 trak->mdia.mdhd.time_info.timescale = rate;
3224 trak->tkhd.width = width << 16;
3225 trak->tkhd.height = height << 16;
3229 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
3230 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
3232 SampleTableEntryMP4A *ste;
3234 atom_trak_set_audio_commons (trak, context, scale);
3235 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3236 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
3238 trak->is_video = FALSE;
3239 trak->is_h264 = FALSE;
3241 ste->version = entry->version;
3242 ste->compression_id = entry->compression_id;
3243 ste->sample_size = entry->sample_size;
3244 ste->sample_rate = entry->sample_rate << 16;
3245 ste->channels = entry->channels;
3247 ste->samples_per_packet = entry->samples_per_packet;
3248 ste->bytes_per_sample = entry->bytes_per_sample;
3249 ste->bytes_per_packet = entry->bytes_per_packet;
3250 ste->bytes_per_frame = entry->bytes_per_frame;
3253 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
3255 /* 0 size means variable size */
3256 atom_trak_set_constant_size_samples (trak, sample_size);
3260 build_pasp_extension (AtomTRAK * trak, gint par_width, gint par_height)
3262 AtomData *atom_data;
3266 buf = gst_buffer_new_and_alloc (8);
3267 data = GST_BUFFER_DATA (buf);
3269 /* ihdr = image header box */
3270 GST_WRITE_UINT32_BE (data, par_width);
3271 GST_WRITE_UINT32_BE (data + 4, par_height);
3273 atom_data = atom_data_new_from_gst_buffer (FOURCC_pasp, buf);
3274 gst_buffer_unref (buf);
3276 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3281 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
3282 VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
3284 SampleTableEntryMP4V *ste;
3285 gint dwidth, dheight;
3286 gint par_n = 0, par_d = 0;
3288 if ((entry->par_n != 1 || entry->par_d != 1) &&
3289 (entry->par_n != entry->par_d)) {
3290 par_n = entry->par_n;
3291 par_d = entry->par_d;
3294 dwidth = entry->width;
3295 dheight = entry->height;
3296 /* ISO file spec says track header w/h indicates track's visual presentation
3297 * (so this together with pixels w/h implicitly defines PAR) */
3298 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
3299 if (par_n > par_d) {
3300 dwidth = entry->width * par_n / par_d;
3301 dheight = entry->height;
3303 dwidth = entry->width * par_n / par_d;
3304 dheight = entry->height;
3308 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
3309 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3310 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
3312 trak->is_video = TRUE;
3313 trak->is_h264 = (entry->fourcc == FOURCC_avc1);
3315 ste->version = entry->version;
3316 ste->width = entry->width;
3317 ste->height = entry->height;
3318 ste->depth = entry->depth;
3319 ste->color_table_id = entry->color_table_id;
3320 ste->frame_count = entry->frame_count;
3323 ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
3325 /* QT spec has a pasp extension atom in stsd that can hold PAR */
3326 if (par_n && (context->flavor == ATOMS_TREE_FLAVOR_MOV)) {
3327 ste->extension_atoms = g_list_append (ste->extension_atoms,
3328 build_pasp_extension (trak, par_n, par_d));
3333 atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
3335 guint8 flags[3] = { 0, 0, 0 };
3337 atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
3338 mfhd->sequence_number = sequence_number;
3342 atom_moof_init (AtomMOOF * moof, AtomsContext * context,
3343 guint32 sequence_number)
3345 atom_header_set (&moof->header, FOURCC_moof, 0, 0);
3346 atom_mfhd_init (&moof->mfhd, sequence_number);
3351 atom_moof_new (AtomsContext * context, guint32 sequence_number)
3353 AtomMOOF *moof = g_new0 (AtomMOOF, 1);
3355 atom_moof_init (moof, context, sequence_number);
3360 atom_trun_free (AtomTRUN * trun)
3362 atom_full_clear (&trun->header);
3363 atom_array_clear (&trun->entries);
3368 atom_sdtp_free (AtomSDTP * sdtp)
3370 atom_full_clear (&sdtp->header);
3371 atom_array_clear (&sdtp->entries);
3376 atom_traf_free (AtomTRAF * traf)
3380 walker = traf->truns;
3382 atom_trun_free ((AtomTRUN *) walker->data);
3383 walker = g_list_next (walker);
3385 g_list_free (traf->truns);
3388 walker = traf->sdtps;
3390 atom_sdtp_free ((AtomSDTP *) walker->data);
3391 walker = g_list_next (walker);
3393 g_list_free (traf->sdtps);
3400 atom_moof_free (AtomMOOF * moof)
3404 walker = moof->trafs;
3406 atom_traf_free ((AtomTRAF *) walker->data);
3407 walker = g_list_next (walker);
3409 g_list_free (moof->trafs);
3416 atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
3419 guint64 original_offset = *offset;
3421 if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
3425 prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
3427 atom_write_size (buffer, size, offset, original_offset);
3428 return *offset - original_offset;
3432 atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
3435 guint64 original_offset = *offset;
3438 if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
3442 prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
3444 flags = atom_full_get_flags_as_uint (&tfhd->header);
3446 if (flags & TF_BASE_DATA_OFFSET)
3447 prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
3448 if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
3449 prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
3450 if (flags & TF_DEFAULT_SAMPLE_DURATION)
3451 prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
3452 if (flags & TF_DEFAULT_SAMPLE_SIZE)
3453 prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
3454 if (flags & TF_DEFAULT_SAMPLE_FLAGS)
3455 prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
3457 atom_write_size (buffer, size, offset, original_offset);
3458 return *offset - original_offset;
3462 atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
3463 guint64 * offset, guint32 * data_offset)
3465 guint64 original_offset = *offset;
3468 flags = atom_full_get_flags_as_uint (&trun->header);
3470 /* if first trun in moof, forcibly add data_offset and record
3471 * where it must be written later on */
3472 if (data_offset && !*data_offset) {
3473 flags |= TR_DATA_OFFSET;
3475 flags &= ~TR_DATA_OFFSET;
3478 atom_full_set_flags_as_uint (&trun->header, flags);
3480 if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
3484 prop_copy_uint32 (trun->sample_count, buffer, size, offset);
3486 if (flags & TR_DATA_OFFSET) {
3487 *data_offset = *offset;
3488 prop_copy_int32 (trun->data_offset, buffer, size, offset);
3490 if (flags & TR_FIRST_SAMPLE_FLAGS)
3491 prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
3493 for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
3494 TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
3496 if (flags & TR_SAMPLE_DURATION)
3497 prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
3498 if (flags & TR_SAMPLE_SIZE)
3499 prop_copy_uint32 (entry->sample_size, buffer, size, offset);
3500 if (flags & TR_SAMPLE_FLAGS)
3501 prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
3502 if (flags & TR_COMPOSITION_TIME_OFFSETS)
3503 prop_copy_uint32 (entry->sample_composition_time_offset,
3504 buffer, size, offset);
3507 atom_write_size (buffer, size, offset, original_offset);
3508 return *offset - original_offset;
3512 atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
3515 guint64 original_offset = *offset;
3517 if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
3521 /* all entries at once */
3522 prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
3523 atom_array_get_len (&sdtp->entries), buffer, size, offset);
3525 atom_write_size (buffer, size, offset, original_offset);
3526 return *offset - original_offset;
3530 atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
3531 guint64 * offset, guint32 * data_offset)
3533 guint64 original_offset = *offset;
3536 if (!atom_copy_data (&traf->header, buffer, size, offset)) {
3539 if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
3543 walker = g_list_first (traf->truns);
3544 while (walker != NULL) {
3545 if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset,
3549 walker = g_list_next (walker);
3552 walker = g_list_first (traf->sdtps);
3553 while (walker != NULL) {
3554 if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
3557 walker = g_list_next (walker);
3560 atom_write_size (buffer, size, offset, original_offset);
3561 return *offset - original_offset;
3564 /* creates moof atom; metadata is written expecting actual buffer data
3565 * is in mdata directly after moof, and is consecutively written per trak */
3567 atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
3568 guint64 * size, guint64 * offset)
3570 guint64 original_offset = *offset;
3572 guint32 data_offset = 0;
3574 if (!atom_copy_data (&moof->header, buffer, size, offset))
3577 if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
3580 walker = g_list_first (moof->trafs);
3581 while (walker != NULL) {
3582 if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset,
3586 walker = g_list_next (walker);
3589 atom_write_size (buffer, size, offset, original_offset);
3591 if (*buffer && data_offset) {
3592 /* first trun needs a data-offset relative to moof start
3593 * = moof size + mdat prefix */
3594 GST_WRITE_UINT32_BE (*buffer + data_offset, *offset - original_offset + 8);
3597 return *offset - original_offset;
3601 atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
3603 guint8 flags[3] = { 0, 0, 0 };
3605 atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
3606 tfhd->track_ID = track_ID;
3607 tfhd->base_data_offset = 0;
3608 tfhd->sample_description_index = 1;
3609 tfhd->default_sample_duration = 0;
3610 tfhd->default_sample_size = 0;
3611 tfhd->default_sample_flags = 0;
3615 atom_trun_init (AtomTRUN * trun)
3617 guint8 flags[3] = { 0, 0, 0 };
3619 atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
3620 trun->sample_count = 0;
3621 trun->data_offset = 0;
3622 trun->first_sample_flags = 0;
3623 atom_array_init (&trun->entries, 512);
3627 atom_trun_new (void)
3629 AtomTRUN *trun = g_new0 (AtomTRUN, 1);
3631 atom_trun_init (trun);
3636 atom_sdtp_init (AtomSDTP * sdtp)
3638 guint8 flags[3] = { 0, 0, 0 };
3640 atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
3641 atom_array_init (&sdtp->entries, 512);
3645 atom_sdtp_new (AtomsContext * context)
3647 AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
3649 atom_sdtp_init (sdtp);
3654 atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
3656 traf->sdtps = g_list_append (traf->sdtps, sdtp);
3660 atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
3662 /* it does not make much/any sense according to specs,
3663 * but that's how MS isml samples seem to do it */
3664 atom_array_append (&sdtp->entries, val, 256);
3668 atom_trun_add_samples (AtomTRUN * trun, guint32 delta, guint32 size,
3669 guint32 flags, gint64 pts_offset)
3671 TRUNSampleEntry nentry;
3673 if (pts_offset != 0)
3674 trun->header.flags[1] |= TR_COMPOSITION_TIME_OFFSETS;
3676 nentry.sample_duration = delta;
3677 nentry.sample_size = size;
3678 nentry.sample_flags = flags;
3679 nentry.sample_composition_time_offset = pts_offset;
3680 atom_array_append (&trun->entries, nentry, 256);
3681 trun->sample_count++;
3685 atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
3687 atom_header_set (&traf->header, FOURCC_traf, 0, 0);
3688 atom_tfhd_init (&traf->tfhd, track_ID);
3691 if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
3692 atom_traf_add_sdtp (traf, atom_sdtp_new (context));
3696 atom_traf_new (AtomsContext * context, guint32 track_ID)
3698 AtomTRAF *traf = g_new0 (AtomTRAF, 1);
3700 atom_traf_init (traf, context, track_ID);
3705 atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
3707 traf->truns = g_list_append (traf->truns, trun);
3711 atom_traf_add_samples (AtomTRAF * traf, guint32 delta, guint32 size,
3712 gboolean sync, gint64 pts_offset, gboolean sdtp_sync)
3717 /* 0x10000 is sample-is-difference-sample flag
3718 * low byte stuff is what ismv uses */
3719 flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
3721 if (G_UNLIKELY (!traf->truns)) {
3722 trun = atom_trun_new ();
3723 atom_traf_add_trun (traf, trun);
3724 /* optimistic; indicate all defaults present in tfhd */
3725 traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
3726 TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
3727 traf->tfhd.default_sample_duration = delta;
3728 traf->tfhd.default_sample_size = size;
3729 traf->tfhd.default_sample_flags = flags;
3730 trun->first_sample_flags = flags;
3733 trun = traf->truns->data;
3735 /* check if still matching defaults,
3736 * if not, abandon default and need entry for each sample */
3737 if (traf->tfhd.default_sample_duration != delta) {
3738 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
3739 trun->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
3741 if (traf->tfhd.default_sample_size != size) {
3742 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
3743 trun->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
3745 if (traf->tfhd.default_sample_flags != flags) {
3746 if (trun->sample_count == 1) {
3747 /* at least will need first sample flag */
3748 traf->tfhd.default_sample_flags = flags;
3749 trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
3751 /* now we need sample flags for each sample */
3752 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
3753 trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
3754 trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
3758 atom_trun_add_samples (traf->truns->data, delta, size, flags, pts_offset);
3761 atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
3765 atom_traf_get_sample_num (AtomTRAF * traf)
3769 if (G_UNLIKELY (!traf->truns))
3772 trun = traf->truns->data;
3773 return atom_array_get_len (&trun->entries);
3777 atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
3779 moof->trafs = g_list_append (moof->trafs, traf);
3783 atom_tfra_free (AtomTFRA * tfra)
3785 atom_full_clear (&tfra->header);
3786 atom_array_clear (&tfra->entries);
3791 atom_mfra_new (AtomsContext * context)
3793 AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
3795 atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
3800 atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
3802 mfra->tfras = g_list_append (mfra->tfras, tfra);
3806 atom_mfra_free (AtomMFRA * mfra)
3810 walker = mfra->tfras;
3812 atom_tfra_free ((AtomTFRA *) walker->data);
3813 walker = g_list_next (walker);
3815 g_list_free (mfra->tfras);
3818 atom_clear (&mfra->header);
3823 atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
3825 guint8 flags[3] = { 0, 0, 0 };
3827 atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
3828 tfra->track_ID = track_ID;
3829 atom_array_init (&tfra->entries, 512);
3833 atom_tfra_new (AtomsContext * context, guint32 track_ID)
3835 AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
3837 atom_tfra_init (tfra, track_ID);
3843 need_bytes (guint32 num)
3854 atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
3860 entry.moof_offset = 0;
3861 /* always write a single trun in a single traf */
3862 entry.traf_number = 1;
3863 entry.trun_number = 1;
3864 entry.sample_number = sample_num;
3866 /* auto-use 64 bits if needed */
3867 if (dts > G_MAXUINT32)
3868 tfra->header.version = 1;
3870 /* 1 byte will always do for traf and trun number,
3871 * check how much sample_num needs */
3872 tfra->lengths = (tfra->lengths & 0xfc) ||
3873 MAX (tfra->lengths, need_bytes (sample_num));
3875 atom_array_append (&tfra->entries, entry, 256);
3879 atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
3883 /* auto-use 64 bits if needed */
3884 if (offset > G_MAXUINT32)
3885 tfra->header.version = 1;
3887 for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
3888 TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
3890 if (entry->moof_offset)
3892 entry->moof_offset = offset;
3897 atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
3900 guint64 original_offset = *offset;
3907 if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
3911 prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
3912 prop_copy_uint32 (tfra->lengths, buffer, size, offset);
3913 prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
3915 version = tfra->header.version;
3916 for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
3917 entry = &atom_array_index (&tfra->entries, i);
3919 prop_copy_uint64 (entry->time, buffer, size, offset);
3920 prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
3922 prop_copy_uint32 (entry->time, buffer, size, offset);
3923 prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
3926 bytes = (tfra->lengths & (0x3 << 4)) + 1;
3927 data = GUINT32_TO_BE (entry->traf_number);
3928 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3929 buffer, size, offset);
3931 bytes = (tfra->lengths & (0x3 << 2)) + 1;
3932 data = GUINT32_TO_BE (entry->trun_number);
3933 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3934 buffer, size, offset);
3936 bytes = (tfra->lengths & (0x3)) + 1;
3937 data = GUINT32_TO_BE (entry->sample_number);
3938 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3939 buffer, size, offset);
3943 atom_write_size (buffer, size, offset, original_offset);
3944 return *offset - original_offset;
3948 atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
3951 guint64 original_offset = *offset;
3952 guint8 flags[3] = { 0, 0, 0 };
3955 atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
3957 if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
3961 prop_copy_uint32 (s, buffer, size, offset);
3963 atom_write_size (buffer, size, offset, original_offset);
3965 return *offset - original_offset;
3970 atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
3973 guint64 original_offset = *offset;
3976 if (!atom_copy_data (&mfra->header, buffer, size, offset))
3979 walker = g_list_first (mfra->tfras);
3980 while (walker != NULL) {
3981 if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
3984 walker = g_list_next (walker);
3987 /* 16 is the size of the mfro atom */
3988 if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
3992 atom_write_size (buffer, size, offset, original_offset);
3993 return *offset - original_offset;
3996 /* some sample description construction helpers */
3999 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
4000 const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
4005 track_id = trak->tkhd.track_ID;
4007 esds = atom_esds_new ();
4008 esds->es.id = track_id & 0xFFFF;
4009 esds->es.dec_conf_desc.object_type = object_type;
4010 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
4012 if (avg_bitrate > 0)
4013 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
4014 if (max_bitrate > 0)
4015 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
4017 /* optional DecoderSpecificInfo */
4019 DecoderSpecificInfoDescriptor *desc;
4021 esds->es.dec_conf_desc.dec_specific_info = desc =
4022 desc_dec_specific_info_new ();
4023 desc_dec_specific_info_alloc_data (desc, GST_BUFFER_SIZE (codec_data));
4025 memcpy (desc->data, GST_BUFFER_DATA (codec_data),
4026 GST_BUFFER_SIZE (codec_data));
4029 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
4034 build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
4035 guint32 max_bitrate)
4037 AtomData *atom_data;
4040 buf = gst_buffer_new_and_alloc (12);
4042 GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf), buffer_size_db);
4043 GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf) + 4, max_bitrate);
4044 GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf) + 8, avg_bitrate);
4046 atom_data = atom_data_new_from_gst_buffer (FOURCC_btrt, buf);
4047 gst_buffer_unref (buf);
4049 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4054 build_mov_wave_extension (AtomTRAK * trak, guint32 fourcc, AtomInfo * atom1,
4055 AtomInfo * atom2, gboolean terminator)
4061 /* Build WAVE atom for sample table entry */
4062 wave = atom_wave_new ();
4064 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
4066 ext_atom = (Atom *) atom_data_new (FOURCC_null);
4067 wave->extension_atoms =
4068 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4069 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4072 /* Add supplied atoms to WAVE */
4074 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
4076 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
4078 /* Add FRMA to the WAVE */
4079 frma = atom_frma_new ();
4080 frma->media_type = fourcc;
4082 wave->extension_atoms =
4083 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4084 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4086 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4091 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
4092 guint32 avg_bitrate, guint32 max_bitrate)
4094 AtomInfo *esds, *mp4a;
4097 /* Add ESDS atom to WAVE */
4098 esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
4099 ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
4101 /* Add MP4A atom to the WAVE:
4102 * not really in spec, but makes offset based players happy */
4103 buf = gst_buffer_new_and_alloc (4);
4104 *((guint32 *) GST_BUFFER_DATA (buf)) = 0;
4105 mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
4106 gst_buffer_unref (buf);
4108 return build_mov_wave_extension (trak, FOURCC_mp4a, mp4a, esds, TRUE);
4112 build_mov_alac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
4116 alac = build_codec_data_extension (FOURCC_alac, codec_data);
4118 return build_mov_wave_extension (trak, FOURCC_alac, NULL, alac, TRUE);
4122 build_fiel_extension (gint fields)
4124 AtomData *atom_data;
4131 buf = gst_buffer_new_and_alloc (1);
4132 GST_BUFFER_DATA (buf)[0] = (guint8) fields;
4135 atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('f', 'i', 'e', 'l'), buf);
4136 gst_buffer_unref (buf);
4138 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4143 build_jp2x_extension (const GstBuffer * prefix)
4145 AtomData *atom_data;
4152 atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('j', 'p', '2', 'x'),
4155 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4160 build_jp2h_extension (AtomTRAK * trak, gint width, gint height, guint32 fourcc,
4161 gint ncomp, const GValue * cmap_array, const GValue * cdef_array)
4163 AtomData *atom_data;
4167 gint idhr_size = 22;
4168 gint colr_size = 15;
4169 gint cmap_size = 0, cdef_size = 0;
4170 gint cmap_array_size = 0;
4171 gint cdef_array_size = 0;
4172 GstByteWriter writer;
4174 g_return_val_if_fail (cmap_array == NULL ||
4175 GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
4176 g_return_val_if_fail (cdef_array == NULL ||
4177 GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
4179 if (fourcc == GST_MAKE_FOURCC ('s', 'R', 'G', 'B')) {
4183 } else if (fourcc == GST_MAKE_FOURCC ('G', 'R', 'A', 'Y')) {
4187 } else if (fourcc == GST_MAKE_FOURCC ('s', 'Y', 'U', 'V')) {
4195 cmap_array_size = gst_value_array_get_size (cmap_array);
4196 cmap_size = 8 + cmap_array_size * 4;
4199 cdef_array_size = gst_value_array_get_size (cdef_array);
4200 cdef_size = 8 + 2 + cdef_array_size * 6;
4203 buf = gst_buffer_new_and_alloc (idhr_size + colr_size + cmap_size +
4205 gst_byte_writer_init_with_buffer (&writer, buf, FALSE);
4207 /* ihdr = image header box */
4208 gst_byte_writer_put_uint32_be (&writer, 22);
4209 gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
4210 gst_byte_writer_put_uint32_be (&writer, height);
4211 gst_byte_writer_put_uint32_be (&writer, width);
4212 gst_byte_writer_put_uint16_be (&writer, ncomp);
4213 /* 8 bits per component, unsigned */
4214 gst_byte_writer_put_uint8 (&writer, 0x7);
4215 /* compression type; reserved */
4216 gst_byte_writer_put_uint8 (&writer, 0x7);
4217 /* colour space (un)known */
4218 gst_byte_writer_put_uint8 (&writer, 0x0);
4219 /* intellectual property right (box present) */
4220 gst_byte_writer_put_uint8 (&writer, 0x0);
4222 /* colour specification box */
4223 gst_byte_writer_put_uint32_be (&writer, 15);
4224 gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
4226 /* specification method: enumerated */
4227 gst_byte_writer_put_uint8 (&writer, 0x1);
4228 /* precedence; reserved */
4229 gst_byte_writer_put_uint8 (&writer, 0x0);
4230 /* approximation; reserved */
4231 gst_byte_writer_put_uint8 (&writer, 0x0);
4232 /* enumerated colourspace */
4233 gst_byte_writer_put_uint32_be (&writer, cenum);
4236 gst_byte_writer_put_uint32_be (&writer, cmap_size);
4237 gst_byte_writer_put_uint32_le (&writer,
4238 GST_MAKE_FOURCC ('c', 'm', 'a', 'p'));
4239 for (i = 0; i < cmap_array_size; i++) {
4245 item = gst_value_array_get_value (cmap_array, i);
4246 value = g_value_get_int (item);
4248 /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
4249 cmp = value & 0xFFFF;
4251 pcol = (value >> 16) & 0xFF;
4254 GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
4255 "handle Pallete mapping atoms yet");
4257 gst_byte_writer_put_uint16_be (&writer, cmp);
4258 gst_byte_writer_put_uint8 (&writer, mtyp);
4259 gst_byte_writer_put_uint8 (&writer, pcol);
4264 gst_byte_writer_put_uint32_be (&writer, cdef_size);
4265 gst_byte_writer_put_uint32_le (&writer,
4266 GST_MAKE_FOURCC ('c', 'd', 'e', 'f'));
4267 gst_byte_writer_put_uint16_be (&writer, cdef_array_size);
4268 for (i = 0; i < cdef_array_size; i++) {
4271 item = gst_value_array_get_value (cdef_array, i);
4272 value = g_value_get_int (item);
4274 gst_byte_writer_put_uint16_be (&writer, i);
4276 gst_byte_writer_put_uint16_be (&writer, 0);
4277 gst_byte_writer_put_uint16_be (&writer, value);
4278 } else if (value < 0) {
4279 gst_byte_writer_put_uint16_be (&writer, -value);
4280 gst_byte_writer_put_uint16_be (&writer, 0); /* TODO what here? */
4282 gst_byte_writer_put_uint16_be (&writer, 1);
4283 gst_byte_writer_put_uint16_be (&writer, 0);
4288 g_assert (gst_byte_writer_get_remaining (&writer) == 0);
4290 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
4291 gst_buffer_unref (buf);
4293 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4298 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
4301 AtomInfo *result = NULL;
4304 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
4305 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
4313 build_amr_extension (void)
4319 buf = gst_buffer_new ();
4320 GST_BUFFER_DATA (buf) = ext;
4321 GST_BUFFER_SIZE (buf) = sizeof (ext);
4324 GST_WRITE_UINT32_LE (ext, 0);
4325 /* decoder version */
4326 GST_WRITE_UINT8 (ext + 4, 0);
4327 /* mode set (all modes) */
4328 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
4329 /* mode change period (no restriction) */
4330 GST_WRITE_UINT8 (ext + 7, 0);
4331 /* frames per sample */
4332 GST_WRITE_UINT8 (ext + 8, 1);
4334 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', 'a', 'm', 'r'), buf);
4335 gst_buffer_unref (buf);
4340 build_h263_extension (void)
4346 buf = gst_buffer_new ();
4347 GST_BUFFER_DATA (buf) = ext;
4348 GST_BUFFER_SIZE (buf) = sizeof (ext);
4351 GST_WRITE_UINT32_LE (ext, 0);
4352 /* decoder version */
4353 GST_WRITE_UINT8 (ext + 4, 0);
4354 /* level / profile */
4355 /* FIXME ? maybe ? obtain somewhere; baseline for now */
4356 GST_WRITE_UINT8 (ext + 5, 10);
4357 GST_WRITE_UINT8 (ext + 6, 0);
4359 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', '2', '6', '3'), buf);
4360 gst_buffer_unref (buf);
4365 build_gama_atom (gdouble gamma)
4371 /* convert to uint32 from fixed point */
4372 gamma_fp = (guint32) 65536 *gamma;
4374 buf = gst_buffer_new_and_alloc (4);
4375 GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf), gamma_fp);
4376 res = build_codec_data_extension (FOURCC_gama, buf);
4377 gst_buffer_unref (buf);
4382 build_SMI_atom (const GstBuffer * seqh)
4387 /* the seqh plus its size and fourcc */
4388 buf = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (seqh) + 8);
4390 GST_WRITE_UINT32_LE (GST_BUFFER_DATA (buf), FOURCC_SEQH);
4391 GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf) + 4, GST_BUFFER_SIZE (seqh));
4392 memcpy (GST_BUFFER_DATA (buf) + 8, GST_BUFFER_DATA (seqh),
4393 GST_BUFFER_SIZE (seqh));
4394 res = build_codec_data_extension (FOURCC_SMI_, buf);
4395 gst_buffer_unref (buf);
4400 build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
4402 AtomData *atom_data;
4405 const gint ima_adpcm_atom_size = 20;
4407 gint samplesperblock;
4410 /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
4411 identifier. Note that the identifier here is big-endian, but when used
4412 within the WAVE header (below), it's little endian. */
4413 fourcc = MS_WAVE_FOURCC (0x11);
4415 buf = gst_buffer_new_and_alloc (ima_adpcm_atom_size);
4416 data = GST_BUFFER_DATA (buf);
4418 /* This atom's content is a WAVE header, including 2 bytes of extra data.
4419 Note that all of this is little-endian, unlike most stuff in qt. */
4420 /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
4421 for the rest. Simplifies to this. */
4422 samplesperblock = 2 * blocksize / channels - 7;
4423 bytespersec = rate * blocksize / samplesperblock;
4424 GST_WRITE_UINT16_LE (data, 0x11);
4425 GST_WRITE_UINT16_LE (data + 2, channels);
4426 GST_WRITE_UINT32_LE (data + 4, rate);
4427 GST_WRITE_UINT32_LE (data + 8, bytespersec);
4428 GST_WRITE_UINT16_LE (data + 12, blocksize);
4429 GST_WRITE_UINT16_LE (data + 14, 4);
4430 GST_WRITE_UINT16_LE (data + 16, 2); /* Two extra bytes */
4431 GST_WRITE_UINT16_LE (data + 18, samplesperblock);
4433 atom_data = atom_data_new_from_gst_buffer (fourcc, buf);
4434 gst_buffer_unref (buf);
4436 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4441 build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
4448 wave = atom_wave_new ();
4450 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
4451 ext_atom = (Atom *) atom_data_new (FOURCC_null);
4452 wave->extension_atoms =
4453 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4454 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4456 /* Add wave ima adpcm atom to WAVE */
4457 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
4458 build_ima_adpcm_atom (channels, rate, blocksize));
4460 /* Add FRMA to the WAVE */
4461 frma = atom_frma_new ();
4462 frma->media_type = MS_WAVE_FOURCC (0x11);
4464 wave->extension_atoms =
4465 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4466 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4468 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4473 build_uuid_xmp_atom (GstBuffer * xmp_data)
4476 static guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
4477 0x97, 0xA9, 0x42, 0xE8,
4478 0x9C, 0x71, 0x99, 0x94,
4479 0x91, 0xE3, 0xAF, 0xAC
4482 if (xmp_data == NULL)
4485 uuid = atom_uuid_new ();
4486 memcpy (uuid->uuid, xmp_uuid, 16);
4488 uuid->data = g_malloc (GST_BUFFER_SIZE (xmp_data));
4489 uuid->datalen = GST_BUFFER_SIZE (xmp_data);
4490 memcpy (uuid->data, GST_BUFFER_DATA (xmp_data), GST_BUFFER_SIZE (xmp_data));
4492 return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,