[PATCH 03/17] winegstreamer: Implement ::Process(Input/Output) for audio conversion transform.

Zebediah Figura (she/her) zfigura at codeweavers.com
Thu Dec 3 21:26:12 CST 2020


On 12/3/20 3:55 PM, Derek Lesho wrote:
> Signed-off-by: Derek Lesho <dlesho at codeweavers.com>
> ---
> v1(v6):
>   - Move unused hr to later patch.
>   - Fix extra parentheses.
>   - Place converter declaration more consistently.
> ---
>  dlls/winegstreamer/audioconvert.c | 175 +++++++++++++++++++++++++++++-
>  dlls/winegstreamer/gst_private.h  |   1 +
>  dlls/winegstreamer/mfplat.c       |  69 ++++++++++++
>  3 files changed, 239 insertions(+), 6 deletions(-)
> 
> diff --git a/dlls/winegstreamer/audioconvert.c b/dlls/winegstreamer/audioconvert.c
> index 7fb0dee99f6..631c57d6d55 100644
> --- a/dlls/winegstreamer/audioconvert.c
> +++ b/dlls/winegstreamer/audioconvert.c
> @@ -40,6 +40,8 @@ struct audio_converter
>      IMFMediaType *input_type;
>      IMFMediaType *output_type;
>      CRITICAL_SECTION cs;
> +    BOOL inflight;
> +    GstElement *container, *appsrc, *audioconvert, *resampler, *appsink;

Both "resampler" and "audioconvert" can be local variables, I think.

