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 "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);
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];
00069 uint16_t block_coefs[NBLOCKS][LPC_ORDER];
00070 unsigned int lpc_refl[LPC_ORDER];
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
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 };