comparison libvo/vo_macosx.m @ 15289:e985d786c5f1

macosx core video module
author nplourde
date Fri, 29 Apr 2005 11:05:16 +0000
parents
children f743828642fd
comparison
equal deleted inserted replaced
15288:7fc3a521d42b 15289:e985d786c5f1
1 /*
2 vo_macosx.m
3 by Nicolas Plourde <nicolasplourde@gmail.com>
4
5 MPlayer Mac OSX video out module.
6 Copyright (c) Nicolas Plourde - 2005
7 */
8
9 #import "vo_macosx.h"
10
11 //MPLAYER
12 #include "config.h"
13 #include "fastmemcpy.h"
14 #include "video_out.h"
15 #include "video_out_internal.h"
16 #include "aspect.h"
17 #include "mp_msg.h"
18 #include "m_option.h"
19
20 #include "input/input.h"
21 #include "input/mouse.h"
22
23 #include "osdep/keycodes.h"
24
25 extern void mplayer_put_key(int code);
26
27 //Cocoa
28 CustomOpenGLView *glView;
29 NSAutoreleasePool *autoreleasepool;
30 OSType pixelFormat;
31
32 //Device
33 static int device_width;
34 static int device_height;
35 static int device_id;
36 static GDHandle device_handle;
37
38 //image
39 unsigned char *image_data;
40 static uint32_t image_width;
41 static uint32_t image_height;
42 static uint32_t image_depth;
43 static uint32_t image_bytes;
44 static uint32_t image_format;
45 static NSRect image_rec;
46
47 //vo
48 extern int vo_rootwin;
49 extern int vo_ontop;
50 extern int vo_fs;
51 static int isFullscreen;
52 extern float monitor_aspect;
53 extern int vo_keepaspect;
54 extern float movie_aspect;
55 static float old_movie_aspect;
56 extern float vo_panscan;
57
58 static int int_pause = 0;
59
60 static vo_info_t info =
61 {
62 "Mac OSX Core Video",
63 "macosx",
64 "Nicolas Plourde <nicolas.plourde@gmail.com>",
65 ""
66 };
67
68 LIBVO_EXTERN(macosx)
69
70 extern void mplayer_put_key(int code);
71 extern void vo_draw_text(int dxs,int dys,void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride));
72
73 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src, unsigned char *srca, int stride)
74 {
75 switch (image_format)
76 {
77 case IMGFMT_RGB32:
78 vo_draw_alpha_rgb32(w,h,src,srca,stride,image_data+4*(y0*image_width+x0),4*image_width);
79 break;
80 case IMGFMT_YUY2:
81 vo_draw_alpha_yuy2(w,h,src,srca,stride,image_data + (x0 + y0 * image_width) * 2,image_width*2);
82 break;
83 }
84 }
85
86 static uint32_t config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
87 {
88 int i;
89
90 //Get Main device info///////////////////////////////////////////////////
91 device_handle = GetMainDevice();
92
93 for(i=0; i<device_id; i++)
94 {
95 device_handle = GetNextDevice(device_handle);
96
97 if(device_handle == NULL)
98 {
99 mp_msg(MSGT_VO, MSGL_FATAL, "Get device error: Device ID %d do not exist, falling back to main device.\n", device_id);
100 device_handle = GetMainDevice();
101 device_id = 0;
102 break;
103 }
104 }
105
106 NSRect device_rect = [[NSScreen mainScreen] frame];
107 device_width = device_rect.size.width;
108 device_height = device_rect.size.height;
109 monitor_aspect = (float)device_width/(float)device_height;
110
111 //misc mplayer setup
112 image_width = width;
113 image_height = height;
114 switch (image_format)
115 {
116 case IMGFMT_BGR32:
117 case IMGFMT_RGB32:
118 image_depth = 32;
119 break;
120 case IMGFMT_YUY2:
121 image_depth = 16;
122 break;
123 }
124 image_bytes = (image_depth + 7) / 8;
125 image_data = (unsigned char*)malloc(image_width*image_height*image_bytes);
126
127 //set aspect
128 panscan_init();
129 aspect_save_orig(width,height);
130 aspect_save_prescale(d_width,d_height);
131 aspect_save_screenres(device_width,device_height);
132 aspect(&d_width,&d_height,A_NOZOOM);
133
134 movie_aspect = (float)d_width/(float)d_height;
135 old_movie_aspect = movie_aspect;
136
137 //init OpenGL View
138 glView = [[CustomOpenGLView alloc] initWithFrame:NSMakeRect(0, 0, d_width, d_height) pixelFormat:[CustomOpenGLView defaultPixelFormat]];
139 [glView initOpenGLView];
140
141 vo_fs = flags & VOFLAG_FULLSCREEN;
142
143 if(vo_fs)
144 [glView fullscreen: NO];
145
146 if(vo_ontop)
147 [glView ontop];
148
149 return 0;
150 }
151
152 static void check_events(void)
153 {
154 [glView check_events];
155
156 //update activity every 60 seconds to prevent
157 //screensaver from starting up.
158 DateTimeRec d;
159 unsigned long curTime;
160 static unsigned long lastTime = 0;
161
162 GetTime(&d);
163 DateToSeconds( &d, &curTime);
164
165 if( ( (curTime - lastTime) >= 60) || (lastTime == 0))
166 {
167 UpdateSystemActivity(UsrActivity);
168 lastTime = curTime;
169 }
170 }
171
172 static void draw_osd(void)
173 {
174 vo_draw_text(image_width, image_height, draw_alpha);
175 }
176
177 static void flip_page(void)
178 {
179 [glView render];
180 }
181
182 static uint32_t draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
183 {
184 [glView setCurrentTexture];
185 return 0;
186 }
187
188
189 static uint32_t draw_frame(uint8_t *src[])
190 {
191 switch (image_format)
192 {
193 case IMGFMT_BGR32:
194 case IMGFMT_RGB32:
195 memcpy(image_data, src[0], image_width*image_height*image_bytes);
196 break;
197
198 case IMGFMT_YUY2:
199 memcpy_pic(image_data, src[0], image_width * 2, image_height, image_width * 2, image_width * 2);
200 break;
201 }
202 [glView setCurrentTexture];
203 return 0;
204 }
205
206 static uint32_t query_format(uint32_t format)
207 {
208 image_format = format;
209
210 switch(format)
211 {
212 case IMGFMT_YUY2:
213 pixelFormat = kYUVSPixelFormat;
214 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
215
216 case IMGFMT_RGB32:
217 case IMGFMT_BGR32:
218 pixelFormat = k32ARGBPixelFormat;
219 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
220 }
221 return 0;
222 }
223
224 static void uninit(void)
225 {
226 [autoreleasepool release];
227 }
228
229 static uint32_t preinit(const char *arg)
230 {
231 int parse_err = 0;
232
233 if(arg)
234 {
235 char *parse_pos = (char *)&arg[0];
236 while (parse_pos[0] && !parse_err)
237 {
238 if (strncmp (parse_pos, "device_id=", 10) == 0)
239 {
240 parse_pos = &parse_pos[10];
241 device_id = strtol(parse_pos, &parse_pos, 0);
242 }
243 if (parse_pos[0] == ':') parse_pos = &parse_pos[1];
244 else if (parse_pos[0]) parse_err = 1;
245 }
246 }
247
248 #if !defined (MACOSX_FINDER_SUPPORT) || !defined (HAVE_SDL)
249 //this chunk of code is heavily based off SDL_macosx.m from SDL
250 //it uses an Apple private function to request foreground operation
251 void CPSEnableForegroundOperation(ProcessSerialNumber* psn);
252 ProcessSerialNumber myProc, frProc;
253 Boolean sameProc;
254
255 if (GetFrontProcess(&frProc) == noErr)
256 {
257 if (GetCurrentProcess(&myProc) == noErr)
258 {
259 if (SameProcess(&frProc, &myProc, &sameProc) == noErr && !sameProc)
260 {
261 CPSEnableForegroundOperation(&myProc);
262 }
263 SetFrontProcess(&myProc);
264 }
265 }
266 #endif
267
268 NSApplicationLoad();
269 autoreleasepool = [[NSAutoreleasePool alloc] init];
270
271 return 0;
272 }
273
274 static uint32_t control(uint32_t request, void *data, ...)
275 {
276 switch (request)
277 {
278 case VOCTRL_PAUSE: return (int_pause=1);
279 case VOCTRL_RESUME: return (int_pause=0);
280 case VOCTRL_QUERY_FORMAT: return query_format(*((uint32_t*)data));
281 case VOCTRL_ONTOP: vo_ontop = (!(vo_ontop)); [glView ontop]; return VO_TRUE;
282 case VOCTRL_FULLSCREEN: vo_fs = (!(vo_fs)); [glView fullscreen: YES]; return VO_TRUE;
283 case VOCTRL_GET_PANSCAN: return VO_TRUE;
284 case VOCTRL_SET_PANSCAN: [glView panscan]; return VO_TRUE;
285 }
286 return VO_NOTIMPL;
287 }
288
289 //////////////////////////////////////////////////////////////////////////
290 // NSOpenGLView Subclass
291 //////////////////////////////////////////////////////////////////////////
292 @implementation CustomOpenGLView
293 - (void) initOpenGLView
294 {
295 NSRect frame = [self frame];
296 CVReturn error = kCVReturnSuccess;
297
298 //create OpenGL Context
299 glContext = [[NSOpenGLContext alloc] initWithFormat:[NSOpenGLView defaultPixelFormat] shareContext:nil];
300
301
302 //create window
303 window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, frame.size.width, frame.size.height)
304 styleMask:NSTitledWindowMask|NSTexturedBackgroundWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask
305 backing:NSBackingStoreBuffered
306 defer:NO];
307
308 [window setContentView:self];
309 [window setInitialFirstResponder:self];
310 [window setAcceptsMouseMovedEvents:YES];
311 [window setTitle:@"MPlayer - The Movie Player"];
312 [window center];
313 [window makeKeyAndOrderFront:nil];
314
315 [self setOpenGLContext:glContext];
316 [glContext setView:self];
317 [glContext makeCurrentContext];
318
319 error = CVPixelBufferCreateWithBytes( NULL,
320 image_width, image_height,
321 pixelFormat,
322 image_data,
323 image_width*image_bytes,
324 NULL, NULL, NULL,
325 &currentFrameBuffer);
326 if(error != kCVReturnSuccess)
327 mp_msg(MSGT_VO, MSGL_ERR,"Failed to create Pixel Buffer(%d)\n", error);
328
329 error = CVOpenGLTextureCacheCreate(NULL, 0, [glContext CGLContextObj], [[self pixelFormat] CGLPixelFormatObj], 0, &textureCache);
330 if(error != kCVReturnSuccess)
331 mp_msg(MSGT_VO, MSGL_ERR,"Failed to create OpenGL texture Cache(%d)\n", error);
332
333 error = CVOpenGLTextureCacheCreateTextureFromImage( NULL, textureCache, currentFrameBuffer, 0, &texture);
334 if(error != kCVReturnSuccess)
335 mp_msg(MSGT_VO, MSGL_ERR,"Failed to create OpenGL texture(%d)\n", error);
336
337 isFullscreen = 0;
338 }
339
340 /*
341 Setup OpenGL
342 */
343 - (void)prepareOpenGL
344 {
345 glDisable(GL_BLEND);
346 glDisable(GL_DEPTH_TEST);
347 glDepthMask(GL_FALSE);
348 glDisable(GL_CULL_FACE);
349 [self reshape];
350 }
351
352 /*
353 reshape OpenGL viewport
354 */
355 - (void)reshape
356 {
357 uint32_t d_width;
358 uint32_t d_height;
359 float aspectX;
360 float aspectY;
361 int padding = 0;
362
363 NSRect frame = [self frame];
364
365 glViewport(0, 0, frame.size.width, frame.size.height);
366 glMatrixMode(GL_PROJECTION);
367 glLoadIdentity();
368 glOrtho(0, frame.size.width, frame.size.height, 0, -1.0, 1.0);
369 glMatrixMode(GL_MODELVIEW);
370 glLoadIdentity();
371
372 //set image_rec
373 if(vo_keepaspect)
374 {
375 aspect( &d_width, &d_height, A_NOZOOM);
376 d_height = ((float)d_width/movie_aspect);
377
378 aspectX = (float)((float)frame.size.width/(float)d_width);
379 aspectY = (float)((float)(frame.size.height)/(float)d_height);
380
381 if((d_height*aspectX)>(frame.size.height))
382 {
383 padding = (frame.size.width - d_width*aspectY)/2;
384 image_rec.origin.x = padding;
385 image_rec.origin.y = 0;
386 image_rec.size.width = d_width*aspectY+padding;
387 image_rec.size.height = d_height*aspectY;
388 }
389 else
390 {
391 padding = ((frame.size.height) - d_height*aspectX)/2;
392 image_rec.origin.x = 0;
393 image_rec.origin.y = padding;
394 image_rec.size.width = d_width*aspectX;
395 image_rec.size.height = d_height*aspectX+padding;
396 }
397 }
398 else
399 {
400 image_rec = frame;
401 }
402 }
403
404 /*
405 Render frame
406 */
407 - (void) render
408 {
409 glClear(GL_COLOR_BUFFER_BIT);
410
411 glEnable(CVOpenGLTextureGetTarget(texture));
412 glBindTexture(CVOpenGLTextureGetTarget(texture), CVOpenGLTextureGetName(texture));
413
414 glColor3f(1,1,1);
415 glBegin(GL_QUADS);
416 glTexCoord2f(upperLeft[0], upperLeft[1]); glVertex2i(image_rec.origin.x, image_rec.origin.y);
417 glTexCoord2f(lowerLeft[0], lowerLeft[1]); glVertex2i(image_rec.origin.x, image_rec.size.height);
418 glTexCoord2f(lowerRight[0], lowerRight[1]); glVertex2i(image_rec.size.width, image_rec.size.height);
419 glTexCoord2f(upperRight[0], upperRight[1]); glVertex2i(image_rec.size.width, image_rec.origin.y);
420 glEnd();
421
422 glFlush();
423
424 //auto hide mouse cursor and futur on-screen control?
425 if(isFullscreen && !mouseHide)
426 {
427 DateTimeRec d;
428 unsigned long curTime;
429 static unsigned long lastTime = 0;
430
431 GetTime(&d);
432 DateToSeconds( &d, &curTime);
433
434 if( ((curTime - lastTime) >= 5) || (lastTime == 0) )
435 {
436 HideMenuBar();
437 HideCursor();
438 mouseHide = YES;
439 lastTime = curTime;
440 }
441 }
442 }
443
444 /*
445 Create OpenGL texture from current frame & set texco
446 */
447 - (void) setCurrentTexture
448 {
449 CVReturn error = kCVReturnSuccess;
450
451 error = CVOpenGLTextureCacheCreateTextureFromImage (NULL, textureCache, currentFrameBuffer, 0, &texture);
452 if(error != kCVReturnSuccess)
453 mp_msg(MSGT_VO, MSGL_ERR,"Failed to create OpenGL texture(%d)\n", error);
454
455 CVOpenGLTextureGetCleanTexCoords(texture, lowerLeft, lowerRight, upperRight, upperLeft);
456 }
457
458 /*
459 redraw win rect
460 */
461 - (void) drawRect: (NSRect *) bounds
462 {
463 [self render];
464 }
465
466 /*
467 Toggle Fullscreen
468 */
469 - (void) fullscreen: (BOOL) animate
470 {
471 static NSRect old_frame;
472 static NSRect old_view_frame;
473 NSRect device_rect = [[window screen] frame];
474
475 //go fullscreen
476 if(vo_fs)
477 {
478 //hide menubar and mouse if fullscreen on main display
479 HideMenuBar();
480 HideCursor();
481 mouseHide = YES;
482
483 panscan_calc();
484 old_frame = [window frame]; //save main window size & position
485 [window setFrame:device_rect display:YES animate:animate]; //zoom-in window with nice useless sfx
486 old_view_frame = [self bounds];
487
488 //fix origin for multi screen setup
489 device_rect.origin.x = 0;
490 device_rect.origin.y = 0;
491 [self setFrame:device_rect];
492 [self setNeedsDisplay:YES];
493 [window setHasShadow:NO];
494 isFullscreen = 1;
495 }
496 else
497 {
498 isFullscreen = 0;
499 ShowMenuBar();
500 ShowCursor();
501 mouseHide = NO;
502
503 //revert window to previous setting
504 [self setFrame:old_view_frame];
505 [self setNeedsDisplay:YES];
506 [window setHasShadow:NO];
507 [window setFrame:old_frame display:YES animate:animate];//zoom-out window with nice useless sfx
508 }
509 }
510
511 /*
512 Toggle ontop
513 */
514 - (void) ontop
515 {
516 }
517
518 /*
519 Toggle panscan
520 */
521 - (void) panscan
522 {
523 }
524
525 /*
526 Check event for new event
527 */
528 - (void) check_events
529 {
530 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate dateWithTimeIntervalSinceNow:0.0001] inMode:NSEventTrackingRunLoopMode dequeue:YES];
531 [NSApp sendEvent:event];
532 }
533
534 /*
535 Process key event
536 */
537 - (void) keyDown: (NSEvent *) theEvent
538 {
539 unsigned int key;
540
541 switch([theEvent keyCode])
542 {
543 case 0x34:
544 case 0x24: key = KEY_ENTER; break;
545 case 0x35: key = KEY_ESC; break;
546 case 0x33: key = KEY_BACKSPACE; break;
547 case 0x3A: key = KEY_BACKSPACE; break;
548 case 0x3B: key = KEY_BACKSPACE; break;
549 case 0x38: key = KEY_BACKSPACE; break;
550 case 0x7A: key = KEY_F+1; break;
551 case 0x78: key = KEY_F+2; break;
552 case 0x63: key = KEY_F+3; break;
553 case 0x76: key = KEY_F+4; break;
554 case 0x60: key = KEY_F+5; break;
555 case 0x61: key = KEY_F+6; break;
556 case 0x62: key = KEY_F+7; break;
557 case 0x64: key = KEY_F+8; break;
558 case 0x65: key = KEY_F+9; break;
559 case 0x6D: key = KEY_F+10; break;
560 case 0x67: key = KEY_F+11; break;
561 case 0x6F: key = KEY_F+12; break;
562 case 0x72: key = KEY_INSERT; break;
563 case 0x75: key = KEY_DELETE; break;
564 case 0x73: key = KEY_HOME; break;
565 case 0x77: key = KEY_END; break;
566 case 0x45: key = '+'; break;
567 case 0x4E: key = '-'; break;
568 case 0x30: key = KEY_TAB; break;
569 case 0x74: key = KEY_PAGE_UP; break;
570 case 0x79: key = KEY_PAGE_DOWN; break;
571 case 0x7B: key = KEY_LEFT; break;
572 case 0x7C: key = KEY_RIGHT; break;
573 case 0x7D: key = KEY_DOWN; break;
574 case 0x7E: key = KEY_UP; break;
575 case 0x43: key = '*'; break;
576 case 0x4B: key = '/'; break;
577 case 0x4C: key = KEY_BACKSPACE; break;
578 case 0x41: key = KEY_KPDEC; break;
579 case 0x52: key = KEY_KP0; break;
580 case 0x53: key = KEY_KP1; break;
581 case 0x54: key = KEY_KP2; break;
582 case 0x55: key = KEY_KP3; break;
583 case 0x56: key = KEY_KP4; break;
584 case 0x57: key = KEY_KP5; break;
585 case 0x58: key = KEY_KP6; break;
586 case 0x59: key = KEY_KP7; break;
587 case 0x5B: key = KEY_KP8; break;
588 case 0x5C: key = KEY_KP9; break;
589 default: key = *[[theEvent characters] UTF8String]; break;
590 }
591 mplayer_put_key(key);
592 }
593
594 /*
595 Process mouse button event
596 */
597 - (void) mouseMoved: (NSEvent *) theEvent
598 {
599 if(isFullscreen)
600 {
601 ShowMenuBar();
602 ShowCursor();
603 mouseHide = NO;
604 }
605 }
606
607 - (void) mouseDown: (NSEvent *) theEvent
608 {
609 [self mouseEvent: theEvent];
610 }
611
612 - (void) rightMouseDown: (NSEvent *) theEvent
613 {
614 [self mouseEvent: theEvent];
615 }
616
617 - (void) otherMouseDown: (NSEvent *) theEvent
618 {
619 [self mouseEvent: theEvent];
620 }
621
622 - (void) scrollWheel: (NSEvent *) theEvent
623 {
624 if([theEvent deltaY] > 0)
625 mplayer_put_key(MOUSE_BTN3);
626 else
627 mplayer_put_key(MOUSE_BTN4);
628 }
629
630 - (void) mouseEvent: (NSEvent *) theEvent
631 {
632 switch( [theEvent buttonNumber] )
633 {
634 case 0: mplayer_put_key(MOUSE_BTN0);break;
635 case 1: mplayer_put_key(MOUSE_BTN1);break;
636 case 2: mplayer_put_key(MOUSE_BTN2);break;
637 }
638 }
639
640 /*
641 NSResponder
642 */
643 - (BOOL) acceptsFirstResponder
644 {
645 return YES;
646 }
647
648 - (BOOL) becomeFirstResponder
649 {
650 return YES;
651 }
652
653 - (BOOL) resignFirstResponder
654 {
655 return YES;
656 }
657 @end