vorbis_dec.c 61 KB

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