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.

1813 lines
62KB

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