]> git.sesse.net Git - ffmpeg/blob - libavcodec/libilbc.c
cook: use AVCodecContext.channels instead of keeping a private copy
[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 "avcodec.h"
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
27 #include "internal.h"
28
29 static int get_mode(AVCodecContext *avctx)
30 {
31     if (avctx->block_align == 38)
32         return 20;
33     else if (avctx->block_align == 50)
34         return 30;
35     else if (avctx->bit_rate > 0)
36         return avctx->bit_rate <= 14000 ? 30 : 20;
37     else
38         return -1;
39 }
40
41 typedef struct ILBCDecContext {
42     const AVClass *class;
43     AVFrame frame;
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     avcodec_get_frame_defaults(&s->frame);
69     avctx->coded_frame = &s->frame;
70
71     avctx->channels = 1;
72     avctx->sample_rate = 8000;
73     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
74
75     return 0;
76 }
77
78 static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
79                              int *got_frame_ptr, AVPacket *avpkt)
80 {
81     const uint8_t *buf = avpkt->data;
82     int buf_size       = avpkt->size;
83     ILBCDecContext *s  = avctx->priv_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     s->frame.nb_samples = s->decoder.blockl;
93     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
94         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
95         return ret;
96     }
97
98     WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) s->frame.data[0],
99                              (const WebRtc_UWord16*) buf, &s->decoder, 1);
100
101     *got_frame_ptr   = 1;
102     *(AVFrame *)data = s->frame;
103
104     return s->decoder.no_of_bytes;
105 }
106
107 AVCodec ff_libilbc_decoder = {
108     .name           = "libilbc",
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   = CODEC_CAP_DR1,
115     .long_name      = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
116     .priv_class     = &ilbc_dec_class,
117 };
118
119 typedef struct ILBCEncContext {
120     const AVClass *class;
121     iLBC_Enc_Inst_t encoder;
122     int mode;
123 } ILBCEncContext;
124
125 static const AVOption ilbc_enc_options[] = {
126     { "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 },
127     { NULL }
128 };
129
130 static const AVClass ilbc_enc_class = {
131     "libilbc", av_default_item_name, ilbc_enc_options, LIBAVUTIL_VERSION_INT
132 };
133
134 static av_cold int ilbc_encode_init(AVCodecContext *avctx)
135 {
136     ILBCEncContext *s = avctx->priv_data;
137     int mode;
138
139     if (avctx->sample_rate != 8000) {
140         av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
141         return AVERROR(EINVAL);
142     }
143
144     if (avctx->channels != 1) {
145         av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
146         return AVERROR(EINVAL);
147     }
148
149     if ((mode = get_mode(avctx)) > 0)
150         s->mode = mode;
151     else
152         s->mode = s->mode != 30 ? 20 : 30;
153     WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
154
155     avctx->block_align = s->encoder.no_of_bytes;
156     avctx->frame_size  = s->encoder.blockl;
157 #if FF_API_OLD_ENCODE_AUDIO
158     avctx->coded_frame = avcodec_alloc_frame();
159     if (!avctx->coded_frame)
160         return AVERROR(ENOMEM);
161 #endif
162
163     return 0;
164 }
165
166 static av_cold int ilbc_encode_close(AVCodecContext *avctx)
167 {
168 #if FF_API_OLD_ENCODE_AUDIO
169     av_freep(&avctx->coded_frame);
170 #endif
171     return 0;
172 }
173
174 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
175                              const AVFrame *frame, int *got_packet_ptr)
176 {
177     ILBCEncContext *s = avctx->priv_data;
178     int ret;
179
180     if ((ret = ff_alloc_packet(avpkt, 50))) {
181         av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
182         return ret;
183     }
184
185     WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
186
187     avpkt->size     = s->encoder.no_of_bytes;
188     *got_packet_ptr = 1;
189     return 0;
190 }
191
192 static const AVCodecDefault ilbc_encode_defaults[] = {
193     { "b", "0" },
194     { NULL }
195 };
196
197 AVCodec ff_libilbc_encoder = {
198     .name           = "libilbc",
199     .type           = AVMEDIA_TYPE_AUDIO,
200     .id             = AV_CODEC_ID_ILBC,
201     .priv_data_size = sizeof(ILBCEncContext),
202     .init           = ilbc_encode_init,
203     .encode2        = ilbc_encode_frame,
204     .close          = ilbc_encode_close,
205     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
206                                                      AV_SAMPLE_FMT_NONE },
207     .long_name      = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
208     .defaults       = ilbc_encode_defaults,
209     .priv_class     = &ilbc_enc_class,
210 };