libavcodec/libopencore-amr.c
0b175caa
 /*
  * AMR Audio decoder stub
41ed7ab4
  * Copyright (c) 2003 The FFmpeg project
0b175caa
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
015c2d92
 #include <inttypes.h>
 
70739381
 #include "libavutil/avstring.h"
a903f8f0
 #include "libavutil/channel_layout.h"
1d9c2dc8
 #include "libavutil/common.h"
651b276e
 #include "libavutil/opt.h"
a903f8f0
 #include "avcodec.h"
8ccf545b
 #include "audio_frame_queue.h"
 #include "internal.h"
0b175caa
 
ad961726
 static int amr_decode_fix_avctx(AVCodecContext *avctx)
0b175caa
 {
36ef5369
     const int is_amr_wb = 1 + (avctx->codec_id == AV_CODEC_ID_AMR_WB);
0b175caa
 
0aaf0a07
     if (!avctx->sample_rate)
         avctx->sample_rate = 8000 * is_amr_wb;
0b175caa
 
d40dab90
     if (avctx->channels > 1) {
63d744e2
         avpriv_report_missing_feature(avctx, "multi-channel AMR");
d40dab90
         return AVERROR_PATCHWELCOME;
     }
0b175caa
 
d40dab90
     avctx->channels       = 1;
     avctx->channel_layout = AV_CH_LAYOUT_MONO;
     avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
ad961726
     return 0;
0b175caa
 }
 
 #if CONFIG_LIBOPENCORE_AMRNB
 
 #include <opencore-amrnb/interf_dec.h>
 #include <opencore-amrnb/interf_enc.h>
 
 typedef struct AMRContext {
651b276e
     AVClass *av_class;
02c63a10
     void *dec_state;
     void *enc_state;
0b175caa
     int   enc_bitrate;
3dd82afc
     int   enc_mode;
651b276e
     int   enc_dtx;
fe78470a
     int   enc_last_frame;
8ccf545b
     AudioFrameQueue afq;
0b175caa
 } AMRContext;
 
e6bda9a9
 #if CONFIG_LIBOPENCORE_AMRNB_DECODER
0b175caa
 static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
 {
882221af
     AMRContext *s  = avctx->priv_data;
ad961726
     int ret;
 
     if ((ret = amr_decode_fix_avctx(avctx)) < 0)
         return ret;
0b175caa
 
02c63a10
     s->dec_state   = Decoder_Interface_init();
     if (!s->dec_state) {
a8ec07c9
         av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\n");
0b175caa
         return -1;
     }
 
     return 0;
 }
 
 static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
 {
     AMRContext *s = avctx->priv_data;
 
02c63a10
     Decoder_Interface_exit(s->dec_state);
0eea2129
 
0b175caa
     return 0;
 }
 
 static int amr_nb_decode_frame(AVCodecContext *avctx, void *data,
0eea2129
                                int *got_frame_ptr, AVPacket *avpkt)
0b175caa
 {
0cd08367
     AVFrame *frame     = data;
0b175caa
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
882221af
     AMRContext *s      = avctx->priv_data;
0b175caa
     static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
     enum Mode dec_mode;
0eea2129
     int packet_size, ret;
0b175caa
 
6a85dfc8
     ff_dlog(avctx, "amr_decode_frame buf=%p buf_size=%d frame_count=%d!!\n",
402c9878
             buf, buf_size, avctx->frame_number);
0b175caa
 
0eea2129
     /* get output buffer */
0cd08367
     frame->nb_samples = 160;
1ec94b0f
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
0eea2129
         return ret;
0b175caa
 
882221af
     dec_mode    = (buf[0] >> 3) & 0x000F;
0b175caa
     packet_size = block_size[dec_mode] + 1;
 
     if (packet_size > buf_size) {
015c2d92
         av_log(avctx, AV_LOG_ERROR, "AMR frame too short (%d, should be %d)\n",
0b175caa
                buf_size, packet_size);
900a129f
         return AVERROR_INVALIDDATA;
0b175caa
     }
 
015c2d92
     ff_dlog(avctx, "packet_size=%d buf= 0x%"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"\n",
             packet_size, buf[0], buf[1], buf[2], buf[3]);
0b175caa
     /* call decoder */
0cd08367
     Decoder_Interface_Decode(s->dec_state, buf, (short *)frame->data[0], 0);
0eea2129
 
0cd08367
     *got_frame_ptr = 1;
0b175caa
 
     return packet_size;
 }
 
