50#define MAX_LSPS_ALIGN16 16
53#define MAX_FRAMESIZE 160
54#define MAX_SIGNAL_HISTORY 416
55#define MAX_SFRAMESIZE (MAX_FRAMESIZE * MAX_FRAMES)
57#define SFRAME_CACHE_MAXSIZE 256
300 int cntr[8] = { 0 }, n, res;
302 memset(vbm_tree, 0xff,
sizeof(vbm_tree[0]) * 25);
303 for (n = 0; n < 17; n++) {
307 vbm_tree[res * 3 + cntr[res]++] = n;
314 static const uint8_t
bits[] = {
317 10, 10, 10, 12, 12, 12,
323 1,
NULL, 0, 0, 0, 0);
331 s->postfilter_agc = 0;
332 s->sframe_cache_size = 0;
333 s->skip_bits_next = 0;
334 for (n = 0; n <
s->lsps; n++)
335 s->prev_lsps[n] =
M_PI * (n + 1.0) / (
s->lsps + 1.0);
336 memset(
s->excitation_history, 0,
338 memset(
s->synth_history, 0,
340 memset(
s->gain_pred_err, 0,
341 sizeof(
s->gain_pred_err));
345 sizeof(*
s->synth_filter_out_buf) *
s->lsps);
346 memset(
s->dcf_mem, 0,
347 sizeof(*
s->dcf_mem) * 2);
348 memset(
s->zero_exc_pf, 0,
349 sizeof(*
s->zero_exc_pf) *
s->history_nsamples);
350 memset(
s->denoise_filter_cache, 0,
sizeof(
s->denoise_filter_cache));
360 int n,
flags, pitch_range, lsp16_flag, ret;
373 if (
ctx->extradata_size != 46) {
375 "Invalid extradata size %d (should be 46)\n",
376 ctx->extradata_size);
379 if (
ctx->block_align <= 0 ||
ctx->block_align > (1<<22)) {
398 scale = 1.0 / (1 << 6);
403 scale = 1.0 / (1 << 6);
409 memcpy(&
s->sin[255],
s->cos, 256 *
sizeof(
s->cos[0]));
410 for (n = 0; n < 255; n++) {
411 s->sin[n] = -
s->sin[510 - n];
412 s->cos[510 - n] =
s->cos[n];
415 s->denoise_strength = (
flags >> 2) & 0xF;
416 if (
s->denoise_strength >= 12) {
418 "Invalid denoise filter strength %d (max=11)\n",
419 s->denoise_strength);
422 s->denoise_tilt_corr = !!(
flags & 0x40);
423 s->dc_level = (
flags >> 7) & 0xF;
424 s->lsp_q_mode = !!(
flags & 0x2000);
425 s->lsp_def_mode = !!(
flags & 0x4000);
426 lsp16_flag =
flags & 0x1000;
432 for (n = 0; n <
s->lsps; n++)
433 s->prev_lsps[n] =
M_PI * (n + 1.0) / (
s->lsps + 1.0);
441 if (
ctx->sample_rate >= INT_MAX / (256 * 37))
444 s->min_pitch_val = ((
ctx->sample_rate << 8) / 400 + 50) >> 8;
445 s->max_pitch_val = ((
ctx->sample_rate << 8) * 37 / 2000 + 50) >> 8;
446 pitch_range =
s->max_pitch_val -
s->min_pitch_val;
447 if (pitch_range <= 0) {
452 s->last_pitch_val = 40;
454 s->history_nsamples =
s->max_pitch_val + 8;
457 int min_sr = ((((1 << 8) - 50) * 400) + 0xFF) >> 8,
461 "Unsupported samplerate %d (min=%d, max=%d)\n",
462 ctx->sample_rate, min_sr, max_sr);
467 s->block_conv_table[0] =
s->min_pitch_val;
468 s->block_conv_table[1] = (pitch_range * 25) >> 6;
469 s->block_conv_table[2] = (pitch_range * 44) >> 6;
470 s->block_conv_table[3] =
s->max_pitch_val - 1;
471 s->block_delta_pitch_hrange = (pitch_range >> 3) & ~0xF;
472 if (
s->block_delta_pitch_hrange <= 0) {
476 s->block_delta_pitch_nbits = 1 +
av_ceil_log2(
s->block_delta_pitch_hrange);
477 s->block_pitch_range =
s->block_conv_table[2] +
478 s->block_conv_table[3] + 1 +
479 2 * (
s->block_conv_table[1] - 2 *
s->min_pitch_val);
511 const float *speech_synth,
515 float speech_energy = 0.0, postfilter_energy = 0.0, gain_scale_factor;
516 float mem = *gain_mem;
519 speech_energy +=
fabsf(speech_synth[
i]);
520 postfilter_energy +=
fabsf(in[
i]);
522 gain_scale_factor = postfilter_energy == 0.0 ? 0.0 :
523 (1.0 -
alpha) * speech_energy / postfilter_energy;
526 mem =
alpha * mem + gain_scale_factor;
527 out[
i] = in[
i] * mem;
552 const float *in,
float *
out,
int size)
555 float optimal_gain = 0, dot;
556 const float *ptr = &in[-
FFMAX(
s->min_pitch_val, pitch - 3)],
557 *end = &in[-
FFMIN(
s->max_pitch_val, pitch + 3)],
558 *best_hist_ptr =
NULL;
563 if (dot > optimal_gain) {
567 }
while (--ptr >= end);
569 if (optimal_gain <= 0)
575 if (optimal_gain <= dot) {
576 dot = dot / (dot + 0.6 * optimal_gain);
581 for (n = 0; n <
size; n++)
582 out[n] = best_hist_ptr[n] + dot * (in[n] - best_hist_ptr[n]);
611 int fcb_type,
float *coeffs_dst,
int remainder)
614 float irange, angle_mul, gain_mul,
range, sq;
620 memcpy(coeffs, coeffs_dst, 0x82*
sizeof(
float));
623 s->rdft_fn(
s->rdft, lpcs, lpcs_src,
sizeof(
float));
624#define log_range(var, assign) do { \
625 float tmp = log10f(assign); var = tmp; \
626 max = FFMAX(max, tmp); min = FFMIN(min, tmp); \
629 for (n = 1; n < 64; n++)
630 log_range(lpcs[n], lpcs[n * 2] * lpcs[n * 2] +
631 lpcs[n * 2 + 1] * lpcs[n * 2 + 1]);
642 irange = 64.0 /
range;
646 for (n = 0; n <= 64; n++) {
649 idx =
lrint((
max - lpcs[n]) * irange - 1);
652 lpcs[n] = angle_mul * pwr;
655 idx =
av_clipd((pwr * gain_mul - 0.0295) * 70.570526123, 0, INT_MAX / 2);
659 powf(1.0331663, idx - 127);
668 s->dct_fn(
s->dct, lpcs_dct, lpcs,
sizeof(
float));
669 s->dst_fn(
s->dst, lpcs, lpcs_dct,
sizeof(
float));
672 idx = 255 +
av_clip(lpcs[64], -255, 255);
673 coeffs[0] = coeffs[0] *
s->cos[idx];
674 idx = 255 +
av_clip(lpcs[64] - 2 * lpcs[63], -255, 255);
677 idx = 255 +
av_clip(-lpcs[64] - 2 * lpcs[n - 1], -255, 255);
678 coeffs[n * 2 + 1] = coeffs[n] *
s->sin[idx];
679 coeffs[n * 2] = coeffs[n] *
s->cos[idx];
683 idx = 255 +
av_clip( lpcs[64] - 2 * lpcs[n - 1], -255, 255);
684 coeffs[n * 2 + 1] = coeffs[n] *
s->sin[idx];
685 coeffs[n * 2] = coeffs[n] *
s->cos[idx];
693 memset(&coeffs_dst[remainder], 0,
sizeof(coeffs_dst[0]) * (128 - remainder));
694 if (
s->denoise_tilt_corr) {
697 coeffs_dst[remainder - 1] = 0;
700 coeffs_dst, remainder);
704 for (n = 0; n < remainder; n++)
735 float *synth_pf,
int size,
738 int remainder, lim, n;
743 float *tilted_lpcs =
s->tilted_lpcs_pf,
744 *coeffs =
s->denoise_coeffs_pf, tilt_mem = 0;
746 tilted_lpcs[0] = 1.0;
747 memcpy(&tilted_lpcs[1], lpcs,
sizeof(lpcs[0]) *
s->lsps);
748 memset(&tilted_lpcs[
s->lsps + 1], 0,
749 sizeof(tilted_lpcs[0]) * (128 -
s->lsps - 1));
751 tilted_lpcs,
s->lsps + 2);
762 memset(&synth_pf[
size], 0,
sizeof(synth_pf[0]) * (128 -
size));
763 s->rdft_fn(
s->rdft, synth_f, synth_pf,
sizeof(
float));
764 s->rdft_fn(
s->rdft, coeffs_f, coeffs,
sizeof(
float));
765 synth_f[0] *= coeffs_f[0];
766 synth_f[1] *= coeffs_f[1];
767 for (n = 1; n <= 64; n++) {
768 float v1 = synth_f[n * 2], v2 = synth_f[n * 2 + 1];
769 synth_f[n * 2] = v1 * coeffs_f[n * 2] - v2 * coeffs_f[n * 2 + 1];
770 synth_f[n * 2 + 1] = v2 * coeffs_f[n * 2] + v1 * coeffs_f[n * 2 + 1];
776 if (
s->denoise_filter_cache_size) {
777 lim =
FFMIN(
s->denoise_filter_cache_size,
size);
778 for (n = 0; n < lim; n++)
779 synth_pf[n] +=
s->denoise_filter_cache[n];
780 s->denoise_filter_cache_size -= lim;
781 memmove(
s->denoise_filter_cache, &
s->denoise_filter_cache[
size],
782 sizeof(
s->denoise_filter_cache[0]) *
s->denoise_filter_cache_size);
787 lim =
FFMIN(remainder,
s->denoise_filter_cache_size);
788 for (n = 0; n < lim; n++)
789 s->denoise_filter_cache[n] += synth_pf[
size + n];
790 if (lim < remainder) {
791 memcpy(&
s->denoise_filter_cache[lim], &synth_pf[
size + lim],
792 sizeof(
s->denoise_filter_cache[0]) * (remainder - lim));
793 s->denoise_filter_cache_size = remainder;
819 float *samples,
int size,
820 const float *lpcs,
float *zero_exc_pf,
821 int fcb_type,
int pitch)
825 *synth_filter_in = zero_exc_pf;
834 synth_filter_in = synth_filter_in_buf;
838 synth_filter_in,
size,
s->lsps);
839 memcpy(&synth_pf[-
s->lsps], &synth_pf[
size -
s->lsps],
840 sizeof(synth_pf[0]) *
s->lsps);
847 if (
s->dc_level > 8) {
852 (
const float[2]) { -1.99997, 1.0 },
853 (
const float[2]) { -1.9330735188, 0.93589198496 },
854 0.93980580475,
s->dcf_mem,
size);
873 const uint16_t *values,
874 const uint16_t *
sizes,
875 int n_stages,
const uint8_t *
table,
877 const double *base_q)
881 memset(lsps, 0, num *
sizeof(*lsps));
882 for (n = 0; n < n_stages; n++) {
883 const uint8_t *t_off = &
table[values[n] * num];
884 double base = base_q[n], mul = mul_q[n];
886 for (m = 0; m < num; m++)
887 lsps[m] +=
base + mul * t_off[m];
905 static const uint16_t vec_sizes[4] = { 256, 64, 32, 32 };
906 static const double mul_lsf[4] = {
907 5.2187144800e-3, 1.4626986422e-3,
908 9.6179549166e-4, 1.1325736225e-3
910 static const double base_lsf[4] = {
911 M_PI * -2.15522e-1,
M_PI * -6.1646e-2,
912 M_PI * -3.3486e-2,
M_PI * -5.7408e-2
930 double *i_lsps,
const double *old,
931 double *
a1,
double *
a2,
int q_mode)
933 static const uint16_t vec_sizes[3] = { 128, 64, 64 };
934 static const double mul_lsf[3] = {
935 2.5807601174e-3, 1.2354460219e-3, 1.1763821673e-3
937 static const double base_lsf[3] = {
938 M_PI * -1.07448e-1,
M_PI * -5.2706e-2,
M_PI * -5.1634e-2
940 const float (*ipol_tab)[2][10] = q_mode ?
952 for (n = 0; n < 10; n++) {
953 double delta = old[n] - i_lsps[n];
967 static const uint16_t vec_sizes[5] = { 256, 64, 128, 64, 128 };
968 static const double mul_lsf[5] = {
969 3.3439586280e-3, 6.9908173703e-4,
970 3.3216608306e-3, 1.0334960326e-3,
973 static const double base_lsf[5] = {
974 M_PI * -1.27576e-1,
M_PI * -2.4292e-2,
975 M_PI * -1.28094e-1,
M_PI * -3.2128e-2,
999 double *i_lsps,
const double *old,
1000 double *
a1,
double *
a2,
int q_mode)
1002 static const uint16_t vec_sizes[3] = { 128, 128, 128 };
1003 static const double mul_lsf[3] = {
1004 1.2232979501e-3, 1.4062241527e-3, 1.6114744851e-3
1006 static const double base_lsf[3] = {
1007 M_PI * -5.5830e-2,
M_PI * -5.2908e-2,
M_PI * -5.4776e-2
1009 const float (*ipol_tab)[2][16] = q_mode ?
1021 for (n = 0; n < 16; n++) {
1022 double delta = old[n] - i_lsps[n];
1051 static const int16_t start_offset[94] = {
1052 -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11,
1053 13, 15, 18, 17, 19, 20, 21, 22, 23, 24, 25, 26,
1054 27, 28, 29, 30, 31, 32, 33, 35, 37, 39, 41, 43,
1055 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67,
1056 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91,
1057 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115,
1058 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139,
1059 141, 143, 145, 147, 149, 151, 153, 155, 157, 159
1064 s->aw_idx_is_ext = 0;
1066 s->aw_idx_is_ext = 1;
1072 s->aw_pulse_range =
FFMIN(pitch[0], pitch[1]) > 32 ? 24 : 16;
1075 s->aw_first_pulse_off[0] =
offset -
s->aw_pulse_range / 2;
1076 offset +=
s->aw_n_pulses[0] * pitch[0];
1084 while (
s->aw_first_pulse_off[1] - pitch[1] +
s->aw_pulse_range > 0)
1085 s->aw_first_pulse_off[1] -= pitch[1];
1086 if (start_offset[
bits] < 0)
1087 while (
s->aw_first_pulse_off[0] - pitch[0] +
s->aw_pulse_range > 0)
1088 s->aw_first_pulse_off[0] -= pitch[0];
1103 uint16_t use_mask_mem[9];
1104 uint16_t *use_mask = use_mask_mem + 2;
1112 int pulse_off =
s->aw_first_pulse_off[block_idx],
1113 pulse_start, n, idx,
range, aidx, start_off = 0;
1116 if (
s->aw_n_pulses[block_idx] > 0)
1117 while (pulse_off +
s->aw_pulse_range < 1)
1121 if (
s->aw_n_pulses[0] > 0) {
1122 if (block_idx == 0) {
1126 if (
s->aw_n_pulses[block_idx] > 0)
1127 pulse_off =
s->aw_next_pulse_off_cache;
1131 pulse_start =
s->aw_n_pulses[block_idx] > 0 ? pulse_off -
range / 2 : 0;
1136 memset(&use_mask[-2], 0, 2 *
sizeof(use_mask[0]));
1137 memset( use_mask, -1, 5 *
sizeof(use_mask[0]));
1138 memset(&use_mask[5], 0, 2 *
sizeof(use_mask[0]));
1139 if (
s->aw_n_pulses[block_idx] > 0)
1141 int excl_range =
s->aw_pulse_range;
1142 uint16_t *use_mask_ptr = &use_mask[idx >> 4];
1143 int first_sh = 16 - (idx & 15);
1144 *use_mask_ptr++ &= 0xFFFFu << first_sh;
1145 excl_range -= first_sh;
1146 if (excl_range >= 16) {
1147 *use_mask_ptr++ = 0;
1148 *use_mask_ptr &= 0xFFFF >> (excl_range - 16);
1150 *use_mask_ptr &= 0xFFFF >> excl_range;
1154 aidx =
get_bits(gb,
s->aw_n_pulses[0] > 0 ? 5 - 2 * block_idx : 4);
1155 for (n = 0; n <= aidx; pulse_start++) {
1156 for (idx = pulse_start; idx < 0; idx += fcb->
pitch_lag) ;
1158 if (use_mask[0]) idx = 0x0F;
1159 else if (use_mask[1]) idx = 0x1F;
1160 else if (use_mask[2]) idx = 0x2F;
1161 else if (use_mask[3]) idx = 0x3F;
1162 else if (use_mask[4]) idx = 0x4F;
1166 if (use_mask[idx >> 4] & (0x8000 >> (idx & 15))) {
1167 use_mask[idx >> 4] &= ~(0x8000 >> (idx & 15));
1173 fcb->
x[fcb->
n] = start_off;
1179 s->aw_next_pulse_off_cache = n ? fcb->
pitch_lag - n : 0;
1193 int val =
get_bits(gb, 12 - 2 * (
s->aw_idx_is_ext && !block_idx));
1196 if (
s->aw_n_pulses[block_idx] > 0) {
1197 int n, v_mask, i_mask, sh, n_pulses;
1199 if (
s->aw_pulse_range == 24) {
1211 for (n = n_pulses - 1; n >= 0; n--,
val >>= sh) {
1212 fcb->
y[fcb->
n] = (
val & v_mask) ? -1.0 : 1.0;
1213 fcb->
x[fcb->
n] = (
val & i_mask) * n_pulses + n +
1214 s->aw_first_pulse_off[block_idx];
1215 while (fcb->
x[fcb->
n] < 0)
1221 int num2 = (
val & 0x1FF) >> 1,
delta, idx;
1223 if (num2 < 1 * 79) {
delta = 1; idx = num2 + 1; }
1224 else if (num2 < 2 * 78) {
delta = 3; idx = num2 + 1 - 1 * 77; }
1225 else if (num2 < 3 * 77) {
delta = 5; idx = num2 + 1 - 2 * 76; }
1226 else {
delta = 7; idx = num2 + 1 - 3 * 75; }
1227 v = (
val & 0x200) ? -1.0 : 1.0;
1232 fcb->
x[fcb->
n + 1] = idx;
1233 fcb->
y[fcb->
n + 1] = (
val & 1) ? -v : v;
1251static int pRNG(
int frame_cntr,
int block_num,
int block_size)
1263 static const unsigned int div_tbl[9][2] = {
1264 { 8332, 3 * 715827883U },
1265 { 4545, 0 * 390451573U },
1266 { 3124, 11 * 268435456U },
1267 { 2380, 15 * 204522253U },
1268 { 1922, 23 * 165191050U },
1269 { 1612, 23 * 138547333U },
1270 { 1388, 27 * 119304648U },
1271 { 1219, 16 * 104755300U },
1272 { 1086, 39 * 93368855U }
1274 unsigned int z, y, x =
MUL16(block_num, 1877) + frame_cntr;
1275 if (x >= 0xFFFF) x -= 0xFFFF;
1277 y = x - 9 *
MULH(477218589, x);
1278 z = (uint16_t) (x * div_tbl[y][0] +
UMULH(x, div_tbl[y][1]));
1280 return z % (1000 - block_size);
1288 int block_idx,
int size,
1299 r_idx =
pRNG(
s->frame_cntr, block_idx,
size);
1300 gain =
s->silence_gain;
1307 memset(
s->gain_pred_err, 0,
sizeof(
s->gain_pred_err));
1310 for (n = 0; n <
size; n++)
1319 int block_idx,
int size,
1320 int block_pitch_sh2,
1324 static const float gain_coeff[6] = {
1325 0.8169, -0.06545, 0.1726, 0.0185, -0.0359, 0.0458
1328 int n, idx, gain_weight;
1347 int r_idx =
pRNG(
s->frame_cntr, block_idx,
size);
1349 for (n = 0; n <
size; n++)
1361 for (n = 0; n < 5; n++) {
1367 fcb.
x[fcb.
n] = n + 5 * pos1;
1368 fcb.
y[fcb.
n++] = sign;
1369 if (n < frame_desc->dbl_pulses) {
1371 fcb.
x[fcb.
n] = n + 5 * pos2;
1372 fcb.
y[fcb.
n++] = (pos1 < pos2) ? -sign : sign;
1390 memmove(&
s->gain_pred_err[gain_weight],
s->gain_pred_err,
1391 sizeof(*
s->gain_pred_err) * (6 - gain_weight));
1392 for (n = 0; n < gain_weight; n++)
1393 s->gain_pred_err[n] = pred_err;
1398 for (n = 0; n <
size; n +=
len) {
1400 int abs_idx = block_idx *
size + n;
1401 int pitch_sh16 = (
s->last_pitch_val << 16) +
1402 s->pitch_diff_sh16 * abs_idx;
1403 int pitch = (pitch_sh16 + 0x6FFF) >> 16;
1404 int idx_sh16 = ((pitch << 16) - pitch_sh16) * 8 + 0x58000;
1405 idx = idx_sh16 >> 16;
1406 if (
s->pitch_diff_sh16) {
1407 if (
s->pitch_diff_sh16 > 0) {
1408 next_idx_sh16 = (idx_sh16) &~ 0xFFFF;
1410 next_idx_sh16 = (idx_sh16 + 0x10000) &~ 0xFFFF;
1411 len =
av_clip((idx_sh16 - next_idx_sh16) /
s->pitch_diff_sh16 / 8,
1421 int block_pitch = block_pitch_sh2 >> 2;
1422 idx = block_pitch_sh2 & 3;
1429 sizeof(
float) *
size);
1434 acb_gain, fcb_gain,
size);
1453 int block_idx,
int size,
1454 int block_pitch_sh2,
1455 const double *lsps,
const double *prev_lsps,
1457 float *excitation,
float *synth)
1468 frame_desc, excitation);
1471 fac = (block_idx + 0.5) / frame_desc->
n_blocks;
1472 for (n = 0; n <
s->lsps; n++)
1473 i_lsps[n] = cos(prev_lsps[n] + fac * (lsps[n] - prev_lsps[n]));
1496 const double *lsps,
const double *prev_lsps,
1497 float *excitation,
float *synth)
1500 int n, n_blocks_x2, log_n_blocks_x2,
av_uninit(cur_pitch_val);
1510 "Invalid frame type VLC code, skipping\n");
1523 log_n_blocks_x2 =
frame_descs[bd_idx].log_n_blocks + 1;
1524 cur_pitch_val =
s->min_pitch_val +
get_bits(gb,
s->pitch_nbits);
1525 cur_pitch_val =
FFMIN(cur_pitch_val,
s->max_pitch_val - 1);
1527 20 *
abs(cur_pitch_val -
s->last_pitch_val) >
1528 (cur_pitch_val +
s->last_pitch_val))
1529 s->last_pitch_val = cur_pitch_val;
1532 for (n = 0; n <
frame_descs[bd_idx].n_blocks; n++) {
1533 int fac = n * 2 + 1;
1535 pitch[n] = (
MUL16(fac, cur_pitch_val) +
1536 MUL16((n_blocks_x2 - fac),
s->last_pitch_val) +
1541 s->pitch_diff_sh16 =
1555 for (n = 0; n <
frame_descs[bd_idx].n_blocks; n++) {
1567 t1 = (
s->block_conv_table[1] -
s->block_conv_table[0]) << 2,
1568 t2 = (
s->block_conv_table[2] -
s->block_conv_table[1]) << 1,
1569 t3 =
s->block_conv_table[3] -
s->block_conv_table[2] + 1;
1572 block_pitch =
get_bits(gb,
s->block_pitch_nbits);
1574 block_pitch = last_block_pitch -
s->block_delta_pitch_hrange +
1575 get_bits(gb,
s->block_delta_pitch_nbits);
1577 last_block_pitch =
av_clip(block_pitch,
1578 s->block_delta_pitch_hrange,
1579 s->block_pitch_range -
1580 s->block_delta_pitch_hrange);
1583 if (block_pitch < t1) {
1584 bl_pitch_sh2 = (
s->block_conv_table[0] << 2) + block_pitch;
1587 if (block_pitch < t2) {
1589 (
s->block_conv_table[1] << 2) + (block_pitch << 1);
1592 if (block_pitch < t3) {
1594 (
s->block_conv_table[2] + block_pitch) << 2;
1596 bl_pitch_sh2 =
s->block_conv_table[3] << 2;
1599 pitch[n] = bl_pitch_sh2 >> 2;
1604 bl_pitch_sh2 = pitch[n] << 2;
1615 &excitation[n * block_nsamples],
1616 &synth[n * block_nsamples]);
1628 for (n = 0; n <
s->lsps; n++)
1629 i_lsps[n] = cos(0.5 * (prev_lsps[n] + lsps[n]));
1635 for (n = 0; n <
s->lsps; n++)
1636 i_lsps[n] = cos(lsps[n]);
1639 &
s->zero_exc_pf[
s->history_nsamples +
MAX_FRAMESIZE * frame_idx + 80],
1642 memcpy(samples, synth, 160 *
sizeof(synth[0]));
1646 if (
s->frame_cntr >= 0xFFFF)
s->frame_cntr -= 0xFFFF;
1650 s->last_pitch_val = 0;
1653 s->last_pitch_val = cur_pitch_val;
1656 s->last_pitch_val = pitch[
frame_descs[bd_idx].n_blocks - 1];
1682 lsps[0] =
FFMAX(lsps[0], 0.0015 *
M_PI);
1683 for (n = 1; n < num; n++)
1684 lsps[n] =
FFMAX(lsps[n], lsps[n - 1] + 0.0125 *
M_PI);
1685 lsps[num - 1] =
FFMIN(lsps[num - 1], 0.9985 *
M_PI);
1689 for (n = 1; n < num; n++) {
1690 if (lsps[n] < lsps[n - 1]) {
1691 for (m = 1; m < num; m++) {
1692 double tmp = lsps[m];
1693 for (l = m - 1; l >= 0; l--) {
1694 if (lsps[l] <=
tmp)
break;
1695 lsps[l + 1] = lsps[l];
1728 const double *
mean_lsf =
s->lsps == 16 ?
1734 memcpy(synth,
s->synth_history,
1735 s->lsps *
sizeof(*synth));
1736 memcpy(excitation,
s->excitation_history,
1737 s->history_nsamples *
sizeof(*excitation));
1739 if (
s->sframe_cache_size > 0) {
1742 s->sframe_cache_size = 0;
1758 "Superframe encodes > %d samples (%d), not allowed\n",
1765 if (
s->has_residual_lsps) {
1768 for (n = 0; n <
s->lsps; n++)
1769 prev_lsps[n] =
s->prev_lsps[n] -
mean_lsf[n];
1771 if (
s->lsps == 10) {
1776 for (n = 0; n <
s->lsps; n++) {
1778 lsps[1][n] =
mean_lsf[n] + (
a1[
s->lsps + n] -
a2[n * 2 + 1]);
1781 for (n = 0; n < 3; n++)
1793 frame->nb_samples = n_samples;
1794 samples = (
float *)
frame->data[0];
1797 for (n = 0; n < 3; n++) {
1798 if (!
s->has_residual_lsps) {
1801 if (
s->lsps == 10) {
1806 for (m = 0; m <
s->lsps; m++)
1813 lsps[n], n == 0 ?
s->prev_lsps : lsps[n - 1],
1837 memcpy(
s->prev_lsps, lsps[2],
1838 s->lsps *
sizeof(*
s->prev_lsps));
1840 s->lsps *
sizeof(*synth));
1842 s->history_nsamples *
sizeof(*excitation));
1845 s->history_nsamples *
sizeof(*
s->zero_exc_pf));
1860 unsigned int res, n_superframes = 0;
1870 n_superframes += res;
1871 }
while (res == 0x3F);
1872 s->spillover_nbits =
get_bits(gb,
s->spillover_bitsize);
1896 int rmn_bytes, rmn_bits;
1899 if (rmn_bits < nbits)
1903 rmn_bits &= 7; rmn_bytes >>= 3;
1904 if ((rmn_bits =
FFMIN(rmn_bits, nbits)) > 0)
1907 FFMIN(nbits - rmn_bits, rmn_bytes << 3));
1922 int *got_frame_ptr,
AVPacket *avpkt)
1926 const uint8_t *buf = avpkt->
data;
1944 if (!(
size %
ctx->block_align)) {
1946 s->spillover_nbits = 0;
1947 s->nb_superframes = 0;
1951 s->nb_superframes = res;
1957 if (
s->sframe_cache_size > 0) {
1959 if (cnt +
s->spillover_nbits > avpkt->
size * 8) {
1960 s->spillover_nbits = avpkt->
size * 8 - cnt;
1964 s->sframe_cache_size +=
s->spillover_nbits;
1967 cnt +=
s->spillover_nbits;
1968 s->skip_bits_next = cnt & 7;
1974 }
else if (
s->spillover_nbits) {
1977 }
else if (
s->skip_bits_next)
1981 s->sframe_cache_size = 0;
1982 s->skip_bits_next = 0;
1984 if (
s->nb_superframes-- == 0) {
1987 }
else if (
s->nb_superframes > 0) {
1990 }
else if (*got_frame_ptr) {
1992 s->skip_bits_next = cnt & 7;
1996 }
else if ((
s->sframe_cache_size =
pos) > 0) {
2022 .p.name =
"wmavoice",
void ff_tilt_compensation(float *mem, float tilt, float *samples, int size)
Apply tilt compensation filter, 1 - tilt * z-1.
void ff_acelp_apply_order_2_transfer_function(float *out, const float *in, const float zero_coeffs[2], const float pole_coeffs[2], float gain, float mem[2], int n)
Apply an order 2 rational transfer function in-place.
void ff_acelp_interpolatef(float *out, const float *in, const float *filter_coeffs, int precision, int frac_pos, int filter_length, int length)
Floating point version of ff_acelp_interpolate()
void ff_set_fixed_vector(float *out, const AMRFixed *in, float scale, int size)
Add fixed vector to an array from a sparse representation.
void ff_weighted_vector_sumf(float *out, const float *in_a, const float *in_b, float weight_coeff_a, float weight_coeff_b, int length)
float implementation of weighted sum of two vectors.
static double val(void *priv, double ch)
const FFCodec ff_wmavoice_decoder
static av_cold void close(AVCodecParserContext *s)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP synthesis filter.
void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP zero synthesis filter.
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
static __device__ float sqrtf(float a)
static __device__ float fabsf(float a)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int(* init)(AVBSFContext *ctx)
static const uint8_t bits[8]
float ff_scalarproduct_float_c(const float *v1, const float *v2, int len)
Return the scalar product of two vectors of floats.
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
static int get_bits_left(GetBitContext *gb)
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static int get_bits_count(const GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
static const int16_t alpha[]
static const int sizes[][2]
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static int ff_thread_once(char *control, void(*routine)(void))
void ff_acelp_lspd2lpc(const double *lsp, float *lpc, int lp_half_order)
Reconstruct LPC coefficients from the line spectral pair frequencies.
static av_always_inline unsigned UMULH(unsigned a, unsigned b)
Memory handling functions.
#define LOCAL_ALIGNED_32(t, v,...)
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
static const uint16_t table[]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static int put_bits_left(PutBitContext *s)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static const uint8_t last_coeff[3]
void ff_sine_window_init(float *window, int n)
Generate a sine window.
#define FF_ARRAY_ELEMS(a)
static const float mean_lsf[10]
Sparse representation for the algebraic codebook (fixed) vector.
An AVChannelLayout holds information about the channel layout of audio data.
main external API structure.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
WMA Voice decoding context.
AVTXContext * dst
contexts for phase shift (in Hilbert
int nb_superframes
number of superframes in current packet
int aw_first_pulse_off[2]
index of first sample to which to apply AW-pulses, or -0xff if unset
float dcf_mem[2]
DC filter history.
AVTXContext * irdft
contexts for FFT-calculation in the
float denoise_filter_cache[MAX_FRAMESIZE]
int lsp_q_mode
defines quantizer defaults [0, 1]
int pitch_nbits
number of bits used to specify the pitch value in the frame header
int last_pitch_val
pitch value of the previous frame
float denoise_coeffs_pf[0x82]
aligned buffer for denoise coefficients
uint16_t block_conv_table[4]
boundaries for block pitch unit/scale conversion
int block_pitch_nbits
number of bits used to specify the first block's pitch value
int last_acb_type
frame type [0-2] of the previous frame
float zero_exc_pf[MAX_SIGNAL_HISTORY+MAX_SFRAMESIZE]
zero filter output (i.e. excitation) by postfilter
int sframe_cache_size
set to >0 if we have data from an (incomplete) superframe from a previous packet that spilled over in...
int aw_idx_is_ext
whether the AW index was encoded in 8 bits (instead of 6)
int8_t vbm_tree[25]
converts VLC codes to frame type
uint8_t sframe_cache[SFRAME_CACHE_MAXSIZE+AV_INPUT_BUFFER_PADDING_SIZE]
cache for superframe data split over multiple packets
GetBitContext gb
packet bitreader.
int denoise_tilt_corr
Whether to apply tilt correction to the Wiener filter coefficients (postfilter)
int lsps
number of LSPs per frame [10 or 16]
float postfilter_agc
gain control memory, used in adaptive_gain_control()
float synth_history[MAX_LSPS]
see excitation_history
float silence_gain
set for use in blocks if ACB_TYPE_NONE
int block_pitch_range
range of the block pitch
int frame_cntr
current frame index [0 - 0xFFFE]; is only used for comfort noise in pRNG()
int max_pitch_val
max value + 1 for pitch parsing
int block_delta_pitch_nbits
number of bits used to specify the delta pitch between this and the last block's pitch value,...
float excitation_history[MAX_SIGNAL_HISTORY]
cache of the signal of
int do_apf
whether to apply the averaged projection filter (APF)
int pitch_diff_sh16
((cur_pitch_val - last_pitch_val) << 16) / MAX_FRAMESIZE
int lsp_def_mode
defines different sets of LSP defaults [0, 1]
int history_nsamples
number of samples in history for signal prediction (through ACB)
float gain_pred_err[6]
cache for gain prediction
int has_residual_lsps
if set, superframes contain one set of LSPs that cover all frames, encoded as independent and residua...
int denoise_filter_cache_size
samples in denoise_filter_cache
float cos[511]
8-bit cosine/sine windows over [-pi,pi] range
int aw_next_pulse_off_cache
the position (relative to start of the second block) at which pulses should start to be positioned,...
av_tx_fn irdft_fn
postfilter (for denoise filter)
int block_delta_pitch_hrange
1/2 range of the delta (full range is from -this to +this-1)
float synth_filter_out_buf[0x80+MAX_LSPS_ALIGN16]
aligned buffer for postfilter speech synthesis
int denoise_strength
strength of denoising in Wiener filter [0-11]
int spillover_bitsize
number of bits used to specify spillover_nbits in the packet header = ceil(log2(ctx->block_align << 3...
int spillover_nbits
number of bits of the previous packet's last superframe preceding this packet's first full superframe...
int skip_bits_next
number of bits to skip at the next call to wmavoice_decode_packet() (since they're part of the previo...
av_tx_fn dst_fn
transform, part of postfilter)
int min_pitch_val
base value for pitch parsing code
int aw_pulse_range
the range over which aw_pulse_set1() can apply the pulse, relative to the value in aw_first_pulse_off...
int dc_level
Predicted amount of DC noise, based on which a DC removal filter is used.
float tilted_lpcs_pf[0x82]
aligned buffer for LPC tilting
PutBitContext pb
bitstream writer for sframe_cache
int aw_n_pulses[2]
number of AW-pulses in each block; note that this number can be negative (in which case it basically ...
double prev_lsps[MAX_LSPS]
LSPs of the last frame of the previous superframe.
Description of frame types.
uint8_t log_n_blocks
log2(n_blocks)
uint8_t acb_type
Adaptive codebook type (ACB_TYPE_*)
uint8_t dbl_pulses
how many pulse vectors have pulse pairs (rather than just one single pulse) only if fcb_type == FCB_T...
uint8_t fcb_type
Fixed codebook type (FCB_TYPE_*)
uint8_t n_blocks
amount of blocks per frame (each block (contains 160/n_blocks samples)
#define avpriv_request_sample(...)
static AVFormatContext * ctx
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
@ AV_TX_FLOAT_DST_I
Discrete Sine Transform I.
@ AV_TX_FLOAT_DCT_I
Discrete Cosine Transform I.
@ AV_TX_FLOAT_RDFT
Real to complex and complex to real DFTs.
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
static double a2(void *priv, double x, double y)
static double a1(void *priv, double x, double y)
#define VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vlc_table, nb_bits, nb_codes, lens, lens_wrap, syms, syms_wrap, syms_size, offset, flags)
static int interpol(MBContext *s, uint32_t *color, int x, int y, int linesize)
#define MAX_FRAMESIZE
maximum compressed frame size
static void dequant_lsp16r(GetBitContext *gb, double *i_lsps, const double *old, double *a1, double *a2, int q_mode)
Parse 16 independently-coded LSPs, and then derive the tables to generate LSPs for the other frames f...
static av_cold int decode_vbmtree(GetBitContext *gb, int8_t vbm_tree[25])
Set up the variable bit mode (VBM) tree from container extradata.
static av_cold int wmavoice_decode_init(AVCodecContext *ctx)
Set up decoder with parameters from demuxer (extradata etc.).
static void aw_parse_coords(WMAVoiceContext *s, GetBitContext *gb, const int *pitch)
Parse the offset of the first pitch-adaptive window pulses, and the distribution of pulses between th...
static int aw_pulse_set2(WMAVoiceContext *s, GetBitContext *gb, int block_idx, AMRFixed *fcb)
Apply second set of pitch-adaptive window pulses.
static const struct frame_type_desc frame_descs[17]
#define SFRAME_CACHE_MAXSIZE
maximum cache size for frame data that was split over two packets
static void synth_block_fcb_acb(WMAVoiceContext *s, GetBitContext *gb, int block_idx, int size, int block_pitch_sh2, const struct frame_type_desc *frame_desc, float *excitation)
Parse FCB/ACB signal for a single block.
static av_cold int wmavoice_decode_end(AVCodecContext *ctx)
#define MAX_LSPS_ALIGN16
same as MAX_LSPS; needs to be multiple of 16 for ASM input buffer alignment
static int synth_superframe(AVCodecContext *ctx, AVFrame *frame, int *got_frame_ptr)
Synthesize output samples for a single superframe.
static float tilt_factor(const float *lpcs, int n_lpcs)
Get the tilt factor of a formant filter from its transfer function.
static void dequant_lsp10i(GetBitContext *gb, double *lsps)
Parse 10 independently-coded LSPs.
static void calc_input_response(WMAVoiceContext *s, float *lpcs_src, int fcb_type, float *coeffs_dst, int remainder)
Derive denoise filter coefficients (in real domain) from the LPCs.
static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, int frame_idx, float *samples, const double *lsps, const double *prev_lsps, float *excitation, float *synth)
Synthesize output samples for a single frame.
static void dequant_lsp16i(GetBitContext *gb, double *lsps)
Parse 16 independently-coded LSPs.
static void dequant_lsp10r(GetBitContext *gb, double *i_lsps, const double *old, double *a1, double *a2, int q_mode)
Parse 10 independently-coded LSPs, and then derive the tables to generate LSPs for the other frames f...
static int parse_packet_header(WMAVoiceContext *s)
Parse the packet header at the start of each packet (input data to this decoder).
@ FCB_TYPE_AW_PULSES
Pitch-adaptive window (AW) pulse signals, used in particular for low-bitrate streams.
@ FCB_TYPE_SILENCE
comfort noise during silence generated from a hardcoded (fixed) codebook with per-frame (low) gain va...
@ FCB_TYPE_HARDCODED
hardcoded (fixed) codebook with per-block gain values
@ FCB_TYPE_EXC_PULSES
Innovation (fixed) codebook pulse sets in combinations of either single pulses or pulse pairs.
static void wiener_denoise(WMAVoiceContext *s, int fcb_type, float *synth_pf, int size, const float *lpcs)
This function applies a Wiener filter on the (noisy) speech signal as a means to denoise it.
static int kalman_smoothen(WMAVoiceContext *s, int pitch, const float *in, float *out, int size)
Kalman smoothing function.
static void copy_bits(PutBitContext *pb, const uint8_t *data, int size, GetBitContext *gb, int nbits)
Copy (unaligned) bits from gb/data/size to pb.
#define MAX_SIGNAL_HISTORY
maximum excitation signal history
static void adaptive_gain_control(float *out, const float *in, const float *speech_synth, int size, float alpha, float *gain_mem)
Adaptive gain control (as used in postfilter).
static av_cold void wmavoice_flush(AVCodecContext *ctx)
#define VLC_NBITS
number of bits to read per VLC iteration
#define log_range(var, assign)
static void postfilter(WMAVoiceContext *s, const float *synth, float *samples, int size, const float *lpcs, float *zero_exc_pf, int fcb_type, int pitch)
Averaging projection filter, the postfilter used in WMAVoice.
static void dequant_lsps(double *lsps, int num, const uint16_t *values, const uint16_t *sizes, int n_stages, const uint8_t *table, const double *mul_q, const double *base_q)
Dequantize LSPs.
static void stabilize_lsps(double *lsps, int num)
Ensure minimum value for first item, maximum value for last value, proper spacing between each value ...
@ ACB_TYPE_HAMMING
Per-block pitch with signal generation using a Hamming sinc window function.
@ ACB_TYPE_ASYMMETRIC
adaptive codebook with per-frame pitch, which we interpolate to get a per-sample pitch.
@ ACB_TYPE_NONE
no adaptive codebook (only hardcoded fixed)
#define MAX_BLOCKS
maximum number of blocks per frame
static int wmavoice_decode_packet(AVCodecContext *ctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Packet decoding: a packet is anything that the (ASF) demuxer contains, and we expect that the demuxer...
static av_cold void wmavoice_init_static_data(void)
#define MAX_SFRAMESIZE
maximum number of samples per superframe
static int pRNG(int frame_cntr, int block_num, int block_size)
Generate a random number from frame_cntr and block_idx, which will live in the range [0,...
static void synth_block_hardcoded(WMAVoiceContext *s, GetBitContext *gb, int block_idx, int size, const struct frame_type_desc *frame_desc, float *excitation)
Parse hardcoded signal for a single block.
static void aw_pulse_set1(WMAVoiceContext *s, GetBitContext *gb, int block_idx, AMRFixed *fcb)
Apply first set of pitch-adaptive window pulses.
#define MAX_FRAMESIZE
maximum number of samples per frame
static void synth_block(WMAVoiceContext *s, GetBitContext *gb, int block_idx, int size, int block_pitch_sh2, const double *lsps, const double *prev_lsps, const struct frame_type_desc *frame_desc, float *excitation, float *synth)
Parse data in a single block.
static VLCElem frame_type_vlc[132]
Frame type VLC coding.
#define MAX_LSPS
maximum filter order
Windows Media Voice (WMAVoice) tables.
static const uint8_t wmavoice_dq_lsp16i3[0x300]
static const float wmavoice_ipol1_coeffs[17 *9]
static const float wmavoice_ipol2_coeffs[32]
Hamming-window sinc function (num = 32, x = [ 0, 31 ]): (0.54 + 0.46 * cos(2 * M_PI * x / (num - 1)))...
static const uint8_t wmavoice_dq_lsp10r[0x1400]
static const uint8_t wmavoice_dq_lsp10i[0xf00]
static const float wmavoice_gain_universal[64]
static const uint8_t wmavoice_dq_lsp16i1[0x640]
static const float wmavoice_gain_codebook_fcb[128]
static const float wmavoice_lsp16_intercoeff_b[32][2][16]
static const uint8_t wmavoice_dq_lsp16r3[0x600]
static const float wmavoice_denoise_power_table[12][64]
LUT for f(x,y) = pow((y + 6.9) / 64, 0.025 * (x + 1)).
static const uint8_t wmavoice_dq_lsp16i2[0x3c0]
static const float wmavoice_energy_table[128]
LUT for 1.071575641632 * pow(1.0331663, n - 127)
static const float wmavoice_lsp10_intercoeff_b[32][2][10]
static const float wmavoice_gain_silence[256]
static const uint8_t wmavoice_dq_lsp16r1[0x500]
static const uint8_t wmavoice_dq_lsp16r2[0x500]
static const float wmavoice_lsp10_intercoeff_a[32][2][10]
static const double wmavoice_mean_lsf16[2][16]
static const double wmavoice_mean_lsf10[2][10]
static const float wmavoice_lsp16_intercoeff_a[32][2][16]
static const float wmavoice_std_codebook[1000]
static const float wmavoice_gain_codebook_acb[128]