Skip to content

Refactor context structs to use constructors - #869

Merged
scotts merged 3 commits into
meta-pytorch:mainfrom
scotts:struct_init
Sep 5, 2025
Merged

Refactor context structs to use constructors#869
scotts merged 3 commits into
meta-pytorch:mainfrom
scotts:struct_init

Conversation

@scotts

@scotts scotts commented Sep 4, 2025

Copy link
Copy Markdown
Contributor

Because we're creating this in one place, with all fields always available, we should use a constructor. Note that the current code does not yet use FiltersContext::hwFramesCtx, so we don't accept it in the constructor yet.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 4, 2025
@scotts
scotts marked this pull request as ready for review September 4, 2025 16:58
AVRational timeBase = {0, 0};
UniqueAVBufferRef hwFramesCtx;

FiltersContext() = default;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should default initializers be removed from the above then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, we can't do that as they are default-initialized as members of CpuDeviceInterface: https://github.com/pytorch/torchcodec/blob/da3eddaa6746d093b09ad1413a42d7244686c58e/src/torchcodec/_core/CpuDeviceInterface.h#L64-L67
Before we decode anything, they start out empty. We could instead make the members unique pointers, but I think that's more complexity than it's worth.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah, you are right. But in this case, I just realized that I forgot to add default initializers for SwsFrameContext struct. Can you help to fix within this PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You might wish to adjust a format as AV_PIX_FMT_NONE defined as -1 (https://github.com/FFmpeg/FFmpeg/blob/6891314db18c96afa35f5e3765d60f4e257fc665/libavutil/pixfmt.h#L72):

AVPixelFormat inputFormat = AV_PIX_FMT_NONE;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@dvrogozh, oh, you mean for the individual fields. Yes, got it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes. Thank you for the change.

@dvrogozh dvrogozh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good.

AVRational timeBase = {0, 0};
UniqueAVBufferRef hwFramesCtx;

FiltersContext() = default;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes. Thank you for the change.

@scotts
scotts merged commit 8662ee1 into meta-pytorch:main Sep 5, 2025
47 checks passed
@scotts
scotts deleted the struct_init branch September 5, 2025 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants