Skip to content

AVFoundation player: load non-square-pixel (anamorphic) video at its real pixel size (revives #4949) - #8656

Merged
danoli3 merged 2 commits into
openframeworks:masterfrom
danoli3:avf-anamorphic-pixels
Nov 8, 2026
Merged

danoli3 merged 2 commits into
openframeworks:masterfrom
danoli3:avf-anamorphic-pixels

Conversation

@danoli3

@danoli3 danoli3 commented Nov 8, 2026

Copy link
Copy Markdown
Member

Revives #4949 by @ofTheo, which no longer applies to master. Fixes #4937.

Problem

ofAVFoundationPlayer takes the video size from AVAssetTrack.naturalSize, which is the display size. Anamorphic video (for example HDV, 1440x1080 stored and shown as 1920x1080) stores fewer pixels than that. The player reports 1920x1080 while the CVPixelBuffer is 1440x1080, so pixels.allocate(getWidth(), getHeight()) is too wide for the vImage conversion (which reads the buffer's own size), and the texture is drawn stretched. See the forum thread: https://forum.openframeworks.cc/t/issues-with-pixel-ratios-in-ofvideoplayer/22120/10

Change

  • Two commits cherry-picked from Bugfix correctly load non square pixels for AVF player. Closes #4937 #4949 with -x, authorship kept.
  • ofAVFoundationVideoPlayer.m now reads the pixel aspect ratio from the track's format description and sets getWidth() / getHeight() to the real pixel size.
  • New ofAVFoundationPlayer::getDisplayWidth(), getDisplayHeight() and isAnamorphic(), so users can draw at the intended size. Drawing stays up to the user, as in the original PR.

Adapted for current master, since the load code is now in a block using self->:

  • Conflict resolved by hand in ofAVFoundationVideoPlayer.m and ofAVFoundationPlayer.h (tabs vs spaces).
  • Skipped the two "travis" cast commits; the casts are folded in. I also read the aspect values as NSNumber (not NSString), require both spacings above zero, and round instead of truncating.
  • The streaming path that fills the size from presentationSize (display size) now sets the display size from it and the pixel size from the first CVPixelBuffer.

Example:

if (player.isAnamorphic()) {
    player.draw(0, 0, player.getDisplayWidth(), player.getDisplayHeight());
} else {
    player.draw(0, 0);
}

Platforms

  • macOS: the fix applies here.
  • iOS: ofxiOSVideoPlayer uses its own addons/ofxiOS/src/video/AVFoundationVideoPlayer.m, which already takes the size from CVPixelBufferGetWidth/Height, so it is not affected and is untouched.

Tested

  • ofAVFoundationVideoPlayer.m and ofAVFoundationPlayer.mm pass clang -fsyntax-only with ARC on macOS (10.15 target); the .m also passes against the iOS 15 simulator SDK. No new warnings.
  • Not run against an anamorphic video file. The HDV test clip from the forum thread would be the way to confirm the crash is gone and the texture is correct.

@danoli3

danoli3 commented Nov 8, 2026 •

Copy link
Copy Markdown
Member Author

Test result

Ran a standalone harness that builds ofAVFoundationVideoPlayer.m and loads two ffmpeg-generated clips through the real player (macOS, no full oF build):

  • HDV style: 1440x1080 stored, pixel aspect 4:3, displayed 1920x1080
  • Square pixels: 640x360 (regression check)
HDV getWidth()/getHeight() Decoded CVPixelBuffer Result
master 1920x1080 1440x1080 FAIL: size does not match the pixel data (CRASH)
This PR 1440x1080 1440x1080 pass

On this PR the HDV clip also reports getDisplayWidth()/getDisplayHeight() = 1920x1080 and isAnamorphic() = true. The square clip passes on both, with isAnamorphic() false on this PR.

Not covered: the iOS player (separate class, untouched), the streaming-URL path, and a full oF app drawing the texture. The harness is not part of this PR.

@danoli3
danoli3 force-pushed the avf-anamorphic-pixels branch from 6fc8e2c to 7f9a645 Compare November 8, 2026 06:02

@danoli3 danoli3 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

looks good review

@danoli3
danoli3 merged commit 956f452 into openframeworks:master Nov 8, 2026
29 checks passed
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.

AVFoundation player doesn't load HDV ( non-square pixels ) correctly.

2 participants