1 /*****************************************************************************
2 * vout_macosx.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: vout_macosx.m,v 1.12 2002/07/16 20:41:48 jlj 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, FOURCC_I420 ) )
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 = FOURCC_I420;
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 /*****************************************************************************
372 * CoSendRequest: send request to interface thread
373 *****************************************************************************
374 * Returns 0 on success, 1 otherwise
375 *****************************************************************************/
376 static int CoSendRequest( vout_thread_t *p_vout, long i_request )
379 NSPortMessage *o_msg;
380 struct vout_req_s req;
381 struct vout_req_s *p_req = &req;
382 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
383 NSPort *recvPort = [[NSPort port] retain];
385 memset( &req, 0, sizeof(req) );
386 req.i_type = i_request;
389 req.o_lock = [[NSConditionLock alloc] initWithCondition: 0];
391 o_array = [NSArray arrayWithObject:
392 [NSData dataWithBytes: &p_req length: sizeof(void *)]];
393 o_msg = [[NSPortMessage alloc]
394 initWithSendPort: p_vout->p_sys->p_intf->p_sys->o_sendport
395 receivePort: recvPort components: o_array];
397 [o_msg sendBeforeDate: [NSDate distantPast]];
399 [req.o_lock lockWhenCondition: 1];
403 [req.o_lock release];
408 return( !req.i_result );
411 /*****************************************************************************
412 * CoCreateWindow: create new window
413 *****************************************************************************
414 * Returns 0 on success, 1 otherwise
415 *****************************************************************************/
416 static int CoCreateWindow( vout_thread_t *p_vout )
418 if( CoSendRequest( p_vout, VOUT_REQ_CREATE_WINDOW ) )
420 msg_Err( p_vout, "CoSendRequest (CREATE_WINDOW) failed" );
427 /*****************************************************************************
428 * CoDestroyWindow: destroy window
429 *****************************************************************************
430 * Returns 0 on success, 1 otherwise
431 *****************************************************************************/
432 static int CoDestroyWindow( vout_thread_t *p_vout )
434 if( !p_vout->p_sys->b_mouse_pointer_visible )
436 CGDisplayShowCursor( kCGDirectMainDisplay );
437 p_vout->p_sys->b_mouse_pointer_visible = 1;
440 if( CoSendRequest( p_vout, VOUT_REQ_DESTROY_WINDOW ) )
442 msg_Err( p_vout, "CoSendRequest (DESTROY_WINDOW) failed" );
449 /*****************************************************************************
450 * CoToggleFullscreen: toggle fullscreen
451 *****************************************************************************
452 * Returns 0 on success, 1 otherwise
453 *****************************************************************************/
454 static int CoToggleFullscreen( vout_thread_t *p_vout )
456 QTDestroySequence( p_vout );
458 if( CoDestroyWindow( p_vout ) )
460 msg_Err( p_vout, "unable to destroy window" );
464 p_vout->b_fullscreen = !p_vout->b_fullscreen;
466 if( p_vout->b_fullscreen )
475 if( CoCreateWindow( p_vout ) )
477 msg_Err( p_vout, "unable to create window" );
481 SetPort( p_vout->p_sys->p_qdport );
482 QTScaleMatrix( p_vout );
484 if( QTCreateSequence( p_vout ) )
486 msg_Err( p_vout, "unable to create sequence" );
493 /*****************************************************************************
494 * QTScaleMatrix: scale matrix
495 *****************************************************************************/
496 static void QTScaleMatrix( vout_thread_t *p_vout )
499 int i_width, i_height;
500 Fixed factor_x, factor_y;
504 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
506 i_width = s_rect.right - s_rect.left;
507 i_height = s_rect.bottom - s_rect.top;
509 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
511 int i_adj_width = i_height * p_vout->output.i_aspect /
514 factor_x = FixDiv( Long2Fix( i_adj_width ),
515 Long2Fix( p_vout->output.i_width ) );
516 factor_y = FixDiv( Long2Fix( i_height ),
517 Long2Fix( p_vout->output.i_height ) );
519 i_offset_x = (i_width - i_adj_width) / 2;
523 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
524 p_vout->output.i_aspect;
526 factor_x = FixDiv( Long2Fix( i_width ),
527 Long2Fix( p_vout->output.i_width ) );
528 factor_y = FixDiv( Long2Fix( i_adj_height ),
529 Long2Fix( p_vout->output.i_height ) );
531 i_offset_y = (i_height - i_adj_height) / 2;
534 SetIdentityMatrix( p_vout->p_sys->p_matrix );
536 ScaleMatrix( p_vout->p_sys->p_matrix,
538 Long2Fix(0), Long2Fix(0) );
540 TranslateMatrix( p_vout->p_sys->p_matrix,
541 Long2Fix(i_offset_x),
542 Long2Fix(i_offset_y) );
545 /*****************************************************************************
546 * QTCreateSequence: create a new sequence
547 *****************************************************************************
548 * Returns 0 on success, 1 otherwise
549 *****************************************************************************/
550 static int QTCreateSequence( vout_thread_t *p_vout )
553 ImageDescriptionPtr p_descr;
555 HLock( (Handle)p_vout->p_sys->h_img_descr );
556 p_descr = *p_vout->p_sys->h_img_descr;
558 p_descr->idSize = sizeof(ImageDescription);
559 p_descr->cType = p_vout->p_sys->i_codec;
560 p_descr->version = 1;
561 p_descr->revisionLevel = 0;
562 p_descr->vendor = 'appl';
563 p_descr->width = p_vout->output.i_width;
564 p_descr->height = p_vout->output.i_height;
565 p_descr->hRes = Long2Fix(72);
566 p_descr->vRes = Long2Fix(72);
567 p_descr->spatialQuality = codecLosslessQuality;
568 p_descr->frameCount = 1;
569 p_descr->clutID = -1;
570 p_descr->dataSize = 0;
573 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
575 if( ( err = DecompressSequenceBeginS(
576 &p_vout->p_sys->i_seq,
577 p_vout->p_sys->h_img_descr,
579 p_vout->p_sys->p_qdport,
581 p_vout->p_sys->p_matrix,
583 codecFlagUseImageBuffer,
584 codecLosslessQuality,
585 p_vout->p_sys->img_dc ) ) )
587 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
594 /*****************************************************************************
595 * QTDestroySequence: destroy sequence
596 *****************************************************************************/
597 static void QTDestroySequence( vout_thread_t *p_vout )
599 CDSequenceEnd( p_vout->p_sys->i_seq );
602 /*****************************************************************************
603 * QTNewPicture: allocate a picture
604 *****************************************************************************
605 * Returns 0 on success, 1 otherwise
606 *****************************************************************************/
607 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
609 int i_width = p_vout->output.i_width;
610 int i_height = p_vout->output.i_height;
612 /* We know the chroma, allocate a buffer which will be used
613 * directly by the decoder */
614 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
616 if( p_pic->p_sys == NULL )
621 switch( p_vout->output.i_chroma )
625 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
626 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
628 /* Allocate the memory buffer */
629 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
630 16, i_width * i_height * 3 / 2 );
633 p_pic->Y_PIXELS = p_pic->p_data;
634 p_pic->p[Y_PLANE].i_lines = i_height;
635 p_pic->p[Y_PLANE].i_pitch = i_width;
636 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
637 p_pic->p[Y_PLANE].b_margin = 0;
640 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
641 p_pic->p[U_PLANE].i_lines = i_height / 2;
642 p_pic->p[U_PLANE].i_pitch = i_width / 2;
643 p_pic->p[U_PLANE].i_pixel_bytes = 1;
644 p_pic->p[U_PLANE].b_margin = 0;
647 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
648 p_pic->p[V_PLANE].i_lines = i_height / 2;
649 p_pic->p[V_PLANE].i_pitch = i_width / 2;
650 p_pic->p[V_PLANE].i_pixel_bytes = 1;
651 p_pic->p[V_PLANE].b_margin = 0;
653 /* We allocated 3 planes */
656 #define P p_pic->p_sys->pixmap_i420
657 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
658 - p_pic->p_sys->p_info;
659 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
660 - p_pic->p_sys->p_info;
661 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
662 - p_pic->p_sys->p_info;
664 P.componentInfoY.rowBytes = i_width;
665 P.componentInfoCb.rowBytes = i_width / 2;
666 P.componentInfoCr.rowBytes = i_width / 2;
672 /* Unknown chroma, tell the guy to get lost */
673 free( p_pic->p_sys );
674 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
675 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
683 /*****************************************************************************
684 * QTFreePicture: destroy a picture allocated with QTNewPicture
685 *****************************************************************************/
686 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
688 switch( p_vout->output.i_chroma )
691 free( p_pic->p_data_orig );
695 free( p_pic->p_sys );
698 /*****************************************************************************
699 * VLCWindow implementation
700 *****************************************************************************/
701 @implementation VLCWindow
703 - (void)setVout:(vout_thread_t *)_p_vout
708 - (void)toggleFullscreen
710 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
715 return( p_vout->b_fullscreen );
718 - (BOOL)canBecomeKeyWindow
723 - (void)keyDown:(NSEvent *)o_event
727 if( [[o_event characters] length] )
729 key = [[o_event characters] characterAtIndex: 0];
734 case (unichar)0xf700: /* up-arrow */
736 aout_thread_t * p_aout = vlc_object_find( p_vout, VLC_OBJECT_AOUT,
740 if( p_aout->i_volume + VOLUME_STEP <= VOLUME_MAX )
742 p_aout->i_volume += VOLUME_STEP;
745 vlc_object_release( p_aout );
750 case (unichar)0xf701: /* down-arrow */
752 aout_thread_t * p_aout = vlc_object_find( p_vout, VLC_OBJECT_AOUT,
756 if( p_aout->i_volume - VOLUME_STEP >= VOLUME_MIN )
758 p_aout->i_volume -= VOLUME_STEP;
761 vlc_object_release( p_aout );
767 [self toggleFullscreen];
770 case (unichar)0x1b: /* escape */
771 if( [self isFullscreen] )
773 [self toggleFullscreen];
778 p_vout->p_vlc->b_die = VLC_TRUE;
782 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
786 [super keyDown: o_event];
793 /*****************************************************************************
794 * VLCView implementation
795 *****************************************************************************/
796 @implementation VLCView
798 - (void)setVout:(vout_thread_t *)_p_vout
803 - (void)drawRect:(NSRect)rect
805 [[NSColor blackColor] set];
807 [super drawRect: rect];
809 p_vout->i_changes |= VOUT_SIZE_CHANGE;