diff mbox

[FFmpeg-devel] lavf/os_support: Add safe win32 dlopen/dlclose/dlsym functions.

Message ID 20161029152234.GV4602@nb4
State New
Headers show

Commit Message

Michael Niedermayer Oct. 29, 2016, 3:22 p.m. UTC
On Sat, Oct 29, 2016 at 06:35:19PM +1100, Matt Oliver wrote:
> >
> > FYI, last time i tried to include this file from libavcodec i was told
> >> not to since it's libavformat specific.
> >>
> >> The proper place for these wrappers is probably the compat folder, in
> >> a new file similar to w32threads.h
> >>
> >
> OK finally updated the patch. it now adds the safe dll function into a new
> file "compat/w32dlfcn.h" and each source file that needs this header has
> been updated accordingly. Otherwise the code is the same as the last
> version that was tested by Michael.
> 
> If there are no other outstanding objections then let me know so i can push
> this.

>  w32dlfcn.h |   83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 83 insertions(+)
> 421c4543291a80c2e52c7e029da921ffcf071f34  0001-compat-w32dlfcn.h-Add-safe-win32-dlopen-dlclose-dlsy.patch
> From 37327de38eb366421ff66246e18cd93b0eb7026a Mon Sep 17 00:00:00 2001
> From: Matt Oliver <protogonoi@gmail.com>
> Date: Sat, 29 Oct 2016 18:28:27 +1100
> Subject: [PATCH] compat/w32dlfcn.h: Add safe win32 dlopen/dlclose/dlsym
>  functions.
> 
> ---
>  compat/w32dlfcn.h | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 83 insertions(+)
>  create mode 100644 compat/w32dlfcn.h
> 
> diff --git a/compat/w32dlfcn.h b/compat/w32dlfcn.h
> new file mode 100644
> index 0000000..23db85d
> --- /dev/null
> +++ b/compat/w32dlfcn.h
> @@ -0,0 +1,83 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#ifndef COMPAT_W32DLFCN_H
> +#define COMPAT_W32DLFCN_H
> +
> +#ifdef _WIN32
> +#include <windows.h>
> +#if _WIN32_WINNT < 0x0602
> +#include "libavutil/wchar_filename.h"
> +#endif
> +/**
> + * Safe function used to open dynamic libs. This attempts to improve program security
> + * by removing the current directory from the dll search path. Only dll's found in the
> + * executable or system directory are allowed to be loaded.
> + * @param name  The dynamic lib name.
> + * @return A handle to the opened lib.
> + */
> +static inline HMODULE win32_dlopen(const char *name)
> +{
> +#if _WIN32_WINNT < 0x0602
> +    // Need to check if KB2533623 is available
> +    if (!GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "SetDefaultDllDirectories")) {
> +        HMODULE module = NULL;
> +        wchar_t *path = NULL, *name_w = NULL;
> +        DWORD pathlen;
> +        if (utf8towchar(name, &name_w))
> +            goto exit;
> +        path = (wchar_t *)av_mallocz_array(MAX_PATH, sizeof(wchar_t));
> +        // Try local directory first
> +        pathlen = GetModuleFileNameW(NULL, path, MAX_PATH);
> +        pathlen = wcsrchr(path, '\\') - path;
> +        if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH)
> +            goto exit;
> +        path[pathlen] = '\\';
> +        wcscpy(path + pathlen + 1, name_w);
> +        module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
> +        if (module == NULL) {
> +            // Next try System32 directory
> +            pathlen = GetSystemDirectoryW(path, MAX_PATH);
> +            if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH)
> +                goto exit;
> +            path[pathlen] = '\\';
> +            wcscpy(path + pathlen + 1, name_w);
> +            module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
> +        }
> +exit:
> +        av_free(path);
> +        av_free(name_w);
> +        return module;
> +    }
> +#endif
> +#ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
> +#   define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200
> +#endif
> +#ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
> +#   define LOAD_LIBRARY_SEARCH_SYSTEM32        0x00000800
> +#endif
> +    return LoadLibraryExA(name, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
> +}
> +#define dlopen(name, flags) win32_dlopen(name)
> +#define dlclose FreeLibrary
> +#define dlsym GetProcAddress
> +#else
> +#include <dlfcn.h>
> +#endif
> +
> +#endif /* COMPAT_W32DLFCN_H */
> \ No newline at end of file
> -- 
> 2.10.1.windows.1
> 

