Skip to content

Commit

Permalink
Some updates
Browse files Browse the repository at this point in the history
- Support all constant input formats (internally clips are converted to float before processing and converted back to their original format afterwards; all subsamplings are supported, but for now, left-aligned chroma is always assumed; the python wrapper is now only needed if you don't want to descale chroma)

- Add a generic `Descale` filter that takes a string `kernel` parameter to make it simpler to write wrapper functions

- Add `opt` parameter to manually select optimizations

- Split the code for the VapourSynth plugin and descale

- Change license to MIT
  • Loading branch information
Frechdachs committed Sep 30, 2021
1 parent 7ea0ca1 commit 6720084
Show file tree
Hide file tree
Showing 12 changed files with 1,007 additions and 688 deletions.
13 changes: 0 additions & 13 deletions COPYING

This file was deleted.

21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2021 Frechdachs <frechdachs@rekt.cc>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -4,8 +4,8 @@ VapourSynth plugin to undo upscaling.

## Usage

The plugin itself only supports GrayS, RGBS, and YUV444PS input.
The included python wrapper supports YUV (every subsampling), Gray, and RGB of every bitdepth.
The plugin itself supports every constant input format. If the format is subsampled, left-aligned chroma planes are always assumed.
The included python wrapper, contrary to using the plugin directly, doesn't descale the chroma planes but scales them normally with `Spline36`.

```
descale.Debilinear(clip src, int width, int height, float src_left=0.0, float src_top=0.0, float src_width=width, float src_height=height)
Expand All @@ -19,6 +19,8 @@ descale.Despline16(clip src, int width, int height, float src_left=0.0, float sr
descale.Despline36(clip src, int width, int height, float src_left=0.0, float src_top=0.0, float src_width=width, float src_height=height)
descale.Despline64(clip src, int width, int height, float src_left=0.0, float src_top=0.0, float src_width=width, float src_height=height)
descale.Descale(clip src, int width, int height, str kernel, int taps=3, float b=0.0, float c=0.0, float src_left=0.0, float src_top=0.0, float src_width=width, float src_height=height)
```

## How does this work?
Expand Down
41 changes: 10 additions & 31 deletions descale.py
@@ -1,43 +1,40 @@
from vapoursynth import core, GRAYS, RGBS, GRAY, YUV, RGB # You need Vapoursynth R37 or newer
from functools import partial
from vapoursynth import core, GRAYS, RGBS, GRAY, YUV, RGB


# If yuv444 is True chroma will be upscaled instead of downscaled
# If gray is True the output will be grayscale
def Debilinear(src, width, height, yuv444=False, gray=False, chromaloc=None):
return Descale(src, width, height, kernel='bilinear', b=None, c=None, taps=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)
return Descale(src, width, height, kernel='bilinear', taps=None, b=None, c=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)

def Debicubic(src, width, height, b=0.0, c=0.5, yuv444=False, gray=False, chromaloc=None):
return Descale(src, width, height, kernel='bicubic', b=b, c=c, taps=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)
return Descale(src, width, height, kernel='bicubic', taps=None, b=b, c=c, yuv444=yuv444, gray=gray, chromaloc=chromaloc)

def Delanczos(src, width, height, taps=3, yuv444=False, gray=False, chromaloc=None):
return Descale(src, width, height, kernel='lanczos', b=None, c=None, taps=taps, yuv444=yuv444, gray=gray, chromaloc=chromaloc)
return Descale(src, width, height, kernel='lanczos', taps=taps, b=None, c=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)

def Despline16(src, width, height, yuv444=False, gray=False, chromaloc=None):
return Descale(src, width, height, kernel='spline16', b=None, c=None, taps=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)
return Descale(src, width, height, kernel='spline16', taps=None, b=None, c=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)

def Despline36(src, width, height, yuv444=False, gray=False, chromaloc=None):
return Descale(src, width, height, kernel='spline36', b=None, c=None, taps=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)
return Descale(src, width, height, kernel='spline36', taps=None, b=None, c=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)

def Despline64(src, width, height, yuv444=False, gray=False, chromaloc=None):
return Descale(src, width, height, kernel='spline64', b=None, c=None, taps=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)
return Descale(src, width, height, kernel='spline64', taps=None, b=None, c=None, yuv444=yuv444, gray=gray, chromaloc=chromaloc)


def Descale(src, width, height, kernel='bilinear', b=0.0, c=0.5, taps=3, yuv444=False, gray=False, chromaloc=None):
def Descale(src, width, height, kernel='bilinear', taps=3, b=0.0, c=0.5, yuv444=False, gray=False, chromaloc=None):
src_f = src.format
src_cf = src_f.color_family
src_st = src_f.sample_type
src_bits = src_f.bits_per_sample
src_sw = src_f.subsampling_w
src_sh = src_f.subsampling_h

descale_filter = get_filter(b, c, taps, kernel)

if src_cf == RGB and not gray:
rgb = descale_filter(to_rgbs(src), width, height)
rgb = to_rgbs(src).descale.Descale(width, height, kernel, taps, b, c)
return rgb.resize.Point(format=src_f.id)

y = descale_filter(to_grays(src), width, height)
y = to_rgbs(src).descale.Descale(width, height, kernel, taps, b, c)
y_f = core.register_format(GRAY, src_st, src_bits, 0, 0)
y = y.resize.Point(format=y_f.id)

Expand Down Expand Up @@ -65,21 +62,3 @@ def to_rgbs(src):

def get_plane(src, plane):
return core.std.ShufflePlanes(src, plane, GRAY)


def get_filter(b, c, taps, kernel):
kernel = kernel.lower()
if kernel == 'bilinear':
return core.descale.Debilinear
elif kernel == 'bicubic':
return partial(core.descale.Debicubic, b=b, c=c)
elif kernel == 'lanczos':
return partial(core.descale.Delanczos, taps=taps)
elif kernel == 'spline16':
return core.descale.Despline16
elif kernel == 'spline36':
return core.descale.Despline36
elif kernel == 'spline64':
return core.descale.Despline64
else:
raise ValueError('Descale: Invalid kernel specified.')
93 changes: 93 additions & 0 deletions include/descale.h
@@ -0,0 +1,93 @@
/*
* Copyright © 2017-2021 Frechdachs <frechdachs@rekt.cc>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/


#ifndef DESCALE_H
#define DESCALE_H


typedef enum DescaleMode
{
DESCALE_MODE_BILINEAR = 1,
DESCALE_MODE_BICUBIC = 2,
DESCALE_MODE_LANCZOS = 3,
DESCALE_MODE_SPLINE16 = 4,
DESCALE_MODE_SPLINE36 = 5,
DESCALE_MODE_SPLINE64 = 6
} DescaleMode;


typedef enum DescaleDir
{
DESCALE_DIR_HORIZONTAL = 0,
DESCALE_DIR_VERTICAL = 1
} DescaleDir;


typedef enum DescaleOpt
{
DESCALE_OPT_AUTO = 0,
DESCALE_OPT_NONE = 1,
DESCALE_OPT_AVX2 = 2
} DescaleOpt;


typedef struct DescaleParams
{
enum DescaleMode mode;
int taps;
double param1;
double param2;
double shift;
double active_dim;
} DescaleParams;


typedef struct DescaleCore
{
int src_dim;
int dst_dim;
int bandwidth;
float **upper;
float **lower;
float *diagonal;
float *weights;
int *weights_left_idx;
int *weights_right_idx;
int weights_columns;
} DescaleCore;


struct DescaleAPI
{
// is __stdcall needed for win32?
struct DescaleCore *(*create_core)(int src_dim, int dst_dim, struct DescaleParams *params);
void (*free_core)(struct DescaleCore *core);
void (*process_vectors)(struct DescaleCore *core, enum DescaleDir dir, int vector_count,
int src_stride, int dst_stride, const float *srcp, float *dstp);
};


struct DescaleAPI get_descale_api(enum DescaleOpt opt);


#endif // DESCALE_H
25 changes: 15 additions & 10 deletions meson.build
@@ -1,12 +1,16 @@
project('Descale', 'c',
default_options: ['buildtype=release', 'b_ndebug=if-release', 'c_std=c99'],
meson_version: '>=0.51.0',
version: '6'
version: '7'
)

add_global_arguments(['-D_XOPEN_SOURCE=700'], language: 'c')

cc = meson.get_compiler('c')

sources = ['src/descale.c']
includedirs = ['include', 'src']

sources = ['src/descale.c', 'src/vsplugin.c']

libs = []

Expand All @@ -18,14 +22,15 @@ deps = []
# are enough.
if host_machine.system() == 'windows'
# Statically link things on Windows.
deps += cc.find_library('m', static: true, required: false)
deps += cc.find_library('winpthread', static: true)
m_dep = cc.find_library('m', static: true, required: false)
p_dep = cc.find_library('winpthread', static: true)
deps += [m_dep, p_dep]
installdir = join_paths(get_option('libdir'), 'vapoursynth')
else
deps += cc.find_library('m', required: false)
deps += cc.find_library('pthread')
m_dep = cc.find_library('m', required: false)
p_dep = cc.find_library('pthread')
vs = dependency('vapoursynth').partial_dependency(compile_args: true, includes: true)
deps += vs
deps += [m_dep, p_dep, vs]
installdir = join_paths(vs.get_pkgconfig_variable('libdir'), 'vapoursynth')
endif

Expand All @@ -35,17 +40,17 @@ if host_machine.cpu_family().startswith('x86')
sources += ['src/x86/cpuinfo_x86.c']

libs += static_library('descale_avx2', 'src/x86/descale_avx2.c',
dependencies: deps,
dependencies: [m_dep],
c_args: ['-mavx2', '-mfma'],
pic: true,
include_directories: 'src'
include_directories: includedirs
)
endif


shared_module('descale', sources,
dependencies: deps,
include_directories: 'src',
include_directories: includedirs,
link_with: libs,
name_prefix: 'lib',
install: true,
Expand Down
33 changes: 33 additions & 0 deletions src/common.h
Expand Up @@ -2,6 +2,17 @@
#define DESCALE_COMMON_H


#include <stddef.h>
#include <stdlib.h>
#ifdef _WIN32
#include <malloc.h>
#endif


#define DSMAX(a, b) ((a) > (b) ? (a) : (b))
#define DSMIN(a, b) ((a) > (b) ? (b) : (a))


static inline int ceil_n(int x, int n)
{
return (x + (n - 1)) & ~(n - 1);
Expand All @@ -14,4 +25,26 @@ static inline int floor_n(int x, int n)
}


static inline void descale_aligned_malloc(void **pptr, size_t size, size_t alignment)
{
#ifdef _WIN32
*pptr = _aligned_malloc(size, alignment);
#else
int err = posix_memalign(pptr, alignment, size);
if (err)
*pptr = NULL;
#endif
}


static inline void descale_aligned_free(void *ptr)
{
#ifdef _WIN32
_aligned_free(ptr);
#else
free(ptr);
#endif
}


#endif // DESCALE_COMMON_H

0 comments on commit 6720084

Please sign in to comment.