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