libavcodec/g722dec.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) CMU 1993 Computer Science, Speech Group
00003  *                        Chengxiang Lu and Alex Hauptmann
00004  * Copyright (c) 2005 Steve Underwood <steveu at coppice.org>
00005  * Copyright (c) 2009 Kenan Gillet
00006  * Copyright (c) 2010 Martin Storsjo
00007  *
00008  * This file is part of Libav.
00009  *
00010  * Libav is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * Libav is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with Libav; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00037 #include "avcodec.h"
00038 #include "internal.h"
00039 #include "get_bits.h"
00040 #include "g722.h"
00041 #include "libavutil/opt.h"
00042 
00043 #define OFFSET(x) offsetof(G722Context, x)
00044 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
00045 static const AVOption options[] = {
00046     { "bits_per_codeword", "Bits per G722 codeword", OFFSET(bits_per_codeword), AV_OPT_TYPE_FLAGS, { 8 }, 6, 8, AD },
00047     { NULL }
00048 };
00049 
00050 static const AVClass g722_decoder_class = {
00051     .class_name = "g722 decoder",
00052     .item_name  = av_default_item_name,
00053     .option     = options,
00054     .version    = LIBAVUTIL_VERSION_INT,
00055 };
00056 
00057 static av_cold int g722_decode_init(AVCodecContext * avctx)
00058 {
00059     G722Context *c = avctx->priv_data;
00060 
00061     if (avctx->channels != 1) {
00062         av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
00063         return AVERROR_INVALIDDATA;
00064     }
00065     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00066 
00067     c->band[0].scale_factor = 8;
00068     c->band[1].scale_factor = 2;
00069     c->prev_samples_pos = 22;
00070 
00071     avcodec_get_frame_defaults(&c->frame);
00072     avctx->coded_frame = &c->frame;
00073 
00074     return 0;
00075 }
00076 
00077 static const int16_t low_inv_quant5[32] = {
00078      -35,   -35, -2919, -2195, -1765, -1458, -1219, -1023,
00079     -858,  -714,  -587,  -473,  -370,  -276,  -190,  -110,
00080     2919,  2195,  1765,  1458,  1219,  1023,   858,   714,
00081      587,   473,   370,   276,   190,   110,    35,   -35
00082 };
00083 
00084 static const int16_t *low_inv_quants[3] = { ff_g722_low_inv_quant6,
00085                                                     low_inv_quant5,
00086                                             ff_g722_low_inv_quant4 };
00087 
00088 static int g722_decode_frame(AVCodecContext *avctx, void *data,
00089                              int *got_frame_ptr, AVPacket *avpkt)
00090 {
00091     G722Context *c = avctx->priv_data;
00092     int16_t *out_buf;
00093     int j, ret;
00094     const int skip = 8 - c->bits_per_codeword;
00095     const int16_t *quantizer_table = low_inv_quants[skip];
00096     GetBitContext gb;
00097 
00098     /* get output buffer */
00099     c->frame.nb_samples = avpkt->size * 2;
00100     if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {
00101         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00102         return ret;
00103     }
00104     out_buf = (int16_t *)c->frame.data[0];
00105 
00106     init_get_bits(&gb, avpkt->data, avpkt->size * 8);
00107 
00108     for (j = 0; j < avpkt->size; j++) {
00109         int ilow, ihigh, rlow, rhigh, dhigh;
00110         int xout1, xout2;
00111 
00112         ihigh = get_bits(&gb, 2);
00113         ilow = get_bits(&gb, 6 - skip);
00114         skip_bits(&gb, skip);
00115 
00116         rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10)
00117                       + c->band[0].s_predictor, -16384, 16383);
00118 
00119         ff_g722_update_low_predictor(&c->band[0], ilow >> (2 - skip));
00120 
00121         dhigh = c->band[1].scale_factor * ff_g722_high_inv_quant[ihigh] >> 10;
00122         rhigh = av_clip(dhigh + c->band[1].s_predictor, -16384, 16383);
00123 
00124         ff_g722_update_high_predictor(&c->band[1], dhigh, ihigh);
00125 
00126         c->prev_samples[c->prev_samples_pos++] = rlow + rhigh;
00127         c->prev_samples[c->prev_samples_pos++] = rlow - rhigh;
00128         ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24,
00129                           &xout1, &xout2);
00130         *out_buf++ = av_clip_int16(xout1 >> 11);
00131         *out_buf++ = av_clip_int16(xout2 >> 11);
00132         if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
00133             memmove(c->prev_samples, c->prev_samples + c->prev_samples_pos - 22,
00134                     22 * sizeof(c->prev_samples[0]));
00135             c->prev_samples_pos = 22;
00136         }
00137     }
00138 
00139     *got_frame_ptr   = 1;
00140     *(AVFrame *)data = c->frame;
00141 
00142     return avpkt->size;
00143 }
00144 
00145 AVCodec ff_adpcm_g722_decoder = {
00146     .name           = "g722",
00147     .type           = AVMEDIA_TYPE_AUDIO,
00148     .id             = CODEC_ID_ADPCM_G722,
00149     .priv_data_size = sizeof(G722Context),
00150     .init           = g722_decode_init,
00151     .decode         = g722_decode_frame,
00152     .capabilities   = CODEC_CAP_DR1,
00153     .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
00154     .priv_class     = &g722_decoder_class,
00155 };