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.

1686 lines
62KB

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