rotate.c 15.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * rotate.c : video rotation filter
 *****************************************************************************
4
 * Copyright (C) 2000-2008 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * $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
 *****************************************************************************/

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

32 33
#include <math.h>                                            /* sin(), cos() */

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
36
#include <vlc_atomic.h>
ivoire's avatar
ivoire committed
37
#include <vlc_filter.h>
38
#include "filter_picture.h"
39
#include "../control/motionlib.h"
40 41 42 43 44 45 46 47

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

static picture_t *Filter( filter_t *, picture_t * );
48
static picture_t *FilterPacked( filter_t *, picture_t * );
49

50 51 52 53
static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
                           vlc_value_t oldval, vlc_value_t newval,
                           void *p_data );

54 55
#define ANGLE_TEXT N_("Angle in degrees")
#define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
56 57 58
#define MOTION_TEXT N_("Use motion sensors")
#define MOTION_LONGTEXT N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion " \
    "sensors to rotate the video")
59 60 61 62 63 64

#define FILTER_PREFIX "rotate-"

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
65 66 67 68 69 70
vlc_module_begin ()
    set_description( N_("Rotate video filter") )
    set_shortname( N_( "Rotate" ))
    set_capability( "video filter2", 0 )
    set_category( CAT_VIDEO )
    set_subcategory( SUBCAT_VIDEO_VFILTER )
71

72
    add_float( FILTER_PREFIX "angle", 30., ANGLE_TEXT, ANGLE_LONGTEXT, false )
73 74
    add_bool( FILTER_PREFIX "use-motion", false, MOTION_TEXT,
              MOTION_LONGTEXT, false )
75

76 77 78
    add_shortcut( "rotate" )
    set_callbacks( Create, Destroy )
vlc_module_end ()
79

80
static const char *const ppsz_filter_options[] = {
81
    "angle", "use-motion", NULL
82 83 84
};

/*****************************************************************************
85
 * filter_sys_t
86 87 88
 *****************************************************************************/
struct filter_sys_t
{
89
    atomic_uint_fast32_t sincos;
90
    motion_sensors_t *p_motion;
91 92
};

93
static void store_trigo( struct filter_sys_t *sys, float f_angle )
94
{
95
    f_angle *= M_PI / 180.f; /* degrees -> radians */
96

97 98
    uint16_t i_sin = lroundf(sinf(f_angle) * 4096.f);
    uint16_t i_cos = lroundf(cosf(f_angle) * 4096.f);
99 100 101 102 103 104 105 106 107
    atomic_store( &sys->sincos, (i_cos << 16) | (i_sin << 0));
}

static void fetch_trigo( struct filter_sys_t *sys, int *i_sin, int *i_cos )
{
    uint32_t sincos = atomic_load( &sys->sincos );

    *i_sin = (int16_t)(sincos & 0xFFFF);
    *i_cos = (int16_t)(sincos >> 16);
108 109
}

110
/*****************************************************************************
111
 * Create: allocates Distort video filter
112 113 114 115
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
116
    filter_sys_t *p_sys;
117

118 119 120 121 122 123
    if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
    {
        msg_Err( p_filter, "Input and output chromas don't match" );
        return VLC_EGENERIC;
    }

124
    switch( p_filter->fmt_in.video.i_chroma )
dionoea's avatar
dionoea committed
125
    {
126
        CASE_PLANAR_YUV
127 128 129 130 131
            p_filter->pf_video_filter = Filter;
            break;

        CASE_PACKED_YUV_422
            p_filter->pf_video_filter = FilterPacked;
132 133 134
            break;

        default:
135
            msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
136 137
                     (char*)&(p_filter->fmt_in.video.i_chroma) );
            return VLC_EGENERIC;
dionoea's avatar
dionoea committed
138 139
    }

140 141 142 143
    /* Allocate structure */
    p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_filter->p_sys == NULL )
        return VLC_ENOMEM;
144
    p_sys = p_filter->p_sys;
145 146 147 148

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

