yading@10: /* yading@10: * iLBC decoder/encoder stub yading@10: * Copyright (c) 2012 Martin Storsjo yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include yading@10: yading@10: #include "libavutil/channel_layout.h" yading@10: #include "libavutil/common.h" yading@10: #include "libavutil/opt.h" yading@10: #include "avcodec.h" yading@10: #include "internal.h" yading@10: yading@10: static int get_mode(AVCodecContext *avctx) yading@10: { yading@10: if (avctx->block_align == 38) yading@10: return 20; yading@10: else if (avctx->block_align == 50) yading@10: return 30; yading@10: else if (avctx->bit_rate > 0) yading@10: return avctx->bit_rate <= 14000 ? 30 : 20; yading@10: else yading@10: return -1; yading@10: } yading@10: yading@10: typedef struct ILBCDecContext { yading@10: const AVClass *class; yading@10: iLBC_Dec_Inst_t decoder; yading@10: int enhance; yading@10: } ILBCDecContext; yading@10: yading@10: static const AVOption ilbc_dec_options[] = { yading@10: { "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: { NULL } yading@10: }; yading@10: yading@10: static const AVClass ilbc_dec_class = { yading@10: .class_name = "libilbc", yading@10: .item_name = av_default_item_name, yading@10: .option = ilbc_dec_options, yading@10: .version = LIBAVUTIL_VERSION_INT, yading@10: }; yading@10: yading@10: static av_cold int ilbc_decode_init(AVCodecContext *avctx) yading@10: { yading@10: ILBCDecContext *s = avctx->priv_data; yading@10: int mode; yading@10: yading@10: if ((mode = get_mode(avctx)) < 0) { yading@10: av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n"); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance); yading@10: yading@10: avctx->channels = 1; yading@10: avctx->channel_layout = AV_CH_LAYOUT_MONO; yading@10: avctx->sample_rate = 8000; yading@10: avctx->sample_fmt = AV_SAMPLE_FMT_S16; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int ilbc_decode_frame(AVCodecContext *avctx, void *data, yading@10: int *got_frame_ptr, AVPacket *avpkt) yading@10: { yading@10: const uint8_t *buf = avpkt->data; yading@10: int buf_size = avpkt->size; yading@10: ILBCDecContext *s = avctx->priv_data; yading@10: AVFrame *frame = data; yading@10: int ret; yading@10: yading@10: if (s->decoder.no_of_bytes > buf_size) { yading@10: av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n", yading@10: buf_size, s->decoder.no_of_bytes); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: yading@10: frame->nb_samples = s->decoder.blockl; yading@10: if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) yading@10: return ret; yading@10: yading@10: WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) frame->data[0], yading@10: (const WebRtc_UWord16*) buf, &s->decoder, 1); yading@10: yading@10: *got_frame_ptr = 1; yading@10: yading@10: return s->decoder.no_of_bytes; yading@10: } yading@10: yading@10: AVCodec ff_libilbc_decoder = { yading@10: .name = "libilbc", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_ILBC, yading@10: .priv_data_size = sizeof(ILBCDecContext), yading@10: .init = ilbc_decode_init, yading@10: .decode = ilbc_decode_frame, yading@10: .capabilities = CODEC_CAP_DR1, yading@10: .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"), yading@10: .priv_class = &ilbc_dec_class, yading@10: }; yading@10: yading@10: typedef struct ILBCEncContext { yading@10: const AVClass *class; yading@10: iLBC_Enc_Inst_t encoder; yading@10: int mode; yading@10: } ILBCEncContext; yading@10: yading@10: static const AVOption ilbc_enc_options[] = { yading@10: { "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: { NULL } yading@10: }; yading@10: yading@10: static const AVClass ilbc_enc_class = { yading@10: .class_name = "libilbc", yading@10: .item_name = av_default_item_name, yading@10: .option = ilbc_enc_options, yading@10: .version = LIBAVUTIL_VERSION_INT, yading@10: }; yading@10: yading@10: static av_cold int ilbc_encode_init(AVCodecContext *avctx) yading@10: { yading@10: ILBCEncContext *s = avctx->priv_data; yading@10: int mode; yading@10: yading@10: if (avctx->sample_rate != 8000) { yading@10: av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n"); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: if (avctx->channels != 1) { yading@10: av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: if ((mode = get_mode(avctx)) > 0) yading@10: s->mode = mode; yading@10: else yading@10: s->mode = s->mode != 30 ? 20 : 30; yading@10: WebRtcIlbcfix_InitEncode(&s->encoder, s->mode); yading@10: yading@10: avctx->block_align = s->encoder.no_of_bytes; yading@10: avctx->frame_size = s->encoder.blockl; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, yading@10: const AVFrame *frame, int *got_packet_ptr) yading@10: { yading@10: ILBCEncContext *s = avctx->priv_data; yading@10: int ret; yading@10: yading@10: if ((ret = ff_alloc_packet2(avctx, avpkt, 50)) < 0) yading@10: return ret; yading@10: yading@10: WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder); yading@10: yading@10: avpkt->size = s->encoder.no_of_bytes; yading@10: *got_packet_ptr = 1; yading@10: return 0; yading@10: } yading@10: yading@10: static const AVCodecDefault ilbc_encode_defaults[] = { yading@10: { "b", "0" }, yading@10: { NULL } yading@10: }; yading@10: yading@10: AVCodec ff_libilbc_encoder = { yading@10: .name = "libilbc", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_ILBC, yading@10: .priv_data_size = sizeof(ILBCEncContext), yading@10: .init = ilbc_encode_init, yading@10: .encode2 = ilbc_encode_frame, yading@10: .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, yading@10: AV_SAMPLE_FMT_NONE }, yading@10: .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"), yading@10: .defaults = ilbc_encode_defaults, yading@10: .priv_class = &ilbc_enc_class, yading@10: };