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

Properly finalize EL writing when lengths differ #317

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
34 changes: 21 additions & 13 deletions src/dovi/muxer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ impl IoProcessor for Muxer {
}

fn finalize(&mut self, parser: &HevcParser) -> Result<()> {
let mut error = None;

let ordered_frames = parser.ordered_frames();
let total_frames = ordered_frames.len();

Expand All @@ -283,20 +285,21 @@ impl IoProcessor for Muxer {
// Write last BL frame
self.write_bl_frame()?;

if self.el_handler.buffered_frames.len() == 1 {
// Maybe incomplete last frame
self.el_processor
.parse_nalus(&mut self.el_reader, &mut self.el_handler)?;

// Write last EL frame
self.el_handler.write_next_frame()?;
} else if let Some(last_frame) = self.el_handler.buffered_frames.back() {
// Zero indexed
bail!(
"Mismatched BL/EL frame count. Expected {} frames, got {} (or more) frames in EL",
// Finalize EL, maybe incomplete last frame
self.el_processor
.parse_nalus(&mut self.el_reader, &mut self.el_handler)?;

// Write last EL frame
self.el_handler.write_next_frame()?;

// There should be no more frames if the BL/EL have the same number
if let Some(last_frame) = self.el_handler.buffered_frames.back() {
// Do not bail here to avoid incomplete processing
error = Some(format!(
"Mismatched BL/EL frame count. Expected {} frames, got {} (or more) frames in EL.\nThe EL will be trimmed to the BL length.",
total_frames,
last_frame.frame_number + 1
);
));
}

// Write remaining EOS/EOB
Expand All @@ -316,7 +319,12 @@ impl IoProcessor for Muxer {

self.progress_bar.finish_and_clear();

Ok(())
if let Some(err) = error {
// Should still error to have correct status code
bail!(err);
} else {
Ok(())
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/hevc/mux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,36 @@ fn edit_config() -> Result<()> {

Ok(())
}

#[test]
fn el_with_more_frames() -> Result<()> {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))?;
let temp = assert_fs::TempDir::new().unwrap();

let input_bl = Path::new("assets/hevc_tests/regular_bl_start_code_4_shorter.hevc");
let input_el = Path::new("assets/hevc_tests/regular.hevc");

let output_file = temp.child("BL_EL_RPU.hevc");
let expected_bl_el_rpu =
Path::new("assets/hevc_tests/regular_start_code_4_shorter_trimmed_el.hevc");

let assert = cmd
.arg(SUBCOMMAND)
.arg("--bl")
.arg(input_bl)
.arg("--el")
.arg(input_el)
.arg("--output")
.arg(output_file.as_ref())
.assert();

assert.failure().stderr(predicate::str::contains(
"Error: Mismatched BL/EL frame count. Expected 258 frames, got 259 (or more) frames in EL",
));

output_file
.assert(predicate::path::is_file())
.assert(predicate::path::eq_file(expected_bl_el_rpu));

Ok(())
}