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.

1854 lines
67KB

  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 BITSTREAM_READER_LE
  30. #include "libavutil/float_dsp.h"
  31. #include "libavutil/avassert.h"
  32. #include "avcodec.h"
  33. #include "get_bits.h"
  34. #include "fft.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. rangemax = (1 << rangebits);
  477. if (rangemax > vc->blocksize[1] / 2) {
  478. av_log(vc->avctx, AV_LOG_ERROR,
  479. "Floor value is too large for blocksize: %u (%"PRIu32")\n",
  480. rangemax, vc->blocksize[1] / 2);
  481. return AVERROR_INVALIDDATA;
  482. }
  483. floor_setup->data.t1.list[0].x = 0;
  484. floor_setup->data.t1.list[1].x = rangemax;
  485. for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
  486. for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
  487. floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits);
  488. ff_dlog(NULL, " %u. floor1 Y coord. %d\n", floor1_values,
  489. floor_setup->data.t1.list[floor1_values].x);
  490. }
  491. }
  492. // Precalculate order of x coordinates - needed for decode
  493. if (ff_vorbis_ready_floor1_list(vc->avctx,
  494. floor_setup->data.t1.list,
  495. floor_setup->data.t1.x_list_dim)) {
  496. return AVERROR_INVALIDDATA;
  497. }
  498. } else if (floor_setup->floor_type == 0) {
  499. unsigned max_codebook_dim = 0;
  500. floor_setup->decode = vorbis_floor0_decode;
  501. floor_setup->data.t0.order = get_bits(gb, 8);
  502. if (!floor_setup->data.t0.order) {
  503. av_log(vc->avctx, AV_LOG_ERROR, "Floor 0 order is 0.\n");
  504. return AVERROR_INVALIDDATA;
  505. }
  506. floor_setup->data.t0.rate = get_bits(gb, 16);
  507. if (!floor_setup->data.t0.rate) {
  508. av_log(vc->avctx, AV_LOG_ERROR, "Floor 0 rate is 0.\n");
  509. return AVERROR_INVALIDDATA;
  510. }
  511. floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
  512. if (!floor_setup->data.t0.bark_map_size) {
  513. av_log(vc->avctx, AV_LOG_ERROR,
  514. "Floor 0 bark map size is 0.\n");
  515. return AVERROR_INVALIDDATA;
  516. }
  517. floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
  518. floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
  519. floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
  520. /* allocate mem for booklist */
  521. floor_setup->data.t0.book_list =
  522. av_malloc(floor_setup->data.t0.num_books);
  523. if (!floor_setup->data.t0.book_list)
  524. return AVERROR(ENOMEM);
  525. /* read book indexes */
  526. {
  527. int idx;
  528. unsigned book_idx;
  529. for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
  530. GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
  531. floor_setup->data.t0.book_list[idx] = book_idx;
  532. if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
  533. max_codebook_dim = vc->codebooks[book_idx].dimensions;
  534. }
  535. }
  536. if ((ret = create_map(vc, i)) < 0)
  537. return ret;
  538. /* codebook dim is for padding if codebook dim doesn't *
  539. * divide order+1 then we need to read more data */
  540. floor_setup->data.t0.lsp =
  541. av_malloc_array((floor_setup->data.t0.order + 1 + max_codebook_dim),
  542. sizeof(*floor_setup->data.t0.lsp));
  543. if (!floor_setup->data.t0.lsp)
  544. return AVERROR(ENOMEM);
  545. /* debug output parsed headers */
  546. ff_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order);
  547. ff_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate);
  548. ff_dlog(NULL, "floor0 bark map size: %u\n",
  549. floor_setup->data.t0.bark_map_size);
  550. ff_dlog(NULL, "floor0 amplitude bits: %u\n",
  551. floor_setup->data.t0.amplitude_bits);
  552. ff_dlog(NULL, "floor0 amplitude offset: %u\n",
  553. floor_setup->data.t0.amplitude_offset);
  554. ff_dlog(NULL, "floor0 number of books: %u\n",
  555. floor_setup->data.t0.num_books);
  556. ff_dlog(NULL, "floor0 book list pointer: %p\n",
  557. floor_setup->data.t0.book_list);
  558. {
  559. int idx;
  560. for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
  561. ff_dlog(NULL, " Book %d: %u\n", idx + 1,
  562. floor_setup->data.t0.book_list[idx]);
  563. }
  564. }
  565. } else {
  566. av_log(vc->avctx, AV_LOG_ERROR, "Invalid floor type!\n");
  567. return AVERROR_INVALIDDATA;
  568. }
  569. }
  570. return 0;
  571. }
  572. // Process residues part
  573. static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
  574. {
  575. GetBitContext *gb = &vc->gb;
  576. unsigned i, j, k;
  577. vc->residue_count = get_bits(gb, 6)+1;
  578. vc->residues = av_mallocz(vc->residue_count * sizeof(*vc->residues));
  579. if (!vc->residues)
  580. return AVERROR(ENOMEM);
  581. ff_dlog(NULL, " There are %d residues. \n", vc->residue_count);
  582. for (i = 0; i < vc->residue_count; ++i) {
  583. vorbis_residue *res_setup = &vc->residues[i];
  584. uint8_t cascade[64];
  585. unsigned high_bits, low_bits;
  586. res_setup->type = get_bits(gb, 16);
  587. ff_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
  588. res_setup->begin = get_bits(gb, 24);
  589. res_setup->end = get_bits(gb, 24);
  590. res_setup->partition_size = get_bits(gb, 24) + 1;
  591. /* Validations to prevent a buffer overflow later. */
  592. if (res_setup->begin>res_setup->end ||
  593. (res_setup->end-res_setup->begin) / res_setup->partition_size > FFMIN(V_MAX_PARTITIONS, 65535)) {
  594. av_log(vc->avctx, AV_LOG_ERROR,
  595. "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
  596. res_setup->type, res_setup->begin, res_setup->end,
  597. res_setup->partition_size, vc->blocksize[1] / 2);
  598. return AVERROR_INVALIDDATA;
  599. }
  600. res_setup->classifications = get_bits(gb, 6) + 1;
  601. GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
  602. res_setup->ptns_to_read =
  603. (res_setup->end - res_setup->begin) / res_setup->partition_size;
  604. res_setup->classifs = av_malloc_array(res_setup->ptns_to_read,
  605. vc->audio_channels *
  606. sizeof(*res_setup->classifs));
  607. if (!res_setup->classifs)
  608. return AVERROR(ENOMEM);
  609. ff_dlog(NULL, " begin %d end %d part.size %d classif.s %d classbook %d \n",
  610. res_setup->begin, res_setup->end, res_setup->partition_size,
  611. res_setup->classifications, res_setup->classbook);
  612. for (j = 0; j < res_setup->classifications; ++j) {
  613. high_bits = 0;
  614. low_bits = get_bits(gb, 3);
  615. if (get_bits1(gb))
  616. high_bits = get_bits(gb, 5);
  617. cascade[j] = (high_bits << 3) + low_bits;
  618. ff_dlog(NULL, " %u class cascade depth: %d\n", j, ilog(cascade[j]));
  619. }
  620. res_setup->maxpass = 0;
  621. for (j = 0; j < res_setup->classifications; ++j) {
  622. for (k = 0; k < 8; ++k) {
  623. if (cascade[j]&(1 << k)) {
  624. GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
  625. ff_dlog(NULL, " %u class cascade depth %u book: %d\n",
  626. j, k, res_setup->books[j][k]);
  627. if (k>res_setup->maxpass)
  628. res_setup->maxpass = k;
  629. } else {
  630. res_setup->books[j][k] = -1;
  631. }
  632. }
  633. }
  634. }
  635. return 0;
  636. }
  637. // Process mappings part
  638. static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
  639. {
  640. GetBitContext *gb = &vc->gb;
  641. unsigned i, j;
  642. vc->mapping_count = get_bits(gb, 6)+1;
  643. vc->mappings = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
  644. if (!vc->mappings)
  645. return AVERROR(ENOMEM);
  646. ff_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
  647. for (i = 0; i < vc->mapping_count; ++i) {
  648. vorbis_mapping *mapping_setup = &vc->mappings[i];
  649. if (get_bits(gb, 16)) {
  650. av_log(vc->avctx, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
  651. return AVERROR_INVALIDDATA;
  652. }
  653. if (get_bits1(gb)) {
  654. mapping_setup->submaps = get_bits(gb, 4) + 1;
  655. } else {
  656. mapping_setup->submaps = 1;
  657. }
  658. if (get_bits1(gb)) {
  659. mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
  660. mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps *
  661. sizeof(*mapping_setup->magnitude));
  662. mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps *
  663. sizeof(*mapping_setup->angle));
  664. if (!mapping_setup->angle || !mapping_setup->magnitude)
  665. return AVERROR(ENOMEM);
  666. for (j = 0; j < mapping_setup->coupling_steps; ++j) {
  667. GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
  668. GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
  669. }
  670. } else {
  671. mapping_setup->coupling_steps = 0;
  672. }
  673. ff_dlog(NULL, " %u mapping coupling steps: %d\n",
  674. i, mapping_setup->coupling_steps);
  675. if (get_bits(gb, 2)) {
  676. av_log(vc->avctx, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i);
  677. return AVERROR_INVALIDDATA; // following spec.
  678. }
  679. if (mapping_setup->submaps>1) {
  680. mapping_setup->mux = av_mallocz_array(vc->audio_channels,
  681. sizeof(*mapping_setup->mux));
  682. if (!mapping_setup->mux)
  683. return AVERROR(ENOMEM);
  684. for (j = 0; j < vc->audio_channels; ++j)
  685. mapping_setup->mux[j] = get_bits(gb, 4);
  686. }
  687. for (j = 0; j < mapping_setup->submaps; ++j) {
  688. skip_bits(gb, 8); // FIXME check?
  689. GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
  690. GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
  691. ff_dlog(NULL, " %u mapping %u submap : floor %d, residue %d\n", i, j,
  692. mapping_setup->submap_floor[j],
  693. mapping_setup->submap_residue[j]);
  694. }
  695. }
  696. return 0;
  697. }
  698. // Process modes part
  699. static int create_map(vorbis_context *vc, unsigned floor_number)
  700. {
  701. vorbis_floor *floors = vc->floors;
  702. vorbis_floor0 *vf;
  703. int idx;
  704. int blockflag, n;
  705. int32_t *map;
  706. for (blockflag = 0; blockflag < 2; ++blockflag) {
  707. n = vc->blocksize[blockflag] / 2;
  708. floors[floor_number].data.t0.map[blockflag] =
  709. av_malloc_array(n + 1, sizeof(int32_t)); // n + sentinel
  710. if (!floors[floor_number].data.t0.map[blockflag])
  711. return AVERROR(ENOMEM);
  712. map = floors[floor_number].data.t0.map[blockflag];
  713. vf = &floors[floor_number].data.t0;
  714. for (idx = 0; idx < n; ++idx) {
  715. map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
  716. (vf->bark_map_size / BARK(vf->rate / 2.0f)));
  717. if (vf->bark_map_size-1 < map[idx])
  718. map[idx] = vf->bark_map_size - 1;
  719. }
  720. map[n] = -1;
  721. vf->map_size[blockflag] = n;
  722. }
  723. for (idx = 0; idx <= n; ++idx) {
  724. ff_dlog(NULL, "floor0 map: map at pos %d is %d\n", idx, map[idx]);
  725. }
  726. return 0;
  727. }
  728. static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
  729. {
  730. GetBitContext *gb = &vc->gb;
  731. unsigned i;
  732. vc->mode_count = get_bits(gb, 6) + 1;
  733. vc->modes = av_mallocz(vc->mode_count * sizeof(*vc->modes));
  734. if (!vc->modes)
  735. return AVERROR(ENOMEM);
  736. ff_dlog(NULL, " There are %d modes.\n", vc->mode_count);
  737. for (i = 0; i < vc->mode_count; ++i) {
  738. vorbis_mode *mode_setup = &vc->modes[i];
  739. mode_setup->blockflag = get_bits1(gb);
  740. mode_setup->windowtype = get_bits(gb, 16); //FIXME check
  741. mode_setup->transformtype = get_bits(gb, 16); //FIXME check
  742. GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
  743. ff_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
  744. i, mode_setup->blockflag, mode_setup->windowtype,
  745. mode_setup->transformtype, mode_setup->mapping);
  746. }
  747. return 0;
  748. }
  749. // Process the whole setup header using the functions above
  750. static int vorbis_parse_setup_hdr(vorbis_context *vc)
  751. {
  752. GetBitContext *gb = &vc->gb;
  753. int ret;
  754. if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
  755. (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
  756. (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
  757. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
  758. return AVERROR_INVALIDDATA;
  759. }
  760. if ((ret = vorbis_parse_setup_hdr_codebooks(vc))) {
  761. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
  762. return ret;
  763. }
  764. if ((ret = vorbis_parse_setup_hdr_tdtransforms(vc))) {
  765. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
  766. return ret;
  767. }
  768. if ((ret = vorbis_parse_setup_hdr_floors(vc))) {
  769. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
  770. return ret;
  771. }
  772. if ((ret = vorbis_parse_setup_hdr_residues(vc))) {
  773. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
  774. return ret;
  775. }
  776. if ((ret = vorbis_parse_setup_hdr_mappings(vc))) {
  777. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
  778. return ret;
  779. }
  780. if ((ret = vorbis_parse_setup_hdr_modes(vc))) {
  781. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
  782. return ret;
  783. }
  784. if (!get_bits1(gb)) {
  785. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
  786. return AVERROR_INVALIDDATA; // framing flag bit unset error
  787. }
  788. return 0;
  789. }
  790. // Process the identification header
  791. static int vorbis_parse_id_hdr(vorbis_context *vc)
  792. {
  793. GetBitContext *gb = &vc->gb;
  794. unsigned bl0, bl1;
  795. if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
  796. (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
  797. (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
  798. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
  799. return AVERROR_INVALIDDATA;
  800. }
  801. vc->version = get_bits_long(gb, 32); //FIXME check 0
  802. vc->audio_channels = get_bits(gb, 8);
  803. if (vc->audio_channels <= 0) {
  804. av_log(vc->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  805. return AVERROR_INVALIDDATA;
  806. }
  807. vc->audio_samplerate = get_bits_long(gb, 32);
  808. if (vc->audio_samplerate <= 0) {
  809. av_log(vc->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
  810. return AVERROR_INVALIDDATA;
  811. }
  812. vc->bitrate_maximum = get_bits_long(gb, 32);
  813. vc->bitrate_nominal = get_bits_long(gb, 32);
  814. vc->bitrate_minimum = get_bits_long(gb, 32);
  815. bl0 = get_bits(gb, 4);
  816. bl1 = get_bits(gb, 4);
  817. if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
  818. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
  819. return AVERROR_INVALIDDATA;
  820. }
  821. vc->blocksize[0] = (1 << bl0);
  822. vc->blocksize[1] = (1 << bl1);
  823. vc->win[0] = ff_vorbis_vwin[bl0 - 6];
  824. vc->win[1] = ff_vorbis_vwin[bl1 - 6];
  825. if ((get_bits1(gb)) == 0) {
  826. av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
  827. return AVERROR_INVALIDDATA;
  828. }
  829. vc->channel_residues = av_malloc_array(vc->blocksize[1] / 2, vc->audio_channels * sizeof(*vc->channel_residues));
  830. vc->saved = av_mallocz_array(vc->blocksize[1] / 4, vc->audio_channels * sizeof(*vc->saved));
  831. if (!vc->channel_residues || !vc->saved)
  832. return AVERROR(ENOMEM);
  833. vc->previous_window = -1;
  834. ff_mdct_init(&vc->mdct[0], bl0, 1, -1.0);
  835. ff_mdct_init(&vc->mdct[1], bl1, 1, -1.0);
  836. vc->fdsp = avpriv_float_dsp_alloc(vc->avctx->flags & AV_CODEC_FLAG_BITEXACT);
  837. if (!vc->fdsp)
  838. return AVERROR(ENOMEM);
  839. 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 ",
  840. vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
  841. /*
  842. BLK = vc->blocksize[0];
  843. for (i = 0; i < BLK / 2; ++i) {
  844. 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)));
  845. }
  846. */
  847. return 0;
  848. }
  849. // Process the extradata using the functions above (identification header, setup header)
  850. static av_cold int vorbis_decode_init(AVCodecContext *avctx)
  851. {
  852. vorbis_context *vc = avctx->priv_data;
  853. uint8_t *headers = avctx->extradata;
  854. int headers_len = avctx->extradata_size;
  855. const uint8_t *header_start[3];
  856. int header_len[3];
  857. GetBitContext *gb = &vc->gb;
  858. int hdr_type, ret;
  859. vc->avctx = avctx;
  860. ff_vorbisdsp_init(&vc->dsp);
  861. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  862. if (!headers_len) {
  863. av_log(avctx, AV_LOG_ERROR, "Extradata missing.\n");
  864. return AVERROR_INVALIDDATA;
  865. }
  866. if ((ret = avpriv_split_xiph_headers(headers, headers_len, 30, header_start, header_len)) < 0) {
  867. av_log(avctx, AV_LOG_ERROR, "Extradata corrupt.\n");
  868. return ret;
  869. }
  870. init_get_bits(gb, header_start[0], header_len[0]*8);
  871. hdr_type = get_bits(gb, 8);
  872. if (hdr_type != 1) {
  873. av_log(avctx, AV_LOG_ERROR, "First header is not the id header.\n");
  874. return AVERROR_INVALIDDATA;
  875. }
  876. if ((ret = vorbis_parse_id_hdr(vc))) {
  877. av_log(avctx, AV_LOG_ERROR, "Id header corrupt.\n");
  878. vorbis_free(vc);
  879. return ret;
  880. }
  881. init_get_bits(gb, header_start[2], header_len[2]*8);
  882. hdr_type = get_bits(gb, 8);
  883. if (hdr_type != 5) {
  884. av_log(avctx, AV_LOG_ERROR, "Third header is not the setup header.\n");
  885. vorbis_free(vc);
  886. return AVERROR_INVALIDDATA;
  887. }
  888. if ((ret = vorbis_parse_setup_hdr(vc))) {
  889. av_log(avctx, AV_LOG_ERROR, "Setup header corrupt.\n");
  890. vorbis_free(vc);
  891. return ret;
  892. }
  893. if (vc->audio_channels > 8)
  894. avctx->channel_layout = 0;
  895. else
  896. avctx->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
  897. avctx->channels = vc->audio_channels;
  898. avctx->sample_rate = vc->audio_samplerate;
  899. return 0;
  900. }
  901. // Decode audiopackets -------------------------------------------------
  902. // Read and decode floor
  903. static int vorbis_floor0_decode(vorbis_context *vc,
  904. vorbis_floor_data *vfu, float *vec)
  905. {
  906. vorbis_floor0 *vf = &vfu->t0;
  907. float *lsp = vf->lsp;
  908. unsigned amplitude, book_idx;
  909. unsigned blockflag = vc->modes[vc->mode_number].blockflag;
  910. if (!vf->amplitude_bits)
  911. return 1;
  912. amplitude = get_bits(&vc->gb, vf->amplitude_bits);
  913. if (amplitude > 0) {
  914. float last = 0;
  915. unsigned idx, lsp_len = 0;
  916. vorbis_codebook codebook;
  917. book_idx = get_bits(&vc->gb, ilog(vf->num_books));
  918. if (book_idx >= vf->num_books) {
  919. av_log(vc->avctx, AV_LOG_ERROR, "floor0 dec: booknumber too high!\n");
  920. book_idx = 0;
  921. }
  922. ff_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx);
  923. codebook = vc->codebooks[vf->book_list[book_idx]];
  924. /* Invalid codebook! */
  925. if (!codebook.codevectors)
  926. return AVERROR_INVALIDDATA;
  927. while (lsp_len<vf->order) {
  928. int vec_off;
  929. ff_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions);
  930. ff_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth);
  931. /* read temp vector */
  932. vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
  933. codebook.nb_bits, codebook.maxdepth)
  934. * codebook.dimensions;
  935. ff_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off);
  936. /* copy each vector component and add last to it */
  937. for (idx = 0; idx < codebook.dimensions; ++idx)
  938. lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
  939. last = lsp[lsp_len+idx-1]; /* set last to last vector component */
  940. lsp_len += codebook.dimensions;
  941. }
  942. /* DEBUG: output lsp coeffs */
  943. {
  944. int idx;
  945. for (idx = 0; idx < lsp_len; ++idx)
  946. ff_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
  947. }
  948. /* synthesize floor output vector */
  949. {
  950. int i;
  951. int order = vf->order;
  952. float wstep = M_PI / vf->bark_map_size;
  953. for (i = 0; i < order; i++)
  954. lsp[i] = 2.0f * cos(lsp[i]);
  955. ff_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n",
  956. vf->map_size[blockflag], order, wstep);
  957. i = 0;
  958. while (i < vf->map_size[blockflag]) {
  959. int j, iter_cond = vf->map[blockflag][i];
  960. float p = 0.5f;
  961. float q = 0.5f;
  962. float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
  963. /* similar part for the q and p products */
  964. for (j = 0; j + 1 < order; j += 2) {
  965. q *= lsp[j] - two_cos_w;
  966. p *= lsp[j + 1] - two_cos_w;
  967. }
  968. if (j == order) { // even order
  969. p *= p * (2.0f - two_cos_w);
  970. q *= q * (2.0f + two_cos_w);
  971. } else { // odd order
  972. q *= two_cos_w-lsp[j]; // one more time for q
  973. /* final step and square */
  974. p *= p * (4.f - two_cos_w * two_cos_w);
  975. q *= q;
  976. }
  977. /* calculate linear floor value */
  978. q = exp((((amplitude*vf->amplitude_offset) /
  979. (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
  980. - vf->amplitude_offset) * .11512925f);
  981. /* fill vector */
  982. do {
  983. vec[i] = q; ++i;
  984. } while (vf->map[blockflag][i] == iter_cond);
  985. }
  986. }
  987. } else {
  988. /* this channel is unused */
  989. return 1;
  990. }
  991. ff_dlog(NULL, " Floor0 decoded\n");
  992. return 0;
  993. }
  994. static int vorbis_floor1_decode(vorbis_context *vc,
  995. vorbis_floor_data *vfu, float *vec)
  996. {
  997. vorbis_floor1 *vf = &vfu->t1;
  998. GetBitContext *gb = &vc->gb;
  999. uint16_t range_v[4] = { 256, 128, 86, 64 };
  1000. unsigned range = range_v[vf->multiplier - 1];
  1001. uint16_t floor1_Y[258];
  1002. uint16_t floor1_Y_final[258];
  1003. int floor1_flag[258];
  1004. unsigned partition_class, cdim, cbits, csub, cval, offset, i, j;
  1005. int book, adx, ady, dy, off, predicted, err;
  1006. if (!get_bits1(gb)) // silence
  1007. return 1;
  1008. // Read values (or differences) for the floor's points
  1009. floor1_Y[0] = get_bits(gb, ilog(range - 1));
  1010. floor1_Y[1] = get_bits(gb, ilog(range - 1));
  1011. ff_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
  1012. offset = 2;
  1013. for (i = 0; i < vf->partitions; ++i) {
  1014. partition_class = vf->partition_class[i];
  1015. cdim = vf->class_dimensions[partition_class];
  1016. cbits = vf->class_subclasses[partition_class];
  1017. csub = (1 << cbits) - 1;
  1018. cval = 0;
  1019. ff_dlog(NULL, "Cbits %u\n", cbits);
  1020. if (cbits) // this reads all subclasses for this partition's class
  1021. cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[partition_class]].vlc.table,
  1022. vc->codebooks[vf->class_masterbook[partition_class]].nb_bits, 3);
  1023. for (j = 0; j < cdim; ++j) {
  1024. book = vf->subclass_books[partition_class][cval & csub];
  1025. ff_dlog(NULL, "book %d Cbits %u cval %u bits:%d\n",
  1026. book, cbits, cval, get_bits_count(gb));
  1027. cval = cval >> cbits;
  1028. if (book > -1) {
  1029. int v = get_vlc2(gb, vc->codebooks[book].vlc.table,
  1030. vc->codebooks[book].nb_bits, 3);
  1031. if (v < 0)
  1032. return AVERROR_INVALIDDATA;
  1033. floor1_Y[offset+j] = v;
  1034. } else {
  1035. floor1_Y[offset+j] = 0;
  1036. }
  1037. ff_dlog(NULL, " floor(%d) = %d \n",
  1038. vf->list[offset+j].x, floor1_Y[offset+j]);
  1039. }
  1040. offset+=cdim;
  1041. }
  1042. // Amplitude calculation from the differences
  1043. floor1_flag[0] = 1;
  1044. floor1_flag[1] = 1;
  1045. floor1_Y_final[0] = floor1_Y[0];
  1046. floor1_Y_final[1] = floor1_Y[1];
  1047. for (i = 2; i < vf->x_list_dim; ++i) {
  1048. unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
  1049. low_neigh_offs = vf->list[i].low;
  1050. high_neigh_offs = vf->list[i].high;
  1051. dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin
  1052. adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
  1053. ady = FFABS(dy);
  1054. err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
  1055. off = err / adx;
  1056. if (dy < 0) {
  1057. predicted = floor1_Y_final[low_neigh_offs] - off;
  1058. } else {
  1059. predicted = floor1_Y_final[low_neigh_offs] + off;
  1060. } // render_point end
  1061. val = floor1_Y[i];
  1062. highroom = range-predicted;
  1063. lowroom = predicted;
  1064. if (highroom < lowroom) {
  1065. room = highroom * 2;
  1066. } else {
  1067. room = lowroom * 2; // SPEC misspelling
  1068. }
  1069. if (val) {
  1070. floor1_flag[low_neigh_offs] = 1;
  1071. floor1_flag[high_neigh_offs] = 1;
  1072. floor1_flag[i] = 1;
  1073. if (val >= room) {
  1074. if (highroom > lowroom) {
  1075. floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
  1076. } else {
  1077. floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
  1078. }
  1079. } else {
  1080. if (val & 1) {
  1081. floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
  1082. } else {
  1083. floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
  1084. }
  1085. }
  1086. } else {
  1087. floor1_flag[i] = 0;
  1088. floor1_Y_final[i] = av_clip_uint16(predicted);
  1089. }
  1090. ff_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
  1091. vf->list[i].x, floor1_Y_final[i], val);
  1092. }
  1093. // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
  1094. ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
  1095. ff_dlog(NULL, " Floor decoded\n");
  1096. return 0;
  1097. }
  1098. static av_always_inline int setup_classifs(vorbis_context *vc,
  1099. vorbis_residue *vr,
  1100. uint8_t *do_not_decode,
  1101. unsigned ch_used,
  1102. int partition_count,
  1103. int ptns_to_read
  1104. )
  1105. {
  1106. vorbis_codebook *codebook = vc->codebooks + vr->classbook;
  1107. int p, j, i;
  1108. unsigned c_p_c = codebook->dimensions;
  1109. unsigned inverse_class = ff_inverse[vr->classifications];
  1110. int temp, temp2;
  1111. for (p = 0, j = 0; j < ch_used; ++j) {
  1112. if (!do_not_decode[j]) {
  1113. temp = get_vlc2(&vc->gb, codebook->vlc.table,
  1114. codebook->nb_bits, 3);
  1115. ff_dlog(NULL, "Classword: %u\n", temp);
  1116. av_assert0(temp < 65536);
  1117. if (temp < 0) {
  1118. av_log(vc->avctx, AV_LOG_ERROR,
  1119. "Invalid vlc code decoding %d channel.", j);
  1120. return AVERROR_INVALIDDATA;
  1121. }
  1122. av_assert0(vr->classifications > 1); //needed for inverse[]
  1123. for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
  1124. temp2 = (((uint64_t)temp) * inverse_class) >> 32;
  1125. if (i < ptns_to_read)
  1126. vr->classifs[p + i] = temp - temp2 * vr->classifications;
  1127. temp = temp2;
  1128. }
  1129. }
  1130. p += ptns_to_read;
  1131. }
  1132. return 0;
  1133. }
  1134. // Read and decode residue
  1135. static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
  1136. vorbis_residue *vr,
  1137. unsigned ch,
  1138. uint8_t *do_not_decode,
  1139. float *vec,
  1140. unsigned vlen,
  1141. unsigned ch_left,
  1142. int vr_type)
  1143. {
  1144. GetBitContext *gb = &vc->gb;
  1145. unsigned c_p_c = vc->codebooks[vr->classbook].dimensions;
  1146. uint8_t *classifs = vr->classifs;
  1147. unsigned pass, ch_used, i, j, k, l;
  1148. unsigned max_output = (ch - 1) * vlen;
  1149. int ptns_to_read = vr->ptns_to_read;
  1150. int libvorbis_bug = 0;
  1151. if (vr_type == 2) {
  1152. for (j = 1; j < ch; ++j)
  1153. do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input
  1154. if (do_not_decode[0])
  1155. return 0;
  1156. ch_used = 1;
  1157. max_output += vr->end / ch;
  1158. } else {
  1159. ch_used = ch;
  1160. max_output += vr->end;
  1161. }
  1162. if (max_output > ch_left * vlen) {
  1163. if (max_output <= ch_left * vlen + vr->partition_size*ch_used/ch) {
  1164. ptns_to_read--;
  1165. libvorbis_bug = 1;
  1166. } else {
  1167. av_log(vc->avctx, AV_LOG_ERROR, "Insufficient output buffer\n");
  1168. return AVERROR_INVALIDDATA;
  1169. }
  1170. }
  1171. ff_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
  1172. for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
  1173. int voffset, partition_count, j_times_ptns_to_read;
  1174. voffset = vr->begin;
  1175. for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error
  1176. if (!pass) {
  1177. int ret = setup_classifs(vc, vr, do_not_decode, ch_used, partition_count, ptns_to_read);
  1178. if (ret < 0)
  1179. return ret;
  1180. }
  1181. for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
  1182. for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
  1183. unsigned voffs;
  1184. if (!do_not_decode[j]) {
  1185. unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
  1186. int vqbook = vr->books[vqclass][pass];
  1187. if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
  1188. unsigned coffs;
  1189. unsigned dim = vc->codebooks[vqbook].dimensions;
  1190. unsigned step = FASTDIV(vr->partition_size << 1, dim << 1);
  1191. vorbis_codebook codebook = vc->codebooks[vqbook];
  1192. if (vr_type == 0) {
  1193. voffs = voffset+j*vlen;
  1194. for (k = 0; k < step; ++k) {
  1195. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1196. for (l = 0; l < dim; ++l)
  1197. vec[voffs + k + l * step] += codebook.codevectors[coffs + l];
  1198. }
  1199. } else if (vr_type == 1) {
  1200. voffs = voffset + j * vlen;
  1201. for (k = 0; k < step; ++k) {
  1202. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1203. for (l = 0; l < dim; ++l, ++voffs) {
  1204. vec[voffs]+=codebook.codevectors[coffs+l];
  1205. ff_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
  1206. pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
  1207. }
  1208. }
  1209. } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
  1210. voffs = voffset >> 1;
  1211. if (dim == 2) {
  1212. for (k = 0; k < step; ++k) {
  1213. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
  1214. vec[voffs + k ] += codebook.codevectors[coffs ];
  1215. vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];
  1216. }
  1217. } else if (dim == 4) {
  1218. for (k = 0; k < step; ++k, voffs += 2) {
  1219. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
  1220. vec[voffs ] += codebook.codevectors[coffs ];
  1221. vec[voffs + 1 ] += codebook.codevectors[coffs + 2];
  1222. vec[voffs + vlen ] += codebook.codevectors[coffs + 1];
  1223. vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];
  1224. }
  1225. } else
  1226. for (k = 0; k < step; ++k) {
  1227. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1228. for (l = 0; l < dim; l += 2, voffs++) {
  1229. vec[voffs ] += codebook.codevectors[coffs + l ];
  1230. vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];
  1231. ff_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
  1232. pass, voffset / ch + (voffs % ch) * vlen,
  1233. vec[voffset / ch + (voffs % ch) * vlen],
  1234. codebook.codevectors[coffs + l], coffs, l);
  1235. }
  1236. }
  1237. } else if (vr_type == 2) {
  1238. unsigned voffs_div = FASTDIV(voffset << 1, ch <<1);
  1239. unsigned voffs_mod = voffset - voffs_div * ch;
  1240. for (k = 0; k < step; ++k) {
  1241. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1242. for (l = 0; l < dim; ++l) {
  1243. vec[voffs_div + voffs_mod * vlen] +=
  1244. codebook.codevectors[coffs + l];
  1245. ff_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
  1246. pass, voffs_div + voffs_mod * vlen,
  1247. vec[voffs_div + voffs_mod * vlen],
  1248. codebook.codevectors[coffs + l], coffs, l);
  1249. if (++voffs_mod == ch) {
  1250. voffs_div++;
  1251. voffs_mod = 0;
  1252. }
  1253. }
  1254. }
  1255. }
  1256. }
  1257. }
  1258. j_times_ptns_to_read += ptns_to_read;
  1259. }
  1260. ++partition_count;
  1261. voffset += vr->partition_size;
  1262. }
  1263. }
  1264. if (libvorbis_bug && !pass) {
  1265. for (j = 0; j < ch_used; ++j) {
  1266. if (!do_not_decode[j]) {
  1267. get_vlc2(&vc->gb, vc->codebooks[vr->classbook].vlc.table,
  1268. vc->codebooks[vr->classbook].nb_bits, 3);
  1269. }
  1270. }
  1271. }
  1272. }
  1273. return 0;
  1274. }
  1275. static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
  1276. unsigned ch,
  1277. uint8_t *do_not_decode,
  1278. float *vec, unsigned vlen,
  1279. unsigned ch_left)
  1280. {
  1281. if (vr->type == 2)
  1282. return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
  1283. else if (vr->type == 1)
  1284. return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
  1285. else if (vr->type == 0)
  1286. return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0);
  1287. else {
  1288. av_log(vc->avctx, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
  1289. return AVERROR_INVALIDDATA;
  1290. }
  1291. }
  1292. void ff_vorbis_inverse_coupling(float *mag, float *ang, intptr_t blocksize)
  1293. {
  1294. int i;
  1295. for (i = 0; i < blocksize; i++) {
  1296. if (mag[i] > 0.0) {
  1297. if (ang[i] > 0.0) {
  1298. ang[i] = mag[i] - ang[i];
  1299. } else {
  1300. float temp = ang[i];
  1301. ang[i] = mag[i];
  1302. mag[i] += temp;
  1303. }
  1304. } else {
  1305. if (ang[i] > 0.0) {
  1306. ang[i] += mag[i];
  1307. } else {
  1308. float temp = ang[i];
  1309. ang[i] = mag[i];
  1310. mag[i] -= temp;
  1311. }
  1312. }
  1313. }
  1314. }
  1315. // Decode the audio packet using the functions above
  1316. static int vorbis_parse_audio_packet(vorbis_context *vc, float **floor_ptr)
  1317. {
  1318. GetBitContext *gb = &vc->gb;
  1319. FFTContext *mdct;
  1320. int previous_window = vc->previous_window;
  1321. unsigned mode_number, blockflag, blocksize;
  1322. int i, j;
  1323. uint8_t no_residue[255];
  1324. uint8_t do_not_decode[255];
  1325. vorbis_mapping *mapping;
  1326. float *ch_res_ptr = vc->channel_residues;
  1327. uint8_t res_chan[255];
  1328. unsigned res_num = 0;
  1329. int retlen = 0;
  1330. unsigned ch_left = vc->audio_channels;
  1331. unsigned vlen;
  1332. if (get_bits1(gb)) {
  1333. av_log(vc->avctx, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
  1334. return AVERROR_INVALIDDATA; // packet type not audio
  1335. }
  1336. if (vc->mode_count == 1) {
  1337. mode_number = 0;
  1338. } else {
  1339. GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
  1340. }
  1341. vc->mode_number = mode_number;
  1342. mapping = &vc->mappings[vc->modes[mode_number].mapping];
  1343. ff_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
  1344. vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
  1345. blockflag = vc->modes[mode_number].blockflag;
  1346. blocksize = vc->blocksize[blockflag];
  1347. vlen = blocksize / 2;
  1348. if (blockflag) {
  1349. int code = get_bits(gb, 2);
  1350. if (previous_window < 0)
  1351. previous_window = code>>1;
  1352. } else if (previous_window < 0)
  1353. previous_window = 0;
  1354. memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
  1355. for (i = 0; i < vc->audio_channels; ++i)
  1356. memset(floor_ptr[i], 0, vlen * sizeof(floor_ptr[0][0])); //FIXME can this be removed ?
  1357. // Decode floor
  1358. for (i = 0; i < vc->audio_channels; ++i) {
  1359. vorbis_floor *floor;
  1360. int ret;
  1361. if (mapping->submaps > 1) {
  1362. floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
  1363. } else {
  1364. floor = &vc->floors[mapping->submap_floor[0]];
  1365. }
  1366. ret = floor->decode(vc, &floor->data, floor_ptr[i]);
  1367. if (ret < 0) {
  1368. av_log(vc->avctx, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
  1369. return AVERROR_INVALIDDATA;
  1370. }
  1371. no_residue[i] = ret;
  1372. }
  1373. // Nonzero vector propagate
  1374. for (i = mapping->coupling_steps - 1; i >= 0; --i) {
  1375. if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
  1376. no_residue[mapping->magnitude[i]] = 0;
  1377. no_residue[mapping->angle[i]] = 0;
  1378. }
  1379. }
  1380. // Decode residue
  1381. for (i = 0; i < mapping->submaps; ++i) {
  1382. vorbis_residue *residue;
  1383. unsigned ch = 0;
  1384. int ret;
  1385. for (j = 0; j < vc->audio_channels; ++j) {
  1386. if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
  1387. res_chan[j] = res_num;
  1388. if (no_residue[j]) {
  1389. do_not_decode[ch] = 1;
  1390. } else {
  1391. do_not_decode[ch] = 0;
  1392. }
  1393. ++ch;
  1394. ++res_num;
  1395. }
  1396. }
  1397. residue = &vc->residues[mapping->submap_residue[i]];
  1398. if (ch_left < ch) {
  1399. av_log(vc->avctx, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n");
  1400. return AVERROR_INVALIDDATA;
  1401. }
  1402. if (ch) {
  1403. ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
  1404. if (ret < 0)
  1405. return ret;
  1406. }
  1407. ch_res_ptr += ch * vlen;
  1408. ch_left -= ch;
  1409. }
  1410. if (ch_left > 0)
  1411. return AVERROR_INVALIDDATA;
  1412. // Inverse coupling
  1413. for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
  1414. float *mag, *ang;
  1415. mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
  1416. ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2;
  1417. vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
  1418. }
  1419. // Dotproduct, MDCT
  1420. mdct = &vc->mdct[blockflag];
  1421. for (j = vc->audio_channels-1;j >= 0; j--) {
  1422. ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
  1423. vc->fdsp->vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
  1424. mdct->imdct_half(mdct, ch_res_ptr, floor_ptr[j]);
  1425. }
  1426. // Overlap/add, save data for next overlapping
  1427. retlen = (blocksize + vc->blocksize[previous_window]) / 4;
  1428. for (j = 0; j < vc->audio_channels; j++) {
  1429. unsigned bs0 = vc->blocksize[0];
  1430. unsigned bs1 = vc->blocksize[1];
  1431. float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
  1432. float *saved = vc->saved + j * bs1 / 4;
  1433. float *ret = floor_ptr[j];
  1434. float *buf = residue;
  1435. const float *win = vc->win[blockflag & previous_window];
  1436. if (blockflag == previous_window) {
  1437. vc->fdsp->vector_fmul_window(ret, saved, buf, win, blocksize / 4);
  1438. } else if (blockflag > previous_window) {
  1439. vc->fdsp->vector_fmul_window(ret, saved, buf, win, bs0 / 4);
  1440. memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
  1441. } else {
  1442. memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
  1443. vc->fdsp->vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
  1444. }
  1445. memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
  1446. }
  1447. vc->previous_window = blockflag;
  1448. return retlen;
  1449. }
  1450. // Return the decoded audio packet through the standard api
  1451. static int vorbis_decode_frame(AVCodecContext *avctx, void *data,
  1452. int *got_frame_ptr, AVPacket *avpkt)
  1453. {
  1454. const uint8_t *buf = avpkt->data;
  1455. int buf_size = avpkt->size;
  1456. vorbis_context *vc = avctx->priv_data;
  1457. AVFrame *frame = data;
  1458. GetBitContext *gb = &vc->gb;
  1459. float *channel_ptrs[255];
  1460. int i, len, ret;
  1461. ff_dlog(NULL, "packet length %d \n", buf_size);
  1462. if (*buf == 1 && buf_size > 7) {
  1463. init_get_bits(gb, buf+1, buf_size*8 - 8);
  1464. vorbis_free(vc);
  1465. if ((ret = vorbis_parse_id_hdr(vc))) {
  1466. av_log(avctx, AV_LOG_ERROR, "Id header corrupt.\n");
  1467. vorbis_free(vc);
  1468. return ret;
  1469. }
  1470. if (vc->audio_channels > 8)
  1471. avctx->channel_layout = 0;
  1472. else
  1473. avctx->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
  1474. avctx->channels = vc->audio_channels;
  1475. avctx->sample_rate = vc->audio_samplerate;
  1476. return buf_size;
  1477. }
  1478. if (*buf == 3 && buf_size > 7) {
  1479. av_log(avctx, AV_LOG_DEBUG, "Ignoring comment header\n");
  1480. return buf_size;
  1481. }
  1482. if (*buf == 5 && buf_size > 7 && vc->channel_residues && !vc->modes) {
  1483. init_get_bits(gb, buf+1, buf_size*8 - 8);
  1484. if ((ret = vorbis_parse_setup_hdr(vc))) {
  1485. av_log(avctx, AV_LOG_ERROR, "Setup header corrupt.\n");
  1486. vorbis_free(vc);
  1487. return ret;
  1488. }
  1489. return buf_size;
  1490. }
  1491. if (!vc->channel_residues || !vc->modes) {
  1492. av_log(avctx, AV_LOG_ERROR, "Data packet before valid headers\n");
  1493. return AVERROR_INVALIDDATA;
  1494. }
  1495. /* get output buffer */
  1496. frame->nb_samples = vc->blocksize[1] / 2;
  1497. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1498. return ret;
  1499. if (vc->audio_channels > 8) {
  1500. for (i = 0; i < vc->audio_channels; i++)
  1501. channel_ptrs[i] = (float *)frame->extended_data[i];
  1502. } else {
  1503. for (i = 0; i < vc->audio_channels; i++) {
  1504. int ch = ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
  1505. channel_ptrs[ch] = (float *)frame->extended_data[i];
  1506. }
  1507. }
  1508. init_get_bits(gb, buf, buf_size*8);
  1509. if ((len = vorbis_parse_audio_packet(vc, channel_ptrs)) <= 0)
  1510. return len;
  1511. if (!vc->first_frame) {
  1512. vc->first_frame = 1;
  1513. *got_frame_ptr = 0;
  1514. av_frame_unref(frame);
  1515. return buf_size;
  1516. }
  1517. ff_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
  1518. get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
  1519. frame->nb_samples = len;
  1520. *got_frame_ptr = 1;
  1521. return buf_size;
  1522. }
  1523. // Close decoder
  1524. static av_cold int vorbis_decode_close(AVCodecContext *avctx)
  1525. {
  1526. vorbis_context *vc = avctx->priv_data;
  1527. vorbis_free(vc);
  1528. return 0;
  1529. }
  1530. static av_cold void vorbis_decode_flush(AVCodecContext *avctx)
  1531. {
  1532. vorbis_context *vc = avctx->priv_data;
  1533. if (vc->saved) {
  1534. memset(vc->saved, 0, (vc->blocksize[1] / 4) * vc->audio_channels *
  1535. sizeof(*vc->saved));
  1536. }
  1537. vc->previous_window = -1;
  1538. vc->first_frame = 0;
  1539. }
  1540. AVCodec ff_vorbis_decoder = {
  1541. .name = "vorbis",
  1542. .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
  1543. .type = AVMEDIA_TYPE_AUDIO,
  1544. .id = AV_CODEC_ID_VORBIS,
  1545. .priv_data_size = sizeof(vorbis_context),
  1546. .init = vorbis_decode_init,
  1547. .close = vorbis_decode_close,
  1548. .decode = vorbis_decode_frame,
  1549. .flush = vorbis_decode_flush,
  1550. .capabilities = AV_CODEC_CAP_DR1,
  1551. .channel_layouts = ff_vorbis_channel_layouts,
  1552. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  1553. AV_SAMPLE_FMT_NONE },
  1554. };