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