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.

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