gst_plugins_good/gst/qtmux/atoms.c
branchRCL_3
changeset 29 567bb019e3e3
equal deleted inserted replaced
6:9b2c3c7a1a9c 29:567bb019e3e3
       
     1 /* Quicktime muxer plugin for GStreamer
       
     2  * Copyright (C) 2008 Thiago Sousa 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 /* only needed for gst_util_uint64_scale */
       
    49 #include <gst/gst.h>
       
    50 
       
    51 /**
       
    52  * Creates a new AtomsContext for the given flavor.
       
    53  */
       
    54 AtomsContext *
       
    55 atoms_context_new (AtomsTreeFlavor flavor)
       
    56 {
       
    57   AtomsContext *context = g_new0 (AtomsContext, 1);
       
    58   context->flavor = flavor;
       
    59   return context;
       
    60 }
       
    61 
       
    62 /**
       
    63  * Frees an AtomsContext and all memory associated with it
       
    64  */
       
    65 void
       
    66 atoms_context_free (AtomsContext * context)
       
    67 {
       
    68   g_free (context);
       
    69 }
       
    70 
       
    71 /* -- creation, initialization, clear and free functions -- */
       
    72 
       
    73 #define SECS_PER_DAY (24 * 60 * 60)
       
    74 #define LEAP_YEARS_FROM_1904_TO_1970 17
       
    75 
       
    76 static guint64
       
    77 get_current_qt_time ()
       
    78 {
       
    79   GTimeVal timeval;
       
    80 
       
    81   g_get_current_time (&timeval);
       
    82   /* FIXME this should use UTC coordinated time */
       
    83   return timeval.tv_sec + (((1970 - 1904) * (guint64) 365) +
       
    84       LEAP_YEARS_FROM_1904_TO_1970) * SECS_PER_DAY;
       
    85 }
       
    86 
       
    87 static void
       
    88 common_time_info_init (TimeInfo * ti)
       
    89 {
       
    90   ti->creation_time = ti->modification_time = get_current_qt_time ();
       
    91   ti->timescale = 0;
       
    92   ti->duration = 0;
       
    93 }
       
    94 
       
    95 static void
       
    96 atom_header_set (Atom * header, guint32 fourcc, gint32 size, gint64 ext_size)
       
    97 {
       
    98   header->type = fourcc;
       
    99   header->size = size;
       
   100   header->extended_size = ext_size;
       
   101 }
       
   102 
       
   103 static void
       
   104 atom_clear (Atom * atom)
       
   105 {
       
   106 }
       
   107 
       
   108 static void
       
   109 atom_full_init (AtomFull * full, guint32 fourcc, gint32 size, gint64 ext_size,
       
   110     guint8 version, guint8 flags[3])
       
   111 {
       
   112   atom_header_set (&(full->header), fourcc, size, ext_size);
       
   113   full->version = version;
       
   114   full->flags[0] = flags[0];
       
   115   full->flags[1] = flags[1];
       
   116   full->flags[2] = flags[2];
       
   117 }
       
   118 
       
   119 static void
       
   120 atom_full_clear (AtomFull * full)
       
   121 {
       
   122   atom_clear (&full->header);
       
   123 }
       
   124 
       
   125 static void
       
   126 atom_full_free (AtomFull * full)
       
   127 {
       
   128   atom_full_clear (full);
       
   129   g_free (full);
       
   130 }
       
   131 
       
   132 static AtomInfo *
       
   133 build_atom_info_wrapper (Atom * atom, gpointer copy_func, gpointer free_func)
       
   134 {
       
   135   AtomInfo *info = NULL;
       
   136 
       
   137   if (atom) {
       
   138     info = g_new0 (AtomInfo, 1);
       
   139 
       
   140     info->atom = atom;
       
   141     info->copy_data_func = copy_func;
       
   142     info->free_func = free_func;
       
   143   }
       
   144 
       
   145   return info;
       
   146 }
       
   147 
       
   148 static GList *
       
   149 atom_info_list_prepend_atom (GList * ai, Atom * atom,
       
   150     AtomCopyDataFunc copy_func, AtomFreeFunc free_func)
       
   151 {
       
   152   if (atom)
       
   153     return g_list_prepend (ai,
       
   154         build_atom_info_wrapper (atom, (gpointer)copy_func, (gpointer)free_func));
       
   155   else
       
   156     return ai;
       
   157 }
       
   158 
       
   159 static void
       
   160 atom_info_list_free (GList * ai)
       
   161 {
       
   162   while (ai) {
       
   163     AtomInfo *info = (AtomInfo *) ai->data;
       
   164 
       
   165     info->free_func (info->atom);
       
   166     g_free (info);
       
   167     ai = g_list_delete_link (ai, ai);
       
   168   }
       
   169 }
       
   170 
       
   171 static AtomData *
       
   172 atom_data_new (guint32 fourcc)
       
   173 {
       
   174   AtomData *data = g_new0 (AtomData, 1);
       
   175 
       
   176   atom_header_set (&data->header, fourcc, 0, 0);
       
   177   return data;
       
   178 }
       
   179 
       
   180 static void
       
   181 atom_data_alloc_mem (AtomData * data, guint32 size)
       
   182 {
       
   183   if (data->data) {
       
   184     g_free (data->data);
       
   185   }
       
   186   data->data = g_new0 (guint8, size);
       
   187   data->datalen = size;
       
   188 }
       
   189 
       
   190 static AtomData *
       
   191 atom_data_new_from_gst_buffer (guint32 fourcc, const GstBuffer * buf)
       
   192 {
       
   193   AtomData *data = atom_data_new (fourcc);
       
   194 
       
   195   atom_data_alloc_mem (data, GST_BUFFER_SIZE (buf));
       
   196   g_memmove (data->data, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
       
   197   return data;
       
   198 }
       
   199 
       
   200 static void
       
   201 atom_data_free (AtomData * data)
       
   202 {
       
   203   atom_clear (&data->header);
       
   204   g_free (data->data);
       
   205   g_free (data);
       
   206 }
       
   207 
       
   208 static void
       
   209 atom_ftyp_init (AtomFTYP * ftyp, guint32 major, guint32 version, GList * brands)
       
   210 {
       
   211   gint index;
       
   212   GList *it = NULL;
       
   213 
       
   214   atom_header_set (&ftyp->header, FOURCC_ftyp, 16, 0);
       
   215   ftyp->major_brand = major;
       
   216   ftyp->version = version;
       
   217 
       
   218   /* always include major brand as compatible brand */
       
   219   ftyp->compatible_brands_size = g_list_length (brands) + 1;
       
   220   ftyp->compatible_brands = g_new (guint32, ftyp->compatible_brands_size);
       
   221 
       
   222   ftyp->compatible_brands[0] = major;
       
   223   index = 1;
       
   224   for (it = brands; it != NULL; it = g_list_next (it)) {
       
   225     ftyp->compatible_brands[index++] = GPOINTER_TO_UINT (it->data);
       
   226   }
       
   227 }
       
   228 
       
   229 AtomFTYP *
       
   230 atom_ftyp_new (AtomsContext * context, guint32 major, guint32 version,
       
   231     GList * brands)
       
   232 {
       
   233   AtomFTYP *ftyp = g_new0 (AtomFTYP, 1);
       
   234 
       
   235   atom_ftyp_init (ftyp, major, version, brands);
       
   236   return ftyp;
       
   237 }
       
   238 
       
   239 void
       
   240 atom_ftyp_free (AtomFTYP * ftyp)
       
   241 {
       
   242   atom_clear (&ftyp->header);
       
   243   g_free (ftyp->compatible_brands);
       
   244   ftyp->compatible_brands = NULL;
       
   245   g_free (ftyp);
       
   246 }
       
   247 
       
   248 static void
       
   249 atom_esds_init (AtomESDS * esds)
       
   250 {
       
   251   guint8 flags[3] = { 0, 0, 0 };
       
   252 
       
   253   atom_full_init (&esds->header, FOURCC_esds, 0, 0, 0, flags);
       
   254   desc_es_init (&esds->es);
       
   255 }
       
   256 
       
   257 static AtomESDS *
       
   258 atom_esds_new ()
       
   259 {
       
   260   AtomESDS *esds = g_new0 (AtomESDS, 1);
       
   261 
       
   262   atom_esds_init (esds);
       
   263   return esds;
       
   264 }
       
   265 
       
   266 static void
       
   267 atom_esds_free (AtomESDS * esds)
       
   268 {
       
   269   atom_full_clear (&esds->header);
       
   270   desc_es_descriptor_clear (&esds->es);
       
   271   g_free (esds);
       
   272 }
       
   273 
       
   274 static AtomFRMA *
       
   275 atom_frma_new ()
       
   276 {
       
   277   AtomFRMA *frma = g_new0 (AtomFRMA, 1);
       
   278 
       
   279   atom_header_set (&frma->header, FOURCC_frma, 0, 0);
       
   280   return frma;
       
   281 }
       
   282 
       
   283 static void
       
   284 atom_frma_free (AtomFRMA * frma)
       
   285 {
       
   286   atom_clear (&frma->header);
       
   287   g_free (frma);
       
   288 }
       
   289 
       
   290 static AtomWAVE *
       
   291 atom_wave_new ()
       
   292 {
       
   293   AtomWAVE *wave = g_new0 (AtomWAVE, 1);
       
   294 
       
   295   atom_header_set (&wave->header, FOURCC_wave, 0, 0);
       
   296   return wave;
       
   297 }
       
   298 
       
   299 static void
       
   300 atom_wave_free (AtomWAVE * wave)
       
   301 {
       
   302   atom_clear (&wave->header);
       
   303   atom_info_list_free (wave->extension_atoms);
       
   304   g_free (wave);
       
   305 }
       
   306 
       
   307 static void
       
   308 atom_sample_entry_init (SampleTableEntry * se, guint32 type)
       
   309 {
       
   310   atom_header_set (&se->header, type, 0, 0);
       
   311 
       
   312   memset (se->reserved, 0, sizeof (guint8) * 6);
       
   313   se->data_reference_index = 0;
       
   314 }
       
   315 
       
   316 static void
       
   317 atom_sample_entry_free (SampleTableEntry * se)
       
   318 {
       
   319   atom_clear (&se->header);
       
   320 }
       
   321 
       
   322 static void
       
   323 sample_entry_mp4a_init (SampleTableEntryMP4A * mp4a)
       
   324 {
       
   325   atom_sample_entry_init (&mp4a->se, FOURCC_mp4a);
       
   326 
       
   327   mp4a->version = 0;
       
   328   mp4a->revision_level = 0;
       
   329   mp4a->vendor = 0;
       
   330   mp4a->channels = 2;
       
   331   mp4a->sample_size = 16;
       
   332   mp4a->compression_id = 0;
       
   333   mp4a->packet_size = 0;
       
   334   mp4a->sample_rate = 0;
       
   335   /* following only used if version is 1 */
       
   336   mp4a->samples_per_packet = 0;
       
   337   mp4a->bytes_per_packet = 0;
       
   338   mp4a->bytes_per_frame = 0;
       
   339   mp4a->bytes_per_sample = 0;
       
   340 
       
   341   mp4a->extension_atoms = NULL;
       
   342 }
       
   343 
       
   344 static SampleTableEntryMP4A *
       
   345 sample_entry_mp4a_new ()
       
   346 {
       
   347   SampleTableEntryMP4A *mp4a = g_new0 (SampleTableEntryMP4A, 1);
       
   348 
       
   349   sample_entry_mp4a_init (mp4a);
       
   350   return mp4a;
       
   351 }
       
   352 
       
   353 static void
       
   354 sample_entry_mp4a_free (SampleTableEntryMP4A * mp4a)
       
   355 {
       
   356   atom_sample_entry_free (&mp4a->se);
       
   357   atom_info_list_free (mp4a->extension_atoms);
       
   358   g_free (mp4a);
       
   359 }
       
   360 
       
   361 static void
       
   362 sample_entry_mp4v_init (SampleTableEntryMP4V * mp4v, AtomsContext * context)
       
   363 {
       
   364   atom_sample_entry_init (&mp4v->se, FOURCC_mp4v);
       
   365 
       
   366   mp4v->version = 0;
       
   367   mp4v->revision_level = 0;
       
   368   mp4v->vendor = 0;
       
   369 
       
   370   mp4v->temporal_quality = 0;
       
   371   mp4v->spatial_quality = 0;
       
   372 
       
   373   /* qt and ISO base media do not contradict, and examples agree */
       
   374   mp4v->horizontal_resolution = 0x00480000;
       
   375   mp4v->vertical_resolution = 0x00480000;
       
   376 
       
   377   mp4v->datasize = 0;
       
   378   mp4v->frame_count = 1;
       
   379 
       
   380   memset (mp4v->compressor, 0, sizeof (guint8) * 32);
       
   381 
       
   382   mp4v->depth = 0;
       
   383   mp4v->color_table_id = 0;
       
   384 
       
   385   mp4v->extension_atoms = NULL;
       
   386 }
       
   387 
       
   388 static void
       
   389 sample_entry_mp4v_free (SampleTableEntryMP4V * mp4v)
       
   390 {
       
   391   atom_sample_entry_free (&mp4v->se);
       
   392   atom_info_list_free (mp4v->extension_atoms);
       
   393   g_free (mp4v);
       
   394 }
       
   395 
       
   396 static SampleTableEntryMP4V *
       
   397 sample_entry_mp4v_new (AtomsContext * context)
       
   398 {
       
   399   SampleTableEntryMP4V *mp4v = g_new0 (SampleTableEntryMP4V, 1);
       
   400 
       
   401   sample_entry_mp4v_init (mp4v, context);
       
   402   return mp4v;
       
   403 }
       
   404 
       
   405 static void
       
   406 atom_stsd_init (AtomSTSD * stsd)
       
   407 {
       
   408   guint8 flags[3] = { 0, 0, 0 };
       
   409 
       
   410   atom_full_init (&stsd->header, FOURCC_stsd, 0, 0, 0, flags);
       
   411   stsd->entries = NULL;
       
   412 }
       
   413 
       
   414 static void
       
   415 atom_stsd_clear (AtomSTSD * stsd)
       
   416 {
       
   417   GList *walker;
       
   418 
       
   419   atom_full_clear (&stsd->header);
       
   420   walker = stsd->entries;
       
   421   while (walker) {
       
   422     GList *aux = walker;
       
   423     SampleTableEntry *se = (SampleTableEntry *) aux->data;
       
   424 
       
   425     walker = g_list_next (walker);
       
   426     stsd->entries = g_list_remove_link (stsd->entries, aux);
       
   427 
       
   428     switch (se->kind) {
       
   429       case AUDIO:
       
   430         sample_entry_mp4a_free ((SampleTableEntryMP4A *) se);
       
   431         break;
       
   432       case VIDEO:
       
   433         sample_entry_mp4v_free ((SampleTableEntryMP4V *) se);
       
   434         break;
       
   435       default:
       
   436         /* best possible cleanup */
       
   437         atom_sample_entry_free (se);
       
   438     }
       
   439     g_list_free (aux);
       
   440   }
       
   441 }
       
   442 
       
   443 static void
       
   444 atom_ctts_init (AtomCTTS * ctts)
       
   445 {
       
   446   guint8 flags[3] = { 0, 0, 0 };
       
   447 
       
   448   atom_full_init (&ctts->header, FOURCC_ctts, 0, 0, 0, flags);
       
   449   ctts->entries = NULL;
       
   450 }
       
   451 
       
   452 static AtomCTTS *
       
   453 atom_ctts_new ()
       
   454 {
       
   455   AtomCTTS *ctts = g_new0 (AtomCTTS, 1);
       
   456 
       
   457   atom_ctts_init (ctts);
       
   458   return ctts;
       
   459 }
       
   460 
       
   461 static void
       
   462 atom_ctts_free (AtomCTTS * ctts)
       
   463 {
       
   464   GList *walker;
       
   465 
       
   466   atom_full_clear (&ctts->header);
       
   467   walker = ctts->entries;
       
   468   while (walker) {
       
   469     GList *aux = walker;
       
   470 
       
   471     walker = g_list_next (walker);
       
   472     ctts->entries = g_list_remove_link (ctts->entries, aux);
       
   473     g_free ((CTTSEntry *) aux->data);
       
   474     g_list_free (aux);
       
   475   }
       
   476   g_free (ctts);
       
   477 }
       
   478 
       
   479 static void
       
   480 atom_stts_init (AtomSTTS * stts)
       
   481 {
       
   482   guint8 flags[3] = { 0, 0, 0 };
       
   483 
       
   484   atom_full_init (&stts->header, FOURCC_stts, 0, 0, 0, flags);
       
   485   stts->entries = NULL;
       
   486 }
       
   487 
       
   488 static void
       
   489 atom_stts_clear (AtomSTTS * stts)
       
   490 {
       
   491   GList *walker;
       
   492 
       
   493   atom_full_clear (&stts->header);
       
   494   walker = stts->entries;
       
   495   while (walker) {
       
   496     GList *aux = walker;
       
   497 
       
   498     walker = g_list_next (walker);
       
   499     stts->entries = g_list_remove_link (stts->entries, aux);
       
   500     g_free ((STTSEntry *) aux->data);
       
   501     g_list_free (aux);
       
   502   }
       
   503   stts->n_entries = 0;
       
   504 }
       
   505 
       
   506 static void
       
   507 atom_stsz_init (AtomSTSZ * stsz)
       
   508 {
       
   509   guint8 flags[3] = { 0, 0, 0 };
       
   510 
       
   511   atom_full_init (&stsz->header, FOURCC_stsz, 0, 0, 0, flags);
       
   512   stsz->sample_size = 0;
       
   513   stsz->table_size = 0;
       
   514   stsz->entries = NULL;
       
   515 }
       
   516 
       
   517 static void
       
   518 atom_stsz_clear (AtomSTSZ * stsz)
       
   519 {
       
   520   atom_full_clear (&stsz->header);
       
   521   g_list_free (stsz->entries);
       
   522   stsz->entries = NULL;
       
   523   stsz->table_size = 0;
       
   524 }
       
   525 
       
   526 static void
       
   527 atom_stsc_init (AtomSTSC * stsc)
       
   528 {
       
   529   guint8 flags[3] = { 0, 0, 0 };
       
   530 
       
   531   atom_full_init (&stsc->header, FOURCC_stsc, 0, 0, 0, flags);
       
   532   stsc->entries = NULL;
       
   533   stsc->n_entries = 0;
       
   534 }
       
   535 
       
   536 static void
       
   537 atom_stsc_clear (AtomSTSC * stsc)
       
   538 {
       
   539   GList *walker;
       
   540 
       
   541   atom_full_clear (&stsc->header);
       
   542   walker = stsc->entries;
       
   543   while (walker) {
       
   544     GList *aux = walker;
       
   545 
       
   546     walker = g_list_next (walker);
       
   547     stsc->entries = g_list_remove_link (stsc->entries, aux);
       
   548     g_free ((STSCEntry *) aux->data);
       
   549     g_list_free (aux);
       
   550   }
       
   551   stsc->n_entries = 0;
       
   552 }
       
   553 
       
   554 static void
       
   555 atom_co64_init (AtomSTCO64 * co64)
       
   556 {
       
   557   guint8 flags[3] = { 0, 0, 0 };
       
   558 
       
   559   atom_full_init (&co64->header, FOURCC_co64, 0, 0, 0, flags);
       
   560   co64->entries = NULL;
       
   561   co64->n_entries = 0;
       
   562 }
       
   563 
       
   564 static void
       
   565 atom_stco64_clear (AtomSTCO64 * stco64)
       
   566 {
       
   567   GList *walker;
       
   568 
       
   569   atom_full_clear (&stco64->header);
       
   570   walker = stco64->entries;
       
   571   while (walker) {
       
   572     GList *aux = walker;
       
   573 
       
   574     walker = g_list_next (walker);
       
   575     stco64->entries = g_list_remove_link (stco64->entries, aux);
       
   576     g_free ((guint64 *) aux->data);
       
   577     g_list_free (aux);
       
   578   }
       
   579   stco64->n_entries = 0;
       
   580 }
       
   581 
       
   582 static void
       
   583 atom_stss_init (AtomSTSS * stss)
       
   584 {
       
   585   guint8 flags[3] = { 0, 0, 0 };
       
   586 
       
   587   atom_full_init (&stss->header, FOURCC_stss, 0, 0, 0, flags);
       
   588   stss->entries = NULL;
       
   589   stss->n_entries = 0;
       
   590 }
       
   591 
       
   592 static void
       
   593 atom_stss_clear (AtomSTSS * stss)
       
   594 {
       
   595   atom_full_clear (&stss->header);
       
   596   g_list_free (stss->entries);
       
   597   stss->entries = NULL;
       
   598   stss->n_entries = 0;
       
   599 }
       
   600 
       
   601 static void
       
   602 atom_stbl_init (AtomSTBL * stbl)
       
   603 {
       
   604   atom_header_set (&stbl->header, FOURCC_stbl, 0, 0);
       
   605 
       
   606   atom_stts_init (&stbl->stts);
       
   607   atom_stss_init (&stbl->stss);
       
   608   atom_stsd_init (&stbl->stsd);
       
   609   atom_stsz_init (&stbl->stsz);
       
   610   atom_stsc_init (&stbl->stsc);
       
   611   stbl->ctts = NULL;
       
   612 
       
   613   atom_co64_init (&stbl->stco64);
       
   614 }
       
   615 
       
   616 static void
       
   617 atom_stbl_clear (AtomSTBL * stbl)
       
   618 {
       
   619   atom_clear (&stbl->header);
       
   620   atom_stsd_clear (&stbl->stsd);
       
   621   atom_stts_clear (&stbl->stts);
       
   622   atom_stss_clear (&stbl->stss);
       
   623   atom_stsc_clear (&stbl->stsc);
       
   624   atom_stsz_clear (&stbl->stsz);
       
   625   if (stbl->ctts) {
       
   626     atom_ctts_free (stbl->ctts);
       
   627   }
       
   628   atom_stco64_clear (&stbl->stco64);
       
   629 }
       
   630 
       
   631 static void
       
   632 atom_vmhd_init (AtomVMHD * vmhd, AtomsContext * context)
       
   633 {
       
   634   guint8 flags[3] = { 0, 0, 1 };
       
   635 
       
   636   atom_full_init (&vmhd->header, FOURCC_vmhd, 0, 0, 0, flags);
       
   637   vmhd->graphics_mode = 0x0;
       
   638   memset (vmhd->opcolor, 0, sizeof (guint16) * 3);
       
   639 
       
   640   if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
       
   641     vmhd->graphics_mode = 0x40;
       
   642     vmhd->opcolor[0] = 32768;
       
   643     vmhd->opcolor[1] = 32768;
       
   644     vmhd->opcolor[2] = 32768;
       
   645   }
       
   646 }
       
   647 
       
   648 static AtomVMHD *
       
   649 atom_vmhd_new (AtomsContext * context)
       
   650 {
       
   651   AtomVMHD *vmhd = g_new0 (AtomVMHD, 1);
       
   652 
       
   653   atom_vmhd_init (vmhd, context);
       
   654   return vmhd;
       
   655 }
       
   656 
       
   657 static void
       
   658 atom_vmhd_free (AtomVMHD * vmhd)
       
   659 {
       
   660   atom_full_clear (&vmhd->header);
       
   661   g_free (vmhd);
       
   662 }
       
   663 
       
   664 static void
       
   665 atom_smhd_init (AtomSMHD * smhd)
       
   666 {
       
   667   guint8 flags[3] = { 0, 0, 0 };
       
   668 
       
   669   atom_full_init (&smhd->header, FOURCC_smhd, 0, 0, 0, flags);
       
   670   smhd->balance = 0;
       
   671   smhd->reserved = 0;
       
   672 }
       
   673 
       
   674 static AtomSMHD *
       
   675 atom_smhd_new ()
       
   676 {
       
   677   AtomSMHD *smhd = g_new0 (AtomSMHD, 1);
       
   678 
       
   679   atom_smhd_init (smhd);
       
   680   return smhd;
       
   681 }
       
   682 
       
   683 static void
       
   684 atom_smhd_free (AtomSMHD * smhd)
       
   685 {
       
   686   atom_full_clear (&smhd->header);
       
   687   g_free (smhd);
       
   688 }
       
   689 
       
   690 static void
       
   691 atom_hmhd_free (AtomHMHD * hmhd)
       
   692 {
       
   693   atom_full_clear (&hmhd->header);
       
   694   g_free (hmhd);
       
   695 }
       
   696 
       
   697 static void
       
   698 atom_hdlr_init (AtomHDLR * hdlr)
       
   699 {
       
   700   guint8 flags[3] = { 0, 0, 0 };
       
   701 
       
   702   atom_full_init (&hdlr->header, FOURCC_hdlr, 0, 0, 0, flags);
       
   703 
       
   704   hdlr->component_type = 0;
       
   705   hdlr->handler_type = 0;
       
   706   hdlr->manufacturer = 0;
       
   707   hdlr->flags = 0;
       
   708   hdlr->flags_mask = 0;
       
   709   hdlr->name = g_strdup ("");
       
   710 }
       
   711 
       
   712 static AtomHDLR *
       
   713 atom_hdlr_new ()
       
   714 {
       
   715   AtomHDLR *hdlr = g_new0 (AtomHDLR, 1);
       
   716 
       
   717   atom_hdlr_init (hdlr);
       
   718   return hdlr;
       
   719 }
       
   720 
       
   721 static void
       
   722 atom_hdlr_clear (AtomHDLR * hdlr)
       
   723 {
       
   724   atom_full_clear (&hdlr->header);
       
   725   if (hdlr->name) {
       
   726     g_free (hdlr->name);
       
   727     hdlr->name = NULL;
       
   728   }
       
   729 }
       
   730 
       
   731 static void
       
   732 atom_hdlr_free (AtomHDLR * hdlr)
       
   733 {
       
   734   atom_hdlr_clear (hdlr);
       
   735   g_free (hdlr);
       
   736 }
       
   737 
       
   738 static void
       
   739 atom_url_init (AtomURL * url)
       
   740 {
       
   741   guint8 flags[3] = { 0, 0, 1 };
       
   742 
       
   743   atom_full_init (&url->header, FOURCC_url_, 0, 0, 0, flags);
       
   744   url->location = NULL;
       
   745 }
       
   746 
       
   747 static void
       
   748 atom_url_free (AtomURL * url)
       
   749 {
       
   750   atom_full_clear (&url->header);
       
   751   if (url->location) {
       
   752     g_free (url->location);
       
   753     url->location = NULL;
       
   754   }
       
   755   g_free (url);
       
   756 }
       
   757 
       
   758 static AtomURL *
       
   759 atom_url_new ()
       
   760 {
       
   761   AtomURL *url = g_new0 (AtomURL, 1);
       
   762 
       
   763   atom_url_init (url);
       
   764   return url;
       
   765 }
       
   766 
       
   767 static AtomFull *
       
   768 atom_alis_new ()
       
   769 {
       
   770   guint8 flags[3] = { 0, 0, 1 };
       
   771   AtomFull *alis = g_new0 (AtomFull, 1);
       
   772 
       
   773   atom_full_init (alis, FOURCC_alis, 0, 0, 0, flags);
       
   774   return alis;
       
   775 }
       
   776 
       
   777 static void
       
   778 atom_dref_init (AtomDREF * dref, AtomsContext * context)
       
   779 {
       
   780   guint8 flags[3] = { 0, 0, 0 };
       
   781 
       
   782   atom_full_init (&dref->header, FOURCC_dref, 0, 0, 0, flags);
       
   783 
       
   784   /* in either case, alis or url init arranges to set self-contained flag */
       
   785   if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
       
   786     /* alis dref for qt */
       
   787     AtomFull *alis = atom_alis_new ();
       
   788     dref->entries = g_list_append (dref->entries, alis);
       
   789   } else {
       
   790     /* url for iso spec, as 'alis' not specified there */
       
   791     AtomURL *url = atom_url_new ();
       
   792     dref->entries = g_list_append (dref->entries, url);
       
   793   }
       
   794 }
       
   795 
       
   796 static void
       
   797 atom_dref_clear (AtomDREF * dref)
       
   798 {
       
   799   GList *walker;
       
   800 
       
   801   atom_full_clear (&dref->header);
       
   802   walker = dref->entries;
       
   803   while (walker) {
       
   804     GList *aux = walker;
       
   805     Atom *atom = (Atom *) aux->data;
       
   806 
       
   807     walker = g_list_next (walker);
       
   808     dref->entries = g_list_remove_link (dref->entries, aux);
       
   809     switch (atom->type) {
       
   810       case FOURCC_alis:
       
   811         atom_full_free ((AtomFull *) atom);
       
   812         break;
       
   813       case FOURCC_url_:
       
   814         atom_url_free ((AtomURL *) atom);
       
   815         break;
       
   816       default:
       
   817         /* we do nothing, better leak than crash */
       
   818         break;
       
   819     }
       
   820     g_list_free (aux);
       
   821   }
       
   822 }
       
   823 
       
   824 static void
       
   825 atom_dinf_init (AtomDINF * dinf, AtomsContext * context)
       
   826 {
       
   827   atom_header_set (&dinf->header, FOURCC_dinf, 0, 0);
       
   828   atom_dref_init (&dinf->dref, context);
       
   829 }
       
   830 
       
   831 static void
       
   832 atom_dinf_clear (AtomDINF * dinf)
       
   833 {
       
   834   atom_clear (&dinf->header);
       
   835   atom_dref_clear (&dinf->dref);
       
   836 }
       
   837 
       
   838 static void
       
   839 atom_minf_init (AtomMINF * minf, AtomsContext * context)
       
   840 {
       
   841   atom_header_set (&minf->header, FOURCC_minf, 0, 0);
       
   842 
       
   843   minf->vmhd = NULL;
       
   844   minf->smhd = NULL;
       
   845   minf->hmhd = NULL;
       
   846 
       
   847   if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
       
   848     minf->hdlr = atom_hdlr_new ();
       
   849     minf->hdlr->component_type = FOURCC_dhlr;
       
   850     minf->hdlr->handler_type = FOURCC_alis;
       
   851   } else {
       
   852     minf->hdlr = NULL;
       
   853   }
       
   854   atom_dinf_init (&minf->dinf, context);
       
   855   atom_stbl_init (&minf->stbl);
       
   856 }
       
   857 
       
   858 static void
       
   859 atom_minf_clear_handlers (AtomMINF * minf)
       
   860 {
       
   861   if (minf->vmhd) {
       
   862     atom_vmhd_free (minf->vmhd);
       
   863     minf->vmhd = NULL;
       
   864   }
       
   865   if (minf->smhd) {
       
   866     atom_smhd_free (minf->smhd);
       
   867     minf->smhd = NULL;
       
   868   }
       
   869   if (minf->hmhd) {
       
   870     atom_hmhd_free (minf->hmhd);
       
   871     minf->hmhd = NULL;
       
   872   }
       
   873 }
       
   874 
       
   875 static void
       
   876 atom_minf_clear (AtomMINF * minf)
       
   877 {
       
   878   atom_clear (&minf->header);
       
   879   atom_minf_clear_handlers (minf);
       
   880   if (minf->hdlr) {
       
   881     atom_hdlr_free (minf->hdlr);
       
   882   }
       
   883   atom_dinf_clear (&minf->dinf);
       
   884   atom_stbl_clear (&minf->stbl);
       
   885 }
       
   886 
       
   887 static void
       
   888 atom_mdhd_init (AtomMDHD * mdhd)
       
   889 {
       
   890   guint8 flags[3] = { 0, 0, 0 };
       
   891 
       
   892   atom_full_init (&mdhd->header, FOURCC_mdhd, 0, 0, 0, flags);
       
   893   common_time_info_init (&mdhd->time_info);
       
   894   mdhd->language_code = 0;
       
   895   mdhd->quality = 0;
       
   896 }
       
   897 
       
   898 static void
       
   899 atom_mdhd_clear (AtomMDHD * mdhd)
       
   900 {
       
   901   atom_full_clear (&mdhd->header);
       
   902 }
       
   903 
       
   904 static void
       
   905 atom_mdia_init (AtomMDIA * mdia, AtomsContext * context)
       
   906 {
       
   907   atom_header_set (&mdia->header, FOURCC_mdia, 0, 0);
       
   908 
       
   909   atom_mdhd_init (&mdia->mdhd);
       
   910   atom_hdlr_init (&mdia->hdlr);
       
   911   atom_minf_init (&mdia->minf, context);
       
   912 }
       
   913 
       
   914 static void
       
   915 atom_mdia_clear (AtomMDIA * mdia)
       
   916 {
       
   917   atom_clear (&mdia->header);
       
   918   atom_mdhd_clear (&mdia->mdhd);
       
   919   atom_hdlr_clear (&mdia->hdlr);
       
   920   atom_minf_clear (&mdia->minf);
       
   921 }
       
   922 
       
   923 static void
       
   924 atom_tkhd_init (AtomTKHD * tkhd, AtomsContext * context)
       
   925 {
       
   926   /*
       
   927    * flags info
       
   928    * 1 -> track enabled
       
   929    * 2 -> track in movie
       
   930    * 4 -> track in preview
       
   931    */
       
   932   guint8 flags[3] = { 0, 0, 7 };
       
   933 
       
   934   atom_full_init (&tkhd->header, FOURCC_tkhd, 0, 0, 0, flags);
       
   935 
       
   936   tkhd->creation_time = tkhd->modification_time = get_current_qt_time ();
       
   937   tkhd->duration = 0;
       
   938   tkhd->track_ID = 0;
       
   939   tkhd->reserved = 0;
       
   940 
       
   941   tkhd->reserved2[0] = tkhd->reserved2[1] = 0;
       
   942   tkhd->layer = 0;
       
   943   tkhd->alternate_group = 0;
       
   944   tkhd->volume = 0;
       
   945   tkhd->reserved3 = 0;
       
   946   memset (tkhd->matrix, 0, sizeof (guint32) * 9);
       
   947   tkhd->matrix[0] = 1 << 16;
       
   948   tkhd->matrix[4] = 1 << 16;
       
   949   tkhd->matrix[8] = 16384 << 16;
       
   950   tkhd->width = 0;
       
   951   tkhd->height = 0;
       
   952 }
       
   953 
       
   954 static void
       
   955 atom_tkhd_clear (AtomTKHD * tkhd)
       
   956 {
       
   957   atom_full_clear (&tkhd->header);
       
   958 }
       
   959 
       
   960 static void
       
   961 atom_trak_init (AtomTRAK * trak, AtomsContext * context)
       
   962 {
       
   963   atom_header_set (&trak->header, FOURCC_trak, 0, 0);
       
   964 
       
   965   atom_tkhd_init (&trak->tkhd, context);
       
   966   atom_mdia_init (&trak->mdia, context);
       
   967 }
       
   968 
       
   969 AtomTRAK *
       
   970 atom_trak_new (AtomsContext * context)
       
   971 {
       
   972   AtomTRAK *trak = g_new0 (AtomTRAK, 1);
       
   973 
       
   974   atom_trak_init (trak, context);
       
   975   return trak;
       
   976 }
       
   977 
       
   978 static void
       
   979 atom_trak_free (AtomTRAK * trak)
       
   980 {
       
   981   atom_clear (&trak->header);
       
   982   atom_tkhd_clear (&trak->tkhd);
       
   983   atom_mdia_clear (&trak->mdia);
       
   984   g_free (trak);
       
   985 }
       
   986 
       
   987 static void
       
   988 atom_ilst_init (AtomILST * ilst)
       
   989 {
       
   990   atom_header_set (&ilst->header, FOURCC_ilst, 0, 0);
       
   991   ilst->entries = NULL;
       
   992 }
       
   993 
       
   994 static AtomILST *
       
   995 atom_ilst_new ()
       
   996 {
       
   997   AtomILST *ilst = g_new0 (AtomILST, 1);
       
   998 
       
   999   atom_ilst_init (ilst);
       
  1000   return ilst;
       
  1001 }
       
  1002 
       
  1003 static void
       
  1004 atom_ilst_free (AtomILST * ilst)
       
  1005 {
       
  1006   if (ilst->entries)
       
  1007     atom_info_list_free (ilst->entries);
       
  1008   atom_clear (&ilst->header);
       
  1009   g_free (ilst);
       
  1010 }
       
  1011 
       
  1012 static void
       
  1013 atom_meta_init (AtomMETA * meta)
       
  1014 {
       
  1015   guint8 flags[3] = { 0, 0, 0 };
       
  1016 
       
  1017   atom_full_init (&meta->header, FOURCC_meta, 0, 0, 0, flags);
       
  1018   atom_hdlr_init (&meta->hdlr);
       
  1019   /* FIXME (ISOM says this is always 0) */
       
  1020   meta->hdlr.component_type = FOURCC_mhlr;
       
  1021   meta->hdlr.handler_type = FOURCC_mdir;
       
  1022   meta->ilst = NULL;
       
  1023 }
       
  1024 
       
  1025 static AtomMETA *
       
  1026 atom_meta_new ()
       
  1027 {
       
  1028   AtomMETA *meta = g_new0 (AtomMETA, 1);
       
  1029 
       
  1030   atom_meta_init (meta);
       
  1031   return meta;
       
  1032 }
       
  1033 
       
  1034 static void
       
  1035 atom_meta_free (AtomMETA * meta)
       
  1036 {
       
  1037   atom_full_clear (&meta->header);
       
  1038   atom_hdlr_clear (&meta->hdlr);
       
  1039   if (meta->ilst)
       
  1040     atom_ilst_free (meta->ilst);
       
  1041   meta->ilst = NULL;
       
  1042   g_free (meta);
       
  1043 }
       
  1044 
       
  1045 static void
       
  1046 atom_udta_init (AtomUDTA * udta)
       
  1047 {
       
  1048   atom_header_set (&udta->header, FOURCC_udta, 0, 0);
       
  1049   udta->meta = NULL;
       
  1050 }
       
  1051 
       
  1052 static AtomUDTA *
       
  1053 atom_udta_new ()
       
  1054 {
       
  1055   AtomUDTA *udta = g_new0 (AtomUDTA, 1);
       
  1056 
       
  1057   atom_udta_init (udta);
       
  1058   return udta;
       
  1059 }
       
  1060 
       
  1061 static void
       
  1062 atom_udta_free (AtomUDTA * udta)
       
  1063 {
       
  1064   atom_clear (&udta->header);
       
  1065   if (udta->meta)
       
  1066     atom_meta_free (udta->meta);
       
  1067   udta->meta = NULL;
       
  1068   if (udta->entries)
       
  1069     atom_info_list_free (udta->entries);
       
  1070   g_free (udta);
       
  1071 }
       
  1072 
       
  1073 static void
       
  1074 atom_tag_data_init (AtomTagData * data)
       
  1075 {
       
  1076   guint8 flags[] = { 0, 0, 0 };
       
  1077 
       
  1078   atom_full_init (&data->header, FOURCC_data, 0, 0, 0, flags);
       
  1079 }
       
  1080 
       
  1081 static void
       
  1082 atom_tag_data_clear (AtomTagData * data)
       
  1083 {
       
  1084   atom_full_clear (&data->header);
       
  1085   g_free (data->data);
       
  1086   data->datalen = 0;
       
  1087 }
       
  1088 
       
  1089 /*
       
  1090  * Fourcc is the tag fourcc
       
  1091  * flags will be truncated to 24bits
       
  1092  */
       
  1093 static AtomTag *
       
  1094 atom_tag_new (guint32 fourcc, guint32 flags_as_uint)
       
  1095 {
       
  1096   AtomTag *tag = g_new0 (AtomTag, 1);
       
  1097 
       
  1098   tag->header.type = fourcc;
       
  1099   atom_tag_data_init (&tag->data);
       
  1100   tag->data.header.flags[2] = flags_as_uint & 0xFF;
       
  1101   tag->data.header.flags[1] = (flags_as_uint & 0xFF00) >> 8;
       
  1102   tag->data.header.flags[0] = (flags_as_uint & 0xFF0000) >> 16;
       
  1103   return tag;
       
  1104 }
       
  1105 
       
  1106 static void
       
  1107 atom_tag_free (AtomTag * tag)
       
  1108 {
       
  1109   atom_clear (&tag->header);
       
  1110   atom_tag_data_clear (&tag->data);
       
  1111   g_free (tag);
       
  1112 }
       
  1113 
       
  1114 static void
       
  1115 atom_mvhd_init (AtomMVHD * mvhd)
       
  1116 {
       
  1117   guint8 flags[3] = { 0, 0, 0 };
       
  1118 
       
  1119   atom_full_init (&(mvhd->header), FOURCC_mvhd, sizeof (AtomMVHD), 0, 0, flags);
       
  1120 
       
  1121   common_time_info_init (&mvhd->time_info);
       
  1122 
       
  1123   mvhd->prefered_rate = 1 << 16;
       
  1124   mvhd->volume = 1 << 8;
       
  1125   mvhd->reserved3 = 0;
       
  1126   memset (mvhd->reserved4, 0, sizeof (guint32[2]));
       
  1127 
       
  1128   memset (mvhd->matrix, 0, sizeof (guint32[9]));
       
  1129   mvhd->matrix[0] = 1 << 16;
       
  1130   mvhd->matrix[4] = 1 << 16;
       
  1131   mvhd->matrix[8] = 16384 << 16;
       
  1132 
       
  1133   mvhd->preview_time = 0;
       
  1134   mvhd->preview_duration = 0;
       
  1135   mvhd->poster_time = 0;
       
  1136   mvhd->selection_time = 0;
       
  1137   mvhd->selection_duration = 0;
       
  1138   mvhd->current_time = 0;
       
  1139 
       
  1140   mvhd->next_track_id = 1;
       
  1141 }
       
  1142 
       
  1143 static void
       
  1144 atom_mvhd_clear (AtomMVHD * mvhd)
       
  1145 {
       
  1146   atom_full_clear (&mvhd->header);
       
  1147 }
       
  1148 
       
  1149 static void
       
  1150 atom_moov_init (AtomMOOV * moov, AtomsContext * context)
       
  1151 {
       
  1152   atom_header_set (&(moov->header), FOURCC_moov, 0, 0);
       
  1153   atom_mvhd_init (&(moov->mvhd));
       
  1154   moov->udta = NULL;
       
  1155   moov->traks = NULL;
       
  1156   moov->context = *context;
       
  1157 }
       
  1158 
       
  1159 AtomMOOV *
       
  1160 atom_moov_new (AtomsContext * context)
       
  1161 {
       
  1162   AtomMOOV *moov = g_new0 (AtomMOOV, 1);
       
  1163 
       
  1164   atom_moov_init (moov, context);
       
  1165   return moov;
       
  1166 }
       
  1167 
       
  1168 void
       
  1169 atom_moov_free (AtomMOOV * moov)
       
  1170 {
       
  1171   GList *walker;
       
  1172 
       
  1173   atom_clear (&moov->header);
       
  1174   atom_mvhd_clear (&moov->mvhd);
       
  1175 
       
  1176   walker = moov->traks;
       
  1177   while (walker) {
       
  1178     atom_trak_free ((AtomTRAK *) walker->data);
       
  1179     walker = g_list_next (walker);
       
  1180   }
       
  1181   g_list_free (moov->traks);
       
  1182   moov->traks = NULL;
       
  1183 
       
  1184   if (moov->udta) {
       
  1185     atom_udta_free (moov->udta);
       
  1186     moov->udta = NULL;
       
  1187   }
       
  1188 
       
  1189   g_free (moov);
       
  1190 }
       
  1191 
       
  1192 /* -- end of init / free -- */
       
  1193 
       
  1194 /* -- copy data functions -- */
       
  1195 
       
  1196 static guint8
       
  1197 atom_full_get_version (AtomFull * full)
       
  1198 {
       
  1199   return full->version;
       
  1200 }
       
  1201 
       
  1202 static guint64
       
  1203 common_time_info_copy_data (TimeInfo * ti, gboolean trunc_to_32,
       
  1204     guint8 ** buffer, guint64 * size, guint64 * offset)
       
  1205 {
       
  1206   guint64 original_offset = *offset;
       
  1207 
       
  1208   if (trunc_to_32) {
       
  1209     prop_copy_uint32 ((guint32) ti->creation_time, buffer, size, offset);
       
  1210     prop_copy_uint32 ((guint32) ti->modification_time, buffer, size, offset);
       
  1211     prop_copy_uint32 (ti->timescale, buffer, size, offset);
       
  1212     prop_copy_uint32 ((guint32) ti->duration, buffer, size, offset);
       
  1213   } else {
       
  1214     prop_copy_uint64 (ti->creation_time, buffer, size, offset);
       
  1215     prop_copy_uint64 (ti->modification_time, buffer, size, offset);
       
  1216     prop_copy_uint32 (ti->timescale, buffer, size, offset);
       
  1217     prop_copy_uint64 (ti->duration, buffer, size, offset);
       
  1218   }
       
  1219   return *offset - original_offset;
       
  1220 }
       
  1221 
       
  1222 static void
       
  1223 atom_write_size (guint8 ** buffer, guint64 * size, guint64 * offset,
       
  1224     guint64 atom_pos)
       
  1225 {
       
  1226   /* this only works for non-extended atom size, which is OK
       
  1227    * (though it could be made to do mem_move, etc and write extended size) */
       
  1228   prop_copy_uint32 (*offset - atom_pos, buffer, size, &atom_pos);
       
  1229 }
       
  1230 
       
  1231 guint64
       
  1232 atom_copy_data (Atom * atom, guint8 ** buffer, guint64 * size, guint64 * offset)
       
  1233 {
       
  1234   guint64 original_offset = *offset;
       
  1235 
       
  1236   /* copies type and size */
       
  1237   prop_copy_uint32 (atom->size, buffer, size, offset);
       
  1238   prop_copy_fourcc (atom->type, buffer, size, offset);
       
  1239 
       
  1240   /* extended size needed */
       
  1241   if (atom->size == 1) {
       
  1242     /* really should not happen other than with mdat atom;
       
  1243      * would be a problem for size (re)write code, not to mention memory */
       
  1244     g_return_val_if_fail (atom->type == FOURCC_mdat, 0);
       
  1245     prop_copy_uint64 (atom->extended_size, buffer, size, offset);
       
  1246   }
       
  1247 
       
  1248   return *offset - original_offset;
       
  1249 }
       
  1250 
       
  1251 static guint64
       
  1252 atom_full_copy_data (AtomFull * atom, guint8 ** buffer, guint64 * size,
       
  1253     guint64 * offset)
       
  1254 {
       
  1255   guint64 original_offset = *offset;
       
  1256 
       
  1257   if (!atom_copy_data (&atom->header, buffer, size, offset)) {
       
  1258     return 0;
       
  1259   }
       
  1260 
       
  1261   prop_copy_uint8 (atom->version, buffer, size, offset);
       
  1262   prop_copy_uint8_array (atom->flags, 3, buffer, size, offset);
       
  1263 
       
  1264   atom_write_size (buffer, size, offset, original_offset);
       
  1265   return *offset - original_offset;
       
  1266 }
       
  1267 
       
  1268 static guint64
       
  1269 atom_info_list_copy_data (GList * ai, guint8 ** buffer, guint64 * size,
       
  1270     guint64 * offset)
       
  1271 {
       
  1272   guint64 original_offset = *offset;
       
  1273 
       
  1274   while (ai) {
       
  1275     AtomInfo *info = (AtomInfo *) ai->data;
       
  1276 
       
  1277     if (!info->copy_data_func (info->atom, buffer, size, offset)) {
       
  1278       return 0;
       
  1279     }
       
  1280     ai = g_list_next (ai);
       
  1281   }
       
  1282 
       
  1283   return *offset - original_offset;
       
  1284 }
       
  1285 
       
  1286 static guint64
       
  1287 atom_data_copy_data (AtomData * data, guint8 ** buffer, guint64 * size,
       
  1288     guint64 * offset)
       
  1289 {
       
  1290   guint64 original_offset = *offset;
       
  1291 
       
  1292   if (!atom_copy_data (&data->header, buffer, size, offset)) {
       
  1293     return 0;
       
  1294   }
       
  1295   if (data->datalen)
       
  1296     prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
       
  1297 
       
  1298   atom_write_size (buffer, size, offset, original_offset);
       
  1299   return *offset - original_offset;
       
  1300 }
       
  1301 
       
  1302 guint64
       
  1303 atom_ftyp_copy_data (AtomFTYP * ftyp, guint8 ** buffer, guint64 * size,
       
  1304     guint64 * offset)
       
  1305 {
       
  1306   guint64 original_offset = *offset;
       
  1307 
       
  1308   if (!atom_copy_data (&ftyp->header, buffer, size, offset)) {
       
  1309     return 0;
       
  1310   }
       
  1311   prop_copy_fourcc (ftyp->major_brand, buffer, size, offset);
       
  1312   prop_copy_uint32 (ftyp->version, buffer, size, offset);
       
  1313 
       
  1314   prop_copy_fourcc_array (ftyp->compatible_brands, ftyp->compatible_brands_size,
       
  1315       buffer, size, offset);
       
  1316 
       
  1317   atom_write_size (buffer, size, offset, original_offset);
       
  1318   return *offset - original_offset;
       
  1319 }
       
  1320 
       
  1321 static guint64
       
  1322 atom_mvhd_copy_data (AtomMVHD * atom, guint8 ** buffer, guint64 * size,
       
  1323     guint64 * offset)
       
  1324 {
       
  1325   guint8 version;
       
  1326   guint64 original_offset = *offset;
       
  1327 
       
  1328   if (!atom_full_copy_data (&(atom->header), buffer, size, offset)) {
       
  1329     return 0;
       
  1330   }
       
  1331 
       
  1332   version = atom_full_get_version (&(atom->header));
       
  1333   if (version == 0) {
       
  1334     common_time_info_copy_data (&atom->time_info, TRUE, buffer, size, offset);
       
  1335   } else if (version == 1) {
       
  1336     common_time_info_copy_data (&atom->time_info, FALSE, buffer, size, offset);
       
  1337   } else {
       
  1338     *offset = original_offset;
       
  1339     return 0;
       
  1340   }
       
  1341 
       
  1342   prop_copy_uint32 (atom->prefered_rate, buffer, size, offset);
       
  1343   prop_copy_uint16 (atom->volume, buffer, size, offset);
       
  1344   prop_copy_uint16 (atom->reserved3, buffer, size, offset);
       
  1345   prop_copy_uint32_array (atom->reserved4, 2, buffer, size, offset);
       
  1346   prop_copy_uint32_array (atom->matrix, 9, buffer, size, offset);
       
  1347   prop_copy_uint32 (atom->preview_time, buffer, size, offset);
       
  1348   prop_copy_uint32 (atom->preview_duration, buffer, size, offset);
       
  1349   prop_copy_uint32 (atom->poster_time, buffer, size, offset);
       
  1350   prop_copy_uint32 (atom->selection_time, buffer, size, offset);
       
  1351   prop_copy_uint32 (atom->selection_duration, buffer, size, offset);
       
  1352   prop_copy_uint32 (atom->current_time, buffer, size, offset);
       
  1353 
       
  1354   prop_copy_uint32 (atom->next_track_id, 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_tkhd_copy_data (AtomTKHD * tkhd, guint8 ** buffer, guint64 * size,
       
  1362     guint64 * offset)
       
  1363 {
       
  1364   guint64 original_offset = *offset;
       
  1365 
       
  1366   if (!atom_full_copy_data (&tkhd->header, buffer, size, offset)) {
       
  1367     return 0;
       
  1368   }
       
  1369 
       
  1370   if (atom_full_get_version (&tkhd->header) == 0) {
       
  1371     prop_copy_uint32 ((guint32) tkhd->creation_time, buffer, size, offset);
       
  1372     prop_copy_uint32 ((guint32) tkhd->modification_time, buffer, size, offset);
       
  1373     prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
       
  1374     prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
       
  1375     prop_copy_uint32 ((guint32) tkhd->duration, buffer, size, offset);
       
  1376   } else {
       
  1377     prop_copy_uint64 (tkhd->creation_time, buffer, size, offset);
       
  1378     prop_copy_uint64 (tkhd->modification_time, buffer, size, offset);
       
  1379     prop_copy_uint32 (tkhd->track_ID, buffer, size, offset);
       
  1380     prop_copy_uint32 (tkhd->reserved, buffer, size, offset);
       
  1381     prop_copy_uint64 (tkhd->duration, buffer, size, offset);
       
  1382   }
       
  1383 
       
  1384   prop_copy_uint32_array (tkhd->reserved2, 2, buffer, size, offset);
       
  1385   prop_copy_uint16 (tkhd->layer, buffer, size, offset);
       
  1386   prop_copy_uint16 (tkhd->alternate_group, buffer, size, offset);
       
  1387   prop_copy_uint16 (tkhd->volume, buffer, size, offset);
       
  1388   prop_copy_uint16 (tkhd->reserved3, buffer, size, offset);
       
  1389   prop_copy_uint32_array (tkhd->matrix, 9, buffer, size, offset);
       
  1390 
       
  1391   prop_copy_uint32 (tkhd->width, buffer, size, offset);
       
  1392   prop_copy_uint32 (tkhd->height, buffer, size, offset);
       
  1393 
       
  1394   atom_write_size (buffer, size, offset, original_offset);
       
  1395   return *offset - original_offset;
       
  1396 }
       
  1397 
       
  1398 static guint64
       
  1399 atom_hdlr_copy_data (AtomHDLR * hdlr, guint8 ** buffer, guint64 * size,
       
  1400     guint64 * offset)
       
  1401 {
       
  1402   guint64 original_offset = *offset;
       
  1403 
       
  1404   if (!atom_full_copy_data (&hdlr->header, buffer, size, offset)) {
       
  1405     return 0;
       
  1406   }
       
  1407 
       
  1408   prop_copy_fourcc (hdlr->component_type, buffer, size, offset);
       
  1409   prop_copy_fourcc (hdlr->handler_type, buffer, size, offset);
       
  1410   prop_copy_fourcc (hdlr->manufacturer, buffer, size, offset);
       
  1411   prop_copy_uint32 (hdlr->flags, buffer, size, offset);
       
  1412   prop_copy_uint32 (hdlr->flags_mask, buffer, size, offset);
       
  1413 
       
  1414   prop_copy_null_terminated_string (hdlr->name, buffer, size, offset);
       
  1415 
       
  1416   atom_write_size (buffer, size, offset, original_offset);
       
  1417   return *offset - original_offset;
       
  1418 }
       
  1419 
       
  1420 static guint64
       
  1421 atom_vmhd_copy_data (AtomVMHD * vmhd, guint8 ** buffer, guint64 * size,
       
  1422     guint64 * offset)
       
  1423 {
       
  1424   guint64 original_offset = *offset;
       
  1425 
       
  1426   if (!atom_full_copy_data (&vmhd->header, buffer, size, offset)) {
       
  1427     return 0;
       
  1428   }
       
  1429   prop_copy_uint16 (vmhd->graphics_mode, buffer, size, offset);
       
  1430   prop_copy_uint16_array (vmhd->opcolor, 3, buffer, size, offset);
       
  1431 
       
  1432   atom_write_size (buffer, size, offset, original_offset);
       
  1433   return original_offset - *offset;
       
  1434 }
       
  1435 
       
  1436 static guint64
       
  1437 atom_smhd_copy_data (AtomSMHD * smhd, guint8 ** buffer, guint64 * size,
       
  1438     guint64 * offset)
       
  1439 {
       
  1440   guint64 original_offset = *offset;
       
  1441 
       
  1442   if (!atom_full_copy_data (&smhd->header, buffer, size, offset)) {
       
  1443     return 0;
       
  1444   }
       
  1445   prop_copy_uint16 (smhd->balance, buffer, size, offset);
       
  1446   prop_copy_uint16 (smhd->reserved, buffer, size, offset);
       
  1447 
       
  1448   atom_write_size (buffer, size, offset, original_offset);
       
  1449   return original_offset - *offset;
       
  1450 }
       
  1451 
       
  1452 static guint64
       
  1453 atom_hmhd_copy_data (AtomHMHD * hmhd, guint8 ** buffer, guint64 * size,
       
  1454     guint64 * offset)
       
  1455 {
       
  1456   guint64 original_offset = *offset;
       
  1457 
       
  1458   if (!atom_full_copy_data (&hmhd->header, buffer, size, offset)) {
       
  1459     return 0;
       
  1460   }
       
  1461   prop_copy_uint16 (hmhd->max_pdu_size, buffer, size, offset);
       
  1462   prop_copy_uint16 (hmhd->avg_pdu_size, buffer, size, offset);
       
  1463   prop_copy_uint32 (hmhd->max_bitrate, buffer, size, offset);
       
  1464   prop_copy_uint32 (hmhd->avg_bitrate, buffer, size, offset);
       
  1465   prop_copy_uint32 (hmhd->sliding_avg_bitrate, buffer, size, offset);
       
  1466 
       
  1467   atom_write_size (buffer, size, offset, original_offset);
       
  1468   return original_offset - *offset;
       
  1469 }
       
  1470 
       
  1471 static gboolean
       
  1472 atom_url_same_file_flag (AtomURL * url)
       
  1473 {
       
  1474   return (url->header.flags[2] & 0x1) == 1;
       
  1475 }
       
  1476 
       
  1477 static guint64
       
  1478 atom_url_copy_data (AtomURL * url, guint8 ** buffer, guint64 * size,
       
  1479     guint64 * offset)
       
  1480 {
       
  1481   guint64 original_offset = *offset;
       
  1482 
       
  1483   if (!atom_full_copy_data (&url->header, buffer, size, offset)) {
       
  1484     return 0;
       
  1485   }
       
  1486 
       
  1487   if (!atom_url_same_file_flag (url)) {
       
  1488     prop_copy_null_terminated_string (url->location, buffer, size, offset);
       
  1489   }
       
  1490 
       
  1491   atom_write_size (buffer, size, offset, original_offset);
       
  1492   return original_offset - *offset;
       
  1493 }
       
  1494 
       
  1495 static guint64
       
  1496 atom_stts_copy_data (AtomSTTS * stts, guint8 ** buffer, guint64 * size,
       
  1497     guint64 * offset)
       
  1498 {
       
  1499   guint64 original_offset = *offset;
       
  1500   GList *walker;
       
  1501 
       
  1502   if (!atom_full_copy_data (&stts->header, buffer, size, offset)) {
       
  1503     return 0;
       
  1504   }
       
  1505 
       
  1506   prop_copy_uint32 (stts->n_entries, buffer, size, offset);
       
  1507   /* minimize realloc */
       
  1508   prop_copy_ensure_buffer (buffer, size, offset, 8 * stts->n_entries);
       
  1509   for (walker = g_list_last (stts->entries); walker != NULL;
       
  1510       walker = g_list_previous (walker)) {
       
  1511     STTSEntry *entry = (STTSEntry *) walker->data;
       
  1512 
       
  1513     prop_copy_uint32 (entry->sample_count, buffer, size, offset);
       
  1514     prop_copy_int32 (entry->sample_delta, buffer, size, offset);
       
  1515   }
       
  1516 
       
  1517   atom_write_size (buffer, size, offset, original_offset);
       
  1518   return *offset - original_offset;
       
  1519 }
       
  1520 
       
  1521 static guint64
       
  1522 atom_sample_entry_copy_data (SampleTableEntry * se, guint8 ** buffer,
       
  1523     guint64 * size, guint64 * offset)
       
  1524 {
       
  1525   guint64 original_offset = *offset;
       
  1526 
       
  1527   if (!atom_copy_data (&se->header, buffer, size, offset)) {
       
  1528     return 0;
       
  1529   }
       
  1530 
       
  1531   prop_copy_uint8_array (se->reserved, 6, buffer, size, offset);
       
  1532   prop_copy_uint16 (se->data_reference_index, buffer, size, offset);
       
  1533 
       
  1534   return *offset - original_offset;
       
  1535 }
       
  1536 
       
  1537 static guint64
       
  1538 atom_esds_copy_data (AtomESDS * esds, guint8 ** buffer, guint64 * size,
       
  1539     guint64 * offset)
       
  1540 {
       
  1541   guint64 original_offset = *offset;
       
  1542 
       
  1543   if (!atom_full_copy_data (&esds->header, buffer, size, offset)) {
       
  1544     return 0;
       
  1545   }
       
  1546   if (!desc_es_descriptor_copy_data (&esds->es, buffer, size, offset)) {
       
  1547     return 0;
       
  1548   }
       
  1549 
       
  1550   atom_write_size (buffer, size, offset, original_offset);
       
  1551   return *offset - original_offset;
       
  1552 }
       
  1553 
       
  1554 static guint64
       
  1555 atom_frma_copy_data (AtomFRMA * frma, guint8 ** buffer,
       
  1556     guint64 * size, guint64 * offset)
       
  1557 {
       
  1558   guint64 original_offset = *offset;
       
  1559 
       
  1560   if (!atom_copy_data (&(frma->header), buffer, size, offset))
       
  1561     return 0;
       
  1562 
       
  1563   prop_copy_fourcc (frma->media_type, buffer, size, offset);
       
  1564 
       
  1565   atom_write_size (buffer, size, offset, original_offset);
       
  1566   return *offset - original_offset;
       
  1567 }
       
  1568 
       
  1569 static guint64
       
  1570 atom_mp4s_copy_data (SampleTableEntryMP4S * mp4s, guint8 ** buffer,
       
  1571     guint64 * size, guint64 * offset)
       
  1572 {
       
  1573   guint64 original_offset = *offset;
       
  1574 
       
  1575   if (!atom_sample_entry_copy_data (&mp4s->se, buffer, size, offset)) {
       
  1576     return 0;
       
  1577   }
       
  1578   if (!atom_esds_copy_data (&mp4s->es, buffer, size, offset)) {
       
  1579     return 0;
       
  1580   }
       
  1581 
       
  1582   atom_write_size (buffer, size, offset, original_offset);
       
  1583   return *offset - original_offset;
       
  1584 }
       
  1585 
       
  1586 static guint64
       
  1587 atom_hint_sample_entry_copy_data (AtomHintSampleEntry * hse, guint8 ** buffer,
       
  1588     guint64 * size, guint64 * offset)
       
  1589 {
       
  1590   guint64 original_offset = *offset;
       
  1591 
       
  1592   if (!atom_sample_entry_copy_data (&hse->se, buffer, size, offset)) {
       
  1593     return 0;
       
  1594   }
       
  1595 
       
  1596   prop_copy_uint32 (hse->size, buffer, size, offset);
       
  1597   prop_copy_uint8_array (hse->data, hse->size, buffer, size, offset);
       
  1598 
       
  1599   atom_write_size (buffer, size, offset, original_offset);
       
  1600   return *offset - original_offset;
       
  1601 }
       
  1602 
       
  1603 static guint64
       
  1604 sample_entry_mp4a_copy_data (SampleTableEntryMP4A * mp4a, guint8 ** buffer,
       
  1605     guint64 * size, guint64 * offset)
       
  1606 {
       
  1607   guint64 original_offset = *offset;
       
  1608 
       
  1609   if (!atom_sample_entry_copy_data (&mp4a->se, buffer, size, offset)) {
       
  1610     return 0;
       
  1611   }
       
  1612 
       
  1613   prop_copy_uint16 (mp4a->version, buffer, size, offset);
       
  1614   prop_copy_uint16 (mp4a->revision_level, buffer, size, offset);
       
  1615   prop_copy_uint32 (mp4a->vendor, buffer, size, offset);
       
  1616   prop_copy_uint16 (mp4a->channels, buffer, size, offset);
       
  1617   prop_copy_uint16 (mp4a->sample_size, buffer, size, offset);
       
  1618   prop_copy_uint16 (mp4a->compression_id, buffer, size, offset);
       
  1619   prop_copy_uint16 (mp4a->packet_size, buffer, size, offset);
       
  1620   prop_copy_uint32 (mp4a->sample_rate, buffer, size, offset);
       
  1621 
       
  1622   /* this should always be 0 for mp4 flavor */
       
  1623   if (mp4a->version == 1) {
       
  1624     prop_copy_uint32 (mp4a->samples_per_packet, buffer, size, offset);
       
  1625     prop_copy_uint32 (mp4a->bytes_per_packet, buffer, size, offset);
       
  1626     prop_copy_uint32 (mp4a->bytes_per_frame, buffer, size, offset);
       
  1627     prop_copy_uint32 (mp4a->bytes_per_sample, buffer, size, offset);
       
  1628   }
       
  1629 
       
  1630   if (mp4a->extension_atoms) {
       
  1631     if (!atom_info_list_copy_data (mp4a->extension_atoms, buffer, size, offset))
       
  1632       return 0;
       
  1633   }
       
  1634 
       
  1635   atom_write_size (buffer, size, offset, original_offset);
       
  1636   return *offset - original_offset;
       
  1637 }
       
  1638 
       
  1639 static guint64
       
  1640 sample_entry_mp4v_copy_data (SampleTableEntryMP4V * mp4v, guint8 ** buffer,
       
  1641     guint64 * size, guint64 * offset)
       
  1642 {
       
  1643   guint64 original_offset = *offset;
       
  1644 
       
  1645   if (!atom_sample_entry_copy_data (&mp4v->se, buffer, size, offset)) {
       
  1646     return 0;
       
  1647   }
       
  1648 
       
  1649   prop_copy_uint16 (mp4v->version, buffer, size, offset);
       
  1650   prop_copy_uint16 (mp4v->revision_level, buffer, size, offset);
       
  1651   prop_copy_fourcc (mp4v->vendor, buffer, size, offset);
       
  1652   prop_copy_uint32 (mp4v->temporal_quality, buffer, size, offset);
       
  1653   prop_copy_uint32 (mp4v->spatial_quality, buffer, size, offset);
       
  1654 
       
  1655   prop_copy_uint16 (mp4v->width, buffer, size, offset);
       
  1656   prop_copy_uint16 (mp4v->height, buffer, size, offset);
       
  1657 
       
  1658   prop_copy_uint32 (mp4v->horizontal_resolution, buffer, size, offset);
       
  1659   prop_copy_uint32 (mp4v->vertical_resolution, buffer, size, offset);
       
  1660   prop_copy_uint32 (mp4v->datasize, buffer, size, offset);
       
  1661 
       
  1662   prop_copy_uint16 (mp4v->frame_count, buffer, size, offset);
       
  1663 
       
  1664   prop_copy_fixed_size_string ((guint8 *) mp4v->compressor, 32, buffer, size,
       
  1665       offset);
       
  1666 
       
  1667   prop_copy_uint16 (mp4v->depth, buffer, size, offset);
       
  1668   prop_copy_uint16 (mp4v->color_table_id, buffer, size, offset);
       
  1669 
       
  1670   /* extra atoms */
       
  1671   if (mp4v->extension_atoms &&
       
  1672       !atom_info_list_copy_data (mp4v->extension_atoms, buffer, size, offset))
       
  1673     return 0;
       
  1674 
       
  1675   atom_write_size (buffer, size, offset, original_offset);
       
  1676   return *offset - original_offset;
       
  1677 }
       
  1678 
       
  1679 static guint64
       
  1680 atom_stsz_copy_data (AtomSTSZ * stsz, guint8 ** buffer, guint64 * size,
       
  1681     guint64 * offset)
       
  1682 {
       
  1683   guint64 original_offset = *offset;
       
  1684   GList *walker;
       
  1685 
       
  1686   if (!atom_full_copy_data (&stsz->header, buffer, size, offset)) {
       
  1687     return 0;
       
  1688   }
       
  1689 
       
  1690   prop_copy_uint32 (stsz->sample_size, buffer, size, offset);
       
  1691   prop_copy_uint32 (stsz->table_size, buffer, size, offset);
       
  1692   /* minimize realloc */
       
  1693   prop_copy_ensure_buffer (buffer, size, offset, 4 * stsz->table_size);
       
  1694   if (stsz->sample_size == 0) {
       
  1695     for (walker = g_list_last (stsz->entries); walker != NULL;
       
  1696         walker = g_list_previous (walker)) {
       
  1697       prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
       
  1698           offset);
       
  1699     }
       
  1700   }
       
  1701 
       
  1702   atom_write_size (buffer, size, offset, original_offset);
       
  1703   return *offset - original_offset;
       
  1704 }
       
  1705 
       
  1706 static guint64
       
  1707 atom_stsc_copy_data (AtomSTSC * stsc, guint8 ** buffer, guint64 * size,
       
  1708     guint64 * offset)
       
  1709 {
       
  1710   guint64 original_offset = *offset;
       
  1711   GList *walker;
       
  1712 
       
  1713   if (!atom_full_copy_data (&stsc->header, buffer, size, offset)) {
       
  1714     return 0;
       
  1715   }
       
  1716 
       
  1717   prop_copy_uint32 (stsc->n_entries, buffer, size, offset);
       
  1718   /* minimize realloc */
       
  1719   prop_copy_ensure_buffer (buffer, size, offset, 12 * stsc->n_entries);
       
  1720 
       
  1721   for (walker = g_list_last (stsc->entries); walker != NULL;
       
  1722       walker = g_list_previous (walker)) {
       
  1723     STSCEntry *entry = (STSCEntry *) walker->data;
       
  1724 
       
  1725     prop_copy_uint32 (entry->first_chunk, buffer, size, offset);
       
  1726     prop_copy_uint32 (entry->samples_per_chunk, buffer, size, offset);
       
  1727     prop_copy_uint32 (entry->sample_description_index, buffer, size, offset);
       
  1728   }
       
  1729 
       
  1730   atom_write_size (buffer, size, offset, original_offset);
       
  1731   return *offset - original_offset;
       
  1732 }
       
  1733 
       
  1734 static guint64
       
  1735 atom_ctts_copy_data (AtomCTTS * ctts, guint8 ** buffer, guint64 * size,
       
  1736     guint64 * offset)
       
  1737 {
       
  1738   guint64 original_offset = *offset;
       
  1739   GList *walker;
       
  1740 
       
  1741   if (!atom_full_copy_data (&ctts->header, buffer, size, offset)) {
       
  1742     return 0;
       
  1743   }
       
  1744 
       
  1745   prop_copy_uint32 (ctts->n_entries, buffer, size, offset);
       
  1746   /* minimize realloc */
       
  1747   prop_copy_ensure_buffer (buffer, size, offset, 8 * ctts->n_entries);
       
  1748   for (walker = g_list_last (ctts->entries); walker != NULL;
       
  1749       walker = g_list_previous (walker)) {
       
  1750     CTTSEntry *entry = (CTTSEntry *) walker->data;
       
  1751 
       
  1752     prop_copy_uint32 (entry->samplecount, buffer, size, offset);
       
  1753     prop_copy_uint32 (entry->sampleoffset, buffer, size, offset);
       
  1754   }
       
  1755 
       
  1756   atom_write_size (buffer, size, offset, original_offset);
       
  1757   return *offset - original_offset;
       
  1758 }
       
  1759 
       
  1760 static guint64
       
  1761 atom_stco64_copy_data (AtomSTCO64 * stco64, guint8 ** buffer, guint64 * size,
       
  1762     guint64 * offset)
       
  1763 {
       
  1764   guint64 original_offset = *offset;
       
  1765   GList *walker;
       
  1766   gboolean trunc_to_32 = stco64->header.header.type == FOURCC_stco;
       
  1767 
       
  1768   if (!atom_full_copy_data (&stco64->header, buffer, size, offset)) {
       
  1769     return 0;
       
  1770   }
       
  1771 
       
  1772   prop_copy_uint32 (stco64->n_entries, buffer, size, offset);
       
  1773 
       
  1774   /* minimize realloc */
       
  1775   prop_copy_ensure_buffer (buffer, size, offset, 8 * stco64->n_entries);
       
  1776   for (walker = g_list_last (stco64->entries); walker != NULL;
       
  1777       walker = g_list_previous (walker)) {
       
  1778     guint64 *value = (guint64 *) walker->data;
       
  1779 
       
  1780     if (trunc_to_32) {
       
  1781       prop_copy_uint32 ((guint32) * value, buffer, size, offset);
       
  1782     } else {
       
  1783       prop_copy_uint64 (*value, buffer, size, offset);
       
  1784     }
       
  1785   }
       
  1786 
       
  1787   atom_write_size (buffer, size, offset, original_offset);
       
  1788   return *offset - original_offset;
       
  1789 }
       
  1790 
       
  1791 static guint64
       
  1792 atom_stss_copy_data (AtomSTSS * stss, guint8 ** buffer, guint64 * size,
       
  1793     guint64 * offset)
       
  1794 {
       
  1795   guint64 original_offset = *offset;
       
  1796   GList *walker;
       
  1797 
       
  1798   if (stss->entries == NULL) {
       
  1799     /* FIXME not needing this atom might be confused with error while copying */
       
  1800     return 0;
       
  1801   }
       
  1802 
       
  1803   if (!atom_full_copy_data (&stss->header, buffer, size, offset)) {
       
  1804     return 0;
       
  1805   }
       
  1806 
       
  1807   prop_copy_uint32 (stss->n_entries, buffer, size, offset);
       
  1808   /* minimize realloc */
       
  1809   prop_copy_ensure_buffer (buffer, size, offset, 4 * stss->n_entries);
       
  1810   for (walker = g_list_last (stss->entries); walker != NULL;
       
  1811       walker = g_list_previous (walker)) {
       
  1812     prop_copy_uint32 ((guint32) GPOINTER_TO_UINT (walker->data), buffer, size,
       
  1813         offset);
       
  1814   }
       
  1815 
       
  1816   atom_write_size (buffer, size, offset, original_offset);
       
  1817   return *offset - original_offset;
       
  1818 }
       
  1819 
       
  1820 static guint64
       
  1821 atom_stsd_copy_data (AtomSTSD * stsd, guint8 ** buffer, guint64 * size,
       
  1822     guint64 * offset)
       
  1823 {
       
  1824   guint64 original_offset = *offset;
       
  1825   GList *walker;
       
  1826 
       
  1827   if (!atom_full_copy_data (&stsd->header, buffer, size, offset)) {
       
  1828     return 0;
       
  1829   }
       
  1830 
       
  1831   prop_copy_uint32 (stsd->n_entries, buffer, size, offset);
       
  1832 
       
  1833   for (walker = g_list_last (stsd->entries); walker != NULL;
       
  1834       walker = g_list_previous (walker)) {
       
  1835     SampleTableEntry *se = (SampleTableEntry *) walker->data;
       
  1836 
       
  1837     switch (((Atom *) walker->data)->type) {
       
  1838       case FOURCC_mp4a:
       
  1839         if (!sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *) walker->data,
       
  1840                 buffer, size, offset)) {
       
  1841           return 0;
       
  1842         }
       
  1843         break;
       
  1844       case FOURCC_mp4s:
       
  1845         if (!atom_mp4s_copy_data ((SampleTableEntryMP4S *) walker->data,
       
  1846                 buffer, size, offset)) {
       
  1847           return 0;
       
  1848         }
       
  1849         break;
       
  1850       case FOURCC_mp4v:
       
  1851         if (!sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *) walker->data,
       
  1852                 buffer, size, offset)) {
       
  1853           return 0;
       
  1854         }
       
  1855         break;
       
  1856       default:
       
  1857         if (se->kind == VIDEO) {
       
  1858           size += sample_entry_mp4v_copy_data ((SampleTableEntryMP4V *)
       
  1859               walker->data, buffer, size, offset);
       
  1860         } else if (se->kind == AUDIO) {
       
  1861           size += sample_entry_mp4a_copy_data ((SampleTableEntryMP4A *)
       
  1862               walker->data, buffer, size, offset);
       
  1863         } else {
       
  1864           if (!atom_hint_sample_entry_copy_data (
       
  1865                   (AtomHintSampleEntry *) walker->data, buffer, size, offset)) {
       
  1866             return 0;
       
  1867           }
       
  1868         }
       
  1869         break;
       
  1870     }
       
  1871   }
       
  1872 
       
  1873   atom_write_size (buffer, size, offset, original_offset);
       
  1874   return *offset - original_offset;
       
  1875 }
       
  1876 
       
  1877 static guint64
       
  1878 atom_stbl_copy_data (AtomSTBL * stbl, guint8 ** buffer, guint64 * size,
       
  1879     guint64 * offset)
       
  1880 {
       
  1881   guint64 original_offset = *offset;
       
  1882 
       
  1883   if (!atom_copy_data (&stbl->header, buffer, size, offset)) {
       
  1884     return 0;
       
  1885   }
       
  1886 
       
  1887   if (!atom_stsd_copy_data (&stbl->stsd, buffer, size, offset)) {
       
  1888     return 0;
       
  1889   }
       
  1890   if (!atom_stts_copy_data (&stbl->stts, buffer, size, offset)) {
       
  1891     return 0;
       
  1892   }
       
  1893   /* this atom is optional, so let's check if we need it
       
  1894    * (to avoid false error) */
       
  1895   if (stbl->stss.entries) {
       
  1896     if (!atom_stss_copy_data (&stbl->stss, buffer, size, offset)) {
       
  1897       return 0;
       
  1898     }
       
  1899   }
       
  1900 
       
  1901   if (!atom_stsc_copy_data (&stbl->stsc, buffer, size, offset)) {
       
  1902     return 0;
       
  1903   }
       
  1904   if (!atom_stsz_copy_data (&stbl->stsz, buffer, size, offset)) {
       
  1905     return 0;
       
  1906   }
       
  1907   if (stbl->ctts) {
       
  1908     if (!atom_ctts_copy_data (stbl->ctts, buffer, size, offset)) {
       
  1909       return 0;
       
  1910     }
       
  1911   }
       
  1912   if (!atom_stco64_copy_data (&stbl->stco64, buffer, size, offset)) {
       
  1913     return 0;
       
  1914   }
       
  1915 
       
  1916   atom_write_size (buffer, size, offset, original_offset);
       
  1917   return original_offset - *offset;
       
  1918 }
       
  1919 
       
  1920 
       
  1921 static guint64
       
  1922 atom_dref_copy_data (AtomDREF * dref, guint8 ** buffer, guint64 * size,
       
  1923     guint64 * offset)
       
  1924 {
       
  1925   guint64 original_offset = *offset;
       
  1926   GList *walker;
       
  1927 
       
  1928   if (!atom_full_copy_data (&dref->header, buffer, size, offset)) {
       
  1929     return 0;
       
  1930   }
       
  1931 
       
  1932   prop_copy_uint32 (g_list_length (dref->entries), buffer, size, offset);
       
  1933 
       
  1934   walker = dref->entries;
       
  1935   while (walker != NULL) {
       
  1936     Atom *atom = (Atom *) walker->data;
       
  1937 
       
  1938     if (atom->type == FOURCC_url_) {
       
  1939       atom_url_copy_data ((AtomURL *) atom, buffer, size, offset);
       
  1940     } else if (atom->type == FOURCC_alis) {
       
  1941       atom_full_copy_data ((AtomFull *) atom, buffer, size, offset);
       
  1942     } else {
       
  1943       g_error ("Unsupported atom used inside dref atom");
       
  1944     }
       
  1945     walker = g_list_next (walker);
       
  1946   }
       
  1947 
       
  1948   atom_write_size (buffer, size, offset, original_offset);
       
  1949   return *offset - original_offset;
       
  1950 }
       
  1951 
       
  1952 static guint64
       
  1953 atom_dinf_copy_data (AtomDINF * dinf, guint8 ** buffer, guint64 * size,
       
  1954     guint64 * offset)
       
  1955 {
       
  1956   guint64 original_offset = *offset;
       
  1957 
       
  1958   if (!atom_copy_data (&dinf->header, buffer, size, offset)) {
       
  1959     return 0;
       
  1960   }
       
  1961 
       
  1962   if (!atom_dref_copy_data (&dinf->dref, buffer, size, offset)) {
       
  1963     return 0;
       
  1964   }
       
  1965 
       
  1966   atom_write_size (buffer, size, offset, original_offset);
       
  1967   return original_offset - *offset;
       
  1968 }
       
  1969 
       
  1970 static guint64
       
  1971 atom_minf_copy_data (AtomMINF * minf, guint8 ** buffer, guint64 * size,
       
  1972     guint64 * offset)
       
  1973 {
       
  1974   guint64 original_offset = *offset;
       
  1975 
       
  1976   if (!atom_copy_data (&minf->header, buffer, size, offset)) {
       
  1977     return 0;
       
  1978   }
       
  1979 
       
  1980   if (minf->vmhd) {
       
  1981     if (!atom_vmhd_copy_data (minf->vmhd, buffer, size, offset)) {
       
  1982       return 0;
       
  1983     }
       
  1984   } else if (minf->smhd) {
       
  1985     if (!atom_smhd_copy_data (minf->smhd, buffer, size, offset)) {
       
  1986       return 0;
       
  1987     }
       
  1988   } else if (minf->hmhd) {
       
  1989     if (!atom_hmhd_copy_data (minf->hmhd, buffer, size, offset)) {
       
  1990       return 0;
       
  1991     }
       
  1992   }
       
  1993 
       
  1994   if (minf->hdlr) {
       
  1995     if (!atom_hdlr_copy_data (minf->hdlr, buffer, size, offset)) {
       
  1996       return 0;
       
  1997     }
       
  1998   }
       
  1999 
       
  2000   if (!atom_dinf_copy_data (&minf->dinf, buffer, size, offset)) {
       
  2001     return 0;
       
  2002   }
       
  2003   if (!atom_stbl_copy_data (&minf->stbl, buffer, size, offset)) {
       
  2004     return 0;
       
  2005   }
       
  2006 
       
  2007   atom_write_size (buffer, size, offset, original_offset);
       
  2008   return *offset - original_offset;
       
  2009 }
       
  2010 
       
  2011 static guint64
       
  2012 atom_mdhd_copy_data (AtomMDHD * mdhd, guint8 ** buffer, guint64 * size,
       
  2013     guint64 * offset)
       
  2014 {
       
  2015   guint64 original_offset = *offset;
       
  2016 
       
  2017   if (!atom_full_copy_data (&mdhd->header, buffer, size, offset)) {
       
  2018     return 0;
       
  2019   }
       
  2020 
       
  2021   if (!common_time_info_copy_data (&mdhd->time_info,
       
  2022           atom_full_get_version (&mdhd->header) == 0, buffer, size, offset)) {
       
  2023     return 0;
       
  2024   }
       
  2025 
       
  2026   prop_copy_uint16 (mdhd->language_code, buffer, size, offset);
       
  2027   prop_copy_uint16 (mdhd->quality, buffer, size, offset);
       
  2028 
       
  2029   atom_write_size (buffer, size, offset, original_offset);
       
  2030   return *offset - original_offset;
       
  2031 }
       
  2032 
       
  2033 static guint64
       
  2034 atom_mdia_copy_data (AtomMDIA * mdia, guint8 ** buffer, guint64 * size,
       
  2035     guint64 * offset)
       
  2036 {
       
  2037   guint64 original_offset = *offset;
       
  2038 
       
  2039   if (!atom_copy_data (&mdia->header, buffer, size, offset)) {
       
  2040     return 0;
       
  2041   }
       
  2042   if (!atom_mdhd_copy_data (&mdia->mdhd, buffer, size, offset)) {
       
  2043     return 0;
       
  2044   }
       
  2045   if (!atom_hdlr_copy_data (&mdia->hdlr, buffer, size, offset)) {
       
  2046     return 0;
       
  2047   }
       
  2048 
       
  2049   if (!atom_minf_copy_data (&mdia->minf, buffer, size, offset)) {
       
  2050     return 0;
       
  2051   }
       
  2052 
       
  2053   atom_write_size (buffer, size, offset, original_offset);
       
  2054   return *offset - original_offset;
       
  2055 }
       
  2056 
       
  2057 static guint64
       
  2058 atom_trak_copy_data (AtomTRAK * trak, guint8 ** buffer, guint64 * size,
       
  2059     guint64 * offset)
       
  2060 {
       
  2061   guint64 original_offset = *offset;
       
  2062 
       
  2063   if (!atom_copy_data (&trak->header, buffer, size, offset)) {
       
  2064     return 0;
       
  2065   }
       
  2066   if (!atom_tkhd_copy_data (&trak->tkhd, buffer, size, offset)) {
       
  2067     return 0;
       
  2068   }
       
  2069 
       
  2070   if (!atom_mdia_copy_data (&trak->mdia, buffer, size, offset)) {
       
  2071     return 0;
       
  2072   }
       
  2073 
       
  2074   atom_write_size (buffer, size, offset, original_offset);
       
  2075   return *offset - original_offset;
       
  2076 }
       
  2077 
       
  2078 static guint64
       
  2079 atom_tag_data_copy_data (AtomTagData * data, guint8 ** buffer, guint64 * size,
       
  2080     guint64 * offset)
       
  2081 {
       
  2082   guint64 original_offset = *offset;
       
  2083 
       
  2084   if (!atom_full_copy_data (&data->header, buffer, size, offset)) {
       
  2085     return 0;
       
  2086   }
       
  2087 
       
  2088   prop_copy_uint32 (data->reserved, buffer, size, offset);
       
  2089   prop_copy_uint8_array (data->data, data->datalen, buffer, size, offset);
       
  2090 
       
  2091   atom_write_size (buffer, size, offset, original_offset);
       
  2092   return *offset - original_offset;
       
  2093 }
       
  2094 
       
  2095 static guint64
       
  2096 atom_tag_copy_data (AtomTag * tag, guint8 ** buffer, guint64 * size,
       
  2097     guint64 * offset)
       
  2098 {
       
  2099   guint64 original_offset = *offset;
       
  2100 
       
  2101   if (!atom_copy_data (&tag->header, buffer, size, offset)) {
       
  2102     return 0;
       
  2103   }
       
  2104 
       
  2105   if (!atom_tag_data_copy_data (&tag->data, buffer, size, offset)) {
       
  2106     return 0;
       
  2107   }
       
  2108 
       
  2109   atom_write_size (buffer, size, offset, original_offset);
       
  2110   return *offset - original_offset;
       
  2111 }
       
  2112 
       
  2113 static guint64
       
  2114 atom_ilst_copy_data (AtomILST * ilst, guint8 ** buffer, guint64 * size,
       
  2115     guint64 * offset)
       
  2116 {
       
  2117   guint64 original_offset = *offset;
       
  2118 
       
  2119   if (!atom_copy_data (&ilst->header, buffer, size, offset)) {
       
  2120     return 0;
       
  2121   }
       
  2122   /* extra atoms */
       
  2123   if (ilst->entries &&
       
  2124       !atom_info_list_copy_data (ilst->entries, buffer, size, offset))
       
  2125     return 0;
       
  2126 
       
  2127   atom_write_size (buffer, size, offset, original_offset);
       
  2128   return *offset - original_offset;
       
  2129 }
       
  2130 
       
  2131 static guint64
       
  2132 atom_meta_copy_data (AtomMETA * meta, guint8 ** buffer, guint64 * size,
       
  2133     guint64 * offset)
       
  2134 {
       
  2135   guint64 original_offset = *offset;
       
  2136 
       
  2137   if (!atom_full_copy_data (&meta->header, buffer, size, offset)) {
       
  2138     return 0;
       
  2139   }
       
  2140   if (!atom_hdlr_copy_data (&meta->hdlr, buffer, size, offset)) {
       
  2141     return 0;
       
  2142   }
       
  2143   if (meta->ilst) {
       
  2144     if (!atom_ilst_copy_data (meta->ilst, buffer, size, offset)) {
       
  2145       return 0;
       
  2146     }
       
  2147   }
       
  2148 
       
  2149   atom_write_size (buffer, size, offset, original_offset);
       
  2150   return *offset - original_offset;
       
  2151 }
       
  2152 
       
  2153 static guint64
       
  2154 atom_udta_copy_data (AtomUDTA * udta, guint8 ** buffer, guint64 * size,
       
  2155     guint64 * offset)
       
  2156 {
       
  2157   guint64 original_offset = *offset;
       
  2158 
       
  2159   if (!atom_copy_data (&udta->header, buffer, size, offset)) {
       
  2160     return 0;
       
  2161   }
       
  2162   if (udta->meta) {
       
  2163     if (!atom_meta_copy_data (udta->meta, buffer, size, offset)) {
       
  2164       return 0;
       
  2165     }
       
  2166   } else if (udta->entries) {
       
  2167     /* extra atoms */
       
  2168     if (!atom_info_list_copy_data (udta->entries, buffer, size, offset))
       
  2169       return 0;
       
  2170   }
       
  2171 
       
  2172   atom_write_size (buffer, size, offset, original_offset);
       
  2173   return *offset - original_offset;
       
  2174 }
       
  2175 
       
  2176 guint64
       
  2177 atom_moov_copy_data (AtomMOOV * atom, guint8 ** buffer, guint64 * size,
       
  2178     guint64 * offset)
       
  2179 {
       
  2180   guint64 original_offset = *offset;
       
  2181   GList *walker;
       
  2182 
       
  2183   if (!atom_copy_data (&(atom->header), buffer, size, offset))
       
  2184     return 0;
       
  2185 
       
  2186   if (!atom_mvhd_copy_data (&(atom->mvhd), buffer, size, offset))
       
  2187     return 0;
       
  2188 
       
  2189   walker = g_list_first (atom->traks);
       
  2190   while (walker != NULL) {
       
  2191     if (!atom_trak_copy_data ((AtomTRAK *) walker->data, buffer, size, offset)) {
       
  2192       return 0;
       
  2193     }
       
  2194     walker = g_list_next (walker);
       
  2195   }
       
  2196 
       
  2197   if (atom->udta) {
       
  2198     if (!atom_udta_copy_data (atom->udta, buffer, size, offset)) {
       
  2199       return 0;
       
  2200     }
       
  2201   }
       
  2202 
       
  2203   atom_write_size (buffer, size, offset, original_offset);
       
  2204   return *offset - original_offset;
       
  2205 }
       
  2206 
       
  2207 static guint64
       
  2208 atom_wave_copy_data (AtomWAVE * wave, guint8 ** buffer,
       
  2209     guint64 * size, guint64 * offset)
       
  2210 {
       
  2211   guint64 original_offset = *offset;
       
  2212 
       
  2213   if (!atom_copy_data (&(wave->header), buffer, size, offset))
       
  2214     return 0;
       
  2215 
       
  2216   if (wave->extension_atoms) {
       
  2217     if (!atom_info_list_copy_data (wave->extension_atoms, buffer, size, offset))
       
  2218       return 0;
       
  2219   }
       
  2220 
       
  2221   atom_write_size (buffer, size, offset, original_offset);
       
  2222   return *offset - original_offset;
       
  2223 }
       
  2224 
       
  2225 /* -- end of copy data functions -- */
       
  2226 
       
  2227 /* -- general functions, API and support functions */
       
  2228 
       
  2229 /* add samples to tables */
       
  2230 
       
  2231 static STSCEntry *
       
  2232 stsc_entry_new (guint32 first_chunk, guint32 samples, guint32 desc_index)
       
  2233 {
       
  2234   STSCEntry *entry = g_new0 (STSCEntry, 1);
       
  2235 
       
  2236   entry->first_chunk = first_chunk;
       
  2237   entry->samples_per_chunk = samples;
       
  2238   entry->sample_description_index = desc_index;
       
  2239   return entry;
       
  2240 }
       
  2241 
       
  2242 static void
       
  2243 atom_stsc_add_new_entry (AtomSTSC * stsc, guint32 first_chunk, guint32 nsamples)
       
  2244 {
       
  2245   if (stsc->entries &&
       
  2246       ((STSCEntry *) stsc->entries->data)->samples_per_chunk == nsamples)
       
  2247     return;
       
  2248 
       
  2249   stsc->entries =
       
  2250       g_list_prepend (stsc->entries, stsc_entry_new (first_chunk, nsamples, 1));
       
  2251   stsc->n_entries++;
       
  2252 }
       
  2253 
       
  2254 static STTSEntry *
       
  2255 stts_entry_new (guint32 sample_count, gint32 sample_delta)
       
  2256 {
       
  2257   STTSEntry *entry = g_new0 (STTSEntry, 1);
       
  2258 
       
  2259   entry->sample_count = sample_count;
       
  2260   entry->sample_delta = sample_delta;
       
  2261   return entry;
       
  2262 }
       
  2263 
       
  2264 static void
       
  2265 atom_stts_add_entry (AtomSTTS * stts, guint32 sample_count, gint32 sample_delta)
       
  2266 {
       
  2267   STTSEntry *entry;
       
  2268 
       
  2269   if (stts->entries == NULL) {
       
  2270     stts->entries =
       
  2271         g_list_prepend (stts->entries, stts_entry_new (sample_count,
       
  2272             sample_delta));
       
  2273     stts->n_entries++;
       
  2274     return;
       
  2275   }
       
  2276   entry = (STTSEntry *) g_list_first (stts->entries)->data;
       
  2277   if (entry->sample_delta == sample_delta) {
       
  2278     entry->sample_count += sample_count;
       
  2279   } else {
       
  2280     stts->entries =
       
  2281         g_list_prepend (stts->entries, stts_entry_new (sample_count,
       
  2282             sample_delta));
       
  2283     stts->n_entries++;
       
  2284   }
       
  2285 }
       
  2286 
       
  2287 static void
       
  2288 atom_stsz_add_entry (AtomSTSZ * stsz, guint32 nsamples, guint32 size)
       
  2289 {
       
  2290   guint32 i;
       
  2291 
       
  2292   stsz->table_size += nsamples;
       
  2293   if (stsz->sample_size != 0) {
       
  2294     /* it is constant size, we don't need entries */
       
  2295     return;
       
  2296   }
       
  2297   for (i = 0; i < nsamples; i++) {
       
  2298     stsz->entries = g_list_prepend (stsz->entries, GUINT_TO_POINTER (size));
       
  2299   }
       
  2300 }
       
  2301 
       
  2302 static guint32
       
  2303 atom_stco64_get_entry_count (AtomSTCO64 * stco64)
       
  2304 {
       
  2305   return stco64->n_entries;
       
  2306 }
       
  2307 
       
  2308 static void
       
  2309 atom_stco64_add_entry (AtomSTCO64 * stco64, guint64 entry)
       
  2310 {
       
  2311   guint64 *pont = g_new (guint64, 1);
       
  2312 
       
  2313   *pont = entry;
       
  2314   stco64->entries = g_list_prepend (stco64->entries, pont);
       
  2315   stco64->n_entries++;
       
  2316 }
       
  2317 
       
  2318 static void
       
  2319 atom_stss_add_entry (AtomSTSS * stss, guint32 sample)
       
  2320 {
       
  2321   stss->entries = g_list_prepend (stss->entries, GUINT_TO_POINTER (sample));
       
  2322   stss->n_entries++;
       
  2323 }
       
  2324 
       
  2325 static void
       
  2326 atom_stbl_add_stss_entry (AtomSTBL * stbl)
       
  2327 {
       
  2328   guint32 sample_index = stbl->stsz.table_size;
       
  2329 
       
  2330   atom_stss_add_entry (&stbl->stss, sample_index);
       
  2331 }
       
  2332 
       
  2333 static void
       
  2334 atom_ctts_add_entry (AtomCTTS * ctts, guint32 nsamples, guint32 offset)
       
  2335 {
       
  2336   GList *walker;
       
  2337   CTTSEntry *entry;
       
  2338 
       
  2339   walker = g_list_first (ctts->entries);
       
  2340   entry = (walker == NULL) ? NULL : (CTTSEntry *) walker->data;
       
  2341 
       
  2342   if (entry == NULL || entry->sampleoffset != offset) {
       
  2343     CTTSEntry *entry = g_new0 (CTTSEntry, 1);
       
  2344 
       
  2345     entry->samplecount = nsamples;
       
  2346     entry->sampleoffset = offset;
       
  2347     ctts->entries = g_list_prepend (ctts->entries, entry);
       
  2348     ctts->n_entries++;
       
  2349   } else {
       
  2350     entry->samplecount += nsamples;
       
  2351   }
       
  2352 }
       
  2353 
       
  2354 static void
       
  2355 atom_stbl_add_ctts_entry (AtomSTBL * stbl, guint32 nsamples, guint32 offset)
       
  2356 {
       
  2357   if (stbl->ctts == NULL) {
       
  2358     stbl->ctts = atom_ctts_new ();
       
  2359   }
       
  2360   atom_ctts_add_entry (stbl->ctts, nsamples, offset);
       
  2361 }
       
  2362 
       
  2363 void
       
  2364 atom_trak_add_samples (AtomTRAK * trak, guint32 nsamples, guint32 delta,
       
  2365     guint32 size, guint64 chunk_offset, gboolean sync,
       
  2366     gboolean do_pts, gint64 pts_offset)
       
  2367 {
       
  2368   AtomSTBL *stbl = &trak->mdia.minf.stbl;
       
  2369 
       
  2370   atom_stts_add_entry (&stbl->stts, nsamples, delta);
       
  2371   atom_stsz_add_entry (&stbl->stsz, nsamples, size);
       
  2372   atom_stco64_add_entry (&stbl->stco64, chunk_offset);
       
  2373   atom_stsc_add_new_entry (&stbl->stsc,
       
  2374       atom_stco64_get_entry_count (&stbl->stco64), nsamples);
       
  2375   if (sync)
       
  2376     atom_stbl_add_stss_entry (stbl);
       
  2377   if (do_pts)
       
  2378     atom_stbl_add_ctts_entry (stbl, nsamples, pts_offset);
       
  2379 }
       
  2380 
       
  2381 /* trak and moov molding */
       
  2382 
       
  2383 guint32
       
  2384 atom_trak_get_timescale (AtomTRAK * trak)
       
  2385 {
       
  2386   return trak->mdia.mdhd.time_info.timescale;
       
  2387 }
       
  2388 
       
  2389 static void
       
  2390 atom_trak_set_id (AtomTRAK * trak, guint32 id)
       
  2391 {
       
  2392   trak->tkhd.track_ID = id;
       
  2393 }
       
  2394 
       
  2395 void
       
  2396 atom_moov_add_trak (AtomMOOV * moov, AtomTRAK * trak)
       
  2397 {
       
  2398   atom_trak_set_id (trak, moov->mvhd.next_track_id++);
       
  2399   moov->traks = g_list_append (moov->traks, trak);
       
  2400 }
       
  2401 
       
  2402 static guint64
       
  2403 atom_trak_get_duration (AtomTRAK * trak)
       
  2404 {
       
  2405   return trak->tkhd.duration;
       
  2406 }
       
  2407 
       
  2408 static guint64
       
  2409 atom_stts_get_total_duration (AtomSTTS * stts)
       
  2410 {
       
  2411   GList *walker = stts->entries;
       
  2412   guint64 sum = 0;
       
  2413 
       
  2414   while (walker) {
       
  2415     STTSEntry *entry = (STTSEntry *) walker->data;
       
  2416 
       
  2417     sum += (guint64) (entry->sample_count) * entry->sample_delta;
       
  2418     walker = g_list_next (walker);
       
  2419   }
       
  2420   return sum;
       
  2421 }
       
  2422 
       
  2423 static void
       
  2424 atom_trak_update_duration (AtomTRAK * trak, guint64 moov_timescale)
       
  2425 {
       
  2426   trak->mdia.mdhd.time_info.duration =
       
  2427       atom_stts_get_total_duration (&trak->mdia.minf.stbl.stts);
       
  2428   if (trak->mdia.mdhd.time_info.timescale != 0) {
       
  2429     trak->tkhd.duration =
       
  2430         gst_util_uint64_scale (trak->mdia.mdhd.time_info.duration,
       
  2431         moov_timescale, trak->mdia.mdhd.time_info.timescale);
       
  2432   } else {
       
  2433     trak->tkhd.duration = 0;
       
  2434   }
       
  2435 }
       
  2436 
       
  2437 static guint32
       
  2438 atom_moov_get_timescale (AtomMOOV * moov)
       
  2439 {
       
  2440   return moov->mvhd.time_info.timescale;
       
  2441 }
       
  2442 
       
  2443 void
       
  2444 atom_moov_update_timescale (AtomMOOV * moov, guint32 timescale)
       
  2445 {
       
  2446   moov->mvhd.time_info.timescale = timescale;
       
  2447 }
       
  2448 
       
  2449 void
       
  2450 atom_moov_update_duration (AtomMOOV * moov)
       
  2451 {
       
  2452   GList *traks = moov->traks;
       
  2453   guint64 dur, duration = 0;
       
  2454 
       
  2455   while (traks) {
       
  2456     AtomTRAK *trak = (AtomTRAK *) traks->data;
       
  2457 
       
  2458     atom_trak_update_duration (trak, atom_moov_get_timescale (moov));
       
  2459     dur = atom_trak_get_duration (trak);
       
  2460     if (dur > duration)
       
  2461       duration = dur;
       
  2462     traks = g_list_next (traks);
       
  2463   }
       
  2464   moov->mvhd.time_info.duration = duration;
       
  2465 }
       
  2466 
       
  2467 static void
       
  2468 atom_set_type (Atom * atom, guint32 fourcc)
       
  2469 {
       
  2470   atom->type = fourcc;
       
  2471 }
       
  2472 
       
  2473 static void
       
  2474 atom_stbl_set_64bits (AtomSTBL * stbl, gboolean use)
       
  2475 {
       
  2476   if (use) {
       
  2477     atom_set_type (&stbl->stco64.header.header, FOURCC_co64);
       
  2478   } else {
       
  2479     atom_set_type (&stbl->stco64.header.header, FOURCC_stco);
       
  2480   }
       
  2481 }
       
  2482 
       
  2483 static void
       
  2484 atom_trak_set_64bits (AtomTRAK * trak, gboolean use)
       
  2485 {
       
  2486   atom_stbl_set_64bits (&trak->mdia.minf.stbl, use);
       
  2487 }
       
  2488 
       
  2489 void
       
  2490 atom_moov_set_64bits (AtomMOOV * moov, gboolean large_file)
       
  2491 {
       
  2492   GList *traks = moov->traks;
       
  2493 
       
  2494   while (traks) {
       
  2495     AtomTRAK *trak = (AtomTRAK *) traks->data;
       
  2496 
       
  2497     atom_trak_set_64bits (trak, large_file);
       
  2498     traks = g_list_next (traks);
       
  2499   }
       
  2500 }
       
  2501 
       
  2502 static void
       
  2503 atom_stco64_chunks_add_offset (AtomSTCO64 * stco64, guint32 offset)
       
  2504 {
       
  2505   GList *entries = stco64->entries;
       
  2506 
       
  2507   while (entries) {
       
  2508     guint64 *value = (guint64 *) entries->data;
       
  2509 
       
  2510     *value += offset;
       
  2511     entries = g_list_next (entries);
       
  2512   }
       
  2513 }
       
  2514 
       
  2515 void
       
  2516 atom_moov_chunks_add_offset (AtomMOOV * moov, guint32 offset)
       
  2517 {
       
  2518   GList *traks = moov->traks;
       
  2519 
       
  2520   while (traks) {
       
  2521     AtomTRAK *trak = (AtomTRAK *) traks->data;
       
  2522 
       
  2523     atom_stco64_chunks_add_offset (&trak->mdia.minf.stbl.stco64, offset);
       
  2524     traks = g_list_next (traks);
       
  2525   }
       
  2526 }
       
  2527 
       
  2528 /*
       
  2529  * Meta tags functions
       
  2530  */
       
  2531 static void
       
  2532 atom_moov_init_metatags (AtomMOOV * moov, AtomsContext * context)
       
  2533 {
       
  2534   if (!moov->udta) {
       
  2535     moov->udta = atom_udta_new ();
       
  2536   }
       
  2537   if (context->flavor != ATOMS_TREE_FLAVOR_3GP) {
       
  2538     if (!moov->udta->meta) {
       
  2539       moov->udta->meta = atom_meta_new ();
       
  2540     }
       
  2541     if (!moov->udta->meta->ilst) {
       
  2542       moov->udta->meta->ilst = atom_ilst_new ();
       
  2543     }
       
  2544   }
       
  2545 }
       
  2546 
       
  2547 static void
       
  2548 atom_tag_data_alloc_data (AtomTagData * data, guint size)
       
  2549 {
       
  2550   if (data->data != NULL) {
       
  2551     g_free (data->data);
       
  2552   }
       
  2553   data->data = g_new0 (guint8, size);
       
  2554   data->datalen = size;
       
  2555 }
       
  2556 
       
  2557 static void
       
  2558 atom_moov_append_tag (AtomMOOV * moov, AtomInfo * tag)
       
  2559 {
       
  2560   GList **entries;
       
  2561 
       
  2562   atom_moov_init_metatags (moov, &moov->context);
       
  2563   if (moov->udta->meta)
       
  2564     entries = &moov->udta->meta->ilst->entries;
       
  2565   else
       
  2566     entries = &moov->udta->entries;
       
  2567   *entries = g_list_append (*entries, tag);
       
  2568 }
       
  2569 
       
  2570 void
       
  2571 atom_moov_add_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
       
  2572     const guint8 * data, guint size)
       
  2573 {
       
  2574   AtomTag *tag;
       
  2575   AtomTagData *tdata;
       
  2576 
       
  2577   tag = atom_tag_new (fourcc, flags);
       
  2578   tdata = &tag->data;
       
  2579   atom_tag_data_alloc_data (tdata, size);
       
  2580   g_memmove (tdata->data, data, size);
       
  2581 
       
  2582   atom_moov_append_tag (moov,
       
  2583       build_atom_info_wrapper ((Atom *) tag, (gpointer)atom_tag_copy_data,
       
  2584               (gpointer)atom_tag_free));
       
  2585 }
       
  2586 
       
  2587 void
       
  2588 atom_moov_add_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
       
  2589 {
       
  2590   gint len = strlen (value);
       
  2591 
       
  2592   if (len > 0)
       
  2593     atom_moov_add_tag (moov, fourcc, METADATA_TEXT_FLAG, (guint8 *) value, len);
       
  2594 }
       
  2595 
       
  2596 void
       
  2597 atom_moov_add_uint_tag (AtomMOOV * moov, guint32 fourcc, guint32 flags,
       
  2598     guint32 value)
       
  2599 {
       
  2600   guint8 data[8] = { 0, };
       
  2601 
       
  2602   if (flags) {
       
  2603     GST_WRITE_UINT16_BE (data, value);
       
  2604     atom_moov_add_tag (moov, fourcc, flags, data, 2);
       
  2605   } else {
       
  2606     GST_WRITE_UINT32_BE (data + 2, value);
       
  2607     atom_moov_add_tag (moov, fourcc, flags, data, 8);
       
  2608   }
       
  2609 }
       
  2610 
       
  2611 void
       
  2612 atom_moov_add_blob_tag (AtomMOOV * moov, guint8 * data, guint size)
       
  2613 {
       
  2614   AtomData *data_atom;
       
  2615   GstBuffer *buf;
       
  2616   guint len;
       
  2617   guint32 fourcc;
       
  2618 
       
  2619   if (size < 8)
       
  2620     return;
       
  2621 
       
  2622   /* blob is unparsed atom;
       
  2623    * extract size and fourcc, and wrap remainder in data atom */
       
  2624   len = GST_READ_UINT32_BE (data);
       
  2625   fourcc = GST_READ_UINT32_LE (data + 4);
       
  2626   if (len > size)
       
  2627     return;
       
  2628 
       
  2629   buf = gst_buffer_new ();
       
  2630   GST_BUFFER_SIZE (buf) = len - 8;
       
  2631   GST_BUFFER_DATA (buf) = data + 8;
       
  2632 
       
  2633   data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
       
  2634   gst_buffer_unref (buf);
       
  2635 
       
  2636   atom_moov_append_tag (moov,
       
  2637       build_atom_info_wrapper ((Atom *) data_atom, (gpointer)atom_data_copy_data,
       
  2638               (gpointer)atom_data_free));
       
  2639 }
       
  2640 
       
  2641 void
       
  2642 atom_moov_add_3gp_tag (AtomMOOV * moov, guint32 fourcc, guint8 * data,
       
  2643     guint size)
       
  2644 {
       
  2645   AtomData *data_atom;
       
  2646   GstBuffer *buf;
       
  2647   guint8 *bdata;
       
  2648 
       
  2649   /* need full atom */
       
  2650   buf = gst_buffer_new_and_alloc (size + 4);
       
  2651   bdata = GST_BUFFER_DATA (buf);
       
  2652   /* full atom: version and flags */
       
  2653   GST_WRITE_UINT32_BE (bdata, 0);
       
  2654   memcpy (bdata + 4, data, size);
       
  2655 
       
  2656   data_atom = atom_data_new_from_gst_buffer (fourcc, buf);
       
  2657   gst_buffer_unref (buf);
       
  2658 
       
  2659   atom_moov_append_tag (moov,
       
  2660       build_atom_info_wrapper ((Atom *) data_atom, (gpointer)atom_data_copy_data,
       
  2661               (gpointer)atom_data_free));
       
  2662 }
       
  2663 
       
  2664 guint16
       
  2665 language_code (const char *lang)
       
  2666 {
       
  2667   g_return_val_if_fail (lang != NULL, 0);
       
  2668   g_return_val_if_fail (strlen (lang) == 3, 0);
       
  2669 
       
  2670   return (((lang[0] - 0x60) & 0x1F) << 10) + (((lang[1] - 0x60) & 0x1F) << 5) +
       
  2671       ((lang[2] - 0x60) & 0x1F);
       
  2672 }
       
  2673 
       
  2674 void
       
  2675 atom_moov_add_3gp_str_int_tag (AtomMOOV * moov, guint32 fourcc,
       
  2676     const gchar * value, gint16 ivalue)
       
  2677 {
       
  2678   gint len = 0, size = 0;
       
  2679   guint8 *data;
       
  2680 
       
  2681   if (value) {
       
  2682     len = strlen (value);
       
  2683     size = len + 3;
       
  2684   }
       
  2685 
       
  2686   if (ivalue >= 0)
       
  2687     size += 2;
       
  2688 
       
  2689   data = g_malloc (size + 3);
       
  2690   /* language tag and null-terminated UTF-8 string */
       
  2691   if (value) {
       
  2692     GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
       
  2693     /* include 0 terminator */
       
  2694     memcpy (data + 2, value, len + 1);
       
  2695   }
       
  2696   /* 16-bit unsigned int if standalone, otherwise 8-bit */
       
  2697   if (ivalue >= 0) {
       
  2698     if (size == 2)
       
  2699       GST_WRITE_UINT16_BE (data + size - 2, ivalue);
       
  2700     else {
       
  2701       GST_WRITE_UINT8 (data + size - 2, ivalue & 0xFF);
       
  2702       size--;
       
  2703     }
       
  2704   }
       
  2705 
       
  2706   atom_moov_add_3gp_tag (moov, fourcc, data, size);
       
  2707   g_free (data);
       
  2708 }
       
  2709 
       
  2710 void
       
  2711 atom_moov_add_3gp_str_tag (AtomMOOV * moov, guint32 fourcc, const gchar * value)
       
  2712 {
       
  2713   atom_moov_add_3gp_str_int_tag (moov, fourcc, value, -1);
       
  2714 }
       
  2715 
       
  2716 void
       
  2717 atom_moov_add_3gp_uint_tag (AtomMOOV * moov, guint32 fourcc, guint16 value)
       
  2718 {
       
  2719   atom_moov_add_3gp_str_int_tag (moov, fourcc, NULL, value);
       
  2720 }
       
  2721 
       
  2722 /*
       
  2723  * Functions for specifying media types
       
  2724  */
       
  2725 
       
  2726 static void
       
  2727 atom_minf_set_audio (AtomMINF * minf)
       
  2728 {
       
  2729   atom_minf_clear_handlers (minf);
       
  2730   minf->smhd = atom_smhd_new ();
       
  2731 }
       
  2732 
       
  2733 static void
       
  2734 atom_minf_set_video (AtomMINF * minf, AtomsContext * context)
       
  2735 {
       
  2736   atom_minf_clear_handlers (minf);
       
  2737   minf->vmhd = atom_vmhd_new (context);
       
  2738 }
       
  2739 
       
  2740 static void
       
  2741 atom_hdlr_set_type (AtomHDLR * hdlr, AtomsContext * context, guint32 comp_type,
       
  2742     guint32 hdlr_type)
       
  2743 {
       
  2744   if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
       
  2745     hdlr->component_type = comp_type;
       
  2746   }
       
  2747   hdlr->handler_type = hdlr_type;
       
  2748 }
       
  2749 
       
  2750 static void
       
  2751 atom_mdia_set_hdlr_type_audio (AtomMDIA * mdia, AtomsContext * context)
       
  2752 {
       
  2753   atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_soun);
       
  2754 }
       
  2755 
       
  2756 static void
       
  2757 atom_mdia_set_hdlr_type_video (AtomMDIA * mdia, AtomsContext * context)
       
  2758 {
       
  2759   atom_hdlr_set_type (&mdia->hdlr, context, FOURCC_mhlr, FOURCC_vide);
       
  2760 }
       
  2761 
       
  2762 static void
       
  2763 atom_mdia_set_audio (AtomMDIA * mdia, AtomsContext * context)
       
  2764 {
       
  2765   atom_mdia_set_hdlr_type_audio (mdia, context);
       
  2766   atom_minf_set_audio (&mdia->minf);
       
  2767 }
       
  2768 
       
  2769 static void
       
  2770 atom_mdia_set_video (AtomMDIA * mdia, AtomsContext * context)
       
  2771 {
       
  2772   atom_mdia_set_hdlr_type_video (mdia, context);
       
  2773   atom_minf_set_video (&mdia->minf, context);
       
  2774 }
       
  2775 
       
  2776 static void
       
  2777 atom_tkhd_set_audio (AtomTKHD * tkhd)
       
  2778 {
       
  2779   tkhd->volume = 0x0100;
       
  2780   tkhd->width = tkhd->height = 0;
       
  2781 }
       
  2782 
       
  2783 static void
       
  2784 atom_tkhd_set_video (AtomTKHD * tkhd, AtomsContext * context, guint32 width,
       
  2785     guint32 height)
       
  2786 {
       
  2787   tkhd->volume = 0;
       
  2788 
       
  2789   /* qt and ISO base media do not contradict, and examples agree */
       
  2790   tkhd->width = width;
       
  2791   tkhd->height = height;
       
  2792 }
       
  2793 
       
  2794 /* re-negotiation is prevented at top-level, so only 1 entry expected.
       
  2795  * Quite some more care here and elsewhere may be needed to
       
  2796  * support several entries */
       
  2797 static SampleTableEntryMP4A *
       
  2798 atom_trak_add_audio_entry (AtomTRAK * trak, AtomsContext * context,
       
  2799     guint32 type)
       
  2800 {
       
  2801   AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
       
  2802   SampleTableEntryMP4A *mp4a = sample_entry_mp4a_new ();
       
  2803 
       
  2804   mp4a->se.header.type = type;
       
  2805   mp4a->se.kind = AUDIO;
       
  2806   mp4a->compression_id = -1;
       
  2807   mp4a->se.data_reference_index = 1;
       
  2808 
       
  2809   stsd->entries = g_list_prepend (stsd->entries, mp4a);
       
  2810   stsd->n_entries++;
       
  2811   return mp4a;
       
  2812 }
       
  2813 
       
  2814 static SampleTableEntryMP4V *
       
  2815 atom_trak_add_video_entry (AtomTRAK * trak, AtomsContext * context,
       
  2816     guint32 type)
       
  2817 {
       
  2818   SampleTableEntryMP4V *mp4v = sample_entry_mp4v_new (context);
       
  2819   AtomSTSD *stsd = &trak->mdia.minf.stbl.stsd;
       
  2820 
       
  2821   mp4v->se.header.type = type;
       
  2822   mp4v->se.kind = VIDEO;
       
  2823   mp4v->se.data_reference_index = 1;
       
  2824   mp4v->horizontal_resolution = 72 << 16;
       
  2825   mp4v->vertical_resolution = 72 << 16;
       
  2826   if (context->flavor == ATOMS_TREE_FLAVOR_MOV) {
       
  2827     mp4v->spatial_quality = 512;
       
  2828     mp4v->temporal_quality = 512;
       
  2829   }
       
  2830 
       
  2831   stsd->entries = g_list_prepend (stsd->entries, mp4v);
       
  2832   stsd->n_entries++;
       
  2833   return mp4v;
       
  2834 }
       
  2835 
       
  2836 static void
       
  2837 atom_trak_set_constant_size_samples (AtomTRAK * trak, guint32 sample_size)
       
  2838 {
       
  2839   trak->mdia.minf.stbl.stsz.sample_size = sample_size;
       
  2840 }
       
  2841 
       
  2842 static void
       
  2843 atom_trak_set_audio (AtomTRAK * trak, AtomsContext * context)
       
  2844 {
       
  2845   atom_tkhd_set_audio (&trak->tkhd);
       
  2846   atom_mdia_set_audio (&trak->mdia, context);
       
  2847 }
       
  2848 
       
  2849 static void
       
  2850 atom_trak_set_video (AtomTRAK * trak, AtomsContext * context, guint32 width,
       
  2851     guint32 height)
       
  2852 {
       
  2853   atom_tkhd_set_video (&trak->tkhd, context, width, height);
       
  2854   atom_mdia_set_video (&trak->mdia, context);
       
  2855 }
       
  2856 
       
  2857 static void
       
  2858 atom_trak_set_audio_commons (AtomTRAK * trak, AtomsContext * context,
       
  2859     guint32 rate)
       
  2860 {
       
  2861   atom_trak_set_audio (trak, context);
       
  2862   trak->mdia.mdhd.time_info.timescale = rate;
       
  2863 }
       
  2864 
       
  2865 static void
       
  2866 atom_trak_set_video_commons (AtomTRAK * trak, AtomsContext * context,
       
  2867     guint32 rate, guint32 width, guint32 height)
       
  2868 {
       
  2869   atom_trak_set_video (trak, context, width, height);
       
  2870   trak->mdia.mdhd.time_info.timescale = rate;
       
  2871   trak->tkhd.width = width << 16;
       
  2872   trak->tkhd.height = height << 16;
       
  2873 }
       
  2874 
       
  2875 void
       
  2876 atom_trak_set_audio_type (AtomTRAK * trak, AtomsContext * context,
       
  2877     AudioSampleEntry * entry, guint32 scale, AtomInfo * ext, gint sample_size)
       
  2878 {
       
  2879   SampleTableEntryMP4A *ste;
       
  2880 
       
  2881   atom_trak_set_audio_commons (trak, context, scale);
       
  2882   ste = atom_trak_add_audio_entry (trak, context, entry->fourcc);
       
  2883 
       
  2884   trak->is_video = FALSE;
       
  2885   trak->is_h264 = FALSE;
       
  2886 
       
  2887   ste->version = entry->version;
       
  2888   ste->compression_id = entry->compression_id;
       
  2889   ste->sample_size = entry->sample_size;
       
  2890   ste->sample_rate = entry->sample_rate << 16;
       
  2891   ste->channels = entry->channels;
       
  2892 
       
  2893   ste->samples_per_packet = entry->samples_per_packet;
       
  2894   ste->bytes_per_sample = entry->bytes_per_sample;
       
  2895   ste->bytes_per_packet = entry->bytes_per_packet;
       
  2896   ste->bytes_per_frame = entry->bytes_per_frame;
       
  2897 
       
  2898   if (ext)
       
  2899     ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
       
  2900 
       
  2901   /* 0 size means variable size */
       
  2902   atom_trak_set_constant_size_samples (trak, sample_size);
       
  2903 }
       
  2904 
       
  2905 AtomInfo *
       
  2906 build_pasp_extension (AtomTRAK * trak, gint par_width, gint par_height)
       
  2907 {
       
  2908   AtomData *atom_data;
       
  2909   GstBuffer *buf;
       
  2910   guint8 *data;
       
  2911 
       
  2912   buf = gst_buffer_new_and_alloc (8);
       
  2913   data = GST_BUFFER_DATA (buf);
       
  2914 
       
  2915   /* ihdr = image header box */
       
  2916   GST_WRITE_UINT32_BE (data, par_width);
       
  2917   GST_WRITE_UINT32_BE (data + 4, par_height);
       
  2918 
       
  2919   atom_data = atom_data_new_from_gst_buffer (FOURCC_pasp, buf);
       
  2920   gst_buffer_unref (buf);
       
  2921 
       
  2922   return build_atom_info_wrapper ((Atom *) atom_data, (gpointer)atom_data_copy_data,
       
  2923           (gpointer)atom_data_free);
       
  2924 }
       
  2925 
       
  2926 void
       
  2927 atom_trak_set_video_type (AtomTRAK * trak, AtomsContext * context,
       
  2928     VisualSampleEntry * entry, guint32 scale, AtomInfo * ext)
       
  2929 {
       
  2930   SampleTableEntryMP4V *ste;
       
  2931   gint dwidth, dheight;
       
  2932   gint par_n = 0, par_d = 0;
       
  2933 
       
  2934   if ((entry->par_n != 1 || entry->par_d != 1) &&
       
  2935       (entry->par_n != entry->par_d)) {
       
  2936     par_n = entry->par_n;
       
  2937     par_d = entry->par_d;
       
  2938   }
       
  2939 
       
  2940   dwidth = entry->width;
       
  2941   dheight = entry->height;
       
  2942   /* ISO file spec says track header w/h indicates track's visual presentation
       
  2943    * (so this together with pixels w/h implicitly defines PAR) */
       
  2944   if (par_n && (context->flavor != ATOMS_TREE_FLAVOR_MOV)) {
       
  2945     if (par_n > par_d) {
       
  2946       dwidth = entry->width * par_n / par_d;
       
  2947       dheight = entry->height;
       
  2948     } else {
       
  2949       dwidth = entry->width * par_n / par_d;
       
  2950       dheight = entry->height;
       
  2951     }
       
  2952   }
       
  2953 
       
  2954   atom_trak_set_video_commons (trak, context, scale, dwidth, dheight);
       
  2955   ste = atom_trak_add_video_entry (trak, context, entry->fourcc);
       
  2956 
       
  2957   trak->is_video = TRUE;
       
  2958   trak->is_h264 = (entry->fourcc == FOURCC_avc1);
       
  2959 
       
  2960   ste->width = entry->width;
       
  2961   ste->height = entry->height;
       
  2962   ste->depth = entry->depth;
       
  2963   ste->color_table_id = entry->color_table_id;
       
  2964   ste->frame_count = entry->frame_count;
       
  2965 
       
  2966   if (ext)
       
  2967     ste->extension_atoms = g_list_prepend (ste->extension_atoms, ext);
       
  2968 
       
  2969   /* QT spec has a pasp extension atom in stsd that can hold PAR */
       
  2970   if (par_n && (context->flavor == ATOMS_TREE_FLAVOR_MOV)) {
       
  2971     ste->extension_atoms = g_list_append (ste->extension_atoms,
       
  2972         build_pasp_extension (trak, par_n, par_d));
       
  2973   }
       
  2974 }
       
  2975 
       
  2976 /* some sample description construction helpers */
       
  2977 
       
  2978 AtomInfo *
       
  2979 build_esds_extension (AtomTRAK * trak, guint8 object_type, guint8 stream_type,
       
  2980     const GstBuffer * codec_data)
       
  2981 {
       
  2982   guint32 track_id;
       
  2983   AtomESDS *esds;
       
  2984 
       
  2985   track_id = trak->tkhd.track_ID;
       
  2986 
       
  2987   esds = atom_esds_new ();
       
  2988   esds->es.id = track_id & 0xFFFF;
       
  2989   esds->es.dec_conf_desc.object_type = object_type;
       
  2990   esds->es.dec_conf_desc.stream_type = stream_type << 2 | 0x01;
       
  2991 
       
  2992   /* optional DecoderSpecificInfo */
       
  2993   if (codec_data) {
       
  2994     DecoderSpecificInfoDescriptor *desc;
       
  2995 
       
  2996     esds->es.dec_conf_desc.dec_specific_info = desc =
       
  2997         desc_dec_specific_info_new ();
       
  2998     desc_dec_specific_info_alloc_data (desc, GST_BUFFER_SIZE (codec_data));
       
  2999 
       
  3000     memcpy (desc->data, GST_BUFFER_DATA (codec_data),
       
  3001         GST_BUFFER_SIZE (codec_data));
       
  3002   }
       
  3003 
       
  3004   return build_atom_info_wrapper ((Atom *) esds, (gpointer)atom_esds_copy_data,
       
  3005           (gpointer)atom_esds_free);
       
  3006 }
       
  3007 
       
  3008 AtomInfo *
       
  3009 build_mov_aac_extension (AtomTRAK * trak, const GstBuffer * codec_data)
       
  3010 {
       
  3011   AtomWAVE *wave;
       
  3012   AtomFRMA *frma;
       
  3013   Atom *ext_atom;
       
  3014   GstBuffer *buf;
       
  3015 
       
  3016   /* Add WAVE atom to the MP4A sample table entry */
       
  3017   wave = atom_wave_new ();
       
  3018 
       
  3019   /* Prepend Terminator atom to the WAVE list first, so it ends up last */
       
  3020   ext_atom = (Atom *) atom_data_new (FOURCC_null);
       
  3021   wave->extension_atoms =
       
  3022       atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
       
  3023       (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
       
  3024 
       
  3025   /* Add ESDS atom to WAVE */
       
  3026   wave->extension_atoms = g_list_prepend (wave->extension_atoms,
       
  3027       build_esds_extension (trak, ESDS_OBJECT_TYPE_MPEG4_P3,
       
  3028           ESDS_STREAM_TYPE_AUDIO, codec_data));
       
  3029 
       
  3030   /* Add MP4A atom to the WAVE:
       
  3031    * not really in spec, but makes offset based players happy */
       
  3032   buf = gst_buffer_new_and_alloc (4);
       
  3033   *((guint32 *) GST_BUFFER_DATA (buf)) = 0;
       
  3034   ext_atom = (Atom *) atom_data_new_from_gst_buffer (FOURCC_mp4a, buf);
       
  3035   gst_buffer_unref (buf);
       
  3036 
       
  3037   wave->extension_atoms =
       
  3038       atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) ext_atom,
       
  3039       (AtomCopyDataFunc) atom_data_copy_data, (AtomFreeFunc) atom_data_free);
       
  3040 
       
  3041   /* Add FRMA to the WAVE */
       
  3042   frma = atom_frma_new ();
       
  3043   frma->media_type = FOURCC_mp4a;
       
  3044 
       
  3045   wave->extension_atoms =
       
  3046       atom_info_list_prepend_atom (wave->extension_atoms, (Atom *) frma,
       
  3047       (AtomCopyDataFunc) atom_frma_copy_data, (AtomFreeFunc) atom_frma_free);
       
  3048 
       
  3049   return build_atom_info_wrapper ((Atom *) wave, (gpointer)atom_wave_copy_data,
       
  3050           (gpointer)atom_wave_free);
       
  3051 }
       
  3052 
       
  3053 AtomInfo *
       
  3054 build_jp2h_extension (AtomTRAK * trak, gint width, gint height, guint32 fourcc)
       
  3055 {
       
  3056   AtomData *atom_data;
       
  3057   GstBuffer *buf;
       
  3058   guint8 *data;
       
  3059   guint8 cenum;
       
  3060 
       
  3061   if (fourcc == GST_MAKE_FOURCC ('s', 'R', 'G', 'B')) {
       
  3062     cenum = 0x10;
       
  3063   } else if (fourcc == GST_MAKE_FOURCC ('s', 'Y', 'U', 'V')) {
       
  3064     cenum = 0x12;
       
  3065   } else
       
  3066     return FALSE;
       
  3067 
       
  3068   buf = gst_buffer_new_and_alloc (22 + 15);
       
  3069   data = GST_BUFFER_DATA (buf);
       
  3070 
       
  3071   /* ihdr = image header box */
       
  3072   GST_WRITE_UINT32_BE (data, 22);
       
  3073   GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('i', 'h', 'd', 'r'));
       
  3074   GST_WRITE_UINT32_BE (data + 8, height);
       
  3075   GST_WRITE_UINT32_BE (data + 12, width);
       
  3076   /* FIXME perhaps parse from stream,
       
  3077    * though exactly 3 in any respectable colourspace */
       
  3078   GST_WRITE_UINT16_BE (data + 16, 3);
       
  3079   /* 8 bits per component, unsigned */
       
  3080   GST_WRITE_UINT8 (data + 18, 0x7);
       
  3081   /* compression type; reserved */
       
  3082   GST_WRITE_UINT8 (data + 19, 0x7);
       
  3083   /* colour space (un)known */
       
  3084   GST_WRITE_UINT8 (data + 20, 0x0);
       
  3085   /* intellectual property right (box present) */
       
  3086   GST_WRITE_UINT8 (data + 21, 0x0);
       
  3087 
       
  3088   /* colour specification box */
       
  3089   data += 22;
       
  3090   GST_WRITE_UINT32_BE (data, 15);
       
  3091   GST_WRITE_UINT32_LE (data + 4, GST_MAKE_FOURCC ('c', 'o', 'l', 'r'));
       
  3092   /* specification method: enumerated */
       
  3093   GST_WRITE_UINT8 (data + 8, 0x1);
       
  3094   /* precedence; reserved */
       
  3095   GST_WRITE_UINT8 (data + 9, 0x0);
       
  3096   /* approximation; reserved */
       
  3097   GST_WRITE_UINT8 (data + 10, 0x0);
       
  3098   /* enumerated colourspace */
       
  3099   GST_WRITE_UINT32_BE (data + 11, cenum);
       
  3100 
       
  3101   atom_data = atom_data_new_from_gst_buffer (FOURCC_jp2h, buf);
       
  3102   gst_buffer_unref (buf);
       
  3103 
       
  3104   return build_atom_info_wrapper ((Atom *) atom_data, (gpointer)atom_data_copy_data,
       
  3105           (gpointer)atom_data_free);
       
  3106 }
       
  3107 
       
  3108 AtomInfo *
       
  3109 build_codec_data_extension (guint32 fourcc, const GstBuffer * codec_data)
       
  3110 {
       
  3111   AtomData *data;
       
  3112   AtomInfo *result = NULL;
       
  3113 
       
  3114   if (codec_data) {
       
  3115     data = atom_data_new_from_gst_buffer (fourcc, codec_data);
       
  3116     result = build_atom_info_wrapper ((Atom *) data, (gpointer)atom_data_copy_data,
       
  3117             (gpointer)atom_data_free);
       
  3118   }
       
  3119 
       
  3120   return result;
       
  3121 }
       
  3122 
       
  3123 AtomInfo *
       
  3124 build_amr_extension ()
       
  3125 {
       
  3126   guint8 ext[9];
       
  3127   GstBuffer *buf;
       
  3128   AtomInfo *res;
       
  3129 
       
  3130   buf = gst_buffer_new ();
       
  3131   GST_BUFFER_DATA (buf) = ext;
       
  3132   GST_BUFFER_SIZE (buf) = sizeof (ext);
       
  3133 
       
  3134   /* vendor */
       
  3135   GST_WRITE_UINT32_LE (ext, 0);
       
  3136   /* decoder version */
       
  3137   GST_WRITE_UINT8 (ext + 4, 0);
       
  3138   /* mode set (all modes) */
       
  3139   GST_WRITE_UINT16_BE (ext + 5, 0x81FF);
       
  3140   /* mode change period (no restriction) */
       
  3141   GST_WRITE_UINT8 (ext + 7, 0);
       
  3142   /* frames per sample */
       
  3143   GST_WRITE_UINT8 (ext + 8, 1);
       
  3144 
       
  3145   res = build_codec_data_extension (GST_MAKE_FOURCC ('d', 'a', 'm', 'r'), buf);
       
  3146   gst_buffer_unref (buf);
       
  3147   return res;
       
  3148 }
       
  3149 
       
  3150 AtomInfo *
       
  3151 build_h263_extension ()
       
  3152 {
       
  3153   guint8 ext[7];
       
  3154   GstBuffer *buf;
       
  3155   AtomInfo *res;
       
  3156 
       
  3157   buf = gst_buffer_new ();
       
  3158   GST_BUFFER_DATA (buf) = ext;
       
  3159   GST_BUFFER_SIZE (buf) = sizeof (ext);
       
  3160 
       
  3161   /* vendor */
       
  3162   GST_WRITE_UINT32_LE (ext, 0);
       
  3163   /* decoder version */
       
  3164   GST_WRITE_UINT8 (ext + 4, 0);
       
  3165   /* level / profile */
       
  3166   /* FIXME ? maybe ? obtain somewhere; baseline for now */
       
  3167   GST_WRITE_UINT8 (ext + 5, 10);
       
  3168   GST_WRITE_UINT8 (ext + 6, 0);
       
  3169 
       
  3170   res = build_codec_data_extension (GST_MAKE_FOURCC ('d', '2', '6', '3'), buf);
       
  3171   gst_buffer_unref (buf);
       
  3172   return res;
       
  3173 }