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.

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