]> git.sesse.net Git - vlc/blob - src/misc/picture.c
Fix config_IsSafe() declaration warning
[vlc] / src / misc / picture.c
1 /*****************************************************************************
2  * picture.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2010 the VideoLAN team
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
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 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 General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, 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
36 #include <vlc_common.h>
37 #include <vlc_picture.h>
38 #include <vlc_image.h>
39 #include <vlc_block.h>
40
41 /**
42  * Allocate a new picture in the heap.
43  *
44  * This function allocates a fake direct buffer in memory, which can be
45  * used exactly like a video buffer. The video output thread then manages
46  * how it gets displayed.
47  */
48 static int vout_AllocatePicture( picture_t *p_pic,
49                                  vlc_fourcc_t i_chroma,
50                                  int i_width, int i_height,
51                                  int i_sar_num, int i_sar_den )
52 {
53     /* Make sure the real dimensions are a multiple of 16 */
54     if( picture_Setup( p_pic, i_chroma, i_width, i_height,
55                        i_sar_num, i_sar_den ) != VLC_SUCCESS )
56         return VLC_EGENERIC;
57
58     /* Calculate how big the new image should be */
59     size_t i_bytes = 0;
60     for( int i = 0; i < p_pic->i_planes; i++ )
61     {
62         const plane_t *p = &p_pic->p[i];
63
64         if( p->i_pitch <= 0 || p->i_lines <= 0 ||
65             p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
66         {
67             p_pic->i_planes = 0;
68             return VLC_ENOMEM;
69         }
70         i_bytes += p->i_pitch * p->i_lines;
71     }
72
73     uint8_t *p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
74     if( !p_data )
75     {
76         p_pic->i_planes = 0;
77         return VLC_EGENERIC;
78     }
79
80     /* Fill the p_pixels field for each plane */
81     p_pic->p[0].p_pixels = p_data;
82     for( int i = 1; i < p_pic->i_planes; i++ )
83     {
84         p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
85                                                         p_pic->p[i-1].i_pitch ];
86     }
87
88     return VLC_SUCCESS;
89 }
90
91 /*****************************************************************************
92  *
93  *****************************************************************************/
94 static void PictureReleaseCallback( picture_t *p_picture )
95 {
96     if( --p_picture->i_refcount > 0 )
97         return;
98     picture_Delete( p_picture );
99 }
100
101 /*****************************************************************************
102  *
103  *****************************************************************************/
104 void picture_Reset( picture_t *p_picture )
105 {
106     /* */
107     p_picture->date = VLC_TS_INVALID;
108     p_picture->b_force = false;
109     p_picture->b_progressive = false;
110     p_picture->i_nb_fields = 2;
111     p_picture->b_top_field_first = false;
112     picture_CleanupQuant( p_picture );
113 }
114
115 /*****************************************************************************
116  *
117  *****************************************************************************/
118 static int LCM( int a, int b )
119 {
120     return a * b / GCD( a, b );
121 }
122
123 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
124                    int i_width, int i_height, int i_sar_num, int i_sar_den )
125 {
126     /* Store default values */
127     p_picture->i_planes = 0;
128     for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
129     {
130         plane_t *p = &p_picture->p[i];
131         p->p_pixels = NULL;
132         p->i_pixel_pitch = 0;
133     }
134
135     p_picture->pf_release = NULL;
136     p_picture->p_release_sys = NULL;
137     p_picture->i_refcount = 0;
138
139     p_picture->i_nb_fields = 2;
140
141     p_picture->i_qtype = QTYPE_NONE;
142     p_picture->i_qstride = 0;
143     p_picture->p_q = NULL;
144
145     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
146                         i_sar_num, i_sar_den );
147
148     const vlc_chroma_description_t *p_dsc =
149         vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
150     if( !p_dsc )
151         return VLC_EGENERIC;
152
153     /* We want V (width/height) to respect:
154         (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
155         (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
156        Which is respected if you have
157        V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
158     */
159     int i_modulo_w = 1;
160     int i_modulo_h = 1;
161     unsigned int i_ratio_h  = 1;
162     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
163     {
164         i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.den );
165         i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.den );
166         if( i_ratio_h < p_dsc->p[i].h.den )
167             i_ratio_h = p_dsc->p[i].h.den;
168     }
169
170     const int i_width_aligned  = ( i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
171     const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
172     const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
173     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
174     {
175         plane_t *p = &p_picture->p[i];
176
177         p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
178         p->i_visible_lines = i_height * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
179         p->i_pitch         = i_width_aligned * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
180         p->i_visible_pitch = i_width * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
181         p->i_pixel_pitch   = p_dsc->pixel_size;
182
183         assert( (p->i_pitch % 16) == 0 );
184     }
185     p_picture->i_planes  = p_dsc->plane_count;
186
187     return VLC_SUCCESS;
188 }
189
190 /*****************************************************************************
191  *
192  *****************************************************************************/
193 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
194 {
195     video_format_t fmt = *p_fmt;
196
197     /* It is needed to be sure all information are filled */
198     video_format_Setup( &fmt, p_fmt->i_chroma,
199                               p_fmt->i_width, p_fmt->i_height,
200                               p_fmt->i_sar_num, p_fmt->i_sar_den );
201     if( p_fmt->i_x_offset < p_fmt->i_width &&
202         p_fmt->i_y_offset < p_fmt->i_height &&
203         p_fmt->i_visible_width  > 0 && p_fmt->i_x_offset + p_fmt->i_visible_width  <= p_fmt->i_width &&
204         p_fmt->i_visible_height > 0 && p_fmt->i_y_offset + p_fmt->i_visible_height <= p_fmt->i_height )
205         video_format_CopyCrop( &fmt, p_fmt );
206
207     /* */
208     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
209     if( !p_picture )
210         return NULL;
211
212     if( p_resource )
213     {
214         if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
215                            fmt.i_sar_num, fmt.i_sar_den ) )
216         {
217             free( p_picture );
218             return NULL;
219         }
220         p_picture->p_sys = p_resource->p_sys;
221
222         for( int i = 0; i < p_picture->i_planes; i++ )
223         {
224             p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
225             p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
226             p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
227         }
228     }
229     else
230     {
231         if( vout_AllocatePicture( p_picture,
232                                   fmt.i_chroma, fmt.i_width, fmt.i_height,
233                                   fmt.i_sar_num, fmt.i_sar_den ) )
234         {
235             free( p_picture );
236             return NULL;
237         }
238     }
239     /* */
240     p_picture->format = fmt;
241     p_picture->i_refcount = 1;
242     p_picture->pf_release = PictureReleaseCallback;
243
244     return p_picture;
245 }
246 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
247 {
248     return picture_NewFromResource( p_fmt, NULL );
249 }
250 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
251 {
252     video_format_t fmt;
253
254     memset( &fmt, 0, sizeof(fmt) );
255     video_format_Setup( &fmt, i_chroma, i_width, i_height,
256                         i_sar_num, i_sar_den );
257
258     return picture_NewFromFormat( &fmt );
259 }
260
261 /*****************************************************************************
262  *
263  *****************************************************************************/
264 void picture_Delete( picture_t *p_picture )
265 {
266     assert( p_picture && p_picture->i_refcount == 0 );
267     assert( p_picture->p_release_sys == NULL );
268
269     free( p_picture->p_q );
270     free( p_picture->p_data_orig );
271     free( p_picture->p_sys );
272     free( p_picture );
273 }
274
275 /*****************************************************************************
276  *
277  *****************************************************************************/
278 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
279 {
280     int i;
281
282     for( i = 0; i < p_src->i_planes ; i++ )
283         plane_CopyPixels( p_dst->p+i, p_src->p+i );
284 }
285
286 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
287 {
288     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
289                                      p_src->i_visible_pitch );
290     const unsigned i_height = __MIN( p_dst->i_visible_lines,
291                                      p_src->i_visible_lines );
292
293     /* The 2x visible pitch check does two things:
294        1) Makes field plane_t's work correctly (see the deinterlacer module)
295        2) Moves less data if the pitch and visible pitch differ much.
296     */
297     if( p_src->i_pitch == p_dst->i_pitch  &&
298         p_src->i_pitch < 2*p_src->i_visible_pitch )
299     {
300         /* There are margins, but with the same width : perfect ! */
301         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
302                     p_src->i_pitch * i_height );
303     }
304     else
305     {
306         /* We need to proceed line by line */
307         uint8_t *p_in = p_src->p_pixels;
308         uint8_t *p_out = p_dst->p_pixels;
309         int i_line;
310
311         assert( p_in );
312         assert( p_out );
313
314         for( i_line = i_height; i_line--; )
315         {
316             vlc_memcpy( p_out, p_in, i_width );
317             p_in += p_src->i_pitch;
318             p_out += p_dst->i_pitch;
319         }
320     }
321 }
322
323 /*****************************************************************************
324  *
325  *****************************************************************************/
326 int picture_Export( vlc_object_t *p_obj,
327                     block_t **pp_image,
328                     video_format_t *p_fmt,
329                     picture_t *p_picture,
330                     vlc_fourcc_t i_format,
331                     int i_override_width, int i_override_height )
332 {
333     /* */
334     video_format_t fmt_in = p_picture->format;
335     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
336     {
337         fmt_in.i_sar_num =
338         fmt_in.i_sar_den = 1;
339     }
340
341     /* */
342     video_format_t fmt_out;
343     memset( &fmt_out, 0, sizeof(fmt_out) );
344     fmt_out.i_sar_num =
345     fmt_out.i_sar_den = 1;
346     fmt_out.i_chroma  = i_format;
347
348     /* compute original width/height */
349     unsigned int i_original_width;
350     unsigned int i_original_height;
351     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
352     {
353         i_original_width = (int64_t)fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
354         i_original_height = fmt_in.i_height;
355     }
356     else
357     {
358         i_original_width =  fmt_in.i_width;
359         i_original_height = (int64_t)fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
360     }
361
362     /* */
363     fmt_out.i_width  = ( i_override_width < 0 ) ?
364                        i_original_width : i_override_width;
365     fmt_out.i_height = ( i_override_height < 0 ) ?
366                        i_original_height : i_override_height;
367
368     /* scale if only one direction is provided */
369     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
370     {
371         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
372                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
373     }
374     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
375     {
376         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
377                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
378     }
379
380     image_handler_t *p_image = image_HandlerCreate( p_obj );
381
382     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
383
384     image_HandlerDelete( p_image );
385
386     if( !p_block )
387         return VLC_EGENERIC;
388
389     p_block->i_pts =
390     p_block->i_dts = p_picture->date;
391
392     if( p_fmt )
393         *p_fmt = fmt_out;
394     *pp_image = p_block;
395
396     return VLC_SUCCESS;
397 }
398
399 void picture_BlendSubpicture(picture_t *dst,
400                              filter_t *blend, subpicture_t *src)
401 {
402     assert(blend && dst && blend->fmt_out.video.i_chroma == dst->format.i_chroma);
403     assert(src && !src->b_fade && src->b_absolute);
404
405     for (subpicture_region_t *r = src->p_region; r != NULL; r = r->p_next) {
406         assert(r->p_picture && r->i_align == 0);
407         if (filter_ConfigureBlend(blend, dst->format.i_width, dst->format.i_height,
408                                   &r->fmt) ||
409             filter_Blend(blend, dst,
410                          r->i_x, r->i_y, r->p_picture,
411                          src->i_alpha * r->i_alpha / 255)) {
412             msg_Err(blend, "blending %4.4s to %4.4s failed",
413                     (char *)&blend->fmt_in.video.i_chroma,
414                     (char *)&blend->fmt_out.video.i_chroma );
415         }
416     }
417 }