crop.c 32.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * crop.c : Crop video plugin for vlc
 *****************************************************************************
4
 * Copyright (C) 2002, 2003 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
8 9
 *          mod by Cedric Cocquebert <Cedric.Cocquebert@supelec.fr>
 *          based of DScaler idea (M. Samblanet)
10 11 12 13 14
 *
 * 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.
15
 *
16 17 18 19 20 21 22
 * 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
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29
 *****************************************************************************/

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

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

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
36 37
#include <vlc_vout.h>
#include <vlc_interface.h>
38 39 40

#include "filter_common.h"

41 42
#define BEST_AUTOCROP 1
#ifdef BEST_AUTOCROP
43
    #define RATIO_MAX 15000  // 10*4/3 for a 360
44 45
#endif

46 47 48 49 50 51 52 53 54 55 56 57 58
/*****************************************************************************
 * 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 int  Manage    ( vout_thread_t * );
static void Render    ( vout_thread_t *, picture_t * );

static void UpdateStats    ( vout_thread_t *, picture_t * );

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

62 63 64 65 66 67 68 69
#ifdef BEST_AUTOCROP
/*****************************************************************************
 * Callback prototypes
 *****************************************************************************/
static int FilterCallback ( vlc_object_t *, char const *,
                            vlc_value_t, vlc_value_t, void * );
#endif

70 71 72
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
73
#define GEOMETRY_TEXT N_("Crop geometry (pixels)")
74
#define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop. This is set as <width> x <height> + <left offset> + <top offset>.")
75

Christophe Massiot's avatar
Christophe Massiot committed
76
#define AUTOCROP_TEXT N_("Automatic cropping")
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
#define AUTOCROP_LONGTEXT N_("Automatically detect black borders and crop them.")

#ifdef BEST_AUTOCROP
#define RATIOMAX_TEXT N_("Ratio max (x 1000)")
#define RATIOMAX_LONGTEXT N_("Maximum image ratio. The crop plugin will never automatically crop to a higher ratio (ie, to a more \"flat\" image). The value is x1000: 1333 means 4/3.")

#define RATIO_TEXT N_("Manual ratio")
#define RATIO_LONGTEXT N_("Force a ratio (0 for automatic). Value is x1000: 1333 means 4/3.")

#define TIME_TEXT N_("Number of images for change")
#define TIME_LONGTEXT N_("The number of consecutive images with the same detected ratio (different from the previously detected ratio) to consider that ratio chnged and trigger recrop.")

#define DIFF_TEXT N_("Number of lines for change")
#define DIFF_LONGTEXT N_("The minimum difference in the number of detected black lines to consider that ratio changed and trigger recrop.")

#define NBP_TEXT N_("Number of non black pixels ")
#define NBP_LONGTEXT N_("The maximum of non-black pixels in a line to consider"\
                        " that the line is black.")

#define SKIP_TEXT N_("Skip percentage (%)")
#define SKIP_LONGTEXT N_("Percentage of the line to consider while checking for black lines. This allows to skip logos in black borders and crop them anyway.")

#define LUM_TEXT N_("Luminance threshold ")
#define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
#endif
102 103

vlc_module_begin();
104 105
    set_description( N_("Crop video filter") );
    set_shortname( N_("Crop" ));
Clément Stenac's avatar
Clément Stenac committed
106 107
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VFILTER );
108
    set_capability( "video filter", 0 );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
109

110
    add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT,
111
                                             GEOMETRY_LONGTEXT, false );
112
    add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT,
113
                                   AUTOCROP_LONGTEXT, false );
114 115 116

#ifdef BEST_AUTOCROP
    add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX, NULL,
117
                            RATIOMAX_TEXT, RATIOMAX_LONGTEXT, true );
118 119

    add_integer_with_range( "crop-ratio", 0, 0, RATIO_MAX, NULL, RATIO_TEXT,
120
                            RATIO_LONGTEXT, false );
121
    add_integer( "autocrop-time", 25, NULL, TIME_TEXT,
122
                 TIME_LONGTEXT, true );
