swscale.c 23.4 KB
Newer Older
1
/*****************************************************************************
2
 * swscale.c: scaling and chroma conversion using libswscale
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2008 VLC authors and VideoLAN
5
 * $Id$
6 7
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Gildas Bazin <gbazin@videolan.org>
9
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
10 11 12
 * 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
13
 * (at your option) any later version.
14
 *
15 16
 * 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
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21 22
 * 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.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
31
#include <assert.h>
32

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
#include <vlc_filter.h>
36
#include <vlc_picture.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
37
#include <vlc_cpu.h>
38

39
#include <libswscale/swscale.h>
40
#include <libswscale/version.h>
41

42 43 44 45
#ifdef __APPLE__
# include <TargetConditionals.h>
#endif

46 47
#include "../codec/avcodec/chroma.h" // Chroma Avutil <-> VLC conversion

48
/* Gruikkkkkkkkkk!!!!! */
49
#undef AVPALETTE_SIZE
50 51
#define AVPALETTE_SIZE (256 * sizeof(uint32_t))

52 53 54
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
55 56 57 58 59 60
static int  OpenScaler( vlc_object_t * );
static void CloseScaler( vlc_object_t * );

#define SCALEMODE_TEXT N_("Scaling mode")
#define SCALEMODE_LONGTEXT N_("Scaling mode to use.")

