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
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;
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,
363 ff_sine_windows[
av_log2(wsize)],
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,
int offset)
380 {
383 int size1, size2, i;
384 float *out1, *out2;
385
389 prev_buf + 2 * i * mtab->
size,
390 i);
391
392 if (!out)
393 return;
394
396 size1 = mtab->
size - size2;
397
398 out1 = &out[0][0] +
offset;
399 memcpy(out1, prev_buf, size1 * sizeof(*out1));
400 memcpy(out1 + size1, tctx->
curr_frame, size2 *
sizeof(*out1));
401
403 out2 = &out[1][0] +
offset;
404 memcpy(out2, &prev_buf[2 * mtab->
size],
405 size1 * sizeof(*out2));
407 size2 * sizeof(*out2));
409 }
410 }
411
414 {
419 int block_size = mtab->
size / sub;
422
423 int i, j;
424
428
430
437 cb_len_p);
438 }
439
441 float *chunk = out + mtab->
size * i;
443
444 for (j = 0; j < sub; j++) {
447 tctx->
tmp_buf, gain[sub * i + j], ftype);
448
450 chunk + block_size * j,
452 }
453
457
460
462
463 for (j = 0; j < mtab->
fmode[ftype].
sub; j++) {
465 chunk += block_size;
466 }
467 }
468 }
469
473 TWINVQ_FT_MEDIUM
474 };
475
477 int *got_frame_ptr,
AVPacket *avpkt)
478 {
481 int buf_size = avpkt->
size;
485 int ret;
486
487 /* get output buffer */
491 return ret;
493 }
494
495 if (buf_size < avctx->block_align) {
497 "Frame too small (%d bytes). Truncated file?\n", buf_size);
499 }
500
502 return ret;
503
508
512
514 }
515
518 *got_frame_ptr = 0;
519 return buf_size;
520 }
521
522 *got_frame_ptr = 1;
523
524 // VQF can deliver packets 1 byte greater than block align
526 return buf_size;
528 }
529
530 /**
531 * Init IMDCT and windowing tables
532 */
534 {
535 int i, j, ret;
540 float norm = channels == 1 ? 2.0 : 1.0;
541
542 for (i = 0; i < 3; i++) {
545 -sqrt(norm / bsize) / (1 << 15))))
546 return ret;
547 }
548
551
554 alloc_fail);
557 alloc_fail);
560 alloc_fail);
561
562 for (i = 0; i < 3; i++) {
564 double freq = 2 *
M_PI / m;
566 (m / 4),
sizeof(*tctx->
cos_tabs[i]), alloc_fail);
567
568 for (j = 0; j <= m / 8; j++)
569 tctx->
cos_tabs[i][j] = cos((2 * j + 1) * freq);
570 for (j = 1; j < m / 8; j++)
572 }
573
577
578 return 0;
579
580 alloc_fail:
582 }
583
584 /**
585 * Interpret the data as if it were a num_blocks x line_len[0] matrix and for
586 * each line do a cyclic permutation, i.e.
587 * abcdefghijklm -> defghijklmabc
588 * where the amount to be shifted is evaluated depending on the column.
589 */
591 int block_size,
592 const uint8_t line_len[2],
int length_div,
594 {
595 int i, j;
596
597 for (i = 0; i < line_len[0]; i++) {
599
600 if (num_blocks == 1 ||
603 i == line_len[1]) {
604 shift = 0;
606 shift = i;
607 } else
608 shift = i * i;
609
610 for (j = 0; j < num_vect && (j + num_vect * i < block_size * num_blocks); j++)
611 tab[i * num_vect + j] = i * num_vect + (j + shift) % num_vect;
612 }
613 }
614
615 /**
616 * Interpret the input data as in the following table:
617 *
618 * @verbatim
619 *
620 * abcdefgh
621 * ijklmnop
622 * qrstuvw
623 * x123456
624 *
625 * @endverbatim
626 *
627 * and transpose it, giving the output
628 * aiqxbjr1cks2dlt3emu4fvn5gow6hp
629 */
631 const uint8_t line_len[2],
int length_div)
632 {
633 int i, j;
634 int cont = 0;
635
636 for (i = 0; i < num_vect; i++)
637 for (j = 0; j < line_len[i >= length_div]; j++)
638 out[cont++] = in[j * num_vect + i];
639 }
640
641 static void linear_perm(int16_t *out, int16_t *in,
int n_blocks,
int size)
642 {
643 int block_size = size / n_blocks;
644 int i;
645
646 for (i = 0; i <
size; i++)
647 out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
648 }
649
652 {
653 int block_size,
size;
655 int16_t *tmp_perm = (int16_t *)tctx->
tmp_buf;
656
660 } else {
663 }
664
666 block_size, tctx->
length[ftype],
668
671
673 size * block_size);
674 }
675
677 {
682
685
688
689 int bsize_no_main_cb[3], bse_bits[3], i;
691
692 for (i = 0; i < 3; i++)
693 // +1 for history usage switch
694 bse_bits[i] = n_ch *
697
698 bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
700
701 for (i = 0; i < 2; i++)
702 bsize_no_main_cb[i] =
703 lsp_bits_per_block + n_ch * TWINVQ_GAIN_BITS +
706
708 bsize_no_main_cb[1] += 2;
709 bsize_no_main_cb[2] += 2;
710 }
711
712 // The remaining bits are all used for the main spectrum coefficients
713 for (i = 0; i < 4; i++) {
714 int bit_size, vect_size;
715 int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
716 if (i == 3) {
719 } else {
720 bit_size = total_fr_bits - bsize_no_main_cb[i];
721 vect_size = n_ch * mtab->
size;
722 }
723
724 tctx->
n_div[i] = (bit_size + 13) / 14;
725
726 rounded_up = (bit_size + tctx->
n_div[i] - 1) /
728 rounded_down = (bit_size) / tctx->
n_div[i];
729 num_rounded_down = rounded_up * tctx->
n_div[i] - bit_size;
730 num_rounded_up = tctx->
n_div[i] - num_rounded_down;
736
737 rounded_up = (vect_size + tctx->
n_div[i] - 1) /
739 rounded_down = (vect_size) / tctx->
n_div[i];
740 num_rounded_down = rounded_up * tctx->
n_div[i] - vect_size;
741 num_rounded_up = tctx->
n_div[i] - num_rounded_down;
742 tctx->
length[i][0] = rounded_up;
743 tctx->
length[i][1] = rounded_down;
745 }
746
749 }
750
752 {
754 int i;
755
756 for (i = 0; i < 3; i++) {
759 }
760
766
767 return 0;
768 }
769
771 {
772 int ret;
774
777
784 }
790 }
791
796 }
800 return ret;
801 }
803
806
807 return 0;
808 }
const char const char void * val
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
uint8_t bark_n_bit
number of bits of the BSE coefs
static int shift(int a, int b)
static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
uint8_t ppc_coeffs[TWINVQ_PPC_SHAPE_LEN_MAX]
int bits_main_spec_change[4]
const TwinVQModeTab * mtab
int64_t bit_rate
the average bitrate
TwinVQFrameData bits[TWINVQ_MAX_FRAMES_PER_PACKET]
int p_coef[TWINVQ_CHANNELS_MAX]
uint8_t cb_len_read
number of spectrum coefficients to read
Medium frame (divided in m<n sub-blocks)
uint8_t bark_n_coef
number of BSE CB coefficients to read
static float cos_tab[256]
uint16_t size
frame size in samples
uint8_t bark_use_hist[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX]
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
uint8_t lpc_idx1[TWINVQ_CHANNELS_MAX]
static const uint8_t wtype_to_wsize[]
enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[]
uint8_t sub_gain_bits[TWINVQ_CHANNELS_MAX *TWINVQ_SUBBLOCKS_MAX]
Short frame (divided in n sub-blocks)
static double cb(void *priv, double x, double y)
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
uint8_t lpc_idx2[TWINVQ_CHANNELS_MAX][TWINVQ_LSP_SPLIT_MAX]
enum AVSampleFormat sample_fmt
audio sample format
const int16_t * ppc_shape_cb
PPC shape CB.
int g_coef[TWINVQ_CHANNELS_MAX]
uint8_t ppc_period_bit
number of the bits for the PPC period value
av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
uint8_t gain_bits[TWINVQ_CHANNELS_MAX]
#define TWINVQ_SUBBLOCKS_MAX
static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float **out, int offset)
static void interpolate(float *out, float v1, float v2, int size)
#define TWINVQ_WINDOW_TYPE_BITS
Parameters and tables that are different for every combination of bitrate/sample rate.
uint8_t lpc_hist_idx[TWINVQ_CHANNELS_MAX]
static void dequant(TwinVQContext *tctx, const uint8_t *cb_bits, float *out, enum TwinVQFrameType ftype, const int16_t *cb0, const int16_t *cb1, int cb_len)
Inverse quantization.
Long frame (single sub-block + PPC)
static float twinvq_mulawinv(float y, float clip, float mu)
static av_cold void init_bitstream_params(TwinVQContext *tctx)
uint8_t main_coeffs[1024]
#define TWINVQ_MAX_FRAMES_PER_PACKET
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats...
void(* butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
#define TWINVQ_PPC_SHAPE_CB_SIZE
int flags
AV_CODEC_FLAG_*.
static void decode_lsp(TwinVQContext *tctx, int lpc_idx1, uint8_t *lpc_idx2, int lpc_hist_idx, float *lsp, float *hist)
static void eval_lpcenv_2parts(TwinVQContext *tctx, enum TwinVQFrameType ftype, const float *buf, float *lpc, int size, int step)
int(* read_bitstream)(AVCodecContext *avctx, struct TwinVQContext *tctx, const uint8_t *buf, int buf_size)
static const uint8_t offset[127][2]
void(* decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
void(* dec_bark_env)(struct TwinVQContext *tctx, const uint8_t *in, int use_hist, int ch, float *out, float gain, enum TwinVQFrameType ftype)
const int16_t * cb0
main codebooks for spectrum data
static void dec_lpc_spectrum_inv(TwinVQContext *tctx, float *lsp, enum TwinVQFrameType ftype, float *lpc)
audio channel layout utility functions
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
uint8_t sub
Number subblocks in each frame.
uint8_t bits_main_spec[2][4][2]
bits for the main codebook
static void twinvq_memset_float(float *buf, float val, int size)
uint8_t n_lsp
number of lsp coefficients
int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static void rearrange_lsp(int order, float *lsp, float min_dist)
Rearrange the LSP coefficients so that they have a minimum distance of min_dist.
#define FF_ARRAY_ELEMS(a)
uint8_t ppc_shape_bit
number of bits of the PPC shape CB coeffs
Libavcodec external API header.
float bark_hist[3][2][40]
BSE coefficients of last frame.
static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks, int block_size, const uint8_t line_len[2], int length_div, enum TwinVQFrameType ftype)
Interpret the data as if it were a num_blocks x line_len[0] matrix and for each line do a cyclic perm...
static void eval_lpcenv(TwinVQContext *tctx, const float *cos_vals, float *lpc)
Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
int sample_rate
samples per second
Periodic Peak Component (part of the long frame)
main external API structure.
enum TwinVQFrameType ftype
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
struct TwinVQFrameMode fmode[3]
frame type-dependent parameters
static void read_and_decode_spectrum(TwinVQContext *tctx, float *out, enum TwinVQFrameType ftype)
#define TWINVQ_PPC_SHAPE_LEN_MAX
float * prev_frame
non-interleaved previous frame
uint8_t pgain_bit
bits for PPC gain
static float eval_lpc_spectrum(const float *lsp, float cos_val, int order)
Evaluate a single LPC amplitude spectrum envelope coefficient from the line spectrum pairs...
static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float *in, float *prev, int ch)
static int chunk_end(AVFormatContext *s)
const float * lspcodebook
#define FF_ALLOC_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
static av_cold int init_mdct_win(TwinVQContext *tctx)
Init IMDCT and windowing tables.
float lsp_hist[2][20]
LSP coefficients of the last frame.
#define TWINVQ_LSP_COEFS_MAX
common internal api header.
uint8_t ppc_shape_len
size of PPC shape CB
#define TWINVQ_SUB_GAIN_BITS
void ff_sort_nearly_sorted_floats(float *vals, int len)
Sort values in ascending order.
int channels
number of audio channels
static void transpose_perm(int16_t *out, int16_t *in, int num_vect, const uint8_t line_len[2], int length_div)
Interpret the input data as in the following table:
static const struct twinvq_data tab
static void eval_lpcenv_or_interp(TwinVQContext *tctx, enum TwinVQFrameType ftype, float *out, const float *in, int size, int step, int part)
Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
static float get_cos(int idx, int part, const float *cos_tab, int size)
float * curr_frame
non-interleaved output
#define FFSWAP(type, a, b)
static void dec_gain(TwinVQContext *tctx, enum TwinVQFrameType ftype, float *out)
#define TWINVQ_CHANNELS_MAX
uint8_t ** extended_data
pointers to the data planes/channels.
uint8_t length[4][2]
main codebook stride
This structure stores compressed data.
static av_cold void construct_perm_table(TwinVQContext *tctx, enum TwinVQFrameType ftype)
#define TWINVQ_SUB_AMP_MAX
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t lsp_split
number of CB entries for the LSP decoding
void AAC_RENAME() ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
uint8_t bark1[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX][TWINVQ_BARK_N_COEF_MAX]
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)