From 70739381213b087cca9570b66561dc57652b6fb9 Mon Sep 17 00:00:00 2001 From: Martin Storsjö Date: Wed, 13 Apr 2011 10:59:08 +0300 Subject: libopencore-amr, libvo-amrwbenc: Find the closest matching bitrate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dynamically print the supported bitrates from the local table, instead of using a hardcoded log message. Signed-off-by: Martin Storsjö --- libavcodec/libopencore-amr.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'libavcodec/libopencore-amr.c') diff --git a/libavcodec/libopencore-amr.c b/libavcodec/libopencore-amr.c index 4d26fc6605..e6216c9e5e 100644 --- a/libavcodec/libopencore-amr.c +++ b/libavcodec/libopencore-amr.c @@ -20,6 +20,7 @@ */ #include "avcodec.h" +#include "libavutil/avstring.h" static void amr_decode_fix_avctx(AVCodecContext *avctx) { @@ -40,9 +41,6 @@ static void amr_decode_fix_avctx(AVCodecContext *avctx) #include #include -static const char nb_bitrate_unsupported[] = - "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n"; - /* Common code for fixed and float version*/ typedef struct AMR_bitrates { int rate; @@ -50,20 +48,32 @@ typedef struct AMR_bitrates { } AMR_bitrates; /* Match desired bitrate */ -static int get_bitrate_mode(int bitrate) +static int get_bitrate_mode(int bitrate, void *log_ctx) { /* make the correspondance between bitrate and mode */ static const AMR_bitrates rates[] = { { 4750, MR475 }, { 5150, MR515 }, { 5900, MR59 }, { 6700, MR67 }, { 7400, MR74 }, { 7950, MR795 }, { 10200, MR102 }, { 12200, MR122 } }; - int i; + int i, best = -1, min_diff = 0; + char log_buf[200]; - for (i = 0; i < 8; i++) + for (i = 0; i < 8; i++) { if (rates[i].rate == bitrate) return rates[i].mode; - /* no bitrate matching, return an error */ - return -1; + if (best < 0 || abs(rates[i].rate - bitrate) < min_diff) { + best = i; + min_diff = abs(rates[i].rate - bitrate); + } + } + /* no bitrate matching exactly, log a warning */ + snprintf(log_buf, sizeof(log_buf), "bitrate not supported: use one of "); + for (i = 0; i < 8; i++) + av_strlcatf(log_buf, sizeof(log_buf), "%.2fk, ", rates[i].rate / 1000.f); + av_strlcatf(log_buf, sizeof(log_buf), "using %.2fk", rates[best].rate / 1000.f); + av_log(log_ctx, AV_LOG_WARNING, "%s\n", log_buf); + + return best; } typedef struct AMRContext { @@ -171,10 +181,7 @@ static av_cold int amr_nb_encode_init(AVCodecContext *avctx) return -1; } - if ((s->enc_bitrate = get_bitrate_mode(avctx->bit_rate)) < 0) { - av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); - return AVERROR(ENOSYS); - } + s->enc_bitrate = get_bitrate_mode(avctx->bit_rate, avctx); return 0; } @@ -195,10 +202,7 @@ static int amr_nb_encode_frame(AVCodecContext *avctx, AMRContext *s = avctx->priv_data; int written; - if ((s->enc_bitrate = get_bitrate_mode(avctx->bit_rate)) < 0) { - av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); - return AVERROR(ENOSYS); - } + s->enc_bitrate = get_bitrate_mode(avctx->bit_rate, avctx); written = Encoder_Interface_Encode(s->enc_state, s->enc_bitrate, data, frame, 0); -- cgit v1.2.3