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