From: Andre Noll Date: Sat, 13 Aug 2011 10:07:14 +0000 (+0200) Subject: Merge branch 't/mad_sync_fix' X-Git-Tag: v0.4.8~12 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=12db97b44ab84de5021f9a40760ccf5931aae347;hp=42c4c4fa5662d5cb0f362ace04d26e9189e165b2 Merge branch 't/mad_sync_fix' --- diff --git a/NEWS b/NEWS index 7d330ce4..a9572d7b 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,7 @@ are the highlights of this release. to replace 5 lines in the config file (one for each audio format) by one single line. See the manual for details. - Compiles cleanly also with llvm/clang. + - Corrupt mp3 files are handled more gracefully. -------------------------------------- 0.4.7 (2011-06-01) "infinite rollback" diff --git a/mp3dec_filter.c b/mp3dec_filter.c index 82f01418..6982f264 100644 --- a/mp3dec_filter.c +++ b/mp3dec_filter.c @@ -24,15 +24,6 @@ #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\ (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15)) -/** State of the decoding process. */ -enum mp3dec_flags { - /** Bad main_data_begin pointer encounterd. */ - MP3DEC_FLAG_BAD_DATA = 1, - /** Some output has already been produced. */ - MP3DEC_FLAG_DECODE_STARTED = 2, - MP3DEC_FLAG_NEED_MORE = 4, -}; - /** Data specific to the mp3dec filter. */ struct private_mp3dec_data { /** Information on the current mp3 stream. */ @@ -41,57 +32,22 @@ struct private_mp3dec_data { struct mad_frame frame; /** Contains the PCM output. */ struct mad_synth synth; - /** See \ref mp3dec_flags. */ - unsigned flags; - /** Defer decoding until this time. */ - struct timeval stream_start_barrier; - /** Wait until this many input bytes are available. */ - size_t input_len_barrier; /** The number of channels of the current stream. */ unsigned int channels; /** Current sample rate in Hz. */ unsigned int sample_rate; }; -static int need_bad_data_delay(struct private_mp3dec_data *pmd, - size_t bytes_available) -{ - if (!(pmd->flags & MP3DEC_FLAG_BAD_DATA)) - return 0; - if (pmd->flags & MP3DEC_FLAG_DECODE_STARTED) - return 0; - if (bytes_available >= pmd->input_len_barrier) - return 0; - if (tv_diff(now, &pmd->stream_start_barrier, NULL) > 0) - return 0; - return 1; -} - -/* - * Returns negative on serious errors, zero if the error should be ignored and - * positive on bad data pointer errors at stream start. - */ -static int handle_decode_error(struct private_mp3dec_data *pmd, size_t len) +/* Returns negative on serious errors. */ +static int handle_decode_error(struct private_mp3dec_data *pmd) { - const struct timeval delay = {0, 60 * 1000}; if (!MAD_RECOVERABLE(pmd->stream.error) && pmd->stream.error != MAD_ERROR_BUFLEN) { PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd->stream)); return -E_MAD_FRAME_DECODE; } PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd->stream)); - if (pmd->stream.error != MAD_ERROR_BADDATAPTR) - return 0; - if (pmd->flags & MP3DEC_FLAG_DECODE_STARTED) - return 0; - /* - * Bad data pointer at stream start. Defer decoding until the amount of - * data we are about to skip is available again, but wait at most 60ms. - */ - pmd->flags |= MP3DEC_FLAG_BAD_DATA; - pmd->input_len_barrier = len; - tv_add(now, &delay, &pmd->stream_start_barrier); - return 1; + return 0; } static size_t used_mad_buffer_bytes(struct mad_stream *s, size_t max) @@ -124,7 +80,7 @@ static void mp3dec_post_select(__a_unused struct sched *s, struct task *t) int i, ret; struct private_mp3dec_data *pmd = fn->private_data; struct btr_node *btrn = fn->btrn; - size_t loaded, used, len, iqs; + size_t loaded = 0, used, len, iqs; char *inbuffer, *outbuffer; next_buffer: @@ -134,8 +90,6 @@ next_buffer: ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL); if (ret < 0) goto err; - if (need_bad_data_delay(pmd, iqs)) - return; if (ret == 0) return; btr_merge(btrn, fn->min_iqs); @@ -158,37 +112,37 @@ next_frame: goto err; } fn->min_iqs += 100; + } + if (loaded == 0) goto next_buffer; - } else if (pmd->stream.error != MAD_ERROR_LOSTSYNC) - PARA_DEBUG_LOG("header decode: %s\n", - mad_stream_errorstr(&pmd->stream)); - goto next_buffer; + return; } fn->min_iqs = 0; pmd->sample_rate = pmd->frame.header.samplerate; pmd->channels = MAD_NCHANNELS(&pmd->frame.header); +decode: ret = mad_frame_decode(&pmd->frame, &pmd->stream); if (ret != 0) { - PARA_INFO_LOG("frame decode: %s\n", mad_stream_errorstr(&pmd->stream)); - used = used_mad_buffer_bytes(&pmd->stream, len); - ret = handle_decode_error(pmd, used); - btr_consume(btrn, used); + ret = handle_decode_error(pmd); if (ret < 0) goto err; - if (ret == 0) - goto next_buffer; + mad_stream_sync(&pmd->stream); + if (pmd->stream.error == MAD_ERROR_BUFLEN) + return; + if (pmd->stream.error != MAD_ERROR_BADDATAPTR) + goto decode; + used = used_mad_buffer_bytes(&pmd->stream, len); + btr_consume(btrn, used); return; } mad_synth_frame(&pmd->synth, &pmd->frame); - pmd->flags |= MP3DEC_FLAG_DECODE_STARTED; - - outbuffer = para_malloc(pmd->synth.pcm.length * 4); + outbuffer = para_malloc(pmd->synth.pcm.length * 2 * pmd->channels); loaded = 0; for (i = 0; i < pmd->synth.pcm.length; i++) { int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]); write_int16_host_endian(outbuffer + loaded, sample); loaded += 2; - if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */ + if (pmd->channels == 2) { /* stereo */ sample = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]); write_int16_host_endian(outbuffer + loaded, sample); loaded += 2;