root/OpenSceneGraph/trunk/src/osgPlugins/quicktime/MovieData.cpp @ 10891

Revision 10891, 6.9 kB (checked in by robert, 4 years ago)

Changed GL_BGRA_EXT to GL_BGRA and added include of include/osg/Image to make sure define is declared

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[2815]1/*
2 *  MovieData.cpp
3 *  lala
4 *
5 *  Created by Stephan Huber on Wed Mar 10 2004.
6 *  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
7 *
8 */
[5799]9#include <osg/GL>
[5803]10#include <osg/Endian>
[10891]11#include <osg/Image>
[5799]12#include <osgDB/FileNameUtils>
[5656]13
[2815]14#include "MovieData.h"
15#include "QTUtils.h"
16
[5656]17
18
[2815]19   
[6701]20MovieData::MovieData() : _pointer(NULL), _movie(NULL), _gw(NULL), _fError(false), _isLooping(false)
[3100]21{
22
23}
24
25
26MovieData::~MovieData()
[8792]27
[5799]28    if (_pointer) free(_pointer);
[3100]29    if (_gw) DisposeGWorld(_gw);
[8792]30
31    if (_movie) {
32        DisposeMovie(_movie);
33    }
[3100]34}
[2815]35   
36   
37   
38   
[5799]39void MovieData::load(osg::Image* image, std::string afilename, float startTime)
[3100]40{
[9932]41    bool isUrl( osgDB::containsServerAddress(afilename) );
42   
[5799]43    std::string filename = afilename;
[9932]44    if (!isUrl) {
45        if (!osgDB::isFileNameNativeStyle(filename))
46            filename = osgDB::convertFileNameToNativeStyle(filename);
47    }
48   
49    image->setFileName(filename);
50   
[5799]51
[9932]52    QTNewMoviePropertyElement newMovieProperties[2];
53    CFStringRef movieLocation = CFStringCreateWithCString(NULL, filename.c_str(), kCFStringEncodingUTF8);
54    CFURLRef movieURL(NULL);
55    Boolean trueValue = true;
56
57    newMovieProperties[0].propClass = kQTPropertyClass_DataLocation;
58    if (!isUrl)
59    {
60        #ifdef __APPLE__
61            newMovieProperties[0].propID = kQTDataLocationPropertyID_CFStringPosixPath;
62        #else
63            newMovieProperties[0].propID = kQTDataLocationPropertyID_CFStringWindowsPath;
64        #endif
65       
66        newMovieProperties[0].propValueSize = sizeof(CFStringRef);
67        newMovieProperties[0].propValueAddress = &movieLocation;
68    }
69    else 
70    {
71        movieURL = CFURLCreateWithString(kCFAllocatorDefault, movieLocation, NULL);
72       
73        newMovieProperties[0].propID = kQTDataLocationPropertyID_CFURL;
74        newMovieProperties[0].propValueSize = sizeof(movieURL);
75        newMovieProperties[0].propValueAddress = (void*)&movieURL;
76    }
77
78    // make movie active
79    newMovieProperties[1].propClass = kQTPropertyClass_NewMovieProperty;
80    newMovieProperties[1].propID = kQTNewMoviePropertyID_Active;
81    newMovieProperties[1].propValueSize = sizeof(trueValue);
82    newMovieProperties[1].propValueAddress = &trueValue;
[2815]83   
[9932]84    // Instantiate the Movie
85    OSStatus status = NewMovieFromProperties(2, newMovieProperties, 0, NULL, &_movie);
86    CFRelease(movieLocation);
87    if (movieURL) CFRelease(movieURL);
88   
89    if (status !=0) {
[2815]90        _fError = true;
[9932]91        osg::notify(osg::FATAL) << " MovieData :: NewMovieFromProperties failed with err " << status<< std::endl;
[2815]92        return;
93    }
[9932]94   
95   
96    Rect bounds;
97 
[2815]98    GetMovieBox(_movie, &bounds);
99    _checkMovieError("Can't get movie box\n");
100   
101    OffsetRect(&bounds, -bounds.left, -bounds.top);
102    SetMovieBox(_movie, &bounds);
103    _checkMovieError("Can't set movie box\n");
104
105    _movieWidth = bounds.right;
106    _movieHeight = bounds.bottom;
107   
108    _timescale = (float)GetMovieTimeScale(_movie);
109
110    _initImage(image);
111    if (!_fError) _initGWorldStuff(image);
112
113       
114    if (!_fError) {
115   
116        if ( startTime == 0.0f)
117            GoToBeginningOfMovie(_movie);
118        else {
119            TimeValue t = (TimeValue) (startTime*_timescale);
120            SetMovieTimeValue(_movie,t);
121        }
122           
123        UpdateMovie(_movie);
124        SetMovieRate(_movie,0);
[5799]125        SetMovieActive(_movie, true);
126        UpdateMovie(_movie);
127        MoviesTask(_movie,0);
[2815]128    }
129}
130
131
132// ---------------------------------------------------------------------------
133// _intImage
134// create image for storing
135// ---------------------------------------------------------------------------
136
[3100]137void MovieData::_initImage(osg::Image* image)
138{
[2815]139
[3100]140    void* buffer;
[5799]141   
[2815]142
143    _textureWidth = ((_movieWidth + 7) >> 3) << 3;
144    _textureHeight = _movieHeight;
145   
146    // some magic alignment...
[5799]147    _pointer = (char*)malloc(4 * _textureWidth * _textureHeight + 32);
[2815]148
[5799]149    if (_pointer == NULL) {
[2815]150        osg::notify(osg::FATAL) << "MovieData: " << "Can't allocate texture buffer" << std::endl;
151        _fError= true;
152    }
153
[5799]154    buffer = (void*)(((unsigned long)(_pointer + 31) >> 5) << 5);
[5656]155
[5803]156    GLenum internalFormat = (osg::getCpuByteOrder()==osg::BigEndian)?
[5656]157                            GL_UNSIGNED_INT_8_8_8_8_REV :
158                            GL_UNSIGNED_INT_8_8_8_8;
159
[5799]160    image->setImage(_textureWidth,_textureHeight,1,
[4801]161                   (GLint) GL_RGBA8,
[10891]162                   (GLenum)GL_BGRA,
[5803]163                   internalFormat,
[5799]164                   (unsigned char*) buffer,osg::Image::NO_DELETE,4);
[2815]165
166}
167
168// ---------------------------------------------------------------------------
169// _initGWorldStuff
170// init gworld-stuff, so quicktime can play the movie into the gWorld.
171// ---------------------------------------------------------------------------
172
173void MovieData::_initGWorldStuff(osg::Image * image)  {
174
175    Rect textureBounds;
176    OSStatus err;
[4801]177    GDHandle        origDevice;
178    CGrafPtr        origPort;
[2815]179    PixMapHandle pixmap = NULL;
180
181    textureBounds.left = 0;
182    textureBounds.top = 0;
183    textureBounds.right = image->s();
184    textureBounds.bottom = image->t();
185    err = QTNewGWorldFromPtr(&_gw, k32ARGBPixelFormat, &textureBounds, NULL, NULL, 0, image->data(), 4 * image->s());
186   
187    if (err !=0 )
188        osg::notify(osg::FATAL) << "MovieData : Could not create gWorld" << std::endl;
189       
190    GetGWorld (&origPort, &origDevice);
[4801]191    SetGWorld(_gw, NULL);                                         // set current graphics port to offscreen
[2815]192    SetMovieGWorld(_movie, (CGrafPtr)_gw, NULL);
193   
194    _checkMovieError("SetMovieGWorld failed");
195
196    pixmap = GetGWorldPixMap (_gw);
197    if (pixmap)
198    {
[4801]199        if (!LockPixels (pixmap))                                        // lock offscreen pixel map
[2815]200        {
201            osg::notify(osg::FATAL) << "Could not lock PixMap" << std::endl;
202            ExitToShell ();
203        }
204    }
205    else
206    {
207        osg::notify(osg::FATAL) << "Could not GetGWorldPixMap" << std::endl;
208        ExitToShell ();
209    }
210
211    SetGWorld(origPort, origDevice);
212
213}
214
[5799]215void MovieData::setMovieTime(float atime) {
216    float time = (atime > getMovieDuration()) ? getMovieDuration() : atime;
217   
218    TimeValue t = (TimeValue) (time * _timescale);
219    SetMovieTimeValue(_movie,t);
220    _checkMovieError("setMovieTime failed");
221    UpdateMovie(_movie);
222    MoviesTask(_movie,0);
223   
224       
225}
[2815]226
[5799]227void MovieData::setMovieRate(float rate) {
228    // osg::notify(osg::ALWAYS) << "new movierate: " << rate << " current: " << getMovieRate() << std::endl;
229    _movieRate = rate;
[5803]230    if ((rate != 0) && (_preRolled == false)) {
[5799]231        PrerollMovie(_movie, GetMovieTime(_movie,NULL), X2Fix(rate));
232        _checkMovieError("PrerollMovie failed");
[5803]233        _preRolled = true;
[5799]234    }
235   
236    SetMovieRate(_movie, X2Fix(rate));
237    _checkMovieError("setMovieRate failed");
238    MoviesTask(_movie, 0);
239    _checkMovieError("MoviesTask failed");
240   
241    UpdateMovie(_movie);
242    _checkMovieError("UpdateMovie failed");
[2815]243
[5799]244}
245
246
247
Note: See TracBrowser for help on using the browser.