61
static const int pi_mode_values[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
62
const char *const ppsz_mode_descriptions[] =
63 64 65 66 67
{ N_("Fast bilinear"), N_("Bilinear"), N_("Bicubic (good quality)"),
  N_("Experimental"), N_("Nearest neighbour (bad quality)"),
  N_("Area"), N_("Luma bicubic / chroma bilinear"), N_("Gauss"),
  N_("SincR"), N_("Lanczos"), N_("Bicubic spline") };

68 69
vlc_module_begin ()
    set_description( N_("Video scaling filter") )
70
    set_shortname( N_("Swscale" ) )
71
    set_capability( "video converter", 150 )
72 73 74
    set_category( CAT_VIDEO )
    set_subcategory( SUBCAT_VIDEO_VFILTER )
    set_callbacks( OpenScaler, CloseScaler )
75
    add_integer( "swscale-mode", 2, SCALEMODE_TEXT, SCALEMODE_LONGTEXT, true )
76
        change_integer_list( pi_mode_values, ppsz_mode_descriptions )
77
vlc_module_end ()
78 79 80

/* Version checking */
#if LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)
81 82 83 84
/****************************************************************************
 * Local prototypes
 ****************************************************************************/

85 86 87
/**
 * Internal swscale filter structure.
 */
88 89
struct filter_sys_t
{
90
    SwsFilter *p_filter;
91 92
    int i_cpu_mask, i_sws_flags;

93 94
    video_format_t fmt_in;
    video_format_t fmt_out;
95 96
    const vlc_chroma_description_t *desc_in;
    const vlc_chroma_description_t *desc_out;
97 98 99 100 101

    struct SwsContext *ctx;
    struct SwsContext *ctxA;
    picture_t *p_src_a;
    picture_t *p_dst_a;
102 103 104
    int i_extend_factor;
    picture_t *p_src_e;
    picture_t *p_dst_e;
105
    bool b_add_a;
106
    bool b_copy;
107 108
    bool b_swap_uvi;
    bool b_swap_uvo;
109 110
};

111 112 113 114
static picture_t *Filter( filter_t *, picture_t * );
static int  Init( filter_t * );
static void Clean( filter_t * );

Laurent Aimar's avatar
Laurent Aimar committed
115 116 117 118 119 120 121
typedef struct
{
    int  i_fmti;
    int  i_fmto;
    bool b_has_a;
    bool b_add_a;
    int  i_sws_flags;
122
    bool b_copy;
123 124
    bool b_swap_uvi;
    bool b_swap_uvo;
Laurent Aimar's avatar
Laurent Aimar committed
125 126 127
} ScalerConfiguration;

static int GetParameters( ScalerConfiguration *,
128
                          const video_format_t *p_fmti,
129 130 131 132 133
                          const video_format_t *p_fmto,
                          int i_sws_flags_default );

static int GetSwsCpuMask(void);

134
/* SwScaler point resize quality seems really bad, let our scale module do it
135 136
 * (change it to true to try) */
#define ALLOW_YUVP (false)
137
/* SwScaler does not like too small picture */
138
#define MINIMUM_WIDTH (32)
139

140 141 142
/* XXX is it always 3 even for BIG_ENDIAN (blend.c seems to think so) ? */
#define OFFSET_A (3)

143 144 145 146 147 148 149 150 151 152
/*****************************************************************************
 * OpenScaler: probe the filter and return score
 *****************************************************************************/
static int OpenScaler( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t*)p_this;
    filter_sys_t *p_sys;

    int i_sws_mode;

Laurent Aimar's avatar
Laurent Aimar committed
153
    if( GetParameters( NULL,
154
                       &p_filter->fmt_in.video,
155
                       &p_filter->fmt_out.video, 0 ) )
156 157 158
        return VLC_EGENERIC;

    /* Allocate the memory needed to store the decoder's structure */
159
    if( ( p_filter->p_sys = p_sys = calloc(1, sizeof(filter_sys_t)) ) == NULL )
160 161 162
        return VLC_ENOMEM;

    /* Set CPU capabilities */
163
    p_sys->i_cpu_mask = GetSwsCpuMask();
164

165
    /* */
166
    i_sws_mode = var_CreateGetInteger( p_filter, "swscale-mode" );
167 168 169 170 171 172 173 174 175 176 177 178 179
    switch( i_sws_mode )
    {
    case 0:  p_sys->i_sws_flags = SWS_FAST_BILINEAR; break;
    case 1:  p_sys->i_sws_flags = SWS_BILINEAR; break;
    case 2:  p_sys->i_sws_flags = SWS_BICUBIC; break;
    case 3:  p_sys->i_sws_flags = SWS_X; break;
    case 4:  p_sys->i_sws_flags = SWS_POINT; break;
    case 5:  p_sys->i_sws_flags = SWS_AREA; break;
    case 6:  p_sys->i_sws_flags = SWS_BICUBLIN; break;
    case 7:  p_sys->i_sws_flags = SWS_GAUSS; break;
    case 8:  p_sys->i_sws_flags = SWS_SINC; break;
    case 9:  p_sys->i_sws_flags = SWS_LANCZOS; break;
    case 10: p_sys->i_sws_flags = SWS_SPLINE; break;
180
    default: p_sys->i_sws_flags = SWS_BICUBIC; i_sws_mode = 2; break;
181 182 183
    }

    /* Misc init */
184 185
    memset( &p_sys->fmt_in,  0, sizeof(p_sys->fmt_in) );
    memset( &p_sys->fmt_out, 0, sizeof(p_sys->fmt_out) );
186

187
    if( Init( p_filter ) )
188
    {
189 190
        if( p_sys->p_filter )
            sws_freeFilter( p_sys->p_filter );
191 192 193 194
        free( p_sys );
        return VLC_EGENERIC;
    }

195 196 197
    /* */
    p_filter->pf_video_filter = Filter;

198 199
    msg_Dbg( p_filter, "%ix%i (%ix%i) chroma: %4.4s -> %ix%i (%ix%i) chroma: %4.4s with scaling using %s",
             p_filter->fmt_in.video.i_visible_width, p_filter->fmt_in.video.i_visible_height,
200 201
             p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
             (char *)&p_filter->fmt_in.video.i_chroma,
202
             p_filter->fmt_out.video.i_visible_width, p_filter->fmt_out.video.i_visible_height,
203
             p_filter->fmt_out.video.i_width, p_filter->fmt_out.video.i_height,
204 205
             (char *)&p_filter->fmt_out.video.i_chroma,
             ppsz_mode_descriptions[i_sws_mode] );
206 207 208 209 210 211 212 213 214 215 216 217

    return VLC_SUCCESS;
}

