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

Revision 10891, 6.9 kB (checked in by robert, 3 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
Line 
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 */
9#include <osg/GL>
10#include <osg/Endian>
11#include <osg/Image>
12#include <osgDB/FileNameUtils>
13
14#include "MovieData.h"
15#include "QTUtils.h"
16
17
18
19   
20MovieData::MovieData() : _pointer(NULL), _movie(NULL), _gw(NULL), _fError(false), _isLooping(false)
21{
22
23}
24
25
26MovieData::~MovieData()
27
28    if (_pointer) free(_pointer);
29    if (_gw) DisposeGWorld(_gw);
30
31    if (_movie) {
32        DisposeMovie(_movie);
33    }
34}
35   
36   
37   
38   
39void MovieData::load(osg::Image* image, std::string afilename, float startTime)
40{
41    bool isUrl( osgDB::containsServerAddress(afilename) );
42   
43    std::string filename = afilename;
44    if (!isUrl) {
45        if (!osgDB::isFileNameNativeStyle(filename))
46            filename = osgDB::convertFileNameToNativeStyle(filename);
47    }
48   
49    image->setFileName(filename);
50   
51
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;
83   
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) {
90        _fError = true;
91        osg::notify(osg::FATAL) << " MovieData :: NewMovieFromProperties failed with err " << status<< std::endl;
92        return;
93    }
94   
95   
96    Rect bounds;
97 
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);
125        SetMovieActive(_movie, true);
126        UpdateMovie(_movie);
127        MoviesTask(_movie,0);
128    }
129}
130
131
132// ---------------------------------------------------------------------------
133// _intImage
134// create image for storing
135// ---------------------------------------------------------------------------
136
137void MovieData::_initImage(osg::Image* image)
138{
139
140    void* buffer;
141   
142
143    _textureWidth = ((_movieWidth + 7) >> 3) << 3;
144    _textureHeight = _movieHeight;
145   
146    // some magic alignment...
147    _pointer = (char*)malloc(4 * _textureWidth * _textureHeight + 32);
148
149    if (_pointer == NULL) {
150        osg::notify(osg::FATAL) << "MovieData: " << "Can't allocate texture buffer" << std::endl;
151        _fError= true;
152    }
153
154    buffer = (void*)(((unsigned long)(_pointer + 31) >> 5) << 5);
155
156    GLenum internalFormat = (osg::getCpuByteOrder()==osg::BigEndian)?
157                            GL_UNSIGNED_INT_8_8_8_8_REV :
158                            GL_UNSIGNED_INT_8_8_8_8;
159
160    image->setImage(_textureWidth,_textureHeight,1,
161                   (GLint) GL_RGBA8,
162                   (GLenum)GL_BGRA,
163                   internalFormat,
164                   (unsigned char*) buffer,osg::Image::NO_DELETE,4);
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;
177    GDHandle        origDevice;
178    CGrafPtr        origPort;
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);
191    SetGWorld(_gw, NULL);                                         // set current graphics port to offscreen
192    SetMovieGWorld(_movie, (CGrafPtr)_gw, NULL);
193   
194    _checkMovieError("SetMovieGWorld failed");
195
196    pixmap = GetGWorldPixMap (_gw);
197    if (pixmap)
198    {
199        if (!LockPixels (pixmap))                                        // lock offscreen pixel map
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
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}
226
227void MovieData::setMovieRate(float rate) {
228    // osg::notify(osg::ALWAYS) << "new movierate: " << rate << " current: " << getMovieRate() << std::endl;
229    _movieRate = rate;
230    if ((rate != 0) && (_preRolled == false)) {
231        PrerollMovie(_movie, GetMovieTime(_movie,NULL), X2Fix(rate));
232        _checkMovieError("PrerollMovie failed");
233        _preRolled = true;
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");
243
244}
245
246
247
Note: See TracBrowser for help on using the browser.