]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/deinterlace.c
Used VLC_CODEC_* and vlc_fourcc_GetCodec when suitable.
[vlc] / modules / video_filter / deinterlace.c
index 6e2a1106e3b28e5faacefbba8a24bb4d2e78c8a8..0460fd952eeb2bc0b45117ed2c9846602846f2c2 100644 (file)
@@ -64,6 +64,9 @@ static int  Init      ( vout_thread_t * );
 static void End       ( vout_thread_t * );
 static void Render    ( vout_thread_t *, picture_t * );
 
+static int  MouseEvent( vlc_object_t *p_this, char const *psz_var,
+                        vlc_value_t oldval, vlc_value_t newval, void *p_data );
+
 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
 static void RenderBob    ( vout_thread_t *, picture_t *, picture_t *, int );
 static void RenderMean   ( vout_thread_t *, picture_t *, picture_t * );
@@ -91,9 +94,6 @@ static void EndMMX       ( void );
 static void End3DNow     ( void );
 #endif
 
-static int  SendEvents   ( vlc_object_t *, char const *,
-                           vlc_value_t, vlc_value_t, void * );
-
 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
 
@@ -103,8 +103,8 @@ static void CloseFilter( vlc_object_t *p_this );
 /*****************************************************************************
  * Callback prototypes
  *****************************************************************************/
-static int FilterCallback ( vlc_object_t *, char const *,
-                            vlc_value_t, vlc_value_t, void * );
+static int FilterCallback( vlc_object_t *, char const *,
+                           vlc_value_t, vlc_value_t, void * );
 
 /*****************************************************************************
  * Module descriptor
@@ -133,6 +133,7 @@ vlc_module_begin ()
     add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
                 MODE_LONGTEXT, false )
         change_string_list( mode_list, mode_list_text, 0 )
+        change_safe ()
 
     add_shortcut( "deinterlace" )
     set_callbacks( Create, Destroy )
@@ -143,6 +144,7 @@ vlc_module_begin ()
     add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
                 SOUT_MODE_LONGTEXT, false )
         change_string_list( mode_list, mode_list_text, 0 )
+    add_shortcut( "deinterlace" )
     set_callbacks( OpenFilter, CloseFilter )
 vlc_module_end ()
 
@@ -308,7 +310,7 @@ static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
     }
     else
     {
-        const bool b_i422 = p_vout->render.i_chroma == VLC_FOURCC('I','4','2','2');
+        const bool b_i422 = p_vout->render.i_chroma == VLC_CODEC_I422;
         if( strcmp( psz_method, "discard" ) )
             msg_Err( p_vout, "no valid deinterlace mode provided, "
                      "using \"discard\"" );
@@ -321,35 +323,59 @@ static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
     msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
 }
 
+static void GetOutputFormat( vout_thread_t *p_vout,
+                             video_format_t *p_dst, const video_format_t *p_src )
+{
+    *p_dst = *p_src;
+
+    if( p_vout->p_sys->b_half_height )
+    {
+        p_dst->i_height /= 2;
+        p_dst->i_visible_height /= 2;
+        p_dst->i_y_offset /= 2;
+        p_dst->i_sar_den *= 2;
+    }
+
+    if( p_src->i_chroma == VLC_CODEC_I422 )
+    {
+        switch( p_vout->p_sys->i_mode )
+        {
+        case DEINTERLACE_MEAN:
+        case DEINTERLACE_LINEAR:
+        case DEINTERLACE_X:
+            p_dst->i_chroma = VLC_CODEC_I422;
+            break;
+        default:
+            p_dst->i_chroma = VLC_CODEC_I420;
+            break;
+        }
+    }
+}
+
+static bool IsChromaSupported( vlc_fourcc_t i_chroma )
+{
+    return i_chroma == VLC_CODEC_I420 ||
+           i_chroma == VLC_CODEC_YV12 ||
+           i_chroma == VLC_CODEC_I422;
+}
+
 /*****************************************************************************
  * Init: initialize Deinterlace video thread output method
  *****************************************************************************/
 static int Init( vout_thread_t *p_vout )
 {
-    int i_index;
-    picture_t *p_pic;
-
     I_OUTPUTPICTURES = 0;
 
+    if( !IsChromaSupported( p_vout->render.i_chroma ) )
+        return VLC_EGENERIC; /* unknown chroma */
+
     /* Initialize the output structure, full of directbuffers since we want
      * the decoder to output directly to our structures. */
-    switch( p_vout->render.i_chroma )
-    {
-        case VLC_FOURCC('I','4','2','0'):
-        case VLC_FOURCC('I','Y','U','V'):
-        case VLC_FOURCC('Y','V','1','2'):
-        case VLC_FOURCC('I','4','2','2'):
-            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;
-            p_vout->fmt_out = p_vout->fmt_in;
-            break;
-
-        default:
-            return VLC_EGENERIC; /* unknown chroma */
-            break;
-    }
+    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;
+    p_vout->fmt_out = p_vout->fmt_in;
 
     /* Try to open the real video output */
     p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
@@ -364,11 +390,9 @@ static int Init( vout_thread_t *p_vout )
 
     var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
 
-    ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
-
-    ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
+    vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
 
-    ADD_PARENT_CALLBACKS( SendEventsToChild );
+    vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
 
     return VLC_SUCCESS;
 }
