FFmpeg
Loading...
Searching...
No Matches
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
36static 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
48typedef 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
57
58static 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
63static 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 %zu)\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 .p.name = "libilbc",
121 CODEC_LONG_NAME("iLBC (Internet Low Bitrate Codec)"),
122 .p.type = AVMEDIA_TYPE_AUDIO,
123 .p.id = AV_CODEC_ID_ILBC,
124 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
125 .priv_data_size = sizeof(ILBCDecContext),
129 .p.priv_class = &ilbc_dec_class,
130};
131
132typedef 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
142static 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
147static 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->ch_layout.nb_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
181static 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 .p.name = "libilbc",
204 CODEC_LONG_NAME("iLBC (Internet Low Bitrate Codec)"),
205 .p.type = AVMEDIA_TYPE_AUDIO,
206 .p.id = AV_CODEC_ID_ILBC,
208 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
209 .priv_data_size = sizeof(ILBCEncContext),
213 .defaults = ilbc_encode_defaults,
214 .p.priv_class = &ilbc_enc_class,
215 .p.wrapper_name = "libbilbc",
216};
const FFCodec ff_libilbc_decoder
Definition libilbc.c:119
const FFCodec ff_libilbc_encoder
Definition libilbc.c:202
Libavcodec external API header.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_DECODE_CB(func)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
common internal and external API header
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
#define AV_OPT_FLAG_AUDIO_PARAM
Definition opt.h:356
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition opt.h:351
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#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:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_ILBC
Definition codec_id.h:512
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
static av_cold int ilbc_decode_init(AVCodecContext *avctx)
Definition ilbcdec.c:1442
static int ilbc_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition ilbcdec.c:1355
#define av_cold
Definition attributes.h:117
static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition libilbc.c:181
static const AVOption ilbc_enc_options[]
Definition libilbc.c:142
static av_cold int ilbc_encode_init(AVCodecContext *avctx)
Definition libilbc.c:154
static const AVOption ilbc_dec_options[]
Definition libilbc.c:58
static const AVClass ilbc_dec_class
Definition libilbc.c:63
static const FFCodecDefault ilbc_encode_defaults[]
Definition libilbc.c:197
static int get_mode(AVCodecContext *avctx)
Definition libilbc.c:36
static av_cold int ilbc_decode_init(AVCodecContext *avctx)
Definition libilbc.c:70
static const AVClass ilbc_enc_class
Definition libilbc.c:147
static int ilbc_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition libilbc.c:90
AVOptions.
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int sample_rate
samples per second
Definition avcodec.h:1040
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition avcodec.h:1075
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
iLBC_Dec_Inst_t decoder
Definition libilbc.c:51
iLBC_Enc_Inst_t encoder
Definition libilbc.c:135
Definition swscale.c:71
#define av_log(a,...)