annotate data/fileio/QuickTimeFileReader.cpp @ 281:9c447d664275

* Add QuickTime file reader class -- totally untested, shouldn't even compile
author Chris Cannam
date Mon, 06 Aug 2007 14:37:59 +0000
parents
children e2fdcf9d35c5
rev   line source
Chris@281 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@281 2
Chris@281 3 /*
Chris@281 4 Sonic Visualiser
Chris@281 5 An audio file viewer and annotation editor.
Chris@281 6 Centre for Digital Music, Queen Mary, University of London.
Chris@281 7 This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@281 8
Chris@281 9 Based on QTAudioFile.cpp from SoundBite, copyright 2006
Chris@281 10 Chris Sutton and Mark Levy.
Chris@281 11
Chris@281 12 This program is free software; you can redistribute it and/or
Chris@281 13 modify it under the terms of the GNU General Public License as
Chris@281 14 published by the Free Software Foundation; either version 2 of the
Chris@281 15 License, or (at your option) any later version. See the file
Chris@281 16 COPYING included with this distribution for more information.
Chris@281 17 */
Chris@281 18
Chris@281 19 #ifdef HAVE_QUICKTIME
Chris@281 20
Chris@281 21 #include "QuickTimeFileReader.h"
Chris@281 22 #include "base/Profiler.h"
Chris@281 23 #include "system/System.h"
Chris@281 24
Chris@281 25 #include <QApplication>
Chris@281 26 #include <QFileInfo>
Chris@281 27 #include <QProgressDialog>
Chris@281 28
Chris@281 29 #ifdef WIN32
Chris@281 30 #include <QTML.h>
Chris@281 31 #include <Movies.h>
Chris@281 32 #else
Chris@281 33 #include <QuickTime/QuickTime.h>
Chris@281 34 #endif
Chris@281 35
Chris@281 36 class QuickTimeFileReader::D
Chris@281 37 {
Chris@281 38 public:
Chris@281 39 D() : data(0), blockSize(1024) { }
Chris@281 40
Chris@281 41 MovieAudioExtractionRef extractionSessionRef;
Chris@281 42 AudioBufferList buffer;
Chris@281 43 float *data;
Chris@281 44 OSErr err;
Chris@281 45 AudioStreamBasicDescription asbd;
Chris@281 46 Movie movie;
Chris@281 47 size_t blockSize;
Chris@281 48 };
Chris@281 49
Chris@281 50
Chris@281 51 QuickTimeFileReader::QuickTimeFileReader(QString path,
Chris@281 52 DecodeMode decodeMode,
Chris@281 53 CacheMode mode) :
Chris@281 54 CodedAudioFileReader(mode),
Chris@281 55 m_path(path),
Chris@281 56 m_d(new D),
Chris@281 57 m_progress(0),
Chris@281 58 m_cancelled(false),
Chris@281 59 m_completion(0),
Chris@281 60 m_decodeThread(0)
Chris@281 61 {
Chris@281 62 m_frameCount = 0;
Chris@281 63 m_channelCount = 0;
Chris@281 64 m_sampleRate = 0;
Chris@281 65
Chris@281 66 Profiler profiler("QuickTimeFileReader::QuickTimeFileReader", true);
Chris@281 67
Chris@281 68 long QTversion;
Chris@281 69
Chris@281 70 #ifdef WIN32
Chris@281 71 InitializeQTML(0); // FIXME should check QT version
Chris@281 72 #else
Chris@281 73 m_d->err = Gestalt(gestaltQuickTime,&QTversion);
Chris@281 74 if ((m_d->err != noErr) || (QTversion < 0x07000000)) {
Chris@281 75 m_error = QString("Failed to find compatible version of QuickTime (version 7 or above required)");
Chris@281 76 return;
Chris@281 77 }
Chris@281 78 #endif
Chris@281 79
Chris@281 80 EnterMovies();
Chris@281 81
Chris@281 82 Handle dataRef;
Chris@281 83 OSType dataRefType;
Chris@281 84
Chris@281 85 CFStringRef URLString = CFStringCreateWithCString
Chris@281 86 (0, m_path.toLocal8Bit().data(), 0);
Chris@281 87
Chris@281 88 m_d->err = QTNewDataReferenceFromURLCFString
Chris@281 89 (URLString, 0, &dataRef, &dataRefType);
Chris@281 90
Chris@281 91 if (m_d->err) {
Chris@281 92 m_error = QString("Error creating data reference for QuickTime decoder: code %1").arg(m_d->err);
Chris@281 93 return;
Chris@281 94 }
Chris@281 95
Chris@281 96 short fileID = movieInDataForkResID;
Chris@281 97 short flags = 0;
Chris@281 98 m_d->err = NewMovieFromDataRef
Chris@281 99 (&m_d->movie, flags, &fileID, dataRef, dataRefType);
Chris@281 100
Chris@281 101 DisposeHandle(dataRef);
Chris@281 102 if (m_d->err) {
Chris@281 103 m_error = QString("Error creating new movie for QuickTime decoder: code %1").arg(m_d->err);
Chris@281 104 return;
Chris@281 105 }
Chris@281 106
Chris@281 107 Boolean isProtected = 0;
Chris@281 108 Track aTrack = GetMovieIndTrackType
Chris@281 109 (m_d->movie, 1, SoundMediaType,
Chris@281 110 movieTrackMediaType | movieTrackEnabledOnly);
Chris@281 111
Chris@281 112 if (aTrack) {
Chris@281 113 Media aMedia = GetTrackMedia(aTrack); // get the track media
Chris@281 114 if (aMedia) {
Chris@281 115 MediaHandler mh = GetMediaHandler(aMedia); // get the media handler we can query
Chris@281 116 if (mh) {
Chris@281 117 m_d->err = QTGetComponentProperty(mh,
Chris@281 118 kQTPropertyClass_DRM,
Chris@281 119 kQTDRMPropertyID_IsProtected,
Chris@281 120 sizeof(Boolean), &isProtected,nil);
Chris@281 121 } else {
Chris@281 122 m_d->err = 1;
Chris@281 123 }
Chris@281 124 } else {
Chris@281 125 m_d->err = 1;
Chris@281 126 }
Chris@281 127 } else {
Chris@281 128 m_d->err = 1;
Chris@281 129 }
Chris@281 130
Chris@281 131 if (m_d->err && m_d->err != kQTPropertyNotSupportedErr) {
Chris@281 132 m_error = QString("Error checking for DRM in QuickTime decoder: code %1").arg(m_d->err);
Chris@281 133 return;
Chris@281 134 } else if (!m_d->err && isProtected) {
Chris@281 135 m_error = QString("File is protected with DRM");
Chris@281 136 return;
Chris@281 137 } else if (m_d->err == kQTPropertyNotSupportedErr && !isProtected) {
Chris@281 138 std::cerr << "QuickTime: File is not protected with DRM" << std::endl;
Chris@281 139 }
Chris@281 140
Chris@281 141 if (m_d->movie) {
Chris@281 142 SetMovieActive(m_d->movie, TRUE);
Chris@281 143 m_d->err = GetMoviesError();
Chris@281 144 if (m_d->err) {
Chris@281 145 m_error = QString("Error in QuickTime decoder activation: code %1").arg(m_d->err);
Chris@281 146 return;
Chris@281 147 }
Chris@281 148 }
Chris@281 149
Chris@281 150 m_d->err = MovieAudioExtractionBegin
Chris@281 151 (m_d->movie, 0, &m_d->extractionSessionRef);
Chris@281 152 if (m_d->err) {
Chris@281 153 m_error = QString("Error in QuickTime decoder extraction init: code %1").arg(m_d->err);
Chris@281 154 return;
Chris@281 155 }
Chris@281 156 /*
Chris@281 157 AudioChannelLayout monoLayout = {0};
Chris@281 158 monoLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
Chris@281 159 m_d->err = MovieAudioExtractionSetProperty(m_d->extractionSessionRef,
Chris@281 160 kQTPropertyClass_MovieAudioExtraction_Audio,
Chris@281 161 kQTMovieAudioExtractionAudioPropertyID_AudioChannelLayout,
Chris@281 162 sizeof(monoLayout), &monoLayout);
Chris@281 163 if (m_d->err) {
Chris@281 164 m_error = QString("Error in QuickTime decoder property set: code %1").arg(m_d->err);
Chris@281 165 return;
Chris@281 166 }
Chris@281 167 */
Chris@281 168
Chris@281 169 m_d->err = MovieAudioExtractionGetProperty
Chris@281 170 (m_d->extractionSessionRef,
Chris@281 171 kQTPropertyClass_MovieAudioExtraction_Audio, kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription,
Chris@281 172 sizeof(m_d->asbd),
Chris@281 173 &m_d->asbd,
Chris@281 174 nil);
Chris@281 175
Chris@281 176 if (m_d->err) {
Chris@281 177 m_error = QString("Error in QuickTime decoder property get: code %1").arg(m_d->err);
Chris@281 178 return;
Chris@281 179 }
Chris@281 180
Chris@281 181 m_channelCount = m_d->asbd.mChannelsPerFrame;
Chris@281 182 m_sampleRate = m_d->asbd.mSampleRate;
Chris@281 183
Chris@281 184 std::cerr << "QuickTime: " << m_channelCount << " channels, " << m_sampleRate << " kHz" << std::endl;
Chris@281 185
Chris@281 186 m_d->asbd.mFormatFlags =
Chris@281 187 kAudioFormatFlagIsFloat |
Chris@281 188 kAudioFormatFlagIsPacked |
Chris@281 189 kAudioFormatFlagsNativeEndian;
Chris@281 190 m_d->asbd.mBitsPerChannel = sizeof(float) * 8;
Chris@281 191 m_d->asbd.mBytesPerFrame = sizeof(float) * m_d->asbd.mChannelsPerFrame;
Chris@281 192 m_d->asbd.mBytesPerPacket = m_d->asbd.mBytesPerFrame;
Chris@281 193
Chris@281 194 m_d->err = MovieAudioExtractionSetProperty
Chris@281 195 (m_d->extractionSessionRef,
Chris@281 196 kQTPropertyClass_MovieAudioExtraction_Audio,
Chris@281 197 kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription,
Chris@281 198 sizeof(m_d->asbd),
Chris@281 199 &m_d->asbd);
Chris@281 200
Chris@281 201 if (m_d->err) {
Chris@281 202 m_error = QString("Error in QuickTime decoder ASBD set: code %1").arg(m_d->err);
Chris@281 203 return;
Chris@281 204 }
Chris@281 205
Chris@281 206 m_d->buffer.mNumberBuffers = 1;
Chris@281 207 m_d->buffer.mBuffers[0].mNumberChannels = m_channelCount;
Chris@281 208 m_d->buffer.mBuffers[0].mDataByteSize =
Chris@281 209 sizeof(float) * m_channelCount * m_d->blockSize;
Chris@281 210 m_d->data = new float[m_d->blockSize];
Chris@281 211 m_d->buffer[0].mData = m_d->data;
Chris@281 212
Chris@281 213 initialiseDecodeCache();
Chris@281 214
Chris@281 215 if (decodeMode == DecodeAtOnce) {
Chris@281 216
Chris@281 217 m_progress = new QProgressDialog
Chris@281 218 (QObject::tr("Decoding %1...").arg(QFileInfo(path).fileName()),
Chris@281 219 QObject::tr("Stop"), 0, 100);
Chris@281 220 m_progress->hide();
Chris@281 221
Chris@281 222 while (1) {
Chris@281 223
Chris@281 224 UInt32 framesRead = 0;
Chris@281 225 UInt32 extractionFlags = 0;
Chris@281 226 m_d->err = MovieAudioExtractionFillBuffer
Chris@281 227 (m_d->extractionSessionRef, &framesRead, &m_d->buffer,
Chris@281 228 &extractionFlags);
Chris@281 229 if (m_d->err) {
Chris@281 230 m_error = QString("Error in QuickTime decoding: code %1")
Chris@281 231 .arg(m_d->err);
Chris@281 232 break;
Chris@281 233 }
Chris@281 234
Chris@281 235 // QuickTime buffers are interleaved unless specified otherwise
Chris@281 236 for (UInt32 i = 0; i < framesRead * m_channelCount; ++i) {
Chris@281 237 addSampleToDecodeCache(m_d->data[i]);
Chris@281 238 }
Chris@281 239
Chris@281 240 if (framesRead < m_d->blockSize) break;
Chris@281 241 }
Chris@281 242
Chris@281 243 finishDecodeCache();
Chris@281 244
Chris@281 245 m_d->err = MovieAudioExtractionEnd(m_d->extractionSessionRef);
Chris@281 246 if (m_d->err) {
Chris@281 247 m_error = QString("Error ending QuickTime extraction session: code %1").arg(m_d->err);
Chris@281 248 }
Chris@281 249
Chris@281 250 m_completion = 100;
Chris@281 251
Chris@281 252 delete m_progress;
Chris@281 253 m_progress = 0;
Chris@281 254
Chris@281 255 } else {
Chris@281 256 if (m_channelCount > 0) {
Chris@281 257 m_decodeThread = new DecodeThread(this);
Chris@281 258 m_decodeThread->start();
Chris@281 259 }
Chris@281 260 }
Chris@281 261 }
Chris@281 262
Chris@281 263 QuickTimeFileReader::~QuickTimeFileReader()
Chris@281 264 {
Chris@281 265 if (m_decodeThread) {
Chris@281 266 m_cancelled = true;
Chris@281 267 m_decodeThread->wait();
Chris@281 268 delete m_decodeThread;
Chris@281 269 }
Chris@281 270
Chris@281 271 SetMovieActive(m_d->movie);
Chris@281 272 DisposeMovie(m_d->movie);
Chris@281 273
Chris@281 274 delete[] m_d->data;
Chris@281 275 delete m_d;
Chris@281 276 }
Chris@281 277
Chris@281 278 void
Chris@281 279 QuickTimeFileReader::DecodeThread::run()
Chris@281 280 {
Chris@281 281 while (1) {
Chris@281 282
Chris@281 283 UInt32 framesRead = 0;
Chris@281 284 UInt32 extractionFlags = 0;
Chris@281 285 m_reader->m_d->err = MovieAudioExtractionFillBuffer
Chris@281 286 (m_reader->m_d->extractionSessionRef, &framesRead,
Chris@281 287 &m_reader->m_d->buffer, &extractionFlags);
Chris@281 288 if (m_reader->m_d->err) {
Chris@281 289 m_error = QString("Error in QuickTime decoding: code %1")
Chris@281 290 .arg(m_reader->m_d->err);
Chris@281 291 break;
Chris@281 292 }
Chris@281 293
Chris@281 294 // QuickTime buffers are interleaved unless specified otherwise
Chris@281 295 for (UInt32 i = 0; i < framesRead * m_channelCount; ++i) {
Chris@281 296 addSampleToDecodeCache(m_reader->m_d->data[i]);
Chris@281 297 }
Chris@281 298
Chris@281 299 if (framesRead < m_reader->m_d->blockSize) break;
Chris@281 300 }
Chris@281 301
Chris@281 302 m_reader->finishDecodeCache();
Chris@281 303
Chris@281 304 m_reader->m_d->err = MovieAudioExtractionEnd(m_reader->m_d->extractionSessionRef);
Chris@281 305 if (m_reader->m_d->err) {
Chris@281 306 m_reader->m_error = QString("Error ending QuickTime extraction session: code %1").arg(m_reader->m_d->err);
Chris@281 307 }
Chris@281 308
Chris@281 309 m_reader->m_completion = 100;
Chris@281 310 }
Chris@281 311
Chris@281 312 void
Chris@281 313 QuickTimeFileReader::getSupportedExtensions(std::set<QString> &extensions)
Chris@281 314 {
Chris@281 315 extensions.insert("aiff");
Chris@281 316 extensions.insert("au");
Chris@281 317 extensions.insert("avi");
Chris@281 318 extensions.insert("m4a");
Chris@281 319 extensions.insert("m4b");
Chris@281 320 extensions.insert("m4p");
Chris@281 321 extensions.insert("m4v");
Chris@281 322 extensions.insert("mov");
Chris@281 323 extensions.insert("mp3");
Chris@281 324 extensions.insert("mp4");
Chris@281 325 extensions.insert("wav");
Chris@281 326 }
Chris@281 327
Chris@281 328 #endif