Welcome to mirror list, hosted at ThFree Co, Russian Federation.

resample_stages.cpp « source « Kasumi « VirtualDub « thirdparty « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fcea6c6694cd4c1b85f161885a94a97eee767332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <vd2/system/math.h>
#include <vd2/system/vdstl.h>
#include <vd2/Kasumi/resample_kernels.h>
#include "resample_stages.h"

VDSteppedAllocator::VDSteppedAllocator(size_t initialSize)
	: mpHead(NULL)
	, mpAllocNext(NULL)
	, mAllocLeft(0)
	, mAllocNext(initialSize)
	, mAllocInit(initialSize)
{
}

VDSteppedAllocator::~VDSteppedAllocator() {
	clear();
}

void VDSteppedAllocator::clear() {
	while(Block *p = mpHead) {
		mpHead = mpHead->next;
		free(p);
	}
	mAllocLeft = 0;
	mAllocNext = mAllocInit;
}

void *VDSteppedAllocator::allocate(size_type n) {
	n = (n+15) & ~15;
	if (mAllocLeft < n) {
		mAllocLeft = mAllocNext;
		mAllocNext += (mAllocNext >> 1);
		if (mAllocLeft < n)
			mAllocLeft = n;

		Block *t = (Block *)malloc(sizeof(Block) + mAllocLeft);

		if (mpHead)
			mpHead->next = t;

		mpHead = t;
		mpHead->next = NULL;

		mpAllocNext = (char *)(mpHead + 1);
	}

	void *p = mpAllocNext;
	mpAllocNext += n;
	mAllocLeft -= n;
	return p;
}

void VDResamplerGenerateTable(sint32 *dst, const IVDResamplerFilter& filter) {
	const unsigned width = filter.GetFilterWidth();
	vdblock<float> filters(width * 256);
	float *src = filters.data();

	filter.GenerateFilterBank(src);

	for(unsigned phase=0; phase < 256; ++phase) {
		float sum = 0;

		for(unsigned i=0; i<width; ++i)
			sum += src[i];

		float scalefac = 16384.0f / sum;

		for(unsigned j=0; j<width; j += 2) {
			int v0 = VDRoundToIntFast(src[j+0] * scalefac);
			int v1 = VDRoundToIntFast(src[j+1] * scalefac);

			dst[j+0] = v0;
			dst[j+1] = v1;
		}

		src += width;
		dst += width;
	}
}

void VDResamplerGenerateTableF(float *dst, const IVDResamplerFilter& filter) {
	const unsigned width = filter.GetFilterWidth();
	filter.GenerateFilterBank(dst);

	for(unsigned phase=0; phase < 256; ++phase) {
		float sum = 0;

		for(unsigned i=0; i<width; ++i)
			sum += dst[i];

		float scalefac = 1.0f / sum;

		for(unsigned j=0; j<width; ++j)
			*dst++ *= scalefac;
	}
}

void VDResamplerGenerateTable2(sint32 *dst, const IVDResamplerFilter& filter, sint32 count, sint32 u0, sint32 dudx) {
	const unsigned width = filter.GetFilterWidth();
	vdblock<float> filters(width);
	float *src = filters.data();

	filter.GenerateFilterBank(src);

	for(sint32 i=0; i<count; ++i) {
		sint32 u = u0 + dudx*i;

		*dst++ = u >> 16;
		filter.GenerateFilter(src, (double)(u & 0xffff) / 65536.0);

		float sum = 0;
		for(uint32 j=0; j<width; ++j)
			sum += src[j];

		float scalefac = 16384.0f / sum;

		sint32 isum = 0;
		for(uint32 j=0; j<width; ++j) {
			sint32 v = VDRoundToIntFast(src[j] * scalefac);

			dst[j] = v;
			isum += v;
		}

		sint32 ierr = 16384 - isum;
		sint32 idelta = 2*(ierr >> 31) - 1;
		while(ierr) {
			for(uint32 j=0; j<width && ierr; ++j) {
				if (!dst[j])
					continue;

				dst[j] += idelta;
				ierr -= idelta;
			}
		}

		dst += width;
	}
}

void VDResamplerSwizzleTable(sint32 *dst, unsigned pairs) {
	do {
		sint32 v0 = dst[0];
		sint32 v1 = dst[1];

		dst[0] = dst[1] = (v0 & 0xffff) + (v1<<16);
		dst += 2;
	} while(--pairs);
}