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);
244 uint32_t *tmp_vlc_codes = NULL;
246 uint16_t *codebook_multiplicands = NULL;
258 !tmp_vlc_bits || !tmp_vlc_codes || !codebook_multiplicands) {
265 unsigned ordered, t, entries, used_entries = 0;
267 av_dlog(NULL,
" %u. Codebook\n", cb);
271 " %u. Codebook setup data corrupt.\n", cb);
279 " %u. Codebook's dimension is invalid (%d).\n",
287 " %u. Codebook has too many entries (%u).\n",
295 av_dlog(NULL,
" codebook_dimensions %d, codebook_entries %u\n",
302 av_dlog(NULL,
" not ordered \n");
308 for (ce = 0; ce < entries; ++ce) {
311 tmp_vlc_bits[ce] =
get_bits(gb, 5) + 1;
314 tmp_vlc_bits[ce] = 0;
317 av_dlog(NULL,
" not sparse \n");
319 used_entries = entries;
320 for (ce = 0; ce < entries; ++ce)
321 tmp_vlc_bits[ce] =
get_bits(gb, 5) + 1;
324 unsigned current_entry = 0;
325 unsigned current_length =
get_bits(gb, 5) + 1;
327 av_dlog(NULL,
" ordered, current length: %u\n", current_length);
329 used_entries = entries;
330 for (; current_entry < used_entries && current_length <= 32; ++current_length) {
333 av_dlog(NULL,
" number bits: %u ",
ilog(entries - current_entry));
337 av_dlog(NULL,
" number: %u\n", number);
339 for (i = current_entry; i < number+current_entry; ++i)
340 if (i < used_entries)
341 tmp_vlc_bits[i] = current_length;
343 current_entry+=number;
345 if (current_entry>used_entries) {
365 unsigned codebook_value_bits =
get_bits(gb, 4) + 1;
366 unsigned codebook_sequence_p =
get_bits1(gb);
368 av_dlog(NULL,
" We expect %d numbers for building the codevectors. \n",
369 codebook_lookup_values);
370 av_dlog(NULL,
" delta %f minmum %f \n",
371 codebook_delta_value, codebook_minimum_value);
373 for (i = 0; i < codebook_lookup_values; ++i) {
374 codebook_multiplicands[i] =
get_bits(gb, codebook_value_bits);
376 av_dlog(NULL,
" multiplicands*delta+minmum : %e \n",
377 (
float)codebook_multiplicands[i] * codebook_delta_value + codebook_minimum_value);
378 av_dlog(NULL,
" multiplicand %u\n", codebook_multiplicands[i]);
386 for (j = 0, i = 0; i < entries; ++i) {
389 if (tmp_vlc_bits[i]) {
391 unsigned lookup_offset = i;
395 for (k = 0; k <
dim; ++k) {
396 unsigned multiplicand_offset = lookup_offset % codebook_lookup_values;
397 codebook_setup->
codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
398 if (codebook_sequence_p)
400 lookup_offset/=codebook_lookup_values;
402 tmp_vlc_bits[j] = tmp_vlc_bits[i];
404 av_dlog(vc->
avctx,
"real lookup offset %u, vector: ", j);
405 for (k = 0; k <
dim; ++k)
413 if (j != used_entries) {
418 entries = used_entries;
432 for (t = 0; t < entries; ++t)
433 if (tmp_vlc_bits[t] >= codebook_setup->
maxdepth)
434 codebook_setup->
maxdepth = tmp_vlc_bits[t];
444 entries, tmp_vlc_bits,
sizeof(*tmp_vlc_bits),
445 sizeof(*tmp_vlc_bits), tmp_vlc_codes,
446 sizeof(*tmp_vlc_codes),
sizeof(*tmp_vlc_codes),
455 av_free(codebook_multiplicands);
462 av_free(codebook_multiplicands);
471 unsigned i, vorbis_time_count =
get_bits(gb, 6) + 1;
473 for (i = 0; i < vorbis_time_count; ++i) {
474 unsigned vorbis_tdtransform =
get_bits(gb, 16);
476 av_dlog(NULL,
" Vorbis time domain transform %u: %u\n",
477 vorbis_time_count, vorbis_tdtransform);
479 if (vorbis_tdtransform) {
513 int maximum_class = -1;
514 unsigned rangebits, rangemax, floor1_values = 2;
520 av_dlog(NULL,
" %d.floor: %d partitions \n",
528 av_dlog(NULL,
" %d. floor %d partition class %d \n",
533 av_dlog(NULL,
" maximum class %d \n", maximum_class);
535 for (j = 0; j <= maximum_class; ++j) {
539 av_dlog(NULL,
" %d floor %d class dim: %d subclasses %d \n", i, j,
571 rangemax = (1 << rangebits);
574 "Floor value is too large for blocksize: %u (%"PRIu32
")\n",
585 av_dlog(NULL,
" %u. floor1 Y coord. %d\n", floor1_values,
597 unsigned max_codebook_dim = 0;
614 "Floor 0 bark map size is 0.\n");
652 av_dlog(NULL,
"floor0 bark map size: %u\n",
654 av_dlog(NULL,
"floor0 amplitude bits: %u\n",
656 av_dlog(NULL,
"floor0 amplitude offset: %u\n",
658 av_dlog(NULL,
"floor0 number of books: %u\n",
660 av_dlog(NULL,
"floor0 book list pointer: %p\n",
665 av_dlog(NULL,
" Book %d: %u\n", idx + 1,
694 unsigned high_bits, low_bits;
698 av_dlog(NULL,
" %u. residue type %d\n", i, res_setup->
type);
704 if (res_setup->
begin>res_setup->
end ||
707 "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16
", %"PRIu32
", %"PRIu32
", %u, %"PRIu32
"\n",
724 av_dlog(NULL,
" begin %d end %d part.size %d classif.s %d classbook %d \n",
733 cascade[j] = (high_bits << 3) + low_bits;
735 av_dlog(NULL,
" %u class cascade depth: %d\n", j,
ilog(cascade[j]));
740 for (k = 0; k < 8; ++k) {
741 if (cascade[j]&(1 << k)) {
744 av_dlog(NULL,
" %u class cascade depth %u book: %d\n",
745 j, k, res_setup->
books[j][k]);
750 res_setup->
books[j][k] = -1;
776 av_log(vc->
avctx,
AV_LOG_ERROR,
"Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
790 sizeof(*mapping_setup->
angle));
802 av_dlog(NULL,
" %u mapping coupling steps: %d\n",
810 if (mapping_setup->
submaps>1) {
812 sizeof(*mapping_setup->
mux));
813 if (!mapping_setup->
mux)
820 for (j = 0; j < mapping_setup->
submaps; ++j) {
825 av_dlog(NULL,
" %u mapping %u submap : floor %d, residue %d\n", i, j,
843 for (blockflag = 0; blockflag < 2; ++blockflag) {
845 floors[floor_number].
data.
t0.
map[blockflag] =
847 if (!floors[floor_number].
data.t0.map[blockflag])
850 map = floors[floor_number].
data.
t0.
map[blockflag];
851 vf = &floors[floor_number].
data.
t0;
853 for (idx = 0; idx <
n; ++idx) {
854 map[idx] = floor(
BARK((vf->rate * idx) / (2.0f * n)) *
855 (vf->bark_map_size /
BARK(vf->rate / 2.0f)));
856 if (vf->bark_map_size-1 < map[idx])
857 map[idx] = vf->bark_map_size - 1;
860 vf->map_size[blockflag] =
n;
863 for (idx = 0; idx <=
n; ++idx) {
864 av_dlog(NULL,
"floor0 map: map at pos %d is %d\n", idx, map[idx]);
890 av_dlog(NULL,
" %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
973 if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
1000 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 ",
1043 if (hdr_type != 1) {
1055 if (hdr_type != 5) {
1085 float *lsp = vf->lsp;
1086 unsigned amplitude, book_idx;
1089 if (!vf->amplitude_bits)
1092 amplitude =
get_bits(&vc->
gb, vf->amplitude_bits);
1093 if (amplitude > 0) {
1095 unsigned idx, lsp_len = 0;
1099 if (book_idx >= vf->num_books) {
1103 av_dlog(NULL,
"floor0 dec: booknumber: %u\n", book_idx);
1104 codebook = vc->
codebooks[vf->book_list[book_idx]];
1109 while (lsp_len<vf->order) {
1118 av_dlog(NULL,
"floor0 dec: vector offset: %d\n", vec_off);
1120 for (idx = 0; idx < codebook.
dimensions; ++idx)
1121 lsp[lsp_len+idx] = codebook.
codevectors[vec_off+idx] + last;
1122 last = lsp[lsp_len+idx-1];
1129 for (idx = 0; idx < lsp_len; ++idx)
1130 av_dlog(NULL,
"floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
1136 int order = vf->order;
1137 float wstep =
M_PI / vf->bark_map_size;
1139 for (i = 0; i < order; i++)
1140 lsp[i] = 2.0f * cos(lsp[i]);
1142 av_dlog(NULL,
"floor0 synth: map_size = %"PRIu32
"; m = %d; wstep = %f\n",
1143 vf->map_size[blockflag], order, wstep);
1146 while (i < vf->map_size[blockflag]) {
1147 int j, iter_cond = vf->map[blockflag][i];
1150 float two_cos_w = 2.0f * cos(wstep * iter_cond);
1153 for (j = 0; j + 1 < order; j += 2) {
1154 q *= lsp[j] - two_cos_w;
1155 p *= lsp[j + 1] - two_cos_w;
1158 p *= p * (2.0f - two_cos_w);
1159 q *= q * (2.0f + two_cos_w);
1161 q *= two_cos_w-lsp[j];
1164 p *= p * (4.f - two_cos_w * two_cos_w);
1169 q = exp((((amplitude*vf->amplitude_offset) /
1170 (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
1171 - vf->amplitude_offset) * .11512925f);
1176 }
while (vf->map[blockflag][i] == iter_cond);
1184 av_dlog(NULL,
" Floor0 decoded\n");
1194 uint16_t range_v[4] = { 256, 128, 86, 64 };
1195 unsigned range = range_v[vf->multiplier - 1];
1196 uint16_t floor1_Y[258];
1197 uint16_t floor1_Y_final[258];
1198 int floor1_flag[258];
1199 unsigned partition_class, cdim, cbits, csub, cval,
offset, i, j;
1200 int book, adx, ady, dy, off, predicted, err;
1211 av_dlog(NULL,
"floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
1214 for (i = 0; i < vf->partitions; ++i) {
1215 partition_class = vf->partition_class[i];
1216 cdim = vf->class_dimensions[partition_class];
1217 cbits = vf->class_subclasses[partition_class];
1218 csub = (1 << cbits) - 1;
1221 av_dlog(NULL,
"Cbits %u\n", cbits);
1227 for (j = 0; j < cdim; ++j) {
1228 book = vf->subclass_books[partition_class][cval & csub];
1230 av_dlog(NULL,
"book %d Cbits %u cval %u bits:%d\n",
1233 cval = cval >> cbits;
1239 floor1_Y[offset+j] =
v;
1241 floor1_Y[offset+j] = 0;
1244 av_dlog(NULL,
" floor(%d) = %d \n",
1245 vf->list[offset+j].x, floor1_Y[offset+j]);
1254 floor1_Y_final[0] = floor1_Y[0];
1255 floor1_Y_final[1] = floor1_Y[1];
1257 for (i = 2; i < vf->x_list_dim; ++i) {
1258 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1260 low_neigh_offs = vf->list[i].low;
1261 high_neigh_offs = vf->list[i].high;
1262 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs];
1263 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
1265 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
1268 predicted = floor1_Y_final[low_neigh_offs] - off;
1270 predicted = floor1_Y_final[low_neigh_offs] + off;
1274 highroom = range-predicted;
1275 lowroom = predicted;
1276 if (highroom < lowroom) {
1277 room = highroom * 2;
1282 floor1_flag[low_neigh_offs] = 1;
1283 floor1_flag[high_neigh_offs] = 1;
1286 if (highroom > lowroom) {
1287 floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
1289 floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
1293 floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
1295 floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
1300 floor1_Y_final[i] = av_clip_uint16(predicted);
1303 av_dlog(NULL,
" Decoded floor(%d) = %u / val %u\n",
1304 vf->list[i].x, floor1_Y_final[i], val);
1311 av_dlog(NULL,
" Floor decoded\n");
1320 int partition_count,
1327 unsigned temp, temp2;
1328 for (p = 0, j = 0; j < ch_used; ++j) {
1329 if (!do_not_decode[j]) {
1333 av_dlog(NULL,
"Classword: %u\n", temp);
1340 if (temp <= 65536) {
1341 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1342 temp2 = (((uint64_t)temp) * inverse_class) >> 32;
1344 if (i < ptns_to_read)
1349 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1352 if (i < ptns_to_read)
1376 unsigned pass, ch_used, i, j, k, l;
1377 unsigned max_output = (ch - 1) * vlen;
1379 int libvorbis_bug = 0;
1382 for (j = 1; j < ch; ++j)
1383 do_not_decode[0] &= do_not_decode[j];
1384 if (do_not_decode[0])
1387 max_output += vr->
end / ch;
1390 max_output += vr->
end;
1393 if (max_output > ch_left * vlen) {
1394 if (max_output <= ch_left * vlen + vr->partition_size*ch_used/ch) {
1403 av_dlog(NULL,
" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
1406 int voffset, partition_count, j_times_ptns_to_read;
1408 voffset = vr->
begin;
1409 for (partition_count = 0; partition_count < ptns_to_read;) {
1412 if ((ret =
setup_classifs(vc, vr, do_not_decode, ch_used, partition_count, ptns_to_read)) < 0)
1415 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
1416 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1419 if (!do_not_decode[j]) {
1420 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1431 voffs = voffset+j*vlen;
1432 for (k = 0; k < step; ++k) {
1434 for (l = 0; l <
dim; ++l)
1435 vec[voffs + k + l * step] += codebook.
codevectors[coffs + l];
1437 }
else if (vr_type == 1) {
1438 voffs = voffset + j * vlen;
1439 for (k = 0; k < step; ++k) {
1441 for (l = 0; l <
dim; ++l, ++voffs) {
1444 av_dlog(NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
1445 pass, voffs, vec[voffs], codebook.
codevectors[coffs+l], coffs);
1448 }
else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) {
1449 voffs = voffset >> 1;
1452 for (k = 0; k < step; ++k) {
1455 vec[voffs + k + vlen] += codebook.
codevectors[coffs + 1];
1457 }
else if (dim == 4) {
1458 for (k = 0; k < step; ++k, voffs += 2) {
1461 vec[voffs + 1 ] += codebook.
codevectors[coffs + 2];
1462 vec[voffs + vlen ] += codebook.
codevectors[coffs + 1];
1463 vec[voffs + vlen + 1] += codebook.
codevectors[coffs + 3];
1466 for (k = 0; k < step; ++k) {
1468 for (l = 0; l <
dim; l += 2, voffs++) {
1470 vec[voffs + vlen] += codebook.
codevectors[coffs + l + 1];
1472 av_dlog(NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1473 pass, voffset / ch + (voffs % ch) * vlen,
1474 vec[voffset / ch + (voffs % ch) * vlen],
1479 }
else if (vr_type == 2) {
1480 unsigned voffs_div =
FASTDIV(voffset << 1, ch <<1);
1481 unsigned voffs_mod = voffset - voffs_div * ch;
1483 for (k = 0; k < step; ++k) {
1485 for (l = 0; l <
dim; ++l) {
1486 vec[voffs_div + voffs_mod * vlen] +=
1489 av_dlog(NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1490 pass, voffs_div + voffs_mod * vlen,
1491 vec[voffs_div + voffs_mod * vlen],
1494 if (++voffs_mod == ch) {
1503 j_times_ptns_to_read += ptns_to_read;
1509 if (libvorbis_bug && !pass) {
1510 for (j = 0; j < ch_used; ++j) {
1511 if (!do_not_decode[j]) {
1524 float *vec,
unsigned vlen,
1529 else if (vr->
type == 1)
1531 else if (vr->
type == 0)
1542 for (i = 0; i < blocksize; i++) {
1545 ang[i] = mag[i] - ang[i];
1547 float temp = ang[i];
1555 float temp = ang[i];
1570 unsigned mode_number, blockflag, blocksize;
1577 unsigned res_num = 0;
1595 av_dlog(NULL,
" Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
1600 vlen = blocksize / 2;
1603 if (previous_window < 0)
1604 previous_window = code>>1;
1605 }
else if (previous_window < 0)
1606 previous_window = 0;
1608 memset(ch_res_ptr, 0,
sizeof(
float) * vc->
audio_channels * vlen);
1610 memset(floor_ptr[i], 0, vlen *
sizeof(floor_ptr[0][0]));
1623 ret = floor->
decode(vc, &floor->
data, floor_ptr[i]);
1629 no_residue[i] =
ret;
1635 if (!(no_residue[mapping->
magnitude[i]] & no_residue[mapping->
angle[i]])) {
1637 no_residue[mapping->
angle[i]] = 0;
1643 for (i = 0; i < mapping->
submaps; ++i) {
1649 if ((mapping->
submaps == 1) || (i == mapping->
mux[j])) {
1650 res_chan[j] = res_num;
1651 if (no_residue[j]) {
1652 do_not_decode[ch] = 1;
1654 do_not_decode[ch] = 0;
1671 ch_res_ptr += ch * vlen;
1690 mdct = &vc->
mdct[blockflag];
1694 vc->
fdsp->
vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1695 mdct->
imdct_half(mdct, ch_res_ptr, floor_ptr[j]);
1700 retlen = (blocksize + vc->
blocksize[previous_window]) / 4;
1705 float *saved = vc->
saved + j * bs1 / 4;
1706 float *
ret = floor_ptr[j];
1707 float *
buf = residue;
1708 const float *win = vc->
win[blockflag & previous_window];
1710 if (blockflag == previous_window) {
1712 }
else if (blockflag > previous_window) {
1714 memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) *
sizeof(
float));
1716 memcpy(ret, saved, ((bs1 - bs0) / 4) *
sizeof(
float));
1719 memcpy(saved, buf + blocksize / 4, blocksize / 4 *
sizeof(
float));
1729 int *got_frame_ptr,
AVPacket *avpkt)
1732 int buf_size = avpkt->
size;
1736 float *channel_ptrs[255];
1739 av_dlog(NULL,
"packet length %d \n", buf_size);
1741 if (*buf == 1 && buf_size > 7) {
1760 if (*buf == 3 && buf_size > 7) {
1807 av_dlog(NULL,
"parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1833 sizeof(*vc->
saved));