2 * Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
3 * FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
8 /** \file mp4.c Paraslash's internal mp4 parser. */
11 * This is a stripped down version of the former mp4ff library which used to be
12 * part of the faad decoder project but was removed from the faad code base in
13 * 2017. The original code has been cleaned up substantially and the public API
14 * has been documented. See the git commit log for details.
21 #include "portable_io.h"
26 * The three states of the mp4 parser. The parser only loads the audio specific
27 * values and tables when it is in the second state.
29 enum audio_track_state {
30 /** We haven't encountered an mp4a atom so far. */
32 /** We have seen an mp4a atom but no subsequent trak atom yet. */
34 /** A trak atom was seen *after* the mp4a atom. */
39 /* determines which atoms we still need to parse. */
40 enum audio_track_state state;
43 uint16_t channel_count;
47 uint32_t stsz_sample_size;
48 uint32_t stsz_sample_count;
52 uint32_t stts_entry_count;
53 uint32_t *stts_sample_count;
56 uint32_t stsc_entry_count;
57 uint32_t *stsc_first_chunk;
58 uint32_t *stsc_samples_per_chunk;
61 uint32_t stco_entry_count;
62 uint32_t *stco_chunk_offset;
70 const struct mp4_callback *cb;
82 struct mp4_track track;
83 struct mp4_metadata meta;
87 * Returns -E_MP4_READ, 0, or 1 on errors/EOF/success. Partial reads followed
88 * by EOF or read errors are treated as errors.
90 static int read_data(struct mp4 *f, void *data, size_t size)
93 ssize_t ret = f->cb->read(f->cb->user_data, data, size);
94 if (ret < 0 && errno == EINTR)
96 /* regard EAGAIN as an error as reads should be blocking. */
98 return ret < 0? -E_MP4_READ : 0;
104 static int read_int64(struct mp4 *f, uint64_t *result)
107 int ret = read_data(f, data, 8);
110 *result = read_u64_be(data);
114 static int read_int32(struct mp4 *f, uint32_t *result)
117 int ret = read_data(f, data, 4);
120 *result = read_u32_be(data);
124 static int read_int16(struct mp4 *f, uint16_t *result)
127 int ret = read_data(f, data, 2);
130 *result = read_u16_be(data);
134 /** A macro defining the atoms we care about. It gets expanded twice. */
136 ATOM_ITEM(MOOV, 'm', 'o', 'o', 'v') /* movie (top-level container) */ \
137 ATOM_ITEM(TRAK, 't', 'r', 'a', 'k') /* container for a single track */ \
138 ATOM_ITEM(MDIA, 'm', 'd', 'i', 'a') /* media information */ \
139 ATOM_ITEM(MINF, 'm', 'i', 'n', 'f') /* extends mdia */ \
140 ATOM_ITEM(STBL, 's', 't', 'b', 'l') /* sample table container */ \
141 ATOM_ITEM(UDTA, 'u', 'd', 't', 'a') /* user data */ \
142 ATOM_ITEM(ILST, 'i', 'l', 's', 't') /* iTunes Metadata list */ \
143 ATOM_ITEM(ARTIST, 0xa9, 'A', 'R', 'T') /* artist */ \
144 ATOM_ITEM(TITLE, 0xa9, 'n', 'a', 'm') /* title */ \
145 ATOM_ITEM(ALBUM, 0xa9, 'a', 'l', 'b') /* album */ \
146 ATOM_ITEM(DATE, 0xa9, 'd', 'a', 'y') /* date */ \
147 ATOM_ITEM(COMMENT, 0xa9, 'c', 'm', 't') /* comment */ \
148 ATOM_ITEM(MDHD, 'm', 'd', 'h', 'd') /* track header */ \
149 ATOM_ITEM(STSD, 's', 't', 's', 'd') /* sample description box */ \
150 ATOM_ITEM(STTS, 's', 't', 't', 's') /* time to sample box */ \
151 ATOM_ITEM(STSZ, 's', 't', 's', 'z') /* sample size box */ \
152 ATOM_ITEM(STCO, 's', 't', 'c', 'o') /* chunk offset box */ \
153 ATOM_ITEM(STSC, 's', 't', 's', 'c') /* sample to chunk box */ \
154 ATOM_ITEM(MP4A, 'm', 'p', '4', 'a') /* mp4 audio */ \
155 ATOM_ITEM(META, 'm', 'e', 't', 'a') /* iTunes Metadata box */ \
156 ATOM_ITEM(DATA, 'd', 'a', 't', 'a') /* iTunes Metadata data box */ \
158 /** For the C enumeration we concatenate ATOM_ with the first argument. */
159 #define ATOM_ITEM(_name, a, b, c, d) ATOM_ ## _name,
160 /** The enumeration of interesting atoms. */
161 enum atom {ATOM_ITEMS};
164 /** A cpp version of read_u32_be(). */
165 #define ATOM_VALUE(a, b, c, d) ((a << 24) + (b << 16) + (c << 8) + d)
167 static uint8_t atom_name_to_type(uint8_t *p)
169 /** Expands to an instance of the following unnamed structure. */
170 #define ATOM_ITEM(_name, a, b, c, d) \
171 {.name = # _name, .val = ATOM_VALUE(a, b, c, d)},
172 static const struct {
175 } atom_table[] = {ATOM_ITEMS};
177 uint32_t val = read_u32_be(p);
179 for (uint8_t n = 0; n < ARRAY_SIZE(atom_table); n++)
180 if (val == atom_table[n].val)
185 /* read atom header, atom size is returned with header included. */
186 static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
187 uint8_t *header_size, uint64_t *atom_size)
191 uint8_t atom_header[8];
193 ret = read_data(f, atom_header, 8);
196 size = read_u32_be(atom_header);
197 if (size == 1) { /* 64 bit atom size */
200 ret = read_int64(f, atom_size);
208 *atom_type = atom_name_to_type(atom_header + 4);
212 static off_t get_position(const struct mp4 *f)
214 return f->cb->seek(f->cb->user_data, 0, SEEK_CUR);
217 static void set_position(struct mp4 *f, off_t position)
219 f->cb->seek(f->cb->user_data, position, SEEK_SET);
222 static void skip_bytes(struct mp4 *f, off_t num_skip)
224 f->cb->seek(f->cb->user_data, num_skip, SEEK_CUR);
227 static int read_stsz(struct mp4 *f)
230 struct mp4_track *t = &f->track;
232 if (t->state != ATS_SEEN_MP4A || t->stsz_table)
234 skip_bytes(f, 4); /* version (1), flags (3) */
235 ret = read_int32(f, &t->stsz_sample_size);
238 ret = read_int32(f, &t->stsz_sample_count);
241 if (t->stsz_sample_size != 0)
243 t->stsz_table = arr_alloc(t->stsz_sample_count, sizeof(int32_t));
244 for (uint32_t n = 0; n < t->stsz_sample_count; n++) {
245 ret = read_int32(f, &t->stsz_table[n]);
252 static int read_stts(struct mp4 *f)
255 struct mp4_track *t = &f->track;
257 if (t->state != ATS_SEEN_MP4A || t->stts_sample_count)
259 skip_bytes(f, 4); /* version (1), flags (3) */
260 ret = read_int32(f, &t->stts_entry_count);
263 t->stts_sample_count = arr_alloc(t->stts_entry_count, sizeof(int32_t));
264 for (uint32_t n = 0; n < t->stts_entry_count; n++) {
265 ret = read_int32(f, &t->stts_sample_count[n]);
268 skip_bytes(f, 4); /* sample delta */
273 static int read_stsc(struct mp4 *f)
276 struct mp4_track *t = &f->track;
278 if (t->state != ATS_SEEN_MP4A)
280 if (t->stsc_first_chunk || t->stsc_samples_per_chunk)
282 skip_bytes(f, 4); /* version (1), flags (3) */
283 ret = read_int32(f, &t->stsc_entry_count);
286 t->stsc_first_chunk = arr_alloc(t->stsc_entry_count, sizeof(int32_t));
287 t->stsc_samples_per_chunk = arr_alloc(t->stsc_entry_count,
289 for (uint32_t n = 0; n < t->stsc_entry_count; n++) {
290 ret = read_int32(f, &t->stsc_first_chunk[n]);
293 ret = read_int32(f, &t->stsc_samples_per_chunk[n]);
296 skip_bytes(f, 4); /* sample desc index */
301 static int read_stco(struct mp4 *f)
304 struct mp4_track *t = &f->track;
306 if (t->state != ATS_SEEN_MP4A || t->stco_chunk_offset)
308 skip_bytes(f, 4); /* version (1), flags (3) */
309 ret = read_int32(f, &t->stco_entry_count);
312 t->stco_chunk_offset = arr_alloc(t->stco_entry_count, sizeof(int32_t));
313 for (uint32_t n = 0; n < t->stco_entry_count; n++) {
314 ret = read_int32(f, &t->stco_chunk_offset[n]);
321 static int read_stsd(struct mp4 *f)
324 uint32_t entry_count;
326 if (f->track.state != ATS_INITIAL)
328 skip_bytes(f, 4); /* version (1), flags (3) */
329 ret = read_int32(f, &entry_count);
332 for (uint32_t n = 0; n < entry_count; n++) {
333 uint64_t skip = get_position(f);
335 uint8_t atom_type = 0;
336 ret = atom_read_header(f, &atom_type, NULL, &size);
340 if (atom_type == ATOM_MP4A) {
341 f->track.state = ATS_SEEN_MP4A;
342 /* reserved (6), data reference index (2), reserved (8) */
344 ret = read_int16(f, &f->track.channel_count);
348 ret = read_int16(f, &f->track.sample_rate);
352 set_position(f, skip);
357 static const char *get_metadata_name(uint8_t atom_type)
360 case ATOM_TITLE: return "title";
361 case ATOM_ARTIST: return "artist";
362 case ATOM_ALBUM: return "album";
363 case ATOM_DATE: return "date";
364 case ATOM_COMMENT: return "comment";
365 default: return "unknown";
369 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
372 uint64_t subsize, sumsize;
381 set_position(f, destpos), sumsize += subsize
384 uint8_t header_size = 0;
385 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
388 destpos = get_position(f) + subsize - header_size;
389 if (atom_type != ATOM_DATA)
391 skip_bytes(f, 8); /* version (1), flags (3), reserved (4) */
392 ret = -E_MP4_CORRUPT;
393 if (subsize < header_size + 8 || subsize > UINT_MAX)
395 len = subsize - (header_size + 8);
397 value = alloc(len + 1);
398 ret = read_data(f, value, len);
404 return -E_MP4_CORRUPT;
405 f->meta.tags = para_realloc(f->meta.tags, (f->meta.count + 1)
406 * sizeof(struct mp4_tag));
407 tag = f->meta.tags + f->meta.count;
408 tag->item = para_strdup(get_metadata_name(parent));
417 static int read_mdhd(struct mp4 *f)
421 struct mp4_track *t = &f->track;
423 if (t->state != ATS_INITIAL)
425 ret = read_int32(f, &version);
429 skip_bytes(f, 16); /* creation time (8), modification time (8) */
430 ret = read_int32(f, &t->time_scale);
433 ret = read_int64(f, &t->duration);
436 } else { /* version == 0 */
439 skip_bytes(f, 8); /* creation time (4), modification time (4) */
440 ret = read_int32(f, &t->time_scale);
443 ret = read_int32(f, &temp);
446 t->duration = (temp == (uint32_t) (-1))?
447 (uint64_t) (-1) : (uint64_t) (temp);
453 static int read_ilst(struct mp4 *f, int32_t size)
456 uint64_t sumsize = 0;
458 while (sumsize < size) {
460 uint64_t subsize, destpos;
461 uint8_t header_size = 0;
462 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
465 destpos = get_position(f) + subsize - header_size;
472 ret = parse_tag(f, atom_type, subsize - header_size);
476 set_position(f, destpos);
482 static int read_meta(struct mp4 *f, uint64_t size)
485 uint64_t subsize, sumsize = 0;
487 uint8_t header_size = 0;
489 skip_bytes(f, 4); /* version (1), flags (3) */
490 while (sumsize < (size - (header_size + 4))) {
491 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
494 if (subsize <= header_size + 4)
496 if (atom_type == ATOM_ILST) {
497 f->ilst_offset = get_position(f) - header_size;
498 f->ilst_size = subsize;
499 ret = read_ilst(f, subsize - (header_size + 4));
503 set_position(f, get_position(f) + subsize - header_size);
509 static bool need_atom(uint8_t atom_type, bool meta_only)
511 /* these are needed in any case */
522 /* meta-only opens don't need anything else */
525 /* these are only required for regular opens */
537 /* parse atoms that are sub atoms of other atoms */
538 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
541 uint64_t dest, size, end = get_position(f) + total_size;
543 for (dest = get_position(f); dest < end; set_position(f, dest)) {
544 uint8_t header_size, atom_type;
545 ret = atom_read_header(f, &atom_type, &header_size, &size);
549 return -E_MP4_CORRUPT;
550 dest = get_position(f) + size - header_size;
551 if (atom_type == ATOM_TRAK && f->track.state == ATS_SEEN_MP4A) {
552 f->track.state = ATS_TRACK_CHANGE;
555 if (atom_type == ATOM_UDTA) {
556 f->udta_offset = get_position(f) - header_size;
559 if (!need_atom(atom_type, meta_only))
562 case ATOM_STSZ: ret = read_stsz(f); break;
563 case ATOM_STTS: ret = read_stts(f); break;
564 case ATOM_STSC: ret = read_stsc(f); break;
565 case ATOM_STCO: ret = read_stco(f); break;
566 case ATOM_STSD: ret = read_stsd(f); break;
567 case ATOM_MDHD: ret = read_mdhd(f); break;
569 f->meta_offset = get_position(f) - header_size;
571 ret = read_meta(f, size);
574 ret = parse_sub_atoms(f, size - header_size, meta_only);
583 * Deallocate all resources associated with an mp4 file handle.
585 * \param f File handle returned by \ref mp4_open() or \ref mp4_open_meta().
587 * This frees the metadata items and various tables which were allocated when
588 * the file was opened. The given file handle must not be NULL.
590 void mp4_close(struct mp4 *f)
592 free(f->track.stsz_table);
593 free(f->track.stts_sample_count);
594 free(f->track.stsc_first_chunk);
595 free(f->track.stsc_samples_per_chunk);
596 free(f->track.stco_chunk_offset);
597 for (uint32_t n = 0; n < f->meta.count; n++) {
598 free(f->meta.tags[n].item);
599 free(f->meta.tags[n].value);
605 static int open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 **result)
609 uint8_t atom_type, header_size;
610 struct mp4 *f = zalloc(sizeof(*f));
613 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
614 f->last_atom = atom_type;
615 if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
616 set_position(f, get_position(f) + size - header_size);
619 f->moov_offset = get_position(f) - header_size;
621 ret = parse_sub_atoms(f, size - header_size, meta_only);
628 if (f->track.channel_count == 0)
630 ret = -E_MP4_BAD_SAMPLERATE;
631 if (f->track.sample_rate == 0)
633 ret = -E_MP4_MISSING_ATOM;
634 if (f->udta_size == 0 || f->meta_size == 0 || f->ilst_size == 0)
645 * Read the audio track and the metadata of an mp4 file.
647 * \param cb Only the ->read() and ->seek() methods need to be supplied.
648 * \param result Initialized to a non-NULL pointer iff the function succeeds.
650 * This detects and parses the first audio track and the metadata information
651 * of the mp4 file. Various error checks are performed after the mp4 atoms have
652 * been parsed successfully.
654 * This function does not modify the file. However, if the caller intents to
655 * update the metadata later, the ->write() and ->truncate() methods must be
656 * supplied in the callback structure.
658 * \return Standard. Several errors are possible.
660 * \sa \ref mp4_open_meta().
662 int mp4_open(const struct mp4_callback *cb, struct mp4 **result)
668 ret = open_file(cb, false, &f);
671 ret = -E_MP4_BAD_SAMPLE_COUNT;
672 if (f->track.stsz_sample_count == 0)
674 ret = -E_MP4_CORRUPT;
675 if (f->track.time_scale == 0)
684 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
687 const struct mp4_track *t = &f->track;
688 uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
689 uint32_t chunk1, chunk1samples, n, total, k;
691 for (k = 1, total = 0; k < t->stsc_entry_count; k++, total += n) {
692 n = (fc[k] - fc[k - 1]) * spc[k - 1]; /* number of samples */
693 if (sample < total + n)
697 chunk1samples = spc[k - 1];
698 if (chunk1samples != 0)
699 *chunk = (sample - total) / chunk1samples + chunk1;
702 return total + (*chunk - chunk1) * chunk1samples;
706 * Compute the duration of an mp4 file.
708 * \param f See \ref mp4_close().
710 * \return The number of milliseconds of the audio track. This function never
713 uint64_t mp4_get_duration(const struct mp4 *f)
715 const struct mp4_track *t = &f->track;
717 return t->duration * 1000 / t->time_scale;
721 * Reposition the read/write file offset.
723 * \param f See \ref mp4_close().
724 * \param sample The number of the sample to reposition to.
726 * The given sample number must be within range, i.e., strictly less than the
727 * value returned by \ref mp4_num_samples().
729 * \return Standard. The only possible error is an invalid sample number.
731 int mp4_set_sample_position(struct mp4 *f, uint32_t sample)
733 const struct mp4_track *t = &f->track;
734 int32_t offset, chunk, chunk_sample;
735 uint32_t n, srs; /* sample range size */
737 if (sample >= t->stsz_sample_count)
738 return -ERRNO_TO_PARA_ERROR(EINVAL);
739 chunk_sample = chunk_of_sample(f, sample, &chunk);
740 if (t->stsz_sample_size > 0)
741 srs = (sample - chunk_sample) * t->stsz_sample_size;
743 for (srs = 0, n = chunk_sample; n < sample; n++)
744 srs += t->stsz_table[n];
746 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
747 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
748 else if (t->stco_entry_count > 0)
749 offset = t->stco_chunk_offset[chunk - 1];
752 set_position(f, offset + srs);
757 * Look up and return the size of the given sample in the stsz table.
759 * \param f See \ref mp4_close().
760 * \param sample The sample number of interest.
761 * \param result Sample size is returned here.
763 * For the sample argument the restriction mentioned in the documentation of
764 * \ref mp4_set_sample_position() applies as well.
766 * \return Standard. Like for \ref mp4_set_sample_position(), EINVAL is the
767 * only possible error.
769 int mp4_get_sample_size(const struct mp4 *f, uint32_t sample, uint32_t *result)
771 const struct mp4_track *t = &f->track;
773 if (sample >= t->stsz_sample_count)
774 return -ERRNO_TO_PARA_ERROR(EINVAL);
775 if (t->stsz_sample_size != 0)
776 *result = t->stsz_sample_size;
778 *result = t->stsz_table[sample];
783 * Return the sample rate stored in the stsd atom.
785 * \param f See \ref mp4_close().
787 * The sample rate is a property of the audio track of the mp4 file and is thus
788 * independent of the sample number.
790 * \return The function always returns a positive value because the open
791 * operation fails if the sample rate happens to be zero. A typical value is
794 uint16_t mp4_get_sample_rate(const struct mp4 *f)
796 return f->track.sample_rate;
800 * Return the number of channels of the audio track.
802 * \param f See \ref mp4_close().
804 * \return The returned channel count is guaranteed to be positive because the
805 * open operation fails if the mp4a atom is missing or contains a zero channel
808 uint16_t mp4_get_channel_count(const struct mp4 *f)
810 return f->track.channel_count;
814 * Return the number of samples of the audio track.
816 * \param f See \ref mp4_close().
818 * \return The sample count is read from the stsz atom during open.
820 uint32_t mp4_num_samples(const struct mp4 *f)
822 return f->track.stsz_sample_count;
826 * Open an mp4 file in metadata-only mode.
828 * \param cb See \ref mp4_open().
829 * \param result See \ref mp4_open().
831 * This is similar to \ref mp4_open() but is cheaper because it only parses the
832 * metadata of the mp4 file. The only functions that can subsequently be called
833 * with the file handle returned here are \ref mp4_get_meta() and \ref
838 * \sa \ref mp4_open(). The comment about ->write() and ->truncate() applies to
839 * this function as well.
841 int mp4_open_meta(const struct mp4_callback *cb, struct mp4 **result)
844 int ret = open_file(cb, true, &f);
853 * Return the metadata of an mp4 file.
855 * \param f See \ref mp4_close().
857 * The caller is allowed to add, delete or modify the entries of the returned
858 * structure with the intention to pass the modified version to \ref
861 * \return This never returns NULL, even if the file contains no metadata tag
862 * items. However, the meta count will be zero and the ->tags pointer NULL in
865 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
870 /** Total length of an on-disk metadata tag. */
871 #define TAG_LEN(_len) (24 + (_len))
872 static void create_ilst(const struct mp4_metadata *meta, uint8_t *out)
874 for (unsigned n = 0; n < meta->count; n++) {
875 struct mp4_tag *tag = meta->tags + n;
876 unsigned len = strlen(tag->value);
877 const char *atom_name;
879 if (!strcasecmp(tag->item, "title"))
880 atom_name = "\xA9" "nam";
881 else if (!strcasecmp(tag->item, "artist"))
882 atom_name = "\xA9" "ART";
883 else if (!strcasecmp(tag->item, "album"))
884 atom_name = "\xA9" "alb";
885 else if (!strcasecmp(tag->item, "date"))
886 atom_name = "\xA9" "day";
887 else if (!strcasecmp(tag->item, "comment"))
888 atom_name = "\xA9" "cmt";
891 write_u32_be(out, TAG_LEN(len));
892 memcpy(out + 4, atom_name, 4);
893 write_u32_be(out + 8, 8 /* data atom header */
894 + 8 /* flags + reserved */
896 memcpy(out + 12, "data", 4);
897 write_u32_be(out + 16, 1); /* flags */
898 write_u32_be(out + 20, 0); /* reserved */
899 memcpy(out + 24, tag->value, len);
904 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
907 uint64_t total_base = f->moov_offset + 8;
908 uint32_t total_size = f->moov_size - 8;
909 uint32_t new_ilst_size = 0;
915 for (unsigned n = 0; n < f->meta.count; n++)
916 new_ilst_size += TAG_LEN(strlen(f->meta.tags[n].value));
917 size_delta = new_ilst_size - (f->ilst_size - 8);
918 *out_size = total_size + size_delta;
919 out_buffer = alloc(*out_size);
921 set_position(f, total_base);
922 ret = read_data(f, p_out, f->udta_offset - total_base);
925 p_out += f->udta_offset - total_base;
926 ret = read_int32(f, &tmp);
929 write_u32_be(p_out, tmp + size_delta);
931 ret = read_data(f, p_out, 4);
935 ret = read_data(f, p_out, f->meta_offset - f->udta_offset - 8);
938 p_out += f->meta_offset - f->udta_offset - 8;
939 ret = read_int32(f, &tmp);
942 write_u32_be(p_out, tmp + size_delta);
944 ret = read_data(f, p_out, 4);
948 ret = read_data(f, p_out, f->ilst_offset - f->meta_offset - 8);
951 p_out += f->ilst_offset - f->meta_offset - 8;
952 ret = read_int32(f, &tmp);
955 write_u32_be(p_out, tmp + size_delta);
957 ret = read_data(f, p_out, 4);
961 create_ilst(&f->meta, p_out);
962 p_out += new_ilst_size;
963 set_position(f, f->ilst_offset + f->ilst_size);
964 ret = read_data(f, p_out, total_size - (f->ilst_offset - total_base)
971 static int write_data(struct mp4 *f, void *data, size_t size)
974 ssize_t ret = f->cb->write(f->cb->user_data, data, size);
978 return -ERRNO_TO_PARA_ERROR(errno);
986 * Write back the modified metadata items to the mp4 file.
988 * This is the only public function which modifies the contents of an mp4 file.
989 * This is achieved by calling the ->write() and ->truncate() methods of the
990 * callback structure passed to \ref mp4_open() or \ref mp4_open_meta().
992 * \param f See \ref mp4_close().
994 * The modified metadata structure does not need to be supplied to this
995 * function because it is part of the mp4 structure.
999 int mp4_update_meta(struct mp4 *f)
1001 void *new_moov_data;
1002 uint32_t new_moov_size;
1003 uint8_t buf[8] = "----moov";
1007 new_moov_data = modify_moov(f, &new_moov_size);
1008 if (!new_moov_data ) {
1012 if (f->last_atom != ATOM_MOOV) {
1013 set_position(f, f->moov_offset + 4);
1014 ret = write_data(f, "free", 4); /* rename old moov to free */
1017 /* write new moov atom at EOF */
1018 f->cb->seek(f->cb->user_data, 0, SEEK_END);
1019 } else /* overwrite old moov atom */
1020 set_position(f, f->moov_offset);
1021 write_u32_be(buf, new_moov_size + 8);
1022 ret = write_data(f, buf, sizeof(buf));
1025 ret = write_data(f, new_moov_data, new_moov_size);
1028 ret = f->cb->truncate(f->cb->user_data);
1030 ret = -ERRNO_TO_PARA_ERROR(errno);
1032 free(new_moov_data);
1037 * Return the value of the given tag item.
1039 * \param f See \ref mp4_close().
1040 * \param item "artist", "title", "album", "comment", or "date".
1042 * \return The function returns NULL if the given item is not in the above
1043 * list. Otherwise, if the file does not contain a tag for the given item, the
1044 * function also returns NULL. Otherwise a copy of the tag value is returned
1045 * and the caller should free this memory when it is no longer needed.
1047 __malloc char *mp4_get_tag_value(const struct mp4 *f, const char *item)
1049 for (unsigned n = 0; n < f->meta.count; n++)
1050 if (!strcasecmp(f->meta.tags[n].item, item))
1051 return para_strdup(f->meta.tags[n].value);