149
    if( var_InheritBool( p_filter, FILTER_PREFIX "use-motion" ) )
150 151 152 153
    {
        p_sys->p_motion = motion_create( VLC_OBJECT( p_filter ) );
        if( p_sys->p_motion == NULL )
        {
154
            free( p_sys );
155 156 157 158 159
            return VLC_EGENERIC;
        }
    }
    else
    {
160 161 162
        float f_angle = var_CreateGetFloatCommand( p_filter,
                                                   FILTER_PREFIX "angle" );
        store_trigo( p_sys, f_angle );
163 164
        var_AddCallback( p_filter, FILTER_PREFIX "angle",
                         RotateCallback, p_sys );
165
        p_sys->p_motion = NULL;
166
    }
167 168 169 170 171

    return VLC_SUCCESS;
}

/*****************************************************************************
172
 * Destroy: destroy Distort filter
173 174 175 176
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
177
    filter_sys_t *p_sys = p_filter->p_sys;
178

179
    if( p_sys->p_motion != NULL )
180 181 182 183 184 185 186
        motion_destroy( p_sys->p_motion );
    else
    {
        var_DelCallback( p_filter, FILTER_PREFIX "angle",
                         RotateCallback, p_sys );
    }
    free( p_sys );
187 188 189
}

/*****************************************************************************
190
 *
191 192 193 194
 *****************************************************************************/
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
    picture_t *p_outpic;
195
    filter_sys_t *p_sys = p_filter->p_sys;
196 197 198

    if( !p_pic ) return NULL;

Laurent Aimar's avatar
Laurent Aimar committed
199
    p_outpic = filter_NewPicture( p_filter );
200 201
    if( !p_outpic )
    {
202
        picture_Release( p_pic );
203 204 205
        return NULL;
    }

206
    if( p_sys->p_motion != NULL )
207
    {
208 209
        int i_angle = motion_get_angle( p_sys->p_motion );
        store_trigo( p_sys, i_angle / 20.f );
210 211
    }

212 213
    int i_sin, i_cos;
    fetch_trigo( p_sys, &i_sin, &i_cos );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
214 215

    for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
