You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1682 lines
62KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Vorbis I decoder
  21. * @author Denes Balatoni ( dbalatoni programozo hu )
  22. */
  23. #include <inttypes.h>
  24. #include <math.h>
  25. #define ALT_BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "dsputil.h"
  29. #include "fft.h"
  30. #include "fmtconvert.h"
  31. #include "vorbis.h"
  32. #include "xiph.h"
  33. #define V_NB_BITS 8
  34. #define V_NB_BITS2 11
  35. #define V_MAX_VLCS (1 << 16)
  36. #define V_MAX_PARTITIONS (1 << 20)
  37. #undef NDEBUG
  38. #include <assert.h>
  39. typedef struct {
  40. uint8_t dimensions;
  41. uint8_t lookup_type;
  42. uint8_t maxdepth;
  43. VLC vlc;
  44. float *codevectors;
  45. unsigned int nb_bits;
  46. } vorbis_codebook;
  47. typedef union vorbis_floor_u vorbis_floor_data;
  48. typedef struct vorbis_floor0_s vorbis_floor0;
  49. typedef struct vorbis_floor1_s vorbis_floor1;
  50. struct vorbis_context_s;
  51. typedef
  52. int (* vorbis_floor_decode_func)
  53. (struct vorbis_context_s *, vorbis_floor_data *, float *);
  54. typedef struct {
  55. uint8_t floor_type;
  56. vorbis_floor_decode_func decode;
  57. union vorbis_floor_u {
  58. struct vorbis_floor0_s {
  59. uint8_t order;
  60. uint16_t rate;
  61. uint16_t bark_map_size;
  62. int32_t *map[2];
  63. uint32_t map_size[2];
  64. uint8_t amplitude_bits;
  65. uint8_t amplitude_offset;
  66. uint8_t num_books;
  67. uint8_t *book_list;
  68. float *lsp;
  69. } t0;
  70. struct vorbis_floor1_s {
  71. uint8_t partitions;
  72. uint8_t partition_class[32];
  73. uint8_t class_dimensions[16];
  74. uint8_t class_subclasses[16];
  75. uint8_t class_masterbook[16];
  76. int16_t subclass_books[16][8];
  77. uint8_t multiplier;
  78. uint16_t x_list_dim;
  79. vorbis_floor1_entry *list;
  80. } t1;
  81. } data;
  82. } vorbis_floor;
  83. typedef struct {
  84. uint16_t type;
  85. uint32_t begin;
  86. uint32_t end;
  87. unsigned partition_size;
  88. uint8_t classifications;
  89. uint8_t classbook;
  90. int16_t books[64][8];
  91. uint8_t maxpass;
  92. uint16_t ptns_to_read;
  93. uint8_t *classifs;
  94. } vorbis_residue;
  95. typedef struct {
  96. uint8_t submaps;
  97. uint16_t coupling_steps;
  98. uint8_t *magnitude;
  99. uint8_t *angle;
  100. uint8_t *mux;
  101. uint8_t submap_floor[16];
  102. uint8_t submap_residue[16];
  103. } vorbis_mapping;
  104. typedef struct {
  105. uint8_t blockflag;
  106. uint16_t windowtype;
  107. uint16_t transformtype;
  108. uint8_t mapping;
  109. } vorbis_mode;
  110. typedef struct vorbis_context_s {
  111. AVCodecContext *avccontext;
  112. GetBitContext gb;
  113. DSPContext dsp;
  114. FmtConvertContext fmt_conv;
  115. FFTContext mdct[2];
  116. uint8_t first_frame;
  117. uint32_t version;
  118. uint8_t audio_channels;
  119. uint32_t audio_samplerate;
  120. uint32_t bitrate_maximum;
  121. uint32_t bitrate_nominal;
  122. uint32_t bitrate_minimum;
  123. uint32_t blocksize[2];
  124. const float *win[2];
  125. uint16_t codebook_count;
  126. vorbis_codebook *codebooks;
  127. uint8_t floor_count;
  128. vorbis_floor *floors;
  129. uint8_t residue_count;
  130. vorbis_residue *residues;
  131. uint8_t mapping_count;
  132. vorbis_mapping *mappings;
  133. uint8_t mode_count;
  134. vorbis_mode *modes;
  135. uint8_t mode_number; // mode number for the current packet
  136. uint8_t previous_window;
  137. float *channel_residues;
  138. float *channel_floors;
  139. float *saved;
  140. float scale_bias; // for float->int conversion
  141. } vorbis_context;
  142. /* Helper functions */
  143. #define BARK(x) \
  144. (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
  145. static const char idx_err_str[] = "Index value %d out of range (0 - %d) for %s at %s:%i\n";
  146. #define VALIDATE_INDEX(idx, limit) \
  147. if (idx >= limit) {\
  148. av_log(vc->avccontext, AV_LOG_ERROR,\
  149. idx_err_str,\
  150. (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
  151. return -1;\
  152. }
  153. #define GET_VALIDATED_INDEX(idx, bits, limit) \
  154. {\
  155. idx = get_bits(gb, bits);\
  156. VALIDATE_INDEX(idx, limit)\
  157. }
  158. static float vorbisfloat2float(unsigned val)
  159. {
  160. double mant = val & 0x1fffff;
  161. long exp = (val & 0x7fe00000L) >> 21;
  162. if (val & 0x80000000)
  163. mant = -mant;
  164. return ldexp(mant, exp - 20 - 768);
  165. }
  166. // Free all allocated memory -----------------------------------------
  167. static void vorbis_free(vorbis_context *vc)
  168. {
  169. int i;
  170. av_freep(&vc->channel_residues);
  171. av_freep(&vc->channel_floors);
  172. av_freep(&vc->saved);
  173. for (i = 0; i < vc->residue_count; i++)
  174. av_free(vc->residues[i].classifs);
  175. av_freep(&vc->residues);
  176. av_freep(&vc->modes);
  177. ff_mdct_end(&vc->mdct[0]);
  178. ff_mdct_end(&vc->mdct[1]);
  179. for (i = 0; i < vc->codebook_count; ++i) {
  180. av_free(vc->codebooks[i].codevectors);
  181. free_vlc(&vc->codebooks[i].vlc);
  182. }
  183. av_freep(&vc->codebooks);
  184. for (i = 0; i < vc->floor_count; ++i) {
  185. if (vc->floors[i].floor_type == 0) {
  186. av_free(vc->floors[i].data.t0.map[0]);
  187. av_free(vc->floors[i].data.t0.map[1]);
  188. av_free(vc->floors[i].data.t0.book_list);
  189. av_free(vc->floors[i].data.t0.lsp);
  190. } else {
  191. av_free(vc->floors[i].data.t1.list);
  192. }
  193. }
  194. av_freep(&vc->floors);
  195. for (i = 0; i < vc->mapping_count; ++i) {
  196. av_free(vc->mappings[i].magnitude);
  197. av_free(vc->mappings[i].angle);
  198. av_free(vc->mappings[i].mux);
  199. }
  200. av_freep(&vc->mappings);
  201. }
  202. // Parse setup header -------------------------------------------------
  203. // Process codebooks part
  204. static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)
  205. {
  206. unsigned cb;
  207. uint8_t *tmp_vlc_bits;
  208. uint32_t *tmp_vlc_codes;
  209. GetBitContext *gb = &vc->gb;
  210. uint16_t *codebook_multiplicands;
  211. vc->codebook_count = get_bits(gb, 8) + 1;
  212. av_dlog(NULL, " Codebooks: %d \n", vc->codebook_count);
  213. vc->codebooks = av_mallocz(vc->codebook_count * sizeof(*vc->codebooks));
  214. tmp_vlc_bits = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_bits));
  215. tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_codes));
  216. codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));
  217. for (cb = 0; cb < vc->codebook_count; ++cb) {
  218. vorbis_codebook *codebook_setup = &vc->codebooks[cb];
  219. unsigned ordered, t, entries, used_entries = 0;
  220. av_dlog(NULL, " %u. Codebook\n", cb);
  221. if (get_bits(gb, 24) != 0x564342) {
  222. av_log(vc->avccontext, AV_LOG_ERROR,
  223. " %u. Codebook setup data corrupt.\n", cb);
  224. goto error;
  225. }
  226. codebook_setup->dimensions=get_bits(gb, 16);
  227. if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {
  228. av_log(vc->avccontext, AV_LOG_ERROR,
  229. " %u. Codebook's dimension is invalid (%d).\n",
  230. cb, codebook_setup->dimensions);
  231. goto error;
  232. }
  233. entries = get_bits(gb, 24);
  234. if (entries > V_MAX_VLCS) {
  235. av_log(vc->avccontext, AV_LOG_ERROR,
  236. " %u. Codebook has too many entries (%u).\n",
  237. cb, entries);
  238. goto error;
  239. }
  240. ordered = get_bits1(gb);
  241. av_dlog(NULL, " codebook_dimensions %d, codebook_entries %u\n",
  242. codebook_setup->dimensions, entries);
  243. if (!ordered) {
  244. unsigned ce, flag;
  245. unsigned sparse = get_bits1(gb);
  246. av_dlog(NULL, " not ordered \n");
  247. if (sparse) {
  248. av_dlog(NULL, " sparse \n");
  249. used_entries = 0;
  250. for (ce = 0; ce < entries; ++ce) {
  251. flag = get_bits1(gb);
  252. if (flag) {
  253. tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
  254. ++used_entries;
  255. } else
  256. tmp_vlc_bits[ce] = 0;
  257. }
  258. } else {
  259. av_dlog(NULL, " not sparse \n");
  260. used_entries = entries;
  261. for (ce = 0; ce < entries; ++ce)
  262. tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
  263. }
  264. } else {
  265. unsigned current_entry = 0;
  266. unsigned current_length = get_bits(gb, 5) + 1;
  267. av_dlog(NULL, " ordered, current length: %u\n", current_length); //FIXME
  268. used_entries = entries;
  269. for (; current_entry < used_entries && current_length <= 32; ++current_length) {
  270. unsigned i, number;
  271. av_dlog(NULL, " number bits: %u ", ilog(entries - current_entry));
  272. number = get_bits(gb, ilog(entries - current_entry));
  273. av_dlog(NULL, " number: %u\n", number);
  274. for (i = current_entry; i < number+current_entry; ++i)
  275. if (i < used_entries)
  276. tmp_vlc_bits[i] = current_length;
  277. current_entry+=number;
  278. }
  279. if (current_entry>used_entries) {
  280. av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
  281. goto error;
  282. }
  283. }
  284. codebook_setup->lookup_type = get_bits(gb, 4);
  285. av_dlog(NULL, " lookup type: %d : %s \n", codebook_setup->lookup_type,
  286. codebook_setup->lookup_type ? "vq" : "no lookup");
  287. // If the codebook is used for (inverse) VQ, calculate codevectors.
  288. if (codebook_setup->lookup_type == 1) {
  289. unsigned i, j, k;
  290. unsigned codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
  291. float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));
  292. float codebook_delta_value = vorbisfloat2float(get_bits_long(gb, 32));
  293. unsigned codebook_value_bits = get_bits(gb, 4) + 1;
  294. unsigned codebook_sequence_p = get_bits1(gb);
  295. av_dlog(NULL, " We expect %d numbers for building the codevectors. \n",
  296. codebook_lookup_values);
  297. av_dlog(NULL, " delta %f minmum %f \n",
  298. codebook_delta_value, codebook_minimum_value);
  299. for (i = 0; i < codebook_lookup_values; ++i) {
  300. codebook_multiplicands[i] = get_bits(gb, codebook_value_bits);
  301. av_dlog(NULL, " multiplicands*delta+minmum : %e \n",
  302. (float)codebook_multiplicands[i] * codebook_delta_value + codebook_minimum_value);
  303. av_dlog(NULL, " multiplicand %u\n", codebook_multiplicands[i]);
  304. }
  305. // Weed out unused vlcs and build codevector vector
  306. codebook_setup->codevectors = used_entries ? av_mallocz(used_entries *
  307. codebook_setup->dimensions *
  308. sizeof(*codebook_setup->codevectors))
  309. : NULL;
  310. for (j = 0, i = 0; i < entries; ++i) {
  311. unsigned dim = codebook_setup->dimensions;
  312. if (tmp_vlc_bits[i]) {
  313. float last = 0.0;
  314. unsigned lookup_offset = i;
  315. av_dlog(vc->avccontext, "Lookup offset %u ,", i);
  316. for (k = 0; k < dim; ++k) {
  317. unsigned multiplicand_offset = lookup_offset % codebook_lookup_values;
  318. codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
  319. if (codebook_sequence_p)
  320. last = codebook_setup->codevectors[j * dim + k];
  321. lookup_offset/=codebook_lookup_values;
  322. }
  323. tmp_vlc_bits[j] = tmp_vlc_bits[i];
  324. av_dlog(vc->avccontext, "real lookup offset %u, vector: ", j);
  325. for (k = 0; k < dim; ++k)
  326. av_dlog(vc->avccontext, " %f ",
  327. codebook_setup->codevectors[j * dim + k]);
  328. av_dlog(vc->avccontext, "\n");
  329. ++j;
  330. }
  331. }
  332. if (j != used_entries) {
  333. av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
  334. goto error;
  335. }
  336. entries = used_entries;
  337. } else if (codebook_setup->lookup_type >= 2) {
  338. av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
  339. goto error;
  340. }
  341. // Initialize VLC table
  342. if (ff_vorbis_len2vlc(tmp_vlc_bits, tmp_vlc_codes, entries)) {
  343. av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n");
  344. goto error;
  345. }
  346. codebook_setup->maxdepth = 0;
  347. for (t = 0; t < entries; ++t)
  348. if (tmp_vlc_bits[t] >= codebook_setup->maxdepth)
  349. codebook_setup->maxdepth = tmp_vlc_bits[t];
  350. if (codebook_setup->maxdepth > 3 * V_NB_BITS)
  351. codebook_setup->nb_bits = V_NB_BITS2;
  352. else
  353. codebook_setup->nb_bits = V_NB_BITS;
  354. codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;
  355. if (init_vlc(&codebook_setup->vlc, codebook_setup->nb_bits, entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits), sizeof(*tmp_vlc_bits), tmp_vlc_codes, sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes), INIT_VLC_LE)) {
  356. av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \n");
  357. goto error;
  358. }
  359. }
  360. av_free(tmp_vlc_bits);
  361. av_free(tmp_vlc_codes);
  362. av_free(codebook_multiplicands);
  363. return 0;
  364. // Error:
  365. error:
  366. av_free(tmp_vlc_bits);
  367. av_free(tmp_vlc_codes);
  368. av_free(codebook_multiplicands);
  369. return -1;
  370. }
  371. // Process time domain transforms part (unused in Vorbis I)
  372. static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc)
  373. {
  374. GetBitContext *gb = &vc->gb;
  375. unsigned i, vorbis_time_count = get_bits(gb, 6) + 1;
  376. for (i = 0; i < vorbis_time_count; ++i) {
  377. unsigned vorbis_tdtransform = get_bits(gb, 16);
  378. av_dlog(NULL, " Vorbis time domain transform %u: %u\n",
  379. vorbis_time_count, vorbis_tdtransform);
  380. if (vorbis_tdtransform) {
  381. av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n");
  382. return -1;
  383. }
  384. }
  385. return 0;
  386. }
  387. // Process floors part
  388. static int vorbis_floor0_decode(vorbis_context *vc,
  389. vorbis_floor_data *vfu, float *vec);
  390. static void create_map(vorbis_context *vc, unsigned floor_number);
  391. static int vorbis_floor1_decode(vorbis_context *vc,
  392. vorbis_floor_data *vfu, float *vec);
  393. static int vorbis_parse_setup_hdr_floors(vorbis_context *vc)
  394. {
  395. GetBitContext *gb = &vc->gb;
  396. int i,j,k;
  397. vc->floor_count = get_bits(gb, 6) + 1;
  398. vc->floors = av_mallocz(vc->floor_count * sizeof(*vc->floors));
  399. for (i = 0; i < vc->floor_count; ++i) {
  400. vorbis_floor *floor_setup = &vc->floors[i];
  401. floor_setup->floor_type = get_bits(gb, 16);
  402. av_dlog(NULL, " %d. floor type %d \n", i, floor_setup->floor_type);
  403. if (floor_setup->floor_type == 1) {
  404. int maximum_class = -1;
  405. unsigned rangebits, rangemax, floor1_values = 2;
  406. floor_setup->decode = vorbis_floor1_decode;
  407. floor_setup->data.t1.partitions = get_bits(gb, 5);
  408. av_dlog(NULL, " %d.floor: %d partitions \n",
  409. i, floor_setup->data.t1.partitions);
  410. for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
  411. floor_setup->data.t1.partition_class[j] = get_bits(gb, 4);
  412. if (floor_setup->data.t1.partition_class[j] > maximum_class)
  413. maximum_class = floor_setup->data.t1.partition_class[j];
  414. av_dlog(NULL, " %d. floor %d partition class %d \n",
  415. i, j, floor_setup->data.t1.partition_class[j]);
  416. }
  417. av_dlog(NULL, " maximum class %d \n", maximum_class);
  418. for (j = 0; j <= maximum_class; ++j) {
  419. floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
  420. floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
  421. av_dlog(NULL, " %d floor %d class dim: %d subclasses %d \n", i, j,
  422. floor_setup->data.t1.class_dimensions[j],
  423. floor_setup->data.t1.class_subclasses[j]);
  424. if (floor_setup->data.t1.class_subclasses[j]) {
  425. GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count)
  426. av_dlog(NULL, " masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
  427. }
  428. for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
  429. int16_t bits = get_bits(gb, 8) - 1;
  430. if (bits != -1)
  431. VALIDATE_INDEX(bits, vc->codebook_count)
  432. floor_setup->data.t1.subclass_books[j][k] = bits;
  433. av_dlog(NULL, " book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
  434. }
  435. }
  436. floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
  437. floor_setup->data.t1.x_list_dim = 2;
  438. for (j = 0; j < floor_setup->data.t1.partitions; ++j)
  439. floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]];
  440. floor_setup->data.t1.list = av_mallocz(floor_setup->data.t1.x_list_dim *
  441. sizeof(*floor_setup->data.t1.list));
  442. rangebits = get_bits(gb, 4);
  443. rangemax = (1 << rangebits);
  444. if (rangemax > vc->blocksize[1] / 2) {
  445. av_log(vc->avccontext, AV_LOG_ERROR,
  446. "Floor value is too large for blocksize: %u (%"PRIu32")\n",
  447. rangemax, vc->blocksize[1] / 2);
  448. return -1;
  449. }
  450. floor_setup->data.t1.list[0].x = 0;
  451. floor_setup->data.t1.list[1].x = rangemax;
  452. for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
  453. for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
  454. floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits);
  455. av_dlog(NULL, " %u. floor1 Y coord. %d\n", floor1_values,
  456. floor_setup->data.t1.list[floor1_values].x);
  457. }
  458. }
  459. // Precalculate order of x coordinates - needed for decode
  460. ff_vorbis_ready_floor1_list(floor_setup->data.t1.list, floor_setup->data.t1.x_list_dim);
  461. } else if (floor_setup->floor_type == 0) {
  462. unsigned max_codebook_dim = 0;
  463. floor_setup->decode = vorbis_floor0_decode;
  464. floor_setup->data.t0.order = get_bits(gb, 8);
  465. floor_setup->data.t0.rate = get_bits(gb, 16);
  466. floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
  467. floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
  468. /* zero would result in a div by zero later *
  469. * 2^0 - 1 == 0 */
  470. if (floor_setup->data.t0.amplitude_bits == 0) {
  471. av_log(vc->avccontext, AV_LOG_ERROR,
  472. "Floor 0 amplitude bits is 0.\n");
  473. return -1;
  474. }
  475. floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
  476. floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
  477. /* allocate mem for booklist */
  478. floor_setup->data.t0.book_list =
  479. av_malloc(floor_setup->data.t0.num_books);
  480. if (!floor_setup->data.t0.book_list)
  481. return -1;
  482. /* read book indexes */
  483. {
  484. int idx;
  485. unsigned book_idx;
  486. for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
  487. GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
  488. floor_setup->data.t0.book_list[idx] = book_idx;
  489. if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
  490. max_codebook_dim = vc->codebooks[book_idx].dimensions;
  491. }
  492. }
  493. create_map(vc, i);
  494. /* codebook dim is for padding if codebook dim doesn't *
  495. * divide order+1 then we need to read more data */
  496. floor_setup->data.t0.lsp =
  497. av_malloc((floor_setup->data.t0.order + 1 + max_codebook_dim)
  498. * sizeof(*floor_setup->data.t0.lsp));
  499. if (!floor_setup->data.t0.lsp)
  500. return -1;
  501. /* debug output parsed headers */
  502. av_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order);
  503. av_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate);
  504. av_dlog(NULL, "floor0 bark map size: %u\n",
  505. floor_setup->data.t0.bark_map_size);
  506. av_dlog(NULL, "floor0 amplitude bits: %u\n",
  507. floor_setup->data.t0.amplitude_bits);
  508. av_dlog(NULL, "floor0 amplitude offset: %u\n",
  509. floor_setup->data.t0.amplitude_offset);
  510. av_dlog(NULL, "floor0 number of books: %u\n",
  511. floor_setup->data.t0.num_books);
  512. av_dlog(NULL, "floor0 book list pointer: %p\n",
  513. floor_setup->data.t0.book_list);
  514. {
  515. int idx;
  516. for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
  517. av_dlog(NULL, " Book %d: %u\n", idx + 1,
  518. floor_setup->data.t0.book_list[idx]);
  519. }
  520. }
  521. } else {
  522. av_log(vc->avccontext, AV_LOG_ERROR, "Invalid floor type!\n");
  523. return -1;
  524. }
  525. }
  526. return 0;
  527. }
  528. // Process residues part
  529. static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
  530. {
  531. GetBitContext *gb = &vc->gb;
  532. unsigned i, j, k;
  533. vc->residue_count = get_bits(gb, 6)+1;
  534. vc->residues = av_mallocz(vc->residue_count * sizeof(*vc->residues));
  535. av_dlog(NULL, " There are %d residues. \n", vc->residue_count);
  536. for (i = 0; i < vc->residue_count; ++i) {
  537. vorbis_residue *res_setup = &vc->residues[i];
  538. uint8_t cascade[64];
  539. unsigned high_bits, low_bits;
  540. res_setup->type = get_bits(gb, 16);
  541. av_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
  542. res_setup->begin = get_bits(gb, 24);
  543. res_setup->end = get_bits(gb, 24);
  544. res_setup->partition_size = get_bits(gb, 24) + 1;
  545. /* Validations to prevent a buffer overflow later. */
  546. if (res_setup->begin>res_setup->end ||
  547. res_setup->end > vc->avccontext->channels * vc->blocksize[1] / 2 ||
  548. (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
  549. av_log(vc->avccontext, AV_LOG_ERROR,
  550. "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
  551. res_setup->type, res_setup->begin, res_setup->end,
  552. res_setup->partition_size, vc->blocksize[1] / 2);
  553. return -1;
  554. }
  555. res_setup->classifications = get_bits(gb, 6) + 1;
  556. GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
  557. res_setup->ptns_to_read =
  558. (res_setup->end - res_setup->begin) / res_setup->partition_size;
  559. res_setup->classifs = av_malloc(res_setup->ptns_to_read *
  560. vc->audio_channels *
  561. sizeof(*res_setup->classifs));
  562. if (!res_setup->classifs)
  563. return AVERROR(ENOMEM);
  564. av_dlog(NULL, " begin %d end %d part.size %d classif.s %d classbook %d \n",
  565. res_setup->begin, res_setup->end, res_setup->partition_size,
  566. res_setup->classifications, res_setup->classbook);
  567. for (j = 0; j < res_setup->classifications; ++j) {
  568. high_bits = 0;
  569. low_bits = get_bits(gb, 3);
  570. if (get_bits1(gb))
  571. high_bits = get_bits(gb, 5);
  572. cascade[j] = (high_bits << 3) + low_bits;
  573. av_dlog(NULL, " %u class cascade depth: %d\n", j, ilog(cascade[j]));
  574. }
  575. res_setup->maxpass = 0;
  576. for (j = 0; j < res_setup->classifications; ++j) {
  577. for (k = 0; k < 8; ++k) {
  578. if (cascade[j]&(1 << k)) {
  579. GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
  580. av_dlog(NULL, " %u class cascade depth %u book: %d\n",
  581. j, k, res_setup->books[j][k]);
  582. if (k>res_setup->maxpass)
  583. res_setup->maxpass = k;
  584. } else {
  585. res_setup->books[j][k] = -1;
  586. }
  587. }
  588. }
  589. }
  590. return 0;
  591. }
  592. // Process mappings part
  593. static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
  594. {
  595. GetBitContext *gb = &vc->gb;
  596. unsigned i, j;
  597. vc->mapping_count = get_bits(gb, 6)+1;
  598. vc->mappings = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
  599. av_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
  600. for (i = 0; i < vc->mapping_count; ++i) {
  601. vorbis_mapping *mapping_setup = &vc->mappings[i];
  602. if (get_bits(gb, 16)) {
  603. av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
  604. return -1;
  605. }
  606. if (get_bits1(gb)) {
  607. mapping_setup->submaps = get_bits(gb, 4) + 1;
  608. } else {
  609. mapping_setup->submaps = 1;
  610. }
  611. if (get_bits1(gb)) {
  612. mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
  613. mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps *
  614. sizeof(*mapping_setup->magnitude));
  615. mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps *
  616. sizeof(*mapping_setup->angle));
  617. for (j = 0; j < mapping_setup->coupling_steps; ++j) {
  618. GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
  619. GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
  620. }
  621. } else {
  622. mapping_setup->coupling_steps = 0;
  623. }
  624. av_dlog(NULL, " %u mapping coupling steps: %d\n",
  625. i, mapping_setup->coupling_steps);
  626. if (get_bits(gb, 2)) {
  627. av_log(vc->avccontext, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i);
  628. return -1; // following spec.
  629. }
  630. if (mapping_setup->submaps>1) {
  631. mapping_setup->mux = av_mallocz(vc->audio_channels *
  632. sizeof(*mapping_setup->mux));
  633. for (j = 0; j < vc->audio_channels; ++j)
  634. mapping_setup->mux[j] = get_bits(gb, 4);
  635. }
  636. for (j = 0; j < mapping_setup->submaps; ++j) {
  637. skip_bits(gb, 8); // FIXME check?
  638. GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
  639. GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
  640. av_dlog(NULL, " %u mapping %u submap : floor %d, residue %d\n", i, j,
  641. mapping_setup->submap_floor[j],
  642. mapping_setup->submap_residue[j]);
  643. }
  644. }
  645. return 0;
  646. }
  647. // Process modes part
  648. static void create_map(vorbis_context *vc, unsigned floor_number)
  649. {
  650. vorbis_floor *floors = vc->floors;
  651. vorbis_floor0 *vf;
  652. int idx;
  653. int blockflag, n;
  654. int32_t *map;
  655. for (blockflag = 0; blockflag < 2; ++blockflag) {
  656. n = vc->blocksize[blockflag] / 2;
  657. floors[floor_number].data.t0.map[blockflag] =
  658. av_malloc((n + 1) * sizeof(int32_t)); // n + sentinel
  659. map = floors[floor_number].data.t0.map[blockflag];
  660. vf = &floors[floor_number].data.t0;
  661. for (idx = 0; idx < n; ++idx) {
  662. map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
  663. ((vf->bark_map_size) /
  664. BARK(vf->rate / 2.0f)));
  665. if (vf->bark_map_size-1 < map[idx])
  666. map[idx] = vf->bark_map_size - 1;
  667. }
  668. map[n] = -1;
  669. vf->map_size[blockflag] = n;
  670. }
  671. for (idx = 0; idx <= n; ++idx) {
  672. av_dlog(NULL, "floor0 map: map at pos %d is %d\n", idx, map[idx]);
  673. }
  674. }
  675. static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
  676. {
  677. GetBitContext *gb = &vc->gb;
  678. unsigned i;
  679. vc->mode_count = get_bits(gb, 6) + 1;
  680. vc->modes = av_mallocz(vc->mode_count * sizeof(*vc->modes));
  681. av_dlog(NULL, " There are %d modes.\n", vc->mode_count);
  682. for (i = 0; i < vc->mode_count; ++i) {
  683. vorbis_mode *mode_setup = &vc->modes[i];
  684. mode_setup->blockflag = get_bits1(gb);
  685. mode_setup->windowtype = get_bits(gb, 16); //FIXME check
  686. mode_setup->transformtype = get_bits(gb, 16); //FIXME check
  687. GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
  688. av_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
  689. i, mode_setup->blockflag, mode_setup->windowtype,
  690. mode_setup->transformtype, mode_setup->mapping);
  691. }
  692. return 0;
  693. }
  694. // Process the whole setup header using the functions above
  695. static int vorbis_parse_setup_hdr(vorbis_context *vc)
  696. {
  697. GetBitContext *gb = &vc->gb;
  698. if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
  699. (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
  700. (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
  701. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
  702. return -1;
  703. }
  704. if (vorbis_parse_setup_hdr_codebooks(vc)) {
  705. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
  706. return -2;
  707. }
  708. if (vorbis_parse_setup_hdr_tdtransforms(vc)) {
  709. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
  710. return -3;
  711. }
  712. if (vorbis_parse_setup_hdr_floors(vc)) {
  713. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
  714. return -4;
  715. }
  716. if (vorbis_parse_setup_hdr_residues(vc)) {
  717. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
  718. return -5;
  719. }
  720. if (vorbis_parse_setup_hdr_mappings(vc)) {
  721. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
  722. return -6;
  723. }
  724. if (vorbis_parse_setup_hdr_modes(vc)) {
  725. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
  726. return -7;
  727. }
  728. if (!get_bits1(gb)) {
  729. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
  730. return -8; // framing flag bit unset error
  731. }
  732. return 0;
  733. }
  734. // Process the identification header
  735. static int vorbis_parse_id_hdr(vorbis_context *vc)
  736. {
  737. GetBitContext *gb = &vc->gb;
  738. unsigned bl0, bl1;
  739. if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
  740. (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
  741. (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
  742. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
  743. return -1;
  744. }
  745. vc->version = get_bits_long(gb, 32); //FIXME check 0
  746. vc->audio_channels = get_bits(gb, 8);
  747. if (vc->audio_channels <= 0) {
  748. av_log(vc->avccontext, AV_LOG_ERROR, "Invalid number of channels\n");
  749. return -1;
  750. }
  751. vc->audio_samplerate = get_bits_long(gb, 32);
  752. if (vc->audio_samplerate <= 0) {
  753. av_log(vc->avccontext, AV_LOG_ERROR, "Invalid samplerate\n");
  754. return -1;
  755. }
  756. vc->bitrate_maximum = get_bits_long(gb, 32);
  757. vc->bitrate_nominal = get_bits_long(gb, 32);
  758. vc->bitrate_minimum = get_bits_long(gb, 32);
  759. bl0 = get_bits(gb, 4);
  760. bl1 = get_bits(gb, 4);
  761. vc->blocksize[0] = (1 << bl0);
  762. vc->blocksize[1] = (1 << bl1);
  763. if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
  764. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
  765. return -3;
  766. }
  767. // output format int16
  768. if (vc->blocksize[1] / 2 * vc->audio_channels * 2 > AVCODEC_MAX_AUDIO_FRAME_SIZE) {
  769. av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis channel count makes "
  770. "output packets too large.\n");
  771. return -4;
  772. }
  773. vc->win[0] = ff_vorbis_vwin[bl0 - 6];
  774. vc->win[1] = ff_vorbis_vwin[bl1 - 6];
  775. if ((get_bits1(gb)) == 0) {
  776. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
  777. return -2;
  778. }
  779. vc->channel_residues = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(*vc->channel_residues));
  780. vc->channel_floors = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(*vc->channel_floors));
  781. vc->saved = av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(*vc->saved));
  782. vc->previous_window = 0;
  783. ff_mdct_init(&vc->mdct[0], bl0, 1, -vc->scale_bias);
  784. ff_mdct_init(&vc->mdct[1], bl1, 1, -vc->scale_bias);
  785. 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 ",
  786. vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
  787. /*
  788. BLK = vc->blocksize[0];
  789. for (i = 0; i < BLK / 2; ++i) {
  790. vc->win[0][i] = sin(0.5*3.14159265358*(sin(((float)i + 0.5) / (float)BLK*3.14159265358))*(sin(((float)i + 0.5) / (float)BLK*3.14159265358)));
  791. }
  792. */
  793. return 0;
  794. }
  795. // Process the extradata using the functions above (identification header, setup header)
  796. static av_cold int vorbis_decode_init(AVCodecContext *avccontext)
  797. {
  798. vorbis_context *vc = avccontext->priv_data ;
  799. uint8_t *headers = avccontext->extradata;
  800. int headers_len = avccontext->extradata_size;
  801. uint8_t *header_start[3];
  802. int header_len[3];
  803. GetBitContext *gb = &(vc->gb);
  804. int hdr_type;
  805. vc->avccontext = avccontext;
  806. dsputil_init(&vc->dsp, avccontext);
  807. ff_fmt_convert_init(&vc->fmt_conv, avccontext);
  808. if (avccontext->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
  809. avccontext->sample_fmt = AV_SAMPLE_FMT_FLT;
  810. vc->scale_bias = 1.0f;
  811. } else {
  812. avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
  813. vc->scale_bias = 32768.0f;
  814. }
  815. if (!headers_len) {
  816. av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
  817. return -1;
  818. }
  819. if (ff_split_xiph_headers(headers, headers_len, 30, header_start, header_len) < 0) {
  820. av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
  821. return -1;
  822. }
  823. init_get_bits(gb, header_start[0], header_len[0]*8);
  824. hdr_type = get_bits(gb, 8);
  825. if (hdr_type != 1) {
  826. av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
  827. return -1;
  828. }
  829. if (vorbis_parse_id_hdr(vc)) {
  830. av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
  831. vorbis_free(vc);
  832. return -1;
  833. }
  834. init_get_bits(gb, header_start[2], header_len[2]*8);
  835. hdr_type = get_bits(gb, 8);
  836. if (hdr_type != 5) {
  837. av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
  838. vorbis_free(vc);
  839. return -1;
  840. }
  841. if (vorbis_parse_setup_hdr(vc)) {
  842. av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
  843. vorbis_free(vc);
  844. return -1;
  845. }
  846. if (vc->audio_channels > 8)
  847. avccontext->channel_layout = 0;
  848. else
  849. avccontext->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
  850. avccontext->channels = vc->audio_channels;
  851. avccontext->sample_rate = vc->audio_samplerate;
  852. avccontext->frame_size = FFMIN(vc->blocksize[0], vc->blocksize[1]) >> 2;
  853. return 0 ;
  854. }
  855. // Decode audiopackets -------------------------------------------------
  856. // Read and decode floor
  857. static int vorbis_floor0_decode(vorbis_context *vc,
  858. vorbis_floor_data *vfu, float *vec)
  859. {
  860. vorbis_floor0 *vf = &vfu->t0;
  861. float *lsp = vf->lsp;
  862. unsigned amplitude, book_idx;
  863. unsigned blockflag = vc->modes[vc->mode_number].blockflag;
  864. amplitude = get_bits(&vc->gb, vf->amplitude_bits);
  865. if (amplitude > 0) {
  866. float last = 0;
  867. unsigned idx, lsp_len = 0;
  868. vorbis_codebook codebook;
  869. book_idx = get_bits(&vc->gb, ilog(vf->num_books));
  870. if (book_idx >= vf->num_books) {
  871. av_log(vc->avccontext, AV_LOG_ERROR,
  872. "floor0 dec: booknumber too high!\n");
  873. book_idx = 0;
  874. }
  875. av_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx);
  876. codebook = vc->codebooks[vf->book_list[book_idx]];
  877. /* Invalid codebook! */
  878. if (!codebook.codevectors)
  879. return -1;
  880. while (lsp_len<vf->order) {
  881. int vec_off;
  882. av_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions);
  883. av_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth);
  884. /* read temp vector */
  885. vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
  886. codebook.nb_bits, codebook.maxdepth)
  887. * codebook.dimensions;
  888. av_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off);
  889. /* copy each vector component and add last to it */
  890. for (idx = 0; idx < codebook.dimensions; ++idx)
  891. lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
  892. last = lsp[lsp_len+idx-1]; /* set last to last vector component */
  893. lsp_len += codebook.dimensions;
  894. }
  895. /* DEBUG: output lsp coeffs */
  896. {
  897. int idx;
  898. for (idx = 0; idx < lsp_len; ++idx)
  899. av_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
  900. }
  901. /* synthesize floor output vector */
  902. {
  903. int i;
  904. int order = vf->order;
  905. float wstep = M_PI / vf->bark_map_size;
  906. for (i = 0; i < order; i++)
  907. lsp[i] = 2.0f * cos(lsp[i]);
  908. av_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n",
  909. vf->map_size[blockflag], order, wstep);
  910. i = 0;
  911. while (i < vf->map_size[blockflag]) {
  912. int j, iter_cond = vf->map[blockflag][i];
  913. float p = 0.5f;
  914. float q = 0.5f;
  915. float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
  916. /* similar part for the q and p products */
  917. for (j = 0; j + 1 < order; j += 2) {
  918. q *= lsp[j] - two_cos_w;
  919. p *= lsp[j + 1] - two_cos_w;
  920. }
  921. if (j == order) { // even order
  922. p *= p * (2.0f - two_cos_w);
  923. q *= q * (2.0f + two_cos_w);
  924. } else { // odd order
  925. q *= two_cos_w-lsp[j]; // one more time for q
  926. /* final step and square */
  927. p *= p * (4.f - two_cos_w * two_cos_w);
  928. q *= q;
  929. }
  930. /* calculate linear floor value */
  931. q = exp((((amplitude*vf->amplitude_offset) /
  932. (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
  933. - vf->amplitude_offset) * .11512925f);
  934. /* fill vector */
  935. do {
  936. vec[i] = q; ++i;
  937. } while (vf->map[blockflag][i] == iter_cond);
  938. }
  939. }
  940. } else {
  941. /* this channel is unused */
  942. return 1;
  943. }
  944. av_dlog(NULL, " Floor0 decoded\n");
  945. return 0;
  946. }
  947. static int vorbis_floor1_decode(vorbis_context *vc,
  948. vorbis_floor_data *vfu, float *vec)
  949. {
  950. vorbis_floor1 *vf = &vfu->t1;
  951. GetBitContext *gb = &vc->gb;
  952. uint16_t range_v[4] = { 256, 128, 86, 64 };
  953. unsigned range = range_v[vf->multiplier - 1];
  954. uint16_t floor1_Y[258];
  955. uint16_t floor1_Y_final[258];
  956. int floor1_flag[258];
  957. unsigned class, cdim, cbits, csub, cval, offset, i, j;
  958. int book, adx, ady, dy, off, predicted, err;
  959. if (!get_bits1(gb)) // silence
  960. return 1;
  961. // Read values (or differences) for the floor's points
  962. floor1_Y[0] = get_bits(gb, ilog(range - 1));
  963. floor1_Y[1] = get_bits(gb, ilog(range - 1));
  964. av_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
  965. offset = 2;
  966. for (i = 0; i < vf->partitions; ++i) {
  967. class = vf->partition_class[i];
  968. cdim = vf->class_dimensions[class];
  969. cbits = vf->class_subclasses[class];
  970. csub = (1 << cbits) - 1;
  971. cval = 0;
  972. av_dlog(NULL, "Cbits %u\n", cbits);
  973. if (cbits) // this reads all subclasses for this partition's class
  974. cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[class]].vlc.table,
  975. vc->codebooks[vf->class_masterbook[class]].nb_bits, 3);
  976. for (j = 0; j < cdim; ++j) {
  977. book = vf->subclass_books[class][cval & csub];
  978. av_dlog(NULL, "book %d Cbits %u cval %u bits:%d\n",
  979. book, cbits, cval, get_bits_count(gb));
  980. cval = cval >> cbits;
  981. if (book > -1) {
  982. floor1_Y[offset+j] = get_vlc2(gb, vc->codebooks[book].vlc.table,
  983. vc->codebooks[book].nb_bits, 3);
  984. } else {
  985. floor1_Y[offset+j] = 0;
  986. }
  987. av_dlog(NULL, " floor(%d) = %d \n",
  988. vf->list[offset+j].x, floor1_Y[offset+j]);
  989. }
  990. offset+=cdim;
  991. }
  992. // Amplitude calculation from the differences
  993. floor1_flag[0] = 1;
  994. floor1_flag[1] = 1;
  995. floor1_Y_final[0] = floor1_Y[0];
  996. floor1_Y_final[1] = floor1_Y[1];
  997. for (i = 2; i < vf->x_list_dim; ++i) {
  998. unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
  999. low_neigh_offs = vf->list[i].low;
  1000. high_neigh_offs = vf->list[i].high;
  1001. dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin
  1002. adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
  1003. ady = FFABS(dy);
  1004. err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
  1005. off = err / adx;
  1006. if (dy < 0) {
  1007. predicted = floor1_Y_final[low_neigh_offs] - off;
  1008. } else {
  1009. predicted = floor1_Y_final[low_neigh_offs] + off;
  1010. } // render_point end
  1011. val = floor1_Y[i];
  1012. highroom = range-predicted;
  1013. lowroom = predicted;
  1014. if (highroom < lowroom) {
  1015. room = highroom * 2;
  1016. } else {
  1017. room = lowroom * 2; // SPEC mispelling
  1018. }
  1019. if (val) {
  1020. floor1_flag[low_neigh_offs] = 1;
  1021. floor1_flag[high_neigh_offs] = 1;
  1022. floor1_flag[i] = 1;
  1023. if (val >= room) {
  1024. if (highroom > lowroom) {
  1025. floor1_Y_final[i] = val - lowroom + predicted;
  1026. } else {
  1027. floor1_Y_final[i] = predicted - val + highroom - 1;
  1028. }
  1029. } else {
  1030. if (val & 1) {
  1031. floor1_Y_final[i] = predicted - (val + 1) / 2;
  1032. } else {
  1033. floor1_Y_final[i] = predicted + val / 2;
  1034. }
  1035. }
  1036. } else {
  1037. floor1_flag[i] = 0;
  1038. floor1_Y_final[i] = predicted;
  1039. }
  1040. av_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
  1041. vf->list[i].x, floor1_Y_final[i], val);
  1042. }
  1043. // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
  1044. ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
  1045. av_dlog(NULL, " Floor decoded\n");
  1046. return 0;
  1047. }
  1048. // Read and decode residue
  1049. static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
  1050. vorbis_residue *vr,
  1051. unsigned ch,
  1052. uint8_t *do_not_decode,
  1053. float *vec,
  1054. unsigned vlen,
  1055. int vr_type)
  1056. {
  1057. GetBitContext *gb = &vc->gb;
  1058. unsigned c_p_c = vc->codebooks[vr->classbook].dimensions;
  1059. unsigned ptns_to_read = vr->ptns_to_read;
  1060. uint8_t *classifs = vr->classifs;
  1061. unsigned pass, ch_used, i, j, k, l;
  1062. if (vr_type == 2) {
  1063. for (j = 1; j < ch; ++j)
  1064. do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input
  1065. if (do_not_decode[0])
  1066. return 0;
  1067. ch_used = 1;
  1068. } else {
  1069. ch_used = ch;
  1070. }
  1071. av_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
  1072. for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
  1073. uint16_t voffset, partition_count, j_times_ptns_to_read;
  1074. voffset = vr->begin;
  1075. for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error
  1076. if (!pass) {
  1077. unsigned inverse_class = ff_inverse[vr->classifications];
  1078. for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
  1079. if (!do_not_decode[j]) {
  1080. unsigned temp = get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table,
  1081. vc->codebooks[vr->classbook].nb_bits, 3);
  1082. av_dlog(NULL, "Classword: %u\n", temp);
  1083. assert(vr->classifications > 1 && temp <= 65536); //needed for inverse[]
  1084. for (i = 0; i < c_p_c; ++i) {
  1085. unsigned temp2;
  1086. temp2 = (((uint64_t)temp) * inverse_class) >> 32;
  1087. if (partition_count + c_p_c - 1 - i < ptns_to_read)
  1088. classifs[j_times_ptns_to_read + partition_count + c_p_c - 1 - i] = temp - temp2 * vr->classifications;
  1089. temp = temp2;
  1090. }
  1091. }
  1092. j_times_ptns_to_read += ptns_to_read;
  1093. }
  1094. }
  1095. for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
  1096. for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
  1097. unsigned voffs;
  1098. if (!do_not_decode[j]) {
  1099. unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
  1100. int vqbook = vr->books[vqclass][pass];
  1101. if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
  1102. unsigned coffs;
  1103. unsigned dim = vc->codebooks[vqbook].dimensions;
  1104. unsigned step = dim == 1 ? vr->partition_size
  1105. : FASTDIV(vr->partition_size, dim);
  1106. vorbis_codebook codebook = vc->codebooks[vqbook];
  1107. if (vr_type == 0) {
  1108. voffs = voffset+j*vlen;
  1109. for (k = 0; k < step; ++k) {
  1110. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1111. for (l = 0; l < dim; ++l)
  1112. vec[voffs + k + l * step] += codebook.codevectors[coffs + l]; // FPMATH
  1113. }
  1114. } else if (vr_type == 1) {
  1115. voffs = voffset + j * vlen;
  1116. for (k = 0; k < step; ++k) {
  1117. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1118. for (l = 0; l < dim; ++l, ++voffs) {
  1119. vec[voffs]+=codebook.codevectors[coffs+l]; // FPMATH
  1120. av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
  1121. pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
  1122. }
  1123. }
  1124. } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
  1125. voffs = voffset >> 1;
  1126. if (dim == 2) {
  1127. for (k = 0; k < step; ++k) {
  1128. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
  1129. vec[voffs + k ] += codebook.codevectors[coffs ]; // FPMATH
  1130. vec[voffs + k + vlen] += codebook.codevectors[coffs + 1]; // FPMATH
  1131. }
  1132. } else if (dim == 4) {
  1133. for (k = 0; k < step; ++k, voffs += 2) {
  1134. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
  1135. vec[voffs ] += codebook.codevectors[coffs ]; // FPMATH
  1136. vec[voffs + 1 ] += codebook.codevectors[coffs + 2]; // FPMATH
  1137. vec[voffs + vlen ] += codebook.codevectors[coffs + 1]; // FPMATH
  1138. vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3]; // FPMATH
  1139. }
  1140. } else
  1141. for (k = 0; k < step; ++k) {
  1142. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1143. for (l = 0; l < dim; l += 2, voffs++) {
  1144. vec[voffs ] += codebook.codevectors[coffs + l ]; // FPMATH
  1145. vec[voffs + vlen] += codebook.codevectors[coffs + l + 1]; // FPMATH
  1146. av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
  1147. pass, voffset / ch + (voffs % ch) * vlen,
  1148. vec[voffset / ch + (voffs % ch) * vlen],
  1149. codebook.codevectors[coffs + l], coffs, l);
  1150. }
  1151. }
  1152. } else if (vr_type == 2) {
  1153. voffs = voffset;
  1154. for (k = 0; k < step; ++k) {
  1155. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1156. for (l = 0; l < dim; ++l, ++voffs) {
  1157. vec[voffs / ch + (voffs % ch) * vlen] += codebook.codevectors[coffs + l]; // FPMATH FIXME use if and counter instead of / and %
  1158. av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
  1159. pass, voffset / ch + (voffs % ch) * vlen,
  1160. vec[voffset / ch + (voffs % ch) * vlen],
  1161. codebook.codevectors[coffs + l], coffs, l);
  1162. }
  1163. }
  1164. }
  1165. }
  1166. }
  1167. j_times_ptns_to_read += ptns_to_read;
  1168. }
  1169. ++partition_count;
  1170. voffset += vr->partition_size;
  1171. }
  1172. }
  1173. }
  1174. return 0;
  1175. }
  1176. static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
  1177. unsigned ch,
  1178. uint8_t *do_not_decode,
  1179. float *vec, unsigned vlen)
  1180. {
  1181. if (vr->type == 2)
  1182. return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 2);
  1183. else if (vr->type == 1)
  1184. return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 1);
  1185. else if (vr->type == 0)
  1186. return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 0);
  1187. else {
  1188. av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
  1189. return -1;
  1190. }
  1191. }
  1192. void vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
  1193. {
  1194. int i;
  1195. for (i = 0; i < blocksize; i++) {
  1196. if (mag[i] > 0.0) {
  1197. if (ang[i] > 0.0) {
  1198. ang[i] = mag[i] - ang[i];
  1199. } else {
  1200. float temp = ang[i];
  1201. ang[i] = mag[i];
  1202. mag[i] += temp;
  1203. }
  1204. } else {
  1205. if (ang[i] > 0.0) {
  1206. ang[i] += mag[i];
  1207. } else {
  1208. float temp = ang[i];
  1209. ang[i] = mag[i];
  1210. mag[i] -= temp;
  1211. }
  1212. }
  1213. }
  1214. }
  1215. // Decode the audio packet using the functions above
  1216. static int vorbis_parse_audio_packet(vorbis_context *vc)
  1217. {
  1218. GetBitContext *gb = &vc->gb;
  1219. FFTContext *mdct;
  1220. unsigned previous_window = vc->previous_window;
  1221. unsigned mode_number, blockflag, blocksize;
  1222. int i, j;
  1223. uint8_t no_residue[255];
  1224. uint8_t do_not_decode[255];
  1225. vorbis_mapping *mapping;
  1226. float *ch_res_ptr = vc->channel_residues;
  1227. float *ch_floor_ptr = vc->channel_floors;
  1228. uint8_t res_chan[255];
  1229. unsigned res_num = 0;
  1230. int retlen = 0;
  1231. if (get_bits1(gb)) {
  1232. av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
  1233. return -1; // packet type not audio
  1234. }
  1235. if (vc->mode_count == 1) {
  1236. mode_number = 0;
  1237. } else {
  1238. GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
  1239. }
  1240. vc->mode_number = mode_number;
  1241. mapping = &vc->mappings[vc->modes[mode_number].mapping];
  1242. av_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
  1243. vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
  1244. blockflag = vc->modes[mode_number].blockflag;
  1245. blocksize = vc->blocksize[blockflag];
  1246. if (blockflag)
  1247. skip_bits(gb, 2); // previous_window, next_window
  1248. memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2); //FIXME can this be removed ?
  1249. memset(ch_floor_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2); //FIXME can this be removed ?
  1250. // Decode floor
  1251. for (i = 0; i < vc->audio_channels; ++i) {
  1252. vorbis_floor *floor;
  1253. int ret;
  1254. if (mapping->submaps > 1) {
  1255. floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
  1256. } else {
  1257. floor = &vc->floors[mapping->submap_floor[0]];
  1258. }
  1259. ret = floor->decode(vc, &floor->data, ch_floor_ptr);
  1260. if (ret < 0) {
  1261. av_log(vc->avccontext, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
  1262. return -1;
  1263. }
  1264. no_residue[i] = ret;
  1265. ch_floor_ptr += blocksize / 2;
  1266. }
  1267. // Nonzero vector propagate
  1268. for (i = mapping->coupling_steps - 1; i >= 0; --i) {
  1269. if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
  1270. no_residue[mapping->magnitude[i]] = 0;
  1271. no_residue[mapping->angle[i]] = 0;
  1272. }
  1273. }
  1274. // Decode residue
  1275. for (i = 0; i < mapping->submaps; ++i) {
  1276. vorbis_residue *residue;
  1277. unsigned ch = 0;
  1278. for (j = 0; j < vc->audio_channels; ++j) {
  1279. if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
  1280. res_chan[j] = res_num;
  1281. if (no_residue[j]) {
  1282. do_not_decode[ch] = 1;
  1283. } else {
  1284. do_not_decode[ch] = 0;
  1285. }
  1286. ++ch;
  1287. ++res_num;
  1288. }
  1289. }
  1290. residue = &vc->residues[mapping->submap_residue[i]];
  1291. vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, blocksize/2);
  1292. ch_res_ptr += ch * blocksize / 2;
  1293. }
  1294. // Inverse coupling
  1295. for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
  1296. float *mag, *ang;
  1297. mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
  1298. ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2;
  1299. vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
  1300. }
  1301. // Dotproduct, MDCT
  1302. mdct = &vc->mdct[blockflag];
  1303. for (j = vc->audio_channels-1;j >= 0; j--) {
  1304. ch_floor_ptr = vc->channel_floors + j * blocksize / 2;
  1305. ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
  1306. vc->dsp.vector_fmul(ch_floor_ptr, ch_floor_ptr, ch_res_ptr, blocksize / 2);
  1307. mdct->imdct_half(mdct, ch_res_ptr, ch_floor_ptr);
  1308. }
  1309. // Overlap/add, save data for next overlapping FPMATH
  1310. retlen = (blocksize + vc->blocksize[previous_window]) / 4;
  1311. for (j = 0; j < vc->audio_channels; j++) {
  1312. unsigned bs0 = vc->blocksize[0];
  1313. unsigned bs1 = vc->blocksize[1];
  1314. float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
  1315. float *saved = vc->saved + j * bs1 / 4;
  1316. float *ret = vc->channel_floors + j * retlen;
  1317. float *buf = residue;
  1318. const float *win = vc->win[blockflag & previous_window];
  1319. if (blockflag == previous_window) {
  1320. vc->dsp.vector_fmul_window(ret, saved, buf, win, blocksize / 4);
  1321. } else if (blockflag > previous_window) {
  1322. vc->dsp.vector_fmul_window(ret, saved, buf, win, bs0 / 4);
  1323. memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
  1324. } else {
  1325. memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
  1326. vc->dsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
  1327. }
  1328. memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
  1329. }
  1330. vc->previous_window = blockflag;
  1331. return retlen;
  1332. }
  1333. // Return the decoded audio packet through the standard api
  1334. static int vorbis_decode_frame(AVCodecContext *avccontext,
  1335. void *data, int *data_size,
  1336. AVPacket *avpkt)
  1337. {
  1338. const uint8_t *buf = avpkt->data;
  1339. int buf_size = avpkt->size;
  1340. vorbis_context *vc = avccontext->priv_data ;
  1341. GetBitContext *gb = &(vc->gb);
  1342. const float *channel_ptrs[255];
  1343. int i, len;
  1344. if (!buf_size)
  1345. return 0;
  1346. av_dlog(NULL, "packet length %d \n", buf_size);
  1347. init_get_bits(gb, buf, buf_size*8);
  1348. len = vorbis_parse_audio_packet(vc);
  1349. if (len <= 0) {
  1350. *data_size = 0;
  1351. return buf_size;
  1352. }
  1353. if (!vc->first_frame) {
  1354. vc->first_frame = 1;
  1355. *data_size = 0;
  1356. return buf_size ;
  1357. }
  1358. av_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
  1359. get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
  1360. if (vc->audio_channels > 8) {
  1361. for (i = 0; i < vc->audio_channels; i++)
  1362. channel_ptrs[i] = vc->channel_floors + i * len;
  1363. } else {
  1364. for (i = 0; i < vc->audio_channels; i++)
  1365. channel_ptrs[i] = vc->channel_floors +
  1366. len * ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
  1367. }
  1368. if (avccontext->sample_fmt == AV_SAMPLE_FMT_FLT)
  1369. vc->fmt_conv.float_interleave(data, channel_ptrs, len, vc->audio_channels);
  1370. else
  1371. vc->fmt_conv.float_to_int16_interleave(data, channel_ptrs, len,
  1372. vc->audio_channels);
  1373. *data_size = len * vc->audio_channels *
  1374. av_get_bytes_per_sample(avccontext->sample_fmt);
  1375. return buf_size ;
  1376. }
  1377. // Close decoder
  1378. static av_cold int vorbis_decode_close(AVCodecContext *avccontext)
  1379. {
  1380. vorbis_context *vc = avccontext->priv_data;
  1381. vorbis_free(vc);
  1382. return 0 ;
  1383. }
  1384. AVCodec ff_vorbis_decoder = {
  1385. .name = "vorbis",
  1386. .type = AVMEDIA_TYPE_AUDIO,
  1387. .id = CODEC_ID_VORBIS,
  1388. .priv_data_size = sizeof(vorbis_context),
  1389. .init = vorbis_decode_init,
  1390. .close = vorbis_decode_close,
  1391. .decode = vorbis_decode_frame,
  1392. .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
  1393. .channel_layouts = ff_vorbis_channel_layouts,
  1394. .sample_fmts = (const enum AVSampleFormat[]) {
  1395. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
  1396. },
  1397. };