8af4b250f9f414fc8791bde4c2fcecfd99c132de
[vlc.git] / 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 #include <limits.h>
36
37 #include <vlc_common.h>
38 #include "picture.h"
39 #include <vlc_image.h>
40 #include <vlc_block.h>
41
42 static void PictureDestroyContext( picture_t *p_picture )
43 {
44     picture_context_t *ctx = p_picture->context;
45     if (ctx != NULL)
46     {
47         ctx->destroy(ctx);
48         p_picture->context = NULL;
49     }
50 }
51
52 /**
53  * Destroys a picture allocated by picture_NewFromResource() but without
54  * a custom destruction callback.
55  */
56 static void picture_DestroyFromResource( picture_t *p_picture )
57 {
58     free( p_picture->p_sys );
59     free( p_picture );
60 }
61
62 /**
63  * Destroys a picture allocated with picture_NewFromFormat().
64  */
65 static void picture_Destroy( picture_t *p_picture )
66 {
67     aligned_free( p_picture->p[0].p_pixels );
68     free( p_picture );
69 }
70
71 /*****************************************************************************
72  *
73  *****************************************************************************/
74 void picture_Reset( picture_t *p_picture )
75 {
76     /* */
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 );
83 }
84
85 /*****************************************************************************
86  *
87  *****************************************************************************/
88 static int LCM( int a, int b )
89 {
90     return a * b / GCD( a, b );
91 }
92
93 int picture_Setup( picture_t *p_picture, const video_format_t *restrict fmt )
94 {
95     /* Store default values */
96     p_picture->i_planes = 0;
97     for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
98     {
99         plane_t *p = &p_picture->p[i];
100         p->p_pixels = NULL;
101         p->i_pixel_pitch = 0;
102     }
103
104     p_picture->i_nb_fields = 2;
105
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 );
109
110     const vlc_chroma_description_t *p_dsc =
111         vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
112     if( !p_dsc )
113         return VLC_EGENERIC;
114
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
120     */
121     unsigned i_modulo_w = 1;
122     unsigned i_modulo_h = 1;
123     unsigned i_ratio_h  = 1;
124
125     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
126     {
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;
131     }
132     i_modulo_h = LCM( i_modulo_h, 32 );
133
134     unsigned width, height;
135
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)))
138         return VLC_EGENERIC;
139
140     width = width / i_modulo_w * i_modulo_w;
141     height = height / i_modulo_h * i_modulo_h;
142
143     /* Hack: append two scan lines for some SIMD assembler */
144     if (unlikely(add_overflow(height, 2 * i_ratio_h, &height)))
145         return VLC_EGENERIC;
146
147     /* plane_t uses 'int'. */
148     if (unlikely(width > INT_MAX) || unlikely(height > INT_MAX))
149         return VLC_EGENERIC;
150
151     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
152     {
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;
156
157         /* A plane cannot be over-sampled. This could lead to overflow. */
158         assert(h->den >= h->num);
159         assert(w->den >= w->num);
160
161         p->i_lines = height * h->num / h->den;
162         p->i_visible_lines = fmt->i_visible_height * h->num / h->den;
163
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
166                              * p_dsc->pixel_size;
167         p->i_pixel_pitch = p_dsc->pixel_size;
168
169         assert( (p->i_pitch % 16) == 0 );
170     }
171     p_picture->i_planes = p_dsc->plane_count;
172
173     return VLC_SUCCESS;
174 }
175
176 /*****************************************************************************
177  *
178  *****************************************************************************/
179
180 static picture_priv_t *picture_NewPrivate(const video_format_t *restrict p_fmt)
181 {
182     video_format_t fmt = *p_fmt;
183
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 );
194
195     /* */
196     picture_priv_t *priv = malloc( sizeof (*priv) );
197     if( unlikely(priv == NULL) )
198         return NULL;
199
200     picture_t *p_picture = &priv->picture;
201
202     memset( p_picture, 0, sizeof( *p_picture ) );
203     p_picture->format = fmt;
204
205     /* Make sure the real dimensions are a multiple of 16 */
206     if( picture_Setup( p_picture, &fmt ) )
207     {
208         free( p_picture );
209         return NULL;
210     }
211
212     atomic_init( &priv->gc.refs, 1 );
213     priv->gc.opaque = NULL;
214
215     return priv;
216 }
217
218 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
219 {
220     assert(p_resource != NULL);
221
222     picture_priv_t *priv = picture_NewPrivate(p_fmt);
223     if (unlikely(priv == NULL))
224         return NULL;
225
226     picture_t *p_picture = &priv->picture;
227
228     p_picture->p_sys = p_resource->p_sys;
229
230     if( p_resource->pf_destroy != NULL )
231         priv->gc.destroy = p_resource->pf_destroy;
232     else
233         priv->gc.destroy = picture_DestroyFromResource;
234
235     for( int i = 0; i < p_picture->i_planes; i++ )
236     {
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;
240     }
241
242     return p_picture;
243 }
244
245 #define PICTURE_SW_SIZE_MAX (UINT32_C(1) << 28) /* 256MB: 8K * 8K * 4*/
246
247 picture_t *picture_NewFromFormat(const video_format_t *restrict fmt)
248 {
249     picture_priv_t *priv = picture_NewPrivate(fmt);
250     if (unlikely(priv == NULL))
251         return NULL;
252
253     picture_t *pic = &priv->picture;
254
255     /* Calculate how big the new image should be */
256     size_t plane_sizes[PICTURE_PLANE_MAX];
257     size_t pic_size = 0;
258
259     for (int i = 0; i < pic->i_planes; i++)
260     {
261         const plane_t *p = &pic->p[i];
262
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)))
265             goto error;
266     }
267
268     if (unlikely(pic_size >= PICTURE_SW_SIZE_MAX))
269         goto error;
270
271     uint8_t *buf = aligned_alloc(16, pic_size);
272     if (unlikely(pic_size > 0 && buf == NULL))
273         goto error;
274
275     /* Fill the p_pixels field for each plane */
276     for (int i = 0; i < pic->i_planes; i++)
277     {
278         pic->p[i].p_pixels = buf;
279         buf += plane_sizes[i];
280     }
281
282     priv->gc.destroy = picture_Destroy;
283     return pic;
284 error:
285     free(pic);
286     return NULL;
287 }
288
289 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
290 {
291     video_format_t fmt;
292
293     video_format_Init( &fmt, 0 );
294     video_format_Setup( &fmt, i_chroma, i_width, i_height,
295                         i_width, i_height, i_sar_num, i_sar_den );
296
297     return picture_NewFromFormat( &fmt );
298 }
299
300 /*****************************************************************************
301  *
302  *****************************************************************************/
303
304 picture_t *picture_Hold( picture_t *p_picture )
305 {
306     assert( p_picture != NULL );
307
308     picture_priv_t *priv = (picture_priv_t *)p_picture;
309     uintptr_t refs = atomic_fetch_add( &priv->gc.refs, 1 );
310     assert( refs > 0 );
311     return p_picture;
312 }
313
314 void picture_Release( picture_t *p_picture )
315 {
316     assert( p_picture != NULL );
317
318     picture_priv_t *priv = (picture_priv_t *)p_picture;
319     uintptr_t refs = atomic_fetch_sub( &priv->gc.refs, 1 );
320     assert( refs != 0 );
321     if( refs > 1 )
322         return;
323
324     PictureDestroyContext( p_picture );
325     assert( priv->gc.destroy != NULL );
326     priv->gc.destroy( p_picture );
327 }
328
329 /*****************************************************************************
330  *
331  *****************************************************************************/
332 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
333 {
334     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
335                                      p_src->i_visible_pitch );
336     const unsigned i_height = __MIN( p_dst->i_visible_lines,
337                                      p_src->i_visible_lines );
338
339     /* The 2x visible pitch check does two things:
340        1) Makes field plane_t's work correctly (see the deinterlacer module)
341        2) Moves less data if the pitch and visible pitch differ much.
342     */
343     if( p_src->i_pitch == p_dst->i_pitch  &&
344         p_src->i_pitch < 2*p_src->i_visible_pitch )
345     {
346         /* There are margins, but with the same width : perfect ! */
347         memcpy( p_dst->p_pixels, p_src->p_pixels,
348                     p_src->i_pitch * i_height );
349     }
350     else
351     {
352         /* We need to proceed line by line */
353         uint8_t *p_in = p_src->p_pixels;
354         uint8_t *p_out = p_dst->p_pixels;
355
356         assert( p_in );
357         assert( p_out );
358
359         for( int i_line = i_height; i_line--; )
360         {
361             memcpy( p_out, p_in, i_width );
362             p_in += p_src->i_pitch;
363             p_out += p_dst->i_pitch;
364         }
365     }
366 }
367
368 void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src )
369 {
370     p_dst->date = p_src->date;
371     p_dst->b_force = p_src->b_force;
372
373     p_dst->b_progressive = p_src->b_progressive;
374     p_dst->i_nb_fields = p_src->i_nb_fields;
375     p_dst->b_top_field_first = p_src->b_top_field_first;
376 }
377
378 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
379 {
380     for( int i = 0; i < p_src->i_planes ; i++ )
381         plane_CopyPixels( p_dst->p+i, p_src->p+i );
382
383     assert( p_dst->context == NULL );
384
385     if( p_src->context != NULL )
386         p_dst->context = p_src->context->copy( p_src->context );
387 }
388
389 void picture_Copy( picture_t *p_dst, const picture_t *p_src )
390 {
391     picture_CopyPixels( p_dst, p_src );
392     picture_CopyProperties( p_dst, p_src );
393 }
394
395 static void picture_DestroyClone(picture_t *clone)
396 {
397     picture_t *picture = ((picture_priv_t *)clone)->gc.opaque;
398
399     free(clone);
400     picture_Release(picture);
401 }
402
403 picture_t *picture_Clone(picture_t *picture)
404 {
405     /* TODO: merge common code with picture_pool_ClonePicture(). */
406     picture_resource_t res = {
407         .p_sys = picture->p_sys,
408         .pf_destroy = picture_DestroyClone,
409     };
410
411     for (int i = 0; i < picture->i_planes; i++) {
412         res.p[i].p_pixels = picture->p[i].p_pixels;
413         res.p[i].i_lines = picture->p[i].i_lines;
414         res.p[i].i_pitch = picture->p[i].i_pitch;
415     }
416
417     picture_t *clone = picture_NewFromResource(&picture->format, &res);
418     if (likely(clone != NULL)) {
419         ((picture_priv_t *)clone)->gc.opaque = picture;
420         picture_Hold(picture);
421
422         if (picture->context != NULL)
423             clone->context = picture->context->copy(picture->context);
424     }
425     return clone;
426 }
427
428 /*****************************************************************************
429  *
430  *****************************************************************************/
431 int picture_Export( vlc_object_t *p_obj,
432                     block_t **pp_image,
433                     video_format_t *p_fmt,
434                     picture_t *p_picture,
435                     vlc_fourcc_t i_format,
436                     int i_override_width, int i_override_height )
437 {
438     /* */
439     video_format_t fmt_in = p_picture->format;
440     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
441     {
442         fmt_in.i_sar_num =
443         fmt_in.i_sar_den = 1;
444     }
445
446     /* */
447     video_format_t fmt_out;
448     memset( &fmt_out, 0, sizeof(fmt_out) );
449     fmt_out.i_sar_num =
450     fmt_out.i_sar_den = 1;
451     fmt_out.i_chroma  = i_format;
452
453     /* compute original width/height */
454     unsigned int i_width, i_height, i_original_width, i_original_height;
455     if( fmt_in.i_visible_width > 0 && fmt_in.i_visible_height > 0 )
456     {
457         i_width = fmt_in.i_visible_width;
458         i_height = fmt_in.i_visible_height;
459     }
460     else
461     {
462         i_width = fmt_in.i_width;
463         i_height = fmt_in.i_height;
464     }
465     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
466     {
467         i_original_width = (int64_t)i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
468         i_original_height = i_height;
469     }
470     else
471     {
472         i_original_width =  i_width;
473         i_original_height = i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
474     }
475
476     /* */
477     fmt_out.i_width  = ( i_override_width < 0 ) ?
478                        i_original_width : (unsigned)i_override_width;
479     fmt_out.i_height = ( i_override_height < 0 ) ?
480                        i_original_height : (unsigned)i_override_height;
481
482     /* scale if only one direction is provided */
483     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
484     {
485         fmt_out.i_height = i_height * fmt_out.i_width
486                          * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
487     }
488     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
489     {
490         fmt_out.i_width  = i_width * fmt_out.i_height
491                          * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
492     }
493
494     image_handler_t *p_image = image_HandlerCreate( p_obj );
495     if( !p_image )
496         return VLC_ENOMEM;
497
498     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
499
500     image_HandlerDelete( p_image );
501
502     if( !p_block )
503         return VLC_EGENERIC;
504
505     p_block->i_pts =
506     p_block->i_dts = p_picture->date;
507
508     if( p_fmt )
509         *p_fmt = fmt_out;
510     *pp_image = p_block;
511
512     return VLC_SUCCESS;
513 }