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.

1815 lines
62KB

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