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.

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