00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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);
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];
00064 uint16_t block_coefs[4][10];
00065 unsigned int lpc_refl[10];
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 };