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.

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