The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_QUICKTIME && ! (JUCE_64BIT || JUCE_IOS)
  18. } // (juce namespace)
  19. #if ! JUCE_WINDOWS
  20. #define Point CarbonDummyPointName // (workaround to avoid definition of "Point" by old Carbon headers)
  21. #define Component CarbonDummyCompName
  22. #include <QuickTime/Movies.h>
  23. #include <QuickTime/QTML.h>
  24. #include <QuickTime/QuickTimeComponents.h>
  25. #include <QuickTime/MediaHandlers.h>
  26. #include <QuickTime/ImageCodec.h>
  27. #undef Point
  28. #undef Component
  29. #else
  30. #if JUCE_MSVC
  31. #pragma warning (push)
  32. #pragma warning (disable : 4100)
  33. #endif
  34. /* If you've got an include error here, you probably need to install the QuickTime SDK and
  35. add its header directory to your include path.
  36. Alternatively, if you don't need any QuickTime services, just set the JUCE_QUICKTIME flag to 0.
  37. */
  38. #undef SIZE_MAX
  39. #include <Movies.h>
  40. #include <QTML.h>
  41. #include <QuickTimeComponents.h>
  42. #include <MediaHandlers.h>
  43. #include <ImageCodec.h>
  44. #undef SIZE_MAX
  45. #if JUCE_MSVC
  46. #pragma warning (pop)
  47. #endif
  48. #endif
  49. namespace juce
  50. {
  51. bool juce_OpenQuickTimeMovieFromStream (InputStream* input, Movie& movie, Handle& dataHandle);
  52. static const char* const quickTimeFormatName = "QuickTime file";
  53. //==============================================================================
  54. class QTAudioReader : public AudioFormatReader
  55. {
  56. public:
  57. QTAudioReader (InputStream* const input_, const int trackNum_)
  58. : AudioFormatReader (input_, quickTimeFormatName),
  59. ok (false),
  60. movie (0),
  61. trackNum (trackNum_),
  62. lastSampleRead (0),
  63. lastThreadId (0),
  64. extractor (0),
  65. dataHandle (0)
  66. {
  67. JUCE_AUTORELEASEPOOL
  68. {
  69. bufferList.calloc (256, 1);
  70. #if JUCE_WINDOWS
  71. if (InitializeQTML (0) != noErr)
  72. return;
  73. #endif
  74. if (EnterMovies() != noErr)
  75. return;
  76. bool opened = juce_OpenQuickTimeMovieFromStream (input_, movie, dataHandle);
  77. if (! opened)
  78. return;
  79. {
  80. const int numTracks = GetMovieTrackCount (movie);
  81. int trackCount = 0;
  82. for (int i = 1; i <= numTracks; ++i)
  83. {
  84. track = GetMovieIndTrack (movie, i);
  85. media = GetTrackMedia (track);
  86. OSType mediaType;
  87. GetMediaHandlerDescription (media, &mediaType, 0, 0);
  88. if (mediaType == SoundMediaType
  89. && trackCount++ == trackNum_)
  90. {
  91. ok = true;
  92. break;
  93. }
  94. }
  95. }
  96. if (! ok)
  97. return;
  98. ok = false;
  99. lengthInSamples = GetMediaDecodeDuration (media);
  100. usesFloatingPointData = false;
  101. samplesPerFrame = (int) (GetMediaDecodeDuration (media) / GetMediaSampleCount (media));
  102. trackUnitsPerFrame = GetMovieTimeScale (movie) * samplesPerFrame
  103. / GetMediaTimeScale (media);
  104. MovieAudioExtractionBegin (movie, 0, &extractor);
  105. unsigned long output_layout_size;
  106. OSStatus err = MovieAudioExtractionGetPropertyInfo (extractor,
  107. kQTPropertyClass_MovieAudioExtraction_Audio,
  108. kQTMovieAudioExtractionAudioPropertyID_AudioChannelLayout,
  109. 0, &output_layout_size, 0);
  110. if (err != noErr)
  111. return;
  112. HeapBlock <AudioChannelLayout> qt_audio_channel_layout;
  113. qt_audio_channel_layout.calloc (output_layout_size, 1);
  114. MovieAudioExtractionGetProperty (extractor,
  115. kQTPropertyClass_MovieAudioExtraction_Audio,
  116. kQTMovieAudioExtractionAudioPropertyID_AudioChannelLayout,
  117. output_layout_size, qt_audio_channel_layout, 0);
  118. qt_audio_channel_layout[0].mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
  119. MovieAudioExtractionSetProperty (extractor,
  120. kQTPropertyClass_MovieAudioExtraction_Audio,
  121. kQTMovieAudioExtractionAudioPropertyID_AudioChannelLayout,
  122. output_layout_size,
  123. qt_audio_channel_layout);
  124. err = MovieAudioExtractionGetProperty (extractor,
  125. kQTPropertyClass_MovieAudioExtraction_Audio,
  126. kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription,
  127. sizeof (inputStreamDesc),
  128. &inputStreamDesc, 0);
  129. if (err != noErr)
  130. return;
  131. inputStreamDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger
  132. | kAudioFormatFlagIsPacked
  133. | kAudioFormatFlagsNativeEndian;
  134. inputStreamDesc.mBitsPerChannel = sizeof (SInt16) * 8;
  135. inputStreamDesc.mChannelsPerFrame = jmin ((UInt32) 2, inputStreamDesc.mChannelsPerFrame);
  136. inputStreamDesc.mBytesPerFrame = sizeof (SInt16) * inputStreamDesc.mChannelsPerFrame;
  137. inputStreamDesc.mBytesPerPacket = inputStreamDesc.mBytesPerFrame;
  138. err = MovieAudioExtractionSetProperty (extractor,
  139. kQTPropertyClass_MovieAudioExtraction_Audio,
  140. kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription,
  141. sizeof (inputStreamDesc),
  142. &inputStreamDesc);
  143. if (err != noErr)
  144. return;
  145. Boolean allChannelsDiscrete = false;
  146. err = MovieAudioExtractionSetProperty (extractor,
  147. kQTPropertyClass_MovieAudioExtraction_Movie,
  148. kQTMovieAudioExtractionMoviePropertyID_AllChannelsDiscrete,
  149. sizeof (allChannelsDiscrete),
  150. &allChannelsDiscrete);
  151. if (err != noErr)
  152. return;
  153. bufferList->mNumberBuffers = 1;
  154. bufferList->mBuffers[0].mNumberChannels = inputStreamDesc.mChannelsPerFrame;
  155. bufferList->mBuffers[0].mDataByteSize = jmax ((UInt32) 4096, (UInt32) (samplesPerFrame * inputStreamDesc.mBytesPerFrame) + 16);
  156. dataBuffer.malloc (bufferList->mBuffers[0].mDataByteSize);
  157. bufferList->mBuffers[0].mData = dataBuffer;
  158. sampleRate = inputStreamDesc.mSampleRate;
  159. bitsPerSample = 16;
  160. numChannels = inputStreamDesc.mChannelsPerFrame;
  161. detachThread();
  162. ok = true;
  163. }
  164. }
  165. ~QTAudioReader()
  166. {
  167. JUCE_AUTORELEASEPOOL
  168. {
  169. checkThreadIsAttached();
  170. if (dataHandle != nullptr)
  171. DisposeHandle (dataHandle);
  172. if (extractor != nullptr)
  173. {
  174. MovieAudioExtractionEnd (extractor);
  175. extractor = nullptr;
  176. }
  177. DisposeMovie (movie);
  178. #if JUCE_MAC
  179. ExitMoviesOnThread ();
  180. #endif
  181. }
  182. }
  183. bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
  184. int64 startSampleInFile, int numSamples)
  185. {
  186. JUCE_AUTORELEASEPOOL
  187. {
  188. checkThreadIsAttached();
  189. bool readOk = true;
  190. while (numSamples > 0)
  191. {
  192. if (lastSampleRead != startSampleInFile)
  193. {
  194. TimeRecord time;
  195. time.scale = (TimeScale) inputStreamDesc.mSampleRate;
  196. time.base = 0;
  197. time.value.hi = 0;
  198. time.value.lo = (UInt32) startSampleInFile;
  199. OSStatus err = MovieAudioExtractionSetProperty (extractor,
  200. kQTPropertyClass_MovieAudioExtraction_Movie,
  201. kQTMovieAudioExtractionMoviePropertyID_CurrentTime,
  202. sizeof (time), &time);
  203. if (err != noErr)
  204. {
  205. readOk = false;
  206. break;
  207. }
  208. }
  209. int framesToDo = jmin (numSamples, (int) (bufferList->mBuffers[0].mDataByteSize / inputStreamDesc.mBytesPerFrame));
  210. bufferList->mBuffers[0].mDataByteSize = inputStreamDesc.mBytesPerFrame * framesToDo;
  211. UInt32 outFlags = 0;
  212. UInt32 actualNumFrames = framesToDo;
  213. OSStatus err = MovieAudioExtractionFillBuffer (extractor, &actualNumFrames, bufferList, &outFlags);
  214. if (err != noErr)
  215. {
  216. readOk = false;
  217. break;
  218. }
  219. lastSampleRead = startSampleInFile + actualNumFrames;
  220. const int samplesReceived = actualNumFrames;
  221. for (int j = numDestChannels; --j >= 0;)
  222. {
  223. if (destSamples[j] != nullptr)
  224. {
  225. const short* src = ((const short*) bufferList->mBuffers[0].mData) + j;
  226. for (int i = 0; i < samplesReceived; ++i)
  227. {
  228. destSamples[j][startOffsetInDestBuffer + i] = (*src << 16);
  229. src += numChannels;
  230. }
  231. }
  232. }
  233. startOffsetInDestBuffer += samplesReceived;
  234. startSampleInFile += samplesReceived;
  235. numSamples -= samplesReceived;
  236. if (((outFlags & kQTMovieAudioExtractionComplete) != 0 || samplesReceived == 0) && numSamples > 0)
  237. {
  238. for (int j = numDestChannels; --j >= 0;)
  239. if (destSamples[j] != nullptr)
  240. zeromem (destSamples[j] + startOffsetInDestBuffer, sizeof (int) * numSamples);
  241. break;
  242. }
  243. }
  244. detachThread();
  245. return readOk;
  246. }
  247. }
  248. bool ok;
  249. private:
  250. Movie movie;
  251. Media media;
  252. Track track;
  253. const int trackNum;
  254. double trackUnitsPerFrame;
  255. int samplesPerFrame;
  256. int64 lastSampleRead;
  257. Thread::ThreadID lastThreadId;
  258. MovieAudioExtractionRef extractor;
  259. AudioStreamBasicDescription inputStreamDesc;
  260. HeapBlock <AudioBufferList> bufferList;
  261. HeapBlock <char> dataBuffer;
  262. Handle dataHandle;
  263. //==============================================================================
  264. void checkThreadIsAttached()
  265. {
  266. #if JUCE_MAC
  267. if (Thread::getCurrentThreadId() != lastThreadId)
  268. EnterMoviesOnThread (0);
  269. AttachMovieToCurrentThread (movie);
  270. #endif
  271. }
  272. void detachThread()
  273. {
  274. #if JUCE_MAC
  275. DetachMovieFromCurrentThread (movie);
  276. #endif
  277. }
  278. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (QTAudioReader)
  279. };
  280. //==============================================================================
  281. QuickTimeAudioFormat::QuickTimeAudioFormat() : AudioFormat (quickTimeFormatName, ".mov .mp3 .mp4 .m4a")
  282. {
  283. }
  284. QuickTimeAudioFormat::~QuickTimeAudioFormat()
  285. {
  286. }
  287. Array<int> QuickTimeAudioFormat::getPossibleSampleRates() { return Array<int>(); }
  288. Array<int> QuickTimeAudioFormat::getPossibleBitDepths() { return Array<int>(); }
  289. bool QuickTimeAudioFormat::canDoStereo() { return true; }
  290. bool QuickTimeAudioFormat::canDoMono() { return true; }
  291. //==============================================================================
  292. AudioFormatReader* QuickTimeAudioFormat::createReaderFor (InputStream* sourceStream,
  293. const bool deleteStreamIfOpeningFails)
  294. {
  295. ScopedPointer<QTAudioReader> r (new QTAudioReader (sourceStream, 0));
  296. if (r->ok)
  297. return r.release();
  298. if (! deleteStreamIfOpeningFails)
  299. r->input = 0;
  300. return nullptr;
  301. }
  302. AudioFormatWriter* QuickTimeAudioFormat::createWriterFor (OutputStream* /*streamToWriteTo*/,
  303. double /*sampleRateToUse*/,
  304. unsigned int /*numberOfChannels*/,
  305. int /*bitsPerSample*/,
  306. const StringPairArray& /*metadataValues*/,
  307. int /*qualityOptionIndex*/)
  308. {
  309. jassertfalse; // not yet implemented!
  310. return nullptr;
  311. }
  312. #endif