1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2008 Thiago Sousa 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
48 /* only needed for gst_util_uint64_scale */
52 * Creates a new AtomsContext for the given flavor.
55 atoms_context_new (AtomsTreeFlavor flavor)
57 AtomsContext *context = g_new0 (AtomsContext, 1);
58 context->flavor = flavor;
63 * Frees an AtomsContext and all memory associated with it
66 atoms_context_free (AtomsContext * context)
71 /* -- creation, initialization, clear and free functions -- */
73 #define SECS_PER_DAY (24 * 60 * 60)
74 #define LEAP_YEARS_FROM_1904_TO_1970 17
77 get_current_qt_time ()
81 g_get_current_time (&timeval);
82 /* FIXME this should use UTC coordinated time */
83 return timeval.tv_sec + (((1970 - 1904) * (guint64) 365) +
84 LEAP_YEARS_FROM_1904_TO_1970) * SECS_PER_DAY;
88 common_time_info_init (TimeInfo * ti)
90 ti->creation_time = ti->modification_time = get_current_qt_time ();
96 atom_header_set (Atom * header, guint32 fourcc, gint32 size, gint64 ext_size)
98 header->type = fourcc;
100 header->extended_size = ext_size;
104 atom_clear (Atom * atom)
109 atom_full_init (AtomFull * full, guint32 fourcc, gint32 size, gint64 ext_size,
110 guint8 version, guint8 flags[3])
112 atom_header_set (&(full->header), fourcc, size, ext_size);
113 full->version = version;
114 full->flags[0] = flags[0];
115 full->flags[1] = flags[1];
116 full->flags[2] = flags[2];
120 atom_full_clear (AtomFull * full)
122 atom_clear (&full->header);
126 atom_full_free (AtomFull * full)
128 atom_full_clear (full);
133 build_atom_info_wrapper (Atom * atom, gpointer copy_func, gpointer free_func)
135 AtomInfo *info = NULL;
138 info = g_new0 (AtomInfo, 1);
141 info->copy_data_func = copy_func;
142 info->free_func = free_func;
149 atom_info_list_prepend_atom (GList * ai, Atom * atom,
150 AtomCopyDataFunc copy_func, AtomFreeFunc free_func)
153 return g_list_prepend (ai,
154 build_atom_info_wrapper (atom, copy_func, free_func));
160 atom_info_list_free (GList * ai)
163 AtomInfo *info = (AtomInfo *) ai->data;
165 info->free_func (info->atom);
167 ai = g_list_delete_link (ai, ai);
172 atom_data_new (guint32 fourcc)
174 AtomData *data = g_new0 (AtomData, 1);
176 atom_header_set (&data->header, fourcc, 0, 0);
181 atom_data_alloc_mem (AtomData * data, guint32 size)
186 data->data = g_new0 (guint8, size);
187 data->datalen = size;
191 atom_data_new_from_gst_buffer (guint32 fourcc, const GstBuffer * buf)
193 AtomData *data = atom_data_new (fourcc);
195 atom_data_alloc_mem (data, GST_BUFFER_SIZE (buf));
196 g_memmove (data->data, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
201 atom_data_free (AtomData * data)
203 atom_clear (&data->header);
209 atom_ftyp_init (AtomFTYP * ftyp, guint32 major, guint32 version, GList * brands)
214 atom_header_set (&ftyp->header, FOURCC_ftyp, 16, 0);
215 ftyp->major_brand = major;
216 ftyp->version = version;
218 /* always include major brand as compatible brand */
219 ftyp->compatible_brands_size = g_list_length (brands) + 1;
220 ftyp->compatible_brands = g_new (guint32, ftyp->compatible_brands_size);
222 ftyp->compatible_brands[0] = major;
224 for (it = brands; it != NULL; it = g_list_next (it)) {
225 ftyp->compatible_brands[index++] = GPOINTER_TO_UINT (it->data);
230 atom_ftyp_new (AtomsContext * context, guint32 major, guint32 version,
233 AtomFTYP *ftyp = g_new0 (AtomFTYP, 1);
235 atom_ftyp_init (ftyp, major, version, brands);
240 atom_ftyp_free (AtomFTYP * ftyp)
242 atom_clear (&ftyp->header);
243 g_free (ftyp->compatible_brands);
244 ftyp->compatible_brands = NULL;
249 atom_esds_init (AtomESDS * esds)
251 guint8 flags[3] = { 0, 0, 0 };
253 atom_full_init (&esds->header, FOURCC_esds, 0, 0, 0, flags);
254 desc_es_init (&esds->es);
260 AtomESDS *esds = g_new0 (AtomESDS, 1);
262 atom_esds_init (esds);
267 atom_esds_free (AtomESDS * esds)
269 atom_full_clear (&esds->header);
270 desc_es_descriptor_clear (&esds->es);
277 AtomFRMA *frma = g_new0 (AtomFRMA, 1);
279 atom_header_set (&frma->header, FOURCC_frma, 0, 0);
284 atom_frma_free (AtomFRMA * frma)
286 atom_clear (&frma->header);
293 AtomWAVE *wave = g_new0 (AtomWAVE, 1);
295 atom_header_set (&wave->header, FOURCC_wave, 0, 0);
300 atom_wave_free (AtomWAVE * wave)
302 atom_clear (&wave->header);
303 atom_info_list_free (wave->extension_atoms);
308 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
310 atom_header_set (&se->header, type, 0, 0);
312 memset (se->reserved, 0, sizeof (guint8) * 6);
313 se->data_reference_index = 0;
317 atom_sample_entry_free (SampleTableEntry * se)
319 atom_clear (&se->header);
323 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
325 atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
328 mp4a->revision_level = 0;
331 mp4a->sample_size = 16;
332 mp4a->compression_id = 0;
333 mp4a->packet_size = 0;
334 mp4a->sample_rate = 0;
335 /* following only used if version is 1 */
336 mp4a->samples_per_packet = 0;
337 mp4a->bytes_per_packet = 0;
338 mp4a->bytes_per_frame = 0;
339 mp4a->bytes_per_sample = 0;
341 mp4a->extension_atoms = NULL;
344 static SampleTableEntryMP4A *
345 sample_entry_mp4a_new ()
347 SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
349 sample_entry_mp4a_init (mp4a);
354 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
356 atom_sample_entry_free (&mp4a->se);
357 atom_info_list_free (mp4a->extension_atoms);
362 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
364 atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
367 mp4v->revision_level = 0;
370 mp4v->temporal_quality = 0;
371 mp4v->spatial_quality = 0;
373 /* qt and ISO base media do not contradict, and examples agree */
374 mp4v->horizontal_resolution = 0x00480000;
375 mp4v->vertical_resolution = 0x00480000;
378 mp4v->frame_count = 1;
380 memset (mp4v->compressor, 0, sizeof (guint8) * 32);
383 mp4v->color_table_id = 0;
385 mp4v->extension_atoms = NULL;
389 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
391 atom_sample_entry_free (&mp4v->se);
392 atom_info_list_free (mp4v->extension_atoms);
396 static SampleTableEntryMP4V *
397 sample_entry_mp4v_new (AtomsContext * context)
399 SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
401 sample_entry_mp4v_init (mp4v, context);
406 atom_stsd_init (AtomSTSD * stsd)
408 guint8 flags[3] = { 0, 0, 0 };
410 atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
411 stsd->entries = NULL;
415 atom_stsd_clear (AtomSTSD * stsd)
419 atom_full_clear (&stsd->header);
420 walker = stsd->entries;
423 SampleTableEntry *se = (SampleTableEntry *) aux->data;
425 walker = g_list_next (walker);
426 stsd->entries = g_list_remove_link (stsd->entries, aux);
430 sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
433 sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
436 /* best possible cleanup */
437 atom_sample_entry_free (se);
444 atom_ctts_init (AtomCTTS * ctts)
446 guint8 flags[3] = { 0, 0, 0 };
448 atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
449 ctts->entries = NULL;
455 AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
457 atom_ctts_init (ctts);
462 atom_ctts_free (AtomCTTS * ctts)
466 atom_full_clear (&ctts->header);
467 walker = ctts->entries;
471 walker = g_list_next (walker);
472 ctts->entries = g_list_remove_link (ctts->entries, aux);
473 g_free ((CTTSEntry *) aux->data);
480 atom_stts_init (AtomSTTS * stts)
482 guint8 flags[3] = { 0, 0, 0 };
484 atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
485 stts->entries = NULL;
489 atom_stts_clear (AtomSTTS * stts)
493 atom_full_clear (&stts->header);
494 walker = stts->entries;
498 walker = g_list_next (walker);
499 stts->entries = g_list_remove_link (stts->entries, aux);
500 g_free ((STTSEntry *) aux->data);
507 atom_stsz_init (AtomSTSZ * stsz)
509 guint8 flags[3] = { 0, 0, 0 };
511 atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
512 stsz->sample_size = 0;
513 stsz->table_size = 0;
514 stsz->entries = NULL;
518 atom_stsz_clear (AtomSTSZ * stsz)
520 atom_full_clear (&stsz->header);
521 g_list_free (stsz->entries);
522 stsz->entries = NULL;
523 stsz->table_size = 0;
527 atom_stsc_init (AtomSTSC * stsc)
529 guint8 flags[3] = { 0, 0, 0 };
531 atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
532 stsc->entries = NULL;
537 atom_stsc_clear (AtomSTSC * stsc)
541 atom_full_clear (&stsc->header);
542 walker = stsc->entries;
546 walker = g_list_next (walker);
547 stsc->entries = g_list_remove_link (stsc->entries, aux);
548 g_free ((STSCEntry *) aux->data);
555 atom_co64_init (AtomSTCO64 * co64)
557 guint8 flags[3] = { 0, 0, 0 };
559 atom_full_init (&co64->header, FOURCC_co64, 0, 0, 0, flags);
560 co64->entries = NULL;
565 atom_stco64_clear (AtomSTCO64 * stco64)
569 atom_full_clear (&stco64->header);
570 walker = stco64->entries;
574 walker = g_list_next (walker);
575 stco64->entries = g_list_remove_link (stco64->entries, aux);
576 g_free ((guint64 *) aux->data);
579 stco64->n_entries = 0;
583 atom_stss_init (AtomSTSS * stss)
585 guint8 flags[3] = { 0, 0, 0 };
587 atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
588 stss->entries = NULL;
593 atom_stss_clear (AtomSTSS * stss)
595 atom_full_clear (&stss->header);
596 g_list_free (stss->entries);
597 stss->entries = NULL;
602 atom_stbl_init (AtomSTBL * stbl)
604 atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
606 atom_stts_init (&stbl->stts);
607 atom_stss_init (&stbl->stss);
608 atom_stsd_init (&stbl->stsd);
609 atom_stsz_init (&stbl->stsz);
610 atom_stsc_init (&stbl->stsc);
613 atom_co64_init (&stbl->stco64);
617 atom_stbl_clear (AtomSTBL * stbl)
619 atom_clear (&stbl->header);
620 atom_stsd_clear (&stbl->stsd);
621 atom_stts_clear (&stbl->stts);
622 atom_stss_clear (&stbl->stss);
623 atom_stsc_clear (&stbl->stsc);
624 atom_stsz_clear (&stbl->stsz);
626 atom_ctts_free (stbl->ctts);
628 atom_stco64_clear (&stbl->stco64);
632 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
634 guint8 flags[3] = { 0, 0, 1 };
636 atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
637 vmhd->graphics_mode = 0x0;
638 memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
640 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
641 vmhd->graphics_mode = 0x40;
642 vmhd->opcolor[0] = 32768;
643 vmhd->opcolor[1] = 32768;
644 vmhd->opcolor[2] = 32768;
649 atom_vmhd_new (AtomsContext * context)
651 AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
653 atom_vmhd_init (vmhd, context);
658 atom_vmhd_free (AtomVMHD * vmhd)
660 atom_full_clear (&vmhd->header);
665 atom_smhd_init (AtomSMHD * smhd)
667 guint8 flags[3] = { 0, 0, 0 };
669 atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
677 AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
679 atom_smhd_init (smhd);
684 atom_smhd_free (AtomSMHD * smhd)
686 atom_full_clear (&smhd->header);
691 atom_hmhd_free (AtomHMHD * hmhd)
693 atom_full_clear (&hmhd->header);
698 atom_hdlr_init (AtomHDLR * hdlr)
700 guint8 flags[3] = { 0, 0, 0 };
702 atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
704 hdlr->component_type = 0;
705 hdlr->handler_type = 0;
706 hdlr->manufacturer = 0;
708 hdlr->flags_mask = 0;
709 hdlr->name = g_strdup ("");
715 AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
717 atom_hdlr_init (hdlr);
722 atom_hdlr_clear (AtomHDLR * hdlr)
724 atom_full_clear (&hdlr->header);
732 atom_hdlr_free (AtomHDLR * hdlr)
734 atom_hdlr_clear (hdlr);
739 atom_url_init (AtomURL * url)
741 guint8 flags[3] = { 0, 0, 1 };
743 atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
744 url->location = NULL;
748 atom_url_free (AtomURL * url)
750 atom_full_clear (&url->header);
752 g_free (url->location);
753 url->location = NULL;
761 AtomURL *url = g_new0 (AtomURL, 1);
770 guint8 flags[3] = { 0, 0, 1 };
771 AtomFull *alis = g_new0 (AtomFull, 1);
773 atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
778 atom_dref_init (AtomDREF * dref, AtomsContext * context)
780 guint8 flags[3] = { 0, 0, 0 };
782 atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
784 /* in either case, alis or url init arranges to set self-contained flag */
785 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
786 /* alis dref for qt */
787 AtomFull *alis = atom_alis_new ();
788 dref->entries = g_list_append (dref->entries, alis);
790 /* url for iso spec, as 'alis' not specified there */
791 AtomURL *url = atom_url_new ();
792 dref->entries = g_list_append (dref->entries, url);
797 atom_dref_clear (AtomDREF * dref)
801 atom_full_clear (&dref->header);
802 walker = dref->entries;
805 Atom *atom = (Atom *) aux->data;
807 walker = g_list_next (walker);
808 dref->entries = g_list_remove_link (dref->entries, aux);
809 switch (atom->type) {
811 atom_full_free ((AtomFull *) atom);
814 atom_url_free ((AtomURL *) atom);
817 /* we do nothing, better leak than crash */
825 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
827 atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
828 atom_dref_init (&dinf->dref, context);
832 atom_dinf_clear (AtomDINF * dinf)
834 atom_clear (&dinf->header);
835 atom_dref_clear (&dinf->dref);
839 atom_minf_init (AtomMINF * minf, AtomsContext * context)
841 atom_header_set (&minf->header, FOURCC_minf, 0, 0);
847 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
848 minf->hdlr = atom_hdlr_new ();
849 minf->hdlr->component_type = FOURCC_dhlr;
850 minf->hdlr->handler_type = FOURCC_alis;
854 atom_dinf_init (&minf->dinf, context);
855 atom_stbl_init (&minf->stbl);
859 atom_minf_clear_handlers (AtomMINF * minf)
862 atom_vmhd_free (minf->vmhd);
866 atom_smhd_free (minf->smhd);
870 atom_hmhd_free (minf->hmhd);
876 atom_minf_clear (AtomMINF * minf)
878 atom_clear (&minf->header);
879 atom_minf_clear_handlers (minf);
881 atom_hdlr_free (minf->hdlr);
883 atom_dinf_clear (&minf->dinf);
884 atom_stbl_clear (&minf->stbl);
888 atom_mdhd_init (AtomMDHD * mdhd)
890 guint8 flags[3] = { 0, 0, 0 };
892 atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
893 common_time_info_init (&mdhd->time_info);
894 mdhd->language_code = 0;
899 atom_mdhd_clear (AtomMDHD * mdhd)
901 atom_full_clear (&mdhd->header);
905 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
907 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
909 atom_mdhd_init (&mdia->mdhd);
910 atom_hdlr_init (&mdia->hdlr);
911 atom_minf_init (&mdia->minf, context);
915 atom_mdia_clear (AtomMDIA * mdia)
917 atom_clear (&mdia->header);
918 atom_mdhd_clear (&mdia->mdhd);
919 atom_hdlr_clear (&mdia->hdlr);
920 atom_minf_clear (&mdia->minf);
924 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
929 * 2 -> track in movie
930 * 4 -> track in preview
932 guint8 flags[3] = { 0, 0, 7 };
934 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
936 tkhd->creation_time = tkhd->modification_time = get_current_qt_time ();
941 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
943 tkhd->alternate_group = 0;
946 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
947 tkhd->matrix[0] = 1 << 16;
948 tkhd->matrix[4] = 1 << 16;
949 tkhd->matrix[8] = 16384 << 16;
955 atom_tkhd_clear (AtomTKHD * tkhd)
957 atom_full_clear (&tkhd->header);
961 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
963 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
965 atom_tkhd_init (&trak->tkhd, context);
966 atom_mdia_init (&trak->mdia, context);
970 atom_trak_new (AtomsContext * context)
972 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
974 atom_trak_init (trak, context);
979 atom_trak_free (AtomTRAK * trak)
981 atom_clear (&trak->header);
982 atom_tkhd_clear (&trak->tkhd);
983 atom_mdia_clear (&trak->mdia);
988 atom_ilst_init (AtomILST * ilst)
990 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
991 ilst->entries = NULL;
997 AtomILST *ilst = g_new0 (AtomILST, 1);
999 atom_ilst_init (ilst);
1004 atom_ilst_free (AtomILST * ilst)
1007 atom_info_list_free (ilst->entries);
1008 atom_clear (&ilst->header);
1013 atom_meta_init (AtomMETA * meta)
1015 guint8 flags[3] = { 0, 0, 0 };
1017 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1018 atom_hdlr_init (&meta->hdlr);
1019 /* FIXME (ISOM says this is always 0) */
1020 meta->hdlr.component_type = FOURCC_mhlr;
1021 meta->hdlr.handler_type = FOURCC_mdir;
1028 AtomMETA *meta = g_new0 (AtomMETA, 1);
1030 atom_meta_init (meta);
1035 atom_meta_free (AtomMETA * meta)
1037 atom_full_clear (&meta->header);
1038 atom_hdlr_clear (&meta->hdlr);
1039 atom_ilst_free (meta->ilst);
1045 atom_udta_init (AtomUDTA * udta)
1047 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1054 AtomUDTA *udta = g_new0 (AtomUDTA, 1);
1056 atom_udta_init (udta);
1061 atom_udta_free (AtomUDTA * udta)
1063 atom_clear (&udta->header);
1064 atom_meta_free (udta->meta);
1070 atom_tag_data_init (AtomTagData * data)
1072 guint8 flags[] = { 0, 0, 0 };
1074 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1078 atom_tag_data_clear (AtomTagData * data)
1080 atom_full_clear (&data->header);
1081 g_free (data->data);
1086 * Fourcc is the tag fourcc
1087 * flags will be truncated to 24bits
1090 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1092 AtomTag *tag = g_new0 (AtomTag, 1);
1094 tag->header.type = fourcc;
1095 atom_tag_data_init (&tag->data);
1096 tag->data.header.flags[2] = flags_as_uint & 0xFF;
1097 tag->data.header.flags[1] = flags_as_uint & 0xFF00;
1098 tag->data.header.flags[0] = flags_as_uint & 0xFF0000;
1103 atom_tag_free (AtomTag * tag)
1105 atom_clear (&tag->header);
1106 atom_tag_data_clear (&tag->data);
1111 atom_mvhd_init (AtomMVHD * mvhd)
1113 guint8 flags[3] = { 0, 0, 0 };
1115 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1117 common_time_info_init (&mvhd->time_info);
1119 mvhd->prefered_rate = 1 << 16;
1120 mvhd->volume = 1 << 8;
1121 mvhd->reserved3 = 0;
1122 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1124 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1125 mvhd->matrix[0] = 1 << 16;
1126 mvhd->matrix[4] = 1 << 16;
1127 mvhd->matrix[8] = 16384 << 16;
1129 mvhd->preview_time = 0;
1130 mvhd->preview_duration = 0;
1131 mvhd->poster_time = 0;
1132 mvhd->selection_time = 0;
1133 mvhd->selection_duration = 0;
1134 mvhd->current_time = 0;
1136 mvhd->next_track_id = 1;
1140 atom_mvhd_clear (AtomMVHD * mvhd)
1142 atom_full_clear (&mvhd->header);
1146 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1148 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1149 atom_mvhd_init (&(moov->mvhd));
1155 atom_moov_new (AtomsContext * context)
1157 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1159 atom_moov_init (moov, context);
1164 atom_moov_free (AtomMOOV * moov)
1168 atom_clear (&moov->header);
1169 atom_mvhd_clear (&moov->mvhd);
1171 walker = moov->traks;
1173 GList *aux = walker;
1175 walker = g_list_next (walker);
1176 moov->traks = g_list_remove_link (moov->traks, aux);
1177 atom_trak_free ((AtomTRAK *) aux->data);
1182 atom_udta_free (moov->udta);
1189 /* -- end of init / free -- */
1191 /* -- copy data functions -- */
1194 atom_full_get_version (AtomFull * full)
1196 return full->version;
1200 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1201 guint8 ** buffer, guint64 * size, guint64 * offset)
1203 guint64 original_offset = *offset;
1206 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1207 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1208 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1209 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1211 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1212 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1213 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1214 prop_copy_uint64 (ti->duration, buffer, size, offset);
1216 return *offset - original_offset;
1220 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1223 /* this only works for non-extended atom size, which is OK
1224 * (though it could be made to do mem_move, etc and write extended size) */
1225 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1229 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1231 guint64 original_offset = *offset;
1233 /* copies type and size */
1234 prop_copy_uint32 (atom->size, buffer, size, offset);
1235 prop_copy_fourcc (atom->type, buffer, size, offset);
1237 /* extended size needed */
1238 if (atom->size == 1) {
1239 /* really should not happen other than with mdat atom;
1240 * would be a problem for size (re)write code, not to mention memory */
1241 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1242 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1244 /* just in case some trivially derived atom does not do so */
1245 atom_write_size (buffer, size, offset, original_offset);
1248 return *offset - original_offset;
1252 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1255 guint64 original_offset = *offset;
1257 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1261 prop_copy_uint8 (atom->version, buffer, size, offset);
1262 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1264 atom_write_size (buffer, size, offset, original_offset);
1265 return *offset - original_offset;
1269 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1272 guint64 original_offset = *offset;
1275 AtomInfo *info = (AtomInfo *) ai->data;
1277 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1280 ai = g_list_next (ai);
1283 return *offset - original_offset;
1287 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1290 guint64 original_offset = *offset;
1292 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1296 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1298 atom_write_size (buffer, size, offset, original_offset);
1299 return *offset - original_offset;
1303 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1306 guint64 original_offset = *offset;
1308 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1311 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1312 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1314 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1315 buffer, size, offset);
1317 atom_write_size (buffer, size, offset, original_offset);
1318 return *offset - original_offset;
1322 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1326 guint64 original_offset = *offset;
1328 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1332 version = atom_full_get_version (&(atom->header));
1334 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1335 } else if (version == 1) {
1336 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1338 *offset = original_offset;
1342 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1343 prop_copy_uint16 (atom->volume, buffer, size, offset);
1344 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1345 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1346 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1347 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1348 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1349 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1350 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1351 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1352 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1354 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1356 atom_write_size (buffer, size, offset, original_offset);
1357 return *offset - original_offset;
1361 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1364 guint64 original_offset = *offset;
1366 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1370 if (atom_full_get_version (&tkhd->header) == 0) {
1371 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1372 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1373 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1374 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1375 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1377 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1378 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1379 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1380 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1381 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1384 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1385 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1386 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1387 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1388 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1389 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1391 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1392 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1394 atom_write_size (buffer, size, offset, original_offset);
1395 return *offset - original_offset;
1399 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1402 guint64 original_offset = *offset;
1404 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1408 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1409 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1410 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1411 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1412 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1414 prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
1416 atom_write_size (buffer, size, offset, original_offset);
1417 return *offset - original_offset;
1421 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1424 guint64 original_offset = *offset;
1426 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1429 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1430 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1432 atom_write_size (buffer, size, offset, original_offset);
1433 return original_offset - *offset;
1437 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1440 guint64 original_offset = *offset;
1442 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1445 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1446 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1448 atom_write_size (buffer, size, offset, original_offset);
1449 return original_offset - *offset;
1453 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1456 guint64 original_offset = *offset;
1458 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1461 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1462 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1463 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1464 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1465 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1467 atom_write_size (buffer, size, offset, original_offset);
1468 return original_offset - *offset;
1472 atom_url_same_file_flag (AtomURL * url)
1474 return (url->header.flags[2] & 0x1) == 1;
1478 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1481 guint64 original_offset = *offset;
1483 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1487 if (!atom_url_same_file_flag (url)) {
1488 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1491 atom_write_size (buffer, size, offset, original_offset);
1492 return original_offset - *offset;
1496 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1499 guint64 original_offset = *offset;
1502 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1506 prop_copy_uint32 (stts->n_entries, buffer, size, offset);
1507 /* minimize realloc */
1508 prop_copy_ensure_buffer (buffer, size, offset, 8 * stts->n_entries);
1509 for (walker = g_list_last (stts->entries); walker != NULL;
1510 walker = g_list_previous (walker)) {
1511 STTSEntry *entry = (STTSEntry *) walker->data;
1513 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
1514 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
1517 atom_write_size (buffer, size, offset, original_offset);
1518 return *offset - original_offset;
1522 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
1523 guint64 * size, guint64 * offset)
1525 guint64 original_offset = *offset;
1527 if (!atom_copy_data (&se->header, buffer, size, offset)) {
1531 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
1532 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
1534 return *offset - original_offset;
1538 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
1541 guint64 original_offset = *offset;
1543 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
1546 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
1550 atom_write_size (buffer, size, offset, original_offset);
1551 return *offset - original_offset;
1555 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
1556 guint64 * size, guint64 * offset)
1558 guint64 original_offset = *offset;
1560 if (!atom_copy_data (&(frma->header), buffer, size, offset))
1563 prop_copy_fourcc (frma->media_type, buffer, size, offset);
1565 atom_write_size (buffer, size, offset, original_offset);
1566 return *offset - original_offset;
1570 atom_mp4s_copy_data (SampleTableEntryMP4S * mp4s, guint8 ** buffer,
1571 guint64 * size, guint64 * offset)
1573 guint64 original_offset = *offset;
1575 if (!atom_sample_entry_copy_data (&mp4s->se, buffer, size, offset)) {
1578 if (!atom_esds_copy_data (&mp4s->es, buffer, size, offset)) {
1582 atom_write_size (buffer, size, offset, original_offset);
1583 return *offset - original_offset;
1587 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
1588 guint64 * size, guint64 * offset)
1590 guint64 original_offset = *offset;
1592 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
1596 prop_copy_uint32 (hse->size, buffer, size, offset);
1597 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
1599 atom_write_size (buffer, size, offset, original_offset);
1600 return *offset - original_offset;
1604 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
1605 guint64 * size, guint64 * offset)
1607 guint64 original_offset = *offset;
1609 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
1613 prop_copy_uint16 (mp4a->version, buffer, size, offset);
1614 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
1615 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
1616 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
1617 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
1618 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
1619 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
1620 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
1622 /* this should always be 0 for mp4 flavor */
1623 if (mp4a->version == 1) {
1624 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
1625 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
1626 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
1627 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
1630 if (mp4a->extension_atoms) {
1631 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
1635 atom_write_size (buffer, size, offset, original_offset);
1636 return *offset - original_offset;
1640 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
1641 guint64 * size, guint64 * offset)
1643 guint64 original_offset = *offset;
1645 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
1649 prop_copy_uint16 (mp4v->version, buffer, size, offset);
1650 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
1651 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
1652 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
1653 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
1655 prop_copy_uint16 (mp4v->width, buffer, size, offset);
1656 prop_copy_uint16 (mp4v->height, buffer, size, offset);
1658 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
1659 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
1660 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
1662 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
1664 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
1667 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
1668 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
1671 if (mp4v->extension_atoms &&
1672 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
1675 atom_write_size (buffer, size, offset, original_offset);
1676 return *offset - original_offset;
1680 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
1683 guint64 original_offset = *offset;
1686 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
1690 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
1691 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
1692 /* minimize realloc */
1693 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
1694 if (stsz->sample_size == 0) {
1695 for (walker = g_list_last (stsz->entries); walker != NULL;
1696 walker = g_list_previous (walker)) {
1697 prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
1702 atom_write_size (buffer, size, offset, original_offset);
1703 return *offset - original_offset;
1707 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
1710 guint64 original_offset = *offset;
1713 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
1717 prop_copy_uint32 (stsc->n_entries, buffer, size, offset);
1718 /* minimize realloc */
1719 prop_copy_ensure_buffer (buffer, size, offset, 12 * stsc->n_entries);
1721 for (walker = g_list_last (stsc->entries); walker != NULL;
1722 walker = g_list_previous (walker)) {
1723 STSCEntry *entry = (STSCEntry *) walker->data;
1725 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
1726 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
1727 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
1730 atom_write_size (buffer, size, offset, original_offset);
1731 return *offset - original_offset;
1735 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
1738 guint64 original_offset = *offset;
1741 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
1745 prop_copy_uint32 (ctts->n_entries, buffer, size, offset);
1746 /* minimize realloc */
1747 prop_copy_ensure_buffer (buffer, size, offset, 8 * ctts->n_entries);
1748 for (walker = g_list_last (ctts->entries); walker != NULL;
1749 walker = g_list_previous (walker)) {
1750 CTTSEntry *entry = (CTTSEntry *) walker->data;
1752 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
1753 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
1756 atom_write_size (buffer, size, offset, original_offset);
1757 return *offset - original_offset;
1761 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
1764 guint64 original_offset = *offset;
1766 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
1768 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
1772 prop_copy_uint32 (stco64->n_entries, buffer, size, offset);
1774 /* minimize realloc */
1775 prop_copy_ensure_buffer (buffer, size, offset, 8 * stco64->n_entries);
1776 for (walker = g_list_last (stco64->entries); walker != NULL;
1777 walker = g_list_previous (walker)) {
1778 guint64 *value = (guint64 *) walker->data;
1781 prop_copy_uint32 ((guint32) * value, buffer, size, offset);
1783 prop_copy_uint64 (*value, buffer, size, offset);
1787 atom_write_size (buffer, size, offset, original_offset);
1788 return *offset - original_offset;
1792 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
1795 guint64 original_offset = *offset;
1798 if (stss->entries == NULL) {
1799 /* FIXME not needing this atom might be confused with error while copying */
1803 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
1807 prop_copy_uint32 (stss->n_entries, buffer, size, offset);
1808 /* minimize realloc */
1809 prop_copy_ensure_buffer (buffer, size, offset, 4 * stss->n_entries);
1810 for (walker = g_list_last (stss->entries); walker != NULL;
1811 walker = g_list_previous (walker)) {
1812 prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
1816 atom_write_size (buffer, size, offset, original_offset);
1817 return *offset - original_offset;
1821 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
1824 guint64 original_offset = *offset;
1827 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
1831 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
1833 for (walker = g_list_last (stsd->entries); walker != NULL;
1834 walker = g_list_previous (walker)) {
1835 SampleTableEntry *se = (SampleTableEntry *) walker->data;
1837 switch (((Atom *) walker->data)->type) {
1839 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
1840 buffer, size, offset)) {
1845 if (!atom_mp4s_copy_data ((SampleTableEntryMP4S *) walker->data,
1846 buffer, size, offset)) {
1851 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
1852 buffer, size, offset)) {
1857 if (se->kind == VIDEO) {
1859 sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->
1860 data, buffer, size, offset);
1861 } else if (se->kind == AUDIO) {
1863 sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->
1864 data, buffer, size, offset);
1866 if (!atom_hint_sample_entry_copy_data (
1867 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
1875 atom_write_size (buffer, size, offset, original_offset);
1876 return *offset - original_offset;
1880 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
1883 guint64 original_offset = *offset;
1885 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
1889 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
1892 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
1895 /* this atom is optional, so let's check if we need it
1896 * (to avoid false error) */
1897 if (stbl->stss.entries) {
1898 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
1903 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
1906 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
1910 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
1914 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
1918 atom_write_size (buffer, size, offset, original_offset);
1919 return original_offset - *offset;
1924 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
1927 guint64 original_offset = *offset;
1930 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
1934 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
1936 walker = dref->entries;
1937 while (walker != NULL) {
1938 Atom *atom = (Atom *) walker->data;
1940 if (atom->type == FOURCC_url_) {
1941 atom_url_copy_data ((AtomURL *) atom, buffer, size, offset);
1942 } else if (atom->type == FOURCC_alis) {
1943 atom_full_copy_data ((AtomFull *) atom, buffer, size, offset);
1945 g_error ("Unsupported atom used inside dref atom");
1947 walker = g_list_next (walker);
1950 atom_write_size (buffer, size, offset, original_offset);
1951 return *offset - original_offset;
1955 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
1958 guint64 original_offset = *offset;
1960 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
1964 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
1968 atom_write_size (buffer, size, offset, original_offset);
1969 return original_offset - *offset;
1973 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
1976 guint64 original_offset = *offset;
1978 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
1983 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
1986 } else if (minf->smhd) {
1987 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
1990 } else if (minf->hmhd) {
1991 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
1997 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2002 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2005 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2009 atom_write_size (buffer, size, offset, original_offset);
2010 return *offset - original_offset;
2014 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2017 guint64 original_offset = *offset;
2019 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2023 if (!common_time_info_copy_data (&mdhd->time_info,
2024 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2028 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2029 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2031 atom_write_size (buffer, size, offset, original_offset);
2032 return *offset - original_offset;
2036 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2039 guint64 original_offset = *offset;
2041 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2044 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2047 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2051 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2055 atom_write_size (buffer, size, offset, original_offset);
2056 return *offset - original_offset;
2060 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2063 guint64 original_offset = *offset;
2065 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2068 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2072 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2076 atom_write_size (buffer, size, offset, original_offset);
2077 return *offset - original_offset;
2081 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2084 guint64 original_offset = *offset;
2086 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2090 prop_copy_uint32 (data->reserved, buffer, size, offset);
2091 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2093 atom_write_size (buffer, size, offset, original_offset);
2094 return *offset - original_offset;
2098 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2101 guint64 original_offset = *offset;
2103 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2107 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2111 atom_write_size (buffer, size, offset, original_offset);
2112 return *offset - original_offset;
2116 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2119 guint64 original_offset = *offset;
2121 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2125 if (ilst->entries &&
2126 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2129 atom_write_size (buffer, size, offset, original_offset);
2130 return *offset - original_offset;
2134 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2137 guint64 original_offset = *offset;
2139 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2142 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2146 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2151 atom_write_size (buffer, size, offset, original_offset);
2152 return *offset - original_offset;
2156 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2159 guint64 original_offset = *offset;
2161 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2165 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2170 atom_write_size (buffer, size, offset, original_offset);
2171 return *offset - original_offset;
2175 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2178 guint64 original_offset = *offset;
2181 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2184 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2187 walker = g_list_first (atom->traks);
2188 while (walker != NULL) {
2189 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2192 walker = g_list_next (walker);
2196 if (!atom_udta_copy_data (atom->udta, buffer, size, offset)) {
2201 atom_write_size (buffer, size, offset, original_offset);
2202 return *offset - original_offset;
2206 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
2207 guint64 * size, guint64 * offset)
2209 guint64 original_offset = *offset;
2211 if (!atom_copy_data (&(wave->header), buffer, size, offset))
2214 if (wave->extension_atoms) {
2215 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
2219 atom_write_size (buffer, size, offset, original_offset);
2220 return *offset - original_offset;
2223 /* -- end of copy data functions -- */
2225 /* -- general functions, API and support functions */
2227 /* add samples to tables */
2230 stsc_entry_new (guint32 first_chunk, guint32 samples, guint32 desc_index)
2232 STSCEntry *entry = g_new0 (STSCEntry, 1);
2234 entry->first_chunk = first_chunk;
2235 entry->samples_per_chunk = samples;
2236 entry->sample_description_index = desc_index;
2241 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
2244 g_list_prepend (stsc->entries, stsc_entry_new (first_chunk, nsamples, 1));
2249 stts_entry_new (guint32 sample_count, gint32 sample_delta)
2251 STTSEntry *entry = g_new0 (STTSEntry, 1);
2253 entry->sample_count = sample_count;
2254 entry->sample_delta = sample_delta;
2259 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
2263 if (stts->entries == NULL) {
2265 g_list_prepend (stts->entries, stts_entry_new (sample_count,
2270 entry = (STTSEntry *) g_list_first (stts->entries)->data;
2271 if (entry->sample_delta == sample_delta) {
2272 entry->sample_count += sample_count;
2275 g_list_prepend (stts->entries, stts_entry_new (sample_count,
2282 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
2286 stsz->table_size += nsamples;
2287 if (stsz->sample_size != 0) {
2288 /* it is constant size, we don't need entries */
2291 for (i = 0; i < nsamples; i++) {
2292 stsz->entries = g_list_prepend (stsz->entries, GUINT_TO_POINTER (size));
2297 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
2299 return stco64->n_entries;
2303 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
2305 guint64 *pont = g_new (guint64, 1);
2308 stco64->entries = g_list_prepend (stco64->entries, pont);
2309 stco64->n_entries++;
2313 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
2315 stss->entries = g_list_prepend (stss->entries, GUINT_TO_POINTER (sample));
2320 atom_stbl_add_stss_entry (AtomSTBL * stbl)
2322 guint32 sample_index = stbl->stsz.table_size;
2324 atom_stss_add_entry (&stbl->stss, sample_index);
2328 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
2333 walker = g_list_first (ctts->entries);
2334 entry = (walker == NULL) ? NULL : (CTTSEntry *) walker->data;
2336 if (entry == NULL || entry->sampleoffset != offset) {
2337 CTTSEntry *entry = g_new0 (CTTSEntry, 1);
2339 entry->samplecount = nsamples;
2340 entry->sampleoffset = offset;
2341 ctts->entries = g_list_prepend (ctts->entries, entry);
2344 entry->samplecount += nsamples;
2349 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
2351 if (stbl->ctts == NULL) {
2352 stbl->ctts = atom_ctts_new ();
2354 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
2358 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
2359 guint32 size, guint64 chunk_offset, gboolean sync,
2360 gboolean do_pts, gint64 pts_offset)
2362 AtomSTBL *stbl = &trak->mdia.minf.stbl;
2364 atom_stts_add_entry (&stbl->stts, nsamples, delta);
2365 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
2366 atom_stco64_add_entry (&stbl->stco64, chunk_offset);
2367 atom_stsc_add_new_entry (&stbl->stsc,
2368 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
2370 atom_stbl_add_stss_entry (stbl);
2372 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
2375 /* trak and moov molding */
2378 atom_trak_get_timescale (AtomTRAK * trak)
2380 return trak->mdia.mdhd.time_info.timescale;
2384 atom_trak_set_id (AtomTRAK * trak, guint32 id)
2386 trak->tkhd.track_ID = id;
2390 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
2392 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
2393 moov->traks = g_list_append (moov->traks, trak);
2397 atom_trak_get_duration (AtomTRAK * trak)
2399 return trak->tkhd.duration;
2403 atom_stts_get_total_duration (AtomSTTS * stts)
2405 GList *walker = stts->entries;
2409 STTSEntry *entry = (STTSEntry *) walker->data;
2411 sum += (guint64) (entry->sample_count) * entry->sample_delta;
2412 walker = g_list_next (walker);
2418 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
2420 trak->mdia.mdhd.time_info.duration =
2421 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
2422 trak->tkhd.duration =
2423 gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration, moov_timescale,
2424 trak->mdia.mdhd.time_info.timescale);
2428 atom_moov_get_timescale (AtomMOOV * moov)
2430 return moov->mvhd.time_info.timescale;
2434 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
2436 moov->mvhd.time_info.timescale = timescale;
2440 atom_moov_update_duration (AtomMOOV * moov)
2442 GList *traks = moov->traks;
2443 guint64 dur, duration = 0;
2446 AtomTRAK *trak = (AtomTRAK *) traks->data;
2448 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
2449 dur = atom_trak_get_duration (trak);
2452 traks = g_list_next (traks);
2454 moov->mvhd.time_info.duration = duration;
2458 atom_set_type (Atom * atom, guint32 fourcc)
2460 atom->type = fourcc;
2464 atom_stbl_set_64bits (AtomSTBL * stbl, gboolean use)
2467 atom_set_type (&stbl->stco64.header.header, FOURCC_co64);
2469 atom_set_type (&stbl->stco64.header.header, FOURCC_stco);
2474 atom_trak_set_64bits (AtomTRAK * trak, gboolean use)
2476 atom_stbl_set_64bits (&trak->mdia.minf.stbl, use);
2480 atom_moov_set_64bits (AtomMOOV * moov, gboolean large_file)
2482 GList *traks = moov->traks;
2485 AtomTRAK *trak = (AtomTRAK *) traks->data;
2487 atom_trak_set_64bits (trak, large_file);
2488 traks = g_list_next (traks);
2493 atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset)
2495 GList *entries = stco64->entries;
2498 guint64 *value = (guint64 *) entries->data;
2501 entries = g_list_next (entries);
2506 atom_moov_chunks_add_offset (AtomMOOV * moov, guint32 offset)
2508 GList *traks = moov->traks;
2511 AtomTRAK *trak = (AtomTRAK *) traks->data;
2513 atom_stco64_chunks_add_offset (&trak->mdia.minf.stbl.stco64, offset);
2514 traks = g_list_next (traks);
2519 * Meta tags functions
2522 atom_moov_init_metatags (AtomMOOV * moov)
2525 moov->udta = atom_udta_new ();
2527 if (!moov->udta->meta) {
2528 moov->udta->meta = atom_meta_new ();
2530 if (!moov->udta->meta->ilst) {
2531 moov->udta->meta->ilst = atom_ilst_new ();
2536 atom_tag_data_alloc_data (AtomTagData * data, guint size)
2538 if (data->data != NULL) {
2539 g_free (data->data);
2541 data->data = g_new0 (guint8, size);
2542 data->datalen = size;
2546 atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
2550 ilst = moov->udta->meta->ilst;
2551 ilst->entries = g_list_append (ilst->entries, tag);
2555 atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2556 const guint8 * data, guint size)
2561 tag = atom_tag_new (fourcc, flags);
2563 atom_tag_data_alloc_data (tdata, size);
2564 g_memmove (tdata->data, data, size);
2566 atom_moov_init_metatags (moov);
2567 atom_moov_append_tag (moov,
2568 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
2573 atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2575 gint len = strlen (value);
2578 atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
2582 atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2585 guint8 data[8] = { 0, };
2588 GST_WRITE_UINT16_BE (data, value);
2589 atom_moov_add_tag (moov, fourcc, flags, data, 2);
2591 GST_WRITE_UINT32_BE (data + 2, value);
2592 atom_moov_add_tag (moov, fourcc, flags, data, 8);
2597 atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
2599 AtomData *data_atom;
2607 /* blob is unparsed atom;
2608 * extract size and fourcc, and wrap remainder in data atom */
2609 len = GST_READ_UINT32_BE (data);
2610 fourcc = GST_READ_UINT32_LE (data + 4);
2614 buf = gst_buffer_new ();
2615 GST_BUFFER_SIZE (buf) = len - 8;
2616 GST_BUFFER_DATA (buf) = data + 8;
2618 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2619 gst_buffer_unref (buf);
2621 atom_moov_append_tag (moov,
2622 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2627 * Functions for specifying media types
2631 atom_minf_set_audio (AtomMINF * minf)
2633 atom_minf_clear_handlers (minf);
2634 minf->smhd = atom_smhd_new ();
2638 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
2640 atom_minf_clear_handlers (minf);
2641 minf->vmhd = atom_vmhd_new (context);
2645 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
2648 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2649 hdlr->component_type = comp_type;
2651 hdlr->handler_type = hdlr_type;
2655 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
2657 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
2661 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
2663 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
2667 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
2669 atom_mdia_set_hdlr_type_audio (mdia, context);
2670 atom_minf_set_audio (&mdia->minf);
2674 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
2676 atom_mdia_set_hdlr_type_video (mdia, context);
2677 atom_minf_set_video (&mdia->minf, context);
2681 atom_tkhd_set_audio (AtomTKHD * tkhd)
2683 tkhd->volume = 0x0100;
2684 tkhd->width = tkhd->height = 0;
2688 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
2693 /* qt and ISO base media do not contradict, and examples agree */
2694 tkhd->width = width;
2695 tkhd->height = height;
2698 /* re-negotiation is prevented at top-level, so only 1 entry expected.
2699 * Quite some more care here and elsewhere may be needed to
2700 * support several entries */
2701 static SampleTableEntryMP4A *
2702 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
2705 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
2706 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
2708 mp4a->se.header.type = type;
2709 mp4a->se.kind = AUDIO;
2710 mp4a->compression_id = -1;
2711 mp4a->se.data_reference_index = 1;
2713 stsd->entries = g_list_prepend (stsd->entries, mp4a);
2718 static SampleTableEntryMP4V *
2719 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
2722 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
2723 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
2725 mp4v->se.header.type = type;
2726 mp4v->se.kind = VIDEO;
2727 mp4v->se.data_reference_index = 1;
2728 mp4v->horizontal_resolution = 72 << 16;
2729 mp4v->vertical_resolution = 72 << 16;
2730 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2731 mp4v->spatial_quality = 512;
2732 mp4v->temporal_quality = 512;
2735 stsd->entries = g_list_prepend (stsd->entries, mp4v);
2741 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
2743 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
2747 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
2749 atom_tkhd_set_audio (&trak->tkhd);
2750 atom_mdia_set_audio (&trak->mdia, context);
2754 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
2757 atom_tkhd_set_video (&trak->tkhd, context, width, height);
2758 atom_mdia_set_video (&trak->mdia, context);
2762 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
2765 atom_trak_set_audio (trak, context);
2766 trak->mdia.mdhd.time_info.timescale = rate;
2770 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
2771 guint32 rate, guint32 width, guint32 height)
2773 atom_trak_set_video (trak, context, width, height);
2774 trak->mdia.mdhd.time_info.timescale = rate;
2775 trak->tkhd.width = width << 16;
2776 trak->tkhd.height = height << 16;
2780 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
2781 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
2783 SampleTableEntryMP4A *ste;
2785 atom_trak_set_audio_commons (trak, context, scale);
2786 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
2788 ste->version = entry->version;
2789 ste->compression_id = entry->compression_id;
2790 ste->sample_size = entry->sample_size;
2791 ste->sample_rate = entry->sample_rate << 16;
2792 ste->channels = entry->channels;
2794 ste->samples_per_packet = entry->samples_per_packet;
2795 ste->bytes_per_sample = entry->bytes_per_sample;
2796 ste->bytes_per_packet = entry->bytes_per_packet;
2797 ste->bytes_per_frame = entry->bytes_per_frame;
2800 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
2802 /* 0 size means variable size */
2803 atom_trak_set_constant_size_samples (trak, sample_size);
2807 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
2808 VisualSampleEntry * entry, guint32 scale, AtomInfo * ext)
2810 SampleTableEntryMP4V *ste;
2812 atom_trak_set_video_commons (trak, context, scale, entry->width,
2814 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
2816 ste->width = entry->width;
2817 ste->height = entry->height;
2818 ste->depth = entry->depth;
2819 ste->color_table_id = entry->color_table_id;
2820 ste->frame_count = entry->frame_count;
2823 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
2826 /* some sample description construction helpers */
2829 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
2830 const GstBuffer * codec_data)
2835 track_id = trak->tkhd.track_ID;
2837 esds = atom_esds_new ();
2838 esds->es.id = track_id & 0xFFFF;
2839 esds->es.dec_conf_desc.object_type = object_type;
2840 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
2842 /* optional DecoderSpecificInfo */
2844 DecoderSpecificInfoDescriptor *desc;
2846 esds->es.dec_conf_desc.dec_specific_info = desc =
2847 desc_dec_specific_info_new ();
2848 desc_dec_specific_info_alloc_data (desc, GST_BUFFER_SIZE (codec_data));
2850 memcpy (desc->data, GST_BUFFER_DATA (codec_data),
2851 GST_BUFFER_SIZE (codec_data));
2854 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
2859 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
2867 track_id = trak->tkhd.track_ID;
2869 /* Add WAVE atom to the MP4A sample table entry */
2870 wave = atom_wave_new ();
2872 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
2873 ext_atom = (Atom *) atom_data_new (FOURCC_null);
2874 wave->extension_atoms =
2875 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
2876 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
2878 /* Add ESDS atom to WAVE */
2879 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
2880 build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
2881 ESDS_STREAM_TYPE_AUDIO, codec_data));
2883 /* Add MP4A atom to the WAVE:
2884 * not really in spec, but makes offset based players happy */
2885 buf = gst_buffer_new_and_alloc (4);
2886 *((guint32 *) GST_BUFFER_DATA (buf)) = 0;
2887 ext_atom = (Atom *) atom_data_new_from_gst_buffer (FOURCC_mp4a, buf);
2888 gst_buffer_unref (buf);
2890 wave->extension_atoms =
2891 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
2892 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
2894 /* Add FRMA to the WAVE */
2895 frma = atom_frma_new ();
2896 frma->media_type = FOURCC_mp4a;
2898 wave->extension_atoms =
2899 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
2900 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
2902 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
2907 build_jp2h_extension (AtomTRAK * trak, gint width, gint height, guint32 fourcc)
2909 AtomData *atom_data;
2914 if (fourcc == GST_MAKE_FOURCC ('s', 'R', 'G', 'B')) {
2916 } else if (fourcc == GST_MAKE_FOURCC ('s', 'Y', 'U', 'V')) {
2921 buf = gst_buffer_new_and_alloc (22 + 12);
2922 data = GST_BUFFER_DATA (buf);
2924 /* ihdr = image header box */
2925 GST_WRITE_UINT32_BE (data, 22);
2926 GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
2927 GST_WRITE_UINT32_BE (data + 8, height);
2928 GST_WRITE_UINT32_BE (data + 12, width);
2929 /* FIXME perhaps parse from stream,
2930 * though exactly 3 in any respectable colourspace */
2931 GST_WRITE_UINT16_BE (data + 16, 3);
2932 /* 8 bits per component, unsigned */
2933 GST_WRITE_UINT8 (data + 18, 0x7);
2934 /* compression type; reserved */
2935 GST_WRITE_UINT8 (data + 19, 0x7);
2936 /* colour space (un)known */
2937 GST_WRITE_UINT8 (data + 20, 0x0);
2938 /* intellectual property right (box present) */
2939 GST_WRITE_UINT8 (data + 21, 0x0);
2941 /* colour specification box */
2943 GST_WRITE_UINT32_BE (data, 12);
2944 GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
2945 /* specification method: enumerated */
2946 GST_WRITE_UINT8 (data + 8, 0x1);
2947 /* precedence; reserved */
2948 GST_WRITE_UINT8 (data + 9, 0x0);
2949 /* approximation; reserved */
2950 GST_WRITE_UINT8 (data + 10, 0x0);
2951 /* enumerated colourspace */
2952 GST_WRITE_UINT8 (data + 11, cenum);
2954 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
2955 gst_buffer_unref (buf);
2957 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
2962 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
2965 AtomInfo *result = NULL;
2968 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
2969 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,