@@ -378,47 +402,12 @@ static int Init( vout_thread_t *p_vout )
  *****************************************************************************/
 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
 {
-    vout_thread_t *p_real_vout = NULL;
-    video_format_t fmt;
-    memset( &fmt, 0, sizeof( video_format_t ) );
-
     msg_Dbg( p_vout, "spawning the real video output" );
 
-    fmt = p_vout->fmt_out;
-    if( p_vout->p_sys->b_half_height )
-    {
-        fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
-        fmt.i_sar_den *= 2;
-    }
-
-    switch( p_vout->render.i_chroma )
-    {
-    case VLC_FOURCC('I','4','2','0'):
-    case VLC_FOURCC('I','Y','U','V'):
-    case VLC_FOURCC('Y','V','1','2'):
-        p_real_vout = vout_Create( p_vout, &fmt );
-        break;
-
-    case VLC_FOURCC('I','4','2','2'):
-        switch( p_vout->p_sys->i_mode )
-        {
-        case DEINTERLACE_MEAN:
-        case DEINTERLACE_LINEAR:
-        case DEINTERLACE_X:
-            fmt.i_chroma = VLC_FOURCC('I','4','2','2');
-            break;
-        default:
-            fmt.i_chroma = VLC_FOURCC('I','4','2','0');
-            break;
-        }
-        p_real_vout = vout_Create( p_vout, &fmt );
-        break;
-
-    default:
-        break;
-    }
+    video_format_t fmt;
+    GetOutputFormat( p_vout, &fmt, &p_vout->fmt_out );
 
-    return p_real_vout;
+    return vout_Create( p_vout, &fmt );
 }
 
 /*****************************************************************************
@@ -426,22 +415,15 @@ static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
  *****************************************************************************/
 static void End( vout_thread_t *p_vout )
 {
-    int i_index;
-
-    DEL_PARENT_CALLBACKS( SendEventsToChild );
-
-    if( p_vout->p_sys->p_vout )
-        DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
+    vout_sys_t *p_sys = p_vout->p_sys;
 
-    /* Free the fake output buffers we allocated */
-    for( i_index = I_OUTPUTPICTURES ; i_index ; )
+    if( p_sys->p_vout )
     {
-        i_index--;
-        free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
+        vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
+        vout_CloseAndRelease( p_sys->p_vout );
     }
 
-    if( p_vout->p_sys->p_vout )
-        vout_CloseAndRelease( p_vout->p_sys->p_vout );
+    vout_filter_ReleaseDirectBuffers( p_vout );
 }
 
 /*****************************************************************************
@@ -456,6 +438,21 @@ static void Destroy( vlc_object_t *p_this )
     free( p_vout->p_sys );
 }
 
+/**
+ * Forward mouse event with proper conversion.
+ */
+static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
+                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
+{
+    vout_thread_t *p_vout = p_data;
+    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
+
+    if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
+        newval.i_int *= 2;
+
+    return var_Set( p_vout, psz_var, newval );
+}
+
 /*****************************************************************************
  * Render: displays previously rendered output
  *****************************************************************************
@@ -610,9 +607,8 @@ static void RenderDiscard( vout_thread_t *p_vout,
 
         switch( p_vout->render.i_chroma )
         {
-        case VLC_FOURCC('I','4','2','0'):
-        case VLC_FOURCC('I','Y','U','V'):
-        case VLC_FOURCC('Y','V','1','2'):
+        case VLC_CODEC_I420:
+        case VLC_CODEC_YV12:
 
             for( ; p_out < p_out_end ; )
             {
@@ -623,7 +619,7 @@ static void RenderDiscard( vout_thread_t *p_vout,
             }
             break;
 
-        case VLC_FOURCC('I','4','2','2'):
+        case VLC_CODEC_I422:
 
             i_increment = 2 * p_pic->p[i_plane].i_pitch;
 
@@ -675,9 +671,8 @@ static void RenderBob( vout_thread_t *p_vout,
 
         switch( p_vout->render.i_chroma )
         {
-            case VLC_FOURCC('I','4','2','0'):
-            case VLC_FOURCC('I','Y','U','V'):
-            case VLC_FOURCC('Y','V','1','2'):
+            case VLC_CODEC_I420:
+            case VLC_CODEC_YV12:
                 /* For BOTTOM field we need to add the first line */
                 if( i_field == 1 )
                 {
@@ -711,7 +706,7 @@ static void RenderBob( vout_thread_t *p_vout,
                 }
                 break;
 
-            case VLC_FOURCC('I','4','2','2'):
+            case VLC_CODEC_I422:
                 /* For BOTTOM field we need to add the first line */
                 if( i_field == 1 )
                 {
@@ -865,9 +860,8 @@ static void RenderBlend( vout_thread_t *p_vout,
 
         switch( p_vout->render.i_chroma )
         {
-            case VLC_FOURCC('I','4','2','0'):
-            case VLC_FOURCC('I','Y','U','V'):
-            case VLC_FOURCC('Y','V','1','2'):
+            case VLC_CODEC_I420:
+            case VLC_CODEC_YV12:
                 /* First line: simple copy */
                 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
                 p_out += p_outpic->p[i_plane].i_pitch;
@@ -883,7 +877,7 @@ static void RenderBlend( vout_thread_t *p_vout,
                 }
                 break;
 
-            case VLC_FOURCC('I','4','2','2'):
+            case VLC_CODEC_I422:
                 /* First line: simple copy */
                 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
                 p_out += p_outpic->p[i_plane].i_pitch;
@@ -1676,24 +1670,6 @@ static void RenderX( picture_t *p_outpic, picture_t *p_pic )
 #endif
 }
 
-/*****************************************************************************
- * 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 )
-{
-    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
-    vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
-    vlc_value_t sentval = newval;
-
-    if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
-        sentval.i_int *= 2;
-
-    var_Set( p_vout, psz_var, sentval );
-
-    return VLC_SUCCESS;
-}
-
 /*****************************************************************************
  * FilterCallback: called when changing the deinterlace method on the fly.
  *****************************************************************************/
@@ -1720,7 +1696,7 @@ static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
     /* We need to kill the old vout */
     if( p_vout->p_sys->p_vout )
     {
-        DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
+        vout_filter_DelChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
         vout_CloseAndRelease( p_vout->p_sys->p_vout );
     }
 
@@ -1736,25 +1712,12 @@ static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
         return VLC_EGENERIC;
     }
 
-    ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
+    vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
 
     vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
     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 )
-{
-    VLC_UNUSED(p_data); VLC_UNUSED(oldval);
-    vout_thread_t *p_vout = (vout_thread_t *)p_this;
-    var_Set( p_vout->p_sys->p_vout, psz_var, newval );
-    return VLC_SUCCESS;
-}
-
-
 /*****************************************************************************
  * video filter2 functions
  *****************************************************************************/
@@ -1766,17 +1729,15 @@ static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
     /* Request output picture */
     p_pic_dst = filter_NewPicture( p_filter );
     if( p_pic_dst == NULL )
-        return p_pic;
+    {
+        picture_Release( p_pic );
+        return NULL;
+    }
 
     switch( p_vout->p_sys->i_mode )
     {
         case DEINTERLACE_DISCARD:
-#if 0
             RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
-#endif
-            msg_Err( p_vout, "discarding lines is not supported yet" );
-            picture_Release( p_pic_dst );
-            return p_pic;
             break;
 
         case DEINTERLACE_BOB:
@@ -1793,8 +1754,8 @@ static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
 #endif
             msg_Err( p_vout, "doubling the frame rate is not supported yet" );
             picture_Release( p_pic_dst );
-            return p_pic;
-            break;
+            picture_Release( p_pic );
+            return NULL;
 
         case DEINTERLACE_MEAN:
             RenderMean( p_vout, p_pic_dst, p_pic );
@@ -1825,13 +1786,8 @@ static int OpenFilter( vlc_object_t *p_this )
     vout_thread_t *p_vout;
     vlc_value_t val;
 
-    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('Y','V','1','2') ) ||
-        p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
-    {
+    if( !IsChromaSupported( p_filter->fmt_in.video.i_chroma ) )
         return VLC_EGENERIC;
-    }
 
     /* Impossible to use VLC_OBJECT_VOUT here because it would be used
      * by spu filters */
@@ -1843,17 +1799,29 @@ static int OpenFilter( vlc_object_t *p_this )
     config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
                    p_filter->p_cfg );
     var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
+
     var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
     var_Set( p_filter, "deinterlace-mode", val );
     free( val.psz_string );
 
-    if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
+    if( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
     {
         vlc_object_detach( p_vout );
         vlc_object_release( p_vout );
         return VLC_EGENERIC;
     }
 
+    video_format_t fmt;
+    GetOutputFormat( p_vout, &fmt, &p_filter->fmt_in.video );
+    if( !p_filter->b_allow_fmt_out_change &&
+        ( fmt.i_chroma != p_filter->fmt_in.video.i_chroma ||
+          fmt.i_height != p_filter->fmt_in.video.i_height ) )
+    {
+        CloseFilter( VLC_OBJECT(p_filter) );
+        return VLC_EGENERIC;
+    }
+    p_filter->fmt_out.video = fmt;
+    p_filter->fmt_out.i_codec = fmt.i_chroma;
     p_filter->pf_video_filter = Deinterlace;
 
     msg_Dbg( p_filter, "deinterlacing" );