transform.c 30.1 KB
Newer Older
1
/*****************************************************************************
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
2
 * transform.c : transform image module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2000-2006 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12
 *
 * Authors: Samuel Hocevar <sam@zoy.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.
13
 *
14 15 16 17 18 19 20
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27
 *****************************************************************************/

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

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

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_vout.h>
35 36

#include "filter_common.h"
37
#include "filter_picture.h"
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

#define TRANSFORM_MODE_HFLIP   1
#define TRANSFORM_MODE_VFLIP   2
#define TRANSFORM_MODE_90      3
#define TRANSFORM_MODE_180     4
#define TRANSFORM_MODE_270     5

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

static int  Init      ( vout_thread_t * );
static void End       ( vout_thread_t * );
static void Render    ( vout_thread_t *, picture_t * );

55
static void FilterPlanar( vout_thread_t *, const picture_t *, picture_t * );
56
static void FilterI422( vout_thread_t *, const picture_t *, picture_t * );
57 58
static void FilterYUYV( vout_thread_t *, const picture_t *, picture_t * );

59 60 61
static int  SendEvents( vlc_object_t *, char const *,
                        vlc_value_t, vlc_value_t, void * );

62 63 64
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Christophe Massiot's avatar
Christophe Massiot committed
65
#define TYPE_TEXT N_("Transform type")
66 67
#define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")

68 69
static const char *const type_list[] = { "90", "180", "270", "hflip", "vflip" };
static const char *const type_list_text[] = { N_("Rotate by 90 degrees"),
Gildas Bazin's avatar
 
Gildas Bazin committed
70 71
  N_("Rotate by 180 degrees"), N_("Rotate by 270 degrees"),
  N_("Flip horizontally"), N_("Flip vertically") };
72

73 74
#define CFG_PREFIX "transform-"

75
vlc_module_begin();
76 77
    set_description( N_("Video transformation filter") );
    set_shortname( N_("Transformation"));
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
78
    set_capability( "video filter", 0 );
Clément Stenac's avatar
Clément Stenac committed
79 80
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VFILTER );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
81

82
    add_string( CFG_PREFIX "type", "90", NULL,
83
                          TYPE_TEXT, TYPE_LONGTEXT, false);
84
        change_string_list( type_list, type_list_text, 0);
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
85

86 87 88 89
    add_shortcut( "transform" );
    set_callbacks( Create, Destroy );
vlc_module_end();

90
static const char *const ppsz_filter_options[] = {
91 92 93
    "type", NULL
};

94 95 96 97 98 99 100 101 102
/*****************************************************************************
 * vout_sys_t: Transform video output method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the Transform specific properties of an output thread.
 *****************************************************************************/
struct vout_sys_t
{
    int i_mode;
103
    bool b_rotation;
104
    vout_thread_t *p_vout;
105 106

    void (*pf_filter)( vout_thread_t *, const picture_t *, picture_t * );
107 108
};

109 110 111 112 113 114 115 116
/*****************************************************************************
 * Control: control facility for the vout (forwards to child vout)
 *****************************************************************************/
static int Control( vout_thread_t *p_vout, int i_query, va_list args )
{
    return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
}

117 118 119 120 121 122 123 124 125 126 127 128 129
/*****************************************************************************
 * Create: allocates Transform video thread output method
 *****************************************************************************
 * This function allocates and initializes a Transform vout method.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
    char *psz_method;

    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
130
        return VLC_ENOMEM;
131 132 133 134 135 136

    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = NULL;
    p_vout->pf_render = Render;
    p_vout->pf_display = NULL;
137
    p_vout->pf_control = Control;
138

139 140 141
    config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
                           p_vout->p_cfg );

142
    /* Look what method was requested */
143
    psz_method = var_CreateGetNonEmptyString( p_vout, "transform-type" );
144

