00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <ilbc.h>
00023
00024 #include "avcodec.h"
00025 #include "libavutil/common.h"
00026 #include "libavutil/opt.h"
00027 #include "internal.h"
00028
00029 static int get_mode(AVCodecContext *avctx)
00030 {
00031 if (avctx->block_align == 38)
00032 return 20;
00033 else if (avctx->block_align == 50)
00034 return 30;
00035 else if (avctx->bit_rate > 0)
00036 return avctx->bit_rate <= 14000 ? 30 : 20;
00037 else
00038 return -1;
00039 }
00040
00041 typedef struct ILBCDecContext {
00042 const AVClass *class;
00043 AVFrame frame;
00044 iLBC_Dec_Inst_t decoder;
00045 int enhance;
00046 } ILBCDecContext;
00047
00048 static const AVOption ilbc_dec_options[] = {
00049 { "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 },
00050 { NULL }
00051 };
00052
00053 static const AVClass ilbc_dec_class = {
00054 .class_name = "libilbc",
00055 .item_name = av_default_item_name,
00056 .option = ilbc_dec_options,
00057 .version = LIBAVUTIL_VERSION_INT,
00058 };
00059
00060 static av_cold int ilbc_decode_init(AVCodecContext *avctx)
00061 {
00062 ILBCDecContext *s = avctx->priv_data;
00063 int mode;
00064
00065 if ((mode = get_mode(avctx)) < 0) {
00066 av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
00067 return AVERROR(EINVAL);
00068 }
00069
00070 WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
00071 avcodec_get_frame_defaults(&s->frame);
00072 avctx->coded_frame = &s->frame;
00073
00074 avctx->channels = 1;
00075 avctx->sample_rate = 8000;
00076 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00077
00078 return 0;
00079 }
00080
00081 static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
00082 int *got_frame_ptr, AVPacket *avpkt)
00083 {
00084 const uint8_t *buf = avpkt->data;
00085 int buf_size = avpkt->size;
00086 ILBCDecContext *s = avctx->priv_data;
00087 int ret;
00088
00089 if (s->decoder.no_of_bytes > buf_size) {
00090 av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
00091 buf_size, s->decoder.no_of_bytes);
00092 return AVERROR_INVALIDDATA;
00093 }
00094
00095 s->frame.nb_samples = s->decoder.blockl;
00096 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00097 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00098 return ret;
00099 }
00100
00101 WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) s->frame.data[0],
00102 (const WebRtc_UWord16*) buf, &s->decoder, 1);
00103
00104 *got_frame_ptr = 1;
00105 *(AVFrame *)data = s->frame;
00106
00107 return s->decoder.no_of_bytes;
00108 }
00109
00110 AVCodec ff_libilbc_decoder = {
00111 .name = "libilbc",
00112 .type = AVMEDIA_TYPE_AUDIO,
00113 .id = AV_CODEC_ID_ILBC,
00114 .priv_data_size = sizeof(ILBCDecContext),
00115 .init = ilbc_decode_init,
00116 .decode = ilbc_decode_frame,
00117 .capabilities = CODEC_CAP_DR1,
00118 .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
00119 .priv_class = &ilbc_dec_class,
00120 };
00121
00122 typedef struct ILBCEncContext {
00123 const AVClass *class;
00124 iLBC_Enc_Inst_t encoder;
00125 int mode;
00126 } ILBCEncContext;
00127
00128 static const AVOption ilbc_enc_options[] = {
00129 { "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 },
00130 { NULL }
00131 };
00132
00133 static const AVClass ilbc_enc_class = {
00134 .class_name = "libilbc",
00135 .item_name = av_default_item_name,
00136 .option = ilbc_enc_options,
00137 .version = LIBAVUTIL_VERSION_INT,
00138 };
00139
00140 static av_cold int ilbc_encode_init(AVCodecContext *avctx)
00141 {
00142 ILBCEncContext *s = avctx->priv_data;
00143 int mode;
00144
00145 if (avctx->sample_rate != 8000) {
00146 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
00147 return AVERROR(EINVAL);
00148 }
00149
00150 if (avctx->channels != 1) {
00151 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
00152 return AVERROR(EINVAL);
00153 }
00154
00155 if ((mode = get_mode(avctx)) > 0)
00156 s->mode = mode;
00157 else
00158 s->mode = s->mode != 30 ? 20 : 30;
00159 WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
00160
00161 avctx->block_align = s->encoder.no_of_bytes;
00162 avctx->frame_size = s->encoder.blockl;
00163 #if FF_API_OLD_ENCODE_AUDIO
00164 avctx->coded_frame = avcodec_alloc_frame();
00165 if (!avctx->coded_frame)
00166 return AVERROR(ENOMEM);
00167 #endif
00168
00169 return 0;
00170 }
00171
00172 static av_cold int ilbc_encode_close(AVCodecContext *avctx)
00173 {
00174 #if FF_API_OLD_ENCODE_AUDIO
00175 av_freep(&avctx->coded_frame);
00176 #endif
00177 return 0;
00178 }
00179
00180 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00181 const AVFrame *frame, int *got_packet_ptr)
00182 {
00183 ILBCEncContext *s = avctx->priv_data;
00184 int ret;
00185
00186 if ((ret = ff_alloc_packet(avpkt, 50))) {
00187 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
00188 return ret;
00189 }
00190
00191 WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
00192
00193 avpkt->size = s->encoder.no_of_bytes;
00194 *got_packet_ptr = 1;
00195 return 0;
00196 }
00197
00198 static const AVCodecDefault ilbc_encode_defaults[] = {
00199 { "b", "0" },
00200 { NULL }
00201 };
00202
00203 AVCodec ff_libilbc_encoder = {
00204 .name = "libilbc",
00205 .type = AVMEDIA_TYPE_AUDIO,
00206 .id = AV_CODEC_ID_ILBC,
00207 .priv_data_size = sizeof(ILBCEncContext),
00208 .init = ilbc_encode_init,
00209 .encode2 = ilbc_encode_frame,
00210 .close = ilbc_encode_close,
00211 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00212 AV_SAMPLE_FMT_NONE },
00213 .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
00214 .defaults = ilbc_encode_defaults,
00215 .priv_class = &ilbc_enc_class,
00216 };