yading@10
|
1 /*
|
yading@10
|
2 * iLBC decoder/encoder stub
|
yading@10
|
3 * Copyright (c) 2012 Martin Storsjo
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 #include <ilbc.h>
|
yading@10
|
23
|
yading@10
|
24 #include "libavutil/channel_layout.h"
|
yading@10
|
25 #include "libavutil/common.h"
|
yading@10
|
26 #include "libavutil/opt.h"
|
yading@10
|
27 #include "avcodec.h"
|
yading@10
|
28 #include "internal.h"
|
yading@10
|
29
|
yading@10
|
30 static int get_mode(AVCodecContext *avctx)
|
yading@10
|
31 {
|
yading@10
|
32 if (avctx->block_align == 38)
|
yading@10
|
33 return 20;
|
yading@10
|
34 else if (avctx->block_align == 50)
|
yading@10
|
35 return 30;
|
yading@10
|
36 else if (avctx->bit_rate > 0)
|
yading@10
|
37 return avctx->bit_rate <= 14000 ? 30 : 20;
|
yading@10
|
38 else
|
yading@10
|
39 return -1;
|
yading@10
|
40 }
|
yading@10
|
41
|
yading@10
|
42 typedef struct ILBCDecContext {
|
yading@10
|
43 const AVClass *class;
|
yading@10
|
44 iLBC_Dec_Inst_t decoder;
|
yading@10
|
45 int enhance;
|
yading@10
|
46 } ILBCDecContext;
|
yading@10
|
47
|
yading@10
|
48 static const AVOption ilbc_dec_options[] = {
|
yading@10
|
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 },
|
yading@10
|
50 { NULL }
|
yading@10
|
51 };
|
yading@10
|
52
|
yading@10
|
53 static const AVClass ilbc_dec_class = {
|
yading@10
|
54 .class_name = "libilbc",
|
yading@10
|
55 .item_name = av_default_item_name,
|
yading@10
|
56 .option = ilbc_dec_options,
|
yading@10
|
57 .version = LIBAVUTIL_VERSION_INT,
|
yading@10
|
58 };
|
yading@10
|
59
|
yading@10
|
60 static av_cold int ilbc_decode_init(AVCodecContext *avctx)
|
yading@10
|
61 {
|
yading@10
|
62 ILBCDecContext *s = avctx->priv_data;
|
yading@10
|
63 int mode;
|
yading@10
|
64
|
yading@10
|
65 if ((mode = get_mode(avctx)) < 0) {
|
yading@10
|
66 av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
|
yading@10
|
67 return AVERROR(EINVAL);
|
yading@10
|
68 }
|
yading@10
|
69
|
yading@10
|
70 WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
|
yading@10
|
71
|
yading@10
|
72 avctx->channels = 1;
|
yading@10
|
73 avctx->channel_layout = AV_CH_LAYOUT_MONO;
|
yading@10
|
74 avctx->sample_rate = 8000;
|
yading@10
|
75 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
|
yading@10
|
76
|
yading@10
|
77 return 0;
|
yading@10
|
78 }
|
yading@10
|
79
|
yading@10
|
80 static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
|
yading@10
|
81 int *got_frame_ptr, AVPacket *avpkt)
|
yading@10
|
82 {
|
yading@10
|
83 const uint8_t *buf = avpkt->data;
|
yading@10
|
84 int buf_size = avpkt->size;
|
yading@10
|
85 ILBCDecContext *s = avctx->priv_data;
|
yading@10
|
86 AVFrame *frame = data;
|
yading@10
|
87 int ret;
|
yading@10
|
88
|
yading@10
|
89 if (s->decoder.no_of_bytes > buf_size) {
|
yading@10
|
90 av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
|
yading@10
|
91 buf_size, s->decoder.no_of_bytes);
|
yading@10
|
92 return AVERROR_INVALIDDATA;
|
yading@10
|
93 }
|
yading@10
|
94
|
yading@10
|
95 frame->nb_samples = s->decoder.blockl;
|
yading@10
|
96 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
yading@10
|
97 return ret;
|
yading@10
|
98
|
yading@10
|
99 WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) frame->data[0],
|
yading@10
|
100 (const WebRtc_UWord16*) buf, &s->decoder, 1);
|
yading@10
|
101
|
yading@10
|
102 *got_frame_ptr = 1;
|
yading@10
|
103
|
yading@10
|
104 return s->decoder.no_of_bytes;
|
yading@10
|
105 }
|
yading@10
|
106
|
yading@10
|
107 AVCodec ff_libilbc_decoder = {
|
yading@10
|
108 .name = "libilbc",
|
yading@10
|
109 .type = AVMEDIA_TYPE_AUDIO,
|
yading@10
|
110 .id = AV_CODEC_ID_ILBC,
|
yading@10
|
111 .priv_data_size = sizeof(ILBCDecContext),
|
yading@10
|
112 .init = ilbc_decode_init,
|
yading@10
|
113 .decode = ilbc_decode_frame,
|
yading@10
|
114 .capabilities = CODEC_CAP_DR1,
|
yading@10
|
115 .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
|
yading@10
|
116 .priv_class = &ilbc_dec_class,
|
yading@10
|
117 };
|
yading@10
|
118
|
yading@10
|
119 typedef struct ILBCEncContext {
|
yading@10
|
120 const AVClass *class;
|
yading@10
|
121 iLBC_Enc_Inst_t encoder;
|
yading@10
|
122 int mode;
|
yading@10
|
123 } ILBCEncContext;
|
yading@10
|
124
|
yading@10
|
125 static const AVOption ilbc_enc_options[] = {
|
yading@10
|
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 },
|
yading@10
|
127 { NULL }
|
yading@10
|
128 };
|
yading@10
|
129
|
yading@10
|
130 static const AVClass ilbc_enc_class = {
|
yading@10
|
131 .class_name = "libilbc",
|
yading@10
|
132 .item_name = av_default_item_name,
|
yading@10
|
133 .option = ilbc_enc_options,
|
yading@10
|
134 .version = LIBAVUTIL_VERSION_INT,
|
yading@10
|
135 };
|
yading@10
|
136
|
yading@10
|
137 static av_cold int ilbc_encode_init(AVCodecContext *avctx)
|
yading@10
|
138 {
|
yading@10
|
139 ILBCEncContext *s = avctx->priv_data;
|
yading@10
|
140 int mode;
|
yading@10
|
141
|
yading@10
|
142 if (avctx->sample_rate != 8000) {
|
yading@10
|
143 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
|
yading@10
|
144 return AVERROR(EINVAL);
|
yading@10
|
145 }
|
yading@10
|
146
|
yading@10
|
147 if (avctx->channels != 1) {
|
yading@10
|
148 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
|
yading@10
|
149 return AVERROR(EINVAL);
|
yading@10
|
150 }
|
yading@10
|
151
|
yading@10
|
152 if ((mode = get_mode(avctx)) > 0)
|
yading@10
|
153 s->mode = mode;
|
yading@10
|
154 else
|
yading@10
|
155 s->mode = s->mode != 30 ? 20 : 30;
|
yading@10
|
156 WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
|
yading@10
|
157
|
yading@10
|
158 avctx->block_align = s->encoder.no_of_bytes;
|
yading@10
|
159 avctx->frame_size = s->encoder.blockl;
|
yading@10
|
160
|
yading@10
|
161 return 0;
|
yading@10
|
162 }
|
yading@10
|
163
|
yading@10
|
164 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
|
yading@10
|
165 const AVFrame *frame, int *got_packet_ptr)
|
yading@10
|
166 {
|
yading@10
|
167 ILBCEncContext *s = avctx->priv_data;
|
yading@10
|
168 int ret;
|
yading@10
|
169
|
yading@10
|
170 if ((ret = ff_alloc_packet2(avctx, avpkt, 50)) < 0)
|
yading@10
|
171 return ret;
|
yading@10
|
172
|
yading@10
|
173 WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
|
yading@10
|
174
|
yading@10
|
175 avpkt->size = s->encoder.no_of_bytes;
|
yading@10
|
176 *got_packet_ptr = 1;
|
yading@10
|
177 return 0;
|
yading@10
|
178 }
|
yading@10
|
179
|
yading@10
|
180 static const AVCodecDefault ilbc_encode_defaults[] = {
|
yading@10
|
181 { "b", "0" },
|
yading@10
|
182 { NULL }
|
yading@10
|
183 };
|
yading@10
|
184
|
yading@10
|
185 AVCodec ff_libilbc_encoder = {
|
yading@10
|
186 .name = "libilbc",
|
yading@10
|
187 .type = AVMEDIA_TYPE_AUDIO,
|
yading@10
|
188 .id = AV_CODEC_ID_ILBC,
|
yading@10
|
189 .priv_data_size = sizeof(ILBCEncContext),
|
yading@10
|
190 .init = ilbc_encode_init,
|
yading@10
|
191 .encode2 = ilbc_encode_frame,
|
yading@10
|
192 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
|
yading@10
|
193 AV_SAMPLE_FMT_NONE },
|
yading@10
|
194 .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
|
yading@10
|
195 .defaults = ilbc_encode_defaults,
|
yading@10
|
196 .priv_class = &ilbc_enc_class,
|
yading@10
|
197 };
|