vout_pictures: Use unsigned for width and height in picture_Export.
[vlc.git] / 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 #include <vlc_picture_pool.h>
43
44 #include "vout_pictures.h"
45 #include "vout_internal.h"
46
47 /**
48  * Display a picture
49  *
50  * Remove the reservation flag of a picture, which will cause it to be ready
51  * for display.
52  */
53 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
54 {
55     vlc_mutex_lock( &p_vout->picture_lock );
56
57     if( p_pic->i_status == RESERVED_PICTURE )
58     {
59         p_pic->i_status = READY_PICTURE;
60         vlc_cond_signal( &p_vout->p->picture_wait );
61     }
62     else
63     {
64         msg_Err( p_vout, "picture to display %p has invalid status %d",
65                          p_pic, p_pic->i_status );
66     }
67     p_vout->p->i_picture_qtype = p_pic->i_qtype;
68
69     vlc_mutex_unlock( &p_vout->picture_lock );
70 }
71
72 /**
73  * Allocate a picture in the video output heap.
74  *
75  * This function creates a reserved image in the video output heap.
76  * A null pointer is returned if the function fails. This method provides an
77  * already allocated zone of memory in the picture data fields.
78  * It needs locking since several pictures can be created by several producers
79  * threads.
80  */
81 int vout_CountPictureAvailable( vout_thread_t *p_vout )
82 {
83     int i_free = 0;
84     int i_pic;
85
86     vlc_mutex_lock( &p_vout->picture_lock );
87     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
88     {
89         picture_t *p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1) % I_RENDERPICTURES];
90
91         switch( p_pic->i_status )
92         {
93             case DESTROYED_PICTURE:
94                 i_free++;
95                 break;
96
97             case FREE_PICTURE:
98                 i_free++;
99                 break;
100
101             default:
102                 break;
103         }
104     }
105     vlc_mutex_unlock( &p_vout->picture_lock );
106
107     return i_free;
108 }
109
110 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
111                                bool b_progressive,
112                                bool b_top_field_first,
113                                unsigned int i_nb_fields )
114 {
115     int         i_pic;                                      /* picture index */
116     picture_t * p_pic;
117     picture_t * p_freepic = NULL;                      /* first free picture */
118
119     /* Get lock */
120     vlc_mutex_lock( &p_vout->picture_lock );
121
122     /*
123      * Look for an empty place in the picture heap.
124      */
125     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
126     {
127         p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
128                                  % I_RENDERPICTURES];
129
130         switch( p_pic->i_status )
131         {
132             case DESTROYED_PICTURE:
133                 /* Memory will not be reallocated, and function can end
134                  * immediately - this is the best possible case, since no
135                  * memory allocation needs to be done */
136                 p_pic->i_status   = RESERVED_PICTURE;
137                 p_pic->i_refcount = 0;
138                 p_pic->b_force    = 0;
139
140                 p_pic->b_progressive        = b_progressive;
141                 p_pic->i_nb_fields          = i_nb_fields;
142                 p_pic->b_top_field_first    = b_top_field_first;
143
144                 p_vout->i_heap_size++;
145                 p_vout->render.i_last_used_pic =
146                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
147                     % I_RENDERPICTURES;
148                 vlc_mutex_unlock( &p_vout->picture_lock );
149                 return( p_pic );
150
151             case FREE_PICTURE:
152                 /* Picture is empty and ready for allocation */
153                 p_vout->render.i_last_used_pic =
154                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
155                     % I_RENDERPICTURES;
156                 p_freepic = p_pic;
157                 break;
158
159             default:
160                 break;
161         }
162     }
163
164     /*
165      * Prepare picture
166      */
167     if( p_freepic != NULL )
168     {
169         vout_AllocatePicture( VLC_OBJECT(p_vout),
170                               p_freepic, p_vout->render.i_chroma,
171                               p_vout->render.i_width, p_vout->render.i_height,
172                               p_vout->render.i_aspect );
173
174         if( p_freepic->i_planes )
175         {
176             /* Copy picture information, set some default values */
177             p_freepic->i_status   = RESERVED_PICTURE;
178             p_freepic->i_type     = MEMORY_PICTURE;
179             p_freepic->b_slow     = 0;
180
181             p_freepic->i_refcount = 0;
182             p_freepic->b_force = 0;
183
184             p_freepic->b_progressive        = b_progressive;
185             p_freepic->i_nb_fields          = i_nb_fields;
186             p_freepic->b_top_field_first    = b_top_field_first;
187
188             p_vout->i_heap_size++;
189         }
190         else
191         {
192             /* Memory allocation failed : set picture as empty */
193             p_freepic->i_status = FREE_PICTURE;
194             p_freepic = NULL;
195
196             msg_Err( p_vout, "picture allocation failed" );
197         }
198
199         vlc_mutex_unlock( &p_vout->picture_lock );
200
201         return( p_freepic );
202     }
203
204     /* No free or destroyed picture could be found, but the decoder
205      * will try again in a while. */
206     vlc_mutex_unlock( &p_vout->picture_lock );
207
208     return( NULL );
209 }
210
211 /* */
212 static void DestroyPicture( vout_thread_t *p_vout, picture_t *p_picture )
213 {
214     vlc_assert_locked( &p_vout->picture_lock );
215
216     p_picture->i_status = DESTROYED_PICTURE;
217     p_vout->i_heap_size--;
218     picture_CleanupQuant( p_picture );
219
220     vlc_cond_signal( &p_vout->p->picture_wait );
221 }
222
223 /**
224  * Remove a permanent or reserved picture from the heap
225  *
226  * This function frees a previously reserved picture or a permanent
227  * picture. It is meant to be used when the construction of a picture aborted.
228  * Note that the picture will be destroyed even if it is linked !
229  *
230  * TODO remove it, vout_DropPicture should be used instead
231  */
232 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
233 {
234 #ifndef NDEBUG
235     /* Check if picture status is valid */
236     vlc_mutex_lock( &p_vout->picture_lock );
237     if( p_pic->i_status != RESERVED_PICTURE )
238     {
239         msg_Err( p_vout, "picture to destroy %p has invalid status %d",
240                          p_pic, p_pic->i_status );
241     }
242     vlc_mutex_unlock( &p_vout->picture_lock );
243 #endif
244
245     vout_DropPicture( p_vout, p_pic );
246 }
247
248 /* */
249 void vout_UsePictureLocked( vout_thread_t *p_vout, picture_t *p_picture )
250 {
251     vlc_assert_locked( &p_vout->picture_lock );
252     if( p_picture->i_refcount > 0 )
253     {
254         /* Pretend we displayed the picture, but don't destroy
255          * it since the decoder might still need it. */
256         p_picture->i_status = DISPLAYED_PICTURE;
257     }
258     else
259     {
260         /* Destroy the picture without displaying it */
261         DestroyPicture( p_vout, p_picture );
262     }
263 }
264
265 /* */
266 void vout_DropPicture( vout_thread_t *p_vout, picture_t *p_pic  )
267 {
268     vlc_mutex_lock( &p_vout->picture_lock );
269
270     if( p_pic->i_status == READY_PICTURE )
271     {
272         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
273         p_pic->date = 1;
274         vlc_cond_signal( &p_vout->p->picture_wait );
275     }
276     else
277     {
278         vout_UsePictureLocked( p_vout, p_pic );
279     }
280
281     vlc_mutex_unlock( &p_vout->picture_lock );
282 }
283
284 /**
285  * Increment reference counter of a picture
286  *
287  * This function increments the reference counter of a picture in the video
288  * heap. It needs a lock since several producer threads can access the picture.
289  */
290 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
291 {
292     vlc_mutex_lock( &p_vout->picture_lock );
293     p_pic->i_refcount++;
294     vlc_mutex_unlock( &p_vout->picture_lock );
295 }
296
297 /**
298  * Decrement reference counter of a picture
299  *
300  * This function decrement the reference counter of a picture in the video heap
301  */
302 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
303 {
304     vlc_mutex_lock( &p_vout->picture_lock );
305
306     if( p_pic->i_refcount > 0 )
307         p_pic->i_refcount--;
308     else
309         msg_Err( p_vout, "Invalid picture reference count (%p, %d)",
310                  p_pic, p_pic->i_refcount );
311
312     if( p_pic->i_refcount == 0 && p_pic->i_status == DISPLAYED_PICTURE )
313         DestroyPicture( p_vout, p_pic );
314
315     vlc_mutex_unlock( &p_vout->picture_lock );
316 }
317
318 static int vout_LockPicture( vout_thread_t *p_vout, picture_t *p_picture )
319 {
320     if( p_picture->pf_lock )
321         return p_picture->pf_lock( p_vout, p_picture );
322     return VLC_SUCCESS;
323 }
324 static void vout_UnlockPicture( vout_thread_t *p_vout, picture_t *p_picture )
325 {
326     if( p_picture->pf_unlock )
327         p_picture->pf_unlock( p_vout, p_picture );
328 }
329
330 /**
331  * Render a picture
332  *
333  * This function chooses whether the current picture needs to be copied
334  * before rendering, does the subpicture magic, and tells the video output
335  * thread which direct buffer needs to be displayed.
336  */
337 picture_t *vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
338                                subpicture_t *p_subpic, mtime_t render_date )
339 {
340     if( p_pic == NULL )
341         return NULL;
342
343     if( p_pic->i_type == DIRECT_PICTURE )
344     {
345         /* Picture is in a direct buffer. */
346
347         if( p_subpic != NULL )
348         {
349             /* We have subtitles. First copy the picture to
350              * the spare direct buffer, then render the
351              * subtitles. */
352             if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
353                 return NULL;
354
355             picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
356
357             spu_RenderSubpictures( p_vout->p_spu,
358                                    PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
359                                    p_subpic, &p_vout->fmt_in, render_date );
360
361             vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
362
363             return PP_OUTPUTPICTURE[0];
364         }
365
366         /* No subtitles, picture is in a directbuffer so
367          * we can display it directly (even if it is still
368          * in use or not). */
369         return p_pic;
370     }
371
372     /* Not a direct buffer. We either need to copy it to a direct buffer,
373      * or render it if the chroma isn't the same. */
374     if( p_vout->p->b_direct )
375     {
376         /* Picture is not in a direct buffer, but is exactly the
377          * same size as the direct buffers. A memcpy() is enough,
378          * then render the subtitles. */
379
380         if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
381             return NULL;
382
383         picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
384         spu_RenderSubpictures( p_vout->p_spu,
385                                PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
386                                p_subpic, &p_vout->fmt_in, render_date );
387
388         vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
389
390         return PP_OUTPUTPICTURE[0];
391     }
392
393     /* Picture is not in a direct buffer, and needs to be converted to
394      * another size/chroma. Then the subtitles need to be rendered as
395      * well. This usually means software YUV, or hardware YUV with a
396      * different chroma. */
397
398     if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
399     {
400         /* The picture buffer is in slow memory. We'll use
401          * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
402          * one for subpictures rendering. */
403         picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
404         if( p_tmp_pic->i_status == FREE_PICTURE )
405         {
406             vout_AllocatePicture( VLC_OBJECT(p_vout),
407                                   p_tmp_pic, p_vout->fmt_out.i_chroma,
408                                   p_vout->fmt_out.i_width,
409                                   p_vout->fmt_out.i_height,
410                                   p_vout->fmt_out.i_aspect );
411             p_tmp_pic->i_type = MEMORY_PICTURE;
412             p_tmp_pic->i_status = RESERVED_PICTURE;
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, render_date );
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, render_date );
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     VLC_UNUSED(p_this);
558     int i_bytes, i_index, i_width_aligned, i_height_aligned;
559
560     /* Make sure the real dimensions are a multiple of 16 */
561     i_width_aligned = (i_width + 15) >> 4 << 4;
562     i_height_aligned = (i_height + 15) >> 4 << 4;
563
564     if( picture_Setup( p_pic, i_chroma,
565                        i_width, i_height, i_aspect ) != VLC_SUCCESS )
566     {
567         p_pic->i_planes = 0;
568         return VLC_EGENERIC;
569     }
570
571     /* Calculate how big the new image should be */
572     i_bytes = p_pic->format.i_bits_per_pixel *
573         i_width_aligned * i_height_aligned / 8;
574
575     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
576
577     if( p_pic->p_data == NULL )
578     {
579         p_pic->i_planes = 0;
580         return VLC_EGENERIC;
581     }
582
583     /* Fill the p_pixels field for each plane */
584     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
585
586     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
587     {
588         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
589             p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
590     }
591
592     return VLC_SUCCESS;
593 }
594
595 /**
596  * Compare two chroma values
597  *
598  * This function returns 1 if the two fourcc values given as argument are
599  * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
600  */
601 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
602 {
603     static const vlc_fourcc_t p_I420[] = {
604         VLC_CODEC_I420, VLC_CODEC_YV12, VLC_CODEC_J420, 0
605     };
606     static const vlc_fourcc_t p_I422[] = {
607         VLC_CODEC_I422, VLC_CODEC_J422, 0
608     };
609     static const vlc_fourcc_t p_I440[] = {
610         VLC_CODEC_I440, VLC_CODEC_J440, 0
611     };
612     static const vlc_fourcc_t p_I444[] = {
613         VLC_CODEC_I444, VLC_CODEC_J444, 0
614     };
615     static const vlc_fourcc_t *pp_fcc[] = {
616         p_I420, p_I422, p_I440, p_I444, NULL
617     };
618
619     /* */
620     i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
621     i_amorhc = vlc_fourcc_GetCodec( VIDEO_ES, i_amorhc );
622
623     /* If they are the same, they are the same ! */
624     if( i_chroma == i_amorhc )
625         return 1;
626
627     /* Check for equivalence classes */
628     for( int i = 0; pp_fcc[i] != NULL; i++ )
629     {
630         bool b_fcc1 = false;
631         bool b_fcc2 = false;
632         for( int j = 0; pp_fcc[i][j] != 0; j++ )
633         {
634             if( i_chroma == pp_fcc[i][j] )
635                 b_fcc1 = true;
636             if( i_amorhc == pp_fcc[i][j] )
637                 b_fcc2 = true;
638         }
639         if( b_fcc1 && b_fcc2 )
640             return 1;
641     }
642     return 0;
643 }
644
645 /*****************************************************************************
646  *
647  *****************************************************************************/
648 static void PictureReleaseCallback( picture_t *p_picture )
649 {
650     if( --p_picture->i_refcount > 0 )
651         return;
652     picture_Delete( p_picture );
653 }
654
655 /*****************************************************************************
656  *
657  *****************************************************************************/
658 void picture_Reset( picture_t *p_picture )
659 {
660     /* */
661     p_picture->date = VLC_TS_INVALID;
662     p_picture->b_force = false;
663     p_picture->b_progressive = false;
664     p_picture->i_nb_fields = 0;
665     p_picture->b_top_field_first = false;
666     picture_CleanupQuant( p_picture );
667 }
668
669 /*****************************************************************************
670  *
671  *****************************************************************************/
672 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
673 {
674     int i_index, i_width_aligned, i_height_aligned;
675
676     /* Store default values */
677     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
678     {
679         p_picture->p[i_index].p_pixels = NULL;
680         p_picture->p[i_index].i_pixel_pitch = 1;
681     }
682
683     p_picture->pf_release = NULL;
684     p_picture->p_release_sys = NULL;
685     p_picture->pf_lock = NULL;
686     p_picture->pf_unlock = NULL;
687     p_picture->i_refcount = 0;
688
689     p_picture->i_qtype = QTYPE_NONE;
690     p_picture->i_qstride = 0;
691     p_picture->p_q = NULL;
692
693     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height, i_aspect );
694
695     /* Make sure the real dimensions are a multiple of 16 */
696     i_width_aligned = (i_width + 15) >> 4 << 4;
697     i_height_aligned = (i_height + 15) >> 4 << 4;
698
699     /* Calculate coordinates */
700     switch( vlc_fourcc_GetCodec( VIDEO_ES, i_chroma ) )
701     {
702     case VLC_CODEC_I411:
703         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
704         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
705         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
706         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
707         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
708         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
709         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
710         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
711         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
712         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
713         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
714         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
715         p_picture->i_planes = 3;
716         break;
717
718     case VLC_CODEC_I410:
719         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
720         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
721         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
722         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
723         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 4;
724         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 4;
725         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
726         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
727         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 4;
728         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 4;
729         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
730         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
731         p_picture->i_planes = 3;
732         break;
733
734     case VLC_CODEC_YV12:
735     case VLC_CODEC_I420:
736     case VLC_CODEC_J420:
737         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
738         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
739         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
740         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
741         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
742         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
743         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
744         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
745         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
746         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
747         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
748         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
749         p_picture->i_planes = 3;
750         break;
751
752     case VLC_CODEC_I422:
753     case VLC_CODEC_J422:
754         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
755         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
756         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
757         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
758         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
759         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
760         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
761         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
762         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
763         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
764         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
765         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
766         p_picture->i_planes = 3;
767         break;
768
769     case VLC_CODEC_I440:
770     case VLC_CODEC_J440:
771         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
772         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
773         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
774         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
775         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
776         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
777         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
778         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
779         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
780         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
781         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
782         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
783         p_picture->i_planes = 3;
784         break;
785
786     case VLC_CODEC_I444:
787     case VLC_CODEC_J444:
788         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
789         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
790         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
791         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
792         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
793         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
794         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
795         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
796         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
797         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
798         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
799         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
800         p_picture->i_planes = 3;
801         break;
802
803     case VLC_CODEC_YUVA:
804         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
805         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
806         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
807         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
808         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
809         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
810         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
811         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
812         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
813         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
814         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
815         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
816         p_picture->p[ A_PLANE ].i_lines = i_height_aligned;
817         p_picture->p[ A_PLANE ].i_visible_lines = i_height;
818         p_picture->p[ A_PLANE ].i_pitch = i_width_aligned;
819         p_picture->p[ A_PLANE ].i_visible_pitch = i_width;
820         p_picture->i_planes = 4;
821         break;
822
823     case VLC_CODEC_YUVP:
824         p_picture->p->i_lines = i_height_aligned;
825         p_picture->p->i_visible_lines = i_height;
826         p_picture->p->i_pitch = i_width_aligned;
827         p_picture->p->i_visible_pitch = i_width;
828         p_picture->p->i_pixel_pitch = 8;
829         p_picture->i_planes = 1;
830         break;
831
832     case VLC_CODEC_Y211:
833         p_picture->p->i_lines = i_height_aligned;
834         p_picture->p->i_visible_lines = i_height;
835         p_picture->p->i_pitch = i_width_aligned;
836         p_picture->p->i_visible_pitch = i_width;
837         p_picture->p->i_pixel_pitch = 4;
838         p_picture->i_planes = 1;
839         break;
840
841     case VLC_CODEC_UYVY:
842     case VLC_CODEC_VYUY:
843     case VLC_CODEC_YUYV:
844     case VLC_CODEC_YVYU:
845         p_picture->p->i_lines = i_height_aligned;
846         p_picture->p->i_visible_lines = i_height;
847         p_picture->p->i_pitch = i_width_aligned * 2;
848         p_picture->p->i_visible_pitch = i_width * 2;
849         p_picture->p->i_pixel_pitch = 4;
850         p_picture->i_planes = 1;
851         break;
852
853     case VLC_CODEC_RGB8:
854         p_picture->p->i_lines = i_height_aligned;
855         p_picture->p->i_visible_lines = i_height;
856         p_picture->p->i_pitch = i_width_aligned;
857         p_picture->p->i_visible_pitch = i_width;
858         p_picture->p->i_pixel_pitch = 1;
859         p_picture->i_planes = 1;
860         break;
861
862     case VLC_CODEC_RGB15:
863         p_picture->p->i_lines = i_height_aligned;
864         p_picture->p->i_visible_lines = i_height;
865         p_picture->p->i_pitch = i_width_aligned * 2;
866         p_picture->p->i_visible_pitch = i_width * 2;
867         p_picture->p->i_pixel_pitch = 2;
868         p_picture->i_planes = 1;
869         break;
870
871     case VLC_CODEC_RGB16:
872         p_picture->p->i_lines = i_height_aligned;
873         p_picture->p->i_visible_lines = i_height;
874         p_picture->p->i_pitch = i_width_aligned * 2;
875         p_picture->p->i_visible_pitch = i_width * 2;
876         p_picture->p->i_pixel_pitch = 2;
877         p_picture->i_planes = 1;
878         break;
879
880     case VLC_CODEC_RGB24:
881         p_picture->p->i_lines = i_height_aligned;
882         p_picture->p->i_visible_lines = i_height;
883         p_picture->p->i_pitch = i_width_aligned * 3;
884         p_picture->p->i_visible_pitch = i_width * 3;
885         p_picture->p->i_pixel_pitch = 3;
886         p_picture->i_planes = 1;
887         break;
888
889     case VLC_CODEC_RGB32:
890     case VLC_CODEC_RGBA:
891         p_picture->p->i_lines = i_height_aligned;
892         p_picture->p->i_visible_lines = i_height;
893         p_picture->p->i_pitch = i_width_aligned * 4;
894         p_picture->p->i_visible_pitch = i_width * 4;
895         p_picture->p->i_pixel_pitch = 4;
896         p_picture->i_planes = 1;
897         break;
898
899     case VLC_CODEC_GREY:
900     case VLC_CODEC_RGBP:
901         p_picture->p->i_lines = i_height_aligned;
902         p_picture->p->i_visible_lines = i_height;
903         p_picture->p->i_pitch = i_width_aligned;
904         p_picture->p->i_visible_pitch = i_width;
905         p_picture->p->i_pixel_pitch = 1;
906         p_picture->i_planes = 1;
907         break;
908
909     default:
910         p_picture->i_planes = 0;
911         return VLC_EGENERIC;
912     }
913
914     return VLC_SUCCESS;
915 }
916
917 /*****************************************************************************
918  *
919  *****************************************************************************/
920 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
921 {
922     video_format_t fmt = *p_fmt;
923
924     /* It is needed to be sure all informations are filled */
925     video_format_Setup( &fmt, p_fmt->i_chroma,
926                               p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
927
928     /* */
929     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
930     if( !p_picture )
931         return NULL;
932
933     if( p_resource )
934     {
935         if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect ) )
936         {
937             free( p_picture );
938             return NULL;
939         }
940         p_picture->p_sys = p_resource->p_sys;
941
942         for( int i = 0; i < p_picture->i_planes; i++ )
943         {
944             p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
945             p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
946             p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
947         }
948     }
949     else
950     {
951         if( __vout_AllocatePicture( NULL, p_picture,
952                                     fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect ) )
953         {
954             free( p_picture );
955             return NULL;
956         }
957     }
958     /* */
959     p_picture->format = fmt;
960     p_picture->i_refcount = 1;
961     p_picture->pf_release = PictureReleaseCallback;
962     p_picture->i_status = RESERVED_PICTURE;
963
964     return p_picture;
965 }
966 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
967 {
968     return picture_NewFromResource( p_fmt, NULL );
969 }
970 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
971 {
972     video_format_t fmt;
973
974     memset( &fmt, 0, sizeof(fmt) );
975     video_format_Setup( &fmt, i_chroma, i_width, i_height, i_aspect );
976
977     return picture_NewFromFormat( &fmt );
978 }
979
980 /*****************************************************************************
981  *
982  *****************************************************************************/
983 void picture_Delete( picture_t *p_picture )
984 {
985     assert( p_picture && p_picture->i_refcount == 0 );
986     assert( p_picture->p_release_sys == NULL );
987
988     free( p_picture->p_q );
989     free( p_picture->p_data_orig );
990     free( p_picture->p_sys );
991     free( p_picture );
992 }
993
994 /*****************************************************************************
995  *
996  *****************************************************************************/
997 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
998 {
999     int i;
1000
1001     for( i = 0; i < p_src->i_planes ; i++ )
1002         plane_CopyPixels( p_dst->p+i, p_src->p+i );
1003 }
1004
1005 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
1006 {
1007     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
1008                                      p_src->i_visible_pitch );
1009     const unsigned i_height = __MIN( p_dst->i_visible_lines,
1010                                      p_src->i_visible_lines );
1011
1012     if( p_src->i_pitch == p_dst->i_pitch )
1013     {
1014         /* There are margins, but with the same width : perfect ! */
1015         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
1016                     p_src->i_pitch * i_height );
1017     }
1018     else
1019     {
1020         /* We need to proceed line by line */
1021         uint8_t *p_in = p_src->p_pixels;
1022         uint8_t *p_out = p_dst->p_pixels;
1023         int i_line;
1024
1025         assert( p_in );
1026         assert( p_out );
1027
1028         for( i_line = i_height; i_line--; )
1029         {
1030             vlc_memcpy( p_out, p_in, i_width );
1031             p_in += p_src->i_pitch;
1032             p_out += p_dst->i_pitch;
1033         }
1034     }
1035 }
1036
1037 /*****************************************************************************
1038  *
1039  *****************************************************************************/
1040 int picture_Export( vlc_object_t *p_obj,
1041                     block_t **pp_image,
1042                     video_format_t *p_fmt,
1043                     picture_t *p_picture,
1044                     vlc_fourcc_t i_format,
1045                     unsigned i_override_width, unsigned i_override_height )
1046 {
1047     /* */
1048     video_format_t fmt_in = p_picture->format;
1049     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
1050     {
1051         fmt_in.i_sar_num =
1052         fmt_in.i_sar_den = 1;
1053     }
1054
1055     /* */
1056     video_format_t fmt_out;
1057     memset( &fmt_out, 0, sizeof(fmt_out) );
1058     fmt_out.i_sar_num =
1059     fmt_out.i_sar_den = 1;
1060     fmt_out.i_chroma  = i_format;
1061
1062     /* compute original width/height */
1063     unsigned int i_original_width;
1064     unsigned int i_original_height;
1065     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
1066     {
1067         i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
1068         i_original_height = fmt_in.i_height;
1069     }
1070     else
1071     {
1072         i_original_width =  fmt_in.i_width;
1073         i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
1074     }
1075
1076     /* */
1077     fmt_out.i_width  = i_override_width > 0 ? i_override_width : i_original_width;
1078     fmt_out.i_height = i_override_height > 0 ? i_override_height : i_original_height;
1079
1080     /* scale if only one direction is provided */
1081     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
1082     {
1083         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
1084                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
1085     }
1086     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
1087     {
1088         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
1089                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
1090     }
1091
1092     image_handler_t *p_image = image_HandlerCreate( p_obj );
1093
1094     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
1095
1096     image_HandlerDelete( p_image );
1097
1098     if( !p_block )
1099         return VLC_EGENERIC;
1100
1101     p_block->i_pts =
1102     p_block->i_dts = p_picture->date;
1103
1104     if( p_fmt )
1105         *p_fmt = fmt_out;
1106     *pp_image = p_block;
1107
1108     return VLC_SUCCESS;
1109 }
1110
1111 /*****************************************************************************
1112  *
1113  *****************************************************************************/
1114 struct picture_fifo_t
1115 {
1116     vlc_mutex_t lock;
1117     picture_t *p_first;
1118     picture_t **pp_last;
1119 };
1120
1121 static void PictureFifoReset( picture_fifo_t *p_fifo )
1122 {
1123     p_fifo->p_first = NULL;
1124     p_fifo->pp_last = &p_fifo->p_first;
1125 }
1126 static void PictureFifoPush( picture_fifo_t *p_fifo, picture_t *p_picture )
1127 {
1128     assert( !p_picture->p_next );
1129     *p_fifo->pp_last = p_picture;
1130     p_fifo->pp_last = &p_picture->p_next;
1131 }
1132 static picture_t *PictureFifoPop( picture_fifo_t *p_fifo )
1133 {
1134     picture_t *p_picture = p_fifo->p_first;
1135
1136     if( p_picture )
1137     {
1138         p_fifo->p_first = p_picture->p_next;
1139         if( !p_fifo->p_first )
1140             p_fifo->pp_last = &p_fifo->p_first;
1141     }
1142     return p_picture;
1143 }
1144
1145 picture_fifo_t *picture_fifo_New(void)
1146 {
1147     picture_fifo_t *p_fifo = malloc( sizeof(*p_fifo) );
1148     if( !p_fifo )
1149         return NULL;
1150
1151     vlc_mutex_init( &p_fifo->lock );
1152     PictureFifoReset( p_fifo );
1153     return p_fifo;
1154 }
1155
1156 void picture_fifo_Push( picture_fifo_t *p_fifo, picture_t *p_picture )
1157 {
1158     vlc_mutex_lock( &p_fifo->lock );
1159     PictureFifoPush( p_fifo, p_picture );
1160     vlc_mutex_unlock( &p_fifo->lock );
1161 }
1162 picture_t *picture_fifo_Pop( picture_fifo_t *p_fifo )
1163 {
1164     vlc_mutex_lock( &p_fifo->lock );
1165     picture_t *p_picture = PictureFifoPop( p_fifo );
1166     vlc_mutex_unlock( &p_fifo->lock );
1167
1168     return p_picture;
1169 }
1170 picture_t *picture_fifo_Peek( picture_fifo_t *p_fifo )
1171 {
1172     vlc_mutex_lock( &p_fifo->lock );
1173     picture_t *p_picture = p_fifo->p_first;
1174     if( p_picture )
1175         picture_Hold( p_picture );
1176     vlc_mutex_unlock( &p_fifo->lock );
1177
1178     return p_picture;
1179 }
1180 void picture_fifo_Flush( picture_fifo_t *p_fifo, mtime_t i_date, bool b_below )
1181 {
1182     picture_t *p_picture;
1183
1184     vlc_mutex_lock( &p_fifo->lock );
1185
1186     p_picture = p_fifo->p_first;
1187     PictureFifoReset( p_fifo );
1188
1189     picture_fifo_t tmp;
1190     PictureFifoReset( &tmp );
1191
1192     while( p_picture )
1193     {
1194         picture_t *p_next = p_picture->p_next;
1195
1196         p_picture->p_next = NULL;
1197         if( (  b_below && p_picture->date <= i_date ) ||
1198             ( !b_below && p_picture->date >= i_date ) )
1199             PictureFifoPush( &tmp, p_picture );
1200         else
1201             PictureFifoPush( p_fifo, p_picture );
1202         p_picture = p_next;
1203     }
1204     vlc_mutex_unlock( &p_fifo->lock );
1205
1206     for( ;; )
1207     {
1208         picture_t *p_picture = PictureFifoPop( &tmp );
1209         if( !p_picture )
1210             break;
1211         picture_Release( p_picture );
1212     }
1213 }
1214 void picture_fifo_OffsetDate( picture_fifo_t *p_fifo, mtime_t i_delta )
1215 {
1216     vlc_mutex_lock( &p_fifo->lock );
1217     for( picture_t *p_picture = p_fifo->p_first; p_picture != NULL; )
1218     {
1219         p_picture->date += i_delta;
1220         p_picture = p_picture->p_next;
1221     }
1222     vlc_mutex_unlock( &p_fifo->lock );
1223 }
1224 void picture_fifo_Delete( picture_fifo_t *p_fifo )
1225 {
1226     picture_fifo_Flush( p_fifo, INT64_MAX, true );
1227     vlc_mutex_destroy( &p_fifo->lock );
1228 }
1229
1230 /*****************************************************************************
1231  *
1232  *****************************************************************************/
1233 struct picture_release_sys_t
1234 {
1235     /* Saved release */
1236     void (*pf_release)( picture_t * );
1237     picture_release_sys_t *p_release_sys;
1238
1239     /* */
1240     int64_t i_tick;
1241 };
1242
1243 struct picture_pool_t
1244 {
1245     int64_t i_tick;
1246
1247     int i_picture;
1248     picture_t **pp_picture;
1249 };
1250
1251 static void PicturePoolPictureRelease( picture_t * );
1252
1253 picture_pool_t *picture_pool_New( int i_picture, picture_t *pp_picture[] )
1254 {
1255     picture_pool_t *p_pool = calloc( 1, sizeof(*p_pool) );
1256     if( !p_pool )
1257         return NULL;
1258
1259     p_pool->i_tick = 1;
1260     p_pool->i_picture = i_picture;
1261     p_pool->pp_picture = calloc( p_pool->i_picture, sizeof(*p_pool->pp_picture) );
1262
1263     for( int i = 0; i < i_picture; i++ )
1264     {
1265         picture_t *p_picture = pp_picture[i];
1266
1267         /* The pool must be the only owner of the picture */
1268         assert( p_picture->i_refcount == 1 );
1269
1270         /* Install the new release callback */
1271         picture_release_sys_t *p_release_sys = malloc( sizeof(*p_release_sys) );
1272         p_release_sys->pf_release = p_picture->pf_release;
1273         p_release_sys->p_release_sys = p_picture->p_release_sys;
1274         p_release_sys->i_tick = 0;
1275
1276         p_picture->i_refcount = 0;
1277         p_picture->pf_release = PicturePoolPictureRelease;
1278         p_picture->p_release_sys = p_release_sys;
1279
1280         /* */
1281         p_pool->pp_picture[i] = p_picture;
1282     }
1283     return p_pool;
1284 }
1285
1286 picture_pool_t *picture_pool_NewFromFormat( const video_format_t *p_fmt, int i_picture )
1287 {
1288     picture_t *pp_picture[i_picture];
1289
1290     for( int i = 0; i < i_picture; i++ )
1291     {
1292         pp_picture[i] = picture_New( p_fmt->i_chroma,
1293                                      p_fmt->i_width, p_fmt->i_height,
1294                                      p_fmt->i_aspect );
1295         if( !pp_picture[i] )
1296             goto error;
1297     }
1298     picture_pool_t *p_pool = picture_pool_New( i_picture, pp_picture );
1299     if( !p_pool )
1300         goto error;
1301
1302     return p_pool;
1303
1304 error:
1305     for( int i = 0; i < i_picture; i++ )
1306     {
1307         if( !pp_picture[i] )
1308             break;
1309         picture_Release( pp_picture[i] );
1310     }
1311     return NULL;
1312 }
1313
1314 void picture_pool_Delete( picture_pool_t *p_pool )
1315 {
1316     for( int i = 0; i < p_pool->i_picture; i++ )
1317     {
1318         picture_t *p_picture = p_pool->pp_picture[i];
1319         picture_release_sys_t *p_release_sys = p_picture->p_release_sys;
1320
1321         assert( p_picture->i_refcount == 0 );
1322
1323         /* Restore old release callback */
1324         p_picture->i_refcount = 1;
1325         p_picture->pf_release = p_release_sys->pf_release;
1326         p_picture->p_release_sys = p_release_sys->p_release_sys;
1327
1328         picture_Release( p_picture );
1329
1330         free( p_release_sys );
1331     }
1332     free( p_pool );
1333 }
1334
1335 picture_t *picture_pool_Get( picture_pool_t *p_pool )
1336 {
1337     for( int i = 0; i < p_pool->i_picture; i++ )
1338     {
1339         picture_t *p_picture = p_pool->pp_picture[i];
1340
1341         if( p_picture->i_refcount <= 0 )
1342         {
1343             p_picture->p_release_sys->i_tick = p_pool->i_tick++;
1344             picture_Hold( p_picture );
1345             return p_picture;
1346         }
1347     }
1348     return NULL;
1349 }
1350
1351 void picture_pool_NonEmpty( picture_pool_t *p_pool, bool b_reset )
1352 {
1353     picture_t *p_old = NULL;
1354
1355     for( int i = 0; i < p_pool->i_picture; i++ )
1356     {
1357         picture_t *p_picture = p_pool->pp_picture[i];
1358
1359         if( b_reset )
1360             p_picture->i_refcount = 0;
1361         else if( p_picture->i_refcount == 0 )
1362             return;
1363         else if( !p_old || p_picture->p_release_sys->i_tick < p_old->p_release_sys->i_tick )
1364             p_old = p_picture;
1365     }
1366     if( !b_reset && p_old )
1367         p_old->i_refcount = 0;
1368 }
1369
1370 static void PicturePoolPictureRelease( picture_t *p_picture )
1371 {
1372     assert( p_picture->i_refcount > 0 );
1373
1374     if( --p_picture->i_refcount > 0 )
1375         return;
1376
1377     /* Nothing to do for the moment */
1378 }
1379