]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/extract.c
Don't print a message a malloc failed.
[vlc] / modules / video_filter / extract.c
index 1d2190b553072698ea8861034d1ec6929abe5584..23716f018307be673ef1697208fab86675144001 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <string.h>
 
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_vout.h>
 
 #include "vlc_filter.h"
+#include "filter_picture.h"
 
 #include "math.h"
 
@@ -52,34 +57,36 @@ static void get_blue_from_yuv422( picture_t *, picture_t *, int, int, int );
 static void make_projection_matrix( filter_t *, int color, int *matrix );
 static void get_custom_from_yuv420( picture_t *, picture_t *, int, int, int, int * );
 static void get_custom_from_yuv422( picture_t *, picture_t *, int, int, int, int * );
+static void get_custom_from_packedyuv422( picture_t *, picture_t *, int * );
 
 
 #define COMPONENT_TEXT N_("RGB component to extract")
 #define COMPONENT_LONGTEXT N_("RGB component to extract. 0 for Red, 1 for Green and 2 for Blue.")
 #define FILTER_PREFIX "extract-"
 
-static int pi_component_values[] = { 0xFF0000, 0x00FF00, 0x0000FF };
-static const char *ppsz_component_descriptions[] = { "Red", "Green", "Blue" };
+static const int pi_component_values[] = { 0xFF0000, 0x00FF00, 0x0000FF };
+static const char *const ppsz_component_descriptions[] = {
+    "Red", "Green", "Blue" };
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin();
-    set_description( _("Extract RGB component video filter") );
-    set_shortname( _("Extract" ));
+    set_description( N_("Extract RGB component video filter") );
+    set_shortname( N_("Extract" ));
     set_category( CAT_VIDEO );
     set_subcategory( SUBCAT_VIDEO_VFILTER );
     set_capability( "video filter2", 0 );
     add_shortcut( "extract" );
 
     add_integer_with_range( FILTER_PREFIX "component", 0xFF0000, 1, 0xFFFFFF,
-                 NULL, COMPONENT_TEXT, COMPONENT_LONGTEXT, VLC_FALSE );
+                 NULL, COMPONENT_TEXT, COMPONENT_LONGTEXT, false );
         change_integer_list( pi_component_values, ppsz_component_descriptions, 0 );
 
     set_callbacks( Create, Destroy );
 vlc_module_end();
 
-static const char *ppsz_filter_options[] = {
+static const char *const ppsz_filter_options[] = {
     "component", NULL
 };
 
@@ -97,18 +104,34 @@ static int Create( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t *)p_this;
 
+    switch( p_filter->fmt_in.video.i_chroma )
+    {
+        case VLC_FOURCC('I','4','2','0'):
+        case VLC_FOURCC('I','Y','U','V'):
+        case VLC_FOURCC('J','4','2','0'):
+        case VLC_FOURCC('Y','V','1','2'):
+
+        case VLC_FOURCC('I','4','2','2'):
+        case VLC_FOURCC('J','4','2','2'):
+
+        CASE_PACKED_YUV_422
+            break;
+
+        default:
+            /* 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;
+    }
+
     /* Allocate structure */
     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
     if( p_filter->p_sys == NULL )
-    {
-        msg_Err( p_filter, "out of memory" );
         return VLC_ENOMEM;
-    }
     p_filter->p_sys->projection_matrix = malloc( 9 * sizeof( int ) );
     if( !p_filter->p_sys->projection_matrix )
     {
         free( p_filter->p_sys );
-        msg_Err( p_filter, "out of memory" );
         return VLC_ENOMEM;
     }
 
@@ -119,17 +142,10 @@ static int Create( vlc_object_t *p_this )
                                                FILTER_PREFIX "component" );
     var_AddCallback( p_filter, FILTER_PREFIX "component",
                      ExtractCallback, p_filter->p_sys );
-    switch( p_filter->p_sys->i_color )
-    {
-        case RED:
-        case GREEN:
-        case BLUE:
-            break;
-        default:
-            make_projection_matrix( p_filter, p_filter->p_sys->i_color,
-                                    p_filter->p_sys->projection_matrix );
-            break;
-    }
+
+    /* Matrix won't be used for RED, GREEN or BLUE in planar formats */
+    make_projection_matrix( p_filter, p_filter->p_sys->i_color,
+                            p_filter->p_sys->projection_matrix );
 
     p_filter->pf_video_filter = Filter;
 
@@ -156,12 +172,10 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 
     if( !p_pic ) return NULL;
 
-    p_outpic = p_filter->pf_vout_buffer_new( p_filter );
+    p_outpic = filter_NewPicture( p_filter );
     if( !p_outpic )
     {
-        msg_Warn( p_filter, "can't get output picture" );
-        if( p_pic->pf_release )
-            p_pic->pf_release( p_pic );
+        picture_Release( p_pic );
         return NULL;
     }
 
@@ -217,28 +231,22 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
             }
             break;
 
+        CASE_PACKED_YUV_422
+            get_custom_from_packedyuv422( p_pic, p_outpic,
+                                          p_filter->p_sys->projection_matrix );
+            break;
+
         default:
             msg_Warn( p_filter, "Unsupported input chroma (%4s)",
                       (char*)&(p_pic->format.i_chroma) );
-            if( p_pic->pf_release )
-                p_pic->pf_release( p_pic );
+            picture_Release( p_pic );
             return NULL;
     }
 
-    p_outpic->date = p_pic->date;
-    p_outpic->b_force = p_pic->b_force;
-    p_outpic->i_nb_fields = p_pic->i_nb_fields;
-    p_outpic->b_progressive = p_pic->b_progressive;
-    p_outpic->b_top_field_first = p_pic->b_top_field_first;
-
-    if( p_pic->pf_release )
-        p_pic->pf_release( p_pic );
-
-    return p_outpic;
+    return CopyInfoAndRelease( p_outpic, p_pic );
 }
 
-inline uint8_t crop( int a );
-inline uint8_t crop( int a )
+static inline uint8_t crop( int a )
 {
     if( a < 0 ) return 0;
     if( a > 255 ) return 255;
@@ -394,6 +402,59 @@ static void get_custom_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
     }
 }
 
+static void get_custom_from_packedyuv422( picture_t *p_inpic,
+                                          picture_t *p_outpic,
+                                          int *m )
+{
+    int i_y_offset, i_u_offset, i_v_offset;
+    if( GetPackedYuvOffsets( p_inpic->format.i_chroma, &i_y_offset,
+                         &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
+        return;
+
+    uint8_t *yin = p_inpic->p->p_pixels + i_y_offset;
+    uint8_t *uin = p_inpic->p->p_pixels + i_u_offset;
+    uint8_t *vin = p_inpic->p->p_pixels + i_v_offset;
+
+    uint8_t *yout = p_outpic->p->p_pixels + i_y_offset;
+    uint8_t *uout = p_outpic->p->p_pixels + i_u_offset;
+    uint8_t *vout = p_outpic->p->p_pixels + i_v_offset;
+
+    const int i_pitch = p_inpic->p->i_pitch;
+    const int i_visible_pitch = p_inpic->p->i_visible_pitch;
+    const int i_visible_lines = p_inpic->p->i_visible_lines;
+
+    const uint8_t *yend = yin + i_visible_lines * i_pitch;
+    while( yin < yend )
+    {
+        const uint8_t *ylend = yin + i_visible_pitch;
+        while( yin < ylend )
+        {
+            *uout = crop( (*yin * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
+                      / 65536 + U );
+            uout += 4;
+            *vout = crop( (*yin * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
+                     / 65536 + V );
+            vout += 4;
+            *yout = crop( (*yin * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
+                       / 65536 );
+            yin  += 2;
+            yout += 2;
+            *yout = crop( (*yin * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
+                       / 65536 );
+            yin  += 2;
+            yout += 2;
+            uin  += 4;
+            vin  += 4;
+        }
+        yin  += i_pitch - i_visible_pitch;
+        yout += i_pitch - i_visible_pitch;
+        uin  += i_pitch - i_visible_pitch;
+        uout += i_pitch - i_visible_pitch;
+        vin  += i_pitch - i_visible_pitch;
+        vout += i_pitch - i_visible_pitch;
+    }
+}
+
 static void get_red_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
                                  int yp, int up, int vp )
 {
@@ -696,22 +757,15 @@ static int ExtractCallback( vlc_object_t *p_this, char const *psz_var,
                             vlc_value_t oldval, vlc_value_t newval,
                             void *p_data )
 {
+    VLC_UNUSED(oldval);
     filter_sys_t *p_sys = (filter_sys_t *)p_data;
 
     if( !strcmp( psz_var, FILTER_PREFIX "component" ) )
     {
         p_sys->i_color = newval.i_int;
-        switch( p_sys->i_color )
-        {
-            case RED:
-            case GREEN:
-            case BLUE:
-                break;
-            default:
-                make_projection_matrix( (filter_t *)p_this, p_sys->i_color,
-                                        p_sys->projection_matrix );
-                break;
-        }
+        /* Matrix won't be used for RED, GREEN or BLUE in planar formats */
+        make_projection_matrix( (filter_t *)p_this, p_sys->i_color,
+                                p_sys->projection_matrix );
     }
     else
     {