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 "internal.h"
00028 #include "get_bits.h"
00029 #include "ra144.h"
00030 
00031 
00032 static av_cold int ra144_decode_init(AVCodecContext * avctx)
00033 {
00034     RA144Context *ractx = avctx->priv_data;
00035 
00036     ractx->avctx = avctx;
00037 
00038     ractx->lpc_coef[0] = ractx->lpc_tables[0];
00039     ractx->lpc_coef[1] = ractx->lpc_tables[1];
00040 
00041     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00042 
00043     avcodec_get_frame_defaults(&ractx->frame);
00044     avctx->coded_frame = &ractx->frame;
00045 
00046     return 0;
00047 }
00048 
00049 static void do_output_subblock(RA144Context *ractx, const uint16_t  *lpc_coefs,
00050                                int gval, GetBitContext *gb)
00051 {
00052     int cba_idx = get_bits(gb, 7); // index of the adaptive CB, 0 if none
00053     int gain    = get_bits(gb, 8);
00054     int cb1_idx = get_bits(gb, 7);
00055     int cb2_idx = get_bits(gb, 7);
00056 
00057     ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, gval,
00058                           gain);
00059 }
00060 
00062 static int ra144_decode_frame(AVCodecContext * avctx, void *data,
00063                               int *got_frame_ptr, AVPacket *avpkt)
00064 {
00065     const uint8_t *buf = avpkt->data;
00066     int buf_size = avpkt->size;
00067     static const uint8_t sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
00068     unsigned int refl_rms[NBLOCKS];           // RMS of the reflection coefficients
00069     uint16_t block_coefs[NBLOCKS][LPC_ORDER]; // LPC coefficients of each sub-block
00070     unsigned int lpc_refl[LPC_ORDER];         // LPC reflection coefficients of the frame
00071     int i, j;
00072     int ret;
00073     int16_t *samples;
00074     unsigned int energy;
00075 
00076     RA144Context *ractx = avctx->priv_data;
00077     GetBitContext gb;
00078 
00079     /* get output buffer */
00080     ractx->frame.nb_samples = NBLOCKS * BLOCKSIZE;
00081     if ((ret = ff_get_buffer(avctx, &ractx->frame)) < 0) {
00082         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00083         return ret;
00084     }
00085     samples = (int16_t *)ractx->frame.data[0];
00086 
00087     if(buf_size < FRAMESIZE) {
00088         av_log(avctx, AV_LOG_ERROR,
00089                "Frame too small (%d bytes). Truncated file?\n", buf_size);
00090         *got_frame_ptr = 0;
00091         return buf_size;
00092     }
00093     init_get_bits(&gb, buf, FRAMESIZE * 8);
00094 
00095     for (i = 0; i < LPC_ORDER; i++)
00096         lpc_refl[i] = ff_lpc_refl_cb[i][get_bits(&gb, sizes[i])];
00097 
00098     ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
00099     ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
00100 
00101     energy = ff_energy_tab[get_bits(&gb, 5)];
00102 
00103     refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
00104     refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
00105                             energy <= ractx->old_energy,
00106                             ff_t_sqrt(energy*ractx->old_energy) >> 12);
00107     refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
00108     refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
00109 
00110     ff_int_to_int16(block_coefs[3], ractx->lpc_coef[0]);
00111 
00112     for (i=0; i < NBLOCKS; i++) {
00113         do_output_subblock(ractx, block_coefs[i], refl_rms[i], &gb);
00114 
00115         for (j=0; j < BLOCKSIZE; j++)
00116             *samples++ = av_clip_int16(ractx->curr_sblock[j + 10] << 2);
00117     }
00118 
00119     ractx->old_energy = energy;
00120     ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
00121 
00122     FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
00123 
00124     *got_frame_ptr   = 1;
00125     *(AVFrame *)data = ractx->frame;
00126 
00127     return FRAMESIZE;
00128 }
00129 
00130 AVCodec ff_ra_144_decoder = {
00131     .name           = "real_144",
00132     .type           = AVMEDIA_TYPE_AUDIO,
00133     .id             = CODEC_ID_RA_144,
00134     .priv_data_size = sizeof(RA144Context),
00135     .init           = ra144_decode_init,
00136     .decode         = ra144_decode_frame,
00137     .capabilities   = CODEC_CAP_DR1,
00138     .long_name      = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
00139 };