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_elst_init (AtomELST * elst)
310 guint8 flags[3] = { 0, 0, 0 };
311 atom_full_init (&elst->header, FOURCC_elst, 0, 0, 0, flags);
316 atom_elst_clear (AtomELST * elst)
320 atom_full_clear (&elst->header);
321 walker = elst->entries;
323 g_free ((EditListEntry *) walker->data);
324 walker = g_slist_next (walker);
326 g_slist_free (elst->entries);
330 atom_edts_init (AtomEDTS * edts)
332 atom_header_set (&edts->header, FOURCC_edts, 0, 0);
333 atom_elst_init (&edts->elst);
337 atom_edts_clear (AtomEDTS * edts)
339 atom_clear (&edts->header);
340 atom_elst_clear (&edts->elst);
346 AtomEDTS *edts = g_new0 (AtomEDTS, 1);
347 atom_edts_init (edts);
352 atom_edts_free (AtomEDTS * edts)
354 atom_edts_clear (edts);
359 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
361 atom_header_set (&se->header, type, 0, 0);
363 memset (se->reserved, 0, sizeof (guint8) * 6);
364 se->data_reference_index = 0;
368 atom_sample_entry_free (SampleTableEntry * se)
370 atom_clear (&se->header);
374 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
376 atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
379 mp4a->revision_level = 0;
382 mp4a->sample_size = 16;
383 mp4a->compression_id = 0;
384 mp4a->packet_size = 0;
385 mp4a->sample_rate = 0;
386 /* following only used if version is 1 */
387 mp4a->samples_per_packet = 0;
388 mp4a->bytes_per_packet = 0;
389 mp4a->bytes_per_frame = 0;
390 mp4a->bytes_per_sample = 0;
392 mp4a->extension_atoms = NULL;
395 static SampleTableEntryMP4A *
396 sample_entry_mp4a_new ()
398 SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
400 sample_entry_mp4a_init (mp4a);
405 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
407 atom_sample_entry_free (&mp4a->se);
408 atom_info_list_free (mp4a->extension_atoms);
413 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
415 atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
418 mp4v->revision_level = 0;
421 mp4v->temporal_quality = 0;
422 mp4v->spatial_quality = 0;
424 /* qt and ISO base media do not contradict, and examples agree */
425 mp4v->horizontal_resolution = 0x00480000;
426 mp4v->vertical_resolution = 0x00480000;
429 mp4v->frame_count = 1;
431 memset (mp4v->compressor, 0, sizeof (guint8) * 32);
434 mp4v->color_table_id = 0;
436 mp4v->extension_atoms = NULL;
440 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
442 atom_sample_entry_free (&mp4v->se);
443 atom_info_list_free (mp4v->extension_atoms);
447 static SampleTableEntryMP4V *
448 sample_entry_mp4v_new (AtomsContext * context)
450 SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
452 sample_entry_mp4v_init (mp4v, context);
457 atom_stsd_init (AtomSTSD * stsd)
459 guint8 flags[3] = { 0, 0, 0 };
461 atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
462 stsd->entries = NULL;
466 atom_stsd_clear (AtomSTSD * stsd)
470 atom_full_clear (&stsd->header);
471 walker = stsd->entries;
474 SampleTableEntry *se = (SampleTableEntry *) aux->data;
476 walker = g_list_next (walker);
477 stsd->entries = g_list_remove_link (stsd->entries, aux);
481 sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
484 sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
487 /* best possible cleanup */
488 atom_sample_entry_free (se);
495 atom_ctts_init (AtomCTTS * ctts)
497 guint8 flags[3] = { 0, 0, 0 };
499 atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
500 ctts->entries = NULL;
506 AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
508 atom_ctts_init (ctts);
513 atom_ctts_free (AtomCTTS * ctts)
517 atom_full_clear (&ctts->header);
518 walker = ctts->entries;
522 walker = g_list_next (walker);
523 ctts->entries = g_list_remove_link (ctts->entries, aux);
524 g_free ((CTTSEntry *) aux->data);
531 atom_stts_init (AtomSTTS * stts)
533 guint8 flags[3] = { 0, 0, 0 };
535 atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
536 stts->entries = NULL;
540 atom_stts_clear (AtomSTTS * stts)
544 atom_full_clear (&stts->header);
545 walker = stts->entries;
549 walker = g_list_next (walker);
550 stts->entries = g_list_remove_link (stts->entries, aux);
551 g_free ((STTSEntry *) aux->data);
558 atom_stsz_init (AtomSTSZ * stsz)
560 guint8 flags[3] = { 0, 0, 0 };
562 atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
563 stsz->sample_size = 0;
564 stsz->table_size = 0;
565 stsz->entries = NULL;
569 atom_stsz_clear (AtomSTSZ * stsz)
571 atom_full_clear (&stsz->header);
572 g_list_free (stsz->entries);
573 stsz->entries = NULL;
574 stsz->table_size = 0;
578 atom_stsc_init (AtomSTSC * stsc)
580 guint8 flags[3] = { 0, 0, 0 };
582 atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
583 stsc->entries = NULL;
588 atom_stsc_clear (AtomSTSC * stsc)
592 atom_full_clear (&stsc->header);
593 walker = stsc->entries;
597 walker = g_list_next (walker);
598 stsc->entries = g_list_remove_link (stsc->entries, aux);
599 g_free ((STSCEntry *) aux->data);
606 atom_co64_init (AtomSTCO64 * co64)
608 guint8 flags[3] = { 0, 0, 0 };
610 atom_full_init (&co64->header, FOURCC_co64, 0, 0, 0, flags);
611 co64->entries = NULL;
616 atom_stco64_clear (AtomSTCO64 * stco64)
620 atom_full_clear (&stco64->header);
621 walker = stco64->entries;
625 walker = g_list_next (walker);
626 stco64->entries = g_list_remove_link (stco64->entries, aux);
627 g_free ((guint64 *) aux->data);
630 stco64->n_entries = 0;
634 atom_stss_init (AtomSTSS * stss)
636 guint8 flags[3] = { 0, 0, 0 };
638 atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
639 stss->entries = NULL;
644 atom_stss_clear (AtomSTSS * stss)
646 atom_full_clear (&stss->header);
647 g_list_free (stss->entries);
648 stss->entries = NULL;
653 atom_stbl_init (AtomSTBL * stbl)
655 atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
657 atom_stts_init (&stbl->stts);
658 atom_stss_init (&stbl->stss);
659 atom_stsd_init (&stbl->stsd);
660 atom_stsz_init (&stbl->stsz);
661 atom_stsc_init (&stbl->stsc);
664 atom_co64_init (&stbl->stco64);
668 atom_stbl_clear (AtomSTBL * stbl)
670 atom_clear (&stbl->header);
671 atom_stsd_clear (&stbl->stsd);
672 atom_stts_clear (&stbl->stts);
673 atom_stss_clear (&stbl->stss);
674 atom_stsc_clear (&stbl->stsc);
675 atom_stsz_clear (&stbl->stsz);
677 atom_ctts_free (stbl->ctts);
679 atom_stco64_clear (&stbl->stco64);
683 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
685 guint8 flags[3] = { 0, 0, 1 };
687 atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
688 vmhd->graphics_mode = 0x0;
689 memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
691 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
692 vmhd->graphics_mode = 0x40;
693 vmhd->opcolor[0] = 32768;
694 vmhd->opcolor[1] = 32768;
695 vmhd->opcolor[2] = 32768;
700 atom_vmhd_new (AtomsContext * context)
702 AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
704 atom_vmhd_init (vmhd, context);
709 atom_vmhd_free (AtomVMHD * vmhd)
711 atom_full_clear (&vmhd->header);
716 atom_smhd_init (AtomSMHD * smhd)
718 guint8 flags[3] = { 0, 0, 0 };
720 atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
728 AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
730 atom_smhd_init (smhd);
735 atom_smhd_free (AtomSMHD * smhd)
737 atom_full_clear (&smhd->header);
742 atom_hmhd_free (AtomHMHD * hmhd)
744 atom_full_clear (&hmhd->header);
749 atom_hdlr_init (AtomHDLR * hdlr)
751 guint8 flags[3] = { 0, 0, 0 };
753 atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
755 hdlr->component_type = 0;
756 hdlr->handler_type = 0;
757 hdlr->manufacturer = 0;
759 hdlr->flags_mask = 0;
760 hdlr->name = g_strdup ("");
766 AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
768 atom_hdlr_init (hdlr);
773 atom_hdlr_clear (AtomHDLR * hdlr)
775 atom_full_clear (&hdlr->header);
783 atom_hdlr_free (AtomHDLR * hdlr)
785 atom_hdlr_clear (hdlr);
790 atom_url_init (AtomURL * url)
792 guint8 flags[3] = { 0, 0, 1 };
794 atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
795 url->location = NULL;
799 atom_url_free (AtomURL * url)
801 atom_full_clear (&url->header);
803 g_free (url->location);
804 url->location = NULL;
812 AtomURL *url = g_new0 (AtomURL, 1);
821 guint8 flags[3] = { 0, 0, 1 };
822 AtomFull *alis = g_new0 (AtomFull, 1);
824 atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
829 atom_dref_init (AtomDREF * dref, AtomsContext * context)
831 guint8 flags[3] = { 0, 0, 0 };
833 atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
835 /* in either case, alis or url init arranges to set self-contained flag */
836 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
837 /* alis dref for qt */
838 AtomFull *alis = atom_alis_new ();
839 dref->entries = g_list_append (dref->entries, alis);
841 /* url for iso spec, as 'alis' not specified there */
842 AtomURL *url = atom_url_new ();
843 dref->entries = g_list_append (dref->entries, url);
848 atom_dref_clear (AtomDREF * dref)
852 atom_full_clear (&dref->header);
853 walker = dref->entries;
856 Atom *atom = (Atom *) aux->data;
858 walker = g_list_next (walker);
859 dref->entries = g_list_remove_link (dref->entries, aux);
860 switch (atom->type) {
862 atom_full_free ((AtomFull *) atom);
865 atom_url_free ((AtomURL *) atom);
868 /* we do nothing, better leak than crash */
876 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
878 atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
879 atom_dref_init (&dinf->dref, context);
883 atom_dinf_clear (AtomDINF * dinf)
885 atom_clear (&dinf->header);
886 atom_dref_clear (&dinf->dref);
890 atom_minf_init (AtomMINF * minf, AtomsContext * context)
892 atom_header_set (&minf->header, FOURCC_minf, 0, 0);
898 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
899 minf->hdlr = atom_hdlr_new ();
900 minf->hdlr->component_type = FOURCC_dhlr;
901 minf->hdlr->handler_type = FOURCC_alis;
905 atom_dinf_init (&minf->dinf, context);
906 atom_stbl_init (&minf->stbl);
910 atom_minf_clear_handlers (AtomMINF * minf)
913 atom_vmhd_free (minf->vmhd);
917 atom_smhd_free (minf->smhd);
921 atom_hmhd_free (minf->hmhd);
927 atom_minf_clear (AtomMINF * minf)
929 atom_clear (&minf->header);
930 atom_minf_clear_handlers (minf);
932 atom_hdlr_free (minf->hdlr);
934 atom_dinf_clear (&minf->dinf);
935 atom_stbl_clear (&minf->stbl);
939 atom_mdhd_init (AtomMDHD * mdhd)
941 guint8 flags[3] = { 0, 0, 0 };
943 atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
944 common_time_info_init (&mdhd->time_info);
945 mdhd->language_code = 0;
950 atom_mdhd_clear (AtomMDHD * mdhd)
952 atom_full_clear (&mdhd->header);
956 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
958 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
960 atom_mdhd_init (&mdia->mdhd);
961 atom_hdlr_init (&mdia->hdlr);
962 atom_minf_init (&mdia->minf, context);
966 atom_mdia_clear (AtomMDIA * mdia)
968 atom_clear (&mdia->header);
969 atom_mdhd_clear (&mdia->mdhd);
970 atom_hdlr_clear (&mdia->hdlr);
971 atom_minf_clear (&mdia->minf);
975 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
980 * 2 -> track in movie
981 * 4 -> track in preview
983 guint8 flags[3] = { 0, 0, 7 };
985 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
987 tkhd->creation_time = tkhd->modification_time = get_current_qt_time ();
992 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
994 tkhd->alternate_group = 0;
997 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
998 tkhd->matrix[0] = 1 << 16;
999 tkhd->matrix[4] = 1 << 16;
1000 tkhd->matrix[8] = 16384 << 16;
1006 atom_tkhd_clear (AtomTKHD * tkhd)
1008 atom_full_clear (&tkhd->header);
1012 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
1014 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
1016 atom_tkhd_init (&trak->tkhd, context);
1018 atom_mdia_init (&trak->mdia, context);
1022 atom_trak_new (AtomsContext * context)
1024 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
1026 atom_trak_init (trak, context);
1031 atom_trak_free (AtomTRAK * trak)
1033 atom_clear (&trak->header);
1034 atom_tkhd_clear (&trak->tkhd);
1036 atom_edts_free (trak->edts);
1037 atom_mdia_clear (&trak->mdia);
1042 atom_ilst_init (AtomILST * ilst)
1044 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
1045 ilst->entries = NULL;
1051 AtomILST *ilst = g_new0 (AtomILST, 1);
1053 atom_ilst_init (ilst);
1058 atom_ilst_free (AtomILST * ilst)
1061 atom_info_list_free (ilst->entries);
1062 atom_clear (&ilst->header);
1067 atom_meta_init (AtomMETA * meta)
1069 guint8 flags[3] = { 0, 0, 0 };
1071 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1072 atom_hdlr_init (&meta->hdlr);
1073 /* FIXME (ISOM says this is always 0) */
1074 meta->hdlr.component_type = FOURCC_mhlr;
1075 meta->hdlr.handler_type = FOURCC_mdir;
1082 AtomMETA *meta = g_new0 (AtomMETA, 1);
1084 atom_meta_init (meta);
1089 atom_meta_free (AtomMETA * meta)
1091 atom_full_clear (&meta->header);
1092 atom_hdlr_clear (&meta->hdlr);
1094 atom_ilst_free (meta->ilst);
1100 atom_udta_init (AtomUDTA * udta)
1102 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1109 AtomUDTA *udta = g_new0 (AtomUDTA, 1);
1111 atom_udta_init (udta);
1116 atom_udta_free (AtomUDTA * udta)
1118 atom_clear (&udta->header);
1120 atom_meta_free (udta->meta);
1123 atom_info_list_free (udta->entries);
1128 atom_tag_data_init (AtomTagData * data)
1130 guint8 flags[] = { 0, 0, 0 };
1132 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1136 atom_tag_data_clear (AtomTagData * data)
1138 atom_full_clear (&data->header);
1139 g_free (data->data);
1144 * Fourcc is the tag fourcc
1145 * flags will be truncated to 24bits
1148 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1150 AtomTag *tag = g_new0 (AtomTag, 1);
1152 tag->header.type = fourcc;
1153 atom_tag_data_init (&tag->data);
1154 tag->data.header.flags[2] = flags_as_uint & 0xFF;
1155 tag->data.header.flags[1] = (flags_as_uint & 0xFF00) >> 8;
1156 tag->data.header.flags[0] = (flags_as_uint & 0xFF0000) >> 16;
1161 atom_tag_free (AtomTag * tag)
1163 atom_clear (&tag->header);
1164 atom_tag_data_clear (&tag->data);
1169 atom_mvhd_init (AtomMVHD * mvhd)
1171 guint8 flags[3] = { 0, 0, 0 };
1173 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1175 common_time_info_init (&mvhd->time_info);
1177 mvhd->prefered_rate = 1 << 16;
1178 mvhd->volume = 1 << 8;
1179 mvhd->reserved3 = 0;
1180 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1182 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1183 mvhd->matrix[0] = 1 << 16;
1184 mvhd->matrix[4] = 1 << 16;
1185 mvhd->matrix[8] = 16384 << 16;
1187 mvhd->preview_time = 0;
1188 mvhd->preview_duration = 0;
1189 mvhd->poster_time = 0;
1190 mvhd->selection_time = 0;
1191 mvhd->selection_duration = 0;
1192 mvhd->current_time = 0;
1194 mvhd->next_track_id = 1;
1198 atom_mvhd_clear (AtomMVHD * mvhd)
1200 atom_full_clear (&mvhd->header);
1204 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1206 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1207 atom_mvhd_init (&(moov->mvhd));
1210 moov->context = *context;
1214 atom_moov_new (AtomsContext * context)
1216 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1218 atom_moov_init (moov, context);
1223 atom_moov_free (AtomMOOV * moov)
1227 atom_clear (&moov->header);
1228 atom_mvhd_clear (&moov->mvhd);
1230 walker = moov->traks;
1232 atom_trak_free ((AtomTRAK *) walker->data);
1233 walker = g_list_next (walker);
1235 g_list_free (moov->traks);
1239 atom_udta_free (moov->udta);
1246 /* -- end of init / free -- */
1248 /* -- copy data functions -- */
1251 atom_full_get_version (AtomFull * full)
1253 return full->version;
1257 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1258 guint8 ** buffer, guint64 * size, guint64 * offset)
1260 guint64 original_offset = *offset;
1263 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1264 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1265 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1266 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1268 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1269 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1270 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1271 prop_copy_uint64 (ti->duration, buffer, size, offset);
1273 return *offset - original_offset;
1277 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1280 /* this only works for non-extended atom size, which is OK
1281 * (though it could be made to do mem_move, etc and write extended size) */
1282 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1286 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1288 guint64 original_offset = *offset;
1290 /* copies type and size */
1291 prop_copy_uint32 (atom->size, buffer, size, offset);
1292 prop_copy_fourcc (atom->type, buffer, size, offset);
1294 /* extended size needed */
1295 if (atom->size == 1) {
1296 /* really should not happen other than with mdat atom;
1297 * would be a problem for size (re)write code, not to mention memory */
1298 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1299 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1302 return *offset - original_offset;
1306 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1309 guint64 original_offset = *offset;
1311 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1315 prop_copy_uint8 (atom->version, buffer, size, offset);
1316 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1318 atom_write_size (buffer, size, offset, original_offset);
1319 return *offset - original_offset;
1323 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1326 guint64 original_offset = *offset;
1329 AtomInfo *info = (AtomInfo *) ai->data;
1331 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1334 ai = g_list_next (ai);
1337 return *offset - original_offset;
1341 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1344 guint64 original_offset = *offset;
1346 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1350 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1352 atom_write_size (buffer, size, offset, original_offset);
1353 return *offset - original_offset;
1357 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1360 guint64 original_offset = *offset;
1362 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1365 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1366 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1368 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1369 buffer, size, offset);
1371 atom_write_size (buffer, size, offset, original_offset);
1372 return *offset - original_offset;
1376 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1380 guint64 original_offset = *offset;
1382 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1386 version = atom_full_get_version (&(atom->header));
1388 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1389 } else if (version == 1) {
1390 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1392 *offset = original_offset;
1396 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1397 prop_copy_uint16 (atom->volume, buffer, size, offset);
1398 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1399 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1400 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1401 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1402 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1403 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1404 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1405 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1406 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1408 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1410 atom_write_size (buffer, size, offset, original_offset);
1411 return *offset - original_offset;
1415 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1418 guint64 original_offset = *offset;
1420 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1424 if (atom_full_get_version (&tkhd->header) == 0) {
1425 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1426 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1427 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1428 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1429 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1431 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1432 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1433 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1434 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1435 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1438 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1439 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1440 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1441 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1442 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1443 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1445 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1446 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1448 atom_write_size (buffer, size, offset, original_offset);
1449 return *offset - original_offset;
1453 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1456 guint64 original_offset = *offset;
1458 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1462 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1463 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1464 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1465 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1466 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1468 prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
1470 atom_write_size (buffer, size, offset, original_offset);
1471 return *offset - original_offset;
1475 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1478 guint64 original_offset = *offset;
1480 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1483 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1484 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1486 atom_write_size (buffer, size, offset, original_offset);
1487 return original_offset - *offset;
1491 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1494 guint64 original_offset = *offset;
1496 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1499 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1500 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1502 atom_write_size (buffer, size, offset, original_offset);
1503 return original_offset - *offset;
1507 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1510 guint64 original_offset = *offset;
1512 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1515 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1516 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1517 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1518 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1519 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1521 atom_write_size (buffer, size, offset, original_offset);
1522 return original_offset - *offset;
1526 atom_url_same_file_flag (AtomURL * url)
1528 return (url->header.flags[2] & 0x1) == 1;
1532 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1535 guint64 original_offset = *offset;
1537 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1541 if (!atom_url_same_file_flag (url)) {
1542 prop_copy_null_terminated_string (url->location, buffer, size, offset);
1545 atom_write_size (buffer, size, offset, original_offset);
1546 return original_offset - *offset;
1550 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1553 guint64 original_offset = *offset;
1556 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1560 prop_copy_uint32 (stts->n_entries, buffer, size, offset);
1561 /* minimize realloc */
1562 prop_copy_ensure_buffer (buffer, size, offset, 8 * stts->n_entries);
1563 for (walker = g_list_last (stts->entries); walker != NULL;
1564 walker = g_list_previous (walker)) {
1565 STTSEntry *entry = (STTSEntry *) walker->data;
1567 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
1568 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
1571 atom_write_size (buffer, size, offset, original_offset);
1572 return *offset - original_offset;
1576 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
1577 guint64 * size, guint64 * offset)
1579 guint64 original_offset = *offset;
1581 if (!atom_copy_data (&se->header, buffer, size, offset)) {
1585 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
1586 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
1588 return *offset - original_offset;
1592 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
1595 guint64 original_offset = *offset;
1597 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
1600 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
1604 atom_write_size (buffer, size, offset, original_offset);
1605 return *offset - original_offset;
1609 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
1610 guint64 * size, guint64 * offset)
1612 guint64 original_offset = *offset;
1614 if (!atom_copy_data (&(frma->header), buffer, size, offset))
1617 prop_copy_fourcc (frma->media_type, buffer, size, offset);
1619 atom_write_size (buffer, size, offset, original_offset);
1620 return *offset - original_offset;
1624 atom_mp4s_copy_data (SampleTableEntryMP4S * mp4s, guint8 ** buffer,
1625 guint64 * size, guint64 * offset)
1627 guint64 original_offset = *offset;
1629 if (!atom_sample_entry_copy_data (&mp4s->se, buffer, size, offset)) {
1632 if (!atom_esds_copy_data (&mp4s->es, buffer, size, offset)) {
1636 atom_write_size (buffer, size, offset, original_offset);
1637 return *offset - original_offset;
1641 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
1642 guint64 * size, guint64 * offset)
1644 guint64 original_offset = *offset;
1646 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
1650 prop_copy_uint32 (hse->size, buffer, size, offset);
1651 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
1653 atom_write_size (buffer, size, offset, original_offset);
1654 return *offset - original_offset;
1658 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
1659 guint64 * size, guint64 * offset)
1661 guint64 original_offset = *offset;
1663 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
1667 prop_copy_uint16 (mp4a->version, buffer, size, offset);
1668 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
1669 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
1670 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
1671 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
1672 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
1673 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
1674 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
1676 /* this should always be 0 for mp4 flavor */
1677 if (mp4a->version == 1) {
1678 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
1679 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
1680 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
1681 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
1684 if (mp4a->extension_atoms) {
1685 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
1689 atom_write_size (buffer, size, offset, original_offset);
1690 return *offset - original_offset;
1694 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
1695 guint64 * size, guint64 * offset)
1697 guint64 original_offset = *offset;
1699 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
1703 prop_copy_uint16 (mp4v->version, buffer, size, offset);
1704 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
1705 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
1706 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
1707 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
1709 prop_copy_uint16 (mp4v->width, buffer, size, offset);
1710 prop_copy_uint16 (mp4v->height, buffer, size, offset);
1712 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
1713 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
1714 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
1716 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
1718 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
1721 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
1722 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
1725 if (mp4v->extension_atoms &&
1726 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
1729 atom_write_size (buffer, size, offset, original_offset);
1730 return *offset - original_offset;
1734 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
1737 guint64 original_offset = *offset;
1740 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
1744 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
1745 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
1746 /* minimize realloc */
1747 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
1748 if (stsz->sample_size == 0) {
1749 for (walker = g_list_last (stsz->entries); walker != NULL;
1750 walker = g_list_previous (walker)) {
1751 prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
1756 atom_write_size (buffer, size, offset, original_offset);
1757 return *offset - original_offset;
1761 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
1764 guint64 original_offset = *offset;
1767 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
1771 prop_copy_uint32 (stsc->n_entries, buffer, size, offset);
1772 /* minimize realloc */
1773 prop_copy_ensure_buffer (buffer, size, offset, 12 * stsc->n_entries);
1775 for (walker = g_list_last (stsc->entries); walker != NULL;
1776 walker = g_list_previous (walker)) {
1777 STSCEntry *entry = (STSCEntry *) walker->data;
1779 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
1780 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
1781 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
1784 atom_write_size (buffer, size, offset, original_offset);
1785 return *offset - original_offset;
1789 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
1792 guint64 original_offset = *offset;
1795 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
1799 prop_copy_uint32 (ctts->n_entries, buffer, size, offset);
1800 /* minimize realloc */
1801 prop_copy_ensure_buffer (buffer, size, offset, 8 * ctts->n_entries);
1802 for (walker = g_list_last (ctts->entries); walker != NULL;
1803 walker = g_list_previous (walker)) {
1804 CTTSEntry *entry = (CTTSEntry *) walker->data;
1806 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
1807 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
1810 atom_write_size (buffer, size, offset, original_offset);
1811 return *offset - original_offset;
1815 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
1818 guint64 original_offset = *offset;
1820 gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
1822 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
1826 prop_copy_uint32 (stco64->n_entries, buffer, size, offset);
1828 /* minimize realloc */
1829 prop_copy_ensure_buffer (buffer, size, offset, 8 * stco64->n_entries);
1830 for (walker = g_list_last (stco64->entries); walker != NULL;
1831 walker = g_list_previous (walker)) {
1832 guint64 *value = (guint64 *) walker->data;
1835 prop_copy_uint32 ((guint32) * value, buffer, size, offset);
1837 prop_copy_uint64 (*value, buffer, size, offset);
1841 atom_write_size (buffer, size, offset, original_offset);
1842 return *offset - original_offset;
1846 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
1849 guint64 original_offset = *offset;
1852 if (stss->entries == NULL) {
1853 /* FIXME not needing this atom might be confused with error while copying */
1857 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
1861 prop_copy_uint32 (stss->n_entries, buffer, size, offset);
1862 /* minimize realloc */
1863 prop_copy_ensure_buffer (buffer, size, offset, 4 * stss->n_entries);
1864 for (walker = g_list_last (stss->entries); walker != NULL;
1865 walker = g_list_previous (walker)) {
1866 prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
1870 atom_write_size (buffer, size, offset, original_offset);
1871 return *offset - original_offset;
1875 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
1878 guint64 original_offset = *offset;
1881 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
1885 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
1887 for (walker = g_list_last (stsd->entries); walker != NULL;
1888 walker = g_list_previous (walker)) {
1889 SampleTableEntry *se = (SampleTableEntry *) walker->data;
1891 switch (((Atom *) walker->data)->type) {
1893 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
1894 buffer, size, offset)) {
1899 if (!atom_mp4s_copy_data ((SampleTableEntryMP4S *) walker->data,
1900 buffer, size, offset)) {
1905 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
1906 buffer, size, offset)) {
1911 if (se->kind == VIDEO) {
1912 size += sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
1913 walker->data, buffer, size, offset);
1914 } else if (se->kind == AUDIO) {
1915 size += sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
1916 walker->data, buffer, size, offset);
1918 if (!atom_hint_sample_entry_copy_data (
1919 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
1927 atom_write_size (buffer, size, offset, original_offset);
1928 return *offset - original_offset;
1932 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
1935 guint64 original_offset = *offset;
1937 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
1941 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
1944 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
1947 /* this atom is optional, so let's check if we need it
1948 * (to avoid false error) */
1949 if (stbl->stss.entries) {
1950 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
1955 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
1958 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
1962 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
1966 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
1970 atom_write_size (buffer, size, offset, original_offset);
1971 return original_offset - *offset;
1976 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
1979 guint64 original_offset = *offset;
1982 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
1986 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
1988 walker = dref->entries;
1989 while (walker != NULL) {
1990 Atom *atom = (Atom *) walker->data;
1992 if (atom->type == FOURCC_url_) {
1993 atom_url_copy_data ((AtomURL *) atom, buffer, size, offset);
1994 } else if (atom->type == FOURCC_alis) {
1995 atom_full_copy_data ((AtomFull *) atom, buffer, size, offset);
1997 g_error ("Unsupported atom used inside dref atom");
1999 walker = g_list_next (walker);
2002 atom_write_size (buffer, size, offset, original_offset);
2003 return *offset - original_offset;
2007 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
2010 guint64 original_offset = *offset;
2012 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
2016 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
2020 atom_write_size (buffer, size, offset, original_offset);
2021 return original_offset - *offset;
2025 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
2028 guint64 original_offset = *offset;
2030 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
2035 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
2038 } else if (minf->smhd) {
2039 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
2042 } else if (minf->hmhd) {
2043 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
2049 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2054 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2057 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2061 atom_write_size (buffer, size, offset, original_offset);
2062 return *offset - original_offset;
2066 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2069 guint64 original_offset = *offset;
2071 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2075 if (!common_time_info_copy_data (&mdhd->time_info,
2076 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2080 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2081 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2083 atom_write_size (buffer, size, offset, original_offset);
2084 return *offset - original_offset;
2088 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2091 guint64 original_offset = *offset;
2093 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2096 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2099 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2103 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2107 atom_write_size (buffer, size, offset, original_offset);
2108 return *offset - original_offset;
2112 atom_elst_copy_data (AtomELST * elst, guint8 ** buffer, guint64 * size,
2115 guint64 original_offset = *offset;
2118 if (!atom_full_copy_data (&elst->header, buffer, size, offset)) {
2122 prop_copy_uint32 (g_slist_length (elst->entries), buffer, size, offset);
2124 for (walker = elst->entries; walker != NULL; walker = g_slist_next (walker)) {
2125 EditListEntry *entry = (EditListEntry *) walker->data;
2126 prop_copy_uint32 (entry->duration, buffer, size, offset);
2127 prop_copy_uint32 (entry->media_time, buffer, size, offset);
2128 prop_copy_uint32 (entry->media_rate, buffer, size, offset);
2130 atom_write_size (buffer, size, offset, original_offset);
2131 return *offset - original_offset;
2135 atom_edts_copy_data (AtomEDTS * edts, guint8 ** buffer, guint64 * size,
2138 guint64 original_offset = *offset;
2140 if (!atom_copy_data (&(edts->header), buffer, size, offset))
2143 if (!atom_elst_copy_data (&(edts->elst), buffer, size, offset))
2146 atom_write_size (buffer, size, offset, original_offset);
2147 return *offset - original_offset;
2151 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2154 guint64 original_offset = *offset;
2156 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2159 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2163 if (!atom_edts_copy_data (trak->edts, buffer, size, offset)) {
2168 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2172 atom_write_size (buffer, size, offset, original_offset);
2173 return *offset - original_offset;
2177 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2180 guint64 original_offset = *offset;
2182 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2186 prop_copy_uint32 (data->reserved, buffer, size, offset);
2187 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2189 atom_write_size (buffer, size, offset, original_offset);
2190 return *offset - original_offset;
2194 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2197 guint64 original_offset = *offset;
2199 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2203 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2207 atom_write_size (buffer, size, offset, original_offset);
2208 return *offset - original_offset;
2212 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2215 guint64 original_offset = *offset;
2217 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2221 if (ilst->entries &&
2222 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2225 atom_write_size (buffer, size, offset, original_offset);
2226 return *offset - original_offset;
2230 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2233 guint64 original_offset = *offset;
2235 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2238 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2242 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2247 atom_write_size (buffer, size, offset, original_offset);
2248 return *offset - original_offset;
2252 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2255 guint64 original_offset = *offset;
2257 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2261 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2264 } else if (udta->entries) {
2266 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2270 atom_write_size (buffer, size, offset, original_offset);
2271 return *offset - original_offset;
2275 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2278 guint64 original_offset = *offset;
2281 if (!atom_copy_data (&(atom->header), buffer, size, offset))
2284 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2287 walker = g_list_first (atom->traks);
2288 while (walker != NULL) {
2289 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2292 walker = g_list_next (walker);
2296 if (!atom_udta_copy_data (atom->udta, buffer, size, offset)) {
2301 atom_write_size (buffer, size, offset, original_offset);
2302 return *offset - original_offset;
2306 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
2307 guint64 * size, guint64 * offset)
2309 guint64 original_offset = *offset;
2311 if (!atom_copy_data (&(wave->header), buffer, size, offset))
2314 if (wave->extension_atoms) {
2315 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
2319 atom_write_size (buffer, size, offset, original_offset);
2320 return *offset - original_offset;
2323 /* -- end of copy data functions -- */
2325 /* -- general functions, API and support functions */
2327 /* add samples to tables */
2330 stsc_entry_new (guint32 first_chunk, guint32 samples, guint32 desc_index)
2332 STSCEntry *entry = g_new0 (STSCEntry, 1);
2334 entry->first_chunk = first_chunk;
2335 entry->samples_per_chunk = samples;
2336 entry->sample_description_index = desc_index;
2341 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
2343 if (stsc->entries &&
2344 ((STSCEntry *) stsc->entries->data)->samples_per_chunk == nsamples)
2348 g_list_prepend (stsc->entries, stsc_entry_new (first_chunk, nsamples, 1));
2353 stts_entry_new (guint32 sample_count, gint32 sample_delta)
2355 STTSEntry *entry = g_new0 (STTSEntry, 1);
2357 entry->sample_count = sample_count;
2358 entry->sample_delta = sample_delta;
2363 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
2367 if (stts->entries == NULL) {
2369 g_list_prepend (stts->entries, stts_entry_new (sample_count,
2374 entry = (STTSEntry *) g_list_first (stts->entries)->data;
2375 if (entry->sample_delta == sample_delta) {
2376 entry->sample_count += sample_count;
2379 g_list_prepend (stts->entries, stts_entry_new (sample_count,
2386 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
2390 stsz->table_size += nsamples;
2391 if (stsz->sample_size != 0) {
2392 /* it is constant size, we don't need entries */
2395 for (i = 0; i < nsamples; i++) {
2396 stsz->entries = g_list_prepend (stsz->entries, GUINT_TO_POINTER (size));
2401 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
2403 return stco64->n_entries;
2407 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
2409 guint64 *pont = g_new (guint64, 1);
2412 stco64->entries = g_list_prepend (stco64->entries, pont);
2413 stco64->n_entries++;
2417 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
2419 stss->entries = g_list_prepend (stss->entries, GUINT_TO_POINTER (sample));
2424 atom_stbl_add_stss_entry (AtomSTBL * stbl)
2426 guint32 sample_index = stbl->stsz.table_size;
2428 atom_stss_add_entry (&stbl->stss, sample_index);
2432 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
2437 walker = g_list_first (ctts->entries);
2438 entry = (walker == NULL) ? NULL : (CTTSEntry *) walker->data;
2440 if (entry == NULL || entry->sampleoffset != offset) {
2441 CTTSEntry *entry = g_new0 (CTTSEntry, 1);
2443 entry->samplecount = nsamples;
2444 entry->sampleoffset = offset;
2445 ctts->entries = g_list_prepend (ctts->entries, entry);
2448 entry->samplecount += nsamples;
2453 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
2455 if (stbl->ctts == NULL) {
2456 stbl->ctts = atom_ctts_new ();
2458 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
2462 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
2463 guint32 size, guint64 chunk_offset, gboolean sync,
2464 gboolean do_pts, gint64 pts_offset)
2466 AtomSTBL *stbl = &trak->mdia.minf.stbl;
2468 atom_stts_add_entry (&stbl->stts, nsamples, delta);
2469 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
2470 atom_stco64_add_entry (&stbl->stco64, chunk_offset);
2471 atom_stsc_add_new_entry (&stbl->stsc,
2472 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
2474 atom_stbl_add_stss_entry (stbl);
2476 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
2479 /* trak and moov molding */
2482 atom_trak_get_timescale (AtomTRAK * trak)
2484 return trak->mdia.mdhd.time_info.timescale;
2488 atom_trak_set_id (AtomTRAK * trak, guint32 id)
2490 trak->tkhd.track_ID = id;
2494 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
2496 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
2497 moov->traks = g_list_append (moov->traks, trak);
2501 atom_trak_get_duration (AtomTRAK * trak)
2503 return trak->tkhd.duration;
2507 atom_stts_get_total_duration (AtomSTTS * stts)
2509 GList *walker = stts->entries;
2513 STTSEntry *entry = (STTSEntry *) walker->data;
2515 sum += (guint64) (entry->sample_count) * entry->sample_delta;
2516 walker = g_list_next (walker);
2522 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
2524 trak->mdia.mdhd.time_info.duration =
2525 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
2526 if (trak->mdia.mdhd.time_info.timescale != 0) {
2527 trak->tkhd.duration =
2528 gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration,
2529 moov_timescale, trak->mdia.mdhd.time_info.timescale);
2531 trak->tkhd.duration = 0;
2536 atom_moov_get_timescale (AtomMOOV * moov)
2538 return moov->mvhd.time_info.timescale;
2542 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
2544 moov->mvhd.time_info.timescale = timescale;
2548 atom_moov_update_duration (AtomMOOV * moov)
2550 GList *traks = moov->traks;
2551 guint64 dur, duration = 0;
2554 AtomTRAK *trak = (AtomTRAK *) traks->data;
2556 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
2557 dur = atom_trak_get_duration (trak);
2560 traks = g_list_next (traks);
2562 moov->mvhd.time_info.duration = duration;
2566 atom_set_type (Atom * atom, guint32 fourcc)
2568 atom->type = fourcc;
2572 atom_stbl_set_64bits (AtomSTBL * stbl, gboolean use)
2575 atom_set_type (&stbl->stco64.header.header, FOURCC_co64);
2577 atom_set_type (&stbl->stco64.header.header, FOURCC_stco);
2582 atom_trak_set_64bits (AtomTRAK * trak, gboolean use)
2584 atom_stbl_set_64bits (&trak->mdia.minf.stbl, use);
2588 atom_moov_set_64bits (AtomMOOV * moov, gboolean large_file)
2590 GList *traks = moov->traks;
2593 AtomTRAK *trak = (AtomTRAK *) traks->data;
2595 atom_trak_set_64bits (trak, large_file);
2596 traks = g_list_next (traks);
2601 atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset)
2603 GList *entries = stco64->entries;
2606 guint64 *value = (guint64 *) entries->data;
2609 entries = g_list_next (entries);
2614 atom_moov_chunks_add_offset (AtomMOOV * moov, guint32 offset)
2616 GList *traks = moov->traks;
2619 AtomTRAK *trak = (AtomTRAK *) traks->data;
2621 atom_stco64_chunks_add_offset (&trak->mdia.minf.stbl.stco64, offset);
2622 traks = g_list_next (traks);
2627 * Meta tags functions
2630 atom_moov_init_metatags (AtomMOOV * moov, AtomsContext * context)
2633 moov->udta = atom_udta_new ();
2635 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
2636 if (!moov->udta->meta) {
2637 moov->udta->meta = atom_meta_new ();
2639 if (!moov->udta->meta->ilst) {
2640 moov->udta->meta->ilst = atom_ilst_new ();
2646 atom_tag_data_alloc_data (AtomTagData * data, guint size)
2648 if (data->data != NULL) {
2649 g_free (data->data);
2651 data->data = g_new0 (guint8, size);
2652 data->datalen = size;
2656 atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
2660 atom_moov_init_metatags (moov, &moov->context);
2661 if (moov->udta->meta)
2662 entries = &moov->udta->meta->ilst->entries;
2664 entries = &moov->udta->entries;
2665 *entries = g_list_append (*entries, tag);
2669 atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2670 const guint8 * data, guint size)
2675 tag = atom_tag_new (fourcc, flags);
2677 atom_tag_data_alloc_data (tdata, size);
2678 g_memmove (tdata->data, data, size);
2680 atom_moov_append_tag (moov,
2681 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
2686 atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2688 gint len = strlen (value);
2691 atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
2695 atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2698 guint8 data[8] = { 0, };
2701 GST_WRITE_UINT16_BE (data, value);
2702 atom_moov_add_tag (moov, fourcc, flags, data, 2);
2704 GST_WRITE_UINT32_BE (data + 2, value);
2705 atom_moov_add_tag (moov, fourcc, flags, data, 8);
2710 atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
2712 AtomData *data_atom;
2720 /* blob is unparsed atom;
2721 * extract size and fourcc, and wrap remainder in data atom */
2722 len = GST_READ_UINT32_BE (data);
2723 fourcc = GST_READ_UINT32_LE (data + 4);
2727 buf = gst_buffer_new ();
2728 GST_BUFFER_SIZE (buf) = len - 8;
2729 GST_BUFFER_DATA (buf) = data + 8;
2731 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2732 gst_buffer_unref (buf);
2734 atom_moov_append_tag (moov,
2735 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2740 atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
2743 AtomData *data_atom;
2747 /* need full atom */
2748 buf = gst_buffer_new_and_alloc (size + 4);
2749 bdata = GST_BUFFER_DATA (buf);
2750 /* full atom: version and flags */
2751 GST_WRITE_UINT32_BE (bdata, 0);
2752 memcpy (bdata + 4, data, size);
2754 data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2755 gst_buffer_unref (buf);
2757 atom_moov_append_tag (moov,
2758 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2763 language_code (const char *lang)
2765 g_return_val_if_fail (lang != NULL, 0);
2766 g_return_val_if_fail (strlen (lang) == 3, 0);
2768 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
2769 ((lang[2] - 0x60) & 0x1F);
2773 atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc,
2774 const gchar * value, gint16 ivalue)
2776 gint len = 0, size = 0;
2780 len = strlen (value);
2787 data = g_malloc (size + 3);
2788 /* language tag and null-terminated UTF-8 string */
2790 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
2791 /* include 0 terminator */
2792 memcpy (data + 2, value, len + 1);
2794 /* 16-bit unsigned int if standalone, otherwise 8-bit */
2797 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
2799 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
2804 atom_moov_add_3gp_tag (moov, fourcc, data, size);
2809 atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2811 atom_moov_add_3gp_str_int_tag (moov, fourcc, value, -1);
2815 atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value)
2817 atom_moov_add_3gp_str_int_tag (moov, fourcc, NULL, value);
2821 * Functions for specifying media types
2825 atom_minf_set_audio (AtomMINF * minf)
2827 atom_minf_clear_handlers (minf);
2828 minf->smhd = atom_smhd_new ();
2832 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
2834 atom_minf_clear_handlers (minf);
2835 minf->vmhd = atom_vmhd_new (context);
2839 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
2842 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2843 hdlr->component_type = comp_type;
2845 hdlr->handler_type = hdlr_type;
2849 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
2851 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
2855 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
2857 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
2861 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
2863 atom_mdia_set_hdlr_type_audio (mdia, context);
2864 atom_minf_set_audio (&mdia->minf);
2868 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
2870 atom_mdia_set_hdlr_type_video (mdia, context);
2871 atom_minf_set_video (&mdia->minf, context);
2875 atom_tkhd_set_audio (AtomTKHD * tkhd)
2877 tkhd->volume = 0x0100;
2878 tkhd->width = tkhd->height = 0;
2882 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
2887 /* qt and ISO base media do not contradict, and examples agree */
2888 tkhd->width = width;
2889 tkhd->height = height;
2893 atom_edts_add_entry (AtomEDTS * edts, EditListEntry * entry)
2895 edts->elst.entries = g_slist_append (edts->elst.entries, entry);
2899 * Adds a new entry to this trak edits list
2900 * duration is in the moov's timescale
2901 * media_time is the offset in the media time to start from (media's timescale)
2902 * rate is a 32 bits fixed-point
2905 atom_trak_add_elst_entry (AtomTRAK * trak, guint32 duration, guint32 media_time,
2908 EditListEntry *entry = g_new (EditListEntry, 1);
2910 entry->duration = duration;
2911 entry->media_time = media_time;
2912 entry->media_rate = rate;
2914 if (trak->edts == NULL) {
2915 trak->edts = atom_edts_new ();
2917 atom_edts_add_entry (trak->edts, entry);
2920 /* re-negotiation is prevented at top-level, so only 1 entry expected.
2921 * Quite some more care here and elsewhere may be needed to
2922 * support several entries */
2923 static SampleTableEntryMP4A *
2924 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
2927 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
2928 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
2930 mp4a->se.header.type = type;
2931 mp4a->se.kind = AUDIO;
2932 mp4a->compression_id = -1;
2933 mp4a->se.data_reference_index = 1;
2935 stsd->entries = g_list_prepend (stsd->entries, mp4a);
2940 static SampleTableEntryMP4V *
2941 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
2944 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
2945 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
2947 mp4v->se.header.type = type;
2948 mp4v->se.kind = VIDEO;
2949 mp4v->se.data_reference_index = 1;
2950 mp4v->horizontal_resolution = 72 << 16;
2951 mp4v->vertical_resolution = 72 << 16;
2952 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2953 mp4v->spatial_quality = 512;
2954 mp4v->temporal_quality = 512;
2957 stsd->entries = g_list_prepend (stsd->entries, mp4v);
2963 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
2965 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
2969 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
2971 atom_tkhd_set_audio (&trak->tkhd);
2972 atom_mdia_set_audio (&trak->mdia, context);
2976 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
2979 atom_tkhd_set_video (&trak->tkhd, context, width, height);
2980 atom_mdia_set_video (&trak->mdia, context);
2984 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
2987 atom_trak_set_audio (trak, context);
2988 trak->mdia.mdhd.time_info.timescale = rate;
2992 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
2993 guint32 rate, guint32 width, guint32 height)
2995 atom_trak_set_video (trak, context, width, height);
2996 trak->mdia.mdhd.time_info.timescale = rate;
2997 trak->tkhd.width = width << 16;
2998 trak->tkhd.height = height << 16;
3002 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
3003 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
3005 SampleTableEntryMP4A *ste;
3007 atom_trak_set_audio_commons (trak, context, scale);
3008 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
3010 trak->is_video = FALSE;
3011 trak->is_h264 = FALSE;
3013 ste->version = entry->version;
3014 ste->compression_id = entry->compression_id;
3015 ste->sample_size = entry->sample_size;
3016 ste->sample_rate = entry->sample_rate << 16;
3017 ste->channels = entry->channels;
3019 ste->samples_per_packet = entry->samples_per_packet;
3020 ste->bytes_per_sample = entry->bytes_per_sample;
3021 ste->bytes_per_packet = entry->bytes_per_packet;
3022 ste->bytes_per_frame = entry->bytes_per_frame;
3025 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
3027 /* 0 size means variable size */
3028 atom_trak_set_constant_size_samples (trak, sample_size);
3032 build_pasp_extension (AtomTRAK * trak, gint par_width, gint par_height)
3034 AtomData *atom_data;
3038 buf = gst_buffer_new_and_alloc (8);
3039 data = GST_BUFFER_DATA (buf);
3041 /* ihdr = image header box */
3042 GST_WRITE_UINT32_BE (data, par_width);
3043 GST_WRITE_UINT32_BE (data + 4, par_height);
3045 atom_data = atom_data_new_from_gst_buffer (FOURCC_pasp, buf);
3046 gst_buffer_unref (buf);
3048 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3053 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
3054 VisualSampleEntry * entry, guint32 scale, AtomInfo * ext)
3056 SampleTableEntryMP4V *ste;
3057 gint dwidth, dheight;
3058 gint par_n = 0, par_d = 0;
3060 if ((entry->par_n != 1 || entry->par_d != 1) &&
3061 (entry->par_n != entry->par_d)) {
3062 par_n = entry->par_n;
3063 par_d = entry->par_d;
3066 dwidth = entry->width;
3067 dheight = entry->height;
3068 /* ISO file spec says track header w/h indicates track's visual presentation
3069 * (so this together with pixels w/h implicitly defines PAR) */
3070 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
3071 if (par_n > par_d) {
3072 dwidth = entry->width * par_n / par_d;
3073 dheight = entry->height;
3075 dwidth = entry->width * par_n / par_d;
3076 dheight = entry->height;
3080 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
3081 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
3083 trak->is_video = TRUE;
3084 trak->is_h264 = (entry->fourcc == FOURCC_avc1);
3086 ste->width = entry->width;
3087 ste->height = entry->height;
3088 ste->depth = entry->depth;
3089 ste->color_table_id = entry->color_table_id;
3090 ste->frame_count = entry->frame_count;
3093 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
3095 /* QT spec has a pasp extension atom in stsd that can hold PAR */
3096 if (par_n && (context->flavor == ATOMS_TREE_FLAVOR_MOV)) {
3097 ste->extension_atoms = g_list_append (ste->extension_atoms,
3098 build_pasp_extension (trak, par_n, par_d));
3102 /* some sample description construction helpers */
3105 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
3106 const GstBuffer * codec_data)
3111 track_id = trak->tkhd.track_ID;
3113 esds = atom_esds_new ();
3114 esds->es.id = track_id & 0xFFFF;
3115 esds->es.dec_conf_desc.object_type = object_type;
3116 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
3118 /* optional DecoderSpecificInfo */
3120 DecoderSpecificInfoDescriptor *desc;
3122 esds->es.dec_conf_desc.dec_specific_info = desc =
3123 desc_dec_specific_info_new ();
3124 desc_dec_specific_info_alloc_data (desc, GST_BUFFER_SIZE (codec_data));
3126 memcpy (desc->data, GST_BUFFER_DATA (codec_data),
3127 GST_BUFFER_SIZE (codec_data));
3130 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
3135 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
3142 /* Add WAVE atom to the MP4A sample table entry */
3143 wave = atom_wave_new ();
3145 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
3146 ext_atom = (Atom *) atom_data_new (FOURCC_null);
3147 wave->extension_atoms =
3148 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
3149 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
3151 /* Add ESDS atom to WAVE */
3152 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
3153 build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
3154 ESDS_STREAM_TYPE_AUDIO, codec_data));
3156 /* Add MP4A atom to the WAVE:
3157 * not really in spec, but makes offset based players happy */
3158 buf = gst_buffer_new_and_alloc (4);
3159 *((guint32 *) GST_BUFFER_DATA (buf)) = 0;
3160 ext_atom = (Atom *) atom_data_new_from_gst_buffer (FOURCC_mp4a, buf);
3161 gst_buffer_unref (buf);
3163 wave->extension_atoms =
3164 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
3165 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
3167 /* Add FRMA to the WAVE */
3168 frma = atom_frma_new ();
3169 frma->media_type = FOURCC_mp4a;
3171 wave->extension_atoms =
3172 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
3173 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
3175 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
3180 build_jp2h_extension (AtomTRAK * trak, gint width, gint height, guint32 fourcc)
3182 AtomData *atom_data;
3187 if (fourcc == GST_MAKE_FOURCC ('s', 'R', 'G', 'B')) {
3189 } else if (fourcc == GST_MAKE_FOURCC ('s', 'Y', 'U', 'V')) {
3194 buf = gst_buffer_new_and_alloc (22 + 15);
3195 data = GST_BUFFER_DATA (buf);
3197 /* ihdr = image header box */
3198 GST_WRITE_UINT32_BE (data, 22);
3199 GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
3200 GST_WRITE_UINT32_BE (data + 8, height);
3201 GST_WRITE_UINT32_BE (data + 12, width);
3202 /* FIXME perhaps parse from stream,
3203 * though exactly 3 in any respectable colourspace */
3204 GST_WRITE_UINT16_BE (data + 16, 3);
3205 /* 8 bits per component, unsigned */
3206 GST_WRITE_UINT8 (data + 18, 0x7);
3207 /* compression type; reserved */
3208 GST_WRITE_UINT8 (data + 19, 0x7);
3209 /* colour space (un)known */
3210 GST_WRITE_UINT8 (data + 20, 0x0);
3211 /* intellectual property right (box present) */
3212 GST_WRITE_UINT8 (data + 21, 0x0);
3214 /* colour specification box */
3216 GST_WRITE_UINT32_BE (data, 15);
3217 GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
3218 /* specification method: enumerated */
3219 GST_WRITE_UINT8 (data + 8, 0x1);
3220 /* precedence; reserved */
3221 GST_WRITE_UINT8 (data + 9, 0x0);
3222 /* approximation; reserved */
3223 GST_WRITE_UINT8 (data + 10, 0x0);
3224 /* enumerated colourspace */
3225 GST_WRITE_UINT32_BE (data + 11, cenum);
3227 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
3228 gst_buffer_unref (buf);
3230 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3235 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
3238 AtomInfo *result = NULL;
3241 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
3242 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
3250 build_amr_extension ()
3256 buf = gst_buffer_new ();
3257 GST_BUFFER_DATA (buf) = ext;
3258 GST_BUFFER_SIZE (buf) = sizeof (ext);
3261 GST_WRITE_UINT32_LE (ext, 0);
3262 /* decoder version */
3263 GST_WRITE_UINT8 (ext + 4, 0);
3264 /* mode set (all modes) */
3265 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
3266 /* mode change period (no restriction) */
3267 GST_WRITE_UINT8 (ext + 7, 0);
3268 /* frames per sample */
3269 GST_WRITE_UINT8 (ext + 8, 1);
3271 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', 'a', 'm', 'r'), buf);
3272 gst_buffer_unref (buf);
3277 build_h263_extension ()
3283 buf = gst_buffer_new ();
3284 GST_BUFFER_DATA (buf) = ext;
3285 GST_BUFFER_SIZE (buf) = sizeof (ext);
3288 GST_WRITE_UINT32_LE (ext, 0);
3289 /* decoder version */
3290 GST_WRITE_UINT8 (ext + 4, 0);
3291 /* level / profile */
3292 /* FIXME ? maybe ? obtain somewhere; baseline for now */
3293 GST_WRITE_UINT8 (ext + 5, 10);
3294 GST_WRITE_UINT8 (ext + 6, 0);
3296 res = build_codec_data_extension (GST_MAKE_FOURCC ('d', '2', '6', '3'), buf);
3297 gst_buffer_unref (buf);