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.

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