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