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.

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