]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
Implemented auto deinterlace mode.
[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_bytes, 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     i_bytes = p_pic->format.i_bits_per_pixel *
574         i_width_aligned * i_height_aligned / 8;
575
576     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
577
578     if( p_pic->p_data == NULL )
579     {
580         p_pic->i_planes = 0;
581         return VLC_EGENERIC;
582     }
583
584     /* Fill the p_pixels field for each plane */
585     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
586
587     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
588     {
589         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
590             p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
591     }
592
593     return VLC_SUCCESS;
594 }
595
596 /**
597  * Compare two chroma values
598  *
599  * This function returns 1 if the two fourcc values given as argument are
600  * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
601  */
602 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
603 {
604     static const vlc_fourcc_t p_I420[] = {
605         VLC_CODEC_I420, VLC_CODEC_YV12, VLC_CODEC_J420, 0
606     };
607     static const vlc_fourcc_t p_I422[] = {
608         VLC_CODEC_I422, VLC_CODEC_J422, 0
609     };
610     static const vlc_fourcc_t p_I440[] = {
611         VLC_CODEC_I440, VLC_CODEC_J440, 0
612     };
613     static const vlc_fourcc_t p_I444[] = {
614         VLC_CODEC_I444, VLC_CODEC_J444, 0
615     };
616     static const vlc_fourcc_t *pp_fcc[] = {
617         p_I420, p_I422, p_I440, p_I444, NULL
618     };
619
620     /* */
621     i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
622     i_amorhc = vlc_fourcc_GetCodec( VIDEO_ES, i_amorhc );
623
624     /* If they are the same, they are the same ! */
625     if( i_chroma == i_amorhc )
626         return 1;
627
628     /* Check for equivalence classes */
629     for( int i = 0; pp_fcc[i] != NULL; i++ )
630     {
631         bool b_fcc1 = false;
632         bool b_fcc2 = false;
633         for( int j = 0; pp_fcc[i][j] != 0; j++ )
634         {
635             if( i_chroma == pp_fcc[i][j] )
636                 b_fcc1 = true;
637             if( i_amorhc == pp_fcc[i][j] )
638                 b_fcc2 = true;
639         }
640         if( b_fcc1 && b_fcc2 )
641             return 1;
642     }
643     return 0;
644 }
645
646 /*****************************************************************************
647  *
648  *****************************************************************************/
649 static void PictureReleaseCallback( picture_t *p_picture )
650 {
651     if( --p_picture->i_refcount > 0 )
652         return;
653     picture_Delete( p_picture );
654 }
655
656 /*****************************************************************************
657  *
658  *****************************************************************************/
659 void picture_Reset( picture_t *p_picture )
660 {
661     /* */
662     p_picture->date = VLC_TS_INVALID;
663     p_picture->b_force = false;
664     p_picture->b_progressive = false;
665     p_picture->i_nb_fields = 0;
666     p_picture->b_top_field_first = false;
667     picture_CleanupQuant( p_picture );
668 }
669
670 /*****************************************************************************
671  *
672  *****************************************************************************/
673 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
674 {
675     int i_index, i_width_aligned, i_height_aligned;
676
677     /* Store default values */
678     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
679     {
680         p_picture->p[i_index].p_pixels = NULL;
681         p_picture->p[i_index].i_pixel_pitch = 1;
682     }
683
684     p_picture->pf_release = NULL;
685     p_picture->p_release_sys = NULL;
686     p_picture->pf_lock = NULL;
687     p_picture->pf_unlock = NULL;
688     p_picture->i_refcount = 0;
689
690     p_picture->i_qtype = QTYPE_NONE;
691     p_picture->i_qstride = 0;
692     p_picture->p_q = NULL;
693
694     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height, i_aspect );
695
696     /* Make sure the real dimensions are a multiple of 16 */
697     i_width_aligned = (i_width + 15) >> 4 << 4;
698     i_height_aligned = (i_height + 15) >> 4 << 4;
699
700     /* Calculate coordinates */
701     switch( vlc_fourcc_GetCodec( VIDEO_ES, i_chroma ) )
702     {
703     case VLC_CODEC_I411:
704         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
705         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
706         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
707         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
708         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
709         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
710         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
711         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
712         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
713         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
714         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
715         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
716         p_picture->i_planes = 3;
717         break;
718
719     case VLC_CODEC_I410:
720         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
721         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
722         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
723         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
724         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 4;
725         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 4;
726         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
727         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
728         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 4;
729         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 4;
730         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
731         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
732         p_picture->i_planes = 3;
733         break;
734
735     case VLC_CODEC_YV12:
736     case VLC_CODEC_I420:
737     case VLC_CODEC_J420:
738         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
739         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
740         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
741         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
742         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
743         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
744         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
745         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
746         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
747         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
748         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
749         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
750         p_picture->i_planes = 3;
751         break;
752
753     case VLC_CODEC_I422:
754     case VLC_CODEC_J422:
755         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
756         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
757         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
758         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
759         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
760         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
761         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
762         p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
763         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
764         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
765         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
766         p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
767         p_picture->i_planes = 3;
768         break;
769
770     case VLC_CODEC_I440:
771     case VLC_CODEC_J440:
772         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
773         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
774         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
775         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
776         p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
777         p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
778         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
779         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
780         p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
781         p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
782         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
783         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
784         p_picture->i_planes = 3;
785         break;
786
787     case VLC_CODEC_I444:
788     case VLC_CODEC_J444:
789         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
790         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
791         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
792         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
793         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
794         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
795         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
796         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
797         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
798         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
799         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
800         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
801         p_picture->i_planes = 3;
802         break;
803
804     case VLC_CODEC_YUVA:
805         p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
806         p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
807         p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
808         p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
809         p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
810         p_picture->p[ U_PLANE ].i_visible_lines = i_height;
811         p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
812         p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
813         p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
814         p_picture->p[ V_PLANE ].i_visible_lines = i_height;
815         p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
816         p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
817         p_picture->p[ A_PLANE ].i_lines = i_height_aligned;
818         p_picture->p[ A_PLANE ].i_visible_lines = i_height;
819         p_picture->p[ A_PLANE ].i_pitch = i_width_aligned;
820         p_picture->p[ A_PLANE ].i_visible_pitch = i_width;
821         p_picture->i_planes = 4;
822         break;
823
824     case VLC_CODEC_YUVP:
825         p_picture->p->i_lines = i_height_aligned;
826         p_picture->p->i_visible_lines = i_height;
827         p_picture->p->i_pitch = i_width_aligned;
828         p_picture->p->i_visible_pitch = i_width;
829         p_picture->p->i_pixel_pitch = 1;
830         p_picture->i_planes = 1;
831         break;
832
833     case VLC_CODEC_Y211:
834         p_picture->p->i_lines = i_height_aligned;
835         p_picture->p->i_visible_lines = i_height;
836         p_picture->p->i_pitch = i_width_aligned;
837         p_picture->p->i_visible_pitch = i_width;
838         p_picture->p->i_pixel_pitch = 4;
839         p_picture->i_planes = 1;
840         break;
841
842     case VLC_CODEC_UYVY:
843     case VLC_CODEC_VYUY:
844     case VLC_CODEC_YUYV:
845     case VLC_CODEC_YVYU:
846         p_picture->p->i_lines = i_height_aligned;
847         p_picture->p->i_visible_lines = i_height;
848         p_picture->p->i_pitch = i_width_aligned * 2;
849         p_picture->p->i_visible_pitch = i_width * 2;
850         p_picture->p->i_pixel_pitch = 2;
851         p_picture->i_planes = 1;
852         break;
853
854     case VLC_CODEC_RGB8:
855         p_picture->p->i_lines = i_height_aligned;
856         p_picture->p->i_visible_lines = i_height;
857         p_picture->p->i_pitch = i_width_aligned;
858         p_picture->p->i_visible_pitch = i_width;
859         p_picture->p->i_pixel_pitch = 1;
860         p_picture->i_planes = 1;
861         break;
862
863     case VLC_CODEC_RGB15:
864         p_picture->p->i_lines = i_height_aligned;
865         p_picture->p->i_visible_lines = i_height;
866         p_picture->p->i_pitch = i_width_aligned * 2;
867         p_picture->p->i_visible_pitch = i_width * 2;
868         p_picture->p->i_pixel_pitch = 2;
869         p_picture->i_planes = 1;
870         break;
871
872     case VLC_CODEC_RGB16:
873         p_picture->p->i_lines = i_height_aligned;
874         p_picture->p->i_visible_lines = i_height;
875         p_picture->p->i_pitch = i_width_aligned * 2;
876         p_picture->p->i_visible_pitch = i_width * 2;
877         p_picture->p->i_pixel_pitch = 2;
878         p_picture->i_planes = 1;
879         break;
880
881     case VLC_CODEC_RGB24:
882         p_picture->p->i_lines = i_height_aligned;
883         p_picture->p->i_visible_lines = i_height;
884         p_picture->p->i_pitch = i_width_aligned * 3;
885         p_picture->p->i_visible_pitch = i_width * 3;
886         p_picture->p->i_pixel_pitch = 3;
887         p_picture->i_planes = 1;
888         break;
889
890     case VLC_CODEC_RGB32:
891     case VLC_CODEC_RGBA:
892         p_picture->p->i_lines = i_height_aligned;
893         p_picture->p->i_visible_lines = i_height;
894         p_picture->p->i_pitch = i_width_aligned * 4;
895         p_picture->p->i_visible_pitch = i_width * 4;
896         p_picture->p->i_pixel_pitch = 4;
897         p_picture->i_planes = 1;
898         break;
899
900     case VLC_CODEC_GREY:
901     case VLC_CODEC_RGBP:
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;
905         p_picture->p->i_visible_pitch = i_width;
906         p_picture->p->i_pixel_pitch = 1;
907         p_picture->i_planes = 1;
908         break;
909
910     default:
911         p_picture->i_planes = 0;
912         return VLC_EGENERIC;
913     }
914
915     return VLC_SUCCESS;
916 }
917
918 /*****************************************************************************
919  *
920  *****************************************************************************/
921 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
922 {
923     video_format_t fmt = *p_fmt;
924
925     /* It is needed to be sure all informations are filled */
926     video_format_Setup( &fmt, p_fmt->i_chroma,
927                               p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
928
929     /* */
930     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
931     if( !p_picture )
932         return NULL;
933
934     if( p_resource )
935     {
936         if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect ) )
937         {
938             free( p_picture );
939             return NULL;
940         }
941         p_picture->p_sys = p_resource->p_sys;
942
943         for( int i = 0; i < p_picture->i_planes; i++ )
944         {
945             p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
946             p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
947             p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
948         }
949     }
950     else
951     {
952         if( __vout_AllocatePicture( NULL, p_picture,
953                                     fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect ) )
954         {
955             free( p_picture );
956             return NULL;
957         }
958     }
959     /* */
960     p_picture->format = fmt;
961     p_picture->i_refcount = 1;
962     p_picture->pf_release = PictureReleaseCallback;
963     p_picture->i_status = RESERVED_PICTURE;
964
965     return p_picture;
966 }
967 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
968 {
969     return picture_NewFromResource( p_fmt, NULL );
970 }
971 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
972 {
973     video_format_t fmt;
974
975     memset( &fmt, 0, sizeof(fmt) );
976     video_format_Setup( &fmt, i_chroma, i_width, i_height, i_aspect );
977
978     return picture_NewFromFormat( &fmt );
979 }
980
981 /*****************************************************************************
982  *
983  *****************************************************************************/
984 void picture_Delete( picture_t *p_picture )
985 {
986     assert( p_picture && p_picture->i_refcount == 0 );
987     assert( p_picture->p_release_sys == NULL );
988
989     free( p_picture->p_q );
990     free( p_picture->p_data_orig );
991     free( p_picture->p_sys );
992     free( p_picture );
993 }
994
995 /*****************************************************************************
996  *
997  *****************************************************************************/
998 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
999 {
1000     int i;
1001
1002     for( i = 0; i < p_src->i_planes ; i++ )
1003         plane_CopyPixels( p_dst->p+i, p_src->p+i );
1004 }
1005
1006 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
1007 {
1008     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
1009                                      p_src->i_visible_pitch );
1010     const unsigned i_height = __MIN( p_dst->i_visible_lines,
1011                                      p_src->i_visible_lines );
1012
1013     if( p_src->i_pitch == p_dst->i_pitch )
1014     {
1015         /* There are margins, but with the same width : perfect ! */
1016         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
1017                     p_src->i_pitch * i_height );
1018     }
1019     else
1020     {
1021         /* We need to proceed line by line */
1022         uint8_t *p_in = p_src->p_pixels;
1023         uint8_t *p_out = p_dst->p_pixels;
1024         int i_line;
1025
1026         assert( p_in );
1027         assert( p_out );
1028
1029         for( i_line = i_height; i_line--; )
1030         {
1031             vlc_memcpy( p_out, p_in, i_width );
1032             p_in += p_src->i_pitch;
1033             p_out += p_dst->i_pitch;
1034         }
1035     }
1036 }
1037
1038 /*****************************************************************************
1039  *
1040  *****************************************************************************/
1041 int picture_Export( vlc_object_t *p_obj,
1042                     block_t **pp_image,
1043                     video_format_t *p_fmt,
1044                     picture_t *p_picture,
1045                     vlc_fourcc_t i_format,
1046                     int i_override_width, int i_override_height )
1047 {
1048     /* */
1049     video_format_t fmt_in = p_picture->format;
1050     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
1051     {
1052         fmt_in.i_sar_num =
1053         fmt_in.i_sar_den = 1;
1054     }
1055
1056     /* */
1057     video_format_t fmt_out;
1058     memset( &fmt_out, 0, sizeof(fmt_out) );
1059     fmt_out.i_sar_num =
1060     fmt_out.i_sar_den = 1;
1061     fmt_out.i_chroma  = i_format;
1062
1063     /* compute original width/height */
1064     unsigned int i_original_width;
1065     unsigned int i_original_height;
1066     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
1067     {
1068         i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
1069         i_original_height = fmt_in.i_height;
1070     }
1071     else
1072     {
1073         i_original_width =  fmt_in.i_width;
1074         i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
1075     }
1076
1077     /* */
1078     fmt_out.i_width  = ( i_override_width < 0 ) ?
1079                        i_original_width : i_override_width;
1080     fmt_out.i_height = ( i_override_height < 0 ) ?
1081                        i_original_height : i_override_height;
1082
1083     /* scale if only one direction is provided */
1084     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
1085     {
1086         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
1087                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
1088     }
1089     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
1090     {
1091         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
1092                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
1093     }
1094
1095     image_handler_t *p_image = image_HandlerCreate( p_obj );
1096
1097     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
1098
1099     image_HandlerDelete( p_image );
1100
1101     if( !p_block )
1102         return VLC_EGENERIC;
1103
1104     p_block->i_pts =
1105     p_block->i_dts = p_picture->date;
1106
1107     if( p_fmt )
1108         *p_fmt = fmt_out;
1109     *pp_image = p_block;
1110
1111     return VLC_SUCCESS;
1112 }
1113