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