Skip to content

Commit 1587571

Browse files
committed
libvmaf/integer_motion: fix subsample
1 parent dd32914 commit 1587571

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

libvmaf/src/feature/integer_motion.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
#include <errno.h>
21+
#include <limits.h>
2122
#include <stddef.h>
2223
#include <stdlib.h>
2324
#include <string.h>
@@ -59,6 +60,7 @@ typedef struct MotionState {
5960
bool motion_moving_average;
6061
bool motion_force_zero;
6162
bool debug;
63+
unsigned last_index;
6264
VmafDictionary *feature_name_dict;
6365
} MotionState;
6466

@@ -267,6 +269,8 @@ static int init(VmafFeatureExtractor *fex, enum VmafPixelFormat pix_fmt,
267269
s->y_row = malloc(sizeof(*s->y_row) * w);
268270
if (!s->y_row) return -ENOMEM;
269271

272+
s->last_index = UINT_MAX;
273+
270274
if (bpc == 8)
271275
s->pipeline = motion_score_pipeline_8;
272276
else
@@ -333,6 +337,8 @@ static int extract(VmafFeatureExtractor *fex,
333337
"VMAF_integer_feature_motion_sad_score", score, index);
334338
if (err) return err;
335339

340+
s->last_index = index;
341+
336342
if (s->debug) {
337343
return vmaf_feature_collector_append_with_dict(feature_collector,
338344
s->feature_name_dict,
@@ -362,18 +368,13 @@ static int flush(VmafFeatureExtractor *fex,
362368

363369
const char *sad_name = "VMAF_integer_feature_motion_sad_score";
364370

365-
unsigned n_frames = 0;
366-
double dummy;
367-
while (!vmaf_feature_collector_get_score(feature_collector,
368-
sad_name, &dummy, n_frames))
369-
n_frames++;
370-
371+
if (s->last_index == UINT_MAX) return 1;
372+
const unsigned n = s->last_index + 1;
371373
const unsigned stride = s->motion_five_frame_window ? 2 : 1;
372374
const unsigned min_idx = s->motion_five_frame_window ? 2 : 1;
373-
if (n_frames == 0) return 1;
374375

375376
double stamp_value = 0.;
376-
if (n_frames > min_idx) {
377+
if (n > min_idx) {
377378
double sad_at_min_idx;
378379
if (!vmaf_feature_collector_get_score(feature_collector, sad_name,
379380
&sad_at_min_idx, min_idx)) {
@@ -385,21 +386,26 @@ static int flush(VmafFeatureExtractor *fex,
385386
}
386387

387388
double prev_processed = 0.;
388-
for (unsigned i = 0; i < n_frames; i++) {
389+
for (unsigned i = 0; i < n; i++) {
390+
double sad_i;
391+
vmaf_feature_collector_get_score(feature_collector, sad_name, &sad_i, i);
392+
389393
double motion2;
390394

391395
if (i < min_idx) {
392396
motion2 = 0.;
393-
} else if (i == n_frames - 1) {
394-
vmaf_feature_collector_get_score(feature_collector, sad_name, &motion2, i);
395397
} else {
396398
const int lo_idx = (int)i - (int)(stride - 1);
397399
const int hi_idx = (int)i + 1;
398400
double hi;
399-
vmaf_feature_collector_get_score(feature_collector, sad_name, &hi, hi_idx);
400-
if (lo_idx >= (int)min_idx) {
401+
const bool has_hi = !vmaf_feature_collector_get_score(
402+
feature_collector, sad_name, &hi, hi_idx);
403+
if (!has_hi) {
404+
motion2 = sad_i;
405+
} else if (lo_idx >= (int)min_idx) {
401406
double lo;
402-
vmaf_feature_collector_get_score(feature_collector, sad_name, &lo, lo_idx);
407+
vmaf_feature_collector_get_score(
408+
feature_collector, sad_name, &lo, lo_idx);
403409
motion2 = lo < hi ? lo : hi;
404410
} else {
405411
motion2 = hi;

libvmaf/src/libvmaf.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,12 @@ static void threaded_extract_batch_func(void *e, void **thread_data)
507507
if (fex->flags & VMAF_FEATURE_EXTRACTOR_TEMPORAL)
508508
continue;
509509

510-
if ((f->n_subsample > 1) && (f->index % f->n_subsample))
511-
continue;
510+
const uint64_t no_subsample_flags =
511+
VMAF_FEATURE_EXTRACTOR_TEMPORAL | VMAF_FEATURE_EXTRACTOR_PREV_REF;
512+
if (!(fex->flags & no_subsample_flags)) {
513+
if ((f->n_subsample > 1) && (f->index % f->n_subsample))
514+
continue;
515+
}
512516

513517
if (!td->fex_ctx[i]) {
514518
VmafDictionary *opts_dict = f->registered_fex->fex_ctx[i]->opts_dict;
@@ -854,7 +858,9 @@ int vmaf_read_pictures(VmafContext *vmaf, VmafPicture *ref, VmafPicture *dist,
854858
VmafFeatureExtractorContext *fex_ctx =
855859
vmaf->registered_feature_extractors.fex_ctx[i];
856860

857-
if (!(fex_ctx->fex->flags & VMAF_FEATURE_EXTRACTOR_TEMPORAL)) {
861+
const uint64_t no_subsample_flags =
862+
VMAF_FEATURE_EXTRACTOR_TEMPORAL | VMAF_FEATURE_EXTRACTOR_PREV_REF;
863+
if (!(fex_ctx->fex->flags & no_subsample_flags)) {
858864
if ((vmaf->cfg.n_subsample > 1) && (index % vmaf->cfg.n_subsample))
859865
continue;
860866
}

0 commit comments

Comments
 (0)