rustsynth-sys 0.5.6

Low level bindings to VapourSynth
Documentation
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Copyright (c) 2012-2019 Fredrik Mellbin
*
* This file is part of VapourSynth.
*
* VapourSynth 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.
*
* VapourSynth 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 VapourSynth; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#define VS_MERGE_IMPL
#include "merge.h"
#include "VSHelper4.h"

#define MERGESHIFT 15
#define ROUND (1U << (MERGESHIFT - 1))

#ifdef _MSC_VER
#    pragma warning(push)
#    pragma warning(disable:4146)
#endif
static uint16_t premul_u8(uint8_t x, uint8_t a, unsigned offset)
{
    uint16_t tmp = (uint16_t)x - (uint16_t)offset;
    int sign = (int16_t)tmp < 0;
    tmp = sign ? -tmp : tmp;
    tmp = (tmp * a + UINT8_MAX / 2) / 255;
    tmp = sign ? -tmp : tmp;

    return tmp;
}

static uint32_t premul_u16(uint16_t x, uint16_t a, unsigned offset, unsigned depth)
{
    unsigned maxval = (1U << depth) - 1;
    unsigned div = div_table[depth - 9];
    unsigned shift = shift_table[depth - 9];

    uint32_t tmp = (uint32_t)x - (uint32_t)offset;
    int sign = (int32_t)tmp < 0;
    tmp = sign ? -tmp : tmp;
    tmp = (((uint64_t)tmp * a + maxval / 2) * div) >> (32 + shift);
    tmp = sign ? -tmp : tmp;

    return tmp;
}
#ifdef _MSC_VER
#    pragma warning(pop)
#endif

void vs_premultiply_byte_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned offset, unsigned n)
{
    const uint8_t *srcp1 = src1;
    const uint8_t *srcp2 = src2;
    uint8_t *dstp = dst;
    unsigned i;

    (void)depth;

    for (i = 0; i < n; i++) {
        uint8_t v1 = srcp1[i];
        uint8_t v2 = srcp2[i];
        dstp[i] = (uint8_t)premul_u8(v1, v2, offset) + offset;
    }
}

void vs_premultiply_word_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned offset, unsigned n)
{
    const uint16_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    uint16_t *dstp = dst;
    unsigned i;

    for (i = 0; i < n; i++) {
        uint16_t v1 = srcp1[i];
        uint16_t v2 = srcp2[i];
        dstp[i] = (uint16_t)premul_u16(v1, v2, offset, depth) + offset;
    }
}

void vs_premultiply_float_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned offset, unsigned n)
{
    const float *srcp1 = src1;
    const float *srcp2 = src2;
    float *dstp = dst;
    unsigned i;

    (void)depth;
    (void)offset;

    for (i = 0; i < n; i++) {
        dstp[i] = srcp1[i] * srcp2[i];
    }

}

void vs_merge_byte_c(const void *src1, const void *src2, void *dst, union vs_merge_weight weight, unsigned n)
{
    const uint8_t *srcp1 = src1;
    const uint8_t *srcp2 = src2;
    uint8_t *dstp = dst;
    unsigned w = weight.u;
    unsigned i;

    for (i = 0; i < n; i++) {
        unsigned v1 = srcp1[i];
        unsigned v2 = srcp2[i];
        dstp[i] = v1 + (((v2 - v1) * w + ROUND) >> MERGESHIFT);
    }
}

void vs_merge_word_c(const void *src1, const void *src2, void *dst, union vs_merge_weight weight, unsigned n)
{
    const uint16_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    uint16_t *dstp = dst;
    unsigned w = weight.u;
    unsigned i;

    for (i = 0; i < n; i++) {
        unsigned v1 = srcp1[i];
        unsigned v2 = srcp2[i];
        dstp[i] = v1 + (((v2 - v1) * w + ROUND) >> MERGESHIFT);
    }
}

void vs_merge_float_c(const void *src1, const void *src2, void *dst, union vs_merge_weight weight, unsigned n)
{
    const float *srcp1 = src1;
    const float *srcp2 = src2;
    float *dstp = dst;
    float w = weight.f;
    unsigned i;

    for (i = 0; i < n; i++) {
        float v1 = srcp1[i];
        float v2 = srcp2[i];
        dstp[i] = v1 + (v2 - v1) * w;
    }
}


