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.

1656 lines
60KB

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