seamcarving.c 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*****************************************************************************
 * seamcarving.c: "Seam Carving for Content-Aware Image Resizing"
 * Based on paper by Shai Avidan and Ariel Shamir.
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *****************************************************************************/

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

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

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <vlc/vlc.h>
#include <vlc_sout.h>
#include <vlc_vout.h>

#include "vlc_filter.h"

#include <assert.h>

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

static picture_t *Filter( filter_t *, picture_t * );
static int CropCallback( vlc_object_t *, char const *,
                         vlc_value_t, vlc_value_t,
                         void * );

static void FilterSeamCarving( filter_t *, picture_t *, picture_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

#define FILTER_PREFIX "seamcarving-"

vlc_module_begin();
    set_description( _("Seam Carving video filter") );
62
    set_shortname( _( "Seam Carving" ));
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    set_capability( "video filter2", 0 );
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VFILTER );

    set_callbacks( Create, Destroy );
vlc_module_end();

static const char *ppsz_filter_options[] = {
    NULL
};

struct filter_sys_t
{
    int *p_energy;
    int *p_grad;

    int i_crop;
};

static int Create( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;

    /* Allocate structure */
    p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_filter->p_sys == NULL )
    {
        msg_Err( p_filter, "out of memory" );
        return VLC_ENOMEM;
    }

    p_filter->pf_video_filter = Filter;
    p_filter->p_sys->p_energy = NULL;
    p_filter->p_sys->p_grad = NULL;
    p_filter->p_sys->i_crop = 0;

    config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
                   p_filter->p_cfg );

    var_Create( p_filter, "crop", VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
    var_AddCallback( p_filter, "crop", CropCallback, p_filter->p_sys );

    return VLC_SUCCESS;
}

static void Destroy( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;

    free( p_filter->p_sys->p_energy );
    free( p_filter->p_sys->p_grad );

    free( p_filter->p_sys );
}

static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
    picture_t *p_outpic;

    if( !p_pic ) return NULL;

    p_outpic = p_filter->pf_vout_buffer_new( p_filter );
    if( !p_outpic )
    {
        msg_Warn( p_filter, "can't get output picture" );
        if( p_pic->pf_release )
            p_pic->pf_release( p_pic );
        return NULL;
    }

    FilterSeamCarving( p_filter, p_pic, p_outpic );

    p_outpic->date = p_pic->date;
    p_outpic->b_force = p_pic->b_force;
    p_outpic->i_nb_fields = p_pic->i_nb_fields;
    p_outpic->b_progressive = p_pic->b_progressive;
    p_outpic->b_top_field_first = p_pic->b_top_field_first;

    if( p_pic->pf_release )
        p_pic->pf_release( p_pic );

    return p_outpic;
}

147 148
static inline int my_min3( int a, int b, int c );
static inline int my_min( int a, int b );
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible );

//#define DRAW_GRADIENT
//#define DRAW_ENERGY
//#define DRAW_SEAM

static void FilterSeamCarving( filter_t *p_filter, picture_t *p_inpic,
                                                picture_t *p_outpic )
{
    const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
    const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;

    int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;

    if( !p_filter->p_sys->p_energy )
        p_filter->p_sys->p_energy = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
    if( !p_filter->p_sys->p_grad )
        p_filter->p_sys->p_grad = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));

//#if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
    p_filter->p_libvlc->pf_memcpy( p_outpic->p[Y_PLANE].p_pixels,
                                   p_inpic->p[Y_PLANE].p_pixels,
        p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
//#else
//    p_filter->p_libvlc->pf_memset( p_outpix, 0x80,
//        p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
//#endif
    p_filter->p_libvlc->pf_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
        p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
    p_filter->p_libvlc->pf_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
        p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );

#if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
    i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
#else
    static int j = 1;
    static int k = 1;
    int i;
    if( p_filter->p_sys->i_crop != 0 )
        j = p_filter->p_sys->i_crop;
    for( i = 0; i < j; i++ )
    i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
    int y;
    for( y = 0; y < p_outpic->p[Y_PLANE].i_lines; y++ )
        p_filter->p_libvlc->pf_memset( p_outpic->p[Y_PLANE].p_pixels + y*p_outpic->p[Y_PLANE].i_pitch + i_src_visible, 0x00, p_outpic->p[Y_PLANE].i_pitch - i_src_visible );
    j += k;
    if( j == 100 ) k = -1;
    if( j == 1 ) k = 1;
#endif
}

