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.

1472 lines
50KB

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