123
    add_integer( "autocrop-diff", 16, NULL, DIFF_TEXT,
124
                                            DIFF_LONGTEXT, true );
125 126

    add_integer( "autocrop-non-black-pixels", 3, NULL,
127
                 NBP_TEXT, NBP_LONGTEXT, true );
128 129

    add_integer_with_range( "autocrop-skip-percent", 17, 0, 100, NULL,
130
                            SKIP_TEXT, SKIP_LONGTEXT, true );
131 132

    add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128, NULL,
133
                            LUM_TEXT, LUM_LONGTEXT, true );
134
#endif //BEST_AUTOCROP
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    add_shortcut( "crop" );
    set_callbacks( Create, Destroy );
vlc_module_end();

/*****************************************************************************
 * vout_sys_t: Crop video output method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the Crop specific properties of an output thread.
 *****************************************************************************/
struct vout_sys_t
{
    vout_thread_t *p_vout;

    unsigned int i_x, i_y;
    unsigned int i_width, i_height, i_aspect;

153
    bool b_autocrop;
154 155 156

    /* Autocrop specific variables */
    unsigned int i_lastchange;
157
    bool   b_changed;
158 159 160 161 162 163
#ifdef BEST_AUTOCROP
    unsigned int i_ratio_max;
    unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
    unsigned int i_ratio;
#endif

164 165
};

166 167 168 169 170 171 172 173
/*****************************************************************************
 * 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 );
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187
/*****************************************************************************
 * Create: allocates Crop video thread output method
 *****************************************************************************
 * This function allocates and initializes a Crop vout method.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
        msg_Err( p_vout, "out of memory" );
188
        return VLC_ENOMEM;
189 190 191 192 193 194 195
    }

    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = Manage;
    p_vout->pf_render = Render;
    p_vout->pf_display = NULL;
196
    p_vout->pf_control = Control;
197

198
    return VLC_SUCCESS;
199 200 201 202 203 204 205 206 207 208
}

/*****************************************************************************
 * Init: initialize Crop video thread output method
 *****************************************************************************/
static int Init( vout_thread_t *p_vout )
{
    int   i_index;
    char *psz_var;
    picture_t *p_pic;
209
    video_format_t fmt;
210

211
    I_OUTPUTPICTURES = 0;
212
    memset( &fmt, 0, sizeof(video_format_t) );
213 214

    p_vout->p_sys->i_lastchange = 0;
215
    p_vout->p_sys->b_changed = false;
216 217 218 219 220 221

    /* 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;
222
    p_vout->fmt_out = p_vout->fmt_in;
223 224 225

    /* Shall we use autocrop ? */
    p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
#ifdef BEST_AUTOCROP
    p_vout->p_sys->i_ratio_max = config_GetInt( p_vout, "autocrop-ratio-max" );
    p_vout->p_sys->i_threshold =
                    config_GetInt( p_vout, "autocrop-luminance-threshold" );
    p_vout->p_sys->i_skipPercent =
                    config_GetInt( p_vout, "autocrop-skip-percent" );
    p_vout->p_sys->i_nonBlackPixel =
                    config_GetInt( p_vout, "autocrop-non-black-pixels" );
    p_vout->p_sys->i_diff = config_GetInt( p_vout, "autocrop-diff" );
    p_vout->p_sys->i_time = config_GetInt( p_vout, "autocrop-time" );
    vlc_value_t val={0};
    var_Get( p_vout, "ratio-crop", &val );
    val.psz_string = "0";
    var_SetString( p_vout, "ratio-crop", val.psz_string);

    if (p_vout->p_sys->b_autocrop)
        p_vout->p_sys->i_ratio = 0;
    else
    {
245
        p_vout->p_sys->i_ratio = config_GetInt( p_vout, "crop-ratio" );
246 247 248 249 250 251
        // ratio < width / height => ratio = 0 (unchange ratio)
        if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) / p_vout->output.i_height)
            p_vout->p_sys->i_ratio = 0;
    }
