X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=aac_afh.c;h=bcf7b785e4bd5518d751b22d5b368e296e43c662;hp=1c7fd706f0a7c9fe8c9d4d8db43846be18a9304d;hb=b6b571e6c6fb52207b11fc7833b272ec8cfa28bf;hpb=21c6e0b09b42e61e72b741bd726856ab0bcd1d64 diff --git a/aac_afh.c b/aac_afh.c index 1c7fd706..bcf7b785 100644 --- a/aac_afh.c +++ b/aac_afh.c @@ -14,6 +14,7 @@ #include #include "para.h" +#include #include "error.h" #include "portable_io.h" #include "afh.h" @@ -21,6 +22,133 @@ #include "aac.h" #include "fd.h" +struct aac_afh_context { + const void *map; + size_t mapsize; + size_t fpos; + int32_t track; + mp4ff_t *mp4ff; + mp4AudioSpecificConfig masc; + mp4ff_callback_t cb; +}; + +static uint32_t aac_afh_read_cb(void *user_data, void *dest, uint32_t want) +{ + struct aac_afh_context *c = user_data; + uint32_t have, rv; + + if (want == 0 || c->fpos >= c->mapsize) { + PARA_INFO_LOG("failed attempt to read %u bytes @%zu\n", want, + c->fpos); + errno = EAGAIN; + return -1; + } + have = c->mapsize - c->fpos; + rv = PARA_MIN(have, want); + PARA_DEBUG_LOG("reading %u bytes @%zu\n", rv, c->fpos); + memcpy(dest, c->map + c->fpos, rv); + c->fpos += rv; + return rv; +} + +static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos) +{ + struct aac_afh_context *c = user_data; + c->fpos = pos; + return 0; +} + +static int32_t aac_afh_get_track(mp4ff_t *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; + struct aac_afh_context *c = para_malloc(sizeof(*c)); + + c->map = map; + c->mapsize = mapsize; + c->fpos = 0; + c->cb.read = aac_afh_read_cb; + 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) + 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; + return ret; +} + +static void aac_afh_close(void *afh_context) +{ + struct aac_afh_context *c = afh_context; + mp4ff_close(c->mp4ff); + free(c); +} + +/** + * Libmp4ff function to reposition the file to the given sample. + * + * \param f The opaque handle returned by mp4ff_open_read(). + * \param track The number of the (audio) track. + * \param sample Destination. + * + * We need this function to obtain the offset of the sample within the audio + * file. Unfortunately, it is not exposed in the mp4ff header. + * + * \return This function always returns 0. + */ +int32_t mp4ff_set_sample_position(mp4ff_t *f, const int32_t track, const int32_t sample); + +static int aac_afh_get_chunk(long unsigned chunk_num, void *afh_context, + const char **buf, size_t *len) +{ + struct aac_afh_context *c = afh_context; + int32_t ss; + size_t offset; + + assert(chunk_num <= INT_MAX); + /* this function always returns zero */ + mp4ff_set_sample_position(c->mp4ff, c->track, chunk_num); + offset = c->fpos; + ss = mp4ff_read_sample_getsize(c->mp4ff, c->track, chunk_num); + if (ss <= 0) + return -E_MP4FF_BAD_SAMPLE; + assert(ss + offset <= c->mapsize); + *buf = c->map + offset; + *len = ss; + return 1; +} static int aac_find_stsz(char *buf, size_t buflen, off_t *skip) { int i; @@ -183,8 +311,6 @@ static int aac_set_chunk_tv(struct afh_info *afhi, struct timeval total; long unsigned ms; - if (!mp4ASC->samplingFrequency) - return -E_MP4ASC; ms = 1000.0 * afhi->chunks_total * tmp / mp4ASC->samplingFrequency; ms2tv(ms, &total); tv_divide(afhi->chunks_total, &total, &afhi->chunk_tv); @@ -242,6 +368,7 @@ static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd, afhi->chunk_table[0] = ret; for (i = 1; i<= afhi->chunks_total; i++) afhi->chunk_table[i] += ret; + set_max_chunk_size(afhi); afhi->channels = channels; afhi->frequency = rate; ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */ @@ -327,4 +454,7 @@ void aac_afh_init(struct audio_format_handler *afh) afh->get_file_info = aac_get_file_info, afh->suffixes = aac_suffixes; afh->rewrite_tags = aac_rewrite_tags; + afh->open = aac_afh_open; + afh->get_chunk = aac_afh_get_chunk; + afh->close = aac_afh_close; }