80 static int quantize(
int value,
const int16_t *table,
unsigned int size)
82 unsigned int low = 0, high = size - 1;
85 int index = (low + high) >> 1;
86 int error = table[
index] - value;
89 return table[high] + error > value ? low : high;
108 float num = 0, den = 0;
134 const float *ortho1,
const float *ortho2,
135 const float *
data,
float *score,
float *gain)
147 g += work[i] * work[i];
148 c += data[i] * work[i];
175 vect[lag + i] = cb[i];
190 const float *coefs,
float *
data)
193 float score, gain, best_score, best_gain;
196 gain = best_score = 0;
200 if (score > best_score) {
216 data[i] -= best_gain * work[i];
217 return best_vect - BLOCKSIZE / 2 + 1;
238 const int8_t cb[][
BLOCKSIZE],
const float *ortho1,
239 const float *ortho2,
float *
data,
int *idx,
243 float g, score, best_score;
246 *idx = *gain = best_score = 0;
251 if (score > best_score) {
273 int cba_idx,
int *cb1_idx,
int *cb2_idx)
285 memcpy(cba_vect, work,
sizeof(cba_vect));
288 data, cb1_idx, &gain);
301 data[i] -= gain * work[i];
302 memcpy(cb1_vect, work,
sizeof(cb1_vect));
308 ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain);
322 const int16_t *sblock_data,
323 const int16_t *lpc_coefs,
unsigned int rms,
330 int cba_idx, cb1_idx, cb2_idx, gain;
333 float error, best_error;
337 coefs[i] = lpc_coefs[i] * (1/4096.0);
344 memset(data, 0,
sizeof(data));
348 zero[i] = work[LPC_ORDER + i];
349 data[i] = sblock_data[i] - zero[i];
357 memset(work, 0, LPC_ORDER *
sizeof(*work));
366 memcpy(cba, work + LPC_ORDER,
sizeof(cba));
369 m[0] = (
ff_irms(cba_vect) * rms) >> 12;
371 fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx);
378 memcpy(cb1, work + LPC_ORDER,
sizeof(cb1));
382 memcpy(cb2, work + LPC_ORDER,
sizeof(cb2));
384 best_error = FLT_MAX;
386 for (n = 0; n < 256; n++) {
396 data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] +
398 error += (data[i] - sblock_data[i]) *
399 (data[i] - sblock_data[i]);
403 data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i];
404 error += (data[i] - sblock_data[i]) *
405 (data[i] - sblock_data[i]);
408 if (error < best_error) {
423 int buf_size,
void *
data)
425 static const uint8_t
sizes[
LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4};
426 static const uint8_t bit_sizes[
LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
434 unsigned int refl_rms[
NBLOCKS];
453 energy += (lpc_data[i] * lpc_data[i]) >> 4;
456 lpc_data[i] = *((int16_t *)data + i - 2 * BLOCKSIZE - BLOCKSIZE / 2) >>
458 energy += (lpc_data[i] * lpc_data[i]) >> 4;
467 block_coefs[
NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] <<
468 (12 - shift[LPC_ORDER - 1]));
482 memset(lpc_refl, 0,
sizeof(lpc_refl));
494 refl_rms[1] =
ff_interp(ractx, block_coefs[1], 2,
495 energy <= ractx->old_energy,
497 refl_rms[2] =
ff_interp(ractx, block_coefs[2], 3, 0, energy);
503 block_coefs[i], refl_rms[i], &pb);
508 for (i = 0; i < NBLOCKS *
BLOCKSIZE; i++)
509 ractx->
curr_block[i] = *((int16_t *)data + i) >> 2;
unsigned int lpc_tables[2][10]
void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP synthesis filter.
int ff_t_sqrt(unsigned int x)
Evaluate sqrt(x << 24).
const int16_t *const ff_lpc_refl_cb[10]
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...
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 max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
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.
static av_cold int ra144_encode_close(AVCodecContext *avctx)
static void orthogonalize(float *v, const float *u)
Orthogonalize a vector to another vector.
uint16_t adapt_cb[146+2]
Adaptive codebook, its size is two units bigger to avoid a buffer overflow.
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, possibly othogonalizing it to up to 2 other vectors.
static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size, void *data)
AVCodec ff_ra_144_encoder
#define NBLOCKS
number of subblocks within a block
enum AVSampleFormat sample_fmt
audio sample format
unsigned int lpc_refl_rms[2]
unsigned int ff_rms(const int *data)
#define FIXED_CB_SIZE
size of fixed codebooks
const uint16_t ff_cb2_base[128]
#define FRAMESIZE
size of encoded frame
void ff_subblock_synthesis(RA144Context *ractx, const uint16_t *lpc_coefs, int cba_idx, int cb1_idx, int cb2_idx, int gval, int gain)
static int init(AVCodecParserContext *s)
unsigned int * lpc_coef[2]
LPC coefficients: lpc_coef[0] is the coefficients of the current frame and lpc_coef[1] of the previou...
const int8_t ff_cb1_vects[128][40]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
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.
int bit_rate
the average bitrate
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
int ff_interp(RA144Context *ractx, int16_t *out, int a, int copyold, int energy)
void ff_copy_and_dup(int16_t *target, const int16_t *source, int offset)
Copy the last offset values of *source to *target.
void ff_int_to_int16(int16_t *out, const int *inp)
const int16_t ff_gain_val_tab[256][3]
int frame_size
Samples per packet, initialized when calling 'init'.
#define BLOCKSIZE
subblock size in 16-bit words
void ff_eval_coefs(int *coefs, const int *refl)
Evaluate the LPC filter coefficients from the reflection coefficients.
main external API structure.
static void close(AVCodecParserContext *s)
int ff_irms(const int16_t *data)
inverse root mean square
static av_cold int ra144_encode_init(AVCodecContext *avctx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Levinson-Durbin recursion.
#define ORDER_METHOD_EST
LPC utility code Copyright (c) 2006 Justin Ruggles justin.ruggles@gmail.com
int ff_eval_refl(int *refl, const int16_t *coefs, AVCodecContext *avctx)
Evaluate the reflection coefficients from the filter coefficients.
#define BUFFERSIZE
the size of the adaptive codebook
unsigned int ff_rescale_rms(unsigned int rms, unsigned int energy)
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
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, possibly othogonalizing them to up to 2 other vectors and matching the results with input data.
const int8_t ff_cb2_vects[128][40]
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
unsigned int old_energy
previous frame energy
const int16_t ff_energy_tab[32]
AVSampleFormat
all in native-endian format
const uint8_t ff_gain_exp_tab[256]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
int16_t curr_sblock[50]
The current subblock padded by the last 10 values of the previous one.
int channels
number of audio channels
#define LPC_ORDER
order of LPC filter
const uint16_t ff_cb1_base[128]
int16_t curr_block[NBLOCKS *BLOCKSIZE]
#define FFSWAP(type, a, b)
static const int sizes[][2]
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.
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.