1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2008-2010 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
3 * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sf.net>
4 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20 /*
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
23 * LICENSE).
24 *
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:
31 *
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
34 *
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
41 * SOFTWARE.
42 */
43
44 #include "atoms.h"
45 #include <string.h>
46 #include <glib.h>
47
48 #include <gst/gst.h>
49 #include <gst/base/gstbytewriter.h>
50 #include <gst/tag/tag.h>
51 #include <gst/video/video.h>
52
53 /*
54 * Creates a new AtomsContext for the given flavor.
55 */
56 AtomsContext *
atoms_context_new(AtomsTreeFlavor flavor,gboolean force_create_timecode_trak)57 atoms_context_new (AtomsTreeFlavor flavor, gboolean force_create_timecode_trak)
58 {
59 AtomsContext *context = g_new0 (AtomsContext, 1);
60 context->flavor = flavor;
61 context->force_create_timecode_trak = force_create_timecode_trak;
62 return context;
63 }
64
65 /*
66 * Frees an AtomsContext and all memory associated with it
67 */
68 void
atoms_context_free(AtomsContext * context)69 atoms_context_free (AtomsContext * context)
70 {
71 g_free (context);
72 }
73
74 /* -- creation, initialization, clear and free functions -- */
75
76 #define SECS_PER_DAY (24 * 60 * 60)
77 #define LEAP_YEARS_FROM_1904_TO_1970 17
78
79 guint64
atoms_get_current_qt_time(void)80 atoms_get_current_qt_time (void)
81 {
82 gint64 curtime_s = g_get_real_time () / G_USEC_PER_SEC;
83
84 /* FIXME this should use UTC coordinated time */
85 return curtime_s + (((1970 - 1904) * (guint64) 365) +
86 LEAP_YEARS_FROM_1904_TO_1970) * SECS_PER_DAY;
87 }
88
89 static void
common_time_info_init(TimeInfo * ti)90 common_time_info_init (TimeInfo * ti)
91 {
92 ti->creation_time = ti->modification_time = atoms_get_current_qt_time ();
93 ti->timescale = 0;
94 ti->duration = 0;
95 }
96
97 static void
atom_header_set(Atom * header,guint32 fourcc,gint32 size,gint64 ext_size)98 atom_header_set (Atom * header, guint32 fourcc, gint32 size, gint64 ext_size)
99 {
100 header->type = fourcc;
101 header->size = size;
102 header->extended_size = ext_size;
103 }
104
105 static void
atom_clear(Atom * atom)106 atom_clear (Atom * atom)
107 {
108 }
109
110 static void
atom_full_init(AtomFull * full,guint32 fourcc,gint32 size,gint64 ext_size,guint8 version,guint8 flags[3])111 atom_full_init (AtomFull * full, guint32 fourcc, gint32 size, gint64 ext_size,
112 guint8 version, guint8 flags[3])
113 {
114 atom_header_set (&(full->header), fourcc, size, ext_size);
115 full->version = version;
116 full->flags[0] = flags[0];
117 full->flags[1] = flags[1];
118 full->flags[2] = flags[2];
119 }
120
121 static void
atom_full_clear(AtomFull * full)122 atom_full_clear (AtomFull * full)
123 {
124 atom_clear (&full->header);
125 }
126
127 static void
atom_full_free(AtomFull * full)128 atom_full_free (AtomFull * full)
129 {
130 atom_full_clear (full);
131 g_free (full);
132 }
133
134 static guint32
atom_full_get_flags_as_uint(AtomFull * full)135 atom_full_get_flags_as_uint (AtomFull * full)
136 {
137 return full->flags[0] << 16 | full->flags[1] << 8 | full->flags[2];
138 }
139
140 static void
atom_full_set_flags_as_uint(AtomFull * full,guint32 flags_as_uint)141 atom_full_set_flags_as_uint (AtomFull * full, guint32 flags_as_uint)
142 {
143 full->flags[2] = flags_as_uint & 0xFF;
144 full->flags[1] = (flags_as_uint & 0xFF00) >> 8;
145 full->flags[0] = (flags_as_uint & 0xFF0000) >> 16;
146 }
147
148 static AtomInfo *
build_atom_info_wrapper(Atom * atom,gpointer copy_func,gpointer free_func)149 build_atom_info_wrapper (Atom * atom, gpointer copy_func, gpointer free_func)
150 {
151 AtomInfo *info = NULL;
152
153 if (atom) {
154 info = g_new0 (AtomInfo, 1);
155
156 info->atom = atom;
157 info->copy_data_func = copy_func;
158 info->free_func = free_func;
159 }
160
161 return info;
162 }
163
164 static GList *
atom_info_list_prepend_atom(GList * ai,Atom * atom,AtomCopyDataFunc copy_func,AtomFreeFunc free_func)165 atom_info_list_prepend_atom (GList * ai, Atom * atom,
166 AtomCopyDataFunc copy_func, AtomFreeFunc free_func)
167 {
168 if (atom)
169 return g_list_prepend (ai,
170 build_atom_info_wrapper (atom, copy_func, free_func));
171 else
172 return ai;
173 }
174
175 static void
atom_info_list_free(GList * ai)176 atom_info_list_free (GList * ai)
177 {
178 while (ai) {
179 AtomInfo *info = (AtomInfo *) ai->data;
180
181 info->free_func (info->atom);
182 g_free (info);
183 ai = g_list_delete_link (ai, ai);
184 }
185 }
186
187 static AtomData *
atom_data_new(guint32 fourcc)188 atom_data_new (guint32 fourcc)
189 {
190 AtomData *data = g_new0 (AtomData, 1);
191
192 atom_header_set (&data->header, fourcc, 0, 0);
193 return data;
194 }
195
196 static void
atom_data_alloc_mem(AtomData * data,guint32 size)197 atom_data_alloc_mem (AtomData * data, guint32 size)
198 {
199 g_free (data->data);
200 data->data = g_new0 (guint8, size);
201 data->datalen = size;
202 }
203
204 static AtomData *
atom_data_new_from_data(guint32 fourcc,const guint8 * mem,gsize size)205 atom_data_new_from_data (guint32 fourcc, const guint8 * mem, gsize size)
206 {
207 AtomData *data = atom_data_new (fourcc);
208
209 atom_data_alloc_mem (data, size);
210 memcpy (data->data, mem, size);
211 return data;
212 }
213
214 static AtomData *
atom_data_new_from_gst_buffer(guint32 fourcc,const GstBuffer * buf)215 atom_data_new_from_gst_buffer (guint32 fourcc, const GstBuffer * buf)
216 {
217 AtomData *data = atom_data_new (fourcc);
218 gsize size = gst_buffer_get_size ((GstBuffer *) buf);
219
220 atom_data_alloc_mem (data, size);
221 gst_buffer_extract ((GstBuffer *) buf, 0, data->data, size);
222 return data;
223 }
224
225 static void
atom_data_free(AtomData * data)226 atom_data_free (AtomData * data)
227 {
228 atom_clear (&data->header);
229 g_free (data->data);
230 g_free (data);
231 }
232
233 static AtomUUID *
atom_uuid_new(void)234 atom_uuid_new (void)
235 {
236 AtomUUID *uuid = g_new0 (AtomUUID, 1);
237
238 atom_header_set (&uuid->header, FOURCC_uuid, 0, 0);
239 return uuid;
240 }
241
242 static void
atom_uuid_free(AtomUUID * data)243 atom_uuid_free (AtomUUID * data)
244 {
245 atom_clear (&data->header);
246 g_free (data->data);
247 g_free (data);
248 }
249
250 static void
atom_ftyp_init(AtomFTYP * ftyp,guint32 major,guint32 version,GList * brands)251 atom_ftyp_init (AtomFTYP * ftyp, guint32 major, guint32 version, GList * brands)
252 {
253 gint index;
254 GList *it = NULL;
255
256 atom_header_set (&ftyp->header, FOURCC_ftyp, 16, 0);
257 ftyp->major_brand = major;
258 ftyp->version = version;
259
260 /* always include major brand as compatible brand */
261 ftyp->compatible_brands_size = g_list_length (brands) + 1;
262 ftyp->compatible_brands = g_new (guint32, ftyp->compatible_brands_size);
263
264 ftyp->compatible_brands[0] = major;
265 index = 1;
266 for (it = brands; it != NULL; it = g_list_next (it)) {
267 ftyp->compatible_brands[index++] = GPOINTER_TO_UINT (it->data);
268 }
269 }
270
271 AtomFTYP *
atom_ftyp_new(AtomsContext * context,guint32 major,guint32 version,GList * brands)272 atom_ftyp_new (AtomsContext * context, guint32 major, guint32 version,
273 GList * brands)
274 {
275 AtomFTYP *ftyp = g_new0 (AtomFTYP, 1);
276
277 atom_ftyp_init (ftyp, major, version, brands);
278 return ftyp;
279 }
280
281 void
atom_ftyp_free(AtomFTYP * ftyp)282 atom_ftyp_free (AtomFTYP * ftyp)
283 {
284 atom_clear (&ftyp->header);
285 g_free (ftyp->compatible_brands);
286 ftyp->compatible_brands = NULL;
287 g_free (ftyp);
288 }
289
290 static void
atom_esds_init(AtomESDS * esds)291 atom_esds_init (AtomESDS * esds)
292 {
293 guint8 flags[3] = { 0, 0, 0 };
294
295 atom_full_init (&esds->header, FOURCC_esds, 0, 0, 0, flags);
296 desc_es_init (&esds->es);
297 }
298
299 static AtomESDS *
atom_esds_new(void)300 atom_esds_new (void)
301 {
302 AtomESDS *esds = g_new0 (AtomESDS, 1);
303
304 atom_esds_init (esds);
305 return esds;
306 }
307
308 static void
atom_esds_free(AtomESDS * esds)309 atom_esds_free (AtomESDS * esds)
310 {
311 atom_full_clear (&esds->header);
312 desc_es_descriptor_clear (&esds->es);
313 g_free (esds);
314 }
315
316 static AtomFRMA *
atom_frma_new(void)317 atom_frma_new (void)
318 {
319 AtomFRMA *frma = g_new0 (AtomFRMA, 1);
320
321 atom_header_set (&frma->header, FOURCC_frma, 0, 0);
322 return frma;
323 }
324
325 static void
atom_frma_free(AtomFRMA * frma)326 atom_frma_free (AtomFRMA * frma)
327 {
328 atom_clear (&frma->header);
329 g_free (frma);
330 }
331
332 static AtomWAVE *
atom_wave_new(void)333 atom_wave_new (void)
334 {
335 AtomWAVE *wave = g_new0 (AtomWAVE, 1);
336
337 atom_header_set (&wave->header, FOURCC_wave, 0, 0);
338 return wave;
339 }
340
341 static void
atom_wave_free(AtomWAVE * wave)342 atom_wave_free (AtomWAVE * wave)
343 {
344 atom_clear (&wave->header);
345 atom_info_list_free (wave->extension_atoms);
346 g_free (wave);
347 }
348
349 static void
atom_elst_init(AtomELST * elst)350 atom_elst_init (AtomELST * elst)
351 {
352 guint8 flags[3] = { 0, 0, 0 };
353 atom_full_init (&elst->header, FOURCC_elst, 0, 0, 0, flags);
354 elst->entries = 0;
355 }
356
357 static void
atom_elst_clear(AtomELST * elst)358 atom_elst_clear (AtomELST * elst)
359 {
360 GSList *walker;
361
362 atom_full_clear (&elst->header);
363 walker = elst->entries;
364 while (walker) {
365 g_free ((EditListEntry *) walker->data);
366 walker = g_slist_next (walker);
367 }
368 g_slist_free (elst->entries);
369 }
370
371 static void
atom_edts_init(AtomEDTS * edts)372 atom_edts_init (AtomEDTS * edts)
373 {
374 atom_header_set (&edts->header, FOURCC_edts, 0, 0);
375 atom_elst_init (&edts->elst);
376 }
377
378 static void
atom_edts_clear(AtomEDTS * edts)379 atom_edts_clear (AtomEDTS * edts)
380 {
381 atom_clear (&edts->header);
382 atom_elst_clear (&edts->elst);
383 }
384
385 static AtomEDTS *
atom_edts_new(void)386 atom_edts_new (void)
387 {
388 AtomEDTS *edts = g_new0 (AtomEDTS, 1);
389 atom_edts_init (edts);
390 return edts;
391 }
392
393 static void
atom_edts_free(AtomEDTS * edts)394 atom_edts_free (AtomEDTS * edts)
395 {
396 atom_edts_clear (edts);
397 g_free (edts);
398 }
399
400 static void
atom_tcmi_init(AtomTCMI * tcmi)401 atom_tcmi_init (AtomTCMI * tcmi)
402 {
403 guint8 flags[3] = { 0, 0, 0 };
404
405 atom_full_init (&tcmi->header, FOURCC_tcmi, 0, 0, 0, flags);
406 }
407
408 static void
atom_tcmi_clear(AtomTCMI * tcmi)409 atom_tcmi_clear (AtomTCMI * tcmi)
410 {
411 atom_full_clear (&tcmi->header);
412 tcmi->text_font = 0;
413 tcmi->text_face = 0;
414 tcmi->text_size = 0;
415 tcmi->text_color[0] = 0;
416 tcmi->text_color[1] = 0;
417 tcmi->text_color[2] = 0;
418 tcmi->bg_color[0] = 0;
419 tcmi->bg_color[1] = 0;
420 tcmi->bg_color[2] = 0;
421 g_free (tcmi->font_name);
422 tcmi->font_name = NULL;
423 }
424
425 static AtomTMCD *
atom_tmcd_new(void)426 atom_tmcd_new (void)
427 {
428 AtomTMCD *tmcd = g_new0 (AtomTMCD, 1);
429
430 atom_header_set (&tmcd->header, FOURCC_tmcd, 0, 0);
431 atom_tcmi_init (&tmcd->tcmi);
432
433 return tmcd;
434 }
435
436 static void
atom_tmcd_free(AtomTMCD * tmcd)437 atom_tmcd_free (AtomTMCD * tmcd)
438 {
439 atom_clear (&tmcd->header);
440 atom_tcmi_clear (&tmcd->tcmi);
441 g_free (tmcd);
442 }
443
444 static void
atom_gmin_init(AtomGMIN * gmin)445 atom_gmin_init (AtomGMIN * gmin)
446 {
447 guint8 flags[3] = { 0, 0, 0 };
448
449 atom_full_init (&gmin->header, FOURCC_gmin, 0, 0, 0, flags);
450 }
451
452 static void
atom_gmin_clear(AtomGMIN * gmin)453 atom_gmin_clear (AtomGMIN * gmin)
454 {
455 atom_full_clear (&gmin->header);
456 gmin->graphics_mode = 0;
457 gmin->opcolor[0] = 0;
458 gmin->opcolor[1] = 0;
459 gmin->opcolor[2] = 0;
460 gmin->balance = 0;
461 gmin->reserved = 0;
462 }
463
464 static void
atom_gmhd_init(AtomGMHD * gmhd)465 atom_gmhd_init (AtomGMHD * gmhd)
466 {
467 atom_header_set (&gmhd->header, FOURCC_gmhd, 0, 0);
468 atom_gmin_init (&gmhd->gmin);
469 }
470
471 static void
atom_gmhd_clear(AtomGMHD * gmhd)472 atom_gmhd_clear (AtomGMHD * gmhd)
473 {
474 atom_clear (&gmhd->header);
475 atom_gmin_clear (&gmhd->gmin);
476 if (gmhd->tmcd) {
477 atom_tmcd_free (gmhd->tmcd);
478 gmhd->tmcd = NULL;
479 }
480 }
481
482 static AtomGMHD *
atom_gmhd_new(void)483 atom_gmhd_new (void)
484 {
485 AtomGMHD *gmhd = g_new0 (AtomGMHD, 1);
486 atom_gmhd_init (gmhd);
487 return gmhd;
488 }
489
490 static void
atom_gmhd_free(AtomGMHD * gmhd)491 atom_gmhd_free (AtomGMHD * gmhd)
492 {
493 atom_gmhd_clear (gmhd);
494 g_free (gmhd);
495 }
496
497 static void
atom_nmhd_init(AtomNMHD * nmhd)498 atom_nmhd_init (AtomNMHD * nmhd)
499 {
500 atom_header_set (&nmhd->header, FOURCC_nmhd, 0, 0);
501 nmhd->flags = 0;
502 }
503
504 static void
atom_nmhd_clear(AtomNMHD * nmhd)505 atom_nmhd_clear (AtomNMHD * nmhd)
506 {
507 atom_clear (&nmhd->header);
508 }
509
510 static AtomNMHD *
atom_nmhd_new(void)511 atom_nmhd_new (void)
512 {
513 AtomNMHD *nmhd = g_new0 (AtomNMHD, 1);
514 atom_nmhd_init (nmhd);
515 return nmhd;
516 }
517
518 static void
atom_nmhd_free(AtomNMHD * nmhd)519 atom_nmhd_free (AtomNMHD * nmhd)
520 {
521 atom_nmhd_clear (nmhd);
522 g_free (nmhd);
523 }
524
525 static void
atom_sample_entry_init(SampleTableEntry * se,guint32 type)526 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
527 {
528 atom_header_set (&se->header, type, 0, 0);
529
530 memset (se->reserved, 0, sizeof (guint8) * 6);
531 se->data_reference_index = 0;
532 }
533
534 static void
atom_sample_entry_free(SampleTableEntry * se)535 atom_sample_entry_free (SampleTableEntry * se)
536 {
537 atom_clear (&se->header);
538 }
539
540 static void
sample_entry_mp4a_init(SampleTableEntryMP4A * mp4a)541 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
542 {
543 atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
544
545 mp4a->version = 0;
546 mp4a->revision_level = 0;
547 mp4a->vendor = 0;
548 mp4a->channels = 2;
549 mp4a->sample_size = 16;
550 mp4a->compression_id = 0;
551 mp4a->packet_size = 0;
552 mp4a->sample_rate = 0;
553 /* following only used if version is 1 */
554 mp4a->samples_per_packet = 0;
555 mp4a->bytes_per_packet = 0;
556 mp4a->bytes_per_frame = 0;
557 mp4a->bytes_per_sample = 0;
558
559 mp4a->extension_atoms = NULL;
560 }
561
562 static SampleTableEntryMP4A *
sample_entry_mp4a_new(void)563 sample_entry_mp4a_new (void)
564 {
565 SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
566
567 sample_entry_mp4a_init (mp4a);
568 return mp4a;
569 }
570
571 static void
sample_entry_mp4a_free(SampleTableEntryMP4A * mp4a)572 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
573 {
574 atom_sample_entry_free (&mp4a->se);
575 atom_info_list_free (mp4a->extension_atoms);
576 g_free (mp4a);
577 }
578
579 static void
sample_entry_tmcd_init(SampleTableEntryTMCD * tmcd)580 sample_entry_tmcd_init (SampleTableEntryTMCD * tmcd)
581 {
582 atom_sample_entry_init (&tmcd->se, FOURCC_tmcd);
583
584 tmcd->tc_flags = 0;
585 tmcd->timescale = 0;
586 tmcd->frame_duration = 0;
587 tmcd->n_frames = 0;
588
589 tmcd->name.language_code = 0;
590 g_free (tmcd->name.name);
591 tmcd->name.name = NULL;
592 }
593
594 static SampleTableEntryTMCD *
sample_entry_tmcd_new(void)595 sample_entry_tmcd_new (void)
596 {
597 SampleTableEntryTMCD *tmcd = g_new0 (SampleTableEntryTMCD, 1);
598
599 sample_entry_tmcd_init (tmcd);
600 return tmcd;
601 }
602
603 static void
sample_entry_tmcd_free(SampleTableEntryTMCD * tmcd)604 sample_entry_tmcd_free (SampleTableEntryTMCD * tmcd)
605 {
606 atom_sample_entry_free (&tmcd->se);
607 g_free (tmcd->name.name);
608 g_free (tmcd);
609 }
610
611 static void
sample_entry_mp4v_init(SampleTableEntryMP4V * mp4v,AtomsContext * context)612 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
613 {
614 atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
615
616 mp4v->version = 0;
617 mp4v->revision_level = 0;
618 mp4v->vendor = 0;
619
620 mp4v->temporal_quality = 0;
621 mp4v->spatial_quality = 0;
622
623 /* qt and ISO base media do not contradict, and examples agree */
624 mp4v->horizontal_resolution = 0x00480000;
625 mp4v->vertical_resolution = 0x00480000;
626
627 mp4v->datasize = 0;
628 mp4v->frame_count = 1;
629
630 memset (mp4v->compressor, 0, sizeof (guint8) * 32);
631
632 mp4v->depth = 0;
633 mp4v->color_table_id = 0;
634
635 mp4v->extension_atoms = NULL;
636 }
637
638 static void
sample_entry_mp4v_free(SampleTableEntryMP4V * mp4v)639 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
640 {
641 atom_sample_entry_free (&mp4v->se);
642 atom_info_list_free (mp4v->extension_atoms);
643 g_free (mp4v);
644 }
645
646 static SampleTableEntryMP4V *
sample_entry_mp4v_new(AtomsContext * context)647 sample_entry_mp4v_new (AtomsContext * context)
648 {
649 SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
650
651 sample_entry_mp4v_init (mp4v, context);
652 return mp4v;
653 }
654
655 static void
sample_entry_tx3g_init(SampleTableEntryTX3G * tx3g)656 sample_entry_tx3g_init (SampleTableEntryTX3G * tx3g)
657 {
658 atom_sample_entry_init (&tx3g->se, FOURCC_tx3g);
659
660 tx3g->display_flags = 0;
661 tx3g->font_id = 1; /* must be 1 as there is a single font */
662 tx3g->font_face = 0;
663 tx3g->foreground_color_rgba = 0xFFFFFFFF; /* white, opaque */
664
665 /* can't set this now */
666 tx3g->default_text_box = 0;
667 tx3g->font_size = 0;
668 }
669
670 static void
sample_entry_tx3g_free(SampleTableEntryTX3G * tx3g)671 sample_entry_tx3g_free (SampleTableEntryTX3G * tx3g)
672 {
673 atom_sample_entry_free (&tx3g->se);
674 g_free (tx3g);
675 }
676
677 static SampleTableEntryTX3G *
sample_entry_tx3g_new(void)678 sample_entry_tx3g_new (void)
679 {
680 SampleTableEntryTX3G *tx3g = g_new0 (SampleTableEntryTX3G, 1);
681
682 sample_entry_tx3g_init (tx3g);
683 return tx3g;
684 }
685
686
687 static void
atom_stsd_init(AtomSTSD * stsd)688 atom_stsd_init (AtomSTSD * stsd)
689 {
690 guint8 flags[3] = { 0, 0, 0 };
691
692 atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
693 stsd->entries = NULL;
694 stsd->n_entries = 0;
695 }
696
697 static void
atom_stsd_remove_entries(AtomSTSD * stsd)698 atom_stsd_remove_entries (AtomSTSD * stsd)
699 {
700 GList *walker;
701
702 walker = stsd->entries;
703 while (walker) {
704 GList *aux = walker;
705 SampleTableEntry *se = (SampleTableEntry *) aux->data;
706
707 walker = g_list_next (walker);
708 stsd->entries = g_list_remove_link (stsd->entries, aux);
709
710 switch (se->kind) {
711 case AUDIO:
712 sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
713 break;
714 case VIDEO:
715 sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
716 break;
717 case SUBTITLE:
718 sample_entry_tx3g_free ((SampleTableEntryTX3G *) se);
719 break;
720 case TIMECODE:
721 sample_entry_tmcd_free ((SampleTableEntryTMCD *) se);
722 break;
723 case CLOSEDCAPTION:
724 default:
725 /* best possible cleanup */
726 atom_sample_entry_free (se);
727 }
728 g_list_free (aux);
729 }
730 stsd->n_entries = 0;
731 }
732
733 static void
atom_stsd_clear(AtomSTSD * stsd)734 atom_stsd_clear (AtomSTSD * stsd)
735 {
736 atom_stsd_remove_entries (stsd);
737 atom_full_clear (&stsd->header);
738 }
739
740 static void
atom_ctts_init(AtomCTTS * ctts)741 atom_ctts_init (AtomCTTS * ctts)
742 {
743 guint8 flags[3] = { 0, 0, 0 };
744
745 atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
746 atom_array_init (&ctts->entries, 128);
747 ctts->do_pts = FALSE;
748 }
749
750 static AtomCTTS *
atom_ctts_new(void)751 atom_ctts_new (void)
752 {
753 AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
754
755 atom_ctts_init (ctts);
756 return ctts;
757 }
758
759 static void
atom_ctts_free(AtomCTTS * ctts)760 atom_ctts_free (AtomCTTS * ctts)
761 {
762 atom_full_clear (&ctts->header);
763 atom_array_clear (&ctts->entries);
764 g_free (ctts);
765 }
766
767 /* svmi is specified in ISO 23000-11 (Stereoscopic video application format)
768 * MPEG-A */
769 static void
atom_svmi_init(AtomSVMI * svmi)770 atom_svmi_init (AtomSVMI * svmi)
771 {
772 guint8 flags[3] = { 0, 0, 0 };
773
774 atom_full_init (&svmi->header, FOURCC_svmi, 0, 0, 0, flags);
775 svmi->stereoscopic_composition_type = 0x00;
776 svmi->is_left_first = FALSE;
777 }
778
779 AtomSVMI *
atom_svmi_new(guint8 stereoscopic_composition_type,gboolean is_left_first)780 atom_svmi_new (guint8 stereoscopic_composition_type, gboolean is_left_first)
781 {
782 AtomSVMI *svmi = g_new0 (AtomSVMI, 1);
783
784 atom_svmi_init (svmi);
785 svmi->stereoscopic_composition_type = stereoscopic_composition_type;
786 svmi->is_left_first = is_left_first;
787 return svmi;
788 }
789
790 static void
atom_svmi_free(AtomSVMI * svmi)791 atom_svmi_free (AtomSVMI * svmi)
792 {
793 g_free (svmi);
794 }
795
796 static void
atom_stts_init(AtomSTTS * stts)797 atom_stts_init (AtomSTTS * stts)
798 {
799 guint8 flags[3] = { 0, 0, 0 };
800
801 atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
802 atom_array_init (&stts->entries, 512);
803 }
804
805 static void
atom_stts_clear(AtomSTTS * stts)806 atom_stts_clear (AtomSTTS * stts)
807 {
808 atom_full_clear (&stts->header);
809 atom_array_clear (&stts->entries);
810 }
811
812 static void
atom_stsz_init(AtomSTSZ * stsz)813 atom_stsz_init (AtomSTSZ * stsz)
814 {
815 guint8 flags[3] = { 0, 0, 0 };
816
817 atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
818 atom_array_init (&stsz->entries, 1024);
819 stsz->sample_size = 0;
820 stsz->table_size = 0;
821 }
822
823 static void
atom_stsz_clear(AtomSTSZ * stsz)824 atom_stsz_clear (AtomSTSZ * stsz)
825 {
826 atom_full_clear (&stsz->header);
827 atom_array_clear (&stsz->entries);
828 stsz->table_size = 0;
829 }
830
831 static void
atom_stsc_init(AtomSTSC * stsc)832 atom_stsc_init (AtomSTSC * stsc)
833 {
834 guint8 flags[3] = { 0, 0, 0 };
835
836 atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
837 atom_array_init (&stsc->entries, 128);
838 }
839
840 static void
atom_stsc_clear(AtomSTSC * stsc)841 atom_stsc_clear (AtomSTSC * stsc)
842 {
843 atom_full_clear (&stsc->header);
844 atom_array_clear (&stsc->entries);
845 }
846
847 static void
atom_co64_init(AtomSTCO64 * co64)848 atom_co64_init (AtomSTCO64 * co64)
849 {
850 guint8 flags[3] = { 0, 0, 0 };
851
852 atom_full_init (&co64->header, FOURCC_stco, 0, 0, 0, flags);
853
854 co64->chunk_offset = 0;
855 co64->max_offset = 0;
856 atom_array_init (&co64->entries, 256);
857 }
858
859 static void
atom_stco64_clear(AtomSTCO64 * stco64)860 atom_stco64_clear (AtomSTCO64 * stco64)
861 {
862 atom_full_clear (&stco64->header);
863 atom_array_clear (&stco64->entries);
864 }
865
866 static void
atom_stss_init(AtomSTSS * stss)867 atom_stss_init (AtomSTSS * stss)
868 {
869 guint8 flags[3] = { 0, 0, 0 };
870
871 atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
872 atom_array_init (&stss->entries, 128);
873 }
874
875 static void
atom_stss_clear(AtomSTSS * stss)876 atom_stss_clear (AtomSTSS * stss)
877 {
878 atom_full_clear (&stss->header);
879 atom_array_clear (&stss->entries);
880 }
881
882 void
atom_stbl_init(AtomSTBL * stbl)883 atom_stbl_init (AtomSTBL * stbl)
884 {
885 atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
886
887 atom_stts_init (&stbl->stts);
888 atom_stss_init (&stbl->stss);
889 atom_stsd_init (&stbl->stsd);
890 atom_stsz_init (&stbl->stsz);
891 atom_stsc_init (&stbl->stsc);
892 stbl->ctts = NULL;
893 stbl->svmi = NULL;
894
895 atom_co64_init (&stbl->stco64);
896 }
897
898 void
atom_stbl_clear(AtomSTBL * stbl)899 atom_stbl_clear (AtomSTBL * stbl)
900 {
901 atom_clear (&stbl->header);
902 atom_stsd_clear (&stbl->stsd);
903 atom_stts_clear (&stbl->stts);
904 atom_stss_clear (&stbl->stss);
905 atom_stsc_clear (&stbl->stsc);
906 atom_stsz_clear (&stbl->stsz);
907 if (stbl->ctts) {
908 atom_ctts_free (stbl->ctts);
909 }
910 if (stbl->svmi) {
911 atom_svmi_free (stbl->svmi);
912 }
913 atom_stco64_clear (&stbl->stco64);
914 }
915
916 static void
atom_vmhd_init(AtomVMHD * vmhd,AtomsContext * context)917 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
918 {
919 guint8 flags[3] = { 0, 0, 1 };
920
921 atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
922 vmhd->graphics_mode = 0x0;
923 memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
924
925 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
926 vmhd->graphics_mode = 0x40;
927 vmhd->opcolor[0] = 32768;
928 vmhd->opcolor[1] = 32768;
929 vmhd->opcolor[2] = 32768;
930 }
931 }
932
933 static AtomVMHD *
atom_vmhd_new(AtomsContext * context)934 atom_vmhd_new (AtomsContext * context)
935 {
936 AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
937
938 atom_vmhd_init (vmhd, context);
939 return vmhd;
940 }
941
942 static void
atom_vmhd_free(AtomVMHD * vmhd)943 atom_vmhd_free (AtomVMHD * vmhd)
944 {
945 atom_full_clear (&vmhd->header);
946 g_free (vmhd);
947 }
948
949 static void
atom_smhd_init(AtomSMHD * smhd)950 atom_smhd_init (AtomSMHD * smhd)
951 {
952 guint8 flags[3] = { 0, 0, 0 };
953
954 atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
955 smhd->balance = 0;
956 smhd->reserved = 0;
957 }
958
959 static AtomSMHD *
atom_smhd_new(void)960 atom_smhd_new (void)
961 {
962 AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
963
964 atom_smhd_init (smhd);
965 return smhd;
966 }
967
968 static void
atom_smhd_free(AtomSMHD * smhd)969 atom_smhd_free (AtomSMHD * smhd)
970 {
971 atom_full_clear (&smhd->header);
972 g_free (smhd);
973 }
974
975 static void
atom_hmhd_free(AtomHMHD * hmhd)976 atom_hmhd_free (AtomHMHD * hmhd)
977 {
978 atom_full_clear (&hmhd->header);
979 g_free (hmhd);
980 }
981
982 static void
atom_hdlr_init(AtomHDLR * hdlr,AtomsContext * context)983 atom_hdlr_init (AtomHDLR * hdlr, AtomsContext * context)
984 {
985 guint8 flags[3] = { 0, 0, 0 };
986
987 atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
988
989 hdlr->component_type = 0;
990 hdlr->handler_type = 0;
991 hdlr->manufacturer = 0;
992 hdlr->flags = 0;
993 hdlr->flags_mask = 0;
994 hdlr->name = g_strdup ("");
995
996 /* Store the flavor to know how to serialize the 'name' string */
997 hdlr->flavor = context->flavor;
998 }
999
1000 static AtomHDLR *
atom_hdlr_new(AtomsContext * context)1001 atom_hdlr_new (AtomsContext * context)
1002 {
1003 AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
1004
1005 atom_hdlr_init (hdlr, context);
1006 return hdlr;
1007 }
1008
1009 static void
atom_hdlr_clear(AtomHDLR * hdlr)1010 atom_hdlr_clear (AtomHDLR * hdlr)
1011 {
1012 atom_full_clear (&hdlr->header);
1013 if (hdlr->name) {
1014 g_free (hdlr->name);
1015 hdlr->name = NULL;
1016 }
1017 }
1018
1019 static void
atom_hdlr_free(AtomHDLR * hdlr)1020 atom_hdlr_free (AtomHDLR * hdlr)
1021 {
1022 atom_hdlr_clear (hdlr);
1023 g_free (hdlr);
1024 }
1025
1026 static void
atom_url_init(AtomURL * url)1027 atom_url_init (AtomURL * url)
1028 {
1029 guint8 flags[3] = { 0, 0, 1 };
1030
1031 atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
1032 url->location = NULL;
1033 }
1034
1035 static void
atom_url_free(AtomURL * url)1036 atom_url_free (AtomURL * url)
1037 {
1038 atom_full_clear (&url->header);
1039 if (url->location) {
1040 g_free (url->location);
1041 url->location = NULL;
1042 }
1043 g_free (url);
1044 }
1045
1046 static AtomURL *
atom_url_new(void)1047 atom_url_new (void)
1048 {
1049 AtomURL *url = g_new0 (AtomURL, 1);
1050
1051 atom_url_init (url);
1052 return url;
1053 }
1054
1055 static AtomFull *
atom_alis_new(void)1056 atom_alis_new (void)
1057 {
1058 guint8 flags[3] = { 0, 0, 1 };
1059 AtomFull *alis = g_new0 (AtomFull, 1);
1060
1061 atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
1062 return alis;
1063 }
1064
1065 static void
atom_dref_init(AtomDREF * dref,AtomsContext * context)1066 atom_dref_init (AtomDREF * dref, AtomsContext * context)
1067 {
1068 guint8 flags[3] = { 0, 0, 0 };
1069
1070 atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
1071
1072 /* in either case, alis or url init arranges to set self-contained flag */
1073 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
1074 /* alis dref for qt */
1075 AtomFull *alis = atom_alis_new ();
1076 dref->entries = g_list_append (dref->entries, alis);
1077 } else {
1078 /* url for iso spec, as 'alis' not specified there */
1079 AtomURL *url = atom_url_new ();
1080 dref->entries = g_list_append (dref->entries, url);
1081 }
1082 }
1083
1084 static void
atom_dref_clear(AtomDREF * dref)1085 atom_dref_clear (AtomDREF * dref)
1086 {
1087 GList *walker;
1088
1089 atom_full_clear (&dref->header);
1090 walker = dref->entries;
1091 while (walker) {
1092 GList *aux = walker;
1093 Atom *atom = (Atom *) aux->data;
1094
1095 walker = g_list_next (walker);
1096 dref->entries = g_list_remove_link (dref->entries, aux);
1097 switch (atom->type) {
1098 case FOURCC_alis:
1099 atom_full_free ((AtomFull *) atom);
1100 break;
1101 case FOURCC_url_:
1102 atom_url_free ((AtomURL *) atom);
1103 break;
1104 default:
1105 /* we do nothing, better leak than crash */
1106 break;
1107 }
1108 g_list_free (aux);
1109 }
1110 }
1111
1112 static void
atom_dinf_init(AtomDINF * dinf,AtomsContext * context)1113 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
1114 {
1115 atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
1116 atom_dref_init (&dinf->dref, context);
1117 }
1118
1119 static void
atom_dinf_clear(AtomDINF * dinf)1120 atom_dinf_clear (AtomDINF * dinf)
1121 {
1122 atom_clear (&dinf->header);
1123 atom_dref_clear (&dinf->dref);
1124 }
1125
1126 static void
atom_minf_init(AtomMINF * minf,AtomsContext * context)1127 atom_minf_init (AtomMINF * minf, AtomsContext * context)
1128 {
1129 atom_header_set (&minf->header, FOURCC_minf, 0, 0);
1130
1131 minf->vmhd = NULL;
1132 minf->smhd = NULL;
1133 minf->hmhd = NULL;
1134 minf->gmhd = NULL;
1135
1136 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
1137 minf->hdlr = atom_hdlr_new (context);
1138 minf->hdlr->component_type = FOURCC_dhlr;
1139 minf->hdlr->handler_type = FOURCC_alis;
1140 } else {
1141 minf->hdlr = NULL;
1142 }
1143 atom_dinf_init (&minf->dinf, context);
1144 atom_stbl_init (&minf->stbl);
1145 }
1146
1147 static void
atom_minf_clear_handlers(AtomMINF * minf)1148 atom_minf_clear_handlers (AtomMINF * minf)
1149 {
1150 if (minf->vmhd) {
1151 atom_vmhd_free (minf->vmhd);
1152 minf->vmhd = NULL;
1153 }
1154 if (minf->smhd) {
1155 atom_smhd_free (minf->smhd);
1156 minf->smhd = NULL;
1157 }
1158 if (minf->hmhd) {
1159 atom_hmhd_free (minf->hmhd);
1160 minf->hmhd = NULL;
1161 }
1162 if (minf->gmhd) {
1163 atom_gmhd_free (minf->gmhd);
1164 minf->gmhd = NULL;
1165 }
1166 if (minf->nmhd) {
1167 atom_nmhd_free (minf->nmhd);
1168 minf->nmhd = NULL;
1169 }
1170 }
1171
1172 static void
atom_minf_clear(AtomMINF * minf)1173 atom_minf_clear (AtomMINF * minf)
1174 {
1175 atom_clear (&minf->header);
1176 atom_minf_clear_handlers (minf);
1177 if (minf->hdlr) {
1178 atom_hdlr_free (minf->hdlr);
1179 }
1180 atom_dinf_clear (&minf->dinf);
1181 atom_stbl_clear (&minf->stbl);
1182 }
1183
1184 static void
atom_mdhd_init(AtomMDHD * mdhd)1185 atom_mdhd_init (AtomMDHD * mdhd)
1186 {
1187 guint8 flags[3] = { 0, 0, 0 };
1188
1189 atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
1190 common_time_info_init (&mdhd->time_info);
1191 /* tempting as it may be to simply 0-initialize,
1192 * that will have the demuxer (correctly) come up with 'eng' as language
1193 * so explicitly specify undefined instead */
1194 mdhd->language_code = language_code ("und");
1195 mdhd->quality = 0;
1196 }
1197
1198 static void
atom_mdhd_clear(AtomMDHD * mdhd)1199 atom_mdhd_clear (AtomMDHD * mdhd)
1200 {
1201 atom_full_clear (&mdhd->header);
1202 }
1203
1204 static void
atom_mdia_init(AtomMDIA * mdia,AtomsContext * context)1205 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
1206 {
1207 atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
1208
1209 atom_mdhd_init (&mdia->mdhd);
1210 atom_hdlr_init (&mdia->hdlr, context);
1211 atom_minf_init (&mdia->minf, context);
1212 }
1213
1214 static void
atom_mdia_clear(AtomMDIA * mdia)1215 atom_mdia_clear (AtomMDIA * mdia)
1216 {
1217 atom_clear (&mdia->header);
1218 atom_mdhd_clear (&mdia->mdhd);
1219 atom_hdlr_clear (&mdia->hdlr);
1220 atom_minf_clear (&mdia->minf);
1221 }
1222
1223 static void
atom_tkhd_init(AtomTKHD * tkhd,AtomsContext * context)1224 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
1225 {
1226 /*
1227 * flags info
1228 * 1 -> track enabled
1229 * 2 -> track in movie
1230 * 4 -> track in preview
1231 */
1232 guint8 flags[3] = { 0, 0, 7 };
1233
1234 atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
1235
1236 tkhd->creation_time = tkhd->modification_time = atoms_get_current_qt_time ();
1237 tkhd->duration = 0;
1238 tkhd->track_ID = 0;
1239 tkhd->reserved = 0;
1240
1241 tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
1242 tkhd->layer = 0;
1243 tkhd->alternate_group = 0;
1244 tkhd->volume = 0;
1245 tkhd->reserved3 = 0;
1246 memset (tkhd->matrix, 0, sizeof (guint32) * 9);
1247 tkhd->matrix[0] = 1 << 16;
1248 tkhd->matrix[4] = 1 << 16;
1249 tkhd->matrix[8] = 16384 << 16;
1250 tkhd->width = 0;
1251 tkhd->height = 0;
1252 }
1253
1254 static void
atom_tkhd_clear(AtomTKHD * tkhd)1255 atom_tkhd_clear (AtomTKHD * tkhd)
1256 {
1257 atom_full_clear (&tkhd->header);
1258 }
1259
1260 static void
atom_ilst_init(AtomILST * ilst)1261 atom_ilst_init (AtomILST * ilst)
1262 {
1263 atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
1264 ilst->entries = NULL;
1265 }
1266
1267 static AtomILST *
atom_ilst_new(void)1268 atom_ilst_new (void)
1269 {
1270 AtomILST *ilst = g_new0 (AtomILST, 1);
1271
1272 atom_ilst_init (ilst);
1273 return ilst;
1274 }
1275
1276 static void
atom_ilst_free(AtomILST * ilst)1277 atom_ilst_free (AtomILST * ilst)
1278 {
1279 if (ilst->entries)
1280 atom_info_list_free (ilst->entries);
1281 atom_clear (&ilst->header);
1282 g_free (ilst);
1283 }
1284
1285 static void
atom_meta_init(AtomMETA * meta,AtomsContext * context)1286 atom_meta_init (AtomMETA * meta, AtomsContext * context)
1287 {
1288 guint8 flags[3] = { 0, 0, 0 };
1289
1290 atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1291 atom_hdlr_init (&meta->hdlr, context);
1292 /* FIXME (ISOM says this is always 0) */
1293 meta->hdlr.component_type = FOURCC_mhlr;
1294 meta->hdlr.handler_type = FOURCC_mdir;
1295 meta->ilst = NULL;
1296 }
1297
1298 static AtomMETA *
atom_meta_new(AtomsContext * context)1299 atom_meta_new (AtomsContext * context)
1300 {
1301 AtomMETA *meta = g_new0 (AtomMETA, 1);
1302
1303 atom_meta_init (meta, context);
1304 return meta;
1305 }
1306
1307 static void
atom_meta_free(AtomMETA * meta)1308 atom_meta_free (AtomMETA * meta)
1309 {
1310 atom_full_clear (&meta->header);
1311 atom_hdlr_clear (&meta->hdlr);
1312 if (meta->ilst)
1313 atom_ilst_free (meta->ilst);
1314 meta->ilst = NULL;
1315 g_free (meta);
1316 }
1317
1318 static void
atom_udta_init_metatags(AtomUDTA * udta,AtomsContext * context)1319 atom_udta_init_metatags (AtomUDTA * udta, AtomsContext * context)
1320 {
1321 if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
1322 if (!udta->meta) {
1323 udta->meta = atom_meta_new (context);
1324 }
1325 if (!udta->meta->ilst) {
1326 udta->meta->ilst = atom_ilst_new ();
1327 }
1328 }
1329 }
1330
1331 static void
atom_udta_init(AtomUDTA * udta,AtomsContext * context)1332 atom_udta_init (AtomUDTA * udta, AtomsContext * context)
1333 {
1334 atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1335 udta->meta = NULL;
1336 udta->context = context;
1337
1338 atom_udta_init_metatags (udta, context);
1339 }
1340
1341 static void
atom_udta_clear(AtomUDTA * udta)1342 atom_udta_clear (AtomUDTA * udta)
1343 {
1344 atom_clear (&udta->header);
1345 if (udta->meta)
1346 atom_meta_free (udta->meta);
1347 udta->meta = NULL;
1348 if (udta->entries)
1349 atom_info_list_free (udta->entries);
1350 }
1351
1352 static void
atom_tref_init(AtomTREF * tref,guint32 reftype)1353 atom_tref_init (AtomTREF * tref, guint32 reftype)
1354 {
1355 atom_header_set (&tref->header, FOURCC_tref, 0, 0);
1356 tref->reftype = reftype;
1357 atom_array_init (&tref->entries, 128);
1358 }
1359
1360 static void
atom_tref_clear(AtomTREF * tref)1361 atom_tref_clear (AtomTREF * tref)
1362 {
1363 atom_clear (&tref->header);
1364 tref->reftype = 0;
1365 atom_array_clear (&tref->entries);
1366 }
1367
1368 AtomTREF *
atom_tref_new(guint32 reftype)1369 atom_tref_new (guint32 reftype)
1370 {
1371 AtomTREF *tref;
1372
1373 tref = g_new0 (AtomTREF, 1);
1374 atom_tref_init (tref, reftype);
1375
1376 return tref;
1377 }
1378
1379 static void
atom_tref_free(AtomTREF * tref)1380 atom_tref_free (AtomTREF * tref)
1381 {
1382 atom_tref_clear (tref);
1383 g_free (tref);
1384 }
1385
1386 /* Clear added tags, but keep the context/flavor the same */
1387 void
atom_udta_clear_tags(AtomUDTA * udta)1388 atom_udta_clear_tags (AtomUDTA * udta)
1389 {
1390 if (udta->entries) {
1391 atom_info_list_free (udta->entries);
1392 udta->entries = NULL;
1393 }
1394 if (udta->meta && udta->meta->ilst->entries) {
1395 atom_info_list_free (udta->meta->ilst->entries);
1396 udta->meta->ilst->entries = NULL;
1397 }
1398 }
1399
1400 static void
atom_tag_data_init(AtomTagData * data)1401 atom_tag_data_init (AtomTagData * data)
1402 {
1403 guint8 flags[] = { 0, 0, 0 };
1404
1405 atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1406 }
1407
1408 static void
atom_tag_data_clear(AtomTagData * data)1409 atom_tag_data_clear (AtomTagData * data)
1410 {
1411 atom_full_clear (&data->header);
1412 g_free (data->data);
1413 data->datalen = 0;
1414 }
1415
1416 /*
1417 * Fourcc is the tag fourcc
1418 * flags will be truncated to 24bits
1419 */
1420 static AtomTag *
atom_tag_new(guint32 fourcc,guint32 flags_as_uint)1421 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1422 {
1423 AtomTag *tag = g_new0 (AtomTag, 1);
1424
1425 tag->header.type = fourcc;
1426 atom_tag_data_init (&tag->data);
1427 atom_full_set_flags_as_uint (&tag->data.header, flags_as_uint);
1428 return tag;
1429 }
1430
1431 static void
atom_tag_free(AtomTag * tag)1432 atom_tag_free (AtomTag * tag)
1433 {
1434 atom_clear (&tag->header);
1435 atom_tag_data_clear (&tag->data);
1436 g_free (tag);
1437 }
1438
1439 static void
atom_mvhd_init(AtomMVHD * mvhd)1440 atom_mvhd_init (AtomMVHD * mvhd)
1441 {
1442 guint8 flags[3] = { 0, 0, 0 };
1443
1444 atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1445
1446 common_time_info_init (&mvhd->time_info);
1447
1448 mvhd->prefered_rate = 1 << 16;
1449 mvhd->volume = 1 << 8;
1450 mvhd->reserved3 = 0;
1451 memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1452
1453 memset (mvhd->matrix, 0, sizeof (guint32[9]));
1454 mvhd->matrix[0] = 1 << 16;
1455 mvhd->matrix[4] = 1 << 16;
1456 mvhd->matrix[8] = 16384 << 16;
1457
1458 mvhd->preview_time = 0;
1459 mvhd->preview_duration = 0;
1460 mvhd->poster_time = 0;
1461 mvhd->selection_time = 0;
1462 mvhd->selection_duration = 0;
1463 mvhd->current_time = 0;
1464
1465 mvhd->next_track_id = 1;
1466 }
1467
1468 static void
atom_mvhd_clear(AtomMVHD * mvhd)1469 atom_mvhd_clear (AtomMVHD * mvhd)
1470 {
1471 atom_full_clear (&mvhd->header);
1472 }
1473
1474 static void
atom_mehd_init(AtomMEHD * mehd)1475 atom_mehd_init (AtomMEHD * mehd)
1476 {
1477 guint8 flags[3] = { 0, 0, 0 };
1478
1479 atom_full_init (&mehd->header, FOURCC_mehd, 0, 0, 1, flags);
1480 mehd->fragment_duration = 0;
1481 }
1482
1483 static void
atom_mvex_init(AtomMVEX * mvex)1484 atom_mvex_init (AtomMVEX * mvex)
1485 {
1486 atom_header_set (&mvex->header, FOURCC_mvex, 0, 0);
1487 atom_mehd_init (&mvex->mehd);
1488 mvex->trexs = NULL;
1489 }
1490
1491 static void
atom_trak_init(AtomTRAK * trak,AtomsContext * context)1492 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
1493 {
1494 atom_header_set (&trak->header, FOURCC_trak, 0, 0);
1495
1496 atom_tkhd_init (&trak->tkhd, context);
1497 trak->context = context;
1498 atom_udta_init (&trak->udta, context);
1499 trak->edts = NULL;
1500 atom_mdia_init (&trak->mdia, context);
1501 trak->tref = NULL;
1502 }
1503
1504 AtomTRAK *
atom_trak_new(AtomsContext * context)1505 atom_trak_new (AtomsContext * context)
1506 {
1507 AtomTRAK *trak = g_new0 (AtomTRAK, 1);
1508
1509 atom_trak_init (trak, context);
1510 return trak;
1511 }
1512
1513 static void
atom_trak_clear(AtomTRAK * trak)1514 atom_trak_clear (AtomTRAK * trak)
1515 {
1516 atom_clear (&trak->header);
1517 atom_tkhd_clear (&trak->tkhd);
1518 if (trak->edts)
1519 atom_edts_free (trak->edts);
1520 atom_udta_clear (&trak->udta);
1521 atom_mdia_clear (&trak->mdia);
1522 if (trak->tref)
1523 atom_tref_free (trak->tref);
1524 }
1525
1526 static void
atom_trak_free(AtomTRAK * trak)1527 atom_trak_free (AtomTRAK * trak)
1528 {
1529 atom_trak_clear (trak);
1530 g_free (trak);
1531 }
1532
1533
1534 static void
atom_moov_init(AtomMOOV * moov,AtomsContext * context)1535 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1536 {
1537 atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1538 atom_mvhd_init (&(moov->mvhd));
1539 atom_mvex_init (&(moov->mvex));
1540 atom_udta_init (&moov->udta, context);
1541 moov->traks = NULL;
1542 moov->context = *context;
1543 }
1544
1545 AtomMOOV *
atom_moov_new(AtomsContext * context)1546 atom_moov_new (AtomsContext * context)
1547 {
1548 AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1549
1550 atom_moov_init (moov, context);
1551 return moov;
1552 }
1553
1554 static void
atom_trex_free(AtomTREX * trex)1555 atom_trex_free (AtomTREX * trex)
1556 {
1557 atom_full_clear (&trex->header);
1558 g_free (trex);
1559 }
1560
1561 static void
atom_mvex_clear(AtomMVEX * mvex)1562 atom_mvex_clear (AtomMVEX * mvex)
1563 {
1564 GList *walker;
1565
1566 atom_clear (&mvex->header);
1567 walker = mvex->trexs;
1568 while (walker) {
1569 atom_trex_free ((AtomTREX *) walker->data);
1570 walker = g_list_next (walker);
1571 }
1572 g_list_free (mvex->trexs);
1573 mvex->trexs = NULL;
1574 }
1575
1576 void
atom_moov_free(AtomMOOV * moov)1577 atom_moov_free (AtomMOOV * moov)
1578 {
1579 GList *walker;
1580
1581 atom_clear (&moov->header);
1582 atom_mvhd_clear (&moov->mvhd);
1583
1584 walker = moov->traks;
1585 while (walker) {
1586 atom_trak_free ((AtomTRAK *) walker->data);
1587 walker = g_list_next (walker);
1588 }
1589 g_list_free (moov->traks);
1590 moov->traks = NULL;
1591
1592 atom_udta_clear (&moov->udta);
1593 atom_mvex_clear (&moov->mvex);
1594
1595 g_free (moov);
1596 }
1597
1598 /* -- end of init / free -- */
1599
1600 /* -- copy data functions -- */
1601
1602 static guint8
atom_full_get_version(AtomFull * full)1603 atom_full_get_version (AtomFull * full)
1604 {
1605 return full->version;
1606 }
1607
1608 static guint64
common_time_info_copy_data(TimeInfo * ti,gboolean trunc_to_32,guint8 ** buffer,guint64 * size,guint64 * offset)1609 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1610 guint8 ** buffer, guint64 * size, guint64 * offset)
1611 {
1612 guint64 original_offset = *offset;
1613
1614 if (trunc_to_32) {
1615 prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1616 prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1617 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1618 prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1619 } else {
1620 prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1621 prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1622 prop_copy_uint32 (ti->timescale, buffer, size, offset);
1623 prop_copy_uint64 (ti->duration, buffer, size, offset);
1624 }
1625 return *offset - original_offset;
1626 }
1627
1628 static void
atom_write_size(guint8 ** buffer,guint64 * size,guint64 * offset,guint64 atom_pos)1629 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1630 guint64 atom_pos)
1631 {
1632 /* this only works for non-extended atom size, which is OK
1633 * (though it could be made to do mem_move, etc and write extended size) */
1634 prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1635 }
1636
1637 static guint64
atom_copy_empty(Atom * atom,guint8 ** buffer,guint64 * size,guint64 * offset)1638 atom_copy_empty (Atom * atom, guint8 ** buffer, guint64 * size,
1639 guint64 * offset)
1640 {
1641 guint64 original_offset = *offset;
1642
1643 prop_copy_uint32 (0, buffer, size, offset);
1644
1645 return *offset - original_offset;
1646 }
1647
1648 guint64
atom_copy_data(Atom * atom,guint8 ** buffer,guint64 * size,guint64 * offset)1649 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1650 {
1651 guint64 original_offset = *offset;
1652
1653 /* copies type and size */
1654 prop_copy_uint32 (atom->size, buffer, size, offset);
1655 prop_copy_fourcc (atom->type, buffer, size, offset);
1656
1657 /* extended size needed */
1658 if (atom->size == 1) {
1659 /* really should not happen other than with mdat atom;
1660 * would be a problem for size (re)write code, not to mention memory */
1661 g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1662 prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1663 }
1664
1665 return *offset - original_offset;
1666 }
1667
1668 static guint64
atom_full_copy_data(AtomFull * atom,guint8 ** buffer,guint64 * size,guint64 * offset)1669 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1670 guint64 * offset)
1671 {
1672 guint64 original_offset = *offset;
1673
1674 if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1675 return 0;
1676 }
1677
1678 prop_copy_uint8 (atom->version, buffer, size, offset);
1679 prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1680
1681 atom_write_size (buffer, size, offset, original_offset);
1682 return *offset - original_offset;
1683 }
1684
1685 static guint64
atom_info_list_copy_data(GList * ai,guint8 ** buffer,guint64 * size,guint64 * offset)1686 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1687 guint64 * offset)
1688 {
1689 guint64 original_offset = *offset;
1690
1691 while (ai) {
1692 AtomInfo *info = (AtomInfo *) ai->data;
1693
1694 if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1695 return 0;
1696 }
1697 ai = g_list_next (ai);
1698 }
1699
1700 return *offset - original_offset;
1701 }
1702
1703 static guint64
atom_data_copy_data(AtomData * data,guint8 ** buffer,guint64 * size,guint64 * offset)1704 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1705 guint64 * offset)
1706 {
1707 guint64 original_offset = *offset;
1708
1709 if (!atom_copy_data (&data->header, buffer, size, offset)) {
1710 return 0;
1711 }
1712 if (data->datalen)
1713 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1714
1715 atom_write_size (buffer, size, offset, original_offset);
1716 return *offset - original_offset;
1717 }
1718
1719 static guint64
atom_uuid_copy_data(AtomUUID * uuid,guint8 ** buffer,guint64 * size,guint64 * offset)1720 atom_uuid_copy_data (AtomUUID * uuid, guint8 ** buffer, guint64 * size,
1721 guint64 * offset)
1722 {
1723 guint64 original_offset = *offset;
1724
1725 if (!atom_copy_data (&uuid->header, buffer, size, offset)) {
1726 return 0;
1727 }
1728 prop_copy_uint8_array (uuid->uuid, 16, buffer, size, offset);
1729 if (uuid->datalen)
1730 prop_copy_uint8_array (uuid->data, uuid->datalen, buffer, size, offset);
1731
1732 atom_write_size (buffer, size, offset, original_offset);
1733 return *offset - original_offset;
1734 }
1735
1736 guint64
atom_ftyp_copy_data(AtomFTYP * ftyp,guint8 ** buffer,guint64 * size,guint64 * offset)1737 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1738 guint64 * offset)
1739 {
1740 guint64 original_offset = *offset;
1741
1742 if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1743 return 0;
1744 }
1745 prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1746 prop_copy_uint32 (ftyp->version, buffer, size, offset);
1747
1748 prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1749 buffer, size, offset);
1750
1751 atom_write_size (buffer, size, offset, original_offset);
1752 return *offset - original_offset;
1753 }
1754
1755 guint64
atom_mvhd_copy_data(AtomMVHD * atom,guint8 ** buffer,guint64 * size,guint64 * offset)1756 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1757 guint64 * offset)
1758 {
1759 guint8 version;
1760 guint64 original_offset = *offset;
1761
1762 if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1763 return 0;
1764 }
1765
1766 version = atom_full_get_version (&(atom->header));
1767 if (version == 0) {
1768 common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1769 } else if (version == 1) {
1770 common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1771 } else {
1772 *offset = original_offset;
1773 return 0;
1774 }
1775
1776 prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1777 prop_copy_uint16 (atom->volume, buffer, size, offset);
1778 prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1779 prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1780 prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1781 prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1782 prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1783 prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1784 prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1785 prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1786 prop_copy_uint32 (atom->current_time, buffer, size, offset);
1787
1788 prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1789
1790 atom_write_size (buffer, size, offset, original_offset);
1791 return *offset - original_offset;
1792 }
1793
1794 static guint64
atom_tkhd_copy_data(AtomTKHD * tkhd,guint8 ** buffer,guint64 * size,guint64 * offset)1795 atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1796 guint64 * offset)
1797 {
1798 guint64 original_offset = *offset;
1799
1800 if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1801 return 0;
1802 }
1803
1804 if (atom_full_get_version (&tkhd->header) == 0) {
1805 prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1806 prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1807 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1808 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1809 prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1810 } else {
1811 prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1812 prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1813 prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1814 prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1815 prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1816 }
1817
1818 prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1819 prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1820 prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1821 prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1822 prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1823 prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1824
1825 prop_copy_uint32 (tkhd->width, buffer, size, offset);
1826 prop_copy_uint32 (tkhd->height, buffer, size, offset);
1827
1828 atom_write_size (buffer, size, offset, original_offset);
1829 return *offset - original_offset;
1830 }
1831
1832 static guint64
atom_hdlr_copy_data(AtomHDLR * hdlr,guint8 ** buffer,guint64 * size,guint64 * offset)1833 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1834 guint64 * offset)
1835 {
1836 guint64 original_offset = *offset;
1837
1838 if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1839 return 0;
1840 }
1841
1842 prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1843 prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1844 prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1845 prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1846 prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1847
1848 if (hdlr->flavor == ATOMS_TREE_FLAVOR_MOV) {
1849 prop_copy_size_string ((guint8 *) hdlr->name, strlen (hdlr->name), buffer,
1850 size, offset);
1851 } else {
1852 /* assume isomedia base is more generic and use null terminated */
1853 prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
1854 }
1855
1856 atom_write_size (buffer, size, offset, original_offset);
1857 return *offset - original_offset;
1858 }
1859
1860 static guint64
atom_vmhd_copy_data(AtomVMHD * vmhd,guint8 ** buffer,guint64 * size,guint64 * offset)1861 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1862 guint64 * offset)
1863 {
1864 guint64 original_offset = *offset;
1865
1866 if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1867 return 0;
1868 }
1869 prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1870 prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1871
1872 atom_write_size (buffer, size, offset, original_offset);
1873 return original_offset - *offset;
1874 }
1875
1876 static guint64
atom_smhd_copy_data(AtomSMHD * smhd,guint8 ** buffer,guint64 * size,guint64 * offset)1877 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1878 guint64 * offset)
1879 {
1880 guint64 original_offset = *offset;
1881
1882 if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1883 return 0;
1884 }
1885 prop_copy_uint16 (smhd->balance, buffer, size, offset);
1886 prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1887
1888 atom_write_size (buffer, size, offset, original_offset);
1889 return original_offset - *offset;
1890 }
1891
1892 static guint64
atom_hmhd_copy_data(AtomHMHD * hmhd,guint8 ** buffer,guint64 * size,guint64 * offset)1893 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1894 guint64 * offset)
1895 {
1896 guint64 original_offset = *offset;
1897
1898 if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1899 return 0;
1900 }
1901 prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1902 prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1903 prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1904 prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1905 prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1906
1907 atom_write_size (buffer, size, offset, original_offset);
1908 return original_offset - *offset;
1909 }
1910
1911 static guint64
atom_tcmi_copy_data(AtomTCMI * tcmi,guint8 ** buffer,guint64 * size,guint64 * offset)1912 atom_tcmi_copy_data (AtomTCMI * tcmi, guint8 ** buffer, guint64 * size,
1913 guint64 * offset)
1914 {
1915 guint64 original_offset = *offset;
1916
1917 if (!atom_full_copy_data (&tcmi->header, buffer, size, offset)) {
1918 return 0;
1919 }
1920 prop_copy_uint16 (tcmi->text_font, buffer, size, offset);
1921 prop_copy_uint16 (tcmi->text_face, buffer, size, offset);
1922 prop_copy_uint16 (tcmi->text_size, buffer, size, offset);
1923 prop_copy_uint16 (tcmi->text_color[0], buffer, size, offset);
1924 prop_copy_uint16 (tcmi->text_color[1], buffer, size, offset);
1925 prop_copy_uint16 (tcmi->text_color[2], buffer, size, offset);
1926 prop_copy_uint16 (tcmi->bg_color[0], buffer, size, offset);
1927 prop_copy_uint16 (tcmi->bg_color[1], buffer, size, offset);
1928 prop_copy_uint16 (tcmi->bg_color[2], buffer, size, offset);
1929 /* reserved */
1930 prop_copy_uint16 (0, buffer, size, offset);
1931 prop_copy_size_string ((guint8 *) tcmi->font_name, strlen (tcmi->font_name),
1932 buffer, size, offset);
1933
1934 atom_write_size (buffer, size, offset, original_offset);
1935 return original_offset - *offset;
1936 }
1937
1938 static guint64
atom_tmcd_copy_data(AtomTMCD * tmcd,guint8 ** buffer,guint64 * size,guint64 * offset)1939 atom_tmcd_copy_data (AtomTMCD * tmcd, guint8 ** buffer, guint64 * size,
1940 guint64 * offset)
1941 {
1942 guint64 original_offset = *offset;
1943
1944 if (!atom_copy_data (&tmcd->header, buffer, size, offset)) {
1945 return 0;
1946 }
1947 if (!atom_tcmi_copy_data (&tmcd->tcmi, buffer, size, offset)) {
1948 return 0;
1949 }
1950
1951 atom_write_size (buffer, size, offset, original_offset);
1952 return original_offset - *offset;
1953 }
1954
1955 static guint64
atom_gmin_copy_data(AtomGMIN * gmin,guint8 ** buffer,guint64 * size,guint64 * offset)1956 atom_gmin_copy_data (AtomGMIN * gmin, guint8 ** buffer, guint64 * size,
1957 guint64 * offset)
1958 {
1959 guint64 original_offset = *offset;
1960
1961 if (!atom_full_copy_data (&gmin->header, buffer, size, offset)) {
1962 return 0;
1963 }
1964 prop_copy_uint16 (gmin->graphics_mode, buffer, size, offset);
1965 prop_copy_uint16 (gmin->opcolor[0], buffer, size, offset);
1966 prop_copy_uint16 (gmin->opcolor[1], buffer, size, offset);
1967 prop_copy_uint16 (gmin->opcolor[2], buffer, size, offset);
1968 prop_copy_uint8 (gmin->balance, buffer, size, offset);
1969 /* reserved */
1970 prop_copy_uint8 (0, buffer, size, offset);
1971
1972 atom_write_size (buffer, size, offset, original_offset);
1973 return original_offset - *offset;
1974 }
1975
1976 static guint64
atom_gmhd_copy_data(AtomGMHD * gmhd,guint8 ** buffer,guint64 * size,guint64 * offset)1977 atom_gmhd_copy_data (AtomGMHD * gmhd, guint8 ** buffer, guint64 * size,
1978 guint64 * offset)
1979 {
1980 guint64 original_offset = *offset;
1981
1982 if (!atom_copy_data (&gmhd->header, buffer, size, offset)) {
1983 return 0;
1984 }
1985 if (!atom_gmin_copy_data (&gmhd->gmin, buffer, size, offset)) {
1986 return 0;
1987 }
1988 if (gmhd->tmcd && !atom_tmcd_copy_data (gmhd->tmcd, buffer, size, offset)) {
1989 return 0;
1990 }
1991
1992 atom_write_size (buffer, size, offset, original_offset);
1993 return original_offset - *offset;
1994 }
1995
1996 static guint64
atom_nmhd_copy_data(AtomNMHD * nmhd,guint8 ** buffer,guint64 * size,guint64 * offset)1997 atom_nmhd_copy_data (AtomNMHD * nmhd, guint8 ** buffer, guint64 * size,
1998 guint64 * offset)
1999 {
2000 guint64 original_offset = *offset;
2001
2002 if (!atom_copy_data (&nmhd->header, buffer, size, offset)) {
2003 return 0;
2004 }
2005 prop_copy_uint32 (nmhd->flags, buffer, size, offset);
2006
2007 atom_write_size (buffer, size, offset, original_offset);
2008 return original_offset - *offset;
2009 }
2010
2011 static gboolean
atom_url_same_file_flag(AtomURL * url)2012 atom_url_same_file_flag (AtomURL * url)
2013 {
2014 return (url->header.flags[2] & 0x1) == 1;
2015 }
2016
2017 static guint64
atom_url_copy_data(AtomURL * url,guint8 ** buffer,guint64 * size,guint64 * offset)2018 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
2019 guint64 * offset)
2020 {
2021 guint64 original_offset = *offset;
2022
2023 if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
2024 return 0;
2025 }
2026
2027 if (!atom_url_same_file_flag (url)) {
2028 prop_copy_null_terminated_string (url->location, buffer, size, offset);
2029 }
2030
2031 atom_write_size (buffer, size, offset, original_offset);
2032 return original_offset - *offset;
2033 }
2034
2035 guint64
atom_stts_copy_data(AtomSTTS * stts,guint8 ** buffer,guint64 * size,guint64 * offset)2036 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
2037 guint64 * offset)
2038 {
2039 guint64 original_offset = *offset;
2040 guint i;
2041
2042 if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
2043 return 0;
2044 }
2045
2046 prop_copy_uint32 (atom_array_get_len (&stts->entries), buffer, size, offset);
2047 /* minimize realloc */
2048 prop_copy_ensure_buffer (buffer, size, offset,
2049 8 * atom_array_get_len (&stts->entries));
2050 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
2051 STTSEntry *entry = &atom_array_index (&stts->entries, i);
2052
2053 prop_copy_uint32 (entry->sample_count, buffer, size, offset);
2054 prop_copy_int32 (entry->sample_delta, buffer, size, offset);
2055 }
2056
2057 atom_write_size (buffer, size, offset, original_offset);
2058 return *offset - original_offset;
2059 }
2060
2061 static guint64
atom_sample_entry_copy_data(SampleTableEntry * se,guint8 ** buffer,guint64 * size,guint64 * offset)2062 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
2063 guint64 * size, guint64 * offset)
2064 {
2065 guint64 original_offset = *offset;
2066
2067 if (!atom_copy_data (&se->header, buffer, size, offset)) {
2068 return 0;
2069 }
2070
2071 prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
2072 prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
2073
2074 return *offset - original_offset;
2075 }
2076
2077 static guint64
atom_esds_copy_data(AtomESDS * esds,guint8 ** buffer,guint64 * size,guint64 * offset)2078 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
2079 guint64 * offset)
2080 {
2081 guint64 original_offset = *offset;
2082
2083 if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
2084 return 0;
2085 }
2086 if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
2087 return 0;
2088 }
2089
2090 atom_write_size (buffer, size, offset, original_offset);
2091 return *offset - original_offset;
2092 }
2093
2094 static guint64
atom_frma_copy_data(AtomFRMA * frma,guint8 ** buffer,guint64 * size,guint64 * offset)2095 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
2096 guint64 * size, guint64 * offset)
2097 {
2098 guint64 original_offset = *offset;
2099
2100 if (!atom_copy_data (&(frma->header), buffer, size, offset))
2101 return 0;
2102
2103 prop_copy_fourcc (frma->media_type, buffer, size, offset);
2104
2105 atom_write_size (buffer, size, offset, original_offset);
2106 return *offset - original_offset;
2107 }
2108
2109 static guint64
atom_hint_sample_entry_copy_data(AtomHintSampleEntry * hse,guint8 ** buffer,guint64 * size,guint64 * offset)2110 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
2111 guint64 * size, guint64 * offset)
2112 {
2113 guint64 original_offset = *offset;
2114
2115 if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
2116 return 0;
2117 }
2118
2119 prop_copy_uint32 (hse->size, buffer, size, offset);
2120 prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
2121
2122 atom_write_size (buffer, size, offset, original_offset);
2123 return *offset - original_offset;
2124 }
2125
2126 static guint64
sample_entry_mp4a_copy_data(SampleTableEntryMP4A * mp4a,guint8 ** buffer,guint64 * size,guint64 * offset)2127 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
2128 guint64 * size, guint64 * offset)
2129 {
2130 guint64 original_offset = *offset;
2131
2132 if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
2133 return 0;
2134 }
2135
2136 prop_copy_uint16 (mp4a->version, buffer, size, offset);
2137 prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
2138 prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
2139 prop_copy_uint16 (mp4a->channels, buffer, size, offset);
2140 prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
2141 prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
2142 prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
2143 prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
2144
2145 /* this should always be 0 for mp4 flavor */
2146 if (mp4a->version == 1) {
2147 prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
2148 prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
2149 prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
2150 prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
2151 }
2152
2153 if (mp4a->extension_atoms) {
2154 if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
2155 return 0;
2156 }
2157
2158 atom_write_size (buffer, size, offset, original_offset);
2159 return *offset - original_offset;
2160 }
2161
2162 static guint64
sample_entry_mp4v_copy_data(SampleTableEntryMP4V * mp4v,guint8 ** buffer,guint64 * size,guint64 * offset)2163 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
2164 guint64 * size, guint64 * offset)
2165 {
2166 guint64 original_offset = *offset;
2167
2168 if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
2169 return 0;
2170 }
2171
2172 prop_copy_uint16 (mp4v->version, buffer, size, offset);
2173 prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
2174 prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
2175 prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
2176 prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
2177
2178 prop_copy_uint16 (mp4v->width, buffer, size, offset);
2179 prop_copy_uint16 (mp4v->height, buffer, size, offset);
2180
2181 prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
2182 prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
2183 prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
2184
2185 prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
2186
2187 prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
2188 offset);
2189
2190 prop_copy_uint16 (mp4v->depth, buffer, size, offset);
2191 prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
2192
2193 /* extra atoms */
2194 if (mp4v->extension_atoms &&
2195 !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
2196 return 0;
2197
2198 atom_write_size (buffer, size, offset, original_offset);
2199 return *offset - original_offset;
2200 }
2201
2202 static guint64
sample_entry_tx3g_copy_data(SampleTableEntryTX3G * tx3g,guint8 ** buffer,guint64 * size,guint64 * offset)2203 sample_entry_tx3g_copy_data (SampleTableEntryTX3G * tx3g, guint8 ** buffer,
2204 guint64 * size, guint64 * offset)
2205 {
2206 guint64 original_offset = *offset;
2207
2208 if (!atom_sample_entry_copy_data (&tx3g->se, buffer, size, offset)) {
2209 return 0;
2210 }
2211
2212 prop_copy_uint32 (tx3g->display_flags, buffer, size, offset);
2213
2214 /* reserved */
2215 prop_copy_uint8 (1, buffer, size, offset);
2216 prop_copy_uint8 (-1, buffer, size, offset);
2217 prop_copy_uint32 (0, buffer, size, offset);
2218
2219 prop_copy_uint64 (tx3g->default_text_box, buffer, size, offset);
2220
2221 /* reserved */
2222 prop_copy_uint32 (0, buffer, size, offset);
2223
2224 prop_copy_uint16 (tx3g->font_id, buffer, size, offset);
2225 prop_copy_uint8 (tx3g->font_face, buffer, size, offset);
2226 prop_copy_uint8 (tx3g->font_size, buffer, size, offset);
2227 prop_copy_uint32 (tx3g->foreground_color_rgba, buffer, size, offset);
2228
2229 /* it must have a fonttable atom */
2230 {
2231 Atom atom;
2232
2233 atom_header_set (&atom, FOURCC_ftab, 18, 0);
2234 if (!atom_copy_data (&atom, buffer, size, offset))
2235 return 0;
2236 prop_copy_uint16 (1, buffer, size, offset); /* Count must be 1 */
2237 prop_copy_uint16 (1, buffer, size, offset); /* Font id: 1 */
2238 prop_copy_size_string ((guint8 *) "Serif", 5, buffer, size, offset);
2239 }
2240
2241 atom_write_size (buffer, size, offset, original_offset);
2242 return *offset - original_offset;
2243 }
2244
2245 static guint64
sample_entry_tmcd_copy_data(SampleTableEntryTMCD * tmcd,guint8 ** buffer,guint64 * size,guint64 * offset)2246 sample_entry_tmcd_copy_data (SampleTableEntryTMCD * tmcd, guint8 ** buffer,
2247 guint64 * size, guint64 * offset)
2248 {
2249 guint64 original_offset = *offset;
2250
2251 if (!atom_sample_entry_copy_data (&tmcd->se, buffer, size, offset)) {
2252 return 0;
2253 }
2254
2255 /* reserved */
2256 prop_copy_uint32 (0, buffer, size, offset);
2257
2258 prop_copy_uint32 (tmcd->tc_flags, buffer, size, offset);
2259 prop_copy_uint32 (tmcd->timescale, buffer, size, offset);
2260 prop_copy_uint32 (tmcd->frame_duration, buffer, size, offset);
2261 prop_copy_uint8 (tmcd->n_frames, buffer, size, offset);
2262
2263 /* reserved */
2264 prop_copy_uint8 (0, buffer, size, offset);
2265 {
2266 Atom atom;
2267 guint64 name_offset = *offset;
2268
2269 atom_header_set (&atom, FOURCC_name, 0, 0);
2270 if (!atom_copy_data (&atom, buffer, size, offset))
2271 return 0;
2272 prop_copy_uint16 (strlen (tmcd->name.name), buffer, size, offset);
2273 prop_copy_uint16 (tmcd->name.language_code, buffer, size, offset);
2274 prop_copy_fixed_size_string ((guint8 *) tmcd->name.name,
2275 strlen (tmcd->name.name), buffer, size, offset);
2276
2277 atom_write_size (buffer, size, offset, name_offset);
2278 }
2279
2280 atom_write_size (buffer, size, offset, original_offset);
2281 return *offset - original_offset;
2282 }
2283
2284 static guint64
sample_entry_generic_copy_data(SampleTableEntry * entry,guint8 ** buffer,guint64 * size,guint64 * offset)2285 sample_entry_generic_copy_data (SampleTableEntry * entry, guint8 ** buffer,
2286 guint64 * size, guint64 * offset)
2287 {
2288 guint64 original_offset = *offset;
2289
2290 if (!atom_sample_entry_copy_data (entry, buffer, size, offset)) {
2291 return 0;
2292 }
2293
2294 atom_write_size (buffer, size, offset, original_offset);
2295 return *offset - original_offset;
2296 }
2297
2298 guint64
atom_stsz_copy_data(AtomSTSZ * stsz,guint8 ** buffer,guint64 * size,guint64 * offset)2299 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
2300 guint64 * offset)
2301 {
2302 guint64 original_offset = *offset;
2303 guint i;
2304
2305 if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
2306 return 0;
2307 }
2308
2309 prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
2310 prop_copy_uint32 (stsz->table_size, buffer, size, offset);
2311 if (stsz->sample_size == 0) {
2312 /* minimize realloc */
2313 prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
2314 /* entry count must match sample count */
2315 g_assert (atom_array_get_len (&stsz->entries) == stsz->table_size);
2316 for (i = 0; i < atom_array_get_len (&stsz->entries); i++) {
2317 prop_copy_uint32 (atom_array_index (&stsz->entries, i), buffer, size,
2318 offset);
2319 }
2320 }
2321
2322 atom_write_size (buffer, size, offset, original_offset);
2323 return *offset - original_offset;
2324 }
2325
2326 guint64
atom_stsc_copy_data(AtomSTSC * stsc,guint8 ** buffer,guint64 * size,guint64 * offset)2327 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
2328 guint64 * offset)
2329 {
2330 guint64 original_offset = *offset;
2331 guint i, len;
2332 gboolean last_entries_merged = FALSE;
2333
2334 if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
2335 return 0;
2336 }
2337
2338 /* Last two entries might be the same size here as we only merge once the
2339 * next chunk is started */
2340 if ((len = atom_array_get_len (&stsc->entries)) > 1) {
2341 STSCEntry *prev_entry = &atom_array_index (&stsc->entries, len - 2);
2342 STSCEntry *current_entry = &atom_array_index (&stsc->entries, len - 1);
2343 if (prev_entry->samples_per_chunk == current_entry->samples_per_chunk &&
2344 prev_entry->sample_description_index ==
2345 current_entry->sample_description_index) {
2346 stsc->entries.len--;
2347 last_entries_merged = TRUE;
2348 }
2349 }
2350
2351 prop_copy_uint32 (atom_array_get_len (&stsc->entries), buffer, size, offset);
2352 /* minimize realloc */
2353 prop_copy_ensure_buffer (buffer, size, offset,
2354 12 * atom_array_get_len (&stsc->entries));
2355
2356 for (i = 0; i < atom_array_get_len (&stsc->entries); i++) {
2357 STSCEntry *entry = &atom_array_index (&stsc->entries, i);
2358
2359 prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
2360 prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
2361 prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
2362 }
2363
2364 atom_write_size (buffer, size, offset, original_offset);
2365
2366 /* Need to add the last entry again as in "robust" muxing mode we will most
2367 * likely add new samples to the last chunk, thus making the
2368 * samples_per_chunk in the last one different to the second to last one,
2369 * and thus making it wrong to keep them merged
2370 */
2371 if (last_entries_merged)
2372 stsc->entries.len++;
2373
2374 return *offset - original_offset;
2375 }
2376
2377 guint64
atom_ctts_copy_data(AtomCTTS * ctts,guint8 ** buffer,guint64 * size,guint64 * offset)2378 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
2379 guint64 * offset)
2380 {
2381 guint64 original_offset = *offset;
2382 guint i;
2383
2384 if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
2385 return 0;
2386 }
2387
2388 prop_copy_uint32 (atom_array_get_len (&ctts->entries), buffer, size, offset);
2389 /* minimize realloc */
2390 prop_copy_ensure_buffer (buffer, size, offset,
2391 8 * atom_array_get_len (&ctts->entries));
2392 for (i = 0; i < atom_array_get_len (&ctts->entries); i++) {
2393 CTTSEntry *entry = &atom_array_index (&ctts->entries, i);
2394
2395 prop_copy_uint32 (entry->samplecount, buffer, size, offset);
2396 prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
2397 }
2398
2399 atom_write_size (buffer, size, offset, original_offset);
2400 return *offset - original_offset;
2401 }
2402
2403 guint64
atom_svmi_copy_data(AtomSVMI * svmi,guint8 ** buffer,guint64 * size,guint64 * offset)2404 atom_svmi_copy_data (AtomSVMI * svmi, guint8 ** buffer, guint64 * size,
2405 guint64 * offset)
2406 {
2407 guint64 original_offset = *offset;
2408
2409 if (!atom_full_copy_data (&svmi->header, buffer, size, offset)) {
2410 return 0;
2411 }
2412
2413 prop_copy_uint8 (svmi->stereoscopic_composition_type, buffer, size, offset);
2414 prop_copy_uint8 (svmi->is_left_first ? 1 : 0, buffer, size, offset);
2415 /* stereo-mono change count */
2416 prop_copy_uint32 (0, buffer, size, offset);
2417
2418 atom_write_size (buffer, size, offset, original_offset);
2419 return *offset - original_offset;
2420 }
2421
2422 guint64
atom_stco64_copy_data(AtomSTCO64 * stco64,guint8 ** buffer,guint64 * size,guint64 * offset)2423 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
2424 guint64 * offset)
2425 {
2426 guint64 original_offset = *offset;
2427 guint i;
2428
2429 /* If any (mdat-relative) offset will by over 32-bits when converted to an
2430 * absolute file offset then we need to write a 64-bit co64 atom, otherwise
2431 * we can write a smaller stco 32-bit table */
2432 gboolean write_stco64 =
2433 (stco64->max_offset + stco64->chunk_offset) > G_MAXUINT32;
2434
2435 if (write_stco64)
2436 stco64->header.header.type = FOURCC_co64;
2437 else
2438 stco64->header.header.type = FOURCC_stco;
2439
2440 if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
2441 return 0;
2442 }
2443
2444 prop_copy_uint32 (atom_array_get_len (&stco64->entries), buffer, size,
2445 offset);
2446
2447 /* minimize realloc */
2448 prop_copy_ensure_buffer (buffer, size, offset,
2449 8 * atom_array_get_len (&stco64->entries));
2450 for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
2451 guint64 value =
2452 atom_array_index (&stco64->entries, i) + stco64->chunk_offset;
2453
2454 if (write_stco64) {
2455 prop_copy_uint64 (value, buffer, size, offset);
2456 } else {
2457 prop_copy_uint32 ((guint32) value, buffer, size, offset);
2458 }
2459 }
2460
2461 atom_write_size (buffer, size, offset, original_offset);
2462 return *offset - original_offset;
2463 }
2464
2465 guint64
atom_stss_copy_data(AtomSTSS * stss,guint8 ** buffer,guint64 * size,guint64 * offset)2466 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
2467 guint64 * offset)
2468 {
2469 guint64 original_offset = *offset;
2470 guint i;
2471
2472 if (atom_array_get_len (&stss->entries) == 0) {
2473 /* FIXME not needing this atom might be confused with error while copying */
2474 return 0;
2475 }
2476
2477 if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
2478 return 0;
2479 }
2480
2481 prop_copy_uint32 (atom_array_get_len (&stss->entries), buffer, size, offset);
2482 /* minimize realloc */
2483 prop_copy_ensure_buffer (buffer, size, offset,
2484 4 * atom_array_get_len (&stss->entries));
2485 for (i = 0; i < atom_array_get_len (&stss->entries); i++) {
2486 prop_copy_uint32 (atom_array_index (&stss->entries, i), buffer, size,
2487 offset);
2488 }
2489
2490 atom_write_size (buffer, size, offset, original_offset);
2491 return *offset - original_offset;
2492 }
2493
2494 static guint64
atom_stsd_copy_data(AtomSTSD * stsd,guint8 ** buffer,guint64 * size,guint64 * offset)2495 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
2496 guint64 * offset)
2497 {
2498 guint64 original_offset = *offset;
2499 GList *walker;
2500
2501 if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
2502 return 0;
2503 }
2504
2505 prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
2506
2507 for (walker = g_list_last (stsd->entries); walker != NULL;
2508 walker = g_list_previous (walker)) {
2509 SampleTableEntry *se = (SampleTableEntry *) walker->data;
2510
2511 switch (((Atom *) walker->data)->type) {
2512 case FOURCC_mp4a:
2513 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
2514 buffer, size, offset)) {
2515 return 0;
2516 }
2517 break;
2518 case FOURCC_mp4v:
2519 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
2520 buffer, size, offset)) {
2521 return 0;
2522 }
2523 break;
2524 default:
2525 if (se->kind == VIDEO) {
2526 if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
2527 walker->data, buffer, size, offset)) {
2528 return 0;
2529 }
2530 } else if (se->kind == AUDIO) {
2531 if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
2532 walker->data, buffer, size, offset)) {
2533 return 0;
2534 }
2535 } else if (se->kind == SUBTITLE) {
2536 if (!sample_entry_tx3g_copy_data ((SampleTableEntryTX3G *)
2537 walker->data, buffer, size, offset)) {
2538 return 0;
2539 }
2540 } else if (se->kind == TIMECODE) {
2541 if (!sample_entry_tmcd_copy_data ((SampleTableEntryTMCD *)
2542 walker->data, buffer, size, offset)) {
2543 return 0;
2544 }
2545 } else if (se->kind == CLOSEDCAPTION) {
2546 if (!sample_entry_generic_copy_data ((SampleTableEntry *)
2547 walker->data, buffer, size, offset)) {
2548 return 0;
2549 }
2550 } else {
2551 if (!atom_hint_sample_entry_copy_data (
2552 (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
2553 return 0;
2554 }
2555 }
2556 break;
2557 }
2558 }
2559
2560 atom_write_size (buffer, size, offset, original_offset);
2561 return *offset - original_offset;
2562 }
2563
2564 static guint64
atom_stbl_copy_data(AtomSTBL * stbl,guint8 ** buffer,guint64 * size,guint64 * offset)2565 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
2566 guint64 * offset)
2567 {
2568 guint64 original_offset = *offset;
2569
2570 if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
2571 return 0;
2572 }
2573
2574 if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
2575 return 0;
2576 }
2577 if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
2578 return 0;
2579 }
2580 /* this atom is optional, so let's check if we need it
2581 * (to avoid false error) */
2582 if (atom_array_get_len (&stbl->stss.entries)) {
2583 if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
2584 return 0;
2585 }
2586 }
2587
2588 if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
2589 return 0;
2590 }
2591 if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
2592 return 0;
2593 }
2594 if (stbl->ctts && stbl->ctts->do_pts) {
2595 if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
2596 return 0;
2597 }
2598 }
2599 if (stbl->svmi) {
2600 if (!atom_svmi_copy_data (stbl->svmi, buffer, size, offset)) {
2601 return 0;
2602 }
2603 }
2604 if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
2605 return 0;
2606 }
2607
2608 atom_write_size (buffer, size, offset, original_offset);
2609 return original_offset - *offset;
2610 }
2611
2612
2613 static guint64
atom_dref_copy_data(AtomDREF * dref,guint8 ** buffer,guint64 * size,guint64 * offset)2614 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
2615 guint64 * offset)
2616 {
2617 guint64 original_offset = *offset;
2618 GList *walker;
2619
2620 if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
2621 return 0;
2622 }
2623
2624 prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
2625
2626 walker = dref->entries;
2627 while (walker != NULL) {
2628 Atom *atom = (Atom *) walker->data;
2629
2630 if (atom->type == FOURCC_url_) {
2631 if (!atom_url_copy_data ((AtomURL *) atom, buffer, size, offset))
2632 return 0;
2633 } else if (atom->type == FOURCC_alis) {
2634 if (!atom_full_copy_data ((AtomFull *) atom, buffer, size, offset))
2635 return 0;
2636 } else {
2637 g_error ("Unsupported atom used inside dref atom");
2638 }
2639 walker = g_list_next (walker);
2640 }
2641
2642 atom_write_size (buffer, size, offset, original_offset);
2643 return *offset - original_offset;
2644 }
2645
2646 static guint64
atom_dinf_copy_data(AtomDINF * dinf,guint8 ** buffer,guint64 * size,guint64 * offset)2647 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
2648 guint64 * offset)
2649 {
2650 guint64 original_offset = *offset;
2651
2652 if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
2653 return 0;
2654 }
2655
2656 if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
2657 return 0;
2658 }
2659
2660 atom_write_size (buffer, size, offset, original_offset);
2661 return original_offset - *offset;
2662 }
2663
2664 static guint64
atom_minf_copy_data(AtomMINF * minf,guint8 ** buffer,guint64 * size,guint64 * offset)2665 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
2666 guint64 * offset)
2667 {
2668 guint64 original_offset = *offset;
2669
2670 if (!atom_copy_data (&minf->header, buffer, size, offset)) {
2671 return 0;
2672 }
2673
2674 if (minf->vmhd) {
2675 if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
2676 return 0;
2677 }
2678 } else if (minf->smhd) {
2679 if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
2680 return 0;
2681 }
2682 } else if (minf->hmhd) {
2683 if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
2684 return 0;
2685 }
2686 } else if (minf->gmhd) {
2687 if (!atom_gmhd_copy_data (minf->gmhd, buffer, size, offset)) {
2688 return 0;
2689 }
2690 } else if (minf->nmhd) {
2691 if (!atom_nmhd_copy_data (minf->nmhd, buffer, size, offset)) {
2692 return 0;
2693 }
2694 }
2695
2696 if (minf->hdlr) {
2697 if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2698 return 0;
2699 }
2700 }
2701
2702 if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2703 return 0;
2704 }
2705 if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2706 return 0;
2707 }
2708
2709 atom_write_size (buffer, size, offset, original_offset);
2710 return *offset - original_offset;
2711 }
2712
2713 static guint64
atom_mdhd_copy_data(AtomMDHD * mdhd,guint8 ** buffer,guint64 * size,guint64 * offset)2714 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2715 guint64 * offset)
2716 {
2717 guint64 original_offset = *offset;
2718
2719 if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2720 return 0;
2721 }
2722
2723 if (!common_time_info_copy_data (&mdhd->time_info,
2724 atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2725 return 0;
2726 }
2727
2728 prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2729 prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2730
2731 atom_write_size (buffer, size, offset, original_offset);
2732 return *offset - original_offset;
2733 }
2734
2735 static guint64
atom_mdia_copy_data(AtomMDIA * mdia,guint8 ** buffer,guint64 * size,guint64 * offset)2736 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2737 guint64 * offset)
2738 {
2739 guint64 original_offset = *offset;
2740
2741 if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2742 return 0;
2743 }
2744 if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2745 return 0;
2746 }
2747 if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2748 return 0;
2749 }
2750
2751 if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2752 return 0;
2753 }
2754
2755 atom_write_size (buffer, size, offset, original_offset);
2756 return *offset - original_offset;
2757 }
2758
2759 static guint64
atom_elst_copy_data(AtomELST * elst,guint8 ** buffer,guint64 * size,guint64 * offset)2760 atom_elst_copy_data (AtomELST * elst, guint8 ** buffer, guint64 * size,
2761 guint64 * offset)
2762 {
2763 guint64 original_offset = *offset;
2764 GSList *walker;
2765
2766 if (!atom_full_copy_data (&elst->header, buffer, size, offset)) {
2767 return 0;
2768 }
2769
2770 prop_copy_uint32 (g_slist_length (elst->entries), buffer, size, offset);
2771
2772 for (walker = elst->entries; walker != NULL; walker = g_slist_next (walker)) {
2773 EditListEntry *entry = (EditListEntry *) walker->data;
2774 prop_copy_uint32 (entry->duration, buffer, size, offset);
2775 prop_copy_uint32 (entry->media_time, buffer, size, offset);
2776 prop_copy_uint32 (entry->media_rate, buffer, size, offset);
2777 }
2778 atom_write_size (buffer, size, offset, original_offset);
2779 return *offset - original_offset;
2780 }
2781
2782 static guint64
atom_tref_copy_data(AtomTREF * tref,guint8 ** buffer,guint64 * size,guint64 * offset)2783 atom_tref_copy_data (AtomTREF * tref, guint8 ** buffer, guint64 * size,
2784 guint64 * offset)
2785 {
2786 guint64 original_offset = *offset;
2787 guint i;
2788
2789 g_assert (atom_array_get_len (&tref->entries) > 0);
2790
2791 if (!atom_copy_data (&tref->header, buffer, size, offset)) {
2792 return 0;
2793 }
2794
2795 prop_copy_uint32 (8 + 4 * atom_array_get_len (&tref->entries), buffer, size,
2796 offset);
2797 prop_copy_fourcc (tref->reftype, buffer, size, offset);
2798 /* minimize realloc */
2799 prop_copy_ensure_buffer (buffer, size, offset,
2800 4 * atom_array_get_len (&tref->entries));
2801 for (i = 0; i < atom_array_get_len (&tref->entries); i++) {
2802 prop_copy_uint32 (atom_array_index (&tref->entries, i), buffer, size,
2803 offset);
2804 }
2805
2806 atom_write_size (buffer, size, offset, original_offset);
2807 return *offset - original_offset;
2808 }
2809
2810 static guint64
atom_edts_copy_data(AtomEDTS * edts,guint8 ** buffer,guint64 * size,guint64 * offset)2811 atom_edts_copy_data (AtomEDTS * edts, guint8 ** buffer, guint64 * size,
2812 guint64 * offset)
2813 {
2814 guint64 original_offset = *offset;
2815
2816 if (!atom_copy_data (&(edts->header), buffer, size, offset))
2817 return 0;
2818
2819 if (!atom_elst_copy_data (&(edts->elst), buffer, size, offset))
2820 return 0;
2821
2822 atom_write_size (buffer, size, offset, original_offset);
2823 return *offset - original_offset;
2824 }
2825
2826 static guint64
atom_tag_data_copy_data(AtomTagData * data,guint8 ** buffer,guint64 * size,guint64 * offset)2827 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2828 guint64 * offset)
2829 {
2830 guint64 original_offset = *offset;
2831
2832 if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2833 return 0;
2834 }
2835
2836 prop_copy_uint32 (data->reserved, buffer, size, offset);
2837 prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2838
2839 atom_write_size (buffer, size, offset, original_offset);
2840 return *offset - original_offset;
2841 }
2842
2843 static guint64
atom_tag_copy_data(AtomTag * tag,guint8 ** buffer,guint64 * size,guint64 * offset)2844 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2845 guint64 * offset)
2846 {
2847 guint64 original_offset = *offset;
2848
2849 if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2850 return 0;
2851 }
2852
2853 if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2854 return 0;
2855 }
2856
2857 atom_write_size (buffer, size, offset, original_offset);
2858 return *offset - original_offset;
2859 }
2860
2861 static guint64
atom_ilst_copy_data(AtomILST * ilst,guint8 ** buffer,guint64 * size,guint64 * offset)2862 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2863 guint64 * offset)
2864 {
2865 guint64 original_offset = *offset;
2866
2867 if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2868 return 0;
2869 }
2870 /* extra atoms */
2871 if (ilst->entries &&
2872 !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2873 return 0;
2874
2875 atom_write_size (buffer, size, offset, original_offset);
2876 return *offset - original_offset;
2877 }
2878
2879 static guint64
atom_meta_copy_data(AtomMETA * meta,guint8 ** buffer,guint64 * size,guint64 * offset)2880 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2881 guint64 * offset)
2882 {
2883 guint64 original_offset = *offset;
2884
2885 if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2886 return 0;
2887 }
2888 if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2889 return 0;
2890 }
2891 if (meta->ilst) {
2892 if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2893 return 0;
2894 }
2895 }
2896
2897 atom_write_size (buffer, size, offset, original_offset);
2898 return *offset - original_offset;
2899 }
2900
2901 /* ohos.ext.func.0016
2902 * add additional features to set geographic location information in mp4 file
2903 * copy the geolocation to udta box accordiong to ISO_IEC_14496
2904 */
2905 #ifdef OHOS_EXT_FUNC
2906 static guint64
atom_xyz_copy_data(Atom * atom,guint8 ** buffer,guint64 * size,guint64 * offset,gint32 latitude,gint32 longitude)2907 atom_xyz_copy_data(Atom * atom, guint8 ** buffer, guint64 * size,
2908 guint64 * offset, gint32 latitude, gint32 longitude)
2909 {
2910 guint64 original_offset = *offset;
2911 guint32 hexadecimal_zero = 48; // to change each number to hexadecimal.
2912 guint8 lat_sign, lng_sign;
2913 guint8 decimal_point = 0x2e; // '.'
2914 guint8 end_sign = 0x2f; // '/'
2915 guint32 boxsize = 30; // accordiong to ISO_IEC_14496.
2916 guint32 format_language_code = 0x001215c7; // accordiong to ISO_IEC_14496.
2917
2918 if (latitude >= 0) {
2919 lat_sign = 0x2b; // '+'
2920 } else {
2921 lat_sign = 0x2d; // '-'
2922 latitude = -latitude;
2923 }
2924
2925 if (longitude >= 0) {
2926 lng_sign = 0x2b; // '+'
2927 } else {
2928 lng_sign = 0x2d; // '-'
2929 longitude = -longitude;
2930 }
2931
2932 guint32 lat_integer = latitude / 10000;
2933 guint32 lat_decimals = latitude % 10000;
2934 guint32 lng_integer = longitude / 10000;
2935 guint32 lng_decimals = longitude % 10000;
2936
2937 prop_copy_uint32 (boxsize, buffer, size, offset);
2938 prop_copy_fourcc (FOURCC__xyz, buffer, size, offset);
2939 prop_copy_uint32 (format_language_code, buffer, size, offset);
2940
2941 prop_copy_uint8 (lat_sign, buffer, size, offset);
2942 prop_copy_uint8 (lat_integer / 10 % 10 + hexadecimal_zero, buffer, size, offset);
2943 prop_copy_uint8 (lat_integer % 10 + hexadecimal_zero, buffer, size, offset);
2944
2945 prop_copy_uint8 (decimal_point, buffer, size, offset);
2946
2947 prop_copy_uint8 (lat_decimals / 1000 % 10 + hexadecimal_zero, buffer, size, offset);
2948 prop_copy_uint8 (lat_decimals / 100 % 10 + hexadecimal_zero, buffer, size, offset);
2949 prop_copy_uint8 (lat_decimals / 10 % 10 + hexadecimal_zero, buffer, size, offset);
2950 prop_copy_uint8 (lat_decimals % 10 + hexadecimal_zero, buffer, size, offset);
2951
2952 prop_copy_uint8 (lng_sign, buffer, size, offset);
2953 prop_copy_uint8 (lng_integer / 100 % 10 + hexadecimal_zero, buffer, size, offset);
2954 prop_copy_uint8 (lng_integer / 10 % 10 + hexadecimal_zero, buffer, size, offset);
2955 prop_copy_uint8 (lng_integer % 10 + hexadecimal_zero, buffer, size, offset);
2956
2957 prop_copy_uint8 (decimal_point, buffer, size, offset);
2958
2959 prop_copy_uint8 (lng_decimals / 1000 % 10 + hexadecimal_zero, buffer, size, offset);
2960 prop_copy_uint8 (lng_decimals / 100 % 10 + hexadecimal_zero, buffer, size, offset);
2961 prop_copy_uint8 (lng_decimals / 10 % 10 + hexadecimal_zero, buffer, size, offset);
2962 prop_copy_uint8 (lng_decimals % 10 + hexadecimal_zero, buffer, size, offset);
2963
2964 prop_copy_uint8 (end_sign, buffer, size, offset);
2965
2966 return *offset - original_offset;
2967 }
2968 #endif
2969
2970 static guint64
atom_udta_copy_data(AtomUDTA * udta,guint8 ** buffer,guint64 * size,guint64 * offset)2971 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2972 guint64 * offset)
2973 {
2974 guint64 original_offset = *offset;
2975
2976 if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2977 return 0;
2978 }
2979 /* ohos.ext.func.0016
2980 * add additional features to set geographic location information in mp4 file
2981 * set_location is the flag to enable this feature
2982 * latitude is the latitude to set, multiply 10000 for the convenience of calculation.
2983 * longitude is the longitude to set, multiply 10000 for the convenience of calculation.
2984 */
2985 #ifdef OHOS_EXT_FUNC
2986 if (udta->set_location) {
2987 if (!atom_xyz_copy_data (&udta->header, buffer, size, offset, udta->latitude, udta->longitude)) {
2988 return 0;
2989 }
2990 }
2991 #endif
2992 if (udta->meta) {
2993 if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2994 return 0;
2995 }
2996 }
2997 if (udta->entries) {
2998 /* extra atoms */
2999 if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
3000 return 0;
3001 }
3002
3003 atom_write_size (buffer, size, offset, original_offset);
3004 return *offset - original_offset;
3005 }
3006
3007 static guint64
atom_mehd_copy_data(AtomMEHD * mehd,guint8 ** buffer,guint64 * size,guint64 * offset)3008 atom_mehd_copy_data (AtomMEHD * mehd, guint8 ** buffer, guint64 * size,
3009 guint64 * offset)
3010 {
3011 guint64 original_offset = *offset;
3012
3013 if (!atom_full_copy_data (&mehd->header, buffer, size, offset)) {
3014 return 0;
3015 }
3016
3017 prop_copy_uint64 (mehd->fragment_duration, buffer, size, offset);
3018
3019 atom_write_size (buffer, size, offset, original_offset);
3020 return *offset - original_offset;
3021 }
3022
3023 static guint64
atom_trex_copy_data(AtomTREX * trex,guint8 ** buffer,guint64 * size,guint64 * offset)3024 atom_trex_copy_data (AtomTREX * trex, guint8 ** buffer, guint64 * size,
3025 guint64 * offset)
3026 {
3027 guint64 original_offset = *offset;
3028
3029 if (!atom_full_copy_data (&trex->header, buffer, size, offset)) {
3030 return 0;
3031 }
3032
3033 prop_copy_uint32 (trex->track_ID, buffer, size, offset);
3034 prop_copy_uint32 (trex->default_sample_description_index, buffer, size,
3035 offset);
3036 prop_copy_uint32 (trex->default_sample_duration, buffer, size, offset);
3037 prop_copy_uint32 (trex->default_sample_size, buffer, size, offset);
3038 prop_copy_uint32 (trex->default_sample_flags, buffer, size, offset);
3039
3040 atom_write_size (buffer, size, offset, original_offset);
3041 return *offset - original_offset;
3042 }
3043
3044 static guint64
atom_mvex_copy_data(AtomMVEX * mvex,guint8 ** buffer,guint64 * size,guint64 * offset)3045 atom_mvex_copy_data (AtomMVEX * mvex, guint8 ** buffer, guint64 * size,
3046 guint64 * offset)
3047 {
3048 guint64 original_offset = *offset;
3049 GList *walker;
3050
3051 if (!atom_copy_data (&mvex->header, buffer, size, offset)) {
3052 return 0;
3053 }
3054
3055 /* only write mehd if we have anything extra to add */
3056 if (!atom_mehd_copy_data (&mvex->mehd, buffer, size, offset)) {
3057 return 0;
3058 }
3059
3060 walker = g_list_first (mvex->trexs);
3061 while (walker != NULL) {
3062 if (!atom_trex_copy_data ((AtomTREX *) walker->data, buffer, size, offset)) {
3063 return 0;
3064 }
3065 walker = g_list_next (walker);
3066 }
3067
3068 atom_write_size (buffer, size, offset, original_offset);
3069 return *offset - original_offset;
3070 }
3071
3072 guint64
atom_trak_copy_data(AtomTRAK * trak,guint8 ** buffer,guint64 * size,guint64 * offset)3073 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
3074 guint64 * offset)
3075 {
3076 guint64 original_offset = *offset;
3077
3078 if (!atom_copy_data (&trak->header, buffer, size, offset)) {
3079 return 0;
3080 }
3081 if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
3082 return 0;
3083 }
3084 if (trak->tapt) {
3085 if (!trak->tapt->copy_data_func (trak->tapt->atom, buffer, size, offset)) {
3086 return 0;
3087 }
3088 }
3089 if (trak->edts) {
3090 if (!atom_edts_copy_data (trak->edts, buffer, size, offset)) {
3091 return 0;
3092 }
3093 }
3094 if (trak->tref) {
3095 /* Make sure we need this atom (there is a referenced track */
3096 if (atom_array_get_len (&trak->tref->entries) > 0) {
3097 if (!atom_tref_copy_data (trak->tref, buffer, size, offset)) {
3098 return 0;
3099 }
3100 }
3101 }
3102
3103 if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
3104 return 0;
3105 }
3106
3107 if (!atom_udta_copy_data (&trak->udta, buffer, size, offset)) {
3108 return 0;
3109 }
3110
3111 atom_write_size (buffer, size, offset, original_offset);
3112 return *offset - original_offset;
3113 }
3114
3115
3116 guint64
atom_moov_copy_data(AtomMOOV * atom,guint8 ** buffer,guint64 * size,guint64 * offset)3117 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
3118 guint64 * offset)
3119 {
3120 guint64 original_offset = *offset;
3121 GList *walker;
3122
3123 if (!atom_copy_data (&(atom->header), buffer, size, offset))
3124 return 0;
3125
3126 if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
3127 return 0;
3128
3129 walker = g_list_first (atom->traks);
3130 while (walker != NULL) {
3131 if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
3132 return 0;
3133 }
3134 walker = g_list_next (walker);
3135 }
3136
3137 if (!atom_udta_copy_data (&atom->udta, buffer, size, offset)) {
3138 return 0;
3139 }
3140
3141 if (atom->fragmented) {
3142 if (!atom_mvex_copy_data (&atom->mvex, buffer, size, offset)) {
3143 return 0;
3144 }
3145 }
3146
3147 atom_write_size (buffer, size, offset, original_offset);
3148 return *offset - original_offset;
3149 }
3150
3151 static guint64
atom_wave_copy_data(AtomWAVE * wave,guint8 ** buffer,guint64 * size,guint64 * offset)3152 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
3153 guint64 * size, guint64 * offset)
3154 {
3155 guint64 original_offset = *offset;
3156
3157 if (!atom_copy_data (&(wave->header), buffer, size, offset))
3158 return 0;
3159
3160 if (wave->extension_atoms) {
3161 if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
3162 return 0;
3163 }
3164
3165 atom_write_size (buffer, size, offset, original_offset);
3166 return *offset - original_offset;
3167 }
3168
3169 /* -- end of copy data functions -- */
3170
3171 /* -- general functions, API and support functions */
3172
3173 /* add samples to tables */
3174
3175 void
atom_stsc_add_new_entry(AtomSTSC * stsc,guint32 first_chunk,guint32 nsamples,guint32 sample_description_index)3176 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples,
3177 guint32 sample_description_index)
3178 {
3179 gint len;
3180
3181 if ((len = atom_array_get_len (&stsc->entries)) > 1 &&
3182 ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
3183 (atom_array_index (&stsc->entries, len - 2)).samples_per_chunk)) {
3184 STSCEntry *nentry;
3185
3186 /* Merge last two entries as they have the same number of samples per chunk */
3187 nentry = &atom_array_index (&stsc->entries, len - 1);
3188 nentry->first_chunk = first_chunk;
3189 nentry->samples_per_chunk = nsamples;
3190 nentry->sample_description_index = sample_description_index;
3191 } else {
3192 STSCEntry nentry;
3193
3194 nentry.first_chunk = first_chunk;
3195 nentry.samples_per_chunk = nsamples;
3196 nentry.sample_description_index = sample_description_index;
3197 atom_array_append (&stsc->entries, nentry, 128);
3198 }
3199 }
3200
3201 static void
atom_stsc_update_entry(AtomSTSC * stsc,guint32 first_chunk,guint32 nsamples)3202 atom_stsc_update_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
3203 {
3204 gint len;
3205
3206 len = atom_array_get_len (&stsc->entries);
3207 g_assert (len != 0);
3208 g_assert (atom_array_index (&stsc->entries,
3209 len - 1).first_chunk == first_chunk);
3210
3211 atom_array_index (&stsc->entries, len - 1).samples_per_chunk += nsamples;
3212 }
3213
3214 static void
atom_stts_add_entry(AtomSTTS * stts,guint32 sample_count,gint32 sample_delta)3215 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
3216 {
3217 STTSEntry *entry = NULL;
3218
3219 if (G_LIKELY (atom_array_get_len (&stts->entries) != 0))
3220 entry = &atom_array_index (&stts->entries,
3221 atom_array_get_len (&stts->entries) - 1);
3222
3223 if (entry && entry->sample_delta == sample_delta) {
3224 entry->sample_count += sample_count;
3225 } else {
3226 STTSEntry nentry;
3227
3228 nentry.sample_count = sample_count;
3229 nentry.sample_delta = sample_delta;
3230 atom_array_append (&stts->entries, nentry, 256);
3231 }
3232 }
3233
3234 static void
atom_stsz_add_entry(AtomSTSZ * stsz,guint32 nsamples,guint32 size)3235 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
3236 {
3237 guint32 i;
3238
3239 stsz->table_size += nsamples;
3240 if (stsz->sample_size != 0) {
3241 /* it is constant size, we don't need entries */
3242 return;
3243 }
3244 for (i = 0; i < nsamples; i++) {
3245 atom_array_append (&stsz->entries, size, 1024);
3246 }
3247 }
3248
3249 static guint32
atom_stco64_get_entry_count(AtomSTCO64 * stco64)3250 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
3251 {
3252 return atom_array_get_len (&stco64->entries);
3253 }
3254
3255 /* returns TRUE if a new entry was added */
3256 static gboolean
atom_stco64_add_entry(AtomSTCO64 * stco64,guint64 entry)3257 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
3258 {
3259 guint32 len;
3260
3261 /* Only add a new entry if the chunk offset changed */
3262 if ((len = atom_array_get_len (&stco64->entries)) &&
3263 ((atom_array_index (&stco64->entries, len - 1)) == entry))
3264 return FALSE;
3265
3266 atom_array_append (&stco64->entries, entry, 256);
3267 if (entry > stco64->max_offset)
3268 stco64->max_offset = entry;
3269
3270 return TRUE;
3271 }
3272
3273 void
atom_tref_add_entry(AtomTREF * tref,guint32 sample)3274 atom_tref_add_entry (AtomTREF * tref, guint32 sample)
3275 {
3276 atom_array_append (&tref->entries, sample, 512);
3277 }
3278
3279 static void
atom_stss_add_entry(AtomSTSS * stss,guint32 sample)3280 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
3281 {
3282 atom_array_append (&stss->entries, sample, 512);
3283 }
3284
3285 static void
atom_stbl_add_stss_entry(AtomSTBL * stbl)3286 atom_stbl_add_stss_entry (AtomSTBL * stbl)
3287 {
3288 guint32 sample_index = stbl->stsz.table_size;
3289
3290 atom_stss_add_entry (&stbl->stss, sample_index);
3291 }
3292
3293 static void
atom_ctts_add_entry(AtomCTTS * ctts,guint32 nsamples,guint32 offset)3294 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
3295 {
3296 CTTSEntry *entry = NULL;
3297
3298 if (G_LIKELY (atom_array_get_len (&ctts->entries) != 0))
3299 entry = &atom_array_index (&ctts->entries,
3300 atom_array_get_len (&ctts->entries) - 1);
3301
3302 if (entry == NULL || entry->sampleoffset != offset) {
3303 CTTSEntry nentry;
3304
3305 nentry.samplecount = nsamples;
3306 nentry.sampleoffset = offset;
3307 atom_array_append (&ctts->entries, nentry, 256);
3308 if (offset != 0)
3309 ctts->do_pts = TRUE;
3310 } else {
3311 entry->samplecount += nsamples;
3312 }
3313 }
3314
3315 static void
atom_stbl_add_ctts_entry(AtomSTBL * stbl,guint32 nsamples,guint32 offset)3316 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
3317 {
3318 if (stbl->ctts == NULL) {
3319 stbl->ctts = atom_ctts_new ();
3320 }
3321 atom_ctts_add_entry (stbl->ctts, nsamples, offset);
3322 }
3323
3324 void
atom_stbl_add_samples(AtomSTBL * stbl,guint32 nsamples,guint32 delta,guint32 size,guint64 chunk_offset,gboolean sync,gint64 pts_offset)3325 atom_stbl_add_samples (AtomSTBL * stbl, guint32 nsamples, guint32 delta,
3326 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3327 {
3328 atom_stts_add_entry (&stbl->stts, nsamples, delta);
3329 atom_stsz_add_entry (&stbl->stsz, nsamples, size);
3330 if (atom_stco64_add_entry (&stbl->stco64, chunk_offset)) {
3331 atom_stsc_add_new_entry (&stbl->stsc,
3332 atom_stco64_get_entry_count (&stbl->stco64), nsamples,
3333 stbl->stsd.n_entries);
3334 } else {
3335 atom_stsc_update_entry (&stbl->stsc,
3336 atom_stco64_get_entry_count (&stbl->stco64), nsamples);
3337 }
3338
3339 if (sync)
3340 atom_stbl_add_stss_entry (stbl);
3341 /* always store to arrange for consistent content */
3342 atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
3343 }
3344
3345 void
atom_trak_add_samples(AtomTRAK * trak,guint32 nsamples,guint32 delta,guint32 size,guint64 chunk_offset,gboolean sync,gint64 pts_offset)3346 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
3347 guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
3348 {
3349 AtomSTBL *stbl = &trak->mdia.minf.stbl;
3350 atom_stbl_add_samples (stbl, nsamples, delta, size, chunk_offset, sync,
3351 pts_offset);
3352 }
3353
3354 /* trak and moov molding */
3355
3356 guint32
atom_trak_get_timescale(AtomTRAK * trak)3357 atom_trak_get_timescale (AtomTRAK * trak)
3358 {
3359 return trak->mdia.mdhd.time_info.timescale;
3360 }
3361
3362 guint32
atom_trak_get_id(AtomTRAK * trak)3363 atom_trak_get_id (AtomTRAK * trak)
3364 {
3365 return trak->tkhd.track_ID;
3366 }
3367
3368 static void
atom_trak_set_id(AtomTRAK * trak,guint32 id)3369 atom_trak_set_id (AtomTRAK * trak, guint32 id)
3370 {
3371 trak->tkhd.track_ID = id;
3372 }
3373
3374 static void
atom_moov_add_trex(AtomMOOV * moov,AtomTREX * trex)3375 atom_moov_add_trex (AtomMOOV * moov, AtomTREX * trex)
3376 {
3377 moov->mvex.trexs = g_list_append (moov->mvex.trexs, trex);
3378 }
3379
3380 static AtomTREX *
atom_trex_new(AtomTRAK * trak)3381 atom_trex_new (AtomTRAK * trak)
3382 {
3383 guint8 flags[3] = { 0, 0, 0 };
3384 AtomTREX *trex = g_new0 (AtomTREX, 1);
3385
3386 atom_full_init (&trex->header, FOURCC_trex, 0, 0, 0, flags);
3387
3388 trex->track_ID = trak->tkhd.track_ID;
3389 trex->default_sample_description_index = 1;
3390 trex->default_sample_duration = 0;
3391 trex->default_sample_size = 0;
3392 trex->default_sample_flags = 0;
3393
3394 return trex;
3395 }
3396
3397 void
atom_moov_add_trak(AtomMOOV * moov,AtomTRAK * trak)3398 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
3399 {
3400 atom_trak_set_id (trak, moov->mvhd.next_track_id++);
3401 moov->traks = g_list_append (moov->traks, trak);
3402 /* additional trak means also new trex */
3403 atom_moov_add_trex (moov, atom_trex_new (trak));
3404 }
3405
3406 guint
atom_moov_get_trak_count(AtomMOOV * moov)3407 atom_moov_get_trak_count (AtomMOOV * moov)
3408 {
3409 return g_list_length (moov->traks);
3410 }
3411
3412 static guint64
atom_trak_get_duration(AtomTRAK * trak)3413 atom_trak_get_duration (AtomTRAK * trak)
3414 {
3415 return trak->tkhd.duration;
3416 }
3417
3418 static guint64
atom_stts_get_total_duration(AtomSTTS * stts)3419 atom_stts_get_total_duration (AtomSTTS * stts)
3420 {
3421 guint i;
3422 guint64 sum = 0;
3423
3424 for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
3425 STTSEntry *entry = &atom_array_index (&stts->entries, i);
3426
3427 sum += (guint64) (entry->sample_count) * entry->sample_delta;
3428 }
3429 return sum;
3430 }
3431
3432 static void
atom_trak_update_duration(AtomTRAK * trak,guint64 moov_timescale)3433 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
3434 {
3435 trak->mdia.mdhd.time_info.duration =
3436 atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
3437 if (trak->mdia.mdhd.time_info.timescale != 0) {
3438 trak->tkhd.duration =
3439 gst_util_uint64_scale_round (trak->mdia.mdhd.time_info.duration,
3440 moov_timescale, trak->mdia.mdhd.time_info.timescale);
3441 } else {
3442 trak->tkhd.duration = 0;
3443 }
3444 }
3445
3446 static void
timecode_atom_trak_set_duration(AtomTRAK * trak,guint64 duration,guint64 timescale)3447 timecode_atom_trak_set_duration (AtomTRAK * trak, guint64 duration,
3448 guint64 timescale)
3449 {
3450 STTSEntry *entry;
3451 GList *iter;
3452
3453 /* Sanity checks to ensure we have a timecode */
3454 g_assert (trak->mdia.minf.gmhd != NULL);
3455 g_assert (atom_array_get_len (&trak->mdia.minf.stbl.stts.entries) == 1);
3456
3457 for (iter = trak->mdia.minf.stbl.stsd.entries; iter;
3458 iter = g_list_next (iter)) {
3459 SampleTableEntry *entry = iter->data;
3460 if (entry->kind == TIMECODE) {
3461 SampleTableEntryTMCD *tmcd = (SampleTableEntryTMCD *) entry;
3462
3463 duration = duration * tmcd->timescale / timescale;
3464 timescale = tmcd->timescale;
3465 break;
3466 }
3467 }
3468
3469 trak->tkhd.duration = duration;
3470 trak->mdia.mdhd.time_info.duration = duration;
3471 trak->mdia.mdhd.time_info.timescale = timescale;
3472
3473 entry = &atom_array_index (&trak->mdia.minf.stbl.stts.entries, 0);
3474 entry->sample_delta = duration;
3475 }
3476
3477 static guint32
atom_moov_get_timescale(AtomMOOV * moov)3478 atom_moov_get_timescale (AtomMOOV * moov)
3479 {
3480 return moov->mvhd.time_info.timescale;
3481 }
3482
3483 void
atom_moov_update_timescale(AtomMOOV * moov,guint32 timescale)3484 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
3485 {
3486 moov->mvhd.time_info.timescale = timescale;
3487 }
3488
3489 void
atom_moov_update_duration(AtomMOOV * moov)3490 atom_moov_update_duration (AtomMOOV * moov)
3491 {
3492 GList *traks = moov->traks;
3493 guint64 dur, duration = 0;
3494
3495 while (traks) {
3496 AtomTRAK *trak = (AtomTRAK *) traks->data;
3497
3498 /* Skip timecodes for now: they have a placeholder duration */
3499 if (trak->mdia.minf.gmhd == NULL || trak->mdia.minf.gmhd->tmcd == NULL) {
3500 atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
3501 dur = atom_trak_get_duration (trak);
3502 if (dur > duration)
3503 duration = dur;
3504 }
3505 traks = g_list_next (traks);
3506 }
3507 /* Now update the duration of the timecodes */
3508 traks = moov->traks;
3509 while (traks) {
3510 AtomTRAK *trak = (AtomTRAK *) traks->data;
3511
3512 if (trak->mdia.minf.gmhd != NULL && trak->mdia.minf.gmhd->tmcd != NULL)
3513 timecode_atom_trak_set_duration (trak, duration,
3514 atom_moov_get_timescale (moov));
3515 traks = g_list_next (traks);
3516 }
3517 moov->mvhd.time_info.duration = duration;
3518 moov->mvex.mehd.fragment_duration = duration;
3519 }
3520
3521 void
atom_moov_set_fragmented(AtomMOOV * moov,gboolean fragmented)3522 atom_moov_set_fragmented (AtomMOOV * moov, gboolean fragmented)
3523 {
3524 moov->fragmented = fragmented;
3525 }
3526
3527 void
atom_stco64_chunks_set_offset(AtomSTCO64 * stco64,guint32 offset)3528 atom_stco64_chunks_set_offset (AtomSTCO64 * stco64, guint32 offset)
3529 {
3530 stco64->chunk_offset = offset;
3531 }
3532
3533 void
atom_moov_chunks_set_offset(AtomMOOV * moov,guint32 offset)3534 atom_moov_chunks_set_offset (AtomMOOV * moov, guint32 offset)
3535 {
3536 GList *traks = moov->traks;
3537
3538 if (offset == moov->chunks_offset)
3539 return; /* Nothing to do */
3540
3541 while (traks) {
3542 AtomTRAK *trak = (AtomTRAK *) traks->data;
3543
3544 atom_stco64_chunks_set_offset (&trak->mdia.minf.stbl.stco64, offset);
3545 traks = g_list_next (traks);
3546 }
3547
3548 moov->chunks_offset = offset;
3549 }
3550
3551 void
atom_trak_update_bitrates(AtomTRAK * trak,guint32 avg_bitrate,guint32 max_bitrate)3552 atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
3553 guint32 max_bitrate)
3554 {
3555 AtomESDS *esds = NULL;
3556 AtomData *btrt = NULL;
3557 AtomWAVE *wave = NULL;
3558 AtomSTSD *stsd;
3559 GList *iter;
3560 GList *extensioniter = NULL;
3561
3562 g_return_if_fail (trak != NULL);
3563
3564 if (avg_bitrate == 0 && max_bitrate == 0)
3565 return;
3566
3567 stsd = &trak->mdia.minf.stbl.stsd;
3568 for (iter = stsd->entries; iter; iter = g_list_next (iter)) {
3569 SampleTableEntry *entry = iter->data;
3570
3571 switch (entry->kind) {
3572 case AUDIO:{
3573 SampleTableEntryMP4A *audioentry = (SampleTableEntryMP4A *) entry;
3574 extensioniter = audioentry->extension_atoms;
3575 break;
3576 }
3577 case VIDEO:{
3578 SampleTableEntryMP4V *videoentry = (SampleTableEntryMP4V *) entry;
3579 extensioniter = videoentry->extension_atoms;
3580 break;
3581 }
3582 default:
3583 break;
3584 }
3585 }
3586
3587 for (; extensioniter; extensioniter = g_list_next (extensioniter)) {
3588 AtomInfo *atominfo = extensioniter->data;
3589 if (atominfo->atom->type == FOURCC_esds) {
3590 esds = (AtomESDS *) atominfo->atom;
3591 } else if (atominfo->atom->type == FOURCC_btrt) {
3592 btrt = (AtomData *) atominfo->atom;
3593 } else if (atominfo->atom->type == FOURCC_wave) {
3594 wave = (AtomWAVE *) atominfo->atom;
3595 }
3596 }
3597
3598 /* wave might have an esds internally */
3599 if (wave) {
3600 for (extensioniter = wave->extension_atoms; extensioniter;
3601 extensioniter = g_list_next (extensioniter)) {
3602 AtomInfo *atominfo = extensioniter->data;
3603 if (atominfo->atom->type == FOURCC_esds) {
3604 esds = (AtomESDS *) atominfo->atom;
3605 break;
3606 }
3607 }
3608 }
3609
3610 if (esds) {
3611 if (avg_bitrate && esds->es.dec_conf_desc.avg_bitrate == 0)
3612 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
3613 if (max_bitrate && esds->es.dec_conf_desc.max_bitrate == 0)
3614 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
3615 }
3616 if (btrt) {
3617 /* type(4bytes) + size(4bytes) + buffersize(4bytes) +
3618 * maxbitrate(bytes) + avgbitrate(bytes) */
3619 if (max_bitrate && GST_READ_UINT32_BE (btrt->data + 4) == 0)
3620 GST_WRITE_UINT32_BE (btrt->data + 4, max_bitrate);
3621 if (avg_bitrate && GST_READ_UINT32_BE (btrt->data + 8) == 0)
3622 GST_WRITE_UINT32_BE (btrt->data + 8, avg_bitrate);
3623 }
3624 }
3625
3626 void
atom_trak_tx3g_update_dimension(AtomTRAK * trak,guint32 width,guint32 height)3627 atom_trak_tx3g_update_dimension (AtomTRAK * trak, guint32 width, guint32 height)
3628 {
3629 AtomSTSD *stsd;
3630 GList *iter;
3631 SampleTableEntryTX3G *tx3g = NULL;
3632
3633 stsd = &trak->mdia.minf.stbl.stsd;
3634 for (iter = stsd->entries; iter && tx3g == NULL; iter = g_list_next (iter)) {
3635 SampleTableEntry *entry = iter->data;
3636
3637 switch (entry->kind) {
3638 case SUBTITLE:{
3639 tx3g = (SampleTableEntryTX3G *) entry;
3640 break;
3641 }
3642 default:
3643 break;
3644 }
3645 }
3646
3647 /* Currently we never set the vertical placement flag, so we don't
3648 * check for it to set the dimensions differently as the spec says.
3649 * Always do it for the not set case */
3650 if (tx3g) {
3651 tx3g->font_size = 0.05 * height;
3652
3653 height = 0.15 * height;
3654 trak->tkhd.width = width << 16;
3655 trak->tkhd.height = height << 16;
3656 tx3g->default_text_box = width | (height << 16);
3657 }
3658 }
3659
3660 /*
3661 * Meta tags functions
3662 */
3663 static void
atom_tag_data_alloc_data(AtomTagData * data,guint size)3664 atom_tag_data_alloc_data (AtomTagData * data, guint size)
3665 {
3666 g_free (data->data);
3667 data->data = g_new0 (guint8, size);
3668 data->datalen = size;
3669 }
3670
3671 static void
atom_udta_append_tag(AtomUDTA * udta,AtomInfo * tag)3672 atom_udta_append_tag (AtomUDTA * udta, AtomInfo * tag)
3673 {
3674 GList **entries;
3675
3676 if (udta->meta)
3677 entries = &udta->meta->ilst->entries;
3678 else
3679 entries = &udta->entries;
3680 *entries = g_list_append (*entries, tag);
3681 }
3682
3683 void
atom_udta_add_tag(AtomUDTA * udta,guint32 fourcc,guint32 flags,const guint8 * data,guint size)3684 atom_udta_add_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3685 const guint8 * data, guint size)
3686 {
3687 AtomTag *tag;
3688 AtomTagData *tdata;
3689
3690 tag = atom_tag_new (fourcc, flags);
3691 tdata = &tag->data;
3692 atom_tag_data_alloc_data (tdata, size);
3693 memmove (tdata->data, data, size);
3694
3695 atom_udta_append_tag (udta,
3696 build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
3697 atom_tag_free));
3698 }
3699
3700 void
atom_udta_add_str_tag(AtomUDTA * udta,guint32 fourcc,const gchar * value)3701 atom_udta_add_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3702 {
3703 gint len = strlen (value);
3704
3705 if (len > 0)
3706 atom_udta_add_tag (udta, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
3707 }
3708
3709 void
atom_udta_add_uint_tag(AtomUDTA * udta,guint32 fourcc,guint32 flags,guint32 value)3710 atom_udta_add_uint_tag (AtomUDTA * udta, guint32 fourcc, guint32 flags,
3711 guint32 value)
3712 {
3713 guint8 data[8] = { 0, };
3714
3715 if (flags) {
3716 GST_WRITE_UINT16_BE (data, value);
3717 atom_udta_add_tag (udta, fourcc, flags, data, 2);
3718 } else {
3719 GST_WRITE_UINT32_BE (data + 2, value);
3720 atom_udta_add_tag (udta, fourcc, flags, data, 8);
3721 }
3722 }
3723
3724 #define GST_BUFFER_NEW_READONLY(mem, size) \
3725 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, mem, size, \
3726 0, size, mem, NULL)
3727
3728 void
atom_udta_add_blob_tag(AtomUDTA * udta,guint8 * data,guint size)3729 atom_udta_add_blob_tag (AtomUDTA * udta, guint8 * data, guint size)
3730 {
3731 AtomData *data_atom;
3732 guint len;
3733 guint32 fourcc;
3734
3735 if (size < 8)
3736 return;
3737
3738 /* blob is unparsed atom;
3739 * extract size and fourcc, and wrap remainder in data atom */
3740 len = GST_READ_UINT32_BE (data);
3741 fourcc = GST_READ_UINT32_LE (data + 4);
3742 if (len > size)
3743 return;
3744
3745 data_atom = atom_data_new_from_data (fourcc, data + 8, len - 8);
3746
3747 atom_udta_append_tag (udta,
3748 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3749 atom_data_free));
3750 }
3751
3752 void
atom_udta_add_3gp_tag(AtomUDTA * udta,guint32 fourcc,guint8 * data,guint size)3753 atom_udta_add_3gp_tag (AtomUDTA * udta, guint32 fourcc, guint8 * data,
3754 guint size)
3755 {
3756 AtomData *data_atom;
3757
3758 data_atom = atom_data_new (fourcc);
3759
3760 /* need full atom */
3761 atom_data_alloc_mem (data_atom, size + 4);
3762
3763 /* full atom: version and flags */
3764 GST_WRITE_UINT32_BE (data_atom->data, 0);
3765 memcpy (data_atom->data + 4, data, size);
3766
3767 atom_udta_append_tag (udta,
3768 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3769 atom_data_free));
3770 }
3771
3772 guint16
language_code(const char * lang)3773 language_code (const char *lang)
3774 {
3775 g_return_val_if_fail (lang != NULL, 0);
3776 g_return_val_if_fail (strlen (lang) == 3, 0);
3777
3778 return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
3779 ((lang[2] - 0x60) & 0x1F);
3780 }
3781
3782 void
atom_udta_add_3gp_str_int_tag(AtomUDTA * udta,guint32 fourcc,const gchar * value,gint16 ivalue)3783 atom_udta_add_3gp_str_int_tag (AtomUDTA * udta, guint32 fourcc,
3784 const gchar * value, gint16 ivalue)
3785 {
3786 gint len = 0, size = 0;
3787 guint8 *data;
3788
3789 if (value) {
3790 len = strlen (value);
3791 size = len + 3;
3792 }
3793
3794 if (ivalue >= 0)
3795 size += 2;
3796
3797 data = g_malloc (size + 3);
3798 /* language tag and null-terminated UTF-8 string */
3799 if (value) {
3800 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
3801 /* include 0 terminator */
3802 memcpy (data + 2, value, len + 1);
3803 }
3804 /* 16-bit unsigned int if standalone, otherwise 8-bit */
3805 if (ivalue >= 0) {
3806 if (size == 2)
3807 GST_WRITE_UINT16_BE (data + size - 2, ivalue);
3808 else {
3809 GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
3810 size--;
3811 }
3812 }
3813
3814 atom_udta_add_3gp_tag (udta, fourcc, data, size);
3815 g_free (data);
3816 }
3817
3818 void
atom_udta_add_3gp_str_tag(AtomUDTA * udta,guint32 fourcc,const gchar * value)3819 atom_udta_add_3gp_str_tag (AtomUDTA * udta, guint32 fourcc, const gchar * value)
3820 {
3821 atom_udta_add_3gp_str_int_tag (udta, fourcc, value, -1);
3822 }
3823
3824 void
atom_udta_add_3gp_uint_tag(AtomUDTA * udta,guint32 fourcc,guint16 value)3825 atom_udta_add_3gp_uint_tag (AtomUDTA * udta, guint32 fourcc, guint16 value)
3826 {
3827 atom_udta_add_3gp_str_int_tag (udta, fourcc, NULL, value);
3828 }
3829
3830 void
atom_udta_add_xmp_tags(AtomUDTA * udta,GstBuffer * xmpbuffer)3831 atom_udta_add_xmp_tags (AtomUDTA * udta, GstBuffer * xmpbuffer)
3832 {
3833 AtomData *data_atom = NULL;
3834
3835 if (udta->context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3836 if (xmpbuffer) {
3837 data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
3838 udta->entries = g_list_append (udta->entries,
3839 build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3840 atom_data_free));
3841 }
3842 } else {
3843 GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
3844 }
3845 }
3846
3847 /*
3848 * Functions for specifying media types
3849 */
3850
3851 static void
atom_minf_set_audio(AtomMINF * minf)3852 atom_minf_set_audio (AtomMINF * minf)
3853 {
3854 atom_minf_clear_handlers (minf);
3855 minf->smhd = atom_smhd_new ();
3856 }
3857
3858 static void
atom_minf_set_video(AtomMINF * minf,AtomsContext * context)3859 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
3860 {
3861 atom_minf_clear_handlers (minf);
3862 minf->vmhd = atom_vmhd_new (context);
3863 }
3864
3865 static void
atom_minf_set_subtitle(AtomMINF * minf)3866 atom_minf_set_subtitle (AtomMINF * minf)
3867 {
3868 atom_minf_clear_handlers (minf);
3869 }
3870
3871 static void
atom_hdlr_set_type(AtomHDLR * hdlr,AtomsContext * context,guint32 comp_type,guint32 hdlr_type)3872 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
3873 guint32 hdlr_type)
3874 {
3875 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3876 hdlr->component_type = comp_type;
3877 }
3878 hdlr->handler_type = hdlr_type;
3879 }
3880
3881 static void
atom_hdlr_set_name(AtomHDLR * hdlr,const char * name)3882 atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
3883 {
3884 g_free (hdlr->name);
3885 hdlr->name = g_strdup (name);
3886 }
3887
3888 static void
atom_mdia_set_hdlr_type_audio(AtomMDIA * mdia,AtomsContext * context)3889 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
3890 {
3891 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3892 /* Some players (low-end hardware) check for this name, which is what
3893 * QuickTime itself sets */
3894 atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3895 }
3896
3897 static void
atom_mdia_set_hdlr_type_video(AtomMDIA * mdia,AtomsContext * context)3898 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3899 {
3900 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3901 /* Some players (low-end hardware) check for this name, which is what
3902 * QuickTime itself sets */
3903 atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3904 }
3905
3906 static void
atom_mdia_set_hdlr_type_subtitle(AtomMDIA * mdia,AtomsContext * context)3907 atom_mdia_set_hdlr_type_subtitle (AtomMDIA * mdia, AtomsContext * context)
3908 {
3909 atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_sbtl);
3910
3911 /* Just follows the pattern from video and audio above */
3912 atom_hdlr_set_name (&mdia->hdlr, "SubtitleHandler");
3913 }
3914
3915 static void
atom_mdia_set_audio(AtomMDIA * mdia,AtomsContext * context)3916 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3917 {
3918 atom_mdia_set_hdlr_type_audio (mdia, context);
3919 atom_minf_set_audio (&mdia->minf);
3920 }
3921
3922 static void
atom_mdia_set_video(AtomMDIA * mdia,AtomsContext * context)3923 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3924 {
3925 atom_mdia_set_hdlr_type_video (mdia, context);
3926 atom_minf_set_video (&mdia->minf, context);
3927 }
3928
3929 static void
atom_mdia_set_subtitle(AtomMDIA * mdia,AtomsContext * context)3930 atom_mdia_set_subtitle (AtomMDIA * mdia, AtomsContext * context)
3931 {
3932 atom_mdia_set_hdlr_type_subtitle (mdia, context);
3933 atom_minf_set_subtitle (&mdia->minf);
3934 }
3935
3936 static void
atom_tkhd_set_audio(AtomTKHD * tkhd)3937 atom_tkhd_set_audio (AtomTKHD * tkhd)
3938 {
3939 tkhd->volume = 0x0100;
3940 tkhd->width = tkhd->height = 0;
3941 }
3942
3943 static void
atom_tkhd_set_video(AtomTKHD * tkhd,AtomsContext * context,guint32 width,guint32 height)3944 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3945 guint32 height)
3946 {
3947 tkhd->volume = 0;
3948
3949 /* qt and ISO base media do not contradict, and examples agree */
3950 tkhd->width = width;
3951 tkhd->height = height;
3952 }
3953
3954 static void
atom_tkhd_set_subtitle(AtomTKHD * tkhd,AtomsContext * context,guint32 width,guint32 height)3955 atom_tkhd_set_subtitle (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3956 guint32 height)
3957 {
3958 tkhd->volume = 0;
3959
3960 /* qt and ISO base media do not contradict, and examples agree */
3961 tkhd->width = width;
3962 tkhd->height = height;
3963 }
3964
3965
3966 static void
atom_edts_add_entry(AtomEDTS * edts,gint index,EditListEntry * entry)3967 atom_edts_add_entry (AtomEDTS * edts, gint index, EditListEntry * entry)
3968 {
3969 EditListEntry *e =
3970 (EditListEntry *) g_slist_nth_data (edts->elst.entries, index);
3971 /* Create a new entry if missing (appends to the list if index is larger) */
3972 if (e == NULL) {
3973 e = g_new (EditListEntry, 1);
3974 edts->elst.entries = g_slist_insert (edts->elst.entries, e, index);
3975 }
3976
3977 /* Update the entry */
3978 *e = *entry;
3979 }
3980
3981 void
atom_trak_edts_clear(AtomTRAK * trak)3982 atom_trak_edts_clear (AtomTRAK * trak)
3983 {
3984 if (trak->edts) {
3985 atom_edts_free (trak->edts);
3986 trak->edts = NULL;
3987 }
3988 }
3989
3990 /*
3991 * Update an entry in this trak edits list, creating it if needed.
3992 * index is the index of the entry to update, or create if it's past the end.
3993 * duration is in the moov's timescale
3994 * media_time is the offset in the media time to start from (media's timescale)
3995 * rate is a 32 bits fixed-point
3996 */
3997 void
atom_trak_set_elst_entry(AtomTRAK * trak,gint index,guint32 duration,guint32 media_time,guint32 rate)3998 atom_trak_set_elst_entry (AtomTRAK * trak, gint index,
3999 guint32 duration, guint32 media_time, guint32 rate)
4000 {
4001 EditListEntry entry;
4002
4003 entry.duration = duration;
4004 entry.media_time = media_time;
4005 entry.media_rate = rate;
4006
4007 if (trak->edts == NULL)
4008 trak->edts = atom_edts_new ();
4009
4010 atom_edts_add_entry (trak->edts, index, &entry);
4011 }
4012
4013 /* re-negotiation is prevented at top-level, so only 1 entry expected.
4014 * Quite some more care here and elsewhere may be needed to
4015 * support several entries */
4016 static SampleTableEntryMP4A *
atom_trak_add_audio_entry(AtomTRAK * trak,AtomsContext * context,guint32 type)4017 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
4018 guint32 type)
4019 {
4020 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
4021 SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
4022
4023 mp4a->se.header.type = type;
4024 mp4a->se.kind = AUDIO;
4025 mp4a->compression_id = -1;
4026 mp4a->se.data_reference_index = 1;
4027
4028 stsd->entries = g_list_prepend (stsd->entries, mp4a);
4029 stsd->n_entries++;
4030 return mp4a;
4031 }
4032
4033 /* return number of centiframes per second */
4034 guint
atom_framerate_to_timescale(gint n,gint d)4035 atom_framerate_to_timescale (gint n, gint d)
4036 {
4037 if (n == 0)
4038 return 10000;
4039
4040 if (d != 1 && d != 1001) {
4041 /* otherwise there are probably rounding errors and we should rather guess
4042 * if it's close enough to a well known framerate */
4043 gst_video_guess_framerate (gst_util_uint64_scale (d, GST_SECOND, n), &n,
4044 &d);
4045 }
4046
4047 return gst_util_uint64_scale (n, 100, d);
4048 }
4049
4050 static SampleTableEntryTMCD *
atom_trak_add_timecode_entry(AtomTRAK * trak,AtomsContext * context,guint32 trak_timescale,GstVideoTimeCode * tc)4051 atom_trak_add_timecode_entry (AtomTRAK * trak, AtomsContext * context,
4052 guint32 trak_timescale, GstVideoTimeCode * tc)
4053 {
4054 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
4055 SampleTableEntryTMCD *tmcd = sample_entry_tmcd_new ();
4056
4057 g_assert (trak_timescale != 0);
4058
4059 trak->mdia.hdlr.component_type = FOURCC_mhlr;
4060 trak->mdia.hdlr.handler_type = FOURCC_tmcd;
4061 g_free (trak->mdia.hdlr.name);
4062 trak->mdia.hdlr.name = g_strdup ("Time Code Media Handler");
4063 trak->mdia.mdhd.time_info.timescale = trak_timescale;
4064
4065 tmcd->se.kind = TIMECODE;
4066 tmcd->se.data_reference_index = 1;
4067 tmcd->tc_flags = TC_24H_MAX;
4068 if (tc->config.flags &= GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME)
4069 tmcd->tc_flags |= TC_DROP_FRAME;
4070 tmcd->name.language_code = 0;
4071 tmcd->name.name = g_strdup ("Tape");
4072 tmcd->timescale = trak_timescale;
4073 tmcd->frame_duration =
4074 gst_util_uint64_scale (tmcd->timescale, tc->config.fps_d,
4075 tc->config.fps_n);
4076 if (tc->config.fps_d == 1001)
4077 tmcd->n_frames = tc->config.fps_n / 1000;
4078 else
4079 tmcd->n_frames = tc->config.fps_n / tc->config.fps_d;
4080
4081 stsd->entries = g_list_prepend (stsd->entries, tmcd);
4082 stsd->n_entries++;
4083 return tmcd;
4084 }
4085
4086 static SampleTableEntryMP4V *
atom_trak_add_video_entry(AtomTRAK * trak,AtomsContext * context,guint32 type)4087 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
4088 guint32 type)
4089 {
4090 SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
4091 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
4092
4093 mp4v->se.header.type = type;
4094 mp4v->se.kind = VIDEO;
4095 mp4v->se.data_reference_index = 1;
4096 mp4v->horizontal_resolution = 72 << 16;
4097 mp4v->vertical_resolution = 72 << 16;
4098 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
4099 mp4v->spatial_quality = 512;
4100 mp4v->temporal_quality = 512;
4101 }
4102
4103 stsd->entries = g_list_prepend (stsd->entries, mp4v);
4104 stsd->n_entries++;
4105 return mp4v;
4106 }
4107
4108 static SampleTableEntryTX3G *
atom_trak_add_subtitle_entry(AtomTRAK * trak,AtomsContext * context,guint32 type)4109 atom_trak_add_subtitle_entry (AtomTRAK * trak, AtomsContext * context,
4110 guint32 type)
4111 {
4112 SampleTableEntryTX3G *tx3g = sample_entry_tx3g_new ();
4113 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
4114
4115 tx3g->se.header.type = type;
4116 tx3g->se.kind = SUBTITLE;
4117 tx3g->se.data_reference_index = 1;
4118
4119 stsd->entries = g_list_prepend (stsd->entries, tx3g);
4120 stsd->n_entries++;
4121 return tx3g;
4122 }
4123
4124
4125 void
atom_trak_set_constant_size_samples(AtomTRAK * trak,guint32 sample_size)4126 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
4127 {
4128 trak->mdia.minf.stbl.stsz.sample_size = sample_size;
4129 }
4130
4131 static void
atom_trak_set_audio(AtomTRAK * trak,AtomsContext * context)4132 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
4133 {
4134 atom_tkhd_set_audio (&trak->tkhd);
4135 atom_mdia_set_audio (&trak->mdia, context);
4136 }
4137
4138 static void
atom_trak_set_video(AtomTRAK * trak,AtomsContext * context,guint32 width,guint32 height)4139 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
4140 guint32 height)
4141 {
4142 atom_tkhd_set_video (&trak->tkhd, context, width, height);
4143 atom_mdia_set_video (&trak->mdia, context);
4144 }
4145
4146 static void
atom_trak_set_subtitle(AtomTRAK * trak,AtomsContext * context)4147 atom_trak_set_subtitle (AtomTRAK * trak, AtomsContext * context)
4148 {
4149 atom_tkhd_set_subtitle (&trak->tkhd, context, 0, 0);
4150 atom_mdia_set_subtitle (&trak->mdia, context);
4151 }
4152
4153 static void
atom_trak_set_audio_commons(AtomTRAK * trak,AtomsContext * context,guint32 rate)4154 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
4155 guint32 rate)
4156 {
4157 atom_trak_set_audio (trak, context);
4158 trak->mdia.mdhd.time_info.timescale = rate;
4159 }
4160
4161 static void
atom_trak_set_video_commons(AtomTRAK * trak,AtomsContext * context,guint32 rate,guint32 width,guint32 height)4162 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
4163 guint32 rate, guint32 width, guint32 height)
4164 {
4165 atom_trak_set_video (trak, context, width, height);
4166 trak->mdia.mdhd.time_info.timescale = rate;
4167 trak->tkhd.width = width << 16;
4168 trak->tkhd.height = height << 16;
4169 }
4170
4171 static void
atom_trak_set_subtitle_commons(AtomTRAK * trak,AtomsContext * context)4172 atom_trak_set_subtitle_commons (AtomTRAK * trak, AtomsContext * context)
4173 {
4174 atom_trak_set_subtitle (trak, context);
4175 trak->mdia.mdhd.time_info.timescale = 1000;
4176
4177 trak->tkhd.alternate_group = 2; /* same for all subtitles */
4178 trak->tkhd.layer = -1; /* above video (layer 0) */
4179 }
4180
4181 void
sample_table_entry_add_ext_atom(SampleTableEntry * ste,AtomInfo * ext)4182 sample_table_entry_add_ext_atom (SampleTableEntry * ste, AtomInfo * ext)
4183 {
4184 GList **list = NULL;
4185 if (ste->kind == AUDIO) {
4186 list = &(((SampleTableEntryMP4A *) ste)->extension_atoms);
4187 } else if (ste->kind == VIDEO) {
4188 list = &(((SampleTableEntryMP4V *) ste)->extension_atoms);
4189 } else {
4190 g_assert_not_reached ();
4191 return;
4192 }
4193
4194 *list = g_list_prepend (*list, ext);
4195 }
4196
4197 SampleTableEntryMP4A *
atom_trak_set_audio_type(AtomTRAK * trak,AtomsContext * context,AudioSampleEntry * entry,guint32 scale,AtomInfo * ext,gint sample_size)4198 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
4199 AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
4200 {
4201 SampleTableEntryMP4A *ste;
4202
4203 atom_trak_set_audio_commons (trak, context, scale);
4204 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4205 ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
4206
4207 trak->is_video = FALSE;
4208 trak->is_h264 = FALSE;
4209
4210 ste->version = entry->version;
4211 ste->compression_id = entry->compression_id;
4212 ste->sample_size = entry->sample_size;
4213 ste->sample_rate = entry->sample_rate << 16;
4214 ste->channels = entry->channels;
4215
4216 ste->samples_per_packet = entry->samples_per_packet;
4217 ste->bytes_per_sample = entry->bytes_per_sample;
4218 ste->bytes_per_packet = entry->bytes_per_packet;
4219 ste->bytes_per_frame = entry->bytes_per_frame;
4220
4221 if (ext)
4222 ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
4223
4224 /* 0 size means variable size */
4225 atom_trak_set_constant_size_samples (trak, sample_size);
4226
4227 return ste;
4228 }
4229
4230 SampleTableEntryTMCD *
atom_trak_set_timecode_type(AtomTRAK * trak,AtomsContext * context,guint32 trak_timescale,GstVideoTimeCode * tc)4231 atom_trak_set_timecode_type (AtomTRAK * trak, AtomsContext * context,
4232 guint32 trak_timescale, GstVideoTimeCode * tc)
4233 {
4234 SampleTableEntryTMCD *ste;
4235
4236 if (context->flavor != ATOMS_TREE_FLAVOR_MOV &&
4237 !context->force_create_timecode_trak) {
4238 return NULL;
4239 }
4240
4241
4242 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
4243 AtomGMHD *gmhd = trak->mdia.minf.gmhd;
4244
4245 gmhd = atom_gmhd_new ();
4246 gmhd->gmin.graphics_mode = 0x0040;
4247 gmhd->gmin.opcolor[0] = 0x8000;
4248 gmhd->gmin.opcolor[1] = 0x8000;
4249 gmhd->gmin.opcolor[2] = 0x8000;
4250 gmhd->tmcd = atom_tmcd_new ();
4251 gmhd->tmcd->tcmi.text_size = 12;
4252 gmhd->tmcd->tcmi.font_name = g_strdup ("Chicago"); /* Pascal string */
4253
4254 trak->mdia.minf.gmhd = gmhd;
4255 } else if (context->force_create_timecode_trak) {
4256 AtomNMHD *nmhd = trak->mdia.minf.nmhd;
4257 /* MOV files use GMHD, other files use NMHD */
4258
4259 nmhd = atom_nmhd_new ();
4260 trak->mdia.minf.nmhd = nmhd;
4261 } else {
4262 return NULL;
4263 }
4264 ste = atom_trak_add_timecode_entry (trak, context, trak_timescale, tc);
4265 trak->is_video = FALSE;
4266 trak->is_h264 = FALSE;
4267
4268 return ste;
4269 }
4270
4271 SampleTableEntry *
atom_trak_set_caption_type(AtomTRAK * trak,AtomsContext * context,guint32 trak_timescale,guint32 caption_type)4272 atom_trak_set_caption_type (AtomTRAK * trak, AtomsContext * context,
4273 guint32 trak_timescale, guint32 caption_type)
4274 {
4275 SampleTableEntry *ste;
4276 AtomGMHD *gmhd = trak->mdia.minf.gmhd;
4277 AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
4278
4279 if (context->flavor != ATOMS_TREE_FLAVOR_MOV) {
4280 return NULL;
4281 }
4282
4283 trak->mdia.mdhd.time_info.timescale = trak_timescale;
4284 trak->mdia.hdlr.component_type = FOURCC_mhlr;
4285 trak->mdia.hdlr.handler_type = FOURCC_clcp;
4286 g_free (trak->mdia.hdlr.name);
4287 trak->mdia.hdlr.name = g_strdup ("Closed Caption Media Handler");
4288
4289 ste = g_new0 (SampleTableEntry, 1);
4290 atom_sample_entry_init (ste, caption_type);
4291 ste->kind = CLOSEDCAPTION;
4292 ste->data_reference_index = 1;
4293 stsd->entries = g_list_prepend (stsd->entries, ste);
4294 stsd->n_entries++;
4295
4296 gmhd = atom_gmhd_new ();
4297 gmhd->gmin.graphics_mode = 0x0040;
4298 gmhd->gmin.opcolor[0] = 0x8000;
4299 gmhd->gmin.opcolor[1] = 0x8000;
4300 gmhd->gmin.opcolor[2] = 0x8000;
4301
4302 trak->mdia.minf.gmhd = gmhd;
4303 trak->is_video = FALSE;
4304 trak->is_h264 = FALSE;
4305
4306 return ste;
4307 }
4308
4309 static AtomInfo *
build_pasp_extension(gint par_width,gint par_height)4310 build_pasp_extension (gint par_width, gint par_height)
4311 {
4312 AtomData *atom_data = atom_data_new (FOURCC_pasp);
4313 guint8 *data;
4314
4315 atom_data_alloc_mem (atom_data, 8);
4316 data = atom_data->data;
4317
4318 /* ihdr = image header box */
4319 GST_WRITE_UINT32_BE (data, par_width);
4320 GST_WRITE_UINT32_BE (data + 4, par_height);
4321
4322 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4323 atom_data_free);
4324 }
4325
4326 AtomInfo *
build_fiel_extension(GstVideoInterlaceMode mode,GstVideoFieldOrder order)4327 build_fiel_extension (GstVideoInterlaceMode mode, GstVideoFieldOrder order)
4328 {
4329 AtomData *atom_data = atom_data_new (FOURCC_fiel);
4330 guint8 *data;
4331 gint field_order;
4332 gint interlace;
4333
4334 atom_data_alloc_mem (atom_data, 2);
4335 data = atom_data->data;
4336
4337 if (mode == GST_VIDEO_INTERLACE_MODE_PROGRESSIVE) {
4338 interlace = 1;
4339 field_order = 0;
4340 } else if (mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED) {
4341 interlace = 2;
4342 field_order = order == GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST ? 9 : 14;
4343 } else {
4344 interlace = 0;
4345 field_order = 0;
4346 }
4347
4348 GST_WRITE_UINT8 (data, interlace);
4349 GST_WRITE_UINT8 (data + 1, field_order);
4350
4351 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4352 atom_data_free);
4353 }
4354
4355 AtomInfo *
build_colr_extension(const GstVideoColorimetry * colorimetry,gboolean is_mp4)4356 build_colr_extension (const GstVideoColorimetry * colorimetry, gboolean is_mp4)
4357 {
4358 AtomData *atom_data = atom_data_new (FOURCC_colr);
4359 guint8 *data;
4360 guint16 primaries;
4361 guint16 transfer_function;
4362 guint16 matrix;
4363
4364 primaries = gst_video_color_primaries_to_iso (colorimetry->primaries);
4365 transfer_function =
4366 gst_video_transfer_function_to_iso (colorimetry->transfer);
4367 matrix = gst_video_color_matrix_to_iso (colorimetry->matrix);
4368
4369 atom_data_alloc_mem (atom_data, 10 + (is_mp4 ? 1 : 0));
4370 data = atom_data->data;
4371
4372 /* colour specification box */
4373 if (is_mp4)
4374 GST_WRITE_UINT32_LE (data, FOURCC_nclx);
4375 else
4376 GST_WRITE_UINT32_LE (data, FOURCC_nclc);
4377
4378 GST_WRITE_UINT16_BE (data + 4, primaries);
4379 GST_WRITE_UINT16_BE (data + 6, transfer_function);
4380 GST_WRITE_UINT16_BE (data + 8, matrix);
4381
4382 if (is_mp4) {
4383 GST_WRITE_UINT8 (data + 10,
4384 colorimetry->range == GST_VIDEO_COLOR_RANGE_0_255 ? 0x80 : 0x00);
4385 }
4386
4387 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4388 atom_data_free);
4389 }
4390
4391 AtomInfo *
build_clap_extension(gint width_n,gint width_d,gint height_n,gint height_d,gint h_off_n,gint h_off_d,gint v_off_n,gint v_off_d)4392 build_clap_extension (gint width_n, gint width_d, gint height_n, gint height_d,
4393 gint h_off_n, gint h_off_d, gint v_off_n, gint v_off_d)
4394 {
4395 AtomData *atom_data = atom_data_new (FOURCC_clap);
4396 guint8 *data;
4397
4398 atom_data_alloc_mem (atom_data, 32);
4399 data = atom_data->data;
4400
4401 GST_WRITE_UINT32_BE (data, width_n);
4402 GST_WRITE_UINT32_BE (data + 4, width_d);
4403 GST_WRITE_UINT32_BE (data + 8, height_n);
4404 GST_WRITE_UINT32_BE (data + 12, height_d);
4405 GST_WRITE_UINT32_BE (data + 16, h_off_n);
4406 GST_WRITE_UINT32_BE (data + 20, h_off_d);
4407 GST_WRITE_UINT32_BE (data + 24, v_off_n);
4408 GST_WRITE_UINT32_BE (data + 28, v_off_d);
4409
4410 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4411 atom_data_free);
4412 }
4413
4414 AtomInfo *
build_tapt_extension(gint clef_width,gint clef_height,gint prof_width,gint prof_height,gint enof_width,gint enof_height)4415 build_tapt_extension (gint clef_width, gint clef_height, gint prof_width,
4416 gint prof_height, gint enof_width, gint enof_height)
4417 {
4418 AtomData *atom_data = atom_data_new (FOURCC_tapt);
4419 guint8 *data;
4420
4421 atom_data_alloc_mem (atom_data, 60);
4422 data = atom_data->data;
4423
4424 GST_WRITE_UINT32_BE (data, 20);
4425 GST_WRITE_UINT32_LE (data + 4, FOURCC_clef);
4426 GST_WRITE_UINT32_BE (data + 8, 0);
4427 GST_WRITE_UINT32_BE (data + 12, clef_width);
4428 GST_WRITE_UINT32_BE (data + 16, clef_height);
4429
4430 GST_WRITE_UINT32_BE (data + 20, 20);
4431 GST_WRITE_UINT32_LE (data + 24, FOURCC_prof);
4432 GST_WRITE_UINT32_BE (data + 28, 0);
4433 GST_WRITE_UINT32_BE (data + 32, prof_width);
4434 GST_WRITE_UINT32_BE (data + 36, prof_height);
4435
4436 GST_WRITE_UINT32_BE (data + 40, 20);
4437 GST_WRITE_UINT32_LE (data + 44, FOURCC_enof);
4438 GST_WRITE_UINT32_BE (data + 48, 0);
4439 GST_WRITE_UINT32_BE (data + 52, enof_width);
4440 GST_WRITE_UINT32_BE (data + 56, enof_height);
4441
4442
4443 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4444 atom_data_free);
4445 }
4446
4447 static AtomInfo *
build_mov_video_sample_description_padding_extension(void)4448 build_mov_video_sample_description_padding_extension (void)
4449 {
4450 AtomData *atom_data = atom_data_new (FOURCC_clap);
4451
4452 return build_atom_info_wrapper ((Atom *) atom_data, atom_copy_empty,
4453 atom_data_free);
4454 }
4455
4456 SampleTableEntryMP4V *
atom_trak_set_video_type(AtomTRAK * trak,AtomsContext * context,VisualSampleEntry * entry,guint32 scale,GList * ext_atoms_list)4457 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
4458 VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
4459 {
4460 SampleTableEntryMP4V *ste;
4461 guint dwidth, dheight;
4462 gint par_n = 0, par_d = 0;
4463
4464 par_n = entry->par_n;
4465 par_d = entry->par_d;
4466
4467 dwidth = entry->width;
4468 dheight = entry->height;
4469 /* ISO file spec says track header w/h indicates track's visual presentation
4470 * (so this together with pixels w/h implicitly defines PAR) */
4471 if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
4472 dwidth = entry->width * par_n / par_d;
4473 dheight = entry->height;
4474 }
4475
4476 if (trak->mdia.minf.stbl.stsd.n_entries < 1) {
4477 atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
4478 trak->is_video = TRUE;
4479 trak->is_h264 = (entry->fourcc == FOURCC_avc1
4480 || entry->fourcc == FOURCC_avc3);
4481 }
4482 ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
4483
4484 ste->version = entry->version;
4485 ste->width = entry->width;
4486 ste->height = entry->height;
4487 ste->depth = entry->depth;
4488 ste->color_table_id = entry->color_table_id;
4489 ste->frame_count = entry->frame_count;
4490
4491 if (ext_atoms_list)
4492 ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
4493
4494 ste->extension_atoms = g_list_append (ste->extension_atoms,
4495 build_pasp_extension (par_n, par_d));
4496
4497 if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
4498 /* append 0 as a terminator "length" to work around some broken software */
4499 ste->extension_atoms =
4500 g_list_append (ste->extension_atoms,
4501 build_mov_video_sample_description_padding_extension ());
4502 }
4503
4504 return ste;
4505 }
4506
4507 void
subtitle_sample_entry_init(SubtitleSampleEntry * entry)4508 subtitle_sample_entry_init (SubtitleSampleEntry * entry)
4509 {
4510 entry->font_size = 0;
4511 entry->font_face = 0;
4512 entry->foreground_color_rgba = 0xFFFFFFFF; /* all white, opaque */
4513 }
4514
4515 SampleTableEntryTX3G *
atom_trak_set_subtitle_type(AtomTRAK * trak,AtomsContext * context,SubtitleSampleEntry * entry)4516 atom_trak_set_subtitle_type (AtomTRAK * trak, AtomsContext * context,
4517 SubtitleSampleEntry * entry)
4518 {
4519 SampleTableEntryTX3G *tx3g;
4520
4521 atom_trak_set_subtitle_commons (trak, context);
4522 atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
4523 tx3g = atom_trak_add_subtitle_entry (trak, context, entry->fourcc);
4524
4525 tx3g->font_face = entry->font_face;
4526 tx3g->font_size = entry->font_size;
4527 tx3g->foreground_color_rgba = entry->foreground_color_rgba;
4528
4529 trak->is_video = FALSE;
4530 trak->is_h264 = FALSE;
4531
4532 return tx3g;
4533 }
4534
4535 static void
atom_mfhd_init(AtomMFHD * mfhd,guint32 sequence_number)4536 atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
4537 {
4538 guint8 flags[3] = { 0, 0, 0 };
4539
4540 atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
4541 mfhd->sequence_number = sequence_number;
4542 }
4543
4544 static void
atom_moof_init(AtomMOOF * moof,AtomsContext * context,guint32 sequence_number)4545 atom_moof_init (AtomMOOF * moof, AtomsContext * context,
4546 guint32 sequence_number)
4547 {
4548 atom_header_set (&moof->header, FOURCC_moof, 0, 0);
4549 atom_mfhd_init (&moof->mfhd, sequence_number);
4550 moof->trafs = NULL;
4551 }
4552
4553 AtomMOOF *
atom_moof_new(AtomsContext * context,guint32 sequence_number)4554 atom_moof_new (AtomsContext * context, guint32 sequence_number)
4555 {
4556 AtomMOOF *moof = g_new0 (AtomMOOF, 1);
4557
4558 atom_moof_init (moof, context, sequence_number);
4559 return moof;
4560 }
4561
4562 void
atom_moof_set_base_offset(AtomMOOF * moof,guint64 offset)4563 atom_moof_set_base_offset (AtomMOOF * moof, guint64 offset)
4564 {
4565 GList *trafs = moof->trafs;
4566
4567 if (offset == moof->traf_offset)
4568 return; /* Nothing to do */
4569
4570 while (trafs) {
4571 AtomTRAF *traf = (AtomTRAF *) trafs->data;
4572
4573 traf->tfhd.header.flags[2] |= TF_BASE_DATA_OFFSET;
4574 traf->tfhd.base_data_offset = offset;
4575 trafs = g_list_next (trafs);
4576 }
4577
4578 moof->traf_offset = offset;
4579 }
4580
4581 static void
atom_trun_free(AtomTRUN * trun)4582 atom_trun_free (AtomTRUN * trun)
4583 {
4584 atom_full_clear (&trun->header);
4585 atom_array_clear (&trun->entries);
4586 g_free (trun);
4587 }
4588
4589 static void
atom_sdtp_free(AtomSDTP * sdtp)4590 atom_sdtp_free (AtomSDTP * sdtp)
4591 {
4592 atom_full_clear (&sdtp->header);
4593 atom_array_clear (&sdtp->entries);
4594 g_free (sdtp);
4595 }
4596
4597 void
atom_traf_free(AtomTRAF * traf)4598 atom_traf_free (AtomTRAF * traf)
4599 {
4600 GList *walker;
4601
4602 walker = traf->truns;
4603 while (walker) {
4604 atom_trun_free ((AtomTRUN *) walker->data);
4605 walker = g_list_next (walker);
4606 }
4607 g_list_free (traf->truns);
4608 traf->truns = NULL;
4609
4610 walker = traf->sdtps;
4611 while (walker) {
4612 atom_sdtp_free ((AtomSDTP *) walker->data);
4613 walker = g_list_next (walker);
4614 }
4615 g_list_free (traf->sdtps);
4616 traf->sdtps = NULL;
4617
4618 g_free (traf);
4619 }
4620
4621 void
atom_moof_free(AtomMOOF * moof)4622 atom_moof_free (AtomMOOF * moof)
4623 {
4624 GList *walker;
4625
4626 walker = moof->trafs;
4627 while (walker) {
4628 atom_traf_free ((AtomTRAF *) walker->data);
4629 walker = g_list_next (walker);
4630 }
4631 g_list_free (moof->trafs);
4632 moof->trafs = NULL;
4633
4634 g_free (moof);
4635 }
4636
4637 static guint64
atom_mfhd_copy_data(AtomMFHD * mfhd,guint8 ** buffer,guint64 * size,guint64 * offset)4638 atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
4639 guint64 * offset)
4640 {
4641 guint64 original_offset = *offset;
4642
4643 if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
4644 return 0;
4645 }
4646
4647 prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
4648
4649 atom_write_size (buffer, size, offset, original_offset);
4650 return *offset - original_offset;
4651 }
4652
4653 static guint64
atom_tfhd_copy_data(AtomTFHD * tfhd,guint8 ** buffer,guint64 * size,guint64 * offset)4654 atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
4655 guint64 * offset)
4656 {
4657 guint64 original_offset = *offset;
4658 guint32 flags;
4659
4660 if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
4661 return 0;
4662 }
4663
4664 prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
4665
4666 flags = atom_full_get_flags_as_uint (&tfhd->header);
4667
4668 if (flags & TF_BASE_DATA_OFFSET)
4669 prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
4670 if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
4671 prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
4672 if (flags & TF_DEFAULT_SAMPLE_DURATION)
4673 prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
4674 if (flags & TF_DEFAULT_SAMPLE_SIZE)
4675 prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
4676 if (flags & TF_DEFAULT_SAMPLE_FLAGS)
4677 prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
4678
4679 atom_write_size (buffer, size, offset, original_offset);
4680 return *offset - original_offset;
4681 }
4682
4683 static guint64
atom_tfdt_copy_data(AtomTFDT * tfdt,guint8 ** buffer,guint64 * size,guint64 * offset)4684 atom_tfdt_copy_data (AtomTFDT * tfdt, guint8 ** buffer, guint64 * size,
4685 guint64 * offset)
4686 {
4687 guint64 original_offset = *offset;
4688
4689 if (!atom_full_copy_data (&tfdt->header, buffer, size, offset)) {
4690 return 0;
4691 }
4692
4693 /* 32-bit time if version == 0 else 64-bit: */
4694 if (tfdt->header.version == 0)
4695 prop_copy_uint32 (tfdt->base_media_decode_time, buffer, size, offset);
4696 else
4697 prop_copy_uint64 (tfdt->base_media_decode_time, buffer, size, offset);
4698
4699 atom_write_size (buffer, size, offset, original_offset);
4700 return *offset - original_offset;
4701 }
4702
4703 static guint64
atom_trun_copy_data(AtomTRUN * trun,guint8 ** buffer,guint64 * size,guint64 * offset)4704 atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
4705 guint64 * offset)
4706 {
4707 guint64 original_offset = *offset;
4708 guint32 flags, i;
4709
4710 flags = atom_full_get_flags_as_uint (&trun->header);
4711
4712 atom_full_set_flags_as_uint (&trun->header, flags);
4713
4714 if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
4715 return 0;
4716 }
4717
4718 prop_copy_uint32 (trun->sample_count, buffer, size, offset);
4719
4720 if (flags & TR_DATA_OFFSET) {
4721 prop_copy_int32 (trun->data_offset, buffer, size, offset);
4722 }
4723 if (flags & TR_FIRST_SAMPLE_FLAGS)
4724 prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
4725
4726 for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
4727 TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
4728
4729 if (flags & TR_SAMPLE_DURATION)
4730 prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
4731 if (flags & TR_SAMPLE_SIZE)
4732 prop_copy_uint32 (entry->sample_size, buffer, size, offset);
4733 if (flags & TR_SAMPLE_FLAGS)
4734 prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
4735 if (flags & TR_COMPOSITION_TIME_OFFSETS)
4736 prop_copy_uint32 (entry->sample_composition_time_offset,
4737 buffer, size, offset);
4738 }
4739
4740 atom_write_size (buffer, size, offset, original_offset);
4741 return *offset - original_offset;
4742 }
4743
4744 static guint64
atom_sdtp_copy_data(AtomSDTP * sdtp,guint8 ** buffer,guint64 * size,guint64 * offset)4745 atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
4746 guint64 * offset)
4747 {
4748 guint64 original_offset = *offset;
4749
4750 if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
4751 return 0;
4752 }
4753
4754 /* all entries at once */
4755 prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
4756 atom_array_get_len (&sdtp->entries), buffer, size, offset);
4757
4758 atom_write_size (buffer, size, offset, original_offset);
4759 return *offset - original_offset;
4760 }
4761
4762 static guint64
atom_traf_copy_data(AtomTRAF * traf,guint8 ** buffer,guint64 * size,guint64 * offset)4763 atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
4764 guint64 * offset)
4765 {
4766 guint64 original_offset = *offset;
4767 GList *walker;
4768
4769 if (!atom_copy_data (&traf->header, buffer, size, offset)) {
4770 return 0;
4771 }
4772 if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
4773 return 0;
4774 }
4775 if (!atom_tfdt_copy_data (&traf->tfdt, buffer, size, offset)) {
4776 return 0;
4777 }
4778 walker = g_list_first (traf->truns);
4779 while (walker != NULL) {
4780 if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset)) {
4781 return 0;
4782 }
4783 walker = g_list_next (walker);
4784 }
4785
4786 walker = g_list_first (traf->sdtps);
4787 while (walker != NULL) {
4788 if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
4789 return 0;
4790 }
4791 walker = g_list_next (walker);
4792 }
4793
4794 atom_write_size (buffer, size, offset, original_offset);
4795 return *offset - original_offset;
4796 }
4797
4798 /* creates moof atom; metadata is written expecting actual buffer data
4799 * is in mdata directly after moof, and is consecutively written per trak */
4800 guint64
atom_moof_copy_data(AtomMOOF * moof,guint8 ** buffer,guint64 * size,guint64 * offset)4801 atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
4802 guint64 * size, guint64 * offset)
4803 {
4804 guint64 original_offset = *offset;
4805 GList *walker;
4806
4807 if (!atom_copy_data (&moof->header, buffer, size, offset))
4808 return 0;
4809
4810 if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
4811 return 0;
4812
4813 walker = g_list_first (moof->trafs);
4814 while (walker != NULL) {
4815 if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset)) {
4816 return 0;
4817 }
4818 walker = g_list_next (walker);
4819 }
4820
4821 atom_write_size (buffer, size, offset, original_offset);
4822
4823 return *offset - original_offset;
4824 }
4825
4826 static void
atom_tfhd_init(AtomTFHD * tfhd,guint32 track_ID)4827 atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
4828 {
4829 guint8 flags[3] = { 0, 0, 0 };
4830
4831 atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
4832 tfhd->track_ID = track_ID;
4833 tfhd->base_data_offset = 0;
4834 tfhd->sample_description_index = 1;
4835 tfhd->default_sample_duration = 0;
4836 tfhd->default_sample_size = 0;
4837 tfhd->default_sample_flags = 0;
4838 }
4839
4840 static void
atom_tfdt_init(AtomTFDT * tfdt)4841 atom_tfdt_init (AtomTFDT * tfdt)
4842 {
4843 guint8 flags[3] = { 0, 0, 0 };
4844 atom_full_init (&tfdt->header, FOURCC_tfdt, 0, 0, 0, flags);
4845
4846 tfdt->base_media_decode_time = 0;
4847 }
4848
4849 static void
atom_trun_init(AtomTRUN * trun)4850 atom_trun_init (AtomTRUN * trun)
4851 {
4852 guint8 flags[3] = { 0, 0, 0 };
4853
4854 atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
4855 trun->sample_count = 0;
4856 trun->data_offset = 0;
4857 trun->first_sample_flags = 0;
4858 atom_array_init (&trun->entries, 512);
4859 }
4860
4861 static AtomTRUN *
atom_trun_new(void)4862 atom_trun_new (void)
4863 {
4864 AtomTRUN *trun = g_new0 (AtomTRUN, 1);
4865
4866 atom_trun_init (trun);
4867 return trun;
4868 }
4869
4870 static void
atom_sdtp_init(AtomSDTP * sdtp)4871 atom_sdtp_init (AtomSDTP * sdtp)
4872 {
4873 guint8 flags[3] = { 0, 0, 0 };
4874
4875 atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
4876 atom_array_init (&sdtp->entries, 512);
4877 }
4878
4879 static AtomSDTP *
atom_sdtp_new(AtomsContext * context)4880 atom_sdtp_new (AtomsContext * context)
4881 {
4882 AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
4883
4884 atom_sdtp_init (sdtp);
4885 return sdtp;
4886 }
4887
4888 static void
atom_traf_add_sdtp(AtomTRAF * traf,AtomSDTP * sdtp)4889 atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
4890 {
4891 traf->sdtps = g_list_append (traf->sdtps, sdtp);
4892 }
4893
4894 static void
atom_sdtp_add_samples(AtomSDTP * sdtp,guint8 val)4895 atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
4896 {
4897 /* it does not make much/any sense according to specs,
4898 * but that's how MS isml samples seem to do it */
4899 atom_array_append (&sdtp->entries, val, 256);
4900 }
4901
4902 void
atom_trun_set_offset(AtomTRUN * trun,gint32 offset)4903 atom_trun_set_offset (AtomTRUN * trun, gint32 offset)
4904 {
4905 trun->header.flags[2] |= TR_DATA_OFFSET;
4906 trun->data_offset = offset;
4907 }
4908
4909 static gboolean
atom_trun_can_append_samples_to_entry(AtomTRUN * trun,TRUNSampleEntry * nentry,guint32 nsamples,guint32 delta,guint32 size,guint32 flags,gint32 data_offset,gint64 pts_offset)4910 atom_trun_can_append_samples_to_entry (AtomTRUN * trun,
4911 TRUNSampleEntry * nentry, guint32 nsamples, guint32 delta, guint32 size,
4912 guint32 flags, gint32 data_offset, gint64 pts_offset)
4913 {
4914 if (pts_offset != 0)
4915 return FALSE;
4916 if (nentry->sample_flags != flags)
4917 return FALSE;
4918 if (trun->data_offset + nentry->sample_size != data_offset)
4919 return FALSE;
4920 if (nentry->sample_size != size)
4921 return FALSE;
4922 if (nentry->sample_duration != delta)
4923 return FALSE;
4924
4925 /* FIXME: this should be TRUE but currently fails on demuxing */
4926 return FALSE;
4927 }
4928
4929 static void
atom_trun_append_samples(AtomTRUN * trun,TRUNSampleEntry * nentry,guint32 nsamples,guint32 delta,guint32 size)4930 atom_trun_append_samples (AtomTRUN * trun, TRUNSampleEntry * nentry,
4931 guint32 nsamples, guint32 delta, guint32 size)
4932 {
4933 trun->sample_count += nsamples;
4934 }
4935
4936 static void
atom_trun_add_samples(AtomTRUN * trun,guint32 nsamples,guint32 delta,guint32 size,guint32 flags,gint64 pts_offset)4937 atom_trun_add_samples (AtomTRUN * trun, guint32 nsamples, guint32 delta,
4938 guint32 size, guint32 flags, gint64 pts_offset)
4939 {
4940 int i;
4941
4942 if (pts_offset != 0)
4943 trun->header.flags[1] |= (TR_COMPOSITION_TIME_OFFSETS >> 8);
4944
4945 for (i = 0; i < nsamples; i++) {
4946 TRUNSampleEntry nentry;
4947
4948 nentry.sample_duration = delta;
4949 nentry.sample_size = size;
4950 nentry.sample_flags = flags;
4951 if (pts_offset != 0) {
4952 nentry.sample_composition_time_offset = pts_offset + i * delta;
4953 } else {
4954 nentry.sample_composition_time_offset = 0;
4955 }
4956 atom_array_append (&trun->entries, nentry, 256);
4957 trun->sample_count++;
4958 }
4959 }
4960
4961 static void
atom_traf_init(AtomTRAF * traf,AtomsContext * context,guint32 track_ID)4962 atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
4963 {
4964 atom_header_set (&traf->header, FOURCC_traf, 0, 0);
4965 atom_tfdt_init (&traf->tfdt);
4966 atom_tfhd_init (&traf->tfhd, track_ID);
4967 traf->truns = NULL;
4968
4969 if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
4970 atom_traf_add_sdtp (traf, atom_sdtp_new (context));
4971 }
4972
4973 AtomTRAF *
atom_traf_new(AtomsContext * context,guint32 track_ID)4974 atom_traf_new (AtomsContext * context, guint32 track_ID)
4975 {
4976 AtomTRAF *traf = g_new0 (AtomTRAF, 1);
4977
4978 atom_traf_init (traf, context, track_ID);
4979 return traf;
4980 }
4981
4982 void
atom_traf_set_base_decode_time(AtomTRAF * traf,guint64 base_decode_time)4983 atom_traf_set_base_decode_time (AtomTRAF * traf, guint64 base_decode_time)
4984 {
4985 traf->tfdt.base_media_decode_time = base_decode_time;
4986 /* If we need to write a 64-bit tfdt, set the atom version */
4987 if (base_decode_time > G_MAXUINT32)
4988 traf->tfdt.header.version = 1;
4989 }
4990
4991 static void
atom_traf_add_trun(AtomTRAF * traf,AtomTRUN * trun)4992 atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
4993 {
4994 traf->truns = g_list_append (traf->truns, trun);
4995 }
4996
4997 void
atom_traf_add_samples(AtomTRAF * traf,guint32 nsamples,guint32 delta,guint32 size,gint32 data_offset,gboolean sync,gint64 pts_offset,gboolean sdtp_sync)4998 atom_traf_add_samples (AtomTRAF * traf, guint32 nsamples,
4999 guint32 delta, guint32 size, gint32 data_offset, gboolean sync,
5000 gint64 pts_offset, gboolean sdtp_sync)
5001 {
5002 GList *l = NULL;
5003 AtomTRUN *prev_trun, *trun = NULL;
5004 TRUNSampleEntry *nentry = NULL;
5005 guint32 flags;
5006
5007 /* 0x10000 is sample-is-difference-sample flag
5008 * low byte stuff is what ismv uses */
5009 flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
5010
5011 if (traf->truns) {
5012 trun = g_list_last (traf->truns)->data;
5013 nentry =
5014 &atom_array_index (&trun->entries,
5015 atom_array_get_len (&trun->entries) - 1);
5016
5017 if (!atom_trun_can_append_samples_to_entry (trun, nentry, nsamples, delta,
5018 size, flags, data_offset, pts_offset)) {
5019 /* if we can't add to the previous trun, write a new one */
5020 trun = NULL;
5021 nentry = NULL;
5022 }
5023 }
5024 prev_trun = trun;
5025
5026 if (!traf->truns) {
5027 /* optimistic; indicate all defaults present in tfhd */
5028 traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
5029 TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
5030 traf->tfhd.default_sample_duration = delta;
5031 traf->tfhd.default_sample_size = size;
5032 traf->tfhd.default_sample_flags = flags;
5033 }
5034
5035 if (!trun) {
5036 trun = atom_trun_new ();
5037 atom_traf_add_trun (traf, trun);
5038 trun->first_sample_flags = flags;
5039 trun->data_offset = data_offset;
5040 if (data_offset != 0)
5041 trun->header.flags[2] |= TR_DATA_OFFSET;
5042 }
5043
5044 /* check if still matching defaults,
5045 * if not, abandon default and need entry for each sample */
5046 if (traf->tfhd.default_sample_duration != delta || prev_trun == trun) {
5047 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
5048 for (l = traf->truns; l; l = g_list_next (l)) {
5049 ((AtomTRUN *) l->data)->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
5050 }
5051 }
5052 if (traf->tfhd.default_sample_size != size || prev_trun == trun) {
5053 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
5054 for (l = traf->truns; l; l = g_list_next (l)) {
5055 ((AtomTRUN *) l->data)->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
5056 }
5057 }
5058 if (traf->tfhd.default_sample_flags != flags || prev_trun == trun) {
5059 if (trun->sample_count == 1) {
5060 /* at least will need first sample flag */
5061 traf->tfhd.default_sample_flags = flags;
5062 trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
5063 } else {
5064 /* now we need sample flags for each sample */
5065 traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
5066 trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
5067 trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
5068 }
5069 }
5070
5071 if (prev_trun == trun) {
5072 atom_trun_append_samples (trun, nentry, nsamples, delta, size);
5073 } else {
5074 atom_trun_add_samples (trun, nsamples, delta, size, flags, pts_offset);
5075 }
5076
5077 if (traf->sdtps)
5078 atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
5079 }
5080
5081 guint32
atom_traf_get_sample_num(AtomTRAF * traf)5082 atom_traf_get_sample_num (AtomTRAF * traf)
5083 {
5084 AtomTRUN *trun;
5085
5086 if (G_UNLIKELY (!traf->truns))
5087 return 0;
5088
5089 /* FIXME: only one trun? */
5090 trun = traf->truns->data;
5091 return atom_array_get_len (&trun->entries);
5092 }
5093
5094 void
atom_moof_add_traf(AtomMOOF * moof,AtomTRAF * traf)5095 atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
5096 {
5097 moof->trafs = g_list_append (moof->trafs, traf);
5098 }
5099
5100 static void
atom_tfra_free(AtomTFRA * tfra)5101 atom_tfra_free (AtomTFRA * tfra)
5102 {
5103 atom_full_clear (&tfra->header);
5104 atom_array_clear (&tfra->entries);
5105 g_free (tfra);
5106 }
5107
5108 AtomMFRA *
atom_mfra_new(AtomsContext * context)5109 atom_mfra_new (AtomsContext * context)
5110 {
5111 AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
5112
5113 atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
5114 return mfra;
5115 }
5116
5117 void
atom_mfra_add_tfra(AtomMFRA * mfra,AtomTFRA * tfra)5118 atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
5119 {
5120 mfra->tfras = g_list_append (mfra->tfras, tfra);
5121 }
5122
5123 void
atom_mfra_free(AtomMFRA * mfra)5124 atom_mfra_free (AtomMFRA * mfra)
5125 {
5126 GList *walker;
5127
5128 walker = mfra->tfras;
5129 while (walker) {
5130 atom_tfra_free ((AtomTFRA *) walker->data);
5131 walker = g_list_next (walker);
5132 }
5133 g_list_free (mfra->tfras);
5134 mfra->tfras = NULL;
5135
5136 atom_clear (&mfra->header);
5137 g_free (mfra);
5138 }
5139
5140 static void
atom_tfra_init(AtomTFRA * tfra,guint32 track_ID)5141 atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
5142 {
5143 guint8 flags[3] = { 0, 0, 0 };
5144
5145 atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
5146 tfra->track_ID = track_ID;
5147 atom_array_init (&tfra->entries, 512);
5148 }
5149
5150 AtomTFRA *
atom_tfra_new(AtomsContext * context,guint32 track_ID)5151 atom_tfra_new (AtomsContext * context, guint32 track_ID)
5152 {
5153 AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
5154
5155 atom_tfra_init (tfra, track_ID);
5156 return tfra;
5157
5158 }
5159
5160 static inline gint
need_bytes(guint32 num)5161 need_bytes (guint32 num)
5162 {
5163 gint n = 0;
5164
5165 while (num >>= 8)
5166 n++;
5167
5168 return n;
5169 }
5170
5171 void
atom_tfra_add_entry(AtomTFRA * tfra,guint64 dts,guint32 sample_num)5172 atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
5173 {
5174 TFRAEntry entry;
5175
5176 entry.time = dts;
5177 /* fill in later */
5178 entry.moof_offset = 0;
5179 /* always write a single trun in a single traf */
5180 entry.traf_number = 1;
5181 entry.trun_number = 1;
5182 entry.sample_number = sample_num;
5183
5184 /* auto-use 64 bits if needed */
5185 if (dts > G_MAXUINT32)
5186 tfra->header.version = 1;
5187
5188 /* 1 byte will always do for traf and trun number,
5189 * check how much sample_num needs */
5190 tfra->lengths = (tfra->lengths & 0xfc) ||
5191 MAX (tfra->lengths, need_bytes (sample_num));
5192
5193 atom_array_append (&tfra->entries, entry, 256);
5194 }
5195
5196 void
atom_tfra_update_offset(AtomTFRA * tfra,guint64 offset)5197 atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
5198 {
5199 gint i;
5200
5201 /* auto-use 64 bits if needed */
5202 if (offset > G_MAXUINT32)
5203 tfra->header.version = 1;
5204
5205 for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
5206 TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
5207
5208 if (entry->moof_offset)
5209 break;
5210 entry->moof_offset = offset;
5211 }
5212 }
5213
5214 static guint64
atom_tfra_copy_data(AtomTFRA * tfra,guint8 ** buffer,guint64 * size,guint64 * offset)5215 atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
5216 guint64 * offset)
5217 {
5218 guint64 original_offset = *offset;
5219 guint32 i;
5220 TFRAEntry *entry;
5221 guint32 data;
5222 guint bytes;
5223 guint version;
5224
5225 if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
5226 return 0;
5227 }
5228
5229 prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
5230 prop_copy_uint32 (tfra->lengths, buffer, size, offset);
5231 prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
5232
5233 version = tfra->header.version;
5234 for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
5235 entry = &atom_array_index (&tfra->entries, i);
5236 if (version) {
5237 prop_copy_uint64 (entry->time, buffer, size, offset);
5238 prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
5239 } else {
5240 prop_copy_uint32 (entry->time, buffer, size, offset);
5241 prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
5242 }
5243
5244 bytes = (tfra->lengths & (0x3 << 4)) + 1;
5245 data = GUINT32_TO_BE (entry->traf_number);
5246 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5247 buffer, size, offset);
5248
5249 bytes = (tfra->lengths & (0x3 << 2)) + 1;
5250 data = GUINT32_TO_BE (entry->trun_number);
5251 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5252 buffer, size, offset);
5253
5254 bytes = (tfra->lengths & (0x3)) + 1;
5255 data = GUINT32_TO_BE (entry->sample_number);
5256 prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
5257 buffer, size, offset);
5258
5259 }
5260
5261 atom_write_size (buffer, size, offset, original_offset);
5262 return *offset - original_offset;
5263 }
5264
5265 static guint64
atom_mfro_copy_data(guint32 s,guint8 ** buffer,guint64 * size,guint64 * offset)5266 atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
5267 guint64 * offset)
5268 {
5269 guint64 original_offset = *offset;
5270 guint8 flags[3] = { 0, 0, 0 };
5271 AtomFull mfro;
5272
5273 atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
5274
5275 if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
5276 return 0;
5277 }
5278
5279 prop_copy_uint32 (s, buffer, size, offset);
5280
5281 atom_write_size (buffer, size, offset, original_offset);
5282
5283 return *offset - original_offset;
5284 }
5285
5286
5287 guint64
atom_mfra_copy_data(AtomMFRA * mfra,guint8 ** buffer,guint64 * size,guint64 * offset)5288 atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
5289 guint64 * offset)
5290 {
5291 guint64 original_offset = *offset;
5292 GList *walker;
5293
5294 if (!atom_copy_data (&mfra->header, buffer, size, offset))
5295 return 0;
5296
5297 walker = g_list_first (mfra->tfras);
5298 while (walker != NULL) {
5299 if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
5300 return 0;
5301 }
5302 walker = g_list_next (walker);
5303 }
5304
5305 /* 16 is the size of the mfro atom */
5306 if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
5307 size, offset))
5308 return 0;
5309
5310 atom_write_size (buffer, size, offset, original_offset);
5311 return *offset - original_offset;
5312 }
5313
5314 /* some sample description construction helpers */
5315
5316 AtomInfo *
build_esds_extension(AtomTRAK * trak,guint8 object_type,guint8 stream_type,const GstBuffer * codec_data,guint32 avg_bitrate,guint32 max_bitrate)5317 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
5318 const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
5319 {
5320 guint32 track_id;
5321 AtomESDS *esds;
5322
5323 track_id = trak->tkhd.track_ID;
5324
5325 esds = atom_esds_new ();
5326 esds->es.id = track_id & 0xFFFF;
5327 esds->es.dec_conf_desc.object_type = object_type;
5328 esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
5329
5330 if (avg_bitrate > 0)
5331 esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
5332 if (max_bitrate > 0)
5333 esds->es.dec_conf_desc.max_bitrate = max_bitrate;
5334
5335 /* optional DecoderSpecificInfo */
5336 if (codec_data) {
5337 DecoderSpecificInfoDescriptor *desc;
5338 gsize size;
5339
5340 esds->es.dec_conf_desc.dec_specific_info = desc =
5341 desc_dec_specific_info_new ();
5342 size = gst_buffer_get_size ((GstBuffer *) codec_data);
5343 desc_dec_specific_info_alloc_data (desc, size);
5344 gst_buffer_extract ((GstBuffer *) codec_data, 0, desc->data, size);
5345 }
5346
5347 return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
5348 atom_esds_free);
5349 }
5350
5351 AtomInfo *
build_btrt_extension(guint32 buffer_size_db,guint32 avg_bitrate,guint32 max_bitrate)5352 build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
5353 guint32 max_bitrate)
5354 {
5355 AtomData *atom_data = atom_data_new (FOURCC_btrt);
5356 guint8 *data;
5357
5358 atom_data_alloc_mem (atom_data, 12);
5359 data = atom_data->data;
5360
5361 GST_WRITE_UINT32_BE (data, buffer_size_db);
5362 GST_WRITE_UINT32_BE (data + 4, max_bitrate);
5363 GST_WRITE_UINT32_BE (data + 8, avg_bitrate);
5364
5365 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5366 atom_data_free);
5367 }
5368
5369 static AtomInfo *
build_mov_wave_extension(guint32 fourcc,AtomInfo * atom1,AtomInfo * atom2,gboolean terminator)5370 build_mov_wave_extension (guint32 fourcc, AtomInfo * atom1, AtomInfo * atom2,
5371 gboolean terminator)
5372 {
5373 AtomWAVE *wave;
5374 AtomFRMA *frma;
5375 Atom *ext_atom;
5376
5377 /* Build WAVE atom for sample table entry */
5378 wave = atom_wave_new ();
5379
5380 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5381 if (terminator) {
5382 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5383 wave->extension_atoms =
5384 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5385 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5386 }
5387
5388 /* Add supplied atoms to WAVE */
5389 if (atom2)
5390 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
5391 if (atom1)
5392 wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
5393
5394 /* Add FRMA to the WAVE */
5395 frma = atom_frma_new ();
5396 frma->media_type = fourcc;
5397
5398 wave->extension_atoms =
5399 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5400 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5401
5402 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5403 atom_wave_free);
5404 }
5405
5406 AtomInfo *
build_mov_aac_extension(AtomTRAK * trak,const GstBuffer * codec_data,guint32 avg_bitrate,guint32 max_bitrate)5407 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
5408 guint32 avg_bitrate, guint32 max_bitrate)
5409 {
5410 AtomInfo *esds, *mp4a;
5411 GstBuffer *buf;
5412 guint32 tmp = 0;
5413
5414 /* Add ESDS atom to WAVE */
5415 esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
5416 ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
5417
5418 /* Add MP4A atom to the WAVE:
5419 * not really in spec, but makes offset based players happy */
5420 buf = GST_BUFFER_NEW_READONLY (&tmp, 4);
5421 mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
5422 gst_buffer_unref (buf);
5423
5424 return build_mov_wave_extension (FOURCC_mp4a, mp4a, esds, TRUE);
5425 }
5426
5427 AtomInfo *
build_mov_alac_extension(const GstBuffer * codec_data)5428 build_mov_alac_extension (const GstBuffer * codec_data)
5429 {
5430 AtomInfo *alac;
5431
5432 alac = build_codec_data_extension (FOURCC_alac, codec_data);
5433
5434 return build_mov_wave_extension (FOURCC_alac, NULL, alac, TRUE);
5435 }
5436
5437 AtomInfo *
build_jp2x_extension(const GstBuffer * prefix)5438 build_jp2x_extension (const GstBuffer * prefix)
5439 {
5440 AtomData *atom_data;
5441
5442 if (!prefix) {
5443 return NULL;
5444 }
5445
5446 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2x, prefix);
5447
5448 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5449 atom_data_free);
5450 }
5451
5452 AtomInfo *
build_jp2h_extension(gint width,gint height,const gchar * colorspace,gint ncomp,const GValue * cmap_array,const GValue * cdef_array)5453 build_jp2h_extension (gint width, gint height, const gchar * colorspace,
5454 gint ncomp, const GValue * cmap_array, const GValue * cdef_array)
5455 {
5456 AtomData *atom_data;
5457 GstBuffer *buf;
5458 guint8 cenum;
5459 gint i;
5460 gint idhr_size = 22;
5461 gint colr_size = 15;
5462 gint cmap_size = 0, cdef_size = 0;
5463 gint cmap_array_size = 0;
5464 gint cdef_array_size = 0;
5465 GstByteWriter writer;
5466
5467 g_return_val_if_fail (cmap_array == NULL ||
5468 GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
5469 g_return_val_if_fail (cdef_array == NULL ||
5470 GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
5471
5472 if (g_str_equal (colorspace, "sRGB")) {
5473 cenum = 0x10;
5474 if (ncomp == 0)
5475 ncomp = 3;
5476 } else if (g_str_equal (colorspace, "GRAY")) {
5477 cenum = 0x11;
5478 if (ncomp == 0)
5479 ncomp = 1;
5480 } else if (g_str_equal (colorspace, "sYUV")) {
5481 cenum = 0x12;
5482 if (ncomp == 0)
5483 ncomp = 3;
5484 } else
5485 return NULL;
5486
5487 if (cmap_array) {
5488 cmap_array_size = gst_value_array_get_size (cmap_array);
5489 cmap_size = 8 + cmap_array_size * 4;
5490 }
5491 if (cdef_array) {
5492 cdef_array_size = gst_value_array_get_size (cdef_array);
5493 cdef_size = 8 + 2 + cdef_array_size * 6;
5494 }
5495
5496 gst_byte_writer_init_with_size (&writer,
5497 idhr_size + colr_size + cmap_size + cdef_size, TRUE);
5498
5499 /* ihdr = image header box */
5500 gst_byte_writer_put_uint32_be_unchecked (&writer, 22);
5501 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_ihdr);
5502 gst_byte_writer_put_uint32_be_unchecked (&writer, height);
5503 gst_byte_writer_put_uint32_be_unchecked (&writer, width);
5504 gst_byte_writer_put_uint16_be_unchecked (&writer, ncomp);
5505 /* 8 bits per component, unsigned */
5506 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5507 /* compression type; reserved */
5508 gst_byte_writer_put_uint8_unchecked (&writer, 0x7);
5509 /* colour space (un)known */
5510 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5511 /* intellectual property right (box present) */
5512 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5513
5514 /* colour specification box */
5515 gst_byte_writer_put_uint32_be_unchecked (&writer, 15);
5516 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_colr);
5517
5518 /* specification method: enumerated */
5519 gst_byte_writer_put_uint8_unchecked (&writer, 0x1);
5520 /* precedence; reserved */
5521 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5522 /* approximation; reserved */
5523 gst_byte_writer_put_uint8_unchecked (&writer, 0x0);
5524 /* enumerated colourspace */
5525 gst_byte_writer_put_uint32_be_unchecked (&writer, cenum);
5526
5527 if (cmap_array) {
5528 gst_byte_writer_put_uint32_be_unchecked (&writer, cmap_size);
5529 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cmap);
5530 for (i = 0; i < cmap_array_size; i++) {
5531 const GValue *item;
5532 gint value;
5533 guint16 cmp;
5534 guint8 mtyp;
5535 guint8 pcol;
5536 item = gst_value_array_get_value (cmap_array, i);
5537 value = g_value_get_int (item);
5538
5539 /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
5540 cmp = value & 0xFFFF;
5541 mtyp = value >> 24;
5542 pcol = (value >> 16) & 0xFF;
5543
5544 if (mtyp == 1)
5545 GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
5546 "handle Pallete mapping atoms yet");
5547
5548 gst_byte_writer_put_uint16_be_unchecked (&writer, cmp);
5549 gst_byte_writer_put_uint8_unchecked (&writer, mtyp);
5550 gst_byte_writer_put_uint8_unchecked (&writer, pcol);
5551 }
5552 }
5553
5554 if (cdef_array) {
5555 gst_byte_writer_put_uint32_be_unchecked (&writer, cdef_size);
5556 gst_byte_writer_put_uint32_le_unchecked (&writer, FOURCC_cdef);
5557 gst_byte_writer_put_uint16_be_unchecked (&writer, cdef_array_size);
5558 for (i = 0; i < cdef_array_size; i++) {
5559 const GValue *item;
5560 gint value;
5561 item = gst_value_array_get_value (cdef_array, i);
5562 value = g_value_get_int (item);
5563
5564 gst_byte_writer_put_uint16_be_unchecked (&writer, i);
5565 if (value > 0) {
5566 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5567 gst_byte_writer_put_uint16_be_unchecked (&writer, value);
5568 } else if (value < 0) {
5569 gst_byte_writer_put_uint16_be_unchecked (&writer, -value);
5570 gst_byte_writer_put_uint16_be_unchecked (&writer, 0); /* TODO what here? */
5571 } else {
5572 gst_byte_writer_put_uint16_be_unchecked (&writer, 1);
5573 gst_byte_writer_put_uint16_be_unchecked (&writer, 0);
5574 }
5575 }
5576 }
5577
5578 g_assert (gst_byte_writer_get_remaining (&writer) == 0);
5579 buf = gst_byte_writer_reset_and_get_buffer (&writer);
5580
5581 atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
5582 gst_buffer_unref (buf);
5583
5584 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5585 atom_data_free);
5586 }
5587
5588 AtomInfo *
build_codec_data_extension(guint32 fourcc,const GstBuffer * codec_data)5589 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
5590 {
5591 AtomData *data;
5592 AtomInfo *result = NULL;
5593
5594 if (codec_data) {
5595 data = atom_data_new_from_gst_buffer (fourcc, codec_data);
5596 result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
5597 atom_data_free);
5598 }
5599
5600 return result;
5601 }
5602
5603 AtomInfo *
build_amr_extension(void)5604 build_amr_extension (void)
5605 {
5606 guint8 ext[9];
5607 GstBuffer *buf;
5608 AtomInfo *res;
5609
5610 /* vendor */
5611 GST_WRITE_UINT32_LE (ext, 0);
5612 /* decoder version */
5613 GST_WRITE_UINT8 (ext + 4, 0);
5614 /* mode set (all modes) */
5615 GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
5616 /* mode change period (no restriction) */
5617 GST_WRITE_UINT8 (ext + 7, 0);
5618 /* frames per sample */
5619 GST_WRITE_UINT8 (ext + 8, 1);
5620
5621 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5622 res = build_codec_data_extension (FOURCC_damr, buf);
5623 gst_buffer_unref (buf);
5624 return res;
5625 }
5626
5627 AtomInfo *
build_h263_extension(void)5628 build_h263_extension (void)
5629 {
5630 guint8 ext[7];
5631 GstBuffer *buf;
5632 AtomInfo *res;
5633
5634 /* vendor */
5635 GST_WRITE_UINT32_LE (ext, 0);
5636 /* decoder version */
5637 GST_WRITE_UINT8 (ext + 4, 0);
5638 /* level / profile */
5639 /* FIXME ? maybe ? obtain somewhere; baseline for now */
5640 GST_WRITE_UINT8 (ext + 5, 10);
5641 GST_WRITE_UINT8 (ext + 6, 0);
5642
5643 buf = GST_BUFFER_NEW_READONLY (ext, sizeof (ext));
5644 res = build_codec_data_extension (FOURCC_d263, buf);
5645 gst_buffer_unref (buf);
5646 return res;
5647 }
5648
5649 AtomInfo *
build_gama_atom(gdouble gamma)5650 build_gama_atom (gdouble gamma)
5651 {
5652 AtomInfo *res;
5653 guint32 gamma_fp;
5654 GstBuffer *buf;
5655
5656 /* convert to uint32 from fixed point */
5657 gamma_fp = (guint32) 65536 *gamma;
5658
5659 gamma_fp = GUINT32_TO_BE (gamma_fp);
5660 buf = GST_BUFFER_NEW_READONLY (&gamma_fp, 4);
5661 res = build_codec_data_extension (FOURCC_gama, buf);
5662 gst_buffer_unref (buf);
5663 return res;
5664 }
5665
5666 AtomInfo *
build_SMI_atom(const GstBuffer * seqh)5667 build_SMI_atom (const GstBuffer * seqh)
5668 {
5669 AtomInfo *res;
5670 GstBuffer *buf;
5671 gsize size;
5672 guint8 *data;
5673
5674 /* the seqh plus its size and fourcc */
5675 size = gst_buffer_get_size ((GstBuffer *) seqh);
5676 data = g_malloc (size + 8);
5677
5678 GST_WRITE_UINT32_LE (data, FOURCC_SEQH);
5679 GST_WRITE_UINT32_BE (data + 4, size + 8);
5680 gst_buffer_extract ((GstBuffer *) seqh, 0, data + 8, size);
5681 buf = gst_buffer_new_wrapped (data, size + 8);
5682 res = build_codec_data_extension (FOURCC_SMI_, buf);
5683 gst_buffer_unref (buf);
5684 return res;
5685 }
5686
5687 static AtomInfo *
build_ima_adpcm_atom(gint channels,gint rate,gint blocksize)5688 build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
5689 {
5690 #define IMA_ADPCM_ATOM_SIZE 20
5691 AtomData *atom_data;
5692 guint8 *data;
5693 guint32 fourcc;
5694 gint samplesperblock;
5695 gint bytespersec;
5696
5697 /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
5698 identifier. Note that the identifier here is big-endian, but when used
5699 within the WAVE header (below), it's little endian. */
5700 fourcc = MS_WAVE_FOURCC (0x11);
5701
5702 atom_data = atom_data_new (fourcc);
5703 atom_data_alloc_mem (atom_data, IMA_ADPCM_ATOM_SIZE);
5704 data = atom_data->data;
5705
5706 /* This atom's content is a WAVE header, including 2 bytes of extra data.
5707 Note that all of this is little-endian, unlike most stuff in qt. */
5708 /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
5709 for the rest. Simplifies to this. */
5710 samplesperblock = 2 * blocksize / channels - 7;
5711 bytespersec = rate * blocksize / samplesperblock;
5712 GST_WRITE_UINT16_LE (data, 0x11);
5713 GST_WRITE_UINT16_LE (data + 2, channels);
5714 GST_WRITE_UINT32_LE (data + 4, rate);
5715 GST_WRITE_UINT32_LE (data + 8, bytespersec);
5716 GST_WRITE_UINT16_LE (data + 12, blocksize);
5717 GST_WRITE_UINT16_LE (data + 14, 4);
5718 GST_WRITE_UINT16_LE (data + 16, 2); /* Two extra bytes */
5719 GST_WRITE_UINT16_LE (data + 18, samplesperblock);
5720
5721 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5722 atom_data_free);
5723 }
5724
5725 AtomInfo *
build_ima_adpcm_extension(gint channels,gint rate,gint blocksize)5726 build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
5727 {
5728 AtomWAVE *wave;
5729 AtomFRMA *frma;
5730 Atom *ext_atom;
5731
5732 /* Add WAVE atom */
5733 wave = atom_wave_new ();
5734
5735 /* Prepend Terminator atom to the WAVE list first, so it ends up last */
5736 ext_atom = (Atom *) atom_data_new (FOURCC_null);
5737 wave->extension_atoms =
5738 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
5739 (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
5740
5741 /* Add wave ima adpcm atom to WAVE */
5742 wave->extension_atoms = g_list_prepend (wave->extension_atoms,
5743 build_ima_adpcm_atom (channels, rate, blocksize));
5744
5745 /* Add FRMA to the WAVE */
5746 frma = atom_frma_new ();
5747 frma->media_type = MS_WAVE_FOURCC (0x11);
5748
5749 wave->extension_atoms =
5750 atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
5751 (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
5752
5753 return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
5754 atom_wave_free);
5755 }
5756
5757 AtomInfo *
build_ac3_extension(guint8 fscod,guint8 bsid,guint8 bsmod,guint8 acmod,guint8 lfe_on,guint8 bitrate_code)5758 build_ac3_extension (guint8 fscod, guint8 bsid, guint8 bsmod, guint8 acmod,
5759 guint8 lfe_on, guint8 bitrate_code)
5760 {
5761 AtomData *atom_data = atom_data_new (FOURCC_dac3);
5762 guint8 *data;
5763
5764 atom_data_alloc_mem (atom_data, 3);
5765 data = atom_data->data;
5766
5767 /* Bits from the spec
5768 * fscod 2
5769 * bsid 5
5770 * bsmod 3
5771 * acmod 3
5772 * lfeon 1
5773 * bit_rate_code 5
5774 * reserved 5
5775 */
5776
5777 /* Some bit manipulation magic. Need bitwriter */
5778 data[0] = (fscod << 6) | (bsid << 1) | ((bsmod >> 2) & 1);
5779 data[1] =
5780 ((bsmod & 0x3) << 6) | (acmod << 3) | ((lfe_on & 1) << 2) | ((bitrate_code
5781 >> 3) & 0x3);
5782 data[2] = ((bitrate_code & 0x7) << 5);
5783
5784 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5785 atom_data_free);
5786 }
5787
5788 AtomInfo *
build_opus_extension(guint32 rate,guint8 channels,guint8 mapping_family,guint8 stream_count,guint8 coupled_count,guint8 channel_mapping[256],guint16 pre_skip,guint16 output_gain)5789 build_opus_extension (guint32 rate, guint8 channels, guint8 mapping_family,
5790 guint8 stream_count, guint8 coupled_count, guint8 channel_mapping[256],
5791 guint16 pre_skip, guint16 output_gain)
5792 {
5793 AtomData *atom_data;
5794 guint8 *data_block;
5795 GstByteWriter bw;
5796 gboolean hdl = TRUE;
5797 guint data_block_len;
5798
5799 gst_byte_writer_init (&bw);
5800 hdl &= gst_byte_writer_put_uint8 (&bw, 0x00); /* version number */
5801 hdl &= gst_byte_writer_put_uint8 (&bw, channels);
5802 hdl &= gst_byte_writer_put_uint16_le (&bw, pre_skip);
5803 hdl &= gst_byte_writer_put_uint32_le (&bw, rate);
5804 hdl &= gst_byte_writer_put_uint16_le (&bw, output_gain);
5805 hdl &= gst_byte_writer_put_uint8 (&bw, mapping_family);
5806 if (mapping_family > 0) {
5807 hdl &= gst_byte_writer_put_uint8 (&bw, stream_count);
5808 hdl &= gst_byte_writer_put_uint8 (&bw, coupled_count);
5809 hdl &= gst_byte_writer_put_data (&bw, channel_mapping, channels);
5810 }
5811
5812 if (!hdl) {
5813 GST_WARNING ("Error creating header");
5814 return NULL;
5815 }
5816
5817 data_block_len = gst_byte_writer_get_size (&bw);
5818 data_block = gst_byte_writer_reset_and_get_data (&bw);
5819 atom_data = atom_data_new_from_data (FOURCC_dops, data_block, data_block_len);
5820 g_free (data_block);
5821
5822 return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
5823 atom_data_free);
5824 }
5825
5826 AtomInfo *
build_uuid_xmp_atom(GstBuffer * xmp_data)5827 build_uuid_xmp_atom (GstBuffer * xmp_data)
5828 {
5829 AtomUUID *uuid;
5830 gsize size;
5831 static const guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
5832 0x97, 0xA9, 0x42, 0xE8,
5833 0x9C, 0x71, 0x99, 0x94,
5834 0x91, 0xE3, 0xAF, 0xAC
5835 };
5836
5837 if (xmp_data == NULL)
5838 return NULL;
5839
5840 uuid = atom_uuid_new ();
5841 memcpy (uuid->uuid, xmp_uuid, 16);
5842
5843 size = gst_buffer_get_size (xmp_data);
5844 uuid->data = g_malloc (size);
5845 uuid->datalen = size;
5846 gst_buffer_extract (xmp_data, 0, uuid->data, size);
5847
5848 return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,
5849 atom_uuid_free);
5850 }
5851