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