tizen 2.0 init
[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 void
2746 atom_trak_update_bitrates (AtomTRAK * trak, guint32 avg_bitrate,
2747     guint32 max_bitrate)
2748 {
2749   AtomESDS *esds = NULL;
2750   AtomData *btrt = NULL;
2751   AtomWAVE *wave = NULL;
2752   AtomSTSD *stsd;
2753   GList *iter;
2754   GList *extensioniter = NULL;
2755
2756   g_return_if_fail (trak != NULL);
2757
2758   if (avg_bitrate == 0 && max_bitrate == 0)
2759     return;
2760
2761   stsd = &trak->mdia.minf.stbl.stsd;
2762   for (iter = stsd->entries; iter; iter = g_list_next (iter)) {
2763     SampleTableEntry *entry = iter->data;
2764
2765     switch (entry->kind) {
2766       case AUDIO:{
2767         SampleTableEntryMP4A *audioentry = (SampleTableEntryMP4A *) entry;
2768         extensioniter = audioentry->extension_atoms;
2769         break;
2770       }
2771       case VIDEO:{
2772         SampleTableEntryMP4V *videoentry = (SampleTableEntryMP4V *) entry;
2773         extensioniter = videoentry->extension_atoms;
2774         break;
2775       }
2776       default:
2777         break;
2778     }
2779   }
2780
2781   for (; extensioniter; extensioniter = g_list_next (extensioniter)) {
2782     AtomInfo *atominfo = extensioniter->data;
2783     if (atominfo->atom->type == FOURCC_esds) {
2784       esds = (AtomESDS *) atominfo->atom;
2785     } else if (atominfo->atom->type == FOURCC_btrt) {
2786       btrt = (AtomData *) atominfo->atom;
2787     } else if (atominfo->atom->type == FOURCC_wave) {
2788       wave = (AtomWAVE *) atominfo->atom;
2789     }
2790   }
2791
2792   /* wave might have an esds internally */
2793   if (wave) {
2794     for (extensioniter = wave->extension_atoms; extensioniter;
2795         extensioniter = g_list_next (extensioniter)) {
2796       AtomInfo *atominfo = extensioniter->data;
2797       if (atominfo->atom->type == FOURCC_esds) {
2798         esds = (AtomESDS *) atominfo->atom;
2799         break;
2800       }
2801     }
2802   }
2803
2804   if (esds) {
2805     if (avg_bitrate && esds->es.dec_conf_desc.avg_bitrate == 0)
2806       esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
2807     if (max_bitrate && esds->es.dec_conf_desc.max_bitrate == 0)
2808       esds->es.dec_conf_desc.max_bitrate = max_bitrate;
2809   }
2810   if (btrt) {
2811     /* type(4bytes) + size(4bytes) + buffersize(4bytes) +
2812      * maxbitrate(bytes) + avgbitrate(bytes) */
2813     if (max_bitrate && GST_READ_UINT32_BE (btrt->data + 4) == 0)
2814       GST_WRITE_UINT32_BE (btrt->data + 4, max_bitrate);
2815     if (avg_bitrate && GST_READ_UINT32_BE (btrt->data + 8) == 0)
2816       GST_WRITE_UINT32_BE (btrt->data + 8, avg_bitrate);
2817   }
2818 }
2819
2820 /*
2821  * Meta tags functions
2822  */
2823 static void
2824 atom_moov_init_metatags (AtomMOOV * moov, AtomsContext * context)
2825 {
2826   if (!moov->udta) {
2827     moov->udta = atom_udta_new ();
2828   }
2829   if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
2830     if (!moov->udta->meta) {
2831       moov->udta->meta = atom_meta_new ();
2832     }
2833     if (!moov->udta->meta->ilst) {
2834       moov->udta->meta->ilst = atom_ilst_new ();
2835     }
2836   }
2837 }
2838
2839 static void
2840 atom_tag_data_alloc_data (AtomTagData * data, guint size)
2841 {
2842   if (data->data != NULL) {
2843     g_free (data->data);
2844   }
2845   data->data = g_new0 (guint8, size);
2846   data->datalen = size;
2847 }
2848
2849 static void
2850 atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
2851 {
2852   GList **entries;
2853
2854   atom_moov_init_metatags (moov, &moov->context);
2855   if (moov->udta->meta)
2856     entries = &moov->udta->meta->ilst->entries;
2857   else
2858     entries = &moov->udta->entries;
2859   *entries = g_list_append (*entries, tag);
2860 }
2861
2862 void
2863 atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2864     const guint8 * data, guint size)
2865 {
2866   AtomTag *tag;
2867   AtomTagData *tdata;
2868
2869   tag = atom_tag_new (fourcc, flags);
2870   tdata = &tag->data;
2871   atom_tag_data_alloc_data (tdata, size);
2872   g_memmove (tdata->data, data, size);
2873
2874   atom_moov_append_tag (moov,
2875       build_atom_info_wrapper ((Atom *) tag, atom_tag_copy_data,
2876           atom_tag_free));
2877 }
2878
2879 void
2880 atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
2881 {
2882   gint len = strlen (value);
2883
2884   if (len > 0)
2885     atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
2886 }
2887
2888 void
2889 atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
2890     guint32 value)
2891 {
2892   guint8 data[8] = { 0, };
2893
2894   if (flags) {
2895     GST_WRITE_UINT16_BE (data, value);
2896     atom_moov_add_tag (moov, fourcc, flags, data, 2);
2897   } else {
2898     GST_WRITE_UINT32_BE (data + 2, value);
2899     atom_moov_add_tag (moov, fourcc, flags, data, 8);
2900   }
2901 }
2902
2903 void
2904 atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
2905 {
2906   AtomData *data_atom;
2907   GstBuffer *buf;
2908   guint len;
2909   guint32 fourcc;
2910
2911   if (size < 8)
2912     return;
2913
2914   /* blob is unparsed atom;
2915    * extract size and fourcc, and wrap remainder in data atom */
2916   len = GST_READ_UINT32_BE (data);
2917   fourcc = GST_READ_UINT32_LE (data + 4);
2918   if (len > size)
2919     return;
2920
2921   buf = gst_buffer_new ();
2922   GST_BUFFER_SIZE (buf) = len - 8;
2923   GST_BUFFER_DATA (buf) = data + 8;
2924
2925   data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2926   gst_buffer_unref (buf);
2927
2928   atom_moov_append_tag (moov,
2929       build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2930           atom_data_free));
2931 }
2932
2933 void
2934 atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
2935     guint size)
2936 {
2937   AtomData *data_atom;
2938   GstBuffer *buf;
2939   guint8 *bdata;
2940
2941   /* need full atom */
2942   buf = gst_buffer_new_and_alloc (size + 4);
2943   bdata = GST_BUFFER_DATA (buf);
2944   /* full atom: version and flags */
2945   GST_WRITE_UINT32_BE (bdata, 0);
2946   memcpy (bdata + 4, data, size);
2947
2948   data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
2949   gst_buffer_unref (buf);
2950
2951   atom_moov_append_tag (moov,
2952       build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
2953           atom_data_free));
2954 }
2955
2956 guint16
2957 language_code (const char *lang)
2958 {
2959   g_return_val_if_fail (lang != NULL, 0);
2960   g_return_val_if_fail (strlen (lang) == 3, 0);
2961
2962   return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
2963       ((lang[2] - 0x60) & 0x1F);
2964 }
2965
2966 void
2967 atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc,
2968     const gchar * value, gint16 ivalue)
2969 {
2970   gint len = 0, size = 0;
2971   guint8 *data;
2972
2973   if (value) {
2974     len = strlen (value);
2975     size = len + 3;
2976   }
2977
2978   if (ivalue >= 0)
2979     size += 2;
2980
2981   data = g_malloc (size + 3);
2982   /* language tag and null-terminated UTF-8 string */
2983   if (value) {
2984     GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
2985     /* include 0 terminator */
2986     memcpy (data + 2, value, len + 1);
2987   }
2988   /* 16-bit unsigned int if standalone, otherwise 8-bit */
2989   if (ivalue >= 0) {
2990     if (size == 2)
2991       GST_WRITE_UINT16_BE (data + size - 2, ivalue);
2992     else {
2993       GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
2994       size--;
2995     }
2996   }
2997
2998   atom_moov_add_3gp_tag (moov, fourcc, data, size);
2999   g_free (data);
3000 }
3001
3002 void
3003 atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
3004 {
3005   atom_moov_add_3gp_str_int_tag (moov, fourcc, value, -1);
3006 }
3007
3008 void
3009 atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value)
3010 {
3011   atom_moov_add_3gp_str_int_tag (moov, fourcc, NULL, value);
3012 }
3013
3014 void
3015 atom_moov_add_xmp_tags (AtomMOOV * moov, GstBuffer * xmpbuffer)
3016 {
3017   AtomData *data_atom = NULL;
3018
3019   if (moov->context.flavor == ATOMS_TREE_FLAVOR_MOV) {
3020     if (xmpbuffer) {
3021       data_atom = atom_data_new_from_gst_buffer (FOURCC_XMP_, xmpbuffer);
3022       atom_moov_init_metatags (moov, &moov->context);
3023       moov->udta->entries = g_list_append (moov->udta->entries,
3024           build_atom_info_wrapper ((Atom *) data_atom, atom_data_copy_data,
3025               atom_data_free));
3026     }
3027   } else {
3028     GST_DEBUG ("Not adding xmp to moov atom, it is only used in 'mov' format");
3029   }
3030
3031 }
3032
3033 /*
3034  * Functions for specifying media types
3035  */
3036
3037 static void
3038 atom_minf_set_audio (AtomMINF * minf)
3039 {
3040   atom_minf_clear_handlers (minf);
3041   minf->smhd = atom_smhd_new ();
3042 }
3043
3044 static void
3045 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
3046 {
3047   atom_minf_clear_handlers (minf);
3048   minf->vmhd = atom_vmhd_new (context);
3049 }
3050
3051 static void
3052 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
3053     guint32 hdlr_type)
3054 {
3055   if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3056     hdlr->component_type = comp_type;
3057   }
3058   hdlr->handler_type = hdlr_type;
3059 }
3060
3061 static void
3062 atom_hdlr_set_name (AtomHDLR * hdlr, const char *name)
3063 {
3064   if (hdlr->name)
3065     g_free (hdlr->name);
3066   hdlr->name = g_strdup (name);
3067 }
3068
3069 static void
3070 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
3071 {
3072   atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
3073   /* Some players (low-end hardware) check for this name, which is what
3074    * QuickTime itself sets */
3075   atom_hdlr_set_name (&mdia->hdlr, "SoundHandler");
3076 }
3077
3078 static void
3079 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
3080 {
3081   atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
3082   /* Some players (low-end hardware) check for this name, which is what
3083    * QuickTime itself sets */
3084   atom_hdlr_set_name (&mdia->hdlr, "VideoHandler");
3085 }
3086
3087 static void
3088 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
3089 {
3090   atom_mdia_set_hdlr_type_audio (mdia, context);
3091   atom_minf_set_audio (&mdia->minf);
3092 }
3093
3094 static void
3095 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
3096 {
3097   atom_mdia_set_hdlr_type_video (mdia, context);
3098   atom_minf_set_video (&mdia->minf, context);
3099 }
3100
3101 static void
3102 atom_tkhd_set_audio (AtomTKHD * tkhd)
3103 {
3104   tkhd->volume = 0x0100;
3105   tkhd->width = tkhd->height = 0;
3106 }
3107
3108 static void
3109 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
3110     guint32 height)
3111 {
3112   tkhd->volume = 0;
3113
3114   /* qt and ISO base media do not contradict, and examples agree */
3115   tkhd->width = width;
3116   tkhd->height = height;
3117 }
3118
3119 static void
3120 atom_edts_add_entry (AtomEDTS * edts, EditListEntry * entry)
3121 {
3122   edts->elst.entries = g_slist_append (edts->elst.entries, entry);
3123 }
3124
3125 /* 
3126  * Adds a new entry to this trak edits list
3127  * duration is in the moov's timescale
3128  * media_time is the offset in the media time to start from (media's timescale)
3129  * rate is a 32 bits fixed-point
3130  */
3131 void
3132 atom_trak_add_elst_entry (AtomTRAK * trak, guint32 duration, guint32 media_time,
3133     guint32 rate)
3134 {
3135   EditListEntry *entry = g_new (EditListEntry, 1);
3136
3137   entry->duration = duration;
3138   entry->media_time = media_time;
3139   entry->media_rate = rate;
3140
3141   if (trak->edts == NULL) {
3142     trak->edts = atom_edts_new ();
3143   }
3144   atom_edts_add_entry (trak->edts, entry);
3145 }
3146
3147 /* re-negotiation is prevented at top-level, so only 1 entry expected.
3148  * Quite some more care here and elsewhere may be needed to
3149  * support several entries */
3150 static SampleTableEntryMP4A *
3151 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
3152     guint32 type)
3153 {
3154   AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3155   SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
3156
3157   mp4a->se.header.type = type;
3158   mp4a->se.kind = AUDIO;
3159   mp4a->compression_id = -1;
3160   mp4a->se.data_reference_index = 1;
3161
3162   stsd->entries = g_list_prepend (stsd->entries, mp4a);
3163   stsd->n_entries++;
3164   return mp4a;
3165 }
3166
3167 static SampleTableEntryMP4V *
3168 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
3169     guint32 type)
3170 {
3171   SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
3172   AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
3173
3174   mp4v->se.header.type = type;
3175   mp4v->se.kind = VIDEO;
3176   mp4v->se.data_reference_index = 1;
3177   mp4v->horizontal_resolution = 72 << 16;
3178   mp4v->vertical_resolution = 72 << 16;
3179   if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
3180     mp4v->spatial_quality = 512;
3181     mp4v->temporal_quality = 512;
3182   }
3183
3184   stsd->entries = g_list_prepend (stsd->entries, mp4v);
3185   stsd->n_entries++;
3186   return mp4v;
3187 }
3188
3189 static void
3190 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
3191 {
3192   trak->mdia.minf.stbl.stsz.sample_size = sample_size;
3193 }
3194
3195 static void
3196 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
3197 {
3198   atom_tkhd_set_audio (&trak->tkhd);
3199   atom_mdia_set_audio (&trak->mdia, context);
3200 }
3201
3202 static void
3203 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
3204     guint32 height)
3205 {
3206   atom_tkhd_set_video (&trak->tkhd, context, width, height);
3207   atom_mdia_set_video (&trak->mdia, context);
3208 }
3209
3210 static void
3211 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
3212     guint32 rate)
3213 {
3214   atom_trak_set_audio (trak, context);
3215   trak->mdia.mdhd.time_info.timescale = rate;
3216 }
3217
3218 static void
3219 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
3220     guint32 rate, guint32 width, guint32 height)
3221 {
3222   atom_trak_set_video (trak, context, width, height);
3223   trak->mdia.mdhd.time_info.timescale = rate;
3224   trak->tkhd.width = width << 16;
3225   trak->tkhd.height = height << 16;
3226 }
3227
3228 void
3229 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
3230     AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
3231 {
3232   SampleTableEntryMP4A *ste;
3233
3234   atom_trak_set_audio_commons (trak, context, scale);
3235   atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3236   ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
3237
3238   trak->is_video = FALSE;
3239   trak->is_h264 = FALSE;
3240
3241   ste->version = entry->version;
3242   ste->compression_id = entry->compression_id;
3243   ste->sample_size = entry->sample_size;
3244   ste->sample_rate = entry->sample_rate << 16;
3245   ste->channels = entry->channels;
3246
3247   ste->samples_per_packet = entry->samples_per_packet;
3248   ste->bytes_per_sample = entry->bytes_per_sample;
3249   ste->bytes_per_packet = entry->bytes_per_packet;
3250   ste->bytes_per_frame = entry->bytes_per_frame;
3251
3252   if (ext)
3253     ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
3254
3255   /* 0 size means variable size */
3256   atom_trak_set_constant_size_samples (trak, sample_size);
3257 }
3258
3259 static AtomInfo *
3260 build_pasp_extension (AtomTRAK * trak, gint par_width, gint par_height)
3261 {
3262   AtomData *atom_data;
3263   GstBuffer *buf;
3264   guint8 *data;
3265
3266   buf = gst_buffer_new_and_alloc (8);
3267   data = GST_BUFFER_DATA (buf);
3268
3269   /* ihdr = image header box */
3270   GST_WRITE_UINT32_BE (data, par_width);
3271   GST_WRITE_UINT32_BE (data + 4, par_height);
3272
3273   atom_data = atom_data_new_from_gst_buffer (FOURCC_pasp, buf);
3274   gst_buffer_unref (buf);
3275
3276   return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
3277       atom_data_free);
3278 }
3279
3280 void
3281 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
3282     VisualSampleEntry * entry, guint32 scale, GList * ext_atoms_list)
3283 {
3284   SampleTableEntryMP4V *ste;
3285   gint dwidth, dheight;
3286   gint par_n = 0, par_d = 0;
3287
3288   if ((entry->par_n != 1 || entry->par_d != 1) &&
3289       (entry->par_n != entry->par_d)) {
3290     par_n = entry->par_n;
3291     par_d = entry->par_d;
3292   }
3293
3294   dwidth = entry->width;
3295   dheight = entry->height;
3296   /* ISO file spec says track header w/h indicates track's visual presentation
3297    * (so this together with pixels w/h implicitly defines PAR) */
3298   if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
3299     if (par_n > par_d) {
3300       dwidth = entry->width * par_n / par_d;
3301       dheight = entry->height;
3302     } else {
3303       dwidth = entry->width * par_n / par_d;
3304       dheight = entry->height;
3305     }
3306   }
3307
3308   atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
3309   atom_stsd_remove_entries (&trak->mdia.minf.stbl.stsd);
3310   ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
3311
3312   trak->is_video = TRUE;
3313   trak->is_h264 = (entry->fourcc == FOURCC_avc1);
3314
3315   ste->version = entry->version;
3316   ste->width = entry->width;
3317   ste->height = entry->height;
3318   ste->depth = entry->depth;
3319   ste->color_table_id = entry->color_table_id;
3320   ste->frame_count = entry->frame_count;
3321
3322   if (ext_atoms_list)
3323     ste->extension_atoms = g_list_concat (ste->extension_atoms, ext_atoms_list);
3324
3325   /* QT spec has a pasp extension atom in stsd that can hold PAR */
3326   if (par_n && (context->flavor == ATOMS_TREE_FLAVOR_MOV)) {
3327     ste->extension_atoms = g_list_append (ste->extension_atoms,
3328         build_pasp_extension (trak, par_n, par_d));
3329   }
3330 }
3331
3332 static void
3333 atom_mfhd_init (AtomMFHD * mfhd, guint32 sequence_number)
3334 {
3335   guint8 flags[3] = { 0, 0, 0 };
3336
3337   atom_full_init (&(mfhd->header), FOURCC_mfhd, 0, 0, 0, flags);
3338   mfhd->sequence_number = sequence_number;
3339 }
3340
3341 static void
3342 atom_moof_init (AtomMOOF * moof, AtomsContext * context,
3343     guint32 sequence_number)
3344 {
3345   atom_header_set (&moof->header, FOURCC_moof, 0, 0);
3346   atom_mfhd_init (&moof->mfhd, sequence_number);
3347   moof->trafs = NULL;
3348 }
3349
3350 AtomMOOF *
3351 atom_moof_new (AtomsContext * context, guint32 sequence_number)
3352 {
3353   AtomMOOF *moof = g_new0 (AtomMOOF, 1);
3354
3355   atom_moof_init (moof, context, sequence_number);
3356   return moof;
3357 }
3358
3359 static void
3360 atom_trun_free (AtomTRUN * trun)
3361 {
3362   atom_full_clear (&trun->header);
3363   atom_array_clear (&trun->entries);
3364   g_free (trun);
3365 }
3366
3367 static void
3368 atom_sdtp_free (AtomSDTP * sdtp)
3369 {
3370   atom_full_clear (&sdtp->header);
3371   atom_array_clear (&sdtp->entries);
3372   g_free (sdtp);
3373 }
3374
3375 void
3376 atom_traf_free (AtomTRAF * traf)
3377 {
3378   GList *walker;
3379
3380   walker = traf->truns;
3381   while (walker) {
3382     atom_trun_free ((AtomTRUN *) walker->data);
3383     walker = g_list_next (walker);
3384   }
3385   g_list_free (traf->truns);
3386   traf->truns = NULL;
3387
3388   walker = traf->sdtps;
3389   while (walker) {
3390     atom_sdtp_free ((AtomSDTP *) walker->data);
3391     walker = g_list_next (walker);
3392   }
3393   g_list_free (traf->sdtps);
3394   traf->sdtps = NULL;
3395
3396   g_free (traf);
3397 }
3398
3399 void
3400 atom_moof_free (AtomMOOF * moof)
3401 {
3402   GList *walker;
3403
3404   walker = moof->trafs;
3405   while (walker) {
3406     atom_traf_free ((AtomTRAF *) walker->data);
3407     walker = g_list_next (walker);
3408   }
3409   g_list_free (moof->trafs);
3410   moof->trafs = NULL;
3411
3412   g_free (moof);
3413 }
3414
3415 static guint64
3416 atom_mfhd_copy_data (AtomMFHD * mfhd, guint8 ** buffer, guint64 * size,
3417     guint64 * offset)
3418 {
3419   guint64 original_offset = *offset;
3420
3421   if (!atom_full_copy_data (&mfhd->header, buffer, size, offset)) {
3422     return 0;
3423   }
3424
3425   prop_copy_uint32 (mfhd->sequence_number, buffer, size, offset);
3426
3427   atom_write_size (buffer, size, offset, original_offset);
3428   return *offset - original_offset;
3429 }
3430
3431 static guint64
3432 atom_tfhd_copy_data (AtomTFHD * tfhd, guint8 ** buffer, guint64 * size,
3433     guint64 * offset)
3434 {
3435   guint64 original_offset = *offset;
3436   guint32 flags;
3437
3438   if (!atom_full_copy_data (&tfhd->header, buffer, size, offset)) {
3439     return 0;
3440   }
3441
3442   prop_copy_uint32 (tfhd->track_ID, buffer, size, offset);
3443
3444   flags = atom_full_get_flags_as_uint (&tfhd->header);
3445
3446   if (flags & TF_BASE_DATA_OFFSET)
3447     prop_copy_uint64 (tfhd->base_data_offset, buffer, size, offset);
3448   if (flags & TF_SAMPLE_DESCRIPTION_INDEX)
3449     prop_copy_uint32 (tfhd->sample_description_index, buffer, size, offset);
3450   if (flags & TF_DEFAULT_SAMPLE_DURATION)
3451     prop_copy_uint32 (tfhd->default_sample_duration, buffer, size, offset);
3452   if (flags & TF_DEFAULT_SAMPLE_SIZE)
3453     prop_copy_uint32 (tfhd->default_sample_size, buffer, size, offset);
3454   if (flags & TF_DEFAULT_SAMPLE_FLAGS)
3455     prop_copy_uint32 (tfhd->default_sample_flags, buffer, size, offset);
3456
3457   atom_write_size (buffer, size, offset, original_offset);
3458   return *offset - original_offset;
3459 }
3460
3461 static guint64
3462 atom_trun_copy_data (AtomTRUN * trun, guint8 ** buffer, guint64 * size,
3463     guint64 * offset, guint32 * data_offset)
3464 {
3465   guint64 original_offset = *offset;
3466   guint32 flags, i;
3467
3468   flags = atom_full_get_flags_as_uint (&trun->header);
3469
3470   /* if first trun in moof, forcibly add data_offset and record
3471    * where it must be written later on */
3472   if (data_offset && !*data_offset) {
3473     flags |= TR_DATA_OFFSET;
3474   } else {
3475     flags &= ~TR_DATA_OFFSET;
3476   }
3477
3478   atom_full_set_flags_as_uint (&trun->header, flags);
3479
3480   if (!atom_full_copy_data (&trun->header, buffer, size, offset)) {
3481     return 0;
3482   }
3483
3484   prop_copy_uint32 (trun->sample_count, buffer, size, offset);
3485
3486   if (flags & TR_DATA_OFFSET) {
3487     *data_offset = *offset;
3488     prop_copy_int32 (trun->data_offset, buffer, size, offset);
3489   }
3490   if (flags & TR_FIRST_SAMPLE_FLAGS)
3491     prop_copy_uint32 (trun->first_sample_flags, buffer, size, offset);
3492
3493   for (i = 0; i < atom_array_get_len (&trun->entries); i++) {
3494     TRUNSampleEntry *entry = &atom_array_index (&trun->entries, i);
3495
3496     if (flags & TR_SAMPLE_DURATION)
3497       prop_copy_uint32 (entry->sample_duration, buffer, size, offset);
3498     if (flags & TR_SAMPLE_SIZE)
3499       prop_copy_uint32 (entry->sample_size, buffer, size, offset);
3500     if (flags & TR_SAMPLE_FLAGS)
3501       prop_copy_uint32 (entry->sample_flags, buffer, size, offset);
3502     if (flags & TR_COMPOSITION_TIME_OFFSETS)
3503       prop_copy_uint32 (entry->sample_composition_time_offset,
3504           buffer, size, offset);
3505   }
3506
3507   atom_write_size (buffer, size, offset, original_offset);
3508   return *offset - original_offset;
3509 }
3510
3511 static guint64
3512 atom_sdtp_copy_data (AtomSDTP * sdtp, guint8 ** buffer, guint64 * size,
3513     guint64 * offset)
3514 {
3515   guint64 original_offset = *offset;
3516
3517   if (!atom_full_copy_data (&sdtp->header, buffer, size, offset)) {
3518     return 0;
3519   }
3520
3521   /* all entries at once */
3522   prop_copy_fixed_size_string (&atom_array_index (&sdtp->entries, 0),
3523       atom_array_get_len (&sdtp->entries), buffer, size, offset);
3524
3525   atom_write_size (buffer, size, offset, original_offset);
3526   return *offset - original_offset;
3527 }
3528
3529 static guint64
3530 atom_traf_copy_data (AtomTRAF * traf, guint8 ** buffer, guint64 * size,
3531     guint64 * offset, guint32 * data_offset)
3532 {
3533   guint64 original_offset = *offset;
3534   GList *walker;
3535
3536   if (!atom_copy_data (&traf->header, buffer, size, offset)) {
3537     return 0;
3538   }
3539   if (!atom_tfhd_copy_data (&traf->tfhd, buffer, size, offset)) {
3540     return 0;
3541   }
3542
3543   walker = g_list_first (traf->truns);
3544   while (walker != NULL) {
3545     if (!atom_trun_copy_data ((AtomTRUN *) walker->data, buffer, size, offset,
3546             data_offset)) {
3547       return 0;
3548     }
3549     walker = g_list_next (walker);
3550   }
3551
3552   walker = g_list_first (traf->sdtps);
3553   while (walker != NULL) {
3554     if (!atom_sdtp_copy_data ((AtomSDTP *) walker->data, buffer, size, offset)) {
3555       return 0;
3556     }
3557     walker = g_list_next (walker);
3558   }
3559
3560   atom_write_size (buffer, size, offset, original_offset);
3561   return *offset - original_offset;
3562 }
3563
3564 /* creates moof atom; metadata is written expecting actual buffer data
3565  * is in mdata directly after moof, and is consecutively written per trak */
3566 guint64
3567 atom_moof_copy_data (AtomMOOF * moof, guint8 ** buffer,
3568     guint64 * size, guint64 * offset)
3569 {
3570   guint64 original_offset = *offset;
3571   GList *walker;
3572   guint32 data_offset = 0;
3573
3574   if (!atom_copy_data (&moof->header, buffer, size, offset))
3575     return 0;
3576
3577   if (!atom_mfhd_copy_data (&moof->mfhd, buffer, size, offset))
3578     return 0;
3579
3580   walker = g_list_first (moof->trafs);
3581   while (walker != NULL) {
3582     if (!atom_traf_copy_data ((AtomTRAF *) walker->data, buffer, size, offset,
3583             &data_offset)) {
3584       return 0;
3585     }
3586     walker = g_list_next (walker);
3587   }
3588
3589   atom_write_size (buffer, size, offset, original_offset);
3590
3591   if (*buffer && data_offset) {
3592     /* first trun needs a data-offset relative to moof start
3593      *   = moof size + mdat prefix */
3594     GST_WRITE_UINT32_BE (*buffer + data_offset, *offset - original_offset + 8);
3595   }
3596
3597   return *offset - original_offset;
3598 }
3599
3600 static void
3601 atom_tfhd_init (AtomTFHD * tfhd, guint32 track_ID)
3602 {
3603   guint8 flags[3] = { 0, 0, 0 };
3604
3605   atom_full_init (&tfhd->header, FOURCC_tfhd, 0, 0, 0, flags);
3606   tfhd->track_ID = track_ID;
3607   tfhd->base_data_offset = 0;
3608   tfhd->sample_description_index = 1;
3609   tfhd->default_sample_duration = 0;
3610   tfhd->default_sample_size = 0;
3611   tfhd->default_sample_flags = 0;
3612 }
3613
3614 static void
3615 atom_trun_init (AtomTRUN * trun)
3616 {
3617   guint8 flags[3] = { 0, 0, 0 };
3618
3619   atom_full_init (&trun->header, FOURCC_trun, 0, 0, 0, flags);
3620   trun->sample_count = 0;
3621   trun->data_offset = 0;
3622   trun->first_sample_flags = 0;
3623   atom_array_init (&trun->entries, 512);
3624 }
3625
3626 static AtomTRUN *
3627 atom_trun_new (void)
3628 {
3629   AtomTRUN *trun = g_new0 (AtomTRUN, 1);
3630
3631   atom_trun_init (trun);
3632   return trun;
3633 }
3634
3635 static void
3636 atom_sdtp_init (AtomSDTP * sdtp)
3637 {
3638   guint8 flags[3] = { 0, 0, 0 };
3639
3640   atom_full_init (&sdtp->header, FOURCC_sdtp, 0, 0, 0, flags);
3641   atom_array_init (&sdtp->entries, 512);
3642 }
3643
3644 static AtomSDTP *
3645 atom_sdtp_new (AtomsContext * context)
3646 {
3647   AtomSDTP *sdtp = g_new0 (AtomSDTP, 1);
3648
3649   atom_sdtp_init (sdtp);
3650   return sdtp;
3651 }
3652
3653 static void
3654 atom_traf_add_sdtp (AtomTRAF * traf, AtomSDTP * sdtp)
3655 {
3656   traf->sdtps = g_list_append (traf->sdtps, sdtp);
3657 }
3658
3659 static void
3660 atom_sdtp_add_samples (AtomSDTP * sdtp, guint8 val)
3661 {
3662   /* it does not make much/any sense according to specs,
3663    * but that's how MS isml samples seem to do it */
3664   atom_array_append (&sdtp->entries, val, 256);
3665 }
3666
3667 static void
3668 atom_trun_add_samples (AtomTRUN * trun, guint32 delta, guint32 size,
3669     guint32 flags, gint64 pts_offset)
3670 {
3671   TRUNSampleEntry nentry;
3672
3673   if (pts_offset != 0)
3674     trun->header.flags[1] |= TR_COMPOSITION_TIME_OFFSETS;
3675
3676   nentry.sample_duration = delta;
3677   nentry.sample_size = size;
3678   nentry.sample_flags = flags;
3679   nentry.sample_composition_time_offset = pts_offset;
3680   atom_array_append (&trun->entries, nentry, 256);
3681   trun->sample_count++;
3682 }
3683
3684 static void
3685 atom_traf_init (AtomTRAF * traf, AtomsContext * context, guint32 track_ID)
3686 {
3687   atom_header_set (&traf->header, FOURCC_traf, 0, 0);
3688   atom_tfhd_init (&traf->tfhd, track_ID);
3689   traf->truns = NULL;
3690
3691   if (context->flavor == ATOMS_TREE_FLAVOR_ISML)
3692     atom_traf_add_sdtp (traf, atom_sdtp_new (context));
3693 }
3694
3695 AtomTRAF *
3696 atom_traf_new (AtomsContext * context, guint32 track_ID)
3697 {
3698   AtomTRAF *traf = g_new0 (AtomTRAF, 1);
3699
3700   atom_traf_init (traf, context, track_ID);
3701   return traf;
3702 }
3703
3704 static void
3705 atom_traf_add_trun (AtomTRAF * traf, AtomTRUN * trun)
3706 {
3707   traf->truns = g_list_append (traf->truns, trun);
3708 }
3709
3710 void
3711 atom_traf_add_samples (AtomTRAF * traf, guint32 delta, guint32 size,
3712     gboolean sync, gint64 pts_offset, gboolean sdtp_sync)
3713 {
3714   AtomTRUN *trun;
3715   guint32 flags;
3716
3717   /* 0x10000 is sample-is-difference-sample flag
3718    * low byte stuff is what ismv uses */
3719   flags = (sync ? 0x0 : 0x10000) | (sdtp_sync ? 0x40 : 0xc0);
3720
3721   if (G_UNLIKELY (!traf->truns)) {
3722     trun = atom_trun_new ();
3723     atom_traf_add_trun (traf, trun);
3724     /* optimistic; indicate all defaults present in tfhd */
3725     traf->tfhd.header.flags[2] = TF_DEFAULT_SAMPLE_DURATION |
3726         TF_DEFAULT_SAMPLE_SIZE | TF_DEFAULT_SAMPLE_FLAGS;
3727     traf->tfhd.default_sample_duration = delta;
3728     traf->tfhd.default_sample_size = size;
3729     traf->tfhd.default_sample_flags = flags;
3730     trun->first_sample_flags = flags;
3731   }
3732
3733   trun = traf->truns->data;
3734
3735   /* check if still matching defaults,
3736    * if not, abandon default and need entry for each sample */
3737   if (traf->tfhd.default_sample_duration != delta) {
3738     traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_DURATION;
3739     trun->header.flags[1] |= (TR_SAMPLE_DURATION >> 8);
3740   }
3741   if (traf->tfhd.default_sample_size != size) {
3742     traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_SIZE;
3743     trun->header.flags[1] |= (TR_SAMPLE_SIZE >> 8);
3744   }
3745   if (traf->tfhd.default_sample_flags != flags) {
3746     if (trun->sample_count == 1) {
3747       /* at least will need first sample flag */
3748       traf->tfhd.default_sample_flags = flags;
3749       trun->header.flags[2] |= TR_FIRST_SAMPLE_FLAGS;
3750     } else {
3751       /* now we need sample flags for each sample */
3752       traf->tfhd.header.flags[2] &= ~TF_DEFAULT_SAMPLE_FLAGS;
3753       trun->header.flags[1] |= (TR_SAMPLE_FLAGS >> 8);
3754       trun->header.flags[2] &= ~TR_FIRST_SAMPLE_FLAGS;
3755     }
3756   }
3757
3758   atom_trun_add_samples (traf->truns->data, delta, size, flags, pts_offset);
3759
3760   if (traf->sdtps)
3761     atom_sdtp_add_samples (traf->sdtps->data, 0x10 | ((flags & 0xff) >> 4));
3762 }
3763
3764 guint32
3765 atom_traf_get_sample_num (AtomTRAF * traf)
3766 {
3767   AtomTRUN *trun;
3768
3769   if (G_UNLIKELY (!traf->truns))
3770     return 0;
3771
3772   trun = traf->truns->data;
3773   return atom_array_get_len (&trun->entries);
3774 }
3775
3776 void
3777 atom_moof_add_traf (AtomMOOF * moof, AtomTRAF * traf)
3778 {
3779   moof->trafs = g_list_append (moof->trafs, traf);
3780 }
3781
3782 static void
3783 atom_tfra_free (AtomTFRA * tfra)
3784 {
3785   atom_full_clear (&tfra->header);
3786   atom_array_clear (&tfra->entries);
3787   g_free (tfra);
3788 }
3789
3790 AtomMFRA *
3791 atom_mfra_new (AtomsContext * context)
3792 {
3793   AtomMFRA *mfra = g_new0 (AtomMFRA, 1);
3794
3795   atom_header_set (&mfra->header, FOURCC_mfra, 0, 0);
3796   return mfra;
3797 }
3798
3799 void
3800 atom_mfra_add_tfra (AtomMFRA * mfra, AtomTFRA * tfra)
3801 {
3802   mfra->tfras = g_list_append (mfra->tfras, tfra);
3803 }
3804
3805 void
3806 atom_mfra_free (AtomMFRA * mfra)
3807 {
3808   GList *walker;
3809
3810   walker = mfra->tfras;
3811   while (walker) {
3812     atom_tfra_free ((AtomTFRA *) walker->data);
3813     walker = g_list_next (walker);
3814   }
3815   g_list_free (mfra->tfras);
3816   mfra->tfras = NULL;
3817
3818   atom_clear (&mfra->header);
3819   g_free (mfra);
3820 }
3821
3822 static void
3823 atom_tfra_init (AtomTFRA * tfra, guint32 track_ID)
3824 {
3825   guint8 flags[3] = { 0, 0, 0 };
3826
3827   atom_full_init (&tfra->header, FOURCC_tfra, 0, 0, 0, flags);
3828   tfra->track_ID = track_ID;
3829   atom_array_init (&tfra->entries, 512);
3830 }
3831
3832 AtomTFRA *
3833 atom_tfra_new (AtomsContext * context, guint32 track_ID)
3834 {
3835   AtomTFRA *tfra = g_new0 (AtomTFRA, 1);
3836
3837   atom_tfra_init (tfra, track_ID);
3838   return tfra;
3839
3840 }
3841
3842 static inline gint
3843 need_bytes (guint32 num)
3844 {
3845   gint n = 0;
3846
3847   while (num >>= 8)
3848     n++;
3849
3850   return n;
3851 }
3852
3853 void
3854 atom_tfra_add_entry (AtomTFRA * tfra, guint64 dts, guint32 sample_num)
3855 {
3856   TFRAEntry entry;
3857
3858   entry.time = dts;
3859   /* fill in later */
3860   entry.moof_offset = 0;
3861   /* always write a single trun in a single traf */
3862   entry.traf_number = 1;
3863   entry.trun_number = 1;
3864   entry.sample_number = sample_num;
3865
3866   /* auto-use 64 bits if needed */
3867   if (dts > G_MAXUINT32)
3868     tfra->header.version = 1;
3869
3870   /* 1 byte will always do for traf and trun number,
3871    * check how much sample_num needs */
3872   tfra->lengths = (tfra->lengths & 0xfc) ||
3873       MAX (tfra->lengths, need_bytes (sample_num));
3874
3875   atom_array_append (&tfra->entries, entry, 256);
3876 }
3877
3878 void
3879 atom_tfra_update_offset (AtomTFRA * tfra, guint64 offset)
3880 {
3881   gint i;
3882
3883   /* auto-use 64 bits if needed */
3884   if (offset > G_MAXUINT32)
3885     tfra->header.version = 1;
3886
3887   for (i = atom_array_get_len (&tfra->entries) - 1; i >= 0; i--) {
3888     TFRAEntry *entry = &atom_array_index (&tfra->entries, i);
3889
3890     if (entry->moof_offset)
3891       break;
3892     entry->moof_offset = offset;
3893   }
3894 }
3895
3896 static guint64
3897 atom_tfra_copy_data (AtomTFRA * tfra, guint8 ** buffer, guint64 * size,
3898     guint64 * offset)
3899 {
3900   guint64 original_offset = *offset;
3901   guint32 i;
3902   TFRAEntry *entry;
3903   guint32 data;
3904   guint bytes;
3905   guint version;
3906
3907   if (!atom_full_copy_data (&tfra->header, buffer, size, offset)) {
3908     return 0;
3909   }
3910
3911   prop_copy_uint32 (tfra->track_ID, buffer, size, offset);
3912   prop_copy_uint32 (tfra->lengths, buffer, size, offset);
3913   prop_copy_uint32 (atom_array_get_len (&tfra->entries), buffer, size, offset);
3914
3915   version = tfra->header.version;
3916   for (i = 0; i < atom_array_get_len (&tfra->entries); ++i) {
3917     entry = &atom_array_index (&tfra->entries, i);
3918     if (version) {
3919       prop_copy_uint64 (entry->time, buffer, size, offset);
3920       prop_copy_uint64 (entry->moof_offset, buffer, size, offset);
3921     } else {
3922       prop_copy_uint32 (entry->time, buffer, size, offset);
3923       prop_copy_uint32 (entry->moof_offset, buffer, size, offset);
3924     }
3925
3926     bytes = (tfra->lengths & (0x3 << 4)) + 1;
3927     data = GUINT32_TO_BE (entry->traf_number);
3928     prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3929         buffer, size, offset);
3930
3931     bytes = (tfra->lengths & (0x3 << 2)) + 1;
3932     data = GUINT32_TO_BE (entry->trun_number);
3933     prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3934         buffer, size, offset);
3935
3936     bytes = (tfra->lengths & (0x3)) + 1;
3937     data = GUINT32_TO_BE (entry->sample_number);
3938     prop_copy_fixed_size_string (((guint8 *) & data) + 4 - bytes, bytes,
3939         buffer, size, offset);
3940
3941   }
3942
3943   atom_write_size (buffer, size, offset, original_offset);
3944   return *offset - original_offset;
3945 }
3946
3947 static guint64
3948 atom_mfro_copy_data (guint32 s, guint8 ** buffer, guint64 * size,
3949     guint64 * offset)
3950 {
3951   guint64 original_offset = *offset;
3952   guint8 flags[3] = { 0, 0, 0 };
3953   AtomFull mfro;
3954
3955   atom_full_init (&mfro, FOURCC_mfro, 0, 0, 0, flags);
3956
3957   if (!atom_full_copy_data (&mfro, buffer, size, offset)) {
3958     return 0;
3959   }
3960
3961   prop_copy_uint32 (s, buffer, size, offset);
3962
3963   atom_write_size (buffer, size, offset, original_offset);
3964
3965   return *offset - original_offset;
3966 }
3967
3968
3969 guint64
3970 atom_mfra_copy_data (AtomMFRA * mfra, guint8 ** buffer, guint64 * size,
3971     guint64 * offset)
3972 {
3973   guint64 original_offset = *offset;
3974   GList *walker;
3975
3976   if (!atom_copy_data (&mfra->header, buffer, size, offset))
3977     return 0;
3978
3979   walker = g_list_first (mfra->tfras);
3980   while (walker != NULL) {
3981     if (!atom_tfra_copy_data ((AtomTFRA *) walker->data, buffer, size, offset)) {
3982       return 0;
3983     }
3984     walker = g_list_next (walker);
3985   }
3986
3987   /* 16 is the size of the mfro atom */
3988   if (!atom_mfro_copy_data (*offset - original_offset + 16, buffer,
3989           size, offset))
3990     return 0;
3991
3992   atom_write_size (buffer, size, offset, original_offset);
3993   return *offset - original_offset;
3994 }
3995
3996 /* some sample description construction helpers */
3997
3998 AtomInfo *
3999 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
4000     const GstBuffer * codec_data, guint32 avg_bitrate, guint32 max_bitrate)
4001 {
4002   guint32 track_id;
4003   AtomESDS *esds;
4004
4005   track_id = trak->tkhd.track_ID;
4006
4007   esds = atom_esds_new ();
4008   esds->es.id = track_id & 0xFFFF;
4009   esds->es.dec_conf_desc.object_type = object_type;
4010   esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
4011
4012   if (avg_bitrate > 0)
4013     esds->es.dec_conf_desc.avg_bitrate = avg_bitrate;
4014   if (max_bitrate > 0)
4015     esds->es.dec_conf_desc.max_bitrate = max_bitrate;
4016
4017   /* optional DecoderSpecificInfo */
4018   if (codec_data) {
4019     DecoderSpecificInfoDescriptor *desc;
4020
4021     esds->es.dec_conf_desc.dec_specific_info = desc =
4022         desc_dec_specific_info_new ();
4023     desc_dec_specific_info_alloc_data (desc, GST_BUFFER_SIZE (codec_data));
4024
4025     memcpy (desc->data, GST_BUFFER_DATA (codec_data),
4026         GST_BUFFER_SIZE (codec_data));
4027   }
4028
4029   return build_atom_info_wrapper ((Atom *) esds, atom_esds_copy_data,
4030       atom_esds_free);
4031 }
4032
4033 AtomInfo *
4034 build_btrt_extension (guint32 buffer_size_db, guint32 avg_bitrate,
4035     guint32 max_bitrate)
4036 {
4037   AtomData *atom_data;
4038   GstBuffer *buf;
4039
4040   buf = gst_buffer_new_and_alloc (12);
4041
4042   GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf), buffer_size_db);
4043   GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf) + 4, max_bitrate);
4044   GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf) + 8, avg_bitrate);
4045
4046   atom_data = atom_data_new_from_gst_buffer (FOURCC_btrt, buf);
4047   gst_buffer_unref (buf);
4048
4049   return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4050       atom_data_free);
4051 }
4052
4053 static AtomInfo *
4054 build_mov_wave_extension (AtomTRAK * trak, guint32 fourcc, AtomInfo * atom1,
4055     AtomInfo * atom2, gboolean terminator)
4056 {
4057   AtomWAVE *wave;
4058   AtomFRMA *frma;
4059   Atom *ext_atom;
4060
4061   /* Build WAVE atom for sample table entry */
4062   wave = atom_wave_new ();
4063
4064   /* Prepend Terminator atom to the WAVE list first, so it ends up last */
4065   if (terminator) {
4066     ext_atom = (Atom *) atom_data_new (FOURCC_null);
4067     wave->extension_atoms =
4068         atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4069         (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4070   }
4071
4072   /* Add supplied atoms to WAVE */
4073   if (atom2)
4074     wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom2);
4075   if (atom1)
4076     wave->extension_atoms = g_list_prepend (wave->extension_atoms, atom1);
4077
4078   /* Add FRMA to the WAVE */
4079   frma = atom_frma_new ();
4080   frma->media_type = fourcc;
4081
4082   wave->extension_atoms =
4083       atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4084       (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4085
4086   return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4087       atom_wave_free);
4088 }
4089
4090 AtomInfo *
4091 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data,
4092     guint32 avg_bitrate, guint32 max_bitrate)
4093 {
4094   AtomInfo *esds, *mp4a;
4095   GstBuffer *buf;
4096
4097   /* Add ESDS atom to WAVE */
4098   esds = build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
4099       ESDS_STREAM_TYPE_AUDIO, codec_data, avg_bitrate, max_bitrate);
4100
4101   /* Add MP4A atom to the WAVE:
4102    * not really in spec, but makes offset based players happy */
4103   buf = gst_buffer_new_and_alloc (4);
4104   *((guint32 *) GST_BUFFER_DATA (buf)) = 0;
4105   mp4a = build_codec_data_extension (FOURCC_mp4a, buf);
4106   gst_buffer_unref (buf);
4107
4108   return build_mov_wave_extension (trak, FOURCC_mp4a, mp4a, esds, TRUE);
4109 }
4110
4111 AtomInfo *
4112 build_mov_alac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
4113 {
4114   AtomInfo *alac;
4115
4116   alac = build_codec_data_extension (FOURCC_alac, codec_data);
4117
4118   return build_mov_wave_extension (trak, FOURCC_alac, NULL, alac, TRUE);
4119 }
4120
4121 AtomInfo *
4122 build_fiel_extension (gint fields)
4123 {
4124   AtomData *atom_data;
4125   GstBuffer *buf;
4126
4127   if (fields == 1) {
4128     return NULL;
4129   }
4130
4131   buf = gst_buffer_new_and_alloc (1);
4132   GST_BUFFER_DATA (buf)[0] = (guint8) fields;
4133
4134   atom_data =
4135       atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('f', 'i', 'e', 'l'), buf);
4136   gst_buffer_unref (buf);
4137
4138   return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4139       atom_data_free);
4140 }
4141
4142 AtomInfo *
4143 build_jp2x_extension (const GstBuffer * prefix)
4144 {
4145   AtomData *atom_data;
4146
4147   if (!prefix) {
4148     return NULL;
4149   }
4150
4151   atom_data =
4152       atom_data_new_from_gst_buffer (GST_MAKE_FOURCC ('j', 'p', '2', 'x'),
4153       prefix);
4154
4155   return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4156       atom_data_free);
4157 }
4158
4159 AtomInfo *
4160 build_jp2h_extension (AtomTRAK * trak, gint width, gint height, guint32 fourcc,
4161     gint ncomp, const GValue * cmap_array, const GValue * cdef_array)
4162 {
4163   AtomData *atom_data;
4164   GstBuffer *buf;
4165   guint8 cenum;
4166   gint i;
4167   gint idhr_size = 22;
4168   gint colr_size = 15;
4169   gint cmap_size = 0, cdef_size = 0;
4170   gint cmap_array_size = 0;
4171   gint cdef_array_size = 0;
4172   GstByteWriter writer;
4173
4174   g_return_val_if_fail (cmap_array == NULL ||
4175       GST_VALUE_HOLDS_ARRAY (cmap_array), NULL);
4176   g_return_val_if_fail (cdef_array == NULL ||
4177       GST_VALUE_HOLDS_ARRAY (cdef_array), NULL);
4178
4179   if (fourcc == GST_MAKE_FOURCC ('s', 'R', 'G', 'B')) {
4180     cenum = 0x10;
4181     if (ncomp == 0)
4182       ncomp = 3;
4183   } else if (fourcc == GST_MAKE_FOURCC ('G', 'R', 'A', 'Y')) {
4184     cenum = 0x11;
4185     if (ncomp == 0)
4186       ncomp = 1;
4187   } else if (fourcc == GST_MAKE_FOURCC ('s', 'Y', 'U', 'V')) {
4188     cenum = 0x12;
4189     if (ncomp == 0)
4190       ncomp = 3;
4191   } else
4192     return NULL;
4193
4194   if (cmap_array) {
4195     cmap_array_size = gst_value_array_get_size (cmap_array);
4196     cmap_size = 8 + cmap_array_size * 4;
4197   }
4198   if (cdef_array) {
4199     cdef_array_size = gst_value_array_get_size (cdef_array);
4200     cdef_size = 8 + 2 + cdef_array_size * 6;
4201   }
4202
4203   buf = gst_buffer_new_and_alloc (idhr_size + colr_size + cmap_size +
4204       cdef_size);
4205   gst_byte_writer_init_with_buffer (&writer, buf, FALSE);
4206
4207   /* ihdr = image header box */
4208   gst_byte_writer_put_uint32_be (&writer, 22);
4209   gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
4210   gst_byte_writer_put_uint32_be (&writer, height);
4211   gst_byte_writer_put_uint32_be (&writer, width);
4212   gst_byte_writer_put_uint16_be (&writer, ncomp);
4213   /* 8 bits per component, unsigned */
4214   gst_byte_writer_put_uint8 (&writer, 0x7);
4215   /* compression type; reserved */
4216   gst_byte_writer_put_uint8 (&writer, 0x7);
4217   /* colour space (un)known */
4218   gst_byte_writer_put_uint8 (&writer, 0x0);
4219   /* intellectual property right (box present) */
4220   gst_byte_writer_put_uint8 (&writer, 0x0);
4221
4222   /* colour specification box */
4223   gst_byte_writer_put_uint32_be (&writer, 15);
4224   gst_byte_writer_put_uint32_le (&writer, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
4225
4226   /* specification method: enumerated */
4227   gst_byte_writer_put_uint8 (&writer, 0x1);
4228   /* precedence; reserved */
4229   gst_byte_writer_put_uint8 (&writer, 0x0);
4230   /* approximation; reserved */
4231   gst_byte_writer_put_uint8 (&writer, 0x0);
4232   /* enumerated colourspace */
4233   gst_byte_writer_put_uint32_be (&writer, cenum);
4234
4235   if (cmap_array) {
4236     gst_byte_writer_put_uint32_be (&writer, cmap_size);
4237     gst_byte_writer_put_uint32_le (&writer,
4238         GST_MAKE_FOURCC ('c', 'm', 'a', 'p'));
4239     for (i = 0; i < cmap_array_size; i++) {
4240       const GValue *item;
4241       gint value;
4242       guint16 cmp;
4243       guint8 mtyp;
4244       guint8 pcol;
4245       item = gst_value_array_get_value (cmap_array, i);
4246       value = g_value_get_int (item);
4247
4248       /* value is '(mtyp << 24) | (pcol << 16) | cmp' */
4249       cmp = value & 0xFFFF;
4250       mtyp = value >> 24;
4251       pcol = (value >> 16) & 0xFF;
4252
4253       if (mtyp == 1)
4254         GST_WARNING ("MTYP of cmap atom signals Pallete Mapping, but we don't "
4255             "handle Pallete mapping atoms yet");
4256
4257       gst_byte_writer_put_uint16_be (&writer, cmp);
4258       gst_byte_writer_put_uint8 (&writer, mtyp);
4259       gst_byte_writer_put_uint8 (&writer, pcol);
4260     }
4261   }
4262
4263   if (cdef_array) {
4264     gst_byte_writer_put_uint32_be (&writer, cdef_size);
4265     gst_byte_writer_put_uint32_le (&writer,
4266         GST_MAKE_FOURCC ('c', 'd', 'e', 'f'));
4267     gst_byte_writer_put_uint16_be (&writer, cdef_array_size);
4268     for (i = 0; i < cdef_array_size; i++) {
4269       const GValue *item;
4270       gint value;
4271       item = gst_value_array_get_value (cdef_array, i);
4272       value = g_value_get_int (item);
4273
4274       gst_byte_writer_put_uint16_be (&writer, i);
4275       if (value > 0) {
4276         gst_byte_writer_put_uint16_be (&writer, 0);
4277         gst_byte_writer_put_uint16_be (&writer, value);
4278       } else if (value < 0) {
4279         gst_byte_writer_put_uint16_be (&writer, -value);
4280         gst_byte_writer_put_uint16_be (&writer, 0);     /* TODO what here? */
4281       } else {
4282         gst_byte_writer_put_uint16_be (&writer, 1);
4283         gst_byte_writer_put_uint16_be (&writer, 0);
4284       }
4285     }
4286   }
4287
4288   g_assert (gst_byte_writer_get_remaining (&writer) == 0);
4289
4290   atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
4291   gst_buffer_unref (buf);
4292
4293   return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4294       atom_data_free);
4295 }
4296
4297 AtomInfo *
4298 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
4299 {
4300   AtomData *data;
4301   AtomInfo *result = NULL;
4302
4303   if (codec_data) {
4304     data = atom_data_new_from_gst_buffer (fourcc, codec_data);
4305     result = build_atom_info_wrapper ((Atom *) data, atom_data_copy_data,
4306         atom_data_free);
4307   }
4308
4309   return result;
4310 }
4311
4312 AtomInfo *
4313 build_amr_extension (void)
4314 {
4315   guint8 ext[9];
4316   GstBuffer *buf;
4317   AtomInfo *res;
4318
4319   buf = gst_buffer_new ();
4320   GST_BUFFER_DATA (buf) = ext;
4321   GST_BUFFER_SIZE (buf) = sizeof (ext);
4322
4323   /* vendor */
4324   GST_WRITE_UINT32_LE (ext, 0);
4325   /* decoder version */
4326   GST_WRITE_UINT8 (ext + 4, 0);
4327   /* mode set (all modes) */
4328   GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
4329   /* mode change period (no restriction) */
4330   GST_WRITE_UINT8 (ext + 7, 0);
4331   /* frames per sample */
4332   GST_WRITE_UINT8 (ext + 8, 1);
4333
4334   res = build_codec_data_extension (GST_MAKE_FOURCC ('d', 'a', 'm', 'r'), buf);
4335   gst_buffer_unref (buf);
4336   return res;
4337 }
4338
4339 AtomInfo *
4340 build_h263_extension (void)
4341 {
4342   guint8 ext[7];
4343   GstBuffer *buf;
4344   AtomInfo *res;
4345
4346   buf = gst_buffer_new ();
4347   GST_BUFFER_DATA (buf) = ext;
4348   GST_BUFFER_SIZE (buf) = sizeof (ext);
4349
4350   /* vendor */
4351   GST_WRITE_UINT32_LE (ext, 0);
4352   /* decoder version */
4353   GST_WRITE_UINT8 (ext + 4, 0);
4354   /* level / profile */
4355   /* FIXME ? maybe ? obtain somewhere; baseline for now */
4356   GST_WRITE_UINT8 (ext + 5, 10);
4357   GST_WRITE_UINT8 (ext + 6, 0);
4358
4359   res = build_codec_data_extension (GST_MAKE_FOURCC ('d', '2', '6', '3'), buf);
4360   gst_buffer_unref (buf);
4361   return res;
4362 }
4363
4364 AtomInfo *
4365 build_gama_atom (gdouble gamma)
4366 {
4367   AtomInfo *res;
4368   guint32 gamma_fp;
4369   GstBuffer *buf;
4370
4371   /* convert to uint32 from fixed point */
4372   gamma_fp = (guint32) 65536 *gamma;
4373
4374   buf = gst_buffer_new_and_alloc (4);
4375   GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf), gamma_fp);
4376   res = build_codec_data_extension (FOURCC_gama, buf);
4377   gst_buffer_unref (buf);
4378   return res;
4379 }
4380
4381 AtomInfo *
4382 build_SMI_atom (const GstBuffer * seqh)
4383 {
4384   AtomInfo *res;
4385   GstBuffer *buf;
4386
4387   /* the seqh plus its size and fourcc */
4388   buf = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (seqh) + 8);
4389
4390   GST_WRITE_UINT32_LE (GST_BUFFER_DATA (buf), FOURCC_SEQH);
4391   GST_WRITE_UINT32_BE (GST_BUFFER_DATA (buf) + 4, GST_BUFFER_SIZE (seqh));
4392   memcpy (GST_BUFFER_DATA (buf) + 8, GST_BUFFER_DATA (seqh),
4393       GST_BUFFER_SIZE (seqh));
4394   res = build_codec_data_extension (FOURCC_SMI_, buf);
4395   gst_buffer_unref (buf);
4396   return res;
4397 }
4398
4399 static AtomInfo *
4400 build_ima_adpcm_atom (gint channels, gint rate, gint blocksize)
4401 {
4402   AtomData *atom_data;
4403   GstBuffer *buf;
4404   guint8 *data;
4405   const gint ima_adpcm_atom_size = 20;
4406   guint32 fourcc;
4407   gint samplesperblock;
4408   gint bytespersec;
4409
4410   /* The FOURCC for WAV codecs in QT is 'ms' followed by the 16 bit wave codec
4411      identifier. Note that the identifier here is big-endian, but when used
4412      within the WAVE header (below), it's little endian. */
4413   fourcc = MS_WAVE_FOURCC (0x11);
4414
4415   buf = gst_buffer_new_and_alloc (ima_adpcm_atom_size);
4416   data = GST_BUFFER_DATA (buf);
4417
4418   /* This atom's content is a WAVE header, including 2 bytes of extra data.
4419      Note that all of this is little-endian, unlike most stuff in qt. */
4420   /* 4 bytes header per channel (including 1 sample). Then 2 samples per byte
4421      for the rest. Simplifies to this. */
4422   samplesperblock = 2 * blocksize / channels - 7;
4423   bytespersec = rate * blocksize / samplesperblock;
4424   GST_WRITE_UINT16_LE (data, 0x11);
4425   GST_WRITE_UINT16_LE (data + 2, channels);
4426   GST_WRITE_UINT32_LE (data + 4, rate);
4427   GST_WRITE_UINT32_LE (data + 8, bytespersec);
4428   GST_WRITE_UINT16_LE (data + 12, blocksize);
4429   GST_WRITE_UINT16_LE (data + 14, 4);
4430   GST_WRITE_UINT16_LE (data + 16, 2);   /* Two extra bytes */
4431   GST_WRITE_UINT16_LE (data + 18, samplesperblock);
4432
4433   atom_data = atom_data_new_from_gst_buffer (fourcc, buf);
4434   gst_buffer_unref (buf);
4435
4436   return build_atom_info_wrapper ((Atom *) atom_data, atom_data_copy_data,
4437       atom_data_free);
4438 }
4439
4440 AtomInfo *
4441 build_ima_adpcm_extension (gint channels, gint rate, gint blocksize)
4442 {
4443   AtomWAVE *wave;
4444   AtomFRMA *frma;
4445   Atom *ext_atom;
4446
4447   /* Add WAVE atom */
4448   wave = atom_wave_new ();
4449
4450   /* Prepend Terminator atom to the WAVE list first, so it ends up last */
4451   ext_atom = (Atom *) atom_data_new (FOURCC_null);
4452   wave->extension_atoms =
4453       atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
4454       (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
4455
4456   /* Add wave ima adpcm atom to WAVE */
4457   wave->extension_atoms = g_list_prepend (wave->extension_atoms,
4458       build_ima_adpcm_atom (channels, rate, blocksize));
4459
4460   /* Add FRMA to the WAVE */
4461   frma = atom_frma_new ();
4462   frma->media_type = MS_WAVE_FOURCC (0x11);
4463
4464   wave->extension_atoms =
4465       atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
4466       (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
4467
4468   return build_atom_info_wrapper ((Atom *) wave, atom_wave_copy_data,
4469       atom_wave_free);
4470 }
4471
4472 AtomInfo *
4473 build_uuid_xmp_atom (GstBuffer * xmp_data)
4474 {
4475   AtomUUID *uuid;
4476   static guint8 xmp_uuid[] = { 0xBE, 0x7A, 0xCF, 0xCB,
4477     0x97, 0xA9, 0x42, 0xE8,
4478     0x9C, 0x71, 0x99, 0x94,
4479     0x91, 0xE3, 0xAF, 0xAC
4480   };
4481
4482   if (xmp_data == NULL)
4483     return NULL;
4484
4485   uuid = atom_uuid_new ();
4486   memcpy (uuid->uuid, xmp_uuid, 16);
4487
4488   uuid->data = g_malloc (GST_BUFFER_SIZE (xmp_data));
4489   uuid->datalen = GST_BUFFER_SIZE (xmp_data);
4490   memcpy (uuid->data, GST_BUFFER_DATA (xmp_data), GST_BUFFER_SIZE (xmp_data));
4491
4492   return build_atom_info_wrapper ((Atom *) uuid, atom_uuid_copy_data,
4493       atom_uuid_free);
4494 }