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.

1707 lines
59KB

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