1 /*****************************************************************************
2 * vout_macosx.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: vout_macosx.m,v 1.16 2002/07/25 22:48:56 massiot Exp $
7 * Authors: Colin Delacroix <colin@zoy.org>
8 * Florian G. Pflug <fgp@phlo.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* strerror() */
38 #include <Cocoa/Cocoa.h>
39 #include <QuickTime/QuickTime.h>
41 #include "intf_macosx.h"
42 #include "vout_macosx.h"
44 #define QT_MAX_DIRECTBUFFERS 10
51 /* When using I420 output */
52 PlanarPixmapInfoYUV420 pixmap_i420;
55 /*****************************************************************************
57 *****************************************************************************/
58 static int vout_Create ( vout_thread_t * );
59 static int vout_Init ( vout_thread_t * );
60 static void vout_End ( vout_thread_t * );
61 static void vout_Destroy ( vout_thread_t * );
62 static int vout_Manage ( vout_thread_t * );
63 static void vout_Render ( vout_thread_t *, picture_t * );
64 static void vout_Display ( vout_thread_t *, picture_t * );
66 static int CoSendRequest ( vout_thread_t *, long );
67 static int CoCreateWindow ( vout_thread_t * );
68 static int CoDestroyWindow ( vout_thread_t * );
69 static int CoToggleFullscreen ( vout_thread_t * );
71 static void QTScaleMatrix ( vout_thread_t * );
72 static int QTCreateSequence ( vout_thread_t * );
73 static void QTDestroySequence ( vout_thread_t * );
74 static int QTNewPicture ( vout_thread_t *, picture_t * );
75 static void QTFreePicture ( vout_thread_t *, picture_t * );
77 /*****************************************************************************
78 * Functions exported as capabilities. They are declared as static so that
79 * we don't pollute the namespace too much.
80 *****************************************************************************/
81 void _M( vout_getfunctions )( function_list_t * p_function_list )
83 p_function_list->functions.vout.pf_create = vout_Create;
84 p_function_list->functions.vout.pf_init = vout_Init;
85 p_function_list->functions.vout.pf_end = vout_End;
86 p_function_list->functions.vout.pf_destroy = vout_Destroy;
87 p_function_list->functions.vout.pf_manage = vout_Manage;
88 p_function_list->functions.vout.pf_render = vout_Render;
89 p_function_list->functions.vout.pf_display = vout_Display;
92 /*****************************************************************************
93 * vout_Create: allocates MacOS X video thread output method
94 *****************************************************************************
95 * This function allocates and initializes a MacOS X vout method.
96 *****************************************************************************/
97 static int vout_Create( vout_thread_t *p_vout )
101 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
102 if( p_vout->p_sys == NULL )
104 msg_Err( p_vout, "out of memory" );
108 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
110 p_vout->p_sys->p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
112 if( p_vout->p_sys->p_intf == NULL )
114 msg_Err( p_vout, "no interface present" );
115 free( p_vout->p_sys );
119 if( p_vout->p_sys->p_intf->p_module == NULL ||
120 strcmp( p_vout->p_sys->p_intf->p_module->psz_object_name,
121 MODULE_STRING ) != 0 )
123 msg_Err( p_vout, "MacOS X interface module required" );
124 vlc_object_release( p_vout->p_sys->p_intf );
125 free( p_vout->p_sys );
129 p_vout->p_sys->h_img_descr =
130 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
131 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
133 p_vout->p_sys->b_mouse_pointer_visible = 1;
135 /* set window size */
136 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
137 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
139 if( ( err = EnterMovies() ) != noErr )
141 msg_Err( p_vout, "EnterMovies failed: %d", err );
142 free( p_vout->p_sys->p_matrix );
143 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
144 free( p_vout->p_sys );
148 if( vout_ChromaCmp( p_vout->render.i_chroma, VLC_FOURCC('I','4','2','0') ) )
150 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
151 nil, &p_vout->p_sys->img_dc );
152 if( err == noErr && p_vout->p_sys->img_dc != 0 )
154 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
155 p_vout->p_sys->i_codec = kYUV420CodecType;
159 msg_Err( p_vout, "failed to find an appropriate codec" );
164 msg_Err( p_vout, "chroma 0x%08x not supported",
165 p_vout->render.i_chroma );
168 if( p_vout->p_sys->img_dc == 0 )
170 free( p_vout->p_sys->p_matrix );
171 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
172 free( p_vout->p_sys );
176 if( CoCreateWindow( p_vout ) )
178 msg_Err( p_vout, "unable to create window" );
179 free( p_vout->p_sys->p_matrix );
180 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
181 free( p_vout->p_sys );
188 /*****************************************************************************
189 * vout_Init: initialize video thread output method
190 *****************************************************************************/
191 static int vout_Init( vout_thread_t *p_vout )
196 I_OUTPUTPICTURES = 0;
198 /* Initialize the output structure; we already found a codec,
199 * and the corresponding chroma we will be using. Since we can
200 * arbitrary scale, stick to the coordinates and aspect. */
201 p_vout->output.i_width = p_vout->render.i_width;
202 p_vout->output.i_height = p_vout->render.i_height;
203 p_vout->output.i_aspect = p_vout->render.i_aspect;
205 SetPort( p_vout->p_sys->p_qdport );
206 QTScaleMatrix( p_vout );
208 if( QTCreateSequence( p_vout ) )
210 msg_Err( p_vout, "unable to create sequence" );
214 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
215 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
219 /* Find an empty picture slot */
220 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
222 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
224 p_pic = p_vout->p_picture + i_index;
229 /* Allocate the picture */
230 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
235 p_pic->i_status = DESTROYED_PICTURE;
236 p_pic->i_type = DIRECT_PICTURE;
238 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
246 /*****************************************************************************
247 * vout_End: terminate video thread output method
248 *****************************************************************************/
249 static void vout_End( vout_thread_t *p_vout )
253 QTDestroySequence( p_vout );
255 /* Free the direct buffers we allocated */
256 for( i_index = I_OUTPUTPICTURES; i_index; )
259 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
263 /*****************************************************************************
264 * vout_Destroy: destroy video thread output method
265 *****************************************************************************/
266 static void vout_Destroy( vout_thread_t *p_vout )
268 if( CoDestroyWindow( p_vout ) )
270 msg_Err( p_vout, "unable to destroy window" );
275 free( p_vout->p_sys->p_matrix );
276 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
278 vlc_object_release( p_vout->p_sys->p_intf );
280 free( p_vout->p_sys );
283 /*****************************************************************************
284 * vout_Manage: handle events
285 *****************************************************************************
286 * This function should be called regularly by video output thread. It manages
287 * console events. It returns a non null value on error.
288 *****************************************************************************/
289 static int vout_Manage( vout_thread_t *p_vout )
291 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
293 if( CoToggleFullscreen( p_vout ) )
298 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
301 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
303 QTScaleMatrix( p_vout );
304 SetDSequenceMatrix( p_vout->p_sys->i_seq,
305 p_vout->p_sys->p_matrix );
307 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
310 /* hide/show mouse cursor */
311 if( p_vout->p_sys->b_mouse_moved ||
312 p_vout->p_sys->i_time_mouse_last_moved )
314 vlc_bool_t b_change = 0;
316 if( !p_vout->p_sys->b_mouse_pointer_visible )
318 CGDisplayShowCursor( kCGDirectMainDisplay );
322 else if( !p_vout->p_sys->b_mouse_moved &&
323 mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
324 p_vout->p_sys->b_mouse_pointer_visible )
326 CGDisplayHideCursor( kCGDirectMainDisplay );
333 p_vout->p_sys->i_time_mouse_last_moved = 0;
334 p_vout->p_sys->b_mouse_moved = 0;
335 p_vout->p_sys->b_mouse_pointer_visible =
336 !p_vout->p_sys->b_mouse_pointer_visible;
343 /*****************************************************************************
344 * vout_Render: render previously calculated output
345 *****************************************************************************/
346 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
351 /*****************************************************************************
352 * vout_Display: displays previously rendered output
353 *****************************************************************************
354 * This function sends the currently rendered image to the display.
355 *****************************************************************************/
356 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
361 if( ( err = DecompressSequenceFrameS(
362 p_vout->p_sys->i_seq,
363 p_pic->p_sys->p_info,
364 p_pic->p_sys->i_size,
365 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
367 msg_Err( p_vout, "DecompressSequenceFrameS failed: %d", err );
371 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
375 /*****************************************************************************
376 * CoSendRequest: send request to interface thread
377 *****************************************************************************
378 * Returns 0 on success, 1 otherwise
379 *****************************************************************************/
380 static int CoSendRequest( vout_thread_t *p_vout, long i_request )
383 NSPortMessage *o_msg;
384 struct vout_req_t req;
385 struct vout_req_t *p_req = &req;
386 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
387 NSPort *recvPort = [[NSPort port] retain];
389 memset( &req, 0, sizeof(req) );
390 req.i_type = i_request;
393 req.o_lock = [[NSConditionLock alloc] initWithCondition: 0];
395 o_array = [NSArray arrayWithObject:
396 [NSData dataWithBytes: &p_req length: sizeof(void *)]];
397 o_msg = [[NSPortMessage alloc]
398 initWithSendPort: p_vout->p_sys->p_intf->p_sys->o_sendport
399 receivePort: recvPort components: o_array];
401 [o_msg sendBeforeDate: [NSDate distantPast]];
403 [req.o_lock lockWhenCondition: 1];
407 [req.o_lock release];
412 return( !req.i_result );
415 /*****************************************************************************
416 * CoCreateWindow: create new window
417 *****************************************************************************
418 * Returns 0 on success, 1 otherwise
419 *****************************************************************************/
420 static int CoCreateWindow( vout_thread_t *p_vout )
422 if( CoSendRequest( p_vout, VOUT_REQ_CREATE_WINDOW ) )
424 msg_Err( p_vout, "CoSendRequest (CREATE_WINDOW) failed" );
431 /*****************************************************************************
432 * CoDestroyWindow: destroy window
433 *****************************************************************************
434 * Returns 0 on success, 1 otherwise
435 *****************************************************************************/
436 static int CoDestroyWindow( vout_thread_t *p_vout )
438 if( !p_vout->p_sys->b_mouse_pointer_visible )
440 CGDisplayShowCursor( kCGDirectMainDisplay );
441 p_vout->p_sys->b_mouse_pointer_visible = 1;
444 if( CoSendRequest( p_vout, VOUT_REQ_DESTROY_WINDOW ) )
446 msg_Err( p_vout, "CoSendRequest (DESTROY_WINDOW) failed" );
453 /*****************************************************************************
454 * CoToggleFullscreen: toggle fullscreen
455 *****************************************************************************
456 * Returns 0 on success, 1 otherwise
457 *****************************************************************************/
458 static int CoToggleFullscreen( vout_thread_t *p_vout )
460 QTDestroySequence( p_vout );
462 if( CoDestroyWindow( p_vout ) )
464 msg_Err( p_vout, "unable to destroy window" );
468 p_vout->b_fullscreen = !p_vout->b_fullscreen;
470 if( p_vout->b_fullscreen )
479 if( CoCreateWindow( p_vout ) )
481 msg_Err( p_vout, "unable to create window" );
485 SetPort( p_vout->p_sys->p_qdport );
486 QTScaleMatrix( p_vout );
488 if( QTCreateSequence( p_vout ) )
490 msg_Err( p_vout, "unable to create sequence" );
497 /*****************************************************************************
498 * QTScaleMatrix: scale matrix
499 *****************************************************************************/
500 static void QTScaleMatrix( vout_thread_t *p_vout )
503 int i_width, i_height;
504 Fixed factor_x, factor_y;
508 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
510 i_width = s_rect.right - s_rect.left;
511 i_height = s_rect.bottom - s_rect.top;
513 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
515 int i_adj_width = i_height * p_vout->output.i_aspect /
518 factor_x = FixDiv( Long2Fix( i_adj_width ),
519 Long2Fix( p_vout->output.i_width ) );
520 factor_y = FixDiv( Long2Fix( i_height ),
521 Long2Fix( p_vout->output.i_height ) );
523 i_offset_x = (i_width - i_adj_width) / 2;
527 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
528 p_vout->output.i_aspect;
530 factor_x = FixDiv( Long2Fix( i_width ),
531 Long2Fix( p_vout->output.i_width ) );
532 factor_y = FixDiv( Long2Fix( i_adj_height ),
533 Long2Fix( p_vout->output.i_height ) );
535 i_offset_y = (i_height - i_adj_height) / 2;
538 SetIdentityMatrix( p_vout->p_sys->p_matrix );
540 ScaleMatrix( p_vout->p_sys->p_matrix,
542 Long2Fix(0), Long2Fix(0) );
544 TranslateMatrix( p_vout->p_sys->p_matrix,
545 Long2Fix(i_offset_x),
546 Long2Fix(i_offset_y) );
549 /*****************************************************************************
550 * QTCreateSequence: create a new sequence
551 *****************************************************************************
552 * Returns 0 on success, 1 otherwise
553 *****************************************************************************/
554 static int QTCreateSequence( vout_thread_t *p_vout )
557 ImageDescriptionPtr p_descr;
559 HLock( (Handle)p_vout->p_sys->h_img_descr );
560 p_descr = *p_vout->p_sys->h_img_descr;
562 p_descr->idSize = sizeof(ImageDescription);
563 p_descr->cType = p_vout->p_sys->i_codec;
564 p_descr->version = 1;
565 p_descr->revisionLevel = 0;
566 p_descr->vendor = 'appl';
567 p_descr->width = p_vout->output.i_width;
568 p_descr->height = p_vout->output.i_height;
569 p_descr->hRes = Long2Fix(72);
570 p_descr->vRes = Long2Fix(72);
571 p_descr->spatialQuality = codecLosslessQuality;
572 p_descr->frameCount = 1;
573 p_descr->clutID = -1;
574 p_descr->dataSize = 0;
577 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
579 if( ( err = DecompressSequenceBeginS(
580 &p_vout->p_sys->i_seq,
581 p_vout->p_sys->h_img_descr,
583 p_vout->p_sys->p_qdport,
585 p_vout->p_sys->p_matrix,
587 codecFlagUseImageBuffer,
588 codecLosslessQuality,
589 p_vout->p_sys->img_dc ) ) )
591 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
598 /*****************************************************************************
599 * QTDestroySequence: destroy sequence
600 *****************************************************************************/
601 static void QTDestroySequence( vout_thread_t *p_vout )
603 CDSequenceEnd( p_vout->p_sys->i_seq );
606 /*****************************************************************************
607 * QTNewPicture: allocate a picture
608 *****************************************************************************
609 * Returns 0 on success, 1 otherwise
610 *****************************************************************************/
611 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
613 int i_width = p_vout->output.i_width;
614 int i_height = p_vout->output.i_height;
616 /* We know the chroma, allocate a buffer which will be used
617 * directly by the decoder */
618 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
620 if( p_pic->p_sys == NULL )
625 switch( p_vout->output.i_chroma )
627 case VLC_FOURCC('I','4','2','0'):
629 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
630 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
632 /* Allocate the memory buffer */
633 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
634 16, i_width * i_height * 3 / 2 );
637 p_pic->Y_PIXELS = p_pic->p_data;
638 p_pic->p[Y_PLANE].i_lines = i_height;
639 p_pic->p[Y_PLANE].i_pitch = i_width;
640 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
641 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
644 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
645 p_pic->p[U_PLANE].i_lines = i_height / 2;
646 p_pic->p[U_PLANE].i_pitch = i_width / 2;
647 p_pic->p[U_PLANE].i_pixel_pitch = 1;
648 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
651 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
652 p_pic->p[V_PLANE].i_lines = i_height / 2;
653 p_pic->p[V_PLANE].i_pitch = i_width / 2;
654 p_pic->p[V_PLANE].i_pixel_pitch = 1;
655 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
657 /* We allocated 3 planes */
660 #define P p_pic->p_sys->pixmap_i420
661 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
662 - p_pic->p_sys->p_info;
663 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
664 - p_pic->p_sys->p_info;
665 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
666 - p_pic->p_sys->p_info;
668 P.componentInfoY.rowBytes = i_width;
669 P.componentInfoCb.rowBytes = i_width / 2;
670 P.componentInfoCr.rowBytes = i_width / 2;
676 /* Unknown chroma, tell the guy to get lost */
677 free( p_pic->p_sys );
678 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
679 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
687 /*****************************************************************************
688 * QTFreePicture: destroy a picture allocated with QTNewPicture
689 *****************************************************************************/
690 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
692 switch( p_vout->output.i_chroma )
694 case VLC_FOURCC('I','4','2','0'):
695 free( p_pic->p_data_orig );
699 free( p_pic->p_sys );
702 /*****************************************************************************
703 * VLCWindow implementation
704 *****************************************************************************/
705 @implementation VLCWindow
707 - (void)setVout:(vout_thread_t *)_p_vout
712 - (void)toggleFullscreen
714 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
719 return( p_vout->b_fullscreen );
722 - (BOOL)canBecomeKeyWindow
727 - (void)keyDown:(NSEvent *)o_event
731 if( [[o_event characters] length] )
733 key = [[o_event characters] characterAtIndex: 0];
738 case (unichar)0xf700: /* up-arrow */
740 aout_thread_t * p_aout = vlc_object_find( p_vout, VLC_OBJECT_AOUT,
744 if( p_aout->i_volume + VOLUME_STEP <= VOLUME_MAX )
746 p_aout->i_volume += VOLUME_STEP;
749 vlc_object_release( p_aout );
754 case (unichar)0xf701: /* down-arrow */
756 aout_thread_t * p_aout = vlc_object_find( p_vout, VLC_OBJECT_AOUT,
760 if( p_aout->i_volume - VOLUME_STEP >= VOLUME_MIN )
762 p_aout->i_volume -= VOLUME_STEP;
765 vlc_object_release( p_aout );
771 [self toggleFullscreen];
774 case (unichar)0x1b: /* escape */
775 if( [self isFullscreen] )
777 [self toggleFullscreen];
782 p_vout->p_vlc->b_die = VLC_TRUE;
786 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
790 [super keyDown: o_event];
797 /*****************************************************************************
798 * VLCView implementation
799 *****************************************************************************/
800 @implementation VLCView
802 - (void)setVout:(vout_thread_t *)_p_vout
807 - (void)drawRect:(NSRect)rect
809 [[NSColor blackColor] set];
811 [super drawRect: rect];
813 p_vout->i_changes |= VOUT_SIZE_CHANGE;