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 struct thread_time_constraint_policy ttcpolicy;
193 I_OUTPUTPICTURES = 0;
195 /* Initialize the output structure; we already found a codec,
196 * and the corresponding chroma we will be using. Since we can
197 * arbitrary scale, stick to the coordinates and aspect. */
198 p_vout->output.i_width = p_vout->render.i_width;
199 p_vout->output.i_height = p_vout->render.i_height;
200 p_vout->output.i_aspect = p_vout->render.i_aspect;
202 SetPort( p_vout->p_sys->p_qdport );
203 QTScaleMatrix( p_vout );
205 if( QTCreateSequence( p_vout ) )
207 intf_ErrMsg( "vout error: unable to create sequence" );
211 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
212 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
216 /* Find an empty picture slot */
217 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
219 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
221 p_pic = p_vout->p_picture + i_index;
226 /* Allocate the picture */
227 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
232 p_pic->i_status = DESTROYED_PICTURE;
233 p_pic->i_type = DIRECT_PICTURE;
235 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
240 /* Go to time-constrained thread policy */
244 mib[1] = HW_BUS_FREQ;
247 if( sysctl(mib, miblen, &i_busspeed, &len, NULL, 0) == -1 )
249 intf_ErrMsg("vout error: couldn't go to time-constrained policy (bus speed)");
253 /* This is in AbsoluteTime units, which are equal to
254 * 1/4 the bus speed on most machines. */
256 /* hard-coded numbers are approximations for 100 MHz bus speed.
257 * assume that app deals in frame-sized chunks, e.g. 30 per second.
258 * ttcpolicy.period = 833333; */
259 ttcpolicy.period = i_busspeed / 120;
260 /* ttcpolicy.computation = 60000; */
261 ttcpolicy.computation = i_busspeed / 1440;
262 /* ttcpolicy.constraint = 120000; */
263 ttcpolicy.constraint = i_busspeed / 720;
264 ttcpolicy.preemptible = 1;
266 if (thread_policy_set(mach_thread_self(),
267 THREAD_TIME_CONSTRAINT_POLICY, (int *)&ttcpolicy,
268 THREAD_TIME_CONSTRAINT_POLICY_COUNT) != KERN_SUCCESS)
270 intf_ErrMsg("vout error: couldn't go to time-constrained policy (thread_policy_set)");
277 /*****************************************************************************
278 * vout_End: terminate video thread output method
279 *****************************************************************************/
280 static void vout_End( vout_thread_t *p_vout )
284 QTDestroySequence( p_vout );
286 /* Free the direct buffers we allocated */
287 for( i_index = I_OUTPUTPICTURES; i_index; )
290 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
294 /*****************************************************************************
295 * vout_Destroy: destroy video thread output method
296 *****************************************************************************/
297 static void vout_Destroy( vout_thread_t *p_vout )
299 if( CoDestroyWindow( p_vout ) )
301 intf_ErrMsg( "vout error: unable to destroy window" );
306 free( p_vout->p_sys->p_matrix );
307 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
308 free( p_vout->p_sys );
311 /*****************************************************************************
312 * vout_Manage: handle events
313 *****************************************************************************
314 * This function should be called regularly by video output thread. It manages
315 * console events. It returns a non null value on error.
316 *****************************************************************************/
317 static int vout_Manage( vout_thread_t *p_vout )
319 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
321 QTScaleMatrix( p_vout );
322 SetDSequenceMatrix( p_vout->p_sys->i_seq,
323 p_vout->p_sys->p_matrix );
325 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
328 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
330 if( CoToggleFullscreen( p_vout ) )
335 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
338 /* hide/show mouse cursor */
339 if( p_vout->p_sys->b_mouse_moved ||
340 p_vout->p_sys->i_time_mouse_last_moved )
342 boolean_t b_change = 0;
344 if( !p_vout->p_sys->b_mouse_pointer_visible )
346 CGDisplayShowCursor( kCGDirectMainDisplay );
349 else if( !p_vout->p_sys->b_mouse_moved &&
350 mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
351 p_vout->p_sys->b_mouse_pointer_visible )
353 CGDisplayHideCursor( kCGDirectMainDisplay );
359 p_vout->p_sys->i_time_mouse_last_moved = 0;
360 p_vout->p_sys->b_mouse_moved = 0;
361 p_vout->p_sys->b_mouse_pointer_visible =
362 !p_vout->p_sys->b_mouse_pointer_visible;
369 /*****************************************************************************
370 * vout_Render: render previously calculated output
371 *****************************************************************************/
372 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
377 /*****************************************************************************
378 * vout_Display: displays previously rendered output
379 *****************************************************************************
380 * This function sends the currently rendered image to the display.
381 *****************************************************************************/
382 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
387 if( ( err = DecompressSequenceFrameS(
388 p_vout->p_sys->i_seq,
389 p_pic->p_sys->p_info,
390 p_pic->p_sys->i_size,
391 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
393 intf_ErrMsg( "DecompressSequenceFrameS failed: %d", err );
397 /*****************************************************************************
398 * CoSendRequest: send request to interface thread
399 *****************************************************************************
400 * Returns 0 on success, 1 otherwise
401 *****************************************************************************/
402 static int CoSendRequest( vout_thread_t *p_vout, long i_request )
405 NSPortMessage *o_msg;
406 struct vout_req_s req;
407 struct vout_req_s *p_req = &req;
408 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
409 NSPort *recvPort = [[NSPort port] retain];
411 memset( &req, 0, sizeof(req) );
412 req.i_type = i_request;
415 req.o_lock = [[NSConditionLock alloc] initWithCondition: 0];
417 o_array = [NSArray arrayWithObject:
418 [NSData dataWithBytes: &p_req length: sizeof(void *)]];
419 o_msg = [[NSPortMessage alloc]
420 initWithSendPort: p_main->p_intf->p_sys->o_port
421 receivePort: recvPort
422 components: o_array];
424 [o_msg sendBeforeDate: [NSDate distantPast]];
425 [req.o_lock lockWhenCondition: 1];
429 [req.o_lock release];
434 return( !req.i_result );
437 /*****************************************************************************
438 * CoCreateWindow: create new window
439 *****************************************************************************
440 * Returns 0 on success, 1 otherwise
441 *****************************************************************************/
442 static int CoCreateWindow( vout_thread_t *p_vout )
444 if( CoSendRequest( p_vout, VOUT_REQ_CREATE_WINDOW ) )
446 intf_ErrMsg( "CoSendRequest (CREATE_WINDOW) failed" );
453 /*****************************************************************************
454 * CoDestroyWindow: destroy window
455 *****************************************************************************
456 * Returns 0 on success, 1 otherwise
457 *****************************************************************************/
458 static int CoDestroyWindow( vout_thread_t *p_vout )
460 if( !p_vout->p_sys->b_mouse_pointer_visible )
462 CGDisplayShowCursor( kCGDirectMainDisplay );
463 p_vout->p_sys->b_mouse_pointer_visible = 1;
466 if( CoSendRequest( p_vout, VOUT_REQ_DESTROY_WINDOW ) )
468 intf_ErrMsg( "CoSendRequest (DESTROY_WINDOW) failed" );
475 /*****************************************************************************
476 * CoToggleFullscreen: toggle fullscreen
477 *****************************************************************************
478 * Returns 0 on success, 1 otherwise
479 *****************************************************************************/
480 static int CoToggleFullscreen( vout_thread_t *p_vout )
482 QTDestroySequence( p_vout );
484 if( CoDestroyWindow( p_vout ) )
486 intf_ErrMsg( "vout error: unable to destroy window" );
490 p_vout->b_fullscreen = !p_vout->b_fullscreen;
492 if( CoCreateWindow( p_vout ) )
494 intf_ErrMsg( "vout error: unable to create window" );
498 SetPort( p_vout->p_sys->p_qdport );
499 QTScaleMatrix( p_vout );
501 if( QTCreateSequence( p_vout ) )
503 intf_ErrMsg( "vout error: unable to create sequence" );
510 /*****************************************************************************
511 * QTScaleMatrix: scale matrix
512 *****************************************************************************/
513 static void QTScaleMatrix( vout_thread_t *p_vout )
519 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
521 factor_x = FixDiv( Long2Fix( s_rect.right - s_rect.left ),
522 Long2Fix( p_vout->output.i_width ) );
523 factor_y = FixDiv( Long2Fix( s_rect.bottom - s_rect.top ),
524 Long2Fix( p_vout->output.i_height ) );
526 SetIdentityMatrix( p_vout->p_sys->p_matrix );
527 ScaleMatrix( p_vout->p_sys->p_matrix,
529 Long2Fix(0), Long2Fix(0) );
532 /*****************************************************************************
533 * QTCreateSequence: create a new sequence
534 *****************************************************************************
535 * Returns 0 on success, 1 otherwise
536 *****************************************************************************/
537 static int QTCreateSequence( vout_thread_t *p_vout )
540 ImageDescriptionPtr p_descr;
542 HLock( (Handle)p_vout->p_sys->h_img_descr );
543 p_descr = *p_vout->p_sys->h_img_descr;
545 p_descr->idSize = sizeof(ImageDescription);
546 p_descr->cType = p_vout->p_sys->i_codec;
547 p_descr->version = 1;
548 p_descr->revisionLevel = 0;
549 p_descr->vendor = 'appl';
550 p_descr->width = p_vout->output.i_width;
551 p_descr->height = p_vout->output.i_height;
552 p_descr->hRes = Long2Fix(72);
553 p_descr->vRes = Long2Fix(72);
554 p_descr->spatialQuality = codecLosslessQuality;
555 p_descr->frameCount = 1;
556 p_descr->clutID = -1;
558 p_descr->dataSize = p_vout->output.i_width *
559 p_vout->output.i_height * 3 / 2;
562 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
564 if( ( err = DecompressSequenceBeginS(
565 &p_vout->p_sys->i_seq,
566 p_vout->p_sys->h_img_descr,
568 p_vout->p_sys->p_qdport,
570 p_vout->p_sys->p_matrix,
572 codecFlagUseImageBuffer,
573 codecLosslessQuality,
574 p_vout->p_sys->img_dc ) ) )
576 intf_ErrMsg( "DecompressSequenceBeginS failed: %d", err );
583 /*****************************************************************************
584 * QTDestroySequence: destroy sequence
585 *****************************************************************************/
586 static void QTDestroySequence( vout_thread_t *p_vout )
588 CDSequenceEnd( p_vout->p_sys->i_seq );
591 /*****************************************************************************
592 * QTNewPicture: allocate a picture
593 *****************************************************************************
594 * Returns 0 on success, 1 otherwise
595 *****************************************************************************/
596 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
598 int i_width = p_vout->output.i_width;
599 int i_height = p_vout->output.i_height;
601 /* We know the chroma, allocate a buffer which will be used
602 * directly by the decoder */
603 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
605 if( p_pic->p_sys == NULL )
610 switch( p_vout->output.i_chroma )
614 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
615 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
617 /* Allocate the memory buffer */
618 p_pic->p_data = vlc_memalign( 16, i_width * i_height * 3 / 2,
619 &p_pic->p_data_orig );
622 p_pic->Y_PIXELS = p_pic->p_data;
623 p_pic->p[Y_PLANE].i_lines = i_height;
624 p_pic->p[Y_PLANE].i_pitch = i_width;
625 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
626 p_pic->p[Y_PLANE].b_margin = 0;
629 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
630 p_pic->p[U_PLANE].i_lines = i_height / 2;
631 p_pic->p[U_PLANE].i_pitch = i_width / 2;
632 p_pic->p[U_PLANE].i_pixel_bytes = 1;
633 p_pic->p[U_PLANE].b_margin = 0;
636 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
637 p_pic->p[V_PLANE].i_lines = i_height / 2;
638 p_pic->p[V_PLANE].i_pitch = i_width / 2;
639 p_pic->p[V_PLANE].i_pixel_bytes = 1;
640 p_pic->p[V_PLANE].b_margin = 0;
642 /* We allocated 3 planes */
645 #define P p_pic->p_sys->pixmap_i420
646 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
647 - p_pic->p_sys->p_info;
648 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
649 - p_pic->p_sys->p_info;
650 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
651 - p_pic->p_sys->p_info;
653 P.componentInfoY.rowBytes = i_width;
654 P.componentInfoCb.rowBytes = i_width / 2;
655 P.componentInfoCr.rowBytes = i_width / 2;
661 /* Unknown chroma, tell the guy to get lost */
662 free( p_pic->p_sys );
663 intf_ErrMsg( "vout error: never heard of chroma 0x%.8x (%4.4s)",
664 p_vout->output.i_chroma,
665 (char*)&p_vout->output.i_chroma );
673 /*****************************************************************************
674 * QTFreePicture: destroy a picture allocated with QTNewPicture
675 *****************************************************************************/
676 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
678 switch( p_vout->output.i_chroma )
681 free( p_pic->p_data_orig );
685 free( p_pic->p_sys );