• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 "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, { 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     /* get output buffer */
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 >> 12);
00130         *out_buf++ = av_clip_int16(xout2 >> 12);
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             = 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 };
Generated on Sun Apr 22 2012 21:54:01 for Libav by doxygen 1.7.1