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.

1777 lines
65KB

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