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
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
29 static int get_mode(AVCodecContext *avctx)
31 if (avctx->block_align == 38)
33 else if (avctx->block_align == 50)
35 else if (avctx->bit_rate > 0)
36 return avctx->bit_rate <= 14000 ? 30 : 20;
41 typedef struct ILBCDecContext {
44 iLBC_Dec_Inst_t decoder;
48 static const AVOption ilbc_dec_options[] = {
49 { "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 },
53 static const AVClass ilbc_dec_class = {
54 .class_name = "libilbc",
55 .item_name = av_default_item_name,
56 .option = ilbc_dec_options,
57 .version = LIBAVUTIL_VERSION_INT,
60 static av_cold int ilbc_decode_init(AVCodecContext *avctx)
62 ILBCDecContext *s = avctx->priv_data;
65 if ((mode = get_mode(avctx)) < 0) {
66 av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
67 return AVERROR(EINVAL);
70 WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
71 avcodec_get_frame_defaults(&s->frame);
72 avctx->coded_frame = &s->frame;
75 avctx->sample_rate = 8000;
76 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
81 static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
82 int *got_frame_ptr, AVPacket *avpkt)
84 const uint8_t *buf = avpkt->data;
85 int buf_size = avpkt->size;
86 ILBCDecContext *s = avctx->priv_data;
89 if (s->decoder.no_of_bytes > buf_size) {
90 av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
91 buf_size, s->decoder.no_of_bytes);
92 return AVERROR_INVALIDDATA;
95 s->frame.nb_samples = s->decoder.blockl;
96 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
97 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
101 WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) s->frame.data[0],
102 (const WebRtc_UWord16*) buf, &s->decoder, 1);
105 *(AVFrame *)data = s->frame;
107 return s->decoder.no_of_bytes;
110 AVCodec ff_libilbc_decoder = {
112 .type = AVMEDIA_TYPE_AUDIO,
113 .id = AV_CODEC_ID_ILBC,
114 .priv_data_size = sizeof(ILBCDecContext),
115 .init = ilbc_decode_init,
116 .decode = ilbc_decode_frame,
117 .capabilities = CODEC_CAP_DR1,
118 .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
119 .priv_class = &ilbc_dec_class,
122 typedef struct ILBCEncContext {
123 const AVClass *class;
124 iLBC_Enc_Inst_t encoder;
128 static const AVOption ilbc_enc_options[] = {
129 { "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 },
133 static const AVClass ilbc_enc_class = {
134 .class_name = "libilbc",
135 .item_name = av_default_item_name,
136 .option = ilbc_enc_options,
137 .version = LIBAVUTIL_VERSION_INT,
140 static av_cold int ilbc_encode_init(AVCodecContext *avctx)
142 ILBCEncContext *s = avctx->priv_data;
145 if (avctx->sample_rate != 8000) {
146 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
147 return AVERROR(EINVAL);
150 if (avctx->channels != 1) {
151 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
152 return AVERROR(EINVAL);
155 if ((mode = get_mode(avctx)) > 0)
158 s->mode = s->mode != 30 ? 20 : 30;
159 WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
161 avctx->block_align = s->encoder.no_of_bytes;
162 avctx->frame_size = s->encoder.blockl;
163 #if FF_API_OLD_ENCODE_AUDIO
164 avctx->coded_frame = avcodec_alloc_frame();
165 if (!avctx->coded_frame)
166 return AVERROR(ENOMEM);
172 static av_cold int ilbc_encode_close(AVCodecContext *avctx)
174 #if FF_API_OLD_ENCODE_AUDIO
175 av_freep(&avctx->coded_frame);
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_packet(avpkt, 50))) {
187 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
191 WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
193 avpkt->size = s->encoder.no_of_bytes;
198 static const AVCodecDefault ilbc_encode_defaults[] = {
203 AVCodec ff_libilbc_encoder = {
205 .type = AVMEDIA_TYPE_AUDIO,
206 .id = AV_CODEC_ID_ILBC,
207 .priv_data_size = sizeof(ILBCEncContext),
208 .init = ilbc_encode_init,
209 .encode2 = ilbc_encode_frame,
210 .close = ilbc_encode_close,
211 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
212 AV_SAMPLE_FMT_NONE },
213 .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
214 .defaults = ilbc_encode_defaults,
215 .priv_class = &ilbc_enc_class,