/*****************************************************************************
 * CloseFilter: clean up the filter
 *****************************************************************************/
static void CloseScaler( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t*)p_this;
    filter_sys_t *p_sys = p_filter->p_sys;

218
    Clean( p_filter );
219 220
    if( p_sys->p_filter )
        sws_freeFilter( p_sys->p_filter );
221 222 223 224
    free( p_sys );
}

/*****************************************************************************
225
 * Helpers
226
 *****************************************************************************/
227 228 229 230
static int GetSwsCpuMask(void)
{
    int i_sws_cpu = 0;

231
#if LIBSWSCALE_VERSION_MAJOR < 4
232
#if defined(__i386__) || defined(__x86_64__)
233
    if( vlc_CPU_MMX() )
234 235
        i_sws_cpu |= SWS_CPU_CAPS_MMX;
#if (LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0))
236
    if( vlc_CPU_MMXEXT() )
237 238
        i_sws_cpu |= SWS_CPU_CAPS_MMX2;
#endif
239
    if( vlc_CPU_3dNOW() )
240
        i_sws_cpu |= SWS_CPU_CAPS_3DNOW;
241
#elif defined(__ppc__) || defined(__ppc64__) || defined(__powerpc__)
242
    if( vlc_CPU_ALTIVEC() )
243
        i_sws_cpu |= SWS_CPU_CAPS_ALTIVEC;
244
#endif
245
#endif
246 247 248

    return i_sws_cpu;
}
249

250 251 252 253
static void FixParameters( int *pi_fmt, bool *pb_has_a, bool *pb_swap_uv, vlc_fourcc_t fmt )
{
    switch( fmt )
    {
254
    case VLC_CODEC_YUV422A:
255
        *pi_fmt = AV_PIX_FMT_YUV422P;
256 257 258
        *pb_has_a = true;
        break;
    case VLC_CODEC_YUV420A:
259
        *pi_fmt = AV_PIX_FMT_YUV420P;
260 261
        *pb_has_a = true;
        break;
262
    case VLC_CODEC_YUVA:
263
        *pi_fmt = AV_PIX_FMT_YUV444P;
264 265 266
        *pb_has_a = true;
        break;
    case VLC_CODEC_RGBA:
267
        *pi_fmt = AV_PIX_FMT_BGR32;
268 269
        *pb_has_a = true;
        break;
270
    case VLC_CODEC_ARGB:
271
        *pi_fmt = AV_PIX_FMT_BGR32_1;
272 273
        *pb_has_a = true;
        break;
274
    case VLC_CODEC_BGRA:
275
        *pi_fmt = AV_PIX_FMT_RGB32;
276 277
        *pb_has_a = true;
        break;
278
    case VLC_CODEC_YV12:
279
        *pi_fmt = AV_PIX_FMT_YUV420P;
280 281 282
        *pb_swap_uv = true;
        break;
    case VLC_CODEC_YV9:
283
        *pi_fmt = AV_PIX_FMT_YUV410P;
284 285 286 287 288 289 290
        *pb_swap_uv = true;
        break;
    default:
        break;
    }
}

Laurent Aimar's avatar
Laurent Aimar committed
291
static int GetParameters( ScalerConfiguration *p_cfg,
292
                          const video_format_t *p_fmti,
293 294
                          const video_format_t *p_fmto,
                          int i_sws_flags_default )
