ra144enc.c
Go to the documentation of this file.
1 /*
2  * Real Audio 1.0 (14.4K) encoder
3  * Copyright (c) 2010 Francesco Lavra <francescolavra@interfree.it>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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.
11  *
12  * Libav 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include <float.h>
29 
30 #include "avcodec.h"
31 #include "audio_frame_queue.h"
32 #include "internal.h"
33 #include "put_bits.h"
34 #include "celp_filters.h"
35 #include "ra144.h"
36 
37 
39 {
40  RA144Context *ractx = avctx->priv_data;
41  ff_lpc_end(&ractx->lpc_ctx);
42  ff_af_queue_close(&ractx->afq);
43 #if FF_API_OLD_ENCODE_AUDIO
44  av_freep(&avctx->coded_frame);
45 #endif
46  return 0;
47 }
48 
49 
51 {
52  RA144Context *ractx;
53  int ret;
54 
55  if (avctx->channels != 1) {
56  av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n",
57  avctx->channels);
58  return -1;
59  }
60  avctx->frame_size = NBLOCKS * BLOCKSIZE;
61  avctx->delay = avctx->frame_size;
62  avctx->bit_rate = 8000;
63  ractx = avctx->priv_data;
64  ractx->lpc_coef[0] = ractx->lpc_tables[0];
65  ractx->lpc_coef[1] = ractx->lpc_tables[1];
66  ractx->avctx = avctx;
67  ret = ff_lpc_init(&ractx->lpc_ctx, avctx->frame_size, LPC_ORDER,
69  if (ret < 0)
70  goto error;
71 
72  ff_af_queue_init(avctx, &ractx->afq);
73 
74 #if FF_API_OLD_ENCODE_AUDIO
76  if (!avctx->coded_frame) {
77  ret = AVERROR(ENOMEM);
78  goto error;
79  }
80 #endif
81 
82  return 0;
83 error:
84  ra144_encode_close(avctx);
85  return ret;
86 }
87 
88 
99 static int quantize(int value, const int16_t *table, unsigned int size)
100 {
101  unsigned int low = 0, high = size - 1;
102 
103  while (1) {
104  int index = (low + high) >> 1;
105  int error = table[index] - value;
106 
107  if (index == low)
108  return table[high] + error > value ? low : high;
109  if (error > 0) {
110  high = index;
111  } else {
112  low = index;
113  }
114  }
115 }
116 
117 
124 static void orthogonalize(float *v, const float *u)
125 {
126  int i;
127  float num = 0, den = 0;
128 
129  for (i = 0; i < BLOCKSIZE; i++) {
130  num += v[i] * u[i];
131  den += u[i] * u[i];
132  }
133  num /= den;
134  for (i = 0; i < BLOCKSIZE; i++)
135  v[i] -= num * u[i];
136 }
137 
138 
152 static void get_match_score(float *work, const float *coefs, float *vect,
153  const float *ortho1, const float *ortho2,
154  const float *data, float *score, float *gain)
155 {
156  float c, g;
157  int i;
158 
159  ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
160  if (ortho1)
161  orthogonalize(work, ortho1);
162  if (ortho2)
163  orthogonalize(work, ortho2);
164  c = g = 0;
165  for (i = 0; i < BLOCKSIZE; i++) {
166  g += work[i] * work[i];
167  c += data[i] * work[i];
168  }
169  if (c <= 0) {
170  *score = 0;
171  return;
172  }
173  *gain = c / g;
174  *score = *gain * c;
175 }
176 
177 
185 static void create_adapt_vect(float *vect, const int16_t *cb, int lag)
186 {
187  int i;
188 
189  cb += BUFFERSIZE - lag;
190  for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++)
191  vect[i] = cb[i];
192  if (lag < BLOCKSIZE)
193  for (i = 0; i < BLOCKSIZE - lag; i++)
194  vect[lag + i] = cb[i];
195 }
196 
197 
208 static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
209  const float *coefs, float *data)
210 {
211  int i, best_vect;
212  float score, gain, best_score, best_gain;
213  float exc[BLOCKSIZE];
214 
215  gain = best_score = 0;
216  for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {
217  create_adapt_vect(exc, adapt_cb, i);
218  get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);
219  if (score > best_score) {
220  best_score = score;
221  best_vect = i;
222  best_gain = gain;
223  }
224  }
225  if (!best_score)
226  return 0;
227 
232  create_adapt_vect(exc, adapt_cb, best_vect);
234  for (i = 0; i < BLOCKSIZE; i++)
235  data[i] -= best_gain * work[i];
236  return best_vect - BLOCKSIZE / 2 + 1;
237 }
238 
239 
256 static void find_best_vect(float *work, const float *coefs,
257  const int8_t cb[][BLOCKSIZE], const float *ortho1,
258  const float *ortho2, float *data, int *idx,
259  float *gain)
260 {
261  int i, j;
262  float g, score, best_score;
263  float vect[BLOCKSIZE];
264 
265  *idx = *gain = best_score = 0;
266  for (i = 0; i < FIXED_CB_SIZE; i++) {
267  for (j = 0; j < BLOCKSIZE; j++)
268  vect[j] = cb[i][j];
269  get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g);
270  if (score > best_score) {
271  best_score = score;
272  *idx = i;
273  *gain = g;
274  }
275  }
276 }
277 
278 
291 static void fixed_cb_search(float *work, const float *coefs, float *data,
292  int cba_idx, int *cb1_idx, int *cb2_idx)
293 {
294  int i, ortho_cb1;
295  float gain;
296  float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE];
297  float vect[BLOCKSIZE];
298 
303  if (cba_idx)
304  memcpy(cba_vect, work, sizeof(cba_vect));
305 
306  find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL,
307  data, cb1_idx, &gain);
308 
313  if (gain) {
314  for (i = 0; i < BLOCKSIZE; i++)
315  vect[i] = ff_cb1_vects[*cb1_idx][i];
316  ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
317  if (cba_idx)
318  orthogonalize(work, cba_vect);
319  for (i = 0; i < BLOCKSIZE; i++)
320  data[i] -= gain * work[i];
321  memcpy(cb1_vect, work, sizeof(cb1_vect));
322  ortho_cb1 = 1;
323  } else
324  ortho_cb1 = 0;
325 
326  find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL,
327  ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain);
328 }
329 
330 
341  const int16_t *sblock_data,
342  const int16_t *lpc_coefs, unsigned int rms,
343  PutBitContext *pb)
344 {
345  float data[BLOCKSIZE] = { 0 }, work[LPC_ORDER + BLOCKSIZE];
346  float coefs[LPC_ORDER];
347  float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE];
348  int16_t cba_vect[BLOCKSIZE];
349  int cba_idx, cb1_idx, cb2_idx, gain;
350  int i, n, m[3];
351  float g[3];
352  float error, best_error;
353 
354  for (i = 0; i < LPC_ORDER; i++) {
355  work[i] = ractx->curr_sblock[BLOCKSIZE + i];
356  coefs[i] = lpc_coefs[i] * (1/4096.0);
357  }
358 
363  ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, data, BLOCKSIZE,
364  LPC_ORDER);
365  for (i = 0; i < BLOCKSIZE; i++) {
366  zero[i] = work[LPC_ORDER + i];
367  data[i] = sblock_data[i] - zero[i];
368  }
369 
375  memset(work, 0, LPC_ORDER * sizeof(*work));
376 
377  cba_idx = adaptive_cb_search(ractx->adapt_cb, work + LPC_ORDER, coefs,
378  data);
379  if (cba_idx) {
384  memcpy(cba, work + LPC_ORDER, sizeof(cba));
385 
386  ff_copy_and_dup(cba_vect, ractx->adapt_cb, cba_idx + BLOCKSIZE / 2 - 1);
387  m[0] = (ff_irms(cba_vect) * rms) >> 12;
388  }
389  fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx);
390  for (i = 0; i < BLOCKSIZE; i++) {
391  cb1[i] = ff_cb1_vects[cb1_idx][i];
392  cb2[i] = ff_cb2_vects[cb2_idx][i];
393  }
394  ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb1, BLOCKSIZE,
395  LPC_ORDER);
396  memcpy(cb1, work + LPC_ORDER, sizeof(cb1));
397  m[1] = (ff_cb1_base[cb1_idx] * rms) >> 8;
398  ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb2, BLOCKSIZE,
399  LPC_ORDER);
400  memcpy(cb2, work + LPC_ORDER, sizeof(cb2));
401  m[2] = (ff_cb2_base[cb2_idx] * rms) >> 8;
402  best_error = FLT_MAX;
403  gain = 0;
404  for (n = 0; n < 256; n++) {
405  g[1] = ((ff_gain_val_tab[n][1] * m[1]) >> ff_gain_exp_tab[n]) *
406  (1/4096.0);
407  g[2] = ((ff_gain_val_tab[n][2] * m[2]) >> ff_gain_exp_tab[n]) *
408  (1/4096.0);
409  error = 0;
410  if (cba_idx) {
411  g[0] = ((ff_gain_val_tab[n][0] * m[0]) >> ff_gain_exp_tab[n]) *
412  (1/4096.0);
413  for (i = 0; i < BLOCKSIZE; i++) {
414  data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] +
415  g[2] * cb2[i];
416  error += (data[i] - sblock_data[i]) *
417  (data[i] - sblock_data[i]);
418  }
419  } else {
420  for (i = 0; i < BLOCKSIZE; i++) {
421  data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i];
422  error += (data[i] - sblock_data[i]) *
423  (data[i] - sblock_data[i]);
424  }
425  }
426  if (error < best_error) {
427  best_error = error;
428  gain = n;
429  }
430  }
431  put_bits(pb, 7, cba_idx);
432  put_bits(pb, 8, gain);
433  put_bits(pb, 7, cb1_idx);
434  put_bits(pb, 7, cb2_idx);
435  ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, rms,
436  gain);
437 }
438 
439 
440 static int ra144_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
441  const AVFrame *frame, int *got_packet_ptr)
442 {
443  static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4};
444  static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
445  RA144Context *ractx = avctx->priv_data;
446  PutBitContext pb;
447  int32_t lpc_data[NBLOCKS * BLOCKSIZE];
448  int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER];
449  int shift[LPC_ORDER];
450  int16_t block_coefs[NBLOCKS][LPC_ORDER];
451  int lpc_refl[LPC_ORDER];
452  unsigned int refl_rms[NBLOCKS];
453  const int16_t *samples = frame ? (const int16_t *)frame->data[0] : NULL;
454  int energy = 0;
455  int i, idx, ret;
456 
457  if (ractx->last_frame)
458  return 0;
459 
460  if ((ret = ff_alloc_packet(avpkt, FRAMESIZE))) {
461  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
462  return ret;
463  }
464 
472  for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) {
473  lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i];
474  energy += (lpc_data[i] * lpc_data[i]) >> 4;
475  }
476  if (frame) {
477  int j;
478  for (j = 0; j < frame->nb_samples && i < NBLOCKS * BLOCKSIZE; i++, j++) {
479  lpc_data[i] = samples[j] >> 2;
480  energy += (lpc_data[i] * lpc_data[i]) >> 4;
481  }
482  }
483  if (i < NBLOCKS * BLOCKSIZE)
484  memset(&lpc_data[i], 0, (NBLOCKS * BLOCKSIZE - i) * sizeof(*lpc_data));
485  energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab,
486  32)];
487 
488  ff_lpc_calc_coefs(&ractx->lpc_ctx, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
489  LPC_ORDER, 16, lpc_coefs, shift, FF_LPC_TYPE_LEVINSON,
490  0, ORDER_METHOD_EST, 12, 0);
491  for (i = 0; i < LPC_ORDER; i++)
492  block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] <<
493  (12 - shift[LPC_ORDER - 1]));
494 
500  if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
504  ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
505  if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
506  /* the filter is still unstable. set reflection coeffs to zero. */
507  memset(lpc_refl, 0, sizeof(lpc_refl));
508  }
509  }
510  init_put_bits(&pb, avpkt->data, avpkt->size);
511  for (i = 0; i < LPC_ORDER; i++) {
512  idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]);
513  put_bits(&pb, bit_sizes[i], idx);
514  lpc_refl[i] = ff_lpc_refl_cb[i][idx];
515  }
516  ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
517  ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
518  refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
519  refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
520  energy <= ractx->old_energy,
521  ff_t_sqrt(energy * ractx->old_energy) >> 12);
522  refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
523  refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
524  ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]);
525  put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32));
526  for (i = 0; i < NBLOCKS; i++)
527  ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE,
528  block_coefs[i], refl_rms[i], &pb);
529  flush_put_bits(&pb);
530  ractx->old_energy = energy;
531  ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
532  FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
533 
534  /* copy input samples to current block for processing in next call */
535  i = 0;
536  if (frame) {
537  for (; i < frame->nb_samples; i++)
538  ractx->curr_block[i] = samples[i] >> 2;
539 
540  if ((ret = ff_af_queue_add(&ractx->afq, frame)) < 0)
541  return ret;
542  } else
543  ractx->last_frame = 1;
544  memset(&ractx->curr_block[i], 0,
545  (NBLOCKS * BLOCKSIZE - i) * sizeof(*ractx->curr_block));
546 
547  /* Get the next frame pts/duration */
548  ff_af_queue_remove(&ractx->afq, avctx->frame_size, &avpkt->pts,
549  &avpkt->duration);
550 
551  avpkt->size = FRAMESIZE;
552  *got_packet_ptr = 1;
553  return 0;
554 }
555 
556 
558  .name = "real_144",
559  .type = AVMEDIA_TYPE_AUDIO,
560  .id = AV_CODEC_ID_RA_144,
561  .priv_data_size = sizeof(RA144Context),
563  .encode2 = ra144_encode_frame,
566  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
568  .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
569 };