Skip to content

Commit

Permalink
avformat/avisynth: switch to AviSynth+ on Linux
Browse files Browse the repository at this point in the history
AviSynth+ now supports non-Windows OSes, making AvxSynth
obsolete.  Since we no longer support AviSynth 2.5 (which is
essentially what AvxSynth is), remove AvxSynth support and
replace it with AviSynth+.

As a result, the USING_AVISYNTH defines can be switched back
to generic _WIN32.
  • Loading branch information
qyot27 authored and cus committed Apr 4, 2020
1 parent 0c75acb commit 6d8cddd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ version <next>:
- CRI HCA decoder
- CRI HCA demuxer
- overlay_cuda filter
- switch from AvxSynth to AviSynth+ on Linux


version 4.2:
Expand Down
56 changes: 15 additions & 41 deletions libavformat/avisynth.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* AviSynth/AvxSynth support
* AviSynth(+) support
* Copyright (c) 2012 AvxSynth Team
*
* This file is part of FFmpeg
Expand Down Expand Up @@ -31,20 +31,19 @@
/* Enable function pointer definitions for runtime loading. */
#define AVSC_NO_DECLSPEC

/* Platform-specific directives for AviSynth vs AvxSynth. */
/* Platform-specific directives. */
#ifdef _WIN32
#include "compat/w32dlfcn.h"
#undef EXTERN_C
#include "compat/avisynth/avisynth_c.h"
#define AVISYNTH_LIB "avisynth"
#define USING_AVISYNTH
#else
#include <dlfcn.h>
#include "compat/avisynth/avxsynth_c.h"
#define AVISYNTH_NAME "libavxsynth"
#define AVISYNTH_NAME "libavisynth"
#define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
#endif

#include <avisynth/avisynth_c.h>

typedef struct AviSynthLibrary {
void *library;
#define AVSC_DECLARE_FUNC(name) name ## _func name
Expand All @@ -62,15 +61,13 @@ typedef struct AviSynthLibrary {
AVSC_DECLARE_FUNC(avs_release_value);
AVSC_DECLARE_FUNC(avs_release_video_frame);
AVSC_DECLARE_FUNC(avs_take_clip);
#ifdef USING_AVISYNTH
AVSC_DECLARE_FUNC(avs_bits_per_pixel);
AVSC_DECLARE_FUNC(avs_get_height_p);
AVSC_DECLARE_FUNC(avs_get_pitch_p);
AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
AVSC_DECLARE_FUNC(avs_get_row_size_p);
AVSC_DECLARE_FUNC(avs_is_planar_rgb);
AVSC_DECLARE_FUNC(avs_is_planar_rgba);
#endif
#undef AVSC_DECLARE_FUNC
} AviSynthLibrary;

Expand All @@ -97,14 +94,12 @@ static const int avs_planes_packed[1] = { 0 };
static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
AVS_PLANAR_V };
#ifdef USING_AVISYNTH
static const int avs_planes_rgb[3] = { AVS_PLANAR_G, AVS_PLANAR_B,
AVS_PLANAR_R };
static const int avs_planes_yuva[4] = { AVS_PLANAR_Y, AVS_PLANAR_U,
AVS_PLANAR_V, AVS_PLANAR_A };
static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,
AVS_PLANAR_R, AVS_PLANAR_A };
#endif

/* A conflict between C++ global objects, atexit, and dynamic loading requires
* us to register our own atexit handler to prevent double freeing. */
Expand Down Expand Up @@ -142,15 +137,13 @@ static av_cold int avisynth_load_library(void)
LOAD_AVS_FUNC(avs_release_value, 0);
LOAD_AVS_FUNC(avs_release_video_frame, 0);
LOAD_AVS_FUNC(avs_take_clip, 0);
#ifdef USING_AVISYNTH
LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
LOAD_AVS_FUNC(avs_get_height_p, 1);
LOAD_AVS_FUNC(avs_get_pitch_p, 1);
LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
LOAD_AVS_FUNC(avs_get_row_size_p, 1);
LOAD_AVS_FUNC(avs_is_planar_rgb, 1);
LOAD_AVS_FUNC(avs_is_planar_rgba, 1);
#endif
#undef LOAD_AVS_FUNC

atexit(avisynth_atexit_handler);
Expand Down Expand Up @@ -249,7 +242,6 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);