295
{
296 297
    int i_fmti = -1;
    int i_fmto = -1;
298

299 300
    bool b_has_ai = false;
    bool b_has_ao = false;
301
    int i_sws_flags = i_sws_flags_default;
302 303
    bool b_swap_uvi = false;
    bool b_swap_uvo = false;
304

305 306
    GetFfmpegChroma( &i_fmti, p_fmti );
    GetFfmpegChroma( &i_fmto, p_fmto );
307

308
    if( p_fmti->i_chroma == p_fmto->i_chroma )
309
    {
310
        if( p_fmti->i_chroma == VLC_CODEC_YUVP && ALLOW_YUVP )
311
        {
312
            i_fmti = i_fmto = AV_PIX_FMT_GRAY8;
313 314 315
            i_sws_flags = SWS_POINT;
        }
    }
316

317 318
    FixParameters( &i_fmti, &b_has_ai, &b_swap_uvi, p_fmti->i_chroma );
    FixParameters( &i_fmto, &b_has_ao, &b_swap_uvo, p_fmto->i_chroma );
319

320
#if !defined (__ANDROID__) && !defined(TARGET_OS_IPHONE)
321 322 323 324
    /* FIXME TODO removed when ffmpeg is fixed
     * Without SWS_ACCURATE_RND the quality is really bad for some conversions */
    switch( i_fmto )
    {
325 326 327
    case AV_PIX_FMT_ARGB:
    case AV_PIX_FMT_RGBA:
    case AV_PIX_FMT_ABGR:
328 329 330
        i_sws_flags |= SWS_ACCURATE_RND;
        break;
    }
331
#endif
332

Laurent Aimar's avatar
Laurent Aimar committed
333 334 335 336
    if( p_cfg )
    {
        p_cfg->i_fmti = i_fmti;
        p_cfg->i_fmto = i_fmto;
337 338
        p_cfg->b_has_a = b_has_ai && b_has_ao;
        p_cfg->b_add_a = (!b_has_ai) && b_has_ao;
339
        p_cfg->b_copy = i_fmti == i_fmto &&
340 341
                        p_fmti->i_visible_width == p_fmto->i_visible_width &&
                        p_fmti->i_visible_height == p_fmto->i_visible_height;
342 343
        p_cfg->b_swap_uvi = b_swap_uvi;
        p_cfg->b_swap_uvo = b_swap_uvo;
Laurent Aimar's avatar
Laurent Aimar committed
344 345
        p_cfg->i_sws_flags = i_sws_flags;
    }
346 347 348 349 350 351 352

    if( i_fmti < 0 || i_fmto < 0 )
        return VLC_EGENERIC;

    return VLC_SUCCESS;
}

