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.

1869 lines
68KB

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