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.

1717 lines
63KB

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