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