@@ -342,9 +342,27 @@ expand_mask_simd(uint8_t* dstp, uint8_t* srcp, const int dpitch,
342
342
}
343
343
344
344
345
+ Buffer::Buffer (size_t pitch, int height, int hsize, size_t align, bool ip,
346
+ ise_t * e) :
347
+ env(e), isPlus(ip)
348
+ {
349
+ size_t size = pitch * height * hsize + align;
350
+ orig = alloc_buffer (size, align, isPlus, env);
351
+ buffp = reinterpret_cast <uint8_t *>(orig) + align;
352
+ }
353
+
354
+
355
+ Buffer::~Buffer ()
356
+ {
357
+ free_buffer (orig, isPlus, env);
358
+ }
359
+
360
+
361
+
345
362
CombMask::CombMask (PClip c, int cth, int mth, bool ch, arch_t arch, bool e,
346
363
int metric, bool plus) :
347
- GVFmod(c, ch, arch, plus), cthresh(cth), mthresh(mth), expand(e)
364
+ GVFmod(c, ch, arch, plus), cthresh(cth), mthresh(mth), expand(e),
365
+ buff(nullptr )
348
366
{
349
367
validate (!vi.IsPlanar (), " planar format only." );
350
368
validate (metric != 0 && metric != 1 , " metric must be set to 0 or 1." );
@@ -363,6 +381,7 @@ CombMask::CombMask(PClip c, int cth, int mth, bool ch, arch_t arch, bool e,
363
381
buffPitch += 2 ;
364
382
}
365
383
buffPitch &= (~(align - 1 ));
384
+ needBuff = mthresh > 0 || expand;
366
385
367
386
switch (arch) {
368
387
#if defined(__AVX2__)
@@ -391,33 +410,24 @@ CombMask::CombMask(PClip c, int cth, int mth, bool ch, arch_t arch, bool e,
391
410
if (mthresh > 0 && child->SetCacheHints (CACHE_GET_WINDOW, 0 ) < 3 ) {
392
411
child->SetCacheHints (CACHE_WINDOW, 3 );
393
412
}
394
- }
395
-
396
413
414
+ if (!isPlus && needBuff) {
415
+ buff = new Buffer (buffPitch, vi.height , mthresh > 0 ? 2 : 1 , align,
416
+ false , nullptr );
397
417
398
- class Buffer {
399
- ise_t * env;
400
- bool isPlus;
401
- void * orig;
402
- public:
403
- uint8_t * buffp;
404
-
405
- Buffer (size_t pitch, int height, int hsize, size_t align, bool ip, ise_t * e) :
406
- env (e), isPlus(ip)
407
- {
408
- size_t size = pitch * height * hsize + align;
409
- orig = alloc_buffer (size, align, isPlus, env);
410
- buffp = reinterpret_cast <uint8_t *>(orig) + align;
411
418
}
419
+ }
412
420
413
- ~Buffer ()
414
- {
415
- free_buffer (orig, isPlus, env);
421
+
422
+ CombMask::~CombMask ()
423
+ {
424
+ if (!isPlus && needBuff) {
425
+ delete buff;
416
426
}
417
- };
427
+ }
418
428
419
429
420
- PVideoFrame __stdcall CombMask::GetFrame (int n, IScriptEnvironment * env)
430
+ PVideoFrame __stdcall CombMask::GetFrame (int n, ise_t * env)
421
431
{
422
432
static const int planes[] = { PLANAR_Y, PLANAR_U, PLANAR_V };
423
433
@@ -428,11 +438,13 @@ PVideoFrame __stdcall CombMask::GetFrame(int n, IScriptEnvironment* env)
428
438
429
439
PVideoFrame dst = env->NewVideoFrame (vi, align);
430
440
431
- Buffer* b = nullptr ;
432
- uint8_t *buffp = nullptr , *tmpp = nullptr ;
433
- if (mthresh > 0 || expand) {
434
- b = new Buffer (buffPitch, vi.height , mthresh > 0 ? 2 : 1 , align,
435
- isPlus, env);
441
+ Buffer* b = buff;
442
+ uint8_t *buffp, *tmpp;
443
+ if (needBuff) {
444
+ if (isPlus) {
445
+ b = new Buffer (buffPitch, vi.height , mthresh > 0 ? 2 : 1 , align,
446
+ isPlus, env);
447
+ }
436
448
buffp = b->buffp ;
437
449
tmpp = buffp + vi.height * buffPitch;
438
450
}
@@ -447,7 +459,7 @@ PVideoFrame __stdcall CombMask::GetFrame(int n, IScriptEnvironment* env)
447
459
const int width = src->GetRowSize (plane);
448
460
const int height = src->GetHeight (plane);
449
461
450
- if (b == nullptr ) {
462
+ if (!needBuff ) {
451
463
writeCombMask (dstp, srcp, dpitch, spitch, cthresh, width, height);
452
464
continue ;
453
465
}
@@ -473,7 +485,7 @@ PVideoFrame __stdcall CombMask::GetFrame(int n, IScriptEnvironment* env)
473
485
expandMask (dstp, buffp, dpitch, buffPitch, width, height);
474
486
}
475
487
476
- if (b != nullptr ) {
488
+ if (isPlus && needBuff ) {
477
489
delete b;
478
490
}
479
491
0 commit comments