yading@10: /* yading@10: * Copyright (c) CMU 1993 Computer Science, Speech Group yading@10: * Chengxiang Lu and Alex Hauptmann yading@10: * Copyright (c) 2005 Steve Underwood yading@10: * Copyright (c) 2009 Kenan Gillet yading@10: * Copyright (c) 2010 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: /** yading@10: * @file yading@10: * G.722 ADPCM audio decoder yading@10: * yading@10: * This G.722 decoder is a bit-exact implementation of the ITU G.722 yading@10: * specification for all three specified bitrates - 64000bps, 56000bps yading@10: * and 48000bps. It passes the ITU tests. yading@10: * yading@10: * @note For the 56000bps and 48000bps bitrates, the lowest 1 or 2 bits yading@10: * respectively of each byte are ignored. yading@10: */ yading@10: yading@10: #include "libavutil/channel_layout.h" yading@10: #include "libavutil/opt.h" yading@10: #include "avcodec.h" yading@10: #include "get_bits.h" yading@10: #include "g722.h" yading@10: #include "internal.h" yading@10: yading@10: #define OFFSET(x) offsetof(G722Context, x) yading@10: #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM yading@10: static const AVOption options[] = { yading@10: { "bits_per_codeword", "Bits per G722 codeword", OFFSET(bits_per_codeword), AV_OPT_TYPE_FLAGS, { .i64 = 8 }, 6, 8, AD }, yading@10: { NULL } yading@10: }; yading@10: yading@10: static const AVClass g722_decoder_class = { yading@10: .class_name = "g722 decoder", yading@10: .item_name = av_default_item_name, yading@10: .option = options, yading@10: .version = LIBAVUTIL_VERSION_INT, yading@10: }; yading@10: yading@10: static av_cold int g722_decode_init(AVCodecContext * avctx) yading@10: { yading@10: G722Context *c = avctx->priv_data; yading@10: yading@10: avctx->channels = 1; yading@10: avctx->channel_layout = AV_CH_LAYOUT_MONO; yading@10: avctx->sample_fmt = AV_SAMPLE_FMT_S16; yading@10: yading@10: c->band[0].scale_factor = 8; yading@10: c->band[1].scale_factor = 2; yading@10: c->prev_samples_pos = 22; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static const int16_t low_inv_quant5[32] = { yading@10: -35, -35, -2919, -2195, -1765, -1458, -1219, -1023, yading@10: -858, -714, -587, -473, -370, -276, -190, -110, yading@10: 2919, 2195, 1765, 1458, 1219, 1023, 858, 714, yading@10: 587, 473, 370, 276, 190, 110, 35, -35 yading@10: }; yading@10: yading@10: static const int16_t *low_inv_quants[3] = { ff_g722_low_inv_quant6, yading@10: low_inv_quant5, yading@10: ff_g722_low_inv_quant4 }; yading@10: yading@10: static int g722_decode_frame(AVCodecContext *avctx, void *data, yading@10: int *got_frame_ptr, AVPacket *avpkt) yading@10: { yading@10: G722Context *c = avctx->priv_data; yading@10: AVFrame *frame = data; yading@10: int16_t *out_buf; yading@10: int j, ret; yading@10: const int skip = 8 - c->bits_per_codeword; yading@10: const int16_t *quantizer_table = low_inv_quants[skip]; yading@10: GetBitContext gb; yading@10: yading@10: /* get output buffer */ yading@10: frame->nb_samples = avpkt->size * 2; yading@10: if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) yading@10: return ret; yading@10: out_buf = (int16_t *)frame->data[0]; yading@10: yading@10: init_get_bits(&gb, avpkt->data, avpkt->size * 8); yading@10: yading@10: for (j = 0; j < avpkt->size; j++) { yading@10: int ilow, ihigh, rlow, rhigh, dhigh; yading@10: int xout1, xout2; yading@10: yading@10: ihigh = get_bits(&gb, 2); yading@10: ilow = get_bits(&gb, 6 - skip); yading@10: skip_bits(&gb, skip); yading@10: yading@10: rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10) yading@10: + c->band[0].s_predictor, -16384, 16383); yading@10: yading@10: ff_g722_update_low_predictor(&c->band[0], ilow >> (2 - skip)); yading@10: yading@10: dhigh = c->band[1].scale_factor * ff_g722_high_inv_quant[ihigh] >> 10; yading@10: rhigh = av_clip(dhigh + c->band[1].s_predictor, -16384, 16383); yading@10: yading@10: ff_g722_update_high_predictor(&c->band[1], dhigh, ihigh); yading@10: yading@10: c->prev_samples[c->prev_samples_pos++] = rlow + rhigh; yading@10: c->prev_samples[c->prev_samples_pos++] = rlow - rhigh; yading@10: ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24, yading@10: &xout1, &xout2); yading@10: *out_buf++ = av_clip_int16(xout1 >> 11); yading@10: *out_buf++ = av_clip_int16(xout2 >> 11); yading@10: if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) { yading@10: memmove(c->prev_samples, c->prev_samples + c->prev_samples_pos - 22, yading@10: 22 * sizeof(c->prev_samples[0])); yading@10: c->prev_samples_pos = 22; yading@10: } yading@10: } yading@10: yading@10: *got_frame_ptr = 1; yading@10: yading@10: return avpkt->size; yading@10: } yading@10: yading@10: AVCodec ff_adpcm_g722_decoder = { yading@10: .name = "g722", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_ADPCM_G722, yading@10: .priv_data_size = sizeof(G722Context), yading@10: .init = g722_decode_init, yading@10: .decode = g722_decode_frame, yading@10: .capabilities = CODEC_CAP_DR1, yading@10: .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"), yading@10: .priv_class = &g722_decoder_class, yading@10: };