68 for (pass = 0; pass < 20; pass++) {
70 for (i = 0; i < order+1; i++) {
71 int low = i != 0 ? nlsf[i-1] : 0;
72 int high = i != order ? nlsf[i] : 32768;
73 int diff = (high - low) - (min_delta[i]);
75 if (diff < min_diff) {
89 nlsf[0] = min_delta[0];
90 }
else if (k == order) {
92 nlsf[order-1] = 32768 - min_delta[order];
95 int min_center = 0, max_center = 32768, center_val;
98 for (i = 0; i < k; i++)
99 min_center += min_delta[i];
100 min_center += min_delta[k] >> 1;
103 for (i = order; i > k; i--)
104 max_center -= min_delta[i];
105 max_center -= min_delta[k] >> 1;
108 center_val = nlsf[k - 1] + nlsf[k];
109 center_val = (center_val >> 1) + (center_val & 1);
110 center_val =
FFMIN(max_center,
FFMAX(min_center, center_val));
112 nlsf[k - 1] = center_val - (min_delta[k] >> 1);
113 nlsf[k] = nlsf[k - 1] + min_delta[k];
120 for (i = 1; i < order; i++) {
121 int j,
value = nlsf[i];
122 for (j = i - 1; j >= 0 && nlsf[j] >
value; j--)
123 nlsf[j + 1] = nlsf[j];
128 if (nlsf[0] < min_delta[0])
129 nlsf[0] = min_delta[0];
130 for (i = 1; i < order; i++)
131 nlsf[i] =
FFMAX(nlsf[i],
FFMIN(nlsf[i - 1] + min_delta[i], 32767));
134 if (nlsf[order-1] > 32768 - min_delta[order])
135 nlsf[order-1] = 32768 - min_delta[order];
136 for (i = order-2; i >= 0; i--)
137 if (nlsf[i] > nlsf[i + 1] - min_delta[i+1])
138 nlsf[i] = nlsf[i + 1] - min_delta[i+1];
145 int k, j, DC_resp = 0;
147 int totalinvgain = 1 << 30;
148 int32_t *row = lpc32[0], *prevrow;
151 for (k = 0; k < order; k++) {
153 row[k] = lpc[k] * 4096;
160 for (k = order - 1; 1; k--) {
167 if (
FFABS(row[k]) > 16773022)
170 rc = -(row[k] * 128);
171 gaindiv = (1 << 30) -
MULH(rc, rc);
173 totalinvgain =
MULH(totalinvgain, gaindiv) << 2;
175 return (totalinvgain >= 107374);
179 gain = ((1 << 29) - 1) / (gaindiv >> (fbits + 1 - 16));
180 error = (1 << 29) -
MULL(gaindiv << (15 + 16 - fbits), gain, 16);
181 gain = ((gain << 16) + (error * gain >> 13));
187 for (j = 0; j < k; j++) {
188 int x = av_sat_sub32(prevrow[j],
ROUND_MULL(prevrow[k - j - 1], rc, 31));
193 if (tmp < INT32_MIN || tmp > INT32_MAX)
208 for (i = 1; i < half_order; i++) {
209 pol[i + 1] = pol[i - 1] * 2 -
ROUND_MULL(lsp[2 * i], pol[i], 16);
210 for (j = i; j > 1; j--)
211 pol[j] += pol[j - 2] -
ROUND_MULL(lsp[2 * i], pol[j - 1], 16);
213 pol[1] -= lsp[2 * i];
217 static void silk_lsf2lpc(
const int16_t nlsf[16],
float lpcf[16],
int order)
226 for (k = 0; k < order; k++) {
227 int index = nlsf[k] >> 8;
228 int offset = nlsf[k] & 255;
234 lsp[k2] = (lsp[k2] + 4) >> 3;
241 for (k = 0; k < order>>1; k++) {
242 int32_t p_tmp = p[k + 1] + p[k];
243 int32_t q_tmp = q[k + 1] - q[k];
244 lpc32[k] = -q_tmp - p_tmp;
245 lpc32[order-k-1] = q_tmp - p_tmp;
249 for (i = 0; i < 10; i++) {
251 unsigned int maxabs = 0;
252 for (j = 0, k = 0; j < order; j++) {
253 unsigned int x =
FFABS(lpc32[k]);
260 maxabs = (maxabs + 16) >> 5;
262 if (maxabs > 32767) {
264 unsigned int chirp, chirp_base;
265 maxabs =
FFMIN(maxabs, 163838);
266 chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2);
268 for (k = 0; k < order; k++) {
270 chirp = (chirp_base * chirp + 32768) >> 16;
277 for (k = 0; k < order; k++) {
278 int x = (lpc32[k] + 16) >> 5;
279 lpc[k] = av_clip_int16(x);
280 lpc32[k] = lpc[k] << 5;
283 for (k = 0; k < order; k++)
284 lpc[k] = (lpc32[k] + 16) >> 5;
290 unsigned int chirp, chirp_base;
291 chirp_base = chirp = 65536 - (1 << i);
293 for (k = 0; k < order; k++) {
295 lpc[k] = (lpc32[k] + 16) >> 5;
296 chirp = (chirp_base * chirp + 32768) >> 16;
300 for (i = 0; i < order; i++)
301 lpcf[i] = lpc[i] / 4096.0
f;
306 float lpc_leadin[16],
float lpc[16],
307 int *lpc_order,
int *has_lpc_leadin,
int voiced)
311 int8_t lsf_i1, lsf_i2[16];
315 *lpc_order = order = s->
wb ? 16 : 10;
319 for (i = 0; i < order; i++) {
325 else if (lsf_i2[i] == 4)
330 for (i = order - 1; i >= 0; i--) {
331 int qstep = s->
wb ? 9830 : 11796;
333 lsf_res[i] = lsf_i2[i] * 1024;
334 if (lsf_i2[i] < 0) lsf_res[i] += 102;
335 else if (lsf_i2[i] > 0) lsf_res[i] -= 102;
336 lsf_res[i] = (lsf_res[i] * qstep) >> 16;
341 lsf_res[i] += (lsf_res[i+1] *
weight) >> 8;
346 for (i = 0; i < order; i++) {
349 int cur, prev, next, weight_sq,
weight, ipart, fpart, y,
value;
354 prev = i ? codebook[i - 1] : 0;
355 next = i + 1 < order ? codebook[i + 1] : 256;
356 weight_sq = (1024 / (cur - prev) + 1024 / (next - cur)) << 16;
360 fpart = (weight_sq >> (ipart-8)) & 127;
361 y = ((ipart & 1) ? 32768 : 46214) >> ((32 - ipart)>>1);
362 weight = y + ((213 * fpart * y) >> 16);
364 value = cur * 128 + (lsf_res[i] * 16384) / weight;
365 nlsf[i] = av_clip_uintp2(value, 15);
377 if (offset != 4 && frame->
coded) {
380 int16_t nlsf_leadin[16];
381 for (i = 0; i < order; i++)
382 nlsf_leadin[i] = frame->
nlsf[i] +
383 ((nlsf[i] - frame->
nlsf[i]) * offset >> 2);
386 memcpy(lpc_leadin, frame->
lpc, 16 *
sizeof(
float));
397 memcpy(frame->
nlsf, nlsf, order *
sizeof(nlsf[0]));
398 memcpy(frame->
lpc, lpc, order *
sizeof(lpc[0]));
407 child[1] = total - child[0];
416 int qoffset_high,
int active,
int voiced)
431 for (i = 0; i < shellblocks; i++) {
433 if (pulsecount[i] == 17) {
434 while (pulsecount[i] == 17 && ++lsbcount[i] != 10)
436 if (lsbcount[i] == 10)
442 for (i = 0; i < shellblocks; i++) {
443 if (pulsecount[i] != 0) {
445 int32_t * location = excitation + 16*i;
447 branch[0][0] = pulsecount[i];
450 for (a = 0; a < 1; a++) {
452 for (b = 0; b < 2; b++) {
454 for (c = 0; c < 2; c++) {
456 for (d = 0; d < 2; d++) {
464 memset(excitation + 16*i, 0, 16*
sizeof(
int32_t));
468 for (i = 0; i < shellblocks << 4; i++) {
470 for (bit = 0; bit < lsbcount[i >> 4]; bit++)
471 excitation[i] = (excitation[i] << 1) |
476 for (i = 0; i < shellblocks << 4; i++) {
477 if (excitation[i] != 0) {
479 voiced][qoffset_high][
FFMIN(pulsecount[i >> 4], 6)]);
486 for (i = 0; i < shellblocks << 4; i++) {
487 int value = excitation[i];
489 if (value < 0) excitation[i] += 20;
490 else if (value > 0) excitation[i] -= 20;
493 seed = 196314165 * seed + 907633515;
494 if (seed & 0x80000000)
498 excitationf[i] = excitation[i] / 8388608.0f;
503 #define SILK_MAX_LAG (288 + LTP_ORDER / 2)
509 int frame_num,
int channel,
int coded_channels,
int active,
int active1)
531 if (coded_channels == 2 && channel == 0) {
532 int n, wi[2], ws[2],
w[2];
539 for (i = 0; i < 2; i++)
557 qoffset_high = type & 1;
564 int ipart, fpart, lingain;
566 if (i == 0 && (frame_num == 0 || !frame->
coded)) {
576 log_gain = av_clip_uintp2(
FFMAX((delta_gain<<1) - 16,
577 frame->
log_gain + delta_gain - 4), 6);
583 log_gain = (log_gain * 0x1D1C71 >> 16) + 2090;
584 ipart = log_gain >> 7;
585 fpart = log_gain & 127;
586 lingain = (1 << ipart) + ((-174 * fpart * (128-fpart) >>16) + fpart) * ((1<<ipart) >> 7);
587 sf[i].gain = lingain / 65536.0f;
591 silk_decode_lpc(s, frame, rc, lpc_leadin, lpc_body, &order, &has_lpc_leadin, voiced);
595 int lag_absolute = (!frame_num || !frame->
prev_voiced);
598 const int8_t * offsets;
610 int highbits, lowbits;
611 static const uint16_t *
const model[] = {
637 sf[i].pitchlag = av_clip(primarylag + offsets[i],
645 static const uint16_t *
const filter_sel[] = {
649 static const int8_t (*
const filter_taps[])[5] = {
653 for (j = 0; j < 5; j++)
654 sf[i].ltptaps[j] = filter_taps[ltpfilter][index][j] / 128.0
f;
659 if (voiced && frame_num == 0)
662 else ltpscale = 15565.0f/16384.0f;
674 const float * lpc_coeff = (i < 2 && has_lpc_leadin) ? lpc_leadin : lpc_body;
685 if (i < 2 || s->nlsf_interp_factor == 4) {
695 for (j = - sf[i].pitchlag -
LTP_ORDER/2; j < out_end; j++) {
697 for (k = 0; k < order; k++)
698 sum -= lpc_coeff[k] * dst[j - k - 1];
699 resptr[j] = av_clipf(sum, -1.0
f, 1.0
f) * scale / sf[i].gain;
703 float rescale = sf[i-1].gain / sf[i].gain;
704 for (j = out_end; j < 0; j++)
705 resptr[j] *= rescale;
712 sum += sf[i].ltptaps[k] * resptr[j - sf[i].pitchlag + LTP_ORDER/2 - k];
719 sum = resptr[j] * sf[i].gain;
720 for (k = 1; k <= order; k++)
721 sum += lpc_coeff[k - 1] * lpc[j - k];
724 dst[j] = av_clipf(sum, -1.0
f, 1.0
f);
746 for (i = 0; i < n1; i++) {
747 float interp0 = w0_prev + i * (w0 - w0_prev) / n1;
748 float interp1 = w1_prev + i * (w1 - w1_prev) / n1;
749 float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]);
751 l[i] = av_clipf((1 + interp1) * mid[i - 1] + side[i - 1] + interp0 * p0, -1.0, 1.0);
752 r[i] = av_clipf((1 - interp1) * mid[i - 1] - side[i - 1] - interp0 * p0, -1.0, 1.0);
756 float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]);
758 l[i] = av_clipf((1 + w1) * mid[i - 1] + side[i - 1] + w0 * p0, -1.0, 1.0);
759 r[i] = av_clipf((1 - w1) * mid[i - 1] - side[i - 1] - w0 * p0, -1.0, 1.0);
773 memset(frame->
lpc, 0,
sizeof(frame->
lpc));
774 memset(frame->
nlsf, 0,
sizeof(frame->
nlsf));
789 int active[2][6], redundancy[2];
793 coded_channels > 2 || duration_ms > 60) {
795 "to the SILK decoder.\n");
799 nb_frames = 1 + (duration_ms > 20) + (duration_ms > 40);
800 s->
subframes = duration_ms / nb_frames / 5;
812 for (i = 0; i < coded_channels; i++) {
813 for (j = 0; j < nb_frames; j++)
823 for (i = 0; i < nb_frames; i++) {
824 for (j = 0; j < coded_channels && !s->
midonly; j++)
833 memcpy(output[j] + i * s->
flength,
864 if (output_channels != 1 && output_channels != 2) {
static void silk_lsp2poly(const int32_t lsp[16], int32_t pol[16], int half_order)
static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc, int frame_num, int channel, int coded_channels, int active, int active1)
const int8_t ff_silk_ltp_filter1_taps[16][5]
const uint8_t ff_silk_shell_blocks[3][2]
const uint16_t ff_silk_model_ltp_scale_index[]
const uint8_t ff_silk_lsf_codebook_wb[32][16]
const uint16_t ff_silk_pitch_max_lag[]
const uint16_t ff_silk_model_exc_rate[2][10]
const uint8_t ff_silk_lsf_pred_weights_nbmb[2][9]
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
const uint16_t ff_silk_model_pitch_contour_mbwb10ms[]
const uint16_t ff_silk_model_frame_type_active[]
const uint16_t ff_silk_model_mid_only[]
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const uint16_t ff_silk_model_pitch_lowbits_wb[]
const uint16_t ff_silk_model_ltp_filter1_sel[]
const int16_t ff_silk_stereo_weights[]
static void silk_count_children(OpusRangeCoder *rc, int model, int32_t total, int32_t child[2])
const uint16_t ff_silk_model_lsf_interpolation_offset[]
float output[2 *SILK_HISTORY]
const uint16_t ff_silk_model_lsf_s2[32][10]
const uint16_t ff_silk_model_pulse_location[4][168]
const int8_t ff_silk_pitch_offset_mbwb20ms[34][4]
const uint16_t ff_silk_model_gain_delta[]
const uint8_t ff_silk_lsf_ordering_nbmb[]
AVS_FilterInfo AVS_Value child
const int8_t ff_silk_pitch_offset_nb20ms[11][4]
const uint16_t ff_silk_model_excitation_lsb[]
static void silk_stabilize_lsf(int16_t nlsf[16], int order, const uint16_t min_delta[17])
const uint16_t ff_silk_model_lsf_s1[2][2][33]
const uint8_t ff_silk_lsf_s2_model_sel_nbmb[32][10]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const uint16_t ff_silk_model_pitch_contour_nb10ms[]
const uint16_t ff_silk_model_pitch_delta[]
const uint8_t ff_silk_lsf_codebook_nbmb[32][10]
const uint16_t ff_silk_model_gain_lowbits[]
const uint8_t ff_silk_lsf_pred_weights_wb[2][15]
const uint8_t ff_silk_lsf_weight_sel_wb[32][15]
const int8_t ff_silk_pitch_offset_mbwb10ms[12][2]
static const uint8_t offset[127][2]
static void silk_unmix_ms(SilkContext *s, float *l, float *r)
const uint16_t ff_silk_model_ltp_filter2_sel[]
const uint8_t ff_silk_lsf_s2_model_sel_wb[32][16]
const int8_t ff_silk_pitch_offset_nb10ms[3][2]
uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
const uint16_t ff_silk_model_excitation_sign[3][2][7][3]
const uint8_t ff_silk_quant_offset[2][2]
static void silk_decode_excitation(SilkContext *s, OpusRangeCoder *rc, float *excitationf, int qoffset_high, int active, int voiced)
const uint16_t ff_silk_lsf_min_spacing_nbmb[]
const uint16_t ff_silk_model_pitch_contour_mbwb20ms[]
static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
const uint16_t ff_silk_model_stereo_s3[]
const uint16_t ff_silk_model_ltp_filter0_sel[]
const uint16_t ff_silk_model_stereo_s1[]
enum OpusBandwidth bandwidth
GLsizei GLboolean const GLfloat * value
const uint16_t ff_silk_model_pitch_highbits[]
const int8_t ff_silk_ltp_filter0_taps[8][5]
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
const uint16_t ff_silk_model_pulse_count[11][19]
const uint16_t ff_silk_pitch_min_lag[]
int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, float *output[2], enum OpusBandwidth bandwidth, int coded_channels, int duration_ms)
Decode the LP layer of one Opus frame (which may correspond to several SILK frames).
static void error(const char *err)
const uint16_t ff_silk_model_pitch_lowbits_nb[]
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
const uint8_t ff_silk_lsf_weight_sel_nbmb[32][9]
#define SILK_MAX_LAG
Maximum residual history according to 4.2.7.6.1.
const uint16_t ff_silk_model_frame_type_inactive[]
main external API structure.
const int8_t ff_silk_ltp_filter2_taps[32][5]
#define LTP_ORDER
Order of the LTP filter.
static void silk_flush_frame(SilkFrame *frame)
const uint16_t ff_silk_lsf_min_spacing_wb[]
const int ff_silk_stereo_interp_len[3]
const uint8_t ff_silk_lsf_ordering_wb[]
const uint16_t ff_silk_model_pitch_contour_nb20ms[]
static void silk_decode_lpc(SilkContext *s, SilkFrame *frame, OpusRangeCoder *rc, float lpc_leadin[16], float lpc[16], int *lpc_order, int *has_lpc_leadin, int voiced)
#define ROUND_MULL(a, b, s)
static int weight(int i, int blen, int offset)
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
const uint16_t ff_silk_model_ltp_filter[]
const int16_t ff_silk_cosine[]
static int silk_is_lpc_stable(const int16_t lpc[16], int order)
channel
Use these values when setting the channel map with ebur128_set_channel().
float prev_stereo_weights[2]
void ff_silk_free(SilkContext **ps)
const uint16_t ff_silk_pitch_scale[]
static av_always_inline int diff(const uint32_t a, const uint32_t b)
const uint16_t ff_silk_model_gain_highbits[3][9]
void ff_silk_flush(SilkContext *s)
const uint16_t ff_silk_model_pitch_lowbits_mb[]
const uint16_t ff_silk_ltp_scale_factor[]
static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order)
float lpc_history[2 *SILK_HISTORY]
const uint16_t ff_silk_model_stereo_s2[]
const uint16_t ff_silk_model_lsf_s2_ext[]
const uint16_t ff_silk_model_lcg_seed[]