Branch data Line data Source code
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., 59 Temple Place - Suite 330,
18 : : * Boston, MA 02111-1307, 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 : :
52 : : /**
53 : : * Creates a new AtomsContext for the given flavor.
54 : : */
55 : : AtomsContext *
56 : 36 : atoms_context_new (AtomsTreeFlavor flavor)
57 : : {
58 : 36 : AtomsContext *context = g_new0 (AtomsContext, 1);
59 : 36 : context->flavor = flavor;
60 : 36 : return context;
61 : : }
62 : :
63 : : /**
64 : : * Frees an AtomsContext and all memory associated with it
65 : : */
66 : : void
67 : 36 : atoms_context_free (AtomsContext * context)
68 : : {
69 : 36 : g_free (context);
70 : 36 : }
71 : :
72 : : /* -- creation, initialization, clear and free functions -- */
73 : :
74 : : #define SECS_PER_DAY (24 * 60 * 60)
75 : : #define LEAP_YEARS_FROM_1904_TO_1970 17
76 : :
77 : : static guint64
78 : 154 : get_current_qt_time (void)
79 : : {
80 : : GTimeVal timeval;
81 : :
82 : 154 : g_get_current_time (&timeval);
83 : : /* FIXME this should use UTC coordinated time */
84 : 154 : return timeval.tv_sec + (((1970 - 1904) * (guint64) 365) +
85 : : LEAP_YEARS_FROM_1904_TO_1970) * SECS_PER_DAY;
86 : : }
87 : :
88 : : static void
89 : 121 : common_time_info_init (TimeInfo * ti)
90 : : {
91 : 121 : ti->creation_time = ti->modification_time = get_current_qt_time ();
92 : 121 : ti->timescale = 0;
93 : 121 : ti->duration = 0;
94 : 121 : }
95 : :
96 : : static void
97 : 1085 : atom_header_set (Atom * header, guint32 fourcc, gint32 size, gint64 ext_size)
98 : : {
99 : 1085 : header->type = fourcc;
100 : 1085 : header->size = size;
101 : 1085 : header->extended_size = ext_size;
102 : 1085 : }
103 : :
104 : : static void
105 : 979 : atom_clear (Atom * atom)
106 : : {
107 : 979 : }
108 : :
109 : : static void
110 : 665 : atom_full_init (AtomFull * full, guint32 fourcc, gint32 size, gint64 ext_size,
111 : : guint8 version, guint8 flags[3])
112 : : {
113 : 665 : atom_header_set (&(full->header), fourcc, size, ext_size);
114 : 665 : full->version = version;
115 : 665 : full->flags[0] = flags[0];
116 : 665 : full->flags[1] = flags[1];
117 : 665 : full->flags[2] = flags[2];
118 : 665 : }
119 : :
120 : : static void
121 : 563 : atom_full_clear (AtomFull * full)
122 : : {
123 : 563 : atom_clear (&full->header);
124 : 563 : }
125 : :
126 : : static void
127 : 21 : atom_full_free (AtomFull * full)
128 : : {
129 : 21 : atom_full_clear (full);
130 : 21 : g_free (full);
131 : 21 : }
132 : :
133 : : static guint32
134 : 8 : atom_full_get_flags_as_uint (AtomFull * full)
135 : : {
136 : 8 : return full->flags[0] << 16 | full->flags[1] << 8 | full->flags[2];
137 : : }
138 : :
139 : : static void
140 : 12 : atom_full_set_flags_as_uint (AtomFull * full, guint32 flags_as_uint)
141 : : {
142 : 12 : full->flags[2] = flags_as_uint & 0xFF;
143 : 12 : full->flags[1] = (flags_as_uint & 0xFF00) >> 8;
144 : 12 : full->flags[0] = (flags_as_uint & 0xFF0000) >> 16;
145 : 12 : }
146 : :
147 : : static AtomInfo *
148 : 35 : build_atom_info_wrapper (Atom * atom, gpointer copy_func, gpointer free_func)
149 : : {
150 : 35 : AtomInfo *info = NULL;
151 : :
152 [ + - ]: 35 : if (atom) {
153 : 35 : info = g_new0 (AtomInfo, 1);
154 : :
155 : 35 : info->atom = atom;
156 : 35 : info->copy_data_func = copy_func;
157 : 35 : info->free_func = free_func;
158 : : }
159 : :
160 : 35 : return info;
161 : : }
162 : :
163 : : static GList *
164 : 0 : atom_info_list_prepend_atom (GList * ai, Atom * atom,
165 : : AtomCopyDataFunc copy_func, AtomFreeFunc free_func)
166 : : {
167 [ # # ]: 0 : if (atom)
168 : 0 : return g_list_prepend (ai,
169 : 0 : build_atom_info_wrapper (atom, copy_func, free_func));
170 : : else
171 : 0 : return ai;
172 : : }
173 : :
174 : : static void
175 : 23 : atom_info_list_free (GList * ai)
176 : : {
177 [ + + ]: 52 : while (ai) {
178 : 29 : AtomInfo *info = (AtomInfo *) ai->data;
179 : :
180 : 29 : info->free_func (info->atom);
181 : 29 : g_free (info);
182 : 29 : ai = g_list_delete_link (ai, ai);
183 : : }
184 : 23 : }
185 : :
186 : : static AtomData *
187 : 17 : atom_data_new (guint32 fourcc)
188 : : {
189 : 17 : AtomData *data = g_new0 (AtomData, 1);
190 : :
191 : 17 : atom_header_set (&data->header, fourcc, 0, 0);
192 : 17 : return data;
193 : : }
194 : :
195 : : static void
196 : 17 : atom_data_alloc_mem (AtomData * data, guint32 size)
197 : : {
198 [ - + ]: 17 : if (data->data) {
199 : 0 : g_free (data->data);
200 : : }
201 : 17 : data->data = g_new0 (guint8, size);
202 : 17 : data->datalen = size;
203 : 17 : }
204 : :
205 : : static AtomData *
206 : 17 : atom_data_new_from_gst_buffer (guint32 fourcc, const GstBuffer * buf)
207 : : {
208 : 17 : AtomData *data = atom_data_new (fourcc);
209 : :
210 : 17 : atom_data_alloc_mem (data, GST_BUFFER_SIZE (buf));
211 : 17 : g_memmove (data->data, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
212 : 17 : return data;
213 : : }
214 : :
215 : : static void
216 : 17 : atom_data_free (AtomData * data)
217 : : {
218 : 17 : atom_clear (&data->header);
219 : 17 : g_free (data->data);
220 : 17 : g_free (data);
221 : 17 : }
222 : :
223 : : static AtomUUID *
224 : 6 : atom_uuid_new (void)
225 : : {
226 : 6 : AtomUUID *uuid = g_new0 (AtomUUID, 1);
227 : :
228 : 6 : atom_header_set (&uuid->header, FOURCC_uuid, 0, 0);
229 : 6 : return uuid;
230 : : }
231 : :
232 : : static void
233 : 6 : atom_uuid_free (AtomUUID * data)
234 : : {
235 : 6 : atom_clear (&data->header);
236 : 6 : g_free (data->data);
237 : 6 : g_free (data);
238 : 6 : }
239 : :
240 : : static void
241 : 16 : atom_ftyp_init (AtomFTYP * ftyp, guint32 major, guint32 version, GList * brands)
242 : : {
243 : : gint index;
244 : 16 : GList *it = NULL;
245 : :
246 : 16 : atom_header_set (&ftyp->header, FOURCC_ftyp, 16, 0);
247 : 16 : ftyp->major_brand = major;
248 : 16 : ftyp->version = version;
249 : :
250 : : /* always include major brand as compatible brand */
251 : 16 : ftyp->compatible_brands_size = g_list_length (brands) + 1;
252 : 16 : ftyp->compatible_brands = g_new (guint32, ftyp->compatible_brands_size);
253 : :
254 : 16 : ftyp->compatible_brands[0] = major;
255 : 16 : index = 1;
256 [ + - ][ + + ]: 31 : for (it = brands; it != NULL; it = g_list_next (it)) {
257 : 15 : ftyp->compatible_brands[index++] = GPOINTER_TO_UINT (it->data);
258 : : }
259 : 16 : }
260 : :
261 : : AtomFTYP *
262 : 16 : atom_ftyp_new (AtomsContext * context, guint32 major, guint32 version,
263 : : GList * brands)
264 : : {
265 : 16 : AtomFTYP *ftyp = g_new0 (AtomFTYP, 1);
266 : :
267 : 16 : atom_ftyp_init (ftyp, major, version, brands);
268 : 16 : return ftyp;
269 : : }
270 : :
271 : : void
272 : 16 : atom_ftyp_free (AtomFTYP * ftyp)
273 : : {
274 : 16 : atom_clear (&ftyp->header);
275 : 16 : g_free (ftyp->compatible_brands);
276 : 16 : ftyp->compatible_brands = NULL;
277 : 16 : g_free (ftyp);
278 : 16 : }
279 : :
280 : : static void
281 : 4 : atom_esds_init (AtomESDS * esds)
282 : : {
283 : 4 : guint8 flags[3] = { 0, 0, 0 };
284 : :
285 : 4 : atom_full_init (&esds->header, FOURCC_esds, 0, 0, 0, flags);
286 : 4 : desc_es_init (&esds->es);
287 : 4 : }
288 : :
289 : : static AtomESDS *
290 : 4 : atom_esds_new (void)
291 : : {
292 : 4 : AtomESDS *esds = g_new0 (AtomESDS, 1);
293 : :
294 : 4 : atom_esds_init (esds);
295 : 4 : return esds;
296 : : }
297 : :
298 : : static void
299 : 4 : atom_esds_free (AtomESDS * esds)
300 : : {
301 : 4 : atom_full_clear (&esds->header);
302 : 4 : desc_es_descriptor_clear (&esds->es);
303 : 4 : g_free (esds);
304 : 4 : }
305 : :
306 : : static AtomFRMA *
307 : 0 : atom_frma_new (void)
308 : : {
309 : 0 : AtomFRMA *frma = g_new0 (AtomFRMA, 1);
310 : :
311 : 0 : atom_header_set (&frma->header, FOURCC_frma, 0, 0);
312 : 0 : return frma;
313 : : }
314 : :
315 : : static void
316 : 0 : atom_frma_free (AtomFRMA * frma)
317 : : {
318 : 0 : atom_clear (&frma->header);
319 : 0 : g_free (frma);
320 : 0 : }
321 : :
322 : : static AtomWAVE *
323 : 0 : atom_wave_new (void)
324 : : {
325 : 0 : AtomWAVE *wave = g_new0 (AtomWAVE, 1);
326 : :
327 : 0 : atom_header_set (&wave->header, FOURCC_wave, 0, 0);
328 : 0 : return wave;
329 : : }
330 : :
331 : : static void
332 : 0 : atom_wave_free (AtomWAVE * wave)
333 : : {
334 : 0 : atom_clear (&wave->header);
335 : 0 : atom_info_list_free (wave->extension_atoms);
336 : 0 : g_free (wave);
337 : 0 : }
338 : :
339 : : static void
340 : 0 : atom_elst_init (AtomELST * elst)
341 : : {
342 : 0 : guint8 flags[3] = { 0, 0, 0 };
343 : 0 : atom_full_init (&elst->header, FOURCC_elst, 0, 0, 0, flags);
344 : 0 : elst->entries = 0;
345 : 0 : }
346 : :
347 : : static void
348 : 0 : atom_elst_clear (AtomELST * elst)
349 : : {
350 : : GSList *walker;
351 : :
352 : 0 : atom_full_clear (&elst->header);
353 : 0 : walker = elst->entries;
354 [ # # ]: 0 : while (walker) {
355 : 0 : g_free ((EditListEntry *) walker->data);
356 [ # # ]: 0 : walker = g_slist_next (walker);
357 : : }
358 : 0 : g_slist_free (elst->entries);
359 : 0 : }
360 : :
361 : : static void
362 : 0 : atom_edts_init (AtomEDTS * edts)
363 : : {
364 : 0 : atom_header_set (&edts->header, FOURCC_edts, 0, 0);
365 : 0 : atom_elst_init (&edts->elst);
366 : 0 : }
367 : :
368 : : static void
369 : 0 : atom_edts_clear (AtomEDTS * edts)
370 : : {
371 : 0 : atom_clear (&edts->header);
372 : 0 : atom_elst_clear (&edts->elst);
373 : 0 : }
374 : :
375 : : static AtomEDTS *
376 : 0 : atom_edts_new (void)
377 : : {
378 : 0 : AtomEDTS *edts = g_new0 (AtomEDTS, 1);
379 : 0 : atom_edts_init (edts);
380 : 0 : return edts;
381 : : }
382 : :
383 : : static void
384 : 0 : atom_edts_free (AtomEDTS * edts)
385 : : {
386 : 0 : atom_edts_clear (edts);
387 : 0 : g_free (edts);
388 : 0 : }
389 : :
390 : : static void
391 : 16 : atom_sample_entry_init (SampleTableEntry * se, guint32 type)
392 : : {
393 : 16 : atom_header_set (&se->header, type, 0, 0);
394 : :
395 : 16 : memset (se->reserved, 0, sizeof (guint8) * 6);
396 : 16 : se->data_reference_index = 0;
397 : 16 : }
398 : :
399 : : static void
400 : 16 : atom_sample_entry_free (SampleTableEntry * se)
401 : : {
402 : 16 : atom_clear (&se->header);
403 : 16 : }
404 : :
405 : : static void
406 : 3 : sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
407 : : {
408 : 3 : atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
409 : :
410 : 3 : mp4a->version = 0;
411 : 3 : mp4a->revision_level = 0;
412 : 3 : mp4a->vendor = 0;
413 : 3 : mp4a->channels = 2;
414 : 3 : mp4a->sample_size = 16;
415 : 3 : mp4a->compression_id = 0;
416 : 3 : mp4a->packet_size = 0;
417 : 3 : mp4a->sample_rate = 0;
418 : : /* following only used if version is 1 */
419 : 3 : mp4a->samples_per_packet = 0;
420 : 3 : mp4a->bytes_per_packet = 0;
421 : 3 : mp4a->bytes_per_frame = 0;
422 : 3 : mp4a->bytes_per_sample = 0;
423 : :
424 : 3 : mp4a->extension_atoms = NULL;
425 : 3 : }
426 : :
427 : : static SampleTableEntryMP4A *
428 : 3 : sample_entry_mp4a_new (void)
429 : : {
430 : 3 : SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
431 : :
432 : 3 : sample_entry_mp4a_init (mp4a);
433 : 3 : return mp4a;
434 : : }
435 : :
436 : : static void
437 : 3 : sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
438 : : {
439 : 3 : atom_sample_entry_free (&mp4a->se);
440 : 3 : atom_info_list_free (mp4a->extension_atoms);
441 : 3 : g_free (mp4a);
442 : 3 : }
443 : :
444 : : static void
445 : 13 : sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
446 : : {
447 : 13 : atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
448 : :
449 : 13 : mp4v->version = 0;
450 : 13 : mp4v->revision_level = 0;
451 : 13 : mp4v->vendor = 0;
452 : :
453 : 13 : mp4v->temporal_quality = 0;
454 : 13 : mp4v->spatial_quality = 0;
455 : :
456 : : /* qt and ISO base media do not contradict, and examples agree */
457 : 13 : mp4v->horizontal_resolution = 0x00480000;
458 : 13 : mp4v->vertical_resolution = 0x00480000;
459 : :
460 : 13 : mp4v->datasize = 0;
461 : 13 : mp4v->frame_count = 1;
462 : :
463 : 13 : memset (mp4v->compressor, 0, sizeof (guint8) * 32);
464 : :
465 : 13 : mp4v->depth = 0;
466 : 13 : mp4v->color_table_id = 0;
467 : :
468 : 13 : mp4v->extension_atoms = NULL;
469 : 13 : }
470 : :
471 : : static void
472 : 13 : sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
473 : : {
474 : 13 : atom_sample_entry_free (&mp4v->se);
475 : 13 : atom_info_list_free (mp4v->extension_atoms);
476 : 13 : g_free (mp4v);
477 : 13 : }
478 : :
479 : : static SampleTableEntryMP4V *
480 : 13 : sample_entry_mp4v_new (AtomsContext * context)
481 : : {
482 : 13 : SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
483 : :
484 : 13 : sample_entry_mp4v_init (mp4v, context);
485 : 13 : return mp4v;
486 : : }
487 : :
488 : : static void
489 : 33 : atom_stsd_init (AtomSTSD * stsd)
490 : : {
491 : 33 : guint8 flags[3] = { 0, 0, 0 };
492 : :
493 : 33 : atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
494 : 33 : stsd->entries = NULL;
495 : 33 : stsd->n_entries = 0;
496 : 33 : }
497 : :
498 : : static void
499 : 49 : atom_stsd_remove_entries (AtomSTSD * stsd)
500 : : {
501 : : GList *walker;
502 : :
503 : 49 : walker = stsd->entries;
504 [ + + ]: 65 : while (walker) {
505 : 16 : GList *aux = walker;
506 : 16 : SampleTableEntry *se = (SampleTableEntry *) aux->data;
507 : :
508 [ + - ]: 16 : walker = g_list_next (walker);
509 : 16 : stsd->entries = g_list_remove_link (stsd->entries, aux);
510 : :
511 [ + + - ]: 16 : switch (se->kind) {
512 : : case AUDIO:
513 : 3 : sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
514 : 3 : break;
515 : : case VIDEO:
516 : 13 : sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
517 : 13 : break;
518 : : default:
519 : : /* best possible cleanup */
520 : 0 : atom_sample_entry_free (se);
521 : : }
522 : 16 : g_list_free (aux);
523 : : }
524 : 49 : stsd->n_entries = 0;
525 : 49 : }
526 : :
527 : : static void
528 : 33 : atom_stsd_clear (AtomSTSD * stsd)
529 : : {
530 : 33 : atom_stsd_remove_entries (stsd);
531 : 33 : atom_full_clear (&stsd->header);
532 : 33 : }
533 : :
534 : : static void
535 : 12 : atom_ctts_init (AtomCTTS * ctts)
536 : : {
537 : 12 : guint8 flags[3] = { 0, 0, 0 };
538 : :
539 : 12 : atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
540 : 12 : atom_array_init (&ctts->entries, 128);
541 : 12 : ctts->do_pts = FALSE;
542 : 12 : }
543 : :
544 : : static AtomCTTS *
545 : 12 : atom_ctts_new (void)
546 : : {
547 : 12 : AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
548 : :
549 : 12 : atom_ctts_init (ctts);
550 : 12 : return ctts;
551 : : }
552 : :
553 : : static void
554 : 12 : atom_ctts_free (AtomCTTS * ctts)
555 : : {
556 : 12 : atom_full_clear (&ctts->header);
557 : 12 : atom_array_clear (&ctts->entries);
558 : 12 : g_free (ctts);
559 : 12 : }
560 : :
561 : : static void
562 : 33 : atom_stts_init (AtomSTTS * stts)
563 : : {
564 : 33 : guint8 flags[3] = { 0, 0, 0 };
565 : :
566 : 33 : atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
567 : 33 : atom_array_init (&stts->entries, 512);
568 : 33 : }
569 : :
570 : : static void
571 : 33 : atom_stts_clear (AtomSTTS * stts)
572 : : {
573 : 33 : atom_full_clear (&stts->header);
574 : 33 : atom_array_clear (&stts->entries);
575 : 33 : }
576 : :
577 : : static void
578 : 33 : atom_stsz_init (AtomSTSZ * stsz)
579 : : {
580 : 33 : guint8 flags[3] = { 0, 0, 0 };
581 : :
582 : 33 : atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
583 : 33 : atom_array_init (&stsz->entries, 1024);
584 : 33 : stsz->sample_size = 0;
585 : 33 : stsz->table_size = 0;
586 : 33 : }
587 : :
588 : : static void
589 : 33 : atom_stsz_clear (AtomSTSZ * stsz)
590 : : {
591 : 33 : atom_full_clear (&stsz->header);
592 : 33 : atom_array_clear (&stsz->entries);
593 : 33 : stsz->table_size = 0;
594 : 33 : }
595 : :
596 : : static void
597 : 33 : atom_stsc_init (AtomSTSC * stsc)
598 : : {
599 : 33 : guint8 flags[3] = { 0, 0, 0 };
600 : :
601 : 33 : atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
602 : 33 : atom_array_init (&stsc->entries, 128);
603 : 33 : }
604 : :
605 : : static void
606 : 33 : atom_stsc_clear (AtomSTSC * stsc)
607 : : {
608 : 33 : atom_full_clear (&stsc->header);
609 : 33 : atom_array_clear (&stsc->entries);
610 : 33 : }
611 : :
612 : : static void
613 : 33 : atom_co64_init (AtomSTCO64 * co64)
614 : : {
615 : 33 : guint8 flags[3] = { 0, 0, 0 };
616 : :
617 : 33 : atom_full_init (&co64->header, FOURCC_stco, 0, 0, 0, flags);
618 : 33 : atom_array_init (&co64->entries, 256);
619 : 33 : }
620 : :
621 : : static void
622 : 33 : atom_stco64_clear (AtomSTCO64 * stco64)
623 : : {
624 : 33 : atom_full_clear (&stco64->header);
625 : 33 : atom_array_clear (&stco64->entries);
626 : 33 : }
627 : :
628 : : static void
629 : 33 : atom_stss_init (AtomSTSS * stss)
630 : : {
631 : 33 : guint8 flags[3] = { 0, 0, 0 };
632 : :
633 : 33 : atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
634 : 33 : atom_array_init (&stss->entries, 128);
635 : 33 : }
636 : :
637 : : static void
638 : 33 : atom_stss_clear (AtomSTSS * stss)
639 : : {
640 : 33 : atom_full_clear (&stss->header);
641 : 33 : atom_array_clear (&stss->entries);
642 : 33 : }
643 : :
644 : : void
645 : 33 : atom_stbl_init (AtomSTBL * stbl)
646 : : {
647 : 33 : atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
648 : :
649 : 33 : atom_stts_init (&stbl->stts);
650 : 33 : atom_stss_init (&stbl->stss);
651 : 33 : atom_stsd_init (&stbl->stsd);
652 : 33 : atom_stsz_init (&stbl->stsz);
653 : 33 : atom_stsc_init (&stbl->stsc);
654 : 33 : stbl->ctts = NULL;
655 : :
656 : 33 : atom_co64_init (&stbl->stco64);
657 : 33 : }
658 : :
659 : : void
660 : 33 : atom_stbl_clear (AtomSTBL * stbl)
661 : : {
662 : 33 : atom_clear (&stbl->header);
663 : 33 : atom_stsd_clear (&stbl->stsd);
664 : 33 : atom_stts_clear (&stbl->stts);
665 : 33 : atom_stss_clear (&stbl->stss);
666 : 33 : atom_stsc_clear (&stbl->stsc);
667 : 33 : atom_stsz_clear (&stbl->stsz);
668 [ + + ]: 33 : if (stbl->ctts) {
669 : 12 : atom_ctts_free (stbl->ctts);
670 : : }
671 : 33 : atom_stco64_clear (&stbl->stco64);
672 : 33 : }
673 : :
674 : : static void
675 : 13 : atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
676 : : {
677 : 13 : guint8 flags[3] = { 0, 0, 1 };
678 : :
679 : 13 : atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
680 : 13 : vmhd->graphics_mode = 0x0;
681 : 13 : memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
682 : :
683 [ + + ]: 13 : if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
684 : 7 : vmhd->graphics_mode = 0x40;
685 : 7 : vmhd->opcolor[0] = 32768;
686 : 7 : vmhd->opcolor[1] = 32768;
687 : 7 : vmhd->opcolor[2] = 32768;
688 : : }
689 : 13 : }
690 : :
691 : : static AtomVMHD *
692 : 13 : atom_vmhd_new (AtomsContext * context)
693 : : {
694 : 13 : AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
695 : :
696 : 13 : atom_vmhd_init (vmhd, context);
697 : 13 : return vmhd;
698 : : }
699 : :
700 : : static void
701 : 13 : atom_vmhd_free (AtomVMHD * vmhd)
702 : : {
703 : 13 : atom_full_clear (&vmhd->header);
704 : 13 : g_free (vmhd);
705 : 13 : }
706 : :
707 : : static void
708 : 3 : atom_smhd_init (AtomSMHD * smhd)
709 : : {
710 : 3 : guint8 flags[3] = { 0, 0, 0 };
711 : :
712 : 3 : atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
713 : 3 : smhd->balance = 0;
714 : 3 : smhd->reserved = 0;
715 : 3 : }
716 : :
717 : : static AtomSMHD *
718 : 3 : atom_smhd_new (void)
719 : : {
720 : 3 : AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
721 : :
722 : 3 : atom_smhd_init (smhd);
723 : 3 : return smhd;
724 : : }
725 : :
726 : : static void
727 : 3 : atom_smhd_free (AtomSMHD * smhd)
728 : : {
729 : 3 : atom_full_clear (&smhd->header);
730 : 3 : g_free (smhd);
731 : 3 : }
732 : :
733 : : static void
734 : 0 : atom_hmhd_free (AtomHMHD * hmhd)
735 : : {
736 : 0 : atom_full_clear (&hmhd->header);
737 : 0 : g_free (hmhd);
738 : 0 : }
739 : :
740 : : static void
741 : 58 : atom_hdlr_init (AtomHDLR * hdlr)
742 : : {
743 : 58 : guint8 flags[3] = { 0, 0, 0 };
744 : :
745 : 58 : atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
746 : :
747 : 58 : hdlr->component_type = 0;
748 : 58 : hdlr->handler_type = 0;
749 : 58 : hdlr->manufacturer = 0;
750 : 58 : hdlr->flags = 0;
751 : 58 : hdlr->flags_mask = 0;
752 : 58 : hdlr->name = g_strdup ("");
753 : 58 : }
754 : :
755 : : static AtomHDLR *
756 : 21 : atom_hdlr_new (void)
757 : : {
758 : 21 : AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
759 : :
760 : 21 : atom_hdlr_init (hdlr);
761 : 21 : return hdlr;
762 : : }
763 : :
764 : : static void
765 : 58 : atom_hdlr_clear (AtomHDLR * hdlr)
766 : : {
767 : 58 : atom_full_clear (&hdlr->header);
768 [ + - ]: 58 : if (hdlr->name) {
769 : 58 : g_free (hdlr->name);
770 : 58 : hdlr->name = NULL;
771 : : }
772 : 58 : }
773 : :
774 : : static void
775 : 21 : atom_hdlr_free (AtomHDLR * hdlr)
776 : : {
777 : 21 : atom_hdlr_clear (hdlr);
778 : 21 : g_free (hdlr);
779 : 21 : }
780 : :
781 : : static void
782 : 12 : atom_url_init (AtomURL * url)
783 : : {
784 : 12 : guint8 flags[3] = { 0, 0, 1 };
785 : :
786 : 12 : atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
787 : 12 : url->location = NULL;
788 : 12 : }
789 : :
790 : : static void
791 : 12 : atom_url_free (AtomURL * url)
792 : : {
793 : 12 : atom_full_clear (&url->header);
794 [ - + ]: 12 : if (url->location) {
795 : 0 : g_free (url->location);
796 : 0 : url->location = NULL;
797 : : }
798 : 12 : g_free (url);
799 : 12 : }
800 : :
801 : : static AtomURL *
802 : 12 : atom_url_new (void)
803 : : {
804 : 12 : AtomURL *url = g_new0 (AtomURL, 1);
805 : :
806 : 12 : atom_url_init (url);
807 : 12 : return url;
808 : : }
809 : :
810 : : static AtomFull *
811 : 21 : atom_alis_new (void)
812 : : {
813 : 21 : guint8 flags[3] = { 0, 0, 1 };
814 : 21 : AtomFull *alis = g_new0 (AtomFull, 1);
815 : :
816 : 21 : atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
817 : 21 : return alis;
818 : : }
819 : :
820 : : static void
821 : 33 : atom_dref_init (AtomDREF * dref, AtomsContext * context)
822 : : {
823 : 33 : guint8 flags[3] = { 0, 0, 0 };
824 : :
825 : 33 : atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
826 : :
827 : : /* in either case, alis or url init arranges to set self-contained flag */
828 [ + + ]: 33 : if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
829 : : /* alis dref for qt */
830 : 21 : AtomFull *alis = atom_alis_new ();
831 : 21 : dref->entries = g_list_append (dref->entries, alis);
832 : : } else {
833 : : /* url for iso spec, as 'alis' not specified there */
834 : 12 : AtomURL *url = atom_url_new ();
835 : 12 : dref->entries = g_list_append (dref->entries, url);
836 : : }
837 : 33 : }
838 : :
839 : : static void
840 : 33 : atom_dref_clear (AtomDREF * dref)
841 : : {
842 : : GList *walker;
843 : :
844 : 33 : atom_full_clear (&dref->header);
845 : 33 : walker = dref->entries;
846 [ + + ]: 66 : while (walker) {
847 : 33 : GList *aux = walker;
848 : 33 : Atom *atom = (Atom *) aux->data;
849 : :
850 [ + - ]: 33 : walker = g_list_next (walker);
851 : 33 : dref->entries = g_list_remove_link (dref->entries, aux);
852 [ + + - ]: 33 : switch (atom->type) {
853 : : case FOURCC_alis:
854 : 21 : atom_full_free ((AtomFull *) atom);
855 : 21 : break;
856 : : case FOURCC_url_:
857 : 12 : atom_url_free ((AtomURL *) atom);
858 : 12 : break;
859 : : default:
860 : : /* we do nothing, better leak than crash */
861 : 0 : break;
862 : : }
863 : 33 : g_list_free (aux);
864 : : }
865 : 33 : }
866 : :
867 : : static void
868 : 33 : atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
869 : : {
870 : 33 : atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
871 : 33 : atom_dref_init (&dinf->dref, context);
872 : 33 : }
873 : :
874 : : static void
875 : 33 : atom_dinf_clear (AtomDINF * dinf)
876 : : {
877 : 33 : atom_clear (&dinf->header);
878 : 33 : atom_dref_clear (&dinf->dref);
879 : 33 : }
880 : :
881 : : static void
882 : 33 : atom_minf_init (AtomMINF * minf, AtomsContext * context)
883 : : {
884 : 33 : atom_header_set (&minf->header, FOURCC_minf, 0, 0);
885 : :
886 : 33 : minf->vmhd = NULL;
887 : 33 : minf->smhd = NULL;
888 : 33 : minf->hmhd = NULL;
889 : :
890 [ + + ]: 33 : if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
891 : 21 : minf->hdlr = atom_hdlr_new ();
892 : 21 : minf->hdlr->component_type = FOURCC_dhlr;
893 : 21 : minf->hdlr->handler_type = FOURCC_alis;
894 : : } else {
895 : 12 : minf->hdlr = NULL;
896 : : }
897 : 33 : atom_dinf_init (&minf->dinf, context);
898 : 33 : atom_stbl_init (&minf->stbl);
899 : 33 : }
900 : :
901 : : static void
902 : 49 : atom_minf_clear_handlers (AtomMINF * minf)
903 : : {
904 [ + + ]: 49 : if (minf->vmhd) {
905 : 13 : atom_vmhd_free (minf->vmhd);
906 : 13 : minf->vmhd = NULL;
907 : : }
908 [ + + ]: 49 : if (minf->smhd) {
909 : 3 : atom_smhd_free (minf->smhd);
910 : 3 : minf->smhd = NULL;
911 : : }
912 [ - + ]: 49 : if (minf->hmhd) {
913 : 0 : atom_hmhd_free (minf->hmhd);
914 : 0 : minf->hmhd = NULL;
915 : : }
916 : 49 : }
917 : :
918 : : static void
919 : 33 : atom_minf_clear (AtomMINF * minf)
920 : : {
921 : 33 : atom_clear (&minf->header);
922 : 33 : atom_minf_clear_handlers (minf);
923 [ + + ]: 33 : if (minf->hdlr) {
924 : 21 : atom_hdlr_free (minf->hdlr);
925 : : }
926 : 33 : atom_dinf_clear (&minf->dinf);
927 : 33 : atom_stbl_clear (&minf->stbl);
928 : 33 : }
929 : :
930 : : static void
931 : 33 : atom_mdhd_init (AtomMDHD * mdhd)
932 : : {
933 : 33 : guint8 flags[3] = { 0, 0, 0 };
934 : :
935 : 33 : atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
936 : 33 : common_time_info_init (&mdhd->time_info);
937 : 33 : mdhd->language_code = 0;
938 : 33 : mdhd->quality = 0;
939 : 33 : }
940 : :
941 : : static void
942 : 33 : atom_mdhd_clear (AtomMDHD * mdhd)
943 : : {
944 : 33 : atom_full_clear (&mdhd->header);
945 : 33 : }
946 : :
947 : : static void
948 : 33 : atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
949 : : {
950 : 33 : atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
951 : :
952 : 33 : atom_mdhd_init (&mdia->mdhd);
953 : 33 : atom_hdlr_init (&mdia->hdlr);
954 : 33 : atom_minf_init (&mdia->minf, context);
955 : 33 : }
956 : :
957 : : static void
958 : 33 : atom_mdia_clear (AtomMDIA * mdia)
959 : : {
960 : 33 : atom_clear (&mdia->header);
961 : 33 : atom_mdhd_clear (&mdia->mdhd);
962 : 33 : atom_hdlr_clear (&mdia->hdlr);
963 : 33 : atom_minf_clear (&mdia->minf);
964 : 33 : }
965 : :
966 : : static void
967 : 33 : atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
968 : : {
969 : : /*
970 : : * flags info
971 : : * 1 -> track enabled
972 : : * 2 -> track in movie
973 : : * 4 -> track in preview
974 : : */
975 : 33 : guint8 flags[3] = { 0, 0, 7 };
976 : :
977 : 33 : atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
978 : :
979 : 33 : tkhd->creation_time = tkhd->modification_time = get_current_qt_time ();
980 : 33 : tkhd->duration = 0;
981 : 33 : tkhd->track_ID = 0;
982 : 33 : tkhd->reserved = 0;
983 : :
984 : 33 : tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
985 : 33 : tkhd->layer = 0;
986 : 33 : tkhd->alternate_group = 0;
987 : 33 : tkhd->volume = 0;
988 : 33 : tkhd->reserved3 = 0;
989 : 33 : memset (tkhd->matrix, 0, sizeof (guint32) * 9);
990 : 33 : tkhd->matrix[0] = 1 << 16;
991 : 33 : tkhd->matrix[4] = 1 << 16;
992 : 33 : tkhd->matrix[8] = 16384 << 16;
993 : 33 : tkhd->width = 0;
994 : 33 : tkhd->height = 0;
995 : 33 : }
996 : :
997 : : static void
998 : 33 : atom_tkhd_clear (AtomTKHD * tkhd)
999 : : {
1000 : 33 : atom_full_clear (&tkhd->header);
1001 : 33 : }
1002 : :
1003 : : static void
1004 : 33 : atom_trak_init (AtomTRAK * trak, AtomsContext * context)
1005 : : {
1006 : 33 : atom_header_set (&trak->header, FOURCC_trak, 0, 0);
1007 : :
1008 : 33 : atom_tkhd_init (&trak->tkhd, context);
1009 : 33 : trak->edts = NULL;
1010 : 33 : atom_mdia_init (&trak->mdia, context);
1011 : 33 : }
1012 : :
1013 : : AtomTRAK *
1014 : 33 : atom_trak_new (AtomsContext * context)
1015 : : {
1016 : 33 : AtomTRAK *trak = g_new0 (AtomTRAK, 1);
1017 : :
1018 : 33 : atom_trak_init (trak, context);
1019 : 33 : return trak;
1020 : : }
1021 : :
1022 : : static void
1023 : 33 : atom_trak_clear (AtomTRAK * trak)
1024 : : {
1025 : 33 : atom_clear (&trak->header);
1026 : 33 : atom_tkhd_clear (&trak->tkhd);
1027 [ - + ]: 33 : if (trak->edts)
1028 : 0 : atom_edts_free (trak->edts);
1029 : 33 : atom_mdia_clear (&trak->mdia);
1030 : 33 : }
1031 : :
1032 : : static void
1033 : 33 : atom_trak_free (AtomTRAK * trak)
1034 : : {
1035 : 33 : atom_trak_clear (trak);
1036 : 33 : g_free (trak);
1037 : 33 : }
1038 : :
1039 : : static void
1040 : 4 : atom_ilst_init (AtomILST * ilst)
1041 : : {
1042 : 4 : atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
1043 : 4 : ilst->entries = NULL;
1044 : 4 : }
1045 : :
1046 : : static AtomILST *
1047 : 4 : atom_ilst_new (void)
1048 : : {
1049 : 4 : AtomILST *ilst = g_new0 (AtomILST, 1);
1050 : :
1051 : 4 : atom_ilst_init (ilst);
1052 : 4 : return ilst;
1053 : : }
1054 : :
1055 : : static void
1056 : 4 : atom_ilst_free (AtomILST * ilst)
1057 : : {
1058 [ + + ]: 4 : if (ilst->entries)
1059 : 2 : atom_info_list_free (ilst->entries);
1060 : 4 : atom_clear (&ilst->header);
1061 : 4 : g_free (ilst);
1062 : 4 : }
1063 : :
1064 : : static void
1065 : 4 : atom_meta_init (AtomMETA * meta)
1066 : : {
1067 : 4 : guint8 flags[3] = { 0, 0, 0 };
1068 : :
1069 : 4 : atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
1070 : 4 : atom_hdlr_init (&meta->hdlr);
1071 : : /* FIXME (ISOM says this is always 0) */
1072 : 4 : meta->hdlr.component_type = FOURCC_mhlr;
1073 : 4 : meta->hdlr.handler_type = FOURCC_mdir;
1074 : 4 : meta->ilst = NULL;
1075 : 4 : }
1076 : :
1077 : : static AtomMETA *
1078 : 4 : atom_meta_new (void)
1079 : : {
1080 : 4 : AtomMETA *meta = g_new0 (AtomMETA, 1);
1081 : :
1082 : 4 : atom_meta_init (meta);
1083 : 4 : return meta;
1084 : : }
1085 : :
1086 : : static void
1087 : 4 : atom_meta_free (AtomMETA * meta)
1088 : : {
1089 : 4 : atom_full_clear (&meta->header);
1090 : 4 : atom_hdlr_clear (&meta->hdlr);
1091 [ + - ]: 4 : if (meta->ilst)
1092 : 4 : atom_ilst_free (meta->ilst);
1093 : 4 : meta->ilst = NULL;
1094 : 4 : g_free (meta);
1095 : 4 : }
1096 : :
1097 : : static void
1098 : 6 : atom_udta_init (AtomUDTA * udta)
1099 : : {
1100 : 6 : atom_header_set (&udta->header, FOURCC_udta, 0, 0);
1101 : 6 : udta->meta = NULL;
1102 : 6 : }
1103 : :
1104 : : static AtomUDTA *
1105 : 6 : atom_udta_new (void)
1106 : : {
1107 : 6 : AtomUDTA *udta = g_new0 (AtomUDTA, 1);
1108 : :
1109 : 6 : atom_udta_init (udta);
1110 : 6 : return udta;
1111 : : }
1112 : :
1113 : : static void
1114 : 6 : atom_udta_free (AtomUDTA * udta)
1115 : : {
1116 : 6 : atom_clear (&udta->header);
1117 [ + + ]: 6 : if (udta->meta)
1118 : 4 : atom_meta_free (udta->meta);
1119 : 6 : udta->meta = NULL;
1120 [ + + ]: 6 : if (udta->entries)
1121 : 5 : atom_info_list_free (udta->entries);
1122 : 6 : g_free (udta);
1123 : 6 : }
1124 : :
1125 : : static void
1126 : 8 : atom_tag_data_init (AtomTagData * data)
1127 : : {
1128 : 8 : guint8 flags[] = { 0, 0, 0 };
1129 : :
1130 : 8 : atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
1131 : 8 : }
1132 : :
1133 : : static void
1134 : 8 : atom_tag_data_clear (AtomTagData * data)
1135 : : {
1136 : 8 : atom_full_clear (&data->header);
1137 : 8 : g_free (data->data);
1138 : 8 : data->datalen = 0;
1139 : 8 : }
1140 : :
1141 : : /*
1142 : : * Fourcc is the tag fourcc
1143 : : * flags will be truncated to 24bits
1144 : : */
1145 : : static AtomTag *
1146 : 8 : atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
1147 : : {
1148 : 8 : AtomTag *tag = g_new0 (AtomTag, 1);
1149 : :
1150 : 8 : tag->header.type = fourcc;
1151 : 8 : atom_tag_data_init (&tag->data);
1152 : 8 : atom_full_set_flags_as_uint (&tag->data.header, flags_as_uint);
1153 : 8 : return tag;
1154 : : }
1155 : :
1156 : : static void
1157 : 8 : atom_tag_free (AtomTag * tag)
1158 : : {
1159 : 8 : atom_clear (&tag->header);
1160 : 8 : atom_tag_data_clear (&tag->data);
1161 : 8 : g_free (tag);
1162 : 8 : }
1163 : :
1164 : : static void
1165 : 88 : atom_mvhd_init (AtomMVHD * mvhd)
1166 : : {
1167 : 88 : guint8 flags[3] = { 0, 0, 0 };
1168 : :
1169 : 88 : atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
1170 : :
1171 : 88 : common_time_info_init (&mvhd->time_info);
1172 : :
1173 : 88 : mvhd->prefered_rate = 1 << 16;
1174 : 88 : mvhd->volume = 1 << 8;
1175 : 88 : mvhd->reserved3 = 0;
1176 : 88 : memset (mvhd->reserved4, 0, sizeof (guint32[2]));
1177 : :
1178 : 88 : memset (mvhd->matrix, 0, sizeof (guint32[9]));
1179 : 88 : mvhd->matrix[0] = 1 << 16;
1180 : 88 : mvhd->matrix[4] = 1 << 16;
1181 : 88 : mvhd->matrix[8] = 16384 << 16;
1182 : :
1183 : 88 : mvhd->preview_time = 0;
1184 : 88 : mvhd->preview_duration = 0;
1185 : 88 : mvhd->poster_time = 0;
1186 : 88 : mvhd->selection_time = 0;
1187 : 88 : mvhd->selection_duration = 0;
1188 : 88 : mvhd->current_time = 0;
1189 : :
1190 : 88 : mvhd->next_track_id = 1;
1191 : 88 : }
1192 : :
1193 : : static void
1194 : 88 : atom_mvhd_clear (AtomMVHD * mvhd)
1195 : : {
1196 : 88 : atom_full_clear (&mvhd->header);
1197 : 88 : }
1198 : :
1199 : : static void
1200 : 88 : atom_mehd_init (AtomMEHD * mehd)
1201 : : {
1202 : 88 : guint8 flags[3] = { 0, 0, 0 };
1203 : :
1204 : 88 : atom_full_init (&mehd->header, FOURCC_mehd, 0, 0, 1, flags);
1205 : 88 : mehd->fragment_duration = 0;
1206 : 88 : }
1207 : :
1208 : : static void
1209 : 88 : atom_mvex_init (AtomMVEX * mvex)
1210 : : {
1211 : 88 : atom_header_set (&mvex->header, FOURCC_mvex, 0, 0);
1212 : 88 : atom_mehd_init (&mvex->mehd);
1213 : 88 : mvex->trexs = NULL;
1214 : 88 : }
1215 : :
1216 : : static void
1217 : 88 : atom_moov_init (AtomMOOV * moov, AtomsContext * context)
1218 : : {
1219 : 88 : atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
1220 : 88 : atom_mvhd_init (&(moov->mvhd));
1221 : 88 : atom_mvex_init (&(moov->mvex));
1222 : 88 : moov->udta = NULL;
1223 : 88 : moov->traks = NULL;
1224 : 88 : moov->context = *context;
1225 : 88 : }
1226 : :
1227 : : AtomMOOV *
1228 : 88 : atom_moov_new (AtomsContext * context)
1229 : : {
1230 : 88 : AtomMOOV *moov = g_new0 (AtomMOOV, 1);
1231 : :
1232 : 88 : atom_moov_init (moov, context);
1233 : 88 : return moov;
1234 : : }
1235 : :
1236 : : static void
1237 : 33 : atom_trex_free (AtomTREX * trex)
1238 : : {
1239 : 33 : atom_full_clear (&trex->header);
1240 : 33 : g_free (trex);
1241 : 33 : }
1242 : :
1243 : : static void
1244 : 88 : atom_mvex_clear (AtomMVEX * mvex)
1245 : : {
1246 : : GList *walker;
1247 : :
1248 : 88 : atom_clear (&mvex->header);
1249 : 88 : walker = mvex->trexs;
1250 [ + + ]: 121 : while (walker) {
1251 : 33 : atom_trex_free ((AtomTREX *) walker->data);
1252 [ + - ]: 33 : walker = g_list_next (walker);
1253 : : }
1254 : 88 : g_list_free (mvex->trexs);
1255 : 88 : mvex->trexs = NULL;
1256 : 88 : }
1257 : :
1258 : : void
1259 : 88 : atom_moov_free (AtomMOOV * moov)
1260 : : {
1261 : : GList *walker;
1262 : :
1263 : 88 : atom_clear (&moov->header);
1264 : 88 : atom_mvhd_clear (&moov->mvhd);
1265 : :
1266 : 88 : walker = moov->traks;
1267 [ + + ]: 121 : while (walker) {
1268 : 33 : atom_trak_free ((AtomTRAK *) walker->data);
1269 [ + - ]: 33 : walker = g_list_next (walker);
1270 : : }
1271 : 88 : g_list_free (moov->traks);
1272 : 88 : moov->traks = NULL;
1273 : :
1274 [ + + ]: 88 : if (moov->udta) {
1275 : 6 : atom_udta_free (moov->udta);
1276 : 6 : moov->udta = NULL;
1277 : : }
1278 : :
1279 : 88 : atom_mvex_clear (&moov->mvex);
1280 : :
1281 : 88 : g_free (moov);
1282 : 88 : }
1283 : :
1284 : : /* -- end of init / free -- */
1285 : :
1286 : : /* -- copy data functions -- */
1287 : :
1288 : : static guint8
1289 : 54 : atom_full_get_version (AtomFull * full)
1290 : : {
1291 : 54 : return full->version;
1292 : : }
1293 : :
1294 : : static guint64
1295 : 36 : common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
1296 : : guint8 ** buffer, guint64 * size, guint64 * offset)
1297 : : {
1298 : 36 : guint64 original_offset = *offset;
1299 : :
1300 [ + - ]: 36 : if (trunc_to_32) {
1301 : 36 : prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
1302 : 36 : prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
1303 : 36 : prop_copy_uint32 (ti->timescale, buffer, size, offset);
1304 : 36 : prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
1305 : : } else {
1306 : 0 : prop_copy_uint64 (ti->creation_time, buffer, size, offset);
1307 : 0 : prop_copy_uint64 (ti->modification_time, buffer, size, offset);
1308 : 0 : prop_copy_uint32 (ti->timescale, buffer, size, offset);
1309 : 0 : prop_copy_uint64 (ti->duration, buffer, size, offset);
1310 : : }
1311 : 36 : return *offset - original_offset;
1312 : : }
1313 : :
1314 : : static void
1315 : 763 : atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
1316 : : guint64 atom_pos)
1317 : : {
1318 : : /* this only works for non-extended atom size, which is OK
1319 : : * (though it could be made to do mem_move, etc and write extended size) */
1320 : 763 : prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
1321 : 763 : }
1322 : :
1323 : : guint64
1324 : 503 : atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
1325 : : {
1326 : 503 : guint64 original_offset = *offset;
1327 : :
1328 : : /* copies type and size */
1329 : 503 : prop_copy_uint32 (atom->size, buffer, size, offset);
1330 : 503 : prop_copy_fourcc (atom->type, buffer, size, offset);
1331 : :
1332 : : /* extended size needed */
1333 [ + + ]: 503 : if (atom->size == 1) {
1334 : : /* really should not happen other than with mdat atom;
1335 : : * would be a problem for size (re)write code, not to mention memory */
1336 [ - + ]: 12 : g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
1337 : 12 : prop_copy_uint64 (atom->extended_size, buffer, size, offset);
1338 : : }
1339 : :
1340 : 503 : return *offset - original_offset;
1341 : : }
1342 : :
1343 : : static guint64
1344 : 288 : atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
1345 : : guint64 * offset)
1346 : : {
1347 : 288 : guint64 original_offset = *offset;
1348 : :
1349 [ - + ]: 288 : if (!atom_copy_data (&atom->header, buffer, size, offset)) {
1350 : 0 : return 0;
1351 : : }
1352 : :
1353 : 288 : prop_copy_uint8 (atom->version, buffer, size, offset);
1354 : 288 : prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
1355 : :
1356 : 288 : atom_write_size (buffer, size, offset, original_offset);
1357 : 288 : return *offset - original_offset;
1358 : : }
1359 : :
1360 : : static guint64
1361 : 21 : atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
1362 : : guint64 * offset)
1363 : : {
1364 : 21 : guint64 original_offset = *offset;
1365 : :
1366 [ + + ]: 51 : while (ai) {
1367 : 30 : AtomInfo *info = (AtomInfo *) ai->data;
1368 : :
1369 [ - + ]: 30 : if (!info->copy_data_func (info->atom, buffer, size, offset)) {
1370 : 0 : return 0;
1371 : : }
1372 [ + - ]: 30 : ai = g_list_next (ai);
1373 : : }
1374 : :
1375 : 21 : return *offset - original_offset;
1376 : : }
1377 : :
1378 : : static guint64
1379 : 17 : atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
1380 : : guint64 * offset)
1381 : : {
1382 : 17 : guint64 original_offset = *offset;
1383 : :
1384 [ - + ]: 17 : if (!atom_copy_data (&data->header, buffer, size, offset)) {
1385 : 0 : return 0;
1386 : : }
1387 [ + - ]: 17 : if (data->datalen)
1388 : 17 : prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
1389 : :
1390 : 17 : atom_write_size (buffer, size, offset, original_offset);
1391 : 17 : return *offset - original_offset;
1392 : : }
1393 : :
1394 : : static guint64
1395 : 6 : atom_uuid_copy_data (AtomUUID * uuid, guint8 ** buffer, guint64 * size,
1396 : : guint64 * offset)
1397 : : {
1398 : 6 : guint64 original_offset = *offset;
1399 : :
1400 [ - + ]: 6 : if (!atom_copy_data (&uuid->header, buffer, size, offset)) {
1401 : 0 : return 0;
1402 : : }
1403 : 6 : prop_copy_uint8_array (uuid->uuid, 16, buffer, size, offset);
1404 [ + - ]: 6 : if (uuid->datalen)
1405 : 6 : prop_copy_uint8_array (uuid->data, uuid->datalen, buffer, size, offset);
1406 : :
1407 : 6 : atom_write_size (buffer, size, offset, original_offset);
1408 : 6 : return *offset - original_offset;
1409 : : }
1410 : :
1411 : : guint64
1412 : 16 : atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
1413 : : guint64 * offset)
1414 : : {
1415 : 16 : guint64 original_offset = *offset;
1416 : :
1417 [ - + ]: 16 : if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
1418 : 0 : return 0;
1419 : : }
1420 : 16 : prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
1421 : 16 : prop_copy_uint32 (ftyp->version, buffer, size, offset);
1422 : :
1423 : 16 : prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
1424 : : buffer, size, offset);
1425 : :
1426 : 16 : atom_write_size (buffer, size, offset, original_offset);
1427 : 16 : return *offset - original_offset;
1428 : : }
1429 : :
1430 : : guint64
1431 : 18 : atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
1432 : : guint64 * offset)
1433 : : {
1434 : : guint8 version;
1435 : 18 : guint64 original_offset = *offset;
1436 : :
1437 [ - + ]: 18 : if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
1438 : 0 : return 0;
1439 : : }
1440 : :
1441 : 18 : version = atom_full_get_version (&(atom->header));
1442 [ + - ]: 18 : if (version == 0) {
1443 : 18 : common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
1444 [ # # ]: 0 : } else if (version == 1) {
1445 : 0 : common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
1446 : : } else {
1447 : 0 : *offset = original_offset;
1448 : 0 : return 0;
1449 : : }
1450 : :
1451 : 18 : prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
1452 : 18 : prop_copy_uint16 (atom->volume, buffer, size, offset);
1453 : 18 : prop_copy_uint16 (atom->reserved3, buffer, size, offset);
1454 : 18 : prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
1455 : 18 : prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
1456 : 18 : prop_copy_uint32 (atom->preview_time, buffer, size, offset);
1457 : 18 : prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
1458 : 18 : prop_copy_uint32 (atom->poster_time, buffer, size, offset);
1459 : 18 : prop_copy_uint32 (atom->selection_time, buffer, size, offset);
1460 : 18 : prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
1461 : 18 : prop_copy_uint32 (atom->current_time, buffer, size, offset);
1462 : :
1463 : 18 : prop_copy_uint32 (atom->next_track_id, buffer, size, offset);
1464 : :
1465 : 18 : atom_write_size (buffer, size, offset, original_offset);
1466 : 18 : return *offset - original_offset;
1467 : : }
1468 : :
1469 : : static guint64
1470 : 18 : atom_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
1471 : : guint64 * offset)
1472 : : {
1473 : 18 : guint64 original_offset = *offset;
1474 : :
1475 [ - + ]: 18 : if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
1476 : 0 : return 0;
1477 : : }
1478 : :
1479 [ + - ]: 18 : if (atom_full_get_version (&tkhd->header) == 0) {
1480 : 18 : prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
1481 : 18 : prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
1482 : 18 : prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1483 : 18 : prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1484 : 18 : prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
1485 : : } else {
1486 : 0 : prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
1487 : 0 : prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
1488 : 0 : prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
1489 : 0 : prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
1490 : 0 : prop_copy_uint64 (tkhd->duration, buffer, size, offset);
1491 : : }
1492 : :
1493 : 18 : prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
1494 : 18 : prop_copy_uint16 (tkhd->layer, buffer, size, offset);
1495 : 18 : prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
1496 : 18 : prop_copy_uint16 (tkhd->volume, buffer, size, offset);
1497 : 18 : prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
1498 : 18 : prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
1499 : :
1500 : 18 : prop_copy_uint32 (tkhd->width, buffer, size, offset);
1501 : 18 : prop_copy_uint32 (tkhd->height, buffer, size, offset);
1502 : :
1503 : 18 : atom_write_size (buffer, size, offset, original_offset);
1504 : 18 : return *offset - original_offset;
1505 : : }
1506 : :
1507 : : static guint64
1508 : 34 : atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
1509 : : guint64 * offset)
1510 : : {
1511 : 34 : guint64 original_offset = *offset;
1512 : :
1513 [ - + ]: 34 : if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
1514 : 0 : return 0;
1515 : : }
1516 : :
1517 : 34 : prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
1518 : 34 : prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
1519 : 34 : prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
1520 : 34 : prop_copy_uint32 (hdlr->flags, buffer, size, offset);
1521 : 34 : prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
1522 : :
1523 : 34 : prop_copy_size_string ((guint8 *) hdlr->name, strlen (hdlr->name), buffer,
1524 : : size, offset);
1525 : :
1526 : 34 : atom_write_size (buffer, size, offset, original_offset);
1527 : 34 : return *offset - original_offset;
1528 : : }
1529 : :
1530 : : static guint64
1531 : 14 : atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
1532 : : guint64 * offset)
1533 : : {
1534 : 14 : guint64 original_offset = *offset;
1535 : :
1536 [ - + ]: 14 : if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
1537 : 0 : return 0;
1538 : : }
1539 : 14 : prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
1540 : 14 : prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
1541 : :
1542 : 14 : atom_write_size (buffer, size, offset, original_offset);
1543 : 14 : return original_offset - *offset;
1544 : : }
1545 : :
1546 : : static guint64
1547 : 4 : atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
1548 : : guint64 * offset)
1549 : : {
1550 : 4 : guint64 original_offset = *offset;
1551 : :
1552 [ - + ]: 4 : if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
1553 : 0 : return 0;
1554 : : }
1555 : 4 : prop_copy_uint16 (smhd->balance, buffer, size, offset);
1556 : 4 : prop_copy_uint16 (smhd->reserved, buffer, size, offset);
1557 : :
1558 : 4 : atom_write_size (buffer, size, offset, original_offset);
1559 : 4 : return original_offset - *offset;
1560 : : }
1561 : :
1562 : : static guint64
1563 : 0 : atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
1564 : : guint64 * offset)
1565 : : {
1566 : 0 : guint64 original_offset = *offset;
1567 : :
1568 [ # # ]: 0 : if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
1569 : 0 : return 0;
1570 : : }
1571 : 0 : prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
1572 : 0 : prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
1573 : 0 : prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
1574 : 0 : prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
1575 : 0 : prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
1576 : :
1577 : 0 : atom_write_size (buffer, size, offset, original_offset);
1578 : 0 : return original_offset - *offset;
1579 : : }
1580 : :
1581 : : static gboolean
1582 : 6 : atom_url_same_file_flag (AtomURL * url)
1583 : : {
1584 : 6 : return (url->header.flags[2] & 0x1) == 1;
1585 : : }
1586 : :
1587 : : static guint64
1588 : 6 : atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
1589 : : guint64 * offset)
1590 : : {
1591 : 6 : guint64 original_offset = *offset;
1592 : :
1593 [ - + ]: 6 : if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
1594 : 0 : return 0;
1595 : : }
1596 : :
1597 [ - + ]: 6 : if (!atom_url_same_file_flag (url)) {
1598 : 0 : prop_copy_null_terminated_string (url->location, buffer, size, offset);
1599 : : }
1600 : :
1601 : 6 : atom_write_size (buffer, size, offset, original_offset);
1602 : 6 : return original_offset - *offset;
1603 : : }
1604 : :
1605 : : guint64
1606 : 18 : atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
1607 : : guint64 * offset)
1608 : : {
1609 : 18 : guint64 original_offset = *offset;
1610 : : guint i;
1611 : :
1612 [ - + ]: 18 : if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
1613 : 0 : return 0;
1614 : : }
1615 : :
1616 : 18 : prop_copy_uint32 (atom_array_get_len (&stts->entries), buffer, size, offset);
1617 : : /* minimize realloc */
1618 : 18 : prop_copy_ensure_buffer (buffer, size, offset,
1619 : 18 : 8 * atom_array_get_len (&stts->entries));
1620 [ + + ]: 30 : for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
1621 : 12 : STTSEntry *entry = &atom_array_index (&stts->entries, i);
1622 : :
1623 : 12 : prop_copy_uint32 (entry->sample_count, buffer, size, offset);
1624 : 12 : prop_copy_int32 (entry->sample_delta, buffer, size, offset);
1625 : : }
1626 : :
1627 : 18 : atom_write_size (buffer, size, offset, original_offset);
1628 : 18 : return *offset - original_offset;
1629 : : }
1630 : :
1631 : : static guint64
1632 : 18 : atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
1633 : : guint64 * size, guint64 * offset)
1634 : : {
1635 : 18 : guint64 original_offset = *offset;
1636 : :
1637 [ - + ]: 18 : if (!atom_copy_data (&se->header, buffer, size, offset)) {
1638 : 0 : return 0;
1639 : : }
1640 : :
1641 : 18 : prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
1642 : 18 : prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
1643 : :
1644 : 18 : return *offset - original_offset;
1645 : : }
1646 : :
1647 : : static guint64
1648 : 5 : atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
1649 : : guint64 * offset)
1650 : : {
1651 : 5 : guint64 original_offset = *offset;
1652 : :
1653 [ - + ]: 5 : if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
1654 : 0 : return 0;
1655 : : }
1656 [ - + ]: 5 : if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
1657 : 0 : return 0;
1658 : : }
1659 : :
1660 : 5 : atom_write_size (buffer, size, offset, original_offset);
1661 : 5 : return *offset - original_offset;
1662 : : }
1663 : :
1664 : : static guint64
1665 : 0 : atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
1666 : : guint64 * size, guint64 * offset)
1667 : : {
1668 : 0 : guint64 original_offset = *offset;
1669 : :
1670 [ # # ]: 0 : if (!atom_copy_data (&(frma->header), buffer, size, offset))
1671 : 0 : return 0;
1672 : :
1673 : 0 : prop_copy_fourcc (frma->media_type, buffer, size, offset);
1674 : :
1675 : 0 : atom_write_size (buffer, size, offset, original_offset);
1676 : 0 : return *offset - original_offset;
1677 : : }
1678 : :
1679 : : static guint64
1680 : 0 : atom_mp4s_copy_data (SampleTableEntryMP4S * mp4s, guint8 ** buffer,
1681 : : guint64 * size, guint64 * offset)
1682 : : {
1683 : 0 : guint64 original_offset = *offset;
1684 : :
1685 [ # # ]: 0 : if (!atom_sample_entry_copy_data (&mp4s->se, buffer, size, offset)) {
1686 : 0 : return 0;
1687 : : }
1688 [ # # ]: 0 : if (!atom_esds_copy_data (&mp4s->es, buffer, size, offset)) {
1689 : 0 : return 0;
1690 : : }
1691 : :
1692 : 0 : atom_write_size (buffer, size, offset, original_offset);
1693 : 0 : return *offset - original_offset;
1694 : : }
1695 : :
1696 : : static guint64
1697 : 0 : atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
1698 : : guint64 * size, guint64 * offset)
1699 : : {
1700 : 0 : guint64 original_offset = *offset;
1701 : :
1702 [ # # ]: 0 : if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
1703 : 0 : return 0;
1704 : : }
1705 : :
1706 : 0 : prop_copy_uint32 (hse->size, buffer, size, offset);
1707 : 0 : prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
1708 : :
1709 : 0 : atom_write_size (buffer, size, offset, original_offset);
1710 : 0 : return *offset - original_offset;
1711 : : }
1712 : :
1713 : : static guint64
1714 : 4 : sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
1715 : : guint64 * size, guint64 * offset)
1716 : : {
1717 : 4 : guint64 original_offset = *offset;
1718 : :
1719 [ - + ]: 4 : if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
1720 : 0 : return 0;
1721 : : }
1722 : :
1723 : 4 : prop_copy_uint16 (mp4a->version, buffer, size, offset);
1724 : 4 : prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
1725 : 4 : prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
1726 : 4 : prop_copy_uint16 (mp4a->channels, buffer, size, offset);
1727 : 4 : prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
1728 : 4 : prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
1729 : 4 : prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
1730 : 4 : prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
1731 : :
1732 : : /* this should always be 0 for mp4 flavor */
1733 [ + - ]: 4 : if (mp4a->version == 1) {
1734 : 4 : prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
1735 : 4 : prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
1736 : 4 : prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
1737 : 4 : prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
1738 : : }
1739 : :
1740 [ - + ]: 4 : if (mp4a->extension_atoms) {
1741 [ # # ]: 0 : if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
1742 : 0 : return 0;
1743 : : }
1744 : :
1745 : 4 : atom_write_size (buffer, size, offset, original_offset);
1746 : 4 : return *offset - original_offset;
1747 : : }
1748 : :
1749 : : static guint64
1750 : 14 : sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
1751 : : guint64 * size, guint64 * offset)
1752 : : {
1753 : 14 : guint64 original_offset = *offset;
1754 : :
1755 [ - + ]: 14 : if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
1756 : 0 : return 0;
1757 : : }
1758 : :
1759 : 14 : prop_copy_uint16 (mp4v->version, buffer, size, offset);
1760 : 14 : prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
1761 : 14 : prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
1762 : 14 : prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
1763 : 14 : prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
1764 : :
1765 : 14 : prop_copy_uint16 (mp4v->width, buffer, size, offset);
1766 : 14 : prop_copy_uint16 (mp4v->height, buffer, size, offset);
1767 : :
1768 : 14 : prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
1769 : 14 : prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
1770 : 14 : prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
1771 : :
1772 : 14 : prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
1773 : :
1774 : 14 : prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
1775 : : offset);
1776 : :
1777 : 14 : prop_copy_uint16 (mp4v->depth, buffer, size, offset);
1778 : 14 : prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
1779 : :
1780 : : /* extra atoms */
1781 [ + - - + ]: 28 : if (mp4v->extension_atoms &&
1782 : 14 : !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
1783 : 0 : return 0;
1784 : :
1785 : 14 : atom_write_size (buffer, size, offset, original_offset);
1786 : 14 : return *offset - original_offset;
1787 : : }
1788 : :
1789 : : guint64
1790 : 18 : atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
1791 : : guint64 * offset)
1792 : : {
1793 : 18 : guint64 original_offset = *offset;
1794 : : guint i;
1795 : :
1796 [ - + ]: 18 : if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
1797 : 0 : return 0;
1798 : : }
1799 : :
1800 : 18 : prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
1801 : 18 : prop_copy_uint32 (stsz->table_size, buffer, size, offset);
1802 [ + - ]: 18 : if (stsz->sample_size == 0) {
1803 : : /* minimize realloc */
1804 : 18 : prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
1805 : : /* entry count must match sample count */
1806 [ - + ]: 18 : g_assert (atom_array_get_len (&stsz->entries) == stsz->table_size);
1807 [ + + ]: 30 : for (i = 0; i < atom_array_get_len (&stsz->entries); i++) {
1808 : 12 : prop_copy_uint32 (atom_array_index (&stsz->entries, i), buffer, size,
1809 : : offset);
1810 : : }
1811 : : }
1812 : :
1813 : 18 : atom_write_size (buffer, size, offset, original_offset);
1814 : 18 : return *offset - original_offset;
1815 : : }
1816 : :
1817 : : guint64
1818 : 18 : atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
1819 : : guint64 * offset)
1820 : : {
1821 : 18 : guint64 original_offset = *offset;
1822 : : guint i;
1823 : :
1824 [ - + ]: 18 : if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
1825 : 0 : return 0;
1826 : : }
1827 : :
1828 : 18 : prop_copy_uint32 (atom_array_get_len (&stsc->entries), buffer, size, offset);
1829 : : /* minimize realloc */
1830 : 18 : prop_copy_ensure_buffer (buffer, size, offset,
1831 : 18 : 12 * atom_array_get_len (&stsc->entries));
1832 : :
1833 [ + + ]: 30 : for (i = 0; i < atom_array_get_len (&stsc->entries); i++) {
1834 : 12 : STSCEntry *entry = &atom_array_index (&stsc->entries, i);
1835 : :
1836 : 12 : prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
1837 : 12 : prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
1838 : 12 : prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
1839 : : }
1840 : :
1841 : 18 : atom_write_size (buffer, size, offset, original_offset);
1842 : 18 : return *offset - original_offset;
1843 : : }
1844 : :
1845 : : guint64
1846 : 0 : atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
1847 : : guint64 * offset)
1848 : : {
1849 : 0 : guint64 original_offset = *offset;
1850 : : guint i;
1851 : :
1852 [ # # ]: 0 : if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
1853 : 0 : return 0;
1854 : : }
1855 : :
1856 : 0 : prop_copy_uint32 (atom_array_get_len (&ctts->entries), buffer, size, offset);
1857 : : /* minimize realloc */
1858 : 0 : prop_copy_ensure_buffer (buffer, size, offset,
1859 : 0 : 8 * atom_array_get_len (&ctts->entries));
1860 [ # # ]: 0 : for (i = 0; i < atom_array_get_len (&ctts->entries); i++) {
1861 : 0 : CTTSEntry *entry = &atom_array_index (&ctts->entries, i);
1862 : :
1863 : 0 : prop_copy_uint32 (entry->samplecount, buffer, size, offset);
1864 : 0 : prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
1865 : : }
1866 : :
1867 : 0 : atom_write_size (buffer, size, offset, original_offset);
1868 : 0 : return *offset - original_offset;
1869 : : }
1870 : :
1871 : : guint64
1872 : 18 : atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
1873 : : guint64 * offset)
1874 : : {
1875 : 18 : guint64 original_offset = *offset;
1876 : : guint i;
1877 : 18 : gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
1878 : :
1879 [ - + ]: 18 : if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
1880 : 0 : return 0;
1881 : : }
1882 : :
1883 : 18 : prop_copy_uint32 (atom_array_get_len (&stco64->entries), buffer, size,
1884 : : offset);
1885 : :
1886 : : /* minimize realloc */
1887 : 18 : prop_copy_ensure_buffer (buffer, size, offset,
1888 : 18 : 8 * atom_array_get_len (&stco64->entries));
1889 [ + + ]: 30 : for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
1890 : 12 : guint64 *value = &atom_array_index (&stco64->entries, i);
1891 : :
1892 [ + - ]: 12 : if (trunc_to_32) {
1893 : 12 : prop_copy_uint32 ((guint32) * value, buffer, size, offset);
1894 : : } else {
1895 : 0 : prop_copy_uint64 (*value, buffer, size, offset);
1896 : : }
1897 : : }
1898 : :
1899 : 18 : atom_write_size (buffer, size, offset, original_offset);
1900 : 18 : return *offset - original_offset;
1901 : : }
1902 : :
1903 : : guint64
1904 : 11 : atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
1905 : : guint64 * offset)
1906 : : {
1907 : 11 : guint64 original_offset = *offset;
1908 : : guint i;
1909 : :
1910 [ - + ]: 11 : if (atom_array_get_len (&stss->entries) == 0) {
1911 : : /* FIXME not needing this atom might be confused with error while copying */
1912 : 0 : return 0;
1913 : : }
1914 : :
1915 [ - + ]: 11 : if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
1916 : 0 : return 0;
1917 : : }
1918 : :
1919 : 11 : prop_copy_uint32 (atom_array_get_len (&stss->entries), buffer, size, offset);
1920 : : /* minimize realloc */
1921 : 11 : prop_copy_ensure_buffer (buffer, size, offset,
1922 : 11 : 4 * atom_array_get_len (&stss->entries));
1923 [ + + ]: 22 : for (i = 0; i < atom_array_get_len (&stss->entries); i++) {
1924 : 11 : prop_copy_uint32 (atom_array_index (&stss->entries, i), buffer, size,
1925 : : offset);
1926 : : }
1927 : :
1928 : 11 : atom_write_size (buffer, size, offset, original_offset);
1929 : 11 : return *offset - original_offset;
1930 : : }
1931 : :
1932 : : static guint64
1933 : 18 : atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
1934 : : guint64 * offset)
1935 : : {
1936 : 18 : guint64 original_offset = *offset;
1937 : : GList *walker;
1938 : :
1939 [ - + ]: 18 : if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
1940 : 0 : return 0;
1941 : : }
1942 : :
1943 : 18 : prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
1944 : :
1945 [ + + ]: 36 : for (walker = g_list_last (stsd->entries); walker != NULL;
1946 [ + - ]: 18 : walker = g_list_previous (walker)) {
1947 : 18 : SampleTableEntry *se = (SampleTableEntry *) walker->data;
1948 : :
1949 [ - - + + ]: 18 : switch (((Atom *) walker->data)->type) {
1950 : : case FOURCC_mp4a:
1951 [ # # ]: 0 : if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
1952 : : buffer, size, offset)) {
1953 : 0 : return 0;
1954 : : }
1955 : 0 : break;
1956 : : case FOURCC_mp4s:
1957 [ # # ]: 0 : if (!atom_mp4s_copy_data ((SampleTableEntryMP4S *) walker->data,
1958 : : buffer, size, offset)) {
1959 : 0 : return 0;
1960 : : }
1961 : 0 : break;
1962 : : case FOURCC_mp4v:
1963 [ - + ]: 5 : if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
1964 : : buffer, size, offset)) {
1965 : 0 : return 0;
1966 : : }
1967 : 5 : break;
1968 : : default:
1969 [ + + ]: 13 : if (se->kind == VIDEO) {
1970 [ - + ]: 9 : if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
1971 : 9 : walker->data, buffer, size, offset)) {
1972 : 0 : return 0;
1973 : : }
1974 [ + - ]: 4 : } else if (se->kind == AUDIO) {
1975 [ - + ]: 4 : if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
1976 : 4 : walker->data, buffer, size, offset)) {
1977 : 0 : return 0;
1978 : : }
1979 : : } else {
1980 [ # # ]: 0 : if (!atom_hint_sample_entry_copy_data (
1981 : 0 : (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
1982 : 0 : return 0;
1983 : : }
1984 : : }
1985 : 13 : break;
1986 : : }
1987 : : }
1988 : :
1989 : 18 : atom_write_size (buffer, size, offset, original_offset);
1990 : 18 : return *offset - original_offset;
1991 : : }
1992 : :
1993 : : static guint64
1994 : 18 : atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
1995 : : guint64 * offset)
1996 : : {
1997 : 18 : guint64 original_offset = *offset;
1998 : :
1999 [ - + ]: 18 : if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
2000 : 0 : return 0;
2001 : : }
2002 : :
2003 [ - + ]: 18 : if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
2004 : 0 : return 0;
2005 : : }
2006 [ - + ]: 18 : if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
2007 : 0 : return 0;
2008 : : }
2009 : : /* this atom is optional, so let's check if we need it
2010 : : * (to avoid false error) */
2011 [ + + ]: 18 : if (atom_array_get_len (&stbl->stss.entries)) {
2012 [ - + ]: 11 : if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
2013 : 0 : return 0;
2014 : : }
2015 : : }
2016 : :
2017 [ - + ]: 18 : if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
2018 : 0 : return 0;
2019 : : }
2020 [ - + ]: 18 : if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
2021 : 0 : return 0;
2022 : : }
2023 [ + + ][ - + ]: 18 : if (stbl->ctts && stbl->ctts->do_pts) {
2024 [ # # ]: 0 : if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
2025 : 0 : return 0;
2026 : : }
2027 : : }
2028 [ - + ]: 18 : if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
2029 : 0 : return 0;
2030 : : }
2031 : :
2032 : 18 : atom_write_size (buffer, size, offset, original_offset);
2033 : 18 : return original_offset - *offset;
2034 : : }
2035 : :
2036 : :
2037 : : static guint64
2038 : 18 : atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
2039 : : guint64 * offset)
2040 : : {
2041 : 18 : guint64 original_offset = *offset;
2042 : : GList *walker;
2043 : :
2044 [ - + ]: 18 : if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
2045 : 0 : return 0;
2046 : : }
2047 : :
2048 : 18 : prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
2049 : :
2050 : 18 : walker = dref->entries;
2051 [ + + ]: 36 : while (walker != NULL) {
2052 : 18 : Atom *atom = (Atom *) walker->data;
2053 : :
2054 [ + + ]: 18 : if (atom->type == FOURCC_url_) {
2055 : 6 : atom_url_copy_data ((AtomURL *) atom, buffer, size, offset);
2056 [ + - ]: 12 : } else if (atom->type == FOURCC_alis) {
2057 : 12 : atom_full_copy_data ((AtomFull *) atom, buffer, size, offset);
2058 : : } else {
2059 : 0 : g_error ("Unsupported atom used inside dref atom");
2060 : : }
2061 [ + - ]: 18 : walker = g_list_next (walker);
2062 : : }
2063 : :
2064 : 18 : atom_write_size (buffer, size, offset, original_offset);
2065 : 18 : return *offset - original_offset;
2066 : : }
2067 : :
2068 : : static guint64
2069 : 18 : atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
2070 : : guint64 * offset)
2071 : : {
2072 : 18 : guint64 original_offset = *offset;
2073 : :
2074 [ - + ]: 18 : if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
2075 : 0 : return 0;
2076 : : }
2077 : :
2078 [ - + ]: 18 : if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
2079 : 0 : return 0;
2080 : : }
2081 : :
2082 : 18 : atom_write_size (buffer, size, offset, original_offset);
2083 : 18 : return original_offset - *offset;
2084 : : }
2085 : :
2086 : : static guint64
2087 : 18 : atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
2088 : : guint64 * offset)
2089 : : {
2090 : 18 : guint64 original_offset = *offset;
2091 : :
2092 [ - + ]: 18 : if (!atom_copy_data (&minf->header, buffer, size, offset)) {
2093 : 0 : return 0;
2094 : : }
2095 : :
2096 [ + + ]: 18 : if (minf->vmhd) {
2097 [ - + ]: 14 : if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
2098 : 0 : return 0;
2099 : : }
2100 [ + - ]: 4 : } else if (minf->smhd) {
2101 [ - + ]: 4 : if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
2102 : 0 : return 0;
2103 : : }
2104 [ # # ]: 0 : } else if (minf->hmhd) {
2105 [ # # ]: 0 : if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
2106 : 0 : return 0;
2107 : : }
2108 : : }
2109 : :
2110 [ + + ]: 18 : if (minf->hdlr) {
2111 [ - + ]: 12 : if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
2112 : 0 : return 0;
2113 : : }
2114 : : }
2115 : :
2116 [ - + ]: 18 : if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
2117 : 0 : return 0;
2118 : : }
2119 [ - + ]: 18 : if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
2120 : 0 : return 0;
2121 : : }
2122 : :
2123 : 18 : atom_write_size (buffer, size, offset, original_offset);
2124 : 18 : return *offset - original_offset;
2125 : : }
2126 : :
2127 : : static guint64
2128 : 18 : atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
2129 : : guint64 * offset)
2130 : : {
2131 : 18 : guint64 original_offset = *offset;
2132 : :
2133 [ - + ]: 18 : if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
2134 : 0 : return 0;
2135 : : }
2136 : :
2137 [ - + ]: 18 : if (!common_time_info_copy_data (&mdhd->time_info,
2138 : 18 : atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
2139 : 0 : return 0;
2140 : : }
2141 : :
2142 : 18 : prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
2143 : 18 : prop_copy_uint16 (mdhd->quality, buffer, size, offset);
2144 : :
2145 : 18 : atom_write_size (buffer, size, offset, original_offset);
2146 : 18 : return *offset - original_offset;
2147 : : }
2148 : :
2149 : : static guint64
2150 : 18 : atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
2151 : : guint64 * offset)
2152 : : {
2153 : 18 : guint64 original_offset = *offset;
2154 : :
2155 [ - + ]: 18 : if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
2156 : 0 : return 0;
2157 : : }
2158 [ - + ]: 18 : if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
2159 : 0 : return 0;
2160 : : }
2161 [ - + ]: 18 : if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
2162 : 0 : return 0;
2163 : : }
2164 : :
2165 [ - + ]: 18 : if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
2166 : 0 : return 0;
2167 : : }
2168 : :
2169 : 18 : atom_write_size (buffer, size, offset, original_offset);
2170 : 18 : return *offset - original_offset;
2171 : : }
2172 : :
2173 : : static guint64
2174 : 0 : atom_elst_copy_data (AtomELST * elst, guint8 ** buffer, guint64 * size,
2175 : : guint64 * offset)
2176 : : {
2177 : 0 : guint64 original_offset = *offset;
2178 : : GSList *walker;
2179 : :
2180 [ # # ]: 0 : if (!atom_full_copy_data (&elst->header, buffer, size, offset)) {
2181 : 0 : return 0;
2182 : : }
2183 : :
2184 : 0 : prop_copy_uint32 (g_slist_length (elst->entries), buffer, size, offset);
2185 : :
2186 [ # # ][ # # ]: 0 : for (walker = elst->entries; walker != NULL; walker = g_slist_next (walker)) {
2187 : 0 : EditListEntry *entry = (EditListEntry *) walker->data;
2188 : 0 : prop_copy_uint32 (entry->duration, buffer, size, offset);
2189 : 0 : prop_copy_uint32 (entry->media_time, buffer, size, offset);
2190 : 0 : prop_copy_uint32 (entry->media_rate, buffer, size, offset);
2191 : : }
2192 : 0 : atom_write_size (buffer, size, offset, original_offset);
2193 : 0 : return *offset - original_offset;
2194 : : }
2195 : :
2196 : : static guint64
2197 : 0 : atom_edts_copy_data (AtomEDTS * edts, guint8 ** buffer, guint64 * size,
2198 : : guint64 * offset)
2199 : : {
2200 : 0 : guint64 original_offset = *offset;
2201 : :
2202 [ # # ]: 0 : if (!atom_copy_data (&(edts->header), buffer, size, offset))
2203 : 0 : return 0;
2204 : :
2205 [ # # ]: 0 : if (!atom_elst_copy_data (&(edts->elst), buffer, size, offset))
2206 : 0 : return 0;
2207 : :
2208 : 0 : atom_write_size (buffer, size, offset, original_offset);
2209 : 0 : return *offset - original_offset;
2210 : : }
2211 : :
2212 : : guint64
2213 : 18 : atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
2214 : : guint64 * offset)
2215 : : {
2216 : 18 : guint64 original_offset = *offset;
2217 : :
2218 [ - + ]: 18 : if (!atom_copy_data (&trak->header, buffer, size, offset)) {
2219 : 0 : return 0;
2220 : : }
2221 [ - + ]: 18 : if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
2222 : 0 : return 0;
2223 : : }
2224 [ - + ]: 18 : if (trak->edts) {
2225 [ # # ]: 0 : if (!atom_edts_copy_data (trak->edts, buffer, size, offset)) {
2226 : 0 : return 0;
2227 : : }
2228 : : }
2229 : :
2230 [ - + ]: 18 : if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
2231 : 0 : return 0;
2232 : : }
2233 : :
2234 : 18 : atom_write_size (buffer, size, offset, original_offset);
2235 : 18 : return *offset - original_offset;
2236 : : }
2237 : :
2238 : : static guint64
2239 : 8 : atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
2240 : : guint64 * offset)
2241 : : {
2242 : 8 : guint64 original_offset = *offset;
2243 : :
2244 [ - + ]: 8 : if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
2245 : 0 : return 0;
2246 : : }
2247 : :
2248 : 8 : prop_copy_uint32 (data->reserved, buffer, size, offset);
2249 : 8 : prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
2250 : :
2251 : 8 : atom_write_size (buffer, size, offset, original_offset);
2252 : 8 : return *offset - original_offset;
2253 : : }
2254 : :
2255 : : static guint64
2256 : 8 : atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
2257 : : guint64 * offset)
2258 : : {
2259 : 8 : guint64 original_offset = *offset;
2260 : :
2261 [ - + ]: 8 : if (!atom_copy_data (&tag->header, buffer, size, offset)) {
2262 : 0 : return 0;
2263 : : }
2264 : :
2265 [ - + ]: 8 : if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
2266 : 0 : return 0;
2267 : : }
2268 : :
2269 : 8 : atom_write_size (buffer, size, offset, original_offset);
2270 : 8 : return *offset - original_offset;
2271 : : }
2272 : :
2273 : : static guint64
2274 : 4 : atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
2275 : : guint64 * offset)
2276 : : {
2277 : 4 : guint64 original_offset = *offset;
2278 : :
2279 [ - + ]: 4 : if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
2280 : 0 : return 0;
2281 : : }
2282 : : /* extra atoms */
2283 [ + + - + ]: 6 : if (ilst->entries &&
2284 : 2 : !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
2285 : 0 : return 0;
2286 : :
2287 : 4 : atom_write_size (buffer, size, offset, original_offset);
2288 : 4 : return *offset - original_offset;
2289 : : }
2290 : :
2291 : : static guint64
2292 : 4 : atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
2293 : : guint64 * offset)
2294 : : {
2295 : 4 : guint64 original_offset = *offset;
2296 : :
2297 [ - + ]: 4 : if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
2298 : 0 : return 0;
2299 : : }
2300 [ - + ]: 4 : if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
2301 : 0 : return 0;
2302 : : }
2303 [ + - ]: 4 : if (meta->ilst) {
2304 [ - + ]: 4 : if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
2305 : 0 : return 0;
2306 : : }
2307 : : }
2308 : :
2309 : 4 : atom_write_size (buffer, size, offset, original_offset);
2310 : 4 : return *offset - original_offset;
2311 : : }
2312 : :
2313 : : static guint64
2314 : 6 : atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
2315 : : guint64 * offset)
2316 : : {
2317 : 6 : guint64 original_offset = *offset;
2318 : :
2319 [ - + ]: 6 : if (!atom_copy_data (&udta->header, buffer, size, offset)) {
2320 : 0 : return 0;
2321 : : }
2322 [ + + ]: 6 : if (udta->meta) {
2323 [ - + ]: 4 : if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
2324 : 0 : return 0;
2325 : : }
2326 : : }
2327 [ + + ]: 6 : if (udta->entries) {
2328 : : /* extra atoms */
2329 [ - + ]: 5 : if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
2330 : 0 : return 0;
2331 : : }
2332 : :
2333 : 6 : atom_write_size (buffer, size, offset, original_offset);
2334 : 6 : return *offset - original_offset;
2335 : : }
2336 : :
2337 : : static guint64
2338 : 6 : atom_mehd_copy_data (AtomMEHD * mehd, guint8 ** buffer, guint64 * size,
2339 : : guint64 * offset)
2340 : : {
2341 : 6 : guint64 original_offset = *offset;
2342 : :
2343 [ - + ]: 6 : if (!atom_full_copy_data (&mehd->header, buffer, size, offset)) {
2344 : 0 : return 0;
2345 : : }
2346 : :
2347 : 6 : prop_copy_uint64 (mehd->fragment_duration, buffer, size, offset);
2348 : :
2349 : 6 : atom_write_size (buffer, size, offset, original_offset);
2350 : 6 : return *offset - original_offset;
2351 : : }
2352 : :
2353 : : static guint64
2354 : 6 : atom_trex_copy_data (AtomTREX * trex, guint8 ** buffer, guint64 * size,
2355 : : guint64 * offset)
2356 : : {
2357 : 6 : guint64 original_offset = *offset;
2358 : :
2359 [ - + ]: 6 : if (!atom_full_copy_data (&trex->header, buffer, size, offset)) {
2360 : 0 : return 0;
2361 : : }
2362 : :
2363 : 6 : prop_copy_uint32 (trex->track_ID, buffer, size, offset);
2364 : 6 : prop_copy_uint32 (trex->default_sample_description_index, buffer, size,
2365 : : offset);
2366 : 6 : prop_copy_uint32 (trex->default_sample_duration, buffer, size, offset);
2367 : 6 : prop_copy_uint32 (trex->default_sample_size, buffer, size, offset);
2368 : 6 : prop_copy_uint32 (trex->default_sample_flags, buffer, size, offset);
2369 : :
2370 : 6 : atom_write_size (buffer, size, offset, original_offset);
2371 : 6 : return *offset - original_offset;
2372 : : }
2373 : :
2374 : : static guint64
2375 : 6 : atom_mvex_copy_data (AtomMVEX * mvex, guint8 ** buffer, guint64 * size,
2376 : : guint64 * offset)
2377 : : {
2378 : 6 : guint64 original_offset = *offset;
2379 : : GList *walker;
2380 : :
2381 [ - + ]: 6 : if (!atom_copy_data (&mvex->header, buffer, size, offset)) {
2382 : 0 : return 0;
2383 : : }
2384 : :
2385 [ - + ]: 6 : if (!atom_mehd_copy_data (&mvex->mehd, buffer, size, offset)) {
2386 : 0 : return 0;
2387 : : }
2388 : :
2389 : 6 : walker = g_list_first (mvex->trexs);
2390 [ + + ]: 12 : while (walker != NULL) {
2391 [ - + ]: 6 : if (!atom_trex_copy_data ((AtomTREX *) walker->data, buffer, size, offset)) {
2392 : 0 : return 0;
2393 : : }
2394 [ + - ]: 6 : walker = g_list_next (walker);
2395 : : }
2396 : :
2397 : 6 : atom_write_size (buffer, size, offset, original_offset);
2398 : 6 : return *offset - original_offset;
2399 : : }
2400 : :
2401 : : guint64
2402 : 18 : atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
2403 : : guint64 * offset)
2404 : : {
2405 : 18 : guint64 original_offset = *offset;
2406 : : GList *walker;
2407 : :
2408 [ - + ]: 18 : if (!atom_copy_data (&(atom->header), buffer, size, offset))
2409 : 0 : return 0;
2410 : :
2411 [ - + ]: 18 : if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
2412 : 0 : return 0;
2413 : :
2414 : 18 : walker = g_list_first (atom->traks);
2415 [ + + ]: 36 : while (walker != NULL) {
2416 [ - + ]: 18 : if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
2417 : 0 : return 0;
2418 : : }
2419 [ + - ]: 18 : walker = g_list_next (walker);
2420 : : }
2421 : :
2422 [ + + ]: 18 : if (atom->udta) {
2423 [ - + ]: 6 : if (!atom_udta_copy_data (atom->udta, buffer, size, offset)) {
2424 : 0 : return 0;
2425 : : }
2426 : : }
2427 : :
2428 [ + + ]: 18 : if (atom->fragmented) {
2429 [ - + ]: 6 : if (!atom_mvex_copy_data (&atom->mvex, buffer, size, offset)) {
2430 : 0 : return 0;
2431 : : }
2432 : : }
2433 : :
2434 : 18 : atom_write_size (buffer, size, offset, original_offset);
2435 : 18 : return *offset - original_offset;
2436 : : }
2437 : :
2438 : : static guint64
2439 : 0 : atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
2440 : : guint64 * size, guint64 * offset)
2441 : : {
2442 : 0 : guint64 original_offset = *offset;
2443 : :
2444 [ # # ]: 0 : if (!atom_copy_data (&(wave->header), buffer, size, offset))
2445 : 0 : return 0;
2446 : :
2447 [ # # ]: 0 : if (wave->extension_atoms) {
2448 [ # # ]: 0 : if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
2449 : 0 : return 0;
2450 : : }
2451 : :
2452 : 0 : atom_write_size (buffer, size, offset, original_offset);
2453 : 0 : return *offset - original_offset;
2454 : : }
2455 : :
2456 : : /* -- end of copy data functions -- */
2457 : :
2458 : : /* -- general functions, API and support functions */
2459 : :
2460 : : /* add samples to tables */
2461 : :
2462 : : static void
2463 : 12 : atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
2464 : : {
2465 : : STSCEntry nentry;
2466 : : gint len;
2467 : :
2468 [ - + ][ # # ]: 12 : if ((len = atom_array_get_len (&stsc->entries)) &&
2469 : 0 : ((atom_array_index (&stsc->entries, len - 1)).samples_per_chunk ==
2470 : : nsamples))
2471 : 12 : return;
2472 : :
2473 : 12 : nentry.first_chunk = first_chunk;
2474 : 12 : nentry.samples_per_chunk = nsamples;
2475 : 12 : nentry.sample_description_index = 1;
2476 [ - + ][ - + ]: 12 : atom_array_append (&stsc->entries, nentry, 128);
2477 : : }
2478 : :
2479 : : static void
2480 : 12 : atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
2481 : : {
2482 : 12 : STTSEntry *entry = NULL;
2483 : :
2484 [ - + ]: 12 : if (G_LIKELY (atom_array_get_len (&stts->entries) != 0))
2485 : 0 : entry = &atom_array_index (&stts->entries,
2486 : : atom_array_get_len (&stts->entries) - 1);
2487 : :
2488 [ - + ][ # # ]: 12 : if (entry && entry->sample_delta == sample_delta) {
2489 : 0 : entry->sample_count += sample_count;
2490 : : } else {
2491 : : STTSEntry nentry;
2492 : :
2493 : 12 : nentry.sample_count = sample_count;
2494 : 12 : nentry.sample_delta = sample_delta;
2495 [ - + ][ - + ]: 12 : atom_array_append (&stts->entries, nentry, 256);
2496 : : }
2497 : 12 : }
2498 : :
2499 : : static void
2500 : 12 : atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
2501 : : {
2502 : : guint32 i;
2503 : :
2504 : 12 : stsz->table_size += nsamples;
2505 [ - + ]: 12 : if (stsz->sample_size != 0) {
2506 : : /* it is constant size, we don't need entries */
2507 : 12 : return;
2508 : : }
2509 [ + + ]: 24 : for (i = 0; i < nsamples; i++) {
2510 [ - + ][ - + ]: 12 : atom_array_append (&stsz->entries, size, 1024);
2511 : : }
2512 : : }
2513 : :
2514 : : static guint32
2515 : 12 : atom_stco64_get_entry_count (AtomSTCO64 * stco64)
2516 : : {
2517 : 12 : return atom_array_get_len (&stco64->entries);
2518 : : }
2519 : :
2520 : : static void
2521 : 12 : atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
2522 : : {
2523 [ - + ][ - + ]: 12 : atom_array_append (&stco64->entries, entry, 256);
2524 [ - + ]: 12 : if (entry > G_MAXUINT32)
2525 : 0 : stco64->header.header.type = FOURCC_co64;
2526 : 12 : }
2527 : :
2528 : : static void
2529 : 11 : atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
2530 : : {
2531 [ - + ][ - + ]: 11 : atom_array_append (&stss->entries, sample, 512);
2532 : 11 : }
2533 : :
2534 : : static void
2535 : 11 : atom_stbl_add_stss_entry (AtomSTBL * stbl)
2536 : : {
2537 : 11 : guint32 sample_index = stbl->stsz.table_size;
2538 : :
2539 : 11 : atom_stss_add_entry (&stbl->stss, sample_index);
2540 : 11 : }
2541 : :
2542 : : static void
2543 : 12 : atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
2544 : : {
2545 : 12 : CTTSEntry *entry = NULL;
2546 : :
2547 [ - + ]: 12 : if (G_LIKELY (atom_array_get_len (&ctts->entries) != 0))
2548 : 0 : entry = &atom_array_index (&ctts->entries,
2549 : : atom_array_get_len (&ctts->entries) - 1);
2550 : :
2551 [ - + ][ # # ]: 24 : if (entry == NULL || entry->sampleoffset != offset) {
2552 : : CTTSEntry nentry;
2553 : :
2554 : 12 : nentry.samplecount = nsamples;
2555 : 12 : nentry.sampleoffset = offset;
2556 [ - + ][ - + ]: 12 : atom_array_append (&ctts->entries, nentry, 256);
2557 [ - + ]: 12 : if (offset != 0)
2558 : 0 : ctts->do_pts = TRUE;
2559 : : } else {
2560 : 0 : entry->samplecount += nsamples;
2561 : : }
2562 : 12 : }
2563 : :
2564 : : static void
2565 : 12 : atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
2566 : : {
2567 [ + - ]: 12 : if (stbl->ctts == NULL) {
2568 : 12 : stbl->ctts = atom_ctts_new ();
2569 : : }
2570 : 12 : atom_ctts_add_entry (stbl->ctts, nsamples, offset);
2571 : 12 : }
2572 : :
2573 : : void
2574 : 12 : atom_stbl_add_samples (AtomSTBL * stbl, guint32 nsamples, guint32 delta,
2575 : : guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
2576 : : {
2577 : 12 : atom_stts_add_entry (&stbl->stts, nsamples, delta);
2578 : 12 : atom_stsz_add_entry (&stbl->stsz, nsamples, size);
2579 : 12 : atom_stco64_add_entry (&stbl->stco64, chunk_offset);
2580 : 12 : atom_stsc_add_new_entry (&stbl->stsc,
2581 : : atom_stco64_get_entry_count (&stbl->stco64), nsamples);
2582 [ + + ]: 12 : if (sync)
2583 : 11 : atom_stbl_add_stss_entry (stbl);
2584 : : /* always store to arrange for consistent content */
2585 : 12 : atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
2586 : 12 : }
2587 : :
2588 : : void
2589 : 12 : atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
2590 : : guint32 size, guint64 chunk_offset, gboolean sync, gint64 pts_offset)
2591 : : {
2592 : 12 : AtomSTBL *stbl = &trak->mdia.minf.stbl;
2593 : 12 : atom_stbl_add_samples (stbl, nsamples, delta, size, chunk_offset, sync,
2594 : : pts_offset);
2595 : 12 : }
2596 : :
2597 : : /* trak and moov molding */
2598 : :
2599 : : guint32
2600 : 40 : atom_trak_get_timescale (AtomTRAK * trak)
2601 : : {
2602 : 40 : return trak->mdia.mdhd.time_info.timescale;
2603 : : }
2604 : :
2605 : : guint32
2606 : 10 : atom_trak_get_id (AtomTRAK * trak)
2607 : : {
2608 : 10 : return trak->tkhd.track_ID;
2609 : : }
2610 : :
2611 : : static void
2612 : 33 : atom_trak_set_id (AtomTRAK * trak, guint32 id)
2613 : : {
2614 : 33 : trak->tkhd.track_ID = id;
2615 : 33 : }
2616 : :
2617 : : static void
2618 : 33 : atom_moov_add_trex (AtomMOOV * moov, AtomTREX * trex)
2619 : : {
2620 : 33 : moov->mvex.trexs = g_list_append (moov->mvex.trexs, trex);
2621 : 33 : }
2622 : :
2623 : : static AtomTREX *
2624 : 33 : atom_trex_new (AtomTRAK * trak)
2625 : : {
2626 : 33 : guint8 flags[3] = { 0, 0, 0 };
2627 : 33 : AtomTREX *trex = g_new0 (AtomTREX, 1);
2628 : :
2629 : 33 : atom_full_init (&trex->header, FOURCC_trex, 0, 0, 0, flags);
2630 : :
2631 : 33 : trex->track_ID = trak->tkhd.track_ID;
2632 : 33 : trex->default_sample_description_index = 1;
2633 : 33 : trex->default_sample_duration = 0;
2634 : 33 : trex->default_sample_size = 0;
2635 : 33 : trex->default_sample_flags = 0;
2636 : :
2637 : 33 : return trex;
2638 : : }
2639 : :
2640 : : void
2641 : 33 : atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
2642 : : {
2643 : 33 : atom_trak_set_id (trak, moov->mvhd.next_track_id++);
2644 : 33 : moov->traks = g_list_append (moov->traks, trak);
2645 : : /* additional trak means also new trex */
2646 : 33 : atom_moov_add_trex (moov, atom_trex_new (trak));
2647 : 33 : }
2648 : :
2649 : : static guint64
2650 : 16 : atom_trak_get_duration (AtomTRAK * trak)
2651 : : {
2652 : 16 : return trak->tkhd.duration;
2653 : : }
2654 : :
2655 : : static guint64
2656 : 16 : atom_stts_get_total_duration (AtomSTTS * stts)
2657 : : {
2658 : : guint i;
2659 : 16 : guint64 sum = 0;
2660 : :
2661 [ + + ]: 28 : for (i = 0; i < atom_array_get_len (&stts->entries); i++) {
2662 : 12 : STTSEntry *entry = &atom_array_index (&stts->entries, i);
2663 : :
2664 : 12 : sum += (guint64) (entry->sample_count) * entry->sample_delta;
2665 : : }
2666 : 16 : return sum;
2667 : : }
2668 : :
2669 : : static void
2670 : 16 : atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
2671 : : {
2672 : 16 : trak->mdia.mdhd.time_info.duration =
2673 : 16 : atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
2674 [ + - ]: 16 : if (trak->mdia.mdhd.time_info.timescale != 0) {
2675 : 16 : trak->tkhd.duration =
2676 : 16 : gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration,
2677 : 16 : moov_timescale, trak->mdia.mdhd.time_info.timescale);
2678 : : } else {
2679 : 0 : trak->tkhd.duration = 0;
2680 : : }
2681 : 16 : }
2682 : :
2683 : : static guint32
2684 : 16 : atom_moov_get_timescale (AtomMOOV * moov)
2685 : : {
2686 : 16 : return moov->mvhd.time_info.timescale;
2687 : : }
2688 : :
2689 : : void
2690 : 16 : atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
2691 : : {
2692 : 16 : moov->mvhd.time_info.timescale = timescale;
2693 : 16 : }
2694 : :
2695 : : void
2696 : 16 : atom_moov_update_duration (AtomMOOV * moov)
2697 : : {
2698 : 16 : GList *traks = moov->traks;
2699 : 16 : guint64 dur, duration = 0;
2700 : :
2701 [ + + ]: 32 : while (traks) {
2702 : 16 : AtomTRAK *trak = (AtomTRAK *) traks->data;
2703 : :
2704 : 16 : atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
2705 : 16 : dur = atom_trak_get_duration (trak);
2706 [ + + ]: 16 : if (dur > duration)
2707 : 3 : duration = dur;
2708 [ + - ]: 16 : traks = g_list_next (traks);
2709 : : }
2710 : 16 : moov->mvhd.time_info.duration = duration;
2711 : 16 : moov->mvex.mehd.fragment_duration = duration;
2712 : 16 : }
2713 : :
2714 : : void
2715 : 16 : atom_moov_set_fragmented (AtomMOOV * moov, gboolean fragmented)
2716 : : {
2717 : 16 : moov->fragmented = fragmented;
2718 : 16 : }
2719 : :
2720 : : void
2721 : 12 : atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset)
2722 : : {
2723 : : guint i;
2724 : :
2725 [ + + ]: 24 : for (i = 0; i < atom_array_get_len (&stco64->entries); i++) {
2726 : 12 : guint64 *value = &atom_array_index (&stco64->entries, i);
2727 : :
2728 : 12 : *value += offset;
2729 : : }
2730 : 12 : }
2731 : :
2732 : : void
2733 : 12 : atom_moov_chunks_add_offset (AtomMOOV * moov, guint32 offset)
2734 : : {
2735 : 12 : GList *traks = moov->traks;
2736 : :
2737 [ + + ]: 24 : while (traks) {
2738 : 12 : AtomTRAK *trak = (AtomTRAK *) traks->data;
2739 : :
2740 : 12 : atom_stco64_chunks_add_offset (&trak->mdia.minf.stbl.stco64, offset);
2741 [ + - ]: 12 : traks = g_list_next (traks);
2742 : : }
2743 : 12 : }
2744 : :
2745 : : /*
2746 : : * Meta tags functions
2747 : : */
2748 : : static void
2749 : 16 : atom_moov_init_metatags (AtomMOOV * moov, AtomsContext * context)
2750 : : {
2751 [ + + ]: 16 : if (!moov->udta) {
2752 : 6 : moov->udta = atom_udta_new ();
2753 : : }
2754 [ + + ]: 16 : if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
2755 [ + + ]: 11 : if (!moov->udta->meta) {
2756 : 4 : moov->udta->meta = atom_meta_new ();
2757 : : }
2758 [ + + ]: 11 : if (!moov->udta->meta->ilst) {
2759 : 4 : moov->udta->meta->ilst = atom_ilst_new ();
2760 : : }
2761 : : }
2762 : 16 : }
2763 : :
2764 : : static void
2765 : 8 : atom_tag_data_alloc_data (AtomTagData * data, guint size)
2766 : : {
2767 [ - + ]: 8 : if (data->data != NULL) {
2768 : 0 : g_free (data->data);
2769 : : }
2770 : 8 : data->data = g_new0 (guint8, size);
2771 : 8 : data->datalen = size;
2772 : 8 : }
2773 : :
2774 : : static void
2775 : 13 : atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
2776 : : {
2777 : : GList **entries;
2778 : :
2779 : 13 : atom_moov_init_metatags (moov, &moov->context);
2780 [ + + ]: 13 : if (moov->udta->meta)
2781 : 8 : entries = &moov->udta->meta->ilst->entries;
2782 : : else
2783 : 5 : entries = &moov->udta->entries;
2784 : 13 : *entries = g_list_append (*entries, tag);
2785 : 13 : }
2786 : :
2787 : : void
2788 : 8 : atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2789 : : const guint8 * data, guint size)
2790 : : {
2791 : : AtomTag *tag;
2792 : : AtomTagData *tdata;
2793 : :
2794 : 8 : tag = atom_tag_new (fourcc, flags);
2795 : 8 : tdata = &tag->data;
2796 : 8 : atom_tag_data_alloc_data (tdata, size);
2797 : 8 : g_memmove (tdata->data, data, size);
2798 : :
2799 : 8 : atom_moov_append_tag (moov,
2800 : : build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
2801 : : atom_tag_free));
2802 : 8 : }
2803 : :
2804 : : void
2805 : 8 : atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2806 : : {
2807 : 8 : gint len = strlen (value);
2808 : :
2809 [ + - ]: 8 : if (len > 0)
2810 : 8 : atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
2811 : 8 : }
2812 : :
2813 : : void
2814 : 0 : atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2815 : : guint32 value)
2816 : : {
2817 : 0 : guint8 data[8] = { 0, };
2818 : :
2819 [ # # ]: 0 : if (flags) {
2820 : 0 : GST_WRITE_UINT16_BE (data, value);
2821 : 0 : atom_moov_add_tag (moov, fourcc, flags, data, 2);
2822 : : } else {
2823 : 0 : GST_WRITE_UINT32_BE (data + 2, value);
2824 : 0 : atom_moov_add_tag (moov, fourcc, flags, data, 8);
2825 : : }
2826 : 0 : }
2827 : :
2828 : : void
2829 : 0 : atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
2830 : : {
2831 : : AtomData *data_atom;
2832 : : GstBuffer *buf;
2833 : : guint len;
2834 : : guint32 fourcc;
2835 : :
2836 [ # # ]: 0 : if (size < 8)
2837 : 0 : return;
2838 : :
2839 : : /* blob is unparsed atom;
2840 : : * extract size and fourcc, and wrap remainder in data atom */
2841 : 0 : len = GST_READ_UINT32_BE (data);
2842 : 0 : fourcc = GST_READ_UINT32_LE (data + 4);
2843 [ # # ]: 0 : if (len > size)
2844 : 0 : return;
2845 : :
2846 : 0 : buf = gst_buffer_new ();
2847 : 0 : GST_BUFFER_SIZE (buf) = len - 8;
2848 : 0 : GST_BUFFER_DATA (buf) = data + 8;
2849 : :
2850 : 0 : data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2851 : 0 : gst_buffer_unref (buf);
2852 : :
2853 : 0 : atom_moov_append_tag (moov,
2854 : : build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2855 : : atom_data_free));
2856 : : }
2857 : :
2858 : : void
2859 : 5 : atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
2860 : : guint size)
2861 : : {
2862 : : AtomData *data_atom;
2863 : : GstBuffer *buf;
2864 : : guint8 *bdata;
2865 : :
2866 : : /* need full atom */
2867 : 5 : buf = gst_buffer_new_and_alloc (size + 4);
2868 : 5 : bdata = GST_BUFFER_DATA (buf);
2869 : : /* full atom: version and flags */
2870 : 5 : GST_WRITE_UINT32_BE (bdata, 0);
2871 : 5 : memcpy (bdata + 4, data, size);
2872 : :
2873 : 5 : data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2874 : 5 : gst_buffer_unref (buf);
2875 : :
2876 : 5 : atom_moov_append_tag (moov,
2877 : : build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2878 : : atom_data_free));
2879 : 5 : }
2880 : :
2881 : : guint16
2882 : 10 : language_code (const char *lang)
2883 : : {
2884 [ - + ]: 10 : g_return_val_if_fail (lang != NULL, 0);
2885 [ - + ]: 10 : g_return_val_if_fail (strlen (lang) == 3, 0);
2886 : :
2887 : 10 : return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
2888 : 10 : ((lang[2] - 0x60) & 0x1F);
2889 : : }
2890 : :
2891 : : void
2892 : 3 : atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc,
2893 : : const gchar * value, gint16 ivalue)
2894 : : {
2895 : 3 : gint len = 0, size = 0;
2896 : : guint8 *data;
2897 : :
2898 [ + - ]: 3 : if (value) {
2899 : 3 : len = strlen (value);
2900 : 3 : size = len + 3;
2901 : : }
2902 : :
2903 [ - + ]: 3 : if (ivalue >= 0)
2904 : 0 : size += 2;
2905 : :
2906 : 3 : data = g_malloc (size + 3);
2907 : : /* language tag and null-terminated UTF-8 string */
2908 [ + - ]: 3 : if (value) {
2909 : 3 : GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
2910 : : /* include 0 terminator */
2911 : 3 : memcpy (data + 2, value, len + 1);
2912 : : }
2913 : : /* 16-bit unsigned int if standalone, otherwise 8-bit */
2914 [ - + ]: 3 : if (ivalue >= 0) {
2915 [ # # ]: 0 : if (size == 2)
2916 : 0 : GST_WRITE_UINT16_BE (data + size - 2, ivalue);
2917 : : else {
2918 : 0 : GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
2919 : 0 : size--;
2920 : : }
2921 : : }
2922 : :
2923 : 3 : atom_moov_add_3gp_tag (moov, fourcc, data, size);
2924 : 3 : g_free (data);
2925 : 3 : }
2926 : :
2927 : : void
2928 : 3 : atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2929 : : {
2930 : 3 : atom_moov_add_3gp_str_int_tag (moov, fourcc, value, -1);
2931 : 3 : }
2932 : :
2933 : : void
2934 : 0 : atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value)
2935 : : {
2936 : 0 : atom_moov_add_3gp_str_int_tag (moov, fourcc, NULL, value);
2937 : 0 : }
2938 : :
2939 : : void
2940 : 3 : atom_moov_add_xmp_tags (AtomMOOV * moov, const GstTagList * tags)
2941 : : {
2942 : : GstBuffer *xmpbuffer;
2943 : 3 : AtomData *data_atom = NULL;
2944 : :
2945 [ + - ]: 3 : if (moov->context.flavor == ATOMS_TREE_FLAVOR_MOV) {
2946 : 3 : xmpbuffer = gst_tag_list_to_xmp_buffer (tags, TRUE);
2947 [ + - ]: 3 : if (xmpbuffer) {
2948 : 3 : data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
2949 : 3 : atom_moov_init_metatags (moov, &moov->context);
2950 : 3 : moov->udta->entries = g_list_append (moov->udta->entries,
2951 : 3 : build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2952 : : atom_data_free));
2953 : 3 : gst_buffer_unref (xmpbuffer);
2954 : : }
2955 : : } else {
2956 [ # # ]: 0 : GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
2957 : : }
2958 : :
2959 : 3 : }
2960 : :
2961 : : /*
2962 : : * Functions for specifying media types
2963 : : */
2964 : :
2965 : : static void
2966 : 3 : atom_minf_set_audio (AtomMINF * minf)
2967 : : {
2968 : 3 : atom_minf_clear_handlers (minf);
2969 : 3 : minf->smhd = atom_smhd_new ();
2970 : 3 : }
2971 : :
2972 : : static void
2973 : 13 : atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
2974 : : {
2975 : 13 : atom_minf_clear_handlers (minf);
2976 : 13 : minf->vmhd = atom_vmhd_new (context);
2977 : 13 : }
2978 : :
2979 : : static void
2980 : 16 : atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
2981 : : guint32 hdlr_type)
2982 : : {
2983 [ + + ]: 16 : if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
2984 : 10 : hdlr->component_type = comp_type;
2985 : : }
2986 : 16 : hdlr->handler_type = hdlr_type;
2987 : 16 : }
2988 : :
2989 : : static void
2990 : 16 : atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
2991 : : {
2992 [ + - ]: 16 : if (hdlr->name)
2993 : 16 : g_free (hdlr->name);
2994 : 16 : hdlr->name = g_strdup (name);
2995 : 16 : }
2996 : :
2997 : : static void
2998 : 3 : atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
2999 : : {
3000 : 3 : atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3001 : : /* Some players (low-end hardware) check for this name, which is what
3002 : : * QuickTime itself sets */
3003 : 3 : atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3004 : 3 : }
3005 : :
3006 : : static void
3007 : 13 : atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3008 : : {
3009 : 13 : atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3010 : : /* Some players (low-end hardware) check for this name, which is what
3011 : : * QuickTime itself sets */
3012 : 13 : atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3013 : 13 : }
3014 : :
3015 : : static void
3016 : 3 : atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3017 : : {
3018 : 3 : atom_mdia_set_hdlr_type_audio (mdia, context);
3019 : 3 : atom_minf_set_audio (&mdia->minf);
3020 : 3 : }
3021 : :
3022 : : static void
3023 : 13 : atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3024 : : {
3025 : 13 : atom_mdia_set_hdlr_type_video (mdia, context);
3026 : 13 : atom_minf_set_video (&mdia->minf, context);
3027 : 13 : }
3028 : :
3029 : : static void
3030 : 3 : atom_tkhd_set_audio (AtomTKHD * tkhd)
3031 : : {
3032 : 3 : tkhd->volume = 0x0100;
3033 : 3 : tkhd->width = tkhd->height = 0;
3034 : 3 : }
3035 : :
3036 : : static void
3037 : 13 : atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3038 : : guint32 height)
3039 : : {
3040 : 13 : tkhd->volume = 0;
3041 : :
3042 : : /* qt and ISO base media do not contradict, and examples agree */
3043 : 13 : tkhd->width = width;
3044 : 13 : tkhd->height = height;
3045 : 13 : }
3046 : :
3047 : : static void
3048 : 0 : atom_edts_add_entry (AtomEDTS * edts, EditListEntry * entry)
3049 : : {
3050 : 0 : edts->elst.entries = g_slist_append (edts->elst.entries, entry);
3051 : 0 : }
3052 : :
3053 : : /*
3054 : : * Adds a new entry to this trak edits list
3055 : : * duration is in the moov's timescale
3056 : : * media_time is the offset in the media time to start from (media's timescale)
3057 : : * rate is a 32 bits fixed-point
3058 : : */
3059 : : void
3060 : 0 : atom_trak_add_elst_entry (AtomTRAK * trak, guint32 duration, guint32 media_time,
3061 : : guint32 rate)
3062 : : {
3063 : 0 : EditListEntry *entry = g_new (EditListEntry, 1);
3064 : :
3065 : 0 : entry->duration = duration;
3066 : 0 : entry->media_time = media_time;
3067 : 0 : entry->media_rate = rate;
3068 : :
3069 [ # # ]: 0 : if (trak->edts == NULL) {
3070 : 0 : trak->edts = atom_edts_new ();
3071 : : }
3072 : 0 : atom_edts_add_entry (trak->edts, entry);
3073 : 0 : }
3074 : :
3075 : : /* re-negotiation is prevented at top-level, so only 1 entry expected.
3076 : : * Quite some more care here and elsewhere may be needed to
3077 : : * support several entries */
3078 : : static SampleTableEntryMP4A *
3079 : 3 : atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
3080 : : guint32 type)
3081 : : {
3082 : 3 : AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3083 : 3 : SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
3084 : :
3085 : 3 : mp4a->se.header.type = type;
3086 : 3 : mp4a->se.kind = AUDIO;
3087 : 3 : mp4a->compression_id = -1;
3088 : 3 : mp4a->se.data_reference_index = 1;
3089 : :
3090 : 3 : stsd->entries = g_list_prepend (stsd->entries, mp4a);
3091 : 3 : stsd->n_entries++;
3092 : 3 : return mp4a;
3093 : : }
3094 : :
3095 : : static SampleTableEntryMP4V *
3096 : 13 : atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
3097 : : guint32 type)
3098 : : {
3099 : 13 : SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
3100 : 13 : AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3101 : :
3102 : 13 : mp4v->se.header.type = type;
3103 : 13 : mp4v->se.kind = VIDEO;
3104 : 13 : mp4v->se.data_reference_index = 1;
3105 : 13 : mp4v->horizontal_resolution = 72 << 16;
3106 : 13 : mp4v->vertical_resolution = 72 << 16;
3107 [ + + ]: 13 : if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3108 : 7 : mp4v->spatial_quality = 512;
3109 : 7 : mp4v->temporal_quality = 512;
3110 : : }
3111 : :
3112 : 13 : stsd->entries = g_list_prepend (stsd->entries, mp4v);
3113 : 13 : stsd->n_entries++;
3114 : 13 : return mp4v;
3115 : : }
3116 : :
3117 : : static void
3118 : 3 : atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
3119 : : {
3120 : 3 : trak->mdia.minf.stbl.stsz.sample_size = sample_size;
3121 : 3 : }
3122 : :
3123 : : static void
3124 : 3 : atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
3125 : : {
3126 : 3 : atom_tkhd_set_audio (&trak->tkhd);
3127 : 3 : atom_mdia_set_audio (&trak->mdia, context);
3128 : 3 : }
3129 : :
3130 : : static void
3131 : 13 : atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
3132 : : guint32 height)
3133 : : {
3134 : 13 : atom_tkhd_set_video (&trak->tkhd, context, width, height);
3135 : 13 : atom_mdia_set_video (&trak->mdia, context);
3136 : 13 : }
3137 : :
3138 : : static void
3139 : 3 : atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
3140 : : guint32 rate)
3141 : : {
3142 : 3 : atom_trak_set_audio (trak, context);
3143 : 3 : trak->mdia.mdhd.time_info.timescale = rate;
3144 : 3 : }
3145 : :
3146 : : static void
3147 : 13 : atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
3148 : : guint32 rate, guint32 width, guint32 height)
3149 : : {
3150 : 13 : atom_trak_set_video (trak, context, width, height);
3151 : 13 : trak->mdia.mdhd.time_info.timescale = rate;
3152 : 13 : trak->tkhd.width = width << 16;
3153 : 13 : trak->tkhd.height = height << 16;
3154 : 13 : }
3155 : :
3156 : : void
3157 : 3 : atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
3158 : : AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
3159 : : {
3160 : : SampleTableEntryMP4A *ste;
3161 : :
3162 : 3 : atom_trak_set_audio_commons (trak, context, scale);
3163 : 3 : atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3164 : 3 : ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
3165 : :
3166 : 3 : trak->is_video = FALSE;
3167 : 3 : trak->is_h264 = FALSE;
3168 : :
3169 : 3 : ste->version = entry->version;
3170 : 3 : ste->compression_id = entry->compression_id;
3171 : 3 : ste->sample_size = entry->sample_size;
3172 : 3 : ste->sample_rate = entry->sample_rate << 16;
3173 : 3 : ste->channels = entry->channels;
3174 : :
3175 : 3 : ste->samples_per_packet = entry->samples_per_packet;
3176 : 3 : ste->bytes_per_sample = entry->bytes_per_sample;
3177 : 3 : ste->bytes_per_packet = entry->bytes_per_packet;
3178 : 3 : ste->bytes_per_frame = entry->bytes_per_frame;
3179 : :
3180 [ - + ]: 3 : if (ext)
3181 : 0 : ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
3182 : :
3183 : : /* 0 size means variable size */
3184 : 3 : atom_trak_set_constant_size_samples (trak, sample_size);
3185 : 3 : }
3186 : :
3187 : : static AtomInfo *
3188 : 0 : build_pasp_extension (AtomTRAK * trak, gint par_width, gint par_height)
3189 : : {
3190 : : AtomData *atom_data;
3191 : : GstBuffer *buf;
3192 : : guint8 *data;
3193 : :
3194 : 0 : buf = gst_buffer_new_and_alloc (8);
3195 : 0 : data = GST_BUFFER_DATA (buf);
3196 : :
3197 : : /* ihdr = image header box */
3198 : 0 : GST_WRITE_UINT32_BE (data, par_width);
3199 : 0 : GST_WRITE_UINT32_BE (data + 4, par_height);
3200 : :
3201 : 0 : atom_data = atom_data_new_from_gst_buffer (FOURCC_pasp, buf);
3202 : 0 : gst_buffer_unref (buf);
3203 : :
3204 : 0 : return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3205 : : atom_data_free);
3206 : : }
3207 : :
3208 : : void
3209 : 13 : atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
3210 : : VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
3211 : : {
3212 : : SampleTableEntryMP4V *ste;
3213 : : gint dwidth, dheight;
3214 : 13 : gint par_n = 0, par_d = 0;
3215 : :
3216 [ + - ][ - + ]: 13 : if ((entry->par_n != 1 || entry->par_d != 1) &&
[ # # ]
3217 : 0 : (entry->par_n != entry->par_d)) {
3218 : 0 : par_n = entry->par_n;
3219 : 0 : par_d = entry->par_d;
3220 : : }
3221 : :
3222 : 13 : dwidth = entry->width;
3223 : 13 : dheight = entry->height;
3224 : : /* ISO file spec says track header w/h indicates track's visual presentation
3225 : : * (so this together with pixels w/h implicitly defines PAR) */
3226 [ - + ][ # # ]: 13 : if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
3227 [ # # ]: 0 : if (par_n > par_d) {
3228 : 0 : dwidth = entry->width * par_n / par_d;
3229 : 0 : dheight = entry->height;
3230 : : } else {
3231 : 0 : dwidth = entry->width * par_n / par_d;
3232 : 0 : dheight = entry->height;
3233 : : }
3234 : : }
3235 : :
3236 : 13 : atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
3237 : 13 : atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3238 : 13 : ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
3239 : :
3240 : 13 : trak->is_video = TRUE;
3241 : 13 : trak->is_h264 = (entry->fourcc == FOURCC_avc1);
3242 : :
3243 : 13 : ste->version = entry->version;
3244 : 13 : ste->width = entry->width;
3245 : 13 : ste->height = entry->height;
3246 : 13 : ste->depth = entry->depth;
3247 : 13 : ste->color_table_id = entry->color_table_id;
3248 : 13 : ste->frame_count = entry->frame_count;
3249 : :
3250 [ + - ]: 13 : if (ext_atoms_list)
3251 : 13 : ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
3252 : :
3253 : : /* QT spec has a pasp extension atom in stsd that can hold PAR */
3254 [ - + ][ # # ]: 13 : if (par_n && (context->flavor == ATOMS_TREE_FLAVOR_MOV)) {
3255 : 0 : ste->extension_atoms = g_list_append (ste->extension_atoms,
3256 : 0 : build_pasp_extension (trak, par_n, par_d));
3257 : : }
3258 : 13 : }
3259 : :
3260 : : static void
3261 : 4 : atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
3262 : : {
3263 : 4 : guint8 flags[3] = { 0, 0, 0 };
3264 : :
3265 : 4 : atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
3266 : 4 : mfhd->sequence_number = sequence_number;
3267 : 4 : }
3268 : :
3269 : : static void
3270 : 4 : atom_moof_init (AtomMOOF * moof, AtomsContext * context,
3271 : : guint32 sequence_number)
3272 : : {
3273 : 4 : atom_header_set (&moof->header, FOURCC_moof, 0, 0);
3274 : 4 : atom_mfhd_init (&moof->mfhd, sequence_number);
3275 : 4 : moof->trafs = NULL;
3276 : 4 : }
3277 : :
3278 : : AtomMOOF *
3279 : 4 : atom_moof_new (AtomsContext * context, guint32 sequence_number)
3280 : : {
3281 : 4 : AtomMOOF *moof = g_new0 (AtomMOOF, 1);
3282 : :
3283 : 4 : atom_moof_init (moof, context, sequence_number);
3284 : 4 : return moof;
3285 : : }
3286 : :
3287 : : static void
3288 : 8 : atom_trun_free (AtomTRUN * trun)
3289 : : {
3290 : 8 : atom_full_clear (&trun->header);
3291 : 8 : atom_array_clear (&trun->entries);
3292 : 8 : g_free (trun);
3293 : 8 : }
3294 : :
3295 : : static void
3296 : 0 : atom_sdtp_free (AtomSDTP * sdtp)
3297 : : {
3298 : 0 : atom_full_clear (&sdtp->header);
3299 : 0 : atom_array_clear (&sdtp->entries);
3300 : 0 : g_free (sdtp);
3301 : 0 : }
3302 : :
3303 : : void
3304 : 8 : atom_traf_free (AtomTRAF * traf)
3305 : : {
3306 : : GList *walker;
3307 : :
3308 : 8 : walker = traf->truns;
3309 [ + + ]: 16 : while (walker) {
3310 : 8 : atom_trun_free ((AtomTRUN *) walker->data);
3311 [ + - ]: 8 : walker = g_list_next (walker);
3312 : : }
3313 : 8 : g_list_free (traf->truns);
3314 : 8 : traf->truns = NULL;
3315 : :
3316 : 8 : walker = traf->sdtps;
3317 [ - + ]: 8 : while (walker) {
3318 : 0 : atom_sdtp_free ((AtomSDTP *) walker->data);
3319 [ # # ]: 0 : walker = g_list_next (walker);
3320 : : }
3321 : 8 : g_list_free (traf->sdtps);
3322 : 8 : traf->sdtps = NULL;
3323 : :
3324 : 8 : g_free (traf);
3325 : 8 : }
3326 : :
3327 : : void
3328 : 4 : atom_moof_free (AtomMOOF * moof)
3329 : : {
3330 : : GList *walker;
3331 : :
3332 : 4 : walker = moof->trafs;
3333 [ + + ]: 8 : while (walker) {
3334 : 4 : atom_traf_free ((AtomTRAF *) walker->data);
3335 [ + - ]: 4 : walker = g_list_next (walker);
3336 : : }
3337 : 4 : g_list_free (moof->trafs);
3338 : 4 : moof->trafs = NULL;
3339 : :
3340 : 4 : g_free (moof);
3341 : 4 : }
3342 : :
3343 : : static guint64
3344 : 4 : atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
3345 : : guint64 * offset)
3346 : : {
3347 : 4 : guint64 original_offset = *offset;
3348 : :
3349 [ - + ]: 4 : if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
3350 : 0 : return 0;
3351 : : }
3352 : :
3353 : 4 : prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
3354 : :
3355 : 4 : atom_write_size (buffer, size, offset, original_offset);
3356 : 4 : return *offset - original_offset;
3357 : : }
3358 : :
3359 : : static guint64
3360 : 4 : atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
3361 : : guint64 * offset)
3362 : : {
3363 : 4 : guint64 original_offset = *offset;
3364 : : guint32 flags;
3365 : :
3366 [ - + ]: 4 : if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
3367 : 0 : return 0;
3368 : : }
3369 : :
3370 : 4 : prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
3371 : :
3372 : 4 : flags = atom_full_get_flags_as_uint (&tfhd->header);
3373 : :
3374 [ - + ]: 4 : if (flags & TF_BASE_DATA_OFFSET)
3375 : 0 : prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
3376 [ - + ]: 4 : if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
3377 : 0 : prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
3378 [ + - ]: 4 : if (flags & TF_DEFAULT_SAMPLE_DURATION)
3379 : 4 : prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
3380 [ + - ]: 4 : if (flags & TF_DEFAULT_SAMPLE_SIZE)
3381 : 4 : prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
3382 [ + - ]: 4 : if (flags & TF_DEFAULT_SAMPLE_FLAGS)
3383 : 4 : prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
3384 : :
3385 : 4 : atom_write_size (buffer, size, offset, original_offset);
3386 : 4 : return *offset - original_offset;
3387 : : }
3388 : :
3389 : : static guint64
3390 : 4 : atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
3391 : : guint64 * offset, guint32 * data_offset)
3392 : : {
3393 : 4 : guint64 original_offset = *offset;
3394 : : guint32 flags, i;
3395 : :
3396 : 4 : flags = atom_full_get_flags_as_uint (&trun->header);
3397 : :
3398 : : /* if first trun in moof, forcibly add data_offset and record
3399 : : * where it must be written later on */
3400 [ + - ][ + - ]: 4 : if (data_offset && !*data_offset) {
3401 : 4 : flags |= TR_DATA_OFFSET;
3402 : : } else {
3403 : 0 : flags &= ~TR_DATA_OFFSET;
3404 : : }
3405 : :
3406 : 4 : atom_full_set_flags_as_uint (&trun->header, flags);
3407 : :
3408 [ - + ]: 4 : if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
3409 : 0 : return 0;
3410 : : }
3411 : :
3412 : 4 : prop_copy_uint32 (trun->sample_count, buffer, size, offset);
3413 : :
3414 [ + - ]: 4 : if (flags & TR_DATA_OFFSET) {
3415 : 4 : *data_offset = *offset;
3416 : 4 : prop_copy_int32 (trun->data_offset, buffer, size, offset);
3417 : : }
3418 [ - + ]: 4 : if (flags & TR_FIRST_SAMPLE_FLAGS)
3419 : 0 : prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
3420 : :
3421 [ + + ]: 8 : for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
3422 : 4 : TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
3423 : :
3424 [ - + ]: 4 : if (flags & TR_SAMPLE_DURATION)
3425 : 0 : prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
3426 [ - + ]: 4 : if (flags & TR_SAMPLE_SIZE)
3427 : 0 : prop_copy_uint32 (entry->sample_size, buffer, size, offset);
3428 [ - + ]: 4 : if (flags & TR_SAMPLE_FLAGS)
3429 : 0 : prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
3430 [ - + ]: 4 : if (flags & TR_COMPOSITION_TIME_OFFSETS)
3431 : 0 : prop_copy_uint32 (entry->sample_composition_time_offset,
3432 : : buffer, size, offset);
3433 : : }
3434 : :
3435 : 4 : atom_write_size (buffer, size, offset, original_offset);
3436 : 4 : return *offset - original_offset;
3437 : : }
3438 : :
3439 : : static guint64
3440 : 0 : atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
3441 : : guint64 * offset)
3442 : : {
3443 : 0 : guint64 original_offset = *offset;
3444 : :
3445 [ # # ]: 0 : if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
3446 : 0 : return 0;
3447 : : }
3448 : :
3449 : : /* all entries at once */
3450 : 0 : prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
3451 : : atom_array_get_len (&sdtp->entries), buffer, size, offset);
3452 : :
3453 : 0 : atom_write_size (buffer, size, offset, original_offset);
3454 : 0 : return *offset - original_offset;
3455 : : }
3456 : :
3457 : : static guint64
3458 : 4 : atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
3459 : : guint64 * offset, guint32 * data_offset)
3460 : : {
3461 : 4 : guint64 original_offset = *offset;
3462 : : GList *walker;
3463 : :
3464 [ - + ]: 4 : if (!atom_copy_data (&traf->header, buffer, size, offset)) {
3465 : 0 : return 0;
3466 : : }
3467 [ - + ]: 4 : if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
3468 : 0 : return 0;
3469 : : }
3470 : :
3471 : 4 : walker = g_list_first (traf->truns);
3472 [ + + ]: 8 : while (walker != NULL) {
3473 [ - + ]: 4 : if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset,
3474 : : data_offset)) {
3475 : 0 : return 0;
3476 : : }
3477 [ + - ]: 4 : walker = g_list_next (walker);
3478 : : }
3479 : :
3480 : 4 : walker = g_list_first (traf->sdtps);
3481 [ - + ]: 4 : while (walker != NULL) {
3482 [ # # ]: 0 : if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
3483 : 0 : return 0;
3484 : : }
3485 [ # # ]: 0 : walker = g_list_next (walker);
3486 : : }
3487 : :
3488 : 4 : atom_write_size (buffer, size, offset, original_offset);
3489 : 4 : return *offset - original_offset;
3490 : : }
3491 : :
3492 : : /* creates moof atom; metadata is written expecting actual buffer data
3493 : : * is in mdata directly after moof, and is consecutively written per trak */
3494 : : guint64
3495 : 4 : atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
3496 : : guint64 * size, guint64 * offset)
3497 : : {
3498 : 4 : guint64 original_offset = *offset;
3499 : : GList *walker;
3500 : 4 : guint32 data_offset = 0;
3501 : :
3502 [ - + ]: 4 : if (!atom_copy_data (&moof->header, buffer, size, offset))
3503 : 0 : return 0;
3504 : :
3505 [ - + ]: 4 : if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
3506 : 0 : return 0;
3507 : :
3508 : 4 : walker = g_list_first (moof->trafs);
3509 [ + + ]: 8 : while (walker != NULL) {
3510 [ - + ]: 4 : if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset,
3511 : : &data_offset)) {
3512 : 0 : return 0;
3513 : : }
3514 [ + - ]: 4 : walker = g_list_next (walker);
3515 : : }
3516 : :
3517 : 4 : atom_write_size (buffer, size, offset, original_offset);
3518 : :
3519 [ + - ][ + - ]: 4 : if (*buffer && data_offset) {
3520 : : /* first trun needs a data-offset relative to moof start
3521 : : * = moof size + mdat prefix */
3522 : 4 : GST_WRITE_UINT32_BE (*buffer + data_offset, *offset - original_offset + 8);
3523 : : }
3524 : :
3525 : 4 : return *offset - original_offset;
3526 : : }
3527 : :
3528 : : static void
3529 : 8 : atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
3530 : : {
3531 : 8 : guint8 flags[3] = { 0, 0, 0 };
3532 : :
3533 : 8 : atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
3534 : 8 : tfhd->track_ID = track_ID;
3535 : 8 : tfhd->base_data_offset = 0;
3536 : 8 : tfhd->sample_description_index = 1;
3537 : 8 : tfhd->default_sample_duration = 0;
3538 : 8 : tfhd->default_sample_size = 0;
3539 : 8 : tfhd->default_sample_flags = 0;
3540 : 8 : }
3541 : :
3542 : : static void
3543 : 8 : atom_trun_init (AtomTRUN * trun)
3544 : : {
3545 : 8 : guint8 flags[3] = { 0, 0, 0 };
3546 : :
3547 : 8 : atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
3548 : 8 : trun->sample_count = 0;
3549 : 8 : trun->data_offset = 0;
3550 : 8 : trun->first_sample_flags = 0;
3551 : 8 : atom_array_init (&trun->entries, 512);
3552 : 8 : }
3553 : :
3554 : : static AtomTRUN *
3555 : 8 : atom_trun_new (void)
3556 : : {
3557 : 8 : AtomTRUN *trun = g_new0 (AtomTRUN, 1);
3558 : :
3559 : 8 : atom_trun_init (trun);
3560 : 8 : return trun;
3561 : : }
3562 : :
3563 : : static void
3564 : 0 : atom_sdtp_init (AtomSDTP * sdtp)
3565 : : {
3566 : 0 : guint8 flags[3] = { 0, 0, 0 };
3567 : :
3568 : 0 : atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
3569 : 0 : atom_array_init (&sdtp->entries, 512);
3570 : 0 : }
3571 : :
3572 : : static AtomSDTP *
3573 : 0 : atom_sdtp_new (AtomsContext * context)
3574 : : {
3575 : 0 : AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
3576 : :
3577 : 0 : atom_sdtp_init (sdtp);
3578 : 0 : return sdtp;
3579 : : }
3580 : :
3581 : : static void
3582 : 0 : atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
3583 : : {
3584 : 0 : traf->sdtps = g_list_append (traf->sdtps, sdtp);
3585 : 0 : }
3586 : :
3587 : : static void
3588 : 0 : atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
3589 : : {
3590 : : /* it does not make much/any sense according to specs,
3591 : : * but that's how MS isml samples seem to do it */
3592 [ # # ][ # # ]: 0 : atom_array_append (&sdtp->entries, val, 256);
3593 : 0 : }
3594 : :
3595 : : static void
3596 : 8 : atom_trun_add_samples (AtomTRUN * trun, guint32 delta, guint32 size,
3597 : : guint32 flags, gint64 pts_offset)
3598 : : {
3599 : : TRUNSampleEntry nentry;
3600 : :
3601 [ - + ]: 8 : if (pts_offset != 0)
3602 : 0 : trun->header.flags[1] |= TR_COMPOSITION_TIME_OFFSETS;
3603 : :
3604 : 8 : nentry.sample_duration = delta;
3605 : 8 : nentry.sample_size = size;
3606 : 8 : nentry.sample_flags = flags;
3607 : 8 : nentry.sample_composition_time_offset = pts_offset;
3608 [ - + ][ - + ]: 8 : atom_array_append (&trun->entries, nentry, 256);
3609 : 8 : trun->sample_count++;
3610 : 8 : }
3611 : :
3612 : : static void
3613 : 8 : atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
3614 : : {
3615 : 8 : atom_header_set (&traf->header, FOURCC_traf, 0, 0);
3616 : 8 : atom_tfhd_init (&traf->tfhd, track_ID);
3617 : 8 : traf->truns = NULL;
3618 : :
3619 [ - + ]: 8 : if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
3620 : 0 : atom_traf_add_sdtp (traf, atom_sdtp_new (context));
3621 : 8 : }
3622 : :
3623 : : AtomTRAF *
3624 : 8 : atom_traf_new (AtomsContext * context, guint32 track_ID)
3625 : : {
3626 : 8 : AtomTRAF *traf = g_new0 (AtomTRAF, 1);
3627 : :
3628 : 8 : atom_traf_init (traf, context, track_ID);
3629 : 8 : return traf;
3630 : : }
3631 : :
3632 : : static void
3633 : 8 : atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
3634 : : {
3635 : 8 : traf->truns = g_list_append (traf->truns, trun);
3636 : 8 : }
3637 : :
3638 : : void
3639 : 8 : atom_traf_add_samples (AtomTRAF * traf, guint32 delta, guint32 size,
3640 : : gboolean sync, gint64 pts_offset, gboolean sdtp_sync)
3641 : : {
3642 : : AtomTRUN *trun;
3643 : : guint32 flags;
3644 : :
3645 : : /* 0x10000 is sample-is-difference-sample flag
3646 : : * low byte stuff is what ismv uses */
3647 [ + - ][ + + ]: 8 : flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
3648 : :
3649 [ + - ]: 8 : if (G_UNLIKELY (!traf->truns)) {
3650 : 8 : trun = atom_trun_new ();
3651 : 8 : atom_traf_add_trun (traf, trun);
3652 : : /* optimistic; indicate all defaults present in tfhd */
3653 : 8 : traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
3654 : : TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
3655 : 8 : traf->tfhd.default_sample_duration = delta;
3656 : 8 : traf->tfhd.default_sample_size = size;
3657 : 8 : traf->tfhd.default_sample_flags = flags;
3658 : 8 : trun->first_sample_flags = flags;
3659 : : }
3660 : :
3661 : 8 : trun = traf->truns->data;
3662 : :
3663 : : /* check if still matching defaults,
3664 : : * if not, abandon default and need entry for each sample */
3665 [ - + ]: 8 : if (traf->tfhd.default_sample_duration != delta) {
3666 : 0 : traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
3667 : 0 : trun->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
3668 : : }
3669 [ - + ]: 8 : if (traf->tfhd.default_sample_size != size) {
3670 : 0 : traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
3671 : 0 : trun->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
3672 : : }
3673 [ - + ]: 8 : if (traf->tfhd.default_sample_flags != flags) {
3674 [ # # ]: 0 : if (trun->sample_count == 1) {
3675 : : /* at least will need first sample flag */
3676 : 0 : traf->tfhd.default_sample_flags = flags;
3677 : 0 : trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
3678 : : } else {
3679 : : /* now we need sample flags for each sample */
3680 : 0 : traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
3681 : 0 : trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
3682 : 0 : trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
3683 : : }
3684 : : }
3685 : :
3686 : 8 : atom_trun_add_samples (traf->truns->data, delta, size, flags, pts_offset);
3687 : :
3688 [ - + ]: 8 : if (traf->sdtps)
3689 : 0 : atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
3690 : 8 : }
3691 : :
3692 : : guint32
3693 : 4 : atom_traf_get_sample_num (AtomTRAF * traf)
3694 : : {
3695 : : AtomTRUN *trun;
3696 : :
3697 [ - + ]: 4 : if (G_UNLIKELY (!traf->truns))
3698 : 0 : return 0;
3699 : :
3700 : 4 : trun = traf->truns->data;
3701 : 4 : return atom_array_get_len (&trun->entries);
3702 : : }
3703 : :
3704 : : void
3705 : 4 : atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
3706 : : {
3707 : 4 : moof->trafs = g_list_append (moof->trafs, traf);
3708 : 4 : }
3709 : :
3710 : : static void
3711 : 2 : atom_tfra_free (AtomTFRA * tfra)
3712 : : {
3713 : 2 : atom_full_clear (&tfra->header);
3714 : 2 : atom_array_clear (&tfra->entries);
3715 : 2 : g_free (tfra);
3716 : 2 : }
3717 : :
3718 : : AtomMFRA *
3719 : 2 : atom_mfra_new (AtomsContext * context)
3720 : : {
3721 : 2 : AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
3722 : :
3723 : 2 : atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
3724 : 2 : return mfra;
3725 : : }
3726 : :
3727 : : void
3728 : 2 : atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
3729 : : {
3730 : 2 : mfra->tfras = g_list_append (mfra->tfras, tfra);
3731 : 2 : }
3732 : :
3733 : : void
3734 : 2 : atom_mfra_free (AtomMFRA * mfra)
3735 : : {
3736 : : GList *walker;
3737 : :
3738 : 2 : walker = mfra->tfras;
3739 [ + + ]: 4 : while (walker) {
3740 : 2 : atom_tfra_free ((AtomTFRA *) walker->data);
3741 [ + - ]: 2 : walker = g_list_next (walker);
3742 : : }
3743 : 2 : g_list_free (mfra->tfras);
3744 : 2 : mfra->tfras = NULL;
3745 : :
3746 : 2 : atom_clear (&mfra->header);
3747 : 2 : g_free (mfra);
3748 : 2 : }
3749 : :
3750 : : static void
3751 : 2 : atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
3752 : : {
3753 : 2 : guint8 flags[3] = { 0, 0, 0 };
3754 : :
3755 : 2 : atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
3756 : 2 : tfra->track_ID = track_ID;
3757 : 2 : atom_array_init (&tfra->entries, 512);
3758 : 2 : }
3759 : :
3760 : : AtomTFRA *
3761 : 2 : atom_tfra_new (AtomsContext * context, guint32 track_ID)
3762 : : {
3763 : 2 : AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
3764 : :
3765 : 2 : atom_tfra_init (tfra, track_ID);
3766 : 2 : return tfra;
3767 : :
3768 : : }
3769 : :
3770 : : static inline gint
3771 : 8 : need_bytes (guint32 num)
3772 : : {
3773 : 8 : gint n = 0;
3774 : :
3775 [ - + ]: 8 : while (num >>= 8)
3776 : 0 : n++;
3777 : :
3778 : 8 : return n;
3779 : : }
3780 : :
3781 : : void
3782 : 4 : atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
3783 : : {
3784 : : TFRAEntry entry;
3785 : :
3786 : 4 : entry.time = dts;
3787 : : /* fill in later */
3788 : 4 : entry.moof_offset = 0;
3789 : : /* always write a single trun in a single traf */
3790 : 4 : entry.traf_number = 1;
3791 : 4 : entry.trun_number = 1;
3792 : 4 : entry.sample_number = sample_num;
3793 : :
3794 : : /* auto-use 64 bits if needed */
3795 [ - + ]: 4 : if (dts > G_MAXUINT32)
3796 : 0 : tfra->header.version = 1;
3797 : :
3798 : : /* 1 byte will always do for traf and trun number,
3799 : : * check how much sample_num needs */
3800 [ + - - + ]: 12 : tfra->lengths = (tfra->lengths & 0xfc) ||
[ # # - + ]
3801 : 8 : MAX (tfra->lengths, need_bytes (sample_num));
3802 : :
3803 [ - + ][ - + ]: 4 : atom_array_append (&tfra->entries, entry, 256);
3804 : 4 : }
3805 : :
3806 : : void
3807 : 2 : atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
3808 : : {
3809 : : gint i;
3810 : :
3811 : : /* auto-use 64 bits if needed */
3812 [ - + ]: 2 : if (offset > G_MAXUINT32)
3813 : 0 : tfra->header.version = 1;
3814 : :
3815 [ + + ]: 4 : for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
3816 : 2 : TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
3817 : :
3818 [ - + ]: 2 : if (entry->moof_offset)
3819 : 0 : break;
3820 : 2 : entry->moof_offset = offset;
3821 : : }
3822 : 2 : }
3823 : :
3824 : : static guint64
3825 : 2 : atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
3826 : : guint64 * offset)
3827 : : {
3828 : 2 : guint64 original_offset = *offset;
3829 : : guint32 i;
3830 : : TFRAEntry *entry;
3831 : : guint32 data;
3832 : : guint bytes;
3833 : : guint version;
3834 : :
3835 [ - + ]: 2 : if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
3836 : 0 : return 0;
3837 : : }
3838 : :
3839 : 2 : prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
3840 : 2 : prop_copy_uint32 (tfra->lengths, buffer, size, offset);
3841 : 2 : prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
3842 : :
3843 : 2 : version = tfra->header.version;
3844 [ + + ]: 6 : for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
3845 : 4 : entry = &atom_array_index (&tfra->entries, i);
3846 [ - + ]: 4 : if (version) {
3847 : 0 : prop_copy_uint64 (entry->time, buffer, size, offset);
3848 : 0 : prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
3849 : : } else {
3850 : 4 : prop_copy_uint32 (entry->time, buffer, size, offset);
3851 : 4 : prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
3852 : : }
3853 : :
3854 : 4 : bytes = (tfra->lengths & (0x3 << 4)) + 1;
3855 : 4 : data = GUINT32_TO_BE (entry->traf_number);
3856 : 4 : prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3857 : : buffer, size, offset);
3858 : :
3859 : 4 : bytes = (tfra->lengths & (0x3 << 2)) + 1;
3860 : 4 : data = GUINT32_TO_BE (entry->trun_number);
3861 : 4 : prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3862 : : buffer, size, offset);
3863 : :
3864 : 4 : bytes = (tfra->lengths & (0x3)) + 1;
3865 : 4 : data = GUINT32_TO_BE (entry->sample_number);
3866 : 4 : prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3867 : : buffer, size, offset);
3868 : :
3869 : : }
3870 : :
3871 : 2 : atom_write_size (buffer, size, offset, original_offset);
3872 : 2 : return *offset - original_offset;
3873 : : }
3874 : :
3875 : : static guint64
3876 : 2 : atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
3877 : : guint64 * offset)
3878 : : {
3879 : 2 : guint64 original_offset = *offset;
3880 : 2 : guint8 flags[3] = { 0, 0, 0 };
3881 : : AtomFull mfro;
3882 : :
3883 : 2 : atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
3884 : :
3885 [ - + ]: 2 : if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
3886 : 0 : return 0;
3887 : : }
3888 : :
3889 : 2 : prop_copy_uint32 (s, buffer, size, offset);
3890 : :
3891 : 2 : atom_write_size (buffer, size, offset, original_offset);
3892 : :
3893 : 2 : return *offset - original_offset;
3894 : : }
3895 : :
3896 : :
3897 : : guint64
3898 : 2 : atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
3899 : : guint64 * offset)
3900 : : {
3901 : 2 : guint64 original_offset = *offset;
3902 : : GList *walker;
3903 : :
3904 [ - + ]: 2 : if (!atom_copy_data (&mfra->header, buffer, size, offset))
3905 : 0 : return 0;
3906 : :
3907 : 2 : walker = g_list_first (mfra->tfras);
3908 [ + + ]: 4 : while (walker != NULL) {
3909 [ - + ]: 2 : if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
3910 : 0 : return 0;
3911 : : }
3912 [ + - ]: 2 : walker = g_list_next (walker);
3913 : : }
3914 : :
3915 : : /* 16 is the size of the mfro atom */
3916 [ - + ]: 2 : if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
3917 : : size, offset))
3918 : 0 : return 0;
3919 : :
3920 : 2 : atom_write_size (buffer, size, offset, original_offset);
3921 : 2 : return *offset - original_offset;
3922 : : }
3923 : :
3924 : : /* some sample description construction helpers */
3925 : :
3926 : : AtomInfo *
3927 : 4 : build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
3928 : : const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
3929 : : {
3930 : : guint32 track_id;
3931 : : AtomESDS *esds;
3932 : :
3933 : 4 : track_id = trak->tkhd.track_ID;
3934 : :
3935 : 4 : esds = atom_esds_new ();
3936 : 4 : esds->es.id = track_id & 0xFFFF;
3937 : 4 : esds->es.dec_conf_desc.object_type = object_type;
3938 : 4 : esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
3939 : :
3940 [ - + ]: 4 : if (avg_bitrate > 0)
3941 : 0 : esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
3942 [ - + ]: 4 : if (max_bitrate > 0)
3943 : 0 : esds->es.dec_conf_desc.max_bitrate = max_bitrate;
3944 : :
3945 : : /* optional DecoderSpecificInfo */
3946 [ - + ]: 4 : if (codec_data) {
3947 : : DecoderSpecificInfoDescriptor *desc;
3948 : :
3949 : 0 : esds->es.dec_conf_desc.dec_specific_info = desc =
3950 : : desc_dec_specific_info_new ();
3951 : 0 : desc_dec_specific_info_alloc_data (desc, GST_BUFFER_SIZE (codec_data));
3952 : :
3953 : 0 : memcpy (desc->data, GST_BUFFER_DATA (codec_data),
3954 : 0 : GST_BUFFER_SIZE (codec_data));
3955 : : }
3956 : :
3957 : 4 : return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
3958 : : atom_esds_free);
3959 : : }
3960 : :
3961 : : AtomInfo *
3962 : 9 : build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
3963 : : guint32 max_bitrate)
3964 : : {
3965 : : AtomData *atom_data;
3966 : : GstBuffer *buf;
3967 : :
3968 [ + - ][ + - ]: 9 : if (buffer_size_db == 0 && avg_bitrate == 0 && max_bitrate == 0)
[ + - ]
3969 : 9 : return 0;
3970 : :
3971 : 0 : buf = gst_buffer_new_and_alloc (12);
3972 : :
3973 : 0 : GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf), buffer_size_db);
3974 : 0 : GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf) + 4, max_bitrate);
3975 : 0 : GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf) + 8, avg_bitrate);
3976 : :
3977 : 0 : atom_data =
3978 : : atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('b', 't', 'r', 't'), buf);
3979 : 0 : gst_buffer_unref (buf);
3980 : :
3981 : 9 : return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3982 : : atom_data_free);
3983 : : }
3984 : :
3985 : : static AtomInfo *
3986 : 0 : build_mov_wave_extension (AtomTRAK * trak, guint32 fourcc, AtomInfo * atom1,
3987 : : AtomInfo * atom2, gboolean terminator)
3988 : : {
3989 : : AtomWAVE *wave;
3990 : : AtomFRMA *frma;
3991 : : Atom *ext_atom;
3992 : :
3993 : : /* Build WAVE atom for sample table entry */
3994 : 0 : wave = atom_wave_new ();
3995 : :
3996 : : /* Prepend Terminator atom to the WAVE list first, so it ends up last */
3997 [ # # ]: 0 : if (terminator) {
3998 : 0 : ext_atom = (Atom *) atom_data_new (FOURCC_null);
3999 : 0 : wave->extension_atoms =
4000 : 0 : atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4001 : : (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4002 : : }
4003 : :
4004 : : /* Add supplied atoms to WAVE */
4005 [ # # ]: 0 : if (atom2)
4006 : 0 : wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
4007 [ # # ]: 0 : if (atom1)
4008 : 0 : wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
4009 : :
4010 : : /* Add FRMA to the WAVE */
4011 : 0 : frma = atom_frma_new ();
4012 : 0 : frma->media_type = fourcc;
4013 : :
4014 : 0 : wave->extension_atoms =
4015 : 0 : atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4016 : : (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4017 : :
4018 : 0 : return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4019 : : atom_wave_free);
4020 : : }
4021 : :
4022 : : AtomInfo *
4023 : 0 : build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
4024 : : guint32 avg_bitrate, guint32 max_bitrate)
4025 : : {
4026 : : AtomInfo *esds, *mp4a;
4027 : : GstBuffer *buf;
4028 : :
4029 : : /* Add ESDS atom to WAVE */
4030 : 0 : esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
4031 : : ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
4032 : :
4033 : : /* Add MP4A atom to the WAVE:
4034 : : * not really in spec, but makes offset based players happy */
4035 : 0 : buf = gst_buffer_new_and_alloc (4);
4036 : 0 : *((guint32 *) GST_BUFFER_DATA (buf)) = 0;
4037 : 0 : mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
4038 : 0 : gst_buffer_unref (buf);
4039 : :
4040 : 0 : return build_mov_wave_extension (trak, FOURCC_mp4a, mp4a, esds, TRUE);
4041 : : }
4042 : :
4043 : : AtomInfo *
4044 : 0 : build_mov_alac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
4045 : : {
4046 : : AtomInfo *alac;
4047 : :
4048 : 0 : alac = build_codec_data_extension (FOURCC_alac, codec_data);
4049 : :
4050 : 0 : return build_mov_wave_extension (trak, FOURCC_alac, NULL, alac, TRUE);
4051 : : }
4052 : :
4053 : : AtomInfo *
4054 : 0 : build_fiel_extension (gint fields)
4055 : : {
4056 : : AtomData *atom_data;
4057 : : GstBuffer *buf;
4058 : :
4059 [ # # ]: 0 : if (fields == 1) {
4060 : 0 : return NULL;
4061 : : }
4062 : :
4063 : 0 : buf = gst_buffer_new_and_alloc (1);
4064 : 0 : GST_BUFFER_DATA (buf)[0] = (guint8) fields;
4065 : :
4066 : 0 : atom_data =
4067 : : atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('f', 'i', 'e', 'l'), buf);
4068 : 0 : gst_buffer_unref (buf);
4069 : :
4070 : 0 : return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4071 : : atom_data_free);
4072 : : }
4073 : :
4074 : : AtomInfo *
4075 : 0 : build_jp2x_extension (const GstBuffer * prefix)
4076 : : {
4077 : : AtomData *atom_data;
4078 : :
4079 [ # # ]: 0 : if (!prefix) {
4080 : 0 : return NULL;
4081 : : }
4082 : :
4083 : 0 : atom_data =
4084 : : atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('j', 'p', '2', 'x'),
4085 : : prefix);
4086 : :
4087 : 0 : return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4088 : : atom_data_free);
4089 : : }
4090 : :
4091 : : AtomInfo *
4092 : 0 : build_jp2h_extension (AtomTRAK * trak, gint width, gint height, guint32 fourcc,
4093 : : gint ncomp, const GValue * cmap_array, const GValue * cdef_array)
4094 : : {
4095 : : AtomData *atom_data;
4096 : : GstBuffer *buf;
4097 : : guint8 cenum;
4098 : : gint i;
4099 : 0 : gint idhr_size = 22;
4100 : 0 : gint colr_size = 15;
4101 : 0 : gint cmap_size = 0, cdef_size = 0;
4102 : 0 : gint cmap_array_size = 0;
4103 : 0 : gint cdef_array_size = 0;
4104 : : GstByteWriter writer;
4105 : :
4106 [ # # ][ # # ]: 0 : g_return_val_if_fail (cmap_array == NULL ||
[ # # ][ # # ]
4107 : : GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
4108 [ # # ][ # # ]: 0 : g_return_val_if_fail (cdef_array == NULL ||
[ # # ][ # # ]
4109 : : GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
4110 : :
4111 [ # # ]: 0 : if (fourcc == GST_MAKE_FOURCC ('s', 'R', 'G', 'B')) {
4112 : 0 : cenum = 0x10;
4113 [ # # ]: 0 : if (ncomp == 0)
4114 : 0 : ncomp = 3;
4115 [ # # ]: 0 : } else if (fourcc == GST_MAKE_FOURCC ('G', 'R', 'A', 'Y')) {
4116 : 0 : cenum = 0x11;
4117 [ # # ]: 0 : if (ncomp == 0)
4118 : 0 : ncomp = 1;
4119 [ # # ]: 0 : } else if (fourcc == GST_MAKE_FOURCC ('s', 'Y', 'U', 'V')) {
4120 : 0 : cenum = 0x12;
4121 [ # # ]: 0 : if (ncomp == 0)
4122 : 0 : ncomp = 3;
4123 : : } else
4124 : 0 : return NULL;
4125 : :
4126 [ # # ]: 0 : if (cmap_array) {
4127 : 0 : cmap_array_size = gst_value_array_get_size (cmap_array);
4128 : 0 : cmap_size = 8 + cmap_array_size * 4;
4129 : : }
4130 [ # # ]: 0 : if (cdef_array) {
4131 : 0 : cdef_array_size = gst_value_array_get_size (cdef_array);
4132 : 0 : cdef_size = 8 + 2 + cdef_array_size * 6;
4133 : : }
4134 : :
4135 : 0 : buf = gst_buffer_new_and_alloc (idhr_size + colr_size + cmap_size +
4136 : : cdef_size);
4137 : 0 : gst_byte_writer_init_with_buffer (&writer, buf, FALSE);
4138 : :
4139 : : /* ihdr = image header box */
4140 : 0 : gst_byte_writer_put_uint32_be (&writer, 22);
4141 : 0 : gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
4142 : 0 : gst_byte_writer_put_uint32_be (&writer, height);
4143 : 0 : gst_byte_writer_put_uint32_be (&writer, width);
4144 : 0 : gst_byte_writer_put_uint16_be (&writer, ncomp);
4145 : : /* 8 bits per component, unsigned */
4146 : 0 : gst_byte_writer_put_uint8 (&writer, 0x7);
4147 : : /* compression type; reserved */
4148 : 0 : gst_byte_writer_put_uint8 (&writer, 0x7);
4149 : : /* colour space (un)known */
4150 : 0 : gst_byte_writer_put_uint8 (&writer, 0x0);
4151 : : /* intellectual property right (box present) */
4152 : 0 : gst_byte_writer_put_uint8 (&writer, 0x0);
4153 : :
4154 : : /* colour specification box */
4155 : 0 : gst_byte_writer_put_uint32_be (&writer, 15);
4156 : 0 : gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
4157 : :
4158 : : /* specification method: enumerated */
4159 : 0 : gst_byte_writer_put_uint8 (&writer, 0x1);
4160 : : /* precedence; reserved */
4161 : 0 : gst_byte_writer_put_uint8 (&writer, 0x0);
4162 : : /* approximation; reserved */
4163 : 0 : gst_byte_writer_put_uint8 (&writer, 0x0);
4164 : : /* enumerated colourspace */
4165 : 0 : gst_byte_writer_put_uint32_be (&writer, cenum);
4166 : :
4167 [ # # ]: 0 : if (cmap_array) {
4168 : 0 : gst_byte_writer_put_uint32_be (&writer, cmap_size);
4169 : 0 : gst_byte_writer_put_uint32_le (&writer,
4170 : : GST_MAKE_FOURCC ('c', 'm', 'a', 'p'));
4171 [ # # ]: 0 : for (i = 0; i < cmap_array_size; i++) {
4172 : : const GValue *item;
4173 : : gint value;
4174 : : guint16 cmp;
4175 : : guint8 mtyp;
4176 : : guint8 pcol;
4177 : 0 : item = gst_value_array_get_value (cmap_array, i);
4178 : 0 : value = g_value_get_int (item);
4179 : :
4180 : : /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
4181 : 0 : cmp = value & 0xFFFF;
4182 : 0 : mtyp = value >> 24;
4183 : 0 : pcol = (value >> 16) & 0xFF;
4184 : :
4185 [ # # ]: 0 : if (mtyp == 1)
4186 [ # # ]: 0 : GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
4187 : : "handle Pallete mapping atoms yet");
4188 : :
4189 : 0 : gst_byte_writer_put_uint16_be (&writer, cmp);
4190 : 0 : gst_byte_writer_put_uint8 (&writer, mtyp);
4191 : 0 : gst_byte_writer_put_uint8 (&writer, pcol);
4192 : : }
4193 : : }
4194 : :
4195 [ # # ]: 0 : if (cdef_array) {
4196 : 0 : gst_byte_writer_put_uint32_be (&writer, cdef_size);
4197 : 0 : gst_byte_writer_put_uint32_le (&writer,
4198 : : GST_MAKE_FOURCC ('c', 'd', 'e', 'f'));
4199 : 0 : gst_byte_writer_put_uint16_be (&writer, cdef_array_size);
4200 [ # # ]: 0 : for (i = 0; i < cdef_array_size; i++) {
4201 : : const GValue *item;
4202 : : gint value;
4203 : 0 : item = gst_value_array_get_value (cdef_array, i);
4204 : 0 : value = g_value_get_int (item);
4205 : :
4206 : 0 : gst_byte_writer_put_uint16_be (&writer, i);
4207 [ # # ]: 0 : if (value > 0) {
4208 : 0 : gst_byte_writer_put_uint16_be (&writer, 0);
4209 : 0 : gst_byte_writer_put_uint16_be (&writer, value);
4210 [ # # ]: 0 : } else if (value < 0) {
4211 : 0 : gst_byte_writer_put_uint16_be (&writer, -value);
4212 : 0 : gst_byte_writer_put_uint16_be (&writer, 0); /* TODO what here? */
4213 : : } else {
4214 : 0 : gst_byte_writer_put_uint16_be (&writer, 1);
4215 : 0 : gst_byte_writer_put_uint16_be (&writer, 0);
4216 : : }
4217 : : }
4218 : : }
4219 : :
4220 [ # # ]: 0 : g_assert (gst_byte_writer_get_remaining (&writer) == 0);
4221 : :
4222 : 0 : atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
4223 : 0 : gst_buffer_unref (buf);
4224 : :
4225 : 0 : return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4226 : : atom_data_free);
4227 : : }
4228 : :
4229 : : AtomInfo *
4230 : 9 : build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
4231 : : {
4232 : : AtomData *data;
4233 : 9 : AtomInfo *result = NULL;
4234 : :
4235 [ + - ]: 9 : if (codec_data) {
4236 : 9 : data = atom_data_new_from_gst_buffer (fourcc, codec_data);
4237 : 9 : result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
4238 : : atom_data_free);
4239 : : }
4240 : :
4241 : 9 : return result;
4242 : : }
4243 : :
4244 : : AtomInfo *
4245 : 0 : build_amr_extension (void)
4246 : : {
4247 : : guint8 ext[9];
4248 : : GstBuffer *buf;
4249 : : AtomInfo *res;
4250 : :
4251 : 0 : buf = gst_buffer_new ();
4252 : 0 : GST_BUFFER_DATA (buf) = ext;
4253 : 0 : GST_BUFFER_SIZE (buf) = sizeof (ext);
4254 : :
4255 : : /* vendor */
4256 : 0 : GST_WRITE_UINT32_LE (ext, 0);
4257 : : /* decoder version */
4258 : 0 : GST_WRITE_UINT8 (ext + 4, 0);
4259 : : /* mode set (all modes) */
4260 : 0 : GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
4261 : : /* mode change period (no restriction) */
4262 : 0 : GST_WRITE_UINT8 (ext + 7, 0);
4263 : : /* frames per sample */
4264 : 0 : GST_WRITE_UINT8 (ext + 8, 1);
4265 : :
4266 : 0 : res = build_codec_data_extension (GST_MAKE_FOURCC ('d', 'a', 'm', 'r'), buf);
4267 : 0 : gst_buffer_unref (buf);
4268 : 0 : return res;
4269 : : }
4270 : :
4271 : : AtomInfo *
4272 : 0 : build_h263_extension (void)
4273 : : {
4274 : : guint8 ext[7];
4275 : : GstBuffer *buf;
4276 : : AtomInfo *res;
4277 : :
4278 : 0 : buf = gst_buffer_new ();
4279 : 0 : GST_BUFFER_DATA (buf) = ext;
4280 : 0 : GST_BUFFER_SIZE (buf) = sizeof (ext);
4281 : :
4282 : : /* vendor */
4283 : 0 : GST_WRITE_UINT32_LE (ext, 0);
4284 : : /* decoder version */
4285 : 0 : GST_WRITE_UINT8 (ext + 4, 0);
4286 : : /* level / profile */
4287 : : /* FIXME ? maybe ? obtain somewhere; baseline for now */
4288 : 0 : GST_WRITE_UINT8 (ext + 5, 10);
4289 : 0 : GST_WRITE_UINT8 (ext + 6, 0);
4290 : :
4291 : 0 : res = build_codec_data_extension (GST_MAKE_FOURCC ('d', '2', '6', '3'), buf);
4292 : 0 : gst_buffer_unref (buf);
4293 : 0 : return res;
4294 : : }
4295 : :
4296 : : AtomInfo *
4297 : 0 : build_gama_atom (gdouble gamma)
4298 : : {
4299 : : AtomInfo *res;
4300 : : guint32 gamma_fp;
4301 : : GstBuffer *buf;
4302 : :
4303 : : /* convert to uint32 from fixed point */
4304 : 0 : gamma_fp = (guint32) 65536 *gamma;
4305 : :
4306 : 0 : buf = gst_buffer_new_and_alloc (4);
4307 : 0 : GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf), gamma_fp);
4308 : 0 : res = build_codec_data_extension (FOURCC_gama, buf);
4309 : 0 : gst_buffer_unref (buf);
4310 : 0 : return res;
4311 : : }
4312 : :
4313 : : AtomInfo *
4314 : 0 : build_SMI_atom (const GstBuffer * seqh)
4315 : : {
4316 : : AtomInfo *res;
4317 : : GstBuffer *buf;
4318 : :
4319 : : /* the seqh plus its size and fourcc */
4320 : 0 : buf = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (seqh) + 8);
4321 : :
4322 : 0 : GST_WRITE_UINT32_LE (GST_BUFFER_DATA (buf), FOURCC_SEQH);
4323 : 0 : GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf) + 4, GST_BUFFER_SIZE (seqh));
4324 : 0 : memcpy (GST_BUFFER_DATA (buf) + 8, GST_BUFFER_DATA (seqh),
4325 : 0 : GST_BUFFER_SIZE (seqh));
4326 : 0 : res = build_codec_data_extension (FOURCC_SMI_, buf);
4327 : 0 : gst_buffer_unref (buf);
4328 : 0 : return res;
4329 : : }
4330 : :
4331 : : static AtomInfo *
4332 : 0 : build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
4333 : : {
4334 : : AtomData *atom_data;
4335 : : GstBuffer *buf;
4336 : : guint8 *data;
4337 : 0 : const gint ima_adpcm_atom_size = 20;
4338 : : guint32 fourcc;
4339 : : gint samplesperblock;
4340 : : gint bytespersec;
4341 : :
4342 : : /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
4343 : : identifier. Note that the identifier here is big-endian, but when used
4344 : : within the WAVE header (below), it's little endian. */
4345 : 0 : fourcc = MS_WAVE_FOURCC (0x11);
4346 : :
4347 : 0 : buf = gst_buffer_new_and_alloc (ima_adpcm_atom_size);
4348 : 0 : data = GST_BUFFER_DATA (buf);
4349 : :
4350 : : /* This atom's content is a WAVE header, including 2 bytes of extra data.
4351 : : Note that all of this is little-endian, unlike most stuff in qt. */
4352 : : /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
4353 : : for the rest. Simplifies to this. */
4354 : 0 : samplesperblock = 2 * blocksize / channels - 7;
4355 : 0 : bytespersec = rate * blocksize / samplesperblock;
4356 : 0 : GST_WRITE_UINT16_LE (data, 0x11);
4357 : 0 : GST_WRITE_UINT16_LE (data + 2, channels);
4358 : 0 : GST_WRITE_UINT32_LE (data + 4, rate);
4359 : 0 : GST_WRITE_UINT32_LE (data + 8, bytespersec);
4360 : 0 : GST_WRITE_UINT16_LE (data + 12, blocksize);
4361 : 0 : GST_WRITE_UINT16_LE (data + 14, 4);
4362 : 0 : GST_WRITE_UINT16_LE (data + 16, 2); /* Two extra bytes */
4363 : 0 : GST_WRITE_UINT16_LE (data + 18, samplesperblock);
4364 : :
4365 : 0 : atom_data = atom_data_new_from_gst_buffer (fourcc, buf);
4366 : 0 : gst_buffer_unref (buf);
4367 : :
4368 : 0 : return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4369 : : atom_data_free);
4370 : : }
4371 : :
4372 : : AtomInfo *
4373 : 0 : build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
4374 : : {
4375 : : AtomWAVE *wave;
4376 : : AtomFRMA *frma;
4377 : : Atom *ext_atom;
4378 : :
4379 : : /* Add WAVE atom */
4380 : 0 : wave = atom_wave_new ();
4381 : :
4382 : : /* Prepend Terminator atom to the WAVE list first, so it ends up last */
4383 : 0 : ext_atom = (Atom *) atom_data_new (FOURCC_null);
4384 : 0 : wave->extension_atoms =
4385 : 0 : atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4386 : : (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4387 : :
4388 : : /* Add wave ima adpcm atom to WAVE */
4389 : 0 : wave->extension_atoms = g_list_prepend (wave->extension_atoms,
4390 : 0 : build_ima_adpcm_atom (channels, rate, blocksize));
4391 : :
4392 : : /* Add FRMA to the WAVE */
4393 : 0 : frma = atom_frma_new ();
4394 : 0 : frma->media_type = MS_WAVE_FOURCC (0x11);
4395 : :
4396 : 0 : wave->extension_atoms =
4397 : 0 : atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4398 : : (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4399 : :
4400 : 0 : return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4401 : : atom_wave_free);
4402 : : }
4403 : :
4404 : : AtomInfo *
4405 : 6 : build_uuid_xmp_atom (const GstTagList * taglist)
4406 : : {
4407 : : GstBuffer *xmp_data;
4408 : : AtomUUID *uuid;
4409 : : static guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
4410 : : 0x97, 0xA9, 0x42, 0xE8,
4411 : : 0x9C, 0x71, 0x99, 0x94,
4412 : : 0x91, 0xE3, 0xAF, 0xAC
4413 : : };
4414 : :
4415 : 6 : xmp_data = gst_tag_list_to_xmp_buffer (taglist, TRUE);
4416 [ - + ]: 6 : if (xmp_data == NULL)
4417 : 0 : return NULL;
4418 : :
4419 : 6 : uuid = atom_uuid_new ();
4420 : 6 : memcpy (uuid->uuid, xmp_uuid, 16);
4421 : :
4422 : 6 : uuid->data = g_malloc (GST_BUFFER_SIZE (xmp_data));
4423 : 6 : uuid->datalen = GST_BUFFER_SIZE (xmp_data);
4424 : 6 : memcpy (uuid->data, GST_BUFFER_DATA (xmp_data), GST_BUFFER_SIZE (xmp_data));
4425 : :
4426 : 6 : gst_buffer_unref (xmp_data);
4427 : 6 : return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,
4428 : : atom_uuid_free);
4429 : : }
|