FFmpeg  1.2.12
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 FFmpeg.
6  *
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.
11  *
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.
16  *
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
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 
38 {
39  RA144Context *ractx = avctx->priv_data;
40  ff_lpc_end(&ractx->lpc_ctx);
41  ff_af_queue_close(&ractx->afq);
42 #if FF_API_OLD_ENCODE_AUDIO
43  av_freep(&avctx->coded_frame);
44 #endif
45  return 0;
46 }
47 
48 
50 {
51  RA144Context *ractx;
52  int ret;
53 
54  if (avctx->channels != 1) {
55  av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n",
56  avctx->channels);
57  return -1;
58  }
59  avctx->frame_size = NBLOCKS * BLOCKSIZE;
60  avctx->delay = avctx->frame_size;
61  avctx->bit_rate = 8000;
62  ractx = avctx->priv_data;
63  ractx->lpc_coef[0] = ractx->lpc_tables[0];
64  ractx->lpc_coef[1] = ractx->lpc_tables[1];
65  ractx->avctx = avctx;
66  ret = ff_lpc_init(&ractx->lpc_ctx, avctx->frame_size, LPC_ORDER,
68  if (ret < 0)
69  goto error;
70 
71  ff_af_queue_init(avctx, &ractx->afq);
72 
73 #if FF_API_OLD_ENCODE_AUDIO
75  if (!avctx->coded_frame) {
76  ret = AVERROR(ENOMEM);
77  goto error;
78  }
79 #endif
80 
81  return 0;
82 error:
83  ra144_encode_close(avctx);
84  return ret;
85 }
86 
87 
98 static int quantize(int value, const int16_t *table, unsigned int size)
99 {
100  unsigned int low = 0, high = size - 1;
101 
102  while (1) {
103  int index = (low + high) >> 1;
104  int error = table[index] - value;
105 
106  if (index == low)
107  return table[high] + error > value ? low : high;
108  if (error > 0) {
109  high = index;
110  } else {
111  low = index;
112  }
113  }
114 }
115 
116 
123 static void orthogonalize(float *v, const float *u)
124 {
125  int i;
126  float num = 0, den = 0;
127 
128  for (i = 0; i < BLOCKSIZE; i++) {
129  num += v[i] * u[i];
130  den += u[i] * u[i];
131  }
132  num /= den;
133  for (i = 0; i < BLOCKSIZE; i++)
134  v[i] -= num * u[i];
135 }
136 
137 
151 static void get_match_score(float *work, const float *coefs, float *vect,
152  const float *ortho1, const float *ortho2,
153  const float *data, float *score, float *gain)
154 {
155  float c, g;
156  int i;
157 
158  ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
159  if (ortho1)
160  orthogonalize(work, ortho1);
161  if (ortho2)
162  orthogonalize(work, ortho2);
163  c = g = 0;
164  for (i = 0; i < BLOCKSIZE; i++) {
165  g += work[i] * work[i];
166  c += data[i] * work[i];
167  }
168  if (c <= 0) {
169  *score = 0;
170  return;
171  }
172  *gain = c / g;
173  *score = *gain * c;
174 }
175 
176 
184 static void create_adapt_vect(float *vect, const int16_t *cb, int lag)
185 {
186  int i;
187 
188  cb += BUFFERSIZE - lag;
189  for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++)
190  vect[i] = cb[i];
191  if (lag < BLOCKSIZE)
192  for (i = 0; i < BLOCKSIZE - lag; i++)
193  vect[lag + i] = cb[i];
194 }
195 
196 
207 static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
208  const float *coefs, float *data)
209 {
210  int i, av_uninit(best_vect);
211  float score, gain, best_score, av_uninit(best_gain);
212  float exc[BLOCKSIZE];
213 
214  gain = best_score = 0;
215  for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {
216  create_adapt_vect(exc, adapt_cb, i);
217  get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);
218  if (score > best_score) {
219  best_score = score;
220  best_vect = i;
221  best_gain = gain;
222  }
223  }
224  if (!best_score)
225  return 0;
226 
231  create_adapt_vect(exc, adapt_cb, best_vect);
233  for (i = 0; i < BLOCKSIZE; i++)
234  data[i] -= best_gain * work[i];
235  return best_vect - BLOCKSIZE / 2 + 1;
236 }
237 
238 
255 static void find_best_vect(float *work, const float *coefs,
256  const int8_t cb[][BLOCKSIZE], const float *ortho1,
257  const float *ortho2, float *data, int *idx,
258  float *gain)
259 {
260  int i, j;
261  float g, score, best_score;
262  float vect[BLOCKSIZE];
263 
264  *idx = *gain = best_score = 0;
265  for (i = 0; i < FIXED_CB_SIZE; i++) {
266  for (j = 0; j < BLOCKSIZE; j++)
267  vect[j] = cb[i][j];
268  get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g);
269  if (score > best_score) {
270  best_score = score;
271  *idx = i;
272  *gain = g;
273  }
274  }
275 }
276 
277 
290 static void fixed_cb_search(float *work, const float *coefs, float *data,
291  int cba_idx, int *cb1_idx, int *cb2_idx)
292 {
293  int i, ortho_cb1;
294  float gain;
295  float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE];
296  float vect[BLOCKSIZE];
297 
302  if (cba_idx)
303  memcpy(cba_vect, work, sizeof(cba_vect));
304 
305  find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL,
306  data, cb1_idx, &gain);
307 
312  if (gain) {
313  for (i = 0; i < BLOCKSIZE; i++)
314  vect[i] = ff_cb1_vects[*cb1_idx][i];
315  ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
316  if (cba_idx)
317  orthogonalize(work, cba_vect);
318  for (i = 0; i < BLOCKSIZE; i++)
319  data[i] -= gain * work[i];
320  memcpy(cb1_vect, work, sizeof(cb1_vect));
321  ortho_cb1 = 1;
322  } else
323  ortho_cb1 = 0;
324 
325  find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL,
326  ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain);
327 }
328 
329 
340  const int16_t *sblock_data,
341  const int16_t *lpc_coefs, unsigned int rms,
342  PutBitContext *pb)
343 {
344  float data[BLOCKSIZE] = { 0 }, work[LPC_ORDER + BLOCKSIZE];
345  float coefs[LPC_ORDER];
346  float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE];
347  int16_t cba_vect[BLOCKSIZE];
348  int cba_idx, cb1_idx, cb2_idx, gain;
349  int i, n;
350  unsigned 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_packet2(avctx, avpkt, FRAMESIZE)) < 0)
461  return ret;
462 
470  for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) {
471  lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i];
472  energy += (lpc_data[i] * lpc_data[i]) >> 4;
473  }
474  if (frame) {
475  int j;
476  for (j = 0; j < frame->nb_samples && i < NBLOCKS * BLOCKSIZE; i++, j++) {
477  lpc_data[i] = samples[j] >> 2;
478  energy += (lpc_data[i] * lpc_data[i]) >> 4;
479  }
480  }
481  if (i < NBLOCKS * BLOCKSIZE)
482  memset(&lpc_data[i], 0, (NBLOCKS * BLOCKSIZE - i) * sizeof(*lpc_data));
483  energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab,
484  32)];
485 
486  ff_lpc_calc_coefs(&ractx->lpc_ctx, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
487  LPC_ORDER, 16, lpc_coefs, shift, FF_LPC_TYPE_LEVINSON,
488  0, ORDER_METHOD_EST, 12, 0);
489  for (i = 0; i < LPC_ORDER; i++)
490  block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] <<
491  (12 - shift[LPC_ORDER - 1]));
492 
498  if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
502  ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
503  if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
504  /* the filter is still unstable. set reflection coeffs to zero. */
505  memset(lpc_refl, 0, sizeof(lpc_refl));
506  }
507  }
508  init_put_bits(&pb, avpkt->data, avpkt->size);
509  for (i = 0; i < LPC_ORDER; i++) {
510  idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]);
511  put_bits(&pb, bit_sizes[i], idx);
512  lpc_refl[i] = ff_lpc_refl_cb[i][idx];
513  }
514  ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
515  ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
516  refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
517  refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
518  energy <= ractx->old_energy,
519  ff_t_sqrt(energy * ractx->old_energy) >> 12);
520  refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
521  refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
522  ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]);
523  put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32));
524  for (i = 0; i < NBLOCKS; i++)
525  ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE,
526  block_coefs[i], refl_rms[i], &pb);
527  flush_put_bits(&pb);
528  ractx->old_energy = energy;
529  ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
530  FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
531 
532  /* copy input samples to current block for processing in next call */
533  i = 0;
534  if (frame) {
535  for (; i < frame->nb_samples; i++)
536  ractx->curr_block[i] = samples[i] >> 2;
537 
538  if ((ret = ff_af_queue_add(&ractx->afq, frame)) < 0)
539  return ret;
540  } else
541  ractx->last_frame = 1;
542  memset(&ractx->curr_block[i], 0,
543  (NBLOCKS * BLOCKSIZE - i) * sizeof(*ractx->curr_block));
544 
545  /* Get the next frame pts/duration */
546  ff_af_queue_remove(&ractx->afq, avctx->frame_size, &avpkt->pts,
547  &avpkt->duration);
548 
549  avpkt->size = FRAMESIZE;
550  *got_packet_ptr = 1;
551  return 0;
552 }
553 
554 
556  .name = "real_144",
557  .type = AVMEDIA_TYPE_AUDIO,
558  .id = AV_CODEC_ID_RA_144,
559  .priv_data_size = sizeof(RA144Context),
561  .encode2 = ra144_encode_frame,
564  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
566  .supported_samplerates = (const int[]){ 8000, 0 },
567  .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
568 };