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 {
76
77 for (
i = 0;
i < size_s / 2;
i++) {
81 }
82 }
83
85 {
88
92 }
93 }
94
96 {
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,
119 {
123
124 // Fill the 's'
130
131 // Fill the 'iiiibiiii'
136 } else {
145 }
146 }
147
150 }
151
153 const float *buf, float *lpc,
155 {
159
162
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 {
178
180 int tmp0, tmp1;
181 int sign0 = 1;
182 int sign1 = 1;
183 const int16_t *tab0, *
tab1;
186
188 tmp0 = *cb_bits++;
190 if (tmp0 & 0x40)
191 sign0 = -1;
192 tmp0 &= 0x3F;
193 }
194
196 tmp1 = *cb_bits++;
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++)
209
211 }
212 }
213
216 {
224
227 out[
i] = (1.0 / (1 << 13)) *
230 } else {
232 float val = (1.0 / (1 << 23)) *
235
236 for (j = 0; j <
sub; j++)
239 sub_step *
bits->sub_gain_bits[
i *
sub + j],
241 }
242 }
243 }
244
245 /**
246 * Rearrange the LSP coefficients so that they have a minimum distance of
247 * min_dist. This function does it exactly as described in section of 3.2.4
248 * of the G.729 specification (but interestingly is different from what the
249 * reference decoder actually does).
250 */
252 {
254 float min_dist2 = min_dist * 0.5;
255 for (
i = 1;
i < order;
i++)
256 if (lsp[
i] - lsp[
i - 1] < min_dist) {
257 float avg = (lsp[
i] + lsp[
i - 1]) * 0.5;
258
259 lsp[
i - 1] =
avg - min_dist2;
260 lsp[
i] =
avg + min_dist2;
261 }
262 }
263
265 int lpc_hist_idx, float *lsp, float *hist)
266 {
269
272 const float *cb3 = cb2 + (1 << mtab->
lsp_bit2) * mtab->
n_lsp;
273
274 const int8_t funny_rounding[4] = {
275 -2,
278 0
279 };
280
281 j = 0;
286 lsp[j] =
cb[lpc_idx1 * mtab->
n_lsp + j] +
287 cb2[lpc_idx2[
i] * mtab->
n_lsp + j];
288 }
289
291
293 float tmp1 = 1.0 - cb3[lpc_hist_idx * mtab->
n_lsp +
i];
294 float tmp2 = hist[
i] * cb3[lpc_hist_idx * mtab->
n_lsp +
i];
296 lsp[
i] = lsp[
i] * tmp1 + tmp2;
297 }
298
302 }
303
306 {
309
311 lsp[
i] = 2 * cos(lsp[
i]);
312
316 break;
319 break;
322 break;
323 }
324 }
325
327
329 int wtype, float *in, float *prev, int ch)
330 {
337 int j, first_wsize, wsize; // Window size
340 float *prev_buf;
341 int types_sizes[] = {
345 };
346
348 first_wsize = wsize;
349 prev_buf = prev + (
size - bsize) / 2;
350
353
354 if (!j && wtype == 4)
355 sub_wtype = 4;
357 sub_wtype = 7;
358
360
361 tx_fn(tx, buf1 + bsize * j, in + bsize * j, sizeof(float));
362
364 buf1 + bsize * j,
366 wsize / 2);
367 out2 += wsize;
368
369 memcpy(out2, buf1 + bsize * j + wsize / 2,
370 (bsize - wsize / 2) * sizeof(float));
371
373
374 prev_buf = buf1 + bsize * j + bsize / 2;
375 }
376
378 }
379
382 {
387 float *out1, *out2;
388
392 prev_buf + 2 *
i * mtab->
size,
394
396 return;
397
399 size1 = mtab->
size - size2;
400
402 memcpy(out1, prev_buf, size1 * sizeof(*out1));
403 memcpy(out1 + size1, tctx->
curr_frame, size2 *
sizeof(*out1));
404
407 memcpy(out2, &prev_buf[2 * mtab->
size],
408 size1 * sizeof(*out2));
410 size2 * sizeof(*out2));
412 }
413 }
414
417 {
422 int block_size = mtab->
size /
sub;
425
427
431
433
440 cb_len_p);
441 }
442
444 float *chunk =
out + mtab->
size *
i;
446
447 for (j = 0; j <
sub; j++) {
449 bits->bark_use_hist[
i][j],
i,
451
453 chunk + block_size * j,
455 }
456
460
463
465
468 chunk += block_size;
469 }
470 }
471 }
472
477 };
478
480 int *got_frame_ptr,
AVPacket *avpkt)
481 {
482 const uint8_t *buf = avpkt->
data;
483 int buf_size = avpkt->
size;
488
489 /* get output buffer */
494 out = (
float **)
frame->extended_data;
495 }
496
497 if (buf_size < avctx->block_align) {
499 "Frame too small (%d bytes). Truncated file?\n", buf_size);
501 }
502
505
510
514
516 }
517
520 *got_frame_ptr = 0;
521 return buf_size;
522 }
523
524 *got_frame_ptr = 1;
525
526 // VQF can deliver packets 1 byte greater than block align
528 return buf_size;
530 }
531
532 /**
533 * Init IMDCT and windowing tables
534 */
536 {
542 float norm =
channels == 1 ? 2.0 : 1.0;
544
545 for (
i = 0;
i < 3;
i++) {
547 const float scale = -sqrt(norm / bsize) / (1 << 15);
549 1, bsize, &
scale, 0)))
551 }
552
558
559 for (
i = 0;
i < 3;
i++) {
561 double freq = 2 *
M_PI / m;
564 for (j = 0; j <= m / 8; j++)
565 tctx->
cos_tabs[
i][j] = cos((2 * j + 1) * freq);
566 for (j = 1; j < m / 8; j++)
568 }
569
573
574 return 0;
575 }
576
577 /**
578 * Interpret the data as if it were a num_blocks x line_len[0] matrix and for
579 * each line do a cyclic permutation, i.e.
580 * abcdefghijklm -> defghijklmabc
581 * where the amount to be shifted is evaluated depending on the column.
582 */
584 int block_size,
585 const uint8_t line_len[2], int length_div,
587 {
589
590 for (
i = 0;
i < line_len[0];
i++) {
592
593 if (num_blocks == 1 ||
600 } else
602
603 for (j = 0; j < num_vect && (j + num_vect *
i < block_size * num_blocks); j++)
604 tab[
i * num_vect + j] =
i * num_vect + (j +
shift) % num_vect;
605 }
606 }
607
608 /**
609 * Interpret the input data as in the following table:
610 *
611 * @verbatim
612 *
613 * abcdefgh
614 * ijklmnop
615 * qrstuvw
616 * x123456
617 *
618 * @endverbatim
619 *
620 * and transpose it, giving the output
621 * aiqxbjr1cks2dlt3emu4fvn5gow6hp
622 */
624 const uint8_t line_len[2], int length_div)
625 {
627 int cont = 0;
628
629 for (
i = 0;
i < num_vect;
i++)
630 for (j = 0; j < line_len[i >= length_div]; j++)
631 out[cont++] = in[j * num_vect +
i];
632 }
633
635 {
636 int block_size =
size / n_blocks;
638
640 out[
i] = block_size * (in[
i] % n_blocks) + in[
i] / n_blocks;
641 }
642
645 {
646 int block_size,
size;
648 int16_t *tmp_perm = (int16_t *)tctx->
tmp_buf;
649
653 } else {
656 }
657
661
664
667 }
668
670 {
675
678
681
682 int bsize_no_main_cb[3], bse_bits[3],
i;
684
685 for (
i = 0;
i < 3;
i++)
686 // +1 for history usage switch
690
691 bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
693
694 for (
i = 0;
i < 2;
i++)
695 bsize_no_main_cb[
i] =
699
701 bsize_no_main_cb[1] += 2;
702 bsize_no_main_cb[2] += 2;
703 }
704
705 // The remaining bits are all used for the main spectrum coefficients
706 for (
i = 0;
i < 4;
i++) {
707 int bit_size, vect_size;
708 int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
712 } else {
713 bit_size = total_fr_bits - bsize_no_main_cb[
i];
714 vect_size = n_ch * mtab->
size;
715 }
716
717 tctx->
n_div[
i] = (bit_size + 13) / 14;
718
719 rounded_up = (bit_size + tctx->
n_div[
i] - 1) /
721 rounded_down = (bit_size) / tctx->
n_div[
i];
722 num_rounded_down = rounded_up * tctx->
n_div[
i] - bit_size;
723 num_rounded_up = tctx->
n_div[
i] - num_rounded_down;
729
730 rounded_up = (vect_size + tctx->
n_div[
i] - 1) /
732 rounded_down = (vect_size) / tctx->
n_div[
i];
733 num_rounded_down = rounded_up * tctx->
n_div[
i] - vect_size;
734 num_rounded_up = tctx->
n_div[
i] - num_rounded_down;
735 tctx->
length[
i][0] = rounded_up;
736 tctx->
length[
i][1] = rounded_down;
738 }
739
742 }
743
745 {
748
749 for (
i = 0;
i < 3;
i++) {
752 }
753
759
760 return 0;
761 }
762
764 {
767 int64_t frames_per_packet;
768
771
774 }
776 if (frames_per_packet <= 0) {
780 }
783 frames_per_packet);
785 }
787
794 }
796
799
800 return 0;
801 }