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