]> git.sesse.net Git - ffmpeg/blob - libavcodec/libilbc.c
cosmetics: iff: fix typo
[ffmpeg] / libavcodec / libilbc.c
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 "avcodec.h"
25 #include "libavutil/opt.h"
26 #include "internal.h"
27
28 static int get_mode(AVCodecContext *avctx)
29 {
30     if (avctx->block_align == 38)
31         return 20;
32     else if (avctx->block_align == 50)
33         return 30;
34     else if (avctx->bit_rate > 0)
35         return avctx->bit_rate <= 14000 ? 30 : 20;
36     else
37         return -1;
38 }
39
40 typedef struct ILBCDecContext {
41     const AVClass *class;
42     AVFrame frame;
43     iLBC_Dec_Inst_t decoder;
44     int enhance;
45 } ILBCDecContext;
46
47 static const AVOption ilbc_dec_options[] = {
48     { "enhance", "Enhance the decoded audio (adds delay)", offsetof(ILBCDecContext, enhance), AV_OPT_TYPE_INT, { 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM },
49     { NULL }
50 };
51
52 static const AVClass ilbc_dec_class = {
53     .class_name = "libilbc",
54     .item_name  = av_default_item_name,
55     .option     = ilbc_dec_options,
56     .version    = LIBAVUTIL_VERSION_INT,
57 };
58
59 static av_cold int ilbc_decode_init(AVCodecContext *avctx)
60 {
61     ILBCDecContext *s  = avctx->priv_data;
62     int mode;
63
64     if ((mode = get_mode(avctx)) < 0) {
65         av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
66         return AVERROR(EINVAL);
67     }
68
69     WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
70     avcodec_get_frame_defaults(&s->frame);
71     avctx->coded_frame = &s->frame;
72
73     avctx->channels = 1;
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     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 = avctx->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
109 AVCodec ff_libilbc_decoder = {
110     .name           = "libilbc",
111     .type           = AVMEDIA_TYPE_AUDIO,
112     .id             = CODEC_ID_ILBC,
113     .priv_data_size = sizeof(ILBCDecContext),
114     .init           = ilbc_decode_init,
115     .decode         = ilbc_decode_frame,
116     .capabilities   = CODEC_CAP_DR1,
117     .long_name      = NULL_IF_CONFIG_SMALL("Internet Low Bitrate Codec (iLBC)"),
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;
125 } ILBCEncContext;
126
127 static const AVOption ilbc_enc_options[] = {
128     { "mode", "iLBC mode (20 or 30 ms frames)", offsetof(ILBCEncContext, mode), AV_OPT_TYPE_INT, { 20 }, 20, 30, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
129     { NULL }
130 };
131
132 static const AVClass ilbc_enc_class = {
133     .class_name = "libilbc",
134     .item_name  = av_default_item_name,
135     .option     = ilbc_enc_options,
136     .version    = LIBAVUTIL_VERSION_INT,
137 };
138
139 static av_cold int ilbc_encode_init(AVCodecContext *avctx)
140 {
141     ILBCEncContext *s = avctx->priv_data;
142     int mode;
143
144     if (avctx->sample_rate != 8000) {
145         av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
146         return AVERROR(EINVAL);
147     }
148
149     if (avctx->channels != 1) {
150         av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
151         return AVERROR(EINVAL);
152     }
153
154     if ((mode = get_mode(avctx)) > 0)
155         s->mode = mode;
156     else
157         s->mode = s->mode != 30 ? 20 : 30;
158     WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
159
160     avctx->block_align = s->encoder.no_of_bytes;
161     avctx->frame_size  = s->encoder.blockl;
162 #if FF_API_OLD_ENCODE_AUDIO
163     avctx->coded_frame = avcodec_alloc_frame();
164     if (!avctx->coded_frame)
165         return AVERROR(ENOMEM);
166 #endif
167
168     return 0;
169 }
170
171 static av_cold int ilbc_encode_close(AVCodecContext *avctx)
172 {
173 #if FF_API_OLD_ENCODE_AUDIO
174     av_freep(&avctx->coded_frame);
175 #endif
176     return 0;
177 }
178
179 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
180                              const AVFrame *frame, int *got_packet_ptr)
181 {
182     ILBCEncContext *s = avctx->priv_data;
183     int ret;
184
185     if ((ret = ff_alloc_packet(avpkt, 50))) {
186         av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
187         return ret;
188     }
189
190     WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
191
192     avpkt->size     = s->encoder.no_of_bytes;
193     *got_packet_ptr = 1;
194     return 0;
195 }
196
197 static const AVCodecDefault ilbc_encode_defaults[] = {
198     { "b", "0" },
199     { NULL }
200 };
201
202 AVCodec ff_libilbc_encoder = {
203     .name           = "libilbc",
204     .type           = AVMEDIA_TYPE_AUDIO,
205     .id             = CODEC_ID_ILBC,
206     .priv_data_size = sizeof(ILBCEncContext),
207     .init           = ilbc_encode_init,
208     .encode2        = ilbc_encode_frame,
209     .close          = ilbc_encode_close,
210     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
211                                                      AV_SAMPLE_FMT_NONE },
212     .long_name      = NULL_IF_CONFIG_SMALL("Internet Low Bitrate Codec (iLBC)"),
213     .defaults       = ilbc_encode_defaults,
214     .priv_class     = &ilbc_enc_class,
215 };