e7e2df27
 AVCodec ff_libopencore_amrnb_decoder = {
ec6402b7
     .name           = "libopencore_amrnb",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("OpenCORE AMR-NB (Adaptive Multi-Rate Narrow-Band)"),
ec6402b7
     .type           = AVMEDIA_TYPE_AUDIO,
36ef5369
     .id             = AV_CODEC_ID_AMR_NB,
ec6402b7
     .priv_data_size = sizeof(AMRContext),
     .init           = amr_nb_decode_init,
     .close          = amr_nb_decode_close,
     .decode         = amr_nb_decode_frame,
def97856
     .capabilities   = AV_CODEC_CAP_DR1,
0b175caa
 };
e6bda9a9
 #endif /* CONFIG_LIBOPENCORE_AMRNB_DECODER */
0b175caa
 
e6bda9a9
 #if CONFIG_LIBOPENCORE_AMRNB_ENCODER
56632fef
 /* Common code for fixed and float version*/
 typedef struct AMR_bitrates {
     int       rate;
     enum Mode mode;
 } AMR_bitrates;
 
 /* Match desired bitrate */
 static int get_bitrate_mode(int bitrate, void *log_ctx)
 {
41ed7ab4
     /* make the correspondence between bitrate and mode */
56632fef
     static const AMR_bitrates rates[] = {
         { 4750, MR475 }, { 5150, MR515 }, {  5900, MR59  }, {  6700, MR67  },
         { 7400, MR74 },  { 7950, MR795 }, { 10200, MR102 }, { 12200, MR122 }
     };
     int i, best = -1, min_diff = 0;
     char log_buf[200];
 
     for (i = 0; i < 8; i++) {
         if (rates[i].rate == bitrate)
             return rates[i].mode;
         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;
 }
 
 static const AVOption options[] = {
     { "dtx", "Allow DTX (generate comfort noise)", offsetof(AMRContext, enc_dtx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
     { NULL }
 };
 
bf18abb2
 static const AVClass amrnb_class = {
56632fef
     "libopencore_amrnb", av_default_item_name, options, LIBAVUTIL_VERSION_INT
 };
 
0b175caa
 static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
 {
     AMRContext *s = avctx->priv_data;
 
b30f4510
     if (avctx->sample_rate != 8000 && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
0b175caa
         av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
900a129f
         return AVERROR(ENOSYS);
0b175caa
     }
 
     if (avctx->channels != 1) {
         av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
900a129f
         return AVERROR(ENOSYS);
0b175caa
     }
 
     avctx->frame_size  = 160;
2df0c32e
     avctx->initial_padding = 50;
8ccf545b
     ff_af_queue_init(avctx, &s->afq);
0b175caa
 
651b276e
     s->enc_state = Encoder_Interface_init(s->enc_dtx);
02c63a10
     if (!s->enc_state) {
0b175caa
         av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
         return -1;
     }
 
3dd82afc
     s->enc_mode    = get_bitrate_mode(avctx->bit_rate, avctx);
     s->enc_bitrate = avctx->bit_rate;
0b175caa
 
     return 0;
 }
 
 static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
 {
     AMRContext *s = avctx->priv_data;
 
02c63a10
     Encoder_Interface_exit(s->enc_state);
8ccf545b
     ff_af_queue_close(&s->afq);
0b175caa
     return 0;
 }
 
8ccf545b
 static int amr_nb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                                const AVFrame *frame, int *got_packet_ptr)
0b175caa
 {
     AMRContext *s = avctx->priv_data;
8ccf545b
     int written, ret;
fe78470a
     int16_t *flush_buf = NULL;
8ccf545b
     const int16_t *samples = frame ? (const int16_t *)frame->data[0] : NULL;
0b175caa
 
3dd82afc
     if (s->enc_bitrate != avctx->bit_rate) {
         s->enc_mode    = get_bitrate_mode(avctx->bit_rate, avctx);
         s->enc_bitrate = avctx->bit_rate;
0b175caa
     }
 
e36db49b
     if ((ret = ff_alloc_packet2(avctx, avpkt, 32, 0)) < 0)
8ccf545b
         return ret;
 
     if (frame) {
         if (frame->nb_samples < avctx->frame_size) {
818aaa76
             flush_buf = av_mallocz_array(avctx->frame_size, sizeof(*flush_buf));
fe78470a
             if (!flush_buf)
                 return AVERROR(ENOMEM);
8ccf545b
             memcpy(flush_buf, samples, frame->nb_samples * sizeof(*flush_buf));
fe78470a
             samples = flush_buf;
2df0c32e
             if (frame->nb_samples < avctx->frame_size - avctx->initial_padding)
fe78470a
                 s->enc_last_frame = -1;
         }
d6180aa2
         if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) {
8ccf545b
             av_freep(&flush_buf);
             return ret;
         }
fe78470a
     } else {
         if (s->enc_last_frame < 0)
             return 0;
818aaa76
         flush_buf = av_mallocz_array(avctx->frame_size, sizeof(*flush_buf));
fe78470a
         if (!flush_buf)
             return AVERROR(ENOMEM);
         samples = flush_buf;
         s->enc_last_frame = -1;
     }
 
     written = Encoder_Interface_Encode(s->enc_state, s->enc_mode, samples,
8ccf545b
                                        avpkt->data, 0);
6a85dfc8
     ff_dlog(avctx, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
4a289624
             written, s->enc_mode, avpkt->data[0]);
0b175caa
 
8ccf545b
     /* Get the next frame pts/duration */
     ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
                        &avpkt->duration);
 
     avpkt->size = written;
     *got_packet_ptr = 1;
fe78470a
     av_freep(&flush_buf);
8ccf545b
     return 0;
0b175caa
 }
 