145 146
    switch( p_vout->fmt_in.i_chroma )
    {
147
        CASE_PLANAR_YUV_SQUARE
148 149 150 151
        case VLC_FOURCC('G','R','E','Y'):
            p_vout->p_sys->pf_filter = FilterPlanar;
            break;

152 153 154 155 156
        case VLC_FOURCC('I','4','2','2'):
        case VLC_FOURCC('J','4','2','2'):
            p_vout->p_sys->pf_filter = FilterI422;
            break;

157 158 159 160 161 162 163 164 165 166
        CASE_PACKED_YUV_422
            p_vout->p_sys->pf_filter = FilterYUYV;
            break;

        default:
            msg_Err( p_vout, "Unsupported chroma" );
            free( p_vout->p_sys );
            return VLC_EGENERIC;
    }

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
    if( psz_method == NULL )
    {
        msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
        msg_Err( p_vout, "no valid transform mode provided, using '90'" );
        p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
        p_vout->p_sys->b_rotation = 1;
    }
    else
    {
        if( !strcmp( psz_method, "hflip" ) )
        {
            p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
            p_vout->p_sys->b_rotation = 0;
        }
        else if( !strcmp( psz_method, "vflip" ) )
        {
            p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
            p_vout->p_sys->b_rotation = 0;
        }
        else if( !strcmp( psz_method, "90" ) )
        {
            p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
            p_vout->p_sys->b_rotation = 1;
        }
        else if( !strcmp( psz_method, "180" ) )
        {
            p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
            p_vout->p_sys->b_rotation = 0;
        }
        else if( !strcmp( psz_method, "270" ) )
        {
            p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
            p_vout->p_sys->b_rotation = 1;
        }
        else
        {
            msg_Err( p_vout, "no valid transform mode provided, using '90'" );
            p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
            p_vout->p_sys->b_rotation = 1;
        }

        free( psz_method );
    }
210

211
    return VLC_SUCCESS;
212 213 214 215 216 217 218 219 220
}

/*****************************************************************************
 * Init: initialize Transform video thread output method
 *****************************************************************************/
static int Init( vout_thread_t *p_vout )
{
    int i_index;
    picture_t *p_pic;
221
    video_format_t fmt;
222

223
    I_OUTPUTPICTURES = 0;
224
    memset( &fmt, 0, sizeof(video_format_t) );
225 226 227 228 229 230

    /* Initialize the output structure */
    p_vout->output.i_chroma = p_vout->render.i_chroma;
    p_vout->output.i_width  = p_vout->render.i_width;
    p_vout->output.i_height = p_vout->render.i_height;
    p_vout->output.i_aspect = p_vout->render.i_aspect;
231 232
    p_vout->fmt_out = p_vout->fmt_in;
    fmt = p_vout->fmt_out;
233

234 235 236 237 238
    /* Try to open the real video output */
    msg_Dbg( p_vout, "spawning the real video output" );

    if( p_vout->p_sys->b_rotation )
    {
239 240 241 242 243 244 245 246
        fmt.i_width = p_vout->fmt_out.i_height;
        fmt.i_visible_width = p_vout->fmt_out.i_visible_height;
        fmt.i_x_offset = p_vout->fmt_out.i_y_offset;

        fmt.i_height = p_vout->fmt_out.i_width;
        fmt.i_visible_height = p_vout->fmt_out.i_visible_width;
        fmt.i_y_offset = p_vout->fmt_out.i_x_offset;

247
        fmt.i_aspect = VOUT_ASPECT_FACTOR *
248 249 250 251
            (uint64_t)VOUT_ASPECT_FACTOR / fmt.i_aspect;

        fmt.i_sar_num = p_vout->fmt_out.i_sar_den;
        fmt.i_sar_den = p_vout->fmt_out.i_sar_num;
252 253
    }

254 255
    p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );

256 257 258 259
    /* Everything failed */
    if( p_vout->p_sys->p_vout == NULL )
    {
        msg_Err( p_vout, "cannot open vout, aborting" );
260
        return VLC_EGENERIC;
261
    }
262

263 264
    ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );

265 266
    ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );

Gildas Bazin's avatar
 
Gildas Bazin committed
267 268
    ADD_PARENT_CALLBACKS( SendEventsToChild );

269
    return VLC_SUCCESS;
270 271 272 273 274 275 276 277 278
}

/*****************************************************************************
 * End: terminate Transform video thread output method
 *****************************************************************************/
static void End( vout_thread_t *p_vout )
{
    int i_index;

279 280 281 282
    DEL_PARENT_CALLBACKS( SendEventsToChild );

    DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );

283 284 285 286 287 288
    /* Free the fake output buffers we allocated */
    for( i_index = I_OUTPUTPICTURES ; i_index ; )
    {
        i_index--;
        free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
    }
289

290
    vout_CloseAndRelease( p_vout->p_sys->p_vout );
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
}

