2 * Real Audio 1.0 (14.4K) encoder
3 * Copyright (c) 2010 Francesco Lavra <francescolavra@interfree.it>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Real Audio 1.0 (14.4K) encoder
25 * @author Francesco Lavra <francescolavra@interfree.it>
33 #include "celp_filters.h"
37 static av_cold int ra144_encode_init(AVCodecContext * avctx)
41 if (avctx->sample_fmt != SAMPLE_FMT_S16) {
42 av_log(avctx, AV_LOG_ERROR, "invalid sample format\n");
45 if (avctx->channels != 1) {
46 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n",
50 avctx->frame_size = NBLOCKS * BLOCKSIZE;
51 avctx->bit_rate = 8000;
52 ractx = avctx->priv_data;
53 ractx->lpc_coef[0] = ractx->lpc_tables[0];
54 ractx->lpc_coef[1] = ractx->lpc_tables[1];
56 dsputil_init(&ractx->dsp, avctx);
62 * Quantize a value by searching a sorted table for the element with the
65 * @param value value to quantize
66 * @param table array containing the quantization table
67 * @param size size of the quantization table
68 * @return index of the quantization table corresponding to the element with the
71 static int quantize(int value, const int16_t *table, unsigned int size)
73 unsigned int low = 0, high = size - 1;
76 int index = (low + high) >> 1;
77 int error = table[index] - value;
80 return table[high] + error > value ? low : high;
91 * Orthogonalize a vector to another vector
93 * @param v vector to orthogonalize
94 * @param u vector against which orthogonalization is performed
96 static void orthogonalize(float *v, const float *u)
99 float num = 0, den = 0;
101 for (i = 0; i < BLOCKSIZE; i++) {
106 for (i = 0; i < BLOCKSIZE; i++)
112 * Calculate match score and gain of an LPC-filtered vector with respect to
113 * input data, possibly othogonalizing it to up to 2 other vectors
115 * @param work array used to calculate the filtered vector
116 * @param coefs coefficients of the LPC filter
117 * @param vect original vector
118 * @param ortho1 first vector against which orthogonalization is performed
119 * @param ortho2 second vector against which orthogonalization is performed
120 * @param data input data
121 * @param score pointer to variable where match score is returned
122 * @param gain pointer to variable where gain is returned
124 static void get_match_score(float *work, const float *coefs, float *vect,
125 const float *ortho1, const float *ortho2,
126 const float *data, float *score, float *gain)
131 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
133 orthogonalize(work, ortho1);
135 orthogonalize(work, ortho2);
137 for (i = 0; i < BLOCKSIZE; i++) {
138 g += work[i] * work[i];
139 c += data[i] * work[i];
151 * Create a vector from the adaptive codebook at a given lag value
153 * @param vect array where vector is stored
154 * @param cb adaptive codebook
155 * @param lag lag value
157 static void create_adapt_vect(float *vect, const int16_t *cb, int lag)
161 cb += BUFFERSIZE - lag;
162 for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++)
165 for (i = 0; i < BLOCKSIZE - lag; i++)
166 vect[lag + i] = cb[i];
171 * Search the adaptive codebook for the best entry and gain and remove its
172 * contribution from input data
174 * @param adapt_cb array from which the adaptive codebook is extracted
175 * @param work array used to calculate LPC-filtered vectors
176 * @param coefs coefficients of the LPC filter
177 * @param data input data
178 * @return index of the best entry of the adaptive codebook
180 static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
181 const float *coefs, float *data)
184 float score, gain, best_score, best_gain;
185 float exc[BLOCKSIZE];
187 gain = best_score = 0;
188 for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {
189 create_adapt_vect(exc, adapt_cb, i);
190 get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);
191 if (score > best_score) {
201 * Re-calculate the filtered vector from the vector with maximum match score
202 * and remove its contribution from input data.
204 create_adapt_vect(exc, adapt_cb, best_vect);
205 ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER);
206 for (i = 0; i < BLOCKSIZE; i++)
207 data[i] -= best_gain * work[i];
208 return (best_vect - BLOCKSIZE / 2 + 1);
213 * Find the best vector of a fixed codebook by applying an LPC filter to
214 * codebook entries, possibly othogonalizing them to up to 2 other vectors and
215 * matching the results with input data
217 * @param work array used to calculate the filtered vectors
218 * @param coefs coefficients of the LPC filter
219 * @param cb fixed codebook
220 * @param ortho1 first vector against which orthogonalization is performed
221 * @param ortho2 second vector against which orthogonalization is performed
222 * @param data input data
223 * @param idx pointer to variable where the index of the best codebook entry is
225 * @param gain pointer to variable where the gain of the best codebook entry is
228 static void find_best_vect(float *work, const float *coefs,
229 const int8_t cb[][BLOCKSIZE], const float *ortho1,
230 const float *ortho2, float *data, int *idx,
234 float g, score, best_score;
235 float vect[BLOCKSIZE];
237 *idx = *gain = best_score = 0;
238 for (i = 0; i < FIXED_CB_SIZE; i++) {
239 for (j = 0; j < BLOCKSIZE; j++)
241 get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g);
242 if (score > best_score) {
252 * Search the two fixed codebooks for the best entry and gain
254 * @param work array used to calculate LPC-filtered vectors
255 * @param coefs coefficients of the LPC filter
256 * @param data input data
257 * @param cba_idx index of the best entry of the adaptive codebook
258 * @param cb1_idx pointer to variable where the index of the best entry of the
259 * first fixed codebook is returned
260 * @param cb2_idx pointer to variable where the index of the best entry of the
261 * second fixed codebook is returned
263 static void fixed_cb_search(float *work, const float *coefs, float *data,
264 int cba_idx, int *cb1_idx, int *cb2_idx)
268 float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE];
269 float vect[BLOCKSIZE];
272 * The filtered vector from the adaptive codebook can be retrieved from
273 * work, because this function is called just after adaptive_cb_search().
276 memcpy(cba_vect, work, sizeof(cba_vect));
278 find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL,
279 data, cb1_idx, &gain);
282 * Re-calculate the filtered vector from the vector with maximum match score
283 * and remove its contribution from input data.
286 for (i = 0; i < BLOCKSIZE; i++)
287 vect[i] = ff_cb1_vects[*cb1_idx][i];
288 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
290 orthogonalize(work, cba_vect);
291 for (i = 0; i < BLOCKSIZE; i++)
292 data[i] -= gain * work[i];
293 memcpy(cb1_vect, work, sizeof(cb1_vect));
298 find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL,
299 ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain);
304 * Encode a subblock of the current frame
306 * @param ractx encoder context
307 * @param sblock_data input data of the subblock
308 * @param lpc_coefs coefficients of the LPC filter
309 * @param rms RMS of the reflection coefficients
310 * @param pb pointer to PutBitContext of the current frame
312 static void ra144_encode_subblock(RA144Context *ractx,
313 const int16_t *sblock_data,
314 const int16_t *lpc_coefs, unsigned int rms,
317 float data[BLOCKSIZE], work[LPC_ORDER + BLOCKSIZE];
318 float coefs[LPC_ORDER];
319 float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE];
320 int16_t cba_vect[BLOCKSIZE];
321 int cba_idx, cb1_idx, cb2_idx, gain;
324 float error, best_error;
326 for (i = 0; i < LPC_ORDER; i++) {
327 work[i] = ractx->curr_sblock[BLOCKSIZE + i];
328 coefs[i] = lpc_coefs[i] * (1/4096.0);
332 * Calculate the zero-input response of the LPC filter and subtract it from
335 memset(data, 0, sizeof(data));
336 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, data, BLOCKSIZE,
338 for (i = 0; i < BLOCKSIZE; i++) {
339 zero[i] = work[LPC_ORDER + i];
340 data[i] = sblock_data[i] - zero[i];
344 * Codebook search is performed without taking into account the contribution
345 * of the previous subblock, since it has been just subtracted from input
348 memset(work, 0, LPC_ORDER * sizeof(*work));
350 cba_idx = adaptive_cb_search(ractx->adapt_cb, work + LPC_ORDER, coefs,
354 * The filtered vector from the adaptive codebook can be retrieved from
355 * work, see implementation of adaptive_cb_search().
357 memcpy(cba, work + LPC_ORDER, sizeof(cba));
359 ff_copy_and_dup(cba_vect, ractx->adapt_cb, cba_idx + BLOCKSIZE / 2 - 1);
360 m[0] = (ff_irms(cba_vect) * rms) >> 12;
362 fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx);
363 for (i = 0; i < BLOCKSIZE; i++) {
364 cb1[i] = ff_cb1_vects[cb1_idx][i];
365 cb2[i] = ff_cb2_vects[cb2_idx][i];
367 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb1, BLOCKSIZE,
369 memcpy(cb1, work + LPC_ORDER, sizeof(cb1));
370 m[1] = (ff_cb1_base[cb1_idx] * rms) >> 8;
371 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb2, BLOCKSIZE,
373 memcpy(cb2, work + LPC_ORDER, sizeof(cb2));
374 m[2] = (ff_cb2_base[cb2_idx] * rms) >> 8;
375 best_error = FLT_MAX;
377 for (n = 0; n < 256; n++) {
378 g[1] = ((ff_gain_val_tab[n][1] * m[1]) >> ff_gain_exp_tab[n]) *
380 g[2] = ((ff_gain_val_tab[n][2] * m[2]) >> ff_gain_exp_tab[n]) *
384 g[0] = ((ff_gain_val_tab[n][0] * m[0]) >> ff_gain_exp_tab[n]) *
386 for (i = 0; i < BLOCKSIZE; i++) {
387 data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] +
389 error += (data[i] - sblock_data[i]) *
390 (data[i] - sblock_data[i]);
393 for (i = 0; i < BLOCKSIZE; i++) {
394 data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i];
395 error += (data[i] - sblock_data[i]) *
396 (data[i] - sblock_data[i]);
399 if (error < best_error) {
404 put_bits(pb, 7, cba_idx);
405 put_bits(pb, 8, gain);
406 put_bits(pb, 7, cb1_idx);
407 put_bits(pb, 7, cb2_idx);
408 ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, rms,
413 static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame,
414 int buf_size, void *data)
416 static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4};
417 static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
420 int32_t lpc_data[NBLOCKS * BLOCKSIZE];
421 int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER];
422 int shift[LPC_ORDER];
423 int16_t block_coefs[NBLOCKS][LPC_ORDER];
424 int lpc_refl[LPC_ORDER]; /**< reflection coefficients of the frame */
425 unsigned int refl_rms[NBLOCKS]; /**< RMS of the reflection coefficients */
429 if (buf_size < FRAMESIZE) {
430 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
433 ractx = avctx->priv_data;
436 * Since the LPC coefficients are calculated on a frame centered over the
437 * fourth subframe, to encode a given frame, data from the next frame is
438 * needed. In each call to this function, the previous frame (whose data are
439 * saved in the encoder context) is encoded, and data from the current frame
440 * are saved in the encoder context to be used in the next function call.
442 for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) {
443 lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i];
444 energy += (lpc_data[i] * lpc_data[i]) >> 4;
446 for (i = 2 * BLOCKSIZE + BLOCKSIZE / 2; i < NBLOCKS * BLOCKSIZE; i++) {
447 lpc_data[i] = *((int16_t *)data + i - 2 * BLOCKSIZE - BLOCKSIZE / 2) >>
449 energy += (lpc_data[i] * lpc_data[i]) >> 4;
451 energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab,
454 ff_lpc_calc_coefs(&ractx->dsp, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
455 LPC_ORDER, 16, lpc_coefs, shift, AV_LPC_TYPE_LEVINSON,
456 0, ORDER_METHOD_EST, 12, 0);
457 for (i = 0; i < LPC_ORDER; i++)
458 block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] <<
459 (12 - shift[LPC_ORDER - 1]));
462 * TODO: apply perceptual weighting of the input speech through bandwidth
463 * expansion of the LPC filter.
466 if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
468 * The filter is unstable: use the coefficients of the previous frame.
470 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
471 ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx);
473 init_put_bits(&pb, frame, buf_size);
474 for (i = 0; i < LPC_ORDER; i++) {
475 idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]);
476 put_bits(&pb, bit_sizes[i], idx);
477 lpc_refl[i] = ff_lpc_refl_cb[i][idx];
479 ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
480 ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
481 refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
482 refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
483 energy <= ractx->old_energy,
484 ff_t_sqrt(energy * ractx->old_energy) >> 12);
485 refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
486 refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
487 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]);
488 put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32));
489 for (i = 0; i < NBLOCKS; i++)
490 ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE,
491 block_coefs[i], refl_rms[i], &pb);
493 ractx->old_energy = energy;
494 ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
495 FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
496 for (i = 0; i < NBLOCKS * BLOCKSIZE; i++)
497 ractx->curr_block[i] = *((int16_t *)data + i) >> 2;
502 AVCodec ra_144_encoder =
507 sizeof(RA144Context),
510 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K) encoder"),