]> git.sesse.net Git - vlc/blob - plugins/macosx/vout_macosx.m
* ALL: the first libvlc commit.
[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 "macosx.h"
33
34 #define QT_MAX_DIRECTBUFFERS 10
35
36 struct picture_sys_s
37 {
38     void *p_info;
39     unsigned int i_size;
40
41     /* When using I420 output */
42     PlanarPixmapInfoYUV420 pixmap_i420;
43 };
44
45 /*****************************************************************************
46  * Local prototypes
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 * );
55
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 * );
60
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 * );
66
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 )
72 {
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;
80 }
81
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 )
88 {
89     OSErr err;
90
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 )
93     {
94         msg_Err( p_vout, "MacOS X interface module required" );
95         return( 1 );
96     }
97
98     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
99     if( p_vout->p_sys == NULL )
100     {
101         msg_Err( p_vout, "out of memory" );
102         return( 1 );
103     }
104
105     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
106
107     p_vout->p_sys->h_img_descr = 
108         (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
109     p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
110
111     p_vout->p_sys->b_mouse_pointer_visible = 1;
112
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;
116
117     if( ( err = EnterMovies() ) != noErr )
118     {
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 );
123         return( 1 );
124     } 
125
126     if( vout_ChromaCmp( p_vout->render.i_chroma, FOURCC_I420 ) )
127     {
128         err = FindCodec( kYUV420CodecType, bestSpeedCodec,
129                          nil, &p_vout->p_sys->img_dc );
130         if( err == noErr && p_vout->p_sys->img_dc != 0 )
131         {
132             p_vout->output.i_chroma = FOURCC_I420;
133             p_vout->p_sys->i_codec = kYUV420CodecType;
134         }
135         else
136         {
137             msg_Err( p_vout, "failed to find an appropriate codec" );
138         }
139     }
140     else
141     {
142         msg_Err( p_vout, "chroma 0x%08x not supported",
143                          p_vout->render.i_chroma );
144     }
145
146     if( p_vout->p_sys->img_dc == 0 )
147     {
148         free( p_vout->p_sys->p_matrix );
149         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
150         free( p_vout->p_sys );
151         return( 1 );        
152     }
153
154     if( CoCreateWindow( p_vout ) )
155     {
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 ); 
160         return( 1 );
161     }
162
163     return( 0 );
164 }
165
166 /*****************************************************************************
167  * vout_Init: initialize video thread output method
168  *****************************************************************************/
169 static int vout_Init( vout_thread_t *p_vout )
170 {
171     int i_index;
172     picture_t *p_pic;
173
174     I_OUTPUTPICTURES = 0;
175
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;
182
183     SetPort( p_vout->p_sys->p_qdport );
184     QTScaleMatrix( p_vout );
185
186     if( QTCreateSequence( p_vout ) )
187     {
188         msg_Err( p_vout, "unable to create sequence" );
189         return( 1 );
190     }
191
192     /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
193     while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
194     {
195         p_pic = NULL;
196
197         /* Find an empty picture slot */
198         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
199         {
200             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
201             {
202                 p_pic = p_vout->p_picture + i_index;
203                 break;
204             }
205         }
206
207         /* Allocate the picture */
208         if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
209         {
210             break;
211         }
212
213         p_pic->i_status = DESTROYED_PICTURE;
214         p_pic->i_type   = DIRECT_PICTURE;
215
216         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
217
218         I_OUTPUTPICTURES++;
219     }
220
221     return( 0 );
222 }
223
224 /*****************************************************************************
225  * vout_End: terminate video thread output method
226  *****************************************************************************/
227 static void vout_End( vout_thread_t *p_vout )
228 {
229     int i_index;
230
231     QTDestroySequence( p_vout );
232
233     /* Free the direct buffers we allocated */
234     for( i_index = I_OUTPUTPICTURES; i_index; )
235     {
236         i_index--;
237         QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
238     }
239 }
240
241 /*****************************************************************************
242  * vout_Destroy: destroy video thread output method
243  *****************************************************************************/
244 static void vout_Destroy( vout_thread_t *p_vout )
245 {
246     if( CoDestroyWindow( p_vout ) )
247     {
248         msg_Err( p_vout, "unable to destroy window" );
249     }
250
251     ExitMovies();
252
253     free( p_vout->p_sys->p_matrix );
254     DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
255     free( p_vout->p_sys );
256 }
257
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 )
265 {    
266     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
267     {
268         if( CoToggleFullscreen( p_vout ) )  
269         {
270             return( 1 );
271         }
272
273         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
274     }
275
276     if( p_vout->i_changes & VOUT_SIZE_CHANGE ) 
277     {
278         QTScaleMatrix( p_vout );
279         SetDSequenceMatrix( p_vout->p_sys->i_seq, 
280                             p_vout->p_sys->p_matrix );
281  
282         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
283     }
284
285     /* hide/show mouse cursor */
286     if( p_vout->p_sys->b_mouse_moved ||
287         p_vout->p_sys->i_time_mouse_last_moved )
288     {
289         vlc_bool_t b_change = 0;
290
291         if( !p_vout->p_sys->b_mouse_pointer_visible )
292         {
293             CGDisplayShowCursor( kCGDirectMainDisplay );
294             b_change = 1;
295         }
296 #if 0
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 )
300         {
301             CGDisplayHideCursor( kCGDirectMainDisplay );
302             b_change = 1;
303         }
304 #endif
305
306         if( b_change )
307         {
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;
312         }
313     }
314
315     return( 0 );
316 }
317
318 /*****************************************************************************
319  * vout_Render: render previously calculated output
320  *****************************************************************************/
321 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
322 {
323     ;
324 }
325
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 )
332 {
333     OSErr err;
334     CodecFlags flags;
335
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 ) )
341     {
342         msg_Err( p_vout, "DecompressSequenceFrameS failed: %d", err );
343     }
344 }
345
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 )
352 {
353     NSArray *o_array;
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];
359
360     memset( &req, 0, sizeof(req) );
361     req.i_type = i_request;
362     req.p_vout = p_vout;
363
364     req.o_lock = [[NSConditionLock alloc] initWithCondition: 0];
365
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];
372
373     [o_msg sendBeforeDate: [NSDate distantPast]];
374     [req.o_lock lockWhenCondition: 1];
375     [req.o_lock unlock];
376
377     [o_msg release];
378     [req.o_lock release];
379
380     [recvPort release];
381     [o_pool release];
382
383     return( !req.i_result );
384 }
385
386 /*****************************************************************************
387  * CoCreateWindow: create new window 
388  *****************************************************************************
389  * Returns 0 on success, 1 otherwise
390  *****************************************************************************/
391 static int CoCreateWindow( vout_thread_t *p_vout )
392 {
393     if( CoSendRequest( p_vout, VOUT_REQ_CREATE_WINDOW ) )
394     {
395         msg_Err( p_vout, "CoSendRequest (CREATE_WINDOW) failed" );
396         return( 1 );
397     }
398
399     return( 0 );
400 }
401
402 /*****************************************************************************
403  * CoDestroyWindow: destroy window 
404  *****************************************************************************
405  * Returns 0 on success, 1 otherwise
406  *****************************************************************************/
407 static int CoDestroyWindow( vout_thread_t *p_vout )
408 {
409     if( !p_vout->p_sys->b_mouse_pointer_visible )
410     {
411         CGDisplayShowCursor( kCGDirectMainDisplay );
412         p_vout->p_sys->b_mouse_pointer_visible = 1;
413     }
414
415     if( CoSendRequest( p_vout, VOUT_REQ_DESTROY_WINDOW ) )
416     {
417         msg_Err( p_vout, "CoSendRequest (DESTROY_WINDOW) failed" );
418         return( 1 );
419     }
420
421     return( 0 );
422 }
423
424 /*****************************************************************************
425  * CoToggleFullscreen: toggle fullscreen 
426  *****************************************************************************
427  * Returns 0 on success, 1 otherwise
428  *****************************************************************************/
429 static int CoToggleFullscreen( vout_thread_t *p_vout )
430 {
431     QTDestroySequence( p_vout );
432
433     if( CoDestroyWindow( p_vout ) )
434     {
435         msg_Err( p_vout, "unable to destroy window" );
436         return( 1 );
437     }
438     
439     p_vout->b_fullscreen = !p_vout->b_fullscreen;
440
441     if( CoCreateWindow( p_vout ) )
442     {
443         msg_Err( p_vout, "unable to create window" );
444         return( 1 );
445     }
446
447     SetPort( p_vout->p_sys->p_qdport );
448     QTScaleMatrix( p_vout );
449
450     if( QTCreateSequence( p_vout ) )
451     {
452         msg_Err( p_vout, "unable to create sequence" );
453         return( 1 ); 
454     } 
455
456     return( 0 );
457 }
458
459 /*****************************************************************************
460  * QTScaleMatrix: scale matrix 
461  *****************************************************************************/
462 static void QTScaleMatrix( vout_thread_t *p_vout )
463 {
464     Rect s_rect;
465     int i_width, i_height;
466     Fixed factor_x, factor_y;
467     int i_offset_x = 0;
468     int i_offset_y = 0;
469
470     GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
471
472     i_width = s_rect.right - s_rect.left;
473     i_height = s_rect.bottom - s_rect.top;
474
475     if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
476     {
477         int i_adj_width = i_height * p_vout->output.i_aspect /
478                           VOUT_ASPECT_FACTOR;
479
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 ) );
484
485         i_offset_x = (i_width - i_adj_width) / 2;
486     }
487     else
488     {
489         int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
490                            p_vout->output.i_aspect;
491
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 ) );
496
497         i_offset_y = (i_height - i_adj_height) / 2;
498     }
499
500     SetIdentityMatrix( p_vout->p_sys->p_matrix );
501
502     ScaleMatrix( p_vout->p_sys->p_matrix,
503                  factor_x, factor_y,
504                  Long2Fix(0), Long2Fix(0) );            
505
506     TranslateMatrix( p_vout->p_sys->p_matrix, 
507                      Long2Fix(i_offset_x), 
508                      Long2Fix(i_offset_y) );
509 }
510
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 )
517 {
518     OSErr err;
519     ImageDescriptionPtr p_descr;
520
521     HLock( (Handle)p_vout->p_sys->h_img_descr );
522     p_descr = *p_vout->p_sys->h_img_descr;
523
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;
537     p_descr->depth = 12;
538
539     HUnlock( (Handle)p_vout->p_sys->h_img_descr );
540
541     if( ( err = DecompressSequenceBeginS( 
542                               &p_vout->p_sys->i_seq,
543                               p_vout->p_sys->h_img_descr,
544                               NULL, 0,
545                               p_vout->p_sys->p_qdport,
546                               NULL, NULL,
547                               p_vout->p_sys->p_matrix,
548                               0, NULL,
549                               codecFlagUseImageBuffer,
550                               codecLosslessQuality,
551                               p_vout->p_sys->img_dc ) ) )
552     {
553         msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
554         return( 1 );
555     }
556
557     return( 0 );
558 }
559
560 /*****************************************************************************
561  * QTDestroySequence: destroy sequence 
562  *****************************************************************************/
563 static void QTDestroySequence( vout_thread_t *p_vout )
564 {
565     CDSequenceEnd( p_vout->p_sys->i_seq );
566 }
567
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 )
574 {
575     int i_width  = p_vout->output.i_width;
576     int i_height = p_vout->output.i_height;
577
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 ) );
581
582     if( p_pic->p_sys == NULL )
583     {
584         return( -1 );
585     }
586
587     switch( p_vout->output.i_chroma )
588     {
589         case FOURCC_I420:
590
591             p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
592             p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
593
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 );
597
598             /* Y buffer */
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;
604
605             /* U buffer */
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;
611
612             /* V buffer */
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;
618
619             /* We allocated 3 planes */
620             p_pic->i_planes = 3;
621
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;
629
630             P.componentInfoY.rowBytes = i_width;
631             P.componentInfoCb.rowBytes = i_width / 2;
632             P.componentInfoCr.rowBytes = i_width / 2;
633 #undef P
634
635             break;
636
637     default:
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 );
642         p_pic->i_planes = 0;
643         return( -1 );
644     }
645
646     return( 0 );
647 }
648
649 /*****************************************************************************
650  * QTFreePicture: destroy a picture allocated with QTNewPicture
651  *****************************************************************************/
652 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
653 {
654     switch( p_vout->output.i_chroma )
655     {
656         case FOURCC_I420:
657             free( p_pic->p_data_orig );
658             break;
659     }
660
661     free( p_pic->p_sys );
662 }
663