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.

1750 lines
65KB

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