static int ComputeGradient( filter_t *p_filter, picture_t *p_inpic, int i_src_visible )
{
    int x, y;
    const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
    const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;

    const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
    int *p_grad = p_filter->p_sys->p_grad;

    for( y = 1; y < i_num_lines - 1;
         y++, p_grad += i_src_pitch )
    {
        /* Compute line y's gradient */
#define GRADx( x ) \
            ( \
              abs( \
                 ( p_inpix[(y-1)*i_src_pitch+x-1] \
                   - p_inpix[(y+1)*i_src_pitch+x-1] ) \
               + ( ( p_inpix[(y-1)*i_src_pitch+x] \
                    - p_inpix[(y+1)*i_src_pitch+x] ) <<1 ) \
               + ( p_inpix[(y-1)*i_src_pitch+x+1] \
                   - p_inpix[(y+1)*i_src_pitch+x+1] ) \
              ) \
            + \
              abs( \
                 ( p_inpix[(y-1)*i_src_pitch+x-1] \
                   - p_inpix[(y-1)*i_src_pitch+x+1] ) \
               + ( ( p_inpix[y*i_src_pitch+x-1] \
                    - p_inpix[y*i_src_pitch+x+1] ) <<1 ) \
               + ( p_inpix[(y+1)*i_src_pitch+x-1] \
                   - p_inpix[(y+1)*i_src_pitch+x+1] ) \
              ) \
            )
        for( x = 1; x < i_src_visible - 1; x++ )
        {
            p_grad[x] = GRADx( x );
#ifdef DRAW_GRADIENT
            p_outpix[y*i_src_pitch+x] = p_grad[x]>>3;
#endif
        }
    }
    return 0;
}

static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible )
{
    int x, y;
    const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
    const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;

    uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;

    int *p_energy = p_filter->p_sys->p_energy;
    int *p_grad = p_filter->p_sys->p_grad;

    ComputeGradient( p_filter, p_inpic, i_src_visible );

    /** Compute the image's energy (using a sobel gradient as the base energy
     ** function) */
    /* Set the first energy line to 0 */
    memset( p_energy, 0, i_src_pitch*sizeof(int));

    int *p_energy_prev = p_energy;
    for( y = 1; y < i_num_lines - 1;
         y++, p_energy_prev = p_energy, p_energy += i_src_pitch,
         p_grad += i_src_pitch )
    {
        /* Compute line y's minimum energy value for paths ending on
         * each x */
        x = 1;
270
        p_energy[x] = my_min( p_energy_prev[x  ]+p_grad[x  ],
271 272 273
                           p_energy_prev[x+1]+p_grad[x+1] );
        for( x = 2; x < i_src_visible - 2; x++ )
        {
274
            p_energy[x] = my_min3( p_energy_prev[x-1]+p_grad[x-1],
275 276 277
                                p_energy_prev[x  ]+p_grad[x  ],
                                p_energy_prev[x+1]+p_grad[x+1] );
        }
278
        p_energy[x] = my_min( p_energy_prev[x-1]+p_grad[x-1],
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
                           p_energy_prev[x  ]+p_grad[x  ] );

#ifdef DRAW_ENERGY
        int max = p_energy[1];
        for( x = 1; x < i_src_visible - 1; x++ )
            if( p_energy[x] > max ) max = p_energy[x];
        for( x = 1; x < i_src_visible - 1; x++ )
            p_outpix[y*i_src_pitch+x] = p_energy[x]*0xff/max;
#endif
    }

    /* Find the minimum energy point on the last line */
    y--;
    p_energy -= i_src_pitch;
    p_grad -= i_src_pitch;

    int m = p_energy[1];
    int xmin = 1;
    for( x = 1; x < i_src_visible - 1; x++ )
    {
        if( p_energy[x] < m )
        {
            m = p_energy[x];
            xmin = x;
        }
    }

#ifdef DRAW_SEAM
    p_outpix[y*i_src_pitch+xmin] = 0xff;
    p_outpix[(y+1)*i_src_pitch+xmin] = 0xff;
#else
    memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
    memmove( p_outpix+(y+1)*i_src_pitch+xmin, p_outpix+(y+1)*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
#endif

    p_energy -= i_src_pitch;
    for( ; y>1; y--, p_energy -= i_src_pitch, p_grad -= i_src_pitch )
    {
        if( m != p_energy[xmin]+p_grad[xmin] )
        {
            if( xmin > 1 && m == p_energy[xmin-1]+p_grad[xmin-1] )
            {
                xmin--;
            }
            else if( xmin < i_src_visible - 2 && m == p_energy[xmin+1]+p_grad[xmin+1] )
            {
                xmin++;
            }
            else
            {
                printf("Alarm! %d\n" ,y);
                //assert( 0 );
            }
        }
        m = p_energy[xmin];
#ifdef DRAW_SEAM
        p_outpix[y*i_src_pitch+xmin] = 0xff;
#else
        memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
#endif
    }
#ifdef DRAW_SEAM
    p_outpix[y*i_src_pitch+xmin] = 0xff;
#else
    memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
#endif
    y--;
#ifdef DRAW_SEAM
    p_outpix[y*i_src_pitch+xmin] = 0xff;
#else
    memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
#endif

#if defined( DRAW_SEAM )
    return i_src_visible;
#else
    return i_src_visible-1;
#endif
}

359
static inline int my_min3( int a, int b, int c )
360 361 362 363 364 365 366 367 368
{
    if( a < b )
    {
        if( a < c ) return a;
        return c;
    }
    if( b < c ) return b;
    return c;
}
369
static inline int my_min( int a, int b )
370 371 372 373 374 375 376 377
{
    return a < b ? a : b;
}

static int CropCallback( vlc_object_t *p_this, char const *psz_var,
                                vlc_value_t oldval, vlc_value_t newval,
                                void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
378
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
379 380 381 382
    filter_sys_t *p_sys = (filter_sys_t *)p_data;
    p_sys->i_crop = newval.i_int;
    return VLC_SUCCESS;
}