47 #if CONFIG_LIBOPENCORE_AMRNB
49 #include <opencore-amrnb/interf_dec.h>
50 #include <opencore-amrnb/interf_enc.h>
64 #if CONFIG_LIBOPENCORE_AMRNB_DECODER
73 s->dec_state = Decoder_Interface_init();
75 av_log(avctx, AV_LOG_ERROR,
"Decoder_Interface_init error\n");
89 Decoder_Interface_exit(s->dec_state);
98 int buf_size = avpkt->
size;
100 static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
102 int packet_size, ret;
104 av_dlog(avctx,
"amr_decode_frame buf=%p buf_size=%d frame_count=%d!!\n",
108 s->
frame.nb_samples = 160;
110 av_log(avctx, AV_LOG_ERROR,
"get_buffer() failed\n");
114 dec_mode = (buf[0] >> 3) & 0x000F;
115 packet_size = block_size[dec_mode] + 1;
117 if (packet_size > buf_size) {
118 av_log(avctx, AV_LOG_ERROR,
"amr frame too short (%u, should be %u)\n",
119 buf_size, packet_size);
123 av_dlog(avctx,
"packet_size=%d buf= 0x%X %X %X %X\n",
124 packet_size, buf[0], buf[1], buf[2], buf[3]);
126 Decoder_Interface_Decode(s->dec_state, buf, (
short *)s->
frame.data[0], 0);
134 AVCodec ff_libopencore_amrnb_decoder = {
135 .
name =
"libopencore_amrnb",
139 .
init = amr_nb_decode_init,
140 .
close = amr_nb_decode_close,
141 .
decode = amr_nb_decode_frame,
142 .capabilities = CODEC_CAP_DR1,
147 #if CONFIG_LIBOPENCORE_AMRNB_ENCODER
149 typedef struct AMR_bitrates {
155 static int get_bitrate_mode(
int bitrate,
void *log_ctx)
158 static const AMR_bitrates
rates[] = {
159 { 4750, MR475 }, { 5150, MR515 }, { 5900, MR59 }, { 6700, MR67 },
160 { 7400, MR74 }, { 7950, MR795 }, { 10200, MR102 }, { 12200, MR122 }
162 int i, best = -1, min_diff = 0;
165 for (i = 0; i < 8; i++) {
166 if (rates[i].rate == bitrate)
167 return rates[i].mode;
168 if (best < 0 || abs(rates[i].rate - bitrate) < min_diff) {
170 min_diff = abs(rates[i].rate - bitrate);
174 snprintf(log_buf,
sizeof(log_buf),
"bitrate not supported: use one of ");
175 for (i = 0; i < 8; i++)
176 av_strlcatf(log_buf,
sizeof(log_buf),
"%.2fk, ", rates[i].rate / 1000.f);
177 av_strlcatf(log_buf,
sizeof(log_buf),
"using %.2fk", rates[best].rate / 1000.f);
178 av_log(log_ctx, AV_LOG_WARNING,
"%s\n", log_buf);
197 av_log(avctx, AV_LOG_ERROR,
"Only 8000Hz sample rate supported\n");
202 av_log(avctx, AV_LOG_ERROR,
"Only mono supported\n");
209 #if FF_API_OLD_ENCODE_AUDIO
215 s->enc_state = Encoder_Interface_init(s->enc_dtx);
217 av_log(avctx, AV_LOG_ERROR,
"Encoder_Interface_init error\n");
222 s->enc_mode = get_bitrate_mode(avctx->
bit_rate, avctx);
232 Encoder_Interface_exit(s->enc_state);
234 #if FF_API_OLD_ENCODE_AUDIO
241 const AVFrame *frame,
int *got_packet_ptr)
245 int16_t *flush_buf =
NULL;
246 const int16_t *
samples = frame ? (
const int16_t *)frame->
data[0] :
NULL;
248 if (s->enc_bitrate != avctx->
bit_rate) {
249 s->enc_mode = get_bitrate_mode(avctx->
bit_rate, avctx);
254 av_log(avctx, AV_LOG_ERROR,
"Error getting output packet\n");
263 memcpy(flush_buf, samples, frame->
nb_samples *
sizeof(*flush_buf));
266 s->enc_last_frame = -1;
273 if (s->enc_last_frame < 0)
279 s->enc_last_frame = -1;
282 written = Encoder_Interface_Encode(s->enc_state, s->enc_mode, samples,
284 av_dlog(avctx,
"amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
285 written, s->enc_mode, frame[0]);
291 avpkt->
size = written;
297 AVCodec ff_libopencore_amrnb_encoder = {
298 .
name =
"libopencore_amrnb",
302 .
init = amr_nb_encode_init,
303 .encode2 = amr_nb_encode_frame,
304 .
close = amr_nb_encode_close,
305 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
309 .priv_class = &
class,
316 #if CONFIG_LIBOPENCORE_AMRWB_DECODER
318 #include <opencore-amrwb/dec_if.h>
319 #include <opencore-amrwb/if_rom.h>
334 s->
state = D_IF_init();
343 int *got_frame_ptr,
AVPacket *avpkt)
346 int buf_size = avpkt->
size;
350 static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
353 s->
frame.nb_samples = 320;
355 av_log(avctx, AV_LOG_ERROR,
"get_buffer() failed\n");
359 mode = (buf[0] >> 3) & 0x000F;
360 packet_size = block_size[mode];
362 if (packet_size > buf_size) {
363 av_log(avctx, AV_LOG_ERROR,
"amr frame too short (%u, should be %u)\n",
364 buf_size, packet_size + 1);
368 D_IF_decode(s->
state, buf, (
short *)s->
frame.data[0], _good_frame);
384 AVCodec ff_libopencore_amrwb_decoder = {
385 .
name =
"libopencore_amrwb",
389 .
init = amr_wb_decode_init,
390 .
close = amr_wb_decode_close,
391 .
decode = amr_wb_decode_frame,
392 .capabilities = CODEC_CAP_DR1,