#endif

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 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

    /* Get geometry value from the user */
    psz_var = config_GetPsz( p_vout, "crop-geometry" );
    if( psz_var )
    {
        char *psz_parser, *psz_tmp;

        psz_parser = psz_tmp = psz_var;
        while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;

        if( *psz_tmp )
        {
            psz_tmp[0] = '\0';
            p_vout->p_sys->i_width = atoi( psz_parser );

            psz_parser = ++psz_tmp;
            while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;

            if( *psz_tmp )
            {
                psz_tmp[0] = '\0';
                p_vout->p_sys->i_height = atoi( psz_parser );

                psz_parser = ++psz_tmp;
                while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;

                if( *psz_tmp )
                {
                    psz_tmp[0] = '\0';
                    p_vout->p_sys->i_x = atoi( psz_parser );
                    p_vout->p_sys->i_y = atoi( ++psz_tmp );
                }
                else
                {
                    p_vout->p_sys->i_x = atoi( psz_parser );
                    p_vout->p_sys->i_y =
                     ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
                }
            }
            else
            {
                p_vout->p_sys->i_height = atoi( psz_parser );
                p_vout->p_sys->i_x =
                     ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
                p_vout->p_sys->i_y =
                     ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
            }
        }
        else
        {
            p_vout->p_sys->i_width = atoi( psz_parser );
            p_vout->p_sys->i_height = p_vout->output.i_height;
            p_vout->p_sys->i_x =
                     ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
            p_vout->p_sys->i_y =
                     ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
        }

        /* Check for validity */
        if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
                                                   > p_vout->output.i_width )
        {
            p_vout->p_sys->i_x = 0;
            if( p_vout->p_sys->i_width > p_vout->output.i_width )
            {
                p_vout->p_sys->i_width = p_vout->output.i_width;
            }
        }

        if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
                                                   > p_vout->output.i_height )
        {
            p_vout->p_sys->i_y = 0;
            if( p_vout->p_sys->i_height > p_vout->output.i_height )
            {
                p_vout->p_sys->i_height = p_vout->output.i_height;
            }
        }

        free( psz_var );
    }
    else
334 335 336 337 338 339 340 341 342 343 344 345 346 347
#ifdef BEST_AUTOCROP
    if (p_vout->p_sys->i_ratio)
    {
        p_vout->p_sys->i_aspect    =  p_vout->p_sys->i_ratio * 432;
        p_vout->p_sys->i_width  = p_vout->fmt_out.i_visible_width;
        p_vout->p_sys->i_height = p_vout->output.i_aspect
                                * p_vout->output.i_height / p_vout->p_sys->i_aspect
                                * p_vout->p_sys->i_width / p_vout->output.i_width;
        p_vout->p_sys->i_height += p_vout->p_sys->i_height % 2;
        p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
        p_vout->p_sys->i_y = (p_vout->output.i_height - p_vout->p_sys->i_height) / 2;
    }
    else
#endif
348
    {
349 350 351 352
        p_vout->p_sys->i_width  = p_vout->fmt_out.i_visible_width;
        p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
        p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
        p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
353 354 355 356 357 358 359 360
    }

    /* Pheeew. Parsing done. */
    msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
                     p_vout->p_sys->i_width, p_vout->p_sys->i_height,
                     p_vout->p_sys->i_x, p_vout->p_sys->i_y,
                     p_vout->p_sys->b_autocrop ? "" : "not " );
    /* Set current output image properties */
361 362 363
    p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
           * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
           * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
364

365 366 367
#ifdef BEST_AUTOCROP
    msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
#endif
368 369 370 371 372 373 374 375
    fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
    fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
    fmt.i_x_offset = fmt.i_y_offset = 0;
    fmt.i_chroma = p_vout->render.i_chroma;
    fmt.i_aspect = p_vout->p_sys->i_aspect;
    fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
    fmt.i_sar_den = VOUT_ASPECT_FACTOR;

376
    /* Try to open the real video output */
377
    p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