void vs_mask_merge_byte_c(const void *src1, const void *src2, const void *mask, void *dst, unsigned depth, unsigned offset, unsigned n)
{
    const uint8_t *srcp1 = src1;
    const uint8_t *srcp2 = src2;
    const uint8_t *maskp = mask;
    uint8_t *dstp = dst;
    unsigned i;

    (void)offset;
    (void)depth;

    for (i = 0; i < n; i++) {
        uint8_t v1 = srcp1[i];
        uint8_t v2 = srcp2[i];
        uint8_t mask = maskp[i];
        uint8_t invmask = UINT8_MAX - mask;
        uint16_t tmp = invmask * v1 + mask * v2 + UINT8_MAX / 2;
        dstp[i] = tmp / 255;
    }
}

void vs_mask_merge_word_c(const void *src1, const void *src2, const void *mask, void *dst, unsigned depth, unsigned offset, unsigned n)
{
    const uint16_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    const uint16_t *maskp = mask;
    uint16_t *dstp = dst;
    unsigned i;

    uint16_t maxval = (1U << depth) - 1;
    uint32_t div = div_table[depth - 9];
    uint8_t shift = shift_table[depth - 9];

    (void)offset;

    for (i = 0; i < n; i++) {
        uint16_t v1 = srcp1[i];
        uint16_t v2 = srcp2[i];
        uint16_t mask = maskp[i];
        uint16_t invmask = maxval - mask;
        uint32_t tmp = (uint32_t)invmask * v1 + (uint32_t)mask * v2 + maxval / 2;
        dstp[i] = (uint16_t)(((uint64_t)tmp * div) >> (32 + shift));
    }
}

void vs_mask_merge_float_c(const void *src1, const void *src2, const void *mask, void *dst, unsigned depth, unsigned offset, unsigned n)
{
    const float *srcp1 = src1;
    const float *srcp2 = src2;
    const float *maskp = mask;
    float *dstp = dst;
    unsigned i;

    (void)depth;
    (void)offset;

    for (i = 0; i < n; i++) {
        float v1 = srcp1[i];
        float v2 = srcp2[i];
        dstp[i] = v1 + (v2 - v1) * maskp[i];
    }
}

void vs_mask_merge_premul_byte_c(const void *src1, const void *src2, const void *mask, void *dst, unsigned depth, unsigned offset, unsigned n)
{
    const uint8_t *srcp1 = src1;
    const uint8_t *srcp2 = src2;
    const uint8_t *maskp = mask;
    uint8_t *dstp = dst;
    unsigned i;

    (void)depth;

    for (i = 0; i < n; i++) {
        uint8_t v1 = srcp1[i];
        uint8_t v2 = srcp2[i];
        uint8_t invmask = UINT8_MAX - maskp[i];

        int16_t x = (int16_t)premul_u8(v1, invmask, offset) + (int16_t)v2;
        dstp[i] = VSMIN(VSMAX(x, 0), 255);
    }
}

void vs_mask_merge_premul_word_c(const void *src1, const void *src2, const void *mask, void *dst, unsigned depth, unsigned offset, unsigned n)
{
    const uint16_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    const uint16_t *maskp = mask;
    uint16_t *dstp = dst;
    unsigned i;

    uint16_t maxval = (1U << depth) - 1;

    for (i = 0; i < n; i++) {
        uint16_t v1 = srcp1[i];
        uint16_t v2 = srcp2[i];
        uint16_t invmask = maxval - maskp[i];

        int32_t x = (int32_t)premul_u16(v1, invmask, offset, depth) + (int32_t)v2;
        dstp[i] = VSMIN(VSMAX(x, 0), (int32_t)maxval);
    }
}

void vs_mask_merge_premul_float_c(const void *src1, const void *src2, const void *mask, void *dst, unsigned depth, unsigned offset, unsigned n)
{
    const float *srcp1 = src1;
    const float *srcp2 = src2;
    const float *maskp = mask;
    float *dstp = dst;
    unsigned i;

    (void)depth;
    (void)offset;

    for (i = 0; i < n; i++) {
        float v1 = srcp1[i];
        float v2 = srcp2[i];
        dstp[i] = (1.0f - maskp[i]) * v1 + v2;
    }
}

void vs_makediff_byte_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n)
{
    const uint8_t *srcp1 = src1;
    const uint8_t *srcp2 = src2;
    uint8_t *dstp = dst;
    unsigned i;

    (void)depth;

    for (i = 0; i < n; i++) {
        uint8_t v1 = srcp1[i];
        uint8_t v2 = srcp2[i];
        dstp[i] = VSMIN(VSMAX((int)v1 - (int)v2 + 128, 0), 255);
    }
}

void vs_makediff_word_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n)
{
    const uint16_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    uint16_t *dstp = dst;
    unsigned i;

    int32_t half = 1U << (depth - 1);
    int32_t maxval = (1U << depth) - 1;

    for (i = 0; i < n; i++) {
        uint16_t v1 = srcp1[i];
        uint16_t v2 = srcp2[i];
        int32_t tmp = (int32_t)v1 - (int32_t)v2 + half;
        dstp[i] = VSMIN(VSMAX(tmp, 0), maxval);
    }
}

