]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/rotate.c
video_filter: fix warnings
[vlc] / modules / video_filter / rotate.c
index ad2e4be7137a9936452b6925e044d89ec3720fdc..67359f043c10dbf5eb7fe1df5aef77e452b57d36 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>
 
 #include <math.h>                                            /* sin(), cos() */
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
-#include <vlc/decoder.h>
+#include <vlc_vout.h>
 
 #include "vlc_filter.h"
 
@@ -42,6 +44,14 @@ static void Destroy   ( vlc_object_t * );
 
 static picture_t *Filter( filter_t *, picture_t * );
 
+static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
+                           vlc_value_t oldval, vlc_value_t newval,
+                           void *p_data );
+
+static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
+                           vlc_value_t oldval, vlc_value_t newval,
+                           void *p_data );
+
 #define ANGLE_TEXT N_("Angle in degrees")
 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
 
@@ -57,7 +67,7 @@ vlc_module_begin();
     set_category( CAT_VIDEO );
     set_subcategory( SUBCAT_VIDEO_VFILTER );
 
-    add_integer_with_range( FILTER_PREFIX "angle", 0, 0, 359, NULL,
+    add_integer_with_range( FILTER_PREFIX "angle", 30, 0, 359, NULL,
         ANGLE_TEXT, ANGLE_LONGTEXT, VLC_FALSE );
 
     add_shortcut( "rotate" );
@@ -69,25 +79,50 @@ static const char *ppsz_filter_options[] = {
 };
 
 /*****************************************************************************
- * vout_sys_t: Distort video output method descriptor
- *****************************************************************************
- * This structure is part of the video output thread descriptor.
- * It describes the Distort specific properties of an output thread.
+ * filter_sys_t
  *****************************************************************************/
 struct filter_sys_t
 {
     int     i_angle;
-    mtime_t last_date;
+    int     i_cos;
+    int     i_sin;
 };
 
+static inline void cache_trigo( int i_angle, int *i_sin, int *i_cos )
+{
+    const double f_angle = (((double)i_angle)*M_PI)/1800.;
+    *i_sin = (int)(sin( f_angle )*4096.);
+    *i_cos = (int)(cos( f_angle )*4096.);
+}
+
 /*****************************************************************************
- * Create: allocates Distort video thread output method
- *****************************************************************************
- * This function allocates and initializes a Distort vout method.
+ * Create: allocates Distort video filter
  *****************************************************************************/
 static int Create( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t *)p_this;
+    filter_sys_t *p_sys;
+
+    if(   p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0')
+       && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V')
+       && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','0')
+       && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2')
+
+       && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','2')
+       && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','2')
+      )
+    {
+        /* We only want planar YUV 4:2:0 or 4:2:2 */
+        msg_Err( p_filter, "Unsupported input chroma (%4s)",
+                 (char*)&(p_filter->fmt_in.video.i_chroma) );
+        return VLC_EGENERIC;
+    }
+
+    if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
+    {
+        msg_Err( p_filter, "Input and output chromas don't match" );
+        return VLC_EGENERIC;
+    }
 
     /* Allocate structure */
     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
@@ -96,46 +131,45 @@ static int Create( vlc_object_t *p_this )
         msg_Err( p_filter, "out of memory" );
         return VLC_ENOMEM;
     }
+    p_sys = p_filter->p_sys;
 
     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
                        p_filter->p_cfg );
