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