void vs_makediff_float_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n)
{
    const float *srcp1 = src1;
    const float *srcp2 = src2;
    float *dstp = dst;
    unsigned i;

    (void)depth;

    for (i = 0; i < n; i++) {
        dstp[i] = srcp1[i] - srcp2[i];
    }
}

void vs_makefulldiff_byte_word_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n) {
    const uint8_t *srcp1 = src1;
    const uint8_t *srcp2 = src2;
    uint16_t *dstp = dst;
    unsigned i;

    (void)depth;

    for (i = 0; i < n; i++) {
        uint8_t v1 = srcp1[i];
        uint8_t v2 = srcp2[i];
        dstp[i] = (int)v1 - (int)v2 + 256;
    }
}

void vs_makefulldiff_word_word_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n) {
    const uint16_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    uint16_t *dstp = dst;
    unsigned i;

    uint16_t half = 1U << depth;

    for (i = 0; i < n; i++) {
        uint16_t v1 = srcp1[i];
        uint16_t v2 = srcp2[i];
        dstp[i] = v1 - v2 + half;
    }
}

void vs_makefulldiff_word_dword_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n) {
    const uint16_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    uint32_t *dstp = dst;
    unsigned i;

    int32_t half = 1U << depth;

    for (i = 0; i < n; i++) {
        uint16_t v1 = srcp1[i];
        uint16_t v2 = srcp2[i];
        dstp[i] = (int32_t)v1 - (int32_t)v2 + half;
    }
}

void vs_mergediff_byte_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n)
{
    const uint8_t *srcp1 = src1;
    const uint8_t *srcp2 = src2;
    uint8_t *dstp = dst;
    unsigned i;

    (void)depth;

    for (i = 0; i < n; i++) {
        uint8_t v1 = srcp1[i];
        uint8_t v2 = srcp2[i];
        dstp[i] = VSMIN(VSMAX((int)v1 + (int)v2 - 128, 0), 255);
    }
}

void vs_mergediff_word_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n)
{
    const uint16_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    uint16_t *dstp = dst;
    unsigned i;

    int32_t half = 1U << (depth - 1);
    int32_t maxval = (1U << depth) - 1;

    for (i = 0; i < n; i++) {
        uint16_t v1 = srcp1[i];
        uint16_t v2 = srcp2[i];
        int32_t tmp = (int32_t)v1 + (int32_t)v2 - half;
        dstp[i] = VSMIN(VSMAX(tmp, 0), maxval);
    }
}

void vs_mergediff_float_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n)
{
    const float *srcp1 = src1;
    const float *srcp2 = src2;
    float *dstp = dst;
    unsigned i;

    (void)depth;

    for (i = 0; i < n; i++) {
        dstp[i] = srcp1[i] + srcp2[i];
    }
}

void vs_mergefulldiff_word_byte_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n) {
    const uint8_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    uint8_t *dstp = dst;
    unsigned i;

    (void)depth;

    for (i = 0; i < n; i++) {
        uint8_t v1 = srcp1[i];
        uint16_t v2 = srcp2[i];
        dstp[i] = VSMIN(VSMAX((int32_t)v1 + (int32_t)v2 - 256, 0), 255);
    }
}

void vs_mergefulldiff_word_word_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n) {
    const uint16_t *srcp1 = src1;
    const uint16_t *srcp2 = src2;
    uint16_t *dstp = dst;
    unsigned i;

    int32_t half = 1U << depth;
    int32_t maxval = (1U << depth) - 1;

    for (i = 0; i < n; i++) {
        uint16_t v1 = srcp1[i];
        uint16_t v2 = srcp2[i];
        int32_t tmp = (int32_t)v1 + (int32_t)v2 - half;
        dstp[i] = VSMIN(VSMAX(tmp, 0), maxval);
    }
}

void vs_mergefulldiff_dword_word_c(const void *src1, const void *src2, void *dst, unsigned depth, unsigned n) {
    const uint16_t *srcp1 = src1;
    const uint32_t *srcp2 = src2;
    uint16_t *dstp = dst;
    unsigned i;

    int32_t half = 1U << depth;
    int32_t maxval = (1U << depth) - 1;

    for (i = 0; i < n; i++) {
        uint16_t v1 = srcp1[i];
        uint32_t v2 = srcp2[i];
        int32_t tmp = (int32_t)v1 + (int32_t)v2 - half;
        dstp[i] = VSMIN(VSMAX(tmp, 0), maxval);
    }
}