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