>  hwcontext_dxva2.c |   15 ++++++++-------
>  1 file changed, 8 insertions(+), 7 deletions(-)
> c3b4294a0e4734cfa780064ab66919f430e596bc  0002-avutil-hwcontext_dxva.c-Use-new-safe-dlopen-code.patch
> From 6b3937d020a795cb62a626c5ce1a94576b093b08 Mon Sep 17 00:00:00 2001
> From: Matt Oliver <protogonoi@gmail.com>
> Date: Sat, 29 Oct 2016 18:25:05 +1100
> Subject: [PATCH 2/4] avutil/hwcontext_dxva.c: Use new safe dlopen code.
> 
> ---
>  libavutil/hwcontext_dxva2.c | 15 ++++++++-------
>  1 file changed, 8 insertions(+), 7 deletions(-)
> 
> diff --git a/libavutil/hwcontext_dxva2.c b/libavutil/hwcontext_dxva2.c
> index e79254b..40a4a27 100644
> --- a/libavutil/hwcontext_dxva2.c
> +++ b/libavutil/hwcontext_dxva2.c
> @@ -37,6 +37,7 @@
>  #include "imgutils.h"
>  #include "pixdesc.h"
>  #include "pixfmt.h"
> +#include "compat/w32dlfcn.h"
>  
>  typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
>  typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
> @@ -318,10 +319,10 @@ static void dxva2_device_free(AVHWDeviceContext *ctx)
>          IDirect3D9_Release(priv->d3d9);
>  
>      if (priv->d3dlib)
> -        FreeLibrary(priv->d3dlib);
> +        dlclose(priv->d3dlib);
>  
>      if (priv->dxva2lib)
> -        FreeLibrary(priv->dxva2lib);
> +        dlclose(priv->dxva2lib);
>  
>      av_freep(&ctx->user_opaque);
>  }
> @@ -352,24 +353,24 @@ static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
>  
>      priv->device_handle = INVALID_HANDLE_VALUE;
>  
> -    priv->d3dlib = LoadLibrary("d3d9.dll");
> +    priv->d3dlib = dlopen("d3d9.dll", 0);
>      if (!priv->d3dlib) {
>          av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
>          return AVERROR_UNKNOWN;
>      }
> -    priv->dxva2lib = LoadLibrary("dxva2.dll");
> +    priv->dxva2lib = dlopen("dxva2.dll", 0);
>      if (!priv->dxva2lib) {
>          av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
>          return AVERROR_UNKNOWN;
>      }
>  
> -    createD3D = (pDirect3DCreate9 *)GetProcAddress(priv->d3dlib, "Direct3DCreate9");
> +    createD3D = (pDirect3DCreate9 *)dlsym(priv->d3dlib, "Direct3DCreate9");
>      if (!createD3D) {
>          av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
>          return AVERROR_UNKNOWN;
>      }
> -    createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(priv->dxva2lib,
> -                                                                  "DXVA2CreateDirect3DDeviceManager9");
> +    createDeviceManager = (pCreateDeviceManager9 *)dlsym(priv->dxva2lib,
> +                                                         "DXVA2CreateDirect3DDeviceManager9");
>      if (!createDeviceManager) {
>          av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
>          return AVERROR_UNKNOWN;
> -- 
> 2.10.1.windows.1
> 

>  configure              |    5 +----
>  libavformat/avisynth.c |   14 +++++---------
>  2 files changed, 6 insertions(+), 13 deletions(-)
> b1568f39504e5e14c924d27c8f11ba8f5816d68c  0003-avformat-avisynth.c-Use-new-safe-dlopen-code.patch
> From 633212cf1246b3fde61dd6515229e6a893005664 Mon Sep 17 00:00:00 2001
> From: Matt Oliver <protogonoi@gmail.com>
> Date: Sat, 29 Oct 2016 18:25:25 +1100
> Subject: [PATCH 3/4] avformat/avisynth.c: Use new safe dlopen code.

breaks --enable-avisynth on linux/ubuntu


[...]

Comments

Stephen Hutchinson Oct. 29, 2016, 4:41 p.m. UTC | #1
On 10/29/2016 11:22 AM, Michael Niedermayer wrote:
> On Sat, Oct 29, 2016 at 06:35:19PM +1100, Matt Oliver wrote:
>>  configure              |    5 +----
>>  libavformat/avisynth.c |   14 +++++---------
>>  2 files changed, 6 insertions(+), 13 deletions(-)
>> b1568f39504e5e14c924d27c8f11ba8f5816d68c  0003-avformat-avisynth.c-Use-new-safe-dlopen-code.patch
>> From 633212cf1246b3fde61dd6515229e6a893005664 Mon Sep 17 00:00:00 2001
>> From: Matt Oliver <protogonoi@gmail.com>
>> Date: Sat, 29 Oct 2016 18:25:25 +1100
>> Subject: [PATCH 3/4] avformat/avisynth.c: Use new safe dlopen code.
>
> breaks --enable-avisynth on linux/ubuntu
>
> --- config.h    2016-10-29 17:17:55.214014842 +0200
> +++ delth/config.h      2016-10-29 17:15:41.906012034 +0200
> @@ -1155,7 +1155,7 @@
>  #define CONFIG_AST_DEMUXER 1
>  #define CONFIG_AU_DEMUXER 1
>  #define CONFIG_AVI_DEMUXER 1
> -#define CONFIG_AVISYNTH_DEMUXER 1
> +#define CONFIG_AVISYNTH_DEMUXER 0
>  #define CONFIG_AVR_DEMUXER 1
>  #define CONFIG_AVS_DEMUXER 1
>  #define CONFIG_BETHSOFTVID_DEMUXER 1
>
> [...]
>

Yeah, libdl needs to be linked to on non-Windows, and the
check for it got removed with the rest of the 'enabled avisynth'
case in configure. Just putting dlopen under avisynth_demuxer_deps 
doesn't seem to be sufficient for that to work.
Matt Oliver Oct. 30, 2016, 4:13 a.m. UTC | #2
On 30 October 2016 at 03:41, Stephen Hutchinson <qyot27@gmail.com> wrote:

> On 10/29/2016 11:22 AM, Michael Niedermayer wrote:
>
>> On Sat, Oct 29, 2016 at 06:35:19PM +1100, Matt Oliver wrote:
>>
>>>  configure              |    5 +----
>>>  libavformat/avisynth.c |   14 +++++---------
>>>  2 files changed, 6 insertions(+), 13 deletions(-)
>>> b1568f39504e5e14c924d27c8f11ba8f5816d68c  0003-avformat-avisynth.c-Use-n
>>> ew-safe-dlopen-code.patch
>>> From 633212cf1246b3fde61dd6515229e6a893005664 Mon Sep 17 00:00:00 2001
>>> From: Matt Oliver <protogonoi@gmail.com>
>>> Date: Sat, 29 Oct 2016 18:25:25 +1100
>>> Subject: [PATCH 3/4] avformat/avisynth.c: Use new safe dlopen code.
>>>
>>
>> breaks --enable-avisynth on linux/ubuntu
>>
>> --- config.h    2016-10-29 17:17:55.214014842 +0200
>> +++ delth/config.h      2016-10-29 17:15:41.906012034 +0200
>> @@ -1155,7 +1155,7 @@
>>  #define CONFIG_AST_DEMUXER 1
>>  #define CONFIG_AU_DEMUXER 1
>>  #define CONFIG_AVI_DEMUXER 1
>> -#define CONFIG_AVISYNTH_DEMUXER 1
>> +#define CONFIG_AVISYNTH_DEMUXER 0
>>  #define CONFIG_AVR_DEMUXER 1
>>  #define CONFIG_AVS_DEMUXER 1
>>  #define CONFIG_BETHSOFTVID_DEMUXER 1
>>
>> [...]
>>
>>
> Yeah, libdl needs to be linked to on non-Windows, and the
> check for it got removed with the rest of the 'enabled avisynth'
> case in configure. Just putting dlopen under avisynth_demuxer_deps doesn't
> seem to be sufficient for that to work.


Looks like I missed a line, my apologies. Updated version attached.
Hendrik Leppkes Oct. 30, 2016, 9:21 a.m. UTC | #3
On Sun, Oct 30, 2016 at 5:13 AM, Matt Oliver <protogonoi@gmail.com> wrote:
> On 30 October 2016 at 03:41, Stephen Hutchinson <qyot27@gmail.com> wrote:
>
>> On 10/29/2016 11:22 AM, Michael Niedermayer wrote:
>>
>>> On Sat, Oct 29, 2016 at 06:35:19PM +1100, Matt Oliver wrote:
>>>
>>>>  configure              |    5 +----
>>>>  libavformat/avisynth.c |   14 +++++---------
>>>>  2 files changed, 6 insertions(+), 13 deletions(-)
>>>> b1568f39504e5e14c924d27c8f11ba8f5816d68c  0003-avformat-avisynth.c-Use-n
>>>> ew-safe-dlopen-code.patch
>>>> From 633212cf1246b3fde61dd6515229e6a893005664 Mon Sep 17 00:00:00 2001
>>>> From: Matt Oliver <protogonoi@gmail.com>
>>>> Date: Sat, 29 Oct 2016 18:25:25 +1100
>>>> Subject: [PATCH 3/4] avformat/avisynth.c: Use new safe dlopen code.
>>>>
>>>
>>> breaks --enable-avisynth on linux/ubuntu
>>>
>>> --- config.h    2016-10-29 17:17:55.214014842 +0200
>>> +++ delth/config.h      2016-10-29 17:15:41.906012034 +0200
>>> @@ -1155,7 +1155,7 @@
>>>  #define CONFIG_AST_DEMUXER 1
>>>  #define CONFIG_AU_DEMUXER 1
>>>  #define CONFIG_AVI_DEMUXER 1
>>> -#define CONFIG_AVISYNTH_DEMUXER 1
>>> +#define CONFIG_AVISYNTH_DEMUXER 0
>>>  #define CONFIG_AVR_DEMUXER 1
>>>  #define CONFIG_AVS_DEMUXER 1
>>>  #define CONFIG_BETHSOFTVID_DEMUXER 1
>>>
>>> [...]
>>>
>>>
>> Yeah, libdl needs to be linked to on non-Windows, and the
>> check for it got removed with the rest of the 'enabled avisynth'
>> case in configure. Just putting dlopen under avisynth_demuxer_deps doesn't
>> seem to be sufficient for that to work.
>
>
> Looks like I missed a line, my apologies. Updated version attached.
>

Looking at the configure changes - having a dependency on both dlopen
and LoadLibrary sounds odd. Shouldn't it be either, not both?

- Hendrik
Matt Oliver Oct. 30, 2016, 11:31 a.m. UTC | #4
On 30 October 2016 at 20:21, Hendrik Leppkes <h.leppkes@gmail.com> wrote:

> On Sun, Oct 30, 2016 at 5:13 AM, Matt Oliver <protogonoi@gmail.com> wrote:
> > On 30 October 2016 at 03:41, Stephen Hutchinson <qyot27@gmail.com>
> wrote:
> >
> >> On 10/29/2016 11:22 AM, Michael Niedermayer wrote:
> >>
> >>> On Sat, Oct 29, 2016 at 06:35:19PM +1100, Matt Oliver wrote:
> >>>
> >>>>  configure              |    5 +----
> >>>>  libavformat/avisynth.c |   14 +++++---------
> >>>>  2 files changed, 6 insertions(+), 13 deletions(-)
> >>>> b1568f39504e5e14c924d27c8f11ba8f5816d68c
> 0003-avformat-avisynth.c-Use-n
> >>>> ew-safe-dlopen-code.patch
> >>>> From 633212cf1246b3fde61dd6515229e6a893005664 Mon Sep 17 00:00:00
> 2001
> >>>> From: Matt Oliver <protogonoi@gmail.com>
> >>>> Date: Sat, 29 Oct 2016 18:25:25 +1100
> >>>> Subject: [PATCH 3/4] avformat/avisynth.c: Use new safe dlopen code.
> >>>>
> >>>
> >>> breaks --enable-avisynth on linux/ubuntu
> >>>
> >>> --- config.h    2016-10-29 17:17:55.214014842 +0200
> >>> +++ delth/config.h      2016-10-29 17:15:41.906012034 +0200
> >>> @@ -1155,7 +1155,7 @@
> >>>  #define CONFIG_AST_DEMUXER 1
> >>>  #define CONFIG_AU_DEMUXER 1
> >>>  #define CONFIG_AVI_DEMUXER 1
> >>> -#define CONFIG_AVISYNTH_DEMUXER 1
> >>> +#define CONFIG_AVISYNTH_DEMUXER 0
> >>>  #define CONFIG_AVR_DEMUXER 1
> >>>  #define CONFIG_AVS_DEMUXER 1
> >>>  #define CONFIG_BETHSOFTVID_DEMUXER 1
> >>>
> >>> [...]
> >>>
> >>>
> >> Yeah, libdl needs to be linked to on non-Windows, and the
> >> check for it got removed with the rest of the 'enabled avisynth'
> >> case in configure. Just putting dlopen under avisynth_demuxer_deps
> doesn't
> >> seem to be sufficient for that to work.
> >
> >
> > Looks like I missed a line, my apologies. Updated version attached.
> >
>
> Looking at the configure changes - having a dependency on both dlopen
> and LoadLibrary sounds odd. Shouldn't it be either, not both?
>

Yes, unfortunately i was testing on mingw which has both. Fixed thanks.
Michael Niedermayer Oct. 30, 2016, 4:22 p.m. UTC | #5
On Sun, Oct 30, 2016 at 10:31:16PM +1100, Matt Oliver wrote:
> On 30 October 2016 at 20:21, Hendrik Leppkes <h.leppkes@gmail.com> wrote:
> 
> > On Sun, Oct 30, 2016 at 5:13 AM, Matt Oliver <protogonoi@gmail.com> wrote:
> > > On 30 October 2016 at 03:41, Stephen Hutchinson <qyot27@gmail.com>
> > wrote:
> > >
> > >> On 10/29/2016 11:22 AM, Michael Niedermayer wrote:
> > >>
> > >>> On Sat, Oct 29, 2016 at 06:35:19PM +1100, Matt Oliver wrote:
> > >>>
> > >>>>  configure              |    5 +----
> > >>>>  libavformat/avisynth.c |   14 +++++---------
> > >>>>  2 files changed, 6 insertions(+), 13 deletions(-)
> > >>>> b1568f39504e5e14c924d27c8f11ba8f5816d68c
> > 0003-avformat-avisynth.c-Use-n
> > >>>> ew-safe-dlopen-code.patch
> > >>>> From 633212cf1246b3fde61dd6515229e6a893005664 Mon Sep 17 00:00:00
> > 2001
> > >>>> From: Matt Oliver <protogonoi@gmail.com>
> > >>>> Date: Sat, 29 Oct 2016 18:25:25 +1100
> > >>>> Subject: [PATCH 3/4] avformat/avisynth.c: Use new safe dlopen code.
> > >>>>
> > >>>
> > >>> breaks --enable-avisynth on linux/ubuntu
> > >>>
> > >>> --- config.h    2016-10-29 17:17:55.214014842 +0200
> > >>> +++ delth/config.h      2016-10-29 17:15:41.906012034 +0200
> > >>> @@ -1155,7 +1155,7 @@
> > >>>  #define CONFIG_AST_DEMUXER 1
> > >>>  #define CONFIG_AU_DEMUXER 1
> > >>>  #define CONFIG_AVI_DEMUXER 1
> > >>> -#define CONFIG_AVISYNTH_DEMUXER 1
> > >>> +#define CONFIG_AVISYNTH_DEMUXER 0
> > >>>  #define CONFIG_AVR_DEMUXER 1
> > >>>  #define CONFIG_AVS_DEMUXER 1
> > >>>  #define CONFIG_BETHSOFTVID_DEMUXER 1
> > >>>
> > >>> [...]
> > >>>
> > >>>
> > >> Yeah, libdl needs to be linked to on non-Windows, and the
> > >> check for it got removed with the rest of the 'enabled avisynth'
> > >> case in configure. Just putting dlopen under avisynth_demuxer_deps
> > doesn't
> > >> seem to be sufficient for that to work.
> > >
> > >
> > > Looks like I missed a line, my apologies. Updated version attached.
> > >
> >
> > Looking at the configure changes - having a dependency on both dlopen
> > and LoadLibrary sounds odd. Shouldn't it be either, not both?
> >
> 
> Yes, unfortunately i was testing on mingw which has both. Fixed thanks.

>  configure              |    5 ++---
>  libavformat/avisynth.c |   14 +++++---------
>  2 files changed, 7 insertions(+), 12 deletions(-)
> ed7ff2d7e0c984ee5253e76f54b3115386c1428d  0003-avformat-avisynth.c-Use-new-safe-dlopen-code.patch
> From 1dc1f59158cfb12d9160ee47342f5742d67ad864 Mon Sep 17 00:00:00 2001
> From: Matt Oliver <protogonoi@gmail.com>
> Date: Sun, 30 Oct 2016 15:13:47 +1100
> Subject: [PATCH] avformat/avisynth.c: Use new safe dlopen code.

Works now

thx

[...]
James Almer Oct. 30, 2016, 6:30 p.m. UTC | #6
On 10/30/2016 8:31 AM, Matt Oliver wrote:
> From 1dc1f59158cfb12d9160ee47342f5742d67ad864 Mon Sep 17 00:00:00 2001
> From: Matt Oliver <protogonoi@gmail.com>
> Date: Sun, 30 Oct 2016 15:13:47 +1100
> Subject: [PATCH] avformat/avisynth.c: Use new safe dlopen code.
> 
> ---
>  configure              |  5 ++---
>  libavformat/avisynth.c | 14 +++++---------
>  2 files changed, 7 insertions(+), 12 deletions(-)
> 
> diff --git a/configure b/configure
> index 5993de5..9feb544 100755
> --- a/configure
> +++ b/configure
> @@ -2850,6 +2850,7 @@ asf_stream_muxer_select="asf_muxer"
>  avi_demuxer_select="iso_media riffdec exif"
>  avi_muxer_select="riffenc"
>  avisynth_demuxer_deps="avisynth"
> +avisynth_demuxer_deps_any="dlopen LoadLibrary"
>  avisynth_demuxer_select="riffdec"

With this change and the stuff removed below, avisynth (CONFIG_AVISYNTH)
will always be enabled if you configure with --enable-avisynth, and only
avisynth_demuxer will be disabled depending on the presence of dlopen and
LoadLibrary.
This is probably not intended, seeing how libavformat/Makefile checks for
CONFIG_AVISYNTH and not CONFIG_AVISYNTH_DEMUXER in order to compile the
source file, so you should make it avisynth_deps_any="dlopen LoadLibrary"
since avisynth_demuxer already depends on avisynth.

Also, consider changing libavformat/Makefile to check for the demuxer
rather than the library/feature.

>  caf_demuxer_select="iso_media riffdec"
>  dash_muxer_select="mp4_muxer"
> @@ -5417,6 +5418,7 @@ elif check_func dlopen -ldl && check_func dlsym -ldl; then
>      ldl=-ldl
>  fi
>  
> +avisynth_demuxer_extralibs='$ldl'
>  decklink_outdev_extralibs="$decklink_outdev_extralibs $ldl"
>  decklink_indev_extralibs="$decklink_indev_extralibs $ldl"
>  frei0r_filter_extralibs='$ldl'
> @@ -5664,9 +5666,6 @@ fi
>  enabled avfoundation_indev && { check_header_objcc AVFoundation/AVFoundation.h || disable avfoundation_indev; }
>  enabled avfoundation_indev && { check_lib2 CoreGraphics/CoreGraphics.h CGGetActiveDisplayList -framework CoreGraphics ||
>                                  check_lib2 ApplicationServices/ApplicationServices.h CGGetActiveDisplayList -framework ApplicationServices; }
> -enabled avisynth          && { { check_lib2 "windows.h" LoadLibrary; } ||
> -                               { check_lib2 "dlfcn.h" dlopen -ldl; } ||
> -                               die "ERROR: LoadLibrary/dlopen not found for avisynth"; }
>  enabled cuda              && { check_lib cuda.h cuInit -lcuda ||
>                                 die "ERROR: CUDA not found"; }
>  enabled cuvid             && { add_cflags -I$source_path;
> diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c
> index 1acc44f..514cb99 100644
> --- a/libavformat/avisynth.c
> +++ b/libavformat/avisynth.c
> @@ -29,7 +29,7 @@
>  
>  /* Platform-specific directives for AviSynth vs AvxSynth. */
>  #ifdef _WIN32
> -  #include <windows.h>
> +  #include "compat/w32dlfcn.h"
>    #undef EXTERN_C
>    #include "compat/avisynth/avisynth_c.h"
>    #define AVISYNTH_LIB "avisynth"
> @@ -39,10 +39,6 @@
>    #include "compat/avisynth/avxsynth_c.h"
>    #define AVISYNTH_NAME "libavxsynth"
>    #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
> -
> -  #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_LOCAL)
> -  #define GetProcAddress dlsym
> -  #define FreeLibrary dlclose
>  #endif
>  
>  typedef struct AviSynthLibrary {
> @@ -118,13 +114,13 @@ static av_cold void avisynth_atexit_handler(void);
>  
>  static av_cold int avisynth_load_library(void)
>  {
> -    avs_library.library = LoadLibrary(AVISYNTH_LIB);
> +    avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
>      if (!avs_library.library)
>          return AVERROR_UNKNOWN;
>  
>  #define LOAD_AVS_FUNC(name, continue_on_fail)                          \
>          avs_library.name =                                             \
> -            (void *)GetProcAddress(avs_library.library, #name);        \
> +            (void *)dlsym(avs_library.library, #name);                 \
>          if (!continue_on_fail && !avs_library.name)                    \
>              goto fail;
>  
> @@ -157,7 +153,7 @@ static av_cold int avisynth_load_library(void)
>      return 0;
>  
>  fail:
> -    FreeLibrary(avs_library.library);
> +    dlclose(avs_library.library);
>      return AVERROR_UNKNOWN;
>  }
>  
> @@ -225,7 +221,7 @@ static av_cold void avisynth_atexit_handler(void)
>          avisynth_context_destroy(avs);
>          avs = next;
>      }
> -    FreeLibrary(avs_library.library);
> +    dlclose(avs_library.library);
>  
>      avs_atexit_called = 1;
>  }
> -- 2.10.1.windows.1
Matt Oliver Oct. 31, 2016, 3:51 a.m. UTC | #7
On 31 October 2016 at 05:30, James Almer <jamrial@gmail.com> wrote:

> On 10/30/2016 8:31 AM, Matt Oliver wrote:
> > From 1dc1f59158cfb12d9160ee47342f5742d67ad864 Mon Sep 17 00:00:00 2001
> > From: Matt Oliver <protogonoi@gmail.com>
> > Date: Sun, 30 Oct 2016 15:13:47 +1100
> > Subject: [PATCH] avformat/avisynth.c: Use new safe dlopen code.
> >
> > ---
> >  configure              |  5 ++---
> >  libavformat/avisynth.c | 14 +++++---------
> >  2 files changed, 7 insertions(+), 12 deletions(-)
> >
> > diff --git a/configure b/configure
> > index 5993de5..9feb544 100755
> > --- a/configure
> > +++ b/configure
> > @@ -2850,6 +2850,7 @@ asf_stream_muxer_select="asf_muxer"
> >  avi_demuxer_select="iso_media riffdec exif"
> >  avi_muxer_select="riffenc"
> >  avisynth_demuxer_deps="avisynth"
> > +avisynth_demuxer_deps_any="dlopen LoadLibrary"
> >  avisynth_demuxer_select="riffdec"
>
> With this change and the stuff removed below, avisynth (CONFIG_AVISYNTH)
> will always be enabled if you configure with --enable-avisynth, and only
> avisynth_demuxer will be disabled depending on the presence of dlopen and
> LoadLibrary.
> This is probably not intended, seeing how libavformat/Makefile checks for
> CONFIG_AVISYNTH and not CONFIG_AVISYNTH_DEMUXER in order to compile the
> source file, so you should make it avisynth_deps_any="dlopen LoadLibrary"
> since avisynth_demuxer already depends on avisynth.
>
> Also, consider changing libavformat/Makefile to check for the demuxer
> rather than the library/feature.
>

Changed, I also moved the avisynth stuff up to the external libraries
section of configure


>
> >  caf_demuxer_select="iso_media riffdec"
> >  dash_muxer_select="mp4_muxer"
> > @@ -5417,6 +5418,7 @@ elif check_func dlopen -ldl && check_func dlsym
> -ldl; then
> >      ldl=-ldl
> >  fi
> >
> > +avisynth_demuxer_extralibs='$ldl'
> >  decklink_outdev_extralibs="$decklink_outdev_extralibs $ldl"
> >  decklink_indev_extralibs="$decklink_indev_extralibs $ldl"
> >  frei0r_filter_extralibs='$ldl'
> > @@ -5664,9 +5666,6 @@ fi
> >  enabled avfoundation_indev && { check_header_objcc
> AVFoundation/AVFoundation.h || disable avfoundation_indev; }
> >  enabled avfoundation_indev && { check_lib2 CoreGraphics/CoreGraphics.h
> CGGetActiveDisplayList -framework CoreGraphics ||
> >                                  check_lib2 ApplicationServices/ApplicationServices.h
> CGGetActiveDisplayList -framework ApplicationServices; }
> > -enabled avisynth          && { { check_lib2 "windows.h" LoadLibrary; }
> ||
> > -                               { check_lib2 "dlfcn.h" dlopen -ldl; } ||
> > -                               die "ERROR: LoadLibrary/dlopen not found
> for avisynth"; }
> >  enabled cuda              && { check_lib cuda.h cuInit -lcuda ||
> >                                 die "ERROR: CUDA not found"; }
> >  enabled cuvid             && { add_cflags -I$source_path;
> > diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c
> > index 1acc44f..514cb99 100644
> > --- a/libavformat/avisynth.c
> > +++ b/libavformat/avisynth.c
> > @@ -29,7 +29,7 @@
> >
> >  /* Platform-specific directives for AviSynth vs AvxSynth. */
> >  #ifdef _WIN32
> > -  #include <windows.h>
> > +  #include "compat/w32dlfcn.h"
> >    #undef EXTERN_C
> >    #include "compat/avisynth/avisynth_c.h"
> >    #define AVISYNTH_LIB "avisynth"
> > @@ -39,10 +39,6 @@
> >    #include "compat/avisynth/avxsynth_c.h"
> >    #define AVISYNTH_NAME "libavxsynth"
> >    #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
> > -
> > -  #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_LOCAL)
> > -  #define GetProcAddress dlsym
> > -  #define FreeLibrary dlclose
> >  #endif
> >
> >  typedef struct AviSynthLibrary {
> > @@ -118,13 +114,13 @@ static av_cold void avisynth_atexit_handler(void);
> >
> >  static av_cold int avisynth_load_library(void)
> >  {
> > -    avs_library.library = LoadLibrary(AVISYNTH_LIB);
> > +    avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
> >      if (!avs_library.library)
> >          return AVERROR_UNKNOWN;
> >
> >  #define LOAD_AVS_FUNC(name, continue_on_fail)                          \
> >          avs_library.name =
>  \
> > -            (void *)GetProcAddress(avs_library.library, #name);
> \
> > +            (void *)dlsym(avs_library.library, #name);                 \
> >          if (!continue_on_fail && !avs_library.name)
> \
> >              goto fail;
> >
> > @@ -157,7 +153,7 @@ static av_cold int avisynth_load_library(void)
> >      return 0;
> >
> >  fail:
> > -    FreeLibrary(avs_library.library);
> > +    dlclose(avs_library.library);
> >      return AVERROR_UNKNOWN;
> >  }
> >
> > @@ -225,7 +221,7 @@ static av_cold void avisynth_atexit_handler(void)
> >          avisynth_context_destroy(avs);
> >          avs = next;
> >      }
> > -    FreeLibrary(avs_library.library);
> > +    dlclose(avs_library.library);
> >
> >      avs_atexit_called = 1;
> >  }
> > -- 2.10.1.windows.1
Stephen Hutchinson Nov. 2, 2016, 1:22 a.m. UTC | #8
On 10/30/2016 11:51 PM, Matt Oliver wrote:
> On 31 October 2016 at 05:30, James Almer <jamrial@gmail.com> wrote:
>
>> On 10/30/2016 8:31 AM, Matt Oliver wrote:
>>> From 1dc1f59158cfb12d9160ee47342f5742d67ad864 Mon Sep 17 00:00:00 2001
>>> From: Matt Oliver <protogonoi@gmail.com>
>>> Date: Sun, 30 Oct 2016 15:13:47 +1100
>>> Subject: [PATCH] avformat/avisynth.c: Use new safe dlopen code.
>>>
>>> ---
>>>  configure              |  5 ++---
>>>  libavformat/avisynth.c | 14 +++++---------
>>>  2 files changed, 7 insertions(+), 12 deletions(-)
>>>
>>> diff --git a/configure b/configure
>>> index 5993de5..9feb544 100755
>>> --- a/configure
>>> +++ b/configure
>>> @@ -2850,6 +2850,7 @@ asf_stream_muxer_select="asf_muxer"
>>>  avi_demuxer_select="iso_media riffdec exif"
>>>  avi_muxer_select="riffenc"
>>>  avisynth_demuxer_deps="avisynth"
>>> +avisynth_demuxer_deps_any="dlopen LoadLibrary"
>>>  avisynth_demuxer_select="riffdec"
>>
>> With this change and the stuff removed below, avisynth (CONFIG_AVISYNTH)
>> will always be enabled if you configure with --enable-avisynth, and only
>> avisynth_demuxer will be disabled depending on the presence of dlopen and
>> LoadLibrary.
>> This is probably not intended, seeing how libavformat/Makefile checks for
>> CONFIG_AVISYNTH and not CONFIG_AVISYNTH_DEMUXER in order to compile the
>> source file, so you should make it avisynth_deps_any="dlopen LoadLibrary"
>> since avisynth_demuxer already depends on avisynth.
>>
>> Also, consider changing libavformat/Makefile to check for the demuxer
>> rather than the library/feature.
>>
>
> Changed, I also moved the avisynth stuff up to the external libraries
> section of configure
>
>
>>
>>>  caf_demuxer_select="iso_media riffdec"
>>>  dash_muxer_select="mp4_muxer"
>>> @@ -5417,6 +5418,7 @@ elif check_func dlopen -ldl && check_func dlsym
>> -ldl; then
>>>      ldl=-ldl
>>>  fi
>>>
>>> +avisynth_demuxer_extralibs='$ldl'
>>>  decklink_outdev_extralibs="$decklink_outdev_extralibs $ldl"
>>>  decklink_indev_extralibs="$decklink_indev_extralibs $ldl"
>>>  frei0r_filter_extralibs='$ldl'
>>> @@ -5664,9 +5666,6 @@ fi
>>>  enabled avfoundation_indev && { check_header_objcc
>> AVFoundation/AVFoundation.h || disable avfoundation_indev; }
>>>  enabled avfoundation_indev && { check_lib2 CoreGraphics/CoreGraphics.h
>> CGGetActiveDisplayList -framework CoreGraphics ||
>>>                                  check_lib2 ApplicationServices/ApplicationServices.h
>> CGGetActiveDisplayList -framework ApplicationServices; }
>>> -enabled avisynth          && { { check_lib2 "windows.h" LoadLibrary; }
>> ||
>>> -                               { check_lib2 "dlfcn.h" dlopen -ldl; } ||
>>> -                               die "ERROR: LoadLibrary/dlopen not found
>> for avisynth"; }
>>>  enabled cuda              && { check_lib cuda.h cuInit -lcuda ||
>>>                                 die "ERROR: CUDA not found"; }
>>>  enabled cuvid             && { add_cflags -I$source_path;
>>> diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c
>>> index 1acc44f..514cb99 100644
>>> --- a/libavformat/avisynth.c
>>> +++ b/libavformat/avisynth.c
>>> @@ -29,7 +29,7 @@
>>>
>>>  /* Platform-specific directives for AviSynth vs AvxSynth. */
>>>  #ifdef _WIN32
>>> -  #include <windows.h>
>>> +  #include "compat/w32dlfcn.h"
>>>    #undef EXTERN_C
>>>    #include "compat/avisynth/avisynth_c.h"
>>>    #define AVISYNTH_LIB "avisynth"
>>> @@ -39,10 +39,6 @@
>>>    #include "compat/avisynth/avxsynth_c.h"
>>>    #define AVISYNTH_NAME "libavxsynth"
>>>    #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
>>> -
>>> -  #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_LOCAL)
>>> -  #define GetProcAddress dlsym
>>> -  #define FreeLibrary dlclose
>>>  #endif
>>>
>>>  typedef struct AviSynthLibrary {
>>> @@ -118,13 +114,13 @@ static av_cold void avisynth_atexit_handler(void);
>>>
>>>  static av_cold int avisynth_load_library(void)
>>>  {
>>> -    avs_library.library = LoadLibrary(AVISYNTH_LIB);
>>> +    avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
>>>      if (!avs_library.library)
>>>          return AVERROR_UNKNOWN;
>>>
>>>  #define LOAD_AVS_FUNC(name, continue_on_fail)                          \
>>>          avs_library.name =
>>  \
>>> -            (void *)GetProcAddress(avs_library.library, #name);
>> \
>>> +            (void *)dlsym(avs_library.library, #name);                 \
>>>          if (!continue_on_fail && !avs_library.name)
>> \
>>>              goto fail;
>>>
>>> @@ -157,7 +153,7 @@ static av_cold int avisynth_load_library(void)
>>>      return 0;
>>>
>>>  fail:
>>> -    FreeLibrary(avs_library.library);
>>> +    dlclose(avs_library.library);
>>>      return AVERROR_UNKNOWN;
>>>  }
>>>
>>> @@ -225,7 +221,7 @@ static av_cold void avisynth_atexit_handler(void)
>>>          avisynth_context_destroy(avs);
>>>          avs = next;
>>>      }
>>> -    FreeLibrary(avs_library.library);
>>> +    dlclose(avs_library.library);
>>>
>>>      avs_atexit_called = 1;
>>>  }
>>> -- 2.10.1.windows.1
>>>
>>>
>>> _______________________________________________
>>> ffmpeg-devel mailing list
>>> ffmpeg-devel@ffmpeg.org
>>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Tested under Ubuntu and Windows 10.  The AviSynth stuff LGTM.
diff mbox

Patch

--- config.h    2016-10-29 17:17:55.214014842 +0200
+++ delth/config.h      2016-10-29 17:15:41.906012034 +0200
@@ -1155,7 +1155,7 @@ 
 #define CONFIG_AST_DEMUXER 1
 #define CONFIG_AU_DEMUXER 1
 #define CONFIG_AVI_DEMUXER 1
-#define CONFIG_AVISYNTH_DEMUXER 1
+#define CONFIG_AVISYNTH_DEMUXER 0
 #define CONFIG_AVR_DEMUXER 1
 #define CONFIG_AVS_DEMUXER 1
 #define CONFIG_BETHSOFTVID_DEMUXER 1