annotate ffmpeg/libavcodec/ra144enc.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Real Audio 1.0 (14.4K) encoder
yading@10 3 * Copyright (c) 2010 Francesco Lavra <francescolavra@interfree.it>
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * Real Audio 1.0 (14.4K) encoder
yading@10 25 * @author Francesco Lavra <francescolavra@interfree.it>
yading@10 26 */
yading@10 27
yading@10 28 #include <float.h>
yading@10 29
yading@10 30 #include "avcodec.h"
yading@10 31 #include "audio_frame_queue.h"
yading@10 32 #include "internal.h"
yading@10 33 #include "put_bits.h"
yading@10 34 #include "celp_filters.h"
yading@10 35 #include "ra144.h"
yading@10 36
yading@10 37
yading@10 38 static av_cold int ra144_encode_close(AVCodecContext *avctx)
yading@10 39 {
yading@10 40 RA144Context *ractx = avctx->priv_data;
yading@10 41 ff_lpc_end(&ractx->lpc_ctx);
yading@10 42 ff_af_queue_close(&ractx->afq);
yading@10 43 return 0;
yading@10 44 }
yading@10 45
yading@10 46
yading@10 47 static av_cold int ra144_encode_init(AVCodecContext * avctx)
yading@10 48 {
yading@10 49 RA144Context *ractx;
yading@10 50 int ret;
yading@10 51
yading@10 52 if (avctx->channels != 1) {
yading@10 53 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n",
yading@10 54 avctx->channels);
yading@10 55 return -1;
yading@10 56 }
yading@10 57 avctx->frame_size = NBLOCKS * BLOCKSIZE;
yading@10 58 avctx->delay = avctx->frame_size;
yading@10 59 avctx->bit_rate = 8000;
yading@10 60 ractx = avctx->priv_data;
yading@10 61 ractx->lpc_coef[0] = ractx->lpc_tables[0];
yading@10 62 ractx->lpc_coef[1] = ractx->lpc_tables[1];
yading@10 63 ractx->avctx = avctx;
yading@10 64 ret = ff_lpc_init(&ractx->lpc_ctx, avctx->frame_size, LPC_ORDER,
yading@10 65 FF_LPC_TYPE_LEVINSON);
yading@10 66 if (ret < 0)
yading@10 67 goto error;
yading@10 68
yading@10 69 ff_af_queue_init(avctx, &ractx->afq);
yading@10 70
yading@10 71 return 0;
yading@10 72 error:
yading@10 73 ra144_encode_close(avctx);
yading@10 74 return ret;
yading@10 75 }
yading@10 76
yading@10 77
yading@10 78 /**
yading@10 79 * Quantize a value by searching a sorted table for the element with the
yading@10 80 * nearest value
yading@10 81 *
yading@10 82 * @param value value to quantize
yading@10 83 * @param table array containing the quantization table
yading@10 84 * @param size size of the quantization table
yading@10 85 * @return index of the quantization table corresponding to the element with the
yading@10 86 * nearest value
yading@10 87 */
yading@10 88 static int quantize(int value, const int16_t *table, unsigned int size)
yading@10 89 {
yading@10 90 unsigned int low = 0, high = size - 1;
yading@10 91
yading@10 92 while (1) {
yading@10 93 int index = (low + high) >> 1;
yading@10 94 int error = table[index] - value;
yading@10 95
yading@10 96 if (index == low)
yading@10 97 return table[high] + error > value ? low : high;
yading@10 98 if (error > 0) {
yading@10 99 high = index;
yading@10 100 } else {
yading@10 101 low = index;
yading@10 102 }
yading@10 103 }
yading@10 104 }
yading@10 105
yading@10 106
yading@10 107 /**
yading@10 108 * Orthogonalize a vector to another vector
yading@10 109 *
yading@10 110 * @param v vector to orthogonalize
yading@10 111 * @param u vector against which orthogonalization is performed
yading@10 112 */
yading@10 113 static void orthogonalize(float *v, const float *u)
yading@10 114 {
yading@10 115 int i;
yading@10 116 float num = 0, den = 0;
yading@10 117
yading@10 118 for (i = 0; i < BLOCKSIZE; i++) {
yading@10 119 num += v[i] * u[i];
yading@10 120 den += u[i] * u[i];
yading@10 121 }
yading@10 122 num /= den;
yading@10 123 for (i = 0; i < BLOCKSIZE; i++)
yading@10 124 v[i] -= num * u[i];
yading@10 125 }
yading@10 126
yading@10 127
yading@10 128 /**
yading@10 129 * Calculate match score and gain of an LPC-filtered vector with respect to
yading@10 130 * input data, possibly othogonalizing it to up to 2 other vectors
yading@10 131 *
yading@10 132 * @param work array used to calculate the filtered vector
yading@10 133 * @param coefs coefficients of the LPC filter
yading@10 134 * @param vect original vector
yading@10 135 * @param ortho1 first vector against which orthogonalization is performed
yading@10 136 * @param ortho2 second vector against which orthogonalization is performed
yading@10 137 * @param data input data
yading@10 138 * @param score pointer to variable where match score is returned
yading@10 139 * @param gain pointer to variable where gain is returned
yading@10 140 */
yading@10 141 static void get_match_score(float *work, const float *coefs, float *vect,
yading@10 142 const float *ortho1, const float *ortho2,
yading@10 143 const float *data, float *score, float *gain)
yading@10 144 {
yading@10 145 float c, g;
yading@10 146 int i;
yading@10 147
yading@10 148 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
yading@10 149 if (ortho1)
yading@10 150 orthogonalize(work, ortho1);
yading@10 151 if (ortho2)
yading@10 152 orthogonalize(work, ortho2);
yading@10 153 c = g = 0;
yading@10 154 for (i = 0; i < BLOCKSIZE; i++) {
yading@10 155 g += work[i] * work[i];
yading@10 156 c += data[i] * work[i];
yading@10 157 }
yading@10 158 if (c <= 0) {
yading@10 159 *score = 0;
yading@10 160 return;
yading@10 161 }
yading@10 162 *gain = c / g;
yading@10 163 *score = *gain * c;
yading@10 164 }
yading@10 165
yading@10 166
yading@10 167 /**
yading@10 168 * Create a vector from the adaptive codebook at a given lag value
yading@10 169 *
yading@10 170 * @param vect array where vector is stored
yading@10 171 * @param cb adaptive codebook
yading@10 172 * @param lag lag value
yading@10 173 */
yading@10 174 static void create_adapt_vect(float *vect, const int16_t *cb, int lag)
yading@10 175 {
yading@10 176 int i;
yading@10 177
yading@10 178 cb += BUFFERSIZE - lag;
yading@10 179 for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++)
yading@10 180 vect[i] = cb[i];
yading@10 181 if (lag < BLOCKSIZE)
yading@10 182 for (i = 0; i < BLOCKSIZE - lag; i++)
yading@10 183 vect[lag + i] = cb[i];
yading@10 184 }
yading@10 185
yading@10 186
yading@10 187 /**
yading@10 188 * Search the adaptive codebook for the best entry and gain and remove its
yading@10 189 * contribution from input data
yading@10 190 *
yading@10 191 * @param adapt_cb array from which the adaptive codebook is extracted
yading@10 192 * @param work array used to calculate LPC-filtered vectors
yading@10 193 * @param coefs coefficients of the LPC filter
yading@10 194 * @param data input data
yading@10 195 * @return index of the best entry of the adaptive codebook
yading@10 196 */
yading@10 197 static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
yading@10 198 const float *coefs, float *data)
yading@10 199 {
yading@10 200 int i, av_uninit(best_vect);
yading@10 201 float score, gain, best_score, av_uninit(best_gain);
yading@10 202 float exc[BLOCKSIZE];
yading@10 203
yading@10 204 gain = best_score = 0;
yading@10 205 for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {
yading@10 206 create_adapt_vect(exc, adapt_cb, i);
yading@10 207 get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);
yading@10 208 if (score > best_score) {
yading@10 209 best_score = score;
yading@10 210 best_vect = i;
yading@10 211 best_gain = gain;
yading@10 212 }
yading@10 213 }
yading@10 214 if (!best_score)
yading@10 215 return 0;
yading@10 216
yading@10 217 /**
yading@10 218 * Re-calculate the filtered vector from the vector with maximum match score
yading@10 219 * and remove its contribution from input data.
yading@10 220 */
yading@10 221 create_adapt_vect(exc, adapt_cb, best_vect);
yading@10 222 ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER);
yading@10 223 for (i = 0; i < BLOCKSIZE; i++)
yading@10 224 data[i] -= best_gain * work[i];
yading@10 225 return best_vect - BLOCKSIZE / 2 + 1;
yading@10 226 }
yading@10 227
yading@10 228
yading@10 229 /**
yading@10 230 * Find the best vector of a fixed codebook by applying an LPC filter to
yading@10 231 * codebook entries, possibly othogonalizing them to up to 2 other vectors and
yading@10 232 * matching the results with input data
yading@10 233 *
yading@10 234 * @param work array used to calculate the filtered vectors
yading@10 235 * @param coefs coefficients of the LPC filter
yading@10 236 * @param cb fixed codebook
yading@10 237 * @param ortho1 first vector against which orthogonalization is performed
yading@10 238 * @param ortho2 second vector against which orthogonalization is performed
yading@10 239 * @param data input data
yading@10 240 * @param idx pointer to variable where the index of the best codebook entry is
yading@10 241 * returned
yading@10 242 * @param gain pointer to variable where the gain of the best codebook entry is
yading@10 243 * returned
yading@10 244 */
yading@10 245 static void find_best_vect(float *work, const float *coefs,
yading@10 246 const int8_t cb[][BLOCKSIZE], const float *ortho1,
yading@10 247 const float *ortho2, float *data, int *idx,
yading@10 248 float *gain)
yading@10 249 {
yading@10 250 int i, j;
yading@10 251 float g, score, best_score;
yading@10 252 float vect[BLOCKSIZE];
yading@10 253
yading@10 254 *idx = *gain = best_score = 0;
yading@10 255 for (i = 0; i < FIXED_CB_SIZE; i++) {
yading@10 256 for (j = 0; j < BLOCKSIZE; j++)
yading@10 257 vect[j] = cb[i][j];
yading@10 258 get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g);
yading@10 259 if (score > best_score) {
yading@10 260 best_score = score;
yading@10 261 *idx = i;
yading@10 262 *gain = g;
yading@10 263 }
yading@10 264 }
yading@10 265 }
yading@10 266
yading@10 267
yading@10 268 /**
yading@10 269 * Search the two fixed codebooks for the best entry and gain
yading@10 270 *
yading@10 271 * @param work array used to calculate LPC-filtered vectors
yading@10 272 * @param coefs coefficients of the LPC filter
yading@10 273 * @param data input data
yading@10 274 * @param cba_idx index of the best entry of the adaptive codebook
yading@10 275 * @param cb1_idx pointer to variable where the index of the best entry of the
yading@10 276 * first fixed codebook is returned
yading@10 277 * @param cb2_idx pointer to variable where the index of the best entry of the
yading@10 278 * second fixed codebook is returned
yading@10 279 */
yading@10 280 static void fixed_cb_search(float *work, const float *coefs, float *data,
yading@10 281 int cba_idx, int *cb1_idx, int *cb2_idx)
yading@10 282 {
yading@10 283 int i, ortho_cb1;
yading@10 284 float gain;
yading@10 285 float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE];
yading@10 286 float vect[BLOCKSIZE];
yading@10 287
yading@10 288 /**
yading@10 289 * The filtered vector from the adaptive codebook can be retrieved from
yading@10 290 * work, because this function is called just after adaptive_cb_search().
yading@10 291 */
yading@10 292 if (cba_idx)
yading@10 293 memcpy(cba_vect, work, sizeof(cba_vect));
yading@10 294
yading@10 295 find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL,
yading@10 296 data, cb1_idx, &gain);
yading@10 297
yading@10 298 /**
yading@10 299 * Re-calculate the filtered vector from the vector with maximum match score
yading@10 300 * and remove its contribution from input data.
yading@10 301 */
yading@10 302 if (gain) {
yading@10 303 for (i = 0; i < BLOCKSIZE; i++)
yading@10 304 vect[i] = ff_cb1_vects[*cb1_idx][i];
yading@10 305 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
yading@10 306 if (cba_idx)
yading@10 307 orthogonalize(work, cba_vect);
yading@10 308 for (i = 0; i < BLOCKSIZE; i++)
yading@10 309 data[i] -= gain * work[i];
yading@10 310 memcpy(cb1_vect, work, sizeof(cb1_vect));
yading@10 311 ortho_cb1 = 1;
yading@10 312 } else
yading@10 313 ortho_cb1 = 0;
yading@10 314
yading@10 315 find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL,
yading@10 316 ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain);
yading@10 317 }
yading@10 318
yading@10 319
yading@10 320 /**
yading@10 321 * Encode a subblock of the current frame
yading@10 322 *
yading@10 323 * @param ractx encoder context
yading@10 324 * @param sblock_data input data of the subblock
yading@10 325 * @param lpc_coefs coefficients of the LPC filter
yading@10 326 * @param rms RMS of the reflection coefficients
yading@10 327 * @param pb pointer to PutBitContext of the current frame
yading@10 328 */
yading@10 329 static void ra144_encode_subblock(RA144Context *ractx,
yading@10 330 const int16_t *sblock_data,
yading@10 331 const int16_t *lpc_coefs, unsigned int rms,
yading@10 332 PutBitContext *pb)
yading@10 333 {
yading@10 334 float data[BLOCKSIZE] = { 0 }, work[LPC_ORDER + BLOCKSIZE];
yading@10 335 float coefs[LPC_ORDER];
yading@10 336 float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE];
yading@10 337 int16_t cba_vect[BLOCKSIZE];
yading@10 338 int cba_idx, cb1_idx, cb2_idx, gain;
yading@10 339 int i, n;
yading@10 340 unsigned m[3];
yading@10 341 float g[3];
yading@10 342 float error, best_error;
yading@10 343
yading@10 344 for (i = 0; i < LPC_ORDER; i++) {
yading@10 345 work[i] = ractx->curr_sblock[BLOCKSIZE + i];
yading@10 346 coefs[i] = lpc_coefs[i] * (1/4096.0);
yading@10 347 }
yading@10 348
yading@10 349 /**
yading@10 350 * Calculate the zero-input response of the LPC filter and subtract it from
yading@10 351 * input data.
yading@10 352 */
yading@10 353 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, data, BLOCKSIZE,
yading@10 354 LPC_ORDER);
yading@10 355 for (i = 0; i < BLOCKSIZE; i++) {
yading@10 356 zero[i] = work[LPC_ORDER + i];
yading@10 357 data[i] = sblock_data[i] - zero[i];
yading@10 358 }
yading@10 359
yading@10 360 /**
yading@10 361 * Codebook search is performed without taking into account the contribution
yading@10 362 * of the previous subblock, since it has been just subtracted from input
yading@10 363 * data.
yading@10 364 */
yading@10 365 memset(work, 0, LPC_ORDER * sizeof(*work));
yading@10 366
yading@10 367 cba_idx = adaptive_cb_search(ractx->adapt_cb, work + LPC_ORDER, coefs,
yading@10 368 data);
yading@10 369 if (cba_idx) {
yading@10 370 /**
yading@10 371 * The filtered vector from the adaptive codebook can be retrieved from
yading@10 372 * work, see implementation of adaptive_cb_search().
yading@10 373 */
yading@10 374 memcpy(cba, work + LPC_ORDER, sizeof(cba));
yading@10 375
yading@10 376 ff_copy_and_dup(cba_vect, ractx->adapt_cb, cba_idx + BLOCKSIZE / 2 - 1);
yading@10 377 m[0] = (ff_irms(cba_vect) * rms) >> 12;
yading@10 378 }
yading@10 379 fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx);
yading@10 380 for (i = 0; i < BLOCKSIZE; i++) {
yading@10 381 cb1[i] = ff_cb1_vects[cb1_idx][i];
yading@10 382 cb2[i] = ff_cb2_vects[cb2_idx][i];
yading@10 383 }
yading@10 384 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb1, BLOCKSIZE,
yading@10 385 LPC_ORDER);
yading@10 386 memcpy(cb1, work + LPC_ORDER, sizeof(cb1));
yading@10 387 m[1] = (ff_cb1_base[cb1_idx] * rms) >> 8;
yading@10 388 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb2, BLOCKSIZE,
yading@10 389 LPC_ORDER);
yading@10 390 memcpy(cb2, work + LPC_ORDER, sizeof(cb2));
yading@10 391 m[2] = (ff_cb2_base[cb2_idx] * rms) >> 8;
yading@10 392 best_error = FLT_MAX;
yading@10 393 gain = 0;
yading@10 394 for (n = 0; n < 256; n++) {
yading@10 395 g[1] = ((ff_gain_val_tab[n][1] * m[1]) >> ff_gain_exp_tab[n]) *
yading@10 396 (1/4096.0);
yading@10 397 g[2] = ((ff_gain_val_tab[n][2] * m[2]) >> ff_gain_exp_tab[n]) *
yading@10 398 (1/4096.0);
yading@10 399 error = 0;
yading@10 400 if (cba_idx) {
yading@10 401 g[0] = ((ff_gain_val_tab[n][0] * m[0]) >> ff_gain_exp_tab[n]) *
yading@10 402 (1/4096.0);
yading@10 403 for (i = 0; i < BLOCKSIZE; i++) {
yading@10 404 data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] +
yading@10 405 g[2] * cb2[i];
yading@10 406 error += (data[i] - sblock_data[i]) *
yading@10 407 (data[i] - sblock_data[i]);
yading@10 408 }
yading@10 409 } else {
yading@10 410 for (i = 0; i < BLOCKSIZE; i++) {
yading@10 411 data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i];
yading@10 412 error += (data[i] - sblock_data[i]) *
yading@10 413 (data[i] - sblock_data[i]);
yading@10 414 }
yading@10 415 }
yading@10 416 if (error < best_error) {
yading@10 417 best_error = error;
yading@10 418 gain = n;
yading@10 419 }
yading@10 420 }
yading@10 421 put_bits(pb, 7, cba_idx);
yading@10 422 put_bits(pb, 8, gain);
yading@10 423 put_bits(pb, 7, cb1_idx);
yading@10 424 put_bits(pb, 7, cb2_idx);
yading@10 425 ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, rms,
yading@10 426 gain);
yading@10 427 }
yading@10 428
yading@10 429
yading@10 430 static int ra144_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
yading@10 431 const AVFrame *frame, int *got_packet_ptr)
yading@10 432 {
yading@10 433 static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4};
yading@10 434 static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
yading@10 435 RA144Context *ractx = avctx->priv_data;
yading@10 436 PutBitContext pb;
yading@10 437 int32_t lpc_data[NBLOCKS * BLOCKSIZE];
yading@10 438 int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER];
yading@10 439 int shift[LPC_ORDER];
yading@10 440 int16_t block_coefs[NBLOCKS][LPC_ORDER];
yading@10 441 int lpc_refl[LPC_ORDER]; /**< reflection coefficients of the frame */
yading@10 442 unsigned int refl_rms[NBLOCKS]; /**< RMS of the reflection coefficients */
yading@10 443 const int16_t *samples = frame ? (const int16_t *)frame->data[0] : NULL;
yading@10 444 int energy = 0;
yading@10 445 int i, idx, ret;
yading@10 446
yading@10 447 if (ractx->last_frame)
yading@10 448 return 0;
yading@10 449
yading@10 450 if ((ret = ff_alloc_packet2(avctx, avpkt, FRAMESIZE)) < 0)
yading@10 451 return ret;
yading@10 452
yading@10 453 /**
yading@10 454 * Since the LPC coefficients are calculated on a frame centered over the
yading@10 455 * fourth subframe, to encode a given frame, data from the next frame is
yading@10 456 * needed. In each call to this function, the previous frame (whose data are
yading@10 457 * saved in the encoder context) is encoded, and data from the current frame
yading@10 458 * are saved in the encoder context to be used in the next function call.
yading@10 459 */
yading@10 460 for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) {
yading@10 461 lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i];
yading@10 462 energy += (lpc_data[i] * lpc_data[i]) >> 4;
yading@10 463 }
yading@10 464 if (frame) {
yading@10 465 int j;
yading@10 466 for (j = 0; j < frame->nb_samples && i < NBLOCKS * BLOCKSIZE; i++, j++) {
yading@10 467 lpc_data[i] = samples[j] >> 2;
yading@10 468 energy += (lpc_data[i] * lpc_data[i]) >> 4;
yading@10 469 }
yading@10 470 }
yading@10 471 if (i < NBLOCKS * BLOCKSIZE)
yading@10 472 memset(&lpc_data[i], 0, (NBLOCKS * BLOCKSIZE - i) * sizeof(*lpc_data));
yading@10 473 energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab,
yading@10 474 32)];
yading@10 475
yading@10 476 ff_lpc_calc_coefs(&ractx->lpc_ctx, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
yading@10 477 LPC_ORDER, 16, lpc_coefs, shift, FF_LPC_TYPE_LEVINSON,
yading@10 478 0, ORDER_METHOD_EST, 12, 0);
yading@10 479 for (i = 0; i < LPC_ORDER; i++)
yading@10 480 block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] <<
yading@10 481 (12 - shift[LPC_ORDER - 1]));
yading@10 482
yading@10 483 /**
yading@10 484 * TODO: apply perceptual weighting of the input speech through bandwidth
yading@10 485 * expansion of the LPC filter.
yading@10 486 */
yading@10 487
yading@10 488 if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
yading@10 489 /**
yading@10 490 * The filter is unstable: use the coefficients of the previous frame.
yading@10 491 */
yading@10 492 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
yading@10 493 if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
yading@10 494 /* the filter is still unstable. set reflection coeffs to zero. */
yading@10 495 memset(lpc_refl, 0, sizeof(lpc_refl));
yading@10 496 }
yading@10 497 }
yading@10 498 init_put_bits(&pb, avpkt->data, avpkt->size);
yading@10 499 for (i = 0; i < LPC_ORDER; i++) {
yading@10 500 idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]);
yading@10 501 put_bits(&pb, bit_sizes[i], idx);
yading@10 502 lpc_refl[i] = ff_lpc_refl_cb[i][idx];
yading@10 503 }
yading@10 504 ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
yading@10 505 ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
yading@10 506 refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
yading@10 507 refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
yading@10 508 energy <= ractx->old_energy,
yading@10 509 ff_t_sqrt(energy * ractx->old_energy) >> 12);
yading@10 510 refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
yading@10 511 refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
yading@10 512 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]);
yading@10 513 put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32));
yading@10 514 for (i = 0; i < NBLOCKS; i++)
yading@10 515 ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE,
yading@10 516 block_coefs[i], refl_rms[i], &pb);
yading@10 517 flush_put_bits(&pb);
yading@10 518 ractx->old_energy = energy;
yading@10 519 ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
yading@10 520 FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
yading@10 521
yading@10 522 /* copy input samples to current block for processing in next call */
yading@10 523 i = 0;
yading@10 524 if (frame) {
yading@10 525 for (; i < frame->nb_samples; i++)
yading@10 526 ractx->curr_block[i] = samples[i] >> 2;
yading@10 527
yading@10 528 if ((ret = ff_af_queue_add(&ractx->afq, frame)) < 0)
yading@10 529 return ret;
yading@10 530 } else
yading@10 531 ractx->last_frame = 1;
yading@10 532 memset(&ractx->curr_block[i], 0,
yading@10 533 (NBLOCKS * BLOCKSIZE - i) * sizeof(*ractx->curr_block));
yading@10 534
yading@10 535 /* Get the next frame pts/duration */
yading@10 536 ff_af_queue_remove(&ractx->afq, avctx->frame_size, &avpkt->pts,
yading@10 537 &avpkt->duration);
yading@10 538
yading@10 539 avpkt->size = FRAMESIZE;
yading@10 540 *got_packet_ptr = 1;
yading@10 541 return 0;
yading@10 542 }
yading@10 543
yading@10 544
yading@10 545 AVCodec ff_ra_144_encoder = {
yading@10 546 .name = "real_144",
yading@10 547 .type = AVMEDIA_TYPE_AUDIO,
yading@10 548 .id = AV_CODEC_ID_RA_144,
yading@10 549 .priv_data_size = sizeof(RA144Context),
yading@10 550 .init = ra144_encode_init,
yading@10 551 .encode2 = ra144_encode_frame,
yading@10 552 .close = ra144_encode_close,
yading@10 553 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
yading@10 554 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
yading@10 555 AV_SAMPLE_FMT_NONE },
yading@10 556 .supported_samplerates = (const int[]){ 8000, 0 },
yading@10 557 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
yading@10 558 };