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