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 "audio_frame_queue.h"
00032 #include "internal.h"
00033 #include "put_bits.h"
00034 #include "celp_filters.h"
00035 #include "ra144.h"
00036
00037
00038 static av_cold int ra144_encode_close(AVCodecContext *avctx)
00039 {
00040 RA144Context *ractx = avctx->priv_data;
00041 ff_lpc_end(&ractx->lpc_ctx);
00042 ff_af_queue_close(&ractx->afq);
00043 #if FF_API_OLD_ENCODE_AUDIO
00044 av_freep(&avctx->coded_frame);
00045 #endif
00046 return 0;
00047 }
00048
00049
00050 static av_cold int ra144_encode_init(AVCodecContext * avctx)
00051 {
00052 RA144Context *ractx;
00053 int ret;
00054
00055 if (avctx->channels != 1) {
00056 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n",
00057 avctx->channels);
00058 return -1;
00059 }
00060 avctx->frame_size = NBLOCKS * BLOCKSIZE;
00061 avctx->delay = avctx->frame_size;
00062 avctx->bit_rate = 8000;
00063 ractx = avctx->priv_data;
00064 ractx->lpc_coef[0] = ractx->lpc_tables[0];
00065 ractx->lpc_coef[1] = ractx->lpc_tables[1];
00066 ractx->avctx = avctx;
00067 ret = ff_lpc_init(&ractx->lpc_ctx, avctx->frame_size, LPC_ORDER,
00068 FF_LPC_TYPE_LEVINSON);
00069 if (ret < 0)
00070 goto error;
00071
00072 ff_af_queue_init(avctx, &ractx->afq);
00073
00074 #if FF_API_OLD_ENCODE_AUDIO
00075 avctx->coded_frame = avcodec_alloc_frame();
00076 if (!avctx->coded_frame) {
00077 ret = AVERROR(ENOMEM);
00078 goto error;
00079 }
00080 #endif
00081
00082 return 0;
00083 error:
00084 ra144_encode_close(avctx);
00085 return ret;
00086 }
00087
00088
00099 static int quantize(int value, const int16_t *table, unsigned int size)
00100 {
00101 unsigned int low = 0, high = size - 1;
00102
00103 while (1) {
00104 int index = (low + high) >> 1;
00105 int error = table[index] - value;
00106
00107 if (index == low)
00108 return table[high] + error > value ? low : high;
00109 if (error > 0) {
00110 high = index;
00111 } else {
00112 low = index;
00113 }
00114 }
00115 }
00116
00117
00124 static void orthogonalize(float *v, const float *u)
00125 {
00126 int i;
00127 float num = 0, den = 0;
00128
00129 for (i = 0; i < BLOCKSIZE; i++) {
00130 num += v[i] * u[i];
00131 den += u[i] * u[i];
00132 }
00133 num /= den;
00134 for (i = 0; i < BLOCKSIZE; i++)
00135 v[i] -= num * u[i];
00136 }
00137
00138
00152 static void get_match_score(float *work, const float *coefs, float *vect,
00153 const float *ortho1, const float *ortho2,
00154 const float *data, float *score, float *gain)
00155 {
00156 float c, g;
00157 int i;
00158
00159 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
00160 if (ortho1)
00161 orthogonalize(work, ortho1);
00162 if (ortho2)
00163 orthogonalize(work, ortho2);
00164 c = g = 0;
00165 for (i = 0; i < BLOCKSIZE; i++) {
00166 g += work[i] * work[i];
00167 c += data[i] * work[i];
00168 }
00169 if (c <= 0) {
00170 *score = 0;
00171 return;
00172 }
00173 *gain = c / g;
00174 *score = *gain * c;
00175 }
00176
00177
00185 static void create_adapt_vect(float *vect, const int16_t *cb, int lag)
00186 {
00187 int i;
00188
00189 cb += BUFFERSIZE - lag;
00190 for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++)
00191 vect[i] = cb[i];
00192 if (lag < BLOCKSIZE)
00193 for (i = 0; i < BLOCKSIZE - lag; i++)
00194 vect[lag + i] = cb[i];
00195 }
00196
00197
00208 static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
00209 const float *coefs, float *data)
00210 {
00211 int i, best_vect;
00212 float score, gain, best_score, best_gain;
00213 float exc[BLOCKSIZE];
00214
00215 gain = best_score = 0;
00216 for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {
00217 create_adapt_vect(exc, adapt_cb, i);
00218 get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);
00219 if (score > best_score) {
00220 best_score = score;
00221 best_vect = i;
00222 best_gain = gain;
00223 }
00224 }
00225 if (!best_score)
00226 return 0;
00227
00232 create_adapt_vect(exc, adapt_cb, best_vect);
00233 ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER);
00234 for (i = 0; i < BLOCKSIZE; i++)
00235 data[i] -= best_gain * work[i];
00236 return best_vect - BLOCKSIZE / 2 + 1;
00237 }
00238
00239
00256 static void find_best_vect(float *work, const float *coefs,
00257 const int8_t cb[][BLOCKSIZE], const float *ortho1,
00258 const float *ortho2, float *data, int *idx,
00259 float *gain)
00260 {
00261 int i, j;
00262 float g, score, best_score;
00263 float vect[BLOCKSIZE];
00264
00265 *idx = *gain = best_score = 0;
00266 for (i = 0; i < FIXED_CB_SIZE; i++) {
00267 for (j = 0; j < BLOCKSIZE; j++)
00268 vect[j] = cb[i][j];
00269 get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g);
00270 if (score > best_score) {
00271 best_score = score;
00272 *idx = i;
00273 *gain = g;
00274 }
00275 }
00276 }
00277
00278
00291 static void fixed_cb_search(float *work, const float *coefs, float *data,
00292 int cba_idx, int *cb1_idx, int *cb2_idx)
00293 {
00294 int i, ortho_cb1;
00295 float gain;
00296 float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE];
00297 float vect[BLOCKSIZE];
00298
00303 if (cba_idx)
00304 memcpy(cba_vect, work, sizeof(cba_vect));
00305
00306 find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL,
00307 data, cb1_idx, &gain);
00308
00313 if (gain) {
00314 for (i = 0; i < BLOCKSIZE; i++)
00315 vect[i] = ff_cb1_vects[*cb1_idx][i];
00316 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
00317 if (cba_idx)
00318 orthogonalize(work, cba_vect);
00319 for (i = 0; i < BLOCKSIZE; i++)
00320 data[i] -= gain * work[i];
00321 memcpy(cb1_vect, work, sizeof(cb1_vect));
00322 ortho_cb1 = 1;
00323 } else
00324 ortho_cb1 = 0;
00325
00326 find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL,
00327 ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain);
00328 }
00329
00330
00340 static void ra144_encode_subblock(RA144Context *ractx,
00341 const int16_t *sblock_data,
00342 const int16_t *lpc_coefs, unsigned int rms,
00343 PutBitContext *pb)
00344 {
00345 float data[BLOCKSIZE] = { 0 }, work[LPC_ORDER + BLOCKSIZE];
00346 float coefs[LPC_ORDER];
00347 float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE];
00348 int16_t cba_vect[BLOCKSIZE];
00349 int cba_idx, cb1_idx, cb2_idx, gain;
00350 int i, n, m[3];
00351 float g[3];
00352 float error, best_error;
00353
00354 for (i = 0; i < LPC_ORDER; i++) {
00355 work[i] = ractx->curr_sblock[BLOCKSIZE + i];
00356 coefs[i] = lpc_coefs[i] * (1/4096.0);
00357 }
00358
00363 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, data, BLOCKSIZE,
00364 LPC_ORDER);
00365 for (i = 0; i < BLOCKSIZE; i++) {
00366 zero[i] = work[LPC_ORDER + i];
00367 data[i] = sblock_data[i] - zero[i];
00368 }
00369
00375 memset(work, 0, LPC_ORDER * sizeof(*work));
00376
00377 cba_idx = adaptive_cb_search(ractx->adapt_cb, work + LPC_ORDER, coefs,
00378 data);
00379 if (cba_idx) {
00384 memcpy(cba, work + LPC_ORDER, sizeof(cba));
00385
00386 ff_copy_and_dup(cba_vect, ractx->adapt_cb, cba_idx + BLOCKSIZE / 2 - 1);
00387 m[0] = (ff_irms(cba_vect) * rms) >> 12;
00388 }
00389 fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx);
00390 for (i = 0; i < BLOCKSIZE; i++) {
00391 cb1[i] = ff_cb1_vects[cb1_idx][i];
00392 cb2[i] = ff_cb2_vects[cb2_idx][i];
00393 }
00394 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb1, BLOCKSIZE,
00395 LPC_ORDER);
00396 memcpy(cb1, work + LPC_ORDER, sizeof(cb1));
00397 m[1] = (ff_cb1_base[cb1_idx] * rms) >> 8;
00398 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb2, BLOCKSIZE,
00399 LPC_ORDER);
00400 memcpy(cb2, work + LPC_ORDER, sizeof(cb2));
00401 m[2] = (ff_cb2_base[cb2_idx] * rms) >> 8;
00402 best_error = FLT_MAX;
00403 gain = 0;
00404 for (n = 0; n < 256; n++) {
00405 g[1] = ((ff_gain_val_tab[n][1] * m[1]) >> ff_gain_exp_tab[n]) *
00406 (1/4096.0);
00407 g[2] = ((ff_gain_val_tab[n][2] * m[2]) >> ff_gain_exp_tab[n]) *
00408 (1/4096.0);
00409 error = 0;
00410 if (cba_idx) {
00411 g[0] = ((ff_gain_val_tab[n][0] * m[0]) >> ff_gain_exp_tab[n]) *
00412 (1/4096.0);
00413 for (i = 0; i < BLOCKSIZE; i++) {
00414 data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] +
00415 g[2] * cb2[i];
00416 error += (data[i] - sblock_data[i]) *
00417 (data[i] - sblock_data[i]);
00418 }
00419 } else {
00420 for (i = 0; i < BLOCKSIZE; i++) {
00421 data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i];
00422 error += (data[i] - sblock_data[i]) *
00423 (data[i] - sblock_data[i]);
00424 }
00425 }
00426 if (error < best_error) {
00427 best_error = error;
00428 gain = n;
00429 }
00430 }
00431 put_bits(pb, 7, cba_idx);
00432 put_bits(pb, 8, gain);
00433 put_bits(pb, 7, cb1_idx);
00434 put_bits(pb, 7, cb2_idx);
00435 ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, rms,
00436 gain);
00437 }
00438
00439
00440 static int ra144_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00441 const AVFrame *frame, int *got_packet_ptr)
00442 {
00443 static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4};
00444 static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
00445 RA144Context *ractx = avctx->priv_data;
00446 PutBitContext pb;
00447 int32_t lpc_data[NBLOCKS * BLOCKSIZE];
00448 int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER];
00449 int shift[LPC_ORDER];
00450 int16_t block_coefs[NBLOCKS][LPC_ORDER];
00451 int lpc_refl[LPC_ORDER];
00452 unsigned int refl_rms[NBLOCKS];
00453 const int16_t *samples = frame ? (const int16_t *)frame->data[0] : NULL;
00454 int energy = 0;
00455 int i, idx, ret;
00456
00457 if (ractx->last_frame)
00458 return 0;
00459
00460 if ((ret = ff_alloc_packet2(avctx, avpkt, FRAMESIZE)))
00461 return ret;
00462
00470 for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) {
00471 lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i];
00472 energy += (lpc_data[i] * lpc_data[i]) >> 4;
00473 }
00474 if (frame) {
00475 int j;
00476 for (j = 0; j < frame->nb_samples && i < NBLOCKS * BLOCKSIZE; i++, j++) {
00477 lpc_data[i] = samples[j] >> 2;
00478 energy += (lpc_data[i] * lpc_data[i]) >> 4;
00479 }
00480 }
00481 if (i < NBLOCKS * BLOCKSIZE)
00482 memset(&lpc_data[i], 0, (NBLOCKS * BLOCKSIZE - i) * sizeof(*lpc_data));
00483 energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab,
00484 32)];
00485
00486 ff_lpc_calc_coefs(&ractx->lpc_ctx, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
00487 LPC_ORDER, 16, lpc_coefs, shift, FF_LPC_TYPE_LEVINSON,
00488 0, ORDER_METHOD_EST, 12, 0);
00489 for (i = 0; i < LPC_ORDER; i++)
00490 block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] <<
00491 (12 - shift[LPC_ORDER - 1]));
00492
00498 if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
00502 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
00503 if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
00504
00505 memset(lpc_refl, 0, sizeof(lpc_refl));
00506 }
00507 }
00508 init_put_bits(&pb, avpkt->data, avpkt->size);
00509 for (i = 0; i < LPC_ORDER; i++) {
00510 idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]);
00511 put_bits(&pb, bit_sizes[i], idx);
00512 lpc_refl[i] = ff_lpc_refl_cb[i][idx];
00513 }
00514 ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
00515 ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
00516 refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
00517 refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
00518 energy <= ractx->old_energy,
00519 ff_t_sqrt(energy * ractx->old_energy) >> 12);
00520 refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
00521 refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
00522 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]);
00523 put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32));
00524 for (i = 0; i < NBLOCKS; i++)
00525 ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE,
00526 block_coefs[i], refl_rms[i], &pb);
00527 flush_put_bits(&pb);
00528 ractx->old_energy = energy;
00529 ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
00530 FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
00531
00532
00533 i = 0;
00534 if (frame) {
00535 for (; i < frame->nb_samples; i++)
00536 ractx->curr_block[i] = samples[i] >> 2;
00537
00538 if ((ret = ff_af_queue_add(&ractx->afq, frame) < 0))
00539 return ret;
00540 } else
00541 ractx->last_frame = 1;
00542 memset(&ractx->curr_block[i], 0,
00543 (NBLOCKS * BLOCKSIZE - i) * sizeof(*ractx->curr_block));
00544
00545
00546 ff_af_queue_remove(&ractx->afq, avctx->frame_size, &avpkt->pts,
00547 &avpkt->duration);
00548
00549 avpkt->size = FRAMESIZE;
00550 *got_packet_ptr = 1;
00551 return 0;
00552 }
00553
00554
00555 AVCodec ff_ra_144_encoder = {
00556 .name = "real_144",
00557 .type = AVMEDIA_TYPE_AUDIO,
00558 .id = CODEC_ID_RA_144,
00559 .priv_data_size = sizeof(RA144Context),
00560 .init = ra144_encode_init,
00561 .encode2 = ra144_encode_frame,
00562 .close = ra144_encode_close,
00563 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
00564 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00565 AV_SAMPLE_FMT_NONE },
00566 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
00567 };