/indra/llaudio/llaudiodecodemgr.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 689 lines · 501 code · 101 blank · 87 comment · 59 complexity · b657995c3a11760cddbeee33ed28d94e MD5 · raw file

  1. /**
  2. * @file llaudiodecodemgr.cpp
  3. *
  4. * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library 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;
  11. * version 2.1 of the License only.
  12. *
  13. * This library 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 this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #include "linden_common.h"
  26. #include "llaudiodecodemgr.h"
  27. #include "llaudioengine.h"
  28. #include "lllfsthread.h"
  29. #include "llvfile.h"
  30. #include "llstring.h"
  31. #include "lldir.h"
  32. #include "llendianswizzle.h"
  33. #include "llassetstorage.h"
  34. #include "llrefcount.h"
  35. #include "llvorbisencode.h"
  36. #include "vorbis/codec.h"
  37. #include "vorbis/vorbisfile.h"
  38. extern LLAudioEngine *gAudiop;
  39. LLAudioDecodeMgr *gAudioDecodeMgrp = NULL;
  40. static const S32 WAV_HEADER_SIZE = 44;
  41. //////////////////////////////////////////////////////////////////////////////
  42. class LLVorbisDecodeState : public LLRefCount
  43. {
  44. public:
  45. class WriteResponder : public LLLFSThread::Responder
  46. {
  47. public:
  48. WriteResponder(LLVorbisDecodeState* decoder) : mDecoder(decoder) {}
  49. ~WriteResponder() {}
  50. void completed(S32 bytes)
  51. {
  52. mDecoder->ioComplete(bytes);
  53. }
  54. LLPointer<LLVorbisDecodeState> mDecoder;
  55. };
  56. LLVorbisDecodeState(const LLUUID &uuid, const std::string &out_filename);
  57. BOOL initDecode();
  58. BOOL decodeSection(); // Return TRUE if done.
  59. BOOL finishDecode();
  60. void flushBadFile();
  61. void ioComplete(S32 bytes) { mBytesRead = bytes; }
  62. BOOL isValid() const { return mValid; }
  63. BOOL isDone() const { return mDone; }
  64. const LLUUID &getUUID() const { return mUUID; }
  65. protected:
  66. virtual ~LLVorbisDecodeState();
  67. BOOL mValid;
  68. BOOL mDone;
  69. LLAtomicS32 mBytesRead;
  70. LLUUID mUUID;
  71. std::vector<U8> mWAVBuffer;
  72. #if !defined(USE_WAV_VFILE)
  73. std::string mOutFilename;
  74. LLLFSThread::handle_t mFileHandle;
  75. #endif
  76. LLVFile *mInFilep;
  77. OggVorbis_File mVF;
  78. S32 mCurrentSection;
  79. };
  80. size_t vfs_read(void *ptr, size_t size, size_t nmemb, void *datasource)
  81. {
  82. LLVFile *file = (LLVFile *)datasource;
  83. if (file->read((U8*)ptr, (S32)(size * nmemb))) /*Flawfinder: ignore*/
  84. {
  85. S32 read = file->getLastBytesRead();
  86. return read / size; /*Flawfinder: ignore*/
  87. }
  88. else
  89. {
  90. return 0;
  91. }
  92. }
  93. int vfs_seek(void *datasource, ogg_int64_t offset, int whence)
  94. {
  95. LLVFile *file = (LLVFile *)datasource;
  96. // vfs has 31-bit files
  97. if (offset > S32_MAX)
  98. {
  99. return -1;
  100. }
  101. S32 origin;
  102. switch (whence) {
  103. case SEEK_SET:
  104. origin = 0;
  105. break;
  106. case SEEK_END:
  107. origin = file->getSize();
  108. break;
  109. case SEEK_CUR:
  110. origin = -1;
  111. break;
  112. default:
  113. llerrs << "Invalid whence argument to vfs_seek" << llendl;
  114. return -1;
  115. }
  116. if (file->seek((S32)offset, origin))
  117. {
  118. return 0;
  119. }
  120. else
  121. {
  122. return -1;
  123. }
  124. }
  125. int vfs_close (void *datasource)
  126. {
  127. LLVFile *file = (LLVFile *)datasource;
  128. delete file;
  129. return 0;
  130. }
  131. long vfs_tell (void *datasource)
  132. {
  133. LLVFile *file = (LLVFile *)datasource;
  134. return file->tell();
  135. }
  136. LLVorbisDecodeState::LLVorbisDecodeState(const LLUUID &uuid, const std::string &out_filename)
  137. {
  138. mDone = FALSE;
  139. mValid = FALSE;
  140. mBytesRead = -1;
  141. mUUID = uuid;
  142. mInFilep = NULL;
  143. mCurrentSection = 0;
  144. #if !defined(USE_WAV_VFILE)
  145. mOutFilename = out_filename;
  146. mFileHandle = LLLFSThread::nullHandle();
  147. #endif
  148. // No default value for mVF, it's an ogg structure?
  149. // Hey, let's zero it anyway, for predictability.
  150. memset(&mVF, 0, sizeof(mVF));
  151. }
  152. LLVorbisDecodeState::~LLVorbisDecodeState()
  153. {
  154. if (!mDone)
  155. {
  156. delete mInFilep;
  157. mInFilep = NULL;
  158. }
  159. }
  160. BOOL LLVorbisDecodeState::initDecode()
  161. {
  162. ov_callbacks vfs_callbacks;
  163. vfs_callbacks.read_func = vfs_read;
  164. vfs_callbacks.seek_func = vfs_seek;
  165. vfs_callbacks.close_func = vfs_close;
  166. vfs_callbacks.tell_func = vfs_tell;
  167. //llinfos << "Initing decode from vfile: " << mUUID << llendl;
  168. mInFilep = new LLVFile(gVFS, mUUID, LLAssetType::AT_SOUND);
  169. if (!mInFilep || !mInFilep->getSize())
  170. {
  171. llwarns << "unable to open vorbis source vfile for reading" << llendl;
  172. delete mInFilep;
  173. mInFilep = NULL;
  174. return FALSE;
  175. }
  176. int r = ov_open_callbacks(mInFilep, &mVF, NULL, 0, vfs_callbacks);
  177. if(r < 0)
  178. {
  179. llwarns << r << " Input to vorbis decode does not appear to be an Ogg bitstream: " << mUUID << llendl;
  180. return(FALSE);
  181. }
  182. S32 sample_count = ov_pcm_total(&mVF, -1);
  183. size_t size_guess = (size_t)sample_count;
  184. vorbis_info* vi = ov_info(&mVF, -1);
  185. size_guess *= (vi? vi->channels : 1);
  186. size_guess *= 2;
  187. size_guess += 2048;
  188. bool abort_decode = false;
  189. if (vi)
  190. {
  191. if( vi->channels < 1 || vi->channels > LLVORBIS_CLIP_MAX_CHANNELS )
  192. {
  193. abort_decode = true;
  194. llwarns << "Bad channel count: " << vi->channels << llendl;
  195. }
  196. }
  197. else // !vi
  198. {
  199. abort_decode = true;
  200. llwarns << "No default bitstream found" << llendl;
  201. }
  202. if( (size_t)sample_count > LLVORBIS_CLIP_REJECT_SAMPLES ||
  203. (size_t)sample_count <= 0)
  204. {
  205. abort_decode = true;
  206. llwarns << "Illegal sample count: " << sample_count << llendl;
  207. }
  208. if( size_guess > LLVORBIS_CLIP_REJECT_SIZE ||
  209. size_guess < 0)
  210. {
  211. abort_decode = true;
  212. llwarns << "Illegal sample size: " << size_guess << llendl;
  213. }
  214. if( abort_decode )
  215. {
  216. llwarns << "Canceling initDecode. Bad asset: " << mUUID << llendl;
  217. vorbis_comment* comment = ov_comment(&mVF,-1);
  218. if (comment && comment->vendor)
  219. {
  220. llwarns << "Bad asset encoded by: " << comment->vendor << llendl;
  221. }
  222. delete mInFilep;
  223. mInFilep = NULL;
  224. return FALSE;
  225. }
  226. mWAVBuffer.reserve(size_guess);
  227. mWAVBuffer.resize(WAV_HEADER_SIZE);
  228. {
  229. // write the .wav format header
  230. //"RIFF"
  231. mWAVBuffer[0] = 0x52;
  232. mWAVBuffer[1] = 0x49;
  233. mWAVBuffer[2] = 0x46;
  234. mWAVBuffer[3] = 0x46;
  235. // length = datalen + 36 (to be filled in later)
  236. mWAVBuffer[4] = 0x00;
  237. mWAVBuffer[5] = 0x00;
  238. mWAVBuffer[6] = 0x00;
  239. mWAVBuffer[7] = 0x00;
  240. //"WAVE"
  241. mWAVBuffer[8] = 0x57;
  242. mWAVBuffer[9] = 0x41;
  243. mWAVBuffer[10] = 0x56;
  244. mWAVBuffer[11] = 0x45;
  245. // "fmt "
  246. mWAVBuffer[12] = 0x66;
  247. mWAVBuffer[13] = 0x6D;
  248. mWAVBuffer[14] = 0x74;
  249. mWAVBuffer[15] = 0x20;
  250. // chunk size = 16
  251. mWAVBuffer[16] = 0x10;
  252. mWAVBuffer[17] = 0x00;
  253. mWAVBuffer[18] = 0x00;
  254. mWAVBuffer[19] = 0x00;
  255. // format (1 = PCM)
  256. mWAVBuffer[20] = 0x01;
  257. mWAVBuffer[21] = 0x00;
  258. // number of channels
  259. mWAVBuffer[22] = 0x01;
  260. mWAVBuffer[23] = 0x00;
  261. // samples per second
  262. mWAVBuffer[24] = 0x44;
  263. mWAVBuffer[25] = 0xAC;
  264. mWAVBuffer[26] = 0x00;
  265. mWAVBuffer[27] = 0x00;
  266. // average bytes per second
  267. mWAVBuffer[28] = 0x88;
  268. mWAVBuffer[29] = 0x58;
  269. mWAVBuffer[30] = 0x01;
  270. mWAVBuffer[31] = 0x00;
  271. // bytes to output at a single time
  272. mWAVBuffer[32] = 0x02;
  273. mWAVBuffer[33] = 0x00;
  274. // 16 bits per sample
  275. mWAVBuffer[34] = 0x10;
  276. mWAVBuffer[35] = 0x00;
  277. // "data"
  278. mWAVBuffer[36] = 0x64;
  279. mWAVBuffer[37] = 0x61;
  280. mWAVBuffer[38] = 0x74;
  281. mWAVBuffer[39] = 0x61;
  282. // these are the length of the data chunk, to be filled in later
  283. mWAVBuffer[40] = 0x00;
  284. mWAVBuffer[41] = 0x00;
  285. mWAVBuffer[42] = 0x00;
  286. mWAVBuffer[43] = 0x00;
  287. }
  288. //{
  289. //char **ptr=ov_comment(&mVF,-1)->user_comments;
  290. // vorbis_info *vi=ov_info(&vf,-1);
  291. //while(*ptr){
  292. // fprintf(stderr,"%s\n",*ptr);
  293. // ++ptr;
  294. //}
  295. // fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
  296. // fprintf(stderr,"\nDecoded length: %ld samples\n", (long)ov_pcm_total(&vf,-1));
  297. // fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
  298. //}
  299. return TRUE;
  300. }
  301. BOOL LLVorbisDecodeState::decodeSection()
  302. {
  303. if (!mInFilep)
  304. {
  305. llwarns << "No VFS file to decode in vorbis!" << llendl;
  306. return TRUE;
  307. }
  308. if (mDone)
  309. {
  310. // llwarns << "Already done with decode, aborting!" << llendl;
  311. return TRUE;
  312. }
  313. char pcmout[4096]; /*Flawfinder: ignore*/
  314. BOOL eof = FALSE;
  315. long ret=ov_read(&mVF, pcmout, sizeof(pcmout), 0, 2, 1, &mCurrentSection);
  316. if (ret == 0)
  317. {
  318. /* EOF */
  319. eof = TRUE;
  320. mDone = TRUE;
  321. mValid = TRUE;
  322. // llinfos << "Vorbis EOF" << llendl;
  323. }
  324. else if (ret < 0)
  325. {
  326. /* error in the stream. Not a problem, just reporting it in
  327. case we (the app) cares. In this case, we don't. */
  328. llwarns << "BAD vorbis decode in decodeSection." << llendl;
  329. mValid = FALSE;
  330. mDone = TRUE;
  331. // We're done, return TRUE.
  332. return TRUE;
  333. }
  334. else
  335. {
  336. // llinfos << "Vorbis read " << ret << "bytes" << llendl;
  337. /* we don't bother dealing with sample rate changes, etc, but.
  338. you'll have to*/
  339. std::copy(pcmout, pcmout+ret, std::back_inserter(mWAVBuffer));
  340. }
  341. return eof;
  342. }
  343. BOOL LLVorbisDecodeState::finishDecode()
  344. {
  345. if (!isValid())
  346. {
  347. llwarns << "Bogus vorbis decode state for " << getUUID() << ", aborting!" << llendl;
  348. return TRUE; // We've finished
  349. }
  350. #if !defined(USE_WAV_VFILE)
  351. if (mFileHandle == LLLFSThread::nullHandle())
  352. #endif
  353. {
  354. ov_clear(&mVF);
  355. // write "data" chunk length, in little-endian format
  356. S32 data_length = mWAVBuffer.size() - WAV_HEADER_SIZE;
  357. mWAVBuffer[40] = (data_length) & 0x000000FF;
  358. mWAVBuffer[41] = (data_length >> 8) & 0x000000FF;
  359. mWAVBuffer[42] = (data_length >> 16) & 0x000000FF;
  360. mWAVBuffer[43] = (data_length >> 24) & 0x000000FF;
  361. // write overall "RIFF" length, in little-endian format
  362. data_length += 36;
  363. mWAVBuffer[4] = (data_length) & 0x000000FF;
  364. mWAVBuffer[5] = (data_length >> 8) & 0x000000FF;
  365. mWAVBuffer[6] = (data_length >> 16) & 0x000000FF;
  366. mWAVBuffer[7] = (data_length >> 24) & 0x000000FF;
  367. //
  368. // FUDGECAKES!!! Vorbis encode/decode messes up loop point transitions (pop)
  369. // do a cheap-and-cheesy crossfade
  370. //
  371. {
  372. S16 *samplep;
  373. S32 i;
  374. S32 fade_length;
  375. char pcmout[4096]; /*Flawfinder: ignore*/
  376. fade_length = llmin((S32)128,(S32)(data_length-36)/8);
  377. if((S32)mWAVBuffer.size() >= (WAV_HEADER_SIZE + 2* fade_length))
  378. {
  379. memcpy(pcmout, &mWAVBuffer[WAV_HEADER_SIZE], (2 * fade_length)); /*Flawfinder: ignore*/
  380. }
  381. llendianswizzle(&pcmout, 2, fade_length);
  382. samplep = (S16 *)pcmout;
  383. for (i = 0 ;i < fade_length; i++)
  384. {
  385. *samplep = llfloor((F32)*samplep * ((F32)i/(F32)fade_length));
  386. samplep++;
  387. }
  388. llendianswizzle(&pcmout, 2, fade_length);
  389. if((WAV_HEADER_SIZE+(2 * fade_length)) < (S32)mWAVBuffer.size())
  390. {
  391. memcpy(&mWAVBuffer[WAV_HEADER_SIZE], pcmout, (2 * fade_length)); /*Flawfinder: ignore*/
  392. }
  393. S32 near_end = mWAVBuffer.size() - (2 * fade_length);
  394. if ((S32)mWAVBuffer.size() >= ( near_end + 2* fade_length))
  395. {
  396. memcpy(pcmout, &mWAVBuffer[near_end], (2 * fade_length)); /*Flawfinder: ignore*/
  397. }
  398. llendianswizzle(&pcmout, 2, fade_length);
  399. samplep = (S16 *)pcmout;
  400. for (i = fade_length-1 ; i >= 0; i--)
  401. {
  402. *samplep = llfloor((F32)*samplep * ((F32)i/(F32)fade_length));
  403. samplep++;
  404. }
  405. llendianswizzle(&pcmout, 2, fade_length);
  406. if (near_end + (2 * fade_length) < (S32)mWAVBuffer.size())
  407. {
  408. memcpy(&mWAVBuffer[near_end], pcmout, (2 * fade_length));/*Flawfinder: ignore*/
  409. }
  410. }
  411. if (36 == data_length)
  412. {
  413. llwarns << "BAD Vorbis decode in finishDecode!" << llendl;
  414. mValid = FALSE;
  415. return TRUE; // we've finished
  416. }
  417. #if !defined(USE_WAV_VFILE)
  418. mBytesRead = -1;
  419. mFileHandle = LLLFSThread::sLocal->write(mOutFilename, &mWAVBuffer[0], 0, mWAVBuffer.size(),
  420. new WriteResponder(this));
  421. #endif
  422. }
  423. if (mFileHandle != LLLFSThread::nullHandle())
  424. {
  425. if (mBytesRead >= 0)
  426. {
  427. if (mBytesRead == 0)
  428. {
  429. llwarns << "Unable to write file in LLVorbisDecodeState::finishDecode" << llendl;
  430. mValid = FALSE;
  431. return TRUE; // we've finished
  432. }
  433. }
  434. else
  435. {
  436. return FALSE; // not done
  437. }
  438. }
  439. mDone = TRUE;
  440. #if defined(USE_WAV_VFILE)
  441. // write the data.
  442. LLVFile output(gVFS, mUUID, LLAssetType::AT_SOUND_WAV);
  443. output.write(&mWAVBuffer[0], mWAVBuffer.size());
  444. #endif
  445. //llinfos << "Finished decode for " << getUUID() << llendl;
  446. return TRUE;
  447. }
  448. void LLVorbisDecodeState::flushBadFile()
  449. {
  450. if (mInFilep)
  451. {
  452. llwarns << "Flushing bad vorbis file from VFS for " << mUUID << llendl;
  453. mInFilep->remove();
  454. }
  455. }
  456. //////////////////////////////////////////////////////////////////////////////
  457. class LLAudioDecodeMgr::Impl
  458. {
  459. friend class LLAudioDecodeMgr;
  460. public:
  461. Impl() {};
  462. ~Impl() {};
  463. void processQueue(const F32 num_secs = 0.005);
  464. protected:
  465. LLLinkedQueue<LLUUID> mDecodeQueue;
  466. LLPointer<LLVorbisDecodeState> mCurrentDecodep;
  467. };
  468. void LLAudioDecodeMgr::Impl::processQueue(const F32 num_secs)
  469. {
  470. LLUUID uuid;
  471. LLTimer decode_timer;
  472. BOOL done = FALSE;
  473. while (!done)
  474. {
  475. if (mCurrentDecodep)
  476. {
  477. BOOL res;
  478. // Decode in a loop until we're done or have run out of time.
  479. while(!(res = mCurrentDecodep->decodeSection()) && (decode_timer.getElapsedTimeF32() < num_secs))
  480. {
  481. // decodeSection does all of the work above
  482. }
  483. if (mCurrentDecodep->isDone() && !mCurrentDecodep->isValid())
  484. {
  485. // We had an error when decoding, abort.
  486. llwarns << mCurrentDecodep->getUUID() << " has invalid vorbis data, aborting decode" << llendl;
  487. mCurrentDecodep->flushBadFile();
  488. LLAudioData *adp = gAudiop->getAudioData(mCurrentDecodep->getUUID());
  489. adp->setHasValidData(FALSE);
  490. mCurrentDecodep = NULL;
  491. done = TRUE;
  492. }
  493. if (!res)
  494. {
  495. // We've used up out time slice, bail...
  496. done = TRUE;
  497. }
  498. else if (mCurrentDecodep)
  499. {
  500. if (mCurrentDecodep->finishDecode())
  501. {
  502. // We finished!
  503. if (mCurrentDecodep->isValid() && mCurrentDecodep->isDone())
  504. {
  505. LLAudioData *adp = gAudiop->getAudioData(mCurrentDecodep->getUUID());
  506. adp->setHasDecodedData(TRUE);
  507. adp->setHasValidData(TRUE);
  508. // At this point, we could see if anyone needs this sound immediately, but
  509. // I'm not sure that there's a reason to - we need to poll all of the playing
  510. // sounds anyway.
  511. //llinfos << "Finished the vorbis decode, now what?" << llendl;
  512. }
  513. else
  514. {
  515. llinfos << "Vorbis decode failed!!!" << llendl;
  516. }
  517. mCurrentDecodep = NULL;
  518. }
  519. done = TRUE; // done for now
  520. }
  521. }
  522. if (!done)
  523. {
  524. if (!mDecodeQueue.getLength())
  525. {
  526. // Nothing else on the queue.
  527. done = TRUE;
  528. }
  529. else
  530. {
  531. LLUUID uuid;
  532. mDecodeQueue.pop(uuid);
  533. if (gAudiop->hasDecodedFile(uuid))
  534. {
  535. // This file has already been decoded, don't decode it again.
  536. continue;
  537. }
  538. lldebugs << "Decoding " << uuid << " from audio queue!" << llendl;
  539. std::string uuid_str;
  540. std::string d_path;
  541. LLTimer timer;
  542. timer.reset();
  543. uuid.toString(uuid_str);
  544. d_path = gDirUtilp->getExpandedFilename(LL_PATH_CACHE,uuid_str) + ".dsf";
  545. mCurrentDecodep = new LLVorbisDecodeState(uuid, d_path);
  546. if (!mCurrentDecodep->initDecode())
  547. {
  548. mCurrentDecodep = NULL;
  549. }
  550. }
  551. }
  552. }
  553. }
  554. //////////////////////////////////////////////////////////////////////////////
  555. LLAudioDecodeMgr::LLAudioDecodeMgr()
  556. {
  557. mImpl = new Impl;
  558. }
  559. LLAudioDecodeMgr::~LLAudioDecodeMgr()
  560. {
  561. delete mImpl;
  562. }
  563. void LLAudioDecodeMgr::processQueue(const F32 num_secs)
  564. {
  565. mImpl->processQueue(num_secs);
  566. }
  567. BOOL LLAudioDecodeMgr::addDecodeRequest(const LLUUID &uuid)
  568. {
  569. if (gAudiop->hasDecodedFile(uuid))
  570. {
  571. // Already have a decoded version, don't need to decode it.
  572. return TRUE;
  573. }
  574. if (gAssetStorage->hasLocalAsset(uuid, LLAssetType::AT_SOUND))
  575. {
  576. // Just put it on the decode queue.
  577. mImpl->mDecodeQueue.push(uuid);
  578. return TRUE;
  579. }
  580. return FALSE;
  581. }
  582. #if LL_DARWIN || LL_LINUX
  583. // HACK: to fool the compiler into not emitting unused warnings.
  584. namespace {
  585. const ov_callbacks callback_array[4] = {OV_CALLBACKS_DEFAULT, OV_CALLBACKS_NOCLOSE, OV_CALLBACKS_STREAMONLY,
  586. OV_CALLBACKS_STREAMONLY_NOCLOSE};
  587. }
  588. #endif