[Mesa-dev] [PATCH v3] vl: implement luma key ring

Nayan Deshmukh nayan26deshmukh at gmail.com
Tue Jun 7 17:42:48 UTC 2016


Apply the luma key filter to the YCbCr values during the CSC conversion
in video buffer shader. The initial values of max and min luma are set
to opposite values to disable the filter initially and will be set when
enabling it.

Add extra parmeters min and max luma for the luma key filter in
vl_compositor_set_csc_matrix in va, xvmc. Setting them
to opposite value 1.f and 0.f respectively won't effect the CSC
conversion

v2: -Squash 1,2 and 3 into one patch to avoid breaking build of
    other components. (Christian)
    -use ureg_swizzle. (Christian)
    -change name of the variables. (Christian)

v3: -Squash all patches in one to avoid breaking of build. (Emil)
    -wrap functions properly. (Emil)
    -use 0.0f and 1.0f instead of 0.f and 1.f respectively. (Emil)

Signed-off-by: Nayan Deshmukh <nayan26deshmukh at gmail.com>
---
 src/gallium/auxiliary/vl/vl_compositor.c         | 52 ++++++++++++++++------
 src/gallium/auxiliary/vl/vl_compositor.h         |  4 +-
 src/gallium/state_trackers/va/context.c          |  2 +-
 src/gallium/state_trackers/vdpau/mixer.c         | 55 +++++++++++++++++-------
 src/gallium/state_trackers/vdpau/output.c        |  4 +-
 src/gallium/state_trackers/vdpau/vdpau_private.h |  6 ++-
 src/gallium/state_trackers/xvmc/attributes.c     |  2 +-
 src/gallium/state_trackers/xvmc/context.c        |  2 +-
 8 files changed, 90 insertions(+), 37 deletions(-)

diff --git a/src/gallium/auxiliary/vl/vl_compositor.c b/src/gallium/auxiliary/vl/vl_compositor.c
index acb2f4f..8efcfb1 100644
--- a/src/gallium/auxiliary/vl/vl_compositor.c
+++ b/src/gallium/auxiliary/vl/vl_compositor.c
@@ -131,7 +131,9 @@ create_frag_shader_video_buffer(struct vl_compositor *c)
    struct ureg_program *shader;
    struct ureg_src tc;
    struct ureg_src csc[3];
+   struct ureg_src lumakey;
    struct ureg_src sampler[3];
+   struct ureg_dst temp[3];
    struct ureg_dst texel;
    struct ureg_dst fragment;
    unsigned i;
@@ -142,9 +144,12 @@ create_frag_shader_video_buffer(struct vl_compositor *c)
 
    tc = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX, TGSI_INTERPOLATE_LINEAR);
    for (i = 0; i < 3; ++i) {
-      csc[i] = ureg_DECL_constant(shader, i);
       sampler[i] = ureg_DECL_sampler(shader, i);
+      csc[i] = ureg_DECL_constant(shader, i);
+      temp[i] = ureg_DECL_temporary(shader);
    }
+
+   lumakey = ureg_DECL_constant(shader, 3);
    texel = ureg_DECL_temporary(shader);
    fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
 
@@ -153,14 +158,30 @@ create_frag_shader_video_buffer(struct vl_compositor *c)
     * fragment = csc * texel
     */
    for (i = 0; i < 3; ++i)
-      ureg_TEX(shader, ureg_writemask(texel, TGSI_WRITEMASK_X << i), TGSI_TEXTURE_2D_ARRAY, tc, sampler[i]);
+      ureg_TEX(shader, ureg_writemask(texel, TGSI_WRITEMASK_X << i),
+              TGSI_TEXTURE_2D_ARRAY, tc, sampler[i]);
 
    ureg_MOV(shader, ureg_writemask(texel, TGSI_WRITEMASK_W), ureg_imm1f(shader, 1.0f));
 
    for (i = 0; i < 3; ++i)
       ureg_DP4(shader, ureg_writemask(fragment, TGSI_WRITEMASK_X << i), csc[i], ureg_src(texel));
 
