4
4
* @author Denes Balatoni ( dbalatoni programozo hu )
6
* This file is part of FFmpeg.
6
* This file is part of Libav.
8
* FFmpeg is free software; you can redistribute it and/or
8
* Libav is free software; you can redistribute it and/or
9
9
* modify it under the terms of the GNU Lesser General Public
10
10
* License as published by the Free Software Foundation; either
11
11
* version 2.1 of the License, or (at your option) any later version.
13
* FFmpeg is distributed in the hope that it will be useful,
13
* Libav is distributed in the hope that it will be useful,
14
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
16
* Lesser General Public License for more details.
18
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with FFmpeg; if not, write to the Free Software
19
* License along with Libav; if not, write to the Free Software
20
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
82
83
struct vorbis_floor1_s {
83
84
uint_fast8_t partitions;
84
uint_fast8_t maximum_class;
85
uint_fast8_t partition_class[32];
85
uint8_t partition_class[32];
86
86
uint_fast8_t class_dimensions[16];
87
87
uint_fast8_t class_subclasses[16];
88
88
uint_fast8_t class_masterbook[16];
245
250
vc->codebooks = av_mallocz(vc->codebook_count * sizeof(vorbis_codebook));
246
251
tmp_vlc_bits = av_mallocz(V_MAX_VLCS * sizeof(uint8_t));
247
252
tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(uint32_t));
253
codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));
249
255
for (cb = 0; cb < vc->codebook_count; ++cb) {
250
256
vorbis_codebook *codebook_setup = &vc->codebooks[cb];
337
343
if (codebook_setup->lookup_type == 1) {
338
344
uint_fast16_t i, j, k;
339
345
uint_fast16_t codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
340
uint_fast16_t codebook_multiplicands[codebook_lookup_values];
342
347
float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));
343
348
float codebook_delta_value = vorbisfloat2float(get_bits_long(gb, 32));
475
482
AV_DEBUG(" %d. floor type %d \n", i, floor_setup->floor_type);
477
484
if (floor_setup->floor_type == 1) {
478
uint_fast8_t maximum_class = 0;
485
int maximum_class = -1;
479
486
uint_fast8_t rangebits;
480
487
uint_fast32_t rangemax;
481
488
uint_fast16_t floor1_values = 2;
498
505
AV_DEBUG(" maximum class %d \n", maximum_class);
500
floor_setup->data.t1.maximum_class = maximum_class;
502
507
for (j = 0; j <= maximum_class; ++j) {
503
508
floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
504
509
floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
563
568
/* zero would result in a div by zero later *
564
569
* 2^0 - 1 == 0 */
565
570
if (floor_setup->data.t0.amplitude_bits == 0) {
566
av_log(vc->avccontext, AV_LOG_ERROR,
567
"Floor 0 amplitude bits is 0.\n");
571
av_log(vc->avccontext, AV_LOG_ERROR,
572
"Floor 0 amplitude bits is 0.\n");
570
575
floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
571
576
floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
581
586
uint_fast8_t book_idx;
582
587
for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
583
GET_VALIDATED_INDEX(floor_setup->data.t0.book_list[idx], 8, vc->codebook_count)
588
GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
589
floor_setup->data.t0.book_list[idx] = book_idx;
584
590
if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
585
591
max_codebook_dim = vc->codebooks[book_idx].dimensions;
589
595
create_map(vc, i);
591
/* allocate mem for lsp coefficients */
593
/* codebook dim is for padding if codebook dim doesn't *
594
* divide order+1 then we need to read more data */
595
floor_setup->data.t0.lsp =
596
av_malloc((floor_setup->data.t0.order+1 + max_codebook_dim)
598
if (!floor_setup->data.t0.lsp)
597
/* codebook dim is for padding if codebook dim doesn't *
598
* divide order+1 then we need to read more data */
599
floor_setup->data.t0.lsp =
600
av_malloc((floor_setup->data.t0.order+1 + max_codebook_dim)
602
if (!floor_setup->data.t0.lsp)
602
605
#ifdef V_DEBUG /* debug output parsed headers */
603
606
AV_DEBUG("floor0 order: %u\n", floor_setup->data.t0.order);
604
607
AV_DEBUG("floor0 rate: %u\n", floor_setup->data.t0.rate);
605
608
AV_DEBUG("floor0 bark map size: %u\n",
606
floor_setup->data.t0.bark_map_size);
609
floor_setup->data.t0.bark_map_size);
607
610
AV_DEBUG("floor0 amplitude bits: %u\n",
608
floor_setup->data.t0.amplitude_bits);
611
floor_setup->data.t0.amplitude_bits);
609
612
AV_DEBUG("floor0 amplitude offset: %u\n",
610
floor_setup->data.t0.amplitude_offset);
613
floor_setup->data.t0.amplitude_offset);
611
614
AV_DEBUG("floor0 number of books: %u\n",
612
floor_setup->data.t0.num_books);
615
floor_setup->data.t0.num_books);
613
616
AV_DEBUG("floor0 book list pointer: %p\n",
614
floor_setup->data.t0.book_list);
617
floor_setup->data.t0.book_list);
617
for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
618
AV_DEBUG(" Book %d: %u\n",
620
floor_setup->data.t0.book_list[idx]);
620
for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
621
AV_DEBUG(" Book %d: %u\n",
623
floor_setup->data.t0.book_list[idx]);
656
659
res_setup->partition_size = get_bits(gb, 24) + 1;
657
660
/* Validations to prevent a buffer overflow later. */
658
661
if (res_setup->begin>res_setup->end ||
659
res_setup->end>vc->blocksize[1] / (res_setup->type == 2 ? 1 : 2) ||
662
res_setup->end > vc->avccontext->channels * vc->blocksize[1] / 2 ||
660
663
(res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
661
av_log(vc->avccontext, AV_LOG_ERROR, "partition out of bounds: type, begin, end, size, blocksize: %"PRIdFAST16", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32"\n", res_setup->type, res_setup->begin, res_setup->end, res_setup->partition_size, vc->blocksize[1] / 2);
664
av_log(vc->avccontext, AV_LOG_ERROR, "partition out of bounds: type, begin, end, size, blocksize: %"PRIdFAST16", %"PRIdFAST32", %"PRIdFAST32", %u, %"PRIdFAST32"\n", res_setup->type, res_setup->begin, res_setup->end, res_setup->partition_size, vc->blocksize[1] / 2);
665
668
res_setup->classifications = get_bits(gb, 6) + 1;
666
669
GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
671
res_setup->ptns_to_read =
672
(res_setup->end - res_setup->begin) / res_setup->partition_size;
673
res_setup->classifs = av_malloc(res_setup->ptns_to_read *
675
sizeof(*res_setup->classifs));
676
if (!res_setup->classifs)
677
return AVERROR(ENOMEM);
668
679
AV_DEBUG(" begin %d end %d part.size %d classif.s %d classbook %d \n", res_setup->begin, res_setup->end, res_setup->partition_size,
669
680
res_setup->classifications, res_setup->classbook);
919
930
vc->saved = av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(float));
920
931
vc->previous_window = 0;
922
ff_mdct_init(&vc->mdct[0], bl0, 1, vc->exp_bias ? -(1 << 15) : -1.0);
923
ff_mdct_init(&vc->mdct[1], bl1, 1, vc->exp_bias ? -(1 << 15) : -1.0);
933
ff_mdct_init(&vc->mdct[0], bl0, 1, -vc->scale_bias);
934
ff_mdct_init(&vc->mdct[1], bl1, 1, -vc->scale_bias);
925
936
AV_DEBUG(" vorbis version %d \n audio_channels %d \n audio_samplerate %d \n bitrate_max %d \n bitrate_nom %d \n bitrate_min %d \n blk_0 %d blk_1 %d \n ",
926
937
vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
950
961
vc->avccontext = avccontext;
951
962
dsputil_init(&vc->dsp, avccontext);
963
ff_fmt_convert_init(&vc->fmt_conv, avccontext);
953
if (vc->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
958
vc->exp_bias = 15 << 23;
965
vc->scale_bias = 32768.0f;
961
967
if (!headers_len) {
962
968
av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
1001
1007
avccontext->channels = vc->audio_channels;
1002
1008
avccontext->sample_rate = vc->audio_samplerate;
1003
1009
avccontext->frame_size = FFMIN(vc->blocksize[0], vc->blocksize[1]) >> 2;
1004
avccontext->sample_fmt = SAMPLE_FMT_S16;
1010
avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
1031
1037
av_log(vc->avccontext, AV_LOG_ERROR,
1032
1038
"floor0 dec: booknumber too high!\n");
1036
1041
AV_DEBUG("floor0 dec: booknumber: %u\n", book_idx);
1037
1042
codebook = vc->codebooks[vf->book_list[book_idx]];
1103
1108
/* calculate linear floor value */
1105
q = exp((((amplitude*vf->amplitude_offset) /
1106
(((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
1107
- vf->amplitude_offset) * .11512925f);
1109
q = exp((((amplitude*vf->amplitude_offset) /
1110
(((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
1111
- vf->amplitude_offset) * .11512925f);
1110
1113
/* fill vector */
1130
1133
GetBitContext *gb = &vc->gb;
1131
1134
uint_fast16_t range_v[4] = { 256, 128, 86, 64 };
1132
1135
uint_fast16_t range = range_v[vf->multiplier-1];
1133
uint_fast16_t floor1_Y[vf->x_list_dim];
1134
uint_fast16_t floor1_Y_final[vf->x_list_dim];
1135
int floor1_flag[vf->x_list_dim];
1136
uint_fast16_t floor1_Y[258];
1137
uint_fast16_t floor1_Y_final[258];
1138
int floor1_flag[258];
1136
1139
uint_fast8_t class_;
1137
1140
uint_fast8_t cdim;
1138
1141
uint_fast8_t cbits;
1141
1144
int_fast16_t book;
1142
1145
uint_fast16_t offset;
1143
1146
uint_fast16_t i,j;
1144
/*u*/int_fast16_t adx, ady, off, predicted; // WTF ? dy/adx = (unsigned)dy/adx ?
1145
int_fast16_t dy, err;
1147
int_fast16_t adx, ady, dy, off, predicted;
1148
1151
if (!get_bits1(gb)) // silence
1267
1270
GetBitContext *gb = &vc->gb;
1268
1271
uint_fast8_t c_p_c = vc->codebooks[vr->classbook].dimensions;
1269
uint_fast16_t n_to_read = vr->end-vr->begin;
1270
uint_fast16_t ptns_to_read = n_to_read/vr->partition_size;
1271
uint_fast8_t classifs[ptns_to_read*vc->audio_channels];
1272
uint_fast16_t ptns_to_read = vr->ptns_to_read;
1273
uint8_t *classifs = vr->classifs;
1272
1274
uint_fast8_t pass;
1273
1275
uint_fast8_t ch_used;
1274
1276
uint_fast8_t i,j,l;
1444
static void copy_normalize(float *dst, float *src, int len, int exp_bias,
1449
memcpy(dst, src, len * sizeof(float));
1451
for (i = 0; i < len; i++)
1452
dst[i] = src[i] + add_bias;
1456
1446
// Decode the audio packet using the functions above
1458
1448
static int vorbis_parse_audio_packet(vorbis_context *vc)
1460
1450
GetBitContext *gb = &vc->gb;
1462
1452
uint_fast8_t previous_window = vc->previous_window;
1463
1453
uint_fast8_t mode_number;
1464
1454
uint_fast8_t blockflag;
1465
1455
uint_fast16_t blocksize;
1466
1456
int_fast32_t i,j;
1467
uint_fast8_t no_residue[vc->audio_channels];
1468
uint_fast8_t do_not_decode[vc->audio_channels];
1457
uint_fast8_t no_residue[255];
1458
uint_fast8_t do_not_decode[255];
1469
1459
vorbis_mapping *mapping;
1470
1460
float *ch_res_ptr = vc->channel_residues;
1471
1461
float *ch_floor_ptr = vc->channel_floors;
1472
uint_fast8_t res_chan[vc->audio_channels];
1462
uint_fast8_t res_chan[255];
1473
1463
uint_fast8_t res_num = 0;
1474
1464
int_fast16_t retlen = 0;
1475
float fadd_bias = vc->add_bias;
1477
1466
if (get_bits1(gb)) {
1478
1467
av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
1564
1553
// Dotproduct, MDCT
1555
mdct = &vc->mdct[blockflag];
1566
1557
for (j = vc->audio_channels-1;j >= 0; j--) {
1567
1558
ch_floor_ptr = vc->channel_floors + j * blocksize / 2;
1568
1559
ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
1569
vc->dsp.vector_fmul(ch_floor_ptr, ch_res_ptr, blocksize / 2);
1570
ff_imdct_half(&vc->mdct[blockflag], ch_res_ptr, ch_floor_ptr);
1560
vc->dsp.vector_fmul(ch_floor_ptr, ch_floor_ptr, ch_res_ptr, blocksize / 2);
1561
mdct->imdct_half(mdct, ch_res_ptr, ch_floor_ptr);
1573
1564
// Overlap/add, save data for next overlapping FPMATH
1583
1574
const float *win = vc->win[blockflag & previous_window];
1585
1576
if (blockflag == previous_window) {
1586
vc->dsp.vector_fmul_window(ret, saved, buf, win, fadd_bias, blocksize / 4);
1577
vc->dsp.vector_fmul_window(ret, saved, buf, win, blocksize / 4);
1587
1578
} else if (blockflag > previous_window) {
1588
vc->dsp.vector_fmul_window(ret, saved, buf, win, fadd_bias, bs0 / 4);
1589
copy_normalize(ret+bs0/2, buf+bs0/4, (bs1-bs0)/4, vc->exp_bias, fadd_bias);
1579
vc->dsp.vector_fmul_window(ret, saved, buf, win, bs0 / 4);
1580
memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
1591
copy_normalize(ret, saved, (bs1 - bs0) / 4, vc->exp_bias, fadd_bias);
1592
vc->dsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, fadd_bias, bs0 / 4);
1582
memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
1583
vc->dsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
1594
1585
memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
1608
1599
int buf_size = avpkt->size;
1609
1600
vorbis_context *vc = avccontext->priv_data ;
1610
1601
GetBitContext *gb = &(vc->gb);
1611
const float *channel_ptrs[vc->audio_channels];
1602
const float *channel_ptrs[255];
1614
1605
int_fast16_t len;
1644
1635
len * ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
1647
vc->dsp.float_to_int16_interleave(data, channel_ptrs, len, vc->audio_channels);
1638
vc->fmt_conv.float_to_int16_interleave(data, channel_ptrs, len,
1639
vc->audio_channels);
1648
1640
*data_size = len * 2 * vc->audio_channels;
1650
1642
return buf_size ;