32 #define BITSTREAM_READER_LE
47 #define V_MAX_VLCS (1 << 16)
48 #define V_MAX_PARTITIONS (1 << 20)
62 struct vorbis_context_s;
88 int16_t subclass_books[16][8];
103 int16_t books[64][8];
126 typedef struct vorbis_context_s {
141 uint32_t blocksize[2];
162 (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
164 static const char idx_err_str[] =
"Index value %d out of range (0 - %d) for %s at %s:%i\n";
165 #define VALIDATE_INDEX(idx, limit) \
167 av_log(vc->avctx, AV_LOG_ERROR,\
169 (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
170 return AVERROR_INVALIDDATA;\
172 #define GET_VALIDATED_INDEX(idx, bits, limit) \
174 idx = get_bits(gb, bits);\
175 VALIDATE_INDEX(idx, limit)\
180 double mant = val & 0x1fffff;
181 long exp = (val & 0x7fe00000
L) >> 21;
182 if (val & 0x80000000)
184 return ldexp(mant, exp - 20 - 768);
243 uint32_t *tmp_vlc_codes = NULL;
245 uint16_t *codebook_multiplicands = NULL;
257 !tmp_vlc_bits || !tmp_vlc_codes || !codebook_multiplicands) {
264 unsigned ordered, t, entries, used_entries = 0;
266 av_dlog(NULL,
" %u. Codebook\n", cb);
270 " %u. Codebook setup data corrupt.\n", cb);
278 " %u. Codebook's dimension is invalid (%d).\n",
286 " %u. Codebook has too many entries (%u).\n",
294 av_dlog(NULL,
" codebook_dimensions %d, codebook_entries %u\n",
301 av_dlog(NULL,
" not ordered \n");
307 for (ce = 0; ce < entries; ++ce) {
310 tmp_vlc_bits[ce] =
get_bits(gb, 5) + 1;
313 tmp_vlc_bits[ce] = 0;
316 av_dlog(NULL,
" not sparse \n");
318 used_entries = entries;
319 for (ce = 0; ce < entries; ++ce)
320 tmp_vlc_bits[ce] =
get_bits(gb, 5) + 1;
323 unsigned current_entry = 0;
324 unsigned current_length =
get_bits(gb, 5) + 1;
326 av_dlog(NULL,
" ordered, current length: %u\n", current_length);
328 used_entries = entries;
329 for (; current_entry < used_entries && current_length <= 32; ++current_length) {
332 av_dlog(NULL,
" number bits: %u ",
ilog(entries - current_entry));
336 av_dlog(NULL,
" number: %u\n", number);
338 for (i = current_entry; i < number+current_entry; ++i)
339 if (i < used_entries)
340 tmp_vlc_bits[i] = current_length;
342 current_entry+=number;
344 if (current_entry>used_entries) {
364 unsigned codebook_value_bits =
get_bits(gb, 4) + 1;
365 unsigned codebook_sequence_p =
get_bits1(gb);
367 av_dlog(NULL,
" We expect %d numbers for building the codevectors. \n",
368 codebook_lookup_values);
369 av_dlog(NULL,
" delta %f minmum %f \n",
370 codebook_delta_value, codebook_minimum_value);
372 for (i = 0; i < codebook_lookup_values; ++i) {
373 codebook_multiplicands[i] =
get_bits(gb, codebook_value_bits);
375 av_dlog(NULL,
" multiplicands*delta+minmum : %e \n",
376 (
float)codebook_multiplicands[i] * codebook_delta_value + codebook_minimum_value);
377 av_dlog(NULL,
" multiplicand %u\n", codebook_multiplicands[i]);
385 for (j = 0, i = 0; i < entries; ++i) {
388 if (tmp_vlc_bits[i]) {
390 unsigned lookup_offset = i;
394 for (k = 0; k <
dim; ++k) {
395 unsigned multiplicand_offset = lookup_offset % codebook_lookup_values;
396 codebook_setup->
codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
397 if (codebook_sequence_p)
399 lookup_offset/=codebook_lookup_values;
401 tmp_vlc_bits[j] = tmp_vlc_bits[i];
403 av_dlog(vc->
avctx,
"real lookup offset %u, vector: ", j);
404 for (k = 0; k <
dim; ++k)
412 if (j != used_entries) {
417 entries = used_entries;
431 for (t = 0; t < entries; ++t)
432 if (tmp_vlc_bits[t] >= codebook_setup->
maxdepth)
433 codebook_setup->
maxdepth = tmp_vlc_bits[t];
443 entries, tmp_vlc_bits,
sizeof(*tmp_vlc_bits),
444 sizeof(*tmp_vlc_bits), tmp_vlc_codes,
445 sizeof(*tmp_vlc_codes),
sizeof(*tmp_vlc_codes),
454 av_free(codebook_multiplicands);
461 av_free(codebook_multiplicands);
470 unsigned i, vorbis_time_count =
get_bits(gb, 6) + 1;
472 for (i = 0; i < vorbis_time_count; ++i) {
473 unsigned vorbis_tdtransform =
get_bits(gb, 16);
475 av_dlog(NULL,
" Vorbis time domain transform %u: %u\n",
476 vorbis_time_count, vorbis_tdtransform);
478 if (vorbis_tdtransform) {
512 int maximum_class = -1;
513 unsigned rangebits, rangemax, floor1_values = 2;
519 av_dlog(NULL,
" %d.floor: %d partitions \n",
527 av_dlog(NULL,
" %d. floor %d partition class %d \n",
532 av_dlog(NULL,
" maximum class %d \n", maximum_class);
534 for (j = 0; j <= maximum_class; ++j) {
538 av_dlog(NULL,
" %d floor %d class dim: %d subclasses %d \n", i, j,
570 rangemax = (1 << rangebits);
573 "Floor value is too large for blocksize: %u (%"PRIu32
")\n",
584 av_dlog(NULL,
" %u. floor1 Y coord. %d\n", floor1_values,
596 unsigned max_codebook_dim = 0;
613 "Floor 0 bark map size is 0.\n");
651 av_dlog(NULL,
"floor0 bark map size: %u\n",
653 av_dlog(NULL,
"floor0 amplitude bits: %u\n",
655 av_dlog(NULL,
"floor0 amplitude offset: %u\n",
657 av_dlog(NULL,
"floor0 number of books: %u\n",
659 av_dlog(NULL,
"floor0 book list pointer: %p\n",
664 av_dlog(NULL,
" Book %d: %u\n", idx + 1,
693 unsigned high_bits, low_bits;
697 av_dlog(NULL,
" %u. residue type %d\n", i, res_setup->
type);
703 if (res_setup->
begin>res_setup->
end ||
706 "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16
", %"PRIu32
", %"PRIu32
", %u, %"PRIu32
"\n",
723 av_dlog(NULL,
" begin %d end %d part.size %d classif.s %d classbook %d \n",
732 cascade[j] = (high_bits << 3) + low_bits;
734 av_dlog(NULL,
" %u class cascade depth: %d\n", j,
ilog(cascade[j]));
739 for (k = 0; k < 8; ++k) {
740 if (cascade[j]&(1 << k)) {
743 av_dlog(NULL,
" %u class cascade depth %u book: %d\n",
744 j, k, res_setup->
books[j][k]);
749 res_setup->
books[j][k] = -1;
775 av_log(vc->
avctx,
AV_LOG_ERROR,
"Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
789 sizeof(*mapping_setup->
angle));
801 av_dlog(NULL,
" %u mapping coupling steps: %d\n",
809 if (mapping_setup->
submaps>1) {
811 sizeof(*mapping_setup->
mux));
812 if (!mapping_setup->
mux)
819 for (j = 0; j < mapping_setup->
submaps; ++j) {
824 av_dlog(NULL,
" %u mapping %u submap : floor %d, residue %d\n", i, j,
842 for (blockflag = 0; blockflag < 2; ++blockflag) {
844 floors[floor_number].
data.
t0.
map[blockflag] =
846 if (!floors[floor_number].
data.t0.map[blockflag])
849 map = floors[floor_number].
data.
t0.
map[blockflag];
850 vf = &floors[floor_number].
data.
t0;
852 for (idx = 0; idx <
n; ++idx) {
853 map[idx] = floor(
BARK((vf->rate * idx) / (2.0f * n)) *
854 (vf->bark_map_size /
BARK(vf->rate / 2.0f)));
855 if (vf->bark_map_size-1 < map[idx])
856 map[idx] = vf->bark_map_size - 1;
859 vf->map_size[blockflag] =
n;
862 for (idx = 0; idx <=
n; ++idx) {
863 av_dlog(NULL,
"floor0 map: map at pos %d is %d\n", idx, map[idx]);
889 av_dlog(NULL,
" %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
972 if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
996 av_dlog(NULL,
" 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 ",
1040 if (hdr_type != 1) {
1052 if (hdr_type != 5) {
1082 float *lsp = vf->lsp;
1083 unsigned amplitude, book_idx;
1086 if (!vf->amplitude_bits)
1089 amplitude =
get_bits(&vc->
gb, vf->amplitude_bits);
1090 if (amplitude > 0) {
1092 unsigned idx, lsp_len = 0;
1096 if (book_idx >= vf->num_books) {
1100 av_dlog(NULL,
"floor0 dec: booknumber: %u\n", book_idx);
1101 codebook = vc->
codebooks[vf->book_list[book_idx]];
1106 while (lsp_len<vf->order) {
1115 av_dlog(NULL,
"floor0 dec: vector offset: %d\n", vec_off);
1117 for (idx = 0; idx < codebook.
dimensions; ++idx)
1118 lsp[lsp_len+idx] = codebook.
codevectors[vec_off+idx] + last;
1119 last = lsp[lsp_len+idx-1];
1126 for (idx = 0; idx < lsp_len; ++idx)
1127 av_dlog(NULL,
"floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
1133 int order = vf->order;
1134 float wstep =
M_PI / vf->bark_map_size;
1136 for (i = 0; i < order; i++)
1137 lsp[i] = 2.0f * cos(lsp[i]);
1139 av_dlog(NULL,
"floor0 synth: map_size = %"PRIu32
"; m = %d; wstep = %f\n",
1140 vf->map_size[blockflag], order, wstep);
1143 while (i < vf->map_size[blockflag]) {
1144 int j, iter_cond = vf->map[blockflag][i];
1147 float two_cos_w = 2.0f * cos(wstep * iter_cond);
1150 for (j = 0; j + 1 < order; j += 2) {
1151 q *= lsp[j] - two_cos_w;
1152 p *= lsp[j + 1] - two_cos_w;
1155 p *= p * (2.0f - two_cos_w);
1156 q *= q * (2.0f + two_cos_w);
1158 q *= two_cos_w-lsp[j];
1161 p *= p * (4.f - two_cos_w * two_cos_w);
1166 q = exp((((amplitude*vf->amplitude_offset) /
1167 (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
1168 - vf->amplitude_offset) * .11512925f);
1173 }
while (vf->map[blockflag][i] == iter_cond);
1181 av_dlog(NULL,
" Floor0 decoded\n");
1191 uint16_t range_v[4] = { 256, 128, 86, 64 };
1192 unsigned range = range_v[vf->multiplier - 1];
1193 uint16_t floor1_Y[258];
1194 uint16_t floor1_Y_final[258];
1195 int floor1_flag[258];
1196 unsigned partition_class, cdim, cbits, csub, cval,
offset, i, j;
1197 int book, adx, ady, dy, off, predicted, err;
1208 av_dlog(NULL,
"floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
1211 for (i = 0; i < vf->partitions; ++i) {
1212 partition_class = vf->partition_class[i];
1213 cdim = vf->class_dimensions[partition_class];
1214 cbits = vf->class_subclasses[partition_class];
1215 csub = (1 << cbits) - 1;
1218 av_dlog(NULL,
"Cbits %u\n", cbits);
1224 for (j = 0; j < cdim; ++j) {
1225 book = vf->subclass_books[partition_class][cval & csub];
1227 av_dlog(NULL,
"book %d Cbits %u cval %u bits:%d\n",
1230 cval = cval >> cbits;
1236 floor1_Y[offset+j] =
v;
1238 floor1_Y[offset+j] = 0;
1241 av_dlog(NULL,
" floor(%d) = %d \n",
1242 vf->list[offset+j].x, floor1_Y[offset+j]);
1251 floor1_Y_final[0] = floor1_Y[0];
1252 floor1_Y_final[1] = floor1_Y[1];
1254 for (i = 2; i < vf->x_list_dim; ++i) {
1255 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1257 low_neigh_offs = vf->list[i].low;
1258 high_neigh_offs = vf->list[i].high;
1259 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs];
1260 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
1262 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
1265 predicted = floor1_Y_final[low_neigh_offs] - off;
1267 predicted = floor1_Y_final[low_neigh_offs] + off;
1271 highroom = range-predicted;
1272 lowroom = predicted;
1273 if (highroom < lowroom) {
1274 room = highroom * 2;
1279 floor1_flag[low_neigh_offs] = 1;
1280 floor1_flag[high_neigh_offs] = 1;
1283 if (highroom > lowroom) {
1284 floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
1286 floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
1290 floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
1292 floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
1297 floor1_Y_final[i] = av_clip_uint16(predicted);
1300 av_dlog(NULL,
" Decoded floor(%d) = %u / val %u\n",
1301 vf->list[i].x, floor1_Y_final[i], val);
1308 av_dlog(NULL,
" Floor decoded\n");
1317 int partition_count)
1322 unsigned temp, temp2;
1323 for (p = 0, j = 0; j < ch_used; ++j) {
1324 if (!do_not_decode[j]) {
1328 av_dlog(NULL,
"Classword: %u\n", temp);
1335 if (temp <= 65536) {
1336 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1337 temp2 = (((uint64_t)temp) * inverse_class) >> 32;
1339 if (i < vr->ptns_to_read)
1344 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1347 if (i < vr->ptns_to_read)
1371 unsigned pass, ch_used, i, j, k, l;
1372 unsigned max_output = (ch - 1) * vlen;
1374 int libvorbis_bug = 0;
1377 for (j = 1; j < ch; ++j)
1378 do_not_decode[0] &= do_not_decode[j];
1379 if (do_not_decode[0])
1382 max_output += vr->
end / ch;
1385 max_output += vr->
end;
1388 if (max_output > ch_left * vlen) {
1389 if (max_output <= ch_left * vlen + vr->partition_size*ch_used/ch) {
1398 av_dlog(NULL,
" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
1401 int voffset, partition_count, j_times_ptns_to_read;
1403 voffset = vr->
begin;
1404 for (partition_count = 0; partition_count < ptns_to_read;) {
1407 if ((ret =
setup_classifs(vc, vr, do_not_decode, ch_used, partition_count)) < 0)
1410 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
1411 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1414 if (!do_not_decode[j]) {
1415 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1426 voffs = voffset+j*vlen;
1427 for (k = 0; k < step; ++k) {
1429 for (l = 0; l <
dim; ++l)
1430 vec[voffs + k + l * step] += codebook.
codevectors[coffs + l];
1432 }
else if (vr_type == 1) {
1433 voffs = voffset + j * vlen;
1434 for (k = 0; k < step; ++k) {
1436 for (l = 0; l <
dim; ++l, ++voffs) {
1439 av_dlog(NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
1440 pass, voffs, vec[voffs], codebook.
codevectors[coffs+l], coffs);
1443 }
else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) {
1444 voffs = voffset >> 1;
1447 for (k = 0; k < step; ++k) {
1450 vec[voffs + k + vlen] += codebook.
codevectors[coffs + 1];
1452 }
else if (dim == 4) {
1453 for (k = 0; k < step; ++k, voffs += 2) {
1456 vec[voffs + 1 ] += codebook.
codevectors[coffs + 2];
1457 vec[voffs + vlen ] += codebook.
codevectors[coffs + 1];
1458 vec[voffs + vlen + 1] += codebook.
codevectors[coffs + 3];
1461 for (k = 0; k < step; ++k) {
1463 for (l = 0; l <
dim; l += 2, voffs++) {
1465 vec[voffs + vlen] += codebook.
codevectors[coffs + l + 1];
1467 av_dlog(NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1468 pass, voffset / ch + (voffs % ch) * vlen,
1469 vec[voffset / ch + (voffs % ch) * vlen],
1474 }
else if (vr_type == 2) {
1475 unsigned voffs_div =
FASTDIV(voffset << 1, ch <<1);
1476 unsigned voffs_mod = voffset - voffs_div * ch;
1478 for (k = 0; k < step; ++k) {
1480 for (l = 0; l <
dim; ++l) {
1481 vec[voffs_div + voffs_mod * vlen] +=
1484 av_dlog(NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1485 pass, voffs_div + voffs_mod * vlen,
1486 vec[voffs_div + voffs_mod * vlen],
1489 if (++voffs_mod == ch) {
1498 j_times_ptns_to_read += ptns_to_read;
1504 if (libvorbis_bug && !pass) {
1505 for (j = 0; j < ch_used; ++j) {
1506 if (!do_not_decode[j]) {
1519 float *vec,
unsigned vlen,
1524 else if (vr->
type == 1)
1526 else if (vr->
type == 0)
1537 for (i = 0; i < blocksize; i++) {
1540 ang[i] = mag[i] - ang[i];
1542 float temp = ang[i];
1550 float temp = ang[i];
1565 unsigned mode_number, blockflag, blocksize;
1572 unsigned res_num = 0;
1590 av_dlog(NULL,
" Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
1595 vlen = blocksize / 2;
1598 if (previous_window < 0)
1599 previous_window = code>>1;
1600 }
else if (previous_window < 0)
1601 previous_window = 0;
1603 memset(ch_res_ptr, 0,
sizeof(
float) * vc->
audio_channels * vlen);
1605 memset(floor_ptr[i], 0, vlen *
sizeof(floor_ptr[0][0]));
1618 ret = floor->
decode(vc, &floor->
data, floor_ptr[i]);
1624 no_residue[i] =
ret;
1630 if (!(no_residue[mapping->
magnitude[i]] & no_residue[mapping->
angle[i]])) {
1632 no_residue[mapping->
angle[i]] = 0;
1638 for (i = 0; i < mapping->
submaps; ++i) {
1644 if ((mapping->
submaps == 1) || (i == mapping->
mux[j])) {
1645 res_chan[j] = res_num;
1646 if (no_residue[j]) {
1647 do_not_decode[ch] = 1;
1649 do_not_decode[ch] = 0;
1666 ch_res_ptr += ch * vlen;
1685 mdct = &vc->
mdct[blockflag];
1689 vc->
fdsp.
vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1690 mdct->
imdct_half(mdct, ch_res_ptr, floor_ptr[j]);
1695 retlen = (blocksize + vc->
blocksize[previous_window]) / 4;
1700 float *saved = vc->
saved + j * bs1 / 4;
1701 float *
ret = floor_ptr[j];
1702 float *
buf = residue;
1703 const float *win = vc->
win[blockflag & previous_window];
1705 if (blockflag == previous_window) {
1707 }
else if (blockflag > previous_window) {
1709 memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) *
sizeof(
float));
1711 memcpy(ret, saved, ((bs1 - bs0) / 4) *
sizeof(
float));
1714 memcpy(saved, buf + blocksize / 4, blocksize / 4 *
sizeof(
float));
1724 int *got_frame_ptr,
AVPacket *avpkt)
1727 int buf_size = avpkt->
size;
1731 float *channel_ptrs[255];
1734 av_dlog(NULL,
"packet length %d \n", buf_size);
1736 if (*buf == 1 && buf_size > 7) {
1755 if (*buf == 3 && buf_size > 7) {
1802 av_dlog(NULL,
"parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1828 sizeof(*vc->
saved));