scale.c 8.54 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * scale.c: video scaling module for YUVP/A, I420 and RGBA pictures
3 4
 *  Uses the low quality "nearest neighbour" algorithm.
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
5
 * Copyright (C) 2003-2007 VLC authors and VideoLAN
6 7
 * $Id$
 *
8 9
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *          Antoine Cellerier <dionoea @t videolan dot org>
10
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
11 12 13
 * 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
14 15 16 17
 * (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
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
21 22 23
 * 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.
24 25 26 27 28
 *****************************************************************************/

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

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
Rémi Duraffort's avatar
Rémi Duraffort committed
35
#include <vlc_filter.h>
36
#include <vlc_picture.h>
37 38 39 40 41 42 43 44 45 46

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
static int  OpenFilter ( vlc_object_t * );
static picture_t *Filter( filter_t *, picture_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
47 48
vlc_module_begin ()
    set_description( N_("Video scaling filter") )
49
    set_capability( "video converter", 10 )
50
    set_callbacks( OpenFilter, NULL )
51
vlc_module_end ()
52 53 54 55 56 57 58 59

/*****************************************************************************
 * OpenFilter: probe the filter and return score
 *****************************************************************************/
static int OpenFilter( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t*)p_this;

60 61 62 63 64
    if( ( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVP &&
          p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVA &&
          p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420 &&
          p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12 &&
          p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGB32 &&
65 66
          p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGBA &&
          p_filter->fmt_in.video.i_chroma != VLC_CODEC_ARGB ) ||
67 68 69 70 71
        p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
    {
        return VLC_EGENERIC;
    }

72 73 74
    if( p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
        return VLC_EGENERIC;

75
#warning Converter cannot (really) change output format.
76
    video_format_ScaleCropAr( &p_filter->fmt_out.video, &p_filter->fmt_in.video );
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    p_filter->pf_video_filter = Filter;

    msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
             p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
             p_filter->fmt_out.video.i_height );

    return VLC_SUCCESS;
}

/****************************************************************************
 * Filter: the whole thing
 ****************************************************************************/
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
    picture_t *p_pic_dst;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
93
    if( !p_pic ) return NULL;
94

95
#warning Converter cannot (really) change output format.
96 97
    video_format_ScaleCropAr( &p_filter->fmt_out.video, &p_filter->fmt_in.video );

98
    /* Request output picture */
Laurent Aimar's avatar
Laurent Aimar committed
99
    p_pic_dst = filter_NewPicture( p_filter );
100 101
    if( !p_pic_dst )
    {
102
        picture_Release( p_pic );
103 104 105
        return NULL;
    }

106
    if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGBA &&
107
        p_filter->fmt_in.video.i_chroma != VLC_CODEC_ARGB &&
108
        p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGB32 )
109
    {
110
        for( int i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
111
        {
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
            const int i_src_pitch    = p_pic->p[i_plane].i_pitch;
            const int i_dst_pitch    = p_pic_dst->p[i_plane].i_pitch;
            const int i_src_height   = p_filter->fmt_in.video.i_height;
            const int i_src_width    = p_filter->fmt_in.video.i_width;
            const int i_dst_height   = p_filter->fmt_out.video.i_height;
            const int i_dst_width    = p_filter->fmt_out.video.i_width;
            const int i_dst_visible_lines =
                                       p_pic_dst->p[i_plane].i_visible_lines;
            const int i_dst_visible_pitch =
                                       p_pic_dst->p[i_plane].i_visible_pitch;
            const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
#define SHIFT_SIZE 16
            const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
                                       / i_dst_height;
            const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
                                       / i_dst_width;
            const int i_src_height_1 = i_src_height - 1;
            const int i_src_width_1  = i_src_width - 1;

131 132
            uint8_t *p_src = p_pic->p[i_plane].p_pixels;
            uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
133 134
            uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
            const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
135

136 137 138 139
            const int i_shift_height = i_dst_height / i_src_height;
            const int i_shift_width = i_dst_width / i_src_width;

            int l = 1<<(SHIFT_SIZE-i_shift_height);
140 141 142
            for( ; p_dst < p_dstend;
                 p_dst += i_dst_hidden_pitch,
                 p_dstendline += i_dst_pitch, l += i_height_coef )
143
            {
144
                int k = 1<<(SHIFT_SIZE-i_shift_width);
145 146
                uint8_t *p_srcl = p_src
                       + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
147

148
                for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
149
                {
150
                    *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
151 152 153 154 155 156
                }
            }
        }
    }
    else /* RGBA */
    {
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        const int i_src_pitch = p_pic->p->i_pitch;
        const int i_dst_pitch = p_pic_dst->p->i_pitch;
        const int i_src_height   = p_filter->fmt_in.video.i_height;
        const int i_src_width    = p_filter->fmt_in.video.i_width;
        const int i_dst_height   = p_filter->fmt_out.video.i_height;
        const int i_dst_width    = p_filter->fmt_out.video.i_width;
        const int i_dst_visible_lines =
                                   p_pic_dst->p->i_visible_lines;
        const int i_dst_visible_pitch =
                                   p_pic_dst->p->i_visible_pitch;
        const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
        const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
                                   / i_dst_height;
        const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
                                   / i_dst_width;
        const int i_src_height_1 = i_src_height - 1;
        const int i_src_width_1  = i_src_width - 1;

        uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
        uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
        uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
        const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);

180 181 182 183
        const int i_shift_height = i_dst_height / i_src_height;
        const int i_shift_width = i_dst_width / i_src_width;

        int l = 1<<(SHIFT_SIZE-i_shift_height);
184 185 186 187
        for( ; p_dst < p_dstend;
             p_dst += (i_dst_hidden_pitch>>2),
             p_dstendline += (i_dst_pitch>>2),
             l += i_height_coef )
188
        {
189
            int k = 1<<(SHIFT_SIZE-i_shift_width);
190 191 192
            uint32_t *p_srcl = p_src
                    + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
            for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
193
            {
194
                *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
195 196 197 198
            }
        }
    }

199 200
    picture_CopyProperties( p_pic_dst, p_pic );
    picture_Release( p_pic );
201 202
    return p_pic_dst;
}