Skip to content

libvmaf: count a picture pair only after it is accepted - #1718

Open
lusoris wants to merge 4 commits into
Netflix:masterfrom
vidiko:libvmaf-pic-cnt-after-1652
Open

lusoris wants to merge 4 commits into
Netflix:masterfrom
vidiko:libvmaf-pic-cnt-after-1652

Conversation

@lusoris

@lusoris lusoris commented Nov 3, 2026

Copy link
Copy Markdown

Depends on #1652.

vmaf_read_pictures() counts a picture pair before checking it, so a pair that is then rejected still counts. This change moves the increment after the checks.

Problem

vmaf->pic_cnt++ runs before validate_pic_params() and check_picture_pool(). When either of them rejects a pair (for example a 96x64 pair after 64x64 pairs), the call returns -EINVAL, but pic_cnt has already been incremented.

The output writers use pic_cnt as the end of the pooling range, so the rejected index is part of it:

  • The pooled metrics in XML and JSON output come out empty (XML writes <metric name="psnr_y" /> with no values), because the rejected index has no score. Per-frame scores for the accepted pairs are still written.
  • The fps value in the output is computed from pic_cnt, so it counts the rejected pair.
  • vmaf_score_pooled() with an explicit range is not affected.

Reproduction: feed five 64x64 pairs, then one 96x64 pair (rejected with -EINVAL), flush, and write JSON. The pooled_metrics section has no PSNR entries.

Change

This branch is stacked on #1652, so the diff here also shows its commit (libvmaf: release both pictures on every return of vmaf_read_pictures()). Only the last commit belongs to this PR:

  • libvmaf/src/libvmaf.c: increment pic_cnt only after validate_pic_params() and check_picture_pool() have accepted the pair. Nothing else changes.
  • libvmaf/test/test_read_pictures_output.c: new test for the scenario above. It checks that the rejected pair is released, as libvmaf: release both pictures on every return of vmaf_read_pictures() #1652 specifies, then writes JSON and checks that the pooled PSNR metrics are present.

Testing

Run on this branch (on top of #1652, 76afad3):

Related

#1639 also touches vmaf_read_pictures(), in a hunk next to this line, and does not change where the count is incremented. #1652 and #1639 are unchanged by this PR.

libvmaf.h says the context takes ownership of both pictures, but a call
that returned an error left them with the caller: a caller that followed
the header leaked the pair, and one that took the pair from
vmaf_preallocate_pictures() hung in vmaf_close(), which waits for every
pool picture.

Once the call has a context and two pictures, every return now drops
both references through the caller's pointers. vmaf_picture_unref()
clears the struct, so a caller that unrefs after the error gets -EINVAL
and releases nothing twice. A call without a context, with only one of
the two pictures, or the flush call still takes nothing.

The new test submits a pair of another shape than the stream's and
checks the references, the harmless second unref, the calls that take
nothing, and that vmaf_close() returns with a pool pair; its meson entry
carries a timeout so a regression fails instead of hanging.
vmaf_read_pictures incremented pic_cnt before a rejected picture
pair passed validation and picture-pool setup. This made output pooling
look for a score at the rejected index, leaving pooled metrics empty
and including rejected input in fps.

Add a public API regression test that writes JSON after five accepted
pairs and a rejected mismatched pair. It verifies that the rejected
pair is released and that the PSNR pooled metrics are populated.

Signed-off-by: Lusoris <lusoris@pm.me>

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few ownership / error-handling gaps in the new vmaf_read_pictures failure paths:

  1. libvmaf/src/libvmaf.c — after translate_picture for ref you correctly release and return on error, but the following translate_picture for dist (around the dist_host / dist_device setup) still has no if (err) check. A failed dist translate leaves err set and falls into the extractor loop with incomplete host/device pictures.

  2. On the CUDA error returns you added inside the extractor loop (and after check_ring_buffer / ref translate), release_picture_pair(ref_in, dist_in) runs but ref_host / ref_device / dist_host / dist_device are not unreferenced, so those translated pictures can leak.

  3. vmaf->pic_cnt++ still happens before check_ring_buffer / translate_picture / extract. If any of those fail, the pictures are released but pic_cnt stays incremented, so the next accepted frame sees a skewed count.

Also worth double-checking the thread_pool path: threaded_read_pictures_batch already unrefs the caller's pictures on the successful enqueue path (return vmaf_picture_unref(ref) | vmaf_picture_unref(dist)). If that combined unref returns non-zero, the new if (err) release_picture_pair(ref_in, dist_in) will unref again on already-cleared structs.

Under HAVE_CUDA, check_ring_buffer() and the reference
translate_picture() can still reject the pair after pic_cnt was
incremented; move the increment below them.
@lusoris

lusoris commented Nov 3, 2026

Copy link
Copy Markdown
Author

Thanks for the review.

  1. Fixed: vmaf->pic_cnt++ now sits after the CUDA check_ring_buffer() and translate_picture() calls (037af51), so a pair rejected there is not counted.
  2. The unchecked dist translate_picture() is an unchanged upstream line; this PR does not touch it (it appears only as diff context).
  3. The host/device copies on the CUDA error returns are not released on master either, and the CUDA unref tail (if (ref_host.priv) onwards) is outside this PR's hunks. This PR only adds the release of the caller's ref/dist on those returns; the translated copies are left as they were.
  4. The second unref in the thread_pool path is a no-op: vmaf_picture_unref() returns -EINVAL for a picture without ref (libvmaf/src/picture.c:179) and clears the struct after every successful unref (picture.c:188), so release_picture_pair() on pictures the batch already released does nothing.

vmaf_write_output() opens the output file in text mode, so on Windows
every newline is written as CRLF. test_read_pictures_output read the
file back in binary mode and looked for "{\n      \"min\":", which
never matched under MSVC.

Read the file in text mode so CRLF folds back to LF, and accept a
short fread(): a text-mode read returns fewer bytes than ftell()
reports once CRLF pairs fold.

Signed-off-by: Lusoris <lusoris@pm.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants