X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=aac_afh.c;h=0a80bfcf3a44205ea7d42f9ac260c03f910ba199;hb=d440a71683940a58747de6dc32643db452d9cf54;hp=dd9247d7ac6bba54a1c49512613ea8011a387a46;hpb=66491ab8498a5b57930fae018d0022579c2c17bb;p=paraslash.git diff --git a/aac_afh.c b/aac_afh.c index dd9247d7..0a80bfcf 100644 --- a/aac_afh.c +++ b/aac_afh.c @@ -22,13 +22,11 @@ struct aac_afh_context { const void *map; size_t mapsize; size_t fpos; - int32_t track; - struct mp4ff *mp4ff; - mp4AudioSpecificConfig masc; - struct mp4ff_callback cb; + struct mp4 *mp4; + struct mp4_callback cb; }; -static uint32_t aac_afh_read_cb(void *user_data, void *dest, uint32_t want) +static ssize_t aac_afh_read_cb(void *user_data, void *dest, size_t want) { struct aac_afh_context *c = user_data; size_t have, rv; @@ -36,7 +34,7 @@ static uint32_t aac_afh_read_cb(void *user_data, void *dest, uint32_t want) if (want == 0 || c->fpos >= c->mapsize) return 0; have = c->mapsize - c->fpos; - rv = PARA_MIN(have, (size_t)want); + rv = PARA_MIN(have, want); PARA_DEBUG_LOG("reading %zu bytes @%zu\n", rv, c->fpos); memcpy(dest, c->map + c->fpos, rv); c->fpos += rv; @@ -50,27 +48,6 @@ static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos) return 0; } -static int32_t aac_afh_get_track(struct mp4ff *mp4ff, mp4AudioSpecificConfig *masc) -{ - int32_t i, rc, num_tracks = mp4ff_total_tracks(mp4ff); - - assert(num_tracks >= 0); - for (i = 0; i < num_tracks; i++) { - unsigned char *buf = NULL; - unsigned buf_size = 0; - - mp4ff_get_decoder_config(mp4ff, i, &buf, &buf_size); - if (buf) { - rc = NeAACDecAudioSpecificConfig(buf, buf_size, masc); - free(buf); - if (rc < 0) - continue; - return i; - } - } - return -1; /* no audio track */ -} - static int aac_afh_open(const void *map, size_t mapsize, void **afh_context) { int ret; @@ -83,18 +60,11 @@ static int aac_afh_open(const void *map, size_t mapsize, void **afh_context) c->cb.seek = aac_afh_seek_cb; c->cb.user_data = c; - ret = -E_MP4FF_OPEN; - c->mp4ff = mp4ff_open_read(&c->cb); - if (!c->mp4ff) + ret = mp4_open_read(&c->cb, &c->mp4); + if (ret < 0) goto free_ctx; - c->track = aac_afh_get_track(c->mp4ff, &c->masc); - ret = -E_MP4FF_TRACK; - if (c->track < 0) - goto close_mp4ff; *afh_context = c; return 0; -close_mp4ff: - mp4ff_close(c->mp4ff); free_ctx: free(c); *afh_context = NULL; @@ -104,7 +74,7 @@ free_ctx: static void aac_afh_close(void *afh_context) { struct aac_afh_context *c = afh_context; - mp4ff_close(c->mp4ff); + mp4_close(c->mp4); free(c); } @@ -114,26 +84,28 @@ static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context, struct aac_afh_context *c = afh_context; int32_t ss; size_t offset; + int ret; - assert(chunk_num <= INT_MAX); - mp4ff_set_sample_position(c->mp4ff, c->track, chunk_num); + ret = mp4_set_sample_position(c->mp4, chunk_num); + if (ret < 0) + return ret; offset = c->fpos; - ss = mp4ff_get_sample_size(c->mp4ff, c->track, chunk_num); + ss = mp4_get_sample_size(c->mp4, chunk_num); if (ss <= 0) - return -E_MP4FF_BAD_SAMPLE; + return -E_MP4_BAD_SAMPLE; assert(ss + offset <= c->mapsize); *buf = c->map + offset; *len = ss; return 1; } -static void _aac_afh_get_taginfo(const struct mp4ff *mp4ff, struct taginfo *tags) +static void _aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags) { - mp4ff_meta_get_artist(mp4ff, &tags->artist); - mp4ff_meta_get_title(mp4ff, &tags->title); - mp4ff_meta_get_date(mp4ff, &tags->year); - mp4ff_meta_get_album(mp4ff, &tags->album); - mp4ff_meta_get_comment(mp4ff, &tags->comment); + tags->artist = mp4_meta_get_artist(mp4); + tags->title = mp4_meta_get_title(mp4); + tags->year = mp4_meta_get_date(mp4); + tags->album = mp4_meta_get_album(mp4); + tags->comment = mp4_meta_get_comment(mp4); } /* @@ -145,7 +117,7 @@ static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd, int ret; int32_t rv; struct aac_afh_context *c; - int64_t tmp; + uint64_t milliseconds; const char *buf; uint32_t n, len; @@ -153,20 +125,20 @@ static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd, if (ret < 0) return ret; - ret = -E_MP4FF_BAD_SAMPLERATE; - rv = mp4ff_get_sample_rate(c->mp4ff, c->track); + ret = -E_MP4_BAD_SAMPLERATE; + rv = mp4_get_sample_rate(c->mp4); if (rv <= 0) goto close; afhi->frequency = rv; - ret = -E_MP4FF_BAD_CHANNEL_COUNT; - rv = mp4ff_get_channel_count(c->mp4ff, c->track); + ret = -E_MP4_BAD_CHANNEL_COUNT; + rv = mp4_get_channel_count(c->mp4); if (rv <= 0) goto close; afhi->channels = rv; - ret = -E_MP4FF_BAD_SAMPLE_COUNT; - rv = mp4ff_num_samples(c->mp4ff, c->track); + ret = -E_MP4_BAD_SAMPLE_COUNT; + rv = mp4_num_samples(c->mp4); if (rv <= 0) goto close; afhi->chunks_total = rv; @@ -176,22 +148,20 @@ static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd, break; afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len); } - - tmp = c->masc.sbr_present_flag == 1? 2048 : 1024; - afhi->seconds_total = tmp * afhi->chunks_total / afhi->frequency; - ms2tv(1000 * tmp / afhi->frequency, &afhi->chunk_tv); - + milliseconds = mp4_get_duration(c->mp4); + afhi->seconds_total = milliseconds / 1000; + ms2tv(milliseconds / afhi->chunks_total, &afhi->chunk_tv); if (aac_afh_get_chunk(0, c, &buf, &len) >= 0) numbytes -= buf - map; afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000; - _aac_afh_get_taginfo(c->mp4ff, &afhi->tags); + _aac_afh_get_taginfo(c->mp4, &afhi->tags); ret = 1; close: aac_afh_close(c); return ret; } -static uint32_t aac_afh_meta_read_cb(void *user_data, void *dest, uint32_t want) +static ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_t want) { int fd = *(int *)user_data; return read(fd, dest, want); @@ -203,10 +173,10 @@ static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos) return lseek(fd, pos, SEEK_SET); } -static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want) +static ssize_t aac_afh_meta_write_cb(void *user_data, void *dest, size_t count) { int fd = *(int *)user_data; - return write(fd, dest, want); + return write(fd, dest, count); } static uint32_t aac_afh_meta_truncate_cb(void *user_data) @@ -216,94 +186,60 @@ static uint32_t aac_afh_meta_truncate_cb(void *user_data) return ftruncate(fd, offset); } -static void replace_tag(struct mp4ff_tag *tag, const char *new_val, bool *found) -{ - free(tag->value); - tag->value = para_strdup(new_val); - *found = true; -} - -static void add_tag(struct mp4ff_metadata *md, const char *item, const char *value) +static void replace_or_add_tag(const char *item, const char *value, + struct mp4_metadata *meta) { - md->tags[md->count].item = para_strdup(item); - md->tags[md->count].value = para_strdup(value); - md->count++; + uint32_t n; + struct mp4_tag *t; + + for (n = 0; n < meta->count; n++) { + t = meta->tags + n; + if (strcasecmp(t->item, item)) + continue; + free(t->value); + t->value = para_strdup(value); + return; + } + /* item not found, add new tag */ + meta->tags = para_realloc(meta->tags, (meta->count + 1) + * sizeof(struct mp4_tag)); + t = meta->tags + meta->count; + t->item = para_strdup(item); + t->value = para_strdup(value); + meta->count++; } static int aac_afh_rewrite_tags(const char *map, size_t mapsize, struct taginfo *tags, int fd, __a_unused const char *filename) { - int ret, i; - int32_t rv; - struct mp4ff_metadata metadata; - struct mp4ff *mp4ff; - struct mp4ff_callback cb = { + int ret; + struct mp4_metadata *metadata; + struct mp4 *mp4; + struct mp4_callback cb = { .read = aac_afh_meta_read_cb, .seek = aac_afh_meta_seek_cb, .write = aac_afh_meta_write_cb, .truncate = aac_afh_meta_truncate_cb, .user_data = &fd }; - bool found_artist = false, found_title = false, found_album = false, - found_year = false, found_comment = false; ret = write_all(fd, map, mapsize); if (ret < 0) return ret; lseek(fd, 0, SEEK_SET); - mp4ff = mp4ff_open_read_metaonly(&cb); - if (!mp4ff) - return -E_MP4FF_OPEN; - - ret = -E_MP4FF_META_READ; - rv = mp4ff_meta_get_num_items(mp4ff); - if (rv < 0) - goto close; - metadata.count = rv; - PARA_NOTICE_LOG("%d metadata item(s) found\n", rv); - - metadata.tags = para_malloc((metadata.count + 5) * sizeof(struct mp4ff_tag)); - for (i = 0; i < metadata.count; i++) { - struct mp4ff_tag *tag = metadata.tags + i; - - ret = -E_MP4FF_META_READ; - if (!mp4ff_meta_get_by_index(mp4ff, i, &tag->item, &tag->value)) - goto free_tags; - PARA_INFO_LOG("found: %s: %s\n", tag->item, tag->value); - if (!strcmp(tag->item, "artist")) - replace_tag(tag, tags->artist, &found_artist); - else if (!strcmp(tag->item, "title")) - replace_tag(tag, tags->title, &found_title); - else if (!strcmp(tag->item, "album")) - replace_tag(tag, tags->album, &found_album); - else if (!strcmp(tag->item, "date")) - replace_tag(tag, tags->year, &found_year); - else if (!strcmp(tag->item, "comment")) - replace_tag(tag, tags->comment, &found_comment); - } - if (!found_artist) - add_tag(&metadata, "artist", tags->artist); - if (!found_title) - add_tag(&metadata, "title", tags->title); - if (!found_album) - add_tag(&metadata, "album", tags->album); - if (!found_year) - add_tag(&metadata, "date", tags->year); - if (!found_comment) - add_tag(&metadata, "comment", tags->comment); - ret = -E_MP4FF_META_WRITE; - if (!mp4ff_meta_update(&cb, &metadata)) - goto free_tags; - ret = 1; -free_tags: - for (; i > 0; i--) { - free(metadata.tags[i - 1].item); - free(metadata.tags[i - 1].value); - } - free(metadata.tags); -close: - mp4ff_close(mp4ff); + ret = mp4_open_meta(&cb, &mp4); + if (ret < 0) + return ret; + metadata = mp4_get_meta(mp4); + PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count); + replace_or_add_tag("artist", tags->artist, metadata); + replace_or_add_tag("title", tags->title, metadata); + replace_or_add_tag("album", tags->album, metadata); + replace_or_add_tag("date", tags->year, metadata); + replace_or_add_tag("comment", tags->comment, metadata); + ret = mp4_meta_update(mp4); + mp4_close(mp4); return ret; }