-
Notifications
You must be signed in to change notification settings - Fork 1
/
ogl.cpp
467 lines (378 loc) · 13.3 KB
/
ogl.cpp
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "ogl.hpp"
#include <X11/Xlib.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include "ahab_fragment_program.hpp"
#include "exceptions.hpp"
#include "colorimetry.hpp"
#include "displayopq.hpp"
const int opqueue_len = 8;
static void *thread_helper( void *ogl )
{
OpenGLDisplay *me = static_cast<OpenGLDisplay *>( ogl );
ahabassert( me );
me->loop();
return NULL;
}
OpenGLDisplay::OpenGLDisplay( char *display_name,
double movie_sar,
uint s_framewidth, uint s_frameheight,
uint s_dispwidth, uint s_dispheight )
: opq( opqueue_len )
{
state.framewidth = s_framewidth;
state.frameheight = s_frameheight;
state.dispwidth = s_dispwidth;
state.dispheight = s_dispheight;
if ( 0 == XInitThreads() ) {
fprintf( stderr, "XInitThreads() failed." );
throw DisplayError();
}
state.display = XOpenDisplay( display_name );
if ( state.display == NULL ) {
fprintf( stderr, "Couldn't open display.\n" );
throw DisplayError();
}
/* Figure out the dimensions given the width and height,
the video sample aspect ratio, and the screen pixel aspect ratio */
double display_sar = ((double)DisplayHeight( state.display, DefaultScreen( state.display ) )
/ (double)DisplayHeightMM( state.display, DefaultScreen( state.display ) ))
/ ((double)DisplayWidth( state.display, DefaultScreen( state.display ) )
/ (double)DisplayWidthMM( state.display, DefaultScreen( state.display ) ));
state.sar = movie_sar / display_sar;
// sar = 1; /* XXX */
if ( state.sar > 1 ) {
state.width = lrint( (double)state.dispwidth * state.sar );
state.height = state.dispheight;
} else {
state.width = state.dispwidth;
state.height = lrint( (double)state.dispheight / state.sar );
}
fprintf( stderr, "Display is %dx%d with pixel AR %.3f:1, display AR %.3f:1.\n",
DisplayWidth( state.display, DefaultScreen( state.display ) ),
DisplayHeight( state.display, DefaultScreen( state.display ) ),
display_sar,
(double)DisplayWidthMM( state.display, DefaultScreen( state.display ) )
/ (double)DisplayHeightMM( state.display, DefaultScreen( state.display ) ) );
fprintf( stderr, "MPEG-2 sequence is %dx%d with sample AR %.3f:1, display AR %.3f:1.\n",
state.dispwidth, state.dispheight,
movie_sar,
movie_sar * state.dispwidth / (double)state.dispheight );
fprintf( stderr, "Video SAR in display pixel units = %.3f:1. Display size = %dx%d.\n",
state.sar, state.width, state.height );
state.GetSync = (Bool (*)(Display*, GLXDrawable, int64_t*, int64_t*, int64_t*))glXGetProcAddress( (GLubyte *) "glXGetSyncValuesOML" );
ahabassert( state.GetSync );
state.last_mbc = -1;
state.last_us = -1;
unixassert( pthread_create( &thread_handle, NULL,
thread_helper, this ) );
int prio = sched_get_priority_max( SCHED_FIFO );
ahabassert( prio != -1 );
struct sched_param params;
params.sched_priority = prio;
unixassert( pthread_setschedparam( thread_handle,
SCHED_FIFO, ¶ms ) );
}
void OpenGLDisplay::init_context( void ) {
int attributes[] = { GLX_RGBA,
GLX_DOUBLEBUFFER, True,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
None };
XVisualInfo *visual = glXChooseVisual( state.display, 0, attributes );
if ( visual == NULL ) {
fprintf( stderr, "Could not open glX visual.\n" );
throw DisplayError();
}
state.context = glXCreateContext( state.display, visual, NULL, True );
if ( state.context == NULL ) {
fprintf( stderr, "No glX context.\n" );
throw DisplayError();
}
XFree( visual );
if ( !glXMakeCurrent( state.display, state.window, state.context ) ) {
fprintf( stderr, "Could not reactivate OpenGL.\n" );
throw DisplayError();
}
GLcheck( "glXMakeCurrent" );
/* initialize textures */
init_tex( GL_TEXTURE0, GL_LUMINANCE8, &state.Y_tex,
state.framewidth, state.frameheight, GL_LINEAR );
init_tex( GL_TEXTURE1, GL_LUMINANCE8, &state.Cb_tex,
state.framewidth/2, state.frameheight/2, GL_LINEAR );
init_tex( GL_TEXTURE2, GL_LUMINANCE8, &state.Cr_tex,
state.framewidth/2, state.frameheight/2, GL_LINEAR );
/* load the shader */
GLint errorloc;
glEnable( GL_FRAGMENT_PROGRAM_ARB );
glGenProgramsARB( 1, &shader );
glBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, shader );
glProgramStringARB( GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
strlen( ahab_fragment_program ),
ahab_fragment_program );
glGetIntegerv( GL_PROGRAM_ERROR_POSITION_ARB, &errorloc );
if ( errorloc != -1 ) {
fprintf( stderr, "Error in fragment shader at position %d.\n", errorloc );
fprintf( stderr, "Error string: %s\n",
glGetString( GL_PROGRAM_ERROR_STRING_ARB ) );
throw DisplayError();
}
GLcheck( "glProgramString" );
/* guess colors */
if ( state.frameheight <= 480 ) {
smpte170m.execute( state );
} else {
itu709.execute( state );
}
}
void OpcodeState::reset_viewport( void )
{
glXSwapBuffers( display, window );
glFinish();
OpenGLDisplay::GLcheck( "reset_viewport: glFinish" );
XSync( display, False );
glLoadIdentity();
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, width, height, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
OpenGLDisplay::GLcheck( "glPixelStorei" );
glWindowPos2d( 0, 0 );
glClear( GL_COLOR_BUFFER_BIT );
OpenGLDisplay::GLcheck( "reset_viewport" );
}
void OpcodeState::window_setup( void )
{
window = XCreateSimpleWindow( display, DefaultRootWindow( display ),
0, 0, width, height, 0, 0, 0 );
XSetStandardProperties( display, window, "Ahab", "Ahab",
None, NULL, 0, NULL );
XSelectInput( display, window, ExposureMask | KeyPressMask );
}
OpenGLDisplay::~OpenGLDisplay()
{
opq.flush();
DisplayOperation *shutdown = new ShutDown();
opq.enqueue( shutdown );
unixassert( pthread_join( thread_handle, NULL ) );
delete shutdown; /* Doesn't get deleted by loop because opcode execute() exits first. */
glDeleteProgramsARB( 1, &shader );
glDeleteTextures( 1, &state.Y_tex );
glDeleteTextures( 1, &state.Cb_tex );
glDeleteTextures( 1, &state.Cr_tex );
glXDestroyContext( state.display, state.context );
XDestroyWindow( state.display, state.window );
XCloseDisplay( state.display );
}
void OpenGLDisplay::init_tex( GLenum tnum, GLint internalformat, GLuint *tex,
uint width, uint height, GLint interp )
{
glActiveTexture( tnum );
glEnable( GL_TEXTURE_RECTANGLE_ARB );
glGenTextures( 1, tex );
glBindTexture( GL_TEXTURE_RECTANGLE_ARB, *tex );
glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0,
internalformat, width, height,
0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL );
glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, interp );
glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, interp );
glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
GLcheck( "init_tex" );
}
void OpcodeState::paint( void )
{
glPushMatrix();
glLoadIdentity();
glTranslatef( 0, 0, 0 );
glBegin( GL_POLYGON );
const double ff = 1.0/128; /* Mesa fudge factor */
const double xoffset = 0.25; /* MPEG-2 style 4:2:0 subsampling */
glMultiTexCoord2d( GL_TEXTURE0, ff, ff );
glMultiTexCoord2d( GL_TEXTURE1, xoffset+ff, ff );
glMultiTexCoord2d( GL_TEXTURE2, xoffset+ff, ff );
glVertex2s( 0, 0 );
glMultiTexCoord2d( GL_TEXTURE0, dispwidth+ff, ff );
glMultiTexCoord2d( GL_TEXTURE1, dispwidth/2 + xoffset + ff, ff );
glMultiTexCoord2d( GL_TEXTURE2, dispwidth/2 + xoffset + ff, ff );
glVertex2s( width, 0 );
glMultiTexCoord2d( GL_TEXTURE0, dispwidth+ff, dispheight+ff );
glMultiTexCoord2d( GL_TEXTURE1, dispwidth/2 + xoffset + ff, dispheight/2 + ff);
glMultiTexCoord2d( GL_TEXTURE2, dispwidth/2 + xoffset + ff, dispheight/2 + ff);
glVertex2s( width, height);
glMultiTexCoord2d( GL_TEXTURE0, ff, dispheight+ff );
glMultiTexCoord2d( GL_TEXTURE1, xoffset+ff, dispheight/2 + ff );
glMultiTexCoord2d( GL_TEXTURE2, xoffset+ff, dispheight/2 + ff );
glVertex2s( 0, height);
glEnd();
glPopMatrix();
glXSwapBuffers( display, window );
OpenGLDisplay::GLcheck( "glXSwapBuffers" );
glFinish();
/* Check if we stuttered */
int64_t ust, mbc, sbc, us;
struct timeval now;
gettimeofday( &now, NULL );
us = 1000000 * now.tv_sec + now.tv_usec;
long us_diff = us - last_us;
GetSync( display, window, &ust, &mbc, &sbc );
bool msg = false;
if ( (last_mbc != -1) && (mbc != last_mbc + 1) ) {
long int diff = mbc - last_mbc;
fprintf( stderr, "[Skipped %ld retraces.]", diff );
msg = true;
}
if ( (last_us != -1) && ( (us_diff < 8000) || (us_diff > 24000) ) ) {
fprintf( stderr, "%s[Time diff was %ld us.]", msg ? " " : "", (long)us_diff );
msg = true;
}
if ( msg ) {
fprintf( stderr, "\n" );
}
last_mbc = mbc;
last_us = us;
}
typedef struct
{
long flags;
long functions;
long decorations;
long input_mode;
long state;
} MotifWmHints;
#define MWM_HINTS_DECORATIONS (1L << 1)
void OpcodeState::dofullscreen( void )
{
/* move on top */
XEvent xev;
Atom wm_state = XInternAtom( display, "_NET_WM_STATE", False);
Atom fullscreen = XInternAtom( display, "_NET_WM_STATE_FULLSCREEN", False);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 1;
xev.xclient.data.l[1] = fullscreen;
xev.xclient.data.l[2] = 0;
XSendEvent( display, DefaultRootWindow( display ), False,
SubstructureNotifyMask, &xev);
/* hide cursor */
Cursor thecursor;
Pixmap thepixmap;
char no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
XColor bg;
thepixmap = XCreateBitmapFromData( display, window, no_data, 8, 8 );
thecursor = XCreatePixmapCursor( display, thepixmap, thepixmap, &bg, &bg, 0, 0 );
XDefineCursor( display, window, thecursor );
XFreeCursor( display, thecursor );
XFreePixmap( display, thepixmap );
/* adjust width and height of displayed image */
width = DisplayWidth( display, DefaultScreen( display ) );
height = lrint( (double)dispheight * (double)width / ((double)dispwidth * sar) );
if ( (signed)height > DisplayHeight( display, DefaultScreen( display ) ) ) {
height = DisplayHeight( display, DefaultScreen( display ) );
width = lrint( (double)dispwidth * (double) sar * (double)height / (double)dispheight );
}
reset_viewport();
OpenGLDisplay::GLcheck( "fullscreen" );
paint();
}
void OpcodeState::unfullscreen( void )
{
XDestroyWindow( display, window );
/* adjust width and height of displayed image */
if ( sar > 1 ) {
width = lrint( (double)dispwidth * sar );
height = dispheight;
} else {
width = dispwidth;
height = lrint( (double)dispheight / sar );
}
window_setup();
if ( !glXMakeCurrent( display, window, context ) ) {
fprintf( stderr, "Could not reactivate OpenGL.\n" );
throw DisplayError();
}
OpenGLDisplay::GLcheck( "glXMakeCurrent" );
reset_viewport();
XMapRaised( display, window );
paint();
}
void OpenGLDisplay::makeevent( void )
{
XEvent event;
memset( &event, 0, sizeof( event ) );
event.type = Expose;
if ( 0 == XSendEvent( state.display, state.window, False, ExposureMask, &event ) ) {
throw DisplayError();
};
XFlush( state.display );
}
bool OpenGLDisplay::getevent( bool block, XEvent *ev )
{
if ( block || XPending( state.display ) ) {
XNextEvent( state.display, ev );
return true;
} else {
return false;
}
}
void OpcodeState::load_matrix_coefficients( double green[ 3 ],
double blue[ 3 ],
double red[ 3 ] )
{
glProgramLocalParameter4dARB( GL_FRAGMENT_PROGRAM_ARB, 0,
green[ 0 ], green[ 1 ], green[ 2 ], 0 );
glProgramLocalParameter4dARB( GL_FRAGMENT_PROGRAM_ARB, 1,
blue[ 0 ], blue[ 1 ], blue[ 2 ], 0 );
glProgramLocalParameter4dARB( GL_FRAGMENT_PROGRAM_ARB, 2,
red[ 0 ], red[ 1 ], red[ 2 ], 0 );
OpenGLDisplay::GLcheck( "glProgramEnvParamater4dARB" );
}
void OpcodeState::load_tex( GLenum tnum, GLuint tex,
uint width, uint height, uint8_t *data )
{
glActiveTexture( tnum );
OpenGLDisplay::GLcheck( "glActiveTexture" );
glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex );
OpenGLDisplay::GLcheck( "glBindTexture" );
glTexSubImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, width, height,
GL_LUMINANCE, GL_UNSIGNED_BYTE, data );
OpenGLDisplay::GLcheck( "glTexSubImage2D" );
}
void OpcodeState::draw( uint8_t *ycbcr )
{
load_tex( GL_TEXTURE0, Y_tex, framewidth, frameheight, ycbcr );
load_tex( GL_TEXTURE1, Cb_tex, framewidth/2, frameheight/2,
ycbcr + framewidth * frameheight );
load_tex( GL_TEXTURE2, Cr_tex, framewidth/2, frameheight/2,
ycbcr + framewidth * frameheight + framewidth * frameheight / 4);
paint();
}
void OpenGLDisplay::loop( void )
{
state.window_setup();
XMapRaised( state.display, state.window );
init_context();
state.reset_viewport();
while ( 1 ) {
DisplayOperation *op = opq.dequeue( true );
op->execute( state );
delete op;
}
}