FFmpeg
libilbc.c
Go to the documentation of this file.
1 /*
2  * iLBC decoder/encoder stub
3  * Copyright (c) 2012 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include <ilbc.h>
23 
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
27 #include "avcodec.h"
28 #include "internal.h"
29 
30 #ifndef LIBILBC_VERSION_MAJOR
31 #define LIBILBC_VERSION_MAJOR 2
32 #endif
33 
34 static int get_mode(AVCodecContext *avctx)
35 {
36  if (avctx->block_align == 38)
37  return 20;
38  else if (avctx->block_align == 50)
39  return 30;
40  else if (avctx->bit_rate > 0)
41  return avctx->bit_rate <= 14000 ? 30 : 20;
42  else
43  return -1;
44 }
45 
46 typedef struct ILBCDecContext {
47  const AVClass *class;
48 #if LIBILBC_VERSION_MAJOR < 3
49  iLBC_Dec_Inst_t decoder;
50 #else
51  IlbcDecoder decoder;
52 #endif
53  int enhance;
55 
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 },
58  { NULL }
59 };
60 
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,
66 };
67 
69 {
70  ILBCDecContext *s = avctx->priv_data;
71  int mode;
72 
73  if ((mode = get_mode(avctx)) < 0) {
74  av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
75  return AVERROR(EINVAL);
76  }
77 
78  WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
79 
80  avctx->channels = 1;
82  avctx->sample_rate = 8000;
84 
85  return 0;
86 }
87 
88 static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
89  int *got_frame_ptr, AVPacket *avpkt)
90 {
91  const uint8_t *buf = avpkt->data;
92  int buf_size = avpkt->size;
93  ILBCDecContext *s = avctx->priv_data;
94  AVFrame *frame = data;
95  int ret;
96 
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",
100 #else
101  av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be "
102  "%"SIZE_SPECIFIER")\n",
103 #endif
104  buf_size, s->decoder.no_of_bytes);
105  return AVERROR_INVALIDDATA;
106  }
107 
108  frame->nb_samples = s->decoder.blockl;
109  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
110  return ret;
111 
112  WebRtcIlbcfix_DecodeImpl((int16_t *) frame->data[0], (const uint16_t *) buf, &s->decoder, 1);
113 
114  *got_frame_ptr = 1;
115 
116  return s->decoder.no_of_bytes;
117 }
118 
120  .name = "libilbc",
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),
128  .priv_class = &ilbc_dec_class,
129 };
130 
131 typedef struct ILBCEncContext {
132  const AVClass *class;
133 #if LIBILBC_VERSION_MAJOR < 3
134  iLBC_Enc_Inst_t encoder;
135 #else
136  IlbcEncoder encoder;
137 #endif
138  int mode;
140 
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 },
143  { NULL }
144 };
145 
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,
151 };
152 
154 {
155  ILBCEncContext *s = avctx->priv_data;
156  int mode;
157 
158  if (avctx->sample_rate != 8000) {
159  av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
160  return AVERROR(EINVAL);
161  }
162 
163  if (avctx->channels != 1) {
164  av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
165  return AVERROR(EINVAL);
166  }
167 
168  if ((mode = get_mode(avctx)) > 0)
169  s->mode = mode;
170  else
171  s->mode = s->mode != 30 ? 20 : 30;
172  WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
173 
174  avctx->block_align = s->encoder.no_of_bytes;
175  avctx->frame_size = s->encoder.blockl;
176 
177  return 0;
178 }
179 
180 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
181  const AVFrame *frame, int *got_packet_ptr)
182 {
183  ILBCEncContext *s = avctx->priv_data;
184  int ret;
185 
186  if ((ret = ff_alloc_packet2(avctx, avpkt, 50, 0)) < 0)
187  return ret;
188 
189  WebRtcIlbcfix_EncodeImpl((uint16_t *) avpkt->data, (const int16_t *) frame->data[0], &s->encoder);
190 
191  avpkt->size = s->encoder.no_of_bytes;
192  *got_packet_ptr = 1;
193  return 0;
194 }
195 
197  { "b", "0" },
198  { NULL }
199 };
200 
202  .name = "libilbc",
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),
208  .encode2 = ilbc_encode_frame,
209  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
211  .defaults = ilbc_encode_defaults,
212  .priv_class = &ilbc_enc_class,
213  .wrapper_name = "libbilbc",
214 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1118
AVCodec
AVCodec.
Definition: codec.h:197
ILBCEncContext
Definition: libilbc.c:131
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1149
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1098
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:953
ilbc_decode_init
static av_cold int ilbc_decode_init(AVCodecContext *avctx)
Definition: libilbc.c:68
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
ilbc_encode_init
static av_cold int ilbc_encode_init(AVCodecContext *avctx)
Definition: libilbc.c:153
AVOption
AVOption.
Definition: opt.h:248
data
const char data[16]
Definition: mxf.c:142
ilbc_dec_class
static const AVClass ilbc_dec_class
Definition: libilbc.c:61
ilbc_encode_defaults
static const AVCodecDefault ilbc_encode_defaults[]
Definition: libilbc.c:196
ilbc_decode_frame
static int ilbc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libilbc.c:88
ILBCDecContext::decoder
iLBC_Dec_Inst_t decoder
Definition: libilbc.c:49
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:181
av_cold
#define av_cold
Definition: attributes.h:90
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:278
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ilbc_enc_class
static const AVClass ilbc_enc_class
Definition: libilbc.c:146
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:280
AVCodecDefault
Definition: internal.h:207
ff_libilbc_decoder
const AVCodec ff_libilbc_decoder
Definition: libilbc.c:119
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
ff_libilbc_encoder
const AVCodec ff_libilbc_encoder
Definition: libilbc.c:201
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
ILBCEncContext::encoder
iLBC_Enc_Inst_t encoder
Definition: libilbc.c:134
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:551
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:104
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1638
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:366
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1106
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
ilbc_encode_frame
static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libilbc.c:180
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1099
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:279
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
ilbc_enc_options
static const AVOption ilbc_enc_options[]
Definition: libilbc.c:141
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1135
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
SIZE_SPECIFIER
#define SIZE_SPECIFIER
Definition: internal.h:193
AVCodecContext
main external API structure.
Definition: avcodec.h:501
channel_layout.h
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
ilbc_dec_options
static const AVOption ilbc_dec_options[]
Definition: libilbc.c:56
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:528
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:479
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ILBCDecContext::enhance
int enhance
Definition: libilbc.c:53
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
ILBCDecContext
Definition: libilbc.c:46
ILBCEncContext::mode
int mode
Definition: libilbc.c:138
get_mode
static int get_mode(AVCodecContext *avctx)
Definition: libilbc.c:34