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.

1729 lines
64KB

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