]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
* src/video_output/vout_pictures.c: oups, inverted i_visible_lines and i_lines.
[vlc] / src / video_output / vout_pictures.c
1 /*****************************************************************************
2  * vout_pictures.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
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 <stdlib.h>                                                /* free() */
29 #include <stdio.h>                                              /* sprintf() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <vlc/vlc.h>
33
34 #include "vlc_video.h"
35 #include "video_output.h"
36
37 #include "vout_pictures.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static void CopyPicture( vout_thread_t *, picture_t *, picture_t * );
43
44 /**
45  * Display a picture
46  *
47  * Remove the reservation flag of a picture, which will cause it to be ready
48  * for display. The picture won't be displayed until vout_DatePicture has been
49  * called.
50  */
51 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
52 {
53     vlc_mutex_lock( &p_vout->picture_lock );
54     switch( p_pic->i_status )
55     {
56     case RESERVED_PICTURE:
57         p_pic->i_status = RESERVED_DISP_PICTURE;
58         break;
59     case RESERVED_DATED_PICTURE:
60         p_pic->i_status = READY_PICTURE;
61         break;
62     default:
63         msg_Err( p_vout, "picture to display %p has invalid status %d",
64                          p_pic, p_pic->i_status );
65         break;
66     }
67
68     vlc_mutex_unlock( &p_vout->picture_lock );
69 }
70
71 /**
72  * Date a picture
73  *
74  * Remove the reservation flag of a picture, which will cause it to be ready
75  * for display. The picture won't be displayed until vout_DisplayPicture has
76  * been called.
77  * \param p_vout The vout in question
78  * \param p_pic The picture to date
79  * \param date The date to display the picture
80  */
81 void vout_DatePicture( vout_thread_t *p_vout,
82                        picture_t *p_pic, mtime_t date )
83 {
84     vlc_mutex_lock( &p_vout->picture_lock );
85     p_pic->date = date;
86     switch( p_pic->i_status )
87     {
88     case RESERVED_PICTURE:
89         p_pic->i_status = RESERVED_DATED_PICTURE;
90         break;
91     case RESERVED_DISP_PICTURE:
92         p_pic->i_status = READY_PICTURE;
93         break;
94     default:
95         msg_Err( p_vout, "picture to date %p has invalid status %d",
96                          p_pic, p_pic->i_status );
97         break;
98     }
99
100     vlc_mutex_unlock( &p_vout->picture_lock );
101 }
102
103 /**
104  * Allocate a picture in the video output heap.
105  *
106  * This function creates a reserved image in the video output heap.
107  * A null pointer is returned if the function fails. This method provides an
108  * already allocated zone of memory in the picture data fields.
109  * It needs locking since several pictures can be created by several producers
110  * threads.
111  */
112 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
113                                vlc_bool_t b_progressive,
114                                vlc_bool_t b_top_field_first,
115                                unsigned int i_nb_fields )
116 {
117     int         i_pic;                                      /* picture index */
118     picture_t * p_pic;
119     picture_t * p_freepic = NULL;                      /* first free picture */
120
121     /* Get lock */
122     vlc_mutex_lock( &p_vout->picture_lock );
123
124     /*
125      * Look for an empty place in the picture heap.
126      */
127     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
128     {
129         p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
130                                  % I_RENDERPICTURES];
131
132         switch( p_pic->i_status )
133         {
134             case DESTROYED_PICTURE:
135                 /* Memory will not be reallocated, and function can end
136                  * immediately - this is the best possible case, since no
137                  * memory allocation needs to be done */
138                 p_pic->i_status   = RESERVED_PICTURE;
139                 p_pic->i_refcount = 0;
140                 p_pic->b_force    = 0;
141
142                 p_pic->b_progressive        = b_progressive;
143                 p_pic->i_nb_fields          = i_nb_fields;
144                 p_pic->b_top_field_first    = b_top_field_first;
145
146                 p_vout->i_heap_size++;
147                 p_vout->render.i_last_used_pic =
148                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
149                     % I_RENDERPICTURES;
150                 vlc_mutex_unlock( &p_vout->picture_lock );
151                 return( p_pic );
152
153             case FREE_PICTURE:
154                 /* Picture is empty and ready for allocation */
155                 p_vout->render.i_last_used_pic =
156                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
157                     % I_RENDERPICTURES;
158                 p_freepic = p_pic;
159                 break;
160
161             default:
162                 break;
163         }
164     }
165
166     /*
167      * Prepare picture
168      */
169     if( p_freepic != NULL )
170     {
171         vout_AllocatePicture( VLC_OBJECT(p_vout),
172                               p_freepic, p_vout->render.i_chroma,
173                               p_vout->render.i_width, p_vout->render.i_height,
174                               p_vout->render.i_aspect );
175
176         if( p_freepic->i_planes )
177         {
178             /* Copy picture information, set some default values */
179             p_freepic->i_status   = RESERVED_PICTURE;
180             p_freepic->i_type     = MEMORY_PICTURE;
181             p_freepic->b_slow     = 0;
182
183             p_freepic->i_refcount = 0;
184             p_freepic->b_force = 0;
185
186             p_freepic->b_progressive        = b_progressive;
187             p_freepic->i_nb_fields          = i_nb_fields;
188             p_freepic->b_top_field_first    = b_top_field_first;
189
190             p_freepic->i_matrix_coefficients = 1;
191
192             p_vout->i_heap_size++;
193         }
194         else
195         {
196             /* Memory allocation failed : set picture as empty */
197             p_freepic->i_status = FREE_PICTURE;
198             p_freepic = NULL;
199
200             msg_Err( p_vout, "picture allocation failed" );
201         }
202
203         vlc_mutex_unlock( &p_vout->picture_lock );
204
205         return( p_freepic );
206     }
207
208     /* No free or destroyed picture could be found, but the decoder
209      * will try again in a while. */
210     vlc_mutex_unlock( &p_vout->picture_lock );
211
212     return( NULL );
213 }
214
215 /**
216  * Remove a permanent or reserved picture from the heap
217  *
218  * This function frees a previously reserved picture or a permanent
219  * picture. It is meant to be used when the construction of a picture aborted.
220  * Note that the picture will be destroyed even if it is linked !
221  */
222 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
223 {
224     vlc_mutex_lock( &p_vout->picture_lock );
225
226 #ifdef DEBUG
227     /* Check if picture status is valid */
228     if( (p_pic->i_status != RESERVED_PICTURE) &&
229         (p_pic->i_status != RESERVED_DATED_PICTURE) &&
230         (p_pic->i_status != RESERVED_DISP_PICTURE) )
231     {
232         msg_Err( p_vout, "picture to destroy %p has invalid status %d",
233                          p_pic, p_pic->i_status );
234     }
235 #endif
236
237     p_pic->i_status = DESTROYED_PICTURE;
238     p_vout->i_heap_size--;
239
240     vlc_mutex_unlock( &p_vout->picture_lock );
241 }
242
243 /**
244  * Increment reference counter of a picture
245  *
246  * This function increments the reference counter of a picture in the video
247  * heap. It needs a lock since several producer threads can access the picture.
248  */
249 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
250 {
251     vlc_mutex_lock( &p_vout->picture_lock );
252     p_pic->i_refcount++;
253     vlc_mutex_unlock( &p_vout->picture_lock );
254 }
255
256 /**
257  * Decrement reference counter of a picture
258  *
259  * This function decrement the reference counter of a picture in the video heap
260  */
261 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
262 {
263     vlc_mutex_lock( &p_vout->picture_lock );
264     p_pic->i_refcount--;
265
266     if( p_pic->i_refcount < 0 )
267     {
268         msg_Err( p_vout, "picture %p refcount is %i",
269                  p_pic, p_pic->i_refcount );
270         p_pic->i_refcount = 0;
271     }
272
273     if( ( p_pic->i_refcount == 0 ) &&
274         ( p_pic->i_status == DISPLAYED_PICTURE ) )
275     {
276         p_pic->i_status = DESTROYED_PICTURE;
277         p_vout->i_heap_size--;
278     }
279
280     vlc_mutex_unlock( &p_vout->picture_lock );
281 }
282
283 /**
284  * Render a picture
285  *
286  * This function chooses whether the current picture needs to be copied
287  * before rendering, does the subpicture magic, and tells the video output
288  * thread which direct buffer needs to be displayed.
289  */
290 picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
291                                                        subpicture_t *p_subpic )
292 {
293     if( p_pic == NULL )
294     {
295         /* XXX: subtitles */
296
297         return NULL;
298     }
299
300     if( p_pic->i_type == DIRECT_PICTURE )
301     {
302         if( !p_vout->render.b_allow_modify_pics || p_pic->i_refcount ||
303             p_pic->b_force )
304         {
305             /* Picture is in a direct buffer and is still in use,
306              * we need to copy it to another direct buffer before
307              * displaying it if there are subtitles. */
308             if( p_subpic != NULL )
309             {
310                 /* We have subtitles. First copy the picture to
311                  * the spare direct buffer, then render the
312                  * subtitles. */
313                 CopyPicture( p_vout, p_pic, PP_OUTPUTPICTURE[0] );
314
315                 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0],
316                                         p_pic , p_subpic );
317
318                 return PP_OUTPUTPICTURE[0];
319             }
320
321             /* No subtitles, picture is in a directbuffer so
322              * we can display it directly even if it is still
323              * in use. */
324             return p_pic;
325         }
326
327         /* Picture is in a direct buffer but isn't used by the
328          * decoder. We can safely render subtitles on it and
329          * display it. */
330         vout_RenderSubPictures( p_vout, p_pic, p_pic, p_subpic );
331
332         return p_pic;
333     }
334
335     /* Not a direct buffer. We either need to copy it to a direct buffer,
336      * or render it if the chroma isn't the same. */
337     if( p_vout->b_direct )
338     {
339         /* Picture is not in a direct buffer, but is exactly the
340          * same size as the direct buffers. A memcpy() is enough,
341          * then render the subtitles. */
342
343         if( PP_OUTPUTPICTURE[0]->pf_lock )
344             if( PP_OUTPUTPICTURE[0]->pf_lock( p_vout, PP_OUTPUTPICTURE[0] ) )
345             {
346                 if( PP_OUTPUTPICTURE[0]->pf_unlock )
347                 PP_OUTPUTPICTURE[0]->pf_unlock( p_vout, PP_OUTPUTPICTURE[0] );
348
349                 return NULL;
350             }
351
352         CopyPicture( p_vout, p_pic, PP_OUTPUTPICTURE[0] );
353         vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_pic, p_subpic );
354
355         if( PP_OUTPUTPICTURE[0]->pf_unlock )
356             PP_OUTPUTPICTURE[0]->pf_unlock( p_vout, PP_OUTPUTPICTURE[0] );
357
358         return PP_OUTPUTPICTURE[0];
359     }
360
361     /* Picture is not in a direct buffer, and needs to be converted to
362      * another size/chroma. Then the subtitles need to be rendered as
363      * well. This usually means software YUV, or hardware YUV with a
364      * different chroma. */
365
366     if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
367     {
368         /* The picture buffer is in slow memory. We'll use
369          * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
370          * one for subpictures rendering. */
371         picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
372         if( p_tmp_pic->i_status == FREE_PICTURE )
373         {
374             vout_AllocatePicture( VLC_OBJECT(p_vout),
375                                   p_tmp_pic, p_vout->output.i_chroma,
376                                   p_vout->output.i_width,
377                                   p_vout->output.i_height,
378                                   p_vout->output.i_aspect );
379             p_tmp_pic->i_type = MEMORY_PICTURE;
380             p_tmp_pic->i_status = RESERVED_PICTURE;
381         }
382
383         /* Convert image to the first direct buffer */
384         p_vout->chroma.pf_convert( p_vout, p_pic, p_tmp_pic );
385
386         /* Render subpictures on the first direct buffer */
387         vout_RenderSubPictures( p_vout, p_tmp_pic, p_tmp_pic, p_subpic );
388
389         if( p_vout->p_picture[0].pf_lock )
390             if( p_vout->p_picture[0].pf_lock( p_vout, &p_vout->p_picture[0] ) )
391                 return NULL;
392
393         CopyPicture( p_vout, p_tmp_pic, &p_vout->p_picture[0] );
394     }
395     else
396     {
397         if( p_vout->p_picture[0].pf_lock )
398             if( p_vout->p_picture[0].pf_lock( p_vout, &p_vout->p_picture[0] ) )
399                 return NULL;
400
401         /* Convert image to the first direct buffer */
402         p_vout->chroma.pf_convert( p_vout, p_pic, &p_vout->p_picture[0] );
403
404         /* Render subpictures on the first direct buffer */
405         vout_RenderSubPictures( p_vout, &p_vout->p_picture[0],
406                                 &p_vout->p_picture[0], p_subpic );
407     }
408
409     if( p_vout->p_picture[0].pf_unlock )
410         p_vout->p_picture[0].pf_unlock( p_vout, &p_vout->p_picture[0] );
411
412     return &p_vout->p_picture[0];
413 }
414
415 /**
416  * Calculate image window coordinates
417  *
418  * This function will be accessed by plugins. It calculates the relative
419  * position of the output window and the image window.
420  */
421 void vout_PlacePicture( vout_thread_t *p_vout,
422                         unsigned int i_width, unsigned int i_height,
423                         unsigned int *pi_x, unsigned int *pi_y,
424                         unsigned int *pi_width, unsigned int *pi_height )
425 {
426     if( (i_width <= 0) || (i_height <=0) )
427     {
428         *pi_width = *pi_height = *pi_x = *pi_y = 0;
429
430         return;
431     }
432
433     if( p_vout->b_scale )
434     {
435         *pi_width = i_width;
436         *pi_height = i_height;
437     }
438     else
439     {
440         *pi_width = __MIN( i_width, p_vout->render.i_width );
441         *pi_height = __MIN( i_height, p_vout->render.i_height );
442     }
443
444     if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
445     {
446         *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
447     }
448     else
449     {
450         *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
451     }
452
453     if( *pi_width > i_width )
454     {
455         *pi_width = i_width;
456         *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
457     }
458
459     if( *pi_height > i_height )
460     {
461         *pi_height = i_height;
462         *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
463     }
464
465     switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
466     {
467     case VOUT_ALIGN_LEFT:
468         *pi_x = 0;
469         break;
470     case VOUT_ALIGN_RIGHT:
471         *pi_x = i_width - *pi_width;
472         break;
473     default:
474         *pi_x = ( i_width - *pi_width ) / 2;
475     }
476
477     switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
478     {
479     case VOUT_ALIGN_TOP:
480         *pi_y = 0;
481         break;
482     case VOUT_ALIGN_BOTTOM:
483         *pi_y = i_height - *pi_height;
484         break;
485     default:
486         *pi_y = ( i_height - *pi_height ) / 2;
487     }
488 }
489
490 /**
491  * Allocate a new picture in the heap.
492  *
493  * This function allocates a fake direct buffer in memory, which can be
494  * used exactly like a video buffer. The video output thread then manages
495  * how it gets displayed.
496  */
497 void vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
498                            vlc_fourcc_t i_chroma,
499                            int i_width, int i_height, int i_aspect )
500 {
501     int i_bytes, i_index, i_width_aligned, i_height_aligned;
502
503     /* Make sure the real dimensions are a multiple of 16 */
504     i_width_aligned = (i_width + 15) >> 4 << 4;
505     i_height_aligned = (i_height + 15) >> 4 << 4;
506
507     vout_InitPicture( p_this, p_pic, i_chroma,
508                       i_width, i_height, i_aspect );
509
510     /* Calculate how big the new image should be */
511     i_bytes = p_pic->format.i_bits_per_pixel *
512         i_width_aligned * i_height_aligned / 8;
513
514     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
515
516     if( p_pic->p_data == NULL )
517     {
518         p_pic->i_planes = 0;
519         return;
520     }
521
522     /* Fill the p_pixels field for each plane */
523     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
524
525     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
526     {
527         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
528             p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
529     }
530 }
531
532 /**
533  * Initialise the video format fields given chroma/size.
534  *
535  * This function initializes all the video_frame_format_t fields given the
536  * static properties of a picture (chroma and size).
537  * \param p_format Pointer to the format structure to initialize
538  * \param i_chroma Chroma to set
539  * \param i_width Width to set
540  * \param i_height Height to set
541  * \param i_aspect Aspect ratio
542  */
543 void vout_InitFormat( video_frame_format_t *p_format, vlc_fourcc_t i_chroma,
544                       int i_width, int i_height, int i_aspect )
545 {
546     p_format->i_chroma   = i_chroma;
547     p_format->i_width    = p_format->i_visible_width  = i_width;
548     p_format->i_height   = p_format->i_visible_height = i_height;
549     p_format->i_x_offset = p_format->i_y_offset = 0;
550     p_format->i_aspect   = i_aspect;
551
552 #if 0
553     /* Assume we have square pixels */
554     if( i_width && i_height )
555         p_format->i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
556     else
557         p_format->i_aspect = 0;
558 #endif
559
560     switch( i_chroma )
561     {
562         case FOURCC_YUVA:
563             p_format->i_bits_per_pixel = 32;
564             break;
565         case FOURCC_I444:
566             p_format->i_bits_per_pixel = 24;
567             break;
568         case FOURCC_I422:
569         case FOURCC_YUY2:
570         case FOURCC_UYVY:
571             p_format->i_bits_per_pixel = 16;
572             p_format->i_bits_per_pixel = 16;
573             break;
574         case FOURCC_I411:
575         case FOURCC_YV12:
576         case FOURCC_I420:
577         case FOURCC_IYUV:
578             p_format->i_bits_per_pixel = 12;
579             break;
580         case FOURCC_I410:
581         case FOURCC_YVU9:
582             p_format->i_bits_per_pixel = 9;
583             break;
584         case FOURCC_Y211:
585             p_format->i_bits_per_pixel = 8;
586             break;
587         case FOURCC_YUVP:
588             p_format->i_bits_per_pixel = 8;
589             break;
590
591         case FOURCC_RV32:
592             p_format->i_bits_per_pixel = 32;
593             break;
594         case FOURCC_RV24:
595             /* FIXME: Should be 24 here but x11 and our chroma conversion
596              * routines assume 32. */
597 #ifdef WIN32
598             p_format->i_bits_per_pixel = 24;
599 #else
600             p_format->i_bits_per_pixel = 32;
601 #endif
602             break;
603         case FOURCC_RV15:
604         case FOURCC_RV16:
605             p_format->i_bits_per_pixel = 16;
606             break;
607         case FOURCC_RGB2:
608             p_format->i_bits_per_pixel = 8;
609             break;
610         default:
611             p_format->i_bits_per_pixel = 0;
612             break;
613     }
614 }
615
616 /**
617  * Initialise the picture_t fields given chroma/size.
618  *
619  * This function initializes most of the picture_t fields given a chroma and
620  * size. It makes the assumption that stride == width.
621  * \param p_this The calling object
622  * \param p_pic Pointer to the picture to initialize
623  * \param i_chroma The chroma fourcc to set
624  * \param i_width The width of the picture
625  * \param i_height The height of the picture
626  * \param i_aspect The aspect ratio of the picture
627  */
628 void vout_InitPicture( vlc_object_t *p_this, picture_t *p_pic,
629                        vlc_fourcc_t i_chroma,
630                        int i_width, int i_height, int i_aspect )
631 {
632     int i_index, i_width_aligned, i_height_aligned;
633
634     /* Store default values */
635     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
636     {
637         p_pic->p[i_index].p_pixels = NULL;
638         p_pic->p[i_index].i_pixel_pitch = 1;
639     }
640
641     p_pic->pf_release = 0;
642     p_pic->pf_lock = 0;
643     p_pic->pf_unlock = 0;
644     p_pic->i_refcount = 0;
645
646     vout_InitFormat( &p_pic->format, i_chroma, i_width, i_height, i_aspect );
647
648     /* Make sure the real dimensions are a multiple of 16 */
649     i_width_aligned = (i_width + 15) >> 4 << 4;
650     i_height_aligned = (i_height + 15) >> 4 << 4;
651
652     /* Calculate coordinates */
653     switch( i_chroma )
654     {
655         case FOURCC_I411:
656             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
657             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
658             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
659             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
660             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
661             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
662             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
663             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
664             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
665             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
666             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
667             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
668             p_pic->i_planes = 3;
669             break;
670
671         case FOURCC_I410:
672         case FOURCC_YVU9:
673             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
674             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
675             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
676             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
677             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 4;
678             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 4;
679             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
680             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
681             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 4;
682             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 4;
683             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
684             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
685             p_pic->i_planes = 3;
686             break;
687
688         case FOURCC_YV12:
689         case FOURCC_I420:
690         case FOURCC_IYUV:
691             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
692             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
693             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
694             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
695             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 2;
696             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 2;
697             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
698             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
699             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 2;
700             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 2;
701             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
702             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
703             p_pic->i_planes = 3;
704             break;
705
706         case FOURCC_I422:
707             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
708             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
709             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
710             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
711             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
712             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
713             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
714             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
715             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
716             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
717             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
718             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
719             p_pic->i_planes = 3;
720             break;
721
722         case FOURCC_I444:
723             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
724             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
725             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
726             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
727             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
728             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
729             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
730             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
731             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
732             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
733             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
734             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
735             p_pic->i_planes = 3;
736             break;
737
738         case FOURCC_YUVA:
739             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
740             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
741             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
742             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
743             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
744             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
745             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
746             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
747             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
748             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
749             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
750             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
751             p_pic->p[ A_PLANE ].i_lines = i_height_aligned;
752             p_pic->p[ A_PLANE ].i_visible_lines = i_height;
753             p_pic->p[ A_PLANE ].i_pitch = i_width_aligned;
754             p_pic->p[ A_PLANE ].i_visible_pitch = i_width;
755             p_pic->i_planes = 4;
756             break;
757
758         case FOURCC_YUVP:
759             p_pic->p->i_lines = i_height_aligned;
760             p_pic->p->i_visible_lines = i_height;
761             p_pic->p->i_pitch = i_width_aligned;
762             p_pic->p->i_visible_pitch = i_width;
763             p_pic->p->i_pixel_pitch = 8;
764             p_pic->i_planes = 1;
765             break;
766
767         case FOURCC_Y211:
768             p_pic->p->i_lines = i_height_aligned;
769             p_pic->p->i_visible_lines = i_height;
770             p_pic->p->i_pitch = i_width_aligned;
771             p_pic->p->i_visible_pitch = i_width;
772             p_pic->p->i_pixel_pitch = 4;
773             p_pic->i_planes = 1;
774             break;
775
776         case FOURCC_UYVY:
777         case FOURCC_YUY2:
778             p_pic->p->i_lines = i_height_aligned;
779             p_pic->p->i_visible_lines = i_height;
780             p_pic->p->i_pitch = i_width_aligned * 2;
781             p_pic->p->i_visible_pitch = i_width * 2;
782             p_pic->p->i_pixel_pitch = 4;
783             p_pic->i_planes = 1;
784             break;
785
786         case FOURCC_RGB2:
787             p_pic->p->i_lines = i_height_aligned;
788             p_pic->p->i_visible_lines = i_height;
789             p_pic->p->i_pitch = i_width_aligned;
790             p_pic->p->i_visible_pitch = i_width;
791             p_pic->p->i_pixel_pitch = 1;
792             p_pic->i_planes = 1;
793             break;
794
795         case FOURCC_RV15:
796             p_pic->p->i_lines = i_height_aligned;
797             p_pic->p->i_visible_lines = i_height;
798             p_pic->p->i_pitch = i_width_aligned * 2;
799             p_pic->p->i_visible_pitch = i_width * 2;
800             p_pic->p->i_pixel_pitch = 2;
801             p_pic->i_planes = 1;
802             break;
803
804         case FOURCC_RV16:
805             p_pic->p->i_lines = i_height_aligned;
806             p_pic->p->i_visible_lines = i_height;
807             p_pic->p->i_pitch = i_width_aligned * 2;
808             p_pic->p->i_visible_pitch = i_width * 2;
809             p_pic->p->i_pixel_pitch = 2;
810             p_pic->i_planes = 1;
811             break;
812
813         case FOURCC_RV24:
814             p_pic->p->i_lines = i_height_aligned;
815             p_pic->p->i_visible_lines = i_height;
816             p_pic->p->i_pitch = i_width_aligned * 3;
817             p_pic->p->i_visible_pitch = i_width * 3;
818             p_pic->p->i_pixel_pitch = 3;
819             p_pic->i_planes = 1;
820             break;
821
822         case FOURCC_RV32:
823             p_pic->p->i_lines = i_height_aligned;
824             p_pic->p->i_visible_lines = i_height;
825             p_pic->p->i_pitch = i_width_aligned * 4;
826             p_pic->p->i_visible_pitch = i_width * 4;
827             p_pic->p->i_pixel_pitch = 4;
828             p_pic->i_planes = 1;
829             break;
830
831         default:
832             msg_Err( p_this, "unknown chroma type 0x%.8x (%4.4s)",
833                              i_chroma, (char*)&i_chroma );
834             p_pic->i_planes = 0;
835             return;
836     }
837 }
838
839 /**
840  * Compare two chroma values
841  *
842  * This function returns 1 if the two fourcc values given as argument are
843  * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
844  */
845 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
846 {
847     /* If they are the same, they are the same ! */
848     if( i_chroma == i_amorhc )
849     {
850         return 1;
851     }
852
853     /* Check for equivalence classes */
854     switch( i_chroma )
855     {
856         case FOURCC_I420:
857         case FOURCC_IYUV:
858         case FOURCC_YV12:
859             switch( i_amorhc )
860             {
861                 case FOURCC_I420:
862                 case FOURCC_IYUV:
863                 case FOURCC_YV12:
864                     return 1;
865
866                 default:
867                     return 0;
868             }
869
870         case FOURCC_UYVY:
871         case FOURCC_UYNV:
872         case FOURCC_Y422:
873             switch( i_amorhc )
874             {
875                 case FOURCC_UYVY:
876                 case FOURCC_UYNV:
877                 case FOURCC_Y422:
878                     return 1;
879
880                 default:
881                     return 0;
882             }
883
884         case FOURCC_YUY2:
885         case FOURCC_YUNV:
886             switch( i_amorhc )
887             {
888                 case FOURCC_YUY2:
889                 case FOURCC_YUNV:
890                     return 1;
891
892                 default:
893                     return 0;
894             }
895
896         default:
897             return 0;
898     }
899 }
900
901 /* Following functions are local */
902
903 /*****************************************************************************
904  * CopyPicture: copy a picture to another one
905  *****************************************************************************
906  * This function takes advantage of the image format, and reduces the
907  * number of calls to memcpy() to the minimum. Source and destination
908  * images must have same width (hence i_visible_pitch), height, and chroma.
909  *****************************************************************************/
910 static void CopyPicture( vout_thread_t * p_vout,
911                          picture_t *p_src, picture_t *p_dest )
912 {
913     int i;
914
915     for( i = 0; i < p_src->i_planes ; i++ )
916     {
917         if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
918         {
919             /* There are margins, but with the same width : perfect ! */
920             p_vout->p_vlc->pf_memcpy(
921                          p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
922                          p_src->p[i].i_pitch * p_src->p[i].i_visible_lines );
923         }
924         else
925         {
926             /* We need to proceed line by line */
927             uint8_t *p_in = p_src->p[i].p_pixels;
928             uint8_t *p_out = p_dest->p[i].p_pixels;
929             int i_line;
930
931             for( i_line = p_src->p[i].i_visible_lines; i_line--; )
932             {
933                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
934                                           p_src->p[i].i_visible_pitch );
935                 p_in += p_src->p[i].i_pitch;
936                 p_out += p_dest->p[i].i_pitch;
937             }
938         }
939     }
940     p_dest->date = p_src->date;
941 }