FFmpeg: libavcodec/libilbc.c Source File

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 
24 #include "libavutil/channel_layout.h"
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;
46 } ILBCDecContext;
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 
60  static av_cold int ilbc_decode_init(AVCodecContext *avctx)
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;
73  avctx->channel_layout = AV_CH_LAYOUT_MONO;
74  avctx->sample_rate = 8000;
75  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
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((int16_t *) frame->data[0], (const uint16_t *) buf, &s->decoder, 1);
100 
101  *got_frame_ptr = 1;
102 
103  return s->decoder.no_of_bytes;
104 }
105 
106  AVCodec ff_libilbc_decoder = {
107  .name = "libilbc",
108  .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
109  .type = AVMEDIA_TYPE_AUDIO,
110  .id = AV_CODEC_ID_ILBC,
111  .priv_data_size = sizeof(ILBCDecContext),
112  .init = ilbc_decode_init,
113  .decode = ilbc_decode_frame,
114  .capabilities = AV_CODEC_CAP_DR1,
115  .priv_class = &ilbc_dec_class,
116 };
117 
118  typedef struct ILBCEncContext {
119   const AVClass *class;
120   iLBC_Enc_Inst_t encoder;
121   int mode;
122 } ILBCEncContext;
123 
124  static const AVOption ilbc_enc_options[] = {
125  { "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 },
126  { NULL }
127 };
128 
129  static const AVClass ilbc_enc_class = {
130  .class_name = "libilbc",
131  .item_name = av_default_item_name,
132  .option = ilbc_enc_options,
133  .version = LIBAVUTIL_VERSION_INT,
134 };
135 
136  static av_cold int ilbc_encode_init(AVCodecContext *avctx)
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 
160  return 0;
161 }
162 
163  static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
164  const AVFrame *frame, int *got_packet_ptr)
165 {
166  ILBCEncContext *s = avctx->priv_data;
167  int ret;
168 
169  if ((ret = ff_alloc_packet2(avctx, avpkt, 50, 0)) < 0)
170  return ret;
171 
172  WebRtcIlbcfix_EncodeImpl((uint16_t *) avpkt->data, (const int16_t *) frame->data[0], &s->encoder);
173 
174  avpkt->size = s->encoder.no_of_bytes;
175  *got_packet_ptr = 1;
176  return 0;
177 }
178 
179  static const AVCodecDefault ilbc_encode_defaults[] = {
180  { "b", "0" },
181  { NULL }
182 };
183 
184  AVCodec ff_libilbc_encoder = {
185  .name = "libilbc",
186  .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
187  .type = AVMEDIA_TYPE_AUDIO,
188  .id = AV_CODEC_ID_ILBC,
189  .priv_data_size = sizeof(ILBCEncContext),
190  .init = ilbc_encode_init,
191  .encode2 = ilbc_encode_frame,
192  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
193  AV_SAMPLE_FMT_NONE },
194  .defaults = ilbc_encode_defaults,
195  .priv_class = &ilbc_enc_class,
196 };
NULL
#define NULL
Definition: coverity.c:32
s
const char * s
Definition: avisynth_c.h:631
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
AVOption
AVOption.
Definition: opt.h:245
data
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
ilbc_decode_frame
static int ilbc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libilbc.c:80
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1597
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
ilbc_decode_init
static av_cold int ilbc_decode_init(AVCodecContext *avctx)
Definition: libilbc.c:60
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:280
AVPacket::size
int size
Definition: avcodec.h:1468
get_mode
static int get_mode(AVCodecContext *avctx)
Definition: libilbc.c:30
ILBCDecContext::enhance
int enhance
Definition: libilbc.c:45
AVCodec
AVCodec.
Definition: avcodec.h:3392
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:2324
ilbc_dec_class
static const AVClass ilbc_dec_class
Definition: libilbc.c:53
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:72
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2295
uint8_t
uint8_t
Definition: audio_convert.c:194
ff_libilbc_decoder
AVCodec ff_libilbc_decoder
Definition: libilbc.c:106
av_cold
#define av_cold
Definition: attributes.h:82
mode
mode
Definition: f_perms.c:27
opt.h
AVOptions.
frame
static AVFrame * frame
Definition: demuxing_decoding.c:53
AVPacket::data
uint8_t * data
Definition: avcodec.h:1467
ilbc_encode_init
static av_cold int ilbc_encode_init(AVCodecContext *avctx)
Definition: libilbc.c:136
ilbc_encode_defaults
static const AVCodecDefault ilbc_encode_defaults[]
Definition: libilbc.c:179
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
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:275
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
av_default_item_name
Definition: libopenh264enc.c:68
AVERROR
#define AVERROR(e)
Definition: error.h:43
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:176
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
ILBCEncContext::encoder
iLBC_Enc_Inst_t encoder
Definition: libilbc.c:120
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2338
ilbc_enc_class
static const AVClass ilbc_enc_class
Definition: libilbc.c:129
channel_layout.h
audio channel layout utility functions
ff_libilbc_encoder
AVCodec ff_libilbc_encoder
Definition: libilbc.c:184
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2307
ILBCDecContext::decoder
iLBC_Dec_Inst_t decoder
Definition: libilbc.c:44
ilbc_encode_frame
static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libilbc.c:163
avcodec.h
Libavcodec external API header.
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2287
AVCodecContext
main external API structure.
Definition: avcodec.h:1532
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:894
buf
void * buf
Definition: avisynth_c.h:553
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
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:276
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1621
ILBCEncContext::mode
int mode
Definition: libilbc.c:121
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
decode
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:572
internal.h
common internal api header.
common.h
common internal and external API header
AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:62
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1574
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2288
ilbc_enc_options
static const AVOption ilbc_enc_options[]
Definition: libilbc.c:124
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
ilbc_dec_options
static const AVOption ilbc_dec_options[]
Definition: libilbc.c:48
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1444
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:235
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:856

Generated on Mon Feb 15 2016 15:20:39 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /