picture.c 14.7 KB
Newer Older
1
2
3
/*****************************************************************************
 * picture.c : picture management functions
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2000-2010 VLC authors and VideoLAN
5
6
7
8
9
10
11
 * Copyright (C) 2009-2010 Laurent Aimar
 * $Id$
 *
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
 *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
12
13
14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15
16
17
18
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
19
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
22
23
24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>

#include <vlc_common.h>
#include <vlc_picture.h>
#include <vlc_image.h>
#include <vlc_block.h>

/**
 * Allocate a new picture in the heap.
 *
 * This function allocates a fake direct buffer in memory, which can be
 * used exactly like a video buffer. The video output thread then manages
 * how it gets displayed.
 */
48
static int AllocatePicture( picture_t *p_pic )
49
50
51
52
53
54
55
{
    /* Calculate how big the new image should be */
    size_t i_bytes = 0;
    for( int i = 0; i < p_pic->i_planes; i++ )
    {
        const plane_t *p = &p_pic->p[i];

56
        if( p->i_pitch < 0 || p->i_lines <= 0 ||
57
            (size_t)p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
58
59
60
61
62
63
64
        {
            p_pic->i_planes = 0;
            return VLC_ENOMEM;
        }
        i_bytes += p->i_pitch * p->i_lines;
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
65
    uint8_t *p_data = vlc_memalign( 16, i_bytes );
66
    if( i_bytes > 0 && p_data == NULL )
67
68
69
70
    {
        p_pic->i_planes = 0;
        return VLC_EGENERIC;
    }
71
    p_pic->gc.p_sys = (void *)p_data;
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    /* Fill the p_pixels field for each plane */
    p_pic->p[0].p_pixels = p_data;
    for( int i = 1; i < p_pic->i_planes; i++ )
    {
        p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
                                                        p_pic->p[i-1].i_pitch ];
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 *
 *****************************************************************************/
87
88
89
90
91
92
93
94
95
96
97
98

static void PictureDestroyContext( picture_t *p_picture )
{
    void (**context)( void * ) = p_picture->context;
    if( context != NULL )
    {
        void (*context_destroy)( void * ) = *context;
        context_destroy( context );
        p_picture->context = NULL;
    }
}

99
static void PictureDestroy( picture_t *p_picture )
100
{
101
    assert( p_picture &&
102
            atomic_load( &p_picture->gc.refcount ) == 0 );
103

104
    vlc_free( p_picture->gc.p_sys );
105
106
107
108
    free( p_picture->p_sys );
    free( p_picture );
}

109
110
111
112
113
114
115
116
117
/*****************************************************************************
 *
 *****************************************************************************/
void picture_Reset( picture_t *p_picture )
{
    /* */
    p_picture->date = VLC_TS_INVALID;
    p_picture->b_force = false;
    p_picture->b_progressive = false;
118
    p_picture->i_nb_fields = 2;
119
    p_picture->b_top_field_first = false;
120
    PictureDestroyContext( p_picture );
121
122
123
124
125
126
127
128
129
130
}

/*****************************************************************************
 *
 *****************************************************************************/
static int LCM( int a, int b )
{
    return a * b / GCD( a, b );
}

131
int picture_Setup( picture_t *p_picture, const video_format_t *restrict fmt )
132
133
134
135
136
137
138
139
140
141
{
    /* Store default values */
    p_picture->i_planes = 0;
    for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
    {
        plane_t *p = &p_picture->p[i];
        p->p_pixels = NULL;
        p->i_pixel_pitch = 0;
    }

142
    atomic_init( &p_picture->gc.refcount, 0 );
143
144
    p_picture->gc.pf_destroy = NULL;
    p_picture->gc.p_sys = NULL;
145

146
147
    p_picture->i_nb_fields = 2;

148
149
150
    video_format_Setup( &p_picture->format, fmt->i_chroma, fmt->i_width, fmt->i_height,
                        fmt->i_visible_width, fmt->i_visible_height,
                        fmt->i_sar_num, fmt->i_sar_den );
151
152
153
154
155
156
157
158
159
160
161
162
163
164

    const vlc_chroma_description_t *p_dsc =
        vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
    if( !p_dsc )
        return VLC_EGENERIC;

    /* We want V (width/height) to respect:
        (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
        (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
       Which is respected if you have
       V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
    */
    int i_modulo_w = 1;
    int i_modulo_h = 1;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
165
    unsigned int i_ratio_h  = 1;
166
167
168
169
170
171
172
    for( unsigned i = 0; i < p_dsc->plane_count; i++ )
    {
        i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.den );
        i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.den );
        if( i_ratio_h < p_dsc->p[i].h.den )
            i_ratio_h = p_dsc->p[i].h.den;
    }
173
    i_modulo_h = LCM( i_modulo_h, 32 );
174

175
176
    const int i_width_aligned  = ( fmt->i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
    const int i_height_aligned = ( fmt->i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
177
178
179
180
181
182
    const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
    for( unsigned i = 0; i < p_dsc->plane_count; i++ )
    {
        plane_t *p = &p_picture->p[i];

        p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
183
        p->i_visible_lines = fmt->i_visible_height * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
184
        p->i_pitch         = i_width_aligned * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
185
        p->i_visible_pitch = fmt->i_visible_width * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        p->i_pixel_pitch   = p_dsc->pixel_size;

        assert( (p->i_pitch % 16) == 0 );
    }
    p_picture->i_planes  = p_dsc->plane_count;

    return VLC_SUCCESS;
}

/*****************************************************************************
 *
 *****************************************************************************/
picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
{
    video_format_t fmt = *p_fmt;

    /* It is needed to be sure all information are filled */
    video_format_Setup( &fmt, p_fmt->i_chroma,
                              p_fmt->i_width, p_fmt->i_height,
205
                              p_fmt->i_visible_width, p_fmt->i_visible_height,
206
                              p_fmt->i_sar_num, p_fmt->i_sar_den );
207
208
209
210
211
    if( p_fmt->i_x_offset < p_fmt->i_width &&
        p_fmt->i_y_offset < p_fmt->i_height &&
        p_fmt->i_visible_width  > 0 && p_fmt->i_x_offset + p_fmt->i_visible_width  <= p_fmt->i_width &&
        p_fmt->i_visible_height > 0 && p_fmt->i_y_offset + p_fmt->i_visible_height <= p_fmt->i_height )
        video_format_CopyCrop( &fmt, p_fmt );
212
213
214
215
216
217

    /* */
    picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
    if( !p_picture )
        return NULL;

218
    /* Make sure the real dimensions are a multiple of 16 */
219
    if( picture_Setup( p_picture, &fmt ) )
220
221
222
223
224
    {
        free( p_picture );
        return NULL;
    }

225
226
227
    if( p_resource )
    {
        p_picture->p_sys = p_resource->p_sys;
228
        p_picture->gc.pf_destroy = p_resource->pf_destroy;
229
        assert( p_picture->gc.p_sys == NULL );
230
231
232
233
234
235
236
237
238
239

        for( int i = 0; i < p_picture->i_planes; i++ )
        {
            p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
            p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
            p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
        }
    }
    else
    {
240
        if( AllocatePicture( p_picture ) )
241
242
243
244
245
        {
            free( p_picture );
            return NULL;
        }
    }
246

247
248
    /* */
    p_picture->format = fmt;
249

250
    atomic_init( &p_picture->gc.refcount, 1 );
251
252
    if( p_picture->gc.pf_destroy == NULL )
        p_picture->gc.pf_destroy = PictureDestroy;
253
254
255

    return p_picture;
}
256

257
258
259
260
picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
{
    return picture_NewFromResource( p_fmt, NULL );
}
261

262
263
264
265
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 )
{
    video_format_t fmt;

    memset( &fmt, 0, sizeof(fmt) );
    video_format_Setup( &fmt, i_chroma, i_width, i_height,
268
                        i_width, i_height, i_sar_num, i_sar_den );
269
270
271
272
273
274
275
276

    return picture_NewFromFormat( &fmt );
}

/*****************************************************************************
 *
 *****************************************************************************/

277
278
picture_t *picture_Hold( picture_t *p_picture )
{
279
    atomic_fetch_add( &p_picture->gc.refcount, 1 );
280
281
282
283
284
    return p_picture;
}

void picture_Release( picture_t *p_picture )
{
285
286
287
    uintptr_t refs = atomic_fetch_sub( &p_picture->gc.refcount, 1 );
    assert( refs != 0 );
    if( refs > 1 )
288
289
        return;

290
    PictureDestroyContext( p_picture );
291
292
    assert( p_picture->gc.pf_destroy != NULL );
    p_picture->gc.pf_destroy( p_picture );
293
294
295
296
}

bool picture_IsReferenced( picture_t *p_picture )
{
297
    return atomic_load( &p_picture->gc.refcount ) > 1;
298
299
}

300
301
302
303
304
305
306
307
308
309
/*****************************************************************************
 *
 *****************************************************************************/
void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
{
    const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
                                     p_src->i_visible_pitch );
    const unsigned i_height = __MIN( p_dst->i_visible_lines,
                                     p_src->i_visible_lines );

310
311
312
313
314
315
    /* The 2x visible pitch check does two things:
       1) Makes field plane_t's work correctly (see the deinterlacer module)
       2) Moves less data if the pitch and visible pitch differ much.
    */
    if( p_src->i_pitch == p_dst->i_pitch  &&
        p_src->i_pitch < 2*p_src->i_visible_pitch )
316
317
    {
        /* There are margins, but with the same width : perfect ! */
Rafaël Carré's avatar
Rafaël Carré committed
318
        memcpy( p_dst->p_pixels, p_src->p_pixels,
319
320
321
322
323
324
325
326
327
328
329
330
331
332
                    p_src->i_pitch * i_height );
    }
    else
    {
        /* We need to proceed line by line */
        uint8_t *p_in = p_src->p_pixels;
        uint8_t *p_out = p_dst->p_pixels;
        int i_line;

        assert( p_in );
        assert( p_out );

        for( i_line = i_height; i_line--; )
        {
Rafaël Carré's avatar
Rafaël Carré committed
333
            memcpy( p_out, p_in, i_width );
334
335
336
337
338
339
            p_in += p_src->i_pitch;
            p_out += p_dst->i_pitch;
        }
    }
}

340
341
342
343
344
345
346
347
348
349
void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src )
{
    p_dst->date = p_src->date;
    p_dst->b_force = p_src->b_force;

    p_dst->b_progressive = p_src->b_progressive;
    p_dst->i_nb_fields = p_src->i_nb_fields;
    p_dst->b_top_field_first = p_src->b_top_field_first;
}

