]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
5c98c5872ae7a8ad14892767103cd3330c0657b0
[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         }
412
413         /* Convert image to the first direct buffer */
414         p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)p_tmp_pic;
415         p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
416
417         /* Render subpictures on the first direct buffer */
418         spu_RenderSubpictures( p_vout->p_spu,
419                                p_tmp_pic, &p_vout->fmt_out,
420                                p_subpic, &p_vout->fmt_in, b_paused );
421
422         if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
423             return NULL;
424
425         picture_Copy( &p_vout->p_picture[0], p_tmp_pic );
426     }
427     else
428     {
429         if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
430             return NULL;
431
432         /* Convert image to the first direct buffer */
433         p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)&p_vout->p_picture[0];
434         p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
435
436         /* Render subpictures on the first direct buffer */
437         spu_RenderSubpictures( p_vout->p_spu,
438                                &p_vout->p_picture[0], &p_vout->fmt_out,
439                                p_subpic, &p_vout->fmt_in, b_paused );
440     }
441
442     vout_UnlockPicture( p_vout, &p_vout->p_picture[0] );
443
444     return &p_vout->p_picture[0];
445 }
446
447 /**
448  * Calculate image window coordinates
449  *
450  * This function will be accessed by plugins. It calculates the relative
451  * position of the output window and the image window.
452  */
453 void vout_PlacePicture( const vout_thread_t *p_vout,
454                         unsigned int i_width, unsigned int i_height,
455                         unsigned int *restrict pi_x,
456                         unsigned int *restrict pi_y,
457                         unsigned int *restrict pi_width,
458                         unsigned int *restrict pi_height )
459 {
460     if( i_width <= 0 || i_height <= 0 )
461     {
462         *pi_width = *pi_height = *pi_x = *pi_y = 0;
463         return;
464     }
465
466     if( p_vout->b_autoscale )
467     {
468         *pi_width = i_width;
469         *pi_height = i_height;
470     }
471     else
472     {
473         int i_zoom = p_vout->i_zoom;
474         /* be realistic, scaling factor confined between .2 and 10. */
475         if( i_zoom > 10 * ZOOM_FP_FACTOR )      i_zoom = 10 * ZOOM_FP_FACTOR;
476         else if( i_zoom <  ZOOM_FP_FACTOR / 5 ) i_zoom = ZOOM_FP_FACTOR / 5;
477
478         unsigned int i_original_width, i_original_height;
479
480         if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
481         {
482             i_original_width = p_vout->fmt_in.i_visible_width *
483                                p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den;
484             i_original_height =  p_vout->fmt_in.i_visible_height;
485         }
486         else
487         {
488             i_original_width =  p_vout->fmt_in.i_visible_width;
489             i_original_height = p_vout->fmt_in.i_visible_height *
490                                 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num;
491         }
492 #ifdef WIN32
493         /* On windows, inner video window exceeding container leads to black screen */
494         *pi_width = __MIN( i_width, i_original_width * i_zoom / ZOOM_FP_FACTOR );
495         *pi_height = __MIN( i_height, i_original_height * i_zoom / ZOOM_FP_FACTOR );
496 #else
497         *pi_width = i_original_width * i_zoom / ZOOM_FP_FACTOR ;
498         *pi_height = i_original_height * i_zoom / ZOOM_FP_FACTOR ;
499 #endif
500     }
501
502     int64_t i_scaled_width = p_vout->fmt_in.i_visible_width * (int64_t)p_vout->fmt_in.i_sar_num *
503                               *pi_height / p_vout->fmt_in.i_visible_height / p_vout->fmt_in.i_sar_den;
504     int64_t i_scaled_height = p_vout->fmt_in.i_visible_height * (int64_t)p_vout->fmt_in.i_sar_den *
505                                *pi_width / p_vout->fmt_in.i_visible_width / p_vout->fmt_in.i_sar_num;
506
507     if( i_scaled_width <= 0 || i_scaled_height <= 0 )
508     {
509         msg_Warn( p_vout, "ignoring broken aspect ratio" );
510         i_scaled_width = *pi_width;
511         i_scaled_height = *pi_height;
512     }
513
514     if( i_scaled_width > *pi_width )
515         *pi_height = i_scaled_height;
516     else
517         *pi_width = i_scaled_width;
518
519     switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
520     {
521     case VOUT_ALIGN_LEFT:
522         *pi_x = 0;
523         break;
524     case VOUT_ALIGN_RIGHT:
525         *pi_x = i_width - *pi_width;
526         break;
527     default:
528         *pi_x = ( i_width - *pi_width ) / 2;
529     }
530
531     switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
532     {
533     case VOUT_ALIGN_TOP:
534         *pi_y = 0;
535         break;
536     case VOUT_ALIGN_BOTTOM:
537         *pi_y = i_height - *pi_height;
538         break;
539     default:
540         *pi_y = ( i_height - *pi_height ) / 2;
541     }
542 }
543
544 /**
545  * Allocate a new picture in the heap.
546  *
547  * This function allocates a fake direct buffer in memory, which can be
548  * used exactly like a video buffer. The video output thread then manages
549  * how it gets displayed.
550  */
551 int __vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
552                             vlc_fourcc_t i_chroma,
553                             int i_width, int i_height, int i_aspect )
554 {
555     int i_bytes, i_index, i_width_aligned, i_height_aligned;
556
557     /* Make sure the real dimensions are a multiple of 16 */
558     i_width_aligned = (i_width + 15) >> 4 << 4;
559     i_height_aligned = (i_height + 15) >> 4 << 4;
560
561     if( picture_Setup( p_pic, i_chroma,
562                        i_width, i_height, i_aspect ) != VLC_SUCCESS )
563     {
564         p_pic->i_planes = 0;
565         return VLC_EGENERIC;
566     }
567
568     /* Calculate how big the new image should be */
569     i_bytes = p_pic->format.i_bits_per_pixel *
570         i_width_aligned * i_height_aligned / 8;
571
572     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
573
574     if( p_pic->p_data == NULL )
575     {
576         p_pic->i_planes = 0;
577         return VLC_EGENERIC;
578     }
579
580     /* Fill the p_pixels field for each plane */
581     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
582
583     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
584     {
585         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
586             p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
587     }
588
589     return VLC_SUCCESS;
590 }
591
592 /**
593  * Compare two chroma values
594  *
595  * This function returns 1 if the two fourcc values given as argument are
596  * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
597  */
598 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
599 {
600     static const vlc_fourcc_t p_I420[] = {
601         VLC_CODEC_I420, VLC_CODEC_YV12, VLC_CODEC_J420, 0
602     };
603     static const vlc_fourcc_t p_I422[] = {
604         VLC_CODEC_I422, VLC_CODEC_J422, 0
605     };
606     static const vlc_fourcc_t p_I440[] = {
607         VLC_CODEC_I440, VLC_CODEC_J440, 0
608     };
609     static const vlc_fourcc_t p_I444[] = {
610         VLC_CODEC_I444, VLC_CODEC_J444, 0
611     };
612     static const vlc_fourcc_t *pp_fcc[] = {
613         p_I420, p_I422, p_I440, p_I444, NULL
614     };
615
616     /* */
617     i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
618     i_amorhc = vlc_fourcc_GetCodec( VIDEO_ES, i_amorhc );
619
620     /* If they are the same, they are the same ! */
621     if( i_chroma == i_amorhc )
622         return 1;
623
624     /* Check for equivalence classes */
625     for( int i = 0; pp_fcc[i] != NULL; i++ )
626     {
627         bool b_fcc1 = false;
628         bool b_fcc2 = false;
629         for( int j = 0; pp_fcc[i][j] != 0; j++ )
630         {
631             if( i_chroma == pp_fcc[i][j] )
632                 b_fcc1 = true;
633             if( i_amorhc == pp_fcc[i][j] )
634                 b_fcc2 = true;
635         }
636         if( b_fcc1 && b_fcc2 )
637             return 1;
638     }
639     return 0;
640 }
641
642 /*****************************************************************************
643  *
644  *****************************************************************************/
645 static void PictureReleaseCallback( picture_t *p_picture )
646 {
647     if( --p_picture->i_refcount > 0 )
648         return;
649     picture_Delete( p_picture );
650 }
651
652 /*****************************************************************************
653  *
654  *****************************************************************************/
655 void picture_Reset( picture_t *p_picture )
656 {
657     /* */
658     p_picture->date = VLC_TS_INVALID;
659     p_picture->b_force = false;
660     p_picture->b_progressive = false;
661     p_picture->i_nb_fields = 0;
662     p_picture->b_top_field_first = false;
663     picture_CleanupQuant( p_picture );
664 }
665
666 /*****************************************************************************
667  *
668  *****************************************************************************/
669 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
670 {
671     int i_index, i_width_aligned, i_height_aligned;
672
673     /* Store default values */
674     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
675     {
676         p_picture->p[i_index].p_pixels = NULL;
677         p_picture->p[i_index].i_pixel_pitch = 1;
678     }
679
680     p_picture->pf_release = NULL;
681     p_picture->p_release_sys = NULL;
682     p_picture->pf_lock = NULL;
683     p_picture->pf_unlock = NULL;
684     p_picture->i_refcount = 0;
685
686     p_picture->i_qtype = QTYPE_NONE;
687     p_picture->i_qstride = 0;
688     p_picture->p_q = NULL;
689
690     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height, i_aspect );
691
692     /* Make sure the real dimensions are a multiple of 16 */
693     i_width_aligned = (i_width + 15) >> 4 << 4;
694     i_height_aligned = (i_height + 15) >> 4 << 4;
695
696     /* Calculate coordinates */
697     switch( vlc_fourcc_GetCodec( VIDEO_ES, i_chroma ) )
698     {
699     case VLC_CODEC_I411:
700         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
701         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
702         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
703         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
704         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
705         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
706         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
707         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
708         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
709         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
710         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
711         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
712         p_picture->i_planes = 3;
713         break;
714
715     case VLC_CODEC_I410:
716         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
717         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
718         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
719         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
720         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 4;
721         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 4;
722         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
723         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
724         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 4;
725         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 4;
726         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
727         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
728         p_picture->i_planes = 3;
729         break;
730
731     case VLC_CODEC_YV12:
732     case VLC_CODEC_I420:
733     case VLC_CODEC_J420:
734         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
735         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
736         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
737         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
738         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
739         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
740         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
741         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
742         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
743         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
744         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
745         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
746         p_picture->i_planes = 3;
747         break;
748
749     case VLC_CODEC_I422:
750     case VLC_CODEC_J422:
751         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
752         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
753         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
754         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
755         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
756         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
757         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
758         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
759         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
760         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
761         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
762         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
763         p_picture->i_planes = 3;
764         break;
765
766     case VLC_CODEC_I440:
767     case VLC_CODEC_J440:
768         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
769         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
770         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
771         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
772         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
773         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
774         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
775         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
776         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
777         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
778         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
779         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
780         p_picture->i_planes = 3;
781         break;
782
783     case VLC_CODEC_I444:
784     case VLC_CODEC_J444:
785         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
786         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
787         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
788         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
789         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
790         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
791         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
792         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
793         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
794         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
795         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
796         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
797         p_picture->i_planes = 3;
798         break;
799
800     case VLC_CODEC_YUVA:
801         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
802         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
803         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
804         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
805         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
806         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
807         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
808         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
809         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
810         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
811         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
812         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
813         p_picture->p[ A_PLANE ].i_lines = i_height_aligned;
814         p_picture->p[ A_PLANE ].i_visible_lines = i_height;
815         p_picture->p[ A_PLANE ].i_pitch = i_width_aligned;
816         p_picture->p[ A_PLANE ].i_visible_pitch = i_width;
817         p_picture->i_planes = 4;
818         break;
819
820     case VLC_CODEC_YUVP:
821         p_picture->p->i_lines = i_height_aligned;
822         p_picture->p->i_visible_lines = i_height;
823         p_picture->p->i_pitch = i_width_aligned;
824         p_picture->p->i_visible_pitch = i_width;
825         p_picture->p->i_pixel_pitch = 8;
826         p_picture->i_planes = 1;
827         break;
828
829     case VLC_CODEC_Y211:
830         p_picture->p->i_lines = i_height_aligned;
831         p_picture->p->i_visible_lines = i_height;
832         p_picture->p->i_pitch = i_width_aligned;
833         p_picture->p->i_visible_pitch = i_width;
834         p_picture->p->i_pixel_pitch = 4;
835         p_picture->i_planes = 1;
836         break;
837
838     case VLC_CODEC_UYVY:
839     case VLC_CODEC_VYUY:
840     case VLC_CODEC_YUYV:
841     case VLC_CODEC_YVYU:
842         p_picture->p->i_lines = i_height_aligned;
843         p_picture->p->i_visible_lines = i_height;
844         p_picture->p->i_pitch = i_width_aligned * 2;
845         p_picture->p->i_visible_pitch = i_width * 2;
846         p_picture->p->i_pixel_pitch = 4;
847         p_picture->i_planes = 1;
848         break;
849
850     case VLC_CODEC_RGB8:
851         p_picture->p->i_lines = i_height_aligned;
852         p_picture->p->i_visible_lines = i_height;
853         p_picture->p->i_pitch = i_width_aligned;
854         p_picture->p->i_visible_pitch = i_width;
855         p_picture->p->i_pixel_pitch = 1;
856         p_picture->i_planes = 1;
857         break;
858
859     case VLC_CODEC_RGB15:
860         p_picture->p->i_lines = i_height_aligned;
861         p_picture->p->i_visible_lines = i_height;
862         p_picture->p->i_pitch = i_width_aligned * 2;
863         p_picture->p->i_visible_pitch = i_width * 2;
864         p_picture->p->i_pixel_pitch = 2;
865         p_picture->i_planes = 1;
866         break;
867
868     case VLC_CODEC_RGB16:
869         p_picture->p->i_lines = i_height_aligned;
870         p_picture->p->i_visible_lines = i_height;
871         p_picture->p->i_pitch = i_width_aligned * 2;
872         p_picture->p->i_visible_pitch = i_width * 2;
873         p_picture->p->i_pixel_pitch = 2;
874         p_picture->i_planes = 1;
875         break;
876
877     case VLC_CODEC_RGB24:
878         p_picture->p->i_lines = i_height_aligned;
879         p_picture->p->i_visible_lines = i_height;
880         p_picture->p->i_pitch = i_width_aligned * 3;
881         p_picture->p->i_visible_pitch = i_width * 3;
882         p_picture->p->i_pixel_pitch = 3;
883         p_picture->i_planes = 1;
884         break;
885
886     case VLC_CODEC_RGB32:
887     case VLC_CODEC_RGBA:
888         p_picture->p->i_lines = i_height_aligned;
889         p_picture->p->i_visible_lines = i_height;
890         p_picture->p->i_pitch = i_width_aligned * 4;
891         p_picture->p->i_visible_pitch = i_width * 4;
892         p_picture->p->i_pixel_pitch = 4;
893         p_picture->i_planes = 1;
894         break;
895
896     case VLC_CODEC_GREY:
897     case VLC_CODEC_RGBP:
898         p_picture->p->i_lines = i_height_aligned;
899         p_picture->p->i_visible_lines = i_height;
900         p_picture->p->i_pitch = i_width_aligned;
901         p_picture->p->i_visible_pitch = i_width;
902         p_picture->p->i_pixel_pitch = 1;
903         p_picture->i_planes = 1;
904         break;
905
906     default:
907         p_picture->i_planes = 0;
908         return VLC_EGENERIC;
909     }
910
911     return VLC_SUCCESS;
912 }
913
914 /*****************************************************************************
915  *
916  *****************************************************************************/
917 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
918 {
919     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
920     if( !p_picture )
921         return NULL;
922
923     if( __vout_AllocatePicture( NULL, p_picture,
924                                 i_chroma, i_width, i_height, i_aspect ) )
925     {
926         free( p_picture );
927         return NULL;
928     }
929
930     p_picture->i_refcount = 1;
931     p_picture->pf_release = PictureReleaseCallback;
932     p_picture->i_status = RESERVED_PICTURE;
933
934     return p_picture;
935 }
936
937 /*****************************************************************************
938  *
939  *****************************************************************************/
940 void picture_Delete( picture_t *p_picture )
941 {
942     assert( p_picture && p_picture->i_refcount == 0 );
943     assert( p_picture->p_release_sys == NULL );
944
945     free( p_picture->p_q );
946     free( p_picture->p_data_orig );
947     free( p_picture->p_sys );
948     free( p_picture );
949 }
950
951 /*****************************************************************************
952  *
953  *****************************************************************************/
954 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
955 {
956     int i;
957
958     for( i = 0; i < p_src->i_planes ; i++ )
959         plane_CopyPixels( p_dst->p+i, p_src->p+i );
960 }
961
962 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
963 {
964     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
965                                      p_src->i_visible_pitch );
966     const unsigned i_height = __MIN( p_dst->i_visible_lines,
967                                      p_src->i_visible_lines );
968
969     if( p_src->i_pitch == p_dst->i_pitch )
970     {
971         /* There are margins, but with the same width : perfect ! */
972         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
973                     p_src->i_pitch * i_height );
974     }
975     else
976     {
977         /* We need to proceed line by line */
978         uint8_t *p_in = p_src->p_pixels;
979         uint8_t *p_out = p_dst->p_pixels;
980         int i_line;
981
982         assert( p_in );
983         assert( p_out );
984
985         for( i_line = i_height; i_line--; )
986         {
987             vlc_memcpy( p_out, p_in, i_width );
988             p_in += p_src->i_pitch;
989             p_out += p_dst->i_pitch;
990         }
991     }
992 }
993
994 /*****************************************************************************
995  *
996  *****************************************************************************/
997 int picture_Export( vlc_object_t *p_obj,
998                     block_t **pp_image,
999                     video_format_t *p_fmt,
1000                     picture_t *p_picture,
1001                     vlc_fourcc_t i_format,
1002                     int i_override_width, int i_override_height )
1003 {
1004     /* */
1005     video_format_t fmt_in = p_picture->format;
1006     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
1007     {
1008         fmt_in.i_sar_num =
1009         fmt_in.i_sar_den = 1;
1010     }
1011
1012     /* */
1013     video_format_t fmt_out;
1014     memset( &fmt_out, 0, sizeof(fmt_out) );
1015     fmt_out.i_sar_num =
1016     fmt_out.i_sar_den = 1;
1017     fmt_out.i_chroma  = i_format;
1018
1019     /* compute original width/height */
1020     unsigned int i_original_width;
1021     unsigned int i_original_height;
1022     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
1023     {
1024         i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
1025         i_original_height = fmt_in.i_height;
1026     }
1027     else
1028     {
1029         i_original_width =  fmt_in.i_width;
1030         i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
1031     }
1032
1033     /* */
1034     fmt_out.i_width  = ( i_override_width < 0 ) ?
1035                        i_original_width : i_override_width;
1036     fmt_out.i_height = ( i_override_height < 0 ) ?
1037                        i_original_height : i_override_height;
1038
1039     /* scale if only one direction is provided */
1040     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
1041     {
1042         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
1043                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
1044     }
1045     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
1046     {
1047         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
1048                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
1049     }
1050
1051     image_handler_t *p_image = image_HandlerCreate( p_obj );
1052
1053     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
1054
1055     image_HandlerDelete( p_image );
1056
1057     if( !p_block )
1058         return VLC_EGENERIC;
1059
1060     p_block->i_pts =
1061     p_block->i_dts = p_picture->date;
1062
1063     if( p_fmt )
1064         *p_fmt = fmt_out;
1065     *pp_image = p_block;
1066
1067     return VLC_SUCCESS;
1068 }
1069
1070 /*****************************************************************************
1071  *
1072  *****************************************************************************/