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() */
32 #include <videolan/vlc.h>
35 #include "video_output.h"
37 #include "interface.h"
41 #define QT_MAX_DIRECTBUFFERS 10
43 typedef struct picture_sys_s
48 /* When using I420 output */
49 PlanarPixmapInfoYUV420 pixmap_i420;
53 /*****************************************************************************
55 *****************************************************************************/
56 static int vout_Create ( struct vout_thread_s * );
57 static int vout_Init ( struct vout_thread_s * );
58 static void vout_End ( struct vout_thread_s * );
59 static void vout_Destroy ( struct vout_thread_s * );
60 static int vout_Manage ( struct vout_thread_s * );
61 static void vout_Render ( struct vout_thread_s *, struct picture_s * );
62 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
64 static int CoSendRequest ( struct vout_thread_s *, long i_request );
65 static int CoCreateWindow ( struct vout_thread_s * );
66 static int CoDestroyWindow ( struct vout_thread_s * );
67 static int CoToggleFullscreen ( struct vout_thread_s * );
69 static void QTScaleMatrix ( struct vout_thread_s * );
70 static int QTCreateSequence ( struct vout_thread_s * );
71 static void QTDestroySequence ( struct vout_thread_s * );
72 static int QTNewPicture ( struct vout_thread_s *, struct picture_s * );
73 static void QTFreePicture ( struct vout_thread_s *, struct picture_s * );
75 /*****************************************************************************
76 * Functions exported as capabilities. They are declared as static so that
77 * we don't pollute the namespace too much.
78 *****************************************************************************/
79 void _M( vout_getfunctions )( function_list_t * p_function_list )
81 p_function_list->functions.vout.pf_create = vout_Create;
82 p_function_list->functions.vout.pf_init = vout_Init;
83 p_function_list->functions.vout.pf_end = vout_End;
84 p_function_list->functions.vout.pf_destroy = vout_Destroy;
85 p_function_list->functions.vout.pf_manage = vout_Manage;
86 p_function_list->functions.vout.pf_render = vout_Render;
87 p_function_list->functions.vout.pf_display = vout_Display;
90 /*****************************************************************************
91 * vout_Create: allocates MacOS X video thread output method
92 *****************************************************************************
93 * This function allocates and initializes a MacOS X vout method.
94 *****************************************************************************/
95 static int vout_Create( vout_thread_t *p_vout )
99 if( !p_main->p_intf || !p_main->p_intf->p_module ||
100 strcmp( p_main->p_intf->p_module->psz_name, MODULE_STRING ) != 0 )
102 intf_ErrMsg( "vout error: MacOS X interface module required" );
106 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
107 if( p_vout->p_sys == NULL )
109 intf_ErrMsg( "vout error: %s", strerror( ENOMEM ) );
113 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
115 p_vout->p_sys->h_img_descr =
116 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
117 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
119 p_vout->p_sys->b_mouse_pointer_visible = 1;
121 /* set window size */
122 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
123 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
125 if( ( err = EnterMovies() ) != noErr )
127 intf_ErrMsg( "vout error: EnterMovies failed: %d", err );
128 free( p_vout->p_sys->p_matrix );
129 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
130 free( p_vout->p_sys );
134 if( vout_ChromaCmp( p_vout->render.i_chroma, FOURCC_I420 ) )
136 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
137 nil, &p_vout->p_sys->img_dc );
138 if( err == noErr && p_vout->p_sys->img_dc != 0 )
140 p_vout->output.i_chroma = FOURCC_I420;
141 p_vout->p_sys->i_codec = kYUV420CodecType;
145 intf_ErrMsg( "vout error: failed to find an appropriate codec" );
150 intf_ErrMsg( "vout error: chroma 0x%08x not supported",
151 p_vout->render.i_chroma );
154 if( p_vout->p_sys->img_dc == 0 )
156 free( p_vout->p_sys->p_matrix );
157 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
158 free( p_vout->p_sys );
162 if( CoCreateWindow( p_vout ) )
164 intf_ErrMsg( "vout error: unable to create window" );
165 free( p_vout->p_sys->p_matrix );
166 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
167 free( p_vout->p_sys );
174 /*****************************************************************************
175 * vout_Init: initialize video thread output method
176 *****************************************************************************/
177 static int vout_Init( vout_thread_t *p_vout )
182 I_OUTPUTPICTURES = 0;
184 /* Initialize the output structure; we already found a codec,
185 * and the corresponding chroma we will be using. Since we can
186 * arbitrary scale, stick to the coordinates and aspect. */
187 p_vout->output.i_width = p_vout->render.i_width;
188 p_vout->output.i_height = p_vout->render.i_height;
189 p_vout->output.i_aspect = p_vout->render.i_aspect;
191 SetPort( p_vout->p_sys->p_qdport );
192 QTScaleMatrix( p_vout );
194 if( QTCreateSequence( p_vout ) )
196 intf_ErrMsg( "vout error: unable to create sequence" );
200 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
201 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
205 /* Find an empty picture slot */
206 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
208 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
210 p_pic = p_vout->p_picture + i_index;
215 /* Allocate the picture */
216 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
221 p_pic->i_status = DESTROYED_PICTURE;
222 p_pic->i_type = DIRECT_PICTURE;
224 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
232 /*****************************************************************************
233 * vout_End: terminate video thread output method
234 *****************************************************************************/
235 static void vout_End( vout_thread_t *p_vout )
239 QTDestroySequence( p_vout );
241 /* Free the direct buffers we allocated */
242 for( i_index = I_OUTPUTPICTURES; i_index; )
245 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
249 /*****************************************************************************
250 * vout_Destroy: destroy video thread output method
251 *****************************************************************************/
252 static void vout_Destroy( vout_thread_t *p_vout )
254 if( CoDestroyWindow( p_vout ) )
256 intf_ErrMsg( "vout error: unable to destroy window" );
261 free( p_vout->p_sys->p_matrix );
262 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
263 free( p_vout->p_sys );
266 /*****************************************************************************
267 * vout_Manage: handle events
268 *****************************************************************************
269 * This function should be called regularly by video output thread. It manages
270 * console events. It returns a non null value on error.
271 *****************************************************************************/
272 static int vout_Manage( vout_thread_t *p_vout )
274 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
276 if( CoToggleFullscreen( p_vout ) )
281 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
284 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
286 QTScaleMatrix( p_vout );
287 SetDSequenceMatrix( p_vout->p_sys->i_seq,
288 p_vout->p_sys->p_matrix );
290 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
293 /* hide/show mouse cursor */
294 if( p_vout->p_sys->b_mouse_moved ||
295 p_vout->p_sys->i_time_mouse_last_moved )
297 boolean_t b_change = 0;
299 if( !p_vout->p_sys->b_mouse_pointer_visible )
301 CGDisplayShowCursor( kCGDirectMainDisplay );
305 else if( !p_vout->p_sys->b_mouse_moved &&
306 mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
307 p_vout->p_sys->b_mouse_pointer_visible )
309 CGDisplayHideCursor( kCGDirectMainDisplay );
316 p_vout->p_sys->i_time_mouse_last_moved = 0;
317 p_vout->p_sys->b_mouse_moved = 0;
318 p_vout->p_sys->b_mouse_pointer_visible =
319 !p_vout->p_sys->b_mouse_pointer_visible;
326 /*****************************************************************************
327 * vout_Render: render previously calculated output
328 *****************************************************************************/
329 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
334 /*****************************************************************************
335 * vout_Display: displays previously rendered output
336 *****************************************************************************
337 * This function sends the currently rendered image to the display.
338 *****************************************************************************/
339 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
344 if( ( err = DecompressSequenceFrameS(
345 p_vout->p_sys->i_seq,
346 p_pic->p_sys->p_info,
347 p_pic->p_sys->i_size,
348 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
350 intf_ErrMsg( "DecompressSequenceFrameS failed: %d", err );
354 /*****************************************************************************
355 * CoSendRequest: send request to interface thread
356 *****************************************************************************
357 * Returns 0 on success, 1 otherwise
358 *****************************************************************************/
359 static int CoSendRequest( vout_thread_t *p_vout, long i_request )
362 NSPortMessage *o_msg;
363 struct vout_req_s req;
364 struct vout_req_s *p_req = &req;
365 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
366 NSPort *recvPort = [[NSPort port] retain];
368 memset( &req, 0, sizeof(req) );
369 req.i_type = i_request;
372 req.o_lock = [[NSConditionLock alloc] initWithCondition: 0];
374 o_array = [NSArray arrayWithObject:
375 [NSData dataWithBytes: &p_req length: sizeof(void *)]];
376 o_msg = [[NSPortMessage alloc]
377 initWithSendPort: p_main->p_intf->p_sys->o_port
378 receivePort: recvPort
379 components: o_array];
381 [o_msg sendBeforeDate: [NSDate distantPast]];
382 [req.o_lock lockWhenCondition: 1];
386 [req.o_lock release];
391 return( !req.i_result );
394 /*****************************************************************************
395 * CoCreateWindow: create new window
396 *****************************************************************************
397 * Returns 0 on success, 1 otherwise
398 *****************************************************************************/
399 static int CoCreateWindow( vout_thread_t *p_vout )
401 if( CoSendRequest( p_vout, VOUT_REQ_CREATE_WINDOW ) )
403 intf_ErrMsg( "CoSendRequest (CREATE_WINDOW) failed" );
410 /*****************************************************************************
411 * CoDestroyWindow: destroy window
412 *****************************************************************************
413 * Returns 0 on success, 1 otherwise
414 *****************************************************************************/
415 static int CoDestroyWindow( vout_thread_t *p_vout )
417 if( !p_vout->p_sys->b_mouse_pointer_visible )
419 CGDisplayShowCursor( kCGDirectMainDisplay );
420 p_vout->p_sys->b_mouse_pointer_visible = 1;
423 if( CoSendRequest( p_vout, VOUT_REQ_DESTROY_WINDOW ) )
425 intf_ErrMsg( "CoSendRequest (DESTROY_WINDOW) failed" );
432 /*****************************************************************************
433 * CoToggleFullscreen: toggle fullscreen
434 *****************************************************************************
435 * Returns 0 on success, 1 otherwise
436 *****************************************************************************/
437 static int CoToggleFullscreen( vout_thread_t *p_vout )
439 QTDestroySequence( p_vout );
441 if( CoDestroyWindow( p_vout ) )
443 intf_ErrMsg( "vout error: unable to destroy window" );
447 p_vout->b_fullscreen = !p_vout->b_fullscreen;
449 if( CoCreateWindow( p_vout ) )
451 intf_ErrMsg( "vout error: unable to create window" );
455 SetPort( p_vout->p_sys->p_qdport );
456 QTScaleMatrix( p_vout );
458 if( QTCreateSequence( p_vout ) )
460 intf_ErrMsg( "vout error: unable to create sequence" );
467 /*****************************************************************************
468 * QTScaleMatrix: scale matrix
469 *****************************************************************************/
470 static void QTScaleMatrix( vout_thread_t *p_vout )
473 int i_width, i_height;
474 Fixed factor_x, factor_y;
478 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
480 i_width = s_rect.right - s_rect.left;
481 i_height = s_rect.bottom - s_rect.top;
483 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
485 int i_adj_width = i_height * p_vout->output.i_aspect /
488 factor_x = FixDiv( Long2Fix( i_adj_width ),
489 Long2Fix( p_vout->output.i_width ) );
490 factor_y = FixDiv( Long2Fix( i_height ),
491 Long2Fix( p_vout->output.i_height ) );
493 i_offset_x = (i_width - i_adj_width) / 2;
497 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
498 p_vout->output.i_aspect;
500 factor_x = FixDiv( Long2Fix( i_width ),
501 Long2Fix( p_vout->output.i_width ) );
502 factor_y = FixDiv( Long2Fix( i_adj_height ),
503 Long2Fix( p_vout->output.i_height ) );
505 i_offset_y = (i_height - i_adj_height) / 2;
508 SetIdentityMatrix( p_vout->p_sys->p_matrix );
510 ScaleMatrix( p_vout->p_sys->p_matrix,
512 Long2Fix(0), Long2Fix(0) );
514 TranslateMatrix( p_vout->p_sys->p_matrix,
515 Long2Fix(i_offset_x),
516 Long2Fix(i_offset_y) );
519 /*****************************************************************************
520 * QTCreateSequence: create a new sequence
521 *****************************************************************************
522 * Returns 0 on success, 1 otherwise
523 *****************************************************************************/
524 static int QTCreateSequence( vout_thread_t *p_vout )
527 ImageDescriptionPtr p_descr;
529 HLock( (Handle)p_vout->p_sys->h_img_descr );
530 p_descr = *p_vout->p_sys->h_img_descr;
532 p_descr->idSize = sizeof(ImageDescription);
533 p_descr->cType = p_vout->p_sys->i_codec;
534 p_descr->version = 1;
535 p_descr->revisionLevel = 0;
536 p_descr->vendor = 'appl';
537 p_descr->width = p_vout->output.i_width;
538 p_descr->height = p_vout->output.i_height;
539 p_descr->hRes = Long2Fix(72);
540 p_descr->vRes = Long2Fix(72);
541 p_descr->spatialQuality = codecLosslessQuality;
542 p_descr->frameCount = 1;
543 p_descr->clutID = -1;
544 p_descr->dataSize = 0;
547 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
549 if( ( err = DecompressSequenceBeginS(
550 &p_vout->p_sys->i_seq,
551 p_vout->p_sys->h_img_descr,
553 p_vout->p_sys->p_qdport,
555 p_vout->p_sys->p_matrix,
557 codecFlagUseImageBuffer,
558 codecLosslessQuality,
559 p_vout->p_sys->img_dc ) ) )
561 intf_ErrMsg( "DecompressSequenceBeginS failed: %d", err );
568 /*****************************************************************************
569 * QTDestroySequence: destroy sequence
570 *****************************************************************************/
571 static void QTDestroySequence( vout_thread_t *p_vout )
573 CDSequenceEnd( p_vout->p_sys->i_seq );
576 /*****************************************************************************
577 * QTNewPicture: allocate a picture
578 *****************************************************************************
579 * Returns 0 on success, 1 otherwise
580 *****************************************************************************/
581 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
583 int i_width = p_vout->output.i_width;
584 int i_height = p_vout->output.i_height;
586 /* We know the chroma, allocate a buffer which will be used
587 * directly by the decoder */
588 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
590 if( p_pic->p_sys == NULL )
595 switch( p_vout->output.i_chroma )
599 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
600 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
602 /* Allocate the memory buffer */
603 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
604 16, i_width * i_height * 3 / 2 );
607 p_pic->Y_PIXELS = p_pic->p_data;
608 p_pic->p[Y_PLANE].i_lines = i_height;
609 p_pic->p[Y_PLANE].i_pitch = i_width;
610 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
611 p_pic->p[Y_PLANE].b_margin = 0;
614 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
615 p_pic->p[U_PLANE].i_lines = i_height / 2;
616 p_pic->p[U_PLANE].i_pitch = i_width / 2;
617 p_pic->p[U_PLANE].i_pixel_bytes = 1;
618 p_pic->p[U_PLANE].b_margin = 0;
621 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
622 p_pic->p[V_PLANE].i_lines = i_height / 2;
623 p_pic->p[V_PLANE].i_pitch = i_width / 2;
624 p_pic->p[V_PLANE].i_pixel_bytes = 1;
625 p_pic->p[V_PLANE].b_margin = 0;
627 /* We allocated 3 planes */
630 #define P p_pic->p_sys->pixmap_i420
631 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
632 - p_pic->p_sys->p_info;
633 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
634 - p_pic->p_sys->p_info;
635 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
636 - p_pic->p_sys->p_info;
638 P.componentInfoY.rowBytes = i_width;
639 P.componentInfoCb.rowBytes = i_width / 2;
640 P.componentInfoCr.rowBytes = i_width / 2;
646 /* Unknown chroma, tell the guy to get lost */
647 free( p_pic->p_sys );
648 intf_ErrMsg( "vout error: never heard of chroma 0x%.8x (%4.4s)",
649 p_vout->output.i_chroma,
650 (char*)&p_vout->output.i_chroma );
658 /*****************************************************************************
659 * QTFreePicture: destroy a picture allocated with QTNewPicture
660 *****************************************************************************/
661 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
663 switch( p_vout->output.i_chroma )
666 free( p_pic->p_data_orig );
670 free( p_pic->p_sys );