-    var_Create( p_filter, FILTER_PREFIX "angle",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
 
-    p_filter->pf_video_filter = Filter;
+    p_sys->i_angle = var_CreateGetIntegerCommand( p_filter,
+                                                  FILTER_PREFIX "angle" ) * 10;
+    var_Create( p_filter, FILTER_PREFIX "deciangle",
+                VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
+    var_AddCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_sys );
+    var_AddCallback( p_filter, FILTER_PREFIX "deciangle",
+                     PreciseRotateCallback, p_sys );
+
+    cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
 
-    p_filter->p_sys->i_angle = var_GetInteger( p_filter,
-                                               FILTER_PREFIX "angle" );
-    p_filter->p_sys->last_date = 0;
+    p_filter->pf_video_filter = Filter;
 
     return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * Destroy: destroy Distort video thread output method
- *****************************************************************************
- * Terminate an output method created by DistortCreateOutputMethod
+ * Destroy: destroy Distort filter
  *****************************************************************************/
 static void Destroy( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t *)p_this;
+
     free( p_filter->p_sys );
 }
 
 /*****************************************************************************
- * Render: displays previously rendered output
- *****************************************************************************
- * This function send the currently rendered image to Distort image, waits
- * until it is displayed and switch the two rendering buffers, preparing next
- * frame.
+ *
  *****************************************************************************/
 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 {
     picture_t *p_outpic;
-    int i_index;
-    double f_angle;
-    double f_sin, f_cos;
-    mtime_t new_date = mdate();
+    filter_sys_t *p_sys = p_filter->p_sys;
+    int i_plane;
+    const int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
 
     if( !p_pic ) return NULL;
 
@@ -148,50 +182,100 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
         return NULL;
     }
 
