00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include <float.h>
00029
00030 #include "avcodec.h"
00031 #include "put_bits.h"
00032 #include "lpc.h"
00033 #include "celp_filters.h"
00034 #include "ra144.h"
00035
00036
00037 static av_cold int ra144_encode_init(AVCodecContext * avctx)
00038 {
00039 RA144Context *ractx;
00040
00041 if (avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
00042 av_log(avctx, AV_LOG_ERROR, "invalid sample format\n");
00043 return -1;
00044 }
00045 if (avctx->channels != 1) {
00046 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n",
00047 avctx->channels);
00048 return -1;
00049 }
00050 avctx->frame_size = NBLOCKS * BLOCKSIZE;
00051 avctx->bit_rate = 8000;
00052 ractx = avctx->priv_data;
00053 ractx->lpc_coef[0] = ractx->lpc_tables[0];
00054 ractx->lpc_coef[1] = ractx->lpc_tables[1];
00055 ractx->avctx = avctx;
00056 dsputil_init(&ractx->dsp, avctx);
00057 return 0;
00058 }
00059
00060
00071 static int quantize(int value, const int16_t *table, unsigned int size)
00072 {
00073 unsigned int low = 0, high = size - 1;
00074
00075 while (1) {
00076 int index = (low + high) >> 1;
00077 int error = table[index] - value;
00078
00079 if (index == low)
00080 return table[high] + error > value ? low : high;
00081 if (error > 0) {
00082 high = index;
00083 } else {
00084 low = index;
00085 }
00086 }
00087 }
00088
00089
00096 static void orthogonalize(float *v, const float *u)
00097 {
00098 int i;
00099 float num = 0, den = 0;
00100
00101 for (i = 0; i < BLOCKSIZE; i++) {
00102 num += v[i] * u[i];
00103 den += u[i] * u[i];
00104 }
00105 num /= den;
00106 for (i = 0; i < BLOCKSIZE; i++)
00107 v[i] -= num * u[i];
00108 }
00109
00110
00124 static void get_match_score(float *work, const float *coefs, float *vect,
00125 const float *ortho1, const float *ortho2,
00126 const float *data, float *score, float *gain)
00127 {
00128 float c, g;
00129 int i;
00130
00131 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
00132 if (ortho1)
00133 orthogonalize(work, ortho1);
00134 if (ortho2)
00135 orthogonalize(work, ortho2);
00136 c = g = 0;
00137 for (i = 0; i < BLOCKSIZE; i++) {
00138 g += work[i] * work[i];
00139 c += data[i] * work[i];
00140 }
00141 if (c <= 0) {
00142 *score = 0;
00143 return;
00144 }
00145 *gain = c / g;
00146 *score = *gain * c;
00147 }
00148
00149
00157 static void create_adapt_vect(float *vect, const int16_t *cb, int lag)
00158 {
00159 int i;
00160
00161 cb += BUFFERSIZE - lag;
00162 for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++)
00163 vect[i] = cb[i];
00164 if (lag < BLOCKSIZE)
00165 for (i = 0; i < BLOCKSIZE - lag; i++)
00166 vect[lag + i] = cb[i];
00167 }
00168
00169
00180 static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
00181 const float *coefs, float *data)
00182 {
00183 int i, best_vect;
00184 float score, gain, best_score, best_gain;
00185 float exc[BLOCKSIZE];
00186
00187 gain = best_score = 0;
00188 for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {
00189 create_adapt_vect(exc, adapt_cb, i);
00190 get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);
00191 if (score > best_score) {
00192 best_score = score;
00193 best_vect = i;
00194 best_gain = gain;
00195 }
00196 }
00197 if (!best_score)
00198 return 0;
00199
00204 create_adapt_vect(exc, adapt_cb, best_vect);
00205 ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER);
00206 for (i = 0; i < BLOCKSIZE; i++)
00207 data[i] -= best_gain * work[i];
00208 return (best_vect - BLOCKSIZE / 2 + 1);
00209 }
00210
00211
00228 static void find_best_vect(float *work, const float *coefs,
00229 const int8_t cb[][BLOCKSIZE], const float *ortho1,
00230 const float *ortho2, float *data, int *idx,
00231 float *gain)
00232 {
00233 int i, j;
00234 float g, score, best_score;
00235 float vect[BLOCKSIZE];
00236
00237 *idx = *gain = best_score = 0;
00238 for (i = 0; i < FIXED_CB_SIZE; i++) {
00239 for (j = 0; j < BLOCKSIZE; j++)
00240 vect[j] = cb[i][j];
00241 get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g);
00242 if (score > best_score) {
00243 best_score = score;
00244 *idx = i;
00245 *gain = g;
00246 }
00247 }
00248 }
00249
00250
00263 static void fixed_cb_search(float *work, const float *coefs, float *data,
00264 int cba_idx, int *cb1_idx, int *cb2_idx)
00265 {
00266 int i, ortho_cb1;
00267 float gain;
00268 float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE];
00269 float vect[BLOCKSIZE];
00270
00275 if (cba_idx)
00276 memcpy(cba_vect, work, sizeof(cba_vect));
00277
00278 find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL,
00279 data, cb1_idx, &gain);
00280
00285 if (gain) {
00286 for (i = 0; i < BLOCKSIZE; i++)
00287 vect[i] = ff_cb1_vects[*cb1_idx][i];
00288 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
00289 if (cba_idx)
00290 orthogonalize(work, cba_vect);
00291 for (i = 0; i < BLOCKSIZE; i++)
00292 data[i] -= gain * work[i];
00293 memcpy(cb1_vect, work, sizeof(cb1_vect));
00294 ortho_cb1 = 1;
00295 } else
00296 ortho_cb1 = 0;
00297
00298 find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL,
00299 ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain);
00300 }
00301
00302
00312 static void ra144_encode_subblock(RA144Context *ractx,
00313 const int16_t *sblock_data,
00314 const int16_t *lpc_coefs, unsigned int rms,
00315 PutBitContext *pb)
00316 {
00317 float data[BLOCKSIZE], work[LPC_ORDER + BLOCKSIZE];
00318 float coefs[LPC_ORDER];
00319 float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE];
00320 int16_t cba_vect[BLOCKSIZE];
00321 int cba_idx, cb1_idx, cb2_idx, gain;
00322 int i, n, m[3];
00323 float g[3];
00324 float error, best_error;
00325
00326 for (i = 0; i < LPC_ORDER; i++) {
00327 work[i] = ractx->curr_sblock[BLOCKSIZE + i];
00328 coefs[i] = lpc_coefs[i] * (1/4096.0);
00329 }
00330
00335 memset(data, 0, sizeof(data));
00336 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, data, BLOCKSIZE,
00337 LPC_ORDER);
00338 for (i = 0; i < BLOCKSIZE; i++) {
00339 zero[i] = work[LPC_ORDER + i];
00340 data[i] = sblock_data[i] - zero[i];
00341 }
00342
00348 memset(work, 0, LPC_ORDER * sizeof(*work));
00349
00350 cba_idx = adaptive_cb_search(ractx->adapt_cb, work + LPC_ORDER, coefs,
00351 data);
00352 if (cba_idx) {
00357 memcpy(cba, work + LPC_ORDER, sizeof(cba));
00358
00359 ff_copy_and_dup(cba_vect, ractx->adapt_cb, cba_idx + BLOCKSIZE / 2 - 1);
00360 m[0] = (ff_irms(cba_vect) * rms) >> 12;
00361 }
00362 fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx);
00363 for (i = 0; i < BLOCKSIZE; i++) {
00364 cb1[i] = ff_cb1_vects[cb1_idx][i];
00365 cb2[i] = ff_cb2_vects[cb2_idx][i];
00366 }
00367 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb1, BLOCKSIZE,
00368 LPC_ORDER);
00369 memcpy(cb1, work + LPC_ORDER, sizeof(cb1));
00370 m[1] = (ff_cb1_base[cb1_idx] * rms) >> 8;
00371 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb2, BLOCKSIZE,
00372 LPC_ORDER);
00373 memcpy(cb2, work + LPC_ORDER, sizeof(cb2));
00374 m[2] = (ff_cb2_base[cb2_idx] * rms) >> 8;
00375 best_error = FLT_MAX;
00376 gain = 0;
00377 for (n = 0; n < 256; n++) {
00378 g[1] = ((ff_gain_val_tab[n][1] * m[1]) >> ff_gain_exp_tab[n]) *
00379 (1/4096.0);
00380 g[2] = ((ff_gain_val_tab[n][2] * m[2]) >> ff_gain_exp_tab[n]) *
00381 (1/4096.0);
00382 error = 0;
00383 if (cba_idx) {
00384 g[0] = ((ff_gain_val_tab[n][0] * m[0]) >> ff_gain_exp_tab[n]) *
00385 (1/4096.0);
00386 for (i = 0; i < BLOCKSIZE; i++) {
00387 data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] +
00388 g[2] * cb2[i];
00389 error += (data[i] - sblock_data[i]) *
00390 (data[i] - sblock_data[i]);
00391 }
00392 } else {
00393 for (i = 0; i < BLOCKSIZE; i++) {
00394 data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i];
00395 error += (data[i] - sblock_data[i]) *
00396 (data[i] - sblock_data[i]);
00397 }
00398 }
00399 if (error < best_error) {
00400 best_error = error;
00401 gain = n;
00402 }
00403 }
00404 put_bits(pb, 7, cba_idx);
00405 put_bits(pb, 8, gain);
00406 put_bits(pb, 7, cb1_idx);
00407 put_bits(pb, 7, cb2_idx);
00408 ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, rms,
00409 gain);
00410 }
00411
00412
00413 static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame,
00414 int buf_size, void *data)
00415 {
00416 static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4};
00417 static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
00418 RA144Context *ractx;
00419 PutBitContext pb;
00420 int32_t lpc_data[NBLOCKS * BLOCKSIZE];
00421 int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER];
00422 int shift[LPC_ORDER];
00423 int16_t block_coefs[NBLOCKS][LPC_ORDER];
00424 int lpc_refl[LPC_ORDER];
00425 unsigned int refl_rms[NBLOCKS];
00426 int energy = 0;
00427 int i, idx;
00428
00429 if (buf_size < FRAMESIZE) {
00430 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
00431 return 0;
00432 }
00433 ractx = avctx->priv_data;
00434
00442 for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) {
00443 lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i];
00444 energy += (lpc_data[i] * lpc_data[i]) >> 4;
00445 }
00446 for (i = 2 * BLOCKSIZE + BLOCKSIZE / 2; i < NBLOCKS * BLOCKSIZE; i++) {
00447 lpc_data[i] = *((int16_t *)data + i - 2 * BLOCKSIZE - BLOCKSIZE / 2) >>
00448 2;
00449 energy += (lpc_data[i] * lpc_data[i]) >> 4;
00450 }
00451 energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab,
00452 32)];
00453
00454 ff_lpc_calc_coefs(&ractx->dsp, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
00455 LPC_ORDER, 16, lpc_coefs, shift, AV_LPC_TYPE_LEVINSON,
00456 0, ORDER_METHOD_EST, 12, 0);
00457 for (i = 0; i < LPC_ORDER; i++)
00458 block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] <<
00459 (12 - shift[LPC_ORDER - 1]));
00460
00466 if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
00470 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
00471 ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx);
00472 }
00473 init_put_bits(&pb, frame, buf_size);
00474 for (i = 0; i < LPC_ORDER; i++) {
00475 idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]);
00476 put_bits(&pb, bit_sizes[i], idx);
00477 lpc_refl[i] = ff_lpc_refl_cb[i][idx];
00478 }
00479 ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
00480 ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
00481 refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
00482 refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
00483 energy <= ractx->old_energy,
00484 ff_t_sqrt(energy * ractx->old_energy) >> 12);
00485 refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
00486 refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
00487 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]);
00488 put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32));
00489 for (i = 0; i < NBLOCKS; i++)
00490 ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE,
00491 block_coefs[i], refl_rms[i], &pb);
00492 flush_put_bits(&pb);
00493 ractx->old_energy = energy;
00494 ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
00495 FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
00496 for (i = 0; i < NBLOCKS * BLOCKSIZE; i++)
00497 ractx->curr_block[i] = *((int16_t *)data + i) >> 2;
00498 return FRAMESIZE;
00499 }
00500
00501
00502 AVCodec ra_144_encoder =
00503 {
00504 "real_144",
00505 AVMEDIA_TYPE_AUDIO,
00506 CODEC_ID_RA_144,
00507 sizeof(RA144Context),
00508 ra144_encode_init,
00509 ra144_encode_frame,
00510 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K) encoder"),
00511 };