switch (avs->vi->pixel_type) {
#ifdef USING_AVISYNTH
/* 10~16-bit YUV pix_fmts (AviSynth+) */
case AVS_CS_YUV444P10:
st->codecpar->format = AV_PIX_FMT_YUV444P10;
Expand Down Expand Up @@ -434,8 +426,7 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
case AVS_CS_BGR64:
st->codecpar->format = AV_PIX_FMT_BGRA64;
break;
#endif
/* AviSynth 2.5 and AvxSynth pix_fmts */
/* AviSynth 2.5 pix_fmts */
case AVS_CS_BGR24:
st->codecpar->format = AV_PIX_FMT_BGR24;
break;
Expand All @@ -461,7 +452,6 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
}

switch (planar) {
#ifdef USING_AVISYNTH
case 5: // Planar RGB + Alpha
avs->n_planes = 4;
avs->planes = avs_planes_rgba;
Expand All @@ -474,7 +464,6 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
avs->n_planes = 3;
avs->planes = avs_planes_rgb;
break;
#endif
case 2: // Y8
avs->n_planes = 1;
avs->planes = avs_planes_grey;
Expand Down Expand Up @@ -556,15 +545,15 @@ static int avisynth_open_file(AVFormatContext *s)
AviSynthContext *avs = s->priv_data;
AVS_Value arg, val;
int ret;
#ifdef USING_AVISYNTH
#ifdef _WIN32
char filename_ansi[MAX_PATH * 4];
wchar_t filename_wc[MAX_PATH * 4];
#endif

if (ret = avisynth_context_create(s))
return ret;

#ifdef USING_AVISYNTH
#ifdef _WIN32
/* Convert UTF-8 to ANSI code page */
MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
Expand All @@ -588,19 +577,16 @@ static int avisynth_open_file(AVFormatContext *s)
avs->clip = avs_library.avs_take_clip(val, avs->env);
avs->vi = avs_library.avs_get_video_info(avs->clip);

#ifdef USING_AVISYNTH
/* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
* This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
* and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
* as interface version 3 like 2.5.8, this needs to be special-cased. */
* and excludes 2.5 and the 2.6 alphas. */

if (avs_library.avs_get_version(avs->clip) < 6) {
av_log(s, AV_LOG_ERROR,
"AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
ret = AVERROR_UNKNOWN;
goto fail;
}
#endif

/* Release the AVS_Value as it will go out of scope. */
avs_library.avs_release_value(val);
Expand Down Expand Up @@ -652,23 +638,21 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
if (discard)
return 0;

#ifdef USING_AVISYNTH
#ifdef _WIN32
/* Detect whether we're using AviSynth 2.6 or AviSynth+ by
* looking for whether avs_is_planar_rgb exists. */
if (GetProcAddress(avs_library.library, "avs_is_planar_rgb") == NULL)
avsplus = 0;
else
avsplus = 1;

/* avs_bits_per_pixel changed to AVSC_API with AviSynth 2.6, which
* requires going through avs_library, while AvxSynth has it under
* the older AVSC_INLINE type, so special-case this. */

bits = avs_library.avs_bits_per_pixel(avs->vi);
#else
bits = avs_bits_per_pixel(avs->vi);
/* AviSynth+ is now the only variant of AviSynth we support
* on Linux and macOS. */
avsplus = 1;
#endif

bits = avs_library.avs_bits_per_pixel(avs->vi);

/* Without the cast to int64_t, calculation overflows at about 9k x 9k
* resolution. */
pkt->size = (((int64_t)avs->vi->width *
Expand Down Expand Up @@ -696,34 +680,24 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
dst_p = pkt->data;
for (i = 0; i < avs->n_planes; i++) {
plane = avs->planes[i];
#ifdef USING_AVISYNTH
src_p = avs_library.avs_get_read_ptr_p(frame, plane);
pitch = avs_library.avs_get_pitch_p(frame, plane);

rowsize = avs_library.avs_get_row_size_p(frame, plane);
planeheight = avs_library.avs_get_height_p(frame, plane);
#else
src_p = avs_get_read_ptr_p(frame, plane);
pitch = avs_get_pitch_p(frame, plane);

rowsize = avs_get_row_size_p(frame, plane);
planeheight = avs_get_height_p(frame, plane);
#endif

/* Flip RGB video. */
if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
src_p = src_p + (planeheight - 1) * pitch;
pitch = -pitch;
}

#ifdef USING_AVISYNTH
/* Flip Planar RGB video */
if (avsplus && (avs_library.avs_is_planar_rgb(avs->vi) ||
avs_library.avs_is_planar_rgba(avs->vi))) {
src_p = src_p + (planeheight - 1) * pitch;
pitch = -pitch;
}
#endif

avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
rowsize, planeheight);
Expand Down

0 comments on commit 6d8cddd

Please sign in to comment.