1 /*****************************************************************************
2 * vout.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m 8351 2004-08-02 13:06:38Z hartman $
7 * Authors: Colin Delacroix <colin@zoy.org>
8 * Florian G. Pflug <fgp@phlo.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * Derk-Jan Hartman <hartman at videolan dot org>
11 * Eric Petit <titer@m0k.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free() */
33 #include <string.h> /* strerror() */
35 #include <QuickTime/QuickTime.h>
42 #define QT_MAX_DIRECTBUFFERS 10
43 #define VL_MAX_DISPLAYS 16
45 /*****************************************************************************
47 *****************************************************************************/
48 @interface VLCQTView : NSQuickDrawView
50 vout_thread_t * p_vout;
53 - (id) initWithVout:(vout_thread_t *)p_vout;
59 NSAutoreleasePool *o_pool;
63 vlc_bool_t b_saved_frame;
70 MatrixRecordPtr p_matrix;
71 DecompressorComponent img_dc;
72 ImageDescriptionHandle h_img_descr;
80 /* When using I420 output */
81 PlanarPixmapInfoYUV420 pixmap_i420;
84 /*****************************************************************************
86 *****************************************************************************/
88 static int InitVideo ( vout_thread_t * );
89 static void EndVideo ( vout_thread_t * );
90 static int ManageVideo ( vout_thread_t * );
91 static void DisplayVideo ( vout_thread_t *, picture_t * );
92 static int ControlVideo ( vout_thread_t *, int, va_list );
94 static int CoToggleFullscreen( vout_thread_t *p_vout );
95 static void QTScaleMatrix ( vout_thread_t * );
96 static int QTCreateSequence ( vout_thread_t * );
97 static void QTDestroySequence ( vout_thread_t * );
98 static int QTNewPicture ( vout_thread_t *, picture_t * );
99 static void QTFreePicture ( vout_thread_t *, picture_t * );
101 /*****************************************************************************
102 * OpenVideo: allocates MacOS X video thread output method
103 *****************************************************************************
104 * This function allocates and initializes a MacOS X vout method.
105 *****************************************************************************/
106 int E_(OpenVideoQT) ( vlc_object_t *p_this )
108 vout_thread_t * p_vout = (vout_thread_t *)p_this;
113 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
114 if( p_vout->p_sys == NULL )
116 msg_Err( p_vout, "out of memory" );
120 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
122 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
123 for( i_timeout = 20 ; i_timeout-- ; )
127 msleep( INTF_IDLE_SLEEP );
133 /* no MacOS X intf, unable to communicate with MT */
134 msg_Err( p_vout, "no MacOS X interface present" );
135 free( p_vout->p_sys );
139 p_vout->pf_init = InitVideo;
140 p_vout->pf_end = EndVideo;
141 p_vout->pf_manage = ManageVideo;
142 p_vout->pf_render = NULL;
143 p_vout->pf_display = DisplayVideo;
144 p_vout->pf_control = ControlVideo;
146 p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
148 var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
149 var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
150 var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
151 var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
153 p_vout->p_sys->b_altivec = p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC;
154 msg_Dbg( p_vout, "We do%s have Altivec", p_vout->p_sys->b_altivec ? "" : "n't" );
156 /* Initialize QuickTime */
157 p_vout->p_sys->h_img_descr =
158 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
159 p_vout->p_sys->p_matrix =
160 (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
162 if( ( err = EnterMovies() ) != noErr )
164 msg_Err( p_vout, "EnterMovies failed: %d", err );
165 free( p_vout->p_sys->p_matrix );
166 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
167 free( p_vout->p_sys );
171 /* Damn QT isn't thread safe. so keep a lock in the p_vlc object */
172 vlc_mutex_lock( &p_vout->p_vlc->quicktime_lock );
174 /* Can we find the right chroma ? */
175 if( p_vout->p_sys->b_altivec )
177 err = FindCodec( kYUVSPixelFormat, bestSpeedCodec,
178 nil, &p_vout->p_sys->img_dc );
182 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
183 nil, &p_vout->p_sys->img_dc );
185 vlc_mutex_unlock( &p_vout->p_vlc->quicktime_lock );
187 if( err == noErr && p_vout->p_sys->img_dc != 0 )
189 if( p_vout->p_sys->b_altivec )
191 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
192 p_vout->p_sys->i_codec = kYUVSPixelFormat;
196 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
197 p_vout->p_sys->i_codec = kYUV420CodecType;
202 msg_Err( p_vout, "failed to find an appropriate codec" );
205 if( p_vout->p_sys->img_dc == 0 )
207 free( p_vout->p_sys->p_matrix );
208 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
209 free( p_vout->p_sys );
213 /* Setup the menuitem for the multiple displays. Read the vlc preference (macosx-vdev) for the primary display */
214 NSArray * o_screens = [NSScreen screens];
215 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
218 vlc_value_t val2, text;
221 var_Get( p_vout, "macosx-vdev", &val );
223 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
225 text.psz_string = _("Video device");
226 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
228 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
230 while( (o_screen = [o_enumerator nextObject]) != NULL )
233 NSRect s_rect = [o_screen frame];
235 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
236 "%s %d (%dx%d)", _("Screen"), i,
237 (int)s_rect.size.width, (int)s_rect.size.height );
239 text.psz_string = psz_temp;
241 var_Change( p_vout, "video-device",
242 VLC_VAR_ADDCHOICE, &val2, &text );
244 if( ( i - 1 ) == val.i_int )
246 var_Set( p_vout, "video-device", val2 );
251 var_AddCallback( p_vout, "video-device", vout_VarCallback,
254 val2.b_bool = VLC_TRUE;
255 var_Set( p_vout, "intf-change", val2 );
259 p_vout->p_sys->o_window =
260 [[VLCWindow alloc] initWithVout: p_vout frame: nil];
262 #define o_qtview p_vout->p_sys->o_qtview
263 o_qtview = [[VLCQTView alloc] initWithVout: p_vout];
264 [p_vout->p_sys->o_window setContentView: o_qtview];
265 [o_qtview autorelease];
267 /* Retrieve the QuickDraw port */
268 [o_qtview lockFocus];
269 p_vout->p_sys->p_qdport = [o_qtview qdPort];
270 [o_qtview unlockFocus];
276 /*****************************************************************************
277 * CloseVideo: destroy video thread output method
278 *****************************************************************************/
279 void E_(CloseVideoQT) ( vlc_object_t *p_this )
281 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
282 vout_thread_t * p_vout = (vout_thread_t *)p_this;
284 [p_vout->p_sys->o_window close];
286 /* Clean Up Quicktime environment */
288 free( p_vout->p_sys->p_matrix );
289 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
292 free( p_vout->p_sys );
295 /*****************************************************************************
296 * InitVideo: initialize video thread output method
297 *****************************************************************************/
298 static int InitVideo ( vout_thread_t *p_vout )
303 I_OUTPUTPICTURES = 0;
305 /* Initialize the output structure; we already found a codec,
306 * and the corresponding chroma we will be using. Since we can
307 * arbitrary scale, stick to the coordinates and aspect. */
308 p_vout->output.i_width = p_vout->render.i_width;
309 p_vout->output.i_height = p_vout->render.i_height;
310 p_vout->output.i_aspect = p_vout->render.i_aspect;
312 SetPort( p_vout->p_sys->p_qdport );
313 QTScaleMatrix( p_vout );
315 if( QTCreateSequence( p_vout ) )
317 msg_Err( p_vout, "unable to create sequence" );
321 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
322 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
326 /* Find an empty picture slot */
327 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
329 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
331 p_pic = p_vout->p_picture + i_index;
336 /* Allocate the picture */
337 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
342 p_pic->i_status = DESTROYED_PICTURE;
343 p_pic->i_type = DIRECT_PICTURE;
345 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
351 /*****************************************************************************
352 * EndVideo: terminate video thread output method
353 *****************************************************************************/
354 static void EndVideo( vout_thread_t *p_vout )
358 QTDestroySequence( p_vout );
360 /* Free the direct buffers we allocated */
361 for( i_index = I_OUTPUTPICTURES; i_index; )
364 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
368 /*****************************************************************************
369 * ManageVideo: handle events
370 *****************************************************************************
371 * This function should be called regularly by video output thread. It manages
372 * console events. It returns a non null value on error.
373 *****************************************************************************/
374 static int ManageVideo( vout_thread_t *p_vout )
376 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
378 if( CoToggleFullscreen( p_vout ) )
383 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
386 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
388 QTScaleMatrix( p_vout );
389 SetDSequenceMatrix( p_vout->p_sys->i_seq,
390 p_vout->p_sys->p_matrix );
392 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
395 [p_vout->p_sys->o_window manage];
400 /*****************************************************************************
401 * vout_Display: displays previously rendered output
402 *****************************************************************************
403 * This function sends the currently rendered image to the display.
404 *****************************************************************************/
405 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
410 if( ( err = DecompressSequenceFrameWhen(
411 p_vout->p_sys->i_seq,
412 p_pic->p_sys->p_data,
413 p_pic->p_sys->i_size,
414 codecFlagUseImageBuffer, &flags, NULL, NULL ) != noErr ) )
416 msg_Warn( p_vout, "DecompressSequenceFrameWhen failed: %d", err );
420 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
424 /*****************************************************************************
425 * ControlVideo: control facility for the vout
426 *****************************************************************************/
427 static int ControlVideo( vout_thread_t *p_vout, int i_query, va_list args )
433 case VOUT_SET_STAY_ON_TOP:
434 b_arg = va_arg( args, vlc_bool_t );
435 [p_vout->p_sys->o_window setOnTop: b_arg];
441 return vout_vaControlDefault( p_vout, i_query, args );
445 /*****************************************************************************
446 * CoToggleFullscreen: toggle fullscreen
447 *****************************************************************************
448 * Returns 0 on success, 1 otherwise
449 *****************************************************************************/
450 static int CoToggleFullscreen( vout_thread_t *p_vout )
452 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
454 QTDestroySequence( p_vout );
456 if( !p_vout->b_fullscreen )
458 /* Save window size and position */
459 p_vout->p_sys->s_frame.size =
460 [[p_vout->p_sys->o_window contentView] frame].size;
461 p_vout->p_sys->s_frame.origin =
462 [p_vout->p_sys->o_window frame].origin;
463 p_vout->p_sys->b_saved_frame = VLC_TRUE;
465 [p_vout->p_sys->o_window close];
467 p_vout->b_fullscreen = !p_vout->b_fullscreen;
469 if( p_vout->p_sys->b_saved_frame )
471 p_vout->p_sys->o_window = [[VLCWindow alloc]
472 initWithVout: p_vout frame: &p_vout->p_sys->s_frame];
476 p_vout->p_sys->o_window = [[VLCWindow alloc]
477 initWithVout: p_vout frame: nil];
480 #define o_qtview p_vout->p_sys->o_qtview
481 o_qtview = [[VLCQTView alloc] initWithVout: p_vout];
482 [p_vout->p_sys->o_window setContentView: o_qtview];
483 [o_qtview autorelease];
485 /* Retrieve the QuickDraw port */
486 [o_qtview lockFocus];
487 p_vout->p_sys->p_qdport = [o_qtview qdPort];
488 [o_qtview unlockFocus];
491 SetPort( p_vout->p_sys->p_qdport );
492 QTScaleMatrix( p_vout );
494 if( QTCreateSequence( p_vout ) )
496 msg_Err( p_vout, "unable to create sequence" );
504 /*****************************************************************************
505 * QTScaleMatrix: scale matrix
506 *****************************************************************************/
507 static void QTScaleMatrix( vout_thread_t *p_vout )
511 unsigned int i_width, i_height;
512 Fixed factor_x, factor_y;
513 unsigned int i_offset_x = 0;
514 unsigned int i_offset_y = 0;
516 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
518 i_width = s_rect.right - s_rect.left;
519 i_height = s_rect.bottom - s_rect.top;
521 var_Get( p_vout, "macosx-stretch", &val );
524 factor_x = FixDiv( Long2Fix( i_width ),
525 Long2Fix( p_vout->output.i_width ) );
526 factor_y = FixDiv( Long2Fix( i_height ),
527 Long2Fix( p_vout->output.i_height ) );
530 else if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
532 int i_adj_width = i_height * p_vout->output.i_aspect /
535 factor_x = FixDiv( Long2Fix( i_adj_width ),
536 Long2Fix( p_vout->output.i_width ) );
537 factor_y = FixDiv( Long2Fix( i_height ),
538 Long2Fix( p_vout->output.i_height ) );
540 i_offset_x = (i_width - i_adj_width) / 2;
544 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
545 p_vout->output.i_aspect;
547 factor_x = FixDiv( Long2Fix( i_width ),
548 Long2Fix( p_vout->output.i_width ) );
549 factor_y = FixDiv( Long2Fix( i_adj_height ),
550 Long2Fix( p_vout->output.i_height ) );
552 i_offset_y = (i_height - i_adj_height) / 2;
555 SetIdentityMatrix( p_vout->p_sys->p_matrix );
557 ScaleMatrix( p_vout->p_sys->p_matrix,
559 Long2Fix(0), Long2Fix(0) );
561 TranslateMatrix( p_vout->p_sys->p_matrix,
562 Long2Fix(i_offset_x), Long2Fix(i_offset_y) );
565 /*****************************************************************************
566 * QTCreateSequence: create a new sequence
567 *****************************************************************************
568 * Returns 0 on success, 1 otherwise
569 *****************************************************************************/
570 static int QTCreateSequence( vout_thread_t *p_vout )
573 ImageDescriptionPtr p_descr;
575 HLock( (Handle)p_vout->p_sys->h_img_descr );
576 p_descr = *p_vout->p_sys->h_img_descr;
578 p_descr->idSize = sizeof(ImageDescription);
579 p_descr->cType = p_vout->p_sys->i_codec;
580 p_descr->version = 2;
581 p_descr->revisionLevel = 0;
582 p_descr->vendor = 'mpla';
583 p_descr->width = p_vout->output.i_width;
584 p_descr->height = p_vout->output.i_height;
585 p_descr->hRes = Long2Fix(72);
586 p_descr->vRes = Long2Fix(72);
587 p_descr->spatialQuality = codecLosslessQuality;
588 p_descr->frameCount = 1;
589 p_descr->clutID = -1;
590 p_descr->dataSize = 0;
593 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
595 if( ( err = DecompressSequenceBeginS(
596 &p_vout->p_sys->i_seq,
597 p_vout->p_sys->h_img_descr,
599 (p_descr->width * p_descr->height * 16) / 8,
600 p_vout->p_sys->p_qdport,
602 p_vout->p_sys->p_matrix,
604 codecFlagUseImageBuffer,
605 codecLosslessQuality,
608 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
615 /*****************************************************************************
616 * QTDestroySequence: destroy sequence
617 *****************************************************************************/
618 static void QTDestroySequence( vout_thread_t *p_vout )
620 CDSequenceEnd( p_vout->p_sys->i_seq );
623 /*****************************************************************************
624 * QTNewPicture: allocate a picture
625 *****************************************************************************
626 * Returns 0 on success, 1 otherwise
627 *****************************************************************************/
628 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
630 /* We know the chroma, allocate a buffer which will be used
631 * directly by the decoder */
632 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
634 if( p_pic->p_sys == NULL )
639 vout_InitPicture( VLC_OBJECT( p_vout), p_pic, p_vout->output.i_chroma,
640 p_vout->output.i_width, p_vout->output.i_height,
641 p_vout->output.i_aspect );
643 switch( p_vout->output.i_chroma )
645 case VLC_FOURCC('Y','U','Y','2'):
646 p_pic->p_sys->i_size = p_vout->output.i_width * p_vout->output.i_height * 2;
648 /* Allocate the memory buffer */
649 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
650 16, p_pic->p_sys->i_size );
652 p_pic->p[0].p_pixels = p_pic->p_data;
653 p_pic->p[0].i_lines = p_vout->output.i_height;
654 p_pic->p[0].i_visible_lines = p_vout->output.i_height;
655 p_pic->p[0].i_pitch = p_vout->output.i_width * 2;
656 p_pic->p[0].i_pixel_pitch = 1;
657 p_pic->p[0].i_visible_pitch = p_vout->output.i_width * 2;
660 p_pic->p_sys->p_data = (void *)p_pic->p[0].p_pixels;
664 case VLC_FOURCC('I','4','2','0'):
665 p_pic->p_sys->p_data = (void *)&p_pic->p_sys->pixmap_i420;
666 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
668 /* Allocate the memory buffer */
669 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
670 16, p_vout->output.i_width * p_vout->output.i_height * 3 / 2 );
673 p_pic->Y_PIXELS = p_pic->p_data;
674 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
675 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
676 p_pic->p[Y_PLANE].i_pitch = p_vout->output.i_width;
677 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
678 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
681 p_pic->U_PIXELS = p_pic->Y_PIXELS + p_vout->output.i_height * p_vout->output.i_width;
682 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
683 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
684 p_pic->p[U_PLANE].i_pitch = p_vout->output.i_width / 2;
685 p_pic->p[U_PLANE].i_pixel_pitch = 1;
686 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
689 p_pic->V_PIXELS = p_pic->U_PIXELS + p_vout->output.i_height * p_vout->output.i_width / 4;
690 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
691 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
692 p_pic->p[V_PLANE].i_pitch = p_vout->output.i_width / 2;
693 p_pic->p[V_PLANE].i_pixel_pitch = 1;
694 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
696 /* We allocated 3 planes */
699 #define P p_pic->p_sys->pixmap_i420
700 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
701 - p_pic->p_sys->p_data;
702 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
703 - p_pic->p_sys->p_data;
704 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
705 - p_pic->p_sys->p_data;
707 P.componentInfoY.rowBytes = p_vout->output.i_width;
708 P.componentInfoCb.rowBytes = p_vout->output.i_width / 2;
709 P.componentInfoCr.rowBytes = p_vout->output.i_width / 2;
714 /* Unknown chroma, tell the guy to get lost */
715 free( p_pic->p_sys );
716 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
717 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
725 /*****************************************************************************
726 * QTFreePicture: destroy a picture allocated with QTNewPicture
727 *****************************************************************************/
728 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
730 switch( p_vout->output.i_chroma )
732 case VLC_FOURCC('I','4','2','0'):
733 free( p_pic->p_data_orig );
737 free( p_pic->p_sys );
740 /*****************************************************************************
741 * VLCQTView implementation
742 *****************************************************************************/
743 @implementation VLCQTView
745 - (id) initWithVout:(vout_thread_t *)_p_vout
751 - (void)drawRect:(NSRect)rect
753 [[NSColor blackColor] set];
755 [super drawRect: rect];
757 p_vout->i_changes |= VOUT_SIZE_CHANGE;