]> git.sesse.net Git - vlc/blob - src/misc/picture.c
Unexport now unused picture_Delete().
[vlc] / 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
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 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             (size_t)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( 16, i_bytes );
74     if( !p_data )
75     {
76         p_pic->i_planes = 0;
77         return VLC_EGENERIC;
78     }
79     p_pic->p_data_orig = p_data; /* TODO: get rid of this */
80
81     /* Fill the p_pixels field for each plane */
82     p_pic->p[0].p_pixels = p_data;
83     for( int i = 1; i < p_pic->i_planes; i++ )
84     {
85         p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
86                                                         p_pic->p[i-1].i_pitch ];
87     }
88
89     return VLC_SUCCESS;
90 }
91
92 /*****************************************************************************
93  *
94  *****************************************************************************/
95 static void picture_Delete( picture_t *p_picture )
96 {
97     assert( p_picture && p_picture->i_refcount == 0 );
98     assert( p_picture->p_release_sys == NULL );
99
100     free( p_picture->p_q );
101     vlc_free( p_picture->p_data_orig );
102     free( p_picture->p_sys );
103     free( p_picture );
104 }
105
106 static void PictureReleaseCallback( picture_t *p_picture )
107 {
108     if( --p_picture->i_refcount > 0 )
109         return;
110     picture_Delete( p_picture );
111 }
112
113 /*****************************************************************************
114  *
115  *****************************************************************************/
116 void picture_Reset( picture_t *p_picture )
117 {
118     /* */
119     p_picture->date = VLC_TS_INVALID;
120     p_picture->b_force = false;
121     p_picture->b_progressive = false;
122     p_picture->i_nb_fields = 2;
123     p_picture->b_top_field_first = false;
124
125     free( p_picture->p_q );
126     p_picture->p_q = NULL;
127     p_picture->i_qstride = 0;
128     p_picture->i_qtype = 0;
129 }
130
131 /*****************************************************************************
132  *
133  *****************************************************************************/
134 static int LCM( int a, int b )
135 {
136     return a * b / GCD( a, b );
137 }
138
139 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
140                    int i_width, int i_height, int i_sar_num, int i_sar_den )
141 {
142     /* Store default values */
143     p_picture->i_planes = 0;
144     for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
145     {
146         plane_t *p = &p_picture->p[i];
147         p->p_pixels = NULL;
148         p->i_pixel_pitch = 0;
149     }
150
151     p_picture->pf_release = NULL;
152     p_picture->p_release_sys = NULL;
153     p_picture->i_refcount = 0;
154
155     p_picture->i_nb_fields = 2;
156
157     p_picture->i_qtype = QTYPE_NONE;
158     p_picture->i_qstride = 0;
159     p_picture->p_q = NULL;
160
161     video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
162                         i_sar_num, i_sar_den );
163
164     const vlc_chroma_description_t *p_dsc =
165         vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
166     if( !p_dsc )
167         return VLC_EGENERIC;
168
169     /* We want V (width/height) to respect:
170         (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
171         (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
172        Which is respected if you have
173        V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
174     */
175     int i_modulo_w = 1;
176     int i_modulo_h = 1;
177     unsigned int i_ratio_h  = 1;
178     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
179     {
180         i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.den );
181         i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.den );
182         if( i_ratio_h < p_dsc->p[i].h.den )
183             i_ratio_h = p_dsc->p[i].h.den;
184     }
185     i_modulo_h = LCM( i_modulo_h, 32 );
186
187     const int i_width_aligned  = ( i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
188     const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
189     const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
190     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
191     {
192         plane_t *p = &p_picture->p[i];
193
194         p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
195         p->i_visible_lines = i_height * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
196         p->i_pitch         = i_width_aligned * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
197         p->i_visible_pitch = i_width * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
198         p->i_pixel_pitch   = p_dsc->pixel_size;
199
200         assert( (p->i_pitch % 16) == 0 );
201     }
202     p_picture->i_planes  = p_dsc->plane_count;
203
204     return VLC_SUCCESS;
205 }
206
207 /*****************************************************************************
208  *
209  *****************************************************************************/
210 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
211 {
212     video_format_t fmt = *p_fmt;
213
214     /* It is needed to be sure all information are filled */
215     video_format_Setup( &fmt, p_fmt->i_chroma,
216                               p_fmt->i_width, p_fmt->i_height,
217                               p_fmt->i_sar_num, p_fmt->i_sar_den );
218     if( p_fmt->i_x_offset < p_fmt->i_width &&
219         p_fmt->i_y_offset < p_fmt->i_height &&
220         p_fmt->i_visible_width  > 0 && p_fmt->i_x_offset + p_fmt->i_visible_width  <= p_fmt->i_width &&
221         p_fmt->i_visible_height > 0 && p_fmt->i_y_offset + p_fmt->i_visible_height <= p_fmt->i_height )
222         video_format_CopyCrop( &fmt, p_fmt );
223
224     /* */
225     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
226     if( !p_picture )
227         return NULL;
228
229     if( p_resource )
230     {
231         if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
232                            fmt.i_sar_num, fmt.i_sar_den ) )
233         {
234             free( p_picture );
235             return NULL;
236         }
237         p_picture->p_sys = p_resource->p_sys;
238
239         for( int i = 0; i < p_picture->i_planes; i++ )
240         {
241             p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
242             p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
243             p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
244         }
245     }
246     else
247     {
248         if( AllocatePicture( p_picture,
249                              fmt.i_chroma, fmt.i_width, fmt.i_height,
250                              fmt.i_sar_num, fmt.i_sar_den ) )
251         {
252             free( p_picture );
253             return NULL;
254         }
255     }
256     /* */
257     p_picture->format = fmt;
258     p_picture->i_refcount = 1;
259     p_picture->pf_release = PictureReleaseCallback;
260
261     return p_picture;
262 }
263 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
264 {
265     return picture_NewFromResource( p_fmt, NULL );
266 }
267 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
268 {
269     video_format_t fmt;
270
271     memset( &fmt, 0, sizeof(fmt) );
272     video_format_Setup( &fmt, i_chroma, i_width, i_height,
273                         i_sar_num, i_sar_den );
274
275     return picture_NewFromFormat( &fmt );
276 }
277
278 /*****************************************************************************
279  *
280  *****************************************************************************/
281
282 picture_t *picture_Hold( picture_t *p_picture )
283 {
284     if( p_picture->pf_release )
285         p_picture->i_refcount++;
286     return p_picture;
287 }
288
289 void picture_Release( picture_t *p_picture )
290 {
291     /* FIXME why do we let pf_release handle the i_refcount ? */
292     if( p_picture->pf_release )
293         p_picture->pf_release( p_picture );
294 }
295
296 bool picture_IsReferenced( picture_t *p_picture )
297 {
298     return p_picture->i_refcount > 1;
299 }
300
301 /*****************************************************************************
302  *
303  *****************************************************************************/
304 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
305 {
306     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
307                                      p_src->i_visible_pitch );
308     const unsigned i_height = __MIN( p_dst->i_visible_lines,
309                                      p_src->i_visible_lines );
310
311     /* The 2x visible pitch check does two things:
312        1) Makes field plane_t's work correctly (see the deinterlacer module)
313        2) Moves less data if the pitch and visible pitch differ much.
314     */
315     if( p_src->i_pitch == p_dst->i_pitch  &&
316         p_src->i_pitch < 2*p_src->i_visible_pitch )
317     {
318         /* There are margins, but with the same width : perfect ! */
319         vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
320                     p_src->i_pitch * i_height );
321     }
322     else
323     {
324         /* We need to proceed line by line */
325         uint8_t *p_in = p_src->p_pixels;
326         uint8_t *p_out = p_dst->p_pixels;
327         int i_line;
328
329         assert( p_in );
330         assert( p_out );
331
332         for( i_line = i_height; i_line--; )
333         {
334             vlc_memcpy( p_out, p_in, i_width );
335             p_in += p_src->i_pitch;
336             p_out += p_dst->i_pitch;
337         }
338     }
339 }
340
341 void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src )
342 {
343     p_dst->date = p_src->date;
344     p_dst->b_force = p_src->b_force;
345
346     p_dst->b_progressive = p_src->b_progressive;
347     p_dst->i_nb_fields = p_src->i_nb_fields;
348     p_dst->b_top_field_first = p_src->b_top_field_first;
349
350     /* FIXME: copy ->p_q and ->p_qstride */
351 }
352
353 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
354 {
355     int i;
356
357     for( i = 0; i < p_src->i_planes ; i++ )
358         plane_CopyPixels( p_dst->p+i, p_src->p+i );
359 }
360
361 void picture_Copy( picture_t *p_dst, const picture_t *p_src )
362 {
363     picture_CopyPixels( p_dst, p_src );
364     picture_CopyProperties( p_dst, p_src );
365 }
366
367
368 /*****************************************************************************
369  *
370  *****************************************************************************/
371 int picture_Export( vlc_object_t *p_obj,
372                     block_t **pp_image,
373                     video_format_t *p_fmt,
374                     picture_t *p_picture,
375                     vlc_fourcc_t i_format,
376                     int i_override_width, int i_override_height )
377 {
378     /* */
379     video_format_t fmt_in = p_picture->format;
380     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
381     {
382         fmt_in.i_sar_num =
383         fmt_in.i_sar_den = 1;
384     }
385
386     /* */
387     video_format_t fmt_out;
388     memset( &fmt_out, 0, sizeof(fmt_out) );
389     fmt_out.i_sar_num =
390     fmt_out.i_sar_den = 1;
391     fmt_out.i_chroma  = i_format;
392
393     /* compute original width/height */
394     unsigned int i_original_width;
395     unsigned int i_original_height;
396     if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
397     {
398         i_original_width = (int64_t)fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
399         i_original_height = fmt_in.i_height;
400     }
401     else
402     {
403         i_original_width =  fmt_in.i_width;
404         i_original_height = (int64_t)fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
405     }
406
407     /* */
408     fmt_out.i_width  = ( i_override_width < 0 ) ?
409                        i_original_width : (unsigned)i_override_width;
410     fmt_out.i_height = ( i_override_height < 0 ) ?
411                        i_original_height : (unsigned)i_override_height;
412
413     /* scale if only one direction is provided */
414     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
415     {
416         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
417                      * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
418     }
419     else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
420     {
421         fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
422                      * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
423     }
424
425     image_handler_t *p_image = image_HandlerCreate( p_obj );
426
427     block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
428
429     image_HandlerDelete( p_image );
430
431     if( !p_block )
432         return VLC_EGENERIC;
433
434     p_block->i_pts =
435     p_block->i_dts = p_picture->date;
436
437     if( p_fmt )
438         *p_fmt = fmt_out;
439     *pp_image = p_block;
440
441     return VLC_SUCCESS;
442 }
443
444 void picture_BlendSubpicture(picture_t *dst,
445                              filter_t *blend, subpicture_t *src)
446 {
447     assert(src && !src->b_fade && src->b_absolute);
448
449     for (subpicture_region_t *r = src->p_region; r != NULL; r = r->p_next) {
450         assert(r->p_picture && r->i_align == 0);
451         if (filter_ConfigureBlend(blend, dst->format.i_width, dst->format.i_height,
452                                   &r->fmt) ||
453             filter_Blend(blend, dst,
454                          r->i_x, r->i_y, r->p_picture,
455                          src->i_alpha * r->i_alpha / 255)) {
456             msg_Err(blend, "blending %4.4s to %4.4s failed",
457                     (char *)&blend->fmt_in.video.i_chroma,
458                     (char *)&blend->fmt_out.video.i_chroma );
459         }
460     }
461 }