00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00037 #include "avcodec.h"
00038 #include "get_bits.h"
00039 #include "g722.h"
00040 #include "libavutil/opt.h"
00041
00042 #define OFFSET(x) offsetof(G722Context, x)
00043 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
00044 static const AVOption options[] = {
00045 { "bits_per_codeword", "Bits per G722 codeword", OFFSET(bits_per_codeword), AV_OPT_TYPE_FLAGS, { .i64 = 8 }, 6, 8, AD },
00046 { NULL }
00047 };
00048
00049 static const AVClass g722_decoder_class = {
00050 .class_name = "g722 decoder",
00051 .item_name = av_default_item_name,
00052 .option = options,
00053 .version = LIBAVUTIL_VERSION_INT,
00054 };
00055
00056 static av_cold int g722_decode_init(AVCodecContext * avctx)
00057 {
00058 G722Context *c = avctx->priv_data;
00059
00060 if (avctx->channels != 1) {
00061 av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
00062 return AVERROR_INVALIDDATA;
00063 }
00064 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00065
00066 c->band[0].scale_factor = 8;
00067 c->band[1].scale_factor = 2;
00068 c->prev_samples_pos = 22;
00069
00070 avcodec_get_frame_defaults(&c->frame);
00071 avctx->coded_frame = &c->frame;
00072
00073 return 0;
00074 }
00075
00076 static const int16_t low_inv_quant5[32] = {
00077 -35, -35, -2919, -2195, -1765, -1458, -1219, -1023,
00078 -858, -714, -587, -473, -370, -276, -190, -110,
00079 2919, 2195, 1765, 1458, 1219, 1023, 858, 714,
00080 587, 473, 370, 276, 190, 110, 35, -35
00081 };
00082
00083 static const int16_t *low_inv_quants[3] = { ff_g722_low_inv_quant6,
00084 low_inv_quant5,
00085 ff_g722_low_inv_quant4 };
00086
00087 static int g722_decode_frame(AVCodecContext *avctx, void *data,
00088 int *got_frame_ptr, AVPacket *avpkt)
00089 {
00090 G722Context *c = avctx->priv_data;
00091 int16_t *out_buf;
00092 int j, ret;
00093 const int skip = 8 - c->bits_per_codeword;
00094 const int16_t *quantizer_table = low_inv_quants[skip];
00095 GetBitContext gb;
00096
00097
00098 c->frame.nb_samples = avpkt->size * 2;
00099 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
00100 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00101 return ret;
00102 }
00103 out_buf = (int16_t *)c->frame.data[0];
00104
00105 init_get_bits(&gb, avpkt->data, avpkt->size * 8);
00106
00107 for (j = 0; j < avpkt->size; j++) {
00108 int ilow, ihigh, rlow, rhigh, dhigh;
00109 int xout1, xout2;
00110
00111 ihigh = get_bits(&gb, 2);
00112 ilow = get_bits(&gb, 6 - skip);
00113 skip_bits(&gb, skip);
00114
00115 rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10)
00116 + c->band[0].s_predictor, -16384, 16383);
00117
00118 ff_g722_update_low_predictor(&c->band[0], ilow >> (2 - skip));
00119
00120 dhigh = c->band[1].scale_factor * ff_g722_high_inv_quant[ihigh] >> 10;
00121 rhigh = av_clip(dhigh + c->band[1].s_predictor, -16384, 16383);
00122
00123 ff_g722_update_high_predictor(&c->band[1], dhigh, ihigh);
00124
00125 c->prev_samples[c->prev_samples_pos++] = rlow + rhigh;
00126 c->prev_samples[c->prev_samples_pos++] = rlow - rhigh;
00127 ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24,
00128 &xout1, &xout2);
00129 *out_buf++ = av_clip_int16(xout1 >> 11);
00130 *out_buf++ = av_clip_int16(xout2 >> 11);
00131 if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
00132 memmove(c->prev_samples, c->prev_samples + c->prev_samples_pos - 22,
00133 22 * sizeof(c->prev_samples[0]));
00134 c->prev_samples_pos = 22;
00135 }
00136 }
00137
00138 *got_frame_ptr = 1;
00139 *(AVFrame *)data = c->frame;
00140
00141 return avpkt->size;
00142 }
00143
00144 AVCodec ff_adpcm_g722_decoder = {
00145 .name = "g722",
00146 .type = AVMEDIA_TYPE_AUDIO,
00147 .id = AV_CODEC_ID_ADPCM_G722,
00148 .priv_data_size = sizeof(G722Context),
00149 .init = g722_decode_init,
00150 .decode = g722_decode_frame,
00151 .capabilities = CODEC_CAP_DR1,
00152 .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
00153 .priv_class = &g722_decoder_class,
00154 };