FFmpeg
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 
22 /**
23  * @file
24  * Real Audio 1.0 (14.4K) encoder
25  * @author Francesco Lavra <francescolavra@interfree.it>
26  */
27 
28 #include <float.h>
29 
30 #include "avcodec.h"
31 #include "audio_frame_queue.h"
32 #include "celp_filters.h"
33 #include "encode.h"
34 #include "internal.h"
35 #include "mathops.h"
36 #include "put_bits.h"
37 #include "ra144.h"
38 
40 {
41  RA144Context *ractx = avctx->priv_data;
42  ff_lpc_end(&ractx->lpc_ctx);
43  ff_af_queue_close(&ractx->afq);
44  return 0;
45 }
46 
47 
49 {
50  RA144Context *ractx;
51  int ret;
52 
53  if (avctx->channels != 1) {
54  av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n",
55  avctx->channels);
56  return -1;
57  }
58  avctx->frame_size = NBLOCKS * BLOCKSIZE;
59  avctx->initial_padding = avctx->frame_size;
60  avctx->bit_rate = 8000;
61  ractx = avctx->priv_data;
62  ractx->lpc_coef[0] = ractx->lpc_tables[0];
63  ractx->lpc_coef[1] = ractx->lpc_tables[1];
64  ractx->avctx = avctx;
65  ff_audiodsp_init(&ractx->adsp);
66  ret = ff_lpc_init(&ractx->lpc_ctx, avctx->frame_size, LPC_ORDER,
68  if (ret < 0)
69  return ret;
70 
71  ff_af_queue_init(avctx, &ractx->afq);
72 
73  return 0;
74 }
75 
76 
77 /**
78  * Quantize a value by searching a sorted table for the element with the
79  * nearest value
80  *
81  * @param value value to quantize
82  * @param table array containing the quantization table
83  * @param size size of the quantization table
84  * @return index of the quantization table corresponding to the element with the
85  * nearest value
86  */
87 static int quantize(int value, const int16_t *table, unsigned int size)
88 {
89  unsigned int low = 0, high = size - 1;
90 
91  while (1) {
92  int index = (low + high) >> 1;
93  int error = table[index] - value;
94 
95  if (index == low)
96  return table[high] + error > value ? low : high;
97  if (error > 0) {
98  high = index;
99  } else {
100  low = index;
101  }
102  }
103 }
104 
105 
106 /**
107  * Orthogonalize a vector to another vector
108  *
109  * @param v vector to orthogonalize
110  * @param u vector against which orthogonalization is performed
111  */
112 static void orthogonalize(float *v, const float *u)
113 {
114  int i;
115  float num = 0, den = 0;
116 
117  for (i = 0; i < BLOCKSIZE; i++) {
118  num += v[i] * u[i];
119  den += u[i] * u[i];
120  }
121  num /= den;
122  for (i = 0; i < BLOCKSIZE; i++)
123  v[i] -= num * u[i];
124 }
125 
126 
127 /**
128  * Calculate match score and gain of an LPC-filtered vector with respect to
129  * input data, possibly orthogonalizing it to up to two other vectors.
130  *
131  * @param work array used to calculate the filtered vector
132  * @param coefs coefficients of the LPC filter
133  * @param vect original vector
134  * @param ortho1 first vector against which orthogonalization is performed
135  * @param ortho2 second vector against which orthogonalization is performed
136  * @param data input data
137  * @param score pointer to variable where match score is returned
138  * @param gain pointer to variable where gain is returned
139  */
140 static void get_match_score(float *work, const float *coefs, float *vect,
141  const float *ortho1, const float *ortho2,
142  const float *data, float *score, float *gain)
143 {
144  float c, g;
145  int i;
146 
148  if (ortho1)
149  orthogonalize(work, ortho1);
150  if (ortho2)
151  orthogonalize(work, ortho2);
152  c = g = 0;
153  for (i = 0; i < BLOCKSIZE; i++) {
154  g += work[i] * work[i];
155  c += data[i] * work[i];
156  }
157  if (c <= 0) {
158  *score = 0;
159  return;
160  }
161  *gain = c / g;
162  *score = *gain * c;
163 }
164 
165 
166 /**
167  * Create a vector from the adaptive codebook at a given lag value
168  *
169  * @param vect array where vector is stored
170  * @param cb adaptive codebook
171  * @param lag lag value
172  */
173 static void create_adapt_vect(float *vect, const int16_t *cb, int lag)
174 {
175  int i;
176 
177  cb += BUFFERSIZE - lag;
178  for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++)
179  vect[i] = cb[i];
180  if (lag < BLOCKSIZE)
181  for (i = 0; i < BLOCKSIZE - lag; i++)
182  vect[lag + i] = cb[i];
183 }
184 
185 
186 /**
187  * Search the adaptive codebook for the best entry and gain and remove its
188  * contribution from input data
189  *
190  * @param adapt_cb array from which the adaptive codebook is extracted
191  * @param work array used to calculate LPC-filtered vectors
192  * @param coefs coefficients of the LPC filter
193  * @param data input data
194  * @return index of the best entry of the adaptive codebook
195  */
196 static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
197  const float *coefs, float *data)
198 {
199  int i, av_uninit(best_vect);
200  float score, gain, best_score, av_uninit(best_gain);
201  float exc[BLOCKSIZE];
202 
203  gain = best_score = 0;
204  for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {
205  create_adapt_vect(exc, adapt_cb, i);
206  get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);
207  if (score > best_score) {
208  best_score = score;
209  best_vect = i;
210  best_gain = gain;
211  }
212  }
213  if (!best_score)
214  return 0;
215 
216  /**
217  * Re-calculate the filtered vector from the vector with maximum match score
218  * and remove its contribution from input data.
219  */
220  create_adapt_vect(exc, adapt_cb, best_vect);
222  for (i = 0; i < BLOCKSIZE; i++)
223  data[i] -= best_gain * work[i];
224  return best_vect - BLOCKSIZE / 2 + 1;
225 }
226 
227 
228 /**
229  * Find the best vector of a fixed codebook by applying an LPC filter to
230  * codebook entries, possibly orthogonalizing them to up to two other vectors
231  * and matching the results with input data.
232  *
233  * @param work array used to calculate the filtered vectors
234  * @param coefs coefficients of the LPC filter
235  * @param cb fixed codebook
236  * @param ortho1 first vector against which orthogonalization is performed
237  * @param ortho2 second vector against which orthogonalization is performed
238  * @param data input data
239  * @param idx pointer to variable where the index of the best codebook entry is
240  * returned
241  * @param gain pointer to variable where the gain of the best codebook entry is
242  * returned
243  */
244 static void find_best_vect(float *work, const float *coefs,
245  const int8_t cb[][BLOCKSIZE], const float *ortho1,
246  const float *ortho2, float *data, int *idx,
247  float *gain)
248 {
249  int i, j;
250  float g, score, best_score;
251  float vect[BLOCKSIZE];
252 
253  *idx = *gain = best_score = 0;
254  for (i = 0; i < FIXED_CB_SIZE; i++) {
255  for (j = 0; j < BLOCKSIZE; j++)
256  vect[j] = cb[i][j];
257  get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g);
258  if (score > best_score) {
259  best_score = score;
260  *idx = i;
261  *gain = g;
262  }
263  }
264 }
265 
266 
267 /**
268  * Search the two fixed codebooks for the best entry and gain
269  *
270  * @param work array used to calculate LPC-filtered vectors
271  * @param coefs coefficients of the LPC filter
272  * @param data input data
273  * @param cba_idx index of the best entry of the adaptive codebook
274  * @param cb1_idx pointer to variable where the index of the best entry of the
275  * first fixed codebook is returned
276  * @param cb2_idx pointer to variable where the index of the best entry of the
277  * second fixed codebook is returned
278  */
279 static void fixed_cb_search(float *work, const float *coefs, float *data,
280  int cba_idx, int *cb1_idx, int *cb2_idx)
281 {
282  int i, ortho_cb1;
283  float gain;
284  float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE];
285  float vect[BLOCKSIZE];
286 
287  /**
288  * The filtered vector from the adaptive codebook can be retrieved from
289  * work, because this function is called just after adaptive_cb_search().
290  */
291  if (cba_idx)
292  memcpy(cba_vect, work, sizeof(cba_vect));
293 
294  find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL,
295  data, cb1_idx, &gain);
296 
297  /**
298  * Re-calculate the filtered vector from the vector with maximum match score
299  * and remove its contribution from input data.
300  */
301  if (gain) {
302  for (i = 0; i < BLOCKSIZE; i++)
303  vect[i] = ff_cb1_vects[*cb1_idx][i];
305  if (cba_idx)
306  orthogonalize(work, cba_vect);
307  for (i = 0; i < BLOCKSIZE; i++)
308  data[i] -= gain * work[i];
309  memcpy(cb1_vect, work, sizeof(cb1_vect));
310  ortho_cb1 = 1;
311  } else
312  ortho_cb1 = 0;
313 
314  find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL,
315  ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain);
316 }
317 
318 
319 /**
320  * Encode a subblock of the current frame
321  *
322  * @param ractx encoder context
323  * @param sblock_data input data of the subblock
324  * @param lpc_coefs coefficients of the LPC filter
325  * @param rms RMS of the reflection coefficients
326  * @param pb pointer to PutBitContext of the current frame
327  */
329  const int16_t *sblock_data,
330  const int16_t *lpc_coefs, unsigned int rms,
331  PutBitContext *pb)
332 {
333  float data[BLOCKSIZE] = { 0 }, work[LPC_ORDER + BLOCKSIZE];
334  float coefs[LPC_ORDER];
335  float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE];
336  int cba_idx, cb1_idx, cb2_idx, gain;
337  int i, n;
338  unsigned m[3];
339  float g[3];
340  float error, best_error;
341 
342  for (i = 0; i < LPC_ORDER; i++) {
343  work[i] = ractx->curr_sblock[BLOCKSIZE + i];
344  coefs[i] = lpc_coefs[i] * (1/4096.0);
345  }
346 
347  /**
348  * Calculate the zero-input response of the LPC filter and subtract it from
349  * input data.
350  */
352  LPC_ORDER);
353  for (i = 0; i < BLOCKSIZE; i++) {
354  zero[i] = work[LPC_ORDER + i];
355  data[i] = sblock_data[i] - zero[i];
356  }
357 
358  /**
359  * Codebook search is performed without taking into account the contribution
360  * of the previous subblock, since it has been just subtracted from input
361  * data.
362  */
363  memset(work, 0, LPC_ORDER * sizeof(*work));
364 
365  cba_idx = adaptive_cb_search(ractx->adapt_cb, work + LPC_ORDER, coefs,
366  data);
367  if (cba_idx) {
368  /**
369  * The filtered vector from the adaptive codebook can be retrieved from
370  * work, see implementation of adaptive_cb_search().
371  */
372  memcpy(cba, work + LPC_ORDER, sizeof(cba));
373 
374  ff_copy_and_dup(ractx->buffer_a, ractx->adapt_cb, cba_idx + BLOCKSIZE / 2 - 1);
375  m[0] = (ff_irms(&ractx->adsp, ractx->buffer_a) * rms) >> 12;
376  }
377  fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx);
378  for (i = 0; i < BLOCKSIZE; i++) {
379  cb1[i] = ff_cb1_vects[cb1_idx][i];
380  cb2[i] = ff_cb2_vects[cb2_idx][i];
381  }
383  LPC_ORDER);
384  memcpy(cb1, work + LPC_ORDER, sizeof(cb1));
385  m[1] = (ff_cb1_base[cb1_idx] * rms) >> 8;
387  LPC_ORDER);
388  memcpy(cb2, work + LPC_ORDER, sizeof(cb2));
389  m[2] = (ff_cb2_base[cb2_idx] * rms) >> 8;
390  best_error = FLT_MAX;
391  gain = 0;
392  for (n = 0; n < 256; n++) {
393  g[1] = ((ff_gain_val_tab[n][1] * m[1]) >> ff_gain_exp_tab[n]) *
394  (1/4096.0);
395  g[2] = ((ff_gain_val_tab[n][2] * m[2]) >> ff_gain_exp_tab[n]) *
396  (1/4096.0);
397  error = 0;
398  if (cba_idx) {
399  g[0] = ((ff_gain_val_tab[n][0] * m[0]) >> ff_gain_exp_tab[n]) *
400  (1/4096.0);
401  for (i = 0; i < BLOCKSIZE; i++) {
402  data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] +
403  g[2] * cb2[i];
404  error += (data[i] - sblock_data[i]) *
405  (data[i] - sblock_data[i]);
406  }
407  } else {
408  for (i = 0; i < BLOCKSIZE; i++) {
409  data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i];
410  error += (data[i] - sblock_data[i]) *
411  (data[i] - sblock_data[i]);
412  }
413  }
414  if (error < best_error) {
415  best_error = error;
416  gain = n;
417  }
418  }
419  put_bits(pb, 7, cba_idx);
420  put_bits(pb, 8, gain);
421  put_bits(pb, 7, cb1_idx);
422  put_bits(pb, 7, cb2_idx);
423  ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, rms,
424  gain);
425 }
426 
427 
428 static int ra144_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
429  const AVFrame *frame, int *got_packet_ptr)
430 {
431  static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4};
432  static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
433  RA144Context *ractx = avctx->priv_data;
434  PutBitContext pb;
435  int32_t lpc_data[NBLOCKS * BLOCKSIZE];
436  int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER];
437  int shift[LPC_ORDER];
438  int16_t block_coefs[NBLOCKS][LPC_ORDER];
439  int lpc_refl[LPC_ORDER]; /**< reflection coefficients of the frame */
440  unsigned int refl_rms[NBLOCKS]; /**< RMS of the reflection coefficients */
441  const int16_t *samples = frame ? (const int16_t *)frame->data[0] : NULL;
442  int energy = 0;
443  int i, idx, ret;
444 
445  if (ractx->last_frame)
446  return 0;
447 
448  if ((ret = ff_get_encode_buffer(avctx, avpkt, FRAME_SIZE, 0)) < 0)
449  return ret;
450 
451  /**
452  * Since the LPC coefficients are calculated on a frame centered over the
453  * fourth subframe, to encode a given frame, data from the next frame is
454  * needed. In each call to this function, the previous frame (whose data are
455  * saved in the encoder context) is encoded, and data from the current frame
456  * are saved in the encoder context to be used in the next function call.
457  */
458  for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) {
459  lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i];
460  energy += (lpc_data[i] * lpc_data[i]) >> 4;
461  }
462  if (frame) {
463  int j;
464  for (j = 0; j < frame->nb_samples && i < NBLOCKS * BLOCKSIZE; i++, j++) {
465  lpc_data[i] = samples[j] >> 2;
466  energy += (lpc_data[i] * lpc_data[i]) >> 4;
467  }
468  }
469  if (i < NBLOCKS * BLOCKSIZE)
470  memset(&lpc_data[i], 0, (NBLOCKS * BLOCKSIZE - i) * sizeof(*lpc_data));
471  energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab,
472  32)];
473 
474  ff_lpc_calc_coefs(&ractx->lpc_ctx, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
475  LPC_ORDER, 16, lpc_coefs, shift, FF_LPC_TYPE_LEVINSON,
476  0, ORDER_METHOD_EST, 0, 12, 0);
477  for (i = 0; i < LPC_ORDER; i++)
478  block_coefs[NBLOCKS - 1][i] = -lpc_coefs[LPC_ORDER - 1][i]
479  * (1 << (12 - shift[LPC_ORDER - 1]));
480 
481  /**
482  * TODO: apply perceptual weighting of the input speech through bandwidth
483  * expansion of the LPC filter.
484  */
485 
486  if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
487  /**
488  * The filter is unstable: use the coefficients of the previous frame.
489  */
490  ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
491  if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
492  /* the filter is still unstable. set reflection coeffs to zero. */
493  memset(lpc_refl, 0, sizeof(lpc_refl));
494  }
495  }
496  init_put_bits(&pb, avpkt->data, avpkt->size);
497  for (i = 0; i < LPC_ORDER; i++) {
498  idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]);
499  put_bits(&pb, bit_sizes[i], idx);
500  lpc_refl[i] = ff_lpc_refl_cb[i][idx];
501  }
502  ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
503  ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
504  refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
505  refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
506  energy <= ractx->old_energy,
507  ff_t_sqrt(energy * ractx->old_energy) >> 12);
508  refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
509  refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
510  ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]);
511  put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32));
512  for (i = 0; i < NBLOCKS; i++)
513  ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE,
514  block_coefs[i], refl_rms[i], &pb);
515  flush_put_bits(&pb);
516  ractx->old_energy = energy;
517  ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
518  FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
519 
520  /* copy input samples to current block for processing in next call */
521  i = 0;
522  if (frame) {
523  for (; i < frame->nb_samples; i++)
524  ractx->curr_block[i] = samples[i] >> 2;
525 
526  if ((ret = ff_af_queue_add(&ractx->afq, frame)) < 0)
527  return ret;
528  } else
529  ractx->last_frame = 1;
530  memset(&ractx->curr_block[i], 0,
531  (NBLOCKS * BLOCKSIZE - i) * sizeof(*ractx->curr_block));
532 
533  /* Get the next frame pts/duration */
534  ff_af_queue_remove(&ractx->afq, avctx->frame_size, &avpkt->pts,
535  &avpkt->duration);
536 
537  *got_packet_ptr = 1;
538  return 0;
539 }
540 
541 
543  .name = "real_144",
544  .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
545  .type = AVMEDIA_TYPE_AUDIO,
546  .id = AV_CODEC_ID_RA_144,
547  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
549  .priv_data_size = sizeof(RA144Context),
551  .encode2 = ra144_encode_frame,
552  .close = ra144_encode_close,
553  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
555  .supported_samplerates = (const int[]){ 8000, 0 },
556  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 },
557  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
558 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:30
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1118
AVCodec
AVCodec.
Definition: codec.h:197
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
NBLOCKS
#define NBLOCKS
number of subblocks within a block
Definition: ra144.h:33
ra144_encode_subblock
static void ra144_encode_subblock(RA144Context *ractx, const int16_t *sblock_data, const int16_t *lpc_coefs, unsigned int rms, PutBitContext *pb)
Encode a subblock of the current frame.
Definition: ra144enc.c:328
RA144Context::avctx
AVCodecContext * avctx
Definition: ra144.h:41
ff_af_queue_remove
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
Definition: audio_frame_queue.c:75
FFSWAP
#define FFSWAP(type, a, b)
Definition: common.h:108
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:215
ff_af_queue_close
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
Definition: audio_frame_queue.c:36
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:953
adaptive_cb_search
static int adaptive_cb_search(const int16_t *adapt_cb, float *work, const float *coefs, float *data)
Search the adaptive codebook for the best entry and gain and remove its contribution from input data.
Definition: ra144enc.c:196
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
ff_energy_tab
const int16_t ff_energy_tab[32]
Definition: ra144.c:1440
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:61
ff_af_queue_init
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
Definition: audio_frame_queue.c:28
FRAME_SIZE
#define FRAME_SIZE
fixed_cb_search
static void fixed_cb_search(float *work, const float *coefs, float *data, int cba_idx, int *cb1_idx, int *cb2_idx)
Search the two fixed codebooks for the best entry and gain.
Definition: ra144enc.c:279
AV_CODEC_ID_RA_144
@ AV_CODEC_ID_RA_144
Definition: codec_id.h:408
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:219
index
fg index
Definition: ffmpeg_filter.c:168
ff_eval_coefs
void ff_eval_coefs(int *coefs, const int *refl)
Evaluate the LPC filter coefficients from the reflection coefficients.
Definition: ra144.c:1593
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
encode.h
table
static const uint16_t table[]
Definition: prosumer.c:206
data
const char data[16]
Definition: mxf.c:142
ff_audiodsp_init
av_cold void ff_audiodsp_init(AudioDSPContext *c)
Definition: audiodsp.c:106
FIXED_CB_SIZE
#define FIXED_CB_SIZE
size of fixed codebooks
Definition: ra144.h:36
float.h
ff_cb2_vects
const int8_t ff_cb2_vects[128][40]
Definition: ra144.c:758
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:383
BUFFERSIZE
#define BUFFERSIZE
the size of the adaptive codebook
Definition: ra144.h:35
ff_celp_lp_synthesis_filterf
void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP synthesis filter.
Definition: celp_filters.c:84
ff_ra_144_encoder
const AVCodec ff_ra_144_encoder
Definition: ra144enc.c:542
get_match_score
static void get_match_score(float *work, const float *coefs, float *vect, const float *ortho1, const float *ortho2, const float *data, float *score, float *gain)
Calculate match score and gain of an LPC-filtered vector with respect to input data,...
Definition: ra144enc.c:140
RA144Context::buffer_a
int16_t buffer_a[FFALIGN(BLOCKSIZE, 16)]
Definition: ra144.h:66
ff_cb1_vects
const int8_t ff_cb1_vects[128][40]
Definition: ra144.c:114
audio_frame_queue.h
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1807
RA144Context::lpc_coef
unsigned int * lpc_coef[2]
LPC coefficients: lpc_coef[0] is the coefficients of the current frame and lpc_coef[1] of the previou...
Definition: ra144.h:53
create_adapt_vect
static void create_adapt_vect(float *vect, const int16_t *cb, int lag)
Create a vector from the adaptive codebook at a given lag value.
Definition: ra144enc.c:173
ff_af_queue_add
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Definition: audio_frame_queue.c:44
ff_eval_refl
int ff_eval_refl(int *refl, const int16_t *coefs, AVCodecContext *avctx)
Evaluate the reflection coefficients from the filter coefficients.
Definition: ra144.c:1545
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:181
av_cold
#define av_cold
Definition: attributes.h:90
ff_lpc_refl_cb
const int16_t *const ff_lpc_refl_cb[10]
Definition: ra144.c:1502
ff_irms
int ff_irms(AudioDSPContext *adsp, const int16_t *data)
inverse root mean square
Definition: ra144.c:1684
g
const char * g
Definition: vf_curves.c:117
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
quantize
static int quantize(int value, const int16_t *table, unsigned int size)
Quantize a value by searching a sorted table for the element with the nearest value.
Definition: ra144enc.c:87
RA144Context::curr_block
int16_t curr_block[NBLOCKS *BLOCKSIZE]
Definition: ra144.h:57
PutBitContext
Definition: put_bits.h:49
LPC_ORDER
#define LPC_ORDER
Definition: g723_1.h:40
if
if(ret)
Definition: filter_design.txt:179
ff_rescale_rms
unsigned int ff_rescale_rms(unsigned int rms, unsigned int energy)
Definition: ra144.c:1678
ff_cb2_base
const uint16_t ff_cb2_base[128]
Definition: ra144.c:1421
NULL
#define NULL
Definition: coverity.c:32
sizes
static const int sizes[][2]
Definition: img2dec.c:53
RA144Context
Definition: ra144.h:40
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:551
work
must be printed separately If there s no standard function for printing the type you the WRITE_1D_FUNC_ARGV macro is a very quick way to create one See libavcodec dv_tablegen c for an example The h file This file should the initialization functions should not do and instead of the variable declarations the generated *_tables h file should be included Since that will be generated in the build the path must be i e not Makefile changes To make the automatic table creation work
Definition: tablegen.txt:66
mathops.h
RA144Context::adsp
AudioDSPContext adsp
Definition: ra144.h:42
RA144Context::lpc_tables
unsigned int lpc_tables[2][10]
Definition: ra144.h:49
celp_filters.h
ff_lpc_calc_coefs
int ff_lpc_calc_coefs(LPCContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, int32_t coefs[][MAX_LPC_ORDER], int *shift, enum FFLPCType lpc_type, int lpc_passes, int omethod, int min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
Definition: lpc.c:201
find_best_vect
static void find_best_vect(float *work, const float *coefs, const int8_t cb[][BLOCKSIZE], const float *ortho1, const float *ortho2, float *data, int *idx, float *gain)
Find the best vector of a fixed codebook by applying an LPC filter to codebook entries,...
Definition: ra144enc.c:244
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
RA144Context::curr_sblock
int16_t curr_sblock[50]
The current subblock padded by the last 10 values of the previous one.
Definition: ra144.h:60
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:366
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
MAX_LPC_ORDER
#define MAX_LPC_ORDER
Definition: lpc.h:38
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
size
int size
Definition: twinvq_data.h:10344
RA144Context::afq
AudioFrameQueue afq
Definition: ra144.h:44
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
ff_cb1_base
const uint16_t ff_cb1_base[128]
Definition: ra144.c:1402
ORDER_METHOD_EST
#define ORDER_METHOD_EST
Definition: lpc.h:30
ff_copy_and_dup
void ff_copy_and_dup(int16_t *target, const int16_t *source, int offset)
Copy the last offset values of *source to *target.
Definition: ra144.c:1530
ff_rms
unsigned int ff_rms(const int *data)
Definition: ra144.c:1636
ff_lpc_end
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:323
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1099
ff_gain_val_tab
const int16_t ff_gain_val_tab[256][3]
Definition: ra144.c:28
ff_int_to_int16
void ff_int_to_int16(int16_t *out, const int *inp)
Definition: ra144.c:1613
i
int i
Definition: input.c:407
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:358
RA144Context::old_energy
unsigned int old_energy
previous frame energy
Definition: ra144.h:47
ra144_encode_close
static av_cold int ra144_encode_close(AVCodecContext *avctx)
Definition: ra144enc.c:39
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
ff_subblock_synthesis
void ff_subblock_synthesis(RA144Context *ractx, const int16_t *lpc_coefs, int cba_idx, int cb1_idx, int cb2_idx, int gval, int gain)
Definition: ra144.c:1694
avcodec.h
av_uninit
#define av_uninit(x)
Definition: attributes.h:154
ret
ret
Definition: filter_design.txt:187
orthogonalize
static void orthogonalize(float *v, const float *u)
Orthogonalize a vector to another vector.
Definition: ra144enc.c:112
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
RA144Context::adapt_cb
int16_t adapt_cb[146+2]
Adaptive codebook, its size is two units bigger to avoid a buffer overflow.
Definition: ra144.h:64
AVCodecContext
main external API structure.
Definition: avcodec.h:501
ff_t_sqrt
int ff_t_sqrt(unsigned int x)
Evaluate sqrt(x << 24).
Definition: ra144.c:1625
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:81
ff_interp
int ff_interp(RA144Context *ractx, int16_t *out, int a, int copyold, int energy)
Definition: ra144.c:1657
ra144.h
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
ra144_encode_frame
static int ra144_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: ra144enc.c:428
shift
static int shift(int a, int b)
Definition: sonic.c:82
zero
#define zero
Definition: regdef.h:64
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:142
RA144Context::last_frame
int last_frame
Definition: ra144.h:45
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:528
int32_t
int32_t
Definition: audioconvert.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
RA144Context::lpc_refl_rms
unsigned int lpc_refl_rms[2]
Definition: ra144.h:55
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:82
put_bits.h
BLOCKSIZE
#define BLOCKSIZE
subblock size in 16-bit words
Definition: ra144.h:34
FF_LPC_TYPE_LEVINSON
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition: lpc.h:47
ff_gain_exp_tab
const uint8_t ff_gain_exp_tab[256]
Definition: ra144.c:95
ra144_encode_init
static av_cold int ra144_encode_init(AVCodecContext *avctx)
Definition: ra144enc.c:48
RA144Context::lpc_ctx
LPCContext lpc_ctx
Definition: ra144.h:43
ff_lpc_init
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition: lpc.c:301