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