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