e7e2df27
 AVCodec ff_libopencore_amrnb_encoder = {
ec6402b7
     .name           = "libopencore_amrnb",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("OpenCORE AMR-NB (Adaptive Multi-Rate Narrow-Band)"),
ec6402b7
     .type           = AVMEDIA_TYPE_AUDIO,
36ef5369
     .id             = AV_CODEC_ID_AMR_NB,
ec6402b7
     .priv_data_size = sizeof(AMRContext),
     .init           = amr_nb_encode_init,
8ccf545b
     .encode2        = amr_nb_encode_frame,
ec6402b7
     .close          = amr_nb_encode_close,
def97856
     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SMALL_LAST_FRAME,
00c3b67b
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                      AV_SAMPLE_FMT_NONE },
bf18abb2
     .priv_class     = &amrnb_class,
0b175caa
 };
e6bda9a9
 #endif /* CONFIG_LIBOPENCORE_AMRNB_ENCODER */
0b175caa
 
e6bda9a9
 #endif /* CONFIG_LIBOPENCORE_AMRNB */
0b175caa
 
 /* -----------AMR wideband ------------*/
8837f439
 #if CONFIG_LIBOPENCORE_AMRWB_DECODER
0b175caa
 
 #include <opencore-amrwb/dec_if.h>
 #include <opencore-amrwb/if_rom.h>
 
 typedef struct AMRWBContext {
     void  *state;
 } AMRWBContext;
 
 static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
 {
     AMRWBContext *s = avctx->priv_data;
ad961726
     int ret;
0b175caa
 
ad961726
     if ((ret = amr_decode_fix_avctx(avctx)) < 0)
         return ret;
0b175caa
 
ad961726
     s->state        = D_IF_init();
0b175caa
 
     return 0;
 }
 
 static int amr_wb_decode_frame(AVCodecContext *avctx, void *data,
0eea2129
                                int *got_frame_ptr, AVPacket *avpkt)
0b175caa
 {
0cd08367
     AVFrame *frame     = data;
0b175caa
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
882221af
     AMRWBContext *s    = avctx->priv_data;
0eea2129
     int mode, ret;
     int packet_size;
0b175caa
     static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
 
0eea2129
     /* get output buffer */
0cd08367
     frame->nb_samples = 320;
1ec94b0f
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
0eea2129
         return ret;
0b175caa
 
882221af
     mode        = (buf[0] >> 3) & 0x000F;
0b175caa
     packet_size = block_size[mode];
 
     if (packet_size > buf_size) {
015c2d92
         av_log(avctx, AV_LOG_ERROR, "AMR frame too short (%d, should be %d)\n",
0b175caa
                buf_size, packet_size + 1);
900a129f
         return AVERROR_INVALIDDATA;
0b175caa
     }
34b6f1ef
     if (!packet_size) {
         av_log(avctx, AV_LOG_ERROR, "amr packet_size invalid\n");
         return AVERROR_INVALIDDATA;
     }
0b175caa
 
0cd08367
     D_IF_decode(s->state, buf, (short *)frame->data[0], _good_frame);
0eea2129
 
0cd08367
     *got_frame_ptr = 1;
0eea2129
 
0b175caa
     return packet_size;
 }
 
 static int amr_wb_decode_close(AVCodecContext *avctx)
 {
     AMRWBContext *s = avctx->priv_data;
 
     D_IF_exit(s->state);
     return 0;
 }
 
e7e2df27
 AVCodec ff_libopencore_amrwb_decoder = {
ec6402b7
     .name           = "libopencore_amrwb",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("OpenCORE AMR-WB (Adaptive Multi-Rate Wide-Band)"),
ec6402b7
     .type           = AVMEDIA_TYPE_AUDIO,
36ef5369
     .id             = AV_CODEC_ID_AMR_WB,
ec6402b7
     .priv_data_size = sizeof(AMRWBContext),
     .init           = amr_wb_decode_init,
     .close          = amr_wb_decode_close,
     .decode         = amr_wb_decode_frame,
def97856
     .capabilities   = AV_CODEC_CAP_DR1,
0b175caa
 };
 
8837f439
 #endif /* CONFIG_LIBOPENCORE_AMRWB_DECODER */