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.

1659 lines
60KB

  1. /**
  2. * @file libavcodec/vorbis_dec.c
  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. uint_fast8_t (* 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 uint_fast8_t 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 uint_fast8_t 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_fast16_t floor1_values = 2;
  397. floor_setup->decode = vorbis_floor1_decode;
  398. floor_setup->data.t1.partitions = get_bits(gb, 5);
  399. AV_DEBUG(" %d.floor: %d partitions \n", i, floor_setup->data.t1.partitions);
  400. for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
  401. floor_setup->data.t1.partition_class[j] = get_bits(gb, 4);
  402. if (floor_setup->data.t1.partition_class[j] > maximum_class)
  403. maximum_class = floor_setup->data.t1.partition_class[j];
  404. AV_DEBUG(" %d. floor %d partition class %d \n", i, j, floor_setup->data.t1.partition_class[j]);
  405. }
  406. AV_DEBUG(" maximum class %d \n", maximum_class);
  407. floor_setup->data.t1.maximum_class = maximum_class;
  408. for (j = 0; j <= maximum_class; ++j) {
  409. floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
  410. floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
  411. 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]);
  412. if (floor_setup->data.t1.class_subclasses[j]) {
  413. GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count)
  414. AV_DEBUG(" masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
  415. }
  416. for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
  417. int16_t bits = get_bits(gb, 8) - 1;
  418. if (bits != -1)
  419. VALIDATE_INDEX(bits, vc->codebook_count)
  420. floor_setup->data.t1.subclass_books[j][k] = bits;
  421. AV_DEBUG(" book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
  422. }
  423. }
  424. floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
  425. floor_setup->data.t1.x_list_dim = 2;
  426. for (j = 0; j < floor_setup->data.t1.partitions; ++j)
  427. floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]];
  428. floor_setup->data.t1.list = av_mallocz(floor_setup->data.t1.x_list_dim * sizeof(vorbis_floor1_entry));
  429. rangebits = get_bits(gb, 4);
  430. floor_setup->data.t1.list[0].x = 0;
  431. floor_setup->data.t1.list[1].x = (1 << rangebits);
  432. for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
  433. for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
  434. floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits);
  435. AV_DEBUG(" %d. floor1 Y coord. %d \n", floor1_values, floor_setup->data.t1.list[floor1_values].x);
  436. }
  437. }
  438. // Precalculate order of x coordinates - needed for decode
  439. ff_vorbis_ready_floor1_list(floor_setup->data.t1.list, floor_setup->data.t1.x_list_dim);
  440. } else if (floor_setup->floor_type == 0) {
  441. uint_fast8_t max_codebook_dim = 0;
  442. floor_setup->decode = vorbis_floor0_decode;
  443. floor_setup->data.t0.order = get_bits(gb, 8);
  444. floor_setup->data.t0.rate = get_bits(gb, 16);
  445. floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
  446. floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
  447. /* zero would result in a div by zero later *
  448. * 2^0 - 1 == 0 */
  449. if (floor_setup->data.t0.amplitude_bits == 0) {
  450. av_log(vc->avccontext, AV_LOG_ERROR,
  451. "Floor 0 amplitude bits is 0.\n");
  452. return -1;
  453. }
  454. floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
  455. floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
  456. /* allocate mem for booklist */
  457. floor_setup->data.t0.book_list =
  458. av_malloc(floor_setup->data.t0.num_books);
  459. if (!floor_setup->data.t0.book_list)
  460. return -1;
  461. /* read book indexes */
  462. {
  463. int idx;
  464. uint_fast8_t book_idx;
  465. for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
  466. GET_VALIDATED_INDEX(floor_setup->data.t0.book_list[idx], 8, vc->codebook_count)
  467. if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
  468. max_codebook_dim = vc->codebooks[book_idx].dimensions;
  469. }
  470. }
  471. create_map(vc, i);
  472. /* allocate mem for lsp coefficients */
  473. {
  474. /* codebook dim is for padding if codebook dim doesn't *
  475. * divide order+1 then we need to read more data */
  476. floor_setup->data.t0.lsp =
  477. av_malloc((floor_setup->data.t0.order+1 + max_codebook_dim)
  478. * sizeof(float));
  479. if (!floor_setup->data.t0.lsp)
  480. return -1;
  481. }
  482. #ifdef V_DEBUG /* debug output parsed headers */
  483. AV_DEBUG("floor0 order: %u\n", floor_setup->data.t0.order);
  484. AV_DEBUG("floor0 rate: %u\n", floor_setup->data.t0.rate);
  485. AV_DEBUG("floor0 bark map size: %u\n",
  486. floor_setup->data.t0.bark_map_size);
  487. AV_DEBUG("floor0 amplitude bits: %u\n",
  488. floor_setup->data.t0.amplitude_bits);
  489. AV_DEBUG("floor0 amplitude offset: %u\n",
  490. floor_setup->data.t0.amplitude_offset);
  491. AV_DEBUG("floor0 number of books: %u\n",
  492. floor_setup->data.t0.num_books);
  493. AV_DEBUG("floor0 book list pointer: %p\n",
  494. floor_setup->data.t0.book_list);
  495. {
  496. int idx;
  497. for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
  498. AV_DEBUG(" Book %d: %u\n",
  499. idx+1,
  500. floor_setup->data.t0.book_list[idx]);
  501. }
  502. }
  503. #endif
  504. } else {
  505. av_log(vc->avccontext, AV_LOG_ERROR, "Invalid floor type!\n");
  506. return -1;
  507. }
  508. }
  509. return 0;
  510. }
  511. // Process residues part
  512. static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
  513. {
  514. GetBitContext *gb = &vc->gb;
  515. uint_fast8_t i, j, k;
  516. vc->residue_count = get_bits(gb, 6)+1;
  517. vc->residues = av_mallocz(vc->residue_count * sizeof(vorbis_residue));
  518. AV_DEBUG(" There are %d residues. \n", vc->residue_count);
  519. for (i = 0; i < vc->residue_count; ++i) {
  520. vorbis_residue *res_setup = &vc->residues[i];
  521. uint_fast8_t cascade[64];
  522. uint_fast8_t high_bits;
  523. uint_fast8_t low_bits;
  524. res_setup->type = get_bits(gb, 16);
  525. AV_DEBUG(" %d. residue type %d \n", i, res_setup->type);
  526. res_setup->begin = get_bits(gb, 24);
  527. res_setup->end = get_bits(gb, 24);
  528. res_setup->partition_size = get_bits(gb, 24) + 1;
  529. /* Validations to prevent a buffer overflow later. */
  530. if (res_setup->begin>res_setup->end ||
  531. res_setup->end>vc->blocksize[1] / (res_setup->type == 2 ? 1 : 2) ||
  532. (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
  533. 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);
  534. return -1;
  535. }
  536. res_setup->classifications = get_bits(gb, 6) + 1;
  537. GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
  538. 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,
  539. res_setup->classifications, res_setup->classbook);
  540. for (j = 0; j < res_setup->classifications; ++j) {
  541. high_bits = 0;
  542. low_bits = get_bits(gb, 3);
  543. if (get_bits1(gb))
  544. high_bits = get_bits(gb, 5);
  545. cascade[j] = (high_bits << 3) + low_bits;
  546. AV_DEBUG(" %d class casscade depth: %d \n", j, ilog(cascade[j]));
  547. }
  548. res_setup->maxpass = 0;
  549. for (j = 0; j < res_setup->classifications; ++j) {
  550. for (k = 0; k < 8; ++k) {
  551. if (cascade[j]&(1 << k)) {
  552. GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
  553. AV_DEBUG(" %d class casscade depth %d book: %d \n", j, k, res_setup->books[j][k]);
  554. if (k>res_setup->maxpass)
  555. res_setup->maxpass = k;
  556. } else {
  557. res_setup->books[j][k] = -1;
  558. }
  559. }
  560. }
  561. }
  562. return 0;
  563. }
  564. // Process mappings part
  565. static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
  566. {
  567. GetBitContext *gb = &vc->gb;
  568. uint_fast8_t i, j;
  569. vc->mapping_count = get_bits(gb, 6)+1;
  570. vc->mappings = av_mallocz(vc->mapping_count * sizeof(vorbis_mapping));
  571. AV_DEBUG(" There are %d mappings. \n", vc->mapping_count);
  572. for (i = 0; i < vc->mapping_count; ++i) {
  573. vorbis_mapping *mapping_setup = &vc->mappings[i];
  574. if (get_bits(gb, 16)) {
  575. av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
  576. return -1;
  577. }
  578. if (get_bits1(gb)) {
  579. mapping_setup->submaps = get_bits(gb, 4) + 1;
  580. } else {
  581. mapping_setup->submaps = 1;
  582. }
  583. if (get_bits1(gb)) {
  584. mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
  585. mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
  586. mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
  587. for (j = 0; j < mapping_setup->coupling_steps; ++j) {
  588. GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
  589. GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
  590. }
  591. } else {
  592. mapping_setup->coupling_steps = 0;
  593. }
  594. AV_DEBUG(" %d mapping coupling steps: %d \n", i, mapping_setup->coupling_steps);
  595. if (get_bits(gb, 2)) {
  596. av_log(vc->avccontext, AV_LOG_ERROR, "%d. mapping setup data invalid. \n", i);
  597. return -1; // following spec.
  598. }
  599. if (mapping_setup->submaps>1) {
  600. mapping_setup->mux = av_mallocz(vc->audio_channels * sizeof(uint_fast8_t));
  601. for (j = 0; j < vc->audio_channels; ++j)
  602. mapping_setup->mux[j] = get_bits(gb, 4);
  603. }
  604. for (j = 0; j < mapping_setup->submaps; ++j) {
  605. skip_bits(gb, 8); // FIXME check?
  606. GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
  607. GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
  608. AV_DEBUG(" %d mapping %d submap : floor %d, residue %d \n", i, j, mapping_setup->submap_floor[j], mapping_setup->submap_residue[j]);
  609. }
  610. }
  611. return 0;
  612. }
  613. // Process modes part
  614. static void create_map(vorbis_context *vc, uint_fast8_t floor_number)
  615. {
  616. vorbis_floor *floors = vc->floors;
  617. vorbis_floor0 *vf;
  618. int idx;
  619. int_fast8_t blockflag;
  620. int_fast32_t *map;
  621. int_fast32_t n; //TODO: could theoretically be smaller?
  622. for (blockflag = 0; blockflag < 2; ++blockflag) {
  623. n = vc->blocksize[blockflag] / 2;
  624. floors[floor_number].data.t0.map[blockflag] =
  625. av_malloc((n+1) * sizeof(int_fast32_t)); // n + sentinel
  626. map = floors[floor_number].data.t0.map[blockflag];
  627. vf = &floors[floor_number].data.t0;
  628. for (idx = 0; idx < n; ++idx) {
  629. map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
  630. ((vf->bark_map_size) /
  631. BARK(vf->rate / 2.0f)));
  632. if (vf->bark_map_size-1 < map[idx])
  633. map[idx] = vf->bark_map_size - 1;
  634. }
  635. map[n] = -1;
  636. vf->map_size[blockflag] = n;
  637. }
  638. # ifdef V_DEBUG
  639. for (idx = 0; idx <= n; ++idx) {
  640. AV_DEBUG("floor0 map: map at pos %d is %d\n",
  641. idx, map[idx]);
  642. }
  643. # endif
  644. }
  645. static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
  646. {
  647. GetBitContext *gb = &vc->gb;
  648. uint_fast8_t i;
  649. vc->mode_count = get_bits(gb, 6) + 1;
  650. vc->modes = av_mallocz(vc->mode_count * sizeof(vorbis_mode));
  651. AV_DEBUG(" There are %d modes.\n", vc->mode_count);
  652. for (i = 0; i < vc->mode_count; ++i) {
  653. vorbis_mode *mode_setup = &vc->modes[i];
  654. mode_setup->blockflag = get_bits1(gb);
  655. mode_setup->windowtype = get_bits(gb, 16); //FIXME check
  656. mode_setup->transformtype = get_bits(gb, 16); //FIXME check
  657. GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
  658. 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);
  659. }
  660. return 0;
  661. }
  662. // Process the whole setup header using the functions above
  663. static int vorbis_parse_setup_hdr(vorbis_context *vc)
  664. {
  665. GetBitContext *gb = &vc->gb;
  666. if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
  667. (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
  668. (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
  669. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
  670. return -1;
  671. }
  672. if (vorbis_parse_setup_hdr_codebooks(vc)) {
  673. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
  674. return -2;
  675. }
  676. if (vorbis_parse_setup_hdr_tdtransforms(vc)) {
  677. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
  678. return -3;
  679. }
  680. if (vorbis_parse_setup_hdr_floors(vc)) {
  681. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
  682. return -4;
  683. }
  684. if (vorbis_parse_setup_hdr_residues(vc)) {
  685. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
  686. return -5;
  687. }
  688. if (vorbis_parse_setup_hdr_mappings(vc)) {
  689. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
  690. return -6;
  691. }
  692. if (vorbis_parse_setup_hdr_modes(vc)) {
  693. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
  694. return -7;
  695. }
  696. if (!get_bits1(gb)) {
  697. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
  698. return -8; // framing flag bit unset error
  699. }
  700. return 0;
  701. }
  702. // Process the identification header
  703. static int vorbis_parse_id_hdr(vorbis_context *vc)
  704. {
  705. GetBitContext *gb = &vc->gb;
  706. uint_fast8_t bl0, bl1;
  707. if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
  708. (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
  709. (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
  710. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
  711. return -1;
  712. }
  713. vc->version = get_bits_long(gb, 32); //FIXME check 0
  714. vc->audio_channels = get_bits(gb, 8);
  715. if (vc->audio_channels <= 0) {
  716. av_log(vc->avccontext, AV_LOG_ERROR, "Invalid number of channels\n");
  717. return -1;
  718. }
  719. vc->audio_samplerate = get_bits_long(gb, 32);
  720. if (vc->audio_samplerate <= 0) {
  721. av_log(vc->avccontext, AV_LOG_ERROR, "Invalid samplerate\n");
  722. return -1;
  723. }
  724. vc->bitrate_maximum = get_bits_long(gb, 32);
  725. vc->bitrate_nominal = get_bits_long(gb, 32);
  726. vc->bitrate_minimum = get_bits_long(gb, 32);
  727. bl0 = get_bits(gb, 4);
  728. bl1 = get_bits(gb, 4);
  729. vc->blocksize[0] = (1 << bl0);
  730. vc->blocksize[1] = (1 << bl1);
  731. if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
  732. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
  733. return -3;
  734. }
  735. // output format int16
  736. if (vc->blocksize[1] / 2 * vc->audio_channels * 2 > AVCODEC_MAX_AUDIO_FRAME_SIZE) {
  737. av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis channel count makes "
  738. "output packets too large.\n");
  739. return -4;
  740. }
  741. vc->win[0] = ff_vorbis_vwin[bl0 - 6];
  742. vc->win[1] = ff_vorbis_vwin[bl1 - 6];
  743. if ((get_bits1(gb)) == 0) {
  744. av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
  745. return -2;
  746. }
  747. vc->channel_residues = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(float));
  748. vc->channel_floors = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(float));
  749. vc->saved = av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(float));
  750. vc->previous_window = 0;
  751. ff_mdct_init(&vc->mdct[0], bl0, 1, vc->exp_bias ? -(1 << 15) : -1.0);
  752. ff_mdct_init(&vc->mdct[1], bl1, 1, vc->exp_bias ? -(1 << 15) : -1.0);
  753. 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 ",
  754. vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
  755. /*
  756. BLK = vc->blocksize[0];
  757. for (i = 0; i < BLK / 2; ++i) {
  758. 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)));
  759. }
  760. */
  761. return 0;
  762. }
  763. // Process the extradata using the functions above (identification header, setup header)
  764. static av_cold int vorbis_decode_init(AVCodecContext *avccontext)
  765. {
  766. vorbis_context *vc = avccontext->priv_data ;
  767. uint8_t *headers = avccontext->extradata;
  768. int headers_len = avccontext->extradata_size;
  769. uint8_t *header_start[3];
  770. int header_len[3];
  771. GetBitContext *gb = &(vc->gb);
  772. int hdr_type;
  773. vc->avccontext = avccontext;
  774. dsputil_init(&vc->dsp, avccontext);
  775. if (vc->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
  776. vc->add_bias = 385;
  777. vc->exp_bias = 0;
  778. } else {
  779. vc->add_bias = 0;
  780. vc->exp_bias = 15 << 23;
  781. }
  782. if (!headers_len) {
  783. av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
  784. return -1;
  785. }
  786. if (ff_split_xiph_headers(headers, headers_len, 30, header_start, header_len) < 0) {
  787. av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
  788. return -1;
  789. }
  790. init_get_bits(gb, header_start[0], header_len[0]*8);
  791. hdr_type = get_bits(gb, 8);
  792. if (hdr_type != 1) {
  793. av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
  794. return -1;
  795. }
  796. if (vorbis_parse_id_hdr(vc)) {
  797. av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
  798. vorbis_free(vc);
  799. return -1;
  800. }
  801. init_get_bits(gb, header_start[2], header_len[2]*8);
  802. hdr_type = get_bits(gb, 8);
  803. if (hdr_type != 5) {
  804. av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
  805. vorbis_free(vc);
  806. return -1;
  807. }
  808. if (vorbis_parse_setup_hdr(vc)) {
  809. av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
  810. vorbis_free(vc);
  811. return -1;
  812. }
  813. if (vc->audio_channels > 8)
  814. avccontext->channel_layout = 0;
  815. else
  816. avccontext->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
  817. avccontext->channels = vc->audio_channels;
  818. avccontext->sample_rate = vc->audio_samplerate;
  819. avccontext->frame_size = FFMIN(vc->blocksize[0], vc->blocksize[1]) >> 2;
  820. avccontext->sample_fmt = SAMPLE_FMT_S16;
  821. return 0 ;
  822. }
  823. // Decode audiopackets -------------------------------------------------
  824. // Read and decode floor
  825. static uint_fast8_t vorbis_floor0_decode(vorbis_context *vc,
  826. vorbis_floor_data *vfu, float *vec)
  827. {
  828. vorbis_floor0 *vf = &vfu->t0;
  829. float *lsp = vf->lsp;
  830. uint_fast32_t amplitude;
  831. uint_fast32_t book_idx;
  832. uint_fast8_t blockflag = vc->modes[vc->mode_number].blockflag;
  833. amplitude = get_bits(&vc->gb, vf->amplitude_bits);
  834. if (amplitude > 0) {
  835. float last = 0;
  836. uint_fast16_t lsp_len = 0;
  837. uint_fast16_t idx;
  838. vorbis_codebook codebook;
  839. book_idx = get_bits(&vc->gb, ilog(vf->num_books));
  840. if (book_idx >= vf->num_books) {
  841. av_log(vc->avccontext, AV_LOG_ERROR,
  842. "floor0 dec: booknumber too high!\n");
  843. book_idx = 0;
  844. //FIXME: look above
  845. }
  846. AV_DEBUG("floor0 dec: booknumber: %u\n", book_idx);
  847. codebook = vc->codebooks[vf->book_list[book_idx]];
  848. while (lsp_len<vf->order) {
  849. int vec_off;
  850. AV_DEBUG("floor0 dec: book dimension: %d\n", codebook.dimensions);
  851. AV_DEBUG("floor0 dec: maximum depth: %d\n", codebook.maxdepth);
  852. /* read temp vector */
  853. vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
  854. codebook.nb_bits, codebook.maxdepth)
  855. * codebook.dimensions;
  856. AV_DEBUG("floor0 dec: vector offset: %d\n", vec_off);
  857. /* copy each vector component and add last to it */
  858. for (idx = 0; idx < codebook.dimensions; ++idx)
  859. lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
  860. last = lsp[lsp_len+idx-1]; /* set last to last vector component */
  861. lsp_len += codebook.dimensions;
  862. }
  863. #ifdef V_DEBUG
  864. /* DEBUG: output lsp coeffs */
  865. {
  866. int idx;
  867. for (idx = 0; idx < lsp_len; ++idx)
  868. AV_DEBUG("floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
  869. }
  870. #endif
  871. /* synthesize floor output vector */
  872. {
  873. int i;
  874. int order = vf->order;
  875. float wstep = M_PI / vf->bark_map_size;
  876. for (i = 0; i < order; i++)
  877. lsp[i] = 2.0f * cos(lsp[i]);
  878. AV_DEBUG("floor0 synth: map_size = %d; m = %d; wstep = %f\n",
  879. vf->map_size, order, wstep);
  880. i = 0;
  881. while (i < vf->map_size[blockflag]) {
  882. int j, iter_cond = vf->map[blockflag][i];
  883. float p = 0.5f;
  884. float q = 0.5f;
  885. float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
  886. /* similar part for the q and p products */
  887. for (j = 0; j + 1 < order; j += 2) {
  888. q *= lsp[j] - two_cos_w;
  889. p *= lsp[j + 1] - two_cos_w;
  890. }
  891. if (j == order) { // even order
  892. p *= p * (2.0f - two_cos_w);
  893. q *= q * (2.0f + two_cos_w);
  894. } else { // odd order
  895. q *= two_cos_w-lsp[j]; // one more time for q
  896. /* final step and square */
  897. p *= p * (4.f - two_cos_w * two_cos_w);
  898. q *= q;
  899. }
  900. /* calculate linear floor value */
  901. {
  902. q = exp((((amplitude*vf->amplitude_offset) /
  903. (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
  904. - vf->amplitude_offset) * .11512925f);
  905. }
  906. /* fill vector */
  907. do {
  908. vec[i] = q; ++i;
  909. } while (vf->map[blockflag][i] == iter_cond);
  910. }
  911. }
  912. } else {
  913. /* this channel is unused */
  914. return 1;
  915. }
  916. AV_DEBUG(" Floor0 decoded\n");
  917. return 0;
  918. }
  919. static uint_fast8_t vorbis_floor1_decode(vorbis_context *vc,
  920. vorbis_floor_data *vfu, float *vec)
  921. {
  922. vorbis_floor1 *vf = &vfu->t1;
  923. GetBitContext *gb = &vc->gb;
  924. uint_fast16_t range_v[4] = { 256, 128, 86, 64 };
  925. uint_fast16_t range = range_v[vf->multiplier-1];
  926. uint_fast16_t floor1_Y[vf->x_list_dim];
  927. uint_fast16_t floor1_Y_final[vf->x_list_dim];
  928. int floor1_flag[vf->x_list_dim];
  929. uint_fast8_t class_;
  930. uint_fast8_t cdim;
  931. uint_fast8_t cbits;
  932. uint_fast8_t csub;
  933. uint_fast8_t cval;
  934. int_fast16_t book;
  935. uint_fast16_t offset;
  936. uint_fast16_t i,j;
  937. /*u*/int_fast16_t adx, ady, off, predicted; // WTF ? dy/adx = (unsigned)dy/adx ?
  938. int_fast16_t dy, err;
  939. if (!get_bits1(gb)) // silence
  940. return 1;
  941. // Read values (or differences) for the floor's points
  942. floor1_Y[0] = get_bits(gb, ilog(range - 1));
  943. floor1_Y[1] = get_bits(gb, ilog(range - 1));
  944. AV_DEBUG("floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
  945. offset = 2;
  946. for (i = 0; i < vf->partitions; ++i) {
  947. class_ = vf->partition_class[i];
  948. cdim = vf->class_dimensions[class_];
  949. cbits = vf->class_subclasses[class_];
  950. csub = (1 << cbits) - 1;
  951. cval = 0;
  952. AV_DEBUG("Cbits %d \n", cbits);
  953. if (cbits) // this reads all subclasses for this partition's class
  954. cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[class_]].vlc.table,
  955. vc->codebooks[vf->class_masterbook[class_]].nb_bits, 3);
  956. for (j = 0; j < cdim; ++j) {
  957. book = vf->subclass_books[class_][cval & csub];
  958. AV_DEBUG("book %d Cbits %d cval %d bits:%d \n", book, cbits, cval, get_bits_count(gb));
  959. cval = cval >> cbits;
  960. if (book > -1) {
  961. floor1_Y[offset+j] = get_vlc2(gb, vc->codebooks[book].vlc.table,
  962. vc->codebooks[book].nb_bits, 3);
  963. } else {
  964. floor1_Y[offset+j] = 0;
  965. }
  966. AV_DEBUG(" floor(%d) = %d \n", vf->list[offset+j].x, floor1_Y[offset+j]);
  967. }
  968. offset+=cdim;
  969. }
  970. // Amplitude calculation from the differences
  971. floor1_flag[0] = 1;
  972. floor1_flag[1] = 1;
  973. floor1_Y_final[0] = floor1_Y[0];
  974. floor1_Y_final[1] = floor1_Y[1];
  975. for (i = 2; i < vf->x_list_dim; ++i) {
  976. uint_fast16_t val, highroom, lowroom, room;
  977. uint_fast16_t high_neigh_offs;
  978. uint_fast16_t low_neigh_offs;
  979. low_neigh_offs = vf->list[i].low;
  980. high_neigh_offs = vf->list[i].high;
  981. dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin
  982. adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
  983. ady = FFABS(dy);
  984. err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
  985. off = (int16_t)err / (int16_t)adx;
  986. if (dy < 0) {
  987. predicted = floor1_Y_final[low_neigh_offs] - off;
  988. } else {
  989. predicted = floor1_Y_final[low_neigh_offs] + off;
  990. } // render_point end
  991. val = floor1_Y[i];
  992. highroom = range-predicted;
  993. lowroom = predicted;
  994. if (highroom < lowroom) {
  995. room = highroom * 2;
  996. } else {
  997. room = lowroom * 2; // SPEC mispelling
  998. }
  999. if (val) {
  1000. floor1_flag[low_neigh_offs] = 1;
  1001. floor1_flag[high_neigh_offs] = 1;
  1002. floor1_flag[i] = 1;
  1003. if (val >= room) {
  1004. if (highroom > lowroom) {
  1005. floor1_Y_final[i] = val - lowroom + predicted;
  1006. } else {
  1007. floor1_Y_final[i] = predicted - val + highroom - 1;
  1008. }
  1009. } else {
  1010. if (val & 1) {
  1011. floor1_Y_final[i] = predicted - (val + 1) / 2;
  1012. } else {
  1013. floor1_Y_final[i] = predicted + val / 2;
  1014. }
  1015. }
  1016. } else {
  1017. floor1_flag[i] = 0;
  1018. floor1_Y_final[i] = predicted;
  1019. }
  1020. AV_DEBUG(" Decoded floor(%d) = %d / val %d \n", vf->list[i].x, floor1_Y_final[i], val);
  1021. }
  1022. // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
  1023. ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
  1024. AV_DEBUG(" Floor decoded\n");
  1025. return 0;
  1026. }
  1027. // Read and decode residue
  1028. static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
  1029. vorbis_residue *vr,
  1030. uint_fast8_t ch,
  1031. uint_fast8_t *do_not_decode,
  1032. float *vec,
  1033. uint_fast16_t vlen,
  1034. int vr_type)
  1035. {
  1036. GetBitContext *gb = &vc->gb;
  1037. uint_fast8_t c_p_c = vc->codebooks[vr->classbook].dimensions;
  1038. uint_fast16_t n_to_read = vr->end-vr->begin;
  1039. uint_fast16_t ptns_to_read = n_to_read/vr->partition_size;
  1040. uint_fast8_t classifs[ptns_to_read*vc->audio_channels];
  1041. uint_fast8_t pass;
  1042. uint_fast8_t ch_used;
  1043. uint_fast8_t i,j,l;
  1044. uint_fast16_t k;
  1045. if (vr_type == 2) {
  1046. for (j = 1; j < ch; ++j)
  1047. do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input
  1048. if (do_not_decode[0])
  1049. return 0;
  1050. ch_used = 1;
  1051. } else {
  1052. ch_used = ch;
  1053. }
  1054. AV_DEBUG(" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
  1055. for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
  1056. uint_fast16_t voffset;
  1057. uint_fast16_t partition_count;
  1058. uint_fast16_t j_times_ptns_to_read;
  1059. voffset = vr->begin;
  1060. for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error
  1061. if (!pass) {
  1062. uint_fast32_t inverse_class = ff_inverse[vr->classifications];
  1063. for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
  1064. if (!do_not_decode[j]) {
  1065. uint_fast32_t temp = get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table,
  1066. vc->codebooks[vr->classbook].nb_bits, 3);
  1067. AV_DEBUG("Classword: %d \n", temp);
  1068. assert(vr->classifications > 1 && temp <= 65536); //needed for inverse[]
  1069. for (i = 0; i < c_p_c; ++i) {
  1070. uint_fast32_t temp2;
  1071. temp2 = (((uint_fast64_t)temp) * inverse_class) >> 32;
  1072. if (partition_count + c_p_c - 1 - i < ptns_to_read)
  1073. classifs[j_times_ptns_to_read + partition_count + c_p_c - 1 - i] = temp - temp2 * vr->classifications;
  1074. temp = temp2;
  1075. }
  1076. }
  1077. j_times_ptns_to_read += ptns_to_read;
  1078. }
  1079. }
  1080. for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
  1081. for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
  1082. uint_fast16_t voffs;
  1083. if (!do_not_decode[j]) {
  1084. uint_fast8_t vqclass = classifs[j_times_ptns_to_read+partition_count];
  1085. int_fast16_t vqbook = vr->books[vqclass][pass];
  1086. if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
  1087. uint_fast16_t coffs;
  1088. unsigned dim = vc->codebooks[vqbook].dimensions; // not uint_fast8_t: 64bit is slower here on amd64
  1089. uint_fast16_t step = dim == 1 ? vr->partition_size
  1090. : FASTDIV(vr->partition_size, dim);
  1091. vorbis_codebook codebook = vc->codebooks[vqbook];
  1092. if (vr_type == 0) {
  1093. voffs = voffset+j*vlen;
  1094. for (k = 0; k < step; ++k) {
  1095. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1096. for (l = 0; l < dim; ++l)
  1097. vec[voffs + k + l * step] += codebook.codevectors[coffs + l]; // FPMATH
  1098. }
  1099. } else if (vr_type == 1) {
  1100. voffs = voffset + j * vlen;
  1101. for (k = 0; k < step; ++k) {
  1102. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1103. for (l = 0; l < dim; ++l, ++voffs) {
  1104. vec[voffs]+=codebook.codevectors[coffs+l]; // FPMATH
  1105. AV_DEBUG(" pass %d offs: %d curr: %f change: %f cv offs.: %d \n", pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
  1106. }
  1107. }
  1108. } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
  1109. voffs = voffset >> 1;
  1110. if (dim == 2) {
  1111. for (k = 0; k < step; ++k) {
  1112. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
  1113. vec[voffs + k ] += codebook.codevectors[coffs ]; // FPMATH
  1114. vec[voffs + k + vlen] += codebook.codevectors[coffs + 1]; // FPMATH
  1115. }
  1116. } else if (dim == 4) {
  1117. for (k = 0; k < step; ++k, voffs += 2) {
  1118. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
  1119. vec[voffs ] += codebook.codevectors[coffs ]; // FPMATH
  1120. vec[voffs + 1 ] += codebook.codevectors[coffs + 2]; // FPMATH
  1121. vec[voffs + vlen ] += codebook.codevectors[coffs + 1]; // FPMATH
  1122. vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3]; // FPMATH
  1123. }
  1124. } else
  1125. for (k = 0; k < step; ++k) {
  1126. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1127. for (l = 0; l < dim; l += 2, voffs++) {
  1128. vec[voffs ] += codebook.codevectors[coffs + l ]; // FPMATH
  1129. vec[voffs + vlen] += codebook.codevectors[coffs + l + 1]; // FPMATH
  1130. 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);
  1131. }
  1132. }
  1133. } else if (vr_type == 2) {
  1134. voffs = voffset;
  1135. for (k = 0; k < step; ++k) {
  1136. coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
  1137. for (l = 0; l < dim; ++l, ++voffs) {
  1138. vec[voffs / ch + (voffs % ch) * vlen] += codebook.codevectors[coffs + l]; // FPMATH FIXME use if and counter instead of / and %
  1139. 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);
  1140. }
  1141. }
  1142. }
  1143. }
  1144. }
  1145. j_times_ptns_to_read += ptns_to_read;
  1146. }
  1147. ++partition_count;
  1148. voffset += vr->partition_size;
  1149. }
  1150. }
  1151. }
  1152. return 0;
  1153. }
  1154. static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
  1155. uint_fast8_t ch,
  1156. uint_fast8_t *do_not_decode,
  1157. float *vec, uint_fast16_t vlen)
  1158. {
  1159. if (vr->type == 2)
  1160. return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 2);
  1161. else if (vr->type == 1)
  1162. return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 1);
  1163. else if (vr->type == 0)
  1164. return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 0);
  1165. else {
  1166. av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
  1167. return -1;
  1168. }
  1169. }
  1170. void vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
  1171. {
  1172. int i;
  1173. for (i = 0; i < blocksize; i++) {
  1174. if (mag[i] > 0.0) {
  1175. if (ang[i] > 0.0) {
  1176. ang[i] = mag[i] - ang[i];
  1177. } else {
  1178. float temp = ang[i];
  1179. ang[i] = mag[i];
  1180. mag[i] += temp;
  1181. }
  1182. } else {
  1183. if (ang[i] > 0.0) {
  1184. ang[i] += mag[i];
  1185. } else {
  1186. float temp = ang[i];
  1187. ang[i] = mag[i];
  1188. mag[i] -= temp;
  1189. }
  1190. }
  1191. }
  1192. }
  1193. static void copy_normalize(float *dst, float *src, int len, int exp_bias,
  1194. float add_bias)
  1195. {
  1196. int i;
  1197. if (exp_bias) {
  1198. memcpy(dst, src, len * sizeof(float));
  1199. } else {
  1200. for (i = 0; i < len; i++)
  1201. dst[i] = src[i] + add_bias;
  1202. }
  1203. }
  1204. // Decode the audio packet using the functions above
  1205. static int vorbis_parse_audio_packet(vorbis_context *vc)
  1206. {
  1207. GetBitContext *gb = &vc->gb;
  1208. uint_fast8_t previous_window = vc->previous_window;
  1209. uint_fast8_t mode_number;
  1210. uint_fast8_t blockflag;
  1211. uint_fast16_t blocksize;
  1212. int_fast32_t i,j;
  1213. uint_fast8_t no_residue[vc->audio_channels];
  1214. uint_fast8_t do_not_decode[vc->audio_channels];
  1215. vorbis_mapping *mapping;
  1216. float *ch_res_ptr = vc->channel_residues;
  1217. float *ch_floor_ptr = vc->channel_floors;
  1218. uint_fast8_t res_chan[vc->audio_channels];
  1219. uint_fast8_t res_num = 0;
  1220. int_fast16_t retlen = 0;
  1221. float fadd_bias = vc->add_bias;
  1222. if (get_bits1(gb)) {
  1223. av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
  1224. return -1; // packet type not audio
  1225. }
  1226. if (vc->mode_count == 1) {
  1227. mode_number = 0;
  1228. } else {
  1229. GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
  1230. }
  1231. vc->mode_number = mode_number;
  1232. mapping = &vc->mappings[vc->modes[mode_number].mapping];
  1233. AV_DEBUG(" Mode number: %d , mapping: %d , blocktype %d \n", mode_number, vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
  1234. blockflag = vc->modes[mode_number].blockflag;
  1235. blocksize = vc->blocksize[blockflag];
  1236. if (blockflag)
  1237. skip_bits(gb, 2); // previous_window, next_window
  1238. memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2); //FIXME can this be removed ?
  1239. memset(ch_floor_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2); //FIXME can this be removed ?
  1240. // Decode floor
  1241. for (i = 0; i < vc->audio_channels; ++i) {
  1242. vorbis_floor *floor;
  1243. if (mapping->submaps > 1) {
  1244. floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
  1245. } else {
  1246. floor = &vc->floors[mapping->submap_floor[0]];
  1247. }
  1248. no_residue[i] = floor->decode(vc, &floor->data, ch_floor_ptr);
  1249. ch_floor_ptr += blocksize / 2;
  1250. }
  1251. // Nonzero vector propagate
  1252. for (i = mapping->coupling_steps - 1; i >= 0; --i) {
  1253. if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
  1254. no_residue[mapping->magnitude[i]] = 0;
  1255. no_residue[mapping->angle[i]] = 0;
  1256. }
  1257. }
  1258. // Decode residue
  1259. for (i = 0; i < mapping->submaps; ++i) {
  1260. vorbis_residue *residue;
  1261. uint_fast8_t ch = 0;
  1262. for (j = 0; j < vc->audio_channels; ++j) {
  1263. if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
  1264. res_chan[j] = res_num;
  1265. if (no_residue[j]) {
  1266. do_not_decode[ch] = 1;
  1267. } else {
  1268. do_not_decode[ch] = 0;
  1269. }
  1270. ++ch;
  1271. ++res_num;
  1272. }
  1273. }
  1274. residue = &vc->residues[mapping->submap_residue[i]];
  1275. vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, blocksize/2);
  1276. ch_res_ptr += ch * blocksize / 2;
  1277. }
  1278. // Inverse coupling
  1279. for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
  1280. float *mag, *ang;
  1281. mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
  1282. ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2;
  1283. vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
  1284. }
  1285. // Dotproduct, MDCT
  1286. for (j = vc->audio_channels-1;j >= 0; j--) {
  1287. ch_floor_ptr = vc->channel_floors + j * blocksize / 2;
  1288. ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
  1289. vc->dsp.vector_fmul(ch_floor_ptr, ch_res_ptr, blocksize / 2);
  1290. ff_imdct_half(&vc->mdct[blockflag], ch_res_ptr, ch_floor_ptr);
  1291. }
  1292. // Overlap/add, save data for next overlapping FPMATH
  1293. retlen = (blocksize + vc->blocksize[previous_window]) / 4;
  1294. for (j = 0; j < vc->audio_channels; j++) {
  1295. uint_fast16_t bs0 = vc->blocksize[0];
  1296. uint_fast16_t bs1 = vc->blocksize[1];
  1297. float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
  1298. float *saved = vc->saved + j * bs1 / 4;
  1299. float *ret = vc->channel_floors + j * retlen;
  1300. float *buf = residue;
  1301. const float *win = vc->win[blockflag & previous_window];
  1302. if (blockflag == previous_window) {
  1303. vc->dsp.vector_fmul_window(ret, saved, buf, win, fadd_bias, blocksize / 4);
  1304. } else if (blockflag > previous_window) {
  1305. vc->dsp.vector_fmul_window(ret, saved, buf, win, fadd_bias, bs0 / 4);
  1306. copy_normalize(ret+bs0/2, buf+bs0/4, (bs1-bs0)/4, vc->exp_bias, fadd_bias);
  1307. } else {
  1308. copy_normalize(ret, saved, (bs1 - bs0) / 4, vc->exp_bias, fadd_bias);
  1309. vc->dsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, fadd_bias, bs0 / 4);
  1310. }
  1311. memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
  1312. }
  1313. vc->previous_window = blockflag;
  1314. return retlen;
  1315. }
  1316. // Return the decoded audio packet through the standard api
  1317. static int vorbis_decode_frame(AVCodecContext *avccontext,
  1318. void *data, int *data_size,
  1319. AVPacket *avpkt)
  1320. {
  1321. const uint8_t *buf = avpkt->data;
  1322. int buf_size = avpkt->size;
  1323. vorbis_context *vc = avccontext->priv_data ;
  1324. GetBitContext *gb = &(vc->gb);
  1325. const float *channel_ptrs[vc->audio_channels];
  1326. int i;
  1327. int_fast16_t len;
  1328. if (!buf_size)
  1329. return 0;
  1330. AV_DEBUG("packet length %d \n", buf_size);
  1331. init_get_bits(gb, buf, buf_size*8);
  1332. len = vorbis_parse_audio_packet(vc);
  1333. if (len <= 0) {
  1334. *data_size = 0;
  1335. return buf_size;
  1336. }
  1337. if (!vc->first_frame) {
  1338. vc->first_frame = 1;
  1339. *data_size = 0;
  1340. return buf_size ;
  1341. }
  1342. AV_DEBUG("parsed %d bytes %d bits, returned %d samples (*ch*bits) \n", get_bits_count(gb)/8, get_bits_count(gb)%8, len);
  1343. if (vc->audio_channels > 8) {
  1344. for (i = 0; i < vc->audio_channels; i++)
  1345. channel_ptrs[i] = vc->channel_floors + i * len;
  1346. } else {
  1347. for (i = 0; i < vc->audio_channels; i++)
  1348. channel_ptrs[i] = vc->channel_floors +
  1349. len * ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
  1350. }
  1351. vc->dsp.float_to_int16_interleave(data, channel_ptrs, len, vc->audio_channels);
  1352. *data_size = len * 2 * vc->audio_channels;
  1353. return buf_size ;
  1354. }
  1355. // Close decoder
  1356. static av_cold int vorbis_decode_close(AVCodecContext *avccontext)
  1357. {
  1358. vorbis_context *vc = avccontext->priv_data;
  1359. vorbis_free(vc);
  1360. return 0 ;
  1361. }
  1362. AVCodec vorbis_decoder = {
  1363. "vorbis",
  1364. CODEC_TYPE_AUDIO,
  1365. CODEC_ID_VORBIS,
  1366. sizeof(vorbis_context),
  1367. vorbis_decode_init,
  1368. NULL,
  1369. vorbis_decode_close,
  1370. vorbis_decode_frame,
  1371. .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
  1372. .channel_layouts = ff_vorbis_channel_layouts,
  1373. };