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 ;
0 commit comments