>  };
>  
>  static struct audio_converter *impl_audio_converter_from_IMFTransform(IMFTransform *iface)
> @@ -85,6 +87,7 @@ static ULONG WINAPI audio_converter_Release(IMFTransform *iface)
>      {
>          transform->cs.DebugInfo->Spare[0] = 0;
>          DeleteCriticalSection(&transform->cs);
> +        gst_object_unref(transform->container);
>          heap_free(transform);
>      }
>  
> @@ -311,7 +314,8 @@ static HRESULT WINAPI audio_converter_SetInputType(IMFTransform *iface, DWORD id
>          if (!(input_caps = caps_from_mf_media_type(type)))
>              return MF_E_INVALIDTYPE;
>  
> -        gst_caps_unref(input_caps);
> +        if (flags & MFT_SET_TYPE_TEST_ONLY)
> +            gst_caps_unref(input_caps);
>      }
>  
>      if (flags & MFT_SET_TYPE_TEST_ONLY)
> @@ -320,6 +324,7 @@ static HRESULT WINAPI audio_converter_SetInputType(IMFTransform *iface, DWORD id
>      EnterCriticalSection(&converter->cs);
>  
>      hr = S_OK;
> +    gst_element_set_state(converter->container, GST_STATE_READY);

Presumably you also want to reset "inflight" here...

>  
>      if (type)
>      {
> @@ -329,6 +334,9 @@ static HRESULT WINAPI audio_converter_SetInputType(IMFTransform *iface, DWORD id
>          if (SUCCEEDED(hr))
>              hr = IMFMediaType_CopyAllItems(type, (IMFAttributes *) converter->input_type);
>  
> +        g_object_set(converter->appsrc, "caps", input_caps, NULL);
> +        gst_caps_unref(input_caps);
> +
>          if (FAILED(hr))
>          {
>              IMFMediaType_Release(converter->input_type);
> @@ -341,6 +349,9 @@ static HRESULT WINAPI audio_converter_SetInputType(IMFTransform *iface, DWORD id
>          converter->input_type = NULL;
>      }
>  
> +    if (converter->input_type && converter->output_type)
> +        gst_element_set_state(converter->container, GST_STATE_PLAYING);
> +
>      LeaveCriticalSection(&converter->cs);
>  
>      return hr;
> @@ -386,7 +397,8 @@ static HRESULT WINAPI audio_converter_SetOutputType(IMFTransform *iface, DWORD i
>          if (!(output_caps = caps_from_mf_media_type(type)))
>              return MF_E_INVALIDTYPE;
>  
> -        gst_caps_unref(output_caps);
> +        if (flags & MFT_SET_TYPE_TEST_ONLY)
> +            gst_caps_unref(output_caps);

This is something of a style nitpick, but having variables that are
conditionally valid like this is often better avoided. Reorganizing the
function to be something more like

if (CLEAR)
{
    if (TEST_ONLY)
        return;
    EnterCriticalSection();
    // clear
    LeaveCriticalSection();
    return;
}

// validate
// convert to caps
if (TEST_ONLY)
    return;
EnterCriticalSection();
// clear
LeaveCriticalSection();

might be more idiomatic.

>      }
>  
>      if (flags & MFT_SET_TYPE_TEST_ONLY)
> @@ -395,6 +407,7 @@ static HRESULT WINAPI audio_converter_SetOutputType(IMFTransform *iface, DWORD i
>      EnterCriticalSection(&converter->cs);
>  
>      hr = S_OK;
> +    gst_element_set_state(converter->container, GST_STATE_READY);

...and here.

>  
>      if (type)
>      {
> @@ -404,6 +417,9 @@ static HRESULT WINAPI audio_converter_SetOutputType(IMFTransform *iface, DWORD i
>          if (SUCCEEDED(hr))
>              hr = IMFMediaType_CopyAllItems(type, (IMFAttributes *) converter->output_type);
>  
> +        g_object_set(converter->appsink, "caps", output_caps, NULL);
> +        gst_caps_unref(output_caps);
> +
>          if (FAILED(hr))
>          {
>              IMFMediaType_Release(converter->output_type);
> @@ -416,6 +432,9 @@ static HRESULT WINAPI audio_converter_SetOutputType(IMFTransform *iface, DWORD i
>          converter->output_type = NULL;
>      }
>  
> +    if (converter->input_type && converter->output_type)
> +        gst_element_set_state(converter->container, GST_STATE_PLAYING);
> +
>      LeaveCriticalSection(&converter->cs);
>  
>      return hr;
> @@ -479,17 +498,102 @@ static HRESULT WINAPI audio_converter_ProcessMessage(IMFTransform *iface, MFT_ME
>  
>  static HRESULT WINAPI audio_converter_ProcessInput(IMFTransform *iface, DWORD id, IMFSample *sample, DWORD flags)
>  {
> -    FIXME("%p, %u, %p, %#x.\n", iface, id, sample, flags);
> +    GstBuffer *gst_buffer;
> +    int ret;
>  
> -    return E_NOTIMPL;
> +    struct audio_converter *converter = impl_audio_converter_from_IMFTransform(iface);
> +
> +    TRACE("%p, %u, %p, %#x.\n", iface, id, sample, flags);
> +
> +    if (flags)
> +        WARN("Unsupported flags %#x\n", flags);
> +
> +    if (id != 0)
> +        return MF_E_INVALIDSTREAMNUMBER;
> +
> +    EnterCriticalSection(&converter->cs);
> +
> +    if (!converter->input_type || !converter->output_type)
> +    {
> +        LeaveCriticalSection(&converter->cs);
> +        return MF_E_TRANSFORM_TYPE_NOT_SET;
> +    }
> +
> +    if (converter->inflight)
> +    {
> +        LeaveCriticalSection(&converter->cs);
> +        return MF_E_NOTACCEPTING;
> +    }
> +
> +    if (!(gst_buffer = gst_buffer_from_mf_sample(sample)))
> +    {
> +        LeaveCriticalSection(&converter->cs);
> +        return E_FAIL;
> +    }
> +
> +    g_signal_emit_by_name(converter->appsrc, "push-buffer", gst_buffer, &ret);
> +    gst_buffer_unref(gst_buffer);
> +    if (ret != GST_FLOW_OK)
> +    {
> +        ERR("Couldn't push buffer ret = %d (%s)\n", ret, gst_flow_get_name(ret));

Is there a point in tracing the numeric value as well?

> +        LeaveCriticalSection(&converter->cs);
> +        return E_FAIL;
> +    }
> +
> +    converter->inflight = TRUE;
> +    LeaveCriticalSection(&converter->cs);
> +
> +    return S_OK;
>  }
>  
>  static HRESULT WINAPI audio_converter_ProcessOutput(IMFTransform *iface, DWORD flags, DWORD count,
>          MFT_OUTPUT_DATA_BUFFER *samples, DWORD *status)
>  {
> -    FIXME("%p, %#x, %u, %p, %p.\n", iface, flags, count, samples, status);
> +    GstSample *sample;
>  
> -    return E_NOTIMPL;
> +    struct audio_converter *converter = impl_audio_converter_from_IMFTransform(iface);
> +
> +    TRACE("%p, %#x, %u, %p, %p.\n", iface, flags, count, samples, status);
> +
> +    if (flags)
> +        WARN("Unsupported flags %#x\n", flags);
> +
> +    if (!count)
> +        return S_OK;
> +
> +    if (count != 1)
> +        return MF_E_INVALIDSTREAMNUMBER;
> +
> +    if (samples[0].dwStreamID != 0)
> +        return MF_E_INVALIDSTREAMNUMBER;
> +
> +    EnterCriticalSection(&converter->cs);
> +
> +    if (!converter->input_type || !converter->output_type)
> +    {
> +        LeaveCriticalSection(&converter->cs);
> +        return MF_E_TRANSFORM_TYPE_NOT_SET;
> +    }
> +
> +    if (!converter->inflight)
> +    {
> +        LeaveCriticalSection(&converter->cs);
> +        return MF_E_TRANSFORM_NEED_MORE_INPUT;
> +    }
> +
> +    g_signal_emit_by_name(converter->appsink, "pull-sample", &sample);

I'm not fully familiar with mfplat, but this call may block, and it
doesn't look like ProcessOutput() is supposed to. You probably want
"try-pull-sample" instead.

> +
> +    converter->inflight =  FALSE;

Nitpick, but this looks like a spacing error.

> +
> +    samples[0].pSample = mf_sample_from_gst_buffer(gst_sample_get_buffer(sample));
> +    gst_sample_unref(sample);
> +    samples[0].dwStatus = S_OK;
> +    samples[0].pEvents = NULL;
> +    *status = 0;
> +
> +    LeaveCriticalSection(&converter->cs);
> +
> +    return S_OK;
>  }
>  
>  static const IMFTransformVtbl audio_converter_vtbl =
> @@ -537,6 +641,65 @@ HRESULT audio_converter_create(REFIID riid, void **ret)
>      InitializeCriticalSection(&object->cs);
>      object->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": audio_converter_lock");
>  
> +    object->container = gst_bin_new(NULL);
> +
> +    if (!(object->appsrc = gst_element_factory_make("appsrc", NULL)))
> +    {
> +        ERR("Failed to create appsrc, are %u-bit Gstreamer \"base\" plugins installed?\n",
> +                8 * (int)sizeof(void *));
> +        IMFTransform_Release(&object->IMFTransform_iface);
> +        return E_FAIL;
> +    }
> +    gst_bin_add(GST_BIN(object->container), object->appsrc);
> +
> +    if (!(object->audioconvert = gst_element_factory_make("audioconvert", NULL)))
> +    {
> +        ERR("Failed to create audioconvert, are %u-bit Gstreamer \"base\" plugins installed?\n",
> +                8 * (int)sizeof(void *));
> +        IMFTransform_Release(&object->IMFTransform_iface);
> +        return E_FAIL;
> +    }
> +    gst_bin_add(GST_BIN(object->container), object->audioconvert);
> +
> +    if (!(object->resampler = gst_element_factory_make("audioresample", NULL)))
> +    {
> +        ERR("Failed to create audioresample, are %u-bit Gstreamer \"base\" plugins installed?\n",
> +                8 * (int)sizeof(void *));
> +        IMFTransform_Release(&object->IMFTransform_iface);
> +        return E_FAIL;
> +    }
> +    gst_bin_add(GST_BIN(object->container), object->resampler);
> +
> +    if (!(object->appsink = gst_element_factory_make("appsink", NULL)))
> +    {
> +        ERR("Failed to create appsink, are %u-bit Gstreamer \"base\" plugins installed?\n",
> +                8 * (int)sizeof(void *));
> +        IMFTransform_Release(&object->IMFTransform_iface);
> +        return E_FAIL;
> +    }
> +    gst_bin_add(GST_BIN(object->container), object->appsink);
> +
> +    if (!gst_element_link(object->appsrc, object->audioconvert))
> +    {
> +        ERR("Failed to link appsrc to audioconvert\n");
> +        IMFTransform_Release(&object->IMFTransform_iface);
> +        return E_FAIL;
> +    }
> +
> +    if (!gst_element_link(object->audioconvert, object->resampler))
> +    {
> +        ERR("Failed to link audioconvert to resampler\n");
> +        IMFTransform_Release(&object->IMFTransform_iface);
> +        return E_FAIL;
> +    }
> +
> +    if (!gst_element_link(object->resampler, object->appsink))
> +    {
> +        ERR("Failed to link resampler to appsink\n");
> +        IMFTransform_Release(&object->IMFTransform_iface);
> +        return E_FAIL;
> +    }
> +
>      *ret = &object->IMFTransform_iface;
>      return S_OK;
>  }
> diff --git a/dlls/winegstreamer/gst_private.h b/dlls/winegstreamer/gst_private.h
> index 9518f721504..14b6a011ac2 100644
> --- a/dlls/winegstreamer/gst_private.h
> +++ b/dlls/winegstreamer/gst_private.h
> @@ -82,6 +82,7 @@ HRESULT winegstreamer_stream_handler_create(REFIID riid, void **obj) DECLSPEC_HI
>  IMFMediaType *mf_media_type_from_caps(const GstCaps *caps) DECLSPEC_HIDDEN;
>  GstCaps *caps_from_mf_media_type(IMFMediaType *type) DECLSPEC_HIDDEN;
>  IMFSample *mf_sample_from_gst_buffer(GstBuffer *in) DECLSPEC_HIDDEN;
> +GstBuffer *gst_buffer_from_mf_sample(IMFSample *in) DECLSPEC_HIDDEN;
>  
>  HRESULT winegstreamer_stream_handler_create(REFIID riid, void **obj) DECLSPEC_HIDDEN;
>  
> diff --git a/dlls/winegstreamer/mfplat.c b/dlls/winegstreamer/mfplat.c
> index f300988fc5c..883084b2d89 100644
> --- a/dlls/winegstreamer/mfplat.c
> +++ b/dlls/winegstreamer/mfplat.c
> @@ -865,3 +865,72 @@ done:
>  
>      return out;
>  }
> +
> +GstBuffer* gst_buffer_from_mf_sample(IMFSample *mf_sample)

Please use consistent spacing around pointers.

> +{
> +    GstBuffer *out = gst_buffer_new();
> +    IMFMediaBuffer *mf_buffer = NULL;
> +    LONGLONG duration, time;
> +    DWORD buffer_count;
> +    unsigned int i;
> +    HRESULT hr;
> +
> +    if (FAILED(hr = IMFSample_GetSampleDuration(mf_sample, &duration)))
> +        goto fail;
> +
> +    if (FAILED(hr = IMFSample_GetSampleTime(mf_sample, &time)))
> +        goto fail;
> +
> +    GST_BUFFER_DURATION(out) = duration;
> +    GST_BUFFER_PTS(out) = time * 100;
> +
> +    if (FAILED(hr = IMFSample_GetBufferCount(mf_sample, &buffer_count)))
> +        goto fail;
> +
> +    for (i = 0; i < buffer_count; i++)
> +    {
> +        DWORD buffer_size;
> +        GstMapInfo map_info;
> +        GstMemory *memory;
> +        BYTE *buf_data;
> +
> +        if (FAILED(hr = IMFSample_GetBufferByIndex(mf_sample, i, &mf_buffer)))
> +            goto fail;
> +
> +        if (FAILED(hr = IMFMediaBuffer_GetCurrentLength(mf_buffer, &buffer_size)))
> +            goto fail;
> +
> +        memory = gst_allocator_alloc(NULL, buffer_size, NULL);
> +        gst_memory_resize(memory, 0, buffer_size);
> +
> +        if (!(gst_memory_map(memory, &map_info, GST_MAP_WRITE)))

These parentheses are somewhat redundant...

> +        {
> +            hr = E_FAIL;
> +            goto fail;
> +        }
> +
> +        if (FAILED(hr = IMFMediaBuffer_Lock(mf_buffer, &buf_data, NULL, NULL)))
> +            goto fail;
> +
> +        memcpy(map_info.data, buf_data, buffer_size);
> +
> +        if (FAILED(hr = IMFMediaBuffer_Unlock(mf_buffer)))
> +            goto fail;
> +
> +        gst_memory_unmap(memory, &map_info);
> +
> +        gst_buffer_append_memory(out, memory);
> +
> +        IMFMediaBuffer_Release(mf_buffer);
> +        mf_buffer = NULL;
> +    }
> +
> +    return out;
> +
> +fail:
> +    ERR("Failed to copy IMFSample to GstBuffer, hr = %#x\n", hr);
> +    if (mf_buffer)
> +        IMFMediaBuffer_Release(mf_buffer);
> +    gst_buffer_unref(out);
> +    return NULL;
> +}
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_0x0D9D358A07A17840.asc
Type: application/pgp-keys
Size: 1769 bytes
Desc: not available
URL: <http://www.winehq.org/pipermail/wine-devel/attachments/20201203/dfb02449/attachment-0001.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 495 bytes
Desc: OpenPGP digital signature
URL: <http://www.winehq.org/pipermail/wine-devel/attachments/20201203/dfb02449/attachment-0001.sig>


More information about the wine-devel mailing list