1 /*****************************************************************************
2 * picture.c : picture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2010 VLC authors and VideoLAN
5 * Copyright (C) 2009-2010 Laurent Aimar
8 * Authors: Vincent Seguin <seguin@via.ecp.fr>
9 * Samuel Hocevar <sam@zoy.org>
10 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_picture.h>
38 #include <vlc_image.h>
39 #include <vlc_block.h>
40 #include <vlc_atomic.h>
43 * Allocate a new picture in the heap.
45 * This function allocates a fake direct buffer in memory, which can be
46 * used exactly like a video buffer. The video output thread then manages
47 * how it gets displayed.
49 static int AllocatePicture( picture_t *p_pic )
51 /* Calculate how big the new image should be */
53 for( int i = 0; i < p_pic->i_planes; i++ )
55 const plane_t *p = &p_pic->p[i];
57 if( p->i_pitch < 0 || p->i_lines <= 0 ||
58 (size_t)p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
63 i_bytes += p->i_pitch * p->i_lines;
66 uint8_t *p_data = vlc_memalign( 16, i_bytes );
67 if( i_bytes > 0 && p_data == NULL )
72 p_pic->gc.p_sys = (void *)p_data;
74 /* Fill the p_pixels field for each plane */
75 p_pic->p[0].p_pixels = p_data;
76 for( int i = 1; i < p_pic->i_planes; i++ )
78 p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
79 p_pic->p[i-1].i_pitch ];
85 /*****************************************************************************
87 *****************************************************************************/
89 static void PictureDestroyContext( picture_t *p_picture )
91 void (**context)( void * ) = p_picture->context;
94 void (*context_destroy)( void * ) = *context;
95 context_destroy( context );
96 p_picture->context = NULL;
100 static void PictureDestroy( picture_t *p_picture )
103 vlc_atomic_get( &p_picture->gc.refcount ) == 0 );
105 vlc_free( p_picture->gc.p_sys );
106 free( p_picture->p_sys );
110 /*****************************************************************************
112 *****************************************************************************/
113 void picture_Reset( picture_t *p_picture )
116 p_picture->date = VLC_TS_INVALID;
117 p_picture->b_force = false;
118 p_picture->b_progressive = false;
119 p_picture->i_nb_fields = 2;
120 p_picture->b_top_field_first = false;
121 PictureDestroyContext( p_picture );
124 /*****************************************************************************
126 *****************************************************************************/
127 static int LCM( int a, int b )
129 return a * b / GCD( a, b );
132 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
133 int i_width, int i_height, int i_sar_num, int i_sar_den )
135 /* Store default values */
136 p_picture->i_planes = 0;
137 for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
139 plane_t *p = &p_picture->p[i];
141 p->i_pixel_pitch = 0;
144 vlc_atomic_set( &p_picture->gc.refcount, 0 );
145 p_picture->gc.pf_destroy = NULL;
146 p_picture->gc.p_sys = NULL;
148 p_picture->i_nb_fields = 2;
150 video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
151 i_sar_num, i_sar_den );
153 const vlc_chroma_description_t *p_dsc =
154 vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
158 /* We want V (width/height) to respect:
159 (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
160 (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
161 Which is respected if you have
162 V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
166 unsigned int i_ratio_h = 1;
167 for( unsigned i = 0; i < p_dsc->plane_count; i++ )
169 i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.den );
170 i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.den );
171 if( i_ratio_h < p_dsc->p[i].h.den )
172 i_ratio_h = p_dsc->p[i].h.den;
174 i_modulo_h = LCM( i_modulo_h, 32 );
176 const int i_width_aligned = ( i_width + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
177 const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
178 const int i_height_extra = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
179 for( unsigned i = 0; i < p_dsc->plane_count; i++ )
181 plane_t *p = &p_picture->p[i];
183 p->i_lines = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
184 p->i_visible_lines = i_height * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
185 p->i_pitch = i_width_aligned * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
186 p->i_visible_pitch = i_width * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
187 p->i_pixel_pitch = p_dsc->pixel_size;
189 assert( (p->i_pitch % 16) == 0 );
191 p_picture->i_planes = p_dsc->plane_count;
196 /*****************************************************************************
198 *****************************************************************************/
199 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
201 video_format_t fmt = *p_fmt;
203 /* It is needed to be sure all information are filled */
204 video_format_Setup( &fmt, p_fmt->i_chroma,
205 p_fmt->i_width, p_fmt->i_height,
206 p_fmt->i_sar_num, p_fmt->i_sar_den );
207 if( p_fmt->i_x_offset < p_fmt->i_width &&
208 p_fmt->i_y_offset < p_fmt->i_height &&
209 p_fmt->i_visible_width > 0 && p_fmt->i_x_offset + p_fmt->i_visible_width <= p_fmt->i_width &&
210 p_fmt->i_visible_height > 0 && p_fmt->i_y_offset + p_fmt->i_visible_height <= p_fmt->i_height )
211 video_format_CopyCrop( &fmt, p_fmt );
214 picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
218 /* Make sure the real dimensions are a multiple of 16 */
219 if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
220 fmt.i_sar_num, fmt.i_sar_den ) )
228 p_picture->p_sys = p_resource->p_sys;
229 assert( p_picture->gc.p_sys == NULL );
231 for( int i = 0; i < p_picture->i_planes; i++ )
233 p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
234 p_picture->p[i].i_lines = p_resource->p[i].i_lines;
235 p_picture->p[i].i_pitch = p_resource->p[i].i_pitch;
240 if( AllocatePicture( p_picture ) )
245 assert( p_picture->gc.p_sys != NULL );
248 p_picture->format = fmt;
250 vlc_atomic_set( &p_picture->gc.refcount, 1 );
251 p_picture->gc.pf_destroy = PictureDestroy;
255 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
257 return picture_NewFromResource( p_fmt, NULL );
259 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
263 memset( &fmt, 0, sizeof(fmt) );
264 video_format_Setup( &fmt, i_chroma, i_width, i_height,
265 i_sar_num, i_sar_den );
267 return picture_NewFromFormat( &fmt );
270 /*****************************************************************************
272 *****************************************************************************/
274 picture_t *picture_Hold( picture_t *p_picture )
276 vlc_atomic_inc( &p_picture->gc.refcount );
280 void picture_Release( picture_t *p_picture )
282 uintptr_t refs = vlc_atomic_dec( &p_picture->gc.refcount );
283 assert( refs != (uintptr_t)-1 );
287 PictureDestroyContext( p_picture );
288 if( p_picture->gc.pf_destroy != NULL )
289 p_picture->gc.pf_destroy( p_picture );
292 bool picture_IsReferenced( picture_t *p_picture )
294 return vlc_atomic_get( &p_picture->gc.refcount ) > 1;
297 /*****************************************************************************
299 *****************************************************************************/
300 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
302 const unsigned i_width = __MIN( p_dst->i_visible_pitch,
303 p_src->i_visible_pitch );
304 const unsigned i_height = __MIN( p_dst->i_visible_lines,
305 p_src->i_visible_lines );
307 /* The 2x visible pitch check does two things:
308 1) Makes field plane_t's work correctly (see the deinterlacer module)
309 2) Moves less data if the pitch and visible pitch differ much.
311 if( p_src->i_pitch == p_dst->i_pitch &&
312 p_src->i_pitch < 2*p_src->i_visible_pitch )
314 /* There are margins, but with the same width : perfect ! */
315 memcpy( p_dst->p_pixels, p_src->p_pixels,
316 p_src->i_pitch * i_height );
320 /* We need to proceed line by line */
321 uint8_t *p_in = p_src->p_pixels;
322 uint8_t *p_out = p_dst->p_pixels;
328 for( i_line = i_height; i_line--; )
330 memcpy( p_out, p_in, i_width );
331 p_in += p_src->i_pitch;
332 p_out += p_dst->i_pitch;
337 void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src )
339 p_dst->date = p_src->date;
340 p_dst->b_force = p_src->b_force;
342 p_dst->b_progressive = p_src->b_progressive;
343 p_dst->i_nb_fields = p_src->i_nb_fields;
344 p_dst->b_top_field_first = p_src->b_top_field_first;
347 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
351 for( i = 0; i < p_src->i_planes ; i++ )
352 plane_CopyPixels( p_dst->p+i, p_src->p+i );
355 void picture_Copy( picture_t *p_dst, const picture_t *p_src )
357 picture_CopyPixels( p_dst, p_src );
358 picture_CopyProperties( p_dst, p_src );
362 /*****************************************************************************
364 *****************************************************************************/
365 int picture_Export( vlc_object_t *p_obj,
367 video_format_t *p_fmt,
368 picture_t *p_picture,
369 vlc_fourcc_t i_format,
370 int i_override_width, int i_override_height )
373 video_format_t fmt_in = p_picture->format;
374 if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
377 fmt_in.i_sar_den = 1;
381 video_format_t fmt_out;
382 memset( &fmt_out, 0, sizeof(fmt_out) );
384 fmt_out.i_sar_den = 1;
385 fmt_out.i_chroma = i_format;
387 /* compute original width/height */
388 unsigned int i_original_width;
389 unsigned int i_original_height;
390 if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
392 i_original_width = (int64_t)fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
393 i_original_height = fmt_in.i_height;
397 i_original_width = fmt_in.i_width;
398 i_original_height = (int64_t)fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
402 fmt_out.i_width = ( i_override_width < 0 ) ?
403 i_original_width : (unsigned)i_override_width;
404 fmt_out.i_height = ( i_override_height < 0 ) ?
405 i_original_height : (unsigned)i_override_height;
407 /* scale if only one direction is provided */
408 if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
410 fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
411 * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
413 else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
415 fmt_out.i_width = fmt_in.i_width * fmt_out.i_height
416 * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
419 image_handler_t *p_image = image_HandlerCreate( p_obj );
421 block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
423 image_HandlerDelete( p_image );
429 p_block->i_dts = p_picture->date;
438 void picture_BlendSubpicture(picture_t *dst,
439 filter_t *blend, subpicture_t *src)
441 assert(src && !src->b_fade && src->b_absolute);
443 for (subpicture_region_t *r = src->p_region; r != NULL; r = r->p_next) {
444 assert(r->p_picture && r->i_align == 0);
445 if (filter_ConfigureBlend(blend, dst->format.i_width, dst->format.i_height,
447 filter_Blend(blend, dst,
448 r->i_x, r->i_y, r->p_picture,
449 src->i_alpha * r->i_alpha / 255)) {
450 msg_Err(blend, "blending %4.4s to %4.4s failed",
451 (char *)&blend->fmt_in.video.i_chroma,
452 (char *)&blend->fmt_out.video.i_chroma );