353
static int Init( filter_t *p_filter )
354 355
{
    filter_sys_t *p_sys = p_filter->p_sys;
356
    const video_format_t *p_fmti = &p_filter->fmt_in.video;
357
    video_format_t       *p_fmto = &p_filter->fmt_out.video;
358

359 360 361
    if( p_fmti->orientation != p_fmto->orientation )
        return VLC_EGENERIC;

362 363
    if( video_format_IsSimilar( p_fmti, &p_sys->fmt_in ) &&
        video_format_IsSimilar( p_fmto, &p_sys->fmt_out ) &&
364
        p_sys->ctx )
365
    {
366
        return VLC_SUCCESS;
367
    }
368

369
    Clean( p_filter );
370

371
    /* Init with new parameters */
Laurent Aimar's avatar
Laurent Aimar committed
372 373
    ScalerConfiguration cfg;
    if( GetParameters( &cfg, p_fmti, p_fmto, p_sys->i_sws_flags ) )
374 375 376 377
    {
        msg_Err( p_filter, "format not supported" );
        return VLC_EGENERIC;
    }
378 379
    if( p_fmti->i_visible_width == 0 || p_fmti->i_visible_height == 0 ||
        p_fmto->i_visible_width == 0 || p_fmto->i_visible_height == 0 )
380
    {
381 382 383
        msg_Err( p_filter, "invalid scaling: %ix%i -> %ix%i",
                 p_fmti->i_visible_width, p_fmti->i_visible_height,
                 p_fmto->i_visible_width, p_fmto->i_visible_height);
384 385 386
        return VLC_EGENERIC;
    }

387 388 389 390 391
    p_sys->desc_in = vlc_fourcc_GetChromaDescription( p_fmti->i_chroma );
    p_sys->desc_out = vlc_fourcc_GetChromaDescription( p_fmto->i_chroma );
    if( p_sys->desc_in == NULL || p_sys->desc_out == NULL )
        return VLC_EGENERIC;

392 393
    /* swscale does not like too small width */
    p_sys->i_extend_factor = 1;
394
    while( __MIN( p_fmti->i_visible_width, p_fmto->i_visible_width ) * p_sys->i_extend_factor < MINIMUM_WIDTH)
395
        p_sys->i_extend_factor++;
396

397 398
    const unsigned i_fmti_visible_width = p_fmti->i_visible_width * p_sys->i_extend_factor;
    const unsigned i_fmto_visible_width = p_fmto->i_visible_width * p_sys->i_extend_factor;
Laurent Aimar's avatar
Laurent Aimar committed
399
    for( int n = 0; n < (cfg.b_has_a ? 2 : 1); n++ )
400
    {
401 402
        const int i_fmti = n == 0 ? cfg.i_fmti : AV_PIX_FMT_GRAY8;
        const int i_fmto = n == 0 ? cfg.i_fmto : AV_PIX_FMT_GRAY8;
403 404
        struct SwsContext *ctx;

405 406
        ctx = sws_getContext( i_fmti_visible_width, p_fmti->i_visible_height, i_fmti,
                              i_fmto_visible_width, p_fmto->i_visible_height, i_fmto,
Laurent Aimar's avatar
Laurent Aimar committed
407
                              cfg.i_sws_flags | p_sys->i_cpu_mask,
408
                              p_sys->p_filter, NULL, 0 );
409 410 411 412 413 414 415
        if( n == 0 )
            p_sys->ctx = ctx;
        else
            p_sys->ctxA = ctx;
    }
    if( p_sys->ctxA )
    {
416 417
        p_sys->p_src_a = picture_New( VLC_CODEC_GREY, i_fmti_visible_width, p_fmti->i_visible_height, 0, 1 );
        p_sys->p_dst_a = picture_New( VLC_CODEC_GREY, i_fmto_visible_width, p_fmto->i_visible_height, 0, 1 );
418 419 420
    }
    if( p_sys->i_extend_factor != 1 )
    {
421 422
        p_sys->p_src_e = picture_New( p_fmti->i_chroma, i_fmti_visible_width, p_fmti->i_visible_height, 0, 1 );
        p_sys->p_dst_e = picture_New( p_fmto->i_chroma, i_fmto_visible_width, p_fmto->i_visible_height, 0, 1 );
423

424 425 426 427
        if( p_sys->p_src_e )
            memset( p_sys->p_src_e->p[0].p_pixels, 0, p_sys->p_src_e->p[0].i_pitch * p_sys->p_src_e->p[0].i_lines );
        if( p_sys->p_dst_e )
            memset( p_sys->p_dst_e->p[0].p_pixels, 0, p_sys->p_dst_e->p[0].i_pitch * p_sys->p_dst_e->p[0].i_lines );
428
    }
429

430
    if( !p_sys->ctx ||
Laurent Aimar's avatar
Laurent Aimar committed
431
        ( cfg.b_has_a && ( !p_sys->ctxA || !p_sys->p_src_a || !p_sys->p_dst_a ) ) ||
432
        ( p_sys->i_extend_factor != 1 && ( !p_sys->p_src_e || !p_sys->p_dst_e ) ) )
433
    {
434 435
        msg_Err( p_filter, "could not init SwScaler and/or allocate memory" );
        Clean( p_filter );
436 437 438
        return VLC_EGENERIC;
    }

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    if (p_filter->b_allow_fmt_out_change)
    {
        /*
         * If the transformation is not homothetic we must modify the
         * aspect ratio of the output format in order to have the
         * output picture displayed correctly and not stretched
         * horizontally or vertically.
         * WARNING: this is a hack, ideally this should not be needed
         * and the vout should update its video format instead.
         */
        unsigned i_sar_num = p_fmti->i_sar_num * p_fmti->i_visible_width;
        unsigned i_sar_den = p_fmti->i_sar_den * p_fmto->i_visible_width;
        vlc_ureduce(&i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 65536);
        i_sar_num *= p_fmto->i_visible_height;
        i_sar_den *= p_fmti->i_visible_height;
        vlc_ureduce(&i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 65536);
        p_fmto->i_sar_num = i_sar_num;
        p_fmto->i_sar_den = i_sar_den;
    }

Laurent Aimar's avatar
Laurent Aimar committed
459
    p_sys->b_add_a = cfg.b_add_a;
460
    p_sys->b_copy = cfg.b_copy;
461 462
    p_sys->fmt_in  = *p_fmti;
    p_sys->fmt_out = *p_fmto;
463 464
    p_sys->b_swap_uvi = cfg.b_swap_uvi;
    p_sys->b_swap_uvo = cfg.b_swap_uvo;
465

466 467
    return VLC_SUCCESS;
}
468

