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 *****************************************************************************/
37 #include <vlc_common.h>
39 #include <vlc_image.h>
40 #include <vlc_block.h>
42 static void PictureDestroyContext( picture_t
*p_picture
)
44 picture_context_t
*ctx
= p_picture
->context
;
48 p_picture
->context
= NULL
;
53 * Destroys a picture allocated by picture_NewFromResource() but without
54 * a custom destruction callback.
56 static void picture_DestroyFromResource( picture_t
*p_picture
)
58 free( p_picture
->p_sys
);
63 * Destroys a picture allocated with picture_NewFromFormat().
65 static void picture_Destroy( picture_t
*p_picture
)
67 aligned_free( p_picture
->p
[0].p_pixels
);
71 /*****************************************************************************
73 *****************************************************************************/
74 void picture_Reset( picture_t
*p_picture
)
77 p_picture
->date
= VLC_TS_INVALID
;
78 p_picture
->b_force
= false;
79 p_picture
->b_progressive
= false;
80 p_picture
->i_nb_fields
= 2;
81 p_picture
->b_top_field_first
= false;
82 PictureDestroyContext( p_picture
);
85 /*****************************************************************************
87 *****************************************************************************/
88 static int LCM( int a
, int b
)
90 return a
* b
/ GCD( a
, b
);
93 int picture_Setup( picture_t
*p_picture
, const video_format_t
*restrict fmt
)
95 /* Store default values */
96 p_picture
->i_planes
= 0;
97 for( unsigned i
= 0; i
< VOUT_MAX_PLANES
; i
++ )
99 plane_t
*p
= &p_picture
->p
[i
];
101 p
->i_pixel_pitch
= 0;
104 p_picture
->i_nb_fields
= 2;
106 video_format_Setup( &p_picture
->format
, fmt
->i_chroma
, fmt
->i_width
, fmt
->i_height
,
107 fmt
->i_visible_width
, fmt
->i_visible_height
,
108 fmt
->i_sar_num
, fmt
->i_sar_den
);
110 const vlc_chroma_description_t
*p_dsc
=
111 vlc_fourcc_GetChromaDescription( p_picture
->format
.i_chroma
);
115 /* We want V (width/height) to respect:
116 (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
117 (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
118 Which is respected if you have
119 V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
121 unsigned i_modulo_w
= 1;
122 unsigned i_modulo_h
= 1;
123 unsigned i_ratio_h
= 1;
125 for( unsigned i
= 0; i
< p_dsc
->plane_count
; i
++ )
127 i_modulo_w
= LCM( i_modulo_w
, 16 * p_dsc
->p
[i
].w
.den
);
128 i_modulo_h
= LCM( i_modulo_h
, 16 * p_dsc
->p
[i
].h
.den
);
129 if( i_ratio_h
< p_dsc
->p
[i
].h
.den
)
130 i_ratio_h
= p_dsc
->p
[i
].h
.den
;
132 i_modulo_h
= LCM( i_modulo_h
, 32 );
134 unsigned width
, height
;
136 if (unlikely(add_overflow(fmt
->i_width
, i_modulo_w
- 1, &width
))
137 || unlikely(add_overflow(fmt
->i_height
, i_modulo_h
- 1, &height
)))
140 width
= width
/ i_modulo_w
* i_modulo_w
;
141 height
= height
/ i_modulo_h
* i_modulo_h
;
143 /* Hack: append two scan lines for some SIMD assembler */
144 if (unlikely(add_overflow(height
, 2 * i_ratio_h
, &height
)))
147 /* plane_t uses 'int'. */
148 if (unlikely(width
> INT_MAX
) || unlikely(height
> INT_MAX
))
151 for( unsigned i
= 0; i
< p_dsc
->plane_count
; i
++ )
153 plane_t
*p
= &p_picture
->p
[i
];
154 const vlc_rational_t
*h
= &p_dsc
->p
[i
].h
;
155 const vlc_rational_t
*w
= &p_dsc
->p
[i
].w
;
157 /* A plane cannot be over-sampled. This could lead to overflow. */
158 assert(h
->den
>= h
->num
);
159 assert(w
->den
>= w
->num
);
161 p
->i_lines
= height
* h
->num
/ h
->den
;
162 p
->i_visible_lines
= fmt
->i_visible_height
* h
->num
/ h
->den
;
164 p
->i_pitch
= width
* w
->num
/ w
->den
* p_dsc
->pixel_size
;
165 p
->i_visible_pitch
= fmt
->i_visible_width
* w
->num
/ w
->den
167 p
->i_pixel_pitch
= p_dsc
->pixel_size
;
169 assert( (p
->i_pitch
% 16) == 0 );
171 p_picture
->i_planes
= p_dsc
->plane_count
;
176 /*****************************************************************************
178 *****************************************************************************/
180 static picture_priv_t
*picture_NewPrivate(const video_format_t
*restrict p_fmt
)
182 video_format_t fmt
= *p_fmt
;
184 /* It is needed to be sure all information are filled */
185 video_format_Setup( &fmt
, p_fmt
->i_chroma
,
186 p_fmt
->i_width
, p_fmt
->i_height
,
187 p_fmt
->i_visible_width
, p_fmt
->i_visible_height
,
188 p_fmt
->i_sar_num
, p_fmt
->i_sar_den
);
189 if( p_fmt
->i_x_offset
< p_fmt
->i_width
&&
190 p_fmt
->i_y_offset
< p_fmt
->i_height
&&
191 p_fmt
->i_visible_width
> 0 && p_fmt
->i_x_offset
+ p_fmt
->i_visible_width
<= p_fmt
->i_width
&&
192 p_fmt
->i_visible_height
> 0 && p_fmt
->i_y_offset
+ p_fmt
->i_visible_height
<= p_fmt
->i_height
)
193 video_format_CopyCrop( &fmt
, p_fmt
);
196 picture_priv_t
*priv
= malloc( sizeof (*priv
) );
197 if( unlikely(priv
== NULL
) )
200 picture_t
*p_picture
= &priv
->picture
;
202 memset( p_picture
, 0, sizeof( *p_picture
) );
203 p_picture
->format
= fmt
;
205 /* Make sure the real dimensions are a multiple of 16 */
206 if( picture_Setup( p_picture
, &fmt
) )
212 atomic_init( &priv
->gc
.refs
, 1 );
213 priv
->gc
.opaque
= NULL
;
218 picture_t
*picture_NewFromResource( const video_format_t
*p_fmt
, const picture_resource_t
*p_resource
)
220 assert(p_resource
!= NULL
);
222 picture_priv_t
*priv
= picture_NewPrivate(p_fmt
);
223 if (unlikely(priv
== NULL
))
226 picture_t
*p_picture
= &priv
->picture
;
228 p_picture
->p_sys
= p_resource
->p_sys
;
230 if( p_resource
->pf_destroy
!= NULL
)
231 priv
->gc
.destroy
= p_resource
->pf_destroy
;
233 priv
->gc
.destroy
= picture_DestroyFromResource
;
235 for( int i
= 0; i
< p_picture
->i_planes
; i
++ )
237 p_picture
->p
[i
].p_pixels
= p_resource
->p
[i
].p_pixels
;
238 p_picture
->p
[i
].i_lines
= p_resource
->p
[i
].i_lines
;
239 p_picture
->p
[i
].i_pitch
= p_resource
->p
[i
].i_pitch
;
245 #define PICTURE_SW_SIZE_MAX (UINT32_C(1) << 28) /* 256MB: 8K * 8K * 4*/
247 picture_t
*picture_NewFromFormat(const video_format_t
*restrict fmt
)
249 picture_priv_t
*priv
= picture_NewPrivate(fmt
);
250 if (unlikely(priv
== NULL
))
253 priv
->gc
.destroy
= picture_Destroy
;
255 picture_t
*pic
= &priv
->picture
;
256 if (pic
->i_planes
== 0)
259 /* Calculate how big the new image should be */
260 size_t plane_sizes
[PICTURE_PLANE_MAX
];
263 for (int i
= 0; i
< pic
->i_planes
; i
++)
265 const plane_t
*p
= &pic
->p
[i
];
267 if (unlikely(mul_overflow(p
->i_pitch
, p
->i_lines
, &plane_sizes
[i
]))
268 || unlikely(add_overflow(pic_size
, plane_sizes
[i
], &pic_size
)))
272 if (unlikely(pic_size
>= PICTURE_SW_SIZE_MAX
))
275 uint8_t *buf
= aligned_alloc(16, pic_size
);
276 if (unlikely(buf
== NULL
))
279 /* Fill the p_pixels field for each plane */
280 for (int i
= 0; i
< pic
->i_planes
; i
++)
282 pic
->p
[i
].p_pixels
= buf
;
283 buf
+= plane_sizes
[i
];
292 picture_t
*picture_New( vlc_fourcc_t i_chroma
, int i_width
, int i_height
, int i_sar_num
, int i_sar_den
)
296 video_format_Init( &fmt
, 0 );
297 video_format_Setup( &fmt
, i_chroma
, i_width
, i_height
,
298 i_width
, i_height
, i_sar_num
, i_sar_den
);
300 return picture_NewFromFormat( &fmt
);
303 /*****************************************************************************
305 *****************************************************************************/
307 picture_t
*picture_Hold( picture_t
*p_picture
)
309 assert( p_picture
!= NULL
);
311 picture_priv_t
*priv
= (picture_priv_t
*)p_picture
;
312 uintptr_t refs
= atomic_fetch_add( &priv
->gc
.refs
, 1 );
317 void picture_Release( picture_t
*p_picture
)
319 assert( p_picture
!= NULL
);
321 picture_priv_t
*priv
= (picture_priv_t
*)p_picture
;
322 uintptr_t refs
= atomic_fetch_sub( &priv
->gc
.refs
, 1 );
327 PictureDestroyContext( p_picture
);
328 assert( priv
->gc
.destroy
!= NULL
);
329 priv
->gc
.destroy( p_picture
);
332 /*****************************************************************************
334 *****************************************************************************/
335 void plane_CopyPixels( plane_t
*p_dst
, const plane_t
*p_src
)
337 const unsigned i_width
= __MIN( p_dst
->i_visible_pitch
,
338 p_src
->i_visible_pitch
);
339 const unsigned i_height
= __MIN( p_dst
->i_visible_lines
,
340 p_src
->i_visible_lines
);
342 /* The 2x visible pitch check does two things:
343 1) Makes field plane_t's work correctly (see the deinterlacer module)
344 2) Moves less data if the pitch and visible pitch differ much.
346 if( p_src
->i_pitch
== p_dst
->i_pitch
&&
347 p_src
->i_pitch
< 2*p_src
->i_visible_pitch
)
349 /* There are margins, but with the same width : perfect ! */
350 memcpy( p_dst
->p_pixels
, p_src
->p_pixels
,
351 p_src
->i_pitch
* i_height
);
355 /* We need to proceed line by line */
356 uint8_t *p_in
= p_src
->p_pixels
;
357 uint8_t *p_out
= p_dst
->p_pixels
;
362 for( int i_line
= i_height
; i_line
--; )
364 memcpy( p_out
, p_in
, i_width
);
365 p_in
+= p_src
->i_pitch
;
366 p_out
+= p_dst
->i_pitch
;
371 void picture_CopyProperties( picture_t
*p_dst
, const picture_t
*p_src
)
373 p_dst
->date
= p_src
->date
;
374 p_dst
->b_force
= p_src
->b_force
;
376 p_dst
->b_progressive
= p_src
->b_progressive
;
377 p_dst
->i_nb_fields
= p_src
->i_nb_fields
;
378 p_dst
->b_top_field_first
= p_src
->b_top_field_first
;
381 void picture_CopyPixels( picture_t
*p_dst
, const picture_t
*p_src
)
383 for( int i
= 0; i
< p_src
->i_planes
; i
++ )
384 plane_CopyPixels( p_dst
->p
+i
, p_src
->p
+i
);
386 assert( p_dst
->context
== NULL
);
388 if( p_src
->context
!= NULL
)
389 p_dst
->context
= p_src
->context
->copy( p_src
->context
);
392 void picture_Copy( picture_t
*p_dst
, const picture_t
*p_src
)
394 picture_CopyPixels( p_dst
, p_src
);
395 picture_CopyProperties( p_dst
, p_src
);
398 static void picture_DestroyClone(picture_t
*clone
)
400 picture_t
*picture
= ((picture_priv_t
*)clone
)->gc
.opaque
;
403 picture_Release(picture
);
406 picture_t
*picture_Clone(picture_t
*picture
)
408 /* TODO: merge common code with picture_pool_ClonePicture(). */
409 picture_resource_t res
= {
410 .p_sys
= picture
->p_sys
,
411 .pf_destroy
= picture_DestroyClone
,
414 for (int i
= 0; i
< picture
->i_planes
; i
++) {
415 res
.p
[i
].p_pixels
= picture
->p
[i
].p_pixels
;
416 res
.p
[i
].i_lines
= picture
->p
[i
].i_lines
;
417 res
.p
[i
].i_pitch
= picture
->p
[i
].i_pitch
;
420 picture_t
*clone
= picture_NewFromResource(&picture
->format
, &res
);
421 if (likely(clone
!= NULL
)) {
422 ((picture_priv_t
*)clone
)->gc
.opaque
= picture
;
423 picture_Hold(picture
);
425 if (picture
->context
!= NULL
)
426 clone
->context
= picture
->context
->copy(picture
->context
);
431 /*****************************************************************************
433 *****************************************************************************/
434 int picture_Export( vlc_object_t
*p_obj
,
436 video_format_t
*p_fmt
,
437 picture_t
*p_picture
,
438 vlc_fourcc_t i_format
,
439 int i_override_width
, int i_override_height
)
442 video_format_t fmt_in
= p_picture
->format
;
443 if( fmt_in
.i_sar_num
<= 0 || fmt_in
.i_sar_den
<= 0 )
446 fmt_in
.i_sar_den
= 1;
450 video_format_t fmt_out
;
451 memset( &fmt_out
, 0, sizeof(fmt_out
) );
453 fmt_out
.i_sar_den
= 1;
454 fmt_out
.i_chroma
= i_format
;
456 /* compute original width/height */
457 unsigned int i_width
, i_height
, i_original_width
, i_original_height
;
458 if( fmt_in
.i_visible_width
> 0 && fmt_in
.i_visible_height
> 0 )
460 i_width
= fmt_in
.i_visible_width
;
461 i_height
= fmt_in
.i_visible_height
;
465 i_width
= fmt_in
.i_width
;
466 i_height
= fmt_in
.i_height
;
468 if( fmt_in
.i_sar_num
>= fmt_in
.i_sar_den
)
470 i_original_width
= (int64_t)i_width
* fmt_in
.i_sar_num
/ fmt_in
.i_sar_den
;
471 i_original_height
= i_height
;
475 i_original_width
= i_width
;
476 i_original_height
= i_height
* fmt_in
.i_sar_den
/ fmt_in
.i_sar_num
;
480 fmt_out
.i_width
= ( i_override_width
< 0 ) ?
481 i_original_width
: (unsigned)i_override_width
;
482 fmt_out
.i_height
= ( i_override_height
< 0 ) ?
483 i_original_height
: (unsigned)i_override_height
;
485 /* scale if only one direction is provided */
486 if( fmt_out
.i_height
== 0 && fmt_out
.i_width
> 0 )
488 fmt_out
.i_height
= i_height
* fmt_out
.i_width
489 * fmt_in
.i_sar_den
/ fmt_in
.i_width
/ fmt_in
.i_sar_num
;
491 else if( fmt_out
.i_width
== 0 && fmt_out
.i_height
> 0 )
493 fmt_out
.i_width
= i_width
* fmt_out
.i_height
494 * fmt_in
.i_sar_num
/ fmt_in
.i_height
/ fmt_in
.i_sar_den
;
497 image_handler_t
*p_image
= image_HandlerCreate( p_obj
);
501 block_t
*p_block
= image_Write( p_image
, p_picture
, &fmt_in
, &fmt_out
);
503 image_HandlerDelete( p_image
);
509 p_block
->i_dts
= p_picture
->date
;