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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * rotate.c : video rotation filter
  3.  *****************************************************************************
  4.  * Copyright (C) 2000-2008 the VideoLAN team
  5.  * $Id: c0f1965bfbdc8b704a505f75469d337e1539ce51 $
  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 <math.h>                                            /* sin(), cos() */
  30. #include <vlc_common.h>
  31. #include <vlc_plugin.h>
  32. #include <vlc_vout.h>
  33. #include "vlc_filter.h"
  34. #include "filter_picture.h"
  35. /*****************************************************************************
  36.  * Local prototypes
  37.  *****************************************************************************/
  38. static int  Create    ( vlc_object_t * );
  39. static void Destroy   ( vlc_object_t * );
  40. static picture_t *Filter( filter_t *, picture_t * );
  41. static picture_t *FilterPacked( filter_t *, picture_t * );
  42. static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
  43.                            vlc_value_t oldval, vlc_value_t newval,
  44.                            void *p_data );
  45. static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
  46.                            vlc_value_t oldval, vlc_value_t newval,
  47.                            void *p_data );
  48. #define ANGLE_TEXT N_("Angle in degrees")
  49. #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
  50. #define FILTER_PREFIX "rotate-"
  51. /*****************************************************************************
  52.  * Module descriptor
  53.  *****************************************************************************/
  54. vlc_module_begin ()
  55.     set_description( N_("Rotate video filter") )
  56.     set_shortname( N_( "Rotate" ))
  57.     set_capability( "video filter2", 0 )
  58.     set_category( CAT_VIDEO )
  59.     set_subcategory( SUBCAT_VIDEO_VFILTER )
  60.     add_integer_with_range( FILTER_PREFIX "angle", 30, 0, 359, NULL,
  61.         ANGLE_TEXT, ANGLE_LONGTEXT, false )
  62.     add_shortcut( "rotate" )
  63.     set_callbacks( Create, Destroy )
  64. vlc_module_end ()
  65. static const char *const ppsz_filter_options[] = {
  66.     "angle", NULL
  67. };
  68. /*****************************************************************************
  69.  * filter_sys_t
  70.  *****************************************************************************/
  71. struct filter_sys_t
  72. {
  73.     vlc_spinlock_t lock;
  74.     int            i_cos;
  75.     int            i_sin;
  76. };
  77. static inline void cache_trigo( int i_angle, int *i_sin, int *i_cos )
  78. {
  79.     const double f_angle = (((double)i_angle)*M_PI)/1800.;
  80.     *i_sin = (int)(sin( f_angle )*4096.);
  81.     *i_cos = (int)(cos( f_angle )*4096.);
  82. }
  83. /*****************************************************************************
  84.  * Create: allocates Distort video filter
  85.  *****************************************************************************/
  86. static int Create( vlc_object_t *p_this )
  87. {
  88.     filter_t *p_filter = (filter_t *)p_this;
  89.     filter_sys_t *p_sys;
  90.     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
  91.     {
  92.         msg_Err( p_filter, "Input and output chromas don't match" );
  93.         return VLC_EGENERIC;
  94.     }
  95.     switch( p_filter->fmt_in.video.i_chroma )
  96.     {
  97.         CASE_PLANAR_YUV
  98.             p_filter->pf_video_filter = Filter;
  99.             break;
  100.         CASE_PACKED_YUV_422
  101.             p_filter->pf_video_filter = FilterPacked;
  102.             break;
  103.         default:
  104.             msg_Err( p_filter, "Unsupported input chroma (%4s)",
  105.                      (char*)&(p_filter->fmt_in.video.i_chroma) );
  106.             return VLC_EGENERIC;
  107.     }
  108.     /* Allocate structure */
  109.     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
  110.     if( p_filter->p_sys == NULL )
  111.         return VLC_ENOMEM;
  112.     p_sys = p_filter->p_sys;
  113.     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
  114.                        p_filter->p_cfg );
  115.     int i_angle = var_CreateGetIntegerCommand( p_filter,
  116.                                                FILTER_PREFIX "angle" ) * 10;
  117.     cache_trigo( i_angle, &p_sys->i_sin, &p_sys->i_cos );
  118.     var_Create( p_filter, FILTER_PREFIX "deciangle",
  119.                 VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
  120.     vlc_spin_init( &p_sys->lock );
  121.     var_AddCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_sys );
  122.     var_AddCallback( p_filter, FILTER_PREFIX "deciangle",
  123.                      PreciseRotateCallback, p_sys );
  124.     return VLC_SUCCESS;
  125. }
  126. /*****************************************************************************
  127.  * Destroy: destroy Distort filter
  128.  *****************************************************************************/
  129. static void Destroy( vlc_object_t *p_this )
  130. {
  131.     filter_t *p_filter = (filter_t *)p_this;
  132.     var_DelCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_filter->p_sys );
  133.     var_DelCallback( p_filter, FILTER_PREFIX "deciangle",
  134.                      PreciseRotateCallback, p_filter->p_sys );
  135.     vlc_spin_destroy( &p_filter->p_sys->lock );
  136.     free( p_filter->p_sys );
  137. }
  138. /*****************************************************************************
  139.  *
  140.  *****************************************************************************/
  141. static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
  142. {
  143.     picture_t *p_outpic;
  144.     filter_sys_t *p_sys = p_filter->p_sys;
  145.     int i_sin, i_cos;
  146.     if( !p_pic ) return NULL;
  147.     p_outpic = filter_NewPicture( p_filter );
  148.     if( !p_outpic )
  149.     {
  150.         picture_Release( p_pic );
  151.         return NULL;
  152.     }
  153.     vlc_spin_lock( &p_sys->lock );
  154.     i_sin = p_sys->i_sin;
  155.     i_cos = p_sys->i_cos;
  156.     vlc_spin_unlock( &p_sys->lock );
  157.     for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
  158.     {
  159.         const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
  160.         const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
  161.         const int i_pitch         = p_pic->p[i_plane].i_pitch;
  162.         const int i_hidden_pitch  = i_pitch - i_visible_pitch;
  163.         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 ));
  164.         /* = 2 for U and V planes in YUV 4:2:2, = 1 otherwise */
  165.         const int i_line_center = i_visible_lines>>1;
  166.         const int i_col_center  = i_visible_pitch>>1;
  167.         const uint8_t *p_in = p_pic->p[i_plane].p_pixels;
  168.         uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
  169.         uint8_t *p_outendline = p_out + i_visible_pitch;
  170.         const uint8_t *p_outend = p_out + i_visible_lines * i_pitch;
  171.         const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
  172.         const int i_line_next =  i_cos / i_aspect -i_sin*i_visible_pitch;
  173.         const int i_col_next  = -i_sin / i_aspect -i_cos*i_visible_pitch;
  174.         int i_line_orig0 = ( - i_cos * i_line_center / i_aspect
  175.                              - i_sin * i_col_center + (1<<11) );
  176.         int i_col_orig0 =    i_sin * i_line_center / i_aspect
  177.                            - i_cos * i_col_center + (1<<11);
  178.         for( ; p_outendline < p_outend;
  179.              p_out += i_hidden_pitch, p_outendline += i_pitch,
  180.              i_line_orig0 += i_line_next, i_col_orig0 += i_col_next )
  181.         {
  182.             for( ; p_out < p_outendline;
  183.                  p_out++, i_line_orig0 += i_sin, i_col_orig0 += i_cos )
  184.             {
  185.                 const int i_line_orig = (i_line_orig0>>12)*i_aspect + i_line_center;
  186.                 const int i_col_orig  = (i_col_orig0>>12)  + i_col_center;
  187.                 const uint8_t* p_orig_offset = p_in + i_line_orig * i_pitch
  188.                                                 + i_col_orig;
  189.                 const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
  190.                 const uint8_t i_col_percent  = (i_col_orig0 >>4) & 255;
  191.                 if(    -1 <= i_line_orig && i_line_orig < i_visible_lines
  192.                     && -1 <= i_col_orig  && i_col_orig  < i_visible_pitch )
  193.                 {
  194.                 #define test 1
  195.                 #undef test
  196.                 #ifdef test
  197.                     if( ( i_col_orig > i_visible_pitch/2 ) )
  198.                 #endif
  199.                     {
  200.                         uint8_t i_curpix = black_pixel;
  201.                         uint8_t i_colpix = black_pixel;
  202.                         uint8_t i_linpix = black_pixel;
  203.                         uint8_t i_nexpix = black_pixel;
  204.                         if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
  205.                             i_curpix = *p_orig_offset;
  206.                         p_orig_offset++;
  207.                         if(  ( i_col_orig < i_visible_pitch - 1)
  208.                              && ( i_line_orig >= 0 ) )
  209.                             i_colpix = *p_orig_offset;
  210.                         p_orig_offset+=i_pitch;
  211.                         if( ( i_line_orig < i_visible_lines - 1)
  212.                             && ( i_col_orig  < i_visible_pitch - 1) )
  213.                             i_nexpix = *p_orig_offset;
  214.                         p_orig_offset--;
  215.                         if(  ( i_line_orig < i_visible_lines - 1)
  216.                              && ( i_col_orig >= 0 ) )
  217.                             i_linpix = *p_orig_offset;
  218.                         unsigned int temp = 0;
  219.                         temp+= i_curpix *
  220.                             (256 - i_line_percent) * ( 256 - i_col_percent );
  221.                         temp+= i_linpix *
  222.                             i_line_percent * (256 - i_col_percent );
  223.                         temp+= i_nexpix *
  224.                             ( i_col_percent) * ( i_line_percent);
  225.                         temp+= i_colpix *
  226.                             i_col_percent * (256 - i_line_percent );
  227.                         *p_out = temp >> 16;
  228.                     }
  229.                 #ifdef test
  230.                     else if (i_col_orig == i_visible_pitch/2 )
  231.                     {   *p_out = black_pixel;
  232.                     }
  233.                     else
  234.                         *p_out = *p_orig_offset;
  235.                 #endif
  236.                 #undef test
  237.                 }
  238.                 else
  239.                 {
  240.                     *p_out = black_pixel;
  241.                 }
  242.             }
  243.         }
  244.     }
  245.     return CopyInfoAndRelease( p_outpic, p_pic );
  246. }
  247. /*****************************************************************************
  248.  *
  249.  *****************************************************************************/
  250. static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
  251. {
  252.     picture_t *p_outpic;
  253.     filter_sys_t *p_sys = p_filter->p_sys;
  254.     const int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
  255.     if( !p_pic ) return NULL;
  256.     int i_u_offset, i_v_offset, i_y_offset;
  257.     if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
  258.                              &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
  259.     {
  260.         msg_Warn( p_filter, "Unsupported input chroma (%4s)",
  261.                   (char*)&(p_pic->format.i_chroma) );
  262.         picture_Release( p_pic );
  263.         return NULL;
  264.     }
  265.     p_outpic = filter_NewPicture( p_filter );
  266.     if( !p_outpic )
  267.     {
  268.         picture_Release( p_pic );
  269.         return NULL;
  270.     }
  271.     const uint8_t *p_in   = p_pic->p->p_pixels+i_y_offset;
  272.     const uint8_t *p_in_u = p_pic->p->p_pixels+i_u_offset;
  273.     const uint8_t *p_in_v = p_pic->p->p_pixels+i_v_offset;
  274.     const int i_pitch         = p_pic->p->i_pitch;
  275.     const int i_visible_pitch = p_pic->p->i_visible_pitch>>1; /* In fact it's i_visible_pixels */
  276.     const int i_visible_lines = p_pic->p->i_visible_lines;
  277.     uint8_t *p_out   = p_outpic->p->p_pixels+i_y_offset;
  278.     uint8_t *p_out_u = p_outpic->p->p_pixels+i_u_offset;
  279.     uint8_t *p_out_v = p_outpic->p->p_pixels+i_v_offset;
  280.     const int i_line_center = i_visible_lines>>1;
  281.     const int i_col_center  = i_visible_pitch>>1;
  282.     int i_col, i_line;
  283.     for( i_line = 0; i_line < i_visible_lines; i_line++ )
  284.     {
  285.         for( i_col = 0; i_col < i_visible_pitch; i_col++ )
  286.         {
  287.             int i_line_orig;
  288.             int i_col_orig;
  289.             /* Handle "1st Y", U and V */
  290.             i_line_orig = i_line_center +
  291.                 ( ( i_sin * ( i_col - i_col_center )
  292.                   + i_cos * ( i_line - i_line_center ) )>>12 );
  293.             i_col_orig = i_col_center +
  294.                 ( ( i_cos * ( i_col - i_col_center )
  295.                   - i_sin * ( i_line - i_line_center ) )>>12 );
  296.             if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
  297.              && 0 <= i_line_orig && i_line_orig < i_visible_lines )
  298.             {
  299.                 p_out[i_line*i_pitch+2*i_col] = p_in[i_line_orig*i_pitch+2*i_col_orig];
  300.                 i_col_orig /= 2;
  301.                 p_out_u[i_line*i_pitch+2*i_col] = p_in_u[i_line_orig*i_pitch+4*i_col_orig];
  302.                 p_out_v[i_line*i_pitch+2*i_col] = p_in_v[i_line_orig*i_pitch+4*i_col_orig];
  303.             }
  304.             else
  305.             {
  306.                 p_out[i_line*i_pitch+2*i_col] = 0x00;
  307.                 p_out_u[i_line*i_pitch+2*i_col] = 0x80;
  308.                 p_out_v[i_line*i_pitch+2*i_col] = 0x80;
  309.             }
  310.             /* Handle "2nd Y" */
  311.             i_col++;
  312.             if( i_col >= i_visible_pitch )
  313.                 break;
  314.             i_line_orig = i_line_center +
  315.                 ( ( i_sin * ( i_col - i_col_center )
  316.                   + i_cos * ( i_line - i_line_center ) )>>12 );
  317.             i_col_orig = i_col_center +
  318.                 ( ( i_cos * ( i_col - i_col_center )
  319.                   - i_sin * ( i_line - i_line_center ) )>>12 );
  320.             if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
  321.              && 0 <= i_line_orig && i_line_orig < i_visible_lines )
  322.             {
  323.                 p_out[i_line*i_pitch+2*i_col] = p_in[i_line_orig*i_pitch+2*i_col_orig];
  324.             }
  325.             else
  326.             {
  327.                 p_out[i_line*i_pitch+2*i_col] = 0x00;
  328.             }
  329.         }
  330.     }
  331.     return CopyInfoAndRelease( p_outpic, p_pic );
  332. }
  333. /*****************************************************************************
  334.  * Angle modification callbacks.
  335.  *****************************************************************************/
  336. static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
  337.                            vlc_value_t oldval, vlc_value_t newval,
  338.                            void *p_data )
  339. {
  340.     oldval.i_int *= 10;
  341.     newval.i_int *= 10;
  342.     return PreciseRotateCallback( p_this, psz_var, oldval, newval, p_data );
  343. }
  344. static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
  345.                            vlc_value_t oldval, vlc_value_t newval,
  346.                            void *p_data )
  347. {
  348.     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
  349.     filter_sys_t *p_sys = (filter_sys_t *)p_data;
  350.     int i_sin, i_cos;
  351.     cache_trigo( newval.i_int, &i_sin, &i_cos );
  352.     vlc_spin_lock( &p_sys->lock );
  353.     p_sys->i_sin = i_sin;
  354.     p_sys->i_cos = i_cos;
  355.     vlc_spin_unlock( &p_sys->lock );
  356.     return VLC_SUCCESS;
  357. }