Commit 70739381 authored by Martin Storsjö's avatar Martin Storsjö

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: 's avatarMartin Storsjö <martin@martin.st>
parent 957635ba
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
*/ */
#include "avcodec.h" #include "avcodec.h"
#include "libavutil/avstring.h"
static void amr_decode_fix_avctx(AVCodecContext *avctx) static void amr_decode_fix_avctx(AVCodecContext *avctx)
{ {
...@@ -40,9 +41,6 @@ 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_dec.h>
#include <opencore-amrnb/interf_enc.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*/ /* Common code for fixed and float version*/
typedef struct AMR_bitrates { typedef struct AMR_bitrates {
int rate; int rate;
...@@ -50,20 +48,32 @@ typedef struct AMR_bitrates { ...@@ -50,20 +48,32 @@ typedef struct AMR_bitrates {
} AMR_bitrates; } AMR_bitrates;
/* Match desired bitrate */ /* 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 */ /* make the correspondance between bitrate and mode */
static const AMR_bitrates rates[] = { static const AMR_bitrates rates[] = {
{ 4750, MR475 }, { 5150, MR515 }, { 5900, MR59 }, { 6700, MR67 }, { 4750, MR475 }, { 5150, MR515 }, { 5900, MR59 }, { 6700, MR67 },
{ 7400, MR74 }, { 7950, MR795 }, { 10200, MR102 }, { 12200, MR122 } { 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) if (rates[i].rate == bitrate)
return rates[i].mode; return rates[i].mode;
/* no bitrate matching, return an error */ if (best < 0 || abs(rates[i].rate - bitrate) < min_diff) {
return -1; 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 { typedef struct AMRContext {
...@@ -171,10 +181,7 @@ static av_cold int amr_nb_encode_init(AVCodecContext *avctx) ...@@ -171,10 +181,7 @@ static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
return -1; return -1;
} }
if ((s->enc_bitrate = get_bitrate_mode(avctx->bit_rate)) < 0) { s->enc_bitrate = get_bitrate_mode(avctx->bit_rate, avctx);
av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
return AVERROR(ENOSYS);
}
return 0; return 0;
} }
...@@ -195,10 +202,7 @@ static int amr_nb_encode_frame(AVCodecContext *avctx, ...@@ -195,10 +202,7 @@ static int amr_nb_encode_frame(AVCodecContext *avctx,
AMRContext *s = avctx->priv_data; AMRContext *s = avctx->priv_data;
int written; int written;
if ((s->enc_bitrate = get_bitrate_mode(avctx->bit_rate)) < 0) { s->enc_bitrate = get_bitrate_mode(avctx->bit_rate, avctx);
av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
return AVERROR(ENOSYS);
}
written = Encoder_Interface_Encode(s->enc_state, s->enc_bitrate, data, written = Encoder_Interface_Encode(s->enc_state, s->enc_bitrate, data,
frame, 0); frame, 0);
......
...@@ -22,10 +22,7 @@ ...@@ -22,10 +22,7 @@
#include <vo-amrwbenc/enc_if.h> #include <vo-amrwbenc/enc_if.h>
#include "avcodec.h" #include "avcodec.h"
#include "libavutil/avstring.h"
static const char wb_bitrate_unsupported[] =
"bitrate not supported: use one of 6.6k, 8.85k, 12.65k, 14.25k, 15.85k, "
"18.25k, 19.85k, 23.05k, or 23.85k\n";
typedef struct AMRWBContext { typedef struct AMRWBContext {
void *state; void *state;
...@@ -33,18 +30,30 @@ typedef struct AMRWBContext { ...@@ -33,18 +30,30 @@ typedef struct AMRWBContext {
int allow_dtx; int allow_dtx;
} AMRWBContext; } AMRWBContext;
static int get_wb_bitrate_mode(int bitrate) static int get_wb_bitrate_mode(int bitrate, void *log_ctx)
{ {
/* make the correspondance between bitrate and mode */ /* make the correspondance between bitrate and mode */
static const int rates[] = { 6600, 8850, 12650, 14250, 15850, 18250, static const int rates[] = { 6600, 8850, 12650, 14250, 15850, 18250,
19850, 23050, 23850 }; 19850, 23050, 23850 };
int i; int i, best = -1, min_diff = 0;
char log_buf[200];
for (i = 0; i < 9; i++) for (i = 0; i < 9; i++) {
if (rates[i] == bitrate) if (rates[i] == bitrate)
return i; return i;
/* no bitrate matching, return an error */ if (best < 0 || abs(rates[i] - bitrate) < min_diff) {
return -1; best = i;
min_diff = abs(rates[i] - bitrate);
}
}
/* no bitrate matching exactly, log a warning */
snprintf(log_buf, sizeof(log_buf), "bitrate not supported: use one of ");
for (i = 0; i < 9; i++)
av_strlcatf(log_buf, sizeof(log_buf), "%.2fk, ", rates[i] / 1000.f);
av_strlcatf(log_buf, sizeof(log_buf), "using %.2fk", rates[best] / 1000.f);
av_log(log_ctx, AV_LOG_WARNING, "%s\n", log_buf);
return best;
} }
static av_cold int amr_wb_encode_init(AVCodecContext *avctx) static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
...@@ -61,10 +70,7 @@ static av_cold int amr_wb_encode_init(AVCodecContext *avctx) ...@@ -61,10 +70,7 @@ static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
return AVERROR(ENOSYS); return AVERROR(ENOSYS);
} }
if ((s->mode = get_wb_bitrate_mode(avctx->bit_rate)) < 0) { s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx);
av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
return AVERROR(ENOSYS);
}
avctx->frame_size = 320; avctx->frame_size = 320;
avctx->coded_frame = avcodec_alloc_frame(); avctx->coded_frame = avcodec_alloc_frame();
...@@ -91,10 +97,7 @@ static int amr_wb_encode_frame(AVCodecContext *avctx, ...@@ -91,10 +97,7 @@ static int amr_wb_encode_frame(AVCodecContext *avctx,
AMRWBContext *s = avctx->priv_data; AMRWBContext *s = avctx->priv_data;
int size; int size;
if ((s->mode = get_wb_bitrate_mode(avctx->bit_rate)) < 0) { s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx);
av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
return AVERROR(ENOSYS);
}
size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx); size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
return size; return size;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment