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() */
34 #define QT_MAX_DIRECTBUFFERS 10
41 /* When using I420 output */
42 PlanarPixmapInfoYUV420 pixmap_i420;
45 /*****************************************************************************
47 *****************************************************************************/
48 static int vout_Create ( vout_thread_t * );
49 static int vout_Init ( vout_thread_t * );
50 static void vout_End ( vout_thread_t * );
51 static void vout_Destroy ( vout_thread_t * );
52 static int vout_Manage ( vout_thread_t * );
53 static void vout_Render ( vout_thread_t *, picture_t * );
54 static void vout_Display ( vout_thread_t *, picture_t * );
56 static int CoSendRequest ( vout_thread_t *, long );
57 static int CoCreateWindow ( vout_thread_t * );
58 static int CoDestroyWindow ( vout_thread_t * );
59 static int CoToggleFullscreen ( vout_thread_t * );
61 static void QTScaleMatrix ( vout_thread_t * );
62 static int QTCreateSequence ( vout_thread_t * );
63 static void QTDestroySequence ( vout_thread_t * );
64 static int QTNewPicture ( vout_thread_t *, picture_t * );
65 static void QTFreePicture ( vout_thread_t *, picture_t * );
67 /*****************************************************************************
68 * Functions exported as capabilities. They are declared as static so that
69 * we don't pollute the namespace too much.
70 *****************************************************************************/
71 void _M( vout_getfunctions )( function_list_t * p_function_list )
73 p_function_list->functions.vout.pf_create = vout_Create;
74 p_function_list->functions.vout.pf_init = vout_Init;
75 p_function_list->functions.vout.pf_end = vout_End;
76 p_function_list->functions.vout.pf_destroy = vout_Destroy;
77 p_function_list->functions.vout.pf_manage = vout_Manage;
78 p_function_list->functions.vout.pf_render = vout_Render;
79 p_function_list->functions.vout.pf_display = vout_Display;
82 /*****************************************************************************
83 * vout_Create: allocates MacOS X video thread output method
84 *****************************************************************************
85 * This function allocates and initializes a MacOS X vout method.
86 *****************************************************************************/
87 static int vout_Create( vout_thread_t *p_vout )
91 if( !p_vout->p_vlc->p_intf || !p_vout->p_vlc->p_intf->p_module ||
92 strcmp( p_vout->p_vlc->p_intf->p_module->psz_name, MODULE_STRING ) != 0 )
94 msg_Err( p_vout, "MacOS X interface module required" );
98 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
99 if( p_vout->p_sys == NULL )
101 msg_Err( p_vout, "out of memory" );
105 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
107 p_vout->p_sys->h_img_descr =
108 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
109 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
111 p_vout->p_sys->b_mouse_pointer_visible = 1;
113 /* set window size */
114 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
115 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
117 if( ( err = EnterMovies() ) != noErr )
119 msg_Err( p_vout, "EnterMovies failed: %d", err );
120 free( p_vout->p_sys->p_matrix );
121 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
122 free( p_vout->p_sys );
126 if( vout_ChromaCmp( p_vout->render.i_chroma, FOURCC_I420 ) )
128 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
129 nil, &p_vout->p_sys->img_dc );
130 if( err == noErr && p_vout->p_sys->img_dc != 0 )
132 p_vout->output.i_chroma = FOURCC_I420;
133 p_vout->p_sys->i_codec = kYUV420CodecType;
137 msg_Err( p_vout, "failed to find an appropriate codec" );
142 msg_Err( p_vout, "chroma 0x%08x not supported",
143 p_vout->render.i_chroma );
146 if( p_vout->p_sys->img_dc == 0 )
148 free( p_vout->p_sys->p_matrix );
149 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
150 free( p_vout->p_sys );
154 if( CoCreateWindow( p_vout ) )
156 msg_Err( p_vout, "unable to create window" );
157 free( p_vout->p_sys->p_matrix );
158 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
159 free( p_vout->p_sys );
166 /*****************************************************************************
167 * vout_Init: initialize video thread output method
168 *****************************************************************************/
169 static int vout_Init( vout_thread_t *p_vout )
174 I_OUTPUTPICTURES = 0;
176 /* Initialize the output structure; we already found a codec,
177 * and the corresponding chroma we will be using. Since we can
178 * arbitrary scale, stick to the coordinates and aspect. */
179 p_vout->output.i_width = p_vout->render.i_width;
180 p_vout->output.i_height = p_vout->render.i_height;
181 p_vout->output.i_aspect = p_vout->render.i_aspect;
183 SetPort( p_vout->p_sys->p_qdport );
184 QTScaleMatrix( p_vout );
186 if( QTCreateSequence( p_vout ) )
188 msg_Err( p_vout, "unable to create sequence" );
192 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
193 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
197 /* Find an empty picture slot */
198 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
200 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
202 p_pic = p_vout->p_picture + i_index;
207 /* Allocate the picture */
208 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
213 p_pic->i_status = DESTROYED_PICTURE;
214 p_pic->i_type = DIRECT_PICTURE;
216 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
224 /*****************************************************************************
225 * vout_End: terminate video thread output method
226 *****************************************************************************/
227 static void vout_End( vout_thread_t *p_vout )
231 QTDestroySequence( p_vout );
233 /* Free the direct buffers we allocated */
234 for( i_index = I_OUTPUTPICTURES; i_index; )
237 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
241 /*****************************************************************************
242 * vout_Destroy: destroy video thread output method
243 *****************************************************************************/
244 static void vout_Destroy( vout_thread_t *p_vout )
246 if( CoDestroyWindow( p_vout ) )
248 msg_Err( p_vout, "unable to destroy window" );
253 free( p_vout->p_sys->p_matrix );
254 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
255 free( p_vout->p_sys );
258 /*****************************************************************************
259 * vout_Manage: handle events
260 *****************************************************************************
261 * This function should be called regularly by video output thread. It manages
262 * console events. It returns a non null value on error.
263 *****************************************************************************/
264 static int vout_Manage( vout_thread_t *p_vout )
266 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
268 if( CoToggleFullscreen( p_vout ) )
273 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
276 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
278 QTScaleMatrix( p_vout );
279 SetDSequenceMatrix( p_vout->p_sys->i_seq,
280 p_vout->p_sys->p_matrix );
282 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
285 /* hide/show mouse cursor */
286 if( p_vout->p_sys->b_mouse_moved ||
287 p_vout->p_sys->i_time_mouse_last_moved )
289 vlc_bool_t b_change = 0;
291 if( !p_vout->p_sys->b_mouse_pointer_visible )
293 CGDisplayShowCursor( kCGDirectMainDisplay );
297 else if( !p_vout->p_sys->b_mouse_moved &&
298 mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
299 p_vout->p_sys->b_mouse_pointer_visible )
301 CGDisplayHideCursor( kCGDirectMainDisplay );
308 p_vout->p_sys->i_time_mouse_last_moved = 0;
309 p_vout->p_sys->b_mouse_moved = 0;
310 p_vout->p_sys->b_mouse_pointer_visible =
311 !p_vout->p_sys->b_mouse_pointer_visible;
318 /*****************************************************************************
319 * vout_Render: render previously calculated output
320 *****************************************************************************/
321 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
326 /*****************************************************************************
327 * vout_Display: displays previously rendered output
328 *****************************************************************************
329 * This function sends the currently rendered image to the display.
330 *****************************************************************************/
331 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
336 if( ( err = DecompressSequenceFrameS(
337 p_vout->p_sys->i_seq,
338 p_pic->p_sys->p_info,
339 p_pic->p_sys->i_size,
340 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
342 msg_Err( p_vout, "DecompressSequenceFrameS failed: %d", err );
346 /*****************************************************************************
347 * CoSendRequest: send request to interface thread
348 *****************************************************************************
349 * Returns 0 on success, 1 otherwise
350 *****************************************************************************/
351 static int CoSendRequest( vout_thread_t *p_vout, long i_request )
354 NSPortMessage *o_msg;
355 struct vout_req_s req;
356 struct vout_req_s *p_req = &req;
357 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
358 NSPort *recvPort = [[NSPort port] retain];
360 memset( &req, 0, sizeof(req) );
361 req.i_type = i_request;
364 req.o_lock = [[NSConditionLock alloc] initWithCondition: 0];
366 o_array = [NSArray arrayWithObject:
367 [NSData dataWithBytes: &p_req length: sizeof(void *)]];
368 o_msg = [[NSPortMessage alloc]
369 initWithSendPort: p_vout->p_vlc->p_intf->p_sys->o_port
370 receivePort: recvPort
371 components: o_array];
373 [o_msg sendBeforeDate: [NSDate distantPast]];
374 [req.o_lock lockWhenCondition: 1];
378 [req.o_lock release];
383 return( !req.i_result );
386 /*****************************************************************************
387 * CoCreateWindow: create new window
388 *****************************************************************************
389 * Returns 0 on success, 1 otherwise
390 *****************************************************************************/
391 static int CoCreateWindow( vout_thread_t *p_vout )
393 if( CoSendRequest( p_vout, VOUT_REQ_CREATE_WINDOW ) )
395 msg_Err( p_vout, "CoSendRequest (CREATE_WINDOW) failed" );
402 /*****************************************************************************
403 * CoDestroyWindow: destroy window
404 *****************************************************************************
405 * Returns 0 on success, 1 otherwise
406 *****************************************************************************/
407 static int CoDestroyWindow( vout_thread_t *p_vout )
409 if( !p_vout->p_sys->b_mouse_pointer_visible )
411 CGDisplayShowCursor( kCGDirectMainDisplay );
412 p_vout->p_sys->b_mouse_pointer_visible = 1;
415 if( CoSendRequest( p_vout, VOUT_REQ_DESTROY_WINDOW ) )
417 msg_Err( p_vout, "CoSendRequest (DESTROY_WINDOW) failed" );
424 /*****************************************************************************
425 * CoToggleFullscreen: toggle fullscreen
426 *****************************************************************************
427 * Returns 0 on success, 1 otherwise
428 *****************************************************************************/
429 static int CoToggleFullscreen( vout_thread_t *p_vout )
431 QTDestroySequence( p_vout );
433 if( CoDestroyWindow( p_vout ) )
435 msg_Err( p_vout, "unable to destroy window" );
439 p_vout->b_fullscreen = !p_vout->b_fullscreen;
441 if( CoCreateWindow( p_vout ) )
443 msg_Err( p_vout, "unable to create window" );
447 SetPort( p_vout->p_sys->p_qdport );
448 QTScaleMatrix( p_vout );
450 if( QTCreateSequence( p_vout ) )
452 msg_Err( p_vout, "unable to create sequence" );
459 /*****************************************************************************
460 * QTScaleMatrix: scale matrix
461 *****************************************************************************/
462 static void QTScaleMatrix( vout_thread_t *p_vout )
465 int i_width, i_height;
466 Fixed factor_x, factor_y;
470 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
472 i_width = s_rect.right - s_rect.left;
473 i_height = s_rect.bottom - s_rect.top;
475 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
477 int i_adj_width = i_height * p_vout->output.i_aspect /
480 factor_x = FixDiv( Long2Fix( i_adj_width ),
481 Long2Fix( p_vout->output.i_width ) );
482 factor_y = FixDiv( Long2Fix( i_height ),
483 Long2Fix( p_vout->output.i_height ) );
485 i_offset_x = (i_width - i_adj_width) / 2;
489 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
490 p_vout->output.i_aspect;
492 factor_x = FixDiv( Long2Fix( i_width ),
493 Long2Fix( p_vout->output.i_width ) );
494 factor_y = FixDiv( Long2Fix( i_adj_height ),
495 Long2Fix( p_vout->output.i_height ) );
497 i_offset_y = (i_height - i_adj_height) / 2;
500 SetIdentityMatrix( p_vout->p_sys->p_matrix );
502 ScaleMatrix( p_vout->p_sys->p_matrix,
504 Long2Fix(0), Long2Fix(0) );
506 TranslateMatrix( p_vout->p_sys->p_matrix,
507 Long2Fix(i_offset_x),
508 Long2Fix(i_offset_y) );
511 /*****************************************************************************
512 * QTCreateSequence: create a new sequence
513 *****************************************************************************
514 * Returns 0 on success, 1 otherwise
515 *****************************************************************************/
516 static int QTCreateSequence( vout_thread_t *p_vout )
519 ImageDescriptionPtr p_descr;
521 HLock( (Handle)p_vout->p_sys->h_img_descr );
522 p_descr = *p_vout->p_sys->h_img_descr;
524 p_descr->idSize = sizeof(ImageDescription);
525 p_descr->cType = p_vout->p_sys->i_codec;
526 p_descr->version = 1;
527 p_descr->revisionLevel = 0;
528 p_descr->vendor = 'appl';
529 p_descr->width = p_vout->output.i_width;
530 p_descr->height = p_vout->output.i_height;
531 p_descr->hRes = Long2Fix(72);
532 p_descr->vRes = Long2Fix(72);
533 p_descr->spatialQuality = codecLosslessQuality;
534 p_descr->frameCount = 1;
535 p_descr->clutID = -1;
536 p_descr->dataSize = 0;
539 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
541 if( ( err = DecompressSequenceBeginS(
542 &p_vout->p_sys->i_seq,
543 p_vout->p_sys->h_img_descr,
545 p_vout->p_sys->p_qdport,
547 p_vout->p_sys->p_matrix,
549 codecFlagUseImageBuffer,
550 codecLosslessQuality,
551 p_vout->p_sys->img_dc ) ) )
553 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
560 /*****************************************************************************
561 * QTDestroySequence: destroy sequence
562 *****************************************************************************/
563 static void QTDestroySequence( vout_thread_t *p_vout )
565 CDSequenceEnd( p_vout->p_sys->i_seq );
568 /*****************************************************************************
569 * QTNewPicture: allocate a picture
570 *****************************************************************************
571 * Returns 0 on success, 1 otherwise
572 *****************************************************************************/
573 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
575 int i_width = p_vout->output.i_width;
576 int i_height = p_vout->output.i_height;
578 /* We know the chroma, allocate a buffer which will be used
579 * directly by the decoder */
580 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
582 if( p_pic->p_sys == NULL )
587 switch( p_vout->output.i_chroma )
591 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
592 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
594 /* Allocate the memory buffer */
595 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
596 16, i_width * i_height * 3 / 2 );
599 p_pic->Y_PIXELS = p_pic->p_data;
600 p_pic->p[Y_PLANE].i_lines = i_height;
601 p_pic->p[Y_PLANE].i_pitch = i_width;
602 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
603 p_pic->p[Y_PLANE].b_margin = 0;
606 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
607 p_pic->p[U_PLANE].i_lines = i_height / 2;
608 p_pic->p[U_PLANE].i_pitch = i_width / 2;
609 p_pic->p[U_PLANE].i_pixel_bytes = 1;
610 p_pic->p[U_PLANE].b_margin = 0;
613 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
614 p_pic->p[V_PLANE].i_lines = i_height / 2;
615 p_pic->p[V_PLANE].i_pitch = i_width / 2;
616 p_pic->p[V_PLANE].i_pixel_bytes = 1;
617 p_pic->p[V_PLANE].b_margin = 0;
619 /* We allocated 3 planes */
622 #define P p_pic->p_sys->pixmap_i420
623 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
624 - p_pic->p_sys->p_info;
625 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
626 - p_pic->p_sys->p_info;
627 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
628 - p_pic->p_sys->p_info;
630 P.componentInfoY.rowBytes = i_width;
631 P.componentInfoCb.rowBytes = i_width / 2;
632 P.componentInfoCr.rowBytes = i_width / 2;
638 /* Unknown chroma, tell the guy to get lost */
639 free( p_pic->p_sys );
640 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
641 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
649 /*****************************************************************************
650 * QTFreePicture: destroy a picture allocated with QTNewPicture
651 *****************************************************************************/
652 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
654 switch( p_vout->output.i_chroma )
657 free( p_pic->p_data_orig );
661 free( p_pic->p_sys );