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 "avcodec.h"
00026 #include "bitstream.h"
00027 #include "ra144.h"
00028 #include "celp_filters.h"
00029
00030 #define NBLOCKS 4
00031 #define BLOCKSIZE 40
00032 #define BUFFERSIZE 146
00033
00034
00035 typedef struct {
00036 unsigned int old_energy;
00037
00038 unsigned int lpc_tables[2][10];
00039
00042 unsigned int *lpc_coef[2];
00043
00044 unsigned int lpc_refl_rms[2];
00045
00047 int16_t curr_sblock[50];
00048
00051 uint16_t adapt_cb[146+2];
00052 } RA144Context;
00053
00054 static av_cold int ra144_decode_init(AVCodecContext * avctx)
00055 {
00056 RA144Context *ractx = avctx->priv_data;
00057
00058 ractx->lpc_coef[0] = ractx->lpc_tables[0];
00059 ractx->lpc_coef[1] = ractx->lpc_tables[1];
00060
00061 avctx->sample_fmt = SAMPLE_FMT_S16;
00062 return 0;
00063 }
00064
00069 static int t_sqrt(unsigned int x)
00070 {
00071 int s = 2;
00072 while (x > 0xfff) {
00073 s++;
00074 x >>= 2;
00075 }
00076
00077 return ff_sqrt(x << 20) << s;
00078 }
00079
00084 static void eval_coefs(int *coefs, const int *refl)
00085 {
00086 int buffer[10];
00087 int *b1 = buffer;
00088 int *b2 = coefs;
00089 int i, j;
00090
00091 for (i=0; i < 10; i++) {
00092 b1[i] = refl[i] << 4;
00093
00094 for (j=0; j < i; j++)
00095 b1[j] = ((refl[i] * b2[i-j-1]) >> 12) + b2[j];
00096
00097 FFSWAP(int *, b1, b2);
00098 }
00099
00100 for (i=0; i < 10; i++)
00101 coefs[i] >>= 4;
00102 }
00103
00108 static void copy_and_dup(int16_t *target, const int16_t *source, int offset)
00109 {
00110 source += BUFFERSIZE - offset;
00111
00112 memcpy(target, source, FFMIN(BLOCKSIZE, offset)*sizeof(*target));
00113 if (offset < BLOCKSIZE)
00114 memcpy(target + offset, source, (BLOCKSIZE - offset)*sizeof(*target));
00115 }
00116
00118 static int irms(const int16_t *data)
00119 {
00120 unsigned int i, sum = 0;
00121
00122 for (i=0; i < BLOCKSIZE; i++)
00123 sum += data[i] * data[i];
00124
00125 if (sum == 0)
00126 return 0;
00127
00128 return 0x20000000 / (t_sqrt(sum) >> 8);
00129 }
00130
00131 static void add_wav(int16_t *dest, int n, int skip_first, int *m,
00132 const int16_t *s1, const int8_t *s2, const int8_t *s3)
00133 {
00134 int i;
00135 int v[3];
00136
00137 v[0] = 0;
00138 for (i=!skip_first; i<3; i++)
00139 v[i] = (gain_val_tab[n][i] * m[i]) >> gain_exp_tab[n];
00140
00141 if (v[0]) {
00142 for (i=0; i < BLOCKSIZE; i++)
00143 dest[i] = (s1[i]*v[0] + s2[i]*v[1] + s3[i]*v[2]) >> 12;
00144 } else {
00145 for (i=0; i < BLOCKSIZE; i++)
00146 dest[i] = ( s2[i]*v[1] + s3[i]*v[2]) >> 12;
00147 }
00148 }
00149
00150 static unsigned int rescale_rms(unsigned int rms, unsigned int energy)
00151 {
00152 return (rms * energy) >> 10;
00153 }
00154
00155 static unsigned int rms(const int *data)
00156 {
00157 int i;
00158 unsigned int res = 0x10000;
00159 int b = 10;
00160
00161 for (i=0; i < 10; i++) {
00162 res = (((0x1000000 - data[i]*data[i]) >> 12) * res) >> 12;
00163
00164 if (res == 0)
00165 return 0;
00166
00167 while (res <= 0x3fff) {
00168 b++;
00169 res <<= 2;
00170 }
00171 }
00172
00173 return t_sqrt(res) >> b;
00174 }
00175
00176 static void do_output_subblock(RA144Context *ractx, const uint16_t *lpc_coefs,
00177 int gval, GetBitContext *gb)
00178 {
00179 uint16_t buffer_a[40];
00180 uint16_t *block;
00181 int cba_idx = get_bits(gb, 7);
00182 int gain = get_bits(gb, 8);
00183 int cb1_idx = get_bits(gb, 7);
00184 int cb2_idx = get_bits(gb, 7);
00185 int m[3];
00186
00187 if (cba_idx) {
00188 cba_idx += BLOCKSIZE/2 - 1;
00189 copy_and_dup(buffer_a, ractx->adapt_cb, cba_idx);
00190 m[0] = (irms(buffer_a) * gval) >> 12;
00191 } else {
00192 m[0] = 0;
00193 }
00194
00195 m[1] = (cb1_base[cb1_idx] * gval) >> 8;
00196 m[2] = (cb2_base[cb2_idx] * gval) >> 8;
00197
00198 memmove(ractx->adapt_cb, ractx->adapt_cb + BLOCKSIZE,
00199 (BUFFERSIZE - BLOCKSIZE) * sizeof(*ractx->adapt_cb));
00200
00201 block = ractx->adapt_cb + BUFFERSIZE - BLOCKSIZE;
00202
00203 add_wav(block, gain, cba_idx, m, cba_idx? buffer_a: NULL,
00204 cb1_vects[cb1_idx], cb2_vects[cb2_idx]);
00205
00206 memcpy(ractx->curr_sblock, ractx->curr_sblock + 40,
00207 10*sizeof(*ractx->curr_sblock));
00208
00209 if (ff_celp_lp_synthesis_filter(ractx->curr_sblock + 10, lpc_coefs,
00210 block, BLOCKSIZE, 10, 1, 0xfff))
00211 memset(ractx->curr_sblock, 0, 50*sizeof(*ractx->curr_sblock));
00212 }
00213
00214 static void int_to_int16(int16_t *out, const int *inp)
00215 {
00216 int i;
00217
00218 for (i=0; i < 30; i++)
00219 *out++ = *inp++;
00220 }
00221
00229 static int eval_refl(int *refl, const int16_t *coefs, RA144Context *ractx)
00230 {
00231 int b, i, j;
00232 int buffer1[10];
00233 int buffer2[10];
00234 int *bp1 = buffer1;
00235 int *bp2 = buffer2;
00236
00237 for (i=0; i < 10; i++)
00238 buffer2[i] = coefs[i];
00239
00240 refl[9] = bp2[9];
00241
00242 if ((unsigned) bp2[9] + 0x1000 > 0x1fff) {
00243 av_log(ractx, AV_LOG_ERROR, "Overflow. Broken sample?\n");
00244 return 1;
00245 }
00246
00247 for (i=8; i >= 0; i--) {
00248 b = 0x1000-((bp2[i+1] * bp2[i+1]) >> 12);
00249
00250 if (!b)
00251 b = -2;
00252
00253 for (j=0; j <= i; j++)
00254 bp1[j] = ((bp2[j] - ((refl[i+1] * bp2[i-j]) >> 12)) * (0x1000000 / b)) >> 12;
00255
00256 if ((unsigned) bp1[i] + 0x1000 > 0x1fff)
00257 return 1;
00258
00259 refl[i] = bp1[i];
00260
00261 FFSWAP(int *, bp1, bp2);
00262 }
00263 return 0;
00264 }
00265
00266 static int interp(RA144Context *ractx, int16_t *out, int a,
00267 int copyold, int energy)
00268 {
00269 int work[10];
00270 int b = NBLOCKS - a;
00271 int i;
00272
00273
00274
00275 for (i=0; i<30; i++)
00276 out[i] = (a * ractx->lpc_coef[0][i] + b * ractx->lpc_coef[1][i])>> 2;
00277
00278 if (eval_refl(work, out, ractx)) {
00279
00280
00281 int_to_int16(out, ractx->lpc_coef[copyold]);
00282 return rescale_rms(ractx->lpc_refl_rms[copyold], energy);
00283 } else {
00284 return rescale_rms(rms(work), energy);
00285 }
00286 }
00287
00289 static int ra144_decode_frame(AVCodecContext * avctx, void *vdata,
00290 int *data_size, const uint8_t *buf, int buf_size)
00291 {
00292 static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
00293 unsigned int refl_rms[4];
00294 uint16_t block_coefs[4][30];
00295 unsigned int lpc_refl[10];
00296 int i, j;
00297 int16_t *data = vdata;
00298 unsigned int energy;
00299
00300 RA144Context *ractx = avctx->priv_data;
00301 GetBitContext gb;
00302
00303 if (*data_size < 2*160)
00304 return -1;
00305
00306 if(buf_size < 20) {
00307 av_log(avctx, AV_LOG_ERROR,
00308 "Frame too small (%d bytes). Truncated file?\n", buf_size);
00309 *data_size = 0;
00310 return buf_size;
00311 }
00312 init_get_bits(&gb, buf, 20 * 8);
00313
00314 for (i=0; i<10; i++)
00315 lpc_refl[i] = lpc_refl_cb[i][get_bits(&gb, sizes[i])];
00316
00317 eval_coefs(ractx->lpc_coef[0], lpc_refl);
00318 ractx->lpc_refl_rms[0] = rms(lpc_refl);
00319
00320 energy = energy_tab[get_bits(&gb, 5)];
00321
00322 refl_rms[0] = interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
00323 refl_rms[1] = interp(ractx, block_coefs[1], 2, energy <= ractx->old_energy,
00324 t_sqrt(energy*ractx->old_energy) >> 12);
00325 refl_rms[2] = interp(ractx, block_coefs[2], 3, 0, energy);
00326 refl_rms[3] = rescale_rms(ractx->lpc_refl_rms[0], energy);
00327
00328 int_to_int16(block_coefs[3], ractx->lpc_coef[0]);
00329
00330 for (i=0; i < 4; i++) {
00331 do_output_subblock(ractx, block_coefs[i], refl_rms[i], &gb);
00332
00333 for (j=0; j < BLOCKSIZE; j++)
00334 *data++ = av_clip_int16(ractx->curr_sblock[j + 10] << 2);
00335 }
00336
00337 ractx->old_energy = energy;
00338 ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
00339
00340 FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
00341
00342 *data_size = 2*160;
00343 return 20;
00344 }
00345
00346 AVCodec ra_144_decoder =
00347 {
00348 "real_144",
00349 CODEC_TYPE_AUDIO,
00350 CODEC_ID_RA_144,
00351 sizeof(RA144Context),
00352 ra144_decode_init,
00353 NULL,
00354 NULL,
00355 ra144_decode_frame,
00356 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
00357 };