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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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;
45  iLBC_Dec_Inst_t decoder;
46  int enhance;
48 
49 static const AVOption ilbc_dec_options[] = {
50  { "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 },
51  { NULL }
52 };
53 
54 static const AVClass ilbc_dec_class = {
56 };
57 
59 {
60  ILBCDecContext *s = avctx->priv_data;
61  int mode;
62 
63  if ((mode = get_mode(avctx)) < 0) {
64  av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
65  return AVERROR(EINVAL);
66  }
67 
68  WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
70  avctx->coded_frame = &s->frame;
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  int ret;
87 
88  if (s->decoder.no_of_bytes > buf_size) {
89  av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
90  buf_size, s->decoder.no_of_bytes);
91  return AVERROR_INVALIDDATA;
92  }
93 
94  s->frame.nb_samples = s->decoder.blockl;
95  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
96  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
97  return ret;
98  }
99 
100  WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) s->frame.data[0],
101  (const WebRtc_UWord16*) buf, &s->decoder, 1);
102 
103  *got_frame_ptr = 1;
104  *(AVFrame *)data = s->frame;
105 
106  return s->decoder.no_of_bytes;
107 }
108 
110  .name = "libilbc",
111  .type = AVMEDIA_TYPE_AUDIO,
112  .id = AV_CODEC_ID_ILBC,
113  .priv_data_size = sizeof(ILBCDecContext),
116  .capabilities = CODEC_CAP_DR1,
117  .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
118  .priv_class = &ilbc_dec_class,
119 };
120 
121 typedef struct ILBCEncContext {
122  const AVClass *class;
123  iLBC_Enc_Inst_t encoder;
124  int mode;
126 
127 static const AVOption ilbc_enc_options[] = {
128  { "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 },
129  { NULL }
130 };
131 
132 static const AVClass ilbc_enc_class = {
134 };
135 
137 {
138  ILBCEncContext *s = avctx->priv_data;
139  int mode;
140 
141  if (avctx->sample_rate != 8000) {
142  av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
143  return AVERROR(EINVAL);
144  }
145 
146  if (avctx->channels != 1) {
147  av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
148  return AVERROR(EINVAL);
149  }
150 
151  if ((mode = get_mode(avctx)) > 0)
152  s->mode = mode;
153  else
154  s->mode = s->mode != 30 ? 20 : 30;
155  WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
156 
157  avctx->block_align = s->encoder.no_of_bytes;
158  avctx->frame_size = s->encoder.blockl;
159 #if FF_API_OLD_ENCODE_AUDIO
160  avctx->coded_frame = avcodec_alloc_frame();
161  if (!avctx->coded_frame)
162  return AVERROR(ENOMEM);
163 #endif
164 
165  return 0;
166 }
167 
169 {
170 #if FF_API_OLD_ENCODE_AUDIO
171  av_freep(&avctx->coded_frame);
172 #endif
173  return 0;
174 }
175 
176 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
177  const AVFrame *frame, int *got_packet_ptr)
178 {
179  ILBCEncContext *s = avctx->priv_data;
180  int ret;
181 
182  if ((ret = ff_alloc_packet(avpkt, 50))) {
183  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
184  return ret;
185  }
186 
187  WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
188 
189  avpkt->size = s->encoder.no_of_bytes;
190  *got_packet_ptr = 1;
191  return 0;
192 }
193 
195  { "b", "0" },
196  { NULL }
197 };
198 
200  .name = "libilbc",
201  .type = AVMEDIA_TYPE_AUDIO,
202  .id = AV_CODEC_ID_ILBC,
203  .priv_data_size = sizeof(ILBCEncContext),
205  .encode2 = ilbc_encode_frame,
207  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
209  .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
210  .defaults = ilbc_encode_defaults,
211  .priv_class = &ilbc_enc_class,
212 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVOption.
Definition: opt.h:233
static int ilbc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libilbc.c:80
struct ILBCDecContext ILBCDecContext
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
static av_cold int ilbc_decode_init(AVCodecContext *avctx)
Definition: libilbc.c:58
int size
Definition: avcodec.h:916
struct ILBCEncContext ILBCEncContext
static int get_mode(AVCodecContext *avctx)
Definition: libilbc.c:30
signed 16 bits
Definition: samplefmt.h:52
AVCodec.
Definition: avcodec.h:2960
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2141
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
static const AVClass ilbc_dec_class
Definition: libilbc.c:54
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
uint8_t
AVCodec ff_libilbc_decoder
Definition: libilbc.c:109
AVOptions.
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static av_cold int ilbc_encode_init(AVCodecContext *avctx)
Definition: libilbc.c:136
static const AVCodecDefault ilbc_encode_defaults[]
Definition: libilbc.c:194
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
sample_fmts
Definition: avconv_filter.c:63
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
iLBC_Enc_Inst_t encoder
Definition: libilbc.c:123
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2165
static const AVClass ilbc_enc_class
Definition: libilbc.c:132
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:616
int bit_rate
the average bitrate
Definition: avcodec.h:1404
audio channel layout utility functions
AVCodec ff_libilbc_encoder
Definition: libilbc.c:199
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:878
LIBAVUTIL_VERSION_INT
Definition: eval.c:52
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2124
iLBC_Dec_Inst_t decoder
Definition: libilbc.c:45
NULL
Definition: eval.c:52
static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libilbc.c:176
external API header
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
int sample_rate
samples per second
Definition: avcodec.h:2104
av_default_item_name
Definition: dnxhdenc.c:43
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:602
Describe the class of an AVClass context structure.
Definition: log.h:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
AVFrame frame
Definition: libilbc.c:44
common internal api header.
common internal and external API header
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
void * priv_data
Definition: avcodec.h:1382
int channels
number of audio channels
Definition: avcodec.h:2105
static av_cold int ilbc_encode_close(AVCodecContext *avctx)
Definition: libilbc.c:168
static const AVOption ilbc_enc_options[]
Definition: libilbc.c:127
static const AVOption ilbc_dec_options[]
Definition: libilbc.c:49
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:898
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042