469 470 471 472
static void Clean( filter_t *p_filter )
{
    filter_sys_t *p_sys = p_filter->p_sys;

473 474 475 476 477
    if( p_sys->p_src_e )
        picture_Release( p_sys->p_src_e );
    if( p_sys->p_dst_e )
        picture_Release( p_sys->p_dst_e );

478 479 480 481
    if( p_sys->p_src_a )
        picture_Release( p_sys->p_src_a );
    if( p_sys->p_dst_a )
        picture_Release( p_sys->p_dst_a );
482

483 484 485 486 487 488 489 490 491 492 493
    if( p_sys->ctxA )
        sws_freeContext( p_sys->ctxA );

    if( p_sys->ctx )
        sws_freeContext( p_sys->ctx );

    /* We have to set it to null has we call be called again :( */
    p_sys->ctx = NULL;
    p_sys->ctxA = NULL;
    p_sys->p_src_a = NULL;
    p_sys->p_dst_a = NULL;
494 495
    p_sys->p_src_e = NULL;
    p_sys->p_dst_e = NULL;
496
}
497

498
static void GetPixels( uint8_t *pp_pixel[4], int pi_pitch[4],
499 500 501
                       const vlc_chroma_description_t *desc,
                       const video_format_t *fmt,
                       const picture_t *p_picture, unsigned planes,
502
                       bool b_swap_uv )
503
{
504 505 506 507 508 509 510
    unsigned i = 0;

    if( planes > (unsigned)p_picture->i_planes )
        planes = p_picture->i_planes;
    assert( !b_swap_uv || planes >= 3 );

    for( ; i < planes; i++ )
511
    {
512 513 514 515 516 517 518 519 520 521
        const plane_t *p = p_picture->p + i;
        if( b_swap_uv && (i == 1 || i== 2) )
            p = p_picture->p + 3 - i;

        pp_pixel[i] = p->p_pixels
            + (((fmt->i_x_offset * desc->p[i].w.num) / desc->p[i].w.den)
                * p->i_pixel_pitch)
            + (((fmt->i_y_offset * desc->p[i].h.num) / desc->p[i].h.den)
                * p->i_pitch);
        pi_pitch[i] = p->i_pitch;
522
    }
523 524

    for( ; i < 4; i++ )
525
    {
526 527
        pp_pixel[i] = NULL;
        pi_pitch[i] = 0;
528
    }
529 530
}

531
static void ExtractA( picture_t *p_dst, const picture_t *restrict p_src,
532
                      unsigned offset )
533 534 535
{
    plane_t *d = &p_dst->p[0];
    const plane_t *s = &p_src->p[0];
536

537 538
    for( unsigned y = 0; y < p_dst->format.i_height; y++ )
        for( unsigned x = 0; x < p_dst->format.i_width; x++ )
539
            d->p_pixels[y*d->i_pitch+x] = s->p_pixels[y*s->i_pitch+4*x+offset];
540
}
541 542

static void InjectA( picture_t *p_dst, const picture_t *restrict p_src,
543
                     unsigned offset )
