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