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.

1791 lines
61KB

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