vout_RenderPicture should not render subtitles on the provided picture.
[vlc.git] / src / video_output / vout_pictures.c
1 /*****************************************************************************
2  * vout_pictures.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_vout.h>
35 #include <vlc_osd.h>
36 #include <vlc_filter.h>
37 #include "vout_pictures.h"
38
39 #include <assert.h>
40
41 /**
42  * Display a picture
43  *
44  * Remove the reservation flag of a picture, which will cause it to be ready
45  * for display. The picture won't be displayed until vout_DatePicture has been
46  * called.
47  */
48 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
49 {
50     vlc_mutex_lock( &p_vout->picture_lock );
51     switch( p_pic->i_status )
52     {
53     case RESERVED_PICTURE:
54         p_pic->i_status = RESERVED_DISP_PICTURE;
55         break;
56     case RESERVED_DATED_PICTURE:
57         p_pic->i_status = READY_PICTURE;
58         break;
59     default:
60         msg_Err( p_vout, "picture to display %p has invalid status %d",
61                          p_pic, p_pic->i_status );
62         break;
63     }
64
65     vlc_mutex_unlock( &p_vout->picture_lock );
66 }
67
68 /**
69  * Date a picture
70  *
71  * Remove the reservation flag of a picture, which will cause it to be ready
72  * for display. The picture won't be displayed until vout_DisplayPicture has
73  * been called.
74  * \param p_vout The vout in question
75  * \param p_pic The picture to date
76  * \param date The date to display the picture
77  */
78 void vout_DatePicture( vout_thread_t *p_vout,
79                        picture_t *p_pic, mtime_t date )
80 {
81     vlc_mutex_lock( &p_vout->picture_lock );
82     p_pic->date = date;
83     switch( p_pic->i_status )
84     {
85     case RESERVED_PICTURE:
86         p_pic->i_status = RESERVED_DATED_PICTURE;
87         break;
88     case RESERVED_DISP_PICTURE:
89         p_pic->i_status = READY_PICTURE;
90         break;
91     default:
92         msg_Err( p_vout, "picture to date %p has invalid status %d",
93                          p_pic, p_pic->i_status );
94         break;
95     }
96
97     vlc_mutex_unlock( &p_vout->picture_lock );
98 }
99
100 /**
101  * Allocate a picture in the video output heap.
102  *
103  * This function creates a reserved image in the video output heap.
104  * A null pointer is returned if the function fails. This method provides an
105  * already allocated zone of memory in the picture data fields.
106  * It needs locking since several pictures can be created by several producers
107  * threads.
108  */
109 int vout_CountPictureAvailable( vout_thread_t *p_vout )
110 {
111     int i_free = 0;
112     int i_pic;
113
114     vlc_mutex_lock( &p_vout->picture_lock );
115     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
116     {
117         picture_t *p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1) % I_RENDERPICTURES];
118
119         switch( p_pic->i_status )
120         {
121             case DESTROYED_PICTURE:
122                 i_free++;
123                 break;
124
125             case FREE_PICTURE:
126                 i_free++;
127                 break;
128
129             default:
130                 break;
131         }
132     }
133     vlc_mutex_unlock( &p_vout->picture_lock );
134
135     return i_free;
136 }
137
138 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
139                                bool b_progressive,
140                                bool b_top_field_first,
141                                unsigned int i_nb_fields )
142 {
143     int         i_pic;                                      /* picture index */
144     picture_t * p_pic;
145     picture_t * p_freepic = NULL;                      /* first free picture */
146
147     /* Get lock */
148     vlc_mutex_lock( &p_vout->picture_lock );
149
150     /*
151      * Look for an empty place in the picture heap.
152      */
153     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
154     {
155         p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
156                                  % I_RENDERPICTURES];
157
158         switch( p_pic->i_status )
159         {
160             case DESTROYED_PICTURE:
161                 /* Memory will not be reallocated, and function can end
162                  * immediately - this is the best possible case, since no
163                  * memory allocation needs to be done */
164                 p_pic->i_status   = RESERVED_PICTURE;
165                 p_pic->i_refcount = 0;
166                 p_pic->b_force    = 0;
167
168                 p_pic->b_progressive        = b_progressive;
169                 p_pic->i_nb_fields          = i_nb_fields;
170                 p_pic->b_top_field_first    = b_top_field_first;
171
172                 p_vout->i_heap_size++;
173                 p_vout->render.i_last_used_pic =
174                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
175                     % I_RENDERPICTURES;
176                 vlc_mutex_unlock( &p_vout->picture_lock );
177                 return( p_pic );
178
179             case FREE_PICTURE:
180                 /* Picture is empty and ready for allocation */
181                 p_vout->render.i_last_used_pic =
182                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
183                     % I_RENDERPICTURES;
184                 p_freepic = p_pic;
185                 break;
186
187             default:
188                 break;
189         }
190     }
191
192     /*
193      * Prepare picture
194      */
195     if( p_freepic != NULL )
196     {
197         vout_AllocatePicture( VLC_OBJECT(p_vout),
198                               p_freepic, p_vout->render.i_chroma,
199                               p_vout->render.i_width, p_vout->render.i_height,
200                               p_vout->render.i_aspect );
201
202         if( p_freepic->i_planes )
203         {
204             /* Copy picture information, set some default values */
205             p_freepic->i_status   = RESERVED_PICTURE;
206             p_freepic->i_type     = MEMORY_PICTURE;
207             p_freepic->b_slow     = 0;
208
209             p_freepic->i_refcount = 0;
210             p_freepic->b_force = 0;
211
212             p_freepic->b_progressive        = b_progressive;
213             p_freepic->i_nb_fields          = i_nb_fields;
214             p_freepic->b_top_field_first    = b_top_field_first;
215
216             p_freepic->i_matrix_coefficients = 1;
217
218             p_vout->i_heap_size++;
219         }
220         else
221         {
222             /* Memory allocation failed : set picture as empty */
223             p_freepic->i_status = FREE_PICTURE;
224             p_freepic = NULL;
225
226             msg_Err( p_vout, "picture allocation failed" );
227         }
228
229         vlc_mutex_unlock( &p_vout->picture_lock );
230
231         return( p_freepic );
232     }
233
234     /* No free or destroyed picture could be found, but the decoder
235      * will try again in a while. */
236     vlc_mutex_unlock( &p_vout->picture_lock );
237
238     return( NULL );
239 }
240
241 /**
242  * Remove a permanent or reserved picture from the heap
243  *
244  * This function frees a previously reserved picture or a permanent
245  * picture. It is meant to be used when the construction of a picture aborted.
246  * Note that the picture will be destroyed even if it is linked !
247  */
248 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
249 {
250     vlc_mutex_lock( &p_vout->picture_lock );
251
252 #ifndef NDEBUG
253     /* Check if picture status is valid */
254     if( (p_pic->i_status != RESERVED_PICTURE) &&
255         (p_pic->i_status != RESERVED_DATED_PICTURE) &&
256         (p_pic->i_status != RESERVED_DISP_PICTURE) )
257     {
258         msg_Err( p_vout, "picture to destroy %p has invalid status %d",
259                          p_pic, p_pic->i_status );
260     }
261 #endif
262
263     p_pic->i_status = DESTROYED_PICTURE;
264     p_vout->i_heap_size--;
265
266     vlc_mutex_unlock( &p_vout->picture_lock );
267 }
268
269 /**
270  * Increment reference counter of a picture
271  *
272  * This function increments the reference counter of a picture in the video
273  * heap. It needs a lock since several producer threads can access the picture.
274  */
275 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
276 {
277     vlc_mutex_lock( &p_vout->picture_lock );
278     p_pic->i_refcount++;
279     vlc_mutex_unlock( &p_vout->picture_lock );
280 }
281
282 /**
283  * Decrement reference counter of a picture
284  *
285  * This function decrement the reference counter of a picture in the video heap
286  */
287 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
288 {
289     vlc_mutex_lock( &p_vout->picture_lock );
290     p_pic->i_refcount--;
291
292     if( ( p_pic->i_refcount == 0 ) &&
293         ( p_pic->i_status == DISPLAYED_PICTURE ) )
294     {
295         p_pic->i_status = DESTROYED_PICTURE;
296         p_vout->i_heap_size--;
297     }
298
299     vlc_mutex_unlock( &p_vout->picture_lock );
300 }
301
302 static int vout_LockPicture( vout_thread_t *p_vout, picture_t *p_picture )
303 {
304     if( p_picture->pf_lock )
305         return p_picture->pf_lock( p_vout, p_picture );
306     return VLC_SUCCESS;
307 }
308 static void vout_UnlockPicture( vout_thread_t *p_vout, picture_t *p_picture )
309 {
310     if( p_picture->pf_unlock )
311         p_picture->pf_unlock( p_vout, p_picture );
312 }
313
314 /**
315  * Render a picture
316  *
317  * This function chooses whether the current picture needs to be copied
318  * before rendering, does the subpicture magic, and tells the video output
319  * thread which direct buffer needs to be displayed.
320  */
321 picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
322                                                        subpicture_t *p_subpic )
323 {
324     int i_scale_width, i_scale_height;
325
326     if( p_pic == NULL )
327     {
328         /* XXX: subtitles */
329         return NULL;
330     }
331
332     i_scale_width = p_vout->fmt_out.i_visible_width * 1000 /
333         p_vout->fmt_in.i_visible_width;
334     i_scale_height = p_vout->fmt_out.i_visible_height * 1000 /
335         p_vout->fmt_in.i_visible_height;
336
337     if( p_pic->i_type == DIRECT_PICTURE )
338     {
339         /* Picture is in a direct buffer. */
340
341         if( p_subpic != NULL )
342         {
343             /* We have subtitles. First copy the picture to
344              * the spare direct buffer, then render the
345              * subtitles. */
346             if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
347                 return NULL;
348
349             vout_CopyPicture( p_vout, PP_OUTPUTPICTURE[0], p_pic );
350
351             spu_RenderSubpictures( p_vout->p_spu, &p_vout->fmt_out,
352                                    PP_OUTPUTPICTURE[0], p_pic, p_subpic,
353                                    i_scale_width, i_scale_height );
354
355             vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
356
357             return PP_OUTPUTPICTURE[0];
358         }
359
360         /* No subtitles, picture is in a directbuffer so
361          * we can display it directly (even if it is still
362          * in use or not). */
363         return p_pic;
364     }
365
366     /* Not a direct buffer. We either need to copy it to a direct buffer,
367      * or render it if the chroma isn't the same. */
368     if( p_vout->b_direct )
369     {
370         /* Picture is not in a direct buffer, but is exactly the
371          * same size as the direct buffers. A memcpy() is enough,
372          * then render the subtitles. */
373
374         if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
375             return NULL;
376
377         vout_CopyPicture( p_vout, PP_OUTPUTPICTURE[0], p_pic );
378         spu_RenderSubpictures( p_vout->p_spu, &p_vout->fmt_out,
379                                PP_OUTPUTPICTURE[0], p_pic,
380                                p_subpic, i_scale_width, i_scale_height );
381
382         vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
383
384         return PP_OUTPUTPICTURE[0];
385     }
386
387     /* Picture is not in a direct buffer, and needs to be converted to
388      * another size/chroma. Then the subtitles need to be rendered as
389      * well. This usually means software YUV, or hardware YUV with a
390      * different chroma. */
391
392     if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
393     {
394         /* The picture buffer is in slow memory. We'll use
395          * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
396          * one for subpictures rendering. */
397         picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
398         if( p_tmp_pic->i_status == FREE_PICTURE )
399         {
400             vout_AllocatePicture( VLC_OBJECT(p_vout),
401                                   p_tmp_pic, p_vout->fmt_out.i_chroma,
402                                   p_vout->fmt_out.i_width,
403                                   p_vout->fmt_out.i_height,
404                                   p_vout->fmt_out.i_aspect );
405             p_tmp_pic->i_type = MEMORY_PICTURE;
406             p_tmp_pic->i_status = RESERVED_PICTURE;
407             /* some modules (such as blend)  needs to know the extra information in picture heap */
408             p_tmp_pic->p_heap = &p_vout->output;
409         }
410
411         /* Convert image to the first direct buffer */
412         p_vout->p_chroma->p_owner = (filter_owner_sys_t *)p_tmp_pic;
413         p_vout->p_chroma->pf_video_filter( p_vout->p_chroma, p_pic );
414
415         /* Render subpictures on the first direct buffer */
416         spu_RenderSubpictures( p_vout->p_spu, &p_vout->fmt_out, p_tmp_pic,
417                                p_tmp_pic, p_subpic,
418                                i_scale_width, i_scale_height );
419
420         if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
421             return NULL;
422
423         vout_CopyPicture( p_vout, &p_vout->p_picture[0], p_tmp_pic );
424     }
425     else
426     {
427         if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
428             return NULL;
429
430         /* Convert image to the first direct buffer */
431         p_vout->p_chroma->p_owner = (filter_owner_sys_t *)&p_vout->p_picture[0];
432         p_vout->p_chroma->pf_video_filter( p_vout->p_chroma, p_pic );
433
434         /* Render subpictures on the first direct buffer */
435         spu_RenderSubpictures( p_vout->p_spu, &p_vout->fmt_out,
436                                &p_vout->p_picture[0], &p_vout->p_picture[0],
437                                p_subpic, i_scale_width, i_scale_height );
438     }
439
440     vout_UnlockPicture( p_vout, &p_vout->p_picture[0] );
441
442     return &p_vout->p_picture[0];
443 }
444
445 /**
446  * Calculate image window coordinates
447  *
448  * This function will be accessed by plugins. It calculates the relative
449  * position of the output window and the image window.
450  */
451 void vout_PlacePicture( vout_thread_t *p_vout,
452                         unsigned int i_width, unsigned int i_height,
453                         unsigned int *pi_x, unsigned int *pi_y,
454                         unsigned int *pi_width, unsigned int *pi_height )
455 {
456     if( (i_width <= 0) || (i_height <=0) )
457     {
458         *pi_width = *pi_height = *pi_x = *pi_y = 0;
459         return;
460     }
461
462     if( p_vout->b_scale )
463     {
464         *pi_width = i_width;
465         *pi_height = i_height;
466     }
467     else
468     {
469         *pi_width = __MIN( i_width, p_vout->fmt_in.i_visible_width );
470         *pi_height = __MIN( i_height, p_vout->fmt_in.i_visible_height );
471     }
472
473      int64_t i_scaled_width = p_vout->fmt_in.i_visible_width * (int64_t)p_vout->fmt_in.i_sar_num *
474                               *pi_height / p_vout->fmt_in.i_visible_height / p_vout->fmt_in.i_sar_den;
475      int64_t i_scaled_height = p_vout->fmt_in.i_visible_height * (int64_t)p_vout->fmt_in.i_sar_den *
476                                *pi_width / p_vout->fmt_in.i_visible_width / p_vout->fmt_in.i_sar_num;
477
478     if( i_scaled_width <= 0 || i_scaled_height <= 0 )
479     {
480         msg_Warn( p_vout, "ignoring broken aspect ratio" );
481         i_scaled_width = *pi_width;
482         i_scaled_height = *pi_height;
483     }
484
485     if( i_scaled_width > *pi_width )
486         *pi_height = i_scaled_height;
487     else
488         *pi_width = i_scaled_width;
489
490     switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
491     {
492     case VOUT_ALIGN_LEFT:
493         *pi_x = 0;
494         break;
495     case VOUT_ALIGN_RIGHT:
496         *pi_x = i_width - *pi_width;
497         break;
498     default:
499         *pi_x = ( i_width - *pi_width ) / 2;
500     }
501
502     switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
503     {
504     case VOUT_ALIGN_TOP:
505         *pi_y = 0;
506         break;
507     case VOUT_ALIGN_BOTTOM:
508         *pi_y = i_height - *pi_height;
509         break;
510     default:
511         *pi_y = ( i_height - *pi_height ) / 2;
512     }
513 }
514
515 /**
516  * Allocate a new picture in the heap.
517  *
518  * This function allocates a fake direct buffer in memory, which can be
519  * used exactly like a video buffer. The video output thread then manages
520  * how it gets displayed.
521  */
522 int __vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
523                             vlc_fourcc_t i_chroma,
524                             int i_width, int i_height, int i_aspect )
525 {
526     int i_bytes, i_index, i_width_aligned, i_height_aligned;
527
528     /* Make sure the real dimensions are a multiple of 16 */
529     i_width_aligned = (i_width + 15) >> 4 << 4;
530     i_height_aligned = (i_height + 15) >> 4 << 4;
531
532     if( vout_InitPicture( p_this, p_pic, i_chroma,
533                           i_width, i_height, i_aspect ) != VLC_SUCCESS )
534     {
535         p_pic->i_planes = 0;
536         return VLC_EGENERIC;
537     }
538
539     /* Calculate how big the new image should be */
540     i_bytes = p_pic->format.i_bits_per_pixel *
541         i_width_aligned * i_height_aligned / 8;
542
543     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
544
545     if( p_pic->p_data == NULL )
546     {
547         p_pic->i_planes = 0;
548         return VLC_EGENERIC;
549     }
550
551     /* Fill the p_pixels field for each plane */
552     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
553
554     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
555     {
556         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
557             p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
558     }
559
560     return VLC_SUCCESS;
561 }
562
563 /**
564  * Initialise the video format fields given chroma/size.
565  *
566  * This function initializes all the video_frame_format_t fields given the
567  * static properties of a picture (chroma and size).
568  * \param p_format Pointer to the format structure to initialize
569  * \param i_chroma Chroma to set
570  * \param i_width Width to set
571  * \param i_height Height to set
572  * \param i_aspect Aspect ratio
573  */
574 void vout_InitFormat( video_frame_format_t *p_format, vlc_fourcc_t i_chroma,
575                       int i_width, int i_height, int i_aspect )
576 {
577     p_format->i_chroma   = i_chroma;
578     p_format->i_width    = p_format->i_visible_width  = i_width;
579     p_format->i_height   = p_format->i_visible_height = i_height;
580     p_format->i_x_offset = p_format->i_y_offset = 0;
581     p_format->i_aspect   = i_aspect;
582
583 #if 0
584     /* Assume we have square pixels */
585     if( i_width && i_height )
586         p_format->i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
587     else
588         p_format->i_aspect = 0;
589 #endif
590
591     switch( i_chroma )
592     {
593         case FOURCC_YUVA:
594             p_format->i_bits_per_pixel = 32;
595             break;
596         case FOURCC_I444:
597         case FOURCC_J444:
598             p_format->i_bits_per_pixel = 24;
599             break;
600         case FOURCC_I422:
601         case FOURCC_YUY2:
602         case FOURCC_UYVY:
603         case FOURCC_J422:
604             p_format->i_bits_per_pixel = 16;
605             break;
606         case FOURCC_I440:
607         case FOURCC_J440:
608             p_format->i_bits_per_pixel = 16;
609             break;
610         case FOURCC_I411:
611         case FOURCC_YV12:
612         case FOURCC_I420:
613         case FOURCC_J420:
614         case FOURCC_IYUV:
615             p_format->i_bits_per_pixel = 12;
616             break;
617         case FOURCC_I410:
618         case FOURCC_YVU9:
619             p_format->i_bits_per_pixel = 9;
620             break;
621         case FOURCC_Y211:
622             p_format->i_bits_per_pixel = 8;
623             break;
624         case FOURCC_YUVP:
625             p_format->i_bits_per_pixel = 8;
626             break;
627
628         case FOURCC_RV32:
629         case FOURCC_RGBA:
630             p_format->i_bits_per_pixel = 32;
631             break;
632         case FOURCC_RV24:
633             p_format->i_bits_per_pixel = 24;
634             break;
635         case FOURCC_RV15:
636         case FOURCC_RV16:
637             p_format->i_bits_per_pixel = 16;
638             break;
639         case FOURCC_RGB2:
640             p_format->i_bits_per_pixel = 8;
641             break;
642
643         case FOURCC_GREY:
644         case FOURCC_Y800:
645         case FOURCC_Y8:
646             p_format->i_bits_per_pixel = 8;
647             break;
648
649         default:
650             p_format->i_bits_per_pixel = 0;
651             break;
652     }
653 }
654
655 /**
656  * Initialise the picture_t fields given chroma/size.
657  *
658  * This function initializes most of the picture_t fields given a chroma and
659  * size. It makes the assumption that stride == width.
660  * \param p_this The calling object
661  * \param p_pic Pointer to the picture to initialize
662  * \param i_chroma The chroma fourcc to set
663  * \param i_width The width of the picture
664  * \param i_height The height of the picture
665  * \param i_aspect The aspect ratio of the picture
666  */
667 int __vout_InitPicture( vlc_object_t *p_this, picture_t *p_pic,
668                         vlc_fourcc_t i_chroma,
669                         int i_width, int i_height, int i_aspect )
670 {
671     int i_index, i_width_aligned, i_height_aligned;
672
673     /* Store default values */
674     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
675     {
676         p_pic->p[i_index].p_pixels = NULL;
677         p_pic->p[i_index].i_pixel_pitch = 1;
678     }
679
680     p_pic->pf_release = NULL;
681     p_pic->pf_lock = NULL;
682     p_pic->pf_unlock = NULL;
683     p_pic->i_refcount = 0;
684
685     p_pic->p_q = NULL;
686     p_pic->i_qstride = 0;
687     p_pic->i_qtype = 0;
688
689     vout_InitFormat( &p_pic->format, i_chroma, i_width, i_height, i_aspect );
690
691     /* Make sure the real dimensions are a multiple of 16 */
692     i_width_aligned = (i_width + 15) >> 4 << 4;
693     i_height_aligned = (i_height + 15) >> 4 << 4;
694
695     /* Calculate coordinates */
696     switch( i_chroma )
697     {
698         case FOURCC_I411:
699             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
700             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
701             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
702             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
703             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
704             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
705             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
706             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
707             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
708             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
709             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
710             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
711             p_pic->i_planes = 3;
712             break;
713
714         case FOURCC_I410:
715         case FOURCC_YVU9:
716             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
717             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
718             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
719             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
720             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 4;
721             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 4;
722             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
723             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
724             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 4;
725             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 4;
726             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
727             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
728             p_pic->i_planes = 3;
729             break;
730
731         case FOURCC_YV12:
732         case FOURCC_I420:
733         case FOURCC_IYUV:
734         case FOURCC_J420:
735             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
736             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
737             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
738             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
739             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 2;
740             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 2;
741             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
742             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
743             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 2;
744             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 2;
745             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
746             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
747             p_pic->i_planes = 3;
748             break;
749
750         case FOURCC_I422:
751         case FOURCC_J422:
752             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
753             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
754             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
755             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
756             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
757             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
758             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
759             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
760             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
761             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
762             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
763             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
764             p_pic->i_planes = 3;
765             break;
766
767         case FOURCC_I440:
768         case FOURCC_J440:
769             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
770             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
771             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
772             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
773             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 2;
774             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 2;
775             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
776             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
777             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 2;
778             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 2;
779             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
780             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
781             p_pic->i_planes = 3;
782             break;
783
784         case FOURCC_I444:
785         case FOURCC_J444:
786             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
787             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
788             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
789             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
790             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
791             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
792             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
793             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
794             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
795             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
796             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
797             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
798             p_pic->i_planes = 3;
799             break;
800
801         case FOURCC_YUVA:
802             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
803             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
804             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
805             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
806             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
807             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
808             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
809             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
810             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
811             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
812             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
813             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
814             p_pic->p[ A_PLANE ].i_lines = i_height_aligned;
815             p_pic->p[ A_PLANE ].i_visible_lines = i_height;
816             p_pic->p[ A_PLANE ].i_pitch = i_width_aligned;
817             p_pic->p[ A_PLANE ].i_visible_pitch = i_width;
818             p_pic->i_planes = 4;
819             break;
820
821         case FOURCC_YUVP:
822             p_pic->p->i_lines = i_height_aligned;
823             p_pic->p->i_visible_lines = i_height;
824             p_pic->p->i_pitch = i_width_aligned;
825             p_pic->p->i_visible_pitch = i_width;
826             p_pic->p->i_pixel_pitch = 8;
827             p_pic->i_planes = 1;
828             break;
829
830         case FOURCC_Y211:
831             p_pic->p->i_lines = i_height_aligned;
832             p_pic->p->i_visible_lines = i_height;
833             p_pic->p->i_pitch = i_width_aligned;
834             p_pic->p->i_visible_pitch = i_width;
835             p_pic->p->i_pixel_pitch = 4;
836             p_pic->i_planes = 1;
837             break;
838
839         case FOURCC_UYVY:
840         case FOURCC_YUY2:
841             p_pic->p->i_lines = i_height_aligned;
842             p_pic->p->i_visible_lines = i_height;
843             p_pic->p->i_pitch = i_width_aligned * 2;
844             p_pic->p->i_visible_pitch = i_width * 2;
845             p_pic->p->i_pixel_pitch = 4;
846             p_pic->i_planes = 1;
847             break;
848
849         case FOURCC_RGB2:
850             p_pic->p->i_lines = i_height_aligned;
851             p_pic->p->i_visible_lines = i_height;
852             p_pic->p->i_pitch = i_width_aligned;
853             p_pic->p->i_visible_pitch = i_width;
854             p_pic->p->i_pixel_pitch = 1;
855             p_pic->i_planes = 1;
856             break;
857
858         case FOURCC_RV15:
859             p_pic->p->i_lines = i_height_aligned;
860             p_pic->p->i_visible_lines = i_height;
861             p_pic->p->i_pitch = i_width_aligned * 2;
862             p_pic->p->i_visible_pitch = i_width * 2;
863             p_pic->p->i_pixel_pitch = 2;
864             p_pic->i_planes = 1;
865             break;
866
867         case FOURCC_RV16:
868             p_pic->p->i_lines = i_height_aligned;
869             p_pic->p->i_visible_lines = i_height;
870             p_pic->p->i_pitch = i_width_aligned * 2;
871             p_pic->p->i_visible_pitch = i_width * 2;
872             p_pic->p->i_pixel_pitch = 2;
873             p_pic->i_planes = 1;
874             break;
875
876         case FOURCC_RV24:
877             p_pic->p->i_lines = i_height_aligned;
878             p_pic->p->i_visible_lines = i_height;
879             p_pic->p->i_pitch = i_width_aligned * 3;
880             p_pic->p->i_visible_pitch = i_width * 3;
881             p_pic->p->i_pixel_pitch = 3;
882             p_pic->i_planes = 1;
883             break;
884
885         case FOURCC_RV32:
886         case FOURCC_RGBA:
887             p_pic->p->i_lines = i_height_aligned;
888             p_pic->p->i_visible_lines = i_height;
889             p_pic->p->i_pitch = i_width_aligned * 4;
890             p_pic->p->i_visible_pitch = i_width * 4;
891             p_pic->p->i_pixel_pitch = 4;
892             p_pic->i_planes = 1;
893             break;
894
895         case FOURCC_GREY:
896         case FOURCC_Y800:
897         case FOURCC_Y8:
898             p_pic->p->i_lines = i_height_aligned;
899             p_pic->p->i_visible_lines = i_height;
900             p_pic->p->i_pitch = i_width_aligned;
901             p_pic->p->i_visible_pitch = i_width;
902             p_pic->p->i_pixel_pitch = 1;
903             p_pic->i_planes = 1;
904             break;
905
906         default:
907             if( p_this )
908                 msg_Err( p_this, "unknown chroma type 0x%.8x (%4.4s)",
909                                  i_chroma, (char*)&i_chroma );
910             p_pic->i_planes = 0;
911             return VLC_EGENERIC;
912     }
913
914     return VLC_SUCCESS;
915 }
916
917 /**
918  * Compare two chroma values
919  *
920  * This function returns 1 if the two fourcc values given as argument are
921  * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
922  */
923 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
924 {
925     /* If they are the same, they are the same ! */
926     if( i_chroma == i_amorhc )
927     {
928         return 1;
929     }
930
931     /* Check for equivalence classes */
932     switch( i_chroma )
933     {
934         case FOURCC_I420:
935         case FOURCC_IYUV:
936         case FOURCC_YV12:
937             switch( i_amorhc )
938             {
939                 case FOURCC_I420:
940                 case FOURCC_IYUV:
941                 case FOURCC_YV12:
942                     return 1;
943
944                 default:
945                     return 0;
946             }
947
948         case FOURCC_UYVY:
949         case FOURCC_UYNV:
950         case FOURCC_Y422:
951             switch( i_amorhc )
952             {
953                 case FOURCC_UYVY:
954                 case FOURCC_UYNV:
955                 case FOURCC_Y422:
956                     return 1;
957
958                 default:
959                     return 0;
960             }
961
962         case FOURCC_YUY2:
963         case FOURCC_YUNV:
964             switch( i_amorhc )
965             {
966                 case FOURCC_YUY2:
967                 case FOURCC_YUNV:
968                     return 1;
969
970                 default:
971                     return 0;
972             }
973
974         case FOURCC_GREY:
975         case FOURCC_Y800:
976         case FOURCC_Y8:
977             switch( i_amorhc )
978             {
979                 case FOURCC_GREY:
980                 case FOURCC_Y800:
981                 case FOURCC_Y8:
982                     return 1;
983
984                 default:
985                     return 0;
986             }
987
988         default:
989             return 0;
990     }
991 }
992
993 /*****************************************************************************
994  * vout_CopyPicture: copy a picture to another one
995  *****************************************************************************
996  * This function takes advantage of the image format, and reduces the
997  * number of calls to memcpy() to the minimum. Source and destination
998  * images must have same width (hence i_visible_pitch), height, and chroma.
999  *****************************************************************************/
1000 void __vout_CopyPicture( vlc_object_t *p_this,
1001                          picture_t *p_dest, picture_t *p_src )
1002 {
1003     VLC_UNUSED(p_this);
1004     picture_Copy( p_dest, p_src );
1005 }
1006
1007 /*****************************************************************************
1008  *
1009  *****************************************************************************/
1010 static void PictureReleaseCallback( picture_t *p_picture )
1011 {
1012     if( --p_picture->i_refcount > 0 )
1013         return;
1014     picture_Delete( p_picture );
1015 }
1016 /*****************************************************************************
1017  *
1018  *****************************************************************************/
1019 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
1020 {
1021     picture_t *p_picture = malloc( sizeof(*p_picture) );
1022
1023     if( !p_picture )
1024         return NULL;
1025
1026     memset( p_picture, 0, sizeof(*p_picture) );
1027     if( __vout_AllocatePicture( NULL, p_picture,
1028                                 i_chroma, i_width, i_height, i_aspect ) )
1029     {
1030         free( p_picture );
1031         return NULL;
1032     }
1033
1034     p_picture->i_refcount = 1;
1035     p_picture->pf_release = PictureReleaseCallback;
1036     p_picture->i_status = RESERVED_PICTURE;
1037
1038     return p_picture;
1039 }
1040
1041 /*****************************************************************************
1042  *
1043  *****************************************************************************/
1044 void picture_Delete( picture_t *p_picture )
1045 {
1046     assert( p_picture && p_picture->i_refcount == 0 );
1047
1048     free( p_picture->p_data_orig );
1049     free( p_picture->p_sys );
1050     free( p_picture );
1051 }
1052
1053 /*****************************************************************************
1054  *
1055  *****************************************************************************/
1056 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
1057 {
1058     int i;
1059
1060     for( i = 0; i < p_src->i_planes ; i++ )
1061         plane_CopyPixels( p_dst->p+i, p_src->p+i );
1062 }
1063
1064 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
1065 {
1066     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
1067                                      p_src->i_visible_pitch );
1068     const unsigned i_height = __MIN( p_dst->i_visible_lines,
1069                                      p_src->i_visible_lines );
1070
1071     if( p_src->i_pitch == p_dst->i_pitch )
1072     {
1073         /* There are margins, but with the same width : perfect ! */
1074         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
1075                     p_src->i_pitch * i_height );
1076     }
1077     else
1078     {
1079         /* We need to proceed line by line */
1080         uint8_t *p_in = p_src->p_pixels;
1081         uint8_t *p_out = p_dst->p_pixels;
1082         int i_line;
1083
1084         assert( p_in );
1085         assert( p_out );
1086
1087         for( i_line = i_height; i_line--; )
1088         {
1089             vlc_memcpy( p_out, p_in, i_width );
1090             p_in += p_src->i_pitch;
1091             p_out += p_dst->i_pitch;
1092         }
1093     }
1094 }
1095
1096 /*****************************************************************************
1097  *
1098  *****************************************************************************/
1099
1100