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