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