]> git.sesse.net Git - ffmpeg/blob - libavcodec/libilbc.c
Improve descriptiveness of a number of codec and container long names
[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/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     "libilbc", av_default_item_name, ilbc_dec_options, LIBAVUTIL_VERSION_INT
54 };
55
56 static av_cold int ilbc_decode_init(AVCodecContext *avctx)
57 {
58     ILBCDecContext *s  = avctx->priv_data;
59     int mode;
60
61     if ((mode = get_mode(avctx)) < 0) {
62         av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
63         return AVERROR(EINVAL);
64     }
65
66     WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
67     avcodec_get_frame_defaults(&s->frame);
68     avctx->coded_frame = &s->frame;
69
70     avctx->channels = 1;
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     int ret;
84
85     if (s->decoder.no_of_bytes > buf_size) {
86         av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
87                buf_size, s->decoder.no_of_bytes);
88         return AVERROR_INVALIDDATA;
89     }
90
91     s->frame.nb_samples = s->decoder.blockl;
92     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
93         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
94         return ret;
95     }
96
97     WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) s->frame.data[0],
98                              (const WebRtc_UWord16*) buf, &s->decoder, 1);
99
100     *got_frame_ptr   = 1;
101     *(AVFrame *)data = s->frame;
102
103     return s->decoder.no_of_bytes;
104 }
105
106 AVCodec ff_libilbc_decoder = {
107     .name           = "libilbc",
108     .type           = AVMEDIA_TYPE_AUDIO,
109     .id             = CODEC_ID_ILBC,
110     .priv_data_size = sizeof(ILBCDecContext),
111     .init           = ilbc_decode_init,
112     .decode         = ilbc_decode_frame,
113     .capabilities   = CODEC_CAP_DR1,
114     .long_name      = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
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, { 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     "libilbc", av_default_item_name, ilbc_enc_options, LIBAVUTIL_VERSION_INT
131 };
132
133 static av_cold int ilbc_encode_init(AVCodecContext *avctx)
134 {
135     ILBCEncContext *s = avctx->priv_data;
136     int mode;
137
138     if (avctx->sample_rate != 8000) {
139         av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
140         return AVERROR(EINVAL);
141     }
142
143     if (avctx->channels != 1) {
144         av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
145         return AVERROR(EINVAL);
146     }
147
148     if ((mode = get_mode(avctx)) > 0)
149         s->mode = mode;
150     else
151         s->mode = s->mode != 30 ? 20 : 30;
152     WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
153
154     avctx->block_align = s->encoder.no_of_bytes;
155     avctx->frame_size  = s->encoder.blockl;
156 #if FF_API_OLD_ENCODE_AUDIO
157     avctx->coded_frame = avcodec_alloc_frame();
158     if (!avctx->coded_frame)
159         return AVERROR(ENOMEM);
160 #endif
161
162     return 0;
163 }
164
165 static av_cold int ilbc_encode_close(AVCodecContext *avctx)
166 {
167 #if FF_API_OLD_ENCODE_AUDIO
168     av_freep(&avctx->coded_frame);
169 #endif
170     return 0;
171 }
172
173 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
174                              const AVFrame *frame, int *got_packet_ptr)
175 {
176     ILBCEncContext *s = avctx->priv_data;
177     int ret;
178
179     if ((ret = ff_alloc_packet(avpkt, 50))) {
180         av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
181         return ret;
182     }
183
184     WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
185
186     avpkt->size     = s->encoder.no_of_bytes;
187     *got_packet_ptr = 1;
188     return 0;
189 }
190
191 static const AVCodecDefault ilbc_encode_defaults[] = {
192     { "b", "0" },
193     { NULL }
194 };
195
196 AVCodec ff_libilbc_encoder = {
197     .name           = "libilbc",
198     .type           = AVMEDIA_TYPE_AUDIO,
199     .id             = CODEC_ID_ILBC,
200     .priv_data_size = sizeof(ILBCEncContext),
201     .init           = ilbc_encode_init,
202     .encode2        = ilbc_encode_frame,
203     .close          = ilbc_encode_close,
204     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
205                                                      AV_SAMPLE_FMT_NONE },
206     .long_name      = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
207     .defaults       = ilbc_encode_defaults,
208     .priv_class     = &ilbc_enc_class,
209 };