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.

1776 lines
65KB

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