]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
fde53fd6c79afbb84a5194c86fcac3fbc3b376bf
[vlc] / src / video_output / vout_pictures.c
1 /*****************************************************************************
2  * vout_pictures.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: vout_pictures.c,v 1.16 2002/02/19 00:50:20 sam Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <videolan/vlc.h>
34
35 #include "video.h"
36 #include "video_output.h"
37
38 /*****************************************************************************
39  * vout_DisplayPicture: display a picture
40  *****************************************************************************
41  * Remove the reservation flag of a picture, which will cause it to be ready for
42  * 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         intf_ErrMsg( "error: picture %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  * vout_DatePicture: 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  *****************************************************************************/
72 void vout_DatePicture( vout_thread_t *p_vout,
73                        picture_t *p_pic, mtime_t date )
74 {
75     vlc_mutex_lock( &p_vout->picture_lock );
76     p_pic->date = date;
77     switch( p_pic->i_status )
78     {
79     case RESERVED_PICTURE:
80         p_pic->i_status = RESERVED_DATED_PICTURE;
81         break;
82     case RESERVED_DISP_PICTURE:
83         p_pic->i_status = READY_PICTURE;
84         break;
85     default:
86         intf_ErrMsg( "error: picture %p has invalid status %d",
87                      p_pic, p_pic->i_status );
88         break;
89     }
90
91     vlc_mutex_unlock( &p_vout->picture_lock );
92 }
93
94 /*****************************************************************************
95  * vout_CreatePicture: allocate a picture in the video output heap.
96  *****************************************************************************
97  * This function creates a reserved image in the video output heap.
98  * A null pointer is returned if the function fails. This method provides an
99  * already allocated zone of memory in the picture data fields. It needs locking
100  * since several pictures can be created by several producers threads.
101  *****************************************************************************/
102 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
103                                boolean_t b_progressive,
104                                boolean_t b_top_field_first,
105                                boolean_t b_repeat_first_field )
106 {
107     int         i_pic;                                      /* picture index */
108     picture_t * p_pic;
109     picture_t * p_freepic = NULL;                      /* first free picture */
110
111     /* Get lock */
112     vlc_mutex_lock( &p_vout->picture_lock );
113
114     /*
115      * Look for an empty place. We start at 1 because the first
116      * directbuffer is reserved for memcpy()ed pictures.
117      */
118     for( i_pic = 0; i_pic < I_RENDERPICTURES && p_freepic == NULL; i_pic++ )
119     {
120         p_pic = PP_RENDERPICTURE[ i_pic ];
121
122         /* If the picture we found is a memory buffer, and we have enough
123          * pictures in the stack, and we might have enough room later for
124          * a direct buffer, skip it. If no other pictures are found, the
125          * video decoder will try again later. */
126         if( p_vout->b_direct && ( p_vout->output.i_pictures > 5 )
127              && ( p_pic->i_type != DIRECT_PICTURE ) )
128         {
129             break;
130         }
131
132         switch( p_pic->i_status )
133         {
134             case DESTROYED_PICTURE:
135                 /* Memory will not be reallocated, and function can end
136                  * immediately - this is the best possible case, since no
137                  * memory allocation needs to be done */
138                 p_pic->i_status   = RESERVED_PICTURE;
139                 p_pic->i_refcount = 0;
140
141                 p_pic->b_progressive        = b_progressive;
142                 p_pic->b_repeat_first_field = b_repeat_first_field;
143                 p_pic->b_top_field_first    = b_top_field_first;
144
145                 p_vout->i_heap_size++;
146                 vlc_mutex_unlock( &p_vout->picture_lock );
147                 return( p_pic );
148
149             case FREE_PICTURE:
150                 /* Picture is empty and ready for allocation */
151                 p_freepic = p_pic;
152                 break;
153
154             default:
155                 break;
156         }
157     }
158
159     /*
160      * Prepare picture
161      */
162     if( p_freepic != NULL )
163     {
164         vout_AllocatePicture( p_freepic,
165                               p_vout->render.i_width, p_vout->render.i_height,
166                               p_vout->render.i_chroma );
167
168         if( p_freepic->i_planes )
169         {
170             /* Copy picture information, set some default values */
171             p_freepic->i_status   = RESERVED_PICTURE;
172             p_freepic->i_type     = MEMORY_PICTURE;
173
174             p_freepic->i_refcount = 0;
175
176             p_freepic->b_progressive        = b_progressive;
177             p_freepic->b_repeat_first_field = b_repeat_first_field;
178             p_freepic->b_top_field_first    = b_top_field_first;
179
180             p_freepic->i_matrix_coefficients = 1;
181
182             p_vout->i_heap_size++;
183         }
184         else
185         {
186             /* Memory allocation failed : set picture as empty */
187             p_freepic->i_status = FREE_PICTURE;
188             p_freepic = NULL;
189
190             intf_ErrMsg( "vout error: picture allocation failed" );
191         }
192
193         vlc_mutex_unlock( &p_vout->picture_lock );
194
195         /* Initialize mutex */
196         vlc_mutex_init( &(p_freepic->lock_deccount) );
197
198         return( p_freepic );
199     }
200
201     /* No free or destroyed picture could be found, but the decoder
202      * will try again in a while. */
203     vlc_mutex_unlock( &p_vout->picture_lock );
204
205     return( NULL );
206 }
207
208 /*****************************************************************************
209  * vout_DestroyPicture: remove a permanent or reserved picture from the heap
210  *****************************************************************************
211  * This function frees a previously reserved picture or a permanent
212  * picture. It is meant to be used when the construction of a picture aborted.
213  * Note that the picture will be destroyed even if it is linked !
214  *****************************************************************************/
215 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
216 {
217     vlc_mutex_lock( &p_vout->picture_lock );
218
219 #ifdef DEBUG
220     /* Check if picture status is valid */
221     if( (p_pic->i_status != RESERVED_PICTURE) &&
222         (p_pic->i_status != RESERVED_DATED_PICTURE) &&
223         (p_pic->i_status != RESERVED_DISP_PICTURE) )
224     {
225         intf_ErrMsg( "error: picture %p has invalid status %d",
226                      p_pic, p_pic->i_status );
227     }
228 #endif
229
230     p_pic->i_status = DESTROYED_PICTURE;
231     p_vout->i_heap_size--;
232
233     /* destroy the lock that had been initialized in CreatePicture */
234     vlc_mutex_destroy( &(p_pic->lock_deccount) );
235
236     vlc_mutex_unlock( &p_vout->picture_lock );
237 }
238
239 /*****************************************************************************
240  * vout_LinkPicture: increment reference counter of a picture
241  *****************************************************************************
242  * This function increments the reference counter of a picture in the video
243  * heap. It needs a lock since several producer threads can access the picture.
244  *****************************************************************************/
245 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
246 {
247     vlc_mutex_lock( &p_vout->picture_lock );
248     p_pic->i_refcount++;
249     vlc_mutex_unlock( &p_vout->picture_lock );
250 }
251
252 /*****************************************************************************
253  * vout_UnlinkPicture: decrement reference counter of a picture
254  *****************************************************************************
255  * This function decrement the reference counter of a picture in the video heap.
256  *****************************************************************************/
257 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
258 {
259     vlc_mutex_lock( &p_vout->picture_lock );
260     p_pic->i_refcount--;
261
262     if( p_pic->i_refcount < 0 )
263     {
264         intf_ErrMsg( "vout error: picture refcount is %i", p_pic->i_refcount );
265         p_pic->i_refcount = 0;
266     }
267
268     if( ( p_pic->i_refcount == 0 ) &&
269         ( p_pic->i_status == DISPLAYED_PICTURE ) )
270     {
271         p_pic->i_status = DESTROYED_PICTURE;
272         p_vout->i_heap_size--;
273     }
274
275     vlc_mutex_unlock( &p_vout->picture_lock );
276 }
277
278 /*****************************************************************************
279  * vout_RenderPicture: render a picture
280  *****************************************************************************
281  * This function chooses whether the current picture needs to be copied
282  * before rendering, does the subpicture magic, and tells the video output
283  * thread which direct buffer needs to be displayed.
284  *****************************************************************************/
285 picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
286                                                        subpicture_t *p_subpic )
287 {
288     if( p_pic == NULL )
289     {
290         /* XXX: subtitles */
291
292         return NULL;
293     }
294
295     if( p_pic->i_type == DIRECT_PICTURE )
296     {
297         if( p_pic->i_refcount )
298         {
299             /* Picture is in a direct buffer and is still in use,
300              * we need to copy it to another direct buffer before
301              * displaying it if there are subtitles. */
302             if( p_subpic != NULL )
303             {
304                 /* We have subtitles. First copy the picture to
305                  * the spare direct buffer, then render the
306                  * subtitles. */
307                 vout_CopyPicture( p_pic, PP_OUTPUTPICTURE[0] );
308
309                 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
310
311                 return PP_OUTPUTPICTURE[0];
312             }
313
314             /* No subtitles, picture is in a directbuffer so
315              * we can display it directly even if it is still
316              * in use. */
317             return p_pic;
318         }
319
320         /* Picture is in a direct buffer but isn't used by the
321          * decoder. We can safely render subtitles on it and
322          * display it. */
323         vout_RenderSubPictures( p_vout, p_pic, p_subpic );
324
325         return p_pic;
326     }
327
328     /* Not a direct buffer. We either need to copy it to a direct buffer,
329      * or render it if the chroma isn't the same. */
330     if( p_vout->b_direct )
331     {
332         /* Picture is not in a direct buffer, but is exactly the
333          * same size as the direct buffers. A memcpy() is enough,
334          * then render the subtitles. */
335         vout_CopyPicture( p_pic, PP_OUTPUTPICTURE[0] );
336
337         vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
338
339         return PP_OUTPUTPICTURE[0];
340     }
341
342     /* Picture is not in a direct buffer, and needs to be converted to
343      * another size/chroma. Then the subtitles need to be rendered as
344      * well. This usually means software YUV, or hardware YUV with a
345      * different chroma. */
346
347     /* Convert image to the first direct buffer */
348     p_vout->chroma.pf_convert( p_vout, p_pic, &p_vout->p_picture[0] );
349
350     /* Render subpictures on the first direct buffer */
351     vout_RenderSubPictures( p_vout, &p_vout->p_picture[0], p_subpic );
352
353     return &p_vout->p_picture[0];
354 }
355
356 /*****************************************************************************
357  * vout_PlacePicture: calculate image window coordinates
358  *****************************************************************************
359  * This function will be accessed by plugins. It calculates the relative
360  * position of the output window and the image window.
361  *****************************************************************************/
362 void vout_PlacePicture( vout_thread_t *p_vout, int i_width, int i_height,
363                         int *pi_x, int *pi_y, int *pi_width, int *pi_height )
364 {
365     if( (i_width <= 0) || (i_height <=0) )
366     {
367         *pi_width = *pi_height = *pi_x = *pi_y = 0;
368
369         return;
370     }
371
372     if( p_vout->b_scale )
373     {
374         *pi_width = i_width;
375         *pi_height = i_height;
376     }
377     else
378     {
379         *pi_width = MIN( i_width, p_vout->render.i_width );
380         *pi_height = MIN( i_height, p_vout->render.i_height );
381     }
382
383     if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
384     {
385         *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
386     }
387     else
388     {
389         *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
390     }
391
392     if( *pi_width > i_width )
393     {
394         *pi_width = i_width;
395         *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
396     }
397
398     if( *pi_height > i_height )
399     {
400         *pi_height = i_height;
401         *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
402     }
403
404     *pi_x = ( i_width - *pi_width ) / 2;
405     *pi_y = ( i_height - *pi_height ) / 2;
406 }
407
408 /*****************************************************************************
409  * vout_AllocatePicture: allocate a new picture in the heap.
410  *****************************************************************************
411  * This function allocates a fake direct buffer in memory, which can be
412  * used exactly like a video buffer. The video output thread then manages
413  * how it gets displayed.
414  *****************************************************************************/
415 void vout_AllocatePicture( picture_t *p_pic,
416                            int i_width, int i_height, u32 i_chroma )
417 {
418     int i_bytes, i_index;
419
420     /* Store default values */
421     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
422     {
423         p_pic->p[i_index].p_pixels = NULL;
424         p_pic->p[i_index].b_margin = 0;
425         p_pic->p[i_index].i_pixel_bytes = 1;
426     }
427
428     /* Calculate coordinates */
429     switch( i_chroma )
430     {
431         case FOURCC_YV12:
432         case FOURCC_I420:
433         case FOURCC_IYUV:
434             p_pic->p[ Y_PLANE ].i_lines = i_height;
435             p_pic->p[ Y_PLANE ].i_pitch = i_width;
436             p_pic->p[ U_PLANE ].i_lines = i_height / 2;
437             p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
438             p_pic->p[ V_PLANE ].i_lines = i_height / 2;
439             p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
440             p_pic->i_planes = 3;
441             break;
442
443         case FOURCC_I422:
444             p_pic->p[ Y_PLANE ].i_lines = i_height;
445             p_pic->p[ Y_PLANE ].i_pitch = i_width;
446             p_pic->p[ U_PLANE ].i_lines = i_height;
447             p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
448             p_pic->p[ V_PLANE ].i_lines = i_height;
449             p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
450             p_pic->i_planes = 3;
451             break;
452
453         case FOURCC_I444:
454             p_pic->p[ Y_PLANE ].i_lines = i_height;
455             p_pic->p[ Y_PLANE ].i_pitch = i_width;
456             p_pic->p[ U_PLANE ].i_lines = i_height;
457             p_pic->p[ U_PLANE ].i_pitch = i_width;
458             p_pic->p[ V_PLANE ].i_lines = i_height;
459             p_pic->p[ V_PLANE ].i_pitch = i_width;
460             p_pic->i_planes = 3;
461             break;
462
463         case FOURCC_Y211:
464             p_pic->p->i_lines = i_height;
465             p_pic->p->i_pitch = i_width;
466             p_pic->p->i_pixel_bytes = 4;
467             p_pic->i_planes = 1;
468             break;
469
470         case FOURCC_RV15:
471             p_pic->p->i_lines = i_height;
472             p_pic->p->i_pitch = i_width * 2;
473             p_pic->p->i_pixel_bytes = 2;
474             p_pic->p->i_red_mask =   0x001f;
475             p_pic->p->i_green_mask = 0x03e0;
476             p_pic->p->i_blue_mask =  0x7c00;
477             p_pic->i_planes = 1;
478             break;
479
480         case FOURCC_RV16:
481             p_pic->p->i_lines = i_height;
482             p_pic->p->i_pitch = i_width * 2;
483             p_pic->p->i_pixel_bytes = 2;
484             p_pic->p->i_red_mask =   0x001f;
485             p_pic->p->i_green_mask = 0x07e0;
486             p_pic->p->i_blue_mask =  0xf800;
487             p_pic->i_planes = 1;
488             break;
489
490         case FOURCC_RV32:
491             p_pic->p->i_lines = i_height;
492             p_pic->p->i_pitch = i_width * 4;
493             p_pic->p->i_pixel_bytes = 4;
494             p_pic->p->i_red_mask =   0xff0000;
495             p_pic->p->i_green_mask = 0x00ff00;
496             p_pic->p->i_blue_mask =  0x0000ff;
497             p_pic->i_planes = 1;
498             break;
499
500         default:
501             intf_ErrMsg( "vout error: unknown chroma type 0x%.8x (%4.4s)",
502                          i_chroma, (char*)&i_chroma );
503             p_pic->i_planes = 0;
504             return;
505     }
506
507     /* Calculate how big the new image should be */
508     for( i_bytes = 0, i_index = 0; i_index < p_pic->i_planes; i_index++ )
509     {
510         i_bytes += p_pic->p[ i_index ].i_lines * p_pic->p[ i_index ].i_pitch;
511     }
512
513     p_pic->p_data = memalign( 16, i_bytes );
514
515     if( p_pic->p_data == NULL )
516     {
517         p_pic->i_planes = 0;
518         return;
519     }
520
521     /* Fill the p_pixels field for each plane */
522     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
523
524     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
525     {
526         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels
527                                           + p_pic->p[i_index-1].i_lines
528                                              * p_pic->p[i_index-1].i_pitch;
529     }
530 }
531