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.

1459 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 11
  28. #define V_MAX_VLCS (1<<16)
  29. #ifndef V_DEBUG
  30. #define AV_DEBUG(...)
  31. #endif
  32. /* Helper functions */
  33. /**
  34. * reads 0-32 bits when using the ALT_BITSTREAM_READER_LE bitstream reader
  35. */
  36. unsigned int get_bits_long_le(GetBitContext *s, int n){
  37. if(n<=17) return get_bits(s, n);
  38. else{
  39. int ret= get_bits(s, 16);
  40. return ret | (get_bits(s, n-16) << 16);
  41. }
  42. }
  43. static unsigned int ilog(unsigned int i) { // unfortunatelly av_log2 uses different rounding
  44. unsigned int ret=0;
  45. while (i!=0) {
  46. ++ret;
  47. i>>=1;
  48. }
  49. return ret;
  50. }
  51. static unsigned int nth_root(unsigned int x, unsigned int n) { // x^(1/n)
  52. unsigned int ret=0, i, j;
  53. do {
  54. ++ret;
  55. for(i=0,j=ret;i<n-1;i++) j*=ret;
  56. } while (j<=x);
  57. return (ret-1);
  58. }
  59. static float vorbisfloat2float(uint_fast32_t val) {
  60. double mant=val&0x1fffff;
  61. long exp=(val&0x7fe00000L)>>21;
  62. if (val&0x80000000) mant=-mant;
  63. return(ldexp(mant, exp-20-768));
  64. }
  65. // Generate vlc codes from vorbis huffman code lengths
  66. static int vorbis_len2vlc(vorbis_context *vc, uint_fast8_t *bits, uint_fast32_t *codes, uint_fast32_t num) {
  67. uint_fast32_t exit_at_level[33]={404,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  68. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  69. uint_fast8_t i,j;
  70. uint_fast32_t code,p;
  71. #ifdef V_DEBUG
  72. GetBitContext gb;
  73. #endif
  74. for(p=0;(bits[p]==0) && (p<num);++p);
  75. if (p==num) {
  76. // av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
  77. return 0;
  78. }
  79. codes[p]=0;
  80. for(i=0;i<bits[p];++i) {
  81. exit_at_level[i+1]=1<<i;
  82. }
  83. #ifdef V_DEBUG
  84. av_log(vc->avccontext, AV_LOG_INFO, " %d. of %d code len %d code %d - ", p, num, bits[p], codes[p]);
  85. init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
  86. for(i=0;i<bits[p];++i) {
  87. av_log(vc->avccontext, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  88. }
  89. av_log(vc->avccontext, AV_LOG_INFO, "\n");
  90. #endif
  91. ++p;
  92. for(;p<num;++p) {
  93. if (bits[p]==0) continue;
  94. // find corresponding exit(node which the tree can grow further from)
  95. for(i=bits[p];i>0;--i) {
  96. if (exit_at_level[i]) break;
  97. }
  98. if (!i) return 1; // overspecified tree
  99. code=exit_at_level[i];
  100. exit_at_level[i]=0;
  101. // construct code (append 0s to end) and introduce new exits
  102. for(j=i+1;j<=bits[p];++j) {
  103. exit_at_level[j]=code+(1<<(j-1));
  104. }
  105. codes[p]=code;
  106. #ifdef V_DEBUG
  107. av_log(vc->avccontext, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
  108. init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
  109. for(i=0;i<bits[p];++i) {
  110. av_log(vc->avccontext, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  111. }
  112. av_log(vc->avccontext, AV_LOG_INFO, "\n");
  113. #endif
  114. }
  115. //FIXME no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
  116. return 0;
  117. }
  118. // Free all allocated memory -----------------------------------------
  119. static void vorbis_free(vorbis_context *vc) {
  120. int_fast16_t i;
  121. av_freep(&vc->channel_residues);
  122. av_freep(&vc->channel_floors);
  123. av_freep(&vc->saved);
  124. av_freep(&vc->ret);
  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. av_free(vc->floors[i].x_list);
  136. av_free(vc->floors[i].x_list_order);
  137. av_free(vc->floors[i].low_neighbour);
  138. av_free(vc->floors[i].high_neighbour);
  139. }
  140. av_freep(&vc->floors);
  141. for(i=0;i<vc->mapping_count;++i) {
  142. av_free(vc->mappings[i].magnitude);
  143. av_free(vc->mappings[i].angle);
  144. av_free(vc->mappings[i].mux);
  145. }
  146. av_freep(&vc->mappings);
  147. }
  148. // Parse setup header -------------------------------------------------
  149. // Process codebooks part
  150. static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc) {
  151. uint_fast16_t cb;
  152. uint_fast8_t *tmp_vlc_bits;
  153. uint_fast32_t *tmp_vlc_codes;
  154. GetBitContext *gb=&vc->gb;
  155. vc->codebook_count=get_bits(gb,8)+1;
  156. AV_DEBUG(" Codebooks: %d \n", vc->codebook_count);
  157. vc->codebooks=(vorbis_codebook *)av_mallocz(vc->codebook_count * sizeof(vorbis_codebook));
  158. tmp_vlc_bits=(uint_fast8_t *)av_mallocz(V_MAX_VLCS * sizeof(uint_fast8_t));
  159. tmp_vlc_codes=(uint_fast32_t *)av_mallocz(V_MAX_VLCS * sizeof(uint_fast32_t));
  160. for(cb=0;cb<vc->codebook_count;++cb) {
  161. vorbis_codebook *codebook_setup=&vc->codebooks[cb];
  162. uint_fast8_t ordered;
  163. uint_fast32_t t, used_entries=0;
  164. uint_fast32_t entries;
  165. AV_DEBUG(" %d. Codebook \n", cb);
  166. if (get_bits(gb, 24)!=0x564342) {
  167. av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook setup data corrupt. \n", cb);
  168. goto error;
  169. }
  170. codebook_setup->dimensions=get_bits(gb, 16);
  171. if (codebook_setup->dimensions>16) {
  172. av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook's dimension is too large (%d). \n", cb, codebook_setup->dimensions);
  173. goto error;
  174. }
  175. entries=get_bits(gb, 24);
  176. if (entries>V_MAX_VLCS) {
  177. av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook has too many entries (%d). \n", cb, entries);
  178. goto error;
  179. }
  180. ordered=get_bits1(gb);
  181. AV_DEBUG(" codebook_dimensions %d, codebook_entries %d \n", codebook_setup->dimensions, entries);
  182. if (!ordered) {
  183. uint_fast16_t ce;
  184. uint_fast8_t flag;
  185. uint_fast8_t sparse=get_bits1(gb);
  186. AV_DEBUG(" not ordered \n");
  187. if (sparse) {
  188. AV_DEBUG(" sparse \n");
  189. used_entries=0;
  190. for(ce=0;ce<entries;++ce) {
  191. flag=get_bits1(gb);
  192. if (flag) {
  193. tmp_vlc_bits[ce]=get_bits(gb, 5)+1;
  194. ++used_entries;
  195. }
  196. else tmp_vlc_bits[ce]=0;
  197. }
  198. } else {
  199. AV_DEBUG(" not sparse \n");
  200. used_entries=entries;
  201. for(ce=0;ce<entries;++ce) {
  202. tmp_vlc_bits[ce]=get_bits(gb, 5)+1;
  203. }
  204. }
  205. } else {
  206. uint_fast16_t current_entry=0;
  207. uint_fast8_t current_length=get_bits(gb, 5)+1;
  208. AV_DEBUG(" ordered, current length: %d \n", current_length); //FIXME
  209. used_entries=entries;
  210. for(;current_entry<used_entries;++current_length) {
  211. uint_fast16_t i, number;
  212. AV_DEBUG(" number bits: %d ", ilog(entries - current_entry));
  213. number=get_bits(gb, ilog(entries - current_entry));
  214. AV_DEBUG(" number: %d \n", number);
  215. for(i=current_entry;i<number+current_entry;++i) {
  216. if (i<used_entries) tmp_vlc_bits[i]=current_length;
  217. }
  218. current_entry+=number;
  219. }
  220. if (current_entry>used_entries) {
  221. av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
  222. goto error;
  223. }
  224. }
  225. codebook_setup->lookup_type=get_bits(gb, 4);
  226. AV_DEBUG(" lookup type: %d : %s \n", codebook_setup->lookup_type, codebook_setup->lookup_type ? "vq" : "no lookup" );
  227. // If the codebook is used for (inverse) VQ, calculate codevectors.
  228. if (codebook_setup->lookup_type==1) {
  229. uint_fast16_t i, j, k;
  230. uint_fast16_t codebook_lookup_values=nth_root(entries, codebook_setup->dimensions);
  231. uint_fast16_t codebook_multiplicands[codebook_lookup_values];
  232. float codebook_minimum_value=vorbisfloat2float(get_bits_long_le(gb, 32));
  233. float codebook_delta_value=vorbisfloat2float(get_bits_long_le(gb, 32));
  234. uint_fast8_t codebook_value_bits=get_bits(gb, 4)+1;
  235. uint_fast8_t codebook_sequence_p=get_bits1(gb);
  236. AV_DEBUG(" We expect %d numbers for building the codevectors. \n", codebook_lookup_values);
  237. AV_DEBUG(" delta %f minmum %f \n", codebook_delta_value, codebook_minimum_value);
  238. for(i=0;i<codebook_lookup_values;++i) {
  239. codebook_multiplicands[i]=get_bits(gb, codebook_value_bits);
  240. AV_DEBUG(" multiplicands*delta+minmum : %e \n", (float)codebook_multiplicands[i]*codebook_delta_value+codebook_minimum_value);
  241. AV_DEBUG(" multiplicand %d \n", codebook_multiplicands[i]);
  242. }
  243. // Weed out unused vlcs and build codevector vector
  244. codebook_setup->codevectors=(float *)av_mallocz(used_entries*codebook_setup->dimensions * sizeof(float));
  245. for(j=0, i=0;i<entries;++i) {
  246. uint_fast8_t dim=codebook_setup->dimensions;
  247. if (tmp_vlc_bits[i]) {
  248. float last=0.0;
  249. uint_fast32_t lookup_offset=i;
  250. #ifdef V_DEBUG
  251. av_log(vc->avccontext, AV_LOG_INFO, "Lookup offset %d ,", i);
  252. #endif
  253. for(k=0;k<dim;++k) {
  254. uint_fast32_t multiplicand_offset = lookup_offset % codebook_lookup_values;
  255. codebook_setup->codevectors[j*dim+k]=codebook_multiplicands[multiplicand_offset]*codebook_delta_value+codebook_minimum_value+last;
  256. if (codebook_sequence_p) {
  257. last=codebook_setup->codevectors[j*dim+k];
  258. }
  259. lookup_offset/=codebook_lookup_values;
  260. }
  261. tmp_vlc_bits[j]=tmp_vlc_bits[i];
  262. #ifdef V_DEBUG
  263. av_log(vc->avccontext, AV_LOG_INFO, "real lookup offset %d, vector: ", j);
  264. for(k=0;k<dim;++k) {
  265. av_log(vc->avccontext, AV_LOG_INFO, " %f ", codebook_setup->codevectors[j*dim+k]);
  266. }
  267. av_log(vc->avccontext, AV_LOG_INFO, "\n");
  268. #endif
  269. ++j;
  270. }
  271. }
  272. if (j!=used_entries) {
  273. av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
  274. goto error;
  275. }
  276. entries=used_entries;
  277. }
  278. else if (codebook_setup->lookup_type>=2) {
  279. av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
  280. goto error;
  281. }
  282. // Initialize VLC table
  283. if (vorbis_len2vlc(vc, tmp_vlc_bits, tmp_vlc_codes, entries)) {
  284. av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n");
  285. goto error;
  286. }
  287. codebook_setup->maxdepth=0;
  288. for(t=0;t<entries;++t)
  289. if (tmp_vlc_bits[t]>=codebook_setup->maxdepth) codebook_setup->maxdepth=tmp_vlc_bits[t];
  290. codebook_setup->maxdepth=(codebook_setup->maxdepth+V_NB_BITS-1)/V_NB_BITS;
  291. if (init_vlc(&codebook_setup->vlc, V_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->saved_start=0;
  599. ff_mdct_init(&vc->mdct0, bl0, 1);
  600. ff_mdct_init(&vc->mdct1, bl1, 1);
  601. 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 ",
  602. vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize_0, vc->blocksize_1);
  603. /*
  604. BLK=vc->blocksize_0;
  605. for(i=0;i<BLK/2;++i) {
  606. 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)));
  607. }
  608. */
  609. return 0;
  610. }
  611. // Process the extradata using the functions above (identification header, setup header)
  612. static int vorbis_decode_init(AVCodecContext *avccontext) {
  613. vorbis_context *vc = avccontext->priv_data ;
  614. uint8_t *headers = avccontext->extradata;
  615. int headers_len=avccontext->extradata_size;
  616. uint8_t *header_start[3];
  617. int header_len[3];
  618. GetBitContext *gb = &(vc->gb);
  619. int i, j, hdr_type;
  620. vc->avccontext = avccontext;
  621. if (!headers_len) {
  622. av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
  623. return -1;
  624. }
  625. if(headers[0] == 0 && headers[1] == 30) {
  626. for(i = 0; i < 3; i++){
  627. header_len[i] = *headers++ << 8;
  628. header_len[i] += *headers++;
  629. header_start[i] = headers;
  630. headers += header_len[i];
  631. }
  632. } else if(headers[0] == 2) {
  633. for(j=1,i=0;i<2;++i, ++j) {
  634. header_len[i]=0;
  635. while(j<headers_len && headers[j]==0xff) {
  636. header_len[i]+=0xff;
  637. ++j;
  638. }
  639. if (j>=headers_len) {
  640. av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
  641. return -1;
  642. }
  643. header_len[i]+=headers[j];
  644. }
  645. header_len[2]=headers_len-header_len[0]-header_len[1]-j;
  646. headers+=j;
  647. header_start[0] = headers;
  648. header_start[1] = header_start[0] + header_len[0];
  649. header_start[2] = header_start[1] + header_len[1];
  650. } else {
  651. av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
  652. return -1;
  653. }
  654. init_get_bits(gb, header_start[0], header_len[0]*8);
  655. hdr_type=get_bits(gb, 8);
  656. if (hdr_type!=1) {
  657. av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
  658. return -1;
  659. }
  660. if (vorbis_parse_id_hdr(vc)) {
  661. av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
  662. vorbis_free(vc);
  663. return -1;
  664. }
  665. init_get_bits(gb, header_start[2], header_len[2]*8);
  666. hdr_type=get_bits(gb, 8);
  667. if (hdr_type!=5) {
  668. av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
  669. return -1;
  670. }
  671. if (vorbis_parse_setup_hdr(vc)) {
  672. av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
  673. vorbis_free(vc);
  674. return -1;
  675. }
  676. avccontext->channels = vc->audio_channels;
  677. avccontext->sample_rate = vc->audio_samplerate;
  678. return 0 ;
  679. }
  680. // Decode audiopackets -------------------------------------------------
  681. // Read and decode floor (type 1 only)
  682. static uint_fast8_t vorbis_floor1_decode(vorbis_context *vc, vorbis_floor *vf, float *vec) {
  683. GetBitContext *gb=&vc->gb;
  684. uint_fast16_t range_v[4]={ 256, 128, 86, 64 };
  685. uint_fast16_t range=range_v[vf->multiplier-1];
  686. uint_fast16_t floor1_Y[vf->x_list_dim];
  687. uint_fast16_t floor1_Y_final[vf->x_list_dim];
  688. uint_fast8_t floor1_flag[vf->x_list_dim];
  689. uint_fast8_t class_;
  690. uint_fast8_t cdim;
  691. uint_fast8_t cbits;
  692. uint_fast8_t csub;
  693. uint_fast8_t cval;
  694. int_fast16_t book;
  695. uint_fast16_t offset;
  696. uint_fast16_t i,j;
  697. uint_fast16_t *floor_x_sort=vf->x_list_order;
  698. /*u*/int_fast16_t adx, ady, off, predicted; // WTF ? dy/adx= (unsigned)dy/adx ?
  699. int_fast16_t dy, err;
  700. uint_fast16_t lx,hx, ly, hy=0;
  701. if (!get_bits1(gb)) return 1; // silence
  702. // Read values (or differences) for the floor's points
  703. floor1_Y[0]=get_bits(gb, ilog(range-1));
  704. floor1_Y[1]=get_bits(gb, ilog(range-1));
  705. AV_DEBUG("floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
  706. offset=2;
  707. for(i=0;i<vf->partitions;++i) {
  708. class_=vf->partition_class[i];
  709. cdim=vf->class_dimensions[class_];
  710. cbits=vf->class_subclasses[class_];
  711. csub=(1<<cbits)-1;
  712. cval=0;
  713. AV_DEBUG("Cbits %d \n", cbits);
  714. if (cbits) { // this reads all subclasses for this partition's class
  715. cval=get_vlc2(gb, vc->codebooks[vf->class_masterbook[class_]].vlc.table,
  716. V_NB_BITS, vc->codebooks[vf->class_masterbook[class_]].maxdepth);
  717. }
  718. for(j=0;j<cdim;++j) {
  719. book=vf->subclass_books[class_][cval & csub];
  720. AV_DEBUG("book %d Cbits %d cval %d bits:%d \n", book, cbits, cval, get_bits_count(gb));
  721. cval=cval>>cbits;
  722. if (book>0) {
  723. floor1_Y[offset+j]=get_vlc2(gb, vc->codebooks[book].vlc.table,
  724. V_NB_BITS, vc->codebooks[book].maxdepth);
  725. } else {
  726. floor1_Y[offset+j]=0;
  727. }
  728. AV_DEBUG(" floor(%d) = %d \n", vf->x_list[offset+j], floor1_Y[offset+j]);
  729. }
  730. offset+=cdim;
  731. }
  732. // Amplitude calculation from the differences
  733. floor1_flag[0]=1;
  734. floor1_flag[1]=1;
  735. floor1_Y_final[0]=floor1_Y[0];
  736. floor1_Y_final[1]=floor1_Y[1];
  737. for(i=2;i<vf->x_list_dim;++i) {
  738. uint_fast16_t val, highroom, lowroom, room;
  739. uint_fast16_t high_neigh_offs;
  740. uint_fast16_t low_neigh_offs;
  741. low_neigh_offs=vf->low_neighbour[i];
  742. high_neigh_offs=vf->high_neighbour[i];
  743. dy=floor1_Y_final[high_neigh_offs]-floor1_Y_final[low_neigh_offs]; // render_point begin
  744. adx=vf->x_list[high_neigh_offs]-vf->x_list[low_neigh_offs];
  745. ady= ABS(dy);
  746. err=ady*(vf->x_list[i]-vf->x_list[low_neigh_offs]);
  747. off=err/adx;
  748. if (dy<0) {
  749. predicted=floor1_Y_final[low_neigh_offs]-off;
  750. } else {
  751. predicted=floor1_Y_final[low_neigh_offs]+off;
  752. } // render_point end
  753. val=floor1_Y[i];
  754. highroom=range-predicted;
  755. lowroom=predicted;
  756. if (highroom < lowroom) {
  757. room=highroom*2;
  758. } else {
  759. room=lowroom*2; // SPEC mispelling
  760. }
  761. if (val) {
  762. floor1_flag[low_neigh_offs]=1;
  763. floor1_flag[high_neigh_offs]=1;
  764. floor1_flag[i]=1;
  765. if (val>=room) {
  766. if (highroom > lowroom) {
  767. floor1_Y_final[i]=val-lowroom+predicted;
  768. } else {
  769. floor1_Y_final[i]=predicted-val+highroom-1;
  770. }
  771. } else {
  772. if (val & 1) {
  773. floor1_Y_final[i]=predicted-(val+1)/2;
  774. } else {
  775. floor1_Y_final[i]=predicted+val/2;
  776. }
  777. }
  778. } else {
  779. floor1_flag[i]=0;
  780. floor1_Y_final[i]=predicted;
  781. }
  782. AV_DEBUG(" Decoded floor(%d) = %d / val %d \n", vf->x_list[i], floor1_Y_final[i], val);
  783. }
  784. // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
  785. hx=0;
  786. lx=0;
  787. ly=floor1_Y_final[0]*vf->multiplier; // conforms to SPEC
  788. vec[0]=floor1_inverse_db_table[ly];
  789. for(i=1;i<vf->x_list_dim;++i) {
  790. AV_DEBUG(" Looking at post %d \n", i);
  791. if (floor1_flag[floor_x_sort[i]]) { // SPEC mispelled
  792. int_fast16_t x, y, dy, base, sy; // if uncommented: dy = -32 adx = 2 base = 2blablabla ?????
  793. hy=floor1_Y_final[floor_x_sort[i]]*vf->multiplier;
  794. hx=vf->x_list[floor_x_sort[i]];
  795. dy=hy-ly;
  796. adx=hx-lx;
  797. ady= (dy<0) ? -dy:dy;//ABS(dy);
  798. base=dy/adx;
  799. AV_DEBUG(" dy %d adx %d base %d = %d \n", dy, adx, base, dy/adx);
  800. x=lx;
  801. y=ly;
  802. err=0;
  803. if (dy<0) {
  804. sy=base-1;
  805. } else {
  806. sy=base+1;
  807. }
  808. ady=ady-(base<0 ? -base : base)*adx;
  809. vec[x]=floor1_inverse_db_table[y];
  810. AV_DEBUG(" vec[ %d ] = %d \n", x, y);
  811. for(x=lx+1;(x<hx) && (x<vf->x_list[1]);++x) {
  812. err+=ady;
  813. if (err>=adx) {
  814. err-=adx;
  815. y+=sy;
  816. } else {
  817. y+=base;
  818. }
  819. vec[x]=floor1_inverse_db_table[y];
  820. AV_DEBUG(" vec[ %d ] = %d \n", x, y);
  821. }
  822. /* for(j=1;j<hx-lx+1;++j) { // iterating render_point
  823. dy=hy-ly;
  824. adx=hx-lx;
  825. ady= dy<0 ? -dy : dy;
  826. err=ady*j;
  827. off=err/adx;
  828. if (dy<0) {
  829. predicted=ly-off;
  830. } else {
  831. predicted=ly+off;
  832. }
  833. if (lx+j < vf->x_list[1]) {
  834. vec[lx+j]=floor1_inverse_db_table[predicted];
  835. }
  836. }*/
  837. lx=hx;
  838. ly=hy;
  839. }
  840. }
  841. if (hx<vf->x_list[1]) {
  842. for(i=hx;i<vf->x_list[1];++i) {
  843. vec[i]=floor1_inverse_db_table[hy];
  844. }
  845. }
  846. AV_DEBUG(" Floor decoded\n");
  847. return 0;
  848. }
  849. // Read and decode residue
  850. 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) {
  851. GetBitContext *gb=&vc->gb;
  852. uint_fast8_t c_p_c=vc->codebooks[vr->classbook].dimensions;
  853. uint_fast16_t n_to_read=vr->end-vr->begin;
  854. uint_fast16_t ptns_to_read=n_to_read/vr->partition_size;
  855. uint_fast8_t classifs[ptns_to_read*vc->audio_channels];
  856. uint_fast8_t pass;
  857. uint_fast8_t ch_used;
  858. uint_fast8_t i,j,l;
  859. uint_fast16_t k;
  860. if (vr->type==2) {
  861. for(j=1;j<ch;++j) {
  862. do_not_decode[0]&=do_not_decode[j]; // FIXME - clobbering input
  863. }
  864. if (do_not_decode[0]) return 0;
  865. ch_used=1;
  866. } else {
  867. ch_used=ch;
  868. }
  869. AV_DEBUG(" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
  870. for(pass=0;pass<=vr->maxpass;++pass) { // FIXME OPTIMIZE?
  871. uint_fast16_t voffset;
  872. uint_fast16_t partition_count;
  873. uint_fast16_t j_times_ptns_to_read;
  874. voffset=vr->begin;
  875. for(partition_count=0;partition_count<ptns_to_read;) { // SPEC error
  876. if (!pass) {
  877. for(j_times_ptns_to_read=0, j=0;j<ch_used;++j) {
  878. if (!do_not_decode[j]) {
  879. uint_fast32_t temp=get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table,
  880. V_NB_BITS, vc->codebooks[vr->classbook].maxdepth);
  881. AV_DEBUG("Classword: %d \n", temp);
  882. for(i=0;i<c_p_c;++i) {
  883. uint_fast32_t temp2;
  884. temp2=temp/vr->classifications;
  885. classifs[j_times_ptns_to_read+partition_count+c_p_c-1-i]=temp-temp2*vr->classifications;
  886. temp=temp2;
  887. }
  888. }
  889. j_times_ptns_to_read+=ptns_to_read;
  890. }
  891. }
  892. for(i=0;(i<c_p_c) && (partition_count<ptns_to_read);++i) {
  893. for(j_times_ptns_to_read=0, j=0;j<ch_used;++j) {
  894. uint_fast16_t voffs;
  895. if (!do_not_decode[j]) {
  896. uint_fast8_t vqclass=classifs[j_times_ptns_to_read+partition_count];
  897. int_fast16_t vqbook=vr->books[vqclass][pass];
  898. if (vqbook>=0) {
  899. uint_fast16_t coffs;
  900. if (vr->type==0) {
  901. uint_fast16_t step=vr->partition_size/vc->codebooks[vqbook].dimensions;
  902. voffs=voffset+j*vlen;
  903. for(k=0;k<step;++k) {
  904. coffs=get_vlc2(gb, vc->codebooks[vqbook].vlc.table,
  905. V_NB_BITS, vc->codebooks[vr->classbook].maxdepth) * vc->codebooks[vqbook].dimensions;
  906. for(l=0;l<vc->codebooks[vqbook].dimensions;++l) {
  907. vec[voffs+k+l*step]+=vc->codebooks[vqbook].codevectors[coffs+l]; // FPMATH
  908. }
  909. }
  910. }
  911. else if (vr->type==1) {
  912. voffs=voffset+j*vlen;
  913. for(k=0;k<vr->partition_size/vc->codebooks[vqbook].dimensions;++k) {
  914. coffs=get_vlc2(gb, vc->codebooks[vqbook].vlc.table,
  915. V_NB_BITS, vc->codebooks[vr->classbook].maxdepth) * vc->codebooks[vqbook].dimensions;
  916. for(l=0;l<vc->codebooks[vqbook].dimensions;++l, ++voffs) {
  917. vec[voffs]+=vc->codebooks[vqbook].codevectors[coffs+l]; // FPMATH
  918. AV_DEBUG(" pass %d offs: %d curr: %f change: %f cv offs.: %d \n", pass, voffs, vec[voffs], vc->codebooks[vqbook].codevectors[coffs+l], coffs);
  919. }
  920. }
  921. }
  922. else if (vr->type==2 && ch==2) { // most frequent case optimized
  923. voffs=voffset;
  924. for(k=0;k<vr->partition_size/vc->codebooks[vqbook].dimensions;++k) {
  925. coffs=get_vlc2(gb, vc->codebooks[vqbook].vlc.table,
  926. V_NB_BITS, vc->codebooks[vr->classbook].maxdepth) * vc->codebooks[vqbook].dimensions;
  927. for(l=0;l<vc->codebooks[vqbook].dimensions;++l, ++voffs) {
  928. vec[(voffs>>1)+((voffs&1) ? vlen : 0)]+=vc->codebooks[vqbook].codevectors[coffs+l]; // FPMATH
  929. 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], vc->codebooks[vqbook].codevectors[coffs+l], coffs, l);
  930. }
  931. }
  932. }
  933. else if (vr->type==2) {
  934. voffs=voffset;
  935. for(k=0;k<vr->partition_size/vc->codebooks[vqbook].dimensions;++k) {
  936. coffs=get_vlc2(gb, vc->codebooks[vqbook].vlc.table,
  937. V_NB_BITS, vc->codebooks[vr->classbook].maxdepth) * vc->codebooks[vqbook].dimensions;
  938. for(l=0;l<vc->codebooks[vqbook].dimensions;++l, ++voffs) {
  939. vec[voffs/ch+(voffs%ch)*vlen]+=vc->codebooks[vqbook].codevectors[coffs+l]; // FPMATH FIXME use if and counter instead of / and %
  940. 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], vc->codebooks[vqbook].codevectors[coffs+l], coffs, l);
  941. }
  942. }
  943. } else {
  944. av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
  945. return 1;
  946. }
  947. }
  948. }
  949. j_times_ptns_to_read+=ptns_to_read;
  950. }
  951. ++partition_count;
  952. voffset+=vr->partition_size;
  953. }
  954. }
  955. }
  956. return 0;
  957. }
  958. // Decode the audio packet using the functions above
  959. static int vorbis_parse_audio_packet(vorbis_context *vc) {
  960. GetBitContext *gb=&vc->gb;
  961. uint_fast8_t previous_window=0,next_window=0;
  962. uint_fast8_t mode_number;
  963. uint_fast16_t blocksize;
  964. int_fast32_t i,j;
  965. uint_fast8_t no_residue[vc->audio_channels];
  966. uint_fast8_t do_not_decode[vc->audio_channels];
  967. vorbis_mapping *mapping;
  968. float *ch_res_ptr=vc->channel_residues;
  969. float *ch_floor_ptr=vc->channel_floors;
  970. uint_fast8_t res_chan[vc->audio_channels];
  971. uint_fast8_t res_num=0;
  972. int_fast16_t retlen=0;
  973. uint_fast16_t saved_start=0;
  974. if (get_bits1(gb)) {
  975. av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
  976. return -1; // packet type not audio
  977. }
  978. if (vc->mode_count==1) {
  979. mode_number=0;
  980. } else {
  981. mode_number=get_bits(gb, ilog(vc->mode_count-1));
  982. }
  983. mapping=&vc->mappings[vc->modes[mode_number].mapping];
  984. AV_DEBUG(" Mode number: %d , mapping: %d , blocktype %d \n", mode_number, vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
  985. if (vc->modes[mode_number].blockflag) {
  986. previous_window=get_bits1(gb);
  987. next_window=get_bits1(gb);
  988. }
  989. blocksize=vc->modes[mode_number].blockflag ? vc->blocksize_1 : vc->blocksize_0;
  990. memset(ch_res_ptr, 0, sizeof(float)*vc->audio_channels*blocksize/2); //FIXME can this be removed ?
  991. memset(ch_floor_ptr, 0, sizeof(float)*vc->audio_channels*blocksize/2); //FIXME can this be removed ?
  992. // Decode floor(1)
  993. for(i=0;i<vc->audio_channels;++i) {
  994. vorbis_floor *floor;
  995. if (mapping->submaps>1) {
  996. floor=&vc->floors[mapping->submap_floor[mapping->mux[i]]];
  997. } else {
  998. floor=&vc->floors[mapping->submap_floor[0]];
  999. }
  1000. no_residue[i]=vorbis_floor1_decode(vc, floor, ch_floor_ptr);
  1001. ch_floor_ptr+=blocksize/2;
  1002. }
  1003. // Nonzero vector propagate
  1004. for(i=mapping->coupling_steps-1;i>=0;--i) {
  1005. if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
  1006. no_residue[mapping->magnitude[i]]=0;
  1007. no_residue[mapping->angle[i]]=0;
  1008. }
  1009. }
  1010. // Decode residue
  1011. for(i=0;i<mapping->submaps;++i) {
  1012. vorbis_residue *residue;
  1013. uint_fast8_t ch=0;
  1014. for(j=0;j<vc->audio_channels;++j) {
  1015. if ((mapping->submaps==1) || (i=mapping->mux[j])) {
  1016. res_chan[j]=res_num;
  1017. if (no_residue[j]) {
  1018. do_not_decode[ch]=1;
  1019. } else {
  1020. do_not_decode[ch]=0;
  1021. }
  1022. ++ch;
  1023. ++res_num;
  1024. }
  1025. }
  1026. residue=&vc->residues[mapping->submap_residue[i]];
  1027. vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, blocksize/2);
  1028. ch_res_ptr+=ch*blocksize/2;
  1029. }
  1030. // Inverse coupling
  1031. for(i=mapping->coupling_steps-1;i>=0;--i) { //warning: i has to be signed
  1032. float *mag, *ang;
  1033. mag=vc->channel_residues+res_chan[mapping->magnitude[i]]*blocksize/2;
  1034. ang=vc->channel_residues+res_chan[mapping->angle[i]]*blocksize/2;
  1035. for(j=0;j<blocksize/2;++j) {
  1036. float temp;
  1037. if (mag[j]>0.0) {
  1038. if (ang[j]>0.0) {
  1039. ang[j]=mag[j]-ang[j];
  1040. } else {
  1041. temp=ang[j];
  1042. ang[j]=mag[j];
  1043. mag[j]+=temp;
  1044. }
  1045. } else {
  1046. if (ang[j]>0.0) {
  1047. ang[j]+=mag[j];
  1048. } else {
  1049. temp=ang[j];
  1050. ang[j]=mag[j];
  1051. mag[j]-=temp;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. // Dotproduct
  1057. for(j=0, ch_floor_ptr=vc->channel_floors;j<vc->audio_channels;++j,ch_floor_ptr+=blocksize/2) {
  1058. ch_res_ptr=vc->channel_residues+res_chan[j]*blocksize/2;
  1059. for(i=0;i<blocksize/2;++i) {
  1060. ch_floor_ptr[i]*=ch_res_ptr[i]; //FPMATH
  1061. }
  1062. }
  1063. // MDCT, overlap/add, save data for next overlapping FPMATH
  1064. for(j=0;j<vc->audio_channels;++j) {
  1065. uint_fast8_t step=vc->audio_channels;
  1066. uint_fast16_t k;
  1067. float *saved=vc->saved+j*vc->blocksize_1/2;
  1068. float *ret=vc->ret;
  1069. const float *lwin=vc->lwin;
  1070. const float *swin=vc->swin;
  1071. float buf[blocksize];
  1072. float buf_tmp[blocksize];
  1073. ch_floor_ptr=vc->channel_floors+j*blocksize/2;
  1074. saved_start=vc->saved_start;
  1075. ff_imdct_calc(vc->modes[mode_number].blockflag ? &vc->mdct1 : &vc->mdct0, buf, ch_floor_ptr, buf_tmp);
  1076. if (vc->modes[mode_number].blockflag) {
  1077. // -- overlap/add
  1078. if (previous_window) {
  1079. for(k=j, i=0;i<vc->blocksize_1/2;++i, k+=step) {
  1080. ret[k]=saved[i]+buf[i]*lwin[i];
  1081. }
  1082. retlen=vc->blocksize_1/2;
  1083. } else {
  1084. for(k=j, i=0;i<vc->blocksize_0/2;++i, k+=step) {
  1085. ret[k]=saved[i]+buf[(vc->blocksize_1-vc->blocksize_0)/4+i]*swin[i];
  1086. }
  1087. for(i=0;i<(vc->blocksize_1-vc->blocksize_0)/4;++i, k+=step) {
  1088. ret[k]=buf[vc->blocksize_0/2+(vc->blocksize_1-vc->blocksize_0)/4+i];
  1089. }
  1090. retlen=vc->blocksize_0/2+(vc->blocksize_1-vc->blocksize_0)/4;
  1091. }
  1092. // -- save
  1093. if (next_window) {
  1094. for(i=0;i<vc->blocksize_1/2;++i) {
  1095. saved[i]=buf[vc->blocksize_1/2+i]*lwin[vc->blocksize_1/2-1-i];
  1096. }
  1097. saved_start=0;
  1098. } else {
  1099. saved_start=(vc->blocksize_1-vc->blocksize_0)/4;
  1100. for(i=0;i<saved_start;++i) {
  1101. saved[i]=buf[vc->blocksize_1/2+i];
  1102. }
  1103. for(i=0;i<vc->blocksize_0/2;++i) {
  1104. saved[saved_start+i]=buf[vc->blocksize_1/2+saved_start+i]*swin[vc->blocksize_0/2-1-i];
  1105. }
  1106. }
  1107. } else {
  1108. // --overlap/add
  1109. for(k=j, i=0;i<saved_start;++i, k+=step) {
  1110. ret[k]=saved[i];
  1111. }
  1112. for(i=0;i<vc->blocksize_0/2;++i, k+=step) {
  1113. ret[k]=saved[saved_start+i]+buf[i]*swin[i];
  1114. }
  1115. retlen=saved_start+vc->blocksize_0/2;
  1116. // -- save
  1117. for(i=0;i<vc->blocksize_0/2;++i) {
  1118. saved[i]=buf[vc->blocksize_0/2+i]*swin[vc->blocksize_0/2-1-i];
  1119. }
  1120. saved_start=0;
  1121. }
  1122. }
  1123. vc->saved_start=saved_start;
  1124. return retlen*vc->audio_channels;
  1125. }
  1126. // Return the decoded audio packet through the standard api
  1127. static int vorbis_decode_frame(AVCodecContext *avccontext,
  1128. void *data, int *data_size,
  1129. uint8_t *buf, int buf_size)
  1130. {
  1131. vorbis_context *vc = avccontext->priv_data ;
  1132. GetBitContext *gb = &(vc->gb);
  1133. int_fast16_t i, len;
  1134. if(!buf_size){
  1135. return 0;
  1136. }
  1137. AV_DEBUG("packet length %d \n", buf_size);
  1138. init_get_bits(gb, buf, buf_size*8);
  1139. len=vorbis_parse_audio_packet(vc);
  1140. if (len<=0) {
  1141. *data_size=0;
  1142. return buf_size;
  1143. }
  1144. if (!vc->first_frame) {
  1145. vc->first_frame=1;
  1146. *data_size=0;
  1147. return buf_size ;
  1148. }
  1149. AV_DEBUG("parsed %d bytes %d bits, returned %d samples (*ch*bits) \n", get_bits_count(gb)/8, get_bits_count(gb)%8, len);
  1150. for(i=0;i<len;++i) {
  1151. int_fast32_t tmp;
  1152. tmp=vc->ret[i]*32768;
  1153. if (tmp>32767) tmp=32767;
  1154. if (tmp<-32768) tmp=-32768;
  1155. ((int16_t*)data)[i]=tmp;
  1156. }
  1157. *data_size=len*2;
  1158. return buf_size ;
  1159. }
  1160. // Close decoder
  1161. static int vorbis_decode_close(AVCodecContext *avccontext) {
  1162. vorbis_context *vc = avccontext->priv_data;
  1163. vorbis_free(vc);
  1164. return 0 ;
  1165. }
  1166. AVCodec vorbis_decoder = {
  1167. "vorbis",
  1168. CODEC_TYPE_AUDIO,
  1169. CODEC_ID_VORBIS,
  1170. sizeof(vorbis_context),
  1171. vorbis_decode_init,
  1172. NULL,
  1173. vorbis_decode_close,
  1174. vorbis_decode_frame,
  1175. };