544 545 546
{
    plane_t *d = &p_dst->p[0];
    const plane_t *s = &p_src->p[0];
547

548 549
    for( unsigned y = 0; y < p_src->format.i_height; y++ )
        for( unsigned x = 0; x < p_src->format.i_width; x++ )
550
            d->p_pixels[y*d->i_pitch+4*x+offset] = s->p_pixels[y*s->i_pitch+x];
551
}
552 553

static void FillA( plane_t *d, unsigned i_offset )
554 555 556 557 558
{
    for( int y = 0; y < d->i_visible_lines; y++ )
        for( int x = 0; x < d->i_visible_pitch; x += d->i_pixel_pitch )
            d->p_pixels[y*d->i_pitch+x+i_offset] = 0xff;
}
559

560 561 562 563 564 565 566 567
static void CopyPad( picture_t *p_dst, const picture_t *p_src )
{
    picture_Copy( p_dst, p_src );
    for( int n = 0; n < p_dst->i_planes; n++ )
    {
        const plane_t *s = &p_src->p[n];
        plane_t *d = &p_dst->p[n];

568
        for( int y = 0; y < s->i_lines && y < d->i_lines; y++ )
569 570 571 572 573 574 575
        {
            for( int x = s->i_visible_pitch; x < d->i_visible_pitch; x += s->i_pixel_pitch )
                memcpy( &d->p_pixels[y*d->i_pitch + x], &d->p_pixels[y*d->i_pitch + s->i_visible_pitch - s->i_pixel_pitch], s->i_pixel_pitch );
        }
    }
}

576 577 578 579 580 581 582 583
static void SwapUV( picture_t *p_dst, const picture_t *p_src )
{
    picture_t tmp = *p_src;
    tmp.p[1] = p_src->p[2];
    tmp.p[2] = p_src->p[1];

    picture_CopyPixels( p_dst, &tmp );
}
584

585
static void Convert( filter_t *p_filter, struct SwsContext *ctx,
586 587
                     picture_t *p_dst, picture_t *p_src, int i_height,
                     int i_plane_count, bool b_swap_uvi, bool b_swap_uvo )
588
{
589
    filter_sys_t *p_sys = p_filter->p_sys;
590
    uint8_t palette[AVPALETTE_SIZE];
591 592
    uint8_t *src[4]; int src_stride[4];
    uint8_t *dst[4]; int dst_stride[4];
593

594 595
    GetPixels( src, src_stride, p_sys->desc_in, &p_filter->fmt_in.video,
               p_src, i_plane_count, b_swap_uvi );
596
    if( p_filter->fmt_in.video.i_chroma == VLC_CODEC_RGBP )
597 598
    {
        memset( palette, 0, sizeof(palette) );
599 600
        if( p_filter->fmt_in.video.p_palette )
            memcpy( palette, p_filter->fmt_in.video.p_palette->palette,
601 602 603 604 605
                    __MIN( sizeof(video_palette_t), AVPALETTE_SIZE ) );
        src[1] = palette;
        src_stride[1] = 4;
    }

606 607
    GetPixels( dst, dst_stride, p_sys->desc_out, &p_filter->fmt_out.video,
               p_dst, i_plane_count, b_swap_uvo );
608 609

#if LIBSWSCALE_VERSION_INT  >= ((0<<16)+(5<<8)+0)
610
    sws_scale( ctx, src, src_stride, 0, i_height,
611 612
               dst, dst_stride );
#else
613
    sws_scale_ordered( ctx, src, src_stride, 0, i_height,
614 615 616 617
                       dst, dst_stride );
#endif
}

618 619 620 621 622 623 624 625
/****************************************************************************
 * Filter: the whole thing
 ****************************************************************************
 * This function is called just after the thread is launched.
 ****************************************************************************/
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
    filter_sys_t *p_sys = p_filter->p_sys;
626 627
    const video_format_t *p_fmti = &p_filter->fmt_in.video;
    const video_format_t *p_fmto = &p_filter->fmt_out.video;
628 629 630
    picture_t *p_pic_dst;

    /* Check if format properties changed */
631
    if( Init( p_filter ) )
