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