216
    {
217 218 219 220 221
        plane_t *p_srcp = &p_pic->p[i_plane];
        plane_t *p_dstp = &p_outpic->p[i_plane];

        const int i_visible_lines = p_srcp->i_visible_lines;
        const int i_visible_pitch = p_srcp->i_visible_pitch;
222

hartman's avatar
hartman committed
223
        const int i_aspect = __MAX( 1, ( i_visible_lines * p_pic->p[Y_PLANE].i_visible_pitch ) / ( p_pic->p[Y_PLANE].i_visible_lines * i_visible_pitch ));
dionoea's avatar
dionoea committed
224 225
        /* = 2 for U and V planes in YUV 4:2:2, = 1 otherwise */

226 227 228 229 230
        const int i_line_center = i_visible_lines>>1;
        const int i_col_center  = i_visible_pitch>>1;

        const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;

dionoea's avatar
dionoea committed
231 232 233 234 235
        const int i_line_next =  i_cos / i_aspect -i_sin*i_visible_pitch;
        const int i_col_next  = -i_sin / i_aspect -i_cos*i_visible_pitch;
        int i_line_orig0 = ( - i_cos * i_line_center / i_aspect
                             - i_sin * i_col_center + (1<<11) );
        int i_col_orig0 =    i_sin * i_line_center / i_aspect
236
                           - i_cos * i_col_center + (1<<11);
237
        for( int y = 0; y < i_visible_lines; y++)
238
        {
239 240 241
            uint8_t *p_out = &p_dstp->p_pixels[y * p_dstp->i_pitch];

            for( int x = 0; x < i_visible_pitch; x++, p_out++ )
242
            {
dionoea's avatar
dionoea committed
243
                const int i_line_orig = (i_line_orig0>>12)*i_aspect + i_line_center;
244
                const int i_col_orig  = (i_col_orig0>>12)  + i_col_center;
245
                const uint8_t *p_orig_offset = &p_srcp->p_pixels[i_line_orig * p_srcp->i_pitch + i_col_orig];
246 247
                const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
                const uint8_t i_col_percent  = (i_col_orig0 >>4) & 255;
248

249 250
                if(    -1 <= i_line_orig && i_line_orig < i_visible_lines
                    && -1 <= i_col_orig  && i_col_orig  < i_visible_pitch )
251
                {
252 253 254 255 256 257
                #define test 1
                #undef test
                #ifdef test
                    if( ( i_col_orig > i_visible_pitch/2 ) )
                #endif
                    {
258 259 260 261 262 263 264 265 266 267
                        uint8_t i_curpix = black_pixel;
                        uint8_t i_colpix = black_pixel;
                        uint8_t i_linpix = black_pixel;
                        uint8_t i_nexpix = black_pixel;
                        if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
                            i_curpix = *p_orig_offset;
                        p_orig_offset++;

                        if(  ( i_col_orig < i_visible_pitch - 1)
                             && ( i_line_orig >= 0 ) )
dionoea's avatar
dionoea committed
268
                            i_colpix = *p_orig_offset;
269

270
                        p_orig_offset += p_srcp->i_pitch;
271 272
                        if( ( i_line_orig < i_visible_lines - 1)
                            && ( i_col_orig  < i_visible_pitch - 1) )
dionoea's avatar
dionoea committed
273
                            i_nexpix = *p_orig_offset;
274 275 276 277

                        p_orig_offset--;
                        if(  ( i_line_orig < i_visible_lines - 1)
                             && ( i_col_orig >= 0 ) )
dionoea's avatar
dionoea committed
278
                            i_linpix = *p_orig_offset;
279

280
                        unsigned int temp = 0;
281
                        temp+= i_curpix *
282
                            (256 - i_line_percent) * ( 256 - i_col_percent );
283
                        temp+= i_linpix *
284
                            i_line_percent * (256 - i_col_percent );
285
                        temp+= i_nexpix *
286
                            ( i_col_percent) * ( i_line_percent);
287
                        temp+= i_colpix *
288 289 290 291 292 293 294 295
                            i_col_percent * (256 - i_line_percent );
                        *p_out = temp >> 16;
                    }
                #ifdef test
                    else if (i_col_orig == i_visible_pitch/2 )
                    {   *p_out = black_pixel;
                    }
                    else
296
                        *p_out = *p_orig_offset;
297 298
                #endif
                #undef test
299 300 301
                }
                else
                {
302
                    *p_out = black_pixel;
303
                }
304 305 306

                i_line_orig0 += i_sin;
                i_col_orig0 += i_cos;
307
            }
308 309 310

            i_line_orig0 += i_line_next;
            i_col_orig0 += i_col_next;
311 312 313
        }
    }

dionoea's avatar
dionoea committed
314
    return CopyInfoAndRelease( p_outpic, p_pic );
315
}
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
/*****************************************************************************
 *
 *****************************************************************************/
static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
{
    picture_t *p_outpic;
    filter_sys_t *p_sys = p_filter->p_sys;

    if( !p_pic ) return NULL;

    int i_u_offset, i_v_offset, i_y_offset;

    if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
                             &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
    {
332
        msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
333
                  (char*)&(p_pic->format.i_chroma) );
334
        picture_Release( p_pic );
335 336 337
        return NULL;
    }

Laurent Aimar's avatar
Laurent Aimar committed
338
    p_outpic = filter_NewPicture( p_filter );
339 340
    if( !p_outpic )
    {
341
        picture_Release( p_pic );
342 343 344
        return NULL;
    }

345 346 347
    const int i_visible_pitch = p_pic->p->i_visible_pitch>>1; /* In fact it's i_visible_pixels */
    const int i_visible_lines = p_pic->p->i_visible_lines;

