1 /*
2 * TwinVQ decoder
3 * Copyright (c) 2009 Vitor Sessak
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 #include <math.h>
23 #include <stdint.h>
24
33
34 /**
35 * Evaluate a single LPC amplitude spectrum envelope coefficient from the line
36 * spectrum pairs.
37 *
38 * @param lsp a vector of the cosine of the LSP values
39 * @param cos_val cos(PI*i/N) where i is the index of the LPC amplitude
40 * @param order the order of the LSP (and the size of the *lsp buffer). Must
41 * be a multiple of four.
42 * @return the LPC value
43 *
44 * @todo reuse code from Vorbis decoder: vorbis_floor0_decode
45 */
47 {
48 int j;
49 float p = 0.5f;
50 float q = 0.5f;
51 float two_cos_w = 2.0f * cos_val;
52
53 for (j = 0; j + 1 < order; j += 2 * 2) {
54 // Unroll the loop once since order is a multiple of four
55 q *= lsp[j] - two_cos_w;
56 p *= lsp[j + 1] - two_cos_w;
57
58 q *= lsp[j + 2] - two_cos_w;
59 p *= lsp[j + 3] - two_cos_w;
60 }
61
62 p *= p * (2.0f - two_cos_w);
63 q *= q * (2.0f + two_cos_w);
64
65 return 0.5 / (p + q);
66 }
67
68 /**
69 * Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
70 */
72 {
73 int i;
76
77 for (i = 0; i < size_s / 2; i++) {
81 }
82 }
83
85 {
86 int i;
87 float step = (v1 - v2) / (size + 1);
88
89 for (i = 0; i <
size; i++) {
90 v2 += step;
91 out[i] = v2;
92 }
93 }
94
96 {
97 return part ? -cos_tab[size - idx - 1]
98 : cos_tab[idx];
99 }
100
101 /**
102 * Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
103 * Probably for speed reasons, the coefficients are evaluated as
104 * siiiibiiiisiiiibiiiisiiiibiiiisiiiibiiiis ...
105 * where s is an evaluated value, i is a value interpolated from the others
106 * and b might be either calculated or interpolated, depending on an
107 * unexplained condition.
108 *
109 * @param step the size of a block "siiiibiiii"
110 * @param in the cosine of the LSP data
111 * @param part is 0 for 0...PI (positive cosine values) and 1 for PI...2PI
112 * (negative cosine values)
113 * @param size the size of the whole output
114 */
117 float *
out,
const float *
in,
118 int size,
int step,
int part)
119 {
120 int i;
123
124 // Fill the 's'
125 for (i = 0; i <
size; i += step)
126 out[i] =
128 get_cos(i, part, cos_tab, size),
130
131 // Fill the 'iiiibiiii'
132 for (i = step; i <= size - 2 * step; i += step) {
133 if (out[i + step] + out[i - step] > 1.95 * out[i] ||
134 out[i + step] >= out[i - step]) {
135 interpolate(out + i - step + 1, out[i], out[i - step], step - 1);
136 } else {
137 out[i - step / 2] =
139 get_cos(i - step / 2, part, cos_tab, size),
142 out[i - step], step / 2 - 1);
144 out[i - step / 2], step / 2 - 1);
145 }
146 }
147
148 interpolate(out + size - 2 * step + 1, out[size - step],
149 out[size - 2 * step], step - 1);
150 }
151
153 const float *
buf,
float *lpc,
155 {
158 2 * step, 1);
159
160 interpolate(lpc + size / 2 - step + 1, lpc[size / 2],
161 lpc[size / 2 - step], step);
162
164 2 * step - 1);
165 }
166
167 /**
168 * Inverse quantization. Read CB coefficients for cb1 and cb2 from the
169 * bitstream, sum the corresponding vectors and write the result to *out
170 * after permutation.
171 */
174 const int16_t *cb0, const int16_t *cb1, int cb_len)
175 {
176 int pos = 0;
177 int i, j;
178
179 for (i = 0; i < tctx->
n_div[ftype]; i++) {
180 int tmp0, tmp1;
181 int sign0 = 1;
182 int sign1 = 1;
183 const int16_t *tab0, *
tab1;
186
188 tmp0 = *cb_bits++;
189 if (bits == 7) {
190 if (tmp0 & 0x40)
191 sign0 = -1;
192 tmp0 &= 0x3F;
193 }
194
196 tmp1 = *cb_bits++;
197 if (bits == 7) {
198 if (tmp1 & 0x40)
199 sign1 = -1;
200 tmp1 &= 0x3F;
201 }
202
203 tab0 = cb0 + tmp0 * cb_len;
204 tab1 = cb1 + tmp1 * cb_len;
205
206 for (j = 0; j <
length; j++)
207 out[tctx->
permut[ftype][pos + j]] = sign0 * tab0[j] +
208 sign1 * tab1[j];
209
210 pos += length;
211 }
212 }
213
216 {
219 int i, j;
223
226 out[i] = (1.0 / (1 << 13)) *
229 } else {
231 float val = (1.0 / (1 << 23)) *
234
235 for (j = 0; j < sub; j++)
236 out[i * sub + j] =
240 }
241 }
242 }
243
244 /**
245 * Rearrange the LSP coefficients so that they have a minimum distance of
246 * min_dist. This function does it exactly as described in section of 3.2.4
247 * of the G.729 specification (but interestingly is different from what the
248 * reference decoder actually does).
249 */
251 {
252 int i;
253 float min_dist2 = min_dist * 0.5;
254 for (i = 1; i < order; i++)
255 if (lsp[i] - lsp[i - 1] < min_dist) {
256 float avg = (lsp[i] + lsp[i - 1]) * 0.5;
257
258 lsp[i - 1] = avg - min_dist2;
259 lsp[i] = avg + min_dist2;
260 }
261 }
262
264 int lpc_hist_idx, float *lsp, float *hist)
265 {
267 int i, j;
268
271 const float *cb3 = cb2 + (1 << mtab->
lsp_bit2) * mtab->
n_lsp;
272
273 const int8_t funny_rounding[4] = {
274 -2,
277 0
278 };
279
280 j = 0;
282 int chunk_end = ((i + 1) * mtab->
n_lsp + funny_rounding[i]) /
284 for (; j < chunk_end; j++)
285 lsp[j] = cb[lpc_idx1 * mtab->
n_lsp + j] +
286 cb2[lpc_idx2[i] * mtab->
n_lsp + j];
287 }
288
290
291 for (i = 0; i < mtab->
n_lsp; i++) {
292 float tmp1 = 1.0 - cb3[lpc_hist_idx * mtab->
n_lsp + i];
293 float tmp2 = hist[i] * cb3[lpc_hist_idx * mtab->
n_lsp + i];
294 hist[i] = lsp[i];
295 lsp[i] = lsp[i] * tmp1 + tmp2;
296 }
297
301 }
302
305 {
306 int i;
308
310 lsp[i] = 2 * cos(lsp[i]);
311
312 switch (ftype) {
315 break;
318 break;
321 break;
322 }
323 }
324
326
328 int wtype,
float *
in,
float *prev,
int ch)
329 {
335 int j, first_wsize, wsize; // Window size
338 float *prev_buf;
339 int types_sizes[] = {
343 };
344
346 first_wsize = wsize;
347 prev_buf = prev + (size - bsize) / 2;
348
349 for (j = 0; j < mtab->
fmode[ftype].
sub; j++) {
351
352 if (!j && wtype == 4)
353 sub_wtype = 4;
354 else if (j == mtab->
fmode[ftype].
sub - 1 && wtype == 7)
355 sub_wtype = 7;
356
357 wsize = types_sizes[wtype_to_wsize[sub_wtype]];
358
359 mdct->
imdct_half(mdct, buf1 + bsize * j, in + bsize * j);
360
362 buf1 + bsize * j,
364 wsize / 2);
365 out2 += wsize;
366
367 memcpy(out2, buf1 + bsize * j + wsize / 2,
368 (bsize - wsize / 2) * sizeof(float));
369
371
372 prev_buf = buf1 + bsize * j + bsize / 2;
373 }
374
376 }
377
379 int wtype, float **out)
380 {
383 int size1, size2, i;
384
388 prev_buf + 2 * i * mtab->
size,
389 i);
390
391 if (!out)
392 return;
393
395 size1 = mtab->
size - size2;
396
397 memcpy(&out[0][0], prev_buf, size1 * sizeof(out[0][0]));
398 memcpy(&out[0][size1], tctx->
curr_frame, size2 *
sizeof(out[0][0]));
399
401 memcpy(&out[1][0], &prev_buf[2 * mtab->
size],
402 size1 * sizeof(out[1][0]));
404 size2 * sizeof(out[1][0]));
406 }
407 }
408
411 {
416 int block_size = mtab->
size / sub;
419
420 int i, j;
421
425
427
434 cb_len_p);
435 }
436
437 for (i = 0; i < channels; i++) {
438 float *chunk = out + mtab->
size * i;
440
441 for (j = 0; j < sub; j++) {
444 tctx->
tmp_buf, gain[sub * i + j], ftype);
445
447 chunk + block_size * j,
449 }
450
454
457
459
460 for (j = 0; j < mtab->
fmode[ftype].
sub; j++) {
462 chunk += block_size;
463 }
464 }
465 }
466
470 TWINVQ_FT_MEDIUM
471 };
472
474 int *got_frame_ptr,
AVPacket *avpkt)
475 {
478 int buf_size = avpkt->
size;
481 float **out = NULL;
483
484 /* get output buffer */
490 }
491
492 if (buf_size < avctx->block_align) {
494 "Frame too small (%d bytes). Truncated file?\n", buf_size);
496 }
497
500
502
504
506
509 *got_frame_ptr = 0;
510 return buf_size;
511 }
512
513 *got_frame_ptr = 1;
514
516 }
517
518 /**
519 * Init IMDCT and windowing tables
520 */
522 {
528 float norm = channels == 1 ? 2.0 : 1.0;
529
530 for (i = 0; i < 3; i++) {
533 -sqrt(norm / bsize) / (1 << 15))))
535 }
536
539
542 alloc_fail);
545 alloc_fail);
548 alloc_fail);
549
550 for (i = 0; i < 3; i++) {
552 double freq = 2 *
M_PI /
m;
554 (m / 4) *
sizeof(*tctx->
cos_tabs[i]), alloc_fail);
555
556 for (j = 0; j <= m / 8; j++)
557 tctx->
cos_tabs[i][j] = cos((2 * j + 1) * freq);
558 for (j = 1; j < m / 8; j++)
560 }
561
565
566 return 0;
567
568 alloc_fail:
570 }
571
572 /**
573 * Interpret the data as if it were a num_blocks x line_len[0] matrix and for
574 * each line do a cyclic permutation, i.e.
575 * abcdefghijklm -> defghijklmabc
576 * where the amount to be shifted is evaluated depending on the column.
577 */
579 int block_size,
580 const uint8_t line_len[2],
int length_div,
582 {
583 int i, j;
584
585 for (i = 0; i < line_len[0]; i++) {
587
588 if (num_blocks == 1 ||
591 i == line_len[1]) {
592 shift = 0;
594 shift = i;
595 } else
596 shift = i * i;
597
598 for (j = 0; j < num_vect && (j + num_vect * i < block_size * num_blocks); j++)
599 tab[i * num_vect + j] = i * num_vect + (j + shift) % num_vect;
600 }
601 }
602
603 /**
604 * Interpret the input data as in the following table:
605 *
606 * @verbatim
607 *
608 * abcdefgh
609 * ijklmnop
610 * qrstuvw
611 * x123456
612 *
613 * @endverbatim
614 *
615 * and transpose it, giving the output
616 * aiqxbjr1cks2dlt3emu4fvn5gow6hp
617 */
619 const uint8_t line_len[2],
int length_div)
620 {
621 int i, j;
622 int cont = 0;
623
624 for (i = 0; i < num_vect; i++)
625 for (j = 0; j < line_len[i >= length_div]; j++)
626 out[cont++] = in[j * num_vect + i];
627 }
628
629 static void linear_perm(int16_t *out, int16_t *in,
int n_blocks,
int size)
630 {
631 int block_size = size / n_blocks;
632 int i;
633
634 for (i = 0; i <
size; i++)
635 out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
636 }
637
640 {
641 int block_size,
size;
643 int16_t *tmp_perm = (int16_t *)tctx->
tmp_buf;
644
648 } else {
651 }
652
654 block_size, tctx->
length[ftype],
656
659
661 size * block_size);
662 }
663
665 {
670
673
676
677 int bsize_no_main_cb[3], bse_bits[3], i;
679
680 for (i = 0; i < 3; i++)
681 // +1 for history usage switch
682 bse_bits[i] = n_ch *
685
686 bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
688
689 for (i = 0; i < 2; i++)
690 bsize_no_main_cb[i] =
691 lsp_bits_per_block + n_ch * TWINVQ_GAIN_BITS +
694
696 bsize_no_main_cb[1] += 2;
697 bsize_no_main_cb[2] += 2;
698 }
699
700 // The remaining bits are all used for the main spectrum coefficients
701 for (i = 0; i < 4; i++) {
702 int bit_size, vect_size;
703 int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
704 if (i == 3) {
707 } else {
708 bit_size = total_fr_bits - bsize_no_main_cb[i];
709 vect_size = n_ch * mtab->
size;
710 }
711
712 tctx->
n_div[i] = (bit_size + 13) / 14;
713
714 rounded_up = (bit_size + tctx->
n_div[i] - 1) /
716 rounded_down = (bit_size) / tctx->
n_div[i];
717 num_rounded_down = rounded_up * tctx->
n_div[i] - bit_size;
718 num_rounded_up = tctx->
n_div[i] - num_rounded_down;
724
725 rounded_up = (vect_size + tctx->
n_div[i] - 1) /
727 rounded_down = (vect_size) / tctx->
n_div[i];
728 num_rounded_down = rounded_up * tctx->
n_div[i] - vect_size;
729 num_rounded_up = tctx->
n_div[i] - num_rounded_down;
730 tctx->
length[i][0] = rounded_up;
731 tctx->
length[i][1] = rounded_down;
733 }
734
737 }
738
740 {
742 int i;
743
744 for (i = 0; i < 3; i++) {
747 }
748
753
754 return 0;
755 }
756
758 {
761
764
770 }
772
775
776 return 0;
777 }