378 379 380
    if( p_vout->p_sys->p_vout == NULL )
    {
        msg_Err( p_vout, "failed to create vout" );
381
        intf_UserFatal( p_vout, false, _("Cropping failed"),
382
                        _("VLC could not open the video output module.") );
383
        return VLC_EGENERIC;
384 385 386 387
    }

    ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );

388 389
    ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );

390 391 392 393
#ifdef BEST_AUTOCROP
    var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
394 395
    ADD_PARENT_CALLBACKS( SendEventsToChild );

396
    return VLC_SUCCESS;
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
}

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

    /* Free the fake output buffers we allocated */
    for( i_index = I_OUTPUTPICTURES ; i_index ; )
    {
        i_index--;
        free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
    }
}

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

423 424 425 426
    if( p_vout->p_sys->p_vout )
    {
        DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
        vlc_object_detach( p_vout->p_sys->p_vout );
427
        vlc_object_release( p_vout->p_sys->p_vout );
428
    }
429

Gildas Bazin's avatar
 
Gildas Bazin committed
430 431
    DEL_PARENT_CALLBACKS( SendEventsToChild );

432 433 434 435 436 437 438 439 440 441 442
    free( p_vout->p_sys );
}

/*****************************************************************************
 * Manage: handle Crop events
 *****************************************************************************
 * This function should be called regularly by video output thread. It manages
 * console events. It returns a non null value on error.
 *****************************************************************************/
static int Manage( vout_thread_t *p_vout )
{
443
    video_format_t fmt;
444

445 446
    if( !p_vout->p_sys->b_changed )
    {
447
        return VLC_SUCCESS;
448 449
    }

450 451
    memset( &fmt, 0, sizeof(video_format_t) );

452 453 454 455 456 457 458 459 460
#ifdef BEST_AUTOCROP
    msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
                     p_vout->p_sys->i_width, p_vout->p_sys->i_height,
                     p_vout->p_sys->i_x, p_vout->p_sys->i_y,
                     p_vout->p_sys->b_autocrop ? "" : "not " );

    msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
#endif

461
    vlc_object_release( p_vout->p_sys->p_vout );
462

463 464 465 466 467 468 469 470 471
    fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
    fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
    fmt.i_x_offset = fmt.i_y_offset = 0;
    fmt.i_chroma = p_vout->render.i_chroma;
    fmt.i_aspect = p_vout->p_sys->i_aspect;
    fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
    fmt.i_sar_den = VOUT_ASPECT_FACTOR;

    p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
472 473 474
    if( p_vout->p_sys->p_vout == NULL )
    {
        msg_Err( p_vout, "failed to create vout" );
475
        intf_UserFatal( p_vout, false, _("Cropping failed"),
476
                        _("VLC could not open the video output module.") );
477
        return VLC_EGENERIC;
478 479
    }

480
    p_vout->p_sys->b_changed = false;
481 482
    p_vout->p_sys->i_lastchange = 0;

483
    return VLC_SUCCESS;
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
}

/*****************************************************************************
 * Render: display previously rendered output
 *****************************************************************************
 * This function sends the currently rendered image to Crop image, waits
 * until it is displayed and switches the two rendering buffers, preparing next
 * frame.
 *****************************************************************************/
static void Render( vout_thread_t *p_vout, picture_t *p_pic )
{
    picture_t *p_outpic = NULL;
    int i_plane;

    if( p_vout->p_sys->b_changed )
    {
        return;
    }

    while( ( p_outpic =
                 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
           ) == NULL )
    {
        if( p_vout->b_die || p_vout->b_error )
        {
            vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
            return;
        }

        msleep( VOUT_OUTMEM_SLEEP );
    }

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

    for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
    {
521
        uint8_t *p_in, *p_out, *p_out_end;
522 523
        int i_in_pitch = p_pic->p[i_plane].i_pitch;
        const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
524
        const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
525 526 527

        p_in = p_pic->p[i_plane].p_pixels
                /* Skip the right amount of lines */
528 529
                + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
                                 p_vout->p_sys->i_y / p_vout->output.i_height )
530 531 532 533
                /* Skip the right amount of columns */
                + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;

        p_out = p_outpic->p[i_plane].p_pixels;
534
        p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
535 536 537

        while( p_out < p_out_end )
        {
538
            vlc_memcpy( p_out, p_in, i_copy_pitch );
539 540 541 542 543 544 545 546 547
            p_in += i_in_pitch;
            p_out += i_out_pitch;
        }
    }

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

    /* The source image may still be in the cache ... parse it! */
