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