/*****************************************************************************
 * Destroy: destroy Transform video thread output method
 *****************************************************************************
 * Terminate an output method created by TransformCreateOutputMethod
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

    free( p_vout->p_sys );
}

/*****************************************************************************
 * Render: displays previously rendered output
 *****************************************************************************
 * This function send the currently rendered image to Transform image, waits
 * until it is displayed and switch the two rendering buffers, preparing next
 * frame.
 *****************************************************************************/
static void Render( vout_thread_t *p_vout, picture_t *p_pic )
{
    picture_t *p_outpic;

    /* This is a new frame. Get a structure from the video_output. */
    while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
              == NULL )
    {
320
        if( !vlc_object_alive (p_vout) || p_vout->b_error )
321 322 323 324
        {
            return;
        }
        msleep( VOUT_OUTMEM_SLEEP );
325
    }
326 327 328 329

    vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
    vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );

330 331 332 333 334 335 336 337 338 339 340 341 342
    p_vout->p_sys->pf_filter( p_vout, p_pic, p_outpic );

    vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );

    vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
}

/*****************************************************************************
 * SendEvents: forward mouse and keyboard events to the parent p_vout
 *****************************************************************************/
static int SendEvents( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
{
Rafaël Carré's avatar
Rafaël Carré committed
343
    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 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
    vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
    vlc_value_t sentval = newval;

    /* Translate the mouse coordinates */
    if( !strcmp( psz_var, "mouse-x" ) )
    {
        switch( p_vout->p_sys->i_mode )
        {
        case TRANSFORM_MODE_270:
            sentval.i_int = p_vout->p_sys->p_vout->output.i_width
                             - sentval.i_int;
        case TRANSFORM_MODE_90:
            var_Set( p_vout, "mouse-y", sentval );
            return VLC_SUCCESS;

        case TRANSFORM_MODE_180:
        case TRANSFORM_MODE_HFLIP:
            sentval.i_int = p_vout->p_sys->p_vout->output.i_width
                             - sentval.i_int;
            break;

        case TRANSFORM_MODE_VFLIP:
        default:
            break;
        }
    }
    else if( !strcmp( psz_var, "mouse-y" ) )
    {
        switch( p_vout->p_sys->i_mode )
        {
        case TRANSFORM_MODE_90:
            sentval.i_int = p_vout->p_sys->p_vout->output.i_height
                             - sentval.i_int;
        case TRANSFORM_MODE_270:
            var_Set( p_vout, "mouse-x", sentval );
            return VLC_SUCCESS;

        case TRANSFORM_MODE_180:
        case TRANSFORM_MODE_VFLIP:
            sentval.i_int = p_vout->p_sys->p_vout->output.i_height
                             - sentval.i_int;
            break;

        case TRANSFORM_MODE_HFLIP:
        default:
            break;
        }
    }

    var_Set( p_vout, psz_var, sentval );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * SendEventsToChild: forward events to the child/children vout
 *****************************************************************************/
static int SendEventsToChild( 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
404
    VLC_UNUSED(p_data); VLC_UNUSED(oldval);
405 406 407 408 409 410 411 412 413
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
    var_Set( p_vout->p_sys->p_vout, psz_var, newval );
    return VLC_SUCCESS;
}

static void FilterPlanar( vout_thread_t *p_vout,
                          const picture_t *p_pic, picture_t *p_outpic )
{
    int i_index;
414 415 416 417 418 419 420
    switch( p_vout->p_sys->i_mode )
    {
        case TRANSFORM_MODE_90:
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
                int i_pitch = p_pic->p[i_index].i_pitch;

421
                uint8_t *p_in = p_pic->p[i_index].p_pixels;
422

423
                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
424 425 426
                uint8_t *p_out_end = p_out +
                    p_outpic->p[i_index].i_visible_lines *
                    p_outpic->p[i_index].i_pitch;
427 428 429

                for( ; p_out < p_out_end ; )
                {
430
                    uint8_t *p_line_end;
431

432 433
                    p_out_end -= p_outpic->p[i_index].i_pitch
                                  - p_outpic->p[i_index].i_visible_pitch;
434 435
                    p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
                        i_pitch;
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

                    for( ; p_in < p_line_end ; )
                    {
                        p_line_end -= i_pitch;
                        *(--p_out_end) = *p_line_end;
                    }

                    p_in++;
                }
            }
            break;

        case TRANSFORM_MODE_180:
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
451
                uint8_t *p_in = p_pic->p[i_index].p_pixels;
452
                uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
453
                                            * p_pic->p[i_index].i_pitch;
454

455
                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
456 457 458

                for( ; p_in < p_in_end ; )
                {
459 460
                    uint8_t *p_line_start = p_in_end
                                             - p_pic->p[i_index].i_pitch;
461 462 463 464 465 466 467 468 469 470
                    p_in_end -= p_pic->p[i_index].i_pitch
                                 - p_pic->p[i_index].i_visible_pitch;

                    for( ; p_line_start < p_in_end ; )
                    {
                        *p_out++ = *(--p_in_end);
                    }

                    p_out += p_outpic->p[i_index].i_pitch
                              - p_outpic->p[i_index].i_visible_pitch;
471 472 473 474 475 476 477 478 479
                }
            }
            break;

        case TRANSFORM_MODE_270:
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
                int i_pitch = p_pic->p[i_index].i_pitch;

480
                uint8_t *p_in = p_pic->p[i_index].p_pixels;
481

482
                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
483 484 485
                uint8_t *p_out_end = p_out +
                    p_outpic->p[i_index].i_visible_lines *
                    p_outpic->p[i_index].i_pitch;
486 487 488

                for( ; p_out < p_out_end ; )
                {
489
                    uint8_t *p_in_end;
490

491 492
                    p_in_end = p_in + p_pic->p[i_index].i_visible_lines *
                        i_pitch;
493 494 495 496 497 498 499

                    for( ; p_in < p_in_end ; )
                    {
                        p_in_end -= i_pitch;
                        *p_out++ = *p_in_end;
                    }

500 501
                    p_out += p_outpic->p[i_index].i_pitch
                              - p_outpic->p[i_index].i_visible_pitch;
502 503 504 505 506
                    p_in++;
                }
            }
            break;

507
        case TRANSFORM_MODE_HFLIP:
508 509
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
510
                uint8_t *p_in = p_pic->p[i_index].p_pixels;
511
                uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
512
                                            * p_pic->p[i_index].i_pitch;
513

514
                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
515 516 517 518

                for( ; p_in < p_in_end ; )
                {
                    p_in_end -= p_pic->p[i_index].i_pitch;
519 520
                    vlc_memcpy( p_out, p_in_end,
                                p_pic->p[i_index].i_visible_pitch );
521 522 523 524 525
                    p_out += p_pic->p[i_index].i_pitch;
                }
            }
            break;

