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.

1746 lines
64KB

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