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 static int get_mode(AVCodecContext *avctx)
31 {
32  if (avctx->block_align == 38)
33  return 20;
34  else if (avctx->block_align == 50)
35  return 30;
36  else if (avctx->bit_rate > 0)
37  return avctx->bit_rate <= 14000 ? 30 : 20;
38  else
39  return -1;
40 }
41 
42 typedef struct ILBCDecContext {
43  const AVClass *class;
44  iLBC_Dec_Inst_t decoder;
45  int enhance;
47 
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 },
50  { NULL }
51 };
52 
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,
58 };
59 
61 {
62  ILBCDecContext *s = avctx->priv_data;
63  int mode;
64 
65  if ((mode = get_mode(avctx)) < 0) {
66  av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
67  return AVERROR(EINVAL);
68  }
69 
70  WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
71 
72  avctx->channels = 1;
74  avctx->sample_rate = 8000;
76 
77  return 0;
78 }
79 
80 static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
81  int *got_frame_ptr, AVPacket *avpkt)
82 {
83  const uint8_t *buf = avpkt->data;
84  int buf_size = avpkt->size;
85  ILBCDecContext *s = avctx->priv_data;
86  AVFrame *frame = data;
87  int ret;
88 
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;
93  }
94 
95  frame->nb_samples = s->decoder.blockl;
96  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
97  return ret;
98 
99  WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) frame->data[0],
100  (const WebRtc_UWord16*) buf, &s->decoder, 1);
101 
102  *got_frame_ptr = 1;
103 
104  return s->decoder.no_of_bytes;
105 }
106 
108  .name = "libilbc",
109  .type = AVMEDIA_TYPE_AUDIO,
110  .id = AV_CODEC_ID_ILBC,
111  .priv_data_size = sizeof(ILBCDecContext),
114  .capabilities = CODEC_CAP_DR1,
115  .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
116  .priv_class = &ilbc_dec_class,
117 };
118 
119 typedef struct ILBCEncContext {
120  const AVClass *class;
121  iLBC_Enc_Inst_t encoder;
122  int mode;
124 
125 static const AVOption ilbc_enc_options[] = {
126  { "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 },
127  { NULL }
128 };
129 
130 static const AVClass ilbc_enc_class = {
131  .class_name = "libilbc",
132  .item_name = av_default_item_name,
133  .option = ilbc_enc_options,
134  .version = LIBAVUTIL_VERSION_INT,
135 };
136 
138 {
139  ILBCEncContext *s = avctx->priv_data;
140  int mode;
141 
142  if (avctx->sample_rate != 8000) {
143  av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
144  return AVERROR(EINVAL);
145  }
146 
147  if (avctx->channels != 1) {
148  av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
149  return AVERROR(EINVAL);
150  }
151 
152  if ((mode = get_mode(avctx)) > 0)
153  s->mode = mode;
154  else
155  s->mode = s->mode != 30 ? 20 : 30;
156  WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
157 
158  avctx->block_align = s->encoder.no_of_bytes;
159  avctx->frame_size = s->encoder.blockl;
160 
161  return 0;
162 }
163 
164 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
165  const AVFrame *frame, int *got_packet_ptr)
166 {
167  ILBCEncContext *s = avctx->priv_data;
168  int ret;
169 
170  if ((ret = ff_alloc_packet2(avctx, avpkt, 50)) < 0)
171  return ret;
172 
173  WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
174 
175  avpkt->size = s->encoder.no_of_bytes;
176  *got_packet_ptr = 1;
177  return 0;
178 }
179 
181  { "b", "0" },
182  { NULL }
183 };
184 
186  .name = "libilbc",
187  .type = AVMEDIA_TYPE_AUDIO,
188  .id = AV_CODEC_ID_ILBC,
189  .priv_data_size = sizeof(ILBCEncContext),
191  .encode2 = ilbc_encode_frame,
192  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
194  .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
195  .defaults = ilbc_encode_defaults,
196  .priv_class = &ilbc_enc_class,
197 };
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
static int ilbc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libilbc.c:80
av_default_item_name
struct ILBCDecContext ILBCDecContext
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static av_cold int ilbc_decode_init(AVCodecContext *avctx)
Definition: libilbc.c:60
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:284
struct ILBCEncContext ILBCEncContext
static int get_mode(AVCodecContext *avctx)
Definition: libilbc.c:30
signed 16 bits
Definition: samplefmt.h:52
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
static const AVClass ilbc_dec_class
Definition: libilbc.c:53
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:55
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
AVCodec ff_libilbc_decoder
Definition: libilbc.c:107
#define av_cold
Definition: attributes.h:78
mode
Definition: f_perms.c:27
AVOptions.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
static av_cold int ilbc_encode_init(AVCodecContext *avctx)
Definition: libilbc.c:137
static const AVCodecDefault ilbc_encode_defaults[]
Definition: libilbc.c:180
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:281
frame
Definition: stft.m:14
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
iLBC_Enc_Inst_t encoder
Definition: libilbc.c:121
uint64_t channel_layout
Audio channel layout.
static const AVClass ilbc_enc_class
Definition: libilbc.c:130
int bit_rate
the average bitrate
audio channel layout utility functions
AVCodec ff_libilbc_encoder
Definition: libilbc.c:185
ret
Definition: avfilter.c:821
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int frame_size
Number of samples per channel in an audio frame.
iLBC_Dec_Inst_t decoder
Definition: libilbc.c:44
NULL
Definition: eval.c:55
static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libilbc.c:164
int sample_rate
samples per second
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
Describe the class of an AVClass context structure.
Definition: log.h:50
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
common internal and external API header
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
int channels
number of audio channels
static const AVOption ilbc_enc_options[]
Definition: libilbc.c:125
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
static const AVOption ilbc_dec_options[]
Definition: libilbc.c:48
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127