Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct Rav1dFrameContext_lf::cdef_lpf_line: Make ptrs into offsets #779

Merged
merged 1 commit into from
Mar 5, 2024

Conversation

randomPoison
Copy link
Collaborator

I'm getting a couple of weird test failures on this, but I'm putting it up in a PR now so that others can review.

@fbossen
Copy link
Collaborator

fbossen commented Mar 1, 2024

I'm getting a couple of weird test failures on this, but I'm putting it up in a PR now so that others can review.

Could it be that cdef_line_buf is a vector of u8 and that some offsets are meant to be applied to vectors of BD::Pixel which may be 1 or 2 bytes wide?

@randomPoison
Copy link
Collaborator Author

In theory that's handled in the PR. The logic for initializing cdef_lpf_line is in decode.rs, it's been updated to use offsets calculated in pixels, which are then used in bitdepth-dependent code to offset into a pixel array.

Also the errors I'm seeing are actually panics for out of bounds indexing, but it's happening because the slice being indexed into is empty. This happens in backup2lines, which is being passed all of cdef_line_buf for failing slice in question. I didn't actually change anything about how backup2lines is being called in this PR, so I'm not sure why that buffer is now sometimes empty. My guess was that it was related to the mem::take I'm now doing, but I don't see how the function in question can get called while we've moved the field out of the struct🤔

Looks like there might also be a segfault happening? That could be related to byte vs pixel offsets if we're calculating a pointer incorrectly and then passing it to asm.

@fbossen
Copy link
Collaborator

fbossen commented Mar 2, 2024

Looks like there might also be a segfault happening? That could be related to byte vs pixel offsets if we're calculating a pointer incorrectly and then passing it to asm.

Yes, I am seeing segfaults. However, those seem to happen only when multiple threads are used.

@randomPoison
Copy link
Collaborator Author

Well none of the code I touch in this PR is used when single threaded, I believe. Or at least we only initialize cdef_lpf_line if there is more than one task context, i.e. if we have multiple threads. I was only able to get tests to hit that code when testing with multiple threads.

@kkysen
Copy link
Collaborator

kkysen commented Mar 2, 2024

Also the errors I'm seeing are actually panics for out of bounds indexing, but it's happening because the slice being indexed into is empty

I didn't look at the code really, but maybe this is because of the mem::take somehow?

@randomPoison
Copy link
Collaborator Author

Yeah that's my current theory, but I can't tell how that's possible. It'd require that rav1d_cdef_brow is called from rav1d_copy_lpf, and I can't see how that could happen.

@kkysen
Copy link
Collaborator

kkysen commented Mar 2, 2024

Is there concurrent access to the Vec?

@fbossen
Copy link
Collaborator

fbossen commented Mar 2, 2024

Looks like multiple threads can call filter_sbrow_deblock_rows / rav1d_filter_sbrow_deblock_rows / rav1d_copy_lpf concurrently.

@randomPoison
Copy link
Collaborator Author

I hope there's not concurrent access because we're not doing anything to synchronize access within those functions 😰 @fbossen are concurrent calls also acting on the same data? I was assuming each task had its own data since there's no synchronization happening.

@kkysen
Copy link
Collaborator

kkysen commented Mar 2, 2024

Maybe the offsets split the Vec into sections where different sections are mutated concurrently, but each section is not mutated concurrently?

@fbossen
Copy link
Collaborator

fbossen commented Mar 3, 2024

I hope there's not concurrent access because we're not doing anything to synchronize access within those functions 😰 @fbossen are concurrent calls also acting on the same data? I was assuming each task had its own data since there's no synchronization happening.

cdf_line_buf is used as a collection of smaller buffers of size 4 * stride. These smaller buffers can be concurrently written to, but there is no overlap/conflict in the store operations.

Copy link
Collaborator

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

The uses of f in fn backup_lpf that prevent us from &mut f.lf.cdef_line_buf borrowing are:

  • frame_hdr:
  • dsp: *const Rav1dDSPContext: this is a raw pointer, so we can ignore borrowck issues for now. Eventually, it can be made a global with a OnceCell/AtomicPtr/etc.
  • resize_step: [c_int; 2]: Copy
  • resize_start: [c_int; 2]: Copy
  • f.bitdepth_max: c_int: Copy

Thus, we can change fn backup_lpf to this:

unsafe fn backup_lpf<BD: BitDepth>(
    c: &Rav1dContext,
    frame_hdr: &Rav1dFrameHeader,
    dsp: &Rav1dDSPContext,
    resize_step: [c_int; 2],
    resize_start: [c_int; 2],
    bitdepth_max: c_int,

That should fix the borrowck issues while keeping the Vec in place so that concurrent modifications to different sections of it work. Hopefully that works.

This is a bit unwieldy passing all of the arguments separately, but I think it's fine. It also reduces the number of frame_hdr .unwrap()s, too, as it's already .unwrap()ed in the caller.

@kkysen kkysen changed the title Rav1dFrameContext_lf::cdef_lpf_line: Make ptrs into offsets struct Rav1dFrameContext_lf::cdef_lpf_line: Make ptrs into offsets Mar 4, 2024
@randomPoison randomPoison force-pushed the legare/rav1dframecontext_lf/cdef_line branch from 73689be to 1d83d68 Compare March 4, 2024 18:03
@randomPoison randomPoison force-pushed the legare/rav1dframecontext_lf/cdef_lpf_line branch 2 times, most recently from e41e8ec to eae6239 Compare March 4, 2024 18:40
@randomPoison randomPoison marked this pull request as ready for review March 4, 2024 18:50
@randomPoison
Copy link
Collaborator Author

Looks like passing in the variables separately worked 🎉

@randomPoison randomPoison requested a review from kkysen March 4, 2024 20:07
Copy link
Collaborator

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

LGTM! Good to merge once #777 is merged (after #777 is rebased on #780).

src/decode.rs Show resolved Hide resolved
@randomPoison randomPoison force-pushed the legare/rav1dframecontext_lf/cdef_line branch from 1d83d68 to 89acd73 Compare March 4, 2024 23:08
@randomPoison randomPoison force-pushed the legare/rav1dframecontext_lf/cdef_lpf_line branch from eae6239 to e1c517e Compare March 4, 2024 23:50
@randomPoison randomPoison force-pushed the legare/rav1dframecontext_lf/cdef_line branch from 89acd73 to 285a342 Compare March 5, 2024 19:04
@randomPoison randomPoison force-pushed the legare/rav1dframecontext_lf/cdef_lpf_line branch from e1c517e to 325b0dd Compare March 5, 2024 19:06
Base automatically changed from legare/rav1dframecontext_lf/cdef_line to main March 5, 2024 19:43
@randomPoison randomPoison merged commit c988a43 into main Mar 5, 2024
34 checks passed
@randomPoison randomPoison deleted the legare/rav1dframecontext_lf/cdef_lpf_line branch March 5, 2024 22:56
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.

3 participants