picture: fix empty allocation leak
[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     priv->gc.destroy = picture_Destroy;
254
255     picture_t *pic = &priv->picture;
256     if (pic->i_planes == 0)
257         return NULL;
258
259     /* Calculate how big the new image should be */
260     size_t plane_sizes[PICTURE_PLANE_MAX];
261     size_t pic_size = 0;
262
263     for (int i = 0; i < pic->i_planes; i++)
264     {
265         const plane_t *p = &pic->p[i];
266
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)))
269             goto error;
270     }
271
272     if (unlikely(pic_size >= PICTURE_SW_SIZE_MAX))
273         goto error;
274
275     uint8_t *buf = aligned_alloc(16, pic_size);
276     if (unlikely(buf == NULL))
277         goto error;
278
279     /* Fill the p_pixels field for each plane */
280     for (int i = 0; i < pic->i_planes; i++)
281     {
282         pic->p[i].p_pixels = buf;
283         buf += plane_sizes[i];
284     }
285
286     return pic;
287 error:
288     free(pic);
289     return NULL;
290 }
291
292 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
293 {
294     video_format_t fmt;
295
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 );
299
300     return picture_NewFromFormat( &fmt );
301 }
302
303 /*****************************************************************************
304  *
305  *****************************************************************************/
306
307 picture_t *picture_Hold( picture_t *p_picture )
308 {
309     assert( p_picture != NULL );
310
311     picture_priv_t *priv = (picture_priv_t *)p_picture;
312     uintptr_t refs = atomic_fetch_add( &priv->gc.refs, 1 );
313     assert( refs > 0 );
314     return p_picture;
315 }
316
317 void picture_Release( picture_t *p_picture )
318 {
319     assert( p_picture != NULL );
320
321     picture_priv_t *priv = (picture_priv_t *)p_picture;
322     uintptr_t refs = atomic_fetch_sub( &priv->gc.refs, 1 );
323     assert( refs != 0 );
324     if( refs > 1 )
325         return;
326
327     PictureDestroyContext( p_picture );
328     assert( priv->gc.destroy != NULL );
329     priv->gc.destroy( p_picture );
330 }
331
332 /*****************************************************************************
333  *
334  *****************************************************************************/
335 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
336 {
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 );
341
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.
345     */
346     if( p_src->i_pitch == p_dst->i_pitch  &&
347         p_src->i_pitch < 2*p_src->i_visible_pitch )
348     {
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 );
352     }
353     else
354     {
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;
358
359         assert( p_in );
360         assert( p_out );
361
362         for( int i_line = i_height; i_line--; )
363         {
364             memcpy( p_out, p_in, i_width );
365             p_in += p_src->i_pitch;
366             p_out += p_dst->i_pitch;
367         }
368     }
369 }
370
371 void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src )
372 {
373     p_dst->date = p_src->date;
374     p_dst->b_force = p_src->b_force;
375
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;
379 }
380
381 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
382 {
383     for( int i = 0; i < p_src->i_planes ; i++ )
384         plane_CopyPixels( p_dst->p+i, p_src->p+i );
385
386     assert( p_dst->context == NULL );
387
388     if( p_src->context != NULL )
389         p_dst->context = p_src->context->copy( p_src->context );
390 }
391
392 void picture_Copy( picture_t *p_dst, const picture_t *p_src )
393 {
394     picture_CopyPixels( p_dst, p_src );
395     picture_CopyProperties( p_dst, p_src );
396 }
397
398 static void picture_DestroyClone(picture_t *clone)
399 {
400     picture_t *picture = ((picture_priv_t *)clone)->gc.opaque;
401
402     free(clone);
403     picture_Release(picture);
404 }
405
406 picture_t *picture_Clone(picture_t *picture)
407 {
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,
412     };
413
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;
418     }
419
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);
424
425         if (picture->context != NULL)
426             clone->context = picture->context->copy(picture->context);
427     }
428     return clone;
429 }
430
431 /*****************************************************************************
432  *
433  *****************************************************************************/
434 int picture_Export( vlc_object_t *p_obj,
435                     block_t **pp_image,
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 )
440 {
441     /* */
442     video_format_t fmt_in = p_picture->format;
443     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
444     {
445         fmt_in.i_sar_num =
446         fmt_in.i_sar_den = 1;
447     }
448
449     /* */
450     video_format_t fmt_out;
451     memset( &fmt_out, 0, sizeof(fmt_out) );
452     fmt_out.i_sar_num =
453     fmt_out.i_sar_den = 1;
454     fmt_out.i_chroma  = i_format;
455
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 )
459     {
460         i_width = fmt_in.i_visible_width;
461         i_height = fmt_in.i_visible_height;
462     }
463     else
464     {
465         i_width = fmt_in.i_width;
466         i_height = fmt_in.i_height;
467     }
468     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
469     {
470         i_original_width = (int64_t)i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
471         i_original_height = i_height;
472     }
473     else
474     {
475         i_original_width =  i_width;
476         i_original_height = i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
477     }
478
479     /* */
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;
484
485     /* scale if only one direction is provided */
486     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
487     {
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;
490     }
491     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
492     {
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;
495     }
496
497     image_handler_t *p_image = image_HandlerCreate( p_obj );
498     if( !p_image )
499         return VLC_ENOMEM;
500
501     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
502
503     image_HandlerDelete( p_image );
504
505     if( !p_block )
506         return VLC_EGENERIC;
507
508     p_block->i_pts =
509     p_block->i_dts = p_picture->date;
510
511     if( p_fmt )
512         *p_fmt = fmt_out;
513     *pp_image = p_block;
514
515     return VLC_SUCCESS;
516 }