2 * iLBC decoder/encoder stub
3 * Copyright (c) 2012 Martin Storsjo
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
30 #ifndef LIBILBC_VERSION_MAJOR
31 #define LIBILBC_VERSION_MAJOR 2
34 static int get_mode(AVCodecContext *avctx)
36 if (avctx->block_align == 38)
38 else if (avctx->block_align == 50)
40 else if (avctx->bit_rate > 0)
41 return avctx->bit_rate <= 14000 ? 30 : 20;
46 typedef struct ILBCDecContext {
48 #if LIBILBC_VERSION_MAJOR < 3
49 iLBC_Dec_Inst_t decoder;
56 static const AVOption ilbc_dec_options[] = {
57 { "enhance", "Enhance the decoded audio (adds delay)", offsetof(ILBCDecContext, enhance), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM },
61 static const AVClass ilbc_dec_class = {
62 .class_name = "libilbc",
63 .item_name = av_default_item_name,
64 .option = ilbc_dec_options,
65 .version = LIBAVUTIL_VERSION_INT,
68 static av_cold int ilbc_decode_init(AVCodecContext *avctx)
70 ILBCDecContext *s = avctx->priv_data;
73 if ((mode = get_mode(avctx)) < 0) {
74 av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
75 return AVERROR(EINVAL);
78 WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
81 avctx->channel_layout = AV_CH_LAYOUT_MONO;
82 avctx->sample_rate = 8000;
83 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
88 static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
89 int *got_frame_ptr, AVPacket *avpkt)
91 const uint8_t *buf = avpkt->data;
92 int buf_size = avpkt->size;
93 ILBCDecContext *s = avctx->priv_data;
94 AVFrame *frame = data;
97 if (s->decoder.no_of_bytes > buf_size) {
98 #if LIBILBC_VERSION_MAJOR < 3
99 av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
101 av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be "
102 "%"SIZE_SPECIFIER")\n",
104 buf_size, s->decoder.no_of_bytes);
105 return AVERROR_INVALIDDATA;
108 frame->nb_samples = s->decoder.blockl;
109 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
112 WebRtcIlbcfix_DecodeImpl((int16_t *) frame->data[0], (const uint16_t *) buf, &s->decoder, 1);
116 return s->decoder.no_of_bytes;
119 const AVCodec ff_libilbc_decoder = {
121 .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
122 .type = AVMEDIA_TYPE_AUDIO,
123 .id = AV_CODEC_ID_ILBC,
124 .priv_data_size = sizeof(ILBCDecContext),
125 .init = ilbc_decode_init,
126 .decode = ilbc_decode_frame,
127 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
128 .priv_class = &ilbc_dec_class,
131 typedef struct ILBCEncContext {
132 const AVClass *class;
133 #if LIBILBC_VERSION_MAJOR < 3
134 iLBC_Enc_Inst_t encoder;
141 static const AVOption ilbc_enc_options[] = {
142 { "mode", "iLBC mode (20 or 30 ms frames)", offsetof(ILBCEncContext, mode), AV_OPT_TYPE_INT, { .i64 = 20 }, 20, 30, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
146 static const AVClass ilbc_enc_class = {
147 .class_name = "libilbc",
148 .item_name = av_default_item_name,
149 .option = ilbc_enc_options,
150 .version = LIBAVUTIL_VERSION_INT,
153 static av_cold int ilbc_encode_init(AVCodecContext *avctx)
155 ILBCEncContext *s = avctx->priv_data;
158 if (avctx->sample_rate != 8000) {
159 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
160 return AVERROR(EINVAL);
163 if (avctx->channels != 1) {
164 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
165 return AVERROR(EINVAL);
168 if ((mode = get_mode(avctx)) > 0)
171 s->mode = s->mode != 30 ? 20 : 30;
172 WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
174 avctx->block_align = s->encoder.no_of_bytes;
175 avctx->frame_size = s->encoder.blockl;
180 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
181 const AVFrame *frame, int *got_packet_ptr)
183 ILBCEncContext *s = avctx->priv_data;
186 if ((ret = ff_alloc_packet2(avctx, avpkt, 50, 0)) < 0)
189 WebRtcIlbcfix_EncodeImpl((uint16_t *) avpkt->data, (const int16_t *) frame->data[0], &s->encoder);
191 avpkt->size = s->encoder.no_of_bytes;
196 static const AVCodecDefault ilbc_encode_defaults[] = {
201 const AVCodec ff_libilbc_encoder = {
203 .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
204 .type = AVMEDIA_TYPE_AUDIO,
205 .id = AV_CODEC_ID_ILBC,
206 .priv_data_size = sizeof(ILBCEncContext),
207 .init = ilbc_encode_init,
208 .encode2 = ilbc_encode_frame,
209 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
210 AV_SAMPLE_FMT_NONE },
211 .defaults = ilbc_encode_defaults,
212 .priv_class = &ilbc_enc_class,
213 .wrapper_name = "libbilbc",