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.

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