vorbis_dec.c 57 KB

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