This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjit.gl.hap.win.c
353 lines (302 loc) · 9.07 KB
/
jit.gl.hap.win.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "ext.h"
#if !defined(C74_X64) && defined(WIN_VERSION)
#include "jit.gl.hap.h"
void jit_gl_hap_new_native(t_jit_gl_hap *x)
{
InitializeQTML(0); // Initialize QTML
EnterMovies(); // Initialize QuickTime
x->movie = NULL;
x->visualContext = NULL;
x->currentImage = NULL;
}
void jit_gl_hap_free_native(t_jit_gl_hap *x)
{
jit_gl_hap_dispose(x);
if(x->visualContext) {
QTVisualContextRelease(x->visualContext);
x->visualContext = NULL;
}
ExitMovies(); // Terminate QuickTime
TerminateQTML(); // Terminate QTML
}
void jit_gl_hap_read_native(t_jit_gl_hap *x, char *fname, short vol)
{
char fpath[MAX_PATH_CHARS];
char cpath[MAX_PATH_CHARS];
Handle dataRef;
OSType dataRefType;
OSErr err;
short resid;
path_topathname(vol, fname, fpath);
path_nameconform(fpath, cpath, PATH_STYLE_SLASH, PATH_TYPE_BOOT);
x->file = gensym(cpath);
XQT_NewDataReferenceFromMaxPath(vol, fname, &dataRef, &dataRefType, &err);
err = NewMovieFromDataRef(&x->movie, newMovieActive, &resid, dataRef, dataRefType);
if(err) {
jit_object_error((t_object*)x, "unknown error loading quicktime movie");
if(x->movie)
DisposeMovie(x->movie);
}
else {
//if(!HapQTQuickTimeMovieHasHapTrackPlayable(x->movie)) {
//DisposeMovie(x->movie);
//jit_object_error((t_object*)x, "non-hap codec movies are unsupported at this time");
//goto out;
//}
//else {
//MediaHandler mh = NULL;
Track track = NULL;
Media media = NULL;
SetMovieDefaultDataRef(x->movie, dataRef, dataRefType);
if (track = GetMovieIndTrackType(x->movie, 1, kCharacteristicHasVideoFrameRate, movieTrackCharacteristic)) {
if (media = GetTrackMedia(track)) {
//if (mh = GetMediaHandler(media)) {
//err = MediaHasCharacteristic(mh, kCharacteristicIsAnMpegTrack, &isMpeg);
//}
}
}
x->newfile = 1;
//MoviesTask(x->movie, 0);
x->framecount = GetMediaSampleCount(media);
x->timescale = GetMovieTimeScale(x->movie);
x->duration = GetMovieDuration(x->movie);
x->fps = (float)((double)x->framecount*(double)x->timescale/(double)x->duration);
//}
}
//out:
DisposeHandle(dataRef);
dataRef = NULL;
}
void jit_gl_hap_dispose(t_jit_gl_hap *x)
{
if (x->movie) {
StopMovie(x->movie);
SetMovieVisualContext(x->movie, NULL);
DisposeMovie(x->movie);
x->movie = NULL;
}
}
t_jit_err jit_gl_hap_dest_closing(t_jit_gl_hap *x)
{
if(x->movieloaded) {
StopMovie(x->movie);
SetMovieVisualContext(x->movie, NULL);
QTVisualContextRelease(x->visualContext);
x->visualContext = NULL;
x->newfile = 1;
}
return JIT_ERR_NONE;
}
t_jit_err jit_gl_hap_load_file(t_jit_gl_hap *x)
{
OSStatus err;
// Check if the movie has a Hap video track
Boolean hashap = HapQTQuickTimeMovieHasHapTrackPlayable(x->movie);
CFDictionaryRef pixelBufferOptions;
if(hashap) {
pixelBufferOptions = HapQTCreateCVPixelBufferOptionsDictionary();
x->hap_format = JIT_GL_HAP_PF_HAP;
}
else {
t_bool hasalpha = HapQTHasAlpha(x->movie);
pixelBufferOptions = HapQTCreateNonHapCVPixelBufferOptionsDictionary(hasalpha);
x->hap_format = hasalpha ? JIT_GL_HAP_PF_RGBA : JIT_GL_HAP_PF_RGB;
}
{
const void* key = kQTVisualContextPixelBufferAttributesKey;
const void* value = pixelBufferOptions;
CFDictionaryRef visualContextOptions = CFDictionaryCreate(
kCFAllocatorDefault, &key, &value, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks
);
CFRelease(pixelBufferOptions);
err = QTPixelBufferContextCreate(kCFAllocatorDefault, (CFDictionaryRef)visualContextOptions, &x->visualContext);
CFRelease(visualContextOptions);
}
//else {
//err = QTOpenGLTextureContextCreate(kCFAllocatorDefault, CGLGetCurrentContext(), CGLGetPixelFormat(CGLGetCurrentContext()), nil, &visualContext);
//jit_object_error((t_object*)x, "non-hap codec movies are unsupported at this time");
//return JIT_ERR_GENERIC;
//}
if (err == noErr) {
err = SetMovieVisualContext(x->movie, x->visualContext);
}
if(err != noErr) {
jit_object_error((t_object*)x, "unknown error adding quicktime movie to context");
return JIT_ERR_GENERIC;
}
x->movieloaded = 1;
x->newfile = 0;
StartMovie(x->movie);
SetMovieRate(x->movie, DoubleToFixed(x->rate));
SetMovieVolume(x->movie, (short)(x->vol*255.));
jit_gl_hap_do_looppoints(x);
jit_attr_user_touch(x, gensym("loopstart"));
jit_attr_user_touch(x, gensym("loopend"));
jit_attr_user_touch(x, gensym("looppoints"));
if(!x->autostart) {
StopMovie(x->movie);
jit_gl_hap_do_set_time(x, 0);
}
return JIT_ERR_NONE;
}
t_bool jit_gl_hap_getcurrentframe(t_jit_gl_hap *x)
{
OSStatus err = noErr;
CVImageBufferRef image = NULL;
err = QTVisualContextCopyImageForTime(x->visualContext, nil, nil, &image);
if (err == noErr && image) {
x->currentImage = image;
jit_gl_hap_draw_frame(x, image);
return 1;
}
return 0;
//else if (err != noErr) {
// NSLog(@"err %hd at QTVisualContextCopyImageForTime(), %s", err, __func__);
//}
}
void jit_gl_hap_releaseframe(t_jit_gl_hap *x)
{
if (x->currentImage) {
CVBufferRelease(x->currentImage);
x->currentImage = NULL;
}
QTVisualContextTask(x->visualContext);
MoviesTask(x->movie, 0);
}
void jit_gl_hap_start(t_jit_gl_hap *x)
{
if(x->movieloaded) {
StartMovie(x->movie);
SetMovieRate(x->movie, DoubleToFixed(x->rate));
}
}
void jit_gl_hap_stop(t_jit_gl_hap *x)
{
if(x->movieloaded) {
StopMovie(x->movie);
}
}
t_jit_err jit_gl_hap_jump(t_jit_gl_hap *x, t_symbol *s, long ac, t_atom *av)
{
if (x->movieloaded) {
t_atom_long jump = jit_gl_hap_frametotime(x, jit_atom_getlong(av));
jit_gl_hap_do_set_time(x, GetMovieTime(x->movie,NULL) + jump);
}
return JIT_ERR_NONE;
}
void jit_gl_hap_do_set_time(t_jit_gl_hap *x, t_atom_long time)
{
if (x->movieloaded) {
TimeBase tb;
TimeRecord tr;
if (x->loop == JIT_GL_HAP_LOOP_LIMITS)
CLIP_ASSIGN(time, x->looppoints[0], x->looppoints[1]);
tb = GetMovieTimeBase(x->movie);
GetTimeBaseStartTime(tb, GetMovieTimeScale(x->movie), &tr); // fill out struct
tr.value.lo = 0;
SetTimeBaseStartTime(tb, &tr);
tr.value.lo = GetMovieDuration(x->movie);
if (tr.value.lo) {
SetTimeBaseStopTime(tb, &tr);
SetMovieTimeValue(x->movie, time);
x->suppress_loopnotify = 1;
}
}
}
t_atom_long jit_gl_hap_do_get_time(t_jit_gl_hap *x)
{
return GetMovieTime(x->movie,NULL);
}
t_jit_err jit_gl_hap_rate_set(t_jit_gl_hap *x, void *attr, long ac, t_atom *av)
{
if (ac && av)
x->rate = (float)jit_atom_getfloat(av);
if(x->movieloaded)
SetMovieRate(x->movie, DoubleToFixed(x->rate));
return JIT_ERR_NONE;
}
t_jit_err jit_gl_hap_vol_set(t_jit_gl_hap *x, void *attr, long ac, t_atom *av)
{
if (ac && av)
x->vol = (float)(CLAMP(jit_atom_getfloat(av), 0, 1));
if(x->movieloaded)
SetMovieVolume(x->movie, (short)(x->vol*255.));
return JIT_ERR_NONE;
}
void jit_gl_hap_do_loop(t_jit_gl_hap *x)
{
long flags = 0;
switch(x->loop) {
case JIT_GL_HAP_LOOP_ON: flags = loopTimeBase; break;
case JIT_GL_HAP_LOOP_PALINDROME: flags = palindromeLoopTimeBase; break;
case JIT_GL_HAP_LOOP_LIMITS:
case JIT_GL_HAP_LOOP_OFF:
default: flags = 0; break;
}
x->loopflags = flags;
if(x->movieloaded)
jit_gl_hap_do_looppoints(x);
}
void jit_gl_hap_do_looppoints(t_jit_gl_hap *x)
{
TimeRecord tr;
TimeBase tb;
long start,end;
long ts;
if(x->looppoints[1]<0)
x->looppoints[1] = x->duration;
if(x->looppoints[0]>=x->looppoints[1] && x->looppoints[1]>0)
x->looppoints[0] = x->looppoints[1]-1;
CLIP_ASSIGN(x->looppoints[1], x->looppoints[0]+1, x->duration);
CLIP_ASSIGN(x->looppoints[0], 0, x->looppoints[1]-1);
start = x->looppoints[0];
end = x->looppoints[1];
tb = GetMovieTimeBase(x->movie);
SetMovieSelection(x->movie, 0, x->duration);
SetMovieActiveSegment(x->movie, 0, x->duration);
ts = GetMovieTimeScale(x->movie);
GetTimeBaseTime(tb, ts, &tr);
tr.value.lo = GetMovieTime(x->movie, NULL);
SetTimeBaseTime(tb, &tr);
// check loop duration
if (end - start < (ts * 0.001))
end = start + (long)(MAX(1, ts * 0.001));
if (x->loop != JIT_GL_HAP_LOOP_OFF) {
SetTimeBaseFlags(tb,0);
GetTimeBaseStartTime(tb, ts, &tr);
tr.value.lo = start;
SetTimeBaseStartTime(tb, &tr);
tr.value.lo = end;
SetTimeBaseStopTime(tb, &tr);
SetTimeBaseFlags(tb, x->loopflags);
if (x->loop == JIT_GL_HAP_LOOP_LIMITS) {
SetMovieSelection(x->movie, start, (end - start));
SetMovieActiveSegment(x->movie, start, (end - start));
}
}
else {
SetTimeBaseFlags(tb,0);
GetTimeBaseStartTime(tb, ts, &tr);
tr.value.lo = 0;
SetTimeBaseStartTime(tb, &tr);
tr.value.lo = x->duration;
SetTimeBaseStopTime(tb, &tr);
SetTimeBaseFlags(tb,x->loopflags);
}
}
long jit_gl_hap_do_loadram(t_jit_gl_hap *x, t_atom_long from, t_atom_long to, short unload)
{
t_atom_long duration = x->duration;
if(to)
CLIP_ASSIGN(to, 0, duration);
else
to = duration;
CLIP_ASSIGN(from, 0, duration);
if (from > to) {
t_atom_long temp = to;
to = from;
from = temp;
}
return LoadMovieIntoRam(x->movie, from, to - from, (unload ? unkeepInRam : keepInRam));
}
#endif