]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
Reworked the way pictures are handled by the vout core.
[vlc] / src / video_output / vout_pictures.c
1 /*****************************************************************************
2  * vout_pictures.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <libvlc.h>
36 #include <vlc_vout.h>
37 #include <vlc_osd.h>
38 #include <vlc_filter.h>
39 #include <vlc_image.h>
40 #include <vlc_block.h>
41 #include <vlc_picture_fifo.h>
42 #include <vlc_picture_pool.h>
43
44 #include "vout_pictures.h"
45 #include "vout_internal.h"
46
47 static void tracep(const char *msg, picture_t *picture)
48 {
49 //fprintf(stderr, "########## %s === picture=%p::%d\n", msg,
50 //                picture, picture ? picture->i_refcount : -1);
51 }
52
53 /**
54  * Display a picture
55  *
56  * Remove the reservation flag of a picture, which will cause it to be ready
57  * for display.
58  */
59 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
60 {
61     vlc_mutex_lock( &p_vout->p->picture_lock );
62
63     tracep("vout_DisplayPicture", p_pic);
64
65     p_pic->p_next = NULL;
66     picture_fifo_Push(p_vout->p->decoder_fifo, p_pic);
67
68     vlc_cond_signal( &p_vout->p->picture_wait );
69     vlc_mutex_unlock( &p_vout->p->picture_lock );
70 }
71
72 /**
73  * Allocate a picture in the video output heap.
74  *
75  * This function creates a reserved image in the video output heap.
76  * A null pointer is returned if the function fails. This method provides an
77  * already allocated zone of memory in the picture data fields.
78  * It needs locking since several pictures can be created by several producers
79  * threads.
80  */
81 int vout_CountPictureAvailable( vout_thread_t *p_vout )
82 {
83 #warning "TODO remove vout_CountPictureAvailable"
84     return VOUT_MAX_PICTURES;
85 }
86
87 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
88                                bool b_progressive,
89                                bool b_top_field_first,
90                                unsigned int i_nb_fields )
91 {
92 #warning "TODO remove unused vout_CreatePicture parameters"
93     /* Get lock */
94     vlc_mutex_lock( &p_vout->p->picture_lock );
95     picture_t *p_pic = picture_pool_Get(p_vout->p->decoder_pool);
96     if (p_pic) {
97         picture_Reset(p_pic);
98         p_pic->p_next = NULL; // FIXME put it in picture_Reset ?
99     }
100     tracep("vout_CreatePicture", p_pic);
101     vlc_mutex_unlock( &p_vout->p->picture_lock );
102
103     return p_pic;
104 }
105
106 /* */
107 void vout_DropPicture( vout_thread_t *p_vout, picture_t *p_pic  )
108 {
109     vlc_mutex_lock( &p_vout->p->picture_lock );
110
111     tracep("vout_DropPicture", p_pic);
112     picture_Release( p_pic );
113
114     vlc_cond_signal( &p_vout->p->picture_wait );
115     vlc_mutex_unlock( &p_vout->p->picture_lock );
116 }
117
118 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
119 {
120     tracep("vout_DestroyPicture", p_pic);
121     vout_DropPicture( p_vout, p_pic );
122 }
123
124
125 /**
126  * Increment reference counter of a picture
127  *
128  * This function increments the reference counter of a picture in the video
129  * heap. It needs a lock since several producer threads can access the picture.
130  */
131 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
132 {
133     vlc_mutex_lock( &p_vout->p->picture_lock );
134     tracep("vout_LinkPicture", p_pic);
135     picture_Hold( p_pic );
136     vlc_mutex_unlock( &p_vout->p->picture_lock );
137 }
138
139 /**
140  * Decrement reference counter of a picture
141  *
142  * This function decrement the reference counter of a picture in the video heap
143  */
144 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
145 {
146     vlc_mutex_lock( &p_vout->p->picture_lock );
147     tracep("vout_UnlinkPicture", p_pic);
148     picture_Release( p_pic );
149
150     vlc_cond_signal( &p_vout->p->picture_wait );
151     vlc_mutex_unlock( &p_vout->p->picture_lock );
152 }
153
154 /**
155  * Allocate a new picture in the heap.
156  *
157  * This function allocates a fake direct buffer in memory, which can be
158  * used exactly like a video buffer. The video output thread then manages
159  * how it gets displayed.
160  */
161 static int vout_AllocatePicture( picture_t *p_pic,
162                                  vlc_fourcc_t i_chroma,
163                                  int i_width, int i_height,
164                                  int i_sar_num, int i_sar_den )
165 {
166     /* Make sure the real dimensions are a multiple of 16 */
167     if( picture_Setup( p_pic, i_chroma, i_width, i_height,
168                        i_sar_num, i_sar_den ) != VLC_SUCCESS )
169         return VLC_EGENERIC;
170
171     /* Calculate how big the new image should be */
172     size_t i_bytes = 0;
173     for( int i = 0; i < p_pic->i_planes; i++ )
174     {
175         const plane_t *p = &p_pic->p[i];
176
177         if( p->i_pitch <= 0 || p->i_lines <= 0 ||
178             p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
179         {
180             p_pic->i_planes = 0;
181             return VLC_ENOMEM;
182         }
183         i_bytes += p->i_pitch * p->i_lines;
184     }
185
186     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
187     if( p_pic->p_data == NULL )
188     {
189         p_pic->i_planes = 0;
190         return VLC_EGENERIC;
191     }
192
193     /* Fill the p_pixels field for each plane */
194     p_pic->p[0].p_pixels = p_pic->p_data;
195     for( int i = 1; i < p_pic->i_planes; i++ )
196     {
197         p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
198                                                         p_pic->p[i-1].i_pitch ];
199     }
200
201     return VLC_SUCCESS;
202 }
203
204 /*****************************************************************************
205  *
206  *****************************************************************************/
207 static void PictureReleaseCallback( picture_t *p_picture )
208 {
209     if( --p_picture->i_refcount > 0 )
210         return;
211     picture_Delete( p_picture );
212 }
213
214 /*****************************************************************************
215  *
216  *****************************************************************************/
217 void picture_Reset( picture_t *p_picture )
218 {
219     /* */
220     p_picture->date = VLC_TS_INVALID;
221     p_picture->b_force = false;
222     p_picture->b_progressive = false;
223     p_picture->i_nb_fields = 0;
224     p_picture->b_top_field_first = false;
225     picture_CleanupQuant( p_picture );
226 }
227
228 /*****************************************************************************
229  *
230  *****************************************************************************/
231 typedef struct
232 {
233     unsigned     i_plane_count;
234     struct
235     {
236         struct
237         {
238             unsigned i_num;
239             unsigned i_den;
240         } w;
241         struct
242         {
243             unsigned i_num;
244             unsigned i_den;
245         } h;
246     } p[VOUT_MAX_PLANES];
247     unsigned i_pixel_size;
248
249 } chroma_description_t;
250
251 #define PLANAR(n, w_den, h_den) \
252     { n, { {{1,1}, {1,1}}, {{1,w_den}, {1,h_den}}, {{1,w_den}, {1,h_den}}, {{1,1}, {1,1}} }, 1 }
253 #define PACKED(size) \
254     { 1, { {{1,1}, {1,1}} }, size }
255
256 static const struct
257 {
258     vlc_fourcc_t            p_fourcc[5];
259     chroma_description_t    description;
260 } p_chromas[] = {
261     { { VLC_CODEC_I411, 0 },                                 PLANAR(3, 4, 1) },
262     { { VLC_CODEC_I410, VLC_CODEC_YV9, 0 },                  PLANAR(3, 4, 4) },
263     { { VLC_CODEC_YV12, VLC_CODEC_I420, VLC_CODEC_J420, 0 }, PLANAR(3, 2, 2) },
264     { { VLC_CODEC_I422, VLC_CODEC_J422, 0 },                 PLANAR(3, 2, 1) },
265     { { VLC_CODEC_I440, VLC_CODEC_J440, 0 },                 PLANAR(3, 1, 2) },
266     { { VLC_CODEC_I444, VLC_CODEC_J444, 0 },                 PLANAR(3, 1, 1) },
267     { { VLC_CODEC_YUVA, 0 },                                 PLANAR(4, 1, 1) },
268
269     { { VLC_CODEC_UYVY, VLC_CODEC_VYUY, VLC_CODEC_YUYV, VLC_CODEC_YVYU, 0 }, PACKED(2) },
270     { { VLC_CODEC_RGB8, VLC_CODEC_GREY, VLC_CODEC_YUVP, VLC_CODEC_RGBP, 0 }, PACKED(1) },
271     { { VLC_CODEC_RGB16, VLC_CODEC_RGB15, 0 },                               PACKED(2) },
272     { { VLC_CODEC_RGB24, 0 },                                                PACKED(3) },
273     { { VLC_CODEC_RGB32, VLC_CODEC_RGBA, 0 },                                PACKED(4) },
274
275     { { VLC_CODEC_Y211, 0 }, { 1, { {{1,4}, {1,1}} }, 4 } },
276
277     { {0}, { 0, {}, 0 } }
278 };
279
280 #undef PACKED
281 #undef PLANAR
282
283 static const chroma_description_t *vlc_fourcc_GetChromaDescription( vlc_fourcc_t i_fourcc )
284 {
285     for( unsigned i = 0; p_chromas[i].p_fourcc[0]; i++ )
286     {
287         const vlc_fourcc_t *p_fourcc = p_chromas[i].p_fourcc;
288         for( unsigned j = 0; p_fourcc[j]; j++ )
289         {
290             if( p_fourcc[j] == i_fourcc )
291                 return &p_chromas[i].description;
292         }
293     }
294     return NULL;
295 }
296
297 static int LCM( int a, int b )
298 {
299     return a * b / GCD( a, b );
300 }
301
302 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
303                    int i_width, int i_height, int i_sar_num, int i_sar_den )
304 {
305     /* Store default values */
306     p_picture->i_planes = 0;
307     for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
308     {
309         plane_t *p = &p_picture->p[i];
310         p->p_pixels = NULL;
311         p->i_pixel_pitch = 0;
312     }
313
314     p_picture->pf_release = NULL;
315     p_picture->p_release_sys = NULL;
316     p_picture->i_refcount = 0;
317
318     p_picture->i_qtype = QTYPE_NONE;
319     p_picture->i_qstride = 0;
320     p_picture->p_q = NULL;
321
322     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
323                         i_sar_num, i_sar_den );
324
325     const chroma_description_t *p_dsc =
326         vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
327     if( !p_dsc )
328         return VLC_EGENERIC;
329
330     /* We want V (width/height) to respect:
331         (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
332         (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
333        Which is respected if you have
334        V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
335     */
336     int i_modulo_w = 1;
337     int i_modulo_h = 1;
338     int i_ratio_h  = 1;
339     for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
340     {
341         i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.i_den );
342         i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.i_den );
343         if( i_ratio_h < p_dsc->p[i].h.i_den )
344             i_ratio_h = p_dsc->p[i].h.i_den;
345     }
346
347     const int i_width_aligned  = ( i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
348     const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
349     const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
350     for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
351     {
352         plane_t *p = &p_picture->p[i];
353
354         p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
355         p->i_visible_lines = i_height * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
356         p->i_pitch         = i_width_aligned * p_dsc->p[i].w.i_num / p_dsc->p[i].w.i_den * p_dsc->i_pixel_size;
357         p->i_visible_pitch = i_width * p_dsc->p[i].w.i_num / p_dsc->p[i].w.i_den * p_dsc->i_pixel_size;
358         p->i_pixel_pitch   = p_dsc->i_pixel_size;
359
360         assert( (p->i_pitch % 16) == 0 );
361     }
362     p_picture->i_planes  = p_dsc->i_plane_count;
363
364     return VLC_SUCCESS;
365 }
366
367 /*****************************************************************************
368  *
369  *****************************************************************************/
370 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
371 {
372     video_format_t fmt = *p_fmt;
373
374     /* It is needed to be sure all informations are filled */
375     video_format_Setup( &fmt, p_fmt->i_chroma,
376                               p_fmt->i_width, p_fmt->i_height,
377                               p_fmt->i_sar_num, p_fmt->i_sar_den );
378
379     /* */
380     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
381     if( !p_picture )
382         return NULL;
383
384     if( p_resource )
385     {
386         if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
387                            fmt.i_sar_num, fmt.i_sar_den ) )
388         {
389             free( p_picture );
390             return NULL;
391         }
392         p_picture->p_sys = p_resource->p_sys;
393
394         for( int i = 0; i < p_picture->i_planes; i++ )
395         {
396             p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
397             p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
398             p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
399         }
400     }
401     else
402     {
403         if( vout_AllocatePicture( p_picture,
404                                   fmt.i_chroma, fmt.i_width, fmt.i_height,
405                                   fmt.i_sar_num, fmt.i_sar_den ) )
406         {
407             free( p_picture );
408             return NULL;
409         }
410     }
411     /* */
412     p_picture->format = fmt;
413     p_picture->i_refcount = 1;
414     p_picture->pf_release = PictureReleaseCallback;
415     p_picture->i_status = RESERVED_PICTURE;
416
417     return p_picture;
418 }
419 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
420 {
421     return picture_NewFromResource( p_fmt, NULL );
422 }
423 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
424 {
425     video_format_t fmt;
426
427     memset( &fmt, 0, sizeof(fmt) );
428     video_format_Setup( &fmt, i_chroma, i_width, i_height,
429                         i_sar_num, i_sar_den );
430
431     return picture_NewFromFormat( &fmt );
432 }
433
434 /*****************************************************************************
435  *
436  *****************************************************************************/
437 void picture_Delete( picture_t *p_picture )
438 {
439     assert( p_picture && p_picture->i_refcount == 0 );
440     assert( p_picture->p_release_sys == NULL );
441
442     free( p_picture->p_q );
443     free( p_picture->p_data_orig );
444     free( p_picture->p_sys );
445     free( p_picture );
446 }
447
448 /*****************************************************************************
449  *
450  *****************************************************************************/
451 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
452 {
453     int i;
454
455     for( i = 0; i < p_src->i_planes ; i++ )
456         plane_CopyPixels( p_dst->p+i, p_src->p+i );
457 }
458
459 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
460 {
461     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
462                                      p_src->i_visible_pitch );
463     const unsigned i_height = __MIN( p_dst->i_visible_lines,
464                                      p_src->i_visible_lines );
465
466     if( p_src->i_pitch == p_dst->i_pitch )
467     {
468         /* There are margins, but with the same width : perfect ! */
469         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
470                     p_src->i_pitch * i_height );
471     }
472     else
473     {
474         /* We need to proceed line by line */
475         uint8_t *p_in = p_src->p_pixels;
476         uint8_t *p_out = p_dst->p_pixels;
477         int i_line;
478
479         assert( p_in );
480         assert( p_out );
481
482         for( i_line = i_height; i_line--; )
483         {
484             vlc_memcpy( p_out, p_in, i_width );
485             p_in += p_src->i_pitch;
486             p_out += p_dst->i_pitch;
487         }
488     }
489 }
490
491 /*****************************************************************************
492  *
493  *****************************************************************************/
494 int picture_Export( vlc_object_t *p_obj,
495                     block_t **pp_image,
496                     video_format_t *p_fmt,
497                     picture_t *p_picture,
498                     vlc_fourcc_t i_format,
499                     int i_override_width, int i_override_height )
500 {
501     /* */
502     video_format_t fmt_in = p_picture->format;
503     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
504     {
505         fmt_in.i_sar_num =
506         fmt_in.i_sar_den = 1;
507     }
508
509     /* */
510     video_format_t fmt_out;
511     memset( &fmt_out, 0, sizeof(fmt_out) );
512     fmt_out.i_sar_num =
513     fmt_out.i_sar_den = 1;
514     fmt_out.i_chroma  = i_format;
515
516     /* compute original width/height */
517     unsigned int i_original_width;
518     unsigned int i_original_height;
519     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
520     {
521         i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
522         i_original_height = fmt_in.i_height;
523     }
524     else
525     {
526         i_original_width =  fmt_in.i_width;
527         i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
528     }
529
530     /* */
531     fmt_out.i_width  = ( i_override_width < 0 ) ?
532                        i_original_width : i_override_width;
533     fmt_out.i_height = ( i_override_height < 0 ) ?
534                        i_original_height : i_override_height;
535
536     /* scale if only one direction is provided */
537     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
538     {
539         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
540                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
541     }
542     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
543     {
544         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
545                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
546     }
547
548     image_handler_t *p_image = image_HandlerCreate( p_obj );
549
550     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
551
552     image_HandlerDelete( p_image );
553
554     if( !p_block )
555         return VLC_EGENERIC;
556
557     p_block->i_pts =
558     p_block->i_dts = p_picture->date;
559
560     if( p_fmt )
561         *p_fmt = fmt_out;
562     *pp_image = p_block;
563
564     return VLC_SUCCESS;
565 }
566