Libav 0.7.1
libavcodec/ra144dec.c
Go to the documentation of this file.
00001 /*
00002  * Real Audio 1.0 (14.4K)
00003  *
00004  * Copyright (c) 2008 Vitor Sessak
00005  * Copyright (c) 2003 Nick Kurshev
00006  *     Based on public domain decoder at http://www.honeypot.net/audio
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 
00025 #include "libavutil/intmath.h"
00026 #include "avcodec.h"
00027 #include "get_bits.h"
00028 #include "ra144.h"
00029 
00030 
00031 static av_cold int ra144_decode_init(AVCodecContext * avctx)
00032 {
00033     RA144Context *ractx = avctx->priv_data;
00034 
00035     ractx->avctx = avctx;
00036 
00037     ractx->lpc_coef[0] = ractx->lpc_tables[0];
00038     ractx->lpc_coef[1] = ractx->lpc_tables[1];
00039 
00040     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00041     return 0;
00042 }
00043 
00044 static void do_output_subblock(RA144Context *ractx, const uint16_t  *lpc_coefs,
00045                                int gval, GetBitContext *gb)
00046 {
00047     int cba_idx = get_bits(gb, 7); // index of the adaptive CB, 0 if none
00048     int gain    = get_bits(gb, 8);
00049     int cb1_idx = get_bits(gb, 7);
00050     int cb2_idx = get_bits(gb, 7);
00051 
00052     ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, gval,
00053                           gain);
00054 }
00055 
00057 static int ra144_decode_frame(AVCodecContext * avctx, void *vdata,
00058                               int *data_size, AVPacket *avpkt)
00059 {
00060     const uint8_t *buf = avpkt->data;
00061     int buf_size = avpkt->size;
00062     static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
00063     unsigned int refl_rms[4];    // RMS of the reflection coefficients
00064     uint16_t block_coefs[4][10]; // LPC coefficients of each sub-block
00065     unsigned int lpc_refl[10];   // LPC reflection coefficients of the frame
00066     int i, j;
00067     int16_t *data = vdata;
00068     unsigned int energy;
00069 
00070     RA144Context *ractx = avctx->priv_data;
00071     GetBitContext gb;
00072 
00073     if (*data_size < 2*160)
00074         return -1;
00075 
00076     if(buf_size < 20) {
00077         av_log(avctx, AV_LOG_ERROR,
00078                "Frame too small (%d bytes). Truncated file?\n", buf_size);
00079         *data_size = 0;
00080         return buf_size;
00081     }
00082     init_get_bits(&gb, buf, 20 * 8);
00083 
00084     for (i=0; i<10; i++)
00085         lpc_refl[i] = ff_lpc_refl_cb[i][get_bits(&gb, sizes[i])];
00086 
00087     ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
00088     ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
00089 
00090     energy = ff_energy_tab[get_bits(&gb, 5)];
00091 
00092     refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
00093     refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
00094                             energy <= ractx->old_energy,
00095                             ff_t_sqrt(energy*ractx->old_energy) >> 12);
00096     refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
00097     refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
00098 
00099     ff_int_to_int16(block_coefs[3], ractx->lpc_coef[0]);
00100 
00101     for (i=0; i < 4; i++) {
00102         do_output_subblock(ractx, block_coefs[i], refl_rms[i], &gb);
00103 
00104         for (j=0; j < BLOCKSIZE; j++)
00105             *data++ = av_clip_int16(ractx->curr_sblock[j + 10] << 2);
00106     }
00107 
00108     ractx->old_energy = energy;
00109     ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
00110 
00111     FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
00112 
00113     *data_size = 2*160;
00114     return 20;
00115 }
00116 
00117 AVCodec ff_ra_144_decoder =
00118 {
00119     "real_144",
00120     AVMEDIA_TYPE_AUDIO,
00121     CODEC_ID_RA_144,
00122     sizeof(RA144Context),
00123     ra144_decode_init,
00124     NULL,
00125     NULL,
00126     ra144_decode_frame,
00127     .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
00128 };