526
        case TRANSFORM_MODE_VFLIP:
527 528
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
529
                uint8_t *p_in = p_pic->p[i_index].p_pixels;
530
                uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
531
                                         * p_pic->p[i_index].i_pitch;
532

533
                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
534 535 536

                for( ; p_in < p_in_end ; )
                {
537
                    uint8_t *p_line_end = p_in
538
                                        + p_pic->p[i_index].i_visible_pitch;
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

                    for( ; p_in < p_line_end ; )
                    {
                        *p_out++ = *(--p_line_end);
                    }

                    p_in += p_pic->p[i_index].i_pitch;
                }
            }
            break;

        default:
            break;
    }
}

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
static void FilterI422( vout_thread_t *p_vout,
                        const picture_t *p_pic, picture_t *p_outpic )
{
    int i_index;
    switch( p_vout->p_sys->i_mode )
    {
        case TRANSFORM_MODE_180:
        case TRANSFORM_MODE_HFLIP:
        case TRANSFORM_MODE_VFLIP:
            /* Fall back on the default implementation */
            FilterPlanar( p_vout, p_pic, p_outpic );
            return;

        case TRANSFORM_MODE_90:
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
                int i_pitch = p_pic->p[i_index].i_pitch;

                uint8_t *p_in = p_pic->p[i_index].p_pixels;

                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
                uint8_t *p_out_end = p_out +
                    p_outpic->p[i_index].i_visible_lines *
                    p_outpic->p[i_index].i_pitch;

                if( i_index == 0 )
                {
                    for( ; p_out < p_out_end ; )
                    {
                        uint8_t *p_line_end;

                        p_out_end -= p_outpic->p[i_index].i_pitch
                                      - p_outpic->p[i_index].i_visible_pitch;
                        p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
                            i_pitch;

                        for( ; p_in < p_line_end ; )
                        {
                            p_line_end -= i_pitch;
                            *(--p_out_end) = *p_line_end;
                        }

                        p_in++;
                    }
                }
                else /* i_index == 1 or 2 */
                {
                    for( ; p_out < p_out_end ; )
                    {
                        uint8_t *p_line_end, *p_out_end2;

                        p_out_end -= p_outpic->p[i_index].i_pitch
                                      - p_outpic->p[i_index].i_visible_pitch;
                        p_out_end2 = p_out_end - p_outpic->p[i_index].i_pitch;
                        p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
                            i_pitch;

                        for( ; p_in < p_line_end ; )
                        {
                            uint8_t p1, p2;

                            p_line_end -= i_pitch;
                            p1 = *p_line_end;
                            p_line_end -= i_pitch;
                            p2 = *p_line_end;

                            /* Trick for (x+y)/2 without overflow, based on
                             *   x + y == (x ^ y) + 2 * (x & y) */
                            *(--p_out_end) = (p1 & p2) + ((p1 ^ p2) / 2);
                            *(--p_out_end2) = (p1 & p2) + ((p1 ^ p2) / 2);
                        }

                        p_out_end = p_out_end2;
                        p_in++;
                    }
                }
            }
            break;

        case TRANSFORM_MODE_270:
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
                int i_pitch = p_pic->p[i_index].i_pitch;

                uint8_t *p_in = p_pic->p[i_index].p_pixels;

                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
                uint8_t *p_out_end = p_out +
                    p_outpic->p[i_index].i_visible_lines *
                    p_outpic->p[i_index].i_pitch;

                if( i_index == 0 )
                {
                    for( ; p_out < p_out_end ; )
                    {
                        uint8_t *p_in_end;

                        p_in_end = p_in + p_pic->p[i_index].i_visible_lines *
                            i_pitch;

                        for( ; p_in < p_in_end ; )
                        {
                            p_in_end -= i_pitch;
                            *p_out++ = *p_in_end;
                        }

                        p_out += p_outpic->p[i_index].i_pitch
                                  - p_outpic->p[i_index].i_visible_pitch;
                        p_in++;
                    }
                }
                else /* i_index == 1 or 2 */
                {
                    for( ; p_out < p_out_end ; )
                    {
                        uint8_t *p_in_end, *p_out2;

                        p_in_end = p_in + p_pic->p[i_index].i_visible_lines *
                            i_pitch;
                        p_out2 = p_out + p_outpic->p[i_index].i_pitch;

                        for( ; p_in < p_in_end ; )
                        {
                            uint8_t p1, p2;

                            p_in_end -= i_pitch;
                            p1 = *p_in_end;
                            p_in_end -= i_pitch;
                            p2 = *p_in_end;

                            /* Trick for (x+y)/2 without overflow, based on
                             *   x + y == (x ^ y) + 2 * (x & y) */
                            *p_out++ = (p1 & p2) + ((p1 ^ p2) / 2);
                            *p_out2++ = (p1 & p2) + ((p1 ^ p2) / 2);
                        }

                        p_out2 += p_outpic->p[i_index].i_pitch
                                   - p_outpic->p[i_index].i_visible_pitch;
                        p_out = p_out2;
                        p_in++;
                    }
                }
            }
            break;

        default:
            break;
    }
}