548
    if( p_vout->p_sys->b_autocrop )
549
    {
550
        UpdateStats( p_vout, p_pic );
551 552 553
    }
}

554
#ifdef BEST_AUTOCROP
555
static bool NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
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
                               int i_visible_pitch, int i_lines,
                               int i_lumThreshold, int i_skipCountPercent,
                               int i_nonBlackPixel, int i_chroma)
{
    const int i_col = i_line * i_pitch / i_lines;
    int i_index, i_count = 0;
    int i_skipCount = 0;

    switch(i_chroma)
    {
    // planar YUV
        case VLC_FOURCC('I','4','4','4'):
        case VLC_FOURCC('I','4','2','2'):
        case VLC_FOURCC('I','4','2','0'):
        case VLC_FOURCC('Y','V','1','2'):
        case VLC_FOURCC('I','Y','U','V'):
        case VLC_FOURCC('I','4','1','1'):
        case VLC_FOURCC('I','4','1','0'):
        case VLC_FOURCC('Y','V','U','9'):
        case VLC_FOURCC('Y','U','V','A'):
            i_skipCount = (i_pitch * i_skipCountPercent) / 100;
            for (i_index = i_col/2 + i_skipCount/2;
                 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
                 i_index++)
            {
                 i_count += (p_in[i_index] > i_lumThreshold);
            if (i_count > i_nonBlackPixel) break;
            }
            break;
    // packed RGB
        case VLC_FOURCC('R','G','B','2'):    // packed by 1
            i_skipCount = (i_pitch * i_skipCountPercent) / 100;
            for (i_index = i_col/2 + i_skipCount/2;
                 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
                 i_index++)
            {
                 i_count += (p_in[i_index] > i_lumThreshold);
            if (i_count > i_nonBlackPixel) break;
            }
            break;
        case VLC_FOURCC('R','V','1','5'):    // packed by 2
        case VLC_FOURCC('R','V','1','6'):    // packed by 2
            i_skipCount = (i_pitch * i_skipCountPercent) / 100;
            for (i_index = i_col/2 + i_skipCount/2 -
                                (i_col/2 + i_skipCount/2) % 2;
                 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
                 i_index+=2)
            {
                 i_count += (p_in[i_index] > i_lumThreshold) &&
                            (p_in[i_index + 1] > i_lumThreshold);
            if (i_count > i_nonBlackPixel) break;
            }
            break;
        case VLC_FOURCC('R','V','2','4'):    // packed by 3
            i_skipCount = (i_pitch * i_skipCountPercent) / 100;
            for (i_index = i_col/2 + i_skipCount/2 - (i_col/2 + i_skipCount/2) % 3; i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2; i_index+=3)
            {
                 i_count += (p_in[i_index] > i_lumThreshold) &&
                            (p_in[i_index + 1] > i_lumThreshold) &&
                            (p_in[i_index + 2] > i_lumThreshold);
            if (i_count > i_nonBlackPixel) break;
            }
            break;
        case VLC_FOURCC('R','V','3','2'):    // packed by 4
            i_skipCount = (i_pitch * i_skipCountPercent) / 100;
            for (i_index = i_col/2 + i_skipCount/2 - (i_col/2 + i_skipCount/2) % 4; i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2; i_index+=4)
            {
                 i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
            if (i_count > i_nonBlackPixel) break;
            }
            break;
    // packed YUV
        case VLC_FOURCC('Y','U','Y','2'):    // packed by 2
        case VLC_FOURCC('Y','U','N','V'):    // packed by 2
        case VLC_FOURCC('U','Y','V','Y'):    // packed by 2
        case VLC_FOURCC('U','Y','N','V'):    // packed by 2
        case VLC_FOURCC('Y','4','2','2'):    // packed by 2
            i_skipCount = (i_pitch * i_skipCountPercent) / 100;
            for (i_index = (i_col/2 + i_skipCount/2) -
                           (i_col/2 + i_skipCount/2) % 2;
                 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
                 i_index+=2)
            {
                 i_count += (p_in[i_index] > i_lumThreshold);
            if (i_count > i_nonBlackPixel) break;
            }
            break;
        default :
            break;
    }
    return (i_count > i_nonBlackPixel);
}
#endif

