yading@10: /* yading@10: * AMR Audio encoder stub yading@10: * Copyright (c) 2003 the ffmpeg project 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: #include yading@10: #include yading@10: yading@10: #include "libavutil/avstring.h" yading@10: #include "libavutil/internal.h" yading@10: #include "libavutil/mem.h" yading@10: #include "libavutil/opt.h" yading@10: #include "avcodec.h" yading@10: #include "internal.h" yading@10: yading@10: #define MAX_PACKET_SIZE (1 + (477 + 7) / 8) yading@10: yading@10: typedef struct AMRWBContext { yading@10: AVClass *av_class; yading@10: void *state; yading@10: int mode; yading@10: int last_bitrate; yading@10: int allow_dtx; yading@10: } AMRWBContext; yading@10: yading@10: static const AVOption options[] = { yading@10: { "dtx", "Allow DTX (generate comfort noise)", offsetof(AMRWBContext, allow_dtx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM }, yading@10: { NULL } yading@10: }; yading@10: yading@10: static const AVClass class = { yading@10: "libvo_amrwbenc", av_default_item_name, options, LIBAVUTIL_VERSION_INT yading@10: }; yading@10: yading@10: static int get_wb_bitrate_mode(int bitrate, void *log_ctx) yading@10: { yading@10: /* make the correspondance between bitrate and mode */ yading@10: static const int rates[] = { 6600, 8850, 12650, 14250, 15850, 18250, yading@10: 19850, 23050, 23850 }; yading@10: int i, best = -1, min_diff = 0; yading@10: char log_buf[200]; yading@10: yading@10: for (i = 0; i < 9; i++) { yading@10: if (rates[i] == bitrate) yading@10: return i; yading@10: if (best < 0 || abs(rates[i] - bitrate) < min_diff) { yading@10: best = i; yading@10: min_diff = abs(rates[i] - bitrate); yading@10: } yading@10: } yading@10: /* no bitrate matching exactly, log a warning */ yading@10: snprintf(log_buf, sizeof(log_buf), "bitrate not supported: use one of "); yading@10: for (i = 0; i < 9; i++) yading@10: av_strlcatf(log_buf, sizeof(log_buf), "%.2fk, ", rates[i] / 1000.f); yading@10: av_strlcatf(log_buf, sizeof(log_buf), "using %.2fk", rates[best] / 1000.f); yading@10: av_log(log_ctx, AV_LOG_WARNING, "%s\n", log_buf); yading@10: yading@10: return best; yading@10: } yading@10: yading@10: static av_cold int amr_wb_encode_init(AVCodecContext *avctx) yading@10: { yading@10: AMRWBContext *s = avctx->priv_data; yading@10: yading@10: if (avctx->sample_rate != 16000 && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { yading@10: av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n"); yading@10: return AVERROR(ENOSYS); 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(ENOSYS); yading@10: } yading@10: yading@10: s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx); yading@10: s->last_bitrate = avctx->bit_rate; yading@10: yading@10: avctx->frame_size = 320; yading@10: avctx->delay = 80; yading@10: yading@10: s->state = E_IF_init(); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int amr_wb_encode_close(AVCodecContext *avctx) yading@10: { yading@10: AMRWBContext *s = avctx->priv_data; yading@10: yading@10: E_IF_exit(s->state); yading@10: return 0; yading@10: } yading@10: yading@10: static int amr_wb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, yading@10: const AVFrame *frame, int *got_packet_ptr) yading@10: { yading@10: AMRWBContext *s = avctx->priv_data; yading@10: const int16_t *samples = (const int16_t *)frame->data[0]; yading@10: int size, ret; yading@10: yading@10: if ((ret = ff_alloc_packet2(avctx, avpkt, MAX_PACKET_SIZE)) < 0) yading@10: return ret; yading@10: yading@10: if (s->last_bitrate != avctx->bit_rate) { yading@10: s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx); yading@10: s->last_bitrate = avctx->bit_rate; yading@10: } yading@10: size = E_IF_encode(s->state, s->mode, samples, avpkt->data, s->allow_dtx); yading@10: if (size <= 0 || size > MAX_PACKET_SIZE) { yading@10: av_log(avctx, AV_LOG_ERROR, "Error encoding frame\n"); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: if (frame->pts != AV_NOPTS_VALUE) yading@10: avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay); yading@10: yading@10: avpkt->size = size; yading@10: *got_packet_ptr = 1; yading@10: return 0; yading@10: } yading@10: yading@10: AVCodec ff_libvo_amrwbenc_encoder = { yading@10: .name = "libvo_amrwbenc", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_AMR_WB, yading@10: .priv_data_size = sizeof(AMRWBContext), yading@10: .init = amr_wb_encode_init, yading@10: .encode2 = amr_wb_encode_frame, yading@10: .close = amr_wb_encode_close, 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("Android VisualOn AMR-WB " yading@10: "(Adaptive Multi-Rate Wide-Band)"), yading@10: .priv_class = &class, yading@10: };