Go to the documentation of this file.
35 #define ROUND_MULL(a,b,s) (((MUL64(a, b) >> ((s) - 1)) + 1) >> 1)
73 for (pass = 0; pass < 20; pass++) {
75 for (
i = 0;
i < order+1;
i++) {
76 int low =
i != 0 ? nlsf[
i-1] : 0;
77 int high =
i != order ? nlsf[
i] : 32768;
78 int diff = (high - low) - (min_delta[
i]);
80 if (
diff < min_diff) {
94 nlsf[0] = min_delta[0];
95 }
else if (k == order) {
97 nlsf[order-1] = 32768 - min_delta[order];
100 int min_center = 0, max_center = 32768, center_val;
103 for (
i = 0;
i < k;
i++)
104 min_center += min_delta[
i];
105 min_center += min_delta[k] >> 1;
108 for (
i = order;
i > k;
i--)
109 max_center -= min_delta[
i];
110 max_center -= min_delta[k] >> 1;
113 center_val = nlsf[k - 1] + nlsf[k];
114 center_val = (center_val >> 1) + (center_val & 1);
115 center_val =
FFMIN(max_center,
FFMAX(min_center, center_val));
117 nlsf[k - 1] = center_val - (min_delta[k] >> 1);
118 nlsf[k] = nlsf[k - 1] + min_delta[k];
125 for (
i = 1;
i < order;
i++) {
127 for (j =
i - 1; j >= 0 && nlsf[j] >
value; j--)
128 nlsf[j + 1] = nlsf[j];
133 if (nlsf[0] < min_delta[0])
134 nlsf[0] = min_delta[0];
135 for (
i = 1;
i < order;
i++)
136 nlsf[
i] =
FFMAX(nlsf[
i],
FFMIN(nlsf[
i - 1] + min_delta[
i], 32767));
139 if (nlsf[order-1] > 32768 - min_delta[order])
140 nlsf[order-1] = 32768 - min_delta[order];
141 for (
i = order-2;
i >= 0;
i--)
142 if (nlsf[
i] > nlsf[
i + 1] - min_delta[
i+1])
143 nlsf[
i] = nlsf[
i + 1] - min_delta[
i+1];
150 int k, j, DC_resp = 0;
152 int totalinvgain = 1 << 30;
153 int32_t *row = lpc32[0], *prevrow;
156 for (k = 0; k < order; k++) {
158 row[k] = lpc[k] * 4096;
165 for (k = order - 1; 1; k--) {
172 if (
FFABS(row[k]) > 16773022)
175 rc = -(row[k] * 128);
176 gaindiv = (1 << 30) -
MULH(rc, rc);
178 totalinvgain =
MULH(totalinvgain, gaindiv) << 2;
180 return (totalinvgain >= 107374);
184 gain = ((1 << 29) - 1) / (gaindiv >> (fbits + 1 - 16));
185 error = (1 << 29) -
MULL(gaindiv << (15 + 16 - fbits), gain, 16);
186 gain = ((gain << 16) + (
error * gain >> 13));
192 for (j = 0; j < k; j++) {
198 if (tmp < INT32_MIN || tmp > INT32_MAX)
214 for (
i = 1;
i < half_order;
i++) {
216 for (j =
i; j > 1; j--)
217 pol[j] += pol[j - 2] -
ROUND_MULL(lsp[2 *
i], pol[j - 1], 16);
219 pol[1] -= lsp[2 *
i];
223 static void silk_lsf2lpc(
const int16_t nlsf[16],
float lpcf[16],
int order)
232 for (k = 0; k < order; k++) {
233 int index = nlsf[k] >> 8;
234 int offset = nlsf[k] & 255;
240 lsp[k2] = (lsp[k2] + 4) >> 3;
247 for (k = 0; k < order>>1; k++) {
248 int32_t p_tmp = p[k + 1] + p[k];
249 int32_t q_tmp = q[k + 1] - q[k];
250 lpc32[k] = -q_tmp - p_tmp;
251 lpc32[order-k-1] = q_tmp - p_tmp;
255 for (
i = 0;
i < 10;
i++) {
257 unsigned int maxabs = 0;
258 for (j = 0, k = 0; j < order; j++) {
259 unsigned int x =
FFABS(lpc32[k]);
266 maxabs = (maxabs + 16) >> 5;
268 if (maxabs > 32767) {
270 unsigned int chirp, chirp_base;
271 maxabs =
FFMIN(maxabs, 163838);
272 chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2);
274 for (k = 0; k < order; k++) {
276 chirp = (chirp_base * chirp + 32768) >> 16;
283 for (k = 0; k < order; k++) {
284 int x = (lpc32[k] + 16) >> 5;
286 lpc32[k] = lpc[k] << 5;
289 for (k = 0; k < order; k++)
290 lpc[k] = (lpc32[k] + 16) >> 5;
296 unsigned int chirp, chirp_base;
297 chirp_base = chirp = 65536 - (1 <<
i);
299 for (k = 0; k < order; k++) {
301 lpc[k] = (lpc32[k] + 16) >> 5;
302 chirp = (chirp_base * chirp + 32768) >> 16;
306 for (
i = 0;
i < order;
i++)
307 lpcf[
i] = lpc[
i] / 4096.0
f;
312 float lpc_leadin[16],
float lpc[16],
313 int *lpc_order,
int *has_lpc_leadin,
int voiced)
317 int8_t lsf_i1, lsf_i2[16];
321 *lpc_order = order =
s->wb ? 16 : 10;
325 for (
i = 0;
i < order;
i++) {
331 else if (lsf_i2[
i] == 4)
336 for (
i = order - 1;
i >= 0;
i--) {
337 int qstep =
s->wb ? 9830 : 11796;
339 lsf_res[
i] = lsf_i2[
i] * 1024;
340 if (lsf_i2[
i] < 0) lsf_res[
i] += 102;
341 else if (lsf_i2[
i] > 0) lsf_res[
i] -= 102;
342 lsf_res[
i] = (lsf_res[
i] * qstep) >> 16;
347 lsf_res[
i] += (lsf_res[
i+1] *
weight) >> 8;
352 for (
i = 0;
i < order;
i++) {
355 int cur, prev, next, weight_sq,
weight, ipart, fpart, y,
value;
362 weight_sq = (1024 / (cur - prev) + 1024 / (next - cur)) << 16;
366 fpart = (weight_sq >> (ipart-8)) & 127;
367 y = ((ipart & 1) ? 32768 : 46214) >> ((32 - ipart)>>1);
368 weight = y + ((213 * fpart * y) >> 16);
381 if (
s->subframes == 4) {
386 int16_t nlsf_leadin[16];
387 for (
i = 0;
i < order;
i++)
388 nlsf_leadin[
i] =
frame->nlsf[
i] +
392 memcpy(lpc_leadin,
frame->lpc, 16 *
sizeof(
float));
395 s->nlsf_interp_factor =
offset;
399 s->nlsf_interp_factor = 4;
403 memcpy(
frame->nlsf, nlsf, order *
sizeof(nlsf[0]));
404 memcpy(
frame->lpc, lpc, order *
sizeof(lpc[0]));
413 child[1] = total - child[0];
422 int qoffset_high,
int active,
int voiced)
428 uint8_t pulsecount[20];
429 uint8_t lsbcount[20] = {0};
437 for (
i = 0;
i < shellblocks;
i++) {
439 if (pulsecount[
i] == 17) {
440 while (pulsecount[
i] == 17 && ++lsbcount[
i] != 10)
442 if (lsbcount[
i] == 10)
448 for (
i = 0;
i < shellblocks;
i++) {
449 if (pulsecount[
i] != 0) {
451 int32_t * location = excitation + 16*
i;
453 branch[0][0] = pulsecount[
i];
456 for (
a = 0;
a < 1;
a++) {
458 for (
b = 0;
b < 2;
b++) {
460 for (
c = 0;
c < 2;
c++) {
462 for (
d = 0;
d < 2;
d++) {
470 memset(excitation + 16*
i, 0, 16*
sizeof(
int32_t));
474 for (
i = 0;
i < shellblocks << 4;
i++) {
476 for (
bit = 0; bit < lsbcount[i >> 4];
bit++)
477 excitation[
i] = (excitation[
i] << 1) |
482 for (
i = 0;
i < shellblocks << 4;
i++) {
483 if (excitation[
i] != 0) {
485 voiced][qoffset_high][
FFMIN(pulsecount[
i >> 4], 6)]);
492 for (
i = 0;
i < shellblocks << 4;
i++) {
493 int value = excitation[
i];
495 if (
value < 0) excitation[
i] += 20;
496 else if (
value > 0) excitation[
i] -= 20;
499 seed = 196314165 *
seed + 907633515;
500 if (
seed & 0x80000000)
504 excitationf[
i] = excitation[
i] / 8388608.0f;
509 #define SILK_MAX_LAG (288 + LTP_ORDER / 2)
515 int frame_num,
int channel,
int coded_channels,
516 int active,
int active1,
int redundant)
538 if (coded_channels == 2 &&
channel == 0) {
539 int n, wi[2], ws[2],
w[2];
546 for (
i = 0;
i < 2;
i++)
551 s->stereo_weights[0] = (
w[0] -
w[1]) / 8192.0;
552 s->stereo_weights[1] =
w[1] / 8192.0;
564 qoffset_high =
type & 1;
569 for (
i = 0;
i <
s->subframes;
i++) {
571 int ipart, fpart, lingain;
573 if (
i == 0 && (frame_num == 0 || !
frame->coded)) {
579 log_gain =
FFMAX(log_gain,
frame->log_gain - 16);
584 frame->log_gain + delta_gain - 4), 6);
587 frame->log_gain = log_gain;
590 log_gain = (log_gain * 0x1D1C71 >> 16) + 2090;
591 ipart = log_gain >> 7;
592 fpart = log_gain & 127;
593 lingain = (1 << ipart) + ((-174 * fpart * (128-fpart) >>16) + fpart) * ((1<<ipart) >> 7);
594 sf[
i].gain = lingain / 65536.0f;
602 int lag_absolute = (!frame_num || !
frame->prev_voiced);
617 int highbits, lowbits;
618 static const uint16_t *
const model[] = {
628 frame->primarylag = primarylag;
630 if (
s->subframes == 2)
643 for (
i = 0;
i <
s->subframes;
i++)
650 for (
i = 0;
i <
s->subframes;
i++) {
652 static const uint16_t *
const filter_sel[] = {
656 static const int8_t (*
const filter_taps[])[5] = {
660 for (j = 0; j < 5; j++)
661 sf[
i].ltptaps[j] = filter_taps[ltpfilter][
index][j] / 128.0
f;
666 if (voiced && frame_num == 0)
669 else ltpscale = 15565.0f/16384.0f;
677 if (
s->output_channels ==
channel || redundant)
681 for (
i = 0;
i <
s->subframes;
i++) {
682 const float * lpc_coeff = (
i < 2 && has_lpc_leadin) ? lpc_leadin : lpc_body;
693 if (i < 2 || s->nlsf_interp_factor == 4) {
694 out_end = -
i *
s->sflength;
697 out_end = -(
i - 2) *
s->sflength;
703 for (j = - sf[
i].pitchlag -
LTP_ORDER/2; j < out_end; j++) {
705 for (k = 0; k < order; k++)
706 sum -= lpc_coeff[k] * dst[j - k - 1];
711 float rescale = sf[
i-1].gain / sf[
i].gain;
712 for (j = out_end; j < 0; j++)
717 for (j = 0; j <
s->sflength; j++) {
720 sum += sf[
i].ltptaps[k] * resptr[j - sf[
i].pitchlag +
LTP_ORDER/2 - k];
726 for (j = 0; j <
s->sflength; j++) {
727 sum = resptr[j] * sf[
i].gain;
728 for (k = 1; k <= order; k++)
729 sum += lpc_coeff[k - 1] * lpc[j - k];
736 frame->prev_voiced = voiced;
747 float w0_prev =
s->prev_stereo_weights[0];
748 float w1_prev =
s->prev_stereo_weights[1];
749 float w0 =
s->stereo_weights[0];
750 float w1 =
s->stereo_weights[1];
754 for (
i = 0;
i < n1;
i++) {
755 float interp0 = w0_prev +
i * (w0 - w0_prev) / n1;
756 float interp1 = w1_prev +
i * (w1 - w1_prev) / n1;
757 float p0 = 0.25 * (mid[
i - 2] + 2 * mid[
i - 1] + mid[
i]);
759 l[
i] =
av_clipf((1 + interp1) * mid[
i - 1] + side[
i - 1] + interp0 * p0, -1.0, 1.0);
760 r[
i] =
av_clipf((1 - interp1) * mid[
i - 1] - side[
i - 1] - interp0 * p0, -1.0, 1.0);
763 for (;
i <
s->flength;
i++) {
764 float p0 = 0.25 * (mid[
i - 2] + 2 * mid[
i - 1] + mid[
i]);
766 l[
i] =
av_clipf((1 + w1) * mid[
i - 1] + side[
i - 1] + w0 * p0, -1.0, 1.0);
767 r[
i] =
av_clipf((1 - w1) * mid[
i - 1] - side[
i - 1] - w0 * p0, -1.0, 1.0);
770 memcpy(
s->prev_stereo_weights,
s->stereo_weights,
sizeof(
s->stereo_weights));
778 memset(
frame->output, 0,
sizeof(
frame->output));
779 memset(
frame->lpc_history, 0,
sizeof(
frame->lpc_history));
786 frame->primarylag = 0;
787 frame->prev_voiced = 0;
797 int active[2][6], redundancy[2];
801 coded_channels > 2 || duration_ms > 60) {
803 "to the SILK decoder.\n");
807 nb_frames = 1 + (duration_ms > 20) + (duration_ms > 40);
808 s->subframes = duration_ms / nb_frames / 5;
809 s->sflength = 20 * (bandwidth + 2);
810 s->flength =
s->sflength *
s->subframes;
811 s->bandwidth = bandwidth;
815 if (coded_channels >
s->prev_coded_channels)
817 s->prev_coded_channels = coded_channels;
820 for (
i = 0;
i < coded_channels;
i++) {
821 for (j = 0; j < nb_frames; j++)
828 for (
i = 0;
i < coded_channels;
i++)
829 if (redundancy[
i] && duration_ms > 20) {
835 for (
i = 0;
i < nb_frames;
i++) {
836 for (j = 0; j < coded_channels; j++)
837 if (redundancy[j] & (1 <<
i)) {
838 int active1 = (j == 0 && !(redundancy[1] & (1 <<
i))) ? 0 : 1;
845 for (
i = 0;
i < nb_frames;
i++) {
846 for (j = 0; j < coded_channels && !
s->midonly; j++)
850 if (
s->midonly &&
s->frame[1].coded)
853 if (coded_channels == 1 ||
s->output_channels == 1) {
854 for (j = 0; j <
s->output_channels; j++) {
857 s->flength *
sizeof(
float));
866 return nb_frames *
s->flength;
879 memset(
s->prev_stereo_weights, 0,
sizeof(
s->prev_stereo_weights));
886 if (output_channels != 1 && output_channels != 2) {
897 s->output_channels = output_channels;
static void error(const char *err)
const uint16_t ff_silk_model_ltp_filter1_sel[]
const uint16_t ff_silk_model_lbrr_flags_40[]
const uint16_t ff_silk_model_mid_only[]
static void silk_flush_frame(SilkFrame *frame)
const uint16_t ff_silk_model_exc_rate[2][10]
const uint16_t ff_silk_model_lsf_interpolation_offset[]
float lpc_history[2 *SILK_HISTORY]
#define LTP_ORDER
Order of the LTP filter.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
const uint16_t ff_silk_model_lsf_s2[32][10]
@ OPUS_BANDWIDTH_NARROWBAND
float prev_stereo_weights[2]
const uint16_t ff_silk_model_pitch_contour_mbwb10ms[]
const uint8_t ff_silk_lsf_ordering_nbmb[]
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
const uint16_t ff_silk_pitch_max_lag[]
float output[2 *SILK_HISTORY]
const uint16_t ff_silk_model_frame_type_active[]
const int8_t ff_silk_pitch_offset_nb20ms[11][4]
const uint16_t ff_silk_model_gain_delta[]
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]
#define bit(string, value)
const uint8_t ff_silk_shell_blocks[3][2]
void ff_silk_flush(SilkContext *s)
const int16_t ff_silk_stereo_weights[]
const uint16_t ff_silk_model_ltp_scale_index[]
enum OpusBandwidth bandwidth
const uint8_t ff_silk_lsf_codebook_wb[32][16]
const uint16_t ff_silk_model_pitch_contour_nb10ms[]
const uint16_t ff_silk_model_pulse_location[4][168]
const int8_t ff_silk_pitch_offset_mbwb20ms[34][4]
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 type
static av_always_inline float scale(float x, float s)
static void silk_lsp2poly(const int32_t lsp[], int32_t pol[], int half_order)
const uint16_t ff_silk_model_excitation_lsb[]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const uint8_t ff_silk_lsf_s2_model_sel_wb[32][16]
static const int offsets[]
const int8_t ff_silk_pitch_offset_mbwb10ms[12][2]
const uint8_t ff_silk_lsf_s2_model_sel_nbmb[32][10]
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
@ OPUS_BANDWIDTH_WIDEBAND
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 SILK_MAX_LAG
Maximum residual history according to 4.2.7.6.1.
const uint16_t ff_silk_model_gain_lowbits[]
const uint16_t ff_silk_model_stereo_s3[]
const uint8_t ff_silk_quant_offset[2][2]
const int8_t ff_silk_pitch_offset_nb10ms[3][2]
const uint16_t ff_silk_model_ltp_filter2_sel[]
const uint16_t ff_silk_model_excitation_sign[3][2][7][3]
int ff_silk_init(void *logctx, SilkContext **ps, int output_channels)
const uint16_t ff_silk_lsf_min_spacing_nbmb[]
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
static int weight(int i, int blen, int offset)
const uint16_t ff_silk_pitch_min_lag[]
static void silk_unmix_ms(SilkContext *s, float *l, float *r)
const uint8_t ff_silk_lsf_codebook_nbmb[32][10]
const uint16_t ff_silk_model_pitch_lowbits_nb[]
const uint16_t ff_silk_model_pitch_delta[]
const uint16_t ff_silk_model_ltp_filter0_sel[]
const uint8_t ff_silk_lsf_weight_sel_wb[32][15]
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 uint8_t ff_silk_lsf_ordering_wb[]
const uint8_t ff_silk_lsf_pred_weights_wb[2][15]
static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order)
static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc, int frame_num, int channel, int coded_channels, int active, int active1, int redundant)
const uint16_t ff_silk_model_pitch_highbits[]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
const int8_t ff_silk_ltp_filter2_taps[32][5]
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
static void silk_count_children(OpusRangeCoder *rc, int model, int32_t total, int32_t child[2])
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 offset
static int silk_is_lpc_stable(const int16_t lpc[16], int order)
uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
const int16_t ff_silk_cosine[]
const uint16_t ff_silk_model_stereo_s1[]
const uint16_t ff_silk_model_pitch_contour_nb20ms[]
const int8_t ff_silk_ltp_filter0_taps[8][5]
#define i(width, name, range_min, range_max)
const int ff_silk_stereo_interp_len[3]
const uint16_t ff_silk_model_pulse_count[11][19]
void ff_silk_free(SilkContext **ps)
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
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_mb[]
const uint8_t ff_silk_lsf_weight_sel_nbmb[32][9]
static void silk_decode_excitation(SilkContext *s, OpusRangeCoder *rc, float *excitationf, int qoffset_high, int active, int voiced)
const uint16_t ff_silk_model_frame_type_inactive[]
const uint16_t ff_silk_model_lsf_s2_ext[]
const uint16_t ff_silk_model_gain_highbits[3][9]
const uint16_t ff_silk_lsf_min_spacing_wb[]
#define ROUND_MULL(a, b, s)
const uint16_t ff_silk_pitch_scale[]
const uint16_t ff_silk_ltp_scale_factor[]
const int8_t ff_silk_ltp_filter1_taps[16][5]
const uint8_t ff_silk_lsf_pred_weights_nbmb[2][9]
const uint16_t ff_silk_model_ltp_filter[]
const uint16_t ff_silk_model_stereo_s2[]
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
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).
const uint16_t ff_silk_model_lcg_seed[]
const uint16_t ff_silk_model_lbrr_flags_60[]
static const unsigned codebook[256][2]
const uint16_t ff_silk_model_pitch_lowbits_wb[]