vorbisdec.c 61 KB

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