348 349 350
    const uint8_t *p_in   = p_pic->p->p_pixels+i_y_offset;
    const uint8_t *p_in_u = p_pic->p->p_pixels+i_u_offset;
    const uint8_t *p_in_v = p_pic->p->p_pixels+i_v_offset;
351
    const int i_in_pitch  = p_pic->p->i_pitch;
352 353 354 355

    uint8_t *p_out   = p_outpic->p->p_pixels+i_y_offset;
    uint8_t *p_out_u = p_outpic->p->p_pixels+i_u_offset;
    uint8_t *p_out_v = p_outpic->p->p_pixels+i_v_offset;
356
    const int i_out_pitch = p_outpic->p->i_pitch;
357 358 359 360

    const int i_line_center = i_visible_lines>>1;
    const int i_col_center  = i_visible_pitch>>1;

361
    if( p_sys->p_motion != NULL )
362
    {
363 364
        int i_angle = motion_get_angle( p_sys->p_motion );
        store_trigo( p_sys, i_angle / 20.f );
365 366
    }

367 368
    int i_sin, i_cos;
    fetch_trigo( p_sys, &i_sin, &i_cos );
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    int i_col, i_line;
    for( i_line = 0; i_line < i_visible_lines; i_line++ )
    {
        for( i_col = 0; i_col < i_visible_pitch; i_col++ )
        {
            int i_line_orig;
            int i_col_orig;
            /* Handle "1st Y", U and V */
            i_line_orig = i_line_center +
                ( ( i_sin * ( i_col - i_col_center )
                  + i_cos * ( i_line - i_line_center ) )>>12 );
            i_col_orig = i_col_center +
                ( ( i_cos * ( i_col - i_col_center )
                  - i_sin * ( i_line - i_line_center ) )>>12 );
            if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
             && 0 <= i_line_orig && i_line_orig < i_visible_lines )
            {
387
                p_out[i_line*i_out_pitch+2*i_col] = p_in[i_line_orig*i_in_pitch+2*i_col_orig];
388
                i_col_orig /= 2;
389 390
                p_out_u[i_line*i_out_pitch+2*i_col] = p_in_u[i_line_orig*i_in_pitch+4*i_col_orig];
                p_out_v[i_line*i_out_pitch+2*i_col] = p_in_v[i_line_orig*i_in_pitch+4*i_col_orig];
391 392 393
            }
            else
            {
394 395 396
                p_out[i_line*i_out_pitch+2*i_col] = 0x00;
                p_out_u[i_line*i_out_pitch+2*i_col] = 0x80;
                p_out_v[i_line*i_out_pitch+2*i_col] = 0x80;
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
            }

            /* Handle "2nd Y" */
            i_col++;
            if( i_col >= i_visible_pitch )
                break;

            i_line_orig = i_line_center +
                ( ( i_sin * ( i_col - i_col_center )
                  + i_cos * ( i_line - i_line_center ) )>>12 );
            i_col_orig = i_col_center +
                ( ( i_cos * ( i_col - i_col_center )
                  - i_sin * ( i_line - i_line_center ) )>>12 );
            if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
             && 0 <= i_line_orig && i_line_orig < i_visible_lines )
            {
413
                p_out[i_line*i_out_pitch+2*i_col] = p_in[i_line_orig*i_in_pitch+2*i_col_orig];
414 415 416
            }
            else
            {
417
                p_out[i_line*i_out_pitch+2*i_col] = 0x00;
418 419 420
            }
        }
    }
421

dionoea's avatar
dionoea committed
422
    return CopyInfoAndRelease( p_outpic, p_pic );
423
}
424

425
/*****************************************************************************
426
 * Angle modification callback.
427
 *****************************************************************************/
428
static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
429
                           vlc_value_t oldval, vlc_value_t newval, void *data )
430
{
431
    filter_sys_t *p_sys = data;
432

433 434
    store_trigo( p_sys, newval.f_float );
    (void) p_this; (void) psz_var; (void) oldval;
435 436
    return VLC_SUCCESS;
}