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 "codec_internal.h"
29 #include "decode.h"
30 #include "encode.h"
31 
32 #ifndef LIBILBC_VERSION_MAJOR
33 #define LIBILBC_VERSION_MAJOR 2
34 #endif
35 
36 static int get_mode(AVCodecContext *avctx)
37 {
38  if (avctx->block_align == 38)
39  return 20;
40  else if (avctx->block_align == 50)
41  return 30;
42  else if (avctx->bit_rate > 0)
43  return avctx->bit_rate <= 14000 ? 30 : 20;
44  else
45  return -1;
46 }
47 
48 typedef struct ILBCDecContext {
49  const AVClass *class;
50 #if LIBILBC_VERSION_MAJOR < 3
51  iLBC_Dec_Inst_t decoder;
52 #else
53  IlbcDecoder decoder;
54 #endif
55  int enhance;
57 
58 static const AVOption ilbc_dec_options[] = {
59  { "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 },
60  { NULL }
61 };
62 
63 static const AVClass ilbc_dec_class = {
64  .class_name = "libilbc",
65  .item_name = av_default_item_name,
66  .option = ilbc_dec_options,
67  .version = LIBAVUTIL_VERSION_INT,
68 };
69 
71 {
72  ILBCDecContext *s = avctx->priv_data;
73  int mode;
74 
75  if ((mode = get_mode(avctx)) < 0) {
76  av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
77  return AVERROR(EINVAL);
78  }
79 
80  WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
81 
84  avctx->sample_rate = 8000;
86 
87  return 0;
88 }
89 
91  int *got_frame_ptr, AVPacket *avpkt)
92 {
93  const uint8_t *buf = avpkt->data;
94  int buf_size = avpkt->size;
95  ILBCDecContext *s = avctx->priv_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  .p.name = "libilbc",
122  CODEC_LONG_NAME("iLBC (Internet Low Bitrate Codec)"),
123  .p.type = AVMEDIA_TYPE_AUDIO,
124  .p.id = AV_CODEC_ID_ILBC,
125  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
126  .priv_data_size = sizeof(ILBCDecContext),
129  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
130  .p.priv_class = &ilbc_dec_class,
131 };
132 
133 typedef struct ILBCEncContext {
134  const AVClass *class;
135 #if LIBILBC_VERSION_MAJOR < 3
136  iLBC_Enc_Inst_t encoder;
137 #else
138  IlbcEncoder encoder;
139 #endif
140  int mode;
142 
143 static const AVOption ilbc_enc_options[] = {
144  { "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 },
145  { NULL }
146 };
147 
148 static const AVClass ilbc_enc_class = {
149  .class_name = "libilbc",
150  .item_name = av_default_item_name,
151  .option = ilbc_enc_options,
152  .version = LIBAVUTIL_VERSION_INT,
153 };
154 
156 {
157  ILBCEncContext *s = avctx->priv_data;
158  int mode;
159 
160  if (avctx->sample_rate != 8000) {
161  av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
162  return AVERROR(EINVAL);
163  }
164 
165  if (avctx->ch_layout.nb_channels != 1) {
166  av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
167  return AVERROR(EINVAL);
168  }
169 
170  if ((mode = get_mode(avctx)) > 0)
171  s->mode = mode;
172  else
173  s->mode = s->mode != 30 ? 20 : 30;
174  WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
175 
176  avctx->block_align = s->encoder.no_of_bytes;
177  avctx->frame_size = s->encoder.blockl;
178 
179  return 0;
180 }
181 
182 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
183  const AVFrame *frame, int *got_packet_ptr)
184 {
185  ILBCEncContext *s = avctx->priv_data;
186  int ret;
187 
188  if ((ret = ff_alloc_packet(avctx, avpkt, 50)) < 0)
189  return ret;
190 
191  WebRtcIlbcfix_EncodeImpl((uint16_t *) avpkt->data, (const int16_t *) frame->data[0], &s->encoder);
192 
193  avpkt->size = s->encoder.no_of_bytes;
194  *got_packet_ptr = 1;
195  return 0;
196 }
197 
199  { "b", "0" },
200  { NULL }
201 };
202 
204  .p.name = "libilbc",
205  CODEC_LONG_NAME("iLBC (Internet Low Bitrate Codec)"),
206  .p.type = AVMEDIA_TYPE_AUDIO,
207  .p.id = AV_CODEC_ID_ILBC,
209  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
210  .priv_data_size = sizeof(ILBCEncContext),
213  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
215  .defaults = ilbc_encode_defaults,
216  .p.priv_class = &ilbc_enc_class,
217  .p.wrapper_name = "libbilbc",
218 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1096
ILBCEncContext
Definition: libilbc.c:133
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::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1068
ilbc_decode_init
static av_cold int ilbc_decode_init(AVCodecContext *avctx)
Definition: libilbc.c:70
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVPacket::data
uint8_t * data
Definition: packet.h:522
ilbc_encode_init
static av_cold int ilbc_encode_init(AVCodecContext *avctx)
Definition: libilbc.c:155
AVOption
AVOption.
Definition: opt.h:251
encode.h
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:34
FFCodec
Definition: codec_internal.h:127
ilbc_dec_class
static const AVClass ilbc_dec_class
Definition: libilbc.c:63
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:323
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
ILBCDecContext::decoder
iLBC_Dec_Inst_t decoder
Definition: libilbc.c:51
FFCodecDefault
Definition: codec_internal.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2111
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:315
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
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
s
#define s(width, name)
Definition: cbs_vp9.c:198
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:281
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
decode.h
ilbc_enc_class
static const AVClass ilbc_enc_class
Definition: libilbc.c:148
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:283
frame
static AVFrame * frame
Definition: demux_decode.c:54
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ILBCEncContext::encoder
iLBC_Enc_Inst_t encoder
Definition: libilbc.c:136
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:495
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
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:106
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1618
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
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:523
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:313
codec_internal.h
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1084
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
ilbc_encode_defaults
static const FFCodecDefault ilbc_encode_defaults[]
Definition: libilbc.c:198
ilbc_encode_frame
static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libilbc.c:182
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
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:282
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
ilbc_enc_options
static const AVOption ilbc_enc_options[]
Definition: libilbc.c:143
avcodec.h
ilbc_decode_frame
static int ilbc_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: libilbc.c:90
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:1117
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
SIZE_SPECIFIER
#define SIZE_SPECIFIER
Definition: internal.h:141
AVCodecContext
main external API structure.
Definition: avcodec.h:445
channel_layout.h
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:662
ff_libilbc_decoder
const FFCodec ff_libilbc_decoder
Definition: libilbc.c:120
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:388
ilbc_dec_options
static const AVOption ilbc_dec_options[]
Definition: libilbc.c:58
ff_libilbc_encoder
const FFCodec ff_libilbc_encoder
Definition: libilbc.c:203
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:502
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ILBCDecContext::enhance
int enhance
Definition: libilbc.c:55
ILBCDecContext
Definition: libilbc.c:48
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:61
ILBCEncContext::mode
int mode
Definition: libilbc.c:140
get_mode
static int get_mode(AVCodecContext *avctx)
Definition: libilbc.c:36