summaryrefslogtreecommitdiff
path: root/libavcodec/libopencore-amr.c
diff options
context:
space:
mode:
authorMartin Storsjö <martin@martin.st>2011-04-13 10:59:08 +0300
committerMartin Storsjö <martin@martin.st>2011-04-14 00:31:33 +0300
commit70739381213b087cca9570b66561dc57652b6fb9 (patch)
tree99dd02cbdf0d7fb2760c608738550e7832b79248 /libavcodec/libopencore-amr.c
parent957635ba1435d95209ee43c7e9fa0195dcf60152 (diff)
libopencore-amr, libvo-amrwbenc: Find the closest matching bitrate
Dynamically print the supported bitrates from the local table, instead of using a hardcoded log message. Signed-off-by: Martin Storsjö <martin@martin.st>
Diffstat (limited to 'libavcodec/libopencore-amr.c')
-rw-r--r--libavcodec/libopencore-amr.c36
1 files changed, 20 insertions, 16 deletions
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 <opencore-amrnb/interf_dec.h>
#include <opencore-amrnb/interf_enc.h>
-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);