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_TICK_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 /* plane_t uses 'int'. */
144 if (unlikely(width
> INT_MAX
) || unlikely(height
> INT_MAX
))
147 for( unsigned i
= 0; i
< p_dsc
->plane_count
; i
++ )
149 plane_t
*p
= &p_picture
->p
[i
];
150 const vlc_rational_t
*h
= &p_dsc
->p
[i
].h
;
151 const vlc_rational_t
*w
= &p_dsc
->p
[i
].w
;
153 /* A plane cannot be over-sampled. This could lead to overflow. */
154 assert(h
->den
>= h
->num
);
155 assert(w
->den
>= w
->num
);
157 p
->i_lines
= height
* h
->num
/ h
->den
;
158 p
->i_visible_lines
= (fmt
->i_visible_height
+ (h
->den
- 1)) / h
->den
* h
->num
;
160 p
->i_pitch
= width
* w
->num
/ w
->den
* p_dsc
->pixel_size
;
161 p
->i_visible_pitch
= (fmt
->i_visible_width
+ (w
->den
- 1)) / w
->den
* w
->num
163 p
->i_pixel_pitch
= p_dsc
->pixel_size
;
165 assert( (p
->i_pitch
% 16) == 0 );
167 p_picture
->i_planes
= p_dsc
->plane_count
;
172 /*****************************************************************************
174 *****************************************************************************/
176 static picture_priv_t
*picture_NewPrivate(const video_format_t
*restrict p_fmt
)
178 video_format_t fmt
= *p_fmt
;
180 /* It is needed to be sure all information are filled */
181 video_format_Setup( &fmt
, p_fmt
->i_chroma
,
182 p_fmt
->i_width
, p_fmt
->i_height
,
183 p_fmt
->i_visible_width
, p_fmt
->i_visible_height
,
184 p_fmt
->i_sar_num
, p_fmt
->i_sar_den
);
185 if( p_fmt
->i_x_offset
< p_fmt
->i_width
&&
186 p_fmt
->i_y_offset
< p_fmt
->i_height
&&
187 p_fmt
->i_visible_width
> 0 && p_fmt
->i_x_offset
+ p_fmt
->i_visible_width
<= p_fmt
->i_width
&&
188 p_fmt
->i_visible_height
> 0 && p_fmt
->i_y_offset
+ p_fmt
->i_visible_height
<= p_fmt
->i_height
)
189 video_format_CopyCrop( &fmt
, p_fmt
);
192 picture_priv_t
*priv
= malloc( sizeof (*priv
) );
193 if( unlikely(priv
== NULL
) )
196 picture_t
*p_picture
= &priv
->picture
;
198 memset( p_picture
, 0, sizeof( *p_picture
) );
199 p_picture
->format
= fmt
;
201 /* Make sure the real dimensions are a multiple of 16 */
202 if( picture_Setup( p_picture
, &fmt
) )
208 atomic_init( &priv
->gc
.refs
, 1 );
209 priv
->gc
.opaque
= NULL
;
214 picture_t
*picture_NewFromResource( const video_format_t
*p_fmt
, const picture_resource_t
*p_resource
)
216 assert(p_resource
!= NULL
);
218 picture_priv_t
*priv
= picture_NewPrivate(p_fmt
);
219 if (unlikely(priv
== NULL
))
222 picture_t
*p_picture
= &priv
->picture
;
224 p_picture
->p_sys
= p_resource
->p_sys
;
226 if( p_resource
->pf_destroy
!= NULL
)
227 priv
->gc
.destroy
= p_resource
->pf_destroy
;
229 priv
->gc
.destroy
= picture_DestroyFromResource
;
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
;
241 #define PICTURE_SW_SIZE_MAX (UINT32_C(1) << 28) /* 256MB: 8K * 8K * 4*/
243 picture_t
*picture_NewFromFormat(const video_format_t
*restrict fmt
)
245 picture_priv_t
*priv
= picture_NewPrivate(fmt
);
246 if (unlikely(priv
== NULL
))
249 priv
->gc
.destroy
= picture_Destroy
;
251 picture_t
*pic
= &priv
->picture
;
252 if (pic
->i_planes
== 0)
255 /* Calculate how big the new image should be */
256 size_t plane_sizes
[PICTURE_PLANE_MAX
];
259 for (int i
= 0; i
< pic
->i_planes
; i
++)
261 const plane_t
*p
= &pic
->p
[i
];
263 if (unlikely(mul_overflow(p
->i_pitch
, p
->i_lines
, &plane_sizes
[i
]))
264 || unlikely(add_overflow(pic_size
, plane_sizes
[i
], &pic_size
)))
268 if (unlikely(pic_size
>= PICTURE_SW_SIZE_MAX
))
271 uint8_t *buf
= aligned_alloc(16, pic_size
);
272 if (unlikely(buf
== NULL
))
275 /* Fill the p_pixels field for each plane */
276 for (int i
= 0; i
< pic
->i_planes
; i
++)
278 pic
->p
[i
].p_pixels
= buf
;
279 buf
+= plane_sizes
[i
];
288 picture_t
*picture_New( vlc_fourcc_t i_chroma
, int i_width
, int i_height
, int i_sar_num
, int i_sar_den
)
292 video_format_Init( &fmt
, 0 );
293 video_format_Setup( &fmt
, i_chroma
, i_width
, i_height
,
294 i_width
, i_height
, i_sar_num
, i_sar_den
);
296 return picture_NewFromFormat( &fmt
);
299 /*****************************************************************************
301 *****************************************************************************/
303 picture_t
*picture_Hold( picture_t
*p_picture
)
305 assert( p_picture
!= NULL
);
307 picture_priv_t
*priv
= (picture_priv_t
*)p_picture
;
308 uintptr_t refs
= atomic_fetch_add( &priv
->gc
.refs
, 1 );
313 void picture_Release( picture_t
*p_picture
)
315 assert( p_picture
!= NULL
);
317 picture_priv_t
*priv
= (picture_priv_t
*)p_picture
;
318 uintptr_t refs
= atomic_fetch_sub( &priv
->gc
.refs
, 1 );
323 PictureDestroyContext( p_picture
);
324 assert( priv
->gc
.destroy
!= NULL
);
325 priv
->gc
.destroy( p_picture
);
328 /*****************************************************************************
330 *****************************************************************************/
331 void plane_CopyPixels( plane_t
*p_dst
, const plane_t
*p_src
)
333 const unsigned i_width
= __MIN( p_dst
->i_visible_pitch
,
334 p_src
->i_visible_pitch
);
335 const unsigned i_height
= __MIN( p_dst
->i_lines
, p_src
->i_lines
);
337 /* The 2x visible pitch check does two things:
338 1) Makes field plane_t's work correctly (see the deinterlacer module)
339 2) Moves less data if the pitch and visible pitch differ much.
341 if( p_src
->i_pitch
== p_dst
->i_pitch
&&
342 p_src
->i_pitch
< 2*p_src
->i_visible_pitch
)
344 /* There are margins, but with the same width : perfect ! */
345 memcpy( p_dst
->p_pixels
, p_src
->p_pixels
,
346 p_src
->i_pitch
* i_height
);
350 /* We need to proceed line by line */
351 uint8_t *p_in
= p_src
->p_pixels
;
352 uint8_t *p_out
= p_dst
->p_pixels
;
357 for( int i_line
= i_height
; i_line
--; )
359 memcpy( p_out
, p_in
, i_width
);
360 p_in
+= p_src
->i_pitch
;
361 p_out
+= p_dst
->i_pitch
;
366 void picture_CopyProperties( picture_t
*p_dst
, const picture_t
*p_src
)
368 p_dst
->date
= p_src
->date
;
369 p_dst
->b_force
= p_src
->b_force
;
371 p_dst
->b_progressive
= p_src
->b_progressive
;
372 p_dst
->i_nb_fields
= p_src
->i_nb_fields
;
373 p_dst
->b_top_field_first
= p_src
->b_top_field_first
;
376 void picture_CopyPixels( picture_t
*p_dst
, const picture_t
*p_src
)
378 for( int i
= 0; i
< p_src
->i_planes
; i
++ )
379 plane_CopyPixels( p_dst
->p
+i
, p_src
->p
+i
);
381 assert( p_dst
->context
== NULL
);
383 if( p_src
->context
!= NULL
)
384 p_dst
->context
= p_src
->context
->copy( p_src
->context
);
387 void picture_Copy( picture_t
*p_dst
, const picture_t
*p_src
)
389 picture_CopyPixels( p_dst
, p_src
);
390 picture_CopyProperties( p_dst
, p_src
);
393 static void picture_DestroyClone(picture_t
*clone
)
395 picture_t
*picture
= ((picture_priv_t
*)clone
)->gc
.opaque
;
398 picture_Release(picture
);
401 picture_t
*picture_Clone(picture_t
*picture
)
403 /* TODO: merge common code with picture_pool_ClonePicture(). */
404 picture_resource_t res
= {
405 .p_sys
= picture
->p_sys
,
406 .pf_destroy
= picture_DestroyClone
,
409 for (int i
= 0; i
< picture
->i_planes
; i
++) {
410 res
.p
[i
].p_pixels
= picture
->p
[i
].p_pixels
;
411 res
.p
[i
].i_lines
= picture
->p
[i
].i_lines
;
412 res
.p
[i
].i_pitch
= picture
->p
[i
].i_pitch
;
415 picture_t
*clone
= picture_NewFromResource(&picture
->format
, &res
);
416 if (likely(clone
!= NULL
)) {
417 ((picture_priv_t
*)clone
)->gc
.opaque
= picture
;
418 picture_Hold(picture
);
420 if (picture
->context
!= NULL
)
421 clone
->context
= picture
->context
->copy(picture
->context
);
426 /*****************************************************************************
428 *****************************************************************************/
429 int picture_Export( vlc_object_t
*p_obj
,
431 video_format_t
*p_fmt
,
432 picture_t
*p_picture
,
433 vlc_fourcc_t i_format
,
434 int i_override_width
, int i_override_height
)
437 video_format_t fmt_in
= p_picture
->format
;
438 if( fmt_in
.i_sar_num
<= 0 || fmt_in
.i_sar_den
<= 0 )
441 fmt_in
.i_sar_den
= 1;
445 video_format_t fmt_out
;
446 memset( &fmt_out
, 0, sizeof(fmt_out
) );
448 fmt_out
.i_sar_den
= 1;
449 fmt_out
.i_chroma
= i_format
;
451 /* compute original width/height */
452 unsigned int i_width
, i_height
, i_original_width
, i_original_height
;
453 if( fmt_in
.i_visible_width
> 0 && fmt_in
.i_visible_height
> 0 )
455 i_width
= fmt_in
.i_visible_width
;
456 i_height
= fmt_in
.i_visible_height
;
460 i_width
= fmt_in
.i_width
;
461 i_height
= fmt_in
.i_height
;
463 if( fmt_in
.i_sar_num
>= fmt_in
.i_sar_den
)
465 i_original_width
= (int64_t)i_width
* fmt_in
.i_sar_num
/ fmt_in
.i_sar_den
;
466 i_original_height
= i_height
;
470 i_original_width
= i_width
;
471 i_original_height
= i_height
* fmt_in
.i_sar_den
/ fmt_in
.i_sar_num
;
475 fmt_out
.i_width
= ( i_override_width
< 0 ) ?
476 i_original_width
: (unsigned)i_override_width
;
477 fmt_out
.i_height
= ( i_override_height
< 0 ) ?
478 i_original_height
: (unsigned)i_override_height
;
480 /* scale if only one direction is provided */
481 if( fmt_out
.i_height
== 0 && fmt_out
.i_width
> 0 )
483 fmt_out
.i_height
= i_height
* fmt_out
.i_width
484 * fmt_in
.i_sar_den
/ fmt_in
.i_width
/ fmt_in
.i_sar_num
;
486 else if( fmt_out
.i_width
== 0 && fmt_out
.i_height
> 0 )
488 fmt_out
.i_width
= i_width
* fmt_out
.i_height
489 * fmt_in
.i_sar_num
/ fmt_in
.i_height
/ fmt_in
.i_sar_den
;
492 image_handler_t
*p_image
= image_HandlerCreate( p_obj
);
496 block_t
*p_block
= image_Write( p_image
, p_picture
, &fmt_in
, &fmt_out
);
498 image_HandlerDelete( p_image
);
504 p_block
->i_dts
= p_picture
->date
;