650 651
static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
{
652
   uint8_t *p_in = p_pic->p[0].p_pixels;
653
    int i_pitch = p_pic->p[0].i_pitch;
654
    int i_visible_pitch = p_pic->p[0].i_visible_pitch;
655
    int i_lines = p_pic->p[0].i_visible_lines;
656
    int i_firstwhite = -1, i_lastwhite = -1, i;
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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
#ifdef BEST_AUTOCROP
    int i_time = p_vout->p_sys->i_time;
    int i_diff = p_vout->p_sys->i_diff;

    if (!p_vout->p_sys->i_ratio)
    {
        /* Determine where black borders are */
        for( i = 0 ; i < i_lines ; i++)
        {
                   if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
                            p_vout->p_sys->i_threshold,
                            p_vout->p_sys->i_skipPercent,
                            p_vout->p_sys->i_nonBlackPixel,
                            p_vout->output.i_chroma))
                {
                    i_firstwhite = i;
                    i_lastwhite = i_lines - i;
                    break;
                }
                p_in += i_pitch;
        }

        /* Decide whether it's worth changing the size */
        if( i_lastwhite == -1 )
        {
            p_vout->p_sys->i_lastchange = 0;
            return;
        }

        if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
        {
            p_vout->p_sys->i_lastchange = 0;
            return;
        }

        if (p_vout->output.i_aspect
                            * p_vout->output.i_height /
                                (i_lastwhite - i_firstwhite + 1)
                            * p_vout->p_sys->i_width /
                               p_vout->output.i_width >
                                    p_vout->p_sys->i_ratio_max * 432)
        {
            int i_height = ((p_vout->output.i_aspect / 432) *
                           p_vout->output.i_height * p_vout->p_sys->i_width) /
                          (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
            i_firstwhite = (p_vout->output.i_height - i_height) / 2;
            i_lastwhite =  p_vout->output.i_height - i_firstwhite;
/*
            p_vout->p_sys->i_lastchange = 0;
            return;
*/
        }

        if( (i_lastwhite - i_firstwhite) <
                        (int) (p_vout->p_sys->i_height + i_diff)
             && (i_lastwhite - i_firstwhite + i_diff) >
                        (int) p_vout->p_sys->i_height )
        {
            p_vout->p_sys->i_lastchange = 0;
            return;
        }

        /* We need at least 'i_time' images to make up our mind */
        p_vout->p_sys->i_lastchange++;
        if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
        {
            return;
        }
    }
    else
    {
        if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
        {
            p_vout->p_sys->i_aspect    =  p_vout->p_sys->i_ratio * 432;
            int i_height = p_vout->output.i_aspect
                                    * p_vout->output.i_height /
                                        p_vout->p_sys->i_aspect
                                    * p_vout->p_sys->i_width /
                                        p_vout->output.i_width;
            i_firstwhite = (p_vout->output.i_height - i_height) / 2;
            i_lastwhite =  p_vout->output.i_height - i_firstwhite;
        }
        else
        {
            return;
        }
    }
744