705 706
static void FilterYUYV( vout_thread_t *p_vout,
                        const picture_t *p_pic, picture_t *p_outpic )
707
{
708 709 710 711 712
    int i_index;
    int i_y_offset, i_u_offset, i_v_offset;
    if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
                             &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
        return;
713

714
    switch( p_vout->p_sys->i_mode )
715
    {
716 717 718 719 720
        case TRANSFORM_MODE_HFLIP:
            /* Fall back on the default implementation */
            FilterPlanar( p_vout, p_pic, p_outpic );
            return;

721
        case TRANSFORM_MODE_90:
722 723 724
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
                int i_pitch = p_pic->p[i_index].i_pitch;
725

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
                uint8_t *p_in = p_pic->p[i_index].p_pixels;

                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
                uint8_t *p_out_end = p_out +
                    p_outpic->p[i_index].i_visible_lines *
                    p_outpic->p[i_index].i_pitch;

                int i_offset  = i_u_offset;
                int i_offset2 = i_v_offset;
                for( ; p_out < p_out_end ; )
                {
                    uint8_t *p_line_end;

                    p_out_end -= p_outpic->p[i_index].i_pitch
                                  - p_outpic->p[i_index].i_visible_pitch;
                    p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
                        i_pitch;

                    for( ; p_in < p_line_end ; )
                    {
                        p_line_end -= i_pitch;
                        p_out_end -= 4;
                        p_out_end[i_y_offset+2] = p_line_end[i_y_offset];
                        p_out_end[i_u_offset] = p_line_end[i_offset];
                        p_line_end -= i_pitch;
                        p_out_end[i_y_offset] = p_line_end[i_y_offset];
                        p_out_end[i_v_offset] = p_line_end[i_offset2];
                    }

                    p_in += 2;

                    {
                        int a = i_offset;
                        i_offset = i_offset2;
                        i_offset2 = a;
                    }
                }
            }
764 765
            break;

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
        case TRANSFORM_MODE_180:
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
                uint8_t *p_in = p_pic->p[i_index].p_pixels;
                uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
                                            * p_pic->p[i_index].i_pitch;

                uint8_t *p_out = p_outpic->p[i_index].p_pixels;

                for( ; p_in < p_in_end ; )
                {
                    uint8_t *p_line_start = p_in_end
                                             - p_pic->p[i_index].i_pitch;
                    p_in_end -= p_pic->p[i_index].i_pitch
                                 - p_pic->p[i_index].i_visible_pitch;

                    for( ; p_line_start < p_in_end ; )
                    {
                        p_in_end -= 4;
                        p_out[i_y_offset] = p_in_end[i_y_offset+2];
                        p_out[i_u_offset] = p_in_end[i_u_offset];
                        p_out[i_y_offset+2] = p_in_end[i_y_offset];
                        p_out[i_v_offset] = p_in_end[i_v_offset];
                        p_out += 4;
                    }

                    p_out += p_outpic->p[i_index].i_pitch
                              - p_outpic->p[i_index].i_visible_pitch;
                }
            }
