]> git.tuebingen.mpg.de Git - paraslash.git/blob - mp4.c
mp4: Merge membuffer_write_std_tag() into create_ilst().
[paraslash.git] / mp4.c
1 /*
2  * Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
3  * FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
4  *
5  * See file COPYING.
6  */
7
8 #include <regex.h>
9
10 #include "para.h"
11 #include "error.h"
12 #include "portable_io.h"
13 #include "string.h"
14 #include "mp4.h"
15
16 struct mp4_track {
17         uint16_t channelCount;
18         uint16_t sampleRate;
19
20         /* stsz */
21         uint32_t stsz_sample_size;
22         uint32_t stsz_sample_count;
23         uint32_t *stsz_table;
24
25         /* stts */
26         uint32_t stts_entry_count;
27         uint32_t *stts_sample_count;
28
29         /* stsc */
30         uint32_t stsc_entry_count;
31         uint32_t *stsc_first_chunk;
32         uint32_t *stsc_samples_per_chunk;
33
34         /* stsc */
35         uint32_t stco_entry_count;
36         uint32_t *stco_chunk_offset;
37
38         uint32_t timeScale;
39         uint64_t duration;
40 };
41
42 #define MAX_TRACKS 1024
43
44 struct mp4 {
45         const struct mp4_callback *cb;
46         int64_t current_position;
47
48         uint64_t moov_offset;
49         uint64_t moov_size;
50         uint64_t meta_offset;
51         uint32_t meta_size;
52         uint64_t ilst_offset;
53         uint32_t ilst_size;
54         uint64_t udta_offset;
55         uint32_t udta_size;
56
57         uint8_t last_atom;
58         uint64_t file_size;
59
60         /* incremental track index while reading the file */
61         int32_t total_tracks;
62         /* track data */
63         struct mp4_track *track[MAX_TRACKS];
64         /* the first audio track found */
65         struct mp4_track *audio_track;
66
67         /* metadata */
68         struct mp4_metadata meta;
69 };
70
71 /*
72  * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or
73  * read errors are treated as errors.
74  */
75 static int read_data(struct mp4 *f, void *data, size_t size)
76 {
77         while (size > 0) {
78                 ssize_t ret = f->cb->read(f->cb->user_data, data, size);
79                 if (ret < 0 && errno == EINTR)
80                         continue;
81                 /* regard EAGAIN as an error as reads should be blocking. */
82                 if (ret <= 0)
83                         return ret < 0? -1 : 0;
84                 f->current_position += ret;
85                 size -= ret;
86         }
87         return 1;
88 }
89
90 static int read_int64(struct mp4 *f, uint64_t *result)
91 {
92         uint8_t data[8];
93         int ret = read_data(f, data, 8);
94
95         if (ret > 0 && result)
96                 *result = read_u64_be(data);
97         return ret;
98 }
99
100 static int read_int32(struct mp4 *f, uint32_t *result)
101 {
102         uint8_t data[4];
103         int ret = read_data(f, data, 4);
104
105         if (ret > 0 && result)
106                 *result = read_u32_be(data);
107         return ret;
108 }
109
110 static int read_int24(struct mp4 *f, uint32_t *result)
111 {
112         uint8_t data[3];
113         int ret = read_data(f, data, 3);
114
115         if (ret > 0 && result)
116                 *result = read_u24_be(data);
117         return ret;
118 }
119
120 static int read_int16(struct mp4 *f, uint16_t *result)
121 {
122         uint8_t data[2];
123         int ret = read_data(f, data, 2);
124
125         if (ret > 0 && result)
126                 *result = read_u16_be(data);
127         return ret;
128 }
129
130 static uint8_t read_int8(struct mp4 *f, uint8_t *result)
131 {
132         uint8_t data[1];
133         int ret = read_data(f, data, 1);
134
135         if (ret > 0 && result)
136                 *result = data[0];
137         return ret;
138 }
139
140 static bool atom_compare(int8_t a1, int8_t b1, int8_t c1, int8_t d1,
141                 int8_t a2, int8_t b2, int8_t c2, int8_t d2)
142 {
143         return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2;
144 }
145
146 enum atoms {
147         /* atoms with subatoms */
148         ATOM_MOOV = 1,
149         ATOM_TRAK = 2,
150         ATOM_EDTS = 3,
151         ATOM_MDIA = 4,
152         ATOM_MINF = 5,
153         ATOM_STBL = 6,
154         ATOM_UDTA = 7,
155         ATOM_ILST = 8, /* iTunes Metadata list */
156         ATOM_TITLE = 9,
157         ATOM_ARTIST = 10,
158         ATOM_WRITER = 11,
159         ATOM_ALBUM = 12,
160         ATOM_DATE = 13,
161         ATOM_TOOL = 14,
162         ATOM_COMMENT = 15,
163         ATOM_GENRE1 = 16,
164         ATOM_TRACK = 17,
165         ATOM_DISC = 18,
166         ATOM_COMPILATION = 19,
167         ATOM_GENRE2 = 20,
168         ATOM_TEMPO = 21,
169         ATOM_COVER = 22,
170         ATOM_DRMS = 23,
171         ATOM_SINF = 24,
172         ATOM_SCHI = 25,
173
174         SUBATOMIC = 128,
175
176         /* atoms without subatoms */
177         ATOM_FTYP = 129,
178         ATOM_MDAT = 130,
179         ATOM_MVHD = 131,
180         ATOM_TKHD = 132,
181         ATOM_TREF = 133,
182         ATOM_MDHD = 134, /* track header */
183         ATOM_VMHD = 135,
184         ATOM_SMHD = 136,
185         ATOM_HMHD = 137,
186         ATOM_STSD = 138, /* sample description box */
187         ATOM_STTS = 139, /* time to sample box */
188         ATOM_STSZ = 140, /* sample size box */
189         ATOM_STZ2 = 141,
190         ATOM_STCO = 142, /* chunk offset box */
191         ATOM_STSC = 143, /* sample to chunk box */
192         ATOM_MP4A = 144,
193         ATOM_MP4V = 145,
194         ATOM_MP4S = 146,
195         ATOM_ESDS = 147,
196         ATOM_META = 148, /* iTunes Metadata box */
197         ATOM_NAME = 149, /* iTunes Metadata name box */
198         ATOM_DATA = 150, /* iTunes Metadata data box */
199         ATOM_CTTS = 151,
200         ATOM_FRMA = 152,
201         ATOM_IVIV = 153,
202         ATOM_PRIV = 154,
203         ATOM_USER = 155,
204         ATOM_KEY = 156,
205         ATOM_ALBUM_ARTIST = 157,
206         ATOM_CONTENTGROUP = 158,
207         ATOM_LYRICS = 159,
208         ATOM_DESCRIPTION = 160,
209         ATOM_NETWORK = 161,
210         ATOM_SHOW = 162,
211         ATOM_EPISODENAME = 163,
212         ATOM_SORTTITLE = 164,
213         ATOM_SORTALBUM = 165,
214         ATOM_SORTARTIST = 166,
215         ATOM_SORTALBUMARTIST = 167,
216         ATOM_SORTWRITER = 168,
217         ATOM_SORTSHOW = 169,
218         ATOM_SEASON = 170,
219         ATOM_EPISODE = 171,
220         ATOM_PODCAST = 172,
221
222         ATOM_UNKNOWN = 255
223 };
224
225 #define ATOM_FREE ATOM_UNKNOWN
226 #define ATOM_SKIP ATOM_UNKNOWN
227
228 #define COPYRIGHT_SYMBOL ((int8_t)0xA9)
229
230 static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
231 {
232         if (a == 'm') {
233                 if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
234                         return ATOM_MOOV;
235                 else if (atom_compare(a, b, c, d, 'm', 'i', 'n', 'f'))
236                         return ATOM_MINF;
237                 else if (atom_compare(a, b, c, d, 'm', 'd', 'i', 'a'))
238                         return ATOM_MDIA;
239                 else if (atom_compare(a, b, c, d, 'm', 'd', 'a', 't'))
240                         return ATOM_MDAT;
241                 else if (atom_compare(a, b, c, d, 'm', 'd', 'h', 'd'))
242                         return ATOM_MDHD;
243                 else if (atom_compare(a, b, c, d, 'm', 'v', 'h', 'd'))
244                         return ATOM_MVHD;
245                 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'a'))
246                         return ATOM_MP4A;
247                 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'v'))
248                         return ATOM_MP4V;
249                 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 's'))
250                         return ATOM_MP4S;
251                 else if (atom_compare(a, b, c, d, 'm', 'e', 't', 'a'))
252                         return ATOM_META;
253         } else if (a == 't') {
254                 if (atom_compare(a, b, c, d, 't', 'r', 'a', 'k'))
255                         return ATOM_TRAK;
256                 else if (atom_compare(a, b, c, d, 't', 'k', 'h', 'd'))
257                         return ATOM_TKHD;
258                 else if (atom_compare(a, b, c, d, 't', 'r', 'e', 'f'))
259                         return ATOM_TREF;
260                 else if (atom_compare(a, b, c, d, 't', 'r', 'k', 'n'))
261                         return ATOM_TRACK;
262                 else if (atom_compare(a, b, c, d, 't', 'm', 'p', 'o'))
263                         return ATOM_TEMPO;
264                 else if (atom_compare(a, b, c, d, 't', 'v', 'n', 'n'))
265                         return ATOM_NETWORK;
266                 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'h'))
267                         return ATOM_SHOW;
268                 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 'n'))
269                         return ATOM_EPISODENAME;
270                 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'n'))
271                         return ATOM_SEASON;
272                 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 's'))
273                         return ATOM_EPISODE;
274         } else if (a == 's') {
275                 if (atom_compare(a, b, c, d, 's', 't', 'b', 'l'))
276                         return ATOM_STBL;
277                 else if (atom_compare(a, b, c, d, 's', 'm', 'h', 'd'))
278                         return ATOM_SMHD;
279                 else if (atom_compare(a, b, c, d, 's', 't', 's', 'd'))
280                         return ATOM_STSD;
281                 else if (atom_compare(a, b, c, d, 's', 't', 't', 's'))
282                         return ATOM_STTS;
283                 else if (atom_compare(a, b, c, d, 's', 't', 'c', 'o'))
284                         return ATOM_STCO;
285                 else if (atom_compare(a, b, c, d, 's', 't', 's', 'c'))
286                         return ATOM_STSC;
287                 else if (atom_compare(a, b, c, d, 's', 't', 's', 'z'))
288                         return ATOM_STSZ;
289                 else if (atom_compare(a, b, c, d, 's', 't', 'z', '2'))
290                         return ATOM_STZ2;
291                 else if (atom_compare(a, b, c, d, 's', 'k', 'i', 'p'))
292                         return ATOM_SKIP;
293                 else if (atom_compare(a, b, c, d, 's', 'i', 'n', 'f'))
294                         return ATOM_SINF;
295                 else if (atom_compare(a, b, c, d, 's', 'c', 'h', 'i'))
296                         return ATOM_SCHI;
297                 else if (atom_compare(a, b, c, d, 's', 'o', 'n', 'm'))
298                         return ATOM_SORTTITLE;
299                 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'l'))
300                         return ATOM_SORTALBUM;
301                 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'r'))
302                         return ATOM_SORTARTIST;
303                 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'a'))
304                         return ATOM_SORTALBUMARTIST;
305                 else if (atom_compare(a, b, c, d, 's', 'o', 'c', 'o'))
306                         return ATOM_SORTWRITER;
307                 else if (atom_compare(a, b, c, d, 's', 'o', 's', 'n'))
308                         return ATOM_SORTSHOW;
309         } else if (a == COPYRIGHT_SYMBOL) {
310                 if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'n', 'a', 'm'))
311                         return ATOM_TITLE;
312                 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'A', 'R', 'T'))
313                         return ATOM_ARTIST;
314                 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'w', 'r', 't'))
315                         return ATOM_WRITER;
316                 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'a', 'l', 'b'))
317                         return ATOM_ALBUM;
318                 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'd', 'a', 'y'))
319                         return ATOM_DATE;
320                 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 't', 'o', 'o'))
321                         return ATOM_TOOL;
322                 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'c', 'm', 't'))
323                         return ATOM_COMMENT;
324                 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'e', 'n'))
325                         return ATOM_GENRE1;
326                 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'r', 'p'))
327                         return ATOM_CONTENTGROUP;
328                 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'l', 'y', 'r'))
329                         return ATOM_LYRICS;
330         }
331
332         if (atom_compare(a, b, c, d, 'e', 'd', 't', 's'))
333                 return ATOM_EDTS;
334         else if (atom_compare(a, b, c, d, 'e', 's', 'd', 's'))
335                 return ATOM_ESDS;
336         else if (atom_compare(a, b, c, d, 'f', 't', 'y', 'p'))
337                 return ATOM_FTYP;
338         else if (atom_compare(a, b, c, d, 'f', 'r', 'e', 'e'))
339                 return ATOM_FREE;
340         else if (atom_compare(a, b, c, d, 'h', 'm', 'h', 'd'))
341                 return ATOM_HMHD;
342         else if (atom_compare(a, b, c, d, 'v', 'm', 'h', 'd'))
343                 return ATOM_VMHD;
344         else if (atom_compare(a, b, c, d, 'u', 'd', 't', 'a'))
345                 return ATOM_UDTA;
346         else if (atom_compare(a, b, c, d, 'i', 'l', 's', 't'))
347                 return ATOM_ILST;
348         else if (atom_compare(a, b, c, d, 'n', 'a', 'm', 'e'))
349                 return ATOM_NAME;
350         else if (atom_compare(a, b, c, d, 'd', 'a', 't', 'a'))
351                 return ATOM_DATA;
352         else if (atom_compare(a, b, c, d, 'd', 'i', 's', 'k'))
353                 return ATOM_DISC;
354         else if (atom_compare(a, b, c, d, 'g', 'n', 'r', 'e'))
355                 return ATOM_GENRE2;
356         else if (atom_compare(a, b, c, d, 'c', 'o', 'v', 'r'))
357                 return ATOM_COVER;
358         else if (atom_compare(a, b, c, d, 'c', 'p', 'i', 'l'))
359                 return ATOM_COMPILATION;
360         else if (atom_compare(a, b, c, d, 'c', 't', 't', 's'))
361                 return ATOM_CTTS;
362         else if (atom_compare(a, b, c, d, 'd', 'r', 'm', 's'))
363                 return ATOM_DRMS;
364         else if (atom_compare(a, b, c, d, 'f', 'r', 'm', 'a'))
365                 return ATOM_FRMA;
366         else if (atom_compare(a, b, c, d, 'p', 'r', 'i', 'v'))
367                 return ATOM_PRIV;
368         else if (atom_compare(a, b, c, d, 'i', 'v', 'i', 'v'))
369                 return ATOM_IVIV;
370         else if (atom_compare(a, b, c, d, 'u', 's', 'e', 'r'))
371                 return ATOM_USER;
372         else if (atom_compare(a, b, c, d, 'k', 'e', 'y', ' '))
373                 return ATOM_KEY;
374         else if (atom_compare(a, b, c, d, 'a', 'A', 'R', 'T'))
375                 return ATOM_ALBUM_ARTIST;
376         else if (atom_compare(a, b, c, d, 'd', 'e', 's', 'c'))
377                 return ATOM_DESCRIPTION;
378         else if (atom_compare(a, b, c, d, 'p', 'c', 's', 't'))
379                 return ATOM_PODCAST;
380         else
381                 return ATOM_UNKNOWN;
382 }
383
384 /* read atom header, atom size is returned with header included. */
385 static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
386                 uint8_t *header_size, uint64_t *atom_size)
387 {
388         uint32_t size;
389         int ret;
390         int8_t atom_header[8];
391
392         ret = read_data(f, atom_header, 8);
393         if (ret <= 0)
394                 return ret;
395         size = read_u32_be(atom_header);
396         if (size == 1) { /* 64 bit atom size */
397                 if (header_size)
398                         *header_size = 16;
399                 ret = read_int64(f, atom_size);
400                 if (ret <= 0)
401                         return ret;
402         } else {
403                 if (header_size)
404                         *header_size = 8;
405                 if (atom_size)
406                         *atom_size = size;
407         }
408         *atom_type = atom_name_to_type(atom_header[4], atom_header[5],
409                 atom_header[6], atom_header[7]);
410         return 1;
411 }
412
413 static int64_t get_position(const struct mp4 *f)
414 {
415         return f->current_position;
416 }
417
418 static int32_t set_position(struct mp4 *f, int64_t position)
419 {
420         f->cb->seek(f->cb->user_data, position);
421         f->current_position = position;
422
423         return 0;
424 }
425
426 static int read_stsz(struct mp4 *f)
427 {
428         int ret;
429         int32_t i;
430         struct mp4_track *t;
431
432         if (f->total_tracks == 0)
433                 return -1;
434         t = f->track[f->total_tracks - 1];
435         ret = read_int8(f, NULL); /* version */
436         if (ret <= 0)
437                 return ret;
438         ret = read_int24(f, NULL); /* flags */
439         if (ret <= 0)
440                 return ret;
441         ret = read_int32(f, &t->stsz_sample_size);
442         if (ret <= 0)
443                 return ret;
444         ret = read_int32(f, &t->stsz_sample_count);
445         if (ret <= 0)
446                 return ret;
447         if (t->stsz_sample_size != 0)
448                 return 1;
449         t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
450         for (i = 0; i < t->stsz_sample_count; i++) {
451                 ret = read_int32(f, &t->stsz_table[i]);
452                 if (ret <= 0)
453                         return ret;
454         }
455         return 1;
456 }
457
458 static int read_stts(struct mp4 *f)
459 {
460         int ret;
461         int32_t i;
462         struct mp4_track *t;
463
464         if (f->total_tracks == 0)
465                 return -1;
466         t = f->track[f->total_tracks - 1];
467         if (t->stts_entry_count)
468                 return 0;
469         ret = read_int8(f, NULL); /* version */
470         if (ret <= 0)
471                 return ret;
472         ret = read_int24(f, NULL); /* flags */
473         if (ret <= 0)
474                 return ret;
475         ret = read_int32(f, &t->stts_entry_count);
476         if (ret <= 0)
477                 return ret;
478         t->stts_sample_count = para_malloc(t->stts_entry_count
479                 * sizeof(int32_t));
480         for (i = 0; i < t->stts_entry_count; i++) {
481                 ret = read_int32(f, &t->stts_sample_count[i]);
482                 if (ret <= 0)
483                         return ret;
484                 ret = read_int32(f, NULL); /* sample delta */
485                 if (ret <= 0)
486                         return ret;
487         }
488         return 1;
489 }
490
491 static int read_stsc(struct mp4 *f)
492 {
493         int ret;
494         int32_t i;
495         struct mp4_track *t;
496
497         if (f->total_tracks == 0)
498                 return -1;
499         t = f->track[f->total_tracks - 1];
500
501         ret = read_int8(f, NULL); /* version */
502         if (ret <= 0)
503                 return ret;
504         ret = read_int24(f, NULL); /* flags */
505         if (ret <= 0)
506                 return ret;
507         ret = read_int32(f, &t->stsc_entry_count);
508         if (ret <= 0)
509                 return ret;
510         t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
511         t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
512                 * sizeof (int32_t));
513         for (i = 0; i < t->stsc_entry_count; i++) {
514                 ret = read_int32(f, &t->stsc_first_chunk[i]);
515                 if (ret <= 0)
516                         return ret;
517                 ret = read_int32(f, &t->stsc_samples_per_chunk[i]);
518                 if (ret <= 0)
519                         return ret;
520                 ret = read_int32(f, NULL); /* sample desc index */
521                 if (ret <= 0)
522                         return ret;
523         }
524         return 1;
525 }
526
527 static int read_stco(struct mp4 *f)
528 {
529         int ret;
530         int32_t i;
531         struct mp4_track *t;
532
533         if (f->total_tracks == 0)
534                 return -1;
535         t = f->track[f->total_tracks - 1];
536
537         ret = read_int8(f, NULL); /* version */
538         if (ret <= 0)
539                 return ret;
540         ret = read_int24(f, NULL); /* flags */
541         if (ret <= 0)
542                 return ret;
543         ret = read_int32(f, &t->stco_entry_count);
544         if (ret <= 0)
545                 return ret;
546         t->stco_chunk_offset = para_malloc(t->stco_entry_count
547                 * sizeof(int32_t));
548         for (i = 0; i < t->stco_entry_count; i++) {
549                 ret = read_int32(f, &t->stco_chunk_offset[i]);
550                 if (ret <= 0)
551                         return ret;
552         }
553         return 1;
554 }
555
556 static int read_mp4a(struct mp4 *f)
557 {
558         int ret;
559         int32_t i;
560         struct mp4_track *t;
561
562         if (f->total_tracks == 0)
563                 return -1;
564         t = f->track[f->total_tracks - 1];
565
566         for (i = 0; i < 6; i++) {
567                 ret = read_int8(f, NULL); /* reserved */
568                 if (ret <= 0)
569                         return ret;
570         }
571         ret = read_int16(f, NULL); /* data_reference_index */
572         if (ret <= 0)
573                 return ret;
574         ret = read_int32(f, NULL); /* reserved */
575         if (ret <= 0)
576                 return ret;
577         ret = read_int32(f, NULL); /* reserved */
578         if (ret <= 0)
579                 return ret;
580         ret = read_int16(f, &t->channelCount);
581         if (ret <= 0)
582                 return ret;
583         ret = read_int16(f, NULL);
584         if (ret <= 0)
585                 return ret;
586         ret = read_int16(f, NULL);
587         if (ret <= 0)
588                 return ret;
589         ret = read_int16(f, NULL);
590         if (ret <= 0)
591                 return ret;
592         return read_int16(f, &t->sampleRate);
593 }
594
595 static int read_stsd(struct mp4 *f)
596 {
597         int ret;
598         uint32_t i, entry_count;
599         struct mp4_track *t;
600
601         if (f->total_tracks == 0)
602                 return -1;
603         t = f->track[f->total_tracks - 1];
604         ret = read_int8(f, NULL); /* version */
605         if (ret <= 0)
606                 return ret;
607         ret = read_int24(f, NULL); /* flags */
608         if (ret <= 0)
609                 return ret;
610         ret = read_int32(f, &entry_count);
611         if (ret <= 0)
612                 return ret;
613         for (i = 0; i < entry_count; i++) {
614                 uint64_t skip = get_position(f);
615                 uint64_t size;
616                 uint8_t atom_type = 0;
617                 ret = atom_read_header(f, &atom_type, NULL, &size);
618                 if (ret <= 0)
619                         return ret;
620                 skip += size;
621                 if (!f->audio_track && atom_type == ATOM_MP4A) {
622                         f->audio_track = t;
623                         read_mp4a(f);
624                 }
625                 set_position(f, skip);
626         }
627         return 1;
628 }
629
630 static const char *get_metadata_name(uint8_t atom_type)
631 {
632         switch (atom_type) {
633         case ATOM_TITLE: return "title";
634         case ATOM_ARTIST: return "artist";
635         case ATOM_ALBUM: return "album";
636         case ATOM_DATE: return "date";
637         case ATOM_COMMENT: return "comment";
638         default: return "unknown";
639         }
640 }
641
642 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
643 {
644         int ret;
645         uint64_t subsize, sumsize;
646         char *value = NULL;
647         uint32_t len = 0;
648         uint64_t destpos;
649         struct mp4_tag *tag;
650
651         for (
652                 sumsize = 0;
653                 sumsize < size;
654                 set_position(f, destpos), sumsize += subsize
655         ) {
656                 uint8_t atom_type;
657                 uint8_t header_size = 0;
658                 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
659                 if (ret <= 0)
660                         goto fail;
661                 destpos = get_position(f) + subsize - header_size;
662                 if (atom_type != ATOM_DATA)
663                         continue;
664                 ret = read_int8(f, NULL); /* version */
665                 if (ret <= 0)
666                         goto fail;
667                 ret = read_int24(f, NULL); /* flags */
668                 if (ret <= 0)
669                         goto fail;
670                 ret = read_int32(f, NULL); /* reserved */
671                 if (ret <= 0)
672                         goto fail;
673                 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
674                 if (subsize < header_size + 8 || subsize > UINT_MAX)
675                         goto fail;
676                 len = subsize - (header_size + 8);
677                 free(value);
678                 value = para_malloc(len + 1);
679                 ret = read_data(f, value, len);
680                 if (ret <= 0)
681                         goto fail;
682                 value[len] = '\0';
683         }
684         if (!value)
685                 return -ERRNO_TO_PARA_ERROR(EINVAL);
686         f->meta.tags = para_realloc(f->meta.tags, (f->meta.count + 1)
687                 * sizeof(struct mp4_tag));
688         tag = f->meta.tags + f->meta.count;
689         tag->item = para_strdup(get_metadata_name(parent));
690         tag->value = value;
691         tag->len = len;
692         f->meta.count++;
693         return 1;
694 fail:
695         free(value);
696         return ret;
697 }
698
699 static int read_mdhd(struct mp4 *f)
700 {
701         int ret;
702         uint32_t version;
703         struct mp4_track *t;
704
705         if (f->total_tracks == 0)
706                 return -1;
707         t = f->track[f->total_tracks - 1];
708
709         ret = read_int32(f, &version);
710         if (ret <= 0)
711                 return ret;
712         if (version == 1) {
713                 ret = read_int64(f, NULL); /* creation-time */
714                 if (ret <= 0)
715                         return ret;
716                 ret = read_int64(f, NULL); /* modification-time */
717                 if (ret <= 0)
718                         return ret;
719                 ret = read_int32(f, &t->timeScale);
720                 if (ret <= 0)
721                         return ret;
722                 ret = read_int64(f, &t->duration);
723                 if (ret <= 0)
724                         return ret;
725         } else { //version == 0
726                 uint32_t temp;
727
728                 ret = read_int32(f, NULL); /* creation-time */
729                 if (ret <= 0)
730                         return ret;
731                 ret = read_int32(f, NULL); /* modification-time */
732                 if (ret <= 0)
733                         return ret;
734                 ret = read_int32(f, &t->timeScale);
735                 if (ret <= 0)
736                         return ret;
737                 ret = read_int32(f, &temp);
738                 if (ret <= 0)
739                         return ret;
740                 t->duration = (temp == (uint32_t) (-1))?
741                         (uint64_t) (-1) : (uint64_t) (temp);
742         }
743         ret = read_int16(f, NULL);
744         if (ret <= 0)
745                 return ret;
746         ret = read_int16(f, NULL);
747         if (ret <= 0)
748                 return ret;
749         return 1;
750 }
751
752 static int32_t read_ilst(struct mp4 *f, int32_t size)
753 {
754         int ret;
755         uint64_t sumsize = 0;
756
757         while (sumsize < size) {
758                 uint8_t atom_type;
759                 uint64_t subsize, destpos;
760                 uint8_t header_size = 0;
761                 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
762                 if (ret <= 0)
763                         return ret;
764                 destpos = get_position(f) + subsize - header_size;
765                 switch (atom_type) {
766                 case ATOM_ARTIST:
767                 case ATOM_TITLE:
768                 case ATOM_ALBUM:
769                 case ATOM_COMMENT:
770                 case ATOM_DATE:
771                         ret = parse_tag(f, atom_type, subsize - header_size);
772                         if (ret <= 0)
773                                 return ret;
774                 }
775                 set_position(f, destpos);
776                 sumsize += subsize;
777         }
778         return 1;
779 }
780
781 static int32_t read_meta(struct mp4 *f, uint64_t size)
782 {
783         int ret;
784         uint64_t subsize, sumsize = 0;
785         uint8_t atom_type;
786         uint8_t header_size = 0;
787
788         ret = read_int8(f, NULL); /* version */
789         if (ret <= 0)
790                 return ret;
791         ret = read_int24(f, NULL); /* flags */
792         if (ret <= 0)
793                 return ret;
794         while (sumsize < (size - (header_size + 4))) {
795                 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
796                 if (ret <= 0)
797                         return ret;
798                 if (subsize <= header_size + 4)
799                         return 1;
800                 if (atom_type == ATOM_ILST) {
801                         f->ilst_offset = get_position(f) - header_size;
802                         f->ilst_size = subsize;
803                         ret = read_ilst(f, subsize - (header_size + 4));
804                         if (ret <= 0)
805                                 return ret;
806                 } else
807                         set_position(f, get_position(f) + subsize - header_size);
808                 sumsize += subsize;
809         }
810         return 1;
811 }
812
813 static int parse_leaf_atom(struct mp4 *f, uint64_t size, uint8_t header_size,
814                 uint8_t atom_type)
815 {
816         uint64_t dest_position = get_position(f) + size - 8;
817         int ret = 1; /* return success for atoms we don't care about */
818
819         switch (atom_type) {
820         case ATOM_STSZ: ret = read_stsz(f); break;
821         case ATOM_STTS: ret = read_stts(f); break;
822         case ATOM_STSC: ret = read_stsc(f); break;
823         case ATOM_STCO: ret = read_stco(f); break;
824         case ATOM_STSD: ret = read_stsd(f); break;
825         case ATOM_MDHD: ret = read_mdhd(f); break;
826         case ATOM_META:
827                 f->meta_offset = get_position(f) - header_size;
828                 f->meta_size = size;
829                 ret = read_meta(f, size);
830                 break;
831         }
832         set_position(f, dest_position);
833         return ret;
834 }
835
836 static bool need_atom(uint8_t atom_type, bool meta_only)
837 {
838         /* these are needed in any case */
839         switch (atom_type) {
840         case ATOM_STSD:
841         case ATOM_META:
842         case ATOM_TRAK:
843         case ATOM_MDIA:
844         case ATOM_MINF:
845         case ATOM_STBL:
846         case ATOM_UDTA:
847                 return true;
848         }
849         /* meta-only opens don't need anything else */
850         if (meta_only)
851                 return false;
852         /* these are only required for regular opens */
853         switch (atom_type) {
854         case ATOM_STTS:
855         case ATOM_STSZ:
856         case ATOM_STCO:
857         case ATOM_STSC:
858         case ATOM_MDHD:
859                 return true;
860         }
861         return false;
862 }
863
864 /* parse atoms that are sub atoms of other atoms */
865 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
866 {
867         int ret;
868         uint64_t size;
869         uint8_t atom_type = 0;
870         uint64_t counted_size = 0;
871         uint8_t header_size = 0;
872
873         while (counted_size < total_size) {
874                 ret = atom_read_header(f, &atom_type, &header_size, &size);
875                 if (ret <= 0)
876                         return ret;
877                 if (size == 0)
878                         return -1;
879                 counted_size += size;
880                 if (atom_type == ATOM_TRAK) {
881                         if (f->total_tracks >= MAX_TRACKS)
882                                 return -1;
883                         f->total_tracks++;
884                         f->track[f->total_tracks - 1] = para_calloc(
885                                 sizeof(struct mp4_track));
886                 } else if (atom_type == ATOM_UDTA) {
887                         f->udta_offset = get_position(f) - header_size;
888                         f->udta_size = size;
889                 }
890                 if (!need_atom(atom_type, meta_only)) {
891                         set_position(f, get_position(f) + size - header_size);
892                         continue;
893                 }
894                 if (atom_type < SUBATOMIC) /* atom contains subatoms */
895                         ret = parse_sub_atoms(f, size - header_size, meta_only);
896                 else
897                         ret = parse_leaf_atom(f, size, header_size, atom_type);
898                 if (ret <= 0)
899                         return ret;
900         }
901         return 1;
902 }
903
904 static int open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 **result)
905 {
906         int ret;
907         uint64_t size;
908         uint8_t atom_type, header_size;
909         struct mp4 *f = para_calloc(sizeof(*f));
910
911         f->cb = cb;
912         while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
913                 f->file_size += size;
914                 f->last_atom = atom_type;
915                 if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
916                         set_position(f, get_position(f) + size - header_size);
917                         continue;
918                 }
919                 f->moov_offset = get_position(f) - header_size;
920                 f->moov_size = size;
921                 ret = parse_sub_atoms(f, size - header_size, meta_only);
922                 if (ret <= 0)
923                         break;
924         }
925         if (ret < 0) {
926                 ret = -E_MP4_OPEN;
927                 goto fail;
928         }
929         ret = -E_MP4_TRACK;
930         if (!f->audio_track)
931                 goto fail;
932         *result = f;
933         return 1;
934 fail:
935         *result = NULL;
936         free(f);
937         return ret;
938 }
939
940 int mp4_open_read(const struct mp4_callback *cb, struct mp4 **result)
941 {
942         return open_file(cb, false, result);
943 }
944
945 void mp4_close(struct mp4 *f)
946 {
947         int32_t i;
948
949         for (i = 0; i < f->total_tracks; i++) {
950                 if (f->track[i]) {
951                         free(f->track[i]->stsz_table);
952                         free(f->track[i]->stts_sample_count);
953                         free(f->track[i]->stsc_first_chunk);
954                         free(f->track[i]->stsc_samples_per_chunk);
955                         free(f->track[i]->stco_chunk_offset);
956                         free(f->track[i]);
957                 }
958         }
959         for (i = 0; i < f->meta.count; i++) {
960                 free(f->meta.tags[i].item);
961                 free(f->meta.tags[i].value);
962         }
963         free(f->meta.tags);
964         free(f);
965 }
966
967 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
968                 int32_t *chunk)
969 {
970         const struct mp4_track *t = f->audio_track;
971         uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
972         int32_t chunk1, chunk1samples, n, total, i;
973
974         for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) {
975                 n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */
976                 if (sample < total + n)
977                         break;
978         }
979         chunk1 = fc[i - 1];
980         chunk1samples = spc[i - 1];
981         if (chunk1samples != 0)
982                 *chunk = (sample - total) / chunk1samples + chunk1;
983         else
984                 *chunk = 1;
985         return total + (*chunk - chunk1) * chunk1samples;
986 }
987
988 /**
989  * Return the number of milliseconds of the audio track.
990  *
991  * \param f As returned by \ref mp4_open_read(), must not be NULL.
992  */
993 uint64_t mp4_get_duration(const struct mp4 *f)
994 {
995         const struct mp4_track *t = f->audio_track;
996
997         if (t->timeScale == 0)
998                 return 0;
999         return t->duration * 1000 / t->timeScale;
1000 }
1001
1002 int mp4_set_sample_position(struct mp4 *f, int32_t sample)
1003 {
1004         const struct mp4_track *t = f->audio_track;
1005         int32_t offset, chunk, chunk_sample;
1006         uint32_t n, srs; /* sample range size */
1007
1008         if (sample >= t->stsz_sample_count)
1009                 return -ERRNO_TO_PARA_ERROR(EINVAL);
1010         chunk_sample = chunk_of_sample(f, sample, &chunk);
1011         if (t->stsz_sample_size > 0)
1012                 srs = (sample - chunk_sample) * t->stsz_sample_size;
1013         else {
1014                 for (srs = 0, n = chunk_sample; n < sample; n++)
1015                         srs += t->stsz_table[n];
1016         }
1017         if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
1018                 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
1019         else if (t->stco_entry_count > 0)
1020                 offset = t->stco_chunk_offset[chunk - 1];
1021         else
1022                 offset = 8;
1023         set_position(f, offset + srs);
1024         return 1;
1025 }
1026
1027 int32_t mp4_get_sample_size(const struct mp4 *f, int sample)
1028 {
1029         const struct mp4_track *t = f->audio_track;
1030
1031         if (t->stsz_sample_size != 0)
1032                 return t->stsz_sample_size;
1033         return t->stsz_table[sample];
1034 }
1035
1036 uint32_t mp4_get_sample_rate(const struct mp4 *f)
1037 {
1038         return f->audio_track->sampleRate;
1039 }
1040
1041 uint32_t mp4_get_channel_count(const struct mp4 *f)
1042 {
1043         return f->audio_track->channelCount ;
1044 }
1045
1046 int32_t mp4_num_samples(const struct mp4 *f)
1047 {
1048         const struct mp4_track *t = f->audio_track;
1049         int32_t i;
1050         int32_t total = 0;
1051
1052         for (i = 0; i < t->stts_entry_count; i++)
1053                 total += t->stts_sample_count[i];
1054         return total;
1055 }
1056
1057 int mp4_open_meta(const struct mp4_callback *cb, struct mp4 **result)
1058 {
1059         return open_file(cb, true, result);
1060 }
1061
1062 /**
1063  * Return the metadata of an mp4 file.
1064  *
1065  * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1066  *
1067  * The caller is allowed to add, delete or modify the entries of the returned
1068  * structure in order to pass the modified version to \ref mp4_meta_update().
1069  */
1070 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1071 {
1072         return &f->meta;
1073 }
1074
1075 struct membuffer {
1076         void *data;
1077         unsigned written;
1078         unsigned allocated;
1079 };
1080
1081 static struct membuffer *membuffer_create(void)
1082 {
1083         struct membuffer *buf = para_calloc(sizeof(*buf));
1084
1085         buf->allocated = 256;
1086         buf->data = para_malloc(buf->allocated);
1087         return buf;
1088 }
1089
1090 static void membuffer_write(struct membuffer *buf, const void *ptr,
1091                 unsigned bytes)
1092 {
1093         unsigned dest_size = buf->written + bytes;
1094
1095         if (dest_size > buf->allocated) {
1096                 do {
1097                         buf->allocated <<= 1;
1098                 } while (dest_size > buf->allocated);
1099                 buf->data = para_realloc(buf->data, buf->allocated);
1100         }
1101
1102         if (ptr)
1103                 memcpy((char *) buf->data + buf->written, ptr, bytes);
1104         buf->written += bytes;
1105 }
1106
1107 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1108 {
1109         membuffer_write(buf, data, 4);
1110 }
1111
1112 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1113 {
1114         uint8_t temp[4];
1115         write_u32_be(temp, data);
1116         membuffer_write(buf, temp, 4);
1117 }
1118
1119 static unsigned membuffer_get_size(const struct membuffer *buf)
1120 {
1121         return buf->written;
1122 }
1123
1124 static void *membuffer_detach(struct membuffer *buf)
1125 {
1126         void *ret = para_realloc(buf->data, buf->written);
1127         free(buf);
1128         return ret;
1129 }
1130
1131 static void *create_ilst(const struct mp4_metadata *meta, uint32_t *out_size)
1132 {
1133         struct membuffer *buf = membuffer_create();
1134         unsigned n;
1135
1136         for (n = 0; n < meta->count; n++) {
1137                 struct mp4_tag *tag = meta->tags + n;
1138                 unsigned len = strlen(tag->value);
1139                 const char *atom_name;
1140
1141                 if (!strcasecmp(tag->item, "title"))
1142                         atom_name = "\xA9" "nam";
1143                 else if (!strcasecmp(tag->item, "artist"))
1144                         atom_name = "\xA9" "ART";
1145                 else if (!strcasecmp(tag->item, "album"))
1146                         atom_name = "\xA9" "alb";
1147                 else if (!strcasecmp(tag->item, "date"))
1148                         atom_name = "\xA9" "day";
1149                 else if (!strcasecmp(tag->item, "comment"))
1150                         atom_name = "\xA9" "cmt";
1151                 else
1152                         assert(false);
1153                 membuffer_write_int32(buf, 8 /* atom header */
1154                         + 8 /* data atom header */
1155                         + 8 /* flags + reserved */
1156                         + len);
1157                 membuffer_write_atom_name(buf, atom_name);
1158                 membuffer_write_int32(buf, 8 /* data atom header */
1159                         + 8 /* flags + reserved */
1160                         + len);
1161                 membuffer_write_atom_name(buf, "data");
1162                 membuffer_write_int32(buf, 1);  /* flags */
1163                 membuffer_write_int32(buf, 0);  /* reserved */
1164                 membuffer_write(buf, tag->value, len);
1165         }
1166         *out_size = membuffer_get_size(buf);
1167         return membuffer_detach(buf);
1168 }
1169
1170 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1171                           const void *data)
1172 {
1173         membuffer_write_int32(buf, size + 8);
1174         membuffer_write_atom_name(buf, name);
1175         membuffer_write(buf, data, size);
1176 }
1177
1178 static void *membuffer_get_ptr(const struct membuffer *buf)
1179 {
1180         return buf->data;
1181 }
1182
1183 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1184                 unsigned bytes)
1185 {
1186         unsigned oldsize = membuffer_get_size(buf);
1187         char *bufptr;
1188
1189         membuffer_write(buf, 0, bytes);
1190         bufptr = membuffer_get_ptr(buf);
1191         if (read_data(src, bufptr + oldsize, bytes) != 1) {
1192                 free(buf->data);
1193                 free(buf);
1194                 return false;
1195         }
1196         return true;
1197 }
1198
1199 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1200                 uint32_t * out_size)
1201 {
1202         struct membuffer *buf;
1203         uint32_t ilst_size;
1204         void *ilst_buffer;
1205
1206         ilst_buffer = create_ilst(meta, &ilst_size);
1207
1208         buf = membuffer_create();
1209         membuffer_write_int32(buf, 0);
1210         membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1211         free(ilst_buffer);
1212
1213         *out_size = membuffer_get_size(buf);
1214         *out_buffer = membuffer_detach(buf);
1215         return 1;
1216 }
1217
1218 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1219 uint32_t * out_size)
1220 {
1221         struct membuffer *buf;
1222         uint32_t meta_size;
1223         void *meta_buffer;
1224
1225         if (!create_meta(meta, &meta_buffer, &meta_size))
1226                 return 0;
1227
1228         buf = membuffer_create();
1229
1230         membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1231
1232         free(meta_buffer);
1233
1234         *out_size = membuffer_get_size(buf);
1235         *out_buffer = membuffer_detach(buf);
1236         return 1;
1237 }
1238
1239 static uint32_t fix_byte_order_32(uint32_t src)
1240 {
1241         return read_u32_be(&src);
1242 }
1243
1244 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1245 {
1246         int ret;
1247         uint64_t total_base = f->moov_offset + 8;
1248         uint32_t total_size = (uint32_t) (f->moov_size - 8);
1249         uint32_t new_ilst_size;
1250         void *new_ilst_buffer, *out_buffer;
1251         uint8_t *p_out;
1252         int32_t size_delta;
1253         uint32_t tmp;
1254
1255         if (f->udta_size == 0) {
1256                 struct membuffer *buf;
1257                 void *new_udta_buffer;
1258                 uint32_t new_udta_size;
1259                 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1260                         return NULL;
1261
1262                 buf = membuffer_create();
1263                 set_position(f, total_base);
1264                 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1265                         free(new_udta_buffer);
1266                         return NULL;
1267                 }
1268                 membuffer_write_atom(buf, "udta", new_udta_size,
1269                         new_udta_buffer);
1270
1271                 free(new_udta_buffer);
1272
1273                 *out_size = membuffer_get_size(buf);
1274                 return membuffer_detach(buf);
1275         }
1276         if (f->meta_size == 0 || f->ilst_size == 0) {
1277                 struct membuffer *buf;
1278                 void *new_meta_buffer;
1279                 uint32_t new_meta_size;
1280
1281                 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1282                         return NULL;
1283
1284                 buf = membuffer_create();
1285                 set_position(f, total_base);
1286                 if (!membuffer_transfer_from_file(buf, f,
1287                                 f->udta_offset - total_base)) {
1288                         free(new_meta_buffer);
1289                         return NULL;
1290                 }
1291
1292                 membuffer_write_int32(buf, f->udta_size + 8 + new_meta_size);
1293                 membuffer_write_atom_name(buf, "udta");
1294                 if (!membuffer_transfer_from_file(buf, f, f->udta_size)) {
1295                         free(new_meta_buffer);
1296                         return NULL;
1297                 }
1298                 membuffer_write_atom(buf, "meta", new_meta_size,
1299                         new_meta_buffer);
1300                 free(new_meta_buffer);
1301
1302                 *out_size = membuffer_get_size(buf);
1303                 return membuffer_detach(buf);
1304         }
1305         new_ilst_buffer = create_ilst(&f->meta, &new_ilst_size);
1306         size_delta = new_ilst_size - (f->ilst_size - 8);
1307         *out_size = total_size + size_delta;
1308         out_buffer = para_malloc(*out_size);
1309         p_out = out_buffer;
1310         set_position(f, total_base);
1311         ret = read_data(f, p_out, f->udta_offset - total_base);
1312         if (ret <= 0)
1313                 return NULL;
1314         p_out += f->udta_offset - total_base;
1315         ret = read_int32(f, &tmp);
1316         if (ret <= 0)
1317                 return NULL;
1318         *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1319         p_out += 4;
1320         ret = read_data(f, p_out, 4);
1321         if (ret <= 0)
1322                 return NULL;
1323         p_out += 4;
1324         ret = read_data(f, p_out, f->meta_offset - f->udta_offset - 8);
1325         if (ret <= 0)
1326                 return NULL;
1327         p_out += f->meta_offset - f->udta_offset - 8;
1328         ret = read_int32(f, &tmp);
1329         if (ret <= 0)
1330                 return NULL;
1331         *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1332         p_out += 4;
1333         ret = read_data(f, p_out, 4);
1334         if (ret <= 0)
1335                 return NULL;
1336         p_out += 4;
1337         ret = read_data(f, p_out, f->ilst_offset - f->meta_offset - 8);
1338         if (ret <= 0)
1339                 return NULL;
1340         p_out += f->ilst_offset - f->meta_offset - 8;
1341         ret = read_int32(f, &tmp);
1342         if (ret <= 0)
1343                 return NULL;
1344         *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1345         p_out += 4;
1346         ret = read_data(f, p_out, 4);
1347         if (ret <= 0)
1348                 return NULL;
1349         p_out += 4;
1350         memcpy(p_out, new_ilst_buffer, new_ilst_size);
1351         p_out += new_ilst_size;
1352         set_position(f, f->ilst_offset + f->ilst_size);
1353         ret = read_data(f, p_out, total_size - (f->ilst_offset - total_base)
1354                 - f->ilst_size);
1355         if (ret <= 0)
1356                 return NULL;
1357         free(new_ilst_buffer);
1358         return out_buffer;
1359 }
1360
1361 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1362 {
1363         int32_t result = 1;
1364
1365         result = f->cb->write(f->cb->user_data, data, size);
1366
1367         f->current_position += size;
1368
1369         return result;
1370 }
1371
1372 static int32_t write_int32(struct mp4 *f, uint32_t data)
1373 {
1374         int8_t temp[4];
1375         write_u32_be(temp, data);
1376         return write_data(f, temp, sizeof(temp));
1377 }
1378
1379 int32_t mp4_meta_update(struct mp4 *f)
1380 {
1381         void *new_moov_data;
1382         uint32_t new_moov_size;
1383
1384         set_position(f, 0);
1385         new_moov_data = modify_moov(f, &new_moov_size);
1386         if (!new_moov_data ) {
1387                 mp4_close(f);
1388                 return 0;
1389         }
1390         /* copy moov atom to end of the file */
1391         if (f->last_atom != ATOM_MOOV) {
1392                 char *free_data = "free";
1393
1394                 /* rename old moov to free */
1395                 set_position(f, f->moov_offset + 4);
1396                 write_data(f, free_data, 4);
1397
1398                 set_position(f, f->file_size);
1399                 write_int32(f, new_moov_size + 8);
1400                 write_data(f, "moov", 4);
1401                 write_data(f, new_moov_data, new_moov_size);
1402         } else {
1403                 set_position(f, f->moov_offset);
1404                 write_int32(f, new_moov_size + 8);
1405                 write_data(f, "moov", 4);
1406                 write_data(f, new_moov_data, new_moov_size);
1407         }
1408         free(new_moov_data);
1409         f->cb->truncate(f->cb->user_data);
1410         return 1;
1411 }
1412
1413 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1414 {
1415         uint32_t i;
1416
1417         for (i = 0; i < f->meta.count; i++)
1418                 if (!strcasecmp(f->meta.tags[i].item, item))
1419                         return para_strdup(f->meta.tags[i].value);
1420         return NULL;
1421 }
1422
1423 /**
1424  * Return the value of the artist meta tag of an mp4 file.
1425  *
1426  * \param f Must not be NULL.
1427  *
1428  * \return If the file does not contain this metadata tag, the function returns
1429  * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1430  * this memory when it is no longer needed.
1431  */
1432 char *mp4_meta_get_artist(const struct mp4 *f)
1433 {
1434         return meta_find_by_name(f, "artist");
1435 }
1436
1437 /**
1438  * Return the value of the title meta tag of an mp4 file.
1439  *
1440  * \param f See \ref mp4_meta_get_artist().
1441  * \return See \ref mp4_meta_get_artist().
1442  */
1443 char *mp4_meta_get_title(const struct mp4 *f)
1444 {
1445         return meta_find_by_name(f, "title");
1446 }
1447
1448 /**
1449  * Return the value of the date meta tag of an mp4 file.
1450  *
1451  * \param f See \ref mp4_meta_get_artist().
1452  * \return See \ref mp4_meta_get_artist().
1453  */
1454 char *mp4_meta_get_date(const struct mp4 *f)
1455 {
1456         return meta_find_by_name(f, "date");
1457 }
1458
1459 /**
1460  * Return the value of the album meta tag of an mp4 file.
1461  *
1462  * \param f See \ref mp4_meta_get_artist().
1463  * \return See \ref mp4_meta_get_artist().
1464  */
1465 char *mp4_meta_get_album(const struct mp4 *f)
1466 {
1467         return meta_find_by_name(f, "album");
1468 }
1469
1470 /**
1471  * Return the value of the comment meta tag of an mp4 file.
1472  *
1473  * \param f See \ref mp4_meta_get_artist().
1474  * \return See \ref mp4_meta_get_artist().
1475  */
1476 char *mp4_meta_get_comment(const struct mp4 *f)
1477 {
1478         return meta_find_by_name(f, "comment");
1479 }