]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
3e500cdb44cf7b002b78fdc5d1f4c680c7f83650
[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 <vlc_image.h>
40 #include <vlc_block.h>
41
42 #include "vout_pictures.h"
43 #include "vout_internal.h"
44
45 /**
46  * Display a picture
47  *
48  * Remove the reservation flag of a picture, which will cause it to be ready
49  * for display.
50  */
51 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
52 {
53     vlc_mutex_lock( &p_vout->picture_lock );
54
55     if( p_pic->i_status == RESERVED_PICTURE )
56     {
57         p_pic->i_status = READY_PICTURE;
58         vlc_cond_signal( &p_vout->p->picture_wait );
59     }
60     else
61     {
62         msg_Err( p_vout, "picture to display %p has invalid status %d",
63                          p_pic, p_pic->i_status );
64     }
65     p_vout->p->i_picture_qtype = p_pic->i_qtype;
66
67     vlc_mutex_unlock( &p_vout->picture_lock );
68 }
69
70 /**
71  * Allocate a picture in the video output heap.
72  *
73  * This function creates a reserved image in the video output heap.
74  * A null pointer is returned if the function fails. This method provides an
75  * already allocated zone of memory in the picture data fields.
76  * It needs locking since several pictures can be created by several producers
77  * threads.
78  */
79 int vout_CountPictureAvailable( vout_thread_t *p_vout )
80 {
81     int i_free = 0;
82     int i_pic;
83
84     vlc_mutex_lock( &p_vout->picture_lock );
85     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
86     {
87         picture_t *p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1) % I_RENDERPICTURES];
88
89         switch( p_pic->i_status )
90         {
91             case DESTROYED_PICTURE:
92                 i_free++;
93                 break;
94
95             case FREE_PICTURE:
96                 i_free++;
97                 break;
98
99             default:
100                 break;
101         }
102     }
103     vlc_mutex_unlock( &p_vout->picture_lock );
104
105     return i_free;
106 }
107
108 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
109                                bool b_progressive,
110                                bool b_top_field_first,
111                                unsigned int i_nb_fields )
112 {
113     int         i_pic;                                      /* picture index */
114     picture_t * p_pic;
115     picture_t * p_freepic = NULL;                      /* first free picture */
116
117     /* Get lock */
118     vlc_mutex_lock( &p_vout->picture_lock );
119
120     /*
121      * Look for an empty place in the picture heap.
122      */
123     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
124     {
125         p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
126                                  % I_RENDERPICTURES];
127
128         switch( p_pic->i_status )
129         {
130             case DESTROYED_PICTURE:
131                 /* Memory will not be reallocated, and function can end
132                  * immediately - this is the best possible case, since no
133                  * memory allocation needs to be done */
134                 p_pic->i_status   = RESERVED_PICTURE;
135                 p_pic->i_refcount = 0;
136                 p_pic->b_force    = 0;
137
138                 p_pic->b_progressive        = b_progressive;
139                 p_pic->i_nb_fields          = i_nb_fields;
140                 p_pic->b_top_field_first    = b_top_field_first;
141
142                 p_vout->i_heap_size++;
143                 p_vout->render.i_last_used_pic =
144                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
145                     % I_RENDERPICTURES;
146                 vlc_mutex_unlock( &p_vout->picture_lock );
147                 return( p_pic );
148
149             case FREE_PICTURE:
150                 /* Picture is empty and ready for allocation */
151                 p_vout->render.i_last_used_pic =
152                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
153                     % I_RENDERPICTURES;
154                 p_freepic = p_pic;
155                 break;
156
157             default:
158                 break;
159         }
160     }
161
162     /*
163      * Prepare picture
164      */
165     if( p_freepic != NULL )
166     {
167         vout_AllocatePicture( VLC_OBJECT(p_vout),
168                               p_freepic, p_vout->render.i_chroma,
169                               p_vout->render.i_width, p_vout->render.i_height,
170                               p_vout->render.i_aspect );
171
172         if( p_freepic->i_planes )
173         {
174             /* Copy picture information, set some default values */
175             p_freepic->i_status   = RESERVED_PICTURE;
176             p_freepic->i_type     = MEMORY_PICTURE;
177             p_freepic->b_slow     = 0;
178
179             p_freepic->i_refcount = 0;
180             p_freepic->b_force = 0;
181
182             p_freepic->b_progressive        = b_progressive;
183             p_freepic->i_nb_fields          = i_nb_fields;
184             p_freepic->b_top_field_first    = b_top_field_first;
185
186             p_vout->i_heap_size++;
187         }
188         else
189         {
190             /* Memory allocation failed : set picture as empty */
191             p_freepic->i_status = FREE_PICTURE;
192             p_freepic = NULL;
193
194             msg_Err( p_vout, "picture allocation failed" );
195         }
196
197         vlc_mutex_unlock( &p_vout->picture_lock );
198
199         return( p_freepic );
200     }
201
202     /* No free or destroyed picture could be found, but the decoder
203      * will try again in a while. */
204     vlc_mutex_unlock( &p_vout->picture_lock );
205
206     return( NULL );
207 }
208
209 /* */
210 static void DestroyPicture( vout_thread_t *p_vout, picture_t *p_picture )
211 {
212     vlc_assert_locked( &p_vout->picture_lock );
213
214     p_picture->i_status = DESTROYED_PICTURE;
215     p_vout->i_heap_size--;
216     picture_CleanupQuant( p_picture );
217
218     vlc_cond_signal( &p_vout->p->picture_wait );
219 }
220
221 /**
222  * Remove a permanent or reserved picture from the heap
223  *
224  * This function frees a previously reserved picture or a permanent
225  * picture. It is meant to be used when the construction of a picture aborted.
226  * Note that the picture will be destroyed even if it is linked !
227  *
228  * TODO remove it, vout_DropPicture should be used instead
229  */
230 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
231 {
232 #ifndef NDEBUG
233     /* Check if picture status is valid */
234     vlc_mutex_lock( &p_vout->picture_lock );
235     if( p_pic->i_status != RESERVED_PICTURE )
236     {
237         msg_Err( p_vout, "picture to destroy %p has invalid status %d",
238                          p_pic, p_pic->i_status );
239     }
240     vlc_mutex_unlock( &p_vout->picture_lock );
241 #endif
242
243     vout_DropPicture( p_vout, p_pic );
244 }
245
246 /* */
247 void vout_UsePictureLocked( vout_thread_t *p_vout, picture_t *p_picture )
248 {
249     vlc_assert_locked( &p_vout->picture_lock );
250     if( p_picture->i_refcount > 0 )
251     {
252         /* Pretend we displayed the picture, but don't destroy
253          * it since the decoder might still need it. */
254         p_picture->i_status = DISPLAYED_PICTURE;
255     }
256     else
257     {
258         /* Destroy the picture without displaying it */
259         DestroyPicture( p_vout, p_picture );
260     }
261 }
262
263 /* */
264 void vout_DropPicture( vout_thread_t *p_vout, picture_t *p_pic  )
265 {
266     vlc_mutex_lock( &p_vout->picture_lock );
267
268     if( p_pic->i_status == READY_PICTURE )
269     {
270         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
271         p_pic->date = 1;
272         vlc_cond_signal( &p_vout->p->picture_wait );
273     }
274     else
275     {
276         vout_UsePictureLocked( p_vout, p_pic );
277     }
278
279     vlc_mutex_unlock( &p_vout->picture_lock );
280 }
281
282 /**
283  * Increment reference counter of a picture
284  *
285  * This function increments the reference counter of a picture in the video
286  * heap. It needs a lock since several producer threads can access the picture.
287  */
288 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
289 {
290     vlc_mutex_lock( &p_vout->picture_lock );
291     p_pic->i_refcount++;
292     vlc_mutex_unlock( &p_vout->picture_lock );
293 }
294
295 /**
296  * Decrement reference counter of a picture
297  *
298  * This function decrement the reference counter of a picture in the video heap
299  */
300 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
301 {
302     vlc_mutex_lock( &p_vout->picture_lock );
303
304     if( p_pic->i_refcount > 0 )
305         p_pic->i_refcount--;
306     else
307         msg_Err( p_vout, "Invalid picture reference count (%p, %d)",
308                  p_pic, p_pic->i_refcount );
309
310     if( p_pic->i_refcount == 0 && p_pic->i_status == DISPLAYED_PICTURE )
311         DestroyPicture( p_vout, p_pic );
312
313     vlc_mutex_unlock( &p_vout->picture_lock );
314 }
315
316 static int vout_LockPicture( vout_thread_t *p_vout, picture_t *p_picture )
317 {
318     if( p_picture->pf_lock )
319         return p_picture->pf_lock( p_vout, p_picture );
320     return VLC_SUCCESS;
321 }
322 static void vout_UnlockPicture( vout_thread_t *p_vout, picture_t *p_picture )
323 {
324     if( p_picture->pf_unlock )
325         p_picture->pf_unlock( p_vout, p_picture );
326 }
327
328 /**
329  * Render a picture
330  *
331  * This function chooses whether the current picture needs to be copied
332  * before rendering, does the subpicture magic, and tells the video output
333  * thread which direct buffer needs to be displayed.
334  */
335 picture_t *vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
336                                subpicture_t *p_subpic, bool b_paused )
337 {
338     if( p_pic == NULL )
339         return NULL;
340
341     if( p_pic->i_type == DIRECT_PICTURE )
342     {
343         /* Picture is in a direct buffer. */
344
345         if( p_subpic != NULL )
346         {
347             /* We have subtitles. First copy the picture to
348              * the spare direct buffer, then render the
349              * subtitles. */
350             if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
351                 return NULL;
352
353             picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
354
355             spu_RenderSubpictures( p_vout->p_spu,
356                                    PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
357                                    p_subpic, &p_vout->fmt_in, b_paused );
358
359             vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
360
361             return PP_OUTPUTPICTURE[0];
362         }
363
364         /* No subtitles, picture is in a directbuffer so
365          * we can display it directly (even if it is still
366          * in use or not). */
367         return p_pic;
368     }
369
370     /* Not a direct buffer. We either need to copy it to a direct buffer,
371      * or render it if the chroma isn't the same. */
372     if( p_vout->p->b_direct )
373     {
374         /* Picture is not in a direct buffer, but is exactly the
375          * same size as the direct buffers. A memcpy() is enough,
376          * then render the subtitles. */
377
378         if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
379             return NULL;
380
381         picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
382         spu_RenderSubpictures( p_vout->p_spu,
383                                PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
384                                p_subpic, &p_vout->fmt_in, b_paused );
385
386         vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
387
388         return PP_OUTPUTPICTURE[0];
389     }
390
391     /* Picture is not in a direct buffer, and needs to be converted to
392      * another size/chroma. Then the subtitles need to be rendered as
393      * well. This usually means software YUV, or hardware YUV with a
394      * different chroma. */
395
396     if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
397     {
398         /* The picture buffer is in slow memory. We'll use
399          * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
400          * one for subpictures rendering. */
401         picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
402         if( p_tmp_pic->i_status == FREE_PICTURE )
403         {
404             vout_AllocatePicture( VLC_OBJECT(p_vout),
405                                   p_tmp_pic, p_vout->fmt_out.i_chroma,
406                                   p_vout->fmt_out.i_width,
407                                   p_vout->fmt_out.i_height,
408                                   p_vout->fmt_out.i_aspect );
409             p_tmp_pic->i_type = MEMORY_PICTURE;
410             p_tmp_pic->i_status = RESERVED_PICTURE;
411             /* some modules (such as blend)  needs to know the extra information in picture heap */
412             p_tmp_pic->p_heap = &p_vout->output;
413         }
414
415         /* Convert image to the first direct buffer */
416         p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)p_tmp_pic;
417         p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
418
419         /* Render subpictures on the first direct buffer */
420         spu_RenderSubpictures( p_vout->p_spu,
421                                p_tmp_pic, &p_vout->fmt_out,
422                                p_subpic, &p_vout->fmt_in, b_paused );
423
424         if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
425             return NULL;
426
427         picture_Copy( &p_vout->p_picture[0], p_tmp_pic );
428     }
429     else
430     {
431         if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
432             return NULL;
433
434         /* Convert image to the first direct buffer */
435         p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)&p_vout->p_picture[0];
436         p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
437
438         /* Render subpictures on the first direct buffer */
439         spu_RenderSubpictures( p_vout->p_spu,
440                                &p_vout->p_picture[0], &p_vout->fmt_out,
441                                p_subpic, &p_vout->fmt_in, b_paused );
442     }
443
444     vout_UnlockPicture( p_vout, &p_vout->p_picture[0] );
445
446     return &p_vout->p_picture[0];
447 }
448
449 /**
450  * Calculate image window coordinates
451  *
452  * This function will be accessed by plugins. It calculates the relative
453  * position of the output window and the image window.
454  */
455 void vout_PlacePicture( const vout_thread_t *p_vout,
456                         unsigned int i_width, unsigned int i_height,
457                         unsigned int *restrict pi_x,
458                         unsigned int *restrict pi_y,
459                         unsigned int *restrict pi_width,
460                         unsigned int *restrict pi_height )
461 {
462     if( i_width <= 0 || i_height <= 0 )
463     {
464         *pi_width = *pi_height = *pi_x = *pi_y = 0;
465         return;
466     }
467
468     if( p_vout->b_autoscale )
469     {
470         *pi_width = i_width;
471         *pi_height = i_height;
472     }
473     else
474     {
475         int i_zoom = p_vout->i_zoom;
476         /* be realistic, scaling factor confined between .2 and 10. */
477         if( i_zoom > 10 * ZOOM_FP_FACTOR )      i_zoom = 10 * ZOOM_FP_FACTOR;
478         else if( i_zoom <  ZOOM_FP_FACTOR / 5 ) i_zoom = ZOOM_FP_FACTOR / 5;
479
480         unsigned int i_original_width, i_original_height;
481
482         if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
483         {
484             i_original_width = p_vout->fmt_in.i_visible_width *
485                                p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den;
486             i_original_height =  p_vout->fmt_in.i_visible_height;
487         }
488         else
489         {
490             i_original_width =  p_vout->fmt_in.i_visible_width;
491             i_original_height = p_vout->fmt_in.i_visible_height *
492                                 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num;
493         }
494 #ifdef WIN32
495         /* On windows, inner video window exceeding container leads to black screen */
496         *pi_width = __MIN( i_width, i_original_width * i_zoom / ZOOM_FP_FACTOR );
497         *pi_height = __MIN( i_height, i_original_height * i_zoom / ZOOM_FP_FACTOR );
498 #else
499         *pi_width = i_original_width * i_zoom / ZOOM_FP_FACTOR ;
500         *pi_height = i_original_height * i_zoom / ZOOM_FP_FACTOR ;
501 #endif
502     }
503
504     int64_t i_scaled_width = p_vout->fmt_in.i_visible_width * (int64_t)p_vout->fmt_in.i_sar_num *
505                               *pi_height / p_vout->fmt_in.i_visible_height / p_vout->fmt_in.i_sar_den;
506     int64_t i_scaled_height = p_vout->fmt_in.i_visible_height * (int64_t)p_vout->fmt_in.i_sar_den *
507                                *pi_width / p_vout->fmt_in.i_visible_width / p_vout->fmt_in.i_sar_num;
508
509     if( i_scaled_width <= 0 || i_scaled_height <= 0 )
510     {
511         msg_Warn( p_vout, "ignoring broken aspect ratio" );
512         i_scaled_width = *pi_width;
513         i_scaled_height = *pi_height;
514     }
515
516     if( i_scaled_width > *pi_width )
517         *pi_height = i_scaled_height;
518     else
519         *pi_width = i_scaled_width;
520
521     switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
522     {
523     case VOUT_ALIGN_LEFT:
524         *pi_x = 0;
525         break;
526     case VOUT_ALIGN_RIGHT:
527         *pi_x = i_width - *pi_width;
528         break;
529     default:
530         *pi_x = ( i_width - *pi_width ) / 2;
531     }
532
533     switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
534     {
535     case VOUT_ALIGN_TOP:
536         *pi_y = 0;
537         break;
538     case VOUT_ALIGN_BOTTOM:
539         *pi_y = i_height - *pi_height;
540         break;
541     default:
542         *pi_y = ( i_height - *pi_height ) / 2;
543     }
544 }
545
546 /**
547  * Allocate a new picture in the heap.
548  *
549  * This function allocates a fake direct buffer in memory, which can be
550  * used exactly like a video buffer. The video output thread then manages
551  * how it gets displayed.
552  */
553 int __vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
554                             vlc_fourcc_t i_chroma,
555                             int i_width, int i_height, int i_aspect )
556 {
557     int i_bytes, i_index, i_width_aligned, i_height_aligned;
558
559     /* Make sure the real dimensions are a multiple of 16 */
560     i_width_aligned = (i_width + 15) >> 4 << 4;
561     i_height_aligned = (i_height + 15) >> 4 << 4;
562
563     if( vout_InitPicture( p_this, p_pic, i_chroma,
564                           i_width, i_height, i_aspect ) != VLC_SUCCESS )
565     {
566         p_pic->i_planes = 0;
567         return VLC_EGENERIC;
568     }
569
570     /* Calculate how big the new image should be */
571     i_bytes = p_pic->format.i_bits_per_pixel *
572         i_width_aligned * i_height_aligned / 8;
573
574     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
575
576     if( p_pic->p_data == NULL )
577     {
578         p_pic->i_planes = 0;
579         return VLC_EGENERIC;
580     }
581
582     /* Fill the p_pixels field for each plane */
583     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
584
585     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
586     {
587         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
588             p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
589     }
590
591     return VLC_SUCCESS;
592 }
593
594 /**
595  * Initialise the video format fields given chroma/size.
596  *
597  * This function initializes all the video_frame_format_t fields given the
598  * static properties of a picture (chroma and size).
599  * \param p_format Pointer to the format structure to initialize
600  * \param i_chroma Chroma to set
601  * \param i_width Width to set
602  * \param i_height Height to set
603  * \param i_aspect Aspect ratio
604  */
605 void vout_InitFormat( video_frame_format_t *p_format, vlc_fourcc_t i_chroma,
606                       int i_width, int i_height, int i_aspect )
607 {
608     p_format->i_chroma   = i_chroma;
609     p_format->i_width    = p_format->i_visible_width  = i_width;
610     p_format->i_height   = p_format->i_visible_height = i_height;
611     p_format->i_x_offset = p_format->i_y_offset = 0;
612     p_format->i_aspect   = i_aspect;
613
614 #if 0
615     /* Assume we have square pixels */
616     if( i_width && i_height )
617         p_format->i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
618     else
619         p_format->i_aspect = 0;
620 #endif
621
622     switch( vlc_fourcc_GetCodec( VIDEO_ES, i_chroma ) )
623     {
624         case VLC_CODEC_YUVA:
625             p_format->i_bits_per_pixel = 32;
626             break;
627         case VLC_CODEC_I444:
628         case VLC_CODEC_J444:
629             p_format->i_bits_per_pixel = 24;
630             break;
631         case VLC_CODEC_I422:
632         case VLC_CODEC_YUYV:
633         case VLC_CODEC_YVYU:
634         case VLC_CODEC_UYVY:
635         case VLC_CODEC_VYUY:
636         case VLC_CODEC_J422:
637             p_format->i_bits_per_pixel = 16;
638             break;
639         case VLC_CODEC_I440:
640         case VLC_CODEC_J440:
641             p_format->i_bits_per_pixel = 16;
642             break;
643         case VLC_CODEC_I411:
644         case VLC_CODEC_YV12:
645         case VLC_CODEC_I420:
646         case VLC_CODEC_J420:
647             p_format->i_bits_per_pixel = 12;
648             break;
649         case VLC_CODEC_I410:
650             p_format->i_bits_per_pixel = 9;
651             break;
652         case VLC_CODEC_Y211:
653             p_format->i_bits_per_pixel = 8;
654             break;
655         case VLC_CODEC_YUVP:
656             p_format->i_bits_per_pixel = 8;
657             break;
658
659         case VLC_CODEC_RGB32:
660         case VLC_CODEC_RGBA:
661             p_format->i_bits_per_pixel = 32;
662             break;
663         case VLC_CODEC_RGB24:
664             p_format->i_bits_per_pixel = 24;
665             break;
666         case VLC_CODEC_RGB15:
667         case VLC_CODEC_RGB16:
668             p_format->i_bits_per_pixel = 16;
669             break;
670         case VLC_CODEC_RGB8:
671             p_format->i_bits_per_pixel = 8;
672             break;
673
674         case VLC_CODEC_GREY:
675         case VLC_CODEC_RGBP:
676             p_format->i_bits_per_pixel = 8;
677             break;
678
679         default:
680             p_format->i_bits_per_pixel = 0;
681             break;
682     }
683 }
684
685 /**
686  * Initialise the picture_t fields given chroma/size.
687  *
688  * This function initializes most of the picture_t fields given a chroma and
689  * size. It makes the assumption that stride == width.
690  * \param p_this The calling object
691  * \param p_pic Pointer to the picture to initialize
692  * \param i_chroma The chroma fourcc to set
693  * \param i_width The width of the picture
694  * \param i_height The height of the picture
695  * \param i_aspect The aspect ratio of the picture
696  */
697 int __vout_InitPicture( vlc_object_t *p_this, picture_t *p_pic,
698                         vlc_fourcc_t i_chroma,
699                         int i_width, int i_height, int i_aspect )
700 {
701     int i_index, i_width_aligned, i_height_aligned;
702
703     /* Store default values */
704     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
705     {
706         p_pic->p[i_index].p_pixels = NULL;
707         p_pic->p[i_index].i_pixel_pitch = 1;
708     }
709
710     p_pic->pf_release = NULL;
711     p_pic->pf_lock = NULL;
712     p_pic->pf_unlock = NULL;
713     p_pic->i_refcount = 0;
714
715     p_pic->i_qtype = QTYPE_NONE;
716     p_pic->i_qstride = 0;
717     p_pic->p_q = NULL;
718
719     vout_InitFormat( &p_pic->format, i_chroma, i_width, i_height, i_aspect );
720
721     /* Make sure the real dimensions are a multiple of 16 */
722     i_width_aligned = (i_width + 15) >> 4 << 4;
723     i_height_aligned = (i_height + 15) >> 4 << 4;
724
725     /* Calculate coordinates */
726     switch( vlc_fourcc_GetCodec( VIDEO_ES, i_chroma ) )
727     {
728         case VLC_CODEC_I411:
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;
734             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
735             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
736             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
737             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
738             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
739             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
740             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
741             p_pic->i_planes = 3;
742             break;
743
744         case VLC_CODEC_I410:
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 VLC_CODEC_YV12:
761         case VLC_CODEC_I420:
762         case VLC_CODEC_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 VLC_CODEC_I422:
779         case VLC_CODEC_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 VLC_CODEC_I440:
796         case VLC_CODEC_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 VLC_CODEC_I444:
813         case VLC_CODEC_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 VLC_CODEC_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 VLC_CODEC_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 VLC_CODEC_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 VLC_CODEC_UYVY:
868         case VLC_CODEC_VYUY:
869         case VLC_CODEC_YUYV:
870         case VLC_CODEC_YVYU:
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 * 2;
874             p_pic->p->i_visible_pitch = i_width * 2;
875             p_pic->p->i_pixel_pitch = 4;
876             p_pic->i_planes = 1;
877             break;
878
879         case VLC_CODEC_RGB8:
880             p_pic->p->i_lines = i_height_aligned;
881             p_pic->p->i_visible_lines = i_height;
882             p_pic->p->i_pitch = i_width_aligned;
883             p_pic->p->i_visible_pitch = i_width;
884             p_pic->p->i_pixel_pitch = 1;
885             p_pic->i_planes = 1;
886             break;
887
888         case VLC_CODEC_RGB15:
889             p_pic->p->i_lines = i_height_aligned;
890             p_pic->p->i_visible_lines = i_height;
891             p_pic->p->i_pitch = i_width_aligned * 2;
892             p_pic->p->i_visible_pitch = i_width * 2;
893             p_pic->p->i_pixel_pitch = 2;
894             p_pic->i_planes = 1;
895             break;
896
897         case VLC_CODEC_RGB16:
898             p_pic->p->i_lines = i_height_aligned;
899             p_pic->p->i_visible_lines = i_height;
900             p_pic->p->i_pitch = i_width_aligned * 2;
901             p_pic->p->i_visible_pitch = i_width * 2;
902             p_pic->p->i_pixel_pitch = 2;
903             p_pic->i_planes = 1;
904             break;
905
906         case VLC_CODEC_RGB24:
907             p_pic->p->i_lines = i_height_aligned;
908             p_pic->p->i_visible_lines = i_height;
909             p_pic->p->i_pitch = i_width_aligned * 3;
910             p_pic->p->i_visible_pitch = i_width * 3;
911             p_pic->p->i_pixel_pitch = 3;
912             p_pic->i_planes = 1;
913             break;
914
915         case VLC_CODEC_RGB32:
916         case VLC_CODEC_RGBA:
917             p_pic->p->i_lines = i_height_aligned;
918             p_pic->p->i_visible_lines = i_height;
919             p_pic->p->i_pitch = i_width_aligned * 4;
920             p_pic->p->i_visible_pitch = i_width * 4;
921             p_pic->p->i_pixel_pitch = 4;
922             p_pic->i_planes = 1;
923             break;
924
925         case VLC_CODEC_GREY:
926         case VLC_CODEC_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     static const vlc_fourcc_t p_I420[] = {
955         VLC_CODEC_I420, VLC_CODEC_YV12, VLC_CODEC_J420, 0
956     };
957     static const vlc_fourcc_t p_I422[] = {
958         VLC_CODEC_I422, VLC_CODEC_J422, 0
959     };
960     static const vlc_fourcc_t p_I440[] = {
961         VLC_CODEC_I440, VLC_CODEC_J440, 0
962     };
963     static const vlc_fourcc_t p_I444[] = {
964         VLC_CODEC_I444, VLC_CODEC_J444, 0
965     };
966     static const vlc_fourcc_t *pp_fcc[] = {
967         p_I420, p_I422, p_I440, p_I444, NULL
968     };
969
970     /* */
971     i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
972     i_amorhc = vlc_fourcc_GetCodec( VIDEO_ES, i_amorhc );
973
974     /* If they are the same, they are the same ! */
975     if( i_chroma == i_amorhc )
976         return 1;
977
978     /* Check for equivalence classes */
979     for( int i = 0; pp_fcc[i] != NULL; i++ )
980     {
981         bool b_fcc1 = false;
982         bool b_fcc2 = false;
983         for( int j = 0; pp_fcc[i][j] != 0; j++ )
984         {
985             if( i_chroma == pp_fcc[i][j] )
986                 b_fcc1 = true;
987             if( i_amorhc == pp_fcc[i][j] )
988                 b_fcc2 = true;
989         }
990         if( b_fcc1 && b_fcc2 )
991             return 1;
992     }
993     return 0;
994 }
995
996 /*****************************************************************************
997  *
998  *****************************************************************************/
999 static void PictureReleaseCallback( picture_t *p_picture )
1000 {
1001     if( --p_picture->i_refcount > 0 )
1002         return;
1003     picture_Delete( p_picture );
1004 }
1005
1006 /*****************************************************************************
1007  *
1008  *****************************************************************************/
1009 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
1010 {
1011     int i_index, i_width_aligned, i_height_aligned;
1012
1013     /* Store default values */
1014     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
1015     {
1016         p_picture->p[i_index].p_pixels = NULL;
1017         p_picture->p[i_index].i_pixel_pitch = 1;
1018     }
1019
1020     p_picture->pf_release = NULL;
1021     p_picture->pf_lock = NULL;
1022     p_picture->pf_unlock = NULL;
1023     p_picture->i_refcount = 0;
1024
1025     p_picture->i_qtype = QTYPE_NONE;
1026     p_picture->i_qstride = 0;
1027     p_picture->p_q = NULL;
1028
1029     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height, i_aspect );
1030
1031     /* Make sure the real dimensions are a multiple of 16 */
1032     i_width_aligned = (i_width + 15) >> 4 << 4;
1033     i_height_aligned = (i_height + 15) >> 4 << 4;
1034
1035     /* Calculate coordinates */
1036     switch( vlc_fourcc_GetCodec( VIDEO_ES, i_chroma ) )
1037     {
1038     case VLC_CODEC_I411:
1039         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
1040         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
1041         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
1042         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
1043         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
1044         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
1045         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
1046         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
1047         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
1048         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
1049         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
1050         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
1051         p_picture->i_planes = 3;
1052         break;
1053
1054     case VLC_CODEC_I410:
1055         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
1056         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
1057         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
1058         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
1059         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 4;
1060         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 4;
1061         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
1062         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
1063         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 4;
1064         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 4;
1065         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
1066         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
1067         p_picture->i_planes = 3;
1068         break;
1069
1070     case VLC_CODEC_YV12:
1071     case VLC_CODEC_I420:
1072     case VLC_CODEC_J420:
1073         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
1074         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
1075         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
1076         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
1077         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
1078         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
1079         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
1080         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
1081         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
1082         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
1083         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
1084         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
1085         p_picture->i_planes = 3;
1086         break;
1087
1088     case VLC_CODEC_I422:
1089     case VLC_CODEC_J422:
1090         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
1091         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
1092         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
1093         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
1094         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
1095         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
1096         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
1097         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
1098         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
1099         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
1100         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
1101         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
1102         p_picture->i_planes = 3;
1103         break;
1104
1105     case VLC_CODEC_I440:
1106     case VLC_CODEC_J440:
1107         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
1108         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
1109         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
1110         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
1111         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
1112         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
1113         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
1114         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
1115         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
1116         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
1117         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
1118         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
1119         p_picture->i_planes = 3;
1120         break;
1121
1122     case VLC_CODEC_I444:
1123     case VLC_CODEC_J444:
1124         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
1125         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
1126         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
1127         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
1128         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
1129         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
1130         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
1131         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
1132         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
1133         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
1134         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
1135         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
1136         p_picture->i_planes = 3;
1137         break;
1138
1139     case VLC_CODEC_YUVA:
1140         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
1141         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
1142         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
1143         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
1144         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
1145         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
1146         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
1147         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
1148         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
1149         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
1150         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
1151         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
1152         p_picture->p[ A_PLANE ].i_lines = i_height_aligned;
1153         p_picture->p[ A_PLANE ].i_visible_lines = i_height;
1154         p_picture->p[ A_PLANE ].i_pitch = i_width_aligned;
1155         p_picture->p[ A_PLANE ].i_visible_pitch = i_width;
1156         p_picture->i_planes = 4;
1157         break;
1158
1159     case VLC_CODEC_YUVP:
1160         p_picture->p->i_lines = i_height_aligned;
1161         p_picture->p->i_visible_lines = i_height;
1162         p_picture->p->i_pitch = i_width_aligned;
1163         p_picture->p->i_visible_pitch = i_width;
1164         p_picture->p->i_pixel_pitch = 8;
1165         p_picture->i_planes = 1;
1166         break;
1167
1168     case VLC_CODEC_Y211:
1169         p_picture->p->i_lines = i_height_aligned;
1170         p_picture->p->i_visible_lines = i_height;
1171         p_picture->p->i_pitch = i_width_aligned;
1172         p_picture->p->i_visible_pitch = i_width;
1173         p_picture->p->i_pixel_pitch = 4;
1174         p_picture->i_planes = 1;
1175         break;
1176
1177     case VLC_CODEC_UYVY:
1178     case VLC_CODEC_VYUY:
1179     case VLC_CODEC_YUYV:
1180     case VLC_CODEC_YVYU:
1181         p_picture->p->i_lines = i_height_aligned;
1182         p_picture->p->i_visible_lines = i_height;
1183         p_picture->p->i_pitch = i_width_aligned * 2;
1184         p_picture->p->i_visible_pitch = i_width * 2;
1185         p_picture->p->i_pixel_pitch = 4;
1186         p_picture->i_planes = 1;
1187         break;
1188
1189     case VLC_CODEC_RGB8:
1190         p_picture->p->i_lines = i_height_aligned;
1191         p_picture->p->i_visible_lines = i_height;
1192         p_picture->p->i_pitch = i_width_aligned;
1193         p_picture->p->i_visible_pitch = i_width;
1194         p_picture->p->i_pixel_pitch = 1;
1195         p_picture->i_planes = 1;
1196         break;
1197
1198     case VLC_CODEC_RGB15:
1199         p_picture->p->i_lines = i_height_aligned;
1200         p_picture->p->i_visible_lines = i_height;
1201         p_picture->p->i_pitch = i_width_aligned * 2;
1202         p_picture->p->i_visible_pitch = i_width * 2;
1203         p_picture->p->i_pixel_pitch = 2;
1204         p_picture->i_planes = 1;
1205         break;
1206
1207     case VLC_CODEC_RGB16:
1208         p_picture->p->i_lines = i_height_aligned;
1209         p_picture->p->i_visible_lines = i_height;
1210         p_picture->p->i_pitch = i_width_aligned * 2;
1211         p_picture->p->i_visible_pitch = i_width * 2;
1212         p_picture->p->i_pixel_pitch = 2;
1213         p_picture->i_planes = 1;
1214         break;
1215
1216     case VLC_CODEC_RGB24:
1217         p_picture->p->i_lines = i_height_aligned;
1218         p_picture->p->i_visible_lines = i_height;
1219         p_picture->p->i_pitch = i_width_aligned * 3;
1220         p_picture->p->i_visible_pitch = i_width * 3;
1221         p_picture->p->i_pixel_pitch = 3;
1222         p_picture->i_planes = 1;
1223         break;
1224
1225     case VLC_CODEC_RGB32:
1226     case VLC_CODEC_RGBA:
1227         p_picture->p->i_lines = i_height_aligned;
1228         p_picture->p->i_visible_lines = i_height;
1229         p_picture->p->i_pitch = i_width_aligned * 4;
1230         p_picture->p->i_visible_pitch = i_width * 4;
1231         p_picture->p->i_pixel_pitch = 4;
1232         p_picture->i_planes = 1;
1233         break;
1234
1235     case VLC_CODEC_GREY:
1236     case VLC_CODEC_RGBP:
1237         p_picture->p->i_lines = i_height_aligned;
1238         p_picture->p->i_visible_lines = i_height;
1239         p_picture->p->i_pitch = i_width_aligned;
1240         p_picture->p->i_visible_pitch = i_width;
1241         p_picture->p->i_pixel_pitch = 1;
1242         p_picture->i_planes = 1;
1243         break;
1244
1245     default:
1246         p_picture->i_planes = 0;
1247         return VLC_EGENERIC;
1248     }
1249
1250     return VLC_SUCCESS;
1251 }
1252
1253 /*****************************************************************************
1254  *
1255  *****************************************************************************/
1256 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
1257 {
1258     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
1259     if( !p_picture )
1260         return NULL;
1261
1262     if( __vout_AllocatePicture( NULL, p_picture,
1263                                 i_chroma, i_width, i_height, i_aspect ) )
1264     {
1265         free( p_picture );
1266         return NULL;
1267     }
1268
1269     p_picture->i_refcount = 1;
1270     p_picture->pf_release = PictureReleaseCallback;
1271     p_picture->i_status = RESERVED_PICTURE;
1272
1273     return p_picture;
1274 }
1275
1276 /*****************************************************************************
1277  *
1278  *****************************************************************************/
1279 void picture_Delete( picture_t *p_picture )
1280 {
1281     assert( p_picture && p_picture->i_refcount == 0 );
1282
1283     free( p_picture->p_q );
1284     free( p_picture->p_data_orig );
1285     free( p_picture->p_sys );
1286     free( p_picture );
1287 }
1288
1289 /*****************************************************************************
1290  *
1291  *****************************************************************************/
1292 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
1293 {
1294     int i;
1295
1296     for( i = 0; i < p_src->i_planes ; i++ )
1297         plane_CopyPixels( p_dst->p+i, p_src->p+i );
1298 }
1299
1300 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
1301 {
1302     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
1303                                      p_src->i_visible_pitch );
1304     const unsigned i_height = __MIN( p_dst->i_visible_lines,
1305                                      p_src->i_visible_lines );
1306
1307     if( p_src->i_pitch == p_dst->i_pitch )
1308     {
1309         /* There are margins, but with the same width : perfect ! */
1310         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
1311                     p_src->i_pitch * i_height );
1312     }
1313     else
1314     {
1315         /* We need to proceed line by line */
1316         uint8_t *p_in = p_src->p_pixels;
1317         uint8_t *p_out = p_dst->p_pixels;
1318         int i_line;
1319
1320         assert( p_in );
1321         assert( p_out );
1322
1323         for( i_line = i_height; i_line--; )
1324         {
1325             vlc_memcpy( p_out, p_in, i_width );
1326             p_in += p_src->i_pitch;
1327             p_out += p_dst->i_pitch;
1328         }
1329     }
1330 }
1331
1332 /*****************************************************************************
1333  *
1334  *****************************************************************************/
1335 int picture_Export( vlc_object_t *p_obj,
1336                     block_t **pp_image,
1337                     video_format_t *p_fmt,
1338                     picture_t *p_picture,
1339                     vlc_fourcc_t i_format,
1340                     int i_override_width, int i_override_height )
1341 {
1342     /* */
1343     video_format_t fmt_in = p_picture->format;
1344     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
1345     {
1346         fmt_in.i_sar_num =
1347         fmt_in.i_sar_den = 1;
1348     }
1349
1350     /* */
1351     video_format_t fmt_out;
1352     memset( &fmt_out, 0, sizeof(fmt_out) );
1353     fmt_out.i_sar_num =
1354     fmt_out.i_sar_den = 1;
1355     fmt_out.i_chroma  = i_format;
1356     fmt_out.i_width   = i_override_width;
1357     fmt_out.i_height  = i_override_height;
1358
1359     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
1360     {
1361         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width / fmt_in.i_width;
1362         const int i_height = fmt_out.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
1363         if( i_height > 0 )
1364             fmt_out.i_height = i_height;
1365     }
1366     else
1367     {
1368         if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
1369         {
1370             fmt_out.i_width = fmt_in.i_width * fmt_out.i_height / fmt_in.i_height;
1371         }
1372         else
1373         {
1374             fmt_out.i_width = fmt_in.i_width;
1375             fmt_out.i_height = fmt_in.i_height;
1376         }
1377         const int i_width = fmt_out.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
1378         if( i_width > 0 )
1379             fmt_out.i_width = i_width;
1380     }
1381
1382     image_handler_t *p_image = image_HandlerCreate( p_obj );
1383
1384     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
1385
1386     image_HandlerDelete( p_image );
1387
1388     if( !p_block )
1389         return VLC_EGENERIC;
1390
1391     p_block->i_pts =
1392     p_block->i_dts = p_picture->date;
1393
1394     if( p_fmt )
1395         *p_fmt = fmt_out;
1396     *pp_image = p_block;
1397
1398     return VLC_SUCCESS;
1399 }
1400
1401 /*****************************************************************************
1402  *
1403  *****************************************************************************/
1404