796
            break;
797

798
        case TRANSFORM_MODE_270:
799 800 801
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
                int i_pitch = p_pic->p[i_index].i_pitch;
802

803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
                uint8_t *p_in = p_pic->p[i_index].p_pixels;

                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
                uint8_t *p_out_end = p_out +
                    p_outpic->p[i_index].i_visible_lines *
                    p_outpic->p[i_index].i_pitch;

                int i_offset  = i_u_offset;
                int i_offset2 = i_v_offset;
                for( ; p_out < p_out_end ; )
                {
                    uint8_t *p_in_end;

                    p_in_end = p_in
                             + p_pic->p[i_index].i_visible_lines * i_pitch;

                    for( ; p_in < p_in_end ; )
                    {
                        p_in_end -= i_pitch;
                        p_out[i_y_offset] = p_in_end[i_y_offset];
                        p_out[i_u_offset] = p_in_end[i_offset];
                        p_in_end -= i_pitch;
                        p_out[i_y_offset+2] = p_in_end[i_y_offset];
                        p_out[i_v_offset] = p_in_end[i_offset2];
                        p_out += 4;
                    }

                    p_out += p_outpic->p[i_index].i_pitch
                           - p_outpic->p[i_index].i_visible_pitch;
                    p_in += 2;

                    {
                        int a = i_offset;
                        i_offset = i_offset2;
                        i_offset2 = a;
                    }
                }
            }
841 842
            break;

843 844 845 846 847 848
        case TRANSFORM_MODE_VFLIP:
            for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
            {
                uint8_t *p_in = p_pic->p[i_index].p_pixels;
                uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
                                         * p_pic->p[i_index].i_pitch;
849

850
                uint8_t *p_out = p_outpic->p[i_index].p_pixels;
851

852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
                for( ; p_in < p_in_end ; )
                {
                    uint8_t *p_line_end = p_in
                                        + p_pic->p[i_index].i_visible_pitch;

                    for( ; p_in < p_line_end ; )
                    {
                        p_line_end -= 4;
                        p_out[i_y_offset] = p_line_end[i_y_offset+2];
                        p_out[i_u_offset] = p_line_end[i_u_offset];
                        p_out[i_y_offset+2] = p_line_end[i_y_offset];
                        p_out[i_v_offset] = p_line_end[i_v_offset];
                        p_out += 4;
                    }

                    p_in += p_pic->p[i_index].i_pitch;
                }
            }
            break;

        default:
            break;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
875
}