]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
Removed unused i_matrix_coefficients picture_t field.
[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_vout->i_heap_size++;
182         }
183         else
184         {
185             /* Memory allocation failed : set picture as empty */
186             p_freepic->i_status = FREE_PICTURE;
187             p_freepic = NULL;
188
189             msg_Err( p_vout, "picture allocation failed" );
190         }
191
192         vlc_mutex_unlock( &p_vout->picture_lock );
193
194         return( p_freepic );
195     }
196
197     /* No free or destroyed picture could be found, but the decoder
198      * will try again in a while. */
199     vlc_mutex_unlock( &p_vout->picture_lock );
200
201     return( NULL );
202 }
203
204 /**
205  * Remove a permanent or reserved picture from the heap
206  *
207  * This function frees a previously reserved picture or a permanent
208  * picture. It is meant to be used when the construction of a picture aborted.
209  * Note that the picture will be destroyed even if it is linked !
210  */
211 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
212 {
213     vlc_mutex_lock( &p_vout->picture_lock );
214
215 #ifndef NDEBUG
216     /* Check if picture status is valid */
217     if( p_pic->i_status != RESERVED_PICTURE )
218     {
219         msg_Err( p_vout, "picture to destroy %p has invalid status %d",
220                          p_pic, p_pic->i_status );
221     }
222 #endif
223
224     p_pic->i_status = DESTROYED_PICTURE;
225     p_vout->i_heap_size--;
226     picture_CleanupQuant( p_pic );
227
228     vlc_mutex_unlock( &p_vout->picture_lock );
229 }
230
231 /* */
232 void vout_DropPicture( vout_thread_t *p_vout, picture_t *p_pic  )
233 {
234     vlc_mutex_lock( &p_vout->picture_lock );
235
236     if( p_pic->i_status == READY_PICTURE )
237     {
238         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
239         p_pic->date = 1;
240     }
241     else if( p_pic->i_refcount > 0 )
242     {
243         p_pic->i_status = DISPLAYED_PICTURE;
244     }
245     else
246     {
247         p_pic->i_status = DESTROYED_PICTURE;
248         p_vout->i_heap_size--;
249         picture_CleanupQuant( p_pic );
250     }
251
252     vlc_mutex_unlock( &p_vout->picture_lock );
253 }
254
255 /**
256  * Increment reference counter of a picture
257  *
258  * This function increments the reference counter of a picture in the video
259  * heap. It needs a lock since several producer threads can access the picture.
260  */
261 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
262 {
263     vlc_mutex_lock( &p_vout->picture_lock );
264     p_pic->i_refcount++;
265     vlc_mutex_unlock( &p_vout->picture_lock );
266 }
267
268 /**
269  * Decrement reference counter of a picture
270  *
271  * This function decrement the reference counter of a picture in the video heap
272  */
273 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
274 {
275     vlc_mutex_lock( &p_vout->picture_lock );
276     p_pic->i_refcount--;
277
278     if( ( p_pic->i_refcount == 0 ) &&
279         ( p_pic->i_status == DISPLAYED_PICTURE ) )
280     {
281         p_pic->i_status = DESTROYED_PICTURE;
282         p_vout->i_heap_size--;
283         picture_CleanupQuant( p_pic );
284     }
285
286     vlc_mutex_unlock( &p_vout->picture_lock );
287 }
288
289 static int vout_LockPicture( vout_thread_t *p_vout, picture_t *p_picture )
290 {
291     if( p_picture->pf_lock )
292         return p_picture->pf_lock( p_vout, p_picture );
293     return VLC_SUCCESS;
294 }
295 static void vout_UnlockPicture( vout_thread_t *p_vout, picture_t *p_picture )
296 {
297     if( p_picture->pf_unlock )
298         p_picture->pf_unlock( p_vout, p_picture );
299 }
300
301 /**
302  * Render a picture
303  *
304  * This function chooses whether the current picture needs to be copied
305  * before rendering, does the subpicture magic, and tells the video output
306  * thread which direct buffer needs to be displayed.
307  */
308 picture_t *vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
309                                subpicture_t *p_subpic, bool b_paused )
310 {
311     if( p_pic == NULL )
312         return NULL;
313
314     if( p_pic->i_type == DIRECT_PICTURE )
315     {
316         /* Picture is in a direct buffer. */
317
318         if( p_subpic != NULL )
319         {
320             /* We have subtitles. First copy the picture to
321              * the spare direct buffer, then render the
322              * subtitles. */
323             if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
324                 return NULL;
325
326             picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
327
328             spu_RenderSubpictures( p_vout->p_spu,
329                                    PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
330                                    p_subpic, &p_vout->fmt_in, b_paused );
331
332             vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
333
334             return PP_OUTPUTPICTURE[0];
335         }
336
337         /* No subtitles, picture is in a directbuffer so
338          * we can display it directly (even if it is still
339          * in use or not). */
340         return p_pic;
341     }
342
343     /* Not a direct buffer. We either need to copy it to a direct buffer,
344      * or render it if the chroma isn't the same. */
345     if( p_vout->p->b_direct )
346     {
347         /* Picture is not in a direct buffer, but is exactly the
348          * same size as the direct buffers. A memcpy() is enough,
349          * then render the subtitles. */
350
351         if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
352             return NULL;
353
354         picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
355         spu_RenderSubpictures( p_vout->p_spu,
356                                PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
357                                p_subpic, &p_vout->fmt_in, b_paused );
358
359         vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
360
361         return PP_OUTPUTPICTURE[0];
362     }
363
364     /* Picture is not in a direct buffer, and needs to be converted to
365      * another size/chroma. Then the subtitles need to be rendered as
366      * well. This usually means software YUV, or hardware YUV with a
367      * different chroma. */
368
369     if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
370     {
371         /* The picture buffer is in slow memory. We'll use
372          * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
373          * one for subpictures rendering. */
374         picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
375         if( p_tmp_pic->i_status == FREE_PICTURE )
376         {
377             vout_AllocatePicture( VLC_OBJECT(p_vout),
378                                   p_tmp_pic, p_vout->fmt_out.i_chroma,
379                                   p_vout->fmt_out.i_width,
380                                   p_vout->fmt_out.i_height,
381                                   p_vout->fmt_out.i_aspect );
382             p_tmp_pic->i_type = MEMORY_PICTURE;
383             p_tmp_pic->i_status = RESERVED_PICTURE;
384             /* some modules (such as blend)  needs to know the extra information in picture heap */
385             p_tmp_pic->p_heap = &p_vout->output;
386         }
387
388         /* Convert image to the first direct buffer */
389         p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)p_tmp_pic;
390         p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
391
392         /* Render subpictures on the first direct buffer */
393         spu_RenderSubpictures( p_vout->p_spu,
394                                p_tmp_pic, &p_vout->fmt_out,
395                                p_subpic, &p_vout->fmt_in, b_paused );
396
397         if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
398             return NULL;
399
400         picture_Copy( &p_vout->p_picture[0], p_tmp_pic );
401     }
402     else
403     {
404         if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
405             return NULL;
406
407         /* Convert image to the first direct buffer */
408         p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)&p_vout->p_picture[0];
409         p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
410
411         /* Render subpictures on the first direct buffer */
412         spu_RenderSubpictures( p_vout->p_spu,
413                                &p_vout->p_picture[0], &p_vout->fmt_out,
414                                p_subpic, &p_vout->fmt_in, b_paused );
415     }
416
417     vout_UnlockPicture( p_vout, &p_vout->p_picture[0] );
418
419     return &p_vout->p_picture[0];
420 }
421
422 /**
423  * Calculate image window coordinates
424  *
425  * This function will be accessed by plugins. It calculates the relative
426  * position of the output window and the image window.
427  */
428 void vout_PlacePicture( vout_thread_t *p_vout,
429                         unsigned int i_width, unsigned int i_height,
430                         unsigned int *pi_x, unsigned int *pi_y,
431                         unsigned int *pi_width, unsigned int *pi_height )
432 {
433     if( (i_width <= 0) || (i_height <=0) )
434     {
435         *pi_width = *pi_height = *pi_x = *pi_y = 0;
436         return;
437     }
438
439     if( p_vout->b_scale )
440     {
441         *pi_width = i_width;
442         *pi_height = i_height;
443     }
444     else
445     {
446         *pi_width = __MIN( i_width, p_vout->fmt_in.i_visible_width );
447         *pi_height = __MIN( i_height, p_vout->fmt_in.i_visible_height );
448     }
449
450      int64_t i_scaled_width = p_vout->fmt_in.i_visible_width * (int64_t)p_vout->fmt_in.i_sar_num *
451                               *pi_height / p_vout->fmt_in.i_visible_height / p_vout->fmt_in.i_sar_den;
452      int64_t i_scaled_height = p_vout->fmt_in.i_visible_height * (int64_t)p_vout->fmt_in.i_sar_den *
453                                *pi_width / p_vout->fmt_in.i_visible_width / p_vout->fmt_in.i_sar_num;
454
455     if( i_scaled_width <= 0 || i_scaled_height <= 0 )
456     {
457         msg_Warn( p_vout, "ignoring broken aspect ratio" );
458         i_scaled_width = *pi_width;
459         i_scaled_height = *pi_height;
460     }
461
462     if( i_scaled_width > *pi_width )
463         *pi_height = i_scaled_height;
464     else
465         *pi_width = i_scaled_width;
466
467     switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
468     {
469     case VOUT_ALIGN_LEFT:
470         *pi_x = 0;
471         break;
472     case VOUT_ALIGN_RIGHT:
473         *pi_x = i_width - *pi_width;
474         break;
475     default:
476         *pi_x = ( i_width - *pi_width ) / 2;
477     }
478
479     switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
480     {
481     case VOUT_ALIGN_TOP:
482         *pi_y = 0;
483         break;
484     case VOUT_ALIGN_BOTTOM:
485         *pi_y = i_height - *pi_height;
486         break;
487     default:
488         *pi_y = ( i_height - *pi_height ) / 2;
489     }
490 }
491
492 /**
493  * Allocate a new picture in the heap.
494  *
495  * This function allocates a fake direct buffer in memory, which can be
496  * used exactly like a video buffer. The video output thread then manages
497  * how it gets displayed.
498  */
499 int __vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
500                             vlc_fourcc_t i_chroma,
501                             int i_width, int i_height, int i_aspect )
502 {
503     int i_bytes, i_index, i_width_aligned, i_height_aligned;
504
505     /* Make sure the real dimensions are a multiple of 16 */
506     i_width_aligned = (i_width + 15) >> 4 << 4;
507     i_height_aligned = (i_height + 15) >> 4 << 4;
508
509     if( vout_InitPicture( p_this, p_pic, i_chroma,
510                           i_width, i_height, i_aspect ) != VLC_SUCCESS )
511     {
512         p_pic->i_planes = 0;
513         return VLC_EGENERIC;
514     }
515
516     /* Calculate how big the new image should be */
517     i_bytes = p_pic->format.i_bits_per_pixel *
518         i_width_aligned * i_height_aligned / 8;
519
520     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
521
522     if( p_pic->p_data == NULL )
523     {
524         p_pic->i_planes = 0;
525         return VLC_EGENERIC;
526     }
527
528     /* Fill the p_pixels field for each plane */
529     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
530
531     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
532     {
533         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
534             p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
535     }
536
537     return VLC_SUCCESS;
538 }
539
540 /**
541  * Initialise the video format fields given chroma/size.
542  *
543  * This function initializes all the video_frame_format_t fields given the
544  * static properties of a picture (chroma and size).
545  * \param p_format Pointer to the format structure to initialize
546  * \param i_chroma Chroma to set
547  * \param i_width Width to set
548  * \param i_height Height to set
549  * \param i_aspect Aspect ratio
550  */
551 void vout_InitFormat( video_frame_format_t *p_format, vlc_fourcc_t i_chroma,
552                       int i_width, int i_height, int i_aspect )
553 {
554     p_format->i_chroma   = i_chroma;
555     p_format->i_width    = p_format->i_visible_width  = i_width;
556     p_format->i_height   = p_format->i_visible_height = i_height;
557     p_format->i_x_offset = p_format->i_y_offset = 0;
558     p_format->i_aspect   = i_aspect;
559
560 #if 0
561     /* Assume we have square pixels */
562     if( i_width && i_height )
563         p_format->i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
564     else
565         p_format->i_aspect = 0;
566 #endif
567
568     switch( i_chroma )
569     {
570         case FOURCC_YUVA:
571             p_format->i_bits_per_pixel = 32;
572             break;
573         case FOURCC_I444:
574         case FOURCC_J444:
575             p_format->i_bits_per_pixel = 24;
576             break;
577         case FOURCC_I422:
578         case FOURCC_YUY2:
579         case FOURCC_UYVY:
580         case FOURCC_J422:
581             p_format->i_bits_per_pixel = 16;
582             break;
583         case FOURCC_I440:
584         case FOURCC_J440:
585             p_format->i_bits_per_pixel = 16;
586             break;
587         case FOURCC_I411:
588         case FOURCC_YV12:
589         case FOURCC_I420:
590         case FOURCC_J420:
591         case FOURCC_IYUV:
592             p_format->i_bits_per_pixel = 12;
593             break;
594         case FOURCC_I410:
595         case FOURCC_YVU9:
596             p_format->i_bits_per_pixel = 9;
597             break;
598         case FOURCC_Y211:
599             p_format->i_bits_per_pixel = 8;
600             break;
601         case FOURCC_YUVP:
602             p_format->i_bits_per_pixel = 8;
603             break;
604
605         case FOURCC_RV32:
606         case FOURCC_RGBA:
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
620         case FOURCC_GREY:
621         case FOURCC_Y800:
622         case FOURCC_Y8:
623         case FOURCC_RGBP:
624             p_format->i_bits_per_pixel = 8;
625             break;
626
627         default:
628             p_format->i_bits_per_pixel = 0;
629             break;
630     }
631 }
632
633 /**
634  * Initialise the picture_t fields given chroma/size.
635  *
636  * This function initializes most of the picture_t fields given a chroma and
637  * size. It makes the assumption that stride == width.
638  * \param p_this The calling object
639  * \param p_pic Pointer to the picture to initialize
640  * \param i_chroma The chroma fourcc to set
641  * \param i_width The width of the picture
642  * \param i_height The height of the picture
643  * \param i_aspect The aspect ratio of the picture
644  */
645 int __vout_InitPicture( vlc_object_t *p_this, picture_t *p_pic,
646                         vlc_fourcc_t i_chroma,
647                         int i_width, int i_height, int i_aspect )
648 {
649     int i_index, i_width_aligned, i_height_aligned;
650
651     /* Store default values */
652     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
653     {
654         p_pic->p[i_index].p_pixels = NULL;
655         p_pic->p[i_index].i_pixel_pitch = 1;
656     }
657
658     p_pic->pf_release = NULL;
659     p_pic->pf_lock = NULL;
660     p_pic->pf_unlock = NULL;
661     p_pic->i_refcount = 0;
662
663     p_pic->p_q = NULL;
664     p_pic->i_qstride = 0;
665     p_pic->i_qtype = 0;
666
667     vout_InitFormat( &p_pic->format, i_chroma, i_width, i_height, i_aspect );
668
669     /* Make sure the real dimensions are a multiple of 16 */
670     i_width_aligned = (i_width + 15) >> 4 << 4;
671     i_height_aligned = (i_height + 15) >> 4 << 4;
672
673     /* Calculate coordinates */
674     switch( i_chroma )
675     {
676         case FOURCC_I411:
677             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
678             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
679             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
680             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
681             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
682             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
683             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
684             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
685             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
686             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
687             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
688             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
689             p_pic->i_planes = 3;
690             break;
691
692         case FOURCC_I410:
693         case FOURCC_YVU9:
694             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
695             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
696             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
697             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
698             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 4;
699             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 4;
700             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
701             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
702             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 4;
703             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 4;
704             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
705             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
706             p_pic->i_planes = 3;
707             break;
708
709         case FOURCC_YV12:
710         case FOURCC_I420:
711         case FOURCC_IYUV:
712         case FOURCC_J420:
713             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
714             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
715             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
716             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
717             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 2;
718             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 2;
719             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
720             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
721             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 2;
722             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 2;
723             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
724             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
725             p_pic->i_planes = 3;
726             break;
727
728         case FOURCC_I422:
729         case FOURCC_J422:
730             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
731             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
732             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
733             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
734             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
735             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
736             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
737             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
738             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
739             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
740             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
741             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
742             p_pic->i_planes = 3;
743             break;
744
745         case FOURCC_I440:
746         case FOURCC_J440:
747             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
748             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
749             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
750             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
751             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 2;
752             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 2;
753             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
754             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
755             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 2;
756             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 2;
757             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
758             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
759             p_pic->i_planes = 3;
760             break;
761
762         case FOURCC_I444:
763         case FOURCC_J444:
764             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
765             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
766             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
767             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
768             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
769             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
770             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
771             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
772             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
773             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
774             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
775             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
776             p_pic->i_planes = 3;
777             break;
778
779         case FOURCC_YUVA:
780             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
781             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
782             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
783             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
784             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
785             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
786             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
787             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
788             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
789             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
790             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
791             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
792             p_pic->p[ A_PLANE ].i_lines = i_height_aligned;
793             p_pic->p[ A_PLANE ].i_visible_lines = i_height;
794             p_pic->p[ A_PLANE ].i_pitch = i_width_aligned;
795             p_pic->p[ A_PLANE ].i_visible_pitch = i_width;
796             p_pic->i_planes = 4;
797             break;
798
799         case FOURCC_YUVP:
800             p_pic->p->i_lines = i_height_aligned;
801             p_pic->p->i_visible_lines = i_height;
802             p_pic->p->i_pitch = i_width_aligned;
803             p_pic->p->i_visible_pitch = i_width;
804             p_pic->p->i_pixel_pitch = 8;
805             p_pic->i_planes = 1;
806             break;
807
808         case FOURCC_Y211:
809             p_pic->p->i_lines = i_height_aligned;
810             p_pic->p->i_visible_lines = i_height;
811             p_pic->p->i_pitch = i_width_aligned;
812             p_pic->p->i_visible_pitch = i_width;
813             p_pic->p->i_pixel_pitch = 4;
814             p_pic->i_planes = 1;
815             break;
816
817         case FOURCC_UYVY:
818         case FOURCC_YUY2:
819             p_pic->p->i_lines = i_height_aligned;
820             p_pic->p->i_visible_lines = i_height;
821             p_pic->p->i_pitch = i_width_aligned * 2;
822             p_pic->p->i_visible_pitch = i_width * 2;
823             p_pic->p->i_pixel_pitch = 4;
824             p_pic->i_planes = 1;
825             break;
826
827         case FOURCC_RGB2:
828             p_pic->p->i_lines = i_height_aligned;
829             p_pic->p->i_visible_lines = i_height;
830             p_pic->p->i_pitch = i_width_aligned;
831             p_pic->p->i_visible_pitch = i_width;
832             p_pic->p->i_pixel_pitch = 1;
833             p_pic->i_planes = 1;
834             break;
835
836         case FOURCC_RV15:
837             p_pic->p->i_lines = i_height_aligned;
838             p_pic->p->i_visible_lines = i_height;
839             p_pic->p->i_pitch = i_width_aligned * 2;
840             p_pic->p->i_visible_pitch = i_width * 2;
841             p_pic->p->i_pixel_pitch = 2;
842             p_pic->i_planes = 1;
843             break;
844
845         case FOURCC_RV16:
846             p_pic->p->i_lines = i_height_aligned;
847             p_pic->p->i_visible_lines = i_height;
848             p_pic->p->i_pitch = i_width_aligned * 2;
849             p_pic->p->i_visible_pitch = i_width * 2;
850             p_pic->p->i_pixel_pitch = 2;
851             p_pic->i_planes = 1;
852             break;
853
854         case FOURCC_RV24:
855             p_pic->p->i_lines = i_height_aligned;
856             p_pic->p->i_visible_lines = i_height;
857             p_pic->p->i_pitch = i_width_aligned * 3;
858             p_pic->p->i_visible_pitch = i_width * 3;
859             p_pic->p->i_pixel_pitch = 3;
860             p_pic->i_planes = 1;
861             break;
862
863         case FOURCC_RV32:
864         case FOURCC_RGBA:
865             p_pic->p->i_lines = i_height_aligned;
866             p_pic->p->i_visible_lines = i_height;
867             p_pic->p->i_pitch = i_width_aligned * 4;
868             p_pic->p->i_visible_pitch = i_width * 4;
869             p_pic->p->i_pixel_pitch = 4;
870             p_pic->i_planes = 1;
871             break;
872
873         case FOURCC_GREY:
874         case FOURCC_Y800:
875         case FOURCC_Y8:
876         case FOURCC_RGBP:
877             p_pic->p->i_lines = i_height_aligned;
878             p_pic->p->i_visible_lines = i_height;
879             p_pic->p->i_pitch = i_width_aligned;
880             p_pic->p->i_visible_pitch = i_width;
881             p_pic->p->i_pixel_pitch = 1;
882             p_pic->i_planes = 1;
883             break;
884
885         default:
886             if( p_this )
887                 msg_Err( p_this, "unknown chroma type 0x%.8x (%4.4s)",
888                                  i_chroma, (char*)&i_chroma );
889             p_pic->i_planes = 0;
890             return VLC_EGENERIC;
891     }
892
893     return VLC_SUCCESS;
894 }
895
896 /**
897  * Compare two chroma values
898  *
899  * This function returns 1 if the two fourcc values given as argument are
900  * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
901  */
902 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
903 {
904     /* If they are the same, they are the same ! */
905     if( i_chroma == i_amorhc )
906     {
907         return 1;
908     }
909
910     /* Check for equivalence classes */
911     switch( i_chroma )
912     {
913         case FOURCC_I420:
914         case FOURCC_IYUV:
915         case FOURCC_YV12:
916             switch( i_amorhc )
917             {
918                 case FOURCC_I420:
919                 case FOURCC_IYUV:
920                 case FOURCC_YV12:
921                     return 1;
922
923                 default:
924                     return 0;
925             }
926
927         case FOURCC_UYVY:
928         case FOURCC_UYNV:
929         case FOURCC_Y422:
930             switch( i_amorhc )
931             {
932                 case FOURCC_UYVY:
933                 case FOURCC_UYNV:
934                 case FOURCC_Y422:
935                     return 1;
936
937                 default:
938                     return 0;
939             }
940
941         case FOURCC_YUY2:
942         case FOURCC_YUNV:
943             switch( i_amorhc )
944             {
945                 case FOURCC_YUY2:
946                 case FOURCC_YUNV:
947                     return 1;
948
949                 default:
950                     return 0;
951             }
952
953         case FOURCC_GREY:
954         case FOURCC_Y800:
955         case FOURCC_Y8:
956             switch( i_amorhc )
957             {
958                 case FOURCC_GREY:
959                 case FOURCC_Y800:
960                 case FOURCC_Y8:
961                     return 1;
962
963                 default:
964                     return 0;
965             }
966
967         default:
968             return 0;
969     }
970 }
971
972 /*****************************************************************************
973  *
974  *****************************************************************************/
975 static void PictureReleaseCallback( picture_t *p_picture )
976 {
977     if( --p_picture->i_refcount > 0 )
978         return;
979     picture_Delete( p_picture );
980 }
981 /*****************************************************************************
982  *
983  *****************************************************************************/
984 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
985 {
986     picture_t *p_picture = malloc( sizeof(*p_picture) );
987
988     if( !p_picture )
989         return NULL;
990
991     memset( p_picture, 0, sizeof(*p_picture) );
992     if( __vout_AllocatePicture( NULL, p_picture,
993                                 i_chroma, i_width, i_height, i_aspect ) )
994     {
995         free( p_picture );
996         return NULL;
997     }
998
999     p_picture->i_refcount = 1;
1000     p_picture->pf_release = PictureReleaseCallback;
1001     p_picture->i_status = RESERVED_PICTURE;
1002
1003     return p_picture;
1004 }
1005
1006 /*****************************************************************************
1007  *
1008  *****************************************************************************/
1009 void picture_Delete( picture_t *p_picture )
1010 {
1011     assert( p_picture && p_picture->i_refcount == 0 );
1012
1013     free( p_picture->p_q );
1014     free( p_picture->p_data_orig );
1015     free( p_picture->p_sys );
1016     free( p_picture );
1017 }
1018
1019 /*****************************************************************************
1020  *
1021  *****************************************************************************/
1022 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
1023 {
1024     int i;
1025
1026     for( i = 0; i < p_src->i_planes ; i++ )
1027         plane_CopyPixels( p_dst->p+i, p_src->p+i );
1028 }
1029
1030 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
1031 {
1032     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
1033                                      p_src->i_visible_pitch );
1034     const unsigned i_height = __MIN( p_dst->i_visible_lines,
1035                                      p_src->i_visible_lines );
1036
1037     if( p_src->i_pitch == p_dst->i_pitch )
1038     {
1039         /* There are margins, but with the same width : perfect ! */
1040         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
1041                     p_src->i_pitch * i_height );
1042     }
1043     else
1044     {
1045         /* We need to proceed line by line */
1046         uint8_t *p_in = p_src->p_pixels;
1047         uint8_t *p_out = p_dst->p_pixels;
1048         int i_line;
1049
1050         assert( p_in );
1051         assert( p_out );
1052
1053         for( i_line = i_height; i_line--; )
1054         {
1055             vlc_memcpy( p_out, p_in, i_width );
1056             p_in += p_src->i_pitch;
1057             p_out += p_dst->i_pitch;
1058         }
1059     }
1060 }
1061
1062 /*****************************************************************************
1063  *
1064  *****************************************************************************/
1065