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.

1677 lines
61KB

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