diff --git a/src/lib_ccx/ccx_decoders_708_output.c b/src/lib_ccx/ccx_decoders_708_output.c index d80dbd311..65b4f25e8 100644 --- a/src/lib_ccx/ccx_decoders_708_output.c +++ b/src/lib_ccx/ccx_decoders_708_output.c @@ -520,15 +520,19 @@ void dtvcc_write_scc(dtvcc_writer_ctx *writer, dtvcc_service_decoder *decoder, s { dtvcc_tv_screen *tv = decoder->tv; + // The header must be the first line of the file: write it on the first + // screen printed for this service, before any early return. + if (tv->cc_count == 1) + { + dtvcc_write_scc_header(tv, encoder); + } + if (dtvcc_is_screen_empty(tv, encoder)) return; if (tv->time_ms_show + encoder->subs_delay < 0) return; - if (tv->cc_count == 2) - dtvcc_write_scc_header(tv, encoder); - char *buf = (char *)encoder->buffer; size_t buf_len; size_t remaining; diff --git a/src/rust/src/decoder/tv_screen.rs b/src/rust/src/decoder/tv_screen.rs index 989d1802f..f724aeb1b 100644 --- a/src/rust/src/decoder/tv_screen.rs +++ b/src/rust/src/decoder/tv_screen.rs @@ -474,6 +474,13 @@ impl dtvcc_tv_screen { } } } + // The header must be the first line of the file. cc_count is 1 on the + // first screen printed for this service, so write it then, before any + // early return for an empty screen. + if self.cc_count == 1 { + writer.write_to_file(b"Scenarist_SCC V1.0\n\n")?; + } + if self.is_screen_empty(writer) { return Ok(()); } @@ -482,10 +489,6 @@ impl dtvcc_tv_screen { return Ok(()); } - if self.cc_count == 2 { - writer.write_to_file(b"Scenarist_SCC V1.0\n\n")?; - } - // Shift by --delay, like the other formats let time_ms_show = self.time_ms_show + writer.subs_delay; let time_ms_hide = self.time_ms_hide + writer.subs_delay; @@ -860,12 +863,21 @@ mod test { #[cfg(unix)] fn test_write_scc_applies_subs_delay() { // current_fps is mocked to 30.0 in tests: 1500 ms minus one frame - // (33 ms) is 1467 ms => 00:00:01:14 + // (33 ms) is 1467 ms => 00:00:01:14. cc_count is 1, so the header + // comes first. let output = write_screen_with_delay(ccx_output_format::CCX_OF_SCC, 0); - assert!(output.starts_with("00:00:01:14\t"), "{}", output); + assert!( + output.starts_with("Scenarist_SCC V1.0\n\n00:00:01:14\t"), + "{}", + output + ); let output = write_screen_with_delay(ccx_output_format::CCX_OF_SCC, 5000); - assert!(output.starts_with("00:00:06:14\t"), "{}", output); + assert!( + output.starts_with("Scenarist_SCC V1.0\n\n00:00:06:14\t"), + "{}", + output + ); } #[test] @@ -916,6 +928,84 @@ mod test { ); } + /// Runs write_scc on `screen` into a temp file and returns what was written. + #[cfg(unix)] + fn write_scc_output(screen: &dtvcc_tv_screen) -> String { + use std::io::{Read, Seek, SeekFrom}; + use std::os::unix::io::AsRawFd; + use tempfile::tempfile; + + let mut writer_ctx = get_zero_allocated_obj::(); + let transcript_settings = get_zero_allocated_obj::(); + let mut temp_file = tempfile().expect("Failed to create temp file"); + writer_ctx.fd = temp_file.as_raw_fd(); + writer_ctx.filename = std::ptr::null_mut(); + + let mut counter = 0u32; + let mut writer = Writer::new( + &mut counter, + 0, + ccx_output_format::CCX_OF_SCC, + &mut writer_ctx, + 0, + &transcript_settings, + 0, + b"\r\n", + ); + screen + .write_scc(&mut writer) + .expect("write_scc should succeed"); + + temp_file.seek(SeekFrom::Start(0)).unwrap(); + let mut output = String::new(); + temp_file.read_to_string(&mut output).unwrap(); + output + } + + #[test] + #[cfg(unix)] + fn test_write_scc_header_before_first_caption() { + let mut screen = get_zero_allocated_obj::(); + screen.cc_count = 1; // first screen printed for this service + screen.time_ms_show = 500; + screen.time_ms_hide = 2000; + screen.chars[0][0] = dtvcc_symbol::new(0x41); + + let output = write_scc_output(&screen); + assert!( + output.starts_with("Scenarist_SCC V1.0\n\n00:00:00:14\t"), + "header must come before the first caption: {:?}", + output + ); + } + + #[test] + #[cfg(unix)] + fn test_write_scc_header_only_once() { + let mut screen = get_zero_allocated_obj::(); + screen.cc_count = 2; // second screen: header was already written + screen.time_ms_show = 500; + screen.time_ms_hide = 2000; + screen.chars[0][0] = dtvcc_symbol::new(0x41); + + let output = write_scc_output(&screen); + assert!( + !output.contains("Scenarist_SCC"), + "header must not be repeated: {:?}", + output + ); + } + + #[test] + #[cfg(unix)] + fn test_write_scc_header_when_first_screen_empty() { + let mut screen = get_zero_allocated_obj::(); + screen.cc_count = 1; + + let output = write_scc_output(&screen); + assert_eq!(output, "Scenarist_SCC V1.0\n\n"); + } + #[test] #[cfg(unix)] fn test_writer_output_with_valid_fd() {