]> git.sesse.net Git - ffmpeg/blob - libavcodec/libilbc.c
lavc: add a null bitstream filter
[ffmpeg] / libavcodec / libilbc.c
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
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     "libilbc", av_default_item_name, ilbc_dec_options, LIBAVUTIL_VERSION_INT
55 };
56
57 static av_cold int ilbc_decode_init(AVCodecContext *avctx)
58 {
59     ILBCDecContext *s  = avctx->priv_data;
60     int mode;
61
62     if ((mode = get_mode(avctx)) < 0) {
63         av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
64         return AVERROR(EINVAL);
65     }
66
67     WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
68
69     avctx->channels       = 1;
70     avctx->channel_layout = AV_CH_LAYOUT_MONO;
71     avctx->sample_rate    = 8000;
72     avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
73
74     return 0;
75 }
76
77 static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
78                              int *got_frame_ptr, AVPacket *avpkt)
79 {
80     const uint8_t *buf = avpkt->data;
81     int buf_size       = avpkt->size;
82     ILBCDecContext *s  = avctx->priv_data;
83     AVFrame *frame     = data;
84     int ret;
85
86     if (s->decoder.no_of_bytes > buf_size) {
87         av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
88                buf_size, s->decoder.no_of_bytes);
89         return AVERROR_INVALIDDATA;
90     }
91
92     frame->nb_samples = s->decoder.blockl;
93     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
94         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
95         return ret;
96     }
97
98     WebRtcIlbcfix_DecodeImpl((int16_t *) frame->data[0], (const uint16_t *) buf, &s->decoder, 1);
99
100     *got_frame_ptr = 1;
101
102     return s->decoder.no_of_bytes;
103 }
104
105 AVCodec ff_libilbc_decoder = {
106     .name           = "libilbc",
107     .long_name      = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
108     .type           = AVMEDIA_TYPE_AUDIO,
109     .id             = AV_CODEC_ID_ILBC,
110     .priv_data_size = sizeof(ILBCDecContext),
111     .init           = ilbc_decode_init,
112     .decode         = ilbc_decode_frame,
113     .capabilities   = AV_CODEC_CAP_DR1,
114     .priv_class     = &ilbc_dec_class,
115 };
116
117 typedef struct ILBCEncContext {
118     const AVClass *class;
119     iLBC_Enc_Inst_t encoder;
120     int mode;
121 } ILBCEncContext;
122
123 static const AVOption ilbc_enc_options[] = {
124     { "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 },
125     { NULL }
126 };
127
128 static const AVClass ilbc_enc_class = {
129     "libilbc", av_default_item_name, ilbc_enc_options, LIBAVUTIL_VERSION_INT
130 };
131
132 static av_cold int ilbc_encode_init(AVCodecContext *avctx)
133 {
134     ILBCEncContext *s = avctx->priv_data;
135     int mode;
136
137     if (avctx->sample_rate != 8000) {
138         av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
139         return AVERROR(EINVAL);
140     }
141
142     if (avctx->channels != 1) {
143         av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
144         return AVERROR(EINVAL);
145     }
146
147     if ((mode = get_mode(avctx)) > 0)
148         s->mode = mode;
149     else
150         s->mode = s->mode != 30 ? 20 : 30;
151     WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
152
153     avctx->block_align = s->encoder.no_of_bytes;
154     avctx->frame_size  = s->encoder.blockl;
155
156     return 0;
157 }
158
159 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
160                              const AVFrame *frame, int *got_packet_ptr)
161 {
162     ILBCEncContext *s = avctx->priv_data;
163     int ret;
164
165     if ((ret = ff_alloc_packet(avpkt, 50))) {
166         av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
167         return ret;
168     }
169
170     WebRtcIlbcfix_EncodeImpl((uint16_t *) avpkt->data, (const int16_t *) frame->data[0], &s->encoder);
171
172     avpkt->size     = s->encoder.no_of_bytes;
173     *got_packet_ptr = 1;
174     return 0;
175 }
176
177 static const AVCodecDefault ilbc_encode_defaults[] = {
178     { "b", "0" },
179     { NULL }
180 };
181
182 AVCodec ff_libilbc_encoder = {
183     .name           = "libilbc",
184     .long_name      = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
185     .type           = AVMEDIA_TYPE_AUDIO,
186     .id             = AV_CODEC_ID_ILBC,
187     .priv_data_size = sizeof(ILBCEncContext),
188     .init           = ilbc_encode_init,
189     .encode2        = ilbc_encode_frame,
190     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
191                                                      AV_SAMPLE_FMT_NONE },
192     .defaults       = ilbc_encode_defaults,
193     .priv_class     = &ilbc_enc_class,
194 };