Chris@281: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@281: Chris@281: /* Chris@281: Sonic Visualiser Chris@281: An audio file viewer and annotation editor. Chris@281: Centre for Digital Music, Queen Mary, University of London. Chris@281: This file copyright 2006-2007 Chris Cannam and QMUL. Chris@281: Chris@281: Based on QTAudioFile.cpp from SoundBite, copyright 2006 Chris@281: Chris Sutton and Mark Levy. Chris@281: Chris@281: This program is free software; you can redistribute it and/or Chris@281: modify it under the terms of the GNU General Public License as Chris@281: published by the Free Software Foundation; either version 2 of the Chris@281: License, or (at your option) any later version. See the file Chris@281: COPYING included with this distribution for more information. Chris@281: */ Chris@281: Chris@281: #ifdef HAVE_QUICKTIME Chris@281: Chris@281: #include "QuickTimeFileReader.h" Chris@281: #include "base/Profiler.h" Chris@281: #include "system/System.h" Chris@281: Chris@281: #include Chris@281: #include Chris@281: #include Chris@281: Chris@333: #ifdef _WIN32 Chris@281: #include Chris@281: #include Chris@281: #else Chris@281: #include Chris@281: #endif Chris@281: Chris@281: class QuickTimeFileReader::D Chris@281: { Chris@281: public: Chris@281: D() : data(0), blockSize(1024) { } Chris@281: Chris@281: MovieAudioExtractionRef extractionSessionRef; Chris@281: AudioBufferList buffer; Chris@309: float *data; Chris@281: OSErr err; Chris@281: AudioStreamBasicDescription asbd; Chris@281: Movie movie; Chris@281: size_t blockSize; Chris@281: }; Chris@281: Chris@281: Chris@317: QuickTimeFileReader::QuickTimeFileReader(FileSource source, Chris@281: DecodeMode decodeMode, Chris@297: CacheMode mode, Chris@297: size_t targetRate) : Chris@297: CodedAudioFileReader(mode, targetRate), Chris@316: m_source(source), Chris@316: m_path(source.getLocalFilename()), Chris@281: m_d(new D), Chris@281: m_progress(0), Chris@281: m_cancelled(false), Chris@281: m_completion(0), Chris@281: m_decodeThread(0) Chris@281: { Chris@281: m_channelCount = 0; Chris@297: m_fileRate = 0; Chris@281: Chris@281: Profiler profiler("QuickTimeFileReader::QuickTimeFileReader", true); Chris@281: Chris@316: std::cerr << "QuickTimeFileReader: path is \"" << m_path.toStdString() << "\"" << std::endl; Chris@282: Chris@281: long QTversion; Chris@281: Chris@281: #ifdef WIN32 Chris@281: InitializeQTML(0); // FIXME should check QT version Chris@281: #else Chris@281: m_d->err = Gestalt(gestaltQuickTime,&QTversion); Chris@281: if ((m_d->err != noErr) || (QTversion < 0x07000000)) { Chris@290: m_error = QString("Failed to find compatible version of QuickTime (version 7 or above required)"); Chris@281: return; Chris@281: } Chris@281: #endif Chris@281: Chris@281: EnterMovies(); Chris@281: Chris@281: Handle dataRef; Chris@281: OSType dataRefType; Chris@281: Chris@282: // CFStringRef URLString = CFStringCreateWithCString Chris@282: // (0, m_path.toLocal8Bit().data(), 0); Chris@281: Chris@282: Chris@282: CFURLRef url = CFURLCreateFromFileSystemRepresentation Chris@282: (kCFAllocatorDefault, Chris@316: (const UInt8 *)m_path.toLocal8Bit().data(), Chris@316: (CFIndex)m_path.length(), Chris@282: false); Chris@282: Chris@282: Chris@282: // m_d->err = QTNewDataReferenceFromURLCFString Chris@282: m_d->err = QTNewDataReferenceFromCFURL Chris@282: (url, 0, &dataRef, &dataRefType); Chris@281: Chris@281: if (m_d->err) { Chris@290: m_error = QString("Error creating data reference for QuickTime decoder: code %1").arg(m_d->err); Chris@281: return; Chris@281: } Chris@281: Chris@281: short fileID = movieInDataForkResID; Chris@281: short flags = 0; Chris@281: m_d->err = NewMovieFromDataRef Chris@281: (&m_d->movie, flags, &fileID, dataRef, dataRefType); Chris@281: Chris@281: DisposeHandle(dataRef); Chris@281: if (m_d->err) { Chris@290: m_error = QString("Error creating new movie for QuickTime decoder: code %1").arg(m_d->err); Chris@281: return; Chris@281: } Chris@281: Chris@281: Boolean isProtected = 0; Chris@281: Track aTrack = GetMovieIndTrackType Chris@281: (m_d->movie, 1, SoundMediaType, Chris@281: movieTrackMediaType | movieTrackEnabledOnly); Chris@281: Chris@281: if (aTrack) { Chris@281: Media aMedia = GetTrackMedia(aTrack); // get the track media Chris@281: if (aMedia) { Chris@281: MediaHandler mh = GetMediaHandler(aMedia); // get the media handler we can query Chris@281: if (mh) { Chris@281: m_d->err = QTGetComponentProperty(mh, Chris@281: kQTPropertyClass_DRM, Chris@281: kQTDRMPropertyID_IsProtected, Chris@281: sizeof(Boolean), &isProtected,nil); Chris@281: } else { Chris@281: m_d->err = 1; Chris@281: } Chris@281: } else { Chris@281: m_d->err = 1; Chris@281: } Chris@281: } else { Chris@281: m_d->err = 1; Chris@281: } Chris@281: Chris@281: if (m_d->err && m_d->err != kQTPropertyNotSupportedErr) { Chris@290: m_error = QString("Error checking for DRM in QuickTime decoder: code %1").arg(m_d->err); Chris@281: return; Chris@281: } else if (!m_d->err && isProtected) { Chris@290: m_error = QString("File is protected with DRM"); Chris@281: return; Chris@281: } else if (m_d->err == kQTPropertyNotSupportedErr && !isProtected) { Chris@281: std::cerr << "QuickTime: File is not protected with DRM" << std::endl; Chris@281: } Chris@281: Chris@281: if (m_d->movie) { Chris@281: SetMovieActive(m_d->movie, TRUE); Chris@281: m_d->err = GetMoviesError(); Chris@281: if (m_d->err) { Chris@290: m_error = QString("Error in QuickTime decoder activation: code %1").arg(m_d->err); Chris@281: return; Chris@281: } Chris@282: } else { Chris@290: m_error = QString("Error in QuickTime decoder: Movie object not valid"); Chris@282: return; Chris@281: } Chris@281: Chris@281: m_d->err = MovieAudioExtractionBegin Chris@281: (m_d->movie, 0, &m_d->extractionSessionRef); Chris@281: if (m_d->err) { Chris@290: m_error = QString("Error in QuickTime decoder extraction init: code %1").arg(m_d->err); Chris@281: return; Chris@281: } Chris@281: Chris@281: m_d->err = MovieAudioExtractionGetProperty Chris@281: (m_d->extractionSessionRef, Chris@281: kQTPropertyClass_MovieAudioExtraction_Audio, kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription, Chris@281: sizeof(m_d->asbd), Chris@281: &m_d->asbd, Chris@281: nil); Chris@281: Chris@281: if (m_d->err) { Chris@290: m_error = QString("Error in QuickTime decoder property get: code %1").arg(m_d->err); Chris@281: return; Chris@281: } Chris@281: Chris@281: m_channelCount = m_d->asbd.mChannelsPerFrame; Chris@297: m_fileRate = m_d->asbd.mSampleRate; Chris@281: Chris@297: std::cerr << "QuickTime: " << m_channelCount << " channels, " << m_fileRate << " kHz" << std::endl; Chris@281: Chris@281: m_d->asbd.mFormatFlags = Chris@281: kAudioFormatFlagIsFloat | Chris@281: kAudioFormatFlagIsPacked | Chris@281: kAudioFormatFlagsNativeEndian; Chris@309: m_d->asbd.mBitsPerChannel = sizeof(float) * 8; Chris@309: m_d->asbd.mBytesPerFrame = sizeof(float) * m_d->asbd.mChannelsPerFrame; Chris@281: m_d->asbd.mBytesPerPacket = m_d->asbd.mBytesPerFrame; Chris@281: Chris@281: m_d->err = MovieAudioExtractionSetProperty Chris@281: (m_d->extractionSessionRef, Chris@281: kQTPropertyClass_MovieAudioExtraction_Audio, Chris@281: kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription, Chris@281: sizeof(m_d->asbd), Chris@281: &m_d->asbd); Chris@281: Chris@281: if (m_d->err) { Chris@290: m_error = QString("Error in QuickTime decoder property set: code %1").arg(m_d->err); Chris@281: return; Chris@281: } Chris@281: m_d->buffer.mNumberBuffers = 1; Chris@281: m_d->buffer.mBuffers[0].mNumberChannels = m_channelCount; Chris@281: m_d->buffer.mBuffers[0].mDataByteSize = Chris@309: sizeof(float) * m_channelCount * m_d->blockSize; Chris@309: m_d->data = new float[m_channelCount * m_d->blockSize]; Chris@282: m_d->buffer.mBuffers[0].mData = m_d->data; Chris@281: Chris@281: initialiseDecodeCache(); Chris@281: Chris@281: if (decodeMode == DecodeAtOnce) { Chris@281: Chris@327: if (dynamic_cast(QCoreApplication::instance())) { Chris@327: m_progress = new QProgressDialog Chris@327: (QObject::tr("Decoding %1...").arg(QFileInfo(m_path).fileName()), Chris@327: QObject::tr("Stop"), 0, 100); Chris@327: m_progress->hide(); Chris@327: } Chris@281: Chris@281: while (1) { Chris@281: Chris@282: UInt32 framesRead = m_d->blockSize; Chris@281: UInt32 extractionFlags = 0; Chris@281: m_d->err = MovieAudioExtractionFillBuffer Chris@281: (m_d->extractionSessionRef, &framesRead, &m_d->buffer, Chris@281: &extractionFlags); Chris@281: if (m_d->err) { Chris@290: m_error = QString("Error in QuickTime decoding: code %1") Chris@290: .arg(m_d->err); Chris@281: break; Chris@281: } Chris@281: Chris@384: //!!! progress? Chris@384: Chris@282: // std::cerr << "Read " << framesRead << " frames (block size " << m_d->blockSize << ")" << std::endl; Chris@282: Chris@281: // QuickTime buffers are interleaved unless specified otherwise Chris@297: addSamplesToDecodeCache(m_d->data, framesRead); Chris@281: Chris@281: if (framesRead < m_d->blockSize) break; Chris@281: } Chris@281: Chris@281: finishDecodeCache(); Chris@281: Chris@281: m_d->err = MovieAudioExtractionEnd(m_d->extractionSessionRef); Chris@281: if (m_d->err) { Chris@290: m_error = QString("Error ending QuickTime extraction session: code %1").arg(m_d->err); Chris@281: } Chris@281: Chris@281: m_completion = 100; Chris@281: Chris@281: delete m_progress; Chris@281: m_progress = 0; Chris@281: Chris@281: } else { Chris@281: if (m_channelCount > 0) { Chris@281: m_decodeThread = new DecodeThread(this); Chris@281: m_decodeThread->start(); Chris@281: } Chris@281: } Chris@282: Chris@290: std::cerr << "QuickTimeFileReader::QuickTimeFileReader: frame count is now " << getFrameCount() << ", error is \"\"" << m_error.toStdString() << "\"" << std::endl; Chris@281: } Chris@281: Chris@281: QuickTimeFileReader::~QuickTimeFileReader() Chris@281: { Chris@282: std::cerr << "QuickTimeFileReader::~QuickTimeFileReader" << std::endl; Chris@282: Chris@281: if (m_decodeThread) { Chris@281: m_cancelled = true; Chris@281: m_decodeThread->wait(); Chris@281: delete m_decodeThread; Chris@281: } Chris@281: Chris@282: SetMovieActive(m_d->movie, FALSE); Chris@281: DisposeMovie(m_d->movie); Chris@281: Chris@281: delete[] m_d->data; Chris@281: delete m_d; Chris@281: } Chris@281: Chris@281: void Chris@281: QuickTimeFileReader::DecodeThread::run() Chris@281: { Chris@297: if (m_reader->m_cacheMode == CacheInTemporaryFile) { Chris@297: m_reader->m_completion = 1; Chris@297: m_reader->startSerialised("QuickTimeFileReader::Decode"); Chris@297: } Chris@297: Chris@281: while (1) { Chris@281: Chris@282: UInt32 framesRead = m_reader->m_d->blockSize; Chris@281: UInt32 extractionFlags = 0; Chris@281: m_reader->m_d->err = MovieAudioExtractionFillBuffer Chris@281: (m_reader->m_d->extractionSessionRef, &framesRead, Chris@281: &m_reader->m_d->buffer, &extractionFlags); Chris@281: if (m_reader->m_d->err) { Chris@290: m_reader->m_error = QString("Error in QuickTime decoding: code %1") Chris@290: .arg(m_reader->m_d->err); Chris@281: break; Chris@281: } Chris@282: Chris@281: // QuickTime buffers are interleaved unless specified otherwise Chris@311: m_reader->addSamplesToDecodeCache(m_reader->m_d->data, framesRead); Chris@281: Chris@281: if (framesRead < m_reader->m_d->blockSize) break; Chris@281: } Chris@281: Chris@281: m_reader->finishDecodeCache(); Chris@281: Chris@281: m_reader->m_d->err = MovieAudioExtractionEnd(m_reader->m_d->extractionSessionRef); Chris@281: if (m_reader->m_d->err) { Chris@290: m_reader->m_error = QString("Error ending QuickTime extraction session: code %1").arg(m_reader->m_d->err); Chris@281: } Chris@281: Chris@281: m_reader->m_completion = 100; Chris@297: m_reader->endSerialised(); Chris@281: } Chris@281: Chris@281: void Chris@290: QuickTimeFileReader::getSupportedExtensions(std::set &extensions) Chris@281: { Chris@281: extensions.insert("aiff"); Chris@291: extensions.insert("aif"); Chris@281: extensions.insert("au"); Chris@281: extensions.insert("avi"); Chris@281: extensions.insert("m4a"); Chris@281: extensions.insert("m4b"); Chris@281: extensions.insert("m4p"); Chris@281: extensions.insert("m4v"); Chris@281: extensions.insert("mov"); Chris@281: extensions.insert("mp3"); Chris@281: extensions.insert("mp4"); Chris@281: extensions.insert("wav"); Chris@281: } Chris@281: Chris@316: bool Chris@316: QuickTimeFileReader::supportsExtension(QString extension) Chris@316: { Chris@316: std::set extensions; Chris@316: getSupportedExtensions(extensions); Chris@316: return (extensions.find(extension.toLower()) != extensions.end()); Chris@316: } Chris@316: Chris@316: bool Chris@316: QuickTimeFileReader::supportsContentType(QString type) Chris@316: { Chris@316: return (type == "audio/x-aiff" || Chris@316: type == "audio/x-wav" || Chris@316: type == "audio/mpeg" || Chris@316: type == "audio/basic" || Chris@316: type == "audio/x-aac" || Chris@316: type == "video/mp4" || Chris@316: type == "video/quicktime"); Chris@316: } Chris@316: Chris@316: bool Chris@317: QuickTimeFileReader::supports(FileSource &source) Chris@316: { Chris@316: return (supportsExtension(source.getExtension()) || Chris@316: supportsContentType(source.getContentType())); Chris@316: } Chris@316: Chris@281: #endif Chris@297: