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.

1681 lines
62KB

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