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