Laurent Aimar's avatar
Laurent Aimar committed
350
351
352
353
354
355
356
357
void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
{
    int i;

    for( i = 0; i < p_src->i_planes ; i++ )
        plane_CopyPixels( p_dst->p+i, p_src->p+i );
}

358
359
360
361
362
363
364
void picture_Copy( picture_t *p_dst, const picture_t *p_src )
{
    picture_CopyPixels( p_dst, p_src );
    picture_CopyProperties( p_dst, p_src );
}


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*****************************************************************************
 *
 *****************************************************************************/
int picture_Export( vlc_object_t *p_obj,
                    block_t **pp_image,
                    video_format_t *p_fmt,
                    picture_t *p_picture,
                    vlc_fourcc_t i_format,
                    int i_override_width, int i_override_height )
{
    /* */
    video_format_t fmt_in = p_picture->format;
    if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
    {
        fmt_in.i_sar_num =
        fmt_in.i_sar_den = 1;
    }

    /* */
    video_format_t fmt_out;
    memset( &fmt_out, 0, sizeof(fmt_out) );
    fmt_out.i_sar_num =
    fmt_out.i_sar_den = 1;
    fmt_out.i_chroma  = i_format;

    /* compute original width/height */
    unsigned int i_original_width;
    unsigned int i_original_height;
    if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
    {
        i_original_width = (int64_t)fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
        i_original_height = fmt_in.i_height;
    }
    else
    {
        i_original_width =  fmt_in.i_width;
        i_original_height = (int64_t)fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
    }

    /* */
    fmt_out.i_width  = ( i_override_width < 0 ) ?
406
                       i_original_width : (unsigned)i_override_width;
407
    fmt_out.i_height = ( i_override_height < 0 ) ?
408
                       i_original_height : (unsigned)i_override_height;
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440

    /* scale if only one direction is provided */
    if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
    {
        fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
                     * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
    }
    else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
    {
        fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
                     * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
    }

    image_handler_t *p_image = image_HandlerCreate( p_obj );

    block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );

    image_HandlerDelete( p_image );

    if( !p_block )
        return VLC_EGENERIC;

    p_block->i_pts =
    p_block->i_dts = p_picture->date;

    if( p_fmt )
        *p_fmt = fmt_out;
    *pp_image = p_block;

    return VLC_SUCCESS;
}

441
442
unsigned picture_BlendSubpicture(picture_t *dst,
                                 filter_t *blend, subpicture_t *src)
443
{
444
445
    unsigned done = 0;

446
447
448
449
    assert(src && !src->b_fade && src->b_absolute);

    for (subpicture_region_t *r = src->p_region; r != NULL; r = r->p_next) {
        assert(r->p_picture && r->i_align == 0);
450
451
452
453
        if (filter_ConfigureBlend(blend, dst->format.i_width,
                                  dst->format.i_height,  &r->fmt)
         || filter_Blend(blend, dst, r->i_x, r->i_y, r->p_picture,
                         src->i_alpha * r->i_alpha / 255))
454
455
456
            msg_Err(blend, "blending %4.4s to %4.4s failed",
                    (char *)&blend->fmt_in.video.i_chroma,
                    (char *)&blend->fmt_out.video.i_chroma );
457
458
        else
            done++;
459
    }
460
    return done;
461
}