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.

1677 lines
61KB

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