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;
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",
307 for (ce = 0; ce < entries; ++ce) {
310 tmp_vlc_bits[ce] =
get_bits(gb, 5) + 1;
313 tmp_vlc_bits[ce] = 0;
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) {
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);
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;
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");
693 unsigned high_bits, low_bits;
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;
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));
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);
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;
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;
1236 floor1_Y[offset+j] = 0;
1240 vf->list[offset+j].x, floor1_Y[offset+j]);
1249 floor1_Y_final[0] = floor1_Y[0];
1250 floor1_Y_final[1] = floor1_Y[1];
1252 for (i = 2; i < vf->x_list_dim; ++i) {
1253 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1255 low_neigh_offs = vf->list[i].low;
1256 high_neigh_offs = vf->list[i].high;
1257 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs];
1258 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
1260 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
1263 predicted = floor1_Y_final[low_neigh_offs] -
off;
1265 predicted = floor1_Y_final[low_neigh_offs] +
off;
1269 highroom = range-predicted;
1270 lowroom = predicted;
1271 if (highroom < lowroom) {
1272 room = highroom * 2;
1277 floor1_flag[low_neigh_offs] = 1;
1278 floor1_flag[high_neigh_offs] = 1;
1281 if (highroom > lowroom) {
1282 floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
1284 floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
1288 floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
1290 floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
1295 floor1_Y_final[i] = av_clip_uint16(predicted);
1298 av_dlog(
NULL,
" Decoded floor(%d) = %u / val %u\n",
1299 vf->list[i].x, floor1_Y_final[i], val);
1326 unsigned pass, ch_used, i, j, k, l;
1327 unsigned max_output = (ch - 1) * vlen;
1330 for (j = 1; j < ch; ++j)
1331 do_not_decode[0] &= do_not_decode[j];
1332 if (do_not_decode[0])
1335 max_output += vr->
end / ch;
1338 max_output += vr->
end;
1341 if (max_output > ch_left * vlen) {
1346 av_dlog(
NULL,
" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
1349 uint16_t voffset, partition_count, j_times_ptns_to_read;
1351 voffset = vr->
begin;
1352 for (partition_count = 0; partition_count < ptns_to_read;) {
1355 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1356 if (!do_not_decode[j]) {
1363 for (i = 0; i < c_p_c; ++i) {
1366 temp2 = (((uint64_t)temp) * inverse_class) >> 32;
1367 if (partition_count + c_p_c - 1 - i < ptns_to_read)
1368 classifs[j_times_ptns_to_read + partition_count + c_p_c - 1 - i] = temp - temp2 * vr->
classifications;
1372 j_times_ptns_to_read += ptns_to_read;
1375 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
1376 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1379 if (!do_not_decode[j]) {
1380 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1391 voffs = voffset+j*vlen;
1392 for (k = 0; k < step; ++k) {
1394 for (l = 0; l <
dim; ++l)
1395 vec[voffs + k + l * step] += codebook.
codevectors[coffs + l];
1397 }
else if (vr_type == 1) {
1398 voffs = voffset + j * vlen;
1399 for (k = 0; k < step; ++k) {
1401 for (l = 0; l <
dim; ++l, ++voffs) {
1404 av_dlog(
NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
1405 pass, voffs, vec[voffs], codebook.
codevectors[coffs+l], coffs);
1408 }
else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) {
1409 voffs = voffset >> 1;
1412 for (k = 0; k < step; ++k) {
1415 vec[voffs + k + vlen] += codebook.
codevectors[coffs + 1];
1417 }
else if (dim == 4) {
1418 for (k = 0; k < step; ++k, voffs += 2) {
1421 vec[voffs + 1 ] += codebook.
codevectors[coffs + 2];
1422 vec[voffs + vlen ] += codebook.
codevectors[coffs + 1];
1423 vec[voffs + vlen + 1] += codebook.
codevectors[coffs + 3];
1426 for (k = 0; k < step; ++k) {
1428 for (l = 0; l <
dim; l += 2, voffs++) {
1430 vec[voffs + vlen] += codebook.
codevectors[coffs + l + 1];
1432 av_dlog(
NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1433 pass, voffset / ch + (voffs % ch) * vlen,
1434 vec[voffset / ch + (voffs % ch) * vlen],
1439 }
else if (vr_type == 2) {
1440 unsigned voffs_div =
FASTDIV(voffset << 1, ch <<1);
1441 unsigned voffs_mod = voffset - voffs_div * ch;
1443 for (k = 0; k < step; ++k) {
1445 for (l = 0; l <
dim; ++l) {
1446 vec[voffs_div + voffs_mod * vlen] +=
1449 av_dlog(
NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1450 pass, voffs_div + voffs_mod * vlen,
1451 vec[voffs_div + voffs_mod * vlen],
1454 if (++voffs_mod == ch) {
1463 j_times_ptns_to_read += ptns_to_read;
1476 float *vec,
unsigned vlen,
1481 else if (vr->
type == 1)
1483 else if (vr->
type == 0)
1494 for (i = 0; i < blocksize; i++) {
1497 ang[i] = mag[i] - ang[i];
1499 float temp = ang[i];
1507 float temp = ang[i];
1522 unsigned mode_number, blockflag, blocksize;
1529 unsigned res_num = 0;
1547 av_dlog(
NULL,
" Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
1552 vlen = blocksize / 2;
1558 memset(ch_res_ptr, 0,
sizeof(
float) * vc->
audio_channels * vlen);
1560 memset(floor_ptr[i], 0, vlen *
sizeof(floor_ptr[0][0]));
1573 ret = floor->
decode(vc, &floor->
data, floor_ptr[i]);
1579 no_residue[i] = ret;
1585 if (!(no_residue[mapping->
magnitude[i]] & no_residue[mapping->
angle[i]])) {
1587 no_residue[mapping->
angle[i]] = 0;
1593 for (i = 0; i < mapping->
submaps; ++i) {
1599 if ((mapping->
submaps == 1) || (i == mapping->
mux[j])) {
1600 res_chan[j] = res_num;
1601 if (no_residue[j]) {
1602 do_not_decode[ch] = 1;
1604 do_not_decode[ch] = 0;
1621 ch_res_ptr += ch * vlen;
1640 mdct = &vc->
mdct[blockflag];
1644 vc->
fdsp.
vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1645 mdct->
imdct_half(mdct, ch_res_ptr, floor_ptr[j]);
1650 retlen = (blocksize + vc->
blocksize[previous_window]) / 4;
1655 float *saved = vc->
saved + j * bs1 / 4;
1656 float *ret = floor_ptr[j];
1657 float *buf = residue;
1658 const float *win = vc->
win[blockflag & previous_window];
1660 if (blockflag == previous_window) {
1662 }
else if (blockflag > previous_window) {
1664 memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) *
sizeof(
float));
1666 memcpy(ret, saved, ((bs1 - bs0) / 4) *
sizeof(
float));
1669 memcpy(saved, buf + blocksize / 4, blocksize / 4 *
sizeof(
float));
1679 int *got_frame_ptr,
AVPacket *avpkt)
1682 int buf_size = avpkt->
size;
1686 float *channel_ptrs[255];
1691 if (*buf == 1 && buf_size > 7) {
1710 if (*buf == 3 && buf_size > 7) {
1758 av_dlog(
NULL,
"parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1784 sizeof(*vc->
saved));