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