]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
Added mouse support to sub-filter.
[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 /**
48  * It retreives a picture from the vout or NULL if no pictures are
49  * available yet.
50  *
51  * You MUST call vout_PutPicture or vout_ReleasePicture on it.
52  *
53  * You may use vout_HoldPicture(paired with vout_ReleasePicture) to keep a
54  * read-only reference.
55  */
56 picture_t *vout_GetPicture( vout_thread_t *p_vout )
57 {
58     /* Get lock */
59     vlc_mutex_lock( &p_vout->p->picture_lock );
60     picture_t *p_pic = picture_pool_Get(p_vout->p->decoder_pool);
61     if (p_pic) {
62         picture_Reset(p_pic);
63         p_pic->p_next = NULL;
64     }
65     vlc_mutex_unlock( &p_vout->p->picture_lock );
66
67     return p_pic;
68 }
69
70 /**
71  * It gives to the vout a picture to be displayed.
72  *
73  * The given picture MUST comes from vout_GetPicture.
74  *
75  * Becareful, after vout_PutPicture is called, picture_t::p_next cannot be
76  * read/used.
77  */
78 void vout_PutPicture( vout_thread_t *p_vout, picture_t *p_pic )
79 {
80     vlc_mutex_lock( &p_vout->p->picture_lock );
81
82     p_pic->p_next = NULL;
83     picture_fifo_Push(p_vout->p->decoder_fifo, p_pic);
84
85     vlc_mutex_unlock( &p_vout->p->picture_lock );
86
87     vout_control_Wake( &p_vout->p->control);
88 }
89
90 /**
91  * It releases a picture retreived by vout_GetPicture.
92  */
93 void vout_ReleasePicture( vout_thread_t *p_vout, picture_t *p_pic  )
94 {
95     vlc_mutex_lock( &p_vout->p->picture_lock );
96
97     picture_Release( p_pic );
98
99     vlc_mutex_unlock( &p_vout->p->picture_lock );
100
101     vout_control_Wake( &p_vout->p->control);
102 }
103
104 /**
105  * It increment the reference counter of a picture retreived by
106  * vout_GetPicture.
107  */
108 void vout_HoldPicture( vout_thread_t *p_vout, picture_t *p_pic )
109 {
110     vlc_mutex_lock( &p_vout->p->picture_lock );
111
112     picture_Hold( p_pic );
113
114     vlc_mutex_unlock( &p_vout->p->picture_lock );
115 }
116
117 /**
118  * Allocate a new picture in the heap.
119  *
120  * This function allocates a fake direct buffer in memory, which can be
121  * used exactly like a video buffer. The video output thread then manages
122  * how it gets displayed.
123  */
124 static int vout_AllocatePicture( picture_t *p_pic,
125                                  vlc_fourcc_t i_chroma,
126                                  int i_width, int i_height,
127                                  int i_sar_num, int i_sar_den )
128 {
129     /* Make sure the real dimensions are a multiple of 16 */
130     if( picture_Setup( p_pic, i_chroma, i_width, i_height,
131                        i_sar_num, i_sar_den ) != VLC_SUCCESS )
132         return VLC_EGENERIC;
133
134     /* Calculate how big the new image should be */
135     size_t i_bytes = 0;
136     for( int i = 0; i < p_pic->i_planes; i++ )
137     {
138         const plane_t *p = &p_pic->p[i];
139
140         if( p->i_pitch <= 0 || p->i_lines <= 0 ||
141             p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
142         {
143             p_pic->i_planes = 0;
144             return VLC_ENOMEM;
145         }
146         i_bytes += p->i_pitch * p->i_lines;
147     }
148
149     uint8_t *p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
150     if( !p_data )
151     {
152         p_pic->i_planes = 0;
153         return VLC_EGENERIC;
154     }
155
156     /* Fill the p_pixels field for each plane */
157     p_pic->p[0].p_pixels = p_data;
158     for( int i = 1; i < p_pic->i_planes; i++ )
159     {
160         p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
161                                                         p_pic->p[i-1].i_pitch ];
162     }
163
164     return VLC_SUCCESS;
165 }
166
167 /*****************************************************************************
168  *
169  *****************************************************************************/
170 static void PictureReleaseCallback( picture_t *p_picture )
171 {
172     if( --p_picture->i_refcount > 0 )
173         return;
174     picture_Delete( p_picture );
175 }
176
177 /*****************************************************************************
178  *
179  *****************************************************************************/
180 void picture_Reset( picture_t *p_picture )
181 {
182     /* */
183     p_picture->date = VLC_TS_INVALID;
184     p_picture->b_force = false;
185     p_picture->b_progressive = false;
186     p_picture->i_nb_fields = 0;
187     p_picture->b_top_field_first = false;
188     picture_CleanupQuant( p_picture );
189 }
190
191 /*****************************************************************************
192  *
193  *****************************************************************************/
194 typedef struct
195 {
196     unsigned     i_plane_count;
197     struct
198     {
199         struct
200         {
201             unsigned i_num;
202             unsigned i_den;
203         } w;
204         struct
205         {
206             unsigned i_num;
207             unsigned i_den;
208         } h;
209     } p[VOUT_MAX_PLANES];
210     unsigned i_pixel_size;
211
212 } chroma_description_t;
213
214 #define PLANAR(n, w_den, h_den) \
215     { n, { {{1,1}, {1,1}}, {{1,w_den}, {1,h_den}}, {{1,w_den}, {1,h_den}}, {{1,1}, {1,1}} }, 1 }
216 #define PACKED(size) \
217     { 1, { {{1,1}, {1,1}} }, size }
218
219 static const struct
220 {
221     vlc_fourcc_t            p_fourcc[5];
222     chroma_description_t    description;
223 } p_chromas[] = {
224     { { VLC_CODEC_I411, 0 },                                 PLANAR(3, 4, 1) },
225     { { VLC_CODEC_I410, VLC_CODEC_YV9, 0 },                  PLANAR(3, 4, 4) },
226     { { VLC_CODEC_YV12, VLC_CODEC_I420, VLC_CODEC_J420, 0 }, PLANAR(3, 2, 2) },
227     { { VLC_CODEC_I422, VLC_CODEC_J422, 0 },                 PLANAR(3, 2, 1) },
228     { { VLC_CODEC_I440, VLC_CODEC_J440, 0 },                 PLANAR(3, 1, 2) },
229     { { VLC_CODEC_I444, VLC_CODEC_J444, 0 },                 PLANAR(3, 1, 1) },
230     { { VLC_CODEC_YUVA, 0 },                                 PLANAR(4, 1, 1) },
231
232     { { VLC_CODEC_UYVY, VLC_CODEC_VYUY, VLC_CODEC_YUYV, VLC_CODEC_YVYU, 0 }, PACKED(2) },
233     { { VLC_CODEC_RGB8, VLC_CODEC_GREY, VLC_CODEC_YUVP, VLC_CODEC_RGBP, 0 }, PACKED(1) },
234     { { VLC_CODEC_RGB16, VLC_CODEC_RGB15, 0 },                               PACKED(2) },
235     { { VLC_CODEC_RGB24, 0 },                                                PACKED(3) },
236     { { VLC_CODEC_RGB32, VLC_CODEC_RGBA, 0 },                                PACKED(4) },
237
238     { { VLC_CODEC_Y211, 0 }, { 1, { {{1,4}, {1,1}} }, 4 } },
239
240     { {0}, { 0, {}, 0 } }
241 };
242
243 #undef PACKED
244 #undef PLANAR
245
246 static const chroma_description_t *vlc_fourcc_GetChromaDescription( vlc_fourcc_t i_fourcc )
247 {
248     for( unsigned i = 0; p_chromas[i].p_fourcc[0]; i++ )
249     {
250         const vlc_fourcc_t *p_fourcc = p_chromas[i].p_fourcc;
251         for( unsigned j = 0; p_fourcc[j]; j++ )
252         {
253             if( p_fourcc[j] == i_fourcc )
254                 return &p_chromas[i].description;
255         }
256     }
257     return NULL;
258 }
259
260 static int LCM( int a, int b )
261 {
262     return a * b / GCD( a, b );
263 }
264
265 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
266                    int i_width, int i_height, int i_sar_num, int i_sar_den )
267 {
268     /* Store default values */
269     p_picture->i_planes = 0;
270     for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
271     {
272         plane_t *p = &p_picture->p[i];
273         p->p_pixels = NULL;
274         p->i_pixel_pitch = 0;
275     }
276
277     p_picture->pf_release = NULL;
278     p_picture->p_release_sys = NULL;
279     p_picture->i_refcount = 0;
280
281     p_picture->i_qtype = QTYPE_NONE;
282     p_picture->i_qstride = 0;
283     p_picture->p_q = NULL;
284
285     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
286                         i_sar_num, i_sar_den );
287
288     const chroma_description_t *p_dsc =
289         vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
290     if( !p_dsc )
291         return VLC_EGENERIC;
292
293     /* We want V (width/height) to respect:
294         (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
295         (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
296        Which is respected if you have
297        V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
298     */
299     int i_modulo_w = 1;
300     int i_modulo_h = 1;
301     int i_ratio_h  = 1;
302     for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
303     {
304         i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.i_den );
305         i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.i_den );
306         if( i_ratio_h < p_dsc->p[i].h.i_den )
307             i_ratio_h = p_dsc->p[i].h.i_den;
308     }
309
310     const int i_width_aligned  = ( i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
311     const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
312     const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
313     for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
314     {
315         plane_t *p = &p_picture->p[i];
316
317         p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
318         p->i_visible_lines = i_height * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
319         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;
320         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;
321         p->i_pixel_pitch   = p_dsc->i_pixel_size;
322
323         assert( (p->i_pitch % 16) == 0 );
324     }
325     p_picture->i_planes  = p_dsc->i_plane_count;
326
327     return VLC_SUCCESS;
328 }
329
330 /*****************************************************************************
331  *
332  *****************************************************************************/
333 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
334 {
335     video_format_t fmt = *p_fmt;
336
337     /* It is needed to be sure all informations are filled */
338     video_format_Setup( &fmt, p_fmt->i_chroma,
339                               p_fmt->i_width, p_fmt->i_height,
340                               p_fmt->i_sar_num, p_fmt->i_sar_den );
341
342     /* */
343     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
344     if( !p_picture )
345         return NULL;
346
347     if( p_resource )
348     {
349         if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
350                            fmt.i_sar_num, fmt.i_sar_den ) )
351         {
352             free( p_picture );
353             return NULL;
354         }
355         p_picture->p_sys = p_resource->p_sys;
356
357         for( int i = 0; i < p_picture->i_planes; i++ )
358         {
359             p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
360             p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
361             p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
362         }
363     }
364     else
365     {
366         if( vout_AllocatePicture( p_picture,
367                                   fmt.i_chroma, fmt.i_width, fmt.i_height,
368                                   fmt.i_sar_num, fmt.i_sar_den ) )
369         {
370             free( p_picture );
371             return NULL;
372         }
373     }
374     /* */
375     p_picture->format = fmt;
376     p_picture->i_refcount = 1;
377     p_picture->pf_release = PictureReleaseCallback;
378
379     return p_picture;
380 }
381 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
382 {
383     return picture_NewFromResource( p_fmt, NULL );
384 }
385 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
386 {
387     video_format_t fmt;
388
389     memset( &fmt, 0, sizeof(fmt) );
390     video_format_Setup( &fmt, i_chroma, i_width, i_height,
391                         i_sar_num, i_sar_den );
392
393     return picture_NewFromFormat( &fmt );
394 }
395
396 /*****************************************************************************
397  *
398  *****************************************************************************/
399 void picture_Delete( picture_t *p_picture )
400 {
401     assert( p_picture && p_picture->i_refcount == 0 );
402     assert( p_picture->p_release_sys == NULL );
403
404     free( p_picture->p_q );
405     free( p_picture->p_data_orig );
406     free( p_picture->p_sys );
407     free( p_picture );
408 }
409
410 /*****************************************************************************
411  *
412  *****************************************************************************/
413 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
414 {
415     int i;
416
417     for( i = 0; i < p_src->i_planes ; i++ )
418         plane_CopyPixels( p_dst->p+i, p_src->p+i );
419 }
420
421 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
422 {
423     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
424                                      p_src->i_visible_pitch );
425     const unsigned i_height = __MIN( p_dst->i_visible_lines,
426                                      p_src->i_visible_lines );
427
428     if( p_src->i_pitch == p_dst->i_pitch )
429     {
430         /* There are margins, but with the same width : perfect ! */
431         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
432                     p_src->i_pitch * i_height );
433     }
434     else
435     {
436         /* We need to proceed line by line */
437         uint8_t *p_in = p_src->p_pixels;
438         uint8_t *p_out = p_dst->p_pixels;
439         int i_line;
440
441         assert( p_in );
442         assert( p_out );
443
444         for( i_line = i_height; i_line--; )
445         {
446             vlc_memcpy( p_out, p_in, i_width );
447             p_in += p_src->i_pitch;
448             p_out += p_dst->i_pitch;
449         }
450     }
451 }
452
453 /*****************************************************************************
454  *
455  *****************************************************************************/
456 int picture_Export( vlc_object_t *p_obj,
457                     block_t **pp_image,
458                     video_format_t *p_fmt,
459                     picture_t *p_picture,
460                     vlc_fourcc_t i_format,
461                     int i_override_width, int i_override_height )
462 {
463     /* */
464     video_format_t fmt_in = p_picture->format;
465     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
466     {
467         fmt_in.i_sar_num =
468         fmt_in.i_sar_den = 1;
469     }
470
471     /* */
472     video_format_t fmt_out;
473     memset( &fmt_out, 0, sizeof(fmt_out) );
474     fmt_out.i_sar_num =
475     fmt_out.i_sar_den = 1;
476     fmt_out.i_chroma  = i_format;
477
478     /* compute original width/height */
479     unsigned int i_original_width;
480     unsigned int i_original_height;
481     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
482     {
483         i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
484         i_original_height = fmt_in.i_height;
485     }
486     else
487     {
488         i_original_width =  fmt_in.i_width;
489         i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
490     }
491
492     /* */
493     fmt_out.i_width  = ( i_override_width < 0 ) ?
494                        i_original_width : i_override_width;
495     fmt_out.i_height = ( i_override_height < 0 ) ?
496                        i_original_height : i_override_height;
497
498     /* scale if only one direction is provided */
499     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
500     {
501         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
502                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
503     }
504     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
505     {
506         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
507                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
508     }
509
510     image_handler_t *p_image = image_HandlerCreate( p_obj );
511
512     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
513
514     image_HandlerDelete( p_image );
515
516     if( !p_block )
517         return VLC_EGENERIC;
518
519     p_block->i_pts =
520     p_block->i_dts = p_picture->date;
521
522     if( p_fmt )
523         *p_fmt = fmt_out;
524     *pp_image = p_block;
525
526     return VLC_SUCCESS;
527 }
528