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 ||
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) {
997 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 ",
1041 if (hdr_type != 1) {
1053 if (hdr_type != 5) {
1083 float *lsp = vf->lsp;
1084 unsigned amplitude, book_idx;
1087 if (!vf->amplitude_bits)
1090 amplitude =
get_bits(&vc->
gb, vf->amplitude_bits);
1091 if (amplitude > 0) {
1093 unsigned idx, lsp_len = 0;
1097 if (book_idx >= vf->num_books) {
1101 av_dlog(NULL,
"floor0 dec: booknumber: %u\n", book_idx);
1102 codebook = vc->
codebooks[vf->book_list[book_idx]];
1107 while (lsp_len<vf->order) {
1116 av_dlog(NULL,
"floor0 dec: vector offset: %d\n", vec_off);
1118 for (idx = 0; idx < codebook.
dimensions; ++idx)
1119 lsp[lsp_len+idx] = codebook.
codevectors[vec_off+idx] + last;
1120 last = lsp[lsp_len+idx-1];
1127 for (idx = 0; idx < lsp_len; ++idx)
1128 av_dlog(NULL,
"floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
1134 int order = vf->order;
1135 float wstep =
M_PI / vf->bark_map_size;
1137 for (i = 0; i < order; i++)
1138 lsp[i] = 2.0f * cos(lsp[i]);
1140 av_dlog(NULL,
"floor0 synth: map_size = %"PRIu32
"; m = %d; wstep = %f\n",
1141 vf->map_size[blockflag], order, wstep);
1144 while (i < vf->map_size[blockflag]) {
1145 int j, iter_cond = vf->map[blockflag][i];
1148 float two_cos_w = 2.0f * cos(wstep * iter_cond);
1151 for (j = 0; j + 1 < order; j += 2) {
1152 q *= lsp[j] - two_cos_w;
1153 p *= lsp[j + 1] - two_cos_w;
1156 p *= p * (2.0f - two_cos_w);
1157 q *= q * (2.0f + two_cos_w);
1159 q *= two_cos_w-lsp[j];
1162 p *= p * (4.f - two_cos_w * two_cos_w);
1167 q = exp((((amplitude*vf->amplitude_offset) /
1168 (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
1169 - vf->amplitude_offset) * .11512925f);
1174 }
while (vf->map[blockflag][i] == iter_cond);
1182 av_dlog(NULL,
" Floor0 decoded\n");
1192 uint16_t range_v[4] = { 256, 128, 86, 64 };
1193 unsigned range = range_v[vf->multiplier - 1];
1194 uint16_t floor1_Y[258];
1195 uint16_t floor1_Y_final[258];
1196 int floor1_flag[258];
1197 unsigned partition_class, cdim, cbits, csub, cval,
offset, i, j;
1198 int book, adx, ady, dy,
off, predicted, err;
1209 av_dlog(NULL,
"floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
1212 for (i = 0; i < vf->partitions; ++i) {
1213 partition_class = vf->partition_class[i];
1214 cdim = vf->class_dimensions[partition_class];
1215 cbits = vf->class_subclasses[partition_class];
1216 csub = (1 << cbits) - 1;
1219 av_dlog(NULL,
"Cbits %u\n", cbits);
1225 for (j = 0; j < cdim; ++j) {
1226 book = vf->subclass_books[partition_class][cval & csub];
1228 av_dlog(NULL,
"book %d Cbits %u cval %u bits:%d\n",
1231 cval = cval >> cbits;
1237 floor1_Y[offset+j] =
v;
1239 floor1_Y[offset+j] = 0;
1242 av_dlog(NULL,
" floor(%d) = %d \n",
1243 vf->list[offset+j].x, floor1_Y[offset+j]);
1252 floor1_Y_final[0] = floor1_Y[0];
1253 floor1_Y_final[1] = floor1_Y[1];
1255 for (i = 2; i < vf->x_list_dim; ++i) {
1256 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1258 low_neigh_offs = vf->list[i].low;
1259 high_neigh_offs = vf->list[i].high;
1260 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs];
1261 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
1263 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
1266 predicted = floor1_Y_final[low_neigh_offs] -
off;
1268 predicted = floor1_Y_final[low_neigh_offs] +
off;
1272 highroom = range-predicted;
1273 lowroom = predicted;
1274 if (highroom < lowroom) {
1275 room = highroom * 2;
1280 floor1_flag[low_neigh_offs] = 1;
1281 floor1_flag[high_neigh_offs] = 1;
1284 if (highroom > lowroom) {
1285 floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
1287 floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
1291 floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
1293 floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
1298 floor1_Y_final[i] = av_clip_uint16(predicted);
1301 av_dlog(NULL,
" Decoded floor(%d) = %u / val %u\n",
1302 vf->list[i].x, floor1_Y_final[i], val);
1309 av_dlog(NULL,
" Floor decoded\n");
1318 int partition_count)
1323 unsigned temp, temp2;
1324 for (p = 0, j = 0; j < ch_used; ++j) {
1325 if (!do_not_decode[j]) {
1329 av_dlog(NULL,
"Classword: %u\n", temp);
1336 if (temp <= 65536) {
1337 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1338 temp2 = (((uint64_t)temp) * inverse_class) >> 32;
1340 if (i < vr->ptns_to_read)
1345 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1348 if (i < vr->ptns_to_read)
1372 unsigned pass, ch_used, i, j, k, l;
1373 unsigned max_output = (ch - 1) * vlen;
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) {
1393 av_dlog(NULL,
" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
1396 int voffset, partition_count, j_times_ptns_to_read;
1398 voffset = vr->
begin;
1399 for (partition_count = 0; partition_count < ptns_to_read;) {
1402 if ((ret =
setup_classifs(vc, vr, do_not_decode, ch_used, partition_count)) < 0)
1405 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
1406 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1409 if (!do_not_decode[j]) {
1410 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1421 voffs = voffset+j*vlen;
1422 for (k = 0; k < step; ++k) {
1424 for (l = 0; l <
dim; ++l)
1425 vec[voffs + k + l * step] += codebook.
codevectors[coffs + l];
1427 }
else if (vr_type == 1) {
1428 voffs = voffset + j * vlen;
1429 for (k = 0; k < step; ++k) {
1431 for (l = 0; l <
dim; ++l, ++voffs) {
1434 av_dlog(NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
1435 pass, voffs, vec[voffs], codebook.
codevectors[coffs+l], coffs);
1438 }
else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) {
1439 voffs = voffset >> 1;
1442 for (k = 0; k < step; ++k) {
1445 vec[voffs + k + vlen] += codebook.
codevectors[coffs + 1];
1447 }
else if (dim == 4) {
1448 for (k = 0; k < step; ++k, voffs += 2) {
1451 vec[voffs + 1 ] += codebook.
codevectors[coffs + 2];
1452 vec[voffs + vlen ] += codebook.
codevectors[coffs + 1];
1453 vec[voffs + vlen + 1] += codebook.
codevectors[coffs + 3];
1456 for (k = 0; k < step; ++k) {
1458 for (l = 0; l <
dim; l += 2, voffs++) {
1460 vec[voffs + vlen] += codebook.
codevectors[coffs + l + 1];
1462 av_dlog(NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1463 pass, voffset / ch + (voffs % ch) * vlen,
1464 vec[voffset / ch + (voffs % ch) * vlen],
1469 }
else if (vr_type == 2) {
1470 unsigned voffs_div =
FASTDIV(voffset << 1, ch <<1);
1471 unsigned voffs_mod = voffset - voffs_div * ch;
1473 for (k = 0; k < step; ++k) {
1475 for (l = 0; l <
dim; ++l) {
1476 vec[voffs_div + voffs_mod * vlen] +=
1479 av_dlog(NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1480 pass, voffs_div + voffs_mod * vlen,
1481 vec[voffs_div + voffs_mod * vlen],
1484 if (++voffs_mod == ch) {
1493 j_times_ptns_to_read += ptns_to_read;
1506 float *vec,
unsigned vlen,
1511 else if (vr->
type == 1)
1513 else if (vr->
type == 0)
1524 for (i = 0; i < blocksize; i++) {
1527 ang[i] = mag[i] - ang[i];
1529 float temp = ang[i];
1537 float temp = ang[i];
1552 unsigned mode_number, blockflag, blocksize;
1559 unsigned res_num = 0;
1577 av_dlog(NULL,
" Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
1582 vlen = blocksize / 2;
1588 memset(ch_res_ptr, 0,
sizeof(
float) * vc->
audio_channels * vlen);
1590 memset(floor_ptr[i], 0, vlen *
sizeof(floor_ptr[0][0]));
1603 ret = floor->
decode(vc, &floor->
data, floor_ptr[i]);
1609 no_residue[i] =
ret;
1615 if (!(no_residue[mapping->
magnitude[i]] & no_residue[mapping->
angle[i]])) {
1617 no_residue[mapping->
angle[i]] = 0;
1623 for (i = 0; i < mapping->
submaps; ++i) {
1629 if ((mapping->
submaps == 1) || (i == mapping->
mux[j])) {
1630 res_chan[j] = res_num;
1631 if (no_residue[j]) {
1632 do_not_decode[ch] = 1;
1634 do_not_decode[ch] = 0;
1651 ch_res_ptr += ch * vlen;
1670 mdct = &vc->
mdct[blockflag];
1674 vc->
fdsp.
vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1675 mdct->
imdct_half(mdct, ch_res_ptr, floor_ptr[j]);
1680 retlen = (blocksize + vc->
blocksize[previous_window]) / 4;
1685 float *saved = vc->
saved + j * bs1 / 4;
1686 float *
ret = floor_ptr[j];
1687 float *
buf = residue;
1688 const float *win = vc->
win[blockflag & previous_window];
1690 if (blockflag == previous_window) {
1692 }
else if (blockflag > previous_window) {
1694 memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) *
sizeof(
float));
1696 memcpy(ret, saved, ((bs1 - bs0) / 4) *
sizeof(
float));
1699 memcpy(saved, buf + blocksize / 4, blocksize / 4 *
sizeof(
float));
1709 int *got_frame_ptr,
AVPacket *avpkt)
1712 int buf_size = avpkt->
size;
1716 float *channel_ptrs[255];
1719 av_dlog(NULL,
"packet length %d \n", buf_size);
1721 if (*buf == 1 && buf_size > 7) {
1740 if (*buf == 3 && buf_size > 7) {
1787 av_dlog(NULL,
"parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1813 sizeof(*vc->
saved));