-    p_filter->p_sys->last_date = new_date;
-    f_angle = (((double)p_filter->p_sys->i_angle)*M_PI)/180.;
-    f_sin = sin( f_angle );
-    f_cos = cos( f_angle );
-
-    for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
+    for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
     {
-        int i_line, i_num_lines, i_line_center, i_col, i_num_cols, i_col_center;
-        uint8_t black_pixel;
-        uint8_t *p_in, *p_out;
-
-        p_in = p_pic->p[i_index].p_pixels;
-        p_out = p_outpic->p[i_index].p_pixels;
-
-        i_num_lines = p_pic->p[i_index].i_visible_lines;
-        i_num_cols = p_pic->p[i_index].i_visible_pitch;
-        i_line_center = i_num_lines/2;
-        i_col_center = i_num_cols/2;
-
-        black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
-
-        /* Ok, we do 3 times the sin() calculation for each line. So what ? */
-        for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
+        const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
+        const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
+        const int i_pitch         = p_pic->p[i_plane].i_pitch;
+        const int i_hidden_pitch  = i_pitch - i_visible_pitch;
+
+        const int i_aspect = ( i_visible_lines * p_pic->p[Y_PLANE].i_visible_pitch ) / ( p_pic->p[Y_PLANE].i_visible_lines * i_visible_pitch );
+        /* = 2 for U and V planes in YUV 4:2:2, = 1 otherwise */
+
+        const int i_line_center = i_visible_lines>>1;
+        const int i_col_center  = i_visible_pitch>>1;
+
+        const uint8_t *p_in = p_pic->p[i_plane].p_pixels;
+        uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
+        uint8_t *p_outendline = p_out + i_visible_pitch;
+        const uint8_t *p_outend = p_out + i_visible_lines * i_pitch;
+
+        const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
+
+        const int i_line_next =  i_cos / i_aspect -i_sin*i_visible_pitch;
+        const int i_col_next  = -i_sin / i_aspect -i_cos*i_visible_pitch;
+        int i_line_orig0 = ( - i_cos * i_line_center / i_aspect
+                             - i_sin * i_col_center + (1<<11) );
+        int i_col_orig0 =    i_sin * i_line_center / i_aspect
+                           - i_cos * i_col_center + (1<<11);
+        for( ; p_outendline < p_outend;
+             p_out += i_hidden_pitch, p_outendline += i_pitch,
+             i_line_orig0 += i_line_next, i_col_orig0 += i_col_next )
         {
-            for( i_col = 0; i_col < i_num_cols ; i_col++ )
+            for( ; p_out < p_outendline;
+                 p_out++, i_line_orig0 += i_sin, i_col_orig0 += i_cos )
             {
-                int i_line_orig, i_col_orig;
-                i_line_orig = (int)( f_cos * (double)(i_line-i_line_center)
-                                   + f_sin * (double)(i_col-i_col_center) )
-                              + i_line_center;
-                i_col_orig = (int)(-f_sin * (double)(i_line-i_line_center)
-                                  + f_cos * (double)(i_col-i_col_center) )
-                             + i_col_center;
-
-                if(    0 <= i_line_orig && i_line_orig < i_num_lines
-                    && 0 <= i_col_orig && i_col_orig < i_num_cols )
+                const int i_line_orig = (i_line_orig0>>12)*i_aspect + i_line_center;
+                const int i_col_orig  = (i_col_orig0>>12)  + i_col_center;
+                const uint8_t* p_orig_offset = p_in + i_line_orig * i_pitch
+                                                + i_col_orig;
+                const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
+                const uint8_t i_col_percent  = (i_col_orig0 >>4) & 255;
+
+                if(    -1 <= i_line_orig && i_line_orig < i_visible_lines
+                    && -1 <= i_col_orig  && i_col_orig  < i_visible_pitch )
                 {
-
-                    p_out[i_line*i_num_cols+i_col] =
-                        p_in[i_line_orig*i_num_cols+i_col_orig];
+                #define test 1
+                #undef test
+                #ifdef test
+                    if( ( i_col_orig > i_visible_pitch/2 ) )
+                #endif
+                    {
+                        uint8_t i_curpix = black_pixel;
+                        uint8_t i_colpix = black_pixel;
+                        uint8_t i_linpix = black_pixel;
+                        uint8_t i_nexpix = black_pixel;
+                        if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
+                            i_curpix = *p_orig_offset;
+                        p_orig_offset++;
+
+                        if(  ( i_col_orig < i_visible_pitch - 1)
+                             && ( i_line_orig >= 0 ) )
+                            i_colpix = *p_orig_offset;
+
+                        p_orig_offset+=i_pitch;
+                        if( ( i_line_orig < i_visible_lines - 1)
+                            && ( i_col_orig  < i_visible_pitch - 1) )
+                            i_nexpix = *p_orig_offset;
+
+                        p_orig_offset--;
+                        if(  ( i_line_orig < i_visible_lines - 1)
+                             && ( i_col_orig >= 0 ) )
+                            i_linpix = *p_orig_offset;
+
+                        unsigned int temp = 0;
+                        temp+= i_curpix *
+                            (256 - i_line_percent) * ( 256 - i_col_percent );
+                        temp+= i_linpix *
+                            i_line_percent * (256 - i_col_percent );
+                        temp+= i_nexpix *
+                            ( i_col_percent) * ( i_line_percent);
+                        temp+= i_colpix *
+                            i_col_percent * (256 - i_line_percent );
+                        *p_out = temp >> 16;
+                    }
+                #ifdef test
+                    else if (i_col_orig == i_visible_pitch/2 )
+                    {   *p_out = black_pixel;
+                    }
+                    else
+                        *p_out = *p_orig_offset;
+                #endif
+                #undef test
                 }
                 else
                 {
-                    p_out[i_line*i_num_cols+i_col] = black_pixel;
+                    *p_out = black_pixel;
                 }
             }
         }
@@ -208,3 +292,28 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 
     return p_outpic;
 }
+
+/*****************************************************************************
+ * Angle modification callbacks.
+ *****************************************************************************/
+static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
+                           vlc_value_t oldval, vlc_value_t newval,
+                           void *p_data )
+{
+    VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
+    filter_sys_t *p_sys = (filter_sys_t *)p_data;
+    p_sys->i_angle = newval.i_int*10;
+    cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
+    return VLC_SUCCESS;
+}
+
+static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
+                           vlc_value_t oldval, vlc_value_t newval,
+                           void *p_data )
+{
+    VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
+    filter_sys_t *p_sys = (filter_sys_t *)p_data;
+    p_sys->i_angle = newval.i_int;
+    cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
+    return VLC_SUCCESS;
+}