From: Andre Noll Date: Wed, 21 Dec 2016 23:57:15 +0000 (+0100) Subject: afh: Dynamic chunks. X-Git-Tag: v0.6.0~7^2~18 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=b6b571e6c6fb52207b11fc7833b272ec8cfa28bf afh: Dynamic chunks. paraslash chunk tables were designed long ago with the idea that the full audio file, with the exception of a potential header, is going to be sent to the client. This allows to store a sequence of offsets as the chunk table. Each chunk is defined as the contiguous region of the file given by two consecutive offsets. For most audio formats, however, not every part of the file corresponds to encoded audio. We work around this on the client side by letting the filters detect and skip those parts which can not be fed to the decoder. This works generally well, but for the aac decoder we have a rather ugly hack that skips over any non aac decoded data of its input. This hack was never very reliable, and the concept of dynamic chunks finally allows to get rid of it. Dynamic chunks work as follows. Each audio format handler signifies support by defining the new ->get_chunk method. In this case afh_get_chunk() no longer consults the chunk table at all but calls the new method instead in order to obtain a reference to the chunk. This comes with a certain overhead at runtime because we need to call into the functions of the mp4ff library (ships together with faad) rather looking up the offset in the chunk table. Only the aac audio format handler supports dynamic chunks per this commit. To keep the patch size relatively small, this commit does not touch ->get_file_info() of the aac audio format handler. Therefore, when a new m4a file is added to the database, the aac audio format handler still creates the chunk table. A subsequent commit will turn off this unnecessary operation. The documentation is updated to mention that mp4ff is now required for the aac audio format handler. The configure script now checks for the mp4ff header and the library and deactivates aac support if it was not found. --- diff --git a/aac_afh.c b/aac_afh.c index 97b0f474..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; @@ -326,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; } diff --git a/aacdec_filter.c b/aacdec_filter.c index 5725ce04..7a757e55 100644 --- a/aacdec_filter.c +++ b/aacdec_filter.c @@ -150,9 +150,6 @@ next_buffer: if (padd->consumed_total < padd->entry) consumed = padd->entry - padd->consumed_total; } - for (; consumed < len; consumed++) - if ((inbuf[consumed] & 0xfe) == 0x20) - break; if (consumed >= len) goto success; p = inbuf + consumed; diff --git a/afh.c b/afh.c index 36c432e5..e6c46c3f 100644 --- a/afh.c +++ b/afh.c @@ -125,29 +125,38 @@ static void print_info(int audio_format_num, struct afh_info *afhi) free(msg); } -static void print_chunk_table(struct afh_info *afhi) +static void print_chunk_table(struct afh_info *afhi, int audio_format_id, + const void *map, size_t mapsize) { - int i; + int i, ret; + void *ctx = NULL; - if (conf.parser_friendly_given) { - printf("chunk_table: "); - for (i = 0; i <= afhi->chunks_total; i++) - printf("%u ", afhi->chunk_table[i]); - printf("\n"); - return; - } - for (i = 1; i <= afhi->chunks_total; i++) { + for (i = 0; i < afhi->chunks_total; i++) { struct timeval tv; long unsigned from, to; - tv_scale(i - 1, &afhi->chunk_tv, &tv); - from = tv2ms(&tv); + const char *buf; + size_t len; tv_scale(i, &afhi->chunk_tv, &tv); + from = tv2ms(&tv); + tv_scale(i + 1, &afhi->chunk_tv, &tv); to = tv2ms(&tv); - printf("%d [%lu.%03lu - %lu.%03lu] %u - %u (%u)\n", i - 1, - from / 1000, from % 1000, to / 1000, to % 1000, - afhi->chunk_table[i - 1], afhi->chunk_table[i], - afhi->chunk_table[i] - afhi->chunk_table[i - 1]); + ret = afh_get_chunk(i, afhi, audio_format_id, map, mapsize, + &buf, &len, &ctx); + if (ret < 0) { + PARA_ERROR_LOG("fatal: chunk %d: %s\n", i, + para_strerror(-ret)); + return; + } + if (!conf.parser_friendly_given) + printf("%d [%lu.%03lu - %lu.%03lu] ", i, from / 1000, + from % 1000, to / 1000, to % 1000); + printf("%td - %td", buf - (const char *)map, + buf + len - (const char *)map); + if (!conf.parser_friendly_given) + printf(" (%zu)", len); + printf("\n"); } + afh_close(ctx, audio_format_id); } __noreturn static void print_help_and_die(void) @@ -201,8 +210,8 @@ int main(int argc, char **argv) printf("File %d: %s\n", i + 1, conf.inputs[i]); print_info(audio_format_num, &afhi); if (conf.chunk_table_given) - print_chunk_table(&afhi); - printf("\n"); + print_chunk_table(&afhi, audio_format_num, + audio_file_data, audio_file_size); } clear_afhi(&afhi); } diff --git a/afh.h b/afh.h index 16c01be3..6b91691f 100644 --- a/afh.h +++ b/afh.h @@ -109,6 +109,26 @@ struct audio_format_handler { struct afh_info *afhi); /** Optional, used for header-rewriting. See \ref afh_get_header(). */ void (*get_header)(void *map, size_t mapsize, char **buf, size_t *len); + /** + * An audio format handler may signify support for dynamic chunks by + * defining ->get_chunk below. In this case the vss calls ->open() at + * BOS, ->get_chunk() for each chunk while streaming, and ->close() at + * EOS. The chunk table is not accessed at all. + * + * The function may return its (opaque) context through the last + * argument. The returned pointer is passed to subsequent calls to + * ->get_chunk() and ->close(). + */ + int (*open)(const void *map, size_t mapsize, void **afh_context); + /** + * Return a reference to one chunk. The returned pointer points to a + * portion of the memory mapped audio file. The caller must not call + * free() on it. + */ + int (*get_chunk)(long unsigned chunk_num, void *afh_context, + const char **buf, size_t *len); + /** Deallocate the resources occupied by ->open(). */ + void (*close)(void *afh_context); /** * Write audio file with altered tags, optional. * @@ -124,10 +144,12 @@ int guess_audio_format(const char *name); int compute_afhi(const char *path, char *data, size_t size, int fd, struct afh_info *afhi); const char *audio_format_name(int); -void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi, - void *map, const char **buf, size_t *len); +__must_check int afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi, + uint8_t audio_format_id, const void *map, size_t mapsize, + const char **buf, size_t *len, void **afh_context); +void afh_close(void *afh_context, uint8_t audio_format_id); int32_t afh_get_start_chunk(int32_t approx_chunk_num, - const struct afh_info *afhi); + const struct afh_info *afhi, uint8_t audio_format_id); void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id, void *map, size_t mapsize, char **buf, size_t *len); void afh_free_header(char *header_buf, uint8_t audio_format_id); diff --git a/afh_common.c b/afh_common.c index 75d8b511..a1021ee5 100644 --- a/afh_common.c +++ b/afh_common.c @@ -109,6 +109,11 @@ void afh_init(void) } } +static bool afh_supports_dynamic_chunks(int audio_format_id) +{ + return afl[audio_format_id].get_chunk; +} + /** * Guess the audio format judging from filename. * @@ -261,21 +266,73 @@ static inline size_t get_chunk_len(long unsigned chunk_num, /** * Get one chunk of audio data. * + * This implicitly calls the ->open method of the audio format handler at the + * first call. + * * \param chunk_num The number of the chunk to get. * \param afhi Describes the audio file. + * \param audio_format_id Determines the afh. * \param map The memory mapped audio file. + * \param mapsize Passed to the afh's ->open() method. * \param buf Result pointer. * \param len The length of the chunk in bytes. + * \param afh_context Value/result, determines whether ->open() is called. * * Upon return, \a buf will point so memory inside \a map. The returned buffer * must therefore not be freed by the caller. + * + * \return Standard. */ -void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi, - void *map, const char **buf, size_t *len) +__must_check int afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi, + uint8_t audio_format_id, const void *map, size_t mapsize, + const char **buf, size_t *len, void **afh_context) { - size_t pos = afhi->chunk_table[chunk_num]; - *buf = map + pos; - *len = get_chunk_len(chunk_num, afhi); + struct audio_format_handler *afh = afl + audio_format_id; + + if (afh_supports_dynamic_chunks(audio_format_id)) { + int ret; + + if (!*afh_context) { + ret = afh->open(map, mapsize, afh_context); + if (ret < 0) + return ret; + } + ret = afl[audio_format_id].get_chunk(chunk_num, *afh_context, + buf, len); + if (ret < 0) { + afh->close(*afh_context); + *afh_context = NULL; + } + return ret; + } else { + size_t pos = afhi->chunk_table[chunk_num]; + *buf = map + pos; + *len = get_chunk_len(chunk_num, afhi); + return 0; + } +} + +/** + * Deallocate resources allocated due to dynamic chunk handling. + * + * This function should be called if afh_get_chunk() was called at least once. + * It is OK to call it even for audio formats which do not support dynamic + * chunks, in which case the function does nothing. + * + * \param afh_context As returned from the ->open method of the afh. + * \param audio_format_id Determines the afh. + */ +void afh_close(void *afh_context, uint8_t audio_format_id) +{ + struct audio_format_handler *afh = afl + audio_format_id; + + if (!afh_supports_dynamic_chunks(audio_format_id)) + return; + if (!afh->close) + return; + if (!afh_context) + return; + afh->close(afh_context); } /** @@ -283,16 +340,22 @@ void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi, * * \param approx_chunk_num Upper bound for the chunk number to return. * \param afhi Needed for the chunk table. + * \param audio_format_id Determines the afh. * - * \return The first non-empty chunk <= \a approx_chunk_num. + * \return For audio format handlers which support dynamic chunks, the function + * returns the given chunk number. Otherwise it returns the first non-empty + * chunk <= \a approx_chunk_num. * * \sa \ref afh_get_chunk(). */ int32_t afh_get_start_chunk(int32_t approx_chunk_num, - const struct afh_info *afhi) + const struct afh_info *afhi, uint8_t audio_format_id) { int32_t k; + if (afh_supports_dynamic_chunks(audio_format_id)) + return approx_chunk_num; + for (k = PARA_MAX(0, approx_chunk_num); k >= 0; k--) if (get_chunk_len(k, afhi) > 0) return k; diff --git a/afh_recv.c b/afh_recv.c index 28d8f398..08f0d1e7 100644 --- a/afh_recv.c +++ b/afh_recv.c @@ -31,6 +31,7 @@ struct private_afh_recv_data { long unsigned last_chunk; struct timeval stream_start; uint32_t current_chunk; + void *afh_context; }; static int afh_execute(struct btr_node *btrn, const char *cmd, char **result) @@ -58,7 +59,8 @@ static int afh_execute(struct btr_node *btrn, const char *cmd, char **result) return ret; if (x >= pard->afhi.chunks_total) return -ERRNO_TO_PARA_ERROR(EINVAL); - pard->first_chunk = afh_get_start_chunk(x, &pard->afhi); + pard->first_chunk = afh_get_start_chunk(x, &pard->afhi, + pard->audio_format_num); pard->current_chunk = pard->first_chunk; return 1; } @@ -110,11 +112,12 @@ static int afh_recv_open(struct receiver_node *rn) goto out_clear_afhi; if (conf->begin_chunk_arg >= 0) pard->first_chunk = afh_get_start_chunk( - conf->begin_chunk_arg, &pard->afhi); + conf->begin_chunk_arg, &pard->afhi, + pard->audio_format_num); else pard->first_chunk = afh_get_start_chunk( afhi->chunks_total + conf->begin_chunk_arg, - &pard->afhi); + &pard->afhi, pard->audio_format_num); if (conf->end_chunk_given) { ret = -ERRNO_TO_PARA_ERROR(EINVAL); if (PARA_ABS(conf->end_chunk_arg) > afhi->chunks_total) @@ -150,6 +153,7 @@ static void afh_recv_close(struct receiver_node *rn) clear_afhi(&pard->afhi); para_munmap(pard->map, pard->map_size); close(pard->fd); + afh_close(pard->afh_context, pard->audio_format_num); freep(&rn->private_data); } @@ -182,7 +186,7 @@ static int afh_recv_post_select(__a_unused struct sched *s, void *context) struct afh_info *afhi = &pard->afhi; int ret; char *buf; - const char *start, *end; + const char *start; size_t size; struct timeval chunk_time; @@ -202,11 +206,16 @@ static int afh_recv_post_select(__a_unused struct sched *s, void *context) } } if (!conf->just_in_time_given) { - afh_get_chunk(pard->first_chunk, afhi, pard->map, &start, &size); - afh_get_chunk(pard->last_chunk, afhi, pard->map, &end, &size); - end += size; - PARA_INFO_LOG("adding %td bytes\n", end - start); - btr_add_output_dont_free(start, end - start, btrn); + long unsigned n; + for (n = pard->first_chunk; n < pard->last_chunk; n++) { + ret = afh_get_chunk(n, afhi, pard->audio_format_num, + pard->map, pard->map_size, &start, &size, + &pard->afh_context); + if (ret < 0) + goto out; + PARA_INFO_LOG("adding %zu bytes\n", size); + btr_add_output_dont_free(start, size, btrn); + } ret = -E_RECV_EOF; goto out; } @@ -219,7 +228,10 @@ static int afh_recv_post_select(__a_unused struct sched *s, void *context) if (ret > 0) goto out; } - afh_get_chunk(pard->current_chunk, afhi, pard->map, &start, &size); + ret = afh_get_chunk(pard->current_chunk, afhi, + pard->audio_format_num, pard->map, + pard->map_size, &start, &size, + &pard->afh_context); PARA_DEBUG_LOG("adding chunk %u\n", pard->current_chunk); btr_add_output_dont_free(start, size, btrn); if (pard->current_chunk >= pard->last_chunk) { diff --git a/configure.ac b/configure.ac index fe6d70c4..dfed1e0b 100644 --- a/configure.ac +++ b/configure.ac @@ -297,10 +297,12 @@ AC_DEFUN([NEED_FLAC_OBJECTS], [{ }]) ########################################################################### faad STASH_FLAGS -LIB_ARG_WITH([faad], [-lfaad]) +LIB_ARG_WITH([faad], [-lfaad -lmp4ff]) HAVE_FAAD=yes AC_CHECK_HEADER(neaacdec.h, [], HAVE_FAAD=no) +AC_CHECK_HEADER(mp4ff.h, [], HAVE_FAAD=no) AC_CHECK_LIB([faad], [NeAACDecOpen], [], HAVE_FAAD=no) +AC_CHECK_LIB([mp4ff], [mp4ff_meta_get_artist], [], HAVE_FAAD=no) LIB_SUBST_FLAGS(faad) UNSTASH_FLAGS ########################################################################### mad diff --git a/error.h b/error.h index 899c574b..e56f482b 100644 --- a/error.h +++ b/error.h @@ -150,6 +150,9 @@ PARA_ERROR(MP3DEC_EOF, "mp3dec: end of file"), \ PARA_ERROR(MP3_INFO, "could not read mp3 info"), \ PARA_ERROR(MP4ASC, "audio spec config error"), \ + PARA_ERROR(MP4FF_BAD_SAMPLE, "mp4ff: invalid sample number"), \ + PARA_ERROR(MP4FF_OPEN, "mp4ff: open failed"), \ + PARA_ERROR(MP4FF_TRACK, "mp4fF: no audio track"), \ PARA_ERROR(MP4V2, "mp4v2 library error"), \ PARA_ERROR(MPI_PRINT, "could not convert multi-precision integer"), \ PARA_ERROR(MPI_SCAN, "could not scan multi-precision integer"), \ diff --git a/vss.c b/vss.c index 792a7393..24dfc6bb 100644 --- a/vss.c +++ b/vss.c @@ -98,6 +98,8 @@ struct vss_task { size_t header_len; /** Time between audio file headers are sent. */ struct timeval header_interval; + /* Only used if afh supports dynamic chunks. */ + void *afh_context; }; /** @@ -349,6 +351,8 @@ static int initialize_fec_client(struct fec_client *fc, struct vss_task *vsst) static void vss_get_chunk(int chunk_num, struct vss_task *vsst, char **buf, size_t *sz) { + int ret; + /* * Chunk zero is special for header streams: It is the first portion of * the audio file which consists of the audio file header. It may be @@ -362,8 +366,15 @@ static void vss_get_chunk(int chunk_num, struct vss_task *vsst, *sz = vsst->header_len; return; } - afh_get_chunk(chunk_num, &mmd->afd.afhi, vsst->map, (const char **)buf, - sz); + ret = afh_get_chunk(chunk_num, &mmd->afd.afhi, + mmd->afd.audio_format_id, vsst->map, vsst->mapsize, + (const char **)buf, sz, &vsst->afh_context); + if (ret < 0) { + PARA_WARNING_LOG("could not get chunk %d: %s\n", + chunk_num, para_strerror(-ret)); + *buf = NULL; + *sz = 0; + } } static void compute_group_size(struct vss_task *vsst, struct fec_group *g, @@ -858,6 +869,8 @@ static void vss_eof(struct vss_task *vsst) free(mmd->afd.afhi.chunk_table); mmd->afd.afhi.chunk_table = NULL; vsst->mapsize = 0; + afh_close(vsst->afh_context, mmd->afd.audio_format_id); + vsst->afh_context = NULL; mmd->events++; } @@ -1115,7 +1128,7 @@ static int vss_post_select(struct sched *s, void *context) set_eof_barrier(vsst); mmd->chunks_sent = 0; mmd->current_chunk = afh_get_start_chunk(mmd->repos_request, - &mmd->afd.afhi); + &mmd->afd.afhi, mmd->afd.audio_format_id); mmd->new_vss_status_flags &= ~VSS_REPOS; set_mmd_offset(); } diff --git a/web/manual.md b/web/manual.md index 12454ee2..efb72b1e 100644 --- a/web/manual.md +++ b/web/manual.md @@ -249,8 +249,11 @@ recognized. The mp3 tagger also needs this library for modifying you need libogg, libvorbis, libvorbisfile. The corresponding Debian packages are called `libogg-dev` and `libvorbis-dev`. -- [libfaad](http://www.audiocoding.com/). For aac files (m4a) you -need libfaad (`libfaad-dev`). +- [libfaad and mp4ff](http://www.audiocoding.com/). For aac files +(m4a) you need libfaad and libmp4ff (package: `libfaad-dev`). Note +that for some distributions, e.g. Ubuntu, mp4ff is not part of the +libfaad package. Install the faad library from sources (available +through the above link) to get the mp4ff library and header files. - [speex](http://www.speex.org/). In order to stream or decode speex files, libspeex (`libspeex-dev`) is required.