| 1 | #include "osg/Image" |
|---|
| 2 | #include "osg/Notify" |
|---|
| 3 | |
|---|
| 4 | #include <osg/Geode> |
|---|
| 5 | |
|---|
| 6 | #include <osg/observer_ptr> |
|---|
| 7 | |
|---|
| 8 | #include "osg/GL" |
|---|
| 9 | |
|---|
| 10 | #include "osgDB/FileNameUtils" |
|---|
| 11 | #include "osgDB/Registry" |
|---|
| 12 | #include "osgDB/FileUtils" |
|---|
| 13 | |
|---|
| 14 | #include <stdio.h> |
|---|
| 15 | #include <stdlib.h> |
|---|
| 16 | #include <string.h> |
|---|
| 17 | #include <sstream> |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef __APPLE__ |
|---|
| 21 | #include "Components.h" |
|---|
| 22 | #include "QuickTimeComponents.h" |
|---|
| 23 | #else |
|---|
| 24 | #include <QuickTime/QuickTime.h> |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #ifndef SEEK_SET |
|---|
| 28 | # define SEEK_SET 0 |
|---|
| 29 | #endif |
|---|
| 30 | #include "QTUtils.h" |
|---|
| 31 | #include "QTLiveUtils.h" |
|---|
| 32 | #include "QTImportExport.h" |
|---|
| 33 | #include "QuicktimeImageStream.h" |
|---|
| 34 | #include "QuicktimeLiveImageStream.h" |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | using namespace osg; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | class QuicktimeExitObserver : public osg::Observer |
|---|
| 45 | { |
|---|
| 46 | public: |
|---|
| 47 | |
|---|
| 48 | QuicktimeExitObserver () : _instanceCount(0) |
|---|
| 49 | { |
|---|
| 50 | } |
|---|
| 51 | virtual ~QuicktimeExitObserver() |
|---|
| 52 | { |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | void addMedia(Image* ptr) |
|---|
| 56 | { |
|---|
| 57 | ptr->addObserver(this); |
|---|
| 58 | ++ _instanceCount; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | virtual void objectDeleted(void*) |
|---|
| 62 | { |
|---|
| 63 | -- _instanceCount; |
|---|
| 64 | if(_instanceCount== 0) |
|---|
| 65 | exitQuicktime(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | private: |
|---|
| 69 | unsigned int _instanceCount; |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | class ReaderWriterQT : public osgDB::ReaderWriter |
|---|
| 75 | { |
|---|
| 76 | public: |
|---|
| 77 | ReaderWriterQT::ReaderWriterQT() |
|---|
| 78 | { |
|---|
| 79 | supportsExtension("mov","Movie format"); |
|---|
| 80 | supportsExtension("mpg","Movie format"); |
|---|
| 81 | supportsExtension("mpv","Movie format"); |
|---|
| 82 | supportsExtension("mp4","Movie format"); |
|---|
| 83 | supportsExtension("m4v","Movie format"); |
|---|
| 84 | supportsExtension("dv","Movie format"); |
|---|
| 85 | supportsExtension("avi","Movie format"); |
|---|
| 86 | supportsExtension("flv","Movie format"); |
|---|
| 87 | supportsExtension("swf","Movie format"); |
|---|
| 88 | supportsExtension("3gp","Mobile movie format"); |
|---|
| 89 | |
|---|
| 90 | supportsExtension("live","Live video streaming"); |
|---|
| 91 | |
|---|
| 92 | #ifdef QT_HANDLE_IMAGES_ALSO |
|---|
| 93 | supportsExtension("jpg","jpg image format"); |
|---|
| 94 | supportsExtension("jpeg","jpeg image format"); |
|---|
| 95 | supportsExtension("tif","tif image format"); |
|---|
| 96 | supportsExtension("tiff","tiff image format"); |
|---|
| 97 | supportsExtension("gif","gif image format"); |
|---|
| 98 | supportsExtension("png","png image format"); |
|---|
| 99 | supportsExtension("pict","pict image format"); |
|---|
| 100 | supportsExtension("pct","pct image format"); |
|---|
| 101 | supportsExtension("tga","tga image format"); |
|---|
| 102 | supportsExtension("psd","psd image format"); |
|---|
| 103 | #endif |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | ReaderWriterQT::~ReaderWriterQT() |
|---|
| 107 | { |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | virtual const char* className() const { return "Default Quicktime Image Reader/Writer"; } |
|---|
| 112 | |
|---|
| 113 | virtual bool acceptsMovieExtension(const std::string& extension) const |
|---|
| 114 | { |
|---|
| 115 | return osgDB::equalCaseInsensitive(extension,"mov") || |
|---|
| 116 | osgDB::equalCaseInsensitive(extension,"mpg") || |
|---|
| 117 | osgDB::equalCaseInsensitive(extension,"mpv") || |
|---|
| 118 | osgDB::equalCaseInsensitive(extension,"mp4") || |
|---|
| 119 | osgDB::equalCaseInsensitive(extension,"m4v") || |
|---|
| 120 | osgDB::equalCaseInsensitive(extension,"dv") || |
|---|
| 121 | osgDB::equalCaseInsensitive(extension,"avi") || |
|---|
| 122 | osgDB::equalCaseInsensitive(extension,"flv") || |
|---|
| 123 | osgDB::equalCaseInsensitive(extension,"swf") || |
|---|
| 124 | osgDB::equalCaseInsensitive(extension,"3gp"); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | virtual bool acceptsLiveExtension(const std::string& extension) const |
|---|
| 128 | { |
|---|
| 129 | return osgDB::equalCaseInsensitive(extension,"live"); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | virtual bool acceptsExtension(const std::string& extension) const |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | return |
|---|
| 137 | |
|---|
| 138 | #ifdef QT_HANDLE_IMAGES_ALSO |
|---|
| 139 | osgDB::equalCaseInsensitive(extension,"jpg") || |
|---|
| 140 | osgDB::equalCaseInsensitive(extension,"jpeg") || |
|---|
| 141 | osgDB::equalCaseInsensitive(extension,"tif") || |
|---|
| 142 | osgDB::equalCaseInsensitive(extension,"tiff") || |
|---|
| 143 | osgDB::equalCaseInsensitive(extension,"gif") || |
|---|
| 144 | osgDB::equalCaseInsensitive(extension,"png") || |
|---|
| 145 | osgDB::equalCaseInsensitive(extension,"pict") || |
|---|
| 146 | osgDB::equalCaseInsensitive(extension,"pct") || |
|---|
| 147 | osgDB::equalCaseInsensitive(extension,"tga") || |
|---|
| 148 | osgDB::equalCaseInsensitive(extension,"psd") || |
|---|
| 149 | #endif |
|---|
| 150 | |
|---|
| 151 | acceptsMovieExtension(extension) || |
|---|
| 152 | acceptsLiveExtension(extension); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 156 | { |
|---|
| 157 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 158 | if (osgDB::equalCaseInsensitive(ext,"qt")) |
|---|
| 159 | { |
|---|
| 160 | return readImage(osgDB::getNameLessExtension(file),options); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | if (acceptsLiveExtension(ext)) |
|---|
| 167 | { |
|---|
| 168 | long num_video_components; |
|---|
| 169 | { |
|---|
| 170 | |
|---|
| 171 | QTScopedQTMLInitialiser qt_init; |
|---|
| 172 | QTScopedMovieInitialiser qt_movie_init; |
|---|
| 173 | |
|---|
| 174 | ComponentDescription video_component_description; |
|---|
| 175 | video_component_description.componentType = 'vdig'; |
|---|
| 176 | video_component_description.componentSubType = 0L; |
|---|
| 177 | video_component_description.componentManufacturer = 0L; |
|---|
| 178 | video_component_description.componentFlags = 0L; |
|---|
| 179 | video_component_description.componentFlagsMask = 0L; |
|---|
| 180 | num_video_components = CountComponents (&video_component_description); |
|---|
| 181 | } |
|---|
| 182 | if (osgDB::getNameLessExtension(file) == "devices") |
|---|
| 183 | { |
|---|
| 184 | osg::notify(osg::ALWAYS) << " available Video DigitizerComponents : " << num_video_components << std::endl; |
|---|
| 185 | if (num_video_components) |
|---|
| 186 | { |
|---|
| 187 | |
|---|
| 188 | probe_video_digitizer_components(); |
|---|
| 189 | |
|---|
| 190 | std::vector<OSG_SGDeviceList> devices_list = probe_sequence_grabber_components(); |
|---|
| 191 | if (devices_list.size()) |
|---|
| 192 | { |
|---|
| 193 | |
|---|
| 194 | OSG_SGDeviceList& video_device_list = devices_list[0]; |
|---|
| 195 | |
|---|
| 196 | osg::notify(osg::ALWAYS) << std::endl; |
|---|
| 197 | osg::notify(osg::ALWAYS) << "Video Component/Input IDs follow: " << std::endl; |
|---|
| 198 | osg::notify(osg::ALWAYS) << std::endl; |
|---|
| 199 | for (unsigned int device_input = 0; device_input < video_device_list.size(); ++device_input) |
|---|
| 200 | { |
|---|
| 201 | OSG_SGDevicePair device_pair = video_device_list[device_input]; |
|---|
| 202 | osg::notify(osg::ALWAYS) << device_pair.first.c_str() << " " << device_pair.second.c_str() << std::endl; |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | if (devices_list.size() > 1) |
|---|
| 206 | { |
|---|
| 207 | |
|---|
| 208 | OSG_SGDeviceList& audio_device_list = devices_list[1]; |
|---|
| 209 | |
|---|
| 210 | osg::notify(osg::ALWAYS) << std::endl; |
|---|
| 211 | osg::notify(osg::ALWAYS) << "Audio Component/Input IDs follow: " << std::endl; |
|---|
| 212 | osg::notify(osg::ALWAYS) << std::endl; |
|---|
| 213 | for (unsigned int device_input = 0; device_input < audio_device_list.size(); ++device_input) |
|---|
| 214 | { |
|---|
| 215 | OSG_SGDevicePair device_pair = audio_device_list[device_input]; |
|---|
| 216 | osg::notify(osg::ALWAYS) << device_pair.first.c_str() << " " << device_pair.second.c_str() << std::endl; |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 221 | } |
|---|
| 222 | else |
|---|
| 223 | { |
|---|
| 224 | osg::notify(osg::DEBUG_INFO) << " available Video DigitizerComponents : " << num_video_components << std::endl; |
|---|
| 225 | if (num_video_components) |
|---|
| 226 | { |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | initQuicktime(); |
|---|
| 233 | |
|---|
| 234 | QuicktimeLiveImageStream* p_qt_image_stream = new QuicktimeLiveImageStream(osgDB::getNameLessExtension(file)); |
|---|
| 235 | |
|---|
| 236 | _qtExitObserver.addMedia(p_qt_image_stream); |
|---|
| 237 | return p_qt_image_stream; |
|---|
| 238 | } |
|---|
| 239 | else |
|---|
| 240 | { |
|---|
| 241 | osg::notify(osg::DEBUG_INFO) << "No available Video DigitizerComponents : " << std::endl; |
|---|
| 242 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | std::string fileName = osgDB::findDataFile( file, options); |
|---|
| 249 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | initQuicktime(); |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | if (acceptsMovieExtension(ext)) |
|---|
| 261 | { |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | QuicktimeImageStream* moov = new QuicktimeImageStream(fileName); |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | _qtExitObserver.addMedia(moov); |
|---|
| 272 | |
|---|
| 273 | return moov; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | QuicktimeImportExport importer; |
|---|
| 277 | |
|---|
| 278 | std::ifstream is; |
|---|
| 279 | is.open (fileName.c_str(), std::ios::binary | std::ios::in ); |
|---|
| 280 | is.seekg (0, std::ios::end); |
|---|
| 281 | long length = is.tellg(); |
|---|
| 282 | is.seekg (0, std::ios::beg); |
|---|
| 283 | |
|---|
| 284 | osg::ref_ptr<osg::Image> image = importer.readFromStream(is, fileName, length); |
|---|
| 285 | is.close(); |
|---|
| 286 | if (!importer.success() || (image == NULL)) { |
|---|
| 287 | osg::notify(osg::WARN) << "Error reading file " << file << " : " << importer.getLastErrorString() << std::endl; |
|---|
| 288 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | _qtExitObserver.addMedia(image.get()); |
|---|
| 292 | |
|---|
| 293 | return image.release(); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | virtual ReadResult readImage (std::istream& is, const osgDB::ReaderWriter::Options* options=NULL) const |
|---|
| 297 | { |
|---|
| 298 | std::string filename = ""; |
|---|
| 299 | long sizeHint(0); |
|---|
| 300 | |
|---|
| 301 | if (options) { |
|---|
| 302 | std::istringstream iss(options->getOptionString()); |
|---|
| 303 | std::string opt; |
|---|
| 304 | while (iss >> opt) |
|---|
| 305 | { |
|---|
| 306 | int index = opt.find( "=" ); |
|---|
| 307 | if( opt.substr( 0, index ) == "filename" || |
|---|
| 308 | opt.substr( 0, index ) == "FILENAME" ) |
|---|
| 309 | { |
|---|
| 310 | filename = opt.substr( index+1 ); |
|---|
| 311 | } else if( opt.substr( 0, index ) == "size" || |
|---|
| 312 | opt.substr( 0, index ) == "SIZE" ) |
|---|
| 313 | { |
|---|
| 314 | std::string sizestr = opt.substr( index+1 ); |
|---|
| 315 | sizeHint = atol(sizestr.c_str()); |
|---|
| 316 | } |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | QuicktimeImportExport importer; |
|---|
| 321 | osg::ref_ptr<osg::Image> image = importer.readFromStream(is, filename, sizeHint); |
|---|
| 322 | |
|---|
| 323 | if (!importer.success() || (image == NULL)) { |
|---|
| 324 | osg::notify(osg::WARN) << "Error reading from stream " << importer.getLastErrorString() << std::endl; |
|---|
| 325 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 326 | } |
|---|
| 327 | _qtExitObserver.addMedia(image.get()); |
|---|
| 328 | return image.release(); |
|---|
| 329 | |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | virtual WriteResult writeImage(const osg::Image &img,const std::string& fileName, const osgDB::ReaderWriter::Options*) const |
|---|
| 333 | { |
|---|
| 334 | std::string ext = osgDB::getFileExtension(fileName); |
|---|
| 335 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 336 | |
|---|
| 337 | initQuicktime(); |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | std::map<std::string, OSType> extmap; |
|---|
| 341 | |
|---|
| 342 | extmap.insert(std::pair<std::string, OSType>("jpg", kQTFileTypeJPEG)); |
|---|
| 343 | extmap.insert(std::pair<std::string, OSType>("jpeg", kQTFileTypeJPEG)); |
|---|
| 344 | extmap.insert(std::pair<std::string, OSType>("bmp", kQTFileTypeBMP)); |
|---|
| 345 | extmap.insert(std::pair<std::string, OSType>("tif", kQTFileTypeTIFF)); |
|---|
| 346 | extmap.insert(std::pair<std::string, OSType>("tiff", kQTFileTypeTIFF)); |
|---|
| 347 | extmap.insert(std::pair<std::string, OSType>("png", kQTFileTypePNG)); |
|---|
| 348 | extmap.insert(std::pair<std::string, OSType>("gif", kQTFileTypeGIF)); |
|---|
| 349 | extmap.insert(std::pair<std::string, OSType>("psd", kQTFileTypePhotoShop)); |
|---|
| 350 | extmap.insert(std::pair<std::string, OSType>("sgi", kQTFileTypeSGIImage)); |
|---|
| 351 | |
|---|
| 352 | std::map<std::string, OSType>::iterator cur = extmap.find(ext); |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | if (cur == extmap.end()) |
|---|
| 356 | return WriteResult::FILE_NOT_HANDLED; |
|---|
| 357 | |
|---|
| 358 | std::ofstream os(fileName.c_str(), std::ios::binary | std::ios::trunc | std::ios::out); |
|---|
| 359 | if(os.good()) |
|---|
| 360 | { |
|---|
| 361 | QuicktimeImportExport exporter; |
|---|
| 362 | exporter.writeToStream(os, const_cast<osg::Image*>(&img), fileName); |
|---|
| 363 | |
|---|
| 364 | if (exporter.success()) |
|---|
| 365 | return WriteResult::FILE_SAVED; |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | virtual WriteResult writeImage (const osg::Image& img, std::ostream& os, const Options* options=NULL) const |
|---|
| 372 | { |
|---|
| 373 | std::string filename = "file.jpg"; |
|---|
| 374 | |
|---|
| 375 | if (options) { |
|---|
| 376 | std::istringstream iss(options->getOptionString()); |
|---|
| 377 | std::string opt; |
|---|
| 378 | while (iss >> opt) |
|---|
| 379 | { |
|---|
| 380 | int index = opt.find( "=" ); |
|---|
| 381 | if( opt.substr( 0, index ) == "filename" || |
|---|
| 382 | opt.substr( 0, index ) == "FILENAME" ) |
|---|
| 383 | { |
|---|
| 384 | filename = opt.substr( index+1 ); |
|---|
| 385 | } |
|---|
| 386 | } |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | QuicktimeImportExport exporter; |
|---|
| 390 | exporter.writeToStream(os, const_cast<osg::Image*>(&img), filename); |
|---|
| 391 | |
|---|
| 392 | if (exporter.success()) |
|---|
| 393 | return WriteResult::FILE_SAVED; |
|---|
| 394 | |
|---|
| 395 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | mutable QuicktimeExitObserver _qtExitObserver; |
|---|
| 400 | }; |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | REGISTER_OSGPLUGIN(quicktime, ReaderWriterQT) |
|---|