Skip to content

Commit 0313f7d

Browse files
sdroegelu-zero
authored andcommitted
Document that get_picture() should be called only once per submitted input frame
Otherwise the decoder can't make best use of frame threading and will also more likely run into a race condition in dav1d that causes a deadlock. Instead calling it in a loop should only happen at the very end to drain all pending frames. Update the example accordingly. Fixes #72
1 parent db21867 commit 0313f7d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ impl Decoder {
320320
///
321321
/// If this returns `Err([Error::Again])` then further data has to be sent to the decoder
322322
/// before further decoded frames become available.
323+
///
324+
/// To make most use of frame threading this function should only be called once per submitted
325+
/// input frame and not until it returns `Err([Error::Again])`. Calling it in a loop should
326+
/// only be done to drain all pending frames at the end.
323327
pub fn get_picture(&mut self) -> Result<Picture, Error> {
324328
unsafe {
325329
let mut pic: Dav1dPicture = mem::zeroed();

tools/src/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct Opt {
7474
use std::fs::File;
7575
use std::io::BufReader;
7676

77-
fn handle_pending_pictures(dec: &mut dav1d::Decoder) {
77+
fn handle_pending_pictures(dec: &mut dav1d::Decoder, drain: bool) {
7878
loop {
7979
match dec.get_picture() {
8080
Ok(p) => println!("{:?}", p),
@@ -84,6 +84,10 @@ fn handle_pending_pictures(dec: &mut dav1d::Decoder) {
8484
panic!("Error getting decoded pictures: {}", e);
8585
}
8686
}
87+
88+
if !drain {
89+
break;
90+
}
8791
}
8892
}
8993

@@ -107,7 +111,7 @@ fn main() -> std::io::Result<()> {
107111
// pending pictures and send pending data to the decoder
108112
// until it is all used up.
109113
loop {
110-
handle_pending_pictures(&mut dec);
114+
handle_pending_pictures(&mut dec, false);
111115

112116
match dec.send_pending_data() {
113117
Err(e) if e.is_again() => continue,
@@ -125,8 +129,11 @@ fn main() -> std::io::Result<()> {
125129
}
126130

127131
// Handle all pending pictures before sending the next data.
128-
handle_pending_pictures(&mut dec);
132+
handle_pending_pictures(&mut dec, false);
129133
}
130134

135+
// Handle all pending pictures that were not output yet.
136+
handle_pending_pictures(&mut dec, true);
137+
131138
Ok(())
132139
}

0 commit comments

Comments
 (0)