a7ff57fbfb1b45736ce42613e09805f2281decc9
[vlc.git] / src / video_output / vout_pictures.c
1 /*****************************************************************************
2  * vout_pictures.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                                /* free() */
29 #include <stdio.h>                                              /* sprintf() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <vlc/vlc.h>
33
34 #include "vlc_video.h"
35 #include "video_output.h"
36 #include "vlc_spu.h"
37
38 #include "vout_pictures.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                                vlc_bool_t b_progressive,
110                                vlc_bool_t 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 #ifdef DEBUG
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     {
264         msg_Err( p_vout, "picture %p refcount is %i",
265                  p_pic, p_pic->i_refcount );
266         p_pic->i_refcount = 0;
267     }
268
269     if( ( p_pic->i_refcount == 0 ) &&
270         ( p_pic->i_status == DISPLAYED_PICTURE ) )
271     {
272         p_pic->i_status = DESTROYED_PICTURE;
273         p_vout->i_heap_size--;
274     }
275
276     vlc_mutex_unlock( &p_vout->picture_lock );
277 }
278
279 /**
280  * Render a picture
281  *
282  * This function chooses whether the current picture needs to be copied
283  * before rendering, does the subpicture magic, and tells the video output
284  * thread which direct buffer needs to be displayed.
285  */
286 picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
287                                                        subpicture_t *p_subpic )
288 {
289     video_format_t fmt;
290     int i_scale_width, i_scale_height;
291
292     if( p_pic == NULL )
293     {
294         /* XXX: subtitles */
295
296         return NULL;
297     }
298
299     fmt.i_aspect = p_vout->output.i_aspect;
300     fmt.i_chroma = p_vout->output.i_chroma;
301     fmt.i_width = p_vout->output.i_width;
302     fmt.i_height = p_vout->output.i_height;
303     fmt.i_sar_num = p_vout->output.i_aspect * fmt.i_height / fmt.i_width;
304     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
305     i_scale_width = p_vout->output.i_width * 1000 / p_vout->render.i_width;
306     i_scale_height = p_vout->output.i_height * 1000 / p_vout->render.i_height;
307
308     if( p_pic->i_type == DIRECT_PICTURE )
309     {
310         if( !p_vout->render.b_allow_modify_pics || p_pic->i_refcount ||
311             p_pic->b_force )
312         {
313             /* Picture is in a direct buffer and is still in use,
314              * we need to copy it to another direct buffer before
315              * displaying it if there are subtitles. */
316             if( p_subpic != NULL )
317             {
318                 /* We have subtitles. First copy the picture to
319                  * the spare direct buffer, then render the
320                  * subtitles. */
321                 vout_CopyPicture( p_vout, PP_OUTPUTPICTURE[0], p_pic );
322
323                 spu_RenderSubpictures( p_vout->p_spu, &fmt,
324                                        PP_OUTPUTPICTURE[0], p_pic, p_subpic,
325                                        i_scale_width, i_scale_height );
326
327                 return PP_OUTPUTPICTURE[0];
328             }
329
330             /* No subtitles, picture is in a directbuffer so
331              * we can display it directly even if it is still
332              * in use. */
333             return p_pic;
334         }
335
336         /* Picture is in a direct buffer but isn't used by the
337          * decoder. We can safely render subtitles on it and
338          * display it. */
339         spu_RenderSubpictures( p_vout->p_spu, &fmt, p_pic, p_pic, p_subpic,
340                                i_scale_width, i_scale_height );
341
342         return p_pic;
343     }
344
345     /* Not a direct buffer. We either need to copy it to a direct buffer,
346      * or render it if the chroma isn't the same. */
347     if( p_vout->b_direct )
348     {
349         /* Picture is not in a direct buffer, but is exactly the
350          * same size as the direct buffers. A memcpy() is enough,
351          * then render the subtitles. */
352
353         if( PP_OUTPUTPICTURE[0]->pf_lock )
354             if( PP_OUTPUTPICTURE[0]->pf_lock( p_vout, PP_OUTPUTPICTURE[0] ) )
355                 return NULL;
356
357         vout_CopyPicture( p_vout, PP_OUTPUTPICTURE[0], p_pic );
358         spu_RenderSubpictures( p_vout->p_spu, &fmt, PP_OUTPUTPICTURE[0],
359                                p_pic, p_subpic, i_scale_width, i_scale_height);
360
361         if( PP_OUTPUTPICTURE[0]->pf_unlock )
362             PP_OUTPUTPICTURE[0]->pf_unlock( p_vout, PP_OUTPUTPICTURE[0] );
363
364         return PP_OUTPUTPICTURE[0];
365     }
366
367     /* Picture is not in a direct buffer, and needs to be converted to
368      * another size/chroma. Then the subtitles need to be rendered as
369      * well. This usually means software YUV, or hardware YUV with a
370      * different chroma. */
371
372     if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
373     {
374         /* The picture buffer is in slow memory. We'll use
375          * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
376          * one for subpictures rendering. */
377         picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
378         if( p_tmp_pic->i_status == FREE_PICTURE )
379         {
380             vout_AllocatePicture( VLC_OBJECT(p_vout),
381                                   p_tmp_pic, p_vout->output.i_chroma,
382                                   p_vout->output.i_width,
383                                   p_vout->output.i_height,
384                                   p_vout->output.i_aspect );
385             p_tmp_pic->i_type = MEMORY_PICTURE;
386             p_tmp_pic->i_status = RESERVED_PICTURE;
387         }
388
389         /* Convert image to the first direct buffer */
390         p_vout->chroma.pf_convert( p_vout, p_pic, p_tmp_pic );
391
392         /* Render subpictures on the first direct buffer */
393         spu_RenderSubpictures( p_vout->p_spu, &fmt, p_tmp_pic,
394                                p_tmp_pic, p_subpic,
395                                i_scale_width, i_scale_height );
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         vout_CopyPicture( p_vout, &p_vout->p_picture[0], p_tmp_pic );
402     }
403     else
404     {
405         if( p_vout->p_picture[0].pf_lock )
406             if( p_vout->p_picture[0].pf_lock( p_vout, &p_vout->p_picture[0] ) )
407                 return NULL;
408
409         /* Convert image to the first direct buffer */
410         p_vout->chroma.pf_convert( p_vout, p_pic, &p_vout->p_picture[0] );
411
412         /* Render subpictures on the first direct buffer */
413         spu_RenderSubpictures( p_vout->p_spu, &fmt, &p_vout->p_picture[0],
414                                &p_vout->p_picture[0], p_subpic,
415                                i_scale_width, i_scale_height );
416     }
417
418     if( p_vout->p_picture[0].pf_unlock )
419         p_vout->p_picture[0].pf_unlock( p_vout, &p_vout->p_picture[0] );
420
421     return &p_vout->p_picture[0];
422 }
423
424 /**
425  * Calculate image window coordinates
426  *
427  * This function will be accessed by plugins. It calculates the relative
428  * position of the output window and the image window.
429  */
430 void vout_PlacePicture( vout_thread_t *p_vout,
431                         unsigned int i_width, unsigned int i_height,
432                         unsigned int *pi_x, unsigned int *pi_y,
433                         unsigned int *pi_width, unsigned int *pi_height )
434 {
435     if( (i_width <= 0) || (i_height <=0) )
436     {
437         *pi_width = *pi_height = *pi_x = *pi_y = 0;
438
439         return;
440     }
441
442     if( p_vout->b_scale )
443     {
444         *pi_width = i_width;
445         *pi_height = i_height;
446     }
447     else
448     {
449         *pi_width = __MIN( i_width, p_vout->render.i_width );
450         *pi_height = __MIN( i_height, p_vout->render.i_height );
451     }
452
453     if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
454     {
455         *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
456     }
457     else
458     {
459         *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
460     }
461
462     if( *pi_width > i_width )
463     {
464         *pi_width = i_width;
465         *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
466     }
467
468     if( *pi_height > i_height )
469     {
470         *pi_height = i_height;
471         *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
472     }
473
474     switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
475     {
476     case VOUT_ALIGN_LEFT:
477         *pi_x = 0;
478         break;
479     case VOUT_ALIGN_RIGHT:
480         *pi_x = i_width - *pi_width;
481         break;
482     default:
483         *pi_x = ( i_width - *pi_width ) / 2;
484     }
485
486     switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
487     {
488     case VOUT_ALIGN_TOP:
489         *pi_y = 0;
490         break;
491     case VOUT_ALIGN_BOTTOM:
492         *pi_y = i_height - *pi_height;
493         break;
494     default:
495         *pi_y = ( i_height - *pi_height ) / 2;
496     }
497 }
498
499 /**
500  * Allocate a new picture in the heap.
501  *
502  * This function allocates a fake direct buffer in memory, which can be
503  * used exactly like a video buffer. The video output thread then manages
504  * how it gets displayed.
505  */
506 int __vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
507                             vlc_fourcc_t i_chroma,
508                             int i_width, int i_height, int i_aspect )
509 {
510     int i_bytes, i_index, i_width_aligned, i_height_aligned;
511
512     /* Make sure the real dimensions are a multiple of 16 */
513     i_width_aligned = (i_width + 15) >> 4 << 4;
514     i_height_aligned = (i_height + 15) >> 4 << 4;
515
516     if( vout_InitPicture( p_this, p_pic, i_chroma,
517                           i_width, i_height, i_aspect ) != VLC_SUCCESS )
518     {
519         p_pic->i_planes = 0;
520         return VLC_EGENERIC;
521     }
522
523     /* Calculate how big the new image should be */
524     i_bytes = p_pic->format.i_bits_per_pixel *
525         i_width_aligned * i_height_aligned / 8;
526
527     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
528
529     if( p_pic->p_data == NULL )
530     {
531         p_pic->i_planes = 0;
532         return VLC_EGENERIC;
533     }
534
535     /* Fill the p_pixels field for each plane */
536     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
537
538     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
539     {
540         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
541             p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
542     }
543
544     return VLC_SUCCESS;
545 }
546
547 /**
548  * Initialise the video format fields given chroma/size.
549  *
550  * This function initializes all the video_frame_format_t fields given the
551  * static properties of a picture (chroma and size).
552  * \param p_format Pointer to the format structure to initialize
553  * \param i_chroma Chroma to set
554  * \param i_width Width to set
555  * \param i_height Height to set
556  * \param i_aspect Aspect ratio
557  */
558 void vout_InitFormat( video_frame_format_t *p_format, vlc_fourcc_t i_chroma,
559                       int i_width, int i_height, int i_aspect )
560 {
561     p_format->i_chroma   = i_chroma;
562     p_format->i_width    = p_format->i_visible_width  = i_width;
563     p_format->i_height   = p_format->i_visible_height = i_height;
564     p_format->i_x_offset = p_format->i_y_offset = 0;
565     p_format->i_aspect   = i_aspect;
566
567 #if 0
568     /* Assume we have square pixels */
569     if( i_width && i_height )
570         p_format->i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
571     else
572         p_format->i_aspect = 0;
573 #endif
574
575     switch( i_chroma )
576     {
577         case FOURCC_YUVA:
578             p_format->i_bits_per_pixel = 32;
579             break;
580         case FOURCC_I444:
581             p_format->i_bits_per_pixel = 24;
582             break;
583         case FOURCC_I422:
584         case FOURCC_YUY2:
585         case FOURCC_UYVY:
586             p_format->i_bits_per_pixel = 16;
587             p_format->i_bits_per_pixel = 16;
588             break;
589         case FOURCC_I411:
590         case FOURCC_YV12:
591         case FOURCC_I420:
592         case FOURCC_IYUV:
593             p_format->i_bits_per_pixel = 12;
594             break;
595         case FOURCC_I410:
596         case FOURCC_YVU9:
597             p_format->i_bits_per_pixel = 9;
598             break;
599         case FOURCC_Y211:
600             p_format->i_bits_per_pixel = 8;
601             break;
602         case FOURCC_YUVP:
603             p_format->i_bits_per_pixel = 8;
604             break;
605
606         case FOURCC_RV32:
607             p_format->i_bits_per_pixel = 32;
608             break;
609         case FOURCC_RV24:
610             p_format->i_bits_per_pixel = 24;
611             break;
612         case FOURCC_RV15:
613         case FOURCC_RV16:
614             p_format->i_bits_per_pixel = 16;
615             break;
616         case FOURCC_RGB2:
617             p_format->i_bits_per_pixel = 8;
618             break;
619         default:
620             p_format->i_bits_per_pixel = 0;
621             break;
622     }
623 }
624
625 /**
626  * Initialise the picture_t fields given chroma/size.
627  *
628  * This function initializes most of the picture_t fields given a chroma and
629  * size. It makes the assumption that stride == width.
630  * \param p_this The calling object
631  * \param p_pic Pointer to the picture to initialize
632  * \param i_chroma The chroma fourcc to set
633  * \param i_width The width of the picture
634  * \param i_height The height of the picture
635  * \param i_aspect The aspect ratio of the picture
636  */
637 int __vout_InitPicture( vlc_object_t *p_this, picture_t *p_pic,
638                         vlc_fourcc_t i_chroma,
639                         int i_width, int i_height, int i_aspect )
640 {
641     int i_index, i_width_aligned, i_height_aligned;
642
643     /* Store default values */
644     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
645     {
646         p_pic->p[i_index].p_pixels = NULL;
647         p_pic->p[i_index].i_pixel_pitch = 1;
648     }
649
650     p_pic->pf_release = 0;
651     p_pic->pf_lock = 0;
652     p_pic->pf_unlock = 0;
653     p_pic->i_refcount = 0;
654
655     vout_InitFormat( &p_pic->format, i_chroma, i_width, i_height, i_aspect );
656
657     /* Make sure the real dimensions are a multiple of 16 */
658     i_width_aligned = (i_width + 15) >> 4 << 4;
659     i_height_aligned = (i_height + 15) >> 4 << 4;
660
661     /* Calculate coordinates */
662     switch( i_chroma )
663     {
664         case FOURCC_I411:
665             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
666             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
667             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
668             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
669             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
670             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
671             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
672             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
673             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
674             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
675             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
676             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
677             p_pic->i_planes = 3;
678             break;
679
680         case FOURCC_I410:
681         case FOURCC_YVU9:
682             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
683             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
684             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
685             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
686             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 4;
687             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 4;
688             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
689             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
690             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 4;
691             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 4;
692             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
693             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
694             p_pic->i_planes = 3;
695             break;
696
697         case FOURCC_YV12:
698         case FOURCC_I420:
699         case FOURCC_IYUV:
700             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
701             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
702             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
703             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
704             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 2;
705             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 2;
706             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
707             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
708             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 2;
709             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 2;
710             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
711             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
712             p_pic->i_planes = 3;
713             break;
714
715         case FOURCC_I422:
716             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
717             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
718             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
719             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
720             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
721             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
722             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
723             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
724             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
725             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
726             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
727             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
728             p_pic->i_planes = 3;
729             break;
730
731         case FOURCC_I444:
732             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
733             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
734             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
735             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
736             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
737             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
738             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
739             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
740             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
741             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
742             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
743             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
744             p_pic->i_planes = 3;
745             break;
746
747         case FOURCC_YUVA:
748             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
749             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
750             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
751             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
752             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
753             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
754             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
755             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
756             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
757             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
758             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
759             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
760             p_pic->p[ A_PLANE ].i_lines = i_height_aligned;
761             p_pic->p[ A_PLANE ].i_visible_lines = i_height;
762             p_pic->p[ A_PLANE ].i_pitch = i_width_aligned;
763             p_pic->p[ A_PLANE ].i_visible_pitch = i_width;
764             p_pic->i_planes = 4;
765             break;
766
767         case FOURCC_YUVP:
768             p_pic->p->i_lines = i_height_aligned;
769             p_pic->p->i_visible_lines = i_height;
770             p_pic->p->i_pitch = i_width_aligned;
771             p_pic->p->i_visible_pitch = i_width;
772             p_pic->p->i_pixel_pitch = 8;
773             p_pic->i_planes = 1;
774             break;
775
776         case FOURCC_Y211:
777             p_pic->p->i_lines = i_height_aligned;
778             p_pic->p->i_visible_lines = i_height;
779             p_pic->p->i_pitch = i_width_aligned;
780             p_pic->p->i_visible_pitch = i_width;
781             p_pic->p->i_pixel_pitch = 4;
782             p_pic->i_planes = 1;
783             break;
784
785         case FOURCC_UYVY:
786         case FOURCC_YUY2:
787             p_pic->p->i_lines = i_height_aligned;
788             p_pic->p->i_visible_lines = i_height;
789             p_pic->p->i_pitch = i_width_aligned * 2;
790             p_pic->p->i_visible_pitch = i_width * 2;
791             p_pic->p->i_pixel_pitch = 4;
792             p_pic->i_planes = 1;
793             break;
794
795         case FOURCC_RGB2:
796             p_pic->p->i_lines = i_height_aligned;
797             p_pic->p->i_visible_lines = i_height;
798             p_pic->p->i_pitch = i_width_aligned;
799             p_pic->p->i_visible_pitch = i_width;
800             p_pic->p->i_pixel_pitch = 1;
801             p_pic->i_planes = 1;
802             break;
803
804         case FOURCC_RV15:
805             p_pic->p->i_lines = i_height_aligned;
806             p_pic->p->i_visible_lines = i_height;
807             p_pic->p->i_pitch = i_width_aligned * 2;
808             p_pic->p->i_visible_pitch = i_width * 2;
809             p_pic->p->i_pixel_pitch = 2;
810             p_pic->i_planes = 1;
811             break;
812
813         case FOURCC_RV16:
814             p_pic->p->i_lines = i_height_aligned;
815             p_pic->p->i_visible_lines = i_height;
816             p_pic->p->i_pitch = i_width_aligned * 2;
817             p_pic->p->i_visible_pitch = i_width * 2;
818             p_pic->p->i_pixel_pitch = 2;
819             p_pic->i_planes = 1;
820             break;
821
822         case FOURCC_RV24:
823             p_pic->p->i_lines = i_height_aligned;
824             p_pic->p->i_visible_lines = i_height;
825             p_pic->p->i_pitch = i_width_aligned * 3;
826             p_pic->p->i_visible_pitch = i_width * 3;
827             p_pic->p->i_pixel_pitch = 3;
828             p_pic->i_planes = 1;
829             break;
830
831         case FOURCC_RV32:
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         default:
841             msg_Err( p_this, "unknown chroma type 0x%.8x (%4.4s)",
842                              i_chroma, (char*)&i_chroma );
843             p_pic->i_planes = 0;
844             return VLC_EGENERIC;
845     }
846
847     return VLC_SUCCESS;
848 }
849
850 /**
851  * Compare two chroma values
852  *
853  * This function returns 1 if the two fourcc values given as argument are
854  * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
855  */
856 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
857 {
858     /* If they are the same, they are the same ! */
859     if( i_chroma == i_amorhc )
860     {
861         return 1;
862     }
863
864     /* Check for equivalence classes */
865     switch( i_chroma )
866     {
867         case FOURCC_I420:
868         case FOURCC_IYUV:
869         case FOURCC_YV12:
870             switch( i_amorhc )
871             {
872                 case FOURCC_I420:
873                 case FOURCC_IYUV:
874                 case FOURCC_YV12:
875                     return 1;
876
877                 default:
878                     return 0;
879             }
880
881         case FOURCC_UYVY:
882         case FOURCC_UYNV:
883         case FOURCC_Y422:
884             switch( i_amorhc )
885             {
886                 case FOURCC_UYVY:
887                 case FOURCC_UYNV:
888                 case FOURCC_Y422:
889                     return 1;
890
891                 default:
892                     return 0;
893             }
894
895         case FOURCC_YUY2:
896         case FOURCC_YUNV:
897             switch( i_amorhc )
898             {
899                 case FOURCC_YUY2:
900                 case FOURCC_YUNV:
901                     return 1;
902
903                 default:
904                     return 0;
905             }
906
907         default:
908             return 0;
909     }
910 }
911
912 /*****************************************************************************
913  * vout_CopyPicture: copy a picture to another one
914  *****************************************************************************
915  * This function takes advantage of the image format, and reduces the
916  * number of calls to memcpy() to the minimum. Source and destination
917  * images must have same width (hence i_visible_pitch), height, and chroma.
918  *****************************************************************************/
919 void __vout_CopyPicture( vlc_object_t *p_this,
920                          picture_t *p_dest, picture_t *p_src )
921 {
922     int i;
923
924     for( i = 0; i < p_src->i_planes ; i++ )
925     {
926         if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
927         {
928             /* There are margins, but with the same width : perfect ! */
929             p_this->p_vlc->pf_memcpy(
930                          p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
931                          p_src->p[i].i_pitch * p_src->p[i].i_visible_lines );
932         }
933         else
934         {
935             /* We need to proceed line by line */
936             uint8_t *p_in = p_src->p[i].p_pixels;
937             uint8_t *p_out = p_dest->p[i].p_pixels;
938             int i_line;
939
940             for( i_line = p_src->p[i].i_visible_lines; i_line--; )
941             {
942                 p_this->p_vlc->pf_memcpy( p_out, p_in,
943                                           p_src->p[i].i_visible_pitch );
944                 p_in += p_src->p[i].i_pitch;
945                 p_out += p_dest->p[i].i_pitch;
946             }
947         }
948     }
949
950     p_dest->date = p_src->date;
951     p_dest->b_force = p_src->b_force;
952     p_dest->i_nb_fields = p_src->i_nb_fields;
953     p_dest->b_progressive = p_src->b_progressive;
954     p_dest->b_top_field_first = p_src->b_top_field_first;
955 }