adts_mp2.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include "main.h"
  2. #include "adts_mp2.h"
  3. #include "../winamp/wa_ipc.h"
  4. #include <math.h>
  5. #include "mpegutil.h"
  6. #include "../nsutil/pcm.h"
  7. extern int g_ds;
  8. <<<<<<< HEAD:in_mp3/adts_mp2.cpp
  9. DecoderHooks hooks={mp3GiveVisData, mp2Equalize, mp3Equalize};
  10. =======
  11. >>>>>>> 5058463... fix old-school vis/eq mp3 stuff:mp3/adts_mp2.cpp
  12. ADTS_MP2::ADTS_MP2() : decoder(0), gain(1.f)
  13. {
  14. memset(&hooks, 0, sizeof(hooks));
  15. #ifndef NO_MP3SURROUND
  16. lineFilled=false;
  17. saDecHandle=0;
  18. saMode = SA_DEC_OFF;
  19. #endif
  20. decoderDelay = 529;
  21. endcut=0;
  22. outputFrameSize = 0;
  23. bitsPerSample = 0;
  24. allowRG = false;
  25. useFloat = false;
  26. channels = 0;
  27. sampleRate = 0;
  28. memset(&delayline, 0, sizeof(delayline));
  29. delaylineSize = 0;
  30. }
  31. void ADTS_MP2::SetDecoderHooks(void *layer3_vis, void *layer2_eq, void *layer3_eq)
  32. {
  33. <<<<<<< HEAD:in_mp3/adts_mp2.cpp
  34. //*(void **)&hooks.layer3_vis = layer3_vis;
  35. =======
  36. *(void **)&hooks.layer3_vis = layer3_vis;
  37. >>>>>>> 5058463... fix old-school vis/eq mp3 stuff:mp3/adts_mp2.cpp
  38. *(void **)&hooks.layer2_eq = layer2_eq;
  39. *(void **)&hooks.layer3_eq = layer3_eq;
  40. }
  41. int ADTS_MP2::Initialize(bool forceMono, bool reverseStereo, bool allowSurround, int maxBits, bool _allowRG, bool _useFloat, bool _useCRC)
  42. {
  43. allowRG = _allowRG;
  44. useFloat = _useFloat;
  45. int downmix = 0;
  46. if (reverseStereo)
  47. downmix = 2;
  48. else
  49. downmix = forceMono ? 1 : 0;
  50. bitsPerSample = maxBits;
  51. <<<<<<< HEAD:in_mp3/adts_mp2.cpp
  52. decoder = new CMpgaDecoder(&hooks, g_ds, downmix, !!_useCRC);
  53. =======
  54. decoder = new CMpgaDecoder(hooks.layer3_vis?&hooks:(DecoderHooks *)0, MPEGAUDIO_QUALITY_FULL/*g_ds*/, downmix, _useCRC);
  55. >>>>>>> 5058463... fix old-school vis/eq mp3 stuff:mp3/adts_mp2.cpp
  56. #ifndef NO_MP3SURROUND
  57. if (allowSurround)
  58. IIS_SADec_Init(&saDecHandle, 6);
  59. #endif
  60. return 0;
  61. }
  62. bool ADTS_MP2::Open(ifc_mpeg_stream_reader *file)
  63. {
  64. decoder->Connect((CGioFile *)file);
  65. if (allowRG)
  66. gain = file->MPEGStream_Gain();
  67. return true;
  68. }
  69. void ADTS_MP2::Close()
  70. {
  71. if (decoder)
  72. {
  73. delete decoder;
  74. decoder = 0;
  75. }
  76. #ifndef NO_MP3SURROUND
  77. if (saDecHandle)
  78. IIS_SADec_Free(&saDecHandle);
  79. saDecHandle=0;
  80. #endif
  81. }
  82. void ADTS_MP2::GetOutputParameters(size_t *numBits, int *numChannels, int *sampleRate)
  83. {
  84. *sampleRate = this->sampleRate;
  85. *numChannels = channels;
  86. *numBits = bitsPerSample;
  87. }
  88. void ADTS_MP2::CalculateFrameSize(int *frameSize)
  89. {
  90. *frameSize = outputFrameSize;
  91. if (decoder->GetStreamInfo()->GetLayer() == 1)
  92. *frameSize *= 3;
  93. }
  94. void ADTS_MP2::Flush(ifc_mpeg_stream_reader *file)
  95. {
  96. decoder->Reset();
  97. #ifndef NO_MP3SURROUND
  98. if (saDecHandle)
  99. IIS_SADec_Reset(saDecHandle);
  100. lineFilled=false;
  101. #endif
  102. }
  103. size_t ADTS_MP2::GetCurrentBitrate()
  104. {
  105. return decoder->GetStreamInfo()->GetBitrate() / 1000;
  106. }
  107. size_t ADTS_MP2::GetDecoderDelay()
  108. {
  109. #ifndef NO_MP3SURROUND
  110. if (!saDecHandle || saMode == SA_DEC_OFF || saMode == SA_DEC_BYPASS)
  111. return decoderDelay;
  112. else /* bcc adds 576 delay */
  113. return decoderDelay+576;
  114. #else
  115. return decoderDelay;
  116. #endif
  117. }
  118. static void Decimate(const float *input, void *output, size_t numSamples, size_t *outputWritten, int bitsPerSample, bool useFloat, float gain)
  119. {
  120. if (!useFloat)
  121. {
  122. // TODO seen a few crashes reported where 'output' is 0
  123. nsutil_pcm_FloatToInt_Interleaved_Gain(output, input, bitsPerSample, numSamples, gain);
  124. }
  125. else if (gain != 1.f)
  126. {
  127. float *data = (float *)output;
  128. for (size_t i=0;i<numSamples;i++)
  129. data[i]*=gain;
  130. //data[i]=input[i]*gain;
  131. }
  132. *outputWritten = numSamples * (bitsPerSample / 8);
  133. }
  134. /*
  135. notes for mp3 surround implementations
  136. need to check the first two frames for ancillary data
  137. store first valid in temp
  138. store second valid frame in delay line
  139. decimate first valid into output buffer
  140. ancillary data is stored one frame behind, so PCM data decoded from mp3 frame n combines with anc data from frame n+1
  141. */
  142. int ADTS_MP2::Sync(ifc_mpeg_stream_reader *_file, unsigned __int8 *output, size_t outputSize, size_t *outputWritten, size_t *bitrate)
  143. {
  144. SSC ssc;
  145. CGioFile *file = (CGioFile *)_file;
  146. unsigned char ancBytes[8192] = {0};
  147. int numAncBytes = 0;
  148. unsigned int delay=0, totalLength=0;
  149. float floatTemp[1152*2] = {0};
  150. float *flData=useFloat?(float *)output:floatTemp;
  151. ssc = decoder->DecodeFrame(flData, sizeof(floatTemp), &outputFrameSize,
  152. <<<<<<< HEAD:in_mp3/adts_mp2.cpp
  153. ancBytes, &numAncBytes, 1, &delay, &totalLength);
  154. // TODO: benski> we should really have CGioFile try to read this stuff
  155. if (delay && !file->prepad)
  156. {
  157. // validate
  158. if (delay >= 529)
  159. {
  160. decoderDelay = delay;
  161. endcut = 1152 - ((totalLength + delay) % 1152); // how many 0 samples had to be added?
  162. endcut += decoderDelay; // also need to cut out the encoder+decoder delay
  163. file->m_vbr_samples = totalLength;
  164. }
  165. }
  166. =======
  167. ancBytes, &numAncBytes);
  168. >>>>>>> fd5c493... have CGioFile read the OFL (from newer fraunhofer encoders):mp3/adts_mp2.cpp
  169. switch (ssc)
  170. {
  171. case SSC_OK:
  172. {
  173. channels = decoder->GetStreamInfo()->GetEffectiveChannels();
  174. sampleRate = decoder->GetStreamInfo()->GetEffectiveSFreq();
  175. #ifndef NO_MP3SURROUND
  176. if (!numAncBytes && saDecHandle)
  177. {
  178. ssc = decoder->DecodeFrame(delayline, sizeof(delayline), &delaylineSize,
  179. ancBytes, &numAncBytes);
  180. if (SSC_SUCCESS(ssc))
  181. {
  182. lineFilled=true;
  183. }
  184. else if (ssc == SSC_W_MPGA_SYNCEOF)
  185. return ENDOFFILE;
  186. else
  187. return NEEDMOREDATA;
  188. }
  189. if (saDecHandle)
  190. {
  191. SA_DEC_ERROR sa_error = IIS_SADec_DecodeAncData(saDecHandle, ancBytes, numAncBytes, 0, 0);
  192. if (sa_error == SA_DEC_NO_ERROR)
  193. {
  194. IIS_SADec_InitInfo(saDecHandle, sampleRate, channels);
  195. SA_DEC_INFO saInfo = IIS_SADec_GetInfo(saDecHandle);
  196. sampleRate = saInfo.SampleRate;
  197. channels = saInfo.nChannelsOut;
  198. saMode = saInfo.configuredMode;
  199. }
  200. else if (saMode == SA_DEC_OFF)
  201. {
  202. IIS_SADec_Free(&saDecHandle);
  203. saDecHandle=0;
  204. }
  205. else
  206. {
  207. lineFilled=false;
  208. return NEEDMOREDATA;
  209. }
  210. }
  211. if (saDecHandle)
  212. {
  213. float surroundFloatTemp[1152*6] = {0};
  214. int outputSamples = 0;
  215. /*SA_DEC_ERROR sa_error = */IIS_SADec_DecodeFrame(saDecHandle,
  216. flData, outputFrameSize/sizeof(float),
  217. (char *)ancBytes, numAncBytes,
  218. surroundFloatTemp, sizeof(surroundFloatTemp),
  219. &outputSamples, saMode, 0, 0,
  220. 0, 0);
  221. if (useFloat)
  222. memcpy(output, surroundFloatTemp, sizeof(surroundFloatTemp));
  223. Decimate(surroundFloatTemp, output, outputSamples, outputWritten, bitsPerSample, useFloat, (float)gain);
  224. outputFrameSize = *outputWritten;
  225. }
  226. else
  227. #endif
  228. {
  229. Decimate(floatTemp, output, outputFrameSize / sizeof(float), outputWritten, bitsPerSample, useFloat, (float)gain);
  230. outputFrameSize = *outputWritten;
  231. }
  232. }
  233. return SSC_OK;
  234. case SSC_W_MPGA_SYNCSEARCHED:
  235. return NEEDMOREDATA;
  236. case SSC_W_MPGA_SYNCEOF:
  237. return ENDOFFILE;
  238. case SSC_E_MPGA_WRONGLAYER:
  239. decoder->m_Mbs.Seek(1);
  240. default:
  241. return NEEDMOREDATA;
  242. }
  243. }
  244. int ADTS_MP2::Decode(ifc_mpeg_stream_reader *file, unsigned __int8 *output, size_t outputSize, size_t *outputWritten, size_t *bitrate, size_t *endCut)
  245. {
  246. if (endcut)
  247. {
  248. *endCut = endcut;
  249. endcut=0;
  250. }
  251. #ifndef NO_MP3SURROUND
  252. if (!saDecHandle && lineFilled) // if we don't have surround info, go ahead and flush out the delayline buffer
  253. {
  254. Decimate(delayline, output, delaylineSize / sizeof(float), outputWritten, bitsPerSample, useFloat, (float)gain);
  255. *bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
  256. lineFilled=false;
  257. return adts::SUCCESS;
  258. }
  259. if (saDecHandle && !lineFilled && !decoder->IsEof()) // have surround info, but don't have a previously decoded frame
  260. {
  261. // resync
  262. int ret = Sync(file, output, outputSize, outputWritten, bitrate);
  263. if (ret == SSC_OK && saDecHandle)
  264. {
  265. *bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
  266. return adts::SUCCESS;
  267. }
  268. else if (saDecHandle)
  269. return ret;
  270. else
  271. return adts::FAILURE;
  272. }
  273. #endif
  274. unsigned char ancBytes[8192] = {0};
  275. int numAncBytes = 0;
  276. int newl;
  277. float floatTemp[1152*2] = {0};
  278. float *flData=useFloat?(float *)output:floatTemp;
  279. SSC ssc = decoder->DecodeFrame(flData, sizeof(floatTemp), &newl, ancBytes, &numAncBytes);
  280. if (SSC_SUCCESS(ssc))
  281. {
  282. #ifndef NO_MP3SURROUND
  283. if (saDecHandle && lineFilled)
  284. {
  285. float surroundFloatTemp[1152*6] = {0};
  286. int outputSamples;
  287. /*SA_DEC_ERROR sa_error = */IIS_SADec_DecodeFrame(saDecHandle,
  288. delayline, delaylineSize/sizeof(float),
  289. (char *)ancBytes, numAncBytes,
  290. surroundFloatTemp, sizeof(surroundFloatTemp),
  291. &outputSamples, saMode, 0, 0,
  292. 0, 0);
  293. if (useFloat)
  294. memcpy(output, surroundFloatTemp, sizeof(surroundFloatTemp));
  295. Decimate(surroundFloatTemp, output, outputSamples, outputWritten, bitsPerSample, useFloat, (float)gain);
  296. memcpy(delayline, flData, delaylineSize);
  297. }
  298. else
  299. #endif
  300. {
  301. Decimate(floatTemp, output, newl / sizeof(float), outputWritten, bitsPerSample, useFloat, (float)gain);
  302. }
  303. *bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
  304. return adts::SUCCESS;
  305. }
  306. else if (decoder->IsEof())
  307. {
  308. #ifndef NO_MP3SURROUND
  309. /* In case of SA processing one ancillary data
  310. package and maybe fill samples left in the dynamic
  311. buffer of the mp3 decoder. Take care, that the
  312. ancillary data buffer is greater then the dynamic buffer of
  313. the mp3 decoder. */
  314. if (saDecHandle && lineFilled)
  315. {
  316. decoder->GetLastAncData(ancBytes, &numAncBytes);
  317. float surroundFloatTemp[1152*6];
  318. int outputSamples = 0;
  319. /*SA_DEC_ERROR sa_error = */IIS_SADec_DecodeFrame(saDecHandle,
  320. delayline, delaylineSize/sizeof(float),
  321. (char *)ancBytes, numAncBytes,
  322. surroundFloatTemp, sizeof(surroundFloatTemp),
  323. &outputSamples, saMode, 0, 0,
  324. 0, 0);
  325. if (useFloat)
  326. memcpy(output, surroundFloatTemp, sizeof(surroundFloatTemp));
  327. Decimate(surroundFloatTemp, output, outputSamples, outputWritten, bitsPerSample, useFloat, (float)gain);
  328. lineFilled=false;
  329. *bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
  330. return adts::SUCCESS;
  331. }
  332. else
  333. #endif
  334. return adts::ENDOFFILE;
  335. }
  336. else if (ssc == SSC_W_MPGA_SYNCNEEDDATA)
  337. {
  338. *bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
  339. return adts::NEEDMOREDATA;
  340. }
  341. else if (ssc==SSC_W_MPGA_SYNCLOST || ssc==SSC_W_MPGA_SYNCSEARCHED)
  342. {
  343. *bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
  344. return adts::NEEDSYNC;
  345. }
  346. else
  347. {
  348. if (ssc == SSC_E_MPGA_WRONGLAYER)
  349. decoder->m_Mbs.Seek(1);
  350. return adts::FAILURE;
  351. }
  352. }
  353. int ADTS_MP2::GetLayer()
  354. {
  355. if (decoder)
  356. return decoder->GetStreamInfo()->GetLayer();
  357. else
  358. return 0;
  359. }
  360. void ADTS_MP2::Release()
  361. {
  362. delete this;
  363. }