]> git.sesse.net Git - vlc/blob - plugins/macosx/vout_macosx.m
165d326fb0f29dd7edd3b7ff7f3e5ad260c0154d
[vlc] / plugins / macosx / vout_macosx.m
1 /*****************************************************************************
2  * vout_macosx.c: MacOS X video output plugin
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  *
6  * Authors: Colin Delacroix <colin@zoy.org>
7  *          Florian G. Pflug <fgp@phlo.org>
8  *          Jon Lech Johansen <jon-vl@nanocrew.net>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <videolan/vlc.h>
33
34 #include "video.h"
35 #include "video_output.h"
36
37 #include "interface.h"
38
39 #include "macosx.h"
40
41 #define QT_MAX_DIRECTBUFFERS 10
42
43 typedef struct picture_sys_s
44 {
45     void *p_info;
46     unsigned int i_size;
47
48     /* When using I420 output */
49     PlanarPixmapInfoYUV420 pixmap_i420;
50
51 } picture_sys_t;
52
53 /*****************************************************************************
54  * Local prototypes
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 * );
63
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 * );
68
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 * );
74
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 )
80 {
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;
88 }
89
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 )
96 {
97     OSErr err;
98
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 )
101     {
102         intf_ErrMsg( "vout error: MacOS X interface module required" );
103         return( 1 );
104     }
105
106     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
107     if( p_vout->p_sys == NULL )
108     {
109         intf_ErrMsg( "vout error: %s", strerror( ENOMEM ) );
110         return( 1 );
111     }
112
113     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
114
115     p_vout->p_sys->h_img_descr = 
116         (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
117     p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
118
119     p_vout->p_sys->b_mouse_pointer_visible = 1;
120
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;
124
125     if( ( err = EnterMovies() ) != noErr )
126     {
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 );
131         return( 1 );
132     } 
133
134     if( vout_ChromaCmp( p_vout->render.i_chroma, FOURCC_I420 ) )
135     {
136         err = FindCodec( kYUV420CodecType, bestSpeedCodec,
137                          nil, &p_vout->p_sys->img_dc );
138         if( err == noErr && p_vout->p_sys->img_dc != 0 )
139         {
140             p_vout->output.i_chroma = FOURCC_I420;
141             p_vout->p_sys->i_codec = kYUV420CodecType;
142         }
143         else
144         {
145             intf_ErrMsg( "vout error: failed to find an appropriate codec" );
146         }
147     }
148     else
149     {
150         intf_ErrMsg( "vout error: chroma 0x%08x not supported",
151                      p_vout->render.i_chroma );
152     }
153
154     if( p_vout->p_sys->img_dc == 0 )
155     {
156         free( p_vout->p_sys->p_matrix );
157         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
158         free( p_vout->p_sys );
159         return( 1 );        
160     }
161
162     if( CoCreateWindow( p_vout ) )
163     {
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 ); 
168         return( 1 );
169     }
170
171     return( 0 );
172 }
173
174 /*****************************************************************************
175  * vout_Init: initialize video thread output method
176  *****************************************************************************/
177 static int vout_Init( vout_thread_t *p_vout )
178 {
179     int i_index;
180     picture_t *p_pic;
181
182     I_OUTPUTPICTURES = 0;
183
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;
190
191     SetPort( p_vout->p_sys->p_qdport );
192     QTScaleMatrix( p_vout );
193
194     if( QTCreateSequence( p_vout ) )
195     {
196         intf_ErrMsg( "vout error: unable to create sequence" );
197         return( 1 );
198     }
199
200     /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
201     while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
202     {
203         p_pic = NULL;
204
205         /* Find an empty picture slot */
206         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
207         {
208             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
209             {
210                 p_pic = p_vout->p_picture + i_index;
211                 break;
212             }
213         }
214
215         /* Allocate the picture */
216         if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
217         {
218             break;
219         }
220
221         p_pic->i_status = DESTROYED_PICTURE;
222         p_pic->i_type   = DIRECT_PICTURE;
223
224         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
225
226         I_OUTPUTPICTURES++;
227     }
228
229     return( 0 );
230 }
231
232 /*****************************************************************************
233  * vout_End: terminate video thread output method
234  *****************************************************************************/
235 static void vout_End( vout_thread_t *p_vout )
236 {
237     int i_index;
238
239     QTDestroySequence( p_vout );
240
241     /* Free the direct buffers we allocated */
242     for( i_index = I_OUTPUTPICTURES; i_index; )
243     {
244         i_index--;
245         QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
246     }
247 }
248
249 /*****************************************************************************
250  * vout_Destroy: destroy video thread output method
251  *****************************************************************************/
252 static void vout_Destroy( vout_thread_t *p_vout )
253 {
254     if( CoDestroyWindow( p_vout ) )
255     {
256         intf_ErrMsg( "vout error: unable to destroy window" );
257     }
258
259     ExitMovies();
260
261     free( p_vout->p_sys->p_matrix );
262     DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
263     free( p_vout->p_sys );
264 }
265
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 )
273 {    
274     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
275     {
276         if( CoToggleFullscreen( p_vout ) )  
277         {
278             return( 1 );
279         }
280
281         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
282     }
283
284     if( p_vout->i_changes & VOUT_SIZE_CHANGE ) 
285     {
286         QTScaleMatrix( p_vout );
287         SetDSequenceMatrix( p_vout->p_sys->i_seq, 
288                             p_vout->p_sys->p_matrix );
289  
290         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
291     }
292
293     /* hide/show mouse cursor */
294     if( p_vout->p_sys->b_mouse_moved ||
295         p_vout->p_sys->i_time_mouse_last_moved )
296     {
297         boolean_t b_change = 0;
298
299         if( !p_vout->p_sys->b_mouse_pointer_visible )
300         {
301             CGDisplayShowCursor( kCGDirectMainDisplay );
302             b_change = 1;
303         }
304 #if 0
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 )
308         {
309             CGDisplayHideCursor( kCGDirectMainDisplay );
310             b_change = 1;
311         }
312 #endif
313
314         if( b_change )
315         {
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;
320         }
321     }
322
323     return( 0 );
324 }
325
326 /*****************************************************************************
327  * vout_Render: render previously calculated output
328  *****************************************************************************/
329 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
330 {
331     ;
332 }
333
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 )
340 {
341     OSErr err;
342     CodecFlags flags;
343
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 ) )
349     {
350         intf_ErrMsg( "DecompressSequenceFrameS failed: %d", err );
351     }
352 }
353
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 )
360 {
361     NSArray *o_array;
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];
367
368     memset( &req, 0, sizeof(req) );
369     req.i_type = i_request;
370     req.p_vout = p_vout;
371
372     req.o_lock = [[NSConditionLock alloc] initWithCondition: 0];
373
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];
380
381     [o_msg sendBeforeDate: [NSDate distantPast]];
382     [req.o_lock lockWhenCondition: 1];
383     [req.o_lock unlock];
384
385     [o_msg release];
386     [req.o_lock release];
387
388     [recvPort release];
389     [o_pool release];
390
391     return( !req.i_result );
392 }
393
394 /*****************************************************************************
395  * CoCreateWindow: create new window 
396  *****************************************************************************
397  * Returns 0 on success, 1 otherwise
398  *****************************************************************************/
399 static int CoCreateWindow( vout_thread_t *p_vout )
400 {
401     if( CoSendRequest( p_vout, VOUT_REQ_CREATE_WINDOW ) )
402     {
403         intf_ErrMsg( "CoSendRequest (CREATE_WINDOW) failed" );
404         return( 1 );
405     }
406
407     return( 0 );
408 }
409
410 /*****************************************************************************
411  * CoDestroyWindow: destroy window 
412  *****************************************************************************
413  * Returns 0 on success, 1 otherwise
414  *****************************************************************************/
415 static int CoDestroyWindow( vout_thread_t *p_vout )
416 {
417     if( !p_vout->p_sys->b_mouse_pointer_visible )
418     {
419         CGDisplayShowCursor( kCGDirectMainDisplay );
420         p_vout->p_sys->b_mouse_pointer_visible = 1;
421     }
422
423     if( CoSendRequest( p_vout, VOUT_REQ_DESTROY_WINDOW ) )
424     {
425         intf_ErrMsg( "CoSendRequest (DESTROY_WINDOW) failed" );
426         return( 1 );
427     }
428
429     return( 0 );
430 }
431
432 /*****************************************************************************
433  * CoToggleFullscreen: toggle fullscreen 
434  *****************************************************************************
435  * Returns 0 on success, 1 otherwise
436  *****************************************************************************/
437 static int CoToggleFullscreen( vout_thread_t *p_vout )
438 {
439     QTDestroySequence( p_vout );
440
441     if( CoDestroyWindow( p_vout ) )
442     {
443         intf_ErrMsg( "vout error: unable to destroy window" );
444         return( 1 );
445     }
446     
447     p_vout->b_fullscreen = !p_vout->b_fullscreen;
448
449     if( CoCreateWindow( p_vout ) )
450     {
451         intf_ErrMsg( "vout error: unable to create window" );
452         return( 1 );
453     }
454
455     SetPort( p_vout->p_sys->p_qdport );
456     QTScaleMatrix( p_vout );
457
458     if( QTCreateSequence( p_vout ) )
459     {
460         intf_ErrMsg( "vout error: unable to create sequence" );
461         return( 1 ); 
462     } 
463
464     return( 0 );
465 }
466
467 /*****************************************************************************
468  * QTScaleMatrix: scale matrix 
469  *****************************************************************************/
470 static void QTScaleMatrix( vout_thread_t *p_vout )
471 {
472     Rect s_rect;
473     int i_width, i_height;
474     Fixed factor_x, factor_y;
475     int i_offset_x = 0;
476     int i_offset_y = 0;
477
478     GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
479
480     i_width = s_rect.right - s_rect.left;
481     i_height = s_rect.bottom - s_rect.top;
482
483     if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
484     {
485         int i_adj_width = i_height * p_vout->output.i_aspect /
486                           VOUT_ASPECT_FACTOR;
487
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 ) );
492
493         i_offset_x = (i_width - i_adj_width) / 2;
494     }
495     else
496     {
497         int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
498                            p_vout->output.i_aspect;
499
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 ) );
504
505         i_offset_y = (i_height - i_adj_height) / 2;
506     }
507
508     SetIdentityMatrix( p_vout->p_sys->p_matrix );
509
510     ScaleMatrix( p_vout->p_sys->p_matrix,
511                  factor_x, factor_y,
512                  Long2Fix(0), Long2Fix(0) );            
513
514     TranslateMatrix( p_vout->p_sys->p_matrix, 
515                      Long2Fix(i_offset_x), 
516                      Long2Fix(i_offset_y) );
517 }
518
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 )
525 {
526     OSErr err;
527     ImageDescriptionPtr p_descr;
528
529     HLock( (Handle)p_vout->p_sys->h_img_descr );
530     p_descr = *p_vout->p_sys->h_img_descr;
531
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;
545     p_descr->depth = 12;
546
547     HUnlock( (Handle)p_vout->p_sys->h_img_descr );
548
549     if( ( err = DecompressSequenceBeginS( 
550                               &p_vout->p_sys->i_seq,
551                               p_vout->p_sys->h_img_descr,
552                               NULL, 0,
553                               p_vout->p_sys->p_qdport,
554                               NULL, NULL,
555                               p_vout->p_sys->p_matrix,
556                               0, NULL,
557                               codecFlagUseImageBuffer,
558                               codecLosslessQuality,
559                               p_vout->p_sys->img_dc ) ) )
560     {
561         intf_ErrMsg( "DecompressSequenceBeginS failed: %d", err );
562         return( 1 );
563     }
564
565     return( 0 );
566 }
567
568 /*****************************************************************************
569  * QTDestroySequence: destroy sequence 
570  *****************************************************************************/
571 static void QTDestroySequence( vout_thread_t *p_vout )
572 {
573     CDSequenceEnd( p_vout->p_sys->i_seq );
574 }
575
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 )
582 {
583     int i_width  = p_vout->output.i_width;
584     int i_height = p_vout->output.i_height;
585
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 ) );
589
590     if( p_pic->p_sys == NULL )
591     {
592         return( -1 );
593     }
594
595     switch( p_vout->output.i_chroma )
596     {
597         case FOURCC_I420:
598
599             p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
600             p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
601
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 );
605
606             /* Y buffer */
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;
612
613             /* U buffer */
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;
619
620             /* V buffer */
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;
626
627             /* We allocated 3 planes */
628             p_pic->i_planes = 3;
629
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;
637
638             P.componentInfoY.rowBytes = i_width;
639             P.componentInfoCb.rowBytes = i_width / 2;
640             P.componentInfoCr.rowBytes = i_width / 2;
641 #undef P
642
643             break;
644
645     default:
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 );
651         p_pic->i_planes = 0;
652         return( -1 );
653     }
654
655     return( 0 );
656 }
657
658 /*****************************************************************************
659  * QTFreePicture: destroy a picture allocated with QTNewPicture
660  *****************************************************************************/
661 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
662 {
663     switch( p_vout->output.i_chroma )
664     {
665         case FOURCC_I420:
666             free( p_pic->p_data_orig );
667             break;
668     }
669
670     free( p_pic->p_sys );
671 }
672