745
#else
746 747 748 749 750 751 752 753 754 755 756
    /* Determine where black borders are */
    switch( p_vout->output.i_chroma )
    {
    case VLC_FOURCC('I','4','2','0'):
        /* XXX: Do not laugh ! I know this is very naive. But it's just a
         *      proof of concept code snippet... */
        for( i = i_lines ; i-- ; )
        {
            const int i_col = i * i_pitch / i_lines;

            if( p_in[i_col/2] > 40
757 758
                 && p_in[i_visible_pitch/2] > 40
                 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
            {
                if( i_lastwhite == -1 )
                {
                    i_lastwhite = i;
                }
                i_firstwhite = i;
            }
            p_in += i_pitch;
        }
        break;

    default:
        break;
    }

    /* Decide whether it's worth changing the size */
    if( i_lastwhite == -1 )
    {
        p_vout->p_sys->i_lastchange = 0;
        return;
    }

781 782
    if( (unsigned int)(i_lastwhite - i_firstwhite)
                                           < p_vout->p_sys->i_height / 2 )
783 784 785 786 787
    {
        p_vout->p_sys->i_lastchange = 0;
        return;
    }

788 789 790 791
    if( (unsigned int)(i_lastwhite - i_firstwhite)
                                          < p_vout->p_sys->i_height + 16
         && (unsigned int)(i_lastwhite - i_firstwhite + 16)
                                                > p_vout->p_sys->i_height )
792 793 794 795 796 797 798 799 800 801 802
    {
        p_vout->p_sys->i_lastchange = 0;
        return;
    }

    /* We need at least 25 images to make up our mind */
    p_vout->p_sys->i_lastchange++;
    if( p_vout->p_sys->i_lastchange < 25 )
    {
        return;
    }
803
#endif //BEST_AUTOCROP
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818

    /* Tune a few values */
    if( i_firstwhite & 1 )
    {
        i_firstwhite--;
    }

    if( !(i_lastwhite & 1) )
    {
        i_lastwhite++;
    }

    /* Change size */
    p_vout->p_sys->i_y = i_firstwhite;
    p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
819 820 821 822 823
#ifdef BEST_AUTOCROP
    // check p_vout->p_sys->i_height <= p_vout->output.i_height
    if (p_vout->p_sys->i_height > p_vout->output.i_height)
        p_vout->p_sys->i_height = p_vout->output.i_height;
#endif
824 825 826 827 828

    p_vout->p_sys->i_aspect = p_vout->output.i_aspect
                            * p_vout->output.i_height / p_vout->p_sys->i_height
                            * p_vout->p_sys->i_width / p_vout->output.i_width;

829
    p_vout->p_sys->b_changed = true;
830
}
831

832 833 834 835 836 837
/*****************************************************************************
 * 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
838
    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
    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" ) )
    {
        sentval.i_int += p_vout->p_sys->i_x;
    }
    else if( !strcmp( psz_var, "mouse-y" ) )
    {
        sentval.i_int += p_vout->p_sys->i_y;
    }

    var_Set( p_vout, psz_var, sentval );

    return VLC_SUCCESS;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
857 858 859 860 861 862
/*****************************************************************************
 * 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
863
    VLC_UNUSED(p_data); VLC_UNUSED(oldval);
Gildas Bazin's avatar
 
Gildas Bazin committed
864 865 866 867
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
    var_Set( p_vout->p_sys->p_vout, psz_var, newval );
    return VLC_SUCCESS;
}
868 869 870 871 872 873 874 875 876

#ifdef BEST_AUTOCROP
/*****************************************************************************
 * FilterCallback: called when changing the ratio on the fly.
 *****************************************************************************/
static int FilterCallback( 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
877
    VLC_UNUSED(p_data); VLC_UNUSED(oldval);
878 879 880 881 882 883 884 885 886 887
    vout_thread_t * p_vout = (vout_thread_t *)p_this;

    if( !strcmp( psz_var, "ratio-crop" ) )
    {
        if ( !strcmp( newval.psz_string, "Auto" ) )
            p_vout->p_sys->i_ratio = 0;
        else
        {
            p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
            p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
888
            p_vout->p_sys->b_autocrop = true;
889 890 891 892 893 894 895 896 897 898 899 900 901 902
        }
        if (p_vout->p_sys->i_ratio)
        {
            if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
                                    p_vout->output.i_height)
                p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
                                    p_vout->output.i_height;
            if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
                p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;
        }
     }
    return VLC_SUCCESS;
}
#endif