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 "encode.h"
29 #include "internal.h"
30 
31 #ifndef LIBILBC_VERSION_MAJOR
32 #define LIBILBC_VERSION_MAJOR 2
33 #endif
34 
35 static int get_mode(AVCodecContext *avctx)
36 {
37  if (avctx->block_align == 38)
38  return 20;
39  else if (avctx->block_align == 50)
40  return 30;
41  else if (avctx->bit_rate > 0)
42  return avctx->bit_rate <= 14000 ? 30 : 20;
43  else
44  return -1;
45 }
46 
47 typedef struct ILBCDecContext {
48  const AVClass *class;
49 #if LIBILBC_VERSION_MAJOR < 3
50  iLBC_Dec_Inst_t decoder;
51 #else
52  IlbcDecoder decoder;
53 #endif
54  int enhance;
56 
57 static const AVOption ilbc_dec_options[] = {
58  { "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 },
59  { NULL }
60 };
61 
62 static const AVClass ilbc_dec_class = {
63  .class_name = "libilbc",
64  .item_name = av_default_item_name,
65  .option = ilbc_dec_options,
66  .version = LIBAVUTIL_VERSION_INT,
67 };
68 
70 {
71  ILBCDecContext *s = avctx->priv_data;
72  int mode;
73 
74  if ((mode = get_mode(avctx)) < 0) {
75  av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
76  return AVERROR(EINVAL);
77  }
78 
79  WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
80 
81  avctx->channels = 1;
83  avctx->sample_rate = 8000;
85 
86  return 0;
87 }
88 
89 static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
90  int *got_frame_ptr, AVPacket *avpkt)
91 {
92  const uint8_t *buf = avpkt->data;
93  int buf_size = avpkt->size;
94  ILBCDecContext *s = avctx->priv_data;
95  AVFrame *frame = data;
96  int ret;
97 
98  if (s->decoder.no_of_bytes > buf_size) {
99 #if LIBILBC_VERSION_MAJOR < 3
100  av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
101 #else
102  av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be "
103  "%"SIZE_SPECIFIER")\n",
104 #endif
105  buf_size, s->decoder.no_of_bytes);
106  return AVERROR_INVALIDDATA;
107  }
108 
109  frame->nb_samples = s->decoder.blockl;
110  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
111  return ret;
112 
113  WebRtcIlbcfix_DecodeImpl((int16_t *) frame->data[0], (const uint16_t *) buf, &s->decoder, 1);
114 
115  *got_frame_ptr = 1;
116 
117  return s->decoder.no_of_bytes;
118 }
119 
121  .name = "libilbc",
122  .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
123  .type = AVMEDIA_TYPE_AUDIO,
124  .id = AV_CODEC_ID_ILBC,
125  .priv_data_size = sizeof(ILBCDecContext),
129  .priv_class = &ilbc_dec_class,
130 };
131 
132 typedef struct ILBCEncContext {
133  const AVClass *class;
134 #if LIBILBC_VERSION_MAJOR < 3
135  iLBC_Enc_Inst_t encoder;
136 #else
137  IlbcEncoder encoder;
138 #endif
139  int mode;
141 
142 static const AVOption ilbc_enc_options[] = {
143  { "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 },
144  { NULL }
145 };
146 
147 static const AVClass ilbc_enc_class = {
148  .class_name = "libilbc",
149  .item_name = av_default_item_name,
150  .option = ilbc_enc_options,
151  .version = LIBAVUTIL_VERSION_INT,
152 };
153 
155 {
156  ILBCEncContext *s = avctx->priv_data;
157  int mode;
158 
159  if (avctx->sample_rate != 8000) {
160  av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
161  return AVERROR(EINVAL);
162  }
163 
164  if (avctx->channels != 1) {
165  av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
166  return AVERROR(EINVAL);
167  }
168 
169  if ((mode = get_mode(avctx)) > 0)
170  s->mode = mode;
171  else
172  s->mode = s->mode != 30 ? 20 : 30;
173  WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
174 
175  avctx->block_align = s->encoder.no_of_bytes;
176  avctx->frame_size = s->encoder.blockl;
177 
178  return 0;
179 }
180 
181 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
182  const AVFrame *frame, int *got_packet_ptr)
183 {
184  ILBCEncContext *s = avctx->priv_data;
185  int ret;
186 
187  if ((ret = ff_alloc_packet(avctx, avpkt, 50)) < 0)
188  return ret;
189 
190  WebRtcIlbcfix_EncodeImpl((uint16_t *) avpkt->data, (const int16_t *) frame->data[0], &s->encoder);
191 
192  avpkt->size = s->encoder.no_of_bytes;
193  *got_packet_ptr = 1;
194  return 0;
195 }
196 
198  { "b", "0" },
199  { NULL }
200 };
201 
203  .name = "libilbc",
204  .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
205  .type = AVMEDIA_TYPE_AUDIO,
206  .id = AV_CODEC_ID_ILBC,
207  .priv_data_size = sizeof(ILBCEncContext),
209  .encode2 = ilbc_encode_frame,
210  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
212  .defaults = ilbc_encode_defaults,
213  .priv_class = &ilbc_enc_class,
214  .wrapper_name = "libbilbc",
215 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1012
AVCodec
AVCodec.
Definition: codec.h:202
ILBCEncContext
Definition: libilbc.c:132
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:1043
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:992
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
ilbc_decode_init
static av_cold int ilbc_decode_init(AVCodecContext *avctx)
Definition: libilbc.c:69
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:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
ilbc_encode_init
static av_cold int ilbc_encode_init(AVCodecContext *avctx)
Definition: libilbc.c:154
AVOption
AVOption.
Definition: opt.h:247
encode.h
data
const char data[16]
Definition: mxf.c:143
ilbc_dec_class
static const AVClass ilbc_dec_class
Definition: libilbc.c:62
ilbc_encode_defaults
static const AVCodecDefault ilbc_encode_defaults[]
Definition: libilbc.c:197
ilbc_decode_frame
static int ilbc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libilbc.c:89
ILBCDecContext::decoder
iLBC_Dec_Inst_t decoder
Definition: libilbc.c:50
init
static int init
Definition: av_tx.c:47
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
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:277
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ilbc_enc_class
static const AVClass ilbc_enc_class
Definition: libilbc.c:147
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:279
AVCodecDefault
Definition: internal.h:215
ff_libilbc_decoder
const AVCodec ff_libilbc_decoder
Definition: libilbc.c:120
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
ff_libilbc_encoder
const AVCodec ff_libilbc_encoder
Definition: libilbc.c:202
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ILBCEncContext::encoder
iLBC_Enc_Inst_t encoder
Definition: libilbc.c:135
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:433
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:109
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1652
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:374
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:1000
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:181
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
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:278
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:209
ilbc_enc_options
static const AVOption ilbc_enc_options[]
Definition: libilbc.c:142
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:1029
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:71
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:383
channel_layout.h
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
ilbc_dec_options
static const AVOption ilbc_dec_options[]
Definition: libilbc.c:57
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:482
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:61
ILBCDecContext::enhance
int enhance
Definition: libilbc.c:54
ILBCDecContext
Definition: libilbc.c:47
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:34
ILBCEncContext::mode
int mode
Definition: libilbc.c:139
get_mode
static int get_mode(AVCodecContext *avctx)
Definition: libilbc.c:35