1 /*****************************************************************************
2 * vout_macosx.c: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
6 * Authors: Colin Delacroix <colin@zoy.org>
7 * Florian G. Pflug <fgp@phlo.org>
8 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
31 #include <mach/mach_init.h>
32 #include <mach/task_policy.h>
33 #include <mach/thread_act.h>
34 #include <mach/thread_policy.h>
35 #include <sys/sysctl.h>
37 #include <videolan/vlc.h>
40 #include "video_output.h"
42 #include "interface.h"
46 #define QT_MAX_DIRECTBUFFERS 10
48 typedef struct picture_sys_s
53 /* When using I420 output */
54 PlanarPixmapInfoYUV420 pixmap_i420;
58 /*****************************************************************************
60 *****************************************************************************/
61 static int vout_Create ( struct vout_thread_s * );
62 static int vout_Init ( struct vout_thread_s * );
63 static void vout_End ( struct vout_thread_s * );
64 static void vout_Destroy ( struct vout_thread_s * );
65 static int vout_Manage ( struct vout_thread_s * );
66 static void vout_Render ( struct vout_thread_s *, struct picture_s * );
67 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
69 static int CoSendRequest ( struct vout_thread_s *, long i_request );
70 static int CoCreateWindow ( struct vout_thread_s * );
71 static int CoDestroyWindow ( struct vout_thread_s * );
72 static int CoToggleFullscreen ( struct vout_thread_s * );
74 static void QTScaleMatrix ( struct vout_thread_s * );
75 static int QTCreateSequence ( struct vout_thread_s * );
76 static void QTDestroySequence ( struct vout_thread_s * );
77 static int QTNewPicture ( struct vout_thread_s *, struct picture_s * );
78 static void QTFreePicture ( struct vout_thread_s *, struct picture_s * );
80 /*****************************************************************************
81 * Functions exported as capabilities. They are declared as static so that
82 * we don't pollute the namespace too much.
83 *****************************************************************************/
84 void _M( vout_getfunctions )( function_list_t * p_function_list )
86 p_function_list->functions.vout.pf_create = vout_Create;
87 p_function_list->functions.vout.pf_init = vout_Init;
88 p_function_list->functions.vout.pf_end = vout_End;
89 p_function_list->functions.vout.pf_destroy = vout_Destroy;
90 p_function_list->functions.vout.pf_manage = vout_Manage;
91 p_function_list->functions.vout.pf_render = vout_Render;
92 p_function_list->functions.vout.pf_display = vout_Display;
95 /*****************************************************************************
96 * vout_Create: allocates MacOS X video thread output method
97 *****************************************************************************
98 * This function allocates and initializes a MacOS X vout method.
99 *****************************************************************************/
100 static int vout_Create( vout_thread_t *p_vout )
104 if( !p_main->p_intf || !p_main->p_intf->p_module ||
105 strcmp( p_main->p_intf->p_module->psz_name, MODULE_STRING ) != 0 )
107 intf_ErrMsg( "vout error: MacOS X interface module required" );
111 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
112 if( p_vout->p_sys == NULL )
114 intf_ErrMsg( "vout error: %s", strerror( ENOMEM ) );
118 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
120 p_vout->p_sys->h_img_descr =
121 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
122 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
124 p_vout->p_sys->b_mouse_pointer_visible = 1;
126 /* set window size */
127 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
128 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
130 if( ( err = EnterMovies() ) != noErr )
132 intf_ErrMsg( "vout error: EnterMovies failed: %d", err );
133 free( p_vout->p_sys->p_matrix );
134 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
135 free( p_vout->p_sys );
139 if( vout_ChromaCmp( p_vout->render.i_chroma, FOURCC_I420 ) )
141 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
142 nil, &p_vout->p_sys->img_dc );
143 if( err == noErr && p_vout->p_sys->img_dc != 0 )
145 p_vout->output.i_chroma = FOURCC_I420;
146 p_vout->p_sys->i_codec = kYUV420CodecType;
150 intf_ErrMsg( "vout error: failed to find an appropriate codec" );
155 intf_ErrMsg( "vout error: chroma 0x%08x not supported",
156 p_vout->render.i_chroma );
159 if( p_vout->p_sys->img_dc == 0 )
161 free( p_vout->p_sys->p_matrix );
162 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
163 free( p_vout->p_sys );
167 if( CoCreateWindow( p_vout ) )
169 intf_ErrMsg( "vout error: unable to create window" );
170 free( p_vout->p_sys->p_matrix );
171 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
172 free( p_vout->p_sys );
179 /*****************************************************************************
180 * vout_Init: initialize video thread output method
181 *****************************************************************************/
182 static int vout_Init( vout_thread_t *p_vout )
187 I_OUTPUTPICTURES = 0;
189 /* Initialize the output structure; we already found a codec,
190 * and the corresponding chroma we will be using. Since we can
191 * arbitrary scale, stick to the coordinates and aspect. */
192 p_vout->output.i_width = p_vout->render.i_width;
193 p_vout->output.i_height = p_vout->render.i_height;
194 p_vout->output.i_aspect = p_vout->render.i_aspect;
196 SetPort( p_vout->p_sys->p_qdport );
197 QTScaleMatrix( p_vout );
199 if( QTCreateSequence( p_vout ) )
201 intf_ErrMsg( "vout error: unable to create sequence" );
205 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
206 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
210 /* Find an empty picture slot */
211 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
213 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
215 p_pic = p_vout->p_picture + i_index;
220 /* Allocate the picture */
221 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
226 p_pic->i_status = DESTROYED_PICTURE;
227 p_pic->i_type = DIRECT_PICTURE;
229 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
237 /*****************************************************************************
238 * vout_End: terminate video thread output method
239 *****************************************************************************/
240 static void vout_End( vout_thread_t *p_vout )
244 QTDestroySequence( p_vout );
246 /* Free the direct buffers we allocated */
247 for( i_index = I_OUTPUTPICTURES; i_index; )
250 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
254 /*****************************************************************************
255 * vout_Destroy: destroy video thread output method
256 *****************************************************************************/
257 static void vout_Destroy( vout_thread_t *p_vout )
259 if( CoDestroyWindow( p_vout ) )
261 intf_ErrMsg( "vout error: unable to destroy window" );
266 free( p_vout->p_sys->p_matrix );
267 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
268 free( p_vout->p_sys );
271 /*****************************************************************************
272 * vout_Manage: handle events
273 *****************************************************************************
274 * This function should be called regularly by video output thread. It manages
275 * console events. It returns a non null value on error.
276 *****************************************************************************/
277 static int vout_Manage( vout_thread_t *p_vout )
279 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
281 if( CoToggleFullscreen( p_vout ) )
286 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
289 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
291 QTScaleMatrix( p_vout );
292 SetDSequenceMatrix( p_vout->p_sys->i_seq,
293 p_vout->p_sys->p_matrix );
295 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
298 /* hide/show mouse cursor */
299 if( p_vout->p_sys->b_mouse_moved ||
300 p_vout->p_sys->i_time_mouse_last_moved )
302 boolean_t b_change = 0;
304 if( !p_vout->p_sys->b_mouse_pointer_visible )
306 CGDisplayShowCursor( kCGDirectMainDisplay );
310 else if( !p_vout->p_sys->b_mouse_moved &&
311 mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
312 p_vout->p_sys->b_mouse_pointer_visible )
314 CGDisplayHideCursor( kCGDirectMainDisplay );
321 p_vout->p_sys->i_time_mouse_last_moved = 0;
322 p_vout->p_sys->b_mouse_moved = 0;
323 p_vout->p_sys->b_mouse_pointer_visible =
324 !p_vout->p_sys->b_mouse_pointer_visible;
331 /*****************************************************************************
332 * vout_Render: render previously calculated output
333 *****************************************************************************/
334 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
339 /*****************************************************************************
340 * vout_Display: displays previously rendered output
341 *****************************************************************************
342 * This function sends the currently rendered image to the display.
343 *****************************************************************************/
344 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
349 if( ( err = DecompressSequenceFrameS(
350 p_vout->p_sys->i_seq,
351 p_pic->p_sys->p_info,
352 p_pic->p_sys->i_size,
353 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
355 intf_ErrMsg( "DecompressSequenceFrameS failed: %d", err );
359 /*****************************************************************************
360 * CoSendRequest: send request to interface thread
361 *****************************************************************************
362 * Returns 0 on success, 1 otherwise
363 *****************************************************************************/
364 static int CoSendRequest( vout_thread_t *p_vout, long i_request )
367 NSPortMessage *o_msg;
368 struct vout_req_s req;
369 struct vout_req_s *p_req = &req;
370 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
371 NSPort *recvPort = [[NSPort port] retain];
373 memset( &req, 0, sizeof(req) );
374 req.i_type = i_request;
377 req.o_lock = [[NSConditionLock alloc] initWithCondition: 0];
379 o_array = [NSArray arrayWithObject:
380 [NSData dataWithBytes: &p_req length: sizeof(void *)]];
381 o_msg = [[NSPortMessage alloc]
382 initWithSendPort: p_main->p_intf->p_sys->o_port
383 receivePort: recvPort
384 components: o_array];
386 [o_msg sendBeforeDate: [NSDate distantPast]];
387 [req.o_lock lockWhenCondition: 1];
391 [req.o_lock release];
396 return( !req.i_result );
399 /*****************************************************************************
400 * CoCreateWindow: create new window
401 *****************************************************************************
402 * Returns 0 on success, 1 otherwise
403 *****************************************************************************/
404 static int CoCreateWindow( vout_thread_t *p_vout )
406 if( CoSendRequest( p_vout, VOUT_REQ_CREATE_WINDOW ) )
408 intf_ErrMsg( "CoSendRequest (CREATE_WINDOW) failed" );
415 /*****************************************************************************
416 * CoDestroyWindow: destroy window
417 *****************************************************************************
418 * Returns 0 on success, 1 otherwise
419 *****************************************************************************/
420 static int CoDestroyWindow( vout_thread_t *p_vout )
422 if( !p_vout->p_sys->b_mouse_pointer_visible )
424 CGDisplayShowCursor( kCGDirectMainDisplay );
425 p_vout->p_sys->b_mouse_pointer_visible = 1;
428 if( CoSendRequest( p_vout, VOUT_REQ_DESTROY_WINDOW ) )
430 intf_ErrMsg( "CoSendRequest (DESTROY_WINDOW) failed" );
437 /*****************************************************************************
438 * CoToggleFullscreen: toggle fullscreen
439 *****************************************************************************
440 * Returns 0 on success, 1 otherwise
441 *****************************************************************************/
442 static int CoToggleFullscreen( vout_thread_t *p_vout )
444 QTDestroySequence( p_vout );
446 if( CoDestroyWindow( p_vout ) )
448 intf_ErrMsg( "vout error: unable to destroy window" );
452 p_vout->b_fullscreen = !p_vout->b_fullscreen;
454 if( CoCreateWindow( p_vout ) )
456 intf_ErrMsg( "vout error: unable to create window" );
460 SetPort( p_vout->p_sys->p_qdport );
461 QTScaleMatrix( p_vout );
463 if( QTCreateSequence( p_vout ) )
465 intf_ErrMsg( "vout error: unable to create sequence" );
472 /*****************************************************************************
473 * QTScaleMatrix: scale matrix
474 *****************************************************************************/
475 static void QTScaleMatrix( vout_thread_t *p_vout )
478 int i_width, i_height;
479 Fixed factor_x, factor_y;
483 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
485 i_width = s_rect.right - s_rect.left;
486 i_height = s_rect.bottom - s_rect.top;
488 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
490 int i_adj_width = i_height * p_vout->output.i_aspect /
493 factor_x = FixDiv( Long2Fix( i_adj_width ),
494 Long2Fix( p_vout->output.i_width ) );
495 factor_y = FixDiv( Long2Fix( i_height ),
496 Long2Fix( p_vout->output.i_height ) );
498 i_offset_x = (i_width - i_adj_width) / 2;
502 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
503 p_vout->output.i_aspect;
505 factor_x = FixDiv( Long2Fix( i_width ),
506 Long2Fix( p_vout->output.i_width ) );
507 factor_y = FixDiv( Long2Fix( i_adj_height ),
508 Long2Fix( p_vout->output.i_height ) );
510 i_offset_y = (i_height - i_adj_height) / 2;
513 SetIdentityMatrix( p_vout->p_sys->p_matrix );
515 ScaleMatrix( p_vout->p_sys->p_matrix,
517 Long2Fix(0), Long2Fix(0) );
519 TranslateMatrix( p_vout->p_sys->p_matrix,
520 Long2Fix(i_offset_x),
521 Long2Fix(i_offset_y) );
524 /*****************************************************************************
525 * QTCreateSequence: create a new sequence
526 *****************************************************************************
527 * Returns 0 on success, 1 otherwise
528 *****************************************************************************/
529 static int QTCreateSequence( vout_thread_t *p_vout )
532 ImageDescriptionPtr p_descr;
534 HLock( (Handle)p_vout->p_sys->h_img_descr );
535 p_descr = *p_vout->p_sys->h_img_descr;
537 p_descr->idSize = sizeof(ImageDescription);
538 p_descr->cType = p_vout->p_sys->i_codec;
539 p_descr->version = 1;
540 p_descr->revisionLevel = 0;
541 p_descr->vendor = 'appl';
542 p_descr->width = p_vout->output.i_width;
543 p_descr->height = p_vout->output.i_height;
544 p_descr->hRes = Long2Fix(72);
545 p_descr->vRes = Long2Fix(72);
546 p_descr->spatialQuality = codecLosslessQuality;
547 p_descr->frameCount = 1;
548 p_descr->clutID = -1;
549 p_descr->dataSize = 0;
552 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
554 if( ( err = DecompressSequenceBeginS(
555 &p_vout->p_sys->i_seq,
556 p_vout->p_sys->h_img_descr,
558 p_vout->p_sys->p_qdport,
560 p_vout->p_sys->p_matrix,
562 codecFlagUseImageBuffer,
563 codecLosslessQuality,
564 p_vout->p_sys->img_dc ) ) )
566 intf_ErrMsg( "DecompressSequenceBeginS failed: %d", err );
573 /*****************************************************************************
574 * QTDestroySequence: destroy sequence
575 *****************************************************************************/
576 static void QTDestroySequence( vout_thread_t *p_vout )
578 CDSequenceEnd( p_vout->p_sys->i_seq );
581 /*****************************************************************************
582 * QTNewPicture: allocate a picture
583 *****************************************************************************
584 * Returns 0 on success, 1 otherwise
585 *****************************************************************************/
586 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
588 int i_width = p_vout->output.i_width;
589 int i_height = p_vout->output.i_height;
591 /* We know the chroma, allocate a buffer which will be used
592 * directly by the decoder */
593 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
595 if( p_pic->p_sys == NULL )
600 switch( p_vout->output.i_chroma )
604 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
605 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
607 /* Allocate the memory buffer */
608 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
609 16, i_width * i_height * 3 / 2 );
612 p_pic->Y_PIXELS = p_pic->p_data;
613 p_pic->p[Y_PLANE].i_lines = i_height;
614 p_pic->p[Y_PLANE].i_pitch = i_width;
615 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
616 p_pic->p[Y_PLANE].b_margin = 0;
619 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
620 p_pic->p[U_PLANE].i_lines = i_height / 2;
621 p_pic->p[U_PLANE].i_pitch = i_width / 2;
622 p_pic->p[U_PLANE].i_pixel_bytes = 1;
623 p_pic->p[U_PLANE].b_margin = 0;
626 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
627 p_pic->p[V_PLANE].i_lines = i_height / 2;
628 p_pic->p[V_PLANE].i_pitch = i_width / 2;
629 p_pic->p[V_PLANE].i_pixel_bytes = 1;
630 p_pic->p[V_PLANE].b_margin = 0;
632 /* We allocated 3 planes */
635 #define P p_pic->p_sys->pixmap_i420
636 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
637 - p_pic->p_sys->p_info;
638 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
639 - p_pic->p_sys->p_info;
640 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
641 - p_pic->p_sys->p_info;
643 P.componentInfoY.rowBytes = i_width;
644 P.componentInfoCb.rowBytes = i_width / 2;
645 P.componentInfoCr.rowBytes = i_width / 2;
651 /* Unknown chroma, tell the guy to get lost */
652 free( p_pic->p_sys );
653 intf_ErrMsg( "vout error: never heard of chroma 0x%.8x (%4.4s)",
654 p_vout->output.i_chroma,
655 (char*)&p_vout->output.i_chroma );
663 /*****************************************************************************
664 * QTFreePicture: destroy a picture allocated with QTNewPicture
665 *****************************************************************************/
666 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
668 switch( p_vout->output.i_chroma )
671 free( p_pic->p_data_orig );
675 free( p_pic->p_sys );