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.

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