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.

1760 lines
64KB

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