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