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