Skip to content

Implement dav1d-rs's Rust API#1439

Open
leo030303 wants to merge 57 commits intomemorysafety:mainfrom
leo030303:add-rust-api
Open

Implement dav1d-rs's Rust API#1439
leo030303 wants to merge 57 commits intomemorysafety:mainfrom
leo030303:add-rust-api

Conversation

@leo030303
Copy link
Contributor

@leo030303 leo030303 commented Jul 6, 2025

I've copy pasted the PR from #1362 and updated it with some of the suggestions made on that pull request. The main changes are:

  • Have all the public-facing Rust API code in its own file.
  • Used some enums from rav1d instead of redefining new ones, and added in doc comments from the original dav1d-rs library to a few items.
  • Removed as much unsafe code as I could and replaced it with the Rust methods from rav1d as much as possible.

It currently works as a drop-in replacement for dav1d-rs; adding in use rav1d as dav1d; to my fork of image makes everything work fine.

The only functional changes I made are I removed the unsafe impls of Send and Sync for InnerPicture so Picture is no longer Sync or Send. I looked through the code and I don't believe DisjointMut<Rav1dPictureDataComponentInner>, which is a field of one of its children, is thread safe, though I'm open to correction there; I'm pretty unfamiliar with unsafe Rust.

I also don't have safety comments on the two unsafe blocks in rust_api.rs; I'm unsure what these would look like, so open to suggestions there. These are mostly taken verbatim from the old pull request.

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.

Thanks for the much safer implementation! I'm still taking a closer look at the unsafes, as the few remaining are still quite critical, but I wanted to give some initial feedback in general first.

@kkysen kkysen changed the title Implement Rust API Implement dav1d-rs's Rust API Jul 7, 2025
kkysen added a commit that referenced this pull request Jul 7, 2025
Pulled out the docs additions from #1439 into its own PR.
@leo030303 leo030303 requested a review from kkysen July 8, 2025 22:23
kkysen added a commit that referenced this pull request Jul 15, 2025
…1442)

Pulled out the `Rav1dError` changes from #1439 into a separate PR.
kkysen added a commit that referenced this pull request Jul 15, 2025
…e `u32` (#1443)

Pulled out type changes from here #1439 into its own PR.
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.

@leo030303, could you rebase this on main? There are a lot of other commits in here now, so it's harder to review.

leo030303 and others added 19 commits July 19, 2025 12:29
Co-authored-by: Khyber Sen <kkysen@gmail.com>
Co-authored-by: Khyber Sen <kkysen@gmail.com>
Co-authored-by: Khyber Sen <kkysen@gmail.com>
Co-authored-by: Khyber Sen <kkysen@gmail.com>
Co-authored-by: Khyber Sen <kkysen@gmail.com>
Co-authored-by: Khyber Sen <kkysen@gmail.com>
Co-authored-by: Khyber Sen <kkysen@gmail.com>
Co-authored-by: Khyber Sen <kkysen@gmail.com>
Co-authored-by: Khyber Sen <kkysen@gmail.com>
src/rust_api.rs Outdated
Comment on lines 217 to 194
if let Some(mut pending_data) = self.pending_data.take() {
let _ = mem::take(&mut pending_data);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

So the self.pending_data.take() here replaces self.pending_data = None and then moves the Rav1dData to the local variable pending_data. That local variable is dropped no matter what, so the mem::take(&mut pending_data) should be redundant.

src/rust_api.rs Outdated
if stride == 0 || data.is_null() {
return &[];
}
// SAFETY: Copied checks from david-rs added in this pull request https://github.com/rust-av/dav1d-rs/pull/121
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this needs its own SAFETY comment. There's none in rust-av/dav1d-rs#121.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest I don't know what a valid SAFETY comment looks like, I've never really used languages like C or unsafe rust so I'm unsure how you'd guarantee there's no undefined behaviour when you're using raw pointers, if yourself or anyone else has any suggestions here I'd be happy to help review

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.

Sorry again for such a long wait. I left some comments, and could you also rebase to remove the merge commits? It's also important that we keep each commit buildable/testable so that we can do benchmarking on each commit, so if any commits aren't, could you rebase them such that they are? Thanks!!

@Shnatsel
Copy link

Shnatsel commented Feb 6, 2026

This PR has a rather long commit history. Rewriting the history with such granularity at this point is completely impractical. It's probably best to squash it down to one commit before merging if every commit on main being testable is a requirement. Github has this as an option in the merge button dropdown.

@tats-u
Copy link

tats-u commented Feb 6, 2026

@Shnatsel How about "Squash and merge" instead of "Create a merge commit" or "Rebase and merge"? It will automatically do it.

kkysen added a commit that referenced this pull request Feb 6, 2026
Pulled from #1439.

Co-authored-by: Leo Ring <leoring03@gmail.com>
kkysen added a commit that referenced this pull request Feb 6, 2026
Pulled from #1439.

Co-authored-by: Leo Ring <leoring03@gmail.com>
@kkysen
Copy link
Collaborator

kkysen commented Feb 6, 2026

This PR has a rather long commit history. Rewriting the history with such granularity at this point is completely impractical. It's probably best to squash it down to one commit before merging if every commit on main being testable is a requirement. Github has this as an option in the merge button dropdown.

Yup, this is fine, too. I pulled out the two small changes that seem more separate (#1468, #1469). The rest seems fine to be squashed into one commit.

kkysen added a commit that referenced this pull request Feb 6, 2026
kkysen added a commit that referenced this pull request Feb 6, 2026
kkysen added a commit that referenced this pull request Feb 6, 2026
kkysen added a commit that referenced this pull request Feb 6, 2026
kkysen added a commit that referenced this pull request Feb 6, 2026
@leo030303 leo030303 requested a review from kkysen February 7, 2026 23:37
@leo030303
Copy link
Contributor Author

@kkysen Thanks for the review, resolved most of the comments, and happy to see all the outside interest in the PR!

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.

Provide Rust API

6 participants