erase.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:16k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * erase.c : logo erase video filter
  3.  *****************************************************************************
  4.  * Copyright (C) 2007 the VideoLAN team
  5.  * $Id: 982858473b4652d2d77cd478aa6ed5c35e27b46a $
  6.  *
  7.  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_plugin.h>
  31. #include <vlc_sout.h>
  32. #include <vlc_vout.h>
  33. #include "vlc_image.h"
  34. #include "vlc_filter.h"
  35. #include "filter_picture.h"
  36. /*****************************************************************************
  37.  * Local prototypes
  38.  *****************************************************************************/
  39. static int  Create    ( vlc_object_t * );
  40. static void Destroy   ( vlc_object_t * );
  41. static picture_t *Filter( filter_t *, picture_t * );
  42. static void FilterErase( filter_t *, picture_t *, picture_t * );
  43. static int EraseCallback( vlc_object_t *, char const *,
  44.                           vlc_value_t, vlc_value_t, void * );
  45. /*****************************************************************************
  46.  * Module descriptor
  47.  *****************************************************************************/
  48. #define MASK_TEXT N_("Image mask")
  49. #define MASK_LONGTEXT N_("Image mask. Pixels with an alpha value greater than 50% will be erased.")
  50. #define POSX_TEXT N_("X coordinate")
  51. #define POSX_LONGTEXT N_("X coordinate of the mask.")
  52. #define POSY_TEXT N_("Y coordinate")
  53. #define POSY_LONGTEXT N_("Y coordinate of the mask.")
  54. #define CFG_PREFIX "erase-"
  55. vlc_module_begin ()
  56.     set_description( N_("Erase video filter") )
  57.     set_shortname( N_( "Erase" ))
  58.     set_capability( "video filter2", 0 )
  59.     set_category( CAT_VIDEO )
  60.     set_subcategory( SUBCAT_VIDEO_VFILTER )
  61.     add_file( CFG_PREFIX "mask", NULL, NULL,
  62.               MASK_TEXT, MASK_LONGTEXT, false )
  63.     add_integer( CFG_PREFIX "x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, false )
  64.     add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, false )
  65.     add_shortcut( "erase" )
  66.     set_callbacks( Create, Destroy )
  67. vlc_module_end ()
  68. static const char *const ppsz_filter_options[] = {
  69.     "mask", "x", "y", NULL
  70. };
  71. /*****************************************************************************
  72.  * filter_sys_t
  73.  *****************************************************************************/
  74. struct filter_sys_t
  75. {
  76.     int i_x;
  77.     int i_y;
  78.     picture_t *p_mask;
  79.     vlc_mutex_t lock;
  80. };
  81. static void LoadMask( filter_t *p_filter, const char *psz_filename )
  82. {
  83.     image_handler_t *p_image;
  84.     video_format_t fmt_in, fmt_out;
  85.     picture_t *p_old_mask = p_filter->p_sys->p_mask;
  86.     memset( &fmt_in, 0, sizeof( video_format_t ) );
  87.     memset( &fmt_out, 0, sizeof( video_format_t ) );
  88.     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
  89.     p_image = image_HandlerCreate( p_filter );
  90.     p_filter->p_sys->p_mask =
  91.         image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
  92.     if( p_filter->p_sys->p_mask )
  93.     {
  94.         if( p_old_mask )
  95.             picture_Release( p_old_mask );
  96.     }
  97.     else if( p_old_mask )
  98.     {
  99.         p_filter->p_sys->p_mask = p_old_mask;
  100.         msg_Err( p_filter, "Error while loading new mask. Keeping old mask." );
  101.     }
  102.     else
  103.         msg_Err( p_filter, "Error while loading new mask. No mask available." );
  104.     image_HandlerDelete( p_image );
  105. }
  106. /*****************************************************************************
  107.  * Create
  108.  *****************************************************************************/
  109. static int Create( vlc_object_t *p_this )
  110. {
  111.     filter_t *p_filter = (filter_t *)p_this;
  112.     filter_sys_t *p_sys;
  113.     char *psz_filename;
  114.     switch( p_filter->fmt_in.video.i_chroma )
  115.     {
  116.         case VLC_FOURCC('I','4','2','0'):
  117.         case VLC_FOURCC('I','Y','U','V'):
  118.         case VLC_FOURCC('J','4','2','0'):
  119.         case VLC_FOURCC('Y','V','1','2'):
  120.         case VLC_FOURCC('I','4','2','2'):
  121.         case VLC_FOURCC('J','4','2','2'):
  122.             break;
  123.         default:
  124.             msg_Err( p_filter, "Unsupported input chroma (%4s)",
  125.                      (char*)&(p_filter->fmt_in.video.i_chroma) );
  126.             return VLC_EGENERIC;
  127.     }
  128.     /* Allocate structure */
  129.     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
  130.     if( p_filter->p_sys == NULL )
  131.         return VLC_ENOMEM;
  132.     p_sys = p_filter->p_sys;
  133.     p_filter->pf_video_filter = Filter;
  134.     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
  135.                        p_filter->p_cfg );
  136.     psz_filename =
  137.         var_CreateGetNonEmptyStringCommand( p_filter, CFG_PREFIX "mask" );
  138.     if( !psz_filename )
  139.     {
  140.         msg_Err( p_filter, "Missing 'mask' option value." );
  141.         free( p_sys );
  142.         return VLC_EGENERIC;
  143.     }
  144.     p_sys->p_mask = NULL;
  145.     LoadMask( p_filter, psz_filename );
  146.     free( psz_filename );
  147.     p_sys->i_x = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "x" );
  148.     p_sys->i_y = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "y" );
  149.     vlc_mutex_init( &p_sys->lock );
  150.     var_AddCallback( p_filter, CFG_PREFIX "x", EraseCallback, p_sys );
  151.     var_AddCallback( p_filter, CFG_PREFIX "y", EraseCallback, p_sys );
  152.     var_AddCallback( p_filter, CFG_PREFIX "mask", EraseCallback, p_sys );
  153.     return VLC_SUCCESS;
  154. }
  155. /*****************************************************************************
  156.  * Destroy
  157.  *****************************************************************************/
  158. static void Destroy( vlc_object_t *p_this )
  159. {
  160.     filter_t *p_filter = (filter_t *)p_this;
  161.     filter_sys_t *p_sys = p_filter->p_sys;
  162.     if( p_sys->p_mask )
  163.         picture_Release( p_sys->p_mask );
  164.     var_DelCallback( p_filter, CFG_PREFIX "x", EraseCallback, p_sys );
  165.     var_DelCallback( p_filter, CFG_PREFIX "y", EraseCallback, p_sys );
  166.     var_DelCallback( p_filter, CFG_PREFIX "mask", EraseCallback, p_sys );
  167.     vlc_mutex_destroy( &p_sys->lock );
  168.     free( p_filter->p_sys );
  169. }
  170. /*****************************************************************************
  171.  * Filter
  172.  *****************************************************************************/
  173. static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
  174. {
  175.     picture_t *p_outpic;
  176.     filter_sys_t *p_sys = p_filter->p_sys;
  177.     if( !p_pic ) return NULL;
  178.     p_outpic = filter_NewPicture( p_filter );
  179.     if( !p_outpic )
  180.     {
  181.         picture_Release( p_pic );
  182.         return NULL;
  183.     }
  184.     /* If the mask is empty: just copy the image */
  185.     vlc_mutex_lock( &p_sys->lock );
  186.     if( p_sys->p_mask )
  187.         FilterErase( p_filter, p_pic, p_outpic );
  188.     else
  189.         picture_CopyPixels( p_outpic, p_pic );
  190.     vlc_mutex_unlock( &p_sys->lock );
  191.     return CopyInfoAndRelease( p_outpic, p_pic );
  192. }
  193. /*****************************************************************************
  194.  * FilterErase
  195.  *****************************************************************************/
  196. static void FilterErase( filter_t *p_filter, picture_t *p_inpic,
  197.                                              picture_t *p_outpic )
  198. {
  199.     filter_sys_t *p_sys = p_filter->p_sys;
  200.     const int i_mask_pitch = p_sys->p_mask->A_PITCH;
  201.     const int i_mask_visible_pitch = p_sys->p_mask->p[A_PLANE].i_visible_pitch;
  202.     const int i_mask_visible_lines = p_sys->p_mask->p[A_PLANE].i_visible_lines;
  203.     for( int i_plane = 0; i_plane < p_inpic->i_planes; i_plane++ )
  204.     {
  205.         const int i_pitch = p_inpic->p[i_plane].i_pitch;
  206.         const int i_2pitch = i_pitch<<1;
  207.         const int i_visible_pitch = p_inpic->p[i_plane].i_visible_pitch;
  208.         const int i_lines = p_inpic->p[i_plane].i_lines;
  209.         const int i_visible_lines = p_inpic->p[i_plane].i_visible_lines;
  210.         uint8_t *p_inpix = p_inpic->p[i_plane].p_pixels;
  211.         uint8_t *p_outpix = p_outpic->p[i_plane].p_pixels;
  212.         uint8_t *p_mask = p_sys->p_mask->A_PIXELS;
  213.         int i_x = p_sys->i_x, i_y = p_sys->i_y;
  214.         int x, y;
  215.         int i_height = i_mask_visible_lines;
  216.         int i_width  = i_mask_visible_pitch;
  217.         const bool b_line_factor = ( i_plane /* U_PLANE or V_PLANE */ &&
  218.             !( p_inpic->format.i_chroma == VLC_FOURCC('I','4','2','2')
  219.             || p_inpic->format.i_chroma == VLC_FOURCC('J','4','2','2') ) );
  220.         if( i_plane ) /* U_PLANE or V_PLANE */
  221.         {
  222.             i_width  >>= 1;
  223.             i_x      >>= 1;
  224.         }
  225.         if( b_line_factor )
  226.         {
  227.             i_height >>= 1;
  228.             i_y      >>= 1;
  229.         }
  230.         i_height = __MIN( i_visible_lines - i_y, i_height );
  231.         i_width  = __MIN( i_visible_pitch - i_x, i_width  );
  232.         /* Copy original pixel buffer */
  233.         vlc_memcpy( p_outpix, p_inpix, i_pitch * i_lines );
  234.         /* Horizontal linear interpolation of masked areas */
  235.         p_outpix = p_outpic->p[i_plane].p_pixels + i_y*i_pitch + i_x;
  236.         for( y = 0; y < i_height;
  237.              y++, p_mask += i_mask_pitch, p_outpix += i_pitch )
  238.         {
  239.             uint8_t prev, next = 0;
  240.             int prev_x = -1, next_x = -2;
  241.             int quot = 0;
  242.             /* Find a suitable value for the previous color to use when
  243.              * interpoling a masked pixel's value */
  244.             if( i_x )
  245.             {
  246.                 /* There are pixels before current position on the same line.
  247.                  * Use those */
  248.                 prev = *(p_outpix-1);
  249.             }
  250.             else if( y || i_y )
  251.             {
  252.                 /* This is the first pixel on a line but there other lines
  253.                  * above us. Use the pixel right above */
  254.                 prev = *(p_outpix-i_pitch);
  255.             }
  256.             else
  257.             {
  258.                 /* We're in the upper left corner. This sucks. We can't use
  259.                  * any previous value, so we'll use a dummy one. In most
  260.                  * cases this dummy value will be fixed later on in the
  261.                  * algorithm */
  262.                 prev = 0xff;
  263.             }
  264.             for( x = 0; x < i_width; x++ )
  265.             {
  266.                 if( p_mask[i_plane?x<<1:x] > 127 )
  267.                 {
  268.                     /* This is a masked pixel */
  269.                     if( next_x <= prev_x )
  270.                     {
  271.                         int x0;
  272.                         /* Look for the next non masked pixel on the same
  273.                          * line (inside the mask's bounding box) */
  274.                         for( x0 = x; x0 < i_width; x0++ )
  275.                         {
  276.                             if( p_mask[i_plane?x0<<1:x0] <= 127 )
  277.                             {
  278.                                 /* We found an unmasked pixel. Victory! */
  279.                                 next_x = x0;
  280.                                 next = p_outpix[x0];
  281.                                 break;
  282.                             }
  283.                         }
  284.                         if( next_x <= prev_x )
  285.                         {
  286.                             /* We didn't find an unmasked pixel yet. Try
  287.                              * harder */
  288.                             if( x0 == x ) x0++;
  289.                             if( x0 < i_visible_pitch )
  290.                             {
  291.                                 /* If we didn't find a non masked pixel on the
  292.                                  * same line inside the mask's bounding box,
  293.                                  * use the next pixel on the line (except if
  294.                                  * it doesn't exist) */
  295.                                 next_x = x0;
  296.                                 next = p_outpix[x0];
  297.                             }
  298.                             else
  299.                             {
  300.                                 /* The last pixel on the line is masked,
  301.                                  * so we'll use the "prev" value. A better
  302.                                  * approach would be to use unmasked pixels
  303.                                  * at the end of adjacent lines */
  304.                                 next_x = x0;
  305.                                 next = prev;
  306.                             }
  307.                         }
  308.                         if( !( i_x || y || i_y ) )
  309.                             /* We were unable to find a suitable value for
  310.                              * the previous color (which means that we are
  311.                              * on the first line in the upper left corner)
  312.                              */
  313.                             prev = next;
  314.                         /* Divide only once instead of next_x-prev_x-1 times */
  315.                         quot = ((next-prev)<<16)/(next_x-prev_x);
  316.                     }
  317.                     /* Interpolate new value, and round correctly */
  318.                     p_outpix[x] = prev + (((x-prev_x)*quot+(1<<16))>>16);
  319.                 }
  320.                 else
  321.                 {
  322.                     /* This pixel isn't masked. It's thus suitable as a
  323.                      * previous color for the next interpolation */
  324.                     prev = p_outpix[x];
  325.                     prev_x = x;
  326.                 }
  327.             }
  328.         }
  329.         /* Vertical bluring */
  330.         p_mask = p_sys->p_mask->A_PIXELS;
  331.         i_height = b_line_factor ? i_mask_visible_lines>>1
  332.                                  : i_mask_visible_lines;
  333.         /* Make sure that we stop at least 2 lines before the picture's end
  334.          * (since our bluring algorithm uses the 2 next lines) */
  335.         i_height = __MIN( i_visible_lines - i_y - 2, i_height );
  336.         /* Make sure that we start at least 2 lines from the top (since our
  337.          * bluring algorithm uses the 2 previous lines) */
  338.         y = __MAX(i_y,2);
  339.         p_outpix = p_outpic->p[i_plane].p_pixels + (i_y+y)*i_pitch + i_x;
  340.         for( ; y < i_height; y++, p_mask += i_mask_pitch, p_outpix += i_pitch )
  341.         {
  342.             for( x = 0; x < i_width; x++ )
  343.             {
  344.                 if( p_mask[i_plane?x<<1:x] > 127 )
  345.                 {
  346.                     /* Ugly bluring function */
  347.                     p_outpix[x] =
  348.                         ( (p_outpix[x-i_2pitch]<<1)       /* 2 */
  349.                         + (p_outpix[x-i_pitch ]<<2)       /* 4 */
  350.                         + (p_outpix[x         ]<<2)       /* 4 */
  351.                         + (p_outpix[x+i_pitch ]<<2)       /* 4 */
  352.                         + (p_outpix[x+i_2pitch]<<1) )>>4; /* 2 */
  353.                 }
  354.             }
  355.         }
  356.     }
  357. }
  358. static int EraseCallback( vlc_object_t *p_this, char const *psz_var,
  359.                           vlc_value_t oldval, vlc_value_t newval, void *p_data )
  360. {
  361.     VLC_UNUSED(oldval);
  362.     filter_sys_t *p_sys = (filter_sys_t *)p_data;
  363.     if( !strcmp( psz_var, CFG_PREFIX "x" ) )
  364.     {
  365.         vlc_mutex_lock( &p_sys->lock );
  366.         p_sys->i_x = newval.i_int;
  367.         vlc_mutex_unlock( &p_sys->lock );
  368.     }
  369.     else if( !strcmp( psz_var, CFG_PREFIX "y" ) )
  370.     {
  371.         vlc_mutex_lock( &p_sys->lock );
  372.         p_sys->i_y = newval.i_int;
  373.         vlc_mutex_unlock( &p_sys->lock );
  374.     }
  375.     else if( !strcmp( psz_var, CFG_PREFIX "mask" ) )
  376.     {
  377.         vlc_mutex_lock( &p_sys->lock );
  378.         LoadMask( (filter_t*)p_this, newval.psz_string );
  379.         vlc_mutex_unlock( &p_sys->lock );
  380.     }
  381.     else
  382.     {
  383.         msg_Warn( p_this, "Unknown callback command." );
  384.     }
  385.     return VLC_SUCCESS;
  386. }