Laurent Aimar's avatar
Laurent Aimar committed
632 633 634 635
    {
        picture_Release( p_pic );
        return NULL;
    }
636 637

    /* Request output picture */
Laurent Aimar's avatar
Laurent Aimar committed
638
    p_pic_dst = filter_NewPicture( p_filter );
639 640
    if( !p_pic_dst )
    {
Laurent Aimar's avatar
Laurent Aimar committed
641
        picture_Release( p_pic );
642 643 644
        return NULL;
    }

645 646 647 648 649 650 651 652 653 654 655
    /* */
    picture_t *p_src = p_pic;
    picture_t *p_dst = p_pic_dst;
    if( p_sys->i_extend_factor != 1 )
    {
        p_src = p_sys->p_src_e;
        p_dst = p_sys->p_dst_e;

        CopyPad( p_src, p_pic );
    }

656
    if( p_sys->b_copy && p_sys->b_swap_uvi == p_sys->b_swap_uvo )
657
        picture_CopyPixels( p_dst, p_src );
658 659
    else if( p_sys->b_copy )
        SwapUV( p_dst, p_src );
660
    else
661 662 663 664
    {
        /* Even if alpha is unused, swscale expects the pointer to be set */
        const int n_planes = !p_sys->ctxA && (p_src->i_planes == 4 ||
                             p_dst->i_planes == 4) ? 4 : 3;
665
        Convert( p_filter, p_sys->ctx, p_dst, p_src, p_fmti->i_visible_height,
666 667
                 n_planes, p_sys->b_swap_uvi, p_sys->b_swap_uvo );
    }
668
    if( p_sys->ctxA )
669
    {
670
        /* We extract the A plane to rescale it, and then we reinject it. */
671
        if( p_fmti->i_chroma == VLC_CODEC_RGBA || p_fmti->i_chroma == VLC_CODEC_BGRA )
672
            ExtractA( p_sys->p_src_a, p_src, OFFSET_A );
673
        else if( p_fmti->i_chroma == VLC_CODEC_ARGB )
674
            ExtractA( p_sys->p_src_a, p_src, 0 );
675 676
        else
            plane_CopyPixels( p_sys->p_src_a->p, p_src->p+A_PLANE );
677

678
        Convert( p_filter, p_sys->ctxA, p_sys->p_dst_a, p_sys->p_src_a,
679
                 p_fmti->i_visible_height, 1, false, false );
680
        if( p_fmto->i_chroma == VLC_CODEC_RGBA || p_fmto->i_chroma == VLC_CODEC_BGRA )
681
            InjectA( p_dst, p_sys->p_dst_a, OFFSET_A );
682
        else if( p_fmto->i_chroma == VLC_CODEC_ARGB )
683
            InjectA( p_dst, p_sys->p_dst_a, 0 );
684
        else
685
            plane_CopyPixels( p_dst->p+A_PLANE, p_sys->p_dst_a->p );
686
    }
Laurent Aimar's avatar
Laurent Aimar committed
687
    else if( p_sys->b_add_a )
688 689
    {
        /* We inject a complete opaque alpha plane */
690
        if( p_fmto->i_chroma == VLC_CODEC_RGBA || p_fmto->i_chroma == VLC_CODEC_BGRA )
691
            FillA( &p_dst->p[0], OFFSET_A );
692
        else if( p_fmto->i_chroma == VLC_CODEC_ARGB )
693
            FillA( &p_dst->p[0], 0 );
694 695 696
        else
            FillA( &p_dst->p[A_PLANE], 0 );
    }
697

698 699 700 701 702
    if( p_sys->i_extend_factor != 1 )
    {
        picture_CopyPixels( p_pic_dst, p_dst );
    }

703 704
    picture_CopyProperties( p_pic_dst, p_pic );
    picture_Release( p_pic );
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    return p_pic_dst;
}

#else /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */

int OpenScaler( vlc_object_t *p_this )
{
    return VLC_EGENERIC;
}

void CloseScaler( vlc_object_t *p_this )
{
}

#endif /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */