decoder.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #include "main.h"
  2. #include "decoder.h"
  3. #include <math.h>
  4. #include <locale.h>
  5. #pragma warning(disable:4244)
  6. #include "shaper.h"
  7. #include "api__in_vorbis.h"
  8. Decoder::~Decoder() {if (shaper) delete shaper;}
  9. extern CfgInt
  10. cfg_mc6_dm, cfg_mc6_map;
  11. /*
  12. if (vorbis_cfg.use_hq_preamp)
  13. {
  14. sample *= pow(10., preamp_db/20);
  15. //hard 6dB limiting
  16. if (sample < -0.5)
  17. sample = tanh((sample + 0.5) / (1-0.5)) * (1-0.5) - 0.5;
  18. else if (sample > 0.5)
  19. sample = tanh((sample - 0.5) / (1-0.5)) * (1-0.5) + 0.5;
  20. } */
  21. #if 0
  22. static float q_tanh(float x)
  23. {
  24. double foo1, foo2;
  25. foo1 = pow(2.71828182845904523536028747135266, x);
  26. foo2 = 1.0 / foo1;
  27. return (foo1 -foo2) / (foo1 + foo2);
  28. }
  29. #else
  30. #define q_tanh tanh
  31. #endif
  32. float VorbisFile::GetGain()
  33. {
  34. float peak;
  35. vorbis_comment * c;
  36. float scale = 1.0f;
  37. c = ov_comment(&vf, -1);
  38. peak = 0.99f;
  39. if (c)
  40. {
  41. if (AGAVE_API_CONFIG->GetBool(playbackConfigGroupGUID, L"replaygain", false))
  42. {
  43. char * _peak = 0, *_gain = 0;
  44. float gain = 0;
  45. bool have_rg = 0;
  46. float lwing_gain = 0;
  47. char *gain1 = 0, *gain2 = 0, *peak1 = 0, *peak2 = 0;
  48. gain1 = vorbis_comment_query(c, "replaygain_album_gain", 0);
  49. if (!gain1) gain1 = vorbis_comment_query(c, "rg_audiophile", 0);
  50. gain2 = vorbis_comment_query(c, "replaygain_track_gain", 0);
  51. if (!gain2) gain2 = vorbis_comment_query(c, "rg_radio", 0);
  52. peak1 = vorbis_comment_query(c, "replaygain_album_peak", 0);
  53. peak2 = vorbis_comment_query(c, "replaygain_track_peak", 0);
  54. switch (AGAVE_API_CONFIG->GetUnsigned(playbackConfigGroupGUID, L"replaygain_source", 0))
  55. {
  56. case 0: // track
  57. _gain = gain2;
  58. if (!_gain && !AGAVE_API_CONFIG->GetBool(playbackConfigGroupGUID, L"replaygain_preferred_only", false))
  59. _gain = gain1;
  60. _peak = peak2;
  61. if (!_peak && !AGAVE_API_CONFIG->GetBool(playbackConfigGroupGUID, L"replaygain_preferred_only", false))
  62. _peak = peak1;
  63. break;
  64. case 1: // album
  65. _gain = gain1;
  66. if (!_gain && !AGAVE_API_CONFIG->GetBool(playbackConfigGroupGUID, L"replaygain_preferred_only", false))
  67. _gain = gain2;
  68. _peak = peak1;
  69. if (!_peak && !AGAVE_API_CONFIG->GetBool(playbackConfigGroupGUID, L"replaygain_preferred_only", false))
  70. _peak = peak2;
  71. break;
  72. }
  73. if (!_peak)
  74. {
  75. _peak = vorbis_comment_query(c, "rg_peak", 0);
  76. }
  77. _locale_t C_locale = WASABI_API_LNG->Get_C_NumericLocale();
  78. if (_peak) peak = _atof_l(_peak, C_locale);
  79. if (_gain) gain = _atof_l(_gain, C_locale);
  80. if (!_peak && !_gain)
  81. {
  82. char * l = vorbis_comment_query(c, "lwing_gain", 0);
  83. if (l)
  84. {
  85. lwing_gain = _atof_l(l, C_locale);
  86. have_rg = 1;
  87. }
  88. }
  89. else have_rg = 1;
  90. if (!have_rg)
  91. {
  92. gain = AGAVE_API_CONFIG->GetFloat(playbackConfigGroupGUID, L"non_replaygain", -6.0f);
  93. }
  94. scale = powf(10, (gain) / 20.0f);
  95. if (lwing_gain)
  96. scale *= lwing_gain;
  97. else if (have_rg)
  98. switch (AGAVE_API_CONFIG->GetUnsigned(playbackConfigGroupGUID, L"replaygain_mode", 1))
  99. {
  100. case 1: // apply gain, but don't clip
  101. if (scale*peak > 1.0) scale = 1.0 / peak;
  102. break;
  103. case 2: // normalize
  104. scale = 1.0 / peak;
  105. break;
  106. case 3: // no clipping
  107. if (peak > 1.0f)
  108. scale = 1.0 / peak;
  109. break;
  110. }
  111. }
  112. }
  113. return scale;
  114. }
  115. void Decoder::process_rg()
  116. {
  117. scale = file->GetGain();
  118. }
  119. void Decoder::setup_mc()
  120. {
  121. if (AGAVE_API_CONFIG->GetBool(playbackConfigGroupGUID, L"mono", false))
  122. nch = 1;
  123. else if (src_nch == 6)
  124. {
  125. switch (cfg_mc6_dm)
  126. {
  127. case 2:
  128. nch = 4;
  129. break;
  130. case 3:
  131. case 4:
  132. nch = 2;
  133. break;
  134. case 5:
  135. nch = 1;
  136. break;
  137. }
  138. if (nch > 2 && !AGAVE_API_CONFIG->GetBool(playbackConfigGroupGUID, L"surround", true))
  139. nch = 2;
  140. }
  141. }
  142. void Decoder::Flush()
  143. {
  144. bptr = 0;
  145. pcmbuf = 0;
  146. data = 0;
  147. pos = 0;
  148. if (shaper) {delete shaper;shaper = 0;}
  149. }
  150. void Decoder::Init(VorbisFile * f, UINT _bits, UINT _nch, bool _useFloat, bool allowRG)
  151. {
  152. useFloat = _useFloat;
  153. file = f;
  154. vorbis_info * i = ov_info(&file->vf, -1);
  155. if (allowRG)
  156. process_rg();
  157. else
  158. scale = 1.0f;
  159. if (useFloat)
  160. {
  161. dither = false;
  162. bps = 32;
  163. }
  164. else
  165. {
  166. dither = AGAVE_API_CONFIG->GetBool(playbackConfigGroupGUID, L"dither", true);
  167. if (_bits)
  168. bps = _bits;
  169. else
  170. bps = AGAVE_API_CONFIG->GetUnsigned(playbackConfigGroupGUID, L"bits", 16);
  171. }
  172. if (useFloat)
  173. {
  174. clipmin = -10000; // some arbitrarily large number
  175. clipmax = 10000; // some arbitrarily large number
  176. }
  177. else
  178. {
  179. clipmin = - (1 << (bps - 1));
  180. clipmax = (1 << (bps - 1)) - 1;
  181. }
  182. sr = i->rate;
  183. nch = src_nch = i->channels;
  184. Flush();
  185. cur_link = file->vf.current_link;
  186. if (_nch)
  187. nch = _nch;
  188. else
  189. setup_mc();
  190. }
  191. UINT Decoder::DataAvailable()
  192. {
  193. return data * (bps >> 3);
  194. }
  195. int Decoder::DoFrame()
  196. {
  197. need_reopen = 0;
  198. while (1)
  199. {
  200. data = ov_read_float(&file->vf, &pcmbuf, 576, 0);
  201. if ((int)data <= 0)
  202. {
  203. if (data == OV_HOLE) {continue;}
  204. data = 0;
  205. return 0;
  206. }
  207. break;
  208. }
  209. pos = 0;
  210. if (cur_link != file->vf.current_link)
  211. {
  212. vorbis_info* i = ov_info(&file->vf, -1);
  213. if (sr != (UINT)i->rate || src_nch != (UINT)i->channels)
  214. {
  215. UINT old_nch = nch, old_sr = sr;
  216. if (shaper) {delete shaper;shaper = 0;}
  217. sr = i->rate;
  218. src_nch = nch = i->channels;
  219. setup_mc();
  220. if (nch != old_nch || sr != old_sr)
  221. {
  222. need_reopen = 1;
  223. }
  224. }
  225. process_rg();
  226. cur_link = file->vf.current_link;
  227. }
  228. data *= nch;
  229. return 1;
  230. }
  231. int Decoder::Read(UINT bytes, void * buf)
  232. {
  233. UINT wr = 0;
  234. if (buf && bytes && data > 0)
  235. {
  236. char* out = (char*)buf;
  237. UINT d;
  238. double mul;
  239. int ofs;
  240. float * img;
  241. d = bytes / (bps >> 3);
  242. if (d > data) d = data;
  243. if (!d) return 0;
  244. data -= d;
  245. if (useFloat)
  246. {
  247. mul = 1.0;
  248. ofs = 0;
  249. }
  250. else
  251. {
  252. mul = (double)( (1 << ((bps) - 1)) - 1 );
  253. ofs = (bps == 8) ? 0x80 : 0;
  254. }
  255. wr += d * (bps >> 3);
  256. img = (float*)alloca(sizeof(float) * nch);
  257. do
  258. {
  259. UINT cur_ch;
  260. if (nch == 1 && src_nch > 0)
  261. {
  262. UINT c;
  263. img[0] = 0;
  264. for (c = 0;c < src_nch;c++)
  265. {
  266. img[0] += pcmbuf[c][pos];
  267. }
  268. img[0] /= (float)src_nch;
  269. }
  270. else if (nch == src_nch && !(nch == 6 && cfg_mc6_dm == 1))
  271. {
  272. UINT c;
  273. for (c = 0;c < nch;c++)
  274. {
  275. img[c] = pcmbuf[c][pos];
  276. }
  277. }
  278. else if (src_nch == 6)
  279. {
  280. UINT FL, FR, C;
  281. if (cfg_mc6_map == 1)
  282. {
  283. FL = 0;
  284. FR = 1;
  285. C = 2;
  286. }
  287. else
  288. {
  289. FL = 0;
  290. C = 1;
  291. FR = 2;
  292. }
  293. if (nch == 6)
  294. { //remap order for correct 5.1 output
  295. img[0] = pcmbuf[FL][pos];
  296. img[1] = pcmbuf[FR][pos];
  297. img[2] = pcmbuf[C][pos];
  298. img[3] = pcmbuf[5][pos];
  299. img[4] = pcmbuf[3][pos];
  300. img[5] = pcmbuf[4][pos];
  301. }
  302. else if (nch == 2)
  303. {
  304. /*
  305. FL FR C BL BR LFE
  306. 0 1 2 3 4 5
  307. L,C,R,SL,SR,LFE
  308. 0 1 2 3 4 5
  309. output:
  310. FL FR C LFE BL BR
  311. stereo:
  312. Lt=L+0.707*(V-SL-SR+LFE)
  313. Rt=R+0.707*(C+SL+SR+LFE)
  314. Lt=L+0.707*(C+LFE)
  315. Rt=R+0.707*(C+LFE)
  316. SLt=SL
  317. SRt=SR
  318. */
  319. if (cfg_mc6_dm == 4) //ds2
  320. {
  321. const double a = pow(10., 1.5 / 20.), b = 1 / a;
  322. img[0] = pcmbuf[FL][pos] + 0.707 * (pcmbuf[C][pos] - a * pcmbuf[3][pos] - b * pcmbuf[4][pos] + pcmbuf[5][pos]);
  323. img[1] = pcmbuf[FR][pos] + 0.707 * (pcmbuf[C][pos] + b * pcmbuf[3][pos] + a * pcmbuf[4][pos] + pcmbuf[5][pos]);
  324. }
  325. else
  326. {
  327. img[0] = pcmbuf[FL][pos] + 0.707 * (pcmbuf[C][pos] - pcmbuf[3][pos] - pcmbuf[4][pos] + pcmbuf[5][pos]);
  328. img[1] = pcmbuf[FR][pos] + 0.707 * (pcmbuf[C][pos] + pcmbuf[3][pos] + pcmbuf[4][pos] + pcmbuf[5][pos]);
  329. }
  330. }
  331. else if (nch == 4)
  332. {
  333. img[0] = pcmbuf[FL][pos] + 0.707 * (pcmbuf[C][pos] + pcmbuf[5][pos]);
  334. img[1] = pcmbuf[FR][pos] + 0.707 * (pcmbuf[C][pos] + pcmbuf[5][pos]);
  335. img[2] = pcmbuf[3][pos];
  336. img[3] = pcmbuf[4][pos];
  337. }
  338. }
  339. for (cur_ch = 0;cur_ch < nch;cur_ch++)
  340. {
  341. float v = img[cur_ch];
  342. int val;
  343. v *= scale;
  344. v *= mul;
  345. if (dither)
  346. {
  347. if (!shaper)
  348. {
  349. //Shaper(int freq,int _nch,int min,int max,int _dtype,int pdf,double noiseamp);
  350. shaper = new Shaper(sr, nch, clipmin, clipmax, 2, DITHER_TRIANGLE, 0);
  351. }
  352. // double peak=0;
  353. val = shaper->do_shaping(v /*,&peak*/, cur_ch);
  354. //shaper clips for us
  355. }
  356. else
  357. {
  358. val = (int)v;
  359. if (val < clipmin) val = clipmin;
  360. else if (val > clipmax) val = clipmax;
  361. //1<<16 = 0x10000
  362. }
  363. val += ofs;
  364. switch (bps)
  365. {
  366. case 8:
  367. *(BYTE*)out = (UINT)val;
  368. break;
  369. case 16:
  370. *(short*)out = val;
  371. break;
  372. case 24:
  373. {
  374. ((BYTE*)out)[0] = (UINT)val;
  375. ((BYTE*)out)[1] = (UINT)val >> 8;
  376. ((BYTE*)out)[2] = (UINT)val >> 16;
  377. }
  378. break;
  379. case 32:
  380. if (useFloat)
  381. {
  382. *(float *)out = v;
  383. }
  384. else
  385. {
  386. //*(long*)out=val;
  387. //break;
  388. *(long*)out = 0;
  389. }
  390. break;
  391. };
  392. out += (bps >> 3);
  393. d--;
  394. }
  395. pos++;
  396. }
  397. while (d);
  398. }
  399. return wr;
  400. }
  401. int VorbisFile::Seek(double p) { return ov_time_seek(&vf, p);}
  402. int Decoder::Seek(double p)
  403. {
  404. Flush();
  405. return file->Seek(p);
  406. }
  407. //char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count)
  408. const char* VorbisFile::get_meta(const char* tag, UINT c)
  409. {
  410. return vorbis_comment_query(vf.seekable ? vf.vc + vf.current_link : vf.vc, (char*)tag, c);
  411. }