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