-   ureg_MOV(shader, ureg_writemask(fragment, TGSI_WRITEMASK_W), ureg_imm1f(shader, 1.0f));
+   ureg_MOV(shader, ureg_writemask(temp[0], TGSI_WRITEMASK_W),
+           ureg_swizzle(ureg_src(texel),TGSI_SWIZZLE_X,
+               TGSI_SWIZZLE_Y, TGSI_SWIZZLE_W, TGSI_SWIZZLE_Z));
+   ureg_MOV(shader, temp[1],
+           ureg_swizzle(lumakey,TGSI_SWIZZLE_X,
+               TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, TGSI_SWIZZLE_X));
+   ureg_MOV(shader, temp[2],
+           ureg_swizzle(lumakey, TGSI_SWIZZLE_Y,
+               TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y));
+   ureg_SLE(shader, temp[1], ureg_src(temp[0]), ureg_src(temp[1]));
+   ureg_SGT(shader, temp[2], ureg_src(temp[0]), ureg_src(temp[2]));
+   ureg_MAX(shader, ureg_writemask(fragment, TGSI_WRITEMASK_W),
+           ureg_src(temp[1]), ureg_src(temp[2]));
+
+   for (i = 0; i < 3; ++i)
+       ureg_release_temporary(shader, temp[i]);
 
    ureg_release_temporary(shader, texel);
    ureg_END(shader);
@@ -852,20 +873,23 @@ vl_compositor_cleanup(struct vl_compositor *c)
 }
 
 void
-vl_compositor_set_csc_matrix(struct vl_compositor_state *s, vl_csc_matrix const *matrix)
+vl_compositor_set_csc_matrix(struct vl_compositor_state *s,
+                             vl_csc_matrix const *matrix,
+                             float luma_min, float luma_max)
 {
    struct pipe_transfer *buf_transfer;
 
    assert(s);
 
-   memcpy
-   (
-      pipe_buffer_map(s->pipe, s->csc_matrix,
-                      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE,
-                      &buf_transfer),
-      matrix,
-      sizeof(vl_csc_matrix)
-   );
+   float *ptr = pipe_buffer_map(s->pipe, s->csc_matrix,
+                               PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE,
+                               &buf_transfer);
+
+   memcpy(ptr, matrix, sizeof(vl_csc_matrix));
+
+   ptr += sizeof(vl_csc_matrix)/sizeof(float);
+   ptr[0] = luma_min;
+   ptr[1] = luma_max;
 
    pipe_buffer_unmap(s->pipe, buf_transfer);
 }
@@ -1142,13 +1166,13 @@ vl_compositor_init_state(struct vl_compositor_state *s, struct pipe_context *pip
       pipe->screen,
       PIPE_BIND_CONSTANT_BUFFER,
       PIPE_USAGE_DEFAULT,
-      sizeof(csc_matrix)
+      sizeof(csc_matrix) + 2*sizeof(float)
    );
 
    vl_compositor_clear_layers(s);
 
    vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, &csc_matrix);
-   vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix);
+   vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix, 1.0f, 0.0f);
 
    return true;
 }
diff --git a/src/gallium/auxiliary/vl/vl_compositor.h b/src/gallium/auxiliary/vl/vl_compositor.h
index 934b634..12976fc 100644
--- a/src/gallium/auxiliary/vl/vl_compositor.h
+++ b/src/gallium/auxiliary/vl/vl_compositor.h
@@ -138,7 +138,9 @@ vl_compositor_init_state(struct vl_compositor_state *state, struct pipe_context
  * set yuv -> rgba conversion matrix
  */
 void
-vl_compositor_set_csc_matrix(struct vl_compositor_state *settings, const vl_csc_matrix *matrix);
+vl_compositor_set_csc_matrix(struct vl_compositor_state *settings,
+                             const vl_csc_matrix *matrix,
+                             float luma_min, float luma_max);
 
 /**
  * reset dirty area, so it's cleared with the clear colour
diff --git a/src/gallium/state_trackers/va/context.c b/src/gallium/state_trackers/va/context.c
index 51abd87..402fbb2 100644
--- a/src/gallium/state_trackers/va/context.c
+++ b/src/gallium/state_trackers/va/context.c
@@ -159,7 +159,7 @@ VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
    vl_compositor_init_state(&drv->cstate, drv->pipe);
 
    vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &drv->csc);
-   vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc);
+   vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc, 1.0f, 0.0f);
    pipe_mutex_init(drv->mutex);
 
    ctx->pDriverData = (void *)drv;
diff --git a/src/gallium/state_trackers/vdpau/mixer.c b/src/gallium/state_trackers/vdpau/mixer.c
index dec79ff..d3fb01f 100644
--- a/src/gallium/state_trackers/vdpau/mixer.c
+++ b/src/gallium/state_trackers/vdpau/mixer.c
@@ -69,7 +69,7 @@ vlVdpVideoMixerCreate(VdpDevice device,
 
    vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &vmixer->csc);
    if (!debug_get_bool_option("G3DVL_NO_CSC", FALSE))
-      vl_compositor_set_csc_matrix(&vmixer->cstate, (const vl_csc_matrix *)&vmixer->csc);
+      vl_compositor_set_csc_matrix(&vmixer->cstate, (const vl_csc_matrix *)&vmixer->csc, 1.0f, 0.0f);
 
    *mixer = vlAddDataHTAB(vmixer);
    if (*mixer == 0) {
@@ -92,7 +92,6 @@ vlVdpVideoMixerCreate(VdpDevice device,
       case VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8:
       case VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9:
       case VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE:
-      case VDP_VIDEO_MIXER_FEATURE_LUMA_KEY:
          break;
 
       case VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL:
@@ -107,6 +106,10 @@ vlVdpVideoMixerCreate(VdpDevice device,
          vmixer->noise_reduction.supported = true;
          break;
 
+      case VDP_VIDEO_MIXER_FEATURE_LUMA_KEY:
+         vmixer->luma_key.supported = true;
+         break;
+
       default: goto no_params;
       }
    }
@@ -148,8 +151,8 @@ vlVdpVideoMixerCreate(VdpDevice device,
                 vmixer->video_height, max_size);
       goto no_params;
    }
-   vmixer->luma_key_min = 0.f;
-   vmixer->luma_key_max = 1.f;
+   vmixer->luma_key.luma_min = 1.0f;
+   vmixer->luma_key.luma_max = 0.0f;
    pipe_mutex_unlock(dev->mutex);
 
    return VDP_STATUS_OK;
@@ -199,6 +202,7 @@ vlVdpVideoMixerDestroy(VdpVideoMixer mixer)
       vl_matrix_filter_cleanup(vmixer->sharpness.filter);
       FREE(vmixer->sharpness.filter);
    }
+
    pipe_mutex_unlock(vmixer->device->mutex);
    DeviceReference(&vmixer->device, NULL);
 
@@ -356,6 +360,7 @@ VdpStatus vlVdpVideoMixerRender(VdpVideoMixer mixer,
       if (vmixer->sharpness.filter)
          vl_matrix_filter_render(vmixer->sharpness.filter,
                                  dst->sampler_view, dst->surface);
+
    }
    pipe_mutex_unlock(vmixer->device->mutex);
 
@@ -490,7 +495,6 @@ vlVdpVideoMixerGetFeatureSupport(VdpVideoMixer mixer,
       case VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8:
       case VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9:
       case VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE:
-      case VDP_VIDEO_MIXER_FEATURE_LUMA_KEY:
          feature_supports[i] = false;
          break;
 
@@ -506,6 +510,10 @@ vlVdpVideoMixerGetFeatureSupport(VdpVideoMixer mixer,
          feature_supports[i] = vmixer->noise_reduction.supported;
          break;
 
+      case VDP_VIDEO_MIXER_FEATURE_LUMA_KEY:
+         feature_supports[i] = vmixer->luma_key.supported;
+         break;
+
       default:
          return VDP_STATUS_INVALID_VIDEO_MIXER_FEATURE;
       }
@@ -548,7 +556,6 @@ vlVdpVideoMixerSetFeatureEnables(VdpVideoMixer mixer,
       case VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8:
       case VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9:
       case VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE:
-      case VDP_VIDEO_MIXER_FEATURE_LUMA_KEY:
          break;
 
       case VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL:
@@ -566,6 +573,13 @@ vlVdpVideoMixerSetFeatureEnables(VdpVideoMixer mixer,
          vlVdpVideoMixerUpdateNoiseReductionFilter(vmixer);
          break;
 
+      case VDP_VIDEO_MIXER_FEATURE_LUMA_KEY:
+         vmixer->luma_key.enabled = feature_enables[i];
+         if (!debug_get_bool_option("G3DVL_NO_CSC", FALSE))
+            vl_compositor_set_csc_matrix(&vmixer->cstate, (const vl_csc_matrix *)&vmixer->csc,
+                                         vmixer->luma_key.luma_min, vmixer->luma_key.luma_max);
+         break;
+
       default:
          pipe_mutex_unlock(vmixer->device->mutex);
          return VDP_STATUS_INVALID_VIDEO_MIXER_FEATURE;
@@ -610,7 +624,6 @@ vlVdpVideoMixerGetFeatureEnables(VdpVideoMixer mixer,
       case VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8:
       case VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9:
       case VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE:
-      case VDP_VIDEO_MIXER_FEATURE_LUMA_KEY:
          break;
 
       case VDP_VIDEO_MIXER_FEATURE_SHARPNESS:
@@ -621,6 +634,10 @@ vlVdpVideoMixerGetFeatureEnables(VdpVideoMixer mixer,
          feature_enables[i] = vmixer->noise_reduction.enabled;
          break;
 
+      case VDP_VIDEO_MIXER_FEATURE_LUMA_KEY:
+         feature_enables[i] = vmixer->luma_key.enabled;
+         break;
+
       default:
          return VDP_STATUS_INVALID_VIDEO_MIXER_FEATURE;
       }
@@ -663,6 +680,7 @@ vlVdpVideoMixerSetAttributeValues(VdpVideoMixer mixer,
          color.f[3] = background_color->alpha;
          vl_compositor_set_clear_color(&vmixer->cstate, &color);
          break;
+
       case VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX:
          vdp_csc = attribute_values[i];
          vmixer->custom_csc = !!vdp_csc;
@@ -671,17 +689,16 @@ vlVdpVideoMixerSetAttributeValues(VdpVideoMixer mixer,
          else
             memcpy(vmixer->csc, vdp_csc, sizeof(vl_csc_matrix));
          if (!debug_get_bool_option("G3DVL_NO_CSC", FALSE))
-            vl_compositor_set_csc_matrix(&vmixer->cstate, (const vl_csc_matrix *)&vmixer->csc);
+            vl_compositor_set_csc_matrix(&vmixer->cstate, (const vl_csc_matrix *)&vmixer->csc,
+                                         vmixer->luma_key.luma_min, vmixer->luma_key.luma_max);
          break;
 
       case VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL:
-
          val = *(float*)attribute_values[i];
          if (val < 0.f || val > 1.f) {
             ret = VDP_STATUS_INVALID_VALUE;
             goto fail;
          }
-
          vmixer->noise_reduction.level = val * 10;
          vlVdpVideoMixerUpdateNoiseReductionFilter(vmixer);
          break;
@@ -692,25 +709,30 @@ vlVdpVideoMixerSetAttributeValues(VdpVideoMixer mixer,
             ret = VDP_STATUS_INVALID_VALUE;
             goto fail;
          }
-         vmixer->luma_key_min = val;
+         vmixer->luma_key.luma_min = val;
+         if (!debug_get_bool_option("G3DVL_NO_CSC", FALSE))
+            vl_compositor_set_csc_matrix(&vmixer->cstate, (const vl_csc_matrix *)&vmixer->csc,
+                                         vmixer->luma_key.luma_min, vmixer->luma_key.luma_max);
          break;
+
       case VDP_VIDEO_MIXER_ATTRIBUTE_LUMA_KEY_MAX_LUMA:
          val = *(float*)attribute_values[i];
          if (val < 0.f || val > 1.f) {
             ret = VDP_STATUS_INVALID_VALUE;
             goto fail;
          }
-         vmixer->luma_key_max = val;
+         vmixer->luma_key.luma_max = val;
+         if (!debug_get_bool_option("G3DVL_NO_CSC", FALSE))
+            vl_compositor_set_csc_matrix(&vmixer->cstate, (const vl_csc_matrix *)&vmixer->csc,
+                                         vmixer->luma_key.luma_min, vmixer->luma_key.luma_max);
          break;
 
       case VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL:
-
          val = *(float*)attribute_values[i];
          if (val < -1.f || val > 1.f) {
             ret = VDP_STATUS_INVALID_VALUE;
             goto fail;
          }
-
          vmixer->sharpness.value = val;
          vlVdpVideoMixerUpdateSharpnessFilter(vmixer);
          break;
@@ -723,6 +745,7 @@ vlVdpVideoMixerSetAttributeValues(VdpVideoMixer mixer,
          vmixer->skip_chroma_deint = *(uint8_t*)attribute_values[i];
          vlVdpVideoMixerUpdateDeinterlaceFilter(vmixer);
          break;
+
       default:
          ret = VDP_STATUS_INVALID_VIDEO_MIXER_ATTRIBUTE;
          goto fail;
@@ -814,10 +837,10 @@ vlVdpVideoMixerGetAttributeValues(VdpVideoMixer mixer,
          break;
 
       case VDP_VIDEO_MIXER_ATTRIBUTE_LUMA_KEY_MIN_LUMA:
-         *(float*)attribute_values[i] = vmixer->luma_key_min;
+         *(float*)attribute_values[i] = vmixer->luma_key.luma_min;
          break;
       case VDP_VIDEO_MIXER_ATTRIBUTE_LUMA_KEY_MAX_LUMA:
-         *(float*)attribute_values[i] = vmixer->luma_key_max;
+         *(float*)attribute_values[i] = vmixer->luma_key.luma_max;
          break;
       case VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL:
          *(float*)attribute_values[i] = vmixer->sharpness.value;
diff --git a/src/gallium/state_trackers/vdpau/output.c b/src/gallium/state_trackers/vdpau/output.c
index 2192f71..8a064e8 100644
--- a/src/gallium/state_trackers/vdpau/output.c
+++ b/src/gallium/state_trackers/vdpau/output.c
@@ -492,9 +492,9 @@ vlVdpOutputSurfacePutBitsYCbCr(VdpOutputSurface surface,
    if (!csc_matrix) {
       vl_csc_matrix csc;
       vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, 1, &csc);
-      vl_compositor_set_csc_matrix(cstate, (const vl_csc_matrix*)&csc);
+      vl_compositor_set_csc_matrix(cstate, (const vl_csc_matrix*)&csc, 1.0f, 0.0f);
    } else {
-      vl_compositor_set_csc_matrix(cstate, csc_matrix);
+      vl_compositor_set_csc_matrix(cstate, csc_matrix, 1.0f, 0.0f);
    }
 
    vl_compositor_clear_layers(cstate);
diff --git a/src/gallium/state_trackers/vdpau/vdpau_private.h b/src/gallium/state_trackers/vdpau/vdpau_private.h
index 3b6647e..8673c6a 100644
--- a/src/gallium/state_trackers/vdpau/vdpau_private.h
+++ b/src/gallium/state_trackers/vdpau/vdpau_private.h
@@ -363,6 +363,11 @@ typedef struct
    struct vl_compositor_state cstate;
 
    struct {
+       bool supported, enabled;
+       float luma_min, luma_max;
+   } luma_key;
+
+   struct {
 	  bool supported, enabled, spatial;
 	  struct vl_deint_filter *filter;
    } deint;
@@ -382,7 +387,6 @@ typedef struct
    unsigned video_width, video_height;
    enum pipe_video_chroma_format chroma_format;
    unsigned max_layers, skip_chroma_deint;
-   float luma_key_min, luma_key_max;
 
    bool custom_csc;
    vl_csc_matrix csc;
diff --git a/src/gallium/state_trackers/xvmc/attributes.c b/src/gallium/state_trackers/xvmc/attributes.c
index 2d8f00b..3757056 100644
--- a/src/gallium/state_trackers/xvmc/attributes.c
+++ b/src/gallium/state_trackers/xvmc/attributes.c
@@ -110,7 +110,7 @@ Status XvMCSetAttribute(Display *dpy, XvMCContext *context, Atom attribute, int
       context_priv->color_standard,
       &context_priv->procamp, true, &csc
    );
-   vl_compositor_set_csc_matrix(&context_priv->cstate, (const vl_csc_matrix *)&csc);
+   vl_compositor_set_csc_matrix(&context_priv->cstate, (const vl_csc_matrix *)&csc, 1.0f, 0.0f);
 
    XVMC_MSG(XVMC_TRACE, "[XvMC] Set attribute %s to value %d.\n", attr, value);
 
diff --git a/src/gallium/state_trackers/xvmc/context.c b/src/gallium/state_trackers/xvmc/context.c
index a6991ab..e9014c8 100644
--- a/src/gallium/state_trackers/xvmc/context.c
+++ b/src/gallium/state_trackers/xvmc/context.c
@@ -293,7 +293,7 @@ Status XvMCCreateContext(Display *dpy, XvPortID port, int surface_type_id,
       context_priv->color_standard,
       &context_priv->procamp, true, &csc
    );
-   vl_compositor_set_csc_matrix(&context_priv->cstate, (const vl_csc_matrix *)&csc);
+   vl_compositor_set_csc_matrix(&context_priv->cstate, (const vl_csc_matrix *)&csc, 1.0f, 0.0f);
 
    context_priv->vscreen = vscreen;
    context_priv->pipe = pipe;
-- 
2.5.5



More information about the mesa-dev mailing list