]> git.sesse.net Git - vlc/blob - src/misc/picture.c
decoder: inline DecoderSignalWait()
[vlc] / src / misc / picture.c
1 /*****************************************************************************
2  * picture.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2010 VLC authors and VideoLAN
5  * Copyright (C) 2009-2010 Laurent Aimar
6  * $Id$
7  *
8  * Authors: Vincent Seguin <seguin@via.ecp.fr>
9  *          Samuel Hocevar <sam@zoy.org>
10  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34 #include <assert.h>
35
36 #include <vlc_common.h>
37 #include <vlc_picture.h>
38 #include <vlc_image.h>
39 #include <vlc_block.h>
40
41 /**
42  * Allocate a new picture in the heap.
43  *
44  * This function allocates a fake direct buffer in memory, which can be
45  * used exactly like a video buffer. The video output thread then manages
46  * how it gets displayed.
47  */
48 static int AllocatePicture( picture_t *p_pic )
49 {
50     /* Calculate how big the new image should be */
51     size_t i_bytes = 0;
52     for( int i = 0; i < p_pic->i_planes; i++ )
53     {
54         const plane_t *p = &p_pic->p[i];
55
56         if( p->i_pitch < 0 || p->i_lines <= 0 ||
57             (size_t)p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
58         {
59             p_pic->i_planes = 0;
60             return VLC_ENOMEM;
61         }
62         i_bytes += p->i_pitch * p->i_lines;
63     }
64
65     uint8_t *p_data = vlc_memalign( 16, i_bytes );
66     if( i_bytes > 0 && p_data == NULL )
67     {
68         p_pic->i_planes = 0;
69         return VLC_EGENERIC;
70     }
71
72     /* Fill the p_pixels field for each plane */
73     p_pic->p[0].p_pixels = p_data;
74     for( int i = 1; i < p_pic->i_planes; i++ )
75     {
76         p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
77                                                         p_pic->p[i-1].i_pitch ];
78     }
79
80     return VLC_SUCCESS;
81 }
82
83 /*****************************************************************************
84  *
85  *****************************************************************************/
86
87 static void PictureDestroyContext( picture_t *p_picture )
88 {
89     void (**context)( void * ) = p_picture->context;
90     if( context != NULL )
91     {
92         void (*context_destroy)( void * ) = *context;
93         context_destroy( context );
94         p_picture->context = NULL;
95     }
96 }
97
98 /**
99  * Destroys a picture allocated by picture_NewFromResource() but without
100  * a custom destruction callback.
101  */
102 static void picture_DestroyFromResource( picture_t *p_picture )
103 {
104     free( p_picture->p_sys );
105     free( p_picture );
106 }
107
108 /**
109  * Destroys a picture allocated with picture_NewFromFormat()
110  * (and thus AllocatePicture()).
111  */
112 static void picture_Destroy( picture_t *p_picture )
113 {
114     vlc_free( p_picture->p[0].p_pixels );
115     free( p_picture );
116 }
117
118 /*****************************************************************************
119  *
120  *****************************************************************************/
121 void picture_Reset( picture_t *p_picture )
122 {
123     /* */
124     p_picture->date = VLC_TS_INVALID;
125     p_picture->b_force = false;
126     p_picture->b_progressive = false;
127     p_picture->i_nb_fields = 2;
128     p_picture->b_top_field_first = false;
129     PictureDestroyContext( p_picture );
130 }
131
132 /*****************************************************************************
133  *
134  *****************************************************************************/
135 static int LCM( int a, int b )
136 {
137     return a * b / GCD( a, b );
138 }
139
140 int picture_Setup( picture_t *p_picture, const video_format_t *restrict fmt )
141 {
142     /* Store default values */
143     p_picture->i_planes = 0;
144     for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
145     {
146         plane_t *p = &p_picture->p[i];
147         p->p_pixels = NULL;
148         p->i_pixel_pitch = 0;
149     }
150
151     atomic_init( &p_picture->gc.refcount, 0 );
152     p_picture->gc.p_sys = NULL;
153
154     p_picture->i_nb_fields = 2;
155
156     video_format_Setup( &p_picture->format, fmt->i_chroma, fmt->i_width, fmt->i_height,
157                         fmt->i_visible_width, fmt->i_visible_height,
158                         fmt->i_sar_num, fmt->i_sar_den );
159
160     const vlc_chroma_description_t *p_dsc =
161         vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
162     if( !p_dsc )
163         return VLC_EGENERIC;
164
165     /* We want V (width/height) to respect:
166         (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
167         (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
168        Which is respected if you have
169        V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
170     */
171     int i_modulo_w = 1;
172     int i_modulo_h = 1;
173     unsigned int i_ratio_h  = 1;
174     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
175     {
176         i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.den );
177         i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.den );
178         if( i_ratio_h < p_dsc->p[i].h.den )
179             i_ratio_h = p_dsc->p[i].h.den;
180     }
181     i_modulo_h = LCM( i_modulo_h, 32 );
182
183     const int i_width_aligned  = ( fmt->i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
184     const int i_height_aligned = ( fmt->i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
185     const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
186     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
187     {
188         plane_t *p = &p_picture->p[i];
189
190         p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
191         p->i_visible_lines = fmt->i_visible_height * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
192         p->i_pitch         = i_width_aligned * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
193         p->i_visible_pitch = fmt->i_visible_width * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
194         p->i_pixel_pitch   = p_dsc->pixel_size;
195
196         assert( (p->i_pitch % 16) == 0 );
197     }
198     p_picture->i_planes  = p_dsc->plane_count;
199
200     return VLC_SUCCESS;
201 }
202
203 /*****************************************************************************
204  *
205  *****************************************************************************/
206 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
207 {
208     video_format_t fmt = *p_fmt;
209
210     /* It is needed to be sure all information are filled */
211     video_format_Setup( &fmt, p_fmt->i_chroma,
212                               p_fmt->i_width, p_fmt->i_height,
213                               p_fmt->i_visible_width, p_fmt->i_visible_height,
214                               p_fmt->i_sar_num, p_fmt->i_sar_den );
215     if( p_fmt->i_x_offset < p_fmt->i_width &&
216         p_fmt->i_y_offset < p_fmt->i_height &&
217         p_fmt->i_visible_width  > 0 && p_fmt->i_x_offset + p_fmt->i_visible_width  <= p_fmt->i_width &&
218         p_fmt->i_visible_height > 0 && p_fmt->i_y_offset + p_fmt->i_visible_height <= p_fmt->i_height )
219         video_format_CopyCrop( &fmt, p_fmt );
220
221     /* */
222     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
223     if( !p_picture )
224         return NULL;
225
226     /* Make sure the real dimensions are a multiple of 16 */
227     if( picture_Setup( p_picture, &fmt ) )
228     {
229         free( p_picture );
230         return NULL;
231     }
232
233     if( p_resource )
234     {
235         p_picture->p_sys = p_resource->p_sys;
236         p_picture->gc.pf_destroy = p_resource->pf_destroy;
237         if( p_picture->gc.pf_destroy == NULL )
238             p_picture->gc.pf_destroy = picture_DestroyFromResource;
239
240         for( int i = 0; i < p_picture->i_planes; i++ )
241         {
242             p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
243             p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
244             p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
245         }
246     }
247     else
248     {
249         if( AllocatePicture( p_picture ) )
250         {
251             free( p_picture );
252             return NULL;
253         }
254         p_picture->gc.pf_destroy = picture_Destroy;
255     }
256
257     /* */
258     p_picture->format = fmt;
259
260     atomic_init( &p_picture->gc.refcount, 1 );
261
262     return p_picture;
263 }
264
265 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
266 {
267     return picture_NewFromResource( p_fmt, NULL );
268 }
269
270 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
271 {
272     video_format_t fmt;
273
274     memset( &fmt, 0, sizeof(fmt) );
275     video_format_Setup( &fmt, i_chroma, i_width, i_height,
276                         i_width, i_height, i_sar_num, i_sar_den );
277
278     return picture_NewFromFormat( &fmt );
279 }
280
281 /*****************************************************************************
282  *
283  *****************************************************************************/
284
285 picture_t *picture_Hold( picture_t *p_picture )
286 {
287     uintptr_t refs = atomic_fetch_add( &p_picture->gc.refcount, 1 );
288     assert( refs > 0 );
289     return p_picture;
290 }
291
292 void picture_Release( picture_t *p_picture )
293 {
294     assert( p_picture != NULL );
295
296     uintptr_t refs = atomic_fetch_sub( &p_picture->gc.refcount, 1 );
297     assert( refs != 0 );
298     if( refs > 1 )
299         return;
300
301     PictureDestroyContext( p_picture );
302     assert( p_picture->gc.pf_destroy != NULL );
303     p_picture->gc.pf_destroy( p_picture );
304 }
305
306 bool picture_IsReferenced( picture_t *p_picture )
307 {
308     return atomic_load( &p_picture->gc.refcount ) > 1;
309 }
310
311 /*****************************************************************************
312  *
313  *****************************************************************************/
314 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
315 {
316     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
317                                      p_src->i_visible_pitch );
318     const unsigned i_height = __MIN( p_dst->i_visible_lines,
319                                      p_src->i_visible_lines );
320
321     /* The 2x visible pitch check does two things:
322        1) Makes field plane_t's work correctly (see the deinterlacer module)
323        2) Moves less data if the pitch and visible pitch differ much.
324     */
325     if( p_src->i_pitch == p_dst->i_pitch  &&
326         p_src->i_pitch < 2*p_src->i_visible_pitch )
327     {
328         /* There are margins, but with the same width : perfect ! */
329         memcpy( p_dst->p_pixels, p_src->p_pixels,
330                     p_src->i_pitch * i_height );
331     }
332     else
333     {
334         /* We need to proceed line by line */
335         uint8_t *p_in = p_src->p_pixels;
336         uint8_t *p_out = p_dst->p_pixels;
337         int i_line;
338
339         assert( p_in );
340         assert( p_out );
341
342         for( i_line = i_height; i_line--; )
343         {
344             memcpy( p_out, p_in, i_width );
345             p_in += p_src->i_pitch;
346             p_out += p_dst->i_pitch;
347         }
348     }
349 }
350
351 void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src )
352 {
353     p_dst->date = p_src->date;
354     p_dst->b_force = p_src->b_force;
355
356     p_dst->b_progressive = p_src->b_progressive;
357     p_dst->i_nb_fields = p_src->i_nb_fields;
358     p_dst->b_top_field_first = p_src->b_top_field_first;
359 }
360
361 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
362 {
363     int i;
364
365     for( i = 0; i < p_src->i_planes ; i++ )
366         plane_CopyPixels( p_dst->p+i, p_src->p+i );
367 }
368
369 void picture_Copy( picture_t *p_dst, const picture_t *p_src )
370 {
371     picture_CopyPixels( p_dst, p_src );
372     picture_CopyProperties( p_dst, p_src );
373 }
374
375
376 /*****************************************************************************
377  *
378  *****************************************************************************/
379 int picture_Export( vlc_object_t *p_obj,
380                     block_t **pp_image,
381                     video_format_t *p_fmt,
382                     picture_t *p_picture,
383                     vlc_fourcc_t i_format,
384                     int i_override_width, int i_override_height )
385 {
386     /* */
387     video_format_t fmt_in = p_picture->format;
388     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
389     {
390         fmt_in.i_sar_num =
391         fmt_in.i_sar_den = 1;
392     }
393
394     /* */
395     video_format_t fmt_out;
396     memset( &fmt_out, 0, sizeof(fmt_out) );
397     fmt_out.i_sar_num =
398     fmt_out.i_sar_den = 1;
399     fmt_out.i_chroma  = i_format;
400
401     /* compute original width/height */
402     unsigned int i_original_width;
403     unsigned int i_original_height;
404     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
405     {
406         i_original_width = (int64_t)fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
407         i_original_height = fmt_in.i_height;
408     }
409     else
410     {
411         i_original_width =  fmt_in.i_width;
412         i_original_height = (int64_t)fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
413     }
414
415     /* */
416     fmt_out.i_width  = ( i_override_width < 0 ) ?
417                        i_original_width : (unsigned)i_override_width;
418     fmt_out.i_height = ( i_override_height < 0 ) ?
419                        i_original_height : (unsigned)i_override_height;
420
421     /* scale if only one direction is provided */
422     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
423     {
424         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
425                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
426     }
427     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
428     {
429         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
430                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
431     }
432
433     image_handler_t *p_image = image_HandlerCreate( p_obj );
434
435     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
436
437     image_HandlerDelete( p_image );
438
439     if( !p_block )
440         return VLC_EGENERIC;
441
442     p_block->i_pts =
443     p_block->i_dts = p_picture->date;
444
445     if( p_fmt )
446         *p_fmt = fmt_out;
447     *pp_image = p_block;
448
449     return VLC_SUCCESS;
450 }
451
452 unsigned picture_BlendSubpicture(picture_t *dst,
453                                  filter_t *blend, subpicture_t *src)
454 {
455     unsigned done = 0;
456
457     assert(src && !src->b_fade && src->b_absolute);
458
459     for (subpicture_region_t *r = src->p_region; r != NULL; r = r->p_next) {
460         assert(r->p_picture && r->i_align == 0);
461         if (filter_ConfigureBlend(blend, dst->format.i_width,
462                                   dst->format.i_height,  &r->fmt)
463          || filter_Blend(blend, dst, r->i_x, r->i_y, r->p_picture,
464                          src->i_alpha * r->i_alpha / 255))
465             msg_Err(blend, "blending %4.4s to %4.4s failed",
466                     (char *)&blend->fmt_in.video.i_chroma,
467                     (char *)&blend->fmt_out.video.i_chroma );
468         else
469             done++;
470     }
471     return done;
472 }