diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6fd7a6022..c7239449a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1208,11 +1208,12 @@ jobs: echo "stage=oracle_qualification: invalid retained oracle pair" >&2 exit 1 fi - python3 -I tools/compat-corpus/copy_check.py --records "$COREAV_FFMPEG_OUT/records.json" \ + python3 -I tools/compat-corpus/flac_concat_ci.py --records "$COREAV_FFMPEG_OUT/records.json" \ --ffmpeg "$FFMPEG" --ffprobe "$FFPROBE" \ --mediainfo "$RUNNER_TEMP/coreav-oracle-bin/mediainfo" \ --report "$RUNNER_TEMP/coreav-ffmpeg.json" COREAV_CURRENT_CI_ORACLE=1 python3 -I -m unittest discover -s tools/compat-corpus/tests -p test_copy_check.py + COREAV_CURRENT_CI_ORACLE=1 python3 -I -m unittest discover -s tools/compat-corpus/tests -p test_flac_concat_ci.py -v - name: Preserve the writers' reports if: always() uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 diff --git a/.github/workflows/wasm.yml b/.github/workflows/wasm.yml index 8b43f39e6..5679bd99b 100644 --- a/.github/workflows/wasm.yml +++ b/.github/workflows/wasm.yml @@ -20,6 +20,7 @@ on: - Cargo.lock - rust-toolchain.toml - scripts/gen-fixtures.sh + - scripts/flac_concat_fixture.py - scripts/wasm-thumbnail-fixtures.py - scripts/install-x264.sh - scripts/chap_craft.py @@ -61,6 +62,7 @@ on: - Cargo.lock - rust-toolchain.toml - scripts/gen-fixtures.sh + - scripts/flac_concat_fixture.py - scripts/wasm-thumbnail-fixtures.py - scripts/install-x264.sh - scripts/chap_craft.py diff --git a/crates/coreav-audio/src/crc.rs b/crates/coreav-audio/src/crc.rs index df61b9c7e..80fefdf51 100644 --- a/crates/coreav-audio/src/crc.rs +++ b/crates/coreav-audio/src/crc.rs @@ -11,7 +11,11 @@ pub(crate) fn crc8(data: &[u8]) -> u8 { /// CRC-16 of `data`, eight bytes at a time ("slicing by eight"). pub(crate) fn crc16(data: &[u8]) -> u16 { - let mut crc: u16 = 0; + crc16_update(0, data) +} + +/// Extends the RFC 9639 frame CRC-16 with the next bytes, starting with zero. +pub fn crc16_update(mut crc: u16, data: &[u8]) -> u16 { let mut chunks = data.chunks_exact(8); for chunk in &mut chunks { let [b0, b1, b2, b3, b4, b5, b6, b7] = <[u8; 8]>::try_from(chunk).unwrap_or([0; 8]); diff --git a/crates/coreav-audio/src/flac.rs b/crates/coreav-audio/src/flac.rs index 5c57d5e8d..a0204988f 100644 --- a/crates/coreav-audio/src/flac.rs +++ b/crates/coreav-audio/src/flac.rs @@ -30,7 +30,10 @@ use crate::{Error, LimitKind, Limits, Pcm, Plane}; pub mod encode; mod md5; mod prepared; +mod rewrite; +pub use crate::crc::crc16_update; pub use prepared::{PreparedStream, prepare_stream}; +pub use rewrite::FrameHeader; /// The streaminfo metadata block (RFC 9639 8.2). #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/crates/coreav-audio/src/flac/rewrite.rs b/crates/coreav-audio/src/flac/rewrite.rs new file mode 100644 index 000000000..f4967f8c5 --- /dev/null +++ b/crates/coreav-audio/src/flac/rewrite.rs @@ -0,0 +1,142 @@ +use super::parse_header; +use crate::crc::crc8; + +/// A CRC-8-validated frame header, with at most 16 retained bytes. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct FrameHeader { + /// Samples per channel in this frame. + pub block_size: u32, + /// Frame number for fixed blocking, sample number for variable blocking. + pub number: u64, + /// Whether the coded number counts samples. + pub variable: bool, + bytes: [u8; 16], + len: usize, + number_end: usize, +} + +impl FrameHeader { + /// Parses only the bounded header at the start of a packet. + pub fn parse(data: &[u8]) -> Option { + let (header, len) = parse_header(data).ok()?; + let mut bytes = [0; 16]; + bytes.get_mut(..len)?.copy_from_slice(data.get(..len)?); + let first = *data.get(4)?; + let number_len = if first & 0x80 == 0 { + 1 + } else { + first.leading_ones() as usize + }; + Some(Self { + block_size: header.block_size, + number: header.number, + variable: header.variable, + bytes, + len, + number_end: 4 + number_len, + }) + } + + /// Header bytes including their CRC-8. + pub fn bytes(&self) -> &[u8] { + self.bytes.get(..self.len).map_or(&[], |bytes| bytes) + } + + /// Writes a variable-blocking header with a 36-bit sample number. + pub fn renumber(&self, number: u64, out: &mut [u8; 16]) -> Option { + if number >= 1 << 36 { + return None; + } + out.get_mut(..4)?.copy_from_slice(self.bytes.get(..4)?); + *out.get_mut(1)? |= 1; + let count = match number { + 0..=0x7f => 1, + 0x80..=0x7ff => 2, + 0x800..=0xffff => 3, + 0x10000..=0x1fffff => 4, + 0x200000..=0x3ffffff => 5, + 0x4000000..=0x7fffffff => 6, + _ => 7, + }; + if count == 1 { + *out.get_mut(4)? = number as u8; + } else { + *out.get_mut(4)? = (0xffu8 << (8 - count)) | (number >> (6 * (count - 1))) as u8; + for index in 1..count { + *out.get_mut(4 + index)? = + 0x80 | ((number >> (6 * (count - index - 1))) as u8 & 0x3f); + } + } + let tail = self.bytes.get(self.number_end..self.len.checked_sub(1)?)?; + let at = 4 + count; + out.get_mut(at..at + tail.len())?.copy_from_slice(tail); + let end = at + tail.len(); + *out.get_mut(end)? = crc8(out.get(..end)?); + Some(end + 1) + } + + /// Packet size after replacing only its header and footer. + pub fn rewritten_size(&self, size: u32, number: u64) -> Option { + let mut bytes = [0; 16]; + let len = self.renumber(number, &mut bytes)?; + let body = size.checked_sub(u32::try_from(self.len + 2).ok()?)?; + body.checked_add(u32::try_from(len + 2).ok()?) + } +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::indexing_slicing)] +mod tests { + use super::*; + + #[test] + fn coded_number_boundaries_preserve_uncommon_fields() { + for size in [6, 7] { + for rate in [12, 13, 14] { + let mut bytes = vec![0xff, 0xf8, (size << 4) | rate, 0x18, 0]; + bytes.extend_from_slice(if size == 6 { &[31] } else { &[1, 255] }); + bytes.extend_from_slice(if rate == 12 { &[48] } else { &[0xbb, 0x80] }); + bytes.push(crc8(&bytes)); + let head = FrameHeader::parse(&bytes).unwrap(); + for number in [ + 0, + 127, + 128, + 2047, + 2048, + 65535, + 65536, + (1 << 21) - 1, + 1 << 21, + (1 << 26) - 1, + 1 << 26, + (1 << 31) - 1, + 1 << 31, + (1 << 36) - 1, + ] { + let mut out = [0; 16]; + let len = head.renumber(number, &mut out).unwrap(); + let rewritten = FrameHeader::parse(&out[..len]).unwrap(); + assert!(rewritten.variable); + assert_eq!(rewritten.number, number); + assert_eq!(rewritten.block_size, head.block_size); + assert_eq!( + &out[rewritten.number_end..len - 1], + &bytes[head.number_end..bytes.len() - 1] + ); + assert_eq!( + head.rewritten_size(100, number), + Some(100 + len as u32 - bytes.len() as u32) + ); + for end in 0..len { + assert!(FrameHeader::parse(&out[..end]).is_none()); + } + out[len - 1] ^= 1; + assert!(FrameHeader::parse(&out[..len]).is_none()); + } + assert!(head.renumber(1 << 36, &mut [0; 16]).is_none()); + assert!(head.rewritten_size(bytes.len() as u32 + 1, 0).is_none()); + } + } + } +} diff --git a/crates/coreav-cli/tests/ffmpeg_concat.rs b/crates/coreav-cli/tests/ffmpeg_concat.rs index c4d87c02a..a7a4b2782 100644 --- a/crates/coreav-cli/tests/ffmpeg_concat.rs +++ b/crates/coreav-cli/tests/ffmpeg_concat.rs @@ -739,6 +739,43 @@ fn lossless_audiovisual_concat_preserves_complete_native_output() { ); std::fs::remove_file(&output).unwrap(); } + if codec == "flac" { + std::fs::write(&list, "file 'flac.mp4'\n").unwrap(); + let copied = ffmpeg(&[args.as_slice(), &[output.to_str().unwrap()]].concat()); + assert_eq!(copied.code, 0, "{}", copied.stderr); + let packets = |path: &Path| { + let (document, stderr) = concat_output::probe(&probe, path); + assert!(stderr.is_empty()); + let audio = document["streams"] + .as_array() + .unwrap() + .iter() + .find(|stream| stream["codec_type"] == "audio") + .unwrap()["index"] + .clone(); + document["packets"] + .as_array() + .unwrap() + .iter() + .filter(|packet| packet["stream_index"] == audio) + .map(|packet| { + ( + packet["size"].clone(), + packet["data"].clone(), + packet["data_hash"].clone(), + ) + }) + .collect::>() + }; + let expected = packets(&source); + assert!(!expected.is_empty()); + assert_eq!( + packets(&output), + expected, + "every original audio packet byte" + ); + std::fs::remove_file(&output).unwrap(); + } let hostile = dir.join("inband.txt"); std::fs::write(&hostile, "file 'reference.mp4'\nfile 'reference.mp4'\n").unwrap(); let hostile_args = [ diff --git a/crates/coreav-cli/tests/support/concat_output.rs b/crates/coreav-cli/tests/support/concat_output.rs index 29270e6cd..5dece252f 100644 --- a/crates/coreav-cli/tests/support/concat_output.rs +++ b/crates/coreav-cli/tests/support/concat_output.rs @@ -167,7 +167,7 @@ fn normalized(stderr: &[u8]) -> String { result } -fn probe(program: &Path, path: &Path) -> (Value, String) { +pub fn probe(program: &Path, path: &Path) -> (Value, String) { let output = Command::new(program) .args(["-v", "error"]) .args(include_str!("../../../../scripts/oracle-audio-args.txt").split_whitespace()) @@ -283,16 +283,18 @@ fn packet_record(native: &Value, oracle: &Value, pos: usize, payload: &[u8], vid fn streams(actual: &Value, expected: &Value, tracks: &[Track], sizes: &[(usize, usize)]) { let mut adjusted = expected.clone(); for (index, track) in tracks.iter().enumerate() { - if track.avcc.is_some() { - assert_eq!(actual[index]["codec_tag_string"], "avc1"); - assert_eq!(actual[index]["codec_tag"], "0x31637661"); + if track.avcc.is_some() || expected[index]["codec_name"] == "flac" { + if track.avcc.is_some() { + assert_eq!(actual[index]["codec_tag_string"], "avc1"); + assert_eq!(actual[index]["codec_tag"], "0x31637661"); + } let native = bitrate(sizes[index].0, track.media_clock); let oracle = bitrate(sizes[index].1, track.media_clock); assert_eq!(actual[index]["bit_rate"], native); assert_eq!(expected[index]["bit_rate"], oracle); assert_ne!( native, oracle, - "the measured SPS/PPS payload difference remains visible" + "the measured frame-header payload difference remains visible" ); eprintln!("stream {index} bit_rate native={native} oracle={oracle}"); adjusted[index]["bit_rate"] = json!(native); @@ -341,6 +343,8 @@ fn streams(actual: &Value, expected: &Value, tracks: &[Track], sizes: &[(usize, assert_ne!(&oracle[18..], &[0; 16]); let mut combined = oracle.clone(); combined[..2].copy_from_slice(&768u16.to_be_bytes()); + combined[4..7].copy_from_slice(&272u32.to_be_bytes()[1..]); + combined[7..10].copy_from_slice(&386u32.to_be_bytes()[1..]); let packed = (integer(&oracle[10..18]) & !((1 << 36) - 1)) | 192_000; combined[10..18].copy_from_slice(&packed.to_be_bytes()); combined[18..].fill(0); @@ -578,6 +582,8 @@ pub fn compare( let mut sample_indices = vec![0; native_tracks.len()]; let mut observed_idrs = Vec::new(); let mut removed_before = 0; + let mut flac_growth = 0; + let mut flac_samples = 0; let mut native_at = native_mdat.start; let mut oracle_at = oracle_mdat.start; let mut positions = Vec::new(); @@ -592,7 +598,7 @@ pub fn compare( assert_eq!(oracle_pos, oracle_at, "oracle samples tile mdat exactly"); assert_eq!( native_pos - native_mdat.start, - oracle_pos - oracle_mdat.start - removed_before + oracle_pos - oracle_mdat.start - removed_before + flac_growth ); positions.push((stream, sample_indices[stream], native_pos, oracle_pos)); if let Some(avcc) = &native_tracks[stream].avcc { @@ -609,37 +615,40 @@ pub fn compare( observed_idrs.push(sample_indices[stream]); } removed_before += oracle_payload.len() - native_payload.len(); + } else if case == "audio-copy-flac" { + use coreav_audio::flac::{FrameHeader, crc16_update}; + let frame = sample_indices[stream]; + let native = FrameHeader::parse(native_payload).unwrap(); + let oracle = FrameHeader::parse(oracle_payload).unwrap(); + assert!(native.variable); + assert!(!oracle.variable); + assert_eq!(native.number, flac_samples); + assert_eq!(oracle.number as usize, frame % 94); + assert_eq!(native.block_size, oracle.block_size); + assert_eq!(crc16_update(0, native_payload), 0); + assert_eq!(crc16_update(0, oracle_payload), 0); + assert_eq!( + &native_payload[native.bytes().len()..native_payload.len() - 2], + &oracle_payload[oracle.bytes().len()..oracle_payload.len() - 2], + "all FLAC subframe bytes" + ); + let mut header = [0; 16]; + let len = oracle.renumber(flac_samples, &mut header).unwrap(); + assert_eq!(native.bytes(), &header[..len]); + let block = native.block_size as usize; + assert_eq!(block, if frame % 94 == 93 { 768 } else { 1024 }); + flac_samples += u64::from(native.block_size); + flac_growth += native_payload.len() - oracle_payload.len(); + flac_frames.push((block, native_payload.len())); } else { assert_eq!(native_payload, oracle_payload, "every audio packet byte"); - if case == "audio-copy-flac" { - let frame = sample_indices[stream]; - assert_eq!(native_payload[0], 0xff); - assert_eq!(native_payload[1] & 0xfe, 0xf8); - assert_eq!( - native_payload[1] & 1, - 0, - "known MP4 packet-copy deviation shared with the oracle: FLAC retains fixed-block strategy, not standalone-stream conformance" - ); - assert_eq!( - usize::from(native_payload[4]), - frame % 94, - "known MP4 packet-copy deviation shared with the oracle: the frame number resets at input two" - ); - let block = match native_payload[2] >> 4 { - 10 => 1024, - 7 => integer(&native_payload[5..7]) as usize + 1, - code => panic!("unexpected generated FLAC block-size code {code}"), - }; - assert_eq!(block, if frame % 94 == 93 { 768 } else { 1024 }); - flac_frames.push((block, native_payload.len())); - } } packet_record( native, oracle, native_pos, native_payload, - native_tracks[stream].avcc.is_some(), + native_tracks[stream].avcc.is_some() || case == "audio-copy-flac", ); sample_indices[stream] += 1; sizes[stream].0 += native_payload.len(); @@ -662,8 +671,9 @@ pub fn compare( flac_frames.iter().map(|(block, _)| *block).max(), Some(1024) ); - assert_eq!(flac_frames.iter().map(|(_, bytes)| *bytes).min(), Some(269)); - assert_eq!(flac_frames.iter().map(|(_, bytes)| *bytes).max(), Some(383)); + assert_eq!(flac_frames.iter().map(|(_, bytes)| *bytes).min(), Some(272)); + assert_eq!(flac_frames.iter().map(|(_, bytes)| *bytes).max(), Some(386)); + assert_eq!(flac_samples, 192_000); } assert_ne!( native_mdat.start, oracle_mdat.start, diff --git a/crates/coreav-isobmff-write/src/join.rs b/crates/coreav-isobmff-write/src/join.rs index 65ddd2ed3..7bcaab86b 100644 --- a/crates/coreav-isobmff-write/src/join.rs +++ b/crates/coreav-isobmff-write/src/join.rs @@ -12,6 +12,7 @@ //! of a split join back into one edit per track. mod avc; +mod flac; use crate::audio; use crate::boxes::Node; @@ -151,6 +152,13 @@ pub fn join_mp4( // Listing 1: each input's samples and presentation. let mut spans = Vec::new(); let mut budgets = Vec::new(); + let mut flac_survey = (compatibility.is_some() + && picks.len() > 1 + && first + .audio + .as_ref() + .is_some_and(|audio| audio.track.description.codec == Codec::Flac)) + .then(flac::Survey::new); for (input, (movie, pick)) in movies.iter().zip(&picks).enumerate() { let source = sources .get(input) @@ -176,8 +184,15 @@ pub fn join_mp4( } let audio = match pick.audio.as_ref() { Some(a) => Some( - audio_span(movie, a.track, &mut Shared(source), limits, &mut budget) - .map_err(|e| at(input, e))?, + audio_span( + movie, + a.track, + source, + limits, + &mut budget, + &mut flac_survey, + ) + .map_err(|e| at(input, e))?, ), None => None, }; @@ -198,6 +213,9 @@ pub fn join_mp4( Shared(source).release(); } + let flac_plan = flac_survey.and_then(flac::Survey::finish); + let mut flac_samples = 0; + // Where each input goes. let tm = ta.map_or(tv, |ta| movie_timescale(tv, ta)); let layout = place(&spans, tv, ta, &options.placement).map_err(JoinError::Output)?; @@ -227,9 +245,10 @@ pub fn join_mp4( &span.video.listed, placed.extended, true, - &mut Shared(source), + source, limits, budget, + None, ) .map_err(|e| at_extract(input, e))?; if let (Some(tables), Some(a), Some(audio)) = @@ -242,15 +261,22 @@ pub fn join_mp4( &audio.listed, 0, false, - &mut Shared(source), + source, limits, budget, + flac_plan.as_ref().map(|_| &mut flac_samples), ) .map_err(|e| at_extract(input, e))?; } Shared(source).release(); } video_tables.finish(); + if flac_plan + .as_ref() + .is_some_and(|plan| plan.samples != flac_samples) + { + return Err(at_extract(0, ExtractError::SourceChanged)); + } if let Some(tables) = audio_tables.as_mut() { tables.finish(); } @@ -355,8 +381,14 @@ pub fn join_mp4( media_duration: media, duration, edits: audio_edits.into(), - stsd: joined_audio_entry(&picks, &spans, tables, compatibility.is_some()) - .map_err(JoinError::Output)?, + stsd: joined_audio_entry( + &picks, + &spans, + tables, + compatibility.is_some(), + flac_plan.as_ref(), + ) + .map_err(JoinError::Output)?, tables, extra: Vec::new(), stbl_extra: Vec::new(), @@ -476,6 +508,7 @@ pub fn join_mp4( let mut window = Window::new(COPY_BUFFER); let mut avc_work = vec![0u64; sources.len()]; let mut current = None; + let mut flac_samples = 0u64; interleave( layout_tables, Ties::TrackOrder, @@ -487,7 +520,8 @@ pub fn join_mp4( .ok_or_else(|| JoinError::Output(overflow()))?, }; for _ in 0..chunk.samples { - let (input, offset, size) = run.next()?; + let rewritten = track != 0 && flac_plan.is_some(); + let (input, offset, size) = run.next(rewritten)?; if current != Some(input) { window.clear(); if let Some(previous) = current.and_then(|p| sources.get(p)) { @@ -498,23 +532,51 @@ pub fn join_mp4( let source = sources .get(input) .ok_or_else(|| JoinError::Output(overflow()))?; - let copied = if let (0, Some(width)) = (track, avc_width) { - let work = avc_work - .get_mut(input) - .ok_or_else(|| JoinError::Output(overflow()))?; - let mut sample = - avc::Sample::new(&mut out, width, size, work, limits.max_sample_work); - let copied = window.copy(&mut Shared(source), offset, size, &mut sample); - if let Some(error) = sample.error { - return Err(at(input, error)); - } - if copied.is_ok() { - sample.finish().map_err(|error| at(input, error))?; - } - copied - } else { - window.copy(&mut Shared(source), offset, size, &mut out) - }; + let copied = + if rewritten { + let mut bytes = [0; 16]; + let prefix = bytes + .get_mut(..(size as usize).min(16)) + .ok_or_else(|| JoinError::Output(overflow()))?; + Shared(source) + .read_exact_at(offset, prefix) + .map_err(|error| at(input, error))?; + let head = coreav_audio::flac::FrameHeader::parse(prefix) + .ok_or_else(|| at_extract(input, ExtractError::SourceChanged))?; + if head.rewritten_size(size, flac_samples) + != run.tables.size(run.copied.saturating_sub(1)) + { + return Err(at_extract(input, ExtractError::SourceChanged)); + } + let mut frame = flac::Frame::new(&mut out, head, flac_samples, size) + .map_err(|error| match error { + ExtractError::Write(kind) => JoinError::Write(kind), + error => at_extract(input, error), + })?; + let copied = window + .copy(&mut Shared(source), offset, size, &mut frame) + .and_then(|()| frame.finish()); + flac_samples = flac_samples + .checked_add(u64::from(head.block_size)) + .ok_or_else(|| JoinError::Output(overflow()))?; + copied + } else if let (0, Some(width)) = (track, avc_width) { + let work = avc_work + .get_mut(input) + .ok_or_else(|| JoinError::Output(overflow()))?; + let mut sample = + avc::Sample::new(&mut out, width, size, work, limits.max_sample_work); + let copied = window.copy(&mut Shared(source), offset, size, &mut sample); + if let Some(error) = sample.error { + return Err(at(input, error)); + } + if copied.is_ok() { + sample.finish().map_err(|error| at(input, error))?; + } + copied + } else { + window.copy(&mut Shared(source), offset, size, &mut out) + }; copied.map_err(|e| match e { // The output failed, not the input. ExtractError::Write(kind) => JoinError::Write(kind), @@ -525,6 +587,12 @@ pub fn join_mp4( }, )?; video_run.finish()?; + if flac_plan + .as_ref() + .is_some_and(|plan| plan.samples != flac_samples) + { + return Err(at_extract(0, ExtractError::SourceChanged)); + } if let Some(run) = audio_run.as_mut() { run.finish()?; } @@ -721,6 +789,7 @@ fn joined_audio_entry( spans: &[Spans<'_>], tables: &Tables, compatibility: bool, + flac: Option<&flac::Plan>, ) -> Result, Error> { let first = picks .first() @@ -840,6 +909,34 @@ fn joined_audio_entry( field.copy_from_slice(total.to_be_bytes().get(3..).ok_or_else(overflow)?); *field.first_mut().ok_or_else(overflow)? |= high; config.get_mut(26..42).ok_or_else(overflow)?.fill(0); + if let Some(plan) = flac { + config + .get_mut(8..10) + .ok_or_else(overflow)? + .copy_from_slice(&plan.minimum.to_be_bytes()); + config + .get_mut(10..12) + .ok_or_else(overflow)? + .copy_from_slice(&plan.maximum.to_be_bytes()); + let mut minimum = u32::MAX; + let mut maximum = 0; + for sample in 0..tables.samples() { + let size = tables.size(sample).ok_or_else(overflow)?; + minimum = minimum.min(size); + maximum = maximum.max(size); + } + for (range, value) in [(12..15, minimum), (15..18, maximum)] { + let value = if value < 1 << 24 { value } else { 0 }; + config + .get_mut(range) + .ok_or_else(overflow)? + .copy_from_slice(value.to_be_bytes().get(1..).ok_or_else(overflow)?); + } + let field = config.get_mut(21..26).ok_or_else(overflow)?; + let high = field.first().copied().ok_or_else(overflow)? & 0xf0; + field.copy_from_slice(plan.samples.to_be_bytes().get(3..).ok_or_else(overflow)?); + *field.first_mut().ok_or_else(overflow)? |= high; + } } Codec::Alac => { let mut maximum = 0u32; @@ -1317,11 +1414,12 @@ fn video_span<'a, S: Source>( fn audio_span<'a, S: Source>( movie: &'a Movie, track: &'a Track, - source: &mut S, + source: &RefCell, limits: &Limits, budget: &mut Budget, + flac_survey: &mut Option, ) -> Result, Error> { - let gapless = gapless(movie, track, &mut *source)?; + let gapless = gapless(movie, track, &mut Shared(source))?; let timeline = Timeline::with_several(track, movie, gapless)?; let scaled = Scaled::of(&timeline)?; let mut listed = Listed { @@ -1332,8 +1430,23 @@ fn audio_span<'a, S: Source>( }; let mut presented: Option<(i64, i64)> = None; let mut next_dts: Option = None; - for packet in movie.packets(track.index, &mut *source, limits, budget)? { + for packet in movie.packets(track.index, Shared(source), limits, budget)? { let packet = packet?; + if let Some(survey) = flac_survey.as_mut() { + let valid = flac::header(source, &packet)?.is_some_and(|head| { + track + .description + .configured_sample_rate + .is_some_and(|rate| { + u64::from(head.block_size) * u64::from(track.timescale) + == u64::from(packet.duration) * u64::from(rate) + }) + && survey.push(head).is_some() + }); + if !valid { + *flac_survey = None; + } + } listed.last_duration = packet.duration; if packet.cts != packet.dts { return Err(unsupported("composition offsets in an audio track")); @@ -1715,13 +1828,14 @@ fn append( listed: &Listed, extended: u64, video: bool, - source: &mut S, + source: &RefCell, limits: &Limits, budget: &mut Budget, + mut flac_samples: Option<&mut u64>, ) -> Result<(), ExtractError> { let mut count = 0u64; let mut duration = 0u64; - for packet in movie.packets(track.index, &mut *source, limits, budget)? { + for packet in movie.packets(track.index, Shared(source), limits, budget)? { let packet = packet?; count = count.saturating_add(1); duration = duration.saturating_add(u64::from(packet.duration)); @@ -1735,7 +1849,19 @@ fn append( } else { 0 }; - tables.push_sample(packet.size, delta, packet.keyframe, offset)?; + let size = if let Some(samples) = flac_samples.as_mut() { + let head = flac::header(source, &packet)?.ok_or(ExtractError::SourceChanged)?; + let size = head + .rewritten_size(packet.size, **samples) + .ok_or(ExtractError::SourceChanged)?; + **samples = samples + .checked_add(u64::from(head.block_size)) + .ok_or_else(overflow)?; + size + } else { + packet.size + }; + tables.push_sample(size, delta, packet.keyframe, offset)?; } if count != listed.samples || duration != listed.duration { return Err(ExtractError::SourceChanged); @@ -1784,7 +1910,7 @@ impl<'a, S: Source, B: Iterator> Chain<'a, S, B> { } /// The input, offset and size of the next sample. - fn next(&mut self) -> Result<(usize, u64, u32), JoinError> { + fn next(&mut self, rewritten: bool) -> Result<(usize, u64, u32), JoinError> { loop { if let Some((input, packets, done)) = self.current.as_mut() { let count = self.counts.get(*input).copied().unwrap_or(0); @@ -1798,7 +1924,7 @@ impl<'a, S: Source, B: Iterator> Chain<'a, S, B> { })? .map_err(|e| at(input, e))?; *done = done.saturating_add(1); - if self.tables.size(self.copied) != Some(packet.size) { + if !rewritten && self.tables.size(self.copied) != Some(packet.size) { return Err(JoinError::Input { input, error: ExtractError::SourceChanged, diff --git a/crates/coreav-isobmff-write/src/join/flac.rs b/crates/coreav-isobmff-write/src/join/flac.rs new file mode 100644 index 000000000..bad20463f --- /dev/null +++ b/crates/coreav-isobmff-write/src/join/flac.rs @@ -0,0 +1,368 @@ +use super::{Shared, overflow}; +use crate::ExtractError; +use coreav_audio::flac::{FrameHeader, crc16_update}; +use coreav_isobmff::{Error, Packet, Source}; +use std::cell::RefCell; +use std::io::{self, Write}; + +pub(super) struct Plan { + pub(super) minimum: u16, + pub(super) maximum: u16, + pub(super) samples: u64, +} + +pub(super) struct Survey { + samples: u64, + frames: u64, + previous: Option, + nominal: Option, + minimum: u32, + maximum: u32, + rewrite: bool, +} + +impl Survey { + pub(super) fn new() -> Self { + Self { + samples: 0, + frames: 0, + previous: None, + nominal: None, + minimum: u32::MAX, + maximum: 0, + rewrite: false, + } + } + + pub(super) fn push(&mut self, head: FrameHeader) -> Option<()> { + if let Some(previous) = self.previous { + self.minimum = self.minimum.min(previous.block_size); + self.rewrite |= previous.variable != head.variable + || (!previous.variable && self.nominal != Some(previous.block_size)); + } + self.rewrite |= head.number + != if head.variable { + self.samples + } else { + self.frames + }; + self.nominal.get_or_insert(head.block_size); + self.maximum = self.maximum.max(head.block_size); + self.samples = self.samples.checked_add(u64::from(head.block_size))?; + self.frames = self.frames.checked_add(1)?; + self.previous = Some(head); + Some(()) + } + + pub(super) fn finish(mut self) -> Option { + self.rewrite |= self.previous.is_some_and(|head| { + !head.variable && self.nominal.is_some_and(|block| head.block_size > block) + }); + if !self.rewrite || self.minimum < 16 || self.samples >= 1 << 36 || self.maximum == 0 { + return None; + } + if self.frames == 1 { + self.minimum = 16; + self.maximum = self.maximum.max(16); + } + if self.minimum == self.maximum { + self.minimum = 16; + self.maximum = self.maximum.max(17); + } + Some(Plan { + minimum: u16::try_from(self.minimum).ok()?, + maximum: u16::try_from(self.maximum).ok()?, + samples: self.samples, + }) + } +} + +pub(super) fn header( + source: &RefCell, + packet: &Packet, +) -> Result, Error> { + let mut bytes = [0; 16]; + let bytes = bytes + .get_mut(..(packet.size as usize).min(16)) + .ok_or_else(overflow)?; + Shared(source).read_exact_at(packet.offset, bytes)?; + Ok(FrameHeader::parse(bytes).filter(|head| head.bytes().len() + 2 <= packet.size as usize)) +} + +pub(super) struct Frame<'a, W> { + out: &'a mut W, + header: FrameHeader, + skipped: usize, + body: usize, + footer: [u8; 2], + footer_len: usize, + original_crc: u16, + rewritten_crc: u16, +} + +impl<'a, W: Write> Frame<'a, W> { + pub(super) fn new( + out: &'a mut W, + header: FrameHeader, + samples: u64, + size: u32, + ) -> Result { + let mut bytes = [0; 16]; + let len = header + .renumber(samples, &mut bytes) + .ok_or(ExtractError::SourceChanged)?; + let bytes = bytes.get(..len).ok_or(ExtractError::SourceChanged)?; + let body = (size as usize) + .checked_sub(header.bytes().len() + 2) + .ok_or(ExtractError::SourceChanged)?; + out.write_all(bytes)?; + Ok(Self { + out, + header, + skipped: 0, + body, + footer: [0; 2], + footer_len: 0, + original_crc: 0, + rewritten_crc: crc16_update(0, bytes), + }) + } + + pub(super) fn finish(self) -> Result<(), ExtractError> { + if self.skipped != self.header.bytes().len() || self.body != 0 || self.footer_len != 2 { + return Err(ExtractError::SourceChanged); + } + let stored = u16::from_be_bytes(self.footer); + self.out + .write_all(&(stored ^ self.original_crc ^ self.rewritten_crc).to_be_bytes())?; + Ok(()) + } +} + +impl Write for Frame<'_, W> { + fn write(&mut self, mut bytes: &[u8]) -> io::Result { + let len = bytes.len(); + let invalid = || io::Error::from(io::ErrorKind::InvalidData); + let skip = bytes + .len() + .min(self.header.bytes().len().saturating_sub(self.skipped)); + let (prefix, rest) = bytes.split_at_checked(skip).ok_or_else(invalid)?; + if self.header.bytes().get(self.skipped..self.skipped + skip) != Some(prefix) { + return Err(invalid()); + } + self.original_crc = crc16_update(self.original_crc, prefix); + self.skipped += skip; + bytes = rest; + let take = bytes.len().min(self.body); + let (body, rest) = bytes.split_at_checked(take).ok_or_else(invalid)?; + self.original_crc = crc16_update(self.original_crc, body); + self.rewritten_crc = crc16_update(self.rewritten_crc, body); + self.out.write_all(body)?; + self.body -= take; + let end = self + .footer_len + .checked_add(rest.len()) + .ok_or_else(invalid)?; + self.footer + .get_mut(self.footer_len..end) + .ok_or_else(invalid)? + .copy_from_slice(rest); + self.footer_len = end; + Ok(len) + } + + fn flush(&mut self) -> io::Result<()> { + self.out.flush() + } +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::indexing_slicing, clippy::panic)] +mod tests { + use super::*; + use coreav_audio::flac::{FlacDecoder, StreamInfo, encode::FlacSink}; + use coreav_audio::{Limits as AudioLimits, OutputFormat, Pcm, Plane, SampleFormat, Sink}; + + fn encoded(count: usize, seed: u32, format: SampleFormat) -> (StreamInfo, Vec, Vec) { + let values: Vec = (0..count * 2) + .map(|index| { + let value = (index as u32).wrapping_mul(7919).wrapping_add(seed * 131) as i32; + if format == SampleFormat::S16 { + (value as i16) as i32 + } else { + (value << 8) >> 8 + } + }) + .collect(); + let pcm: Vec = values + .iter() + .flat_map(|value| value.to_le_bytes()[..format.bytes()].to_vec()) + .collect(); + let mut bytes = Vec::new(); + let mut sink = FlacSink::new(&mut bytes, AudioLimits::default()); + sink.start(&OutputFormat { + channels: 2, + sample_rate: 48000, + format, + channel_mask: 3, + }) + .unwrap(); + sink.write(&pcm).unwrap(); + let header = sink.finish().unwrap(); + let info = StreamInfo::parse(&header[8..42]).unwrap(); + (info, bytes[header.len()..].to_vec(), values) + } + + fn interleaved(pcm: &Pcm) -> Vec { + let planes: Vec<&[i32]> = (0..pcm.channels()) + .map(|channel| match pcm.plane(channel).unwrap() { + Plane::Int { samples, .. } => samples, + _ => panic!("integer PCM expected"), + }) + .collect(); + (0..pcm.frames()) + .flat_map(|index| planes.iter().map(move |plane| plane[index])) + .collect() + } + + #[test] + fn rewritten_concatenations_decode_to_every_original_sample() { + for format in [SampleFormat::S16, SampleFormat::S24] { + for count in [16, 17, 127, 128, 256, 4096, 4112, 8191, 8192] { + let mut samples = 0; + let mut expected = Vec::new(); + let mut actual = Vec::new(); + for seed in 0..3 { + let (info, bytes, values) = encoded(count, seed, format); + expected.extend(values); + let mut original = FlacDecoder::new(info, &AudioLimits::default()).unwrap(); + let mut decoder = FlacDecoder::new(info, &AudioLimits::default()).unwrap(); + let mut pcm = Pcm::new(); + let mut rest = bytes.as_slice(); + while !rest.is_empty() { + let frame = original.decode(rest, &mut pcm).unwrap(); + assert!(frame.crc_ok); + let packet = &rest[..frame.bytes]; + let head = FrameHeader::parse(packet).unwrap(); + let mut out = Vec::new(); + let mut writer = + Frame::new(&mut out, head, samples, packet.len() as u32).unwrap(); + for chunk in packet.chunks((seed as usize + 1) * 7) { + writer.write_all(chunk).unwrap(); + } + writer.finish().unwrap(); + let rewritten = decoder.decode(&out, &mut pcm).unwrap(); + assert!(rewritten.variable && rewritten.crc_ok); + assert_eq!(rewritten.bytes, out.len()); + assert_eq!(rewritten.number, samples); + assert_eq!( + out.len() as u32, + head.rewritten_size(packet.len() as u32, samples).unwrap() + ); + actual.extend(interleaved(&pcm)); + samples += u64::from(frame.block_size); + rest = &rest[frame.bytes..]; + } + } + assert_eq!(actual, expected, "{count} samples per input"); + } + } + } + + #[test] + fn every_write_split_preserves_crc_error_syndromes() { + let (info, mut packet, _) = encoded(32, 1, SampleFormat::S16); + for corrupt in [false, true] { + if corrupt { + let len = packet.len(); + packet[len - 1] ^= 1; + } + let head = FrameHeader::parse(&packet).unwrap(); + for split in 0..=packet.len() { + let mut out = Vec::new(); + let mut writer = Frame::new(&mut out, head, 1 << 31, packet.len() as u32).unwrap(); + writer.write_all(&packet[..split]).unwrap(); + writer.write_all(&packet[split..]).unwrap(); + writer.finish().unwrap(); + let mut decoder = FlacDecoder::new(info, &AudioLimits::default()).unwrap(); + assert_eq!( + decoder.decode(&out, &mut Pcm::new()).unwrap().crc_ok, + !corrupt + ); + assert_eq!(crc16_update(0, &packet), crc16_update(0, &out)); + } + } + } + + #[test] + fn changed_headers_and_incomplete_packets_fail_closed() { + let (_, packet, _) = encoded(32, 0, SampleFormat::S16); + let head = FrameHeader::parse(&packet).unwrap(); + let mut out = Vec::new(); + let mut writer = Frame::new(&mut out, head, 0, packet.len() as u32).unwrap(); + assert!(writer.write_all(&[0]).is_err()); + let mut writer = Frame::new(&mut out, head, 0, packet.len() as u32).unwrap(); + writer.write_all(&packet[..packet.len() - 1]).unwrap(); + assert!(writer.finish().is_err()); + } + + #[test] + fn continuous_streams_keep_headers_and_discontinuities_rewrite_all_frames() { + let (_, packet, _) = encoded(32, 0, SampleFormat::S16); + let base = FrameHeader::parse(&packet).unwrap(); + for variable in [false, true] { + for blocks in [[4096, 4096, 768], [16, 16, 16]] { + let mut survey = Survey::new(); + let mut samples = 0; + for (frame, block) in blocks.into_iter().enumerate() { + let mut head = base; + head.variable = variable; + head.block_size = block; + head.number = if variable { samples } else { frame as u64 }; + samples += u64::from(block); + survey.push(head).unwrap(); + } + assert!(survey.finish().is_none()); + } + } + for blocks in [[4096, 768, 4096, 768], [16, 16, 16, 16]] { + let mut survey = Survey::new(); + for (frame, block) in blocks.into_iter().enumerate() { + let mut head = base; + head.block_size = block; + head.number = (frame % 2) as u64; + survey.push(head).unwrap(); + } + let plan = survey.finish().unwrap(); + assert!(plan.minimum < plan.maximum); + assert!(plan.minimum >= 16); + assert_eq!(plan.samples, blocks.into_iter().map(u64::from).sum::()); + } + let mut survey = Survey::new(); + for (frame, block) in [4096, 768, 4096].into_iter().enumerate() { + let mut head = base; + head.number = frame as u64; + head.block_size = block; + survey.push(head).unwrap(); + } + assert_eq!(survey.finish().unwrap().minimum, 768); + } + + #[test] + fn a_single_repaired_frame_has_legal_representable_bounds() { + let (_, packet, _) = encoded(32, 0, SampleFormat::S16); + let base = FrameHeader::parse(&packet).unwrap(); + assert!(Survey::new().finish().is_none()); + for block in [1, 15, 16, 128, 65535] { + let mut head = base; + head.block_size = block; + head.number = 1; + let mut survey = Survey::new(); + survey.push(head).unwrap(); + let plan = survey.finish().unwrap(); + assert!(plan.minimum >= 16 && plan.minimum < plan.maximum); + assert!(u32::from(plan.maximum) >= block); + assert_eq!(plan.samples, u64::from(block)); + } + } +} diff --git a/docs/agents/compat-corpus.md b/docs/agents/compat-corpus.md index 67715c20f..08b8dee73 100644 --- a/docs/agents/compat-corpus.md +++ b/docs/agents/compat-corpus.md @@ -7,6 +7,7 @@ Corpus exclusions, compatibility and codec-coverage workflows with their candida | A paired top-200 PNS acquisition distinguishes fixture encoding failures from diagnostic failures before any judged command starts | Separate generation and scan handlers emit closed failure labels in `github_shapes.run`; `test_all_generation_faults_stop_before_any_shape` injects both failures and proves neither can reach acquisition. An external encoder exit cannot be established by a type | | The PNS registration describes the workflow's paired-only dispatch separately from the CLI's per-registration selection | The workflow's admission guard owns paired-only dispatch; `test_invalid_pair_selection_stops_before_checkout_or_media` executes its rejection and `test_paired_run_stops_on_pns_free_failure_and_keeps_results_private` checks the matching design statement. External workflow selection and English prose cannot share a declared type | | Both exact and avc1-compatible top-200 copy comparisons preserve every packet's keyframe flag | `packet_rows` and `avc1_carriage` include the `K` flag in their compared rows. The keyframe-only regressions in `test_github_shapes.py` use complete packet records and assert the exact-path documents differ only in the selected flag. They reject both mismatch directions for first and later video and audio packets, while other flag bits stay outside this predicate. External probe records require runtime comparison, not a declared stream type | +| Current writer CI's FLAC concat exception binds exactly six C02 variants to one reproducibly generated fixture digest, requires the original frozen byte disagreement and proves complete concatenated-input PCM plus RFC frame and STREAMINFO validity | `flac_concat_fixture.command` owns scalar CPU dispatch and single-thread generation; its Linux regeneration regression requires the same full-file digest under conflicting CPU settings. `flac_concat_ci` validates every case before frozen output grouping and adapts only numbering, blocking and CRC packet bytes. `test_flac_concat_ci.py` rejects missing, duplicate, unlisted, refused, changed-fixture, stale-agreement, PCM, CRC, subframe, timing and STREAMINFO controls; its six-case integration runs in writer CI. Registered workflows and checker owners remain unchanged. Runtime media identity and oracle results cannot be proved by a binary pin, command pattern or Python type | | HEVC correctness shards retain the complete original roster and cannot count a partial, reordered or substituted part as complete | `corpus_shard` owns the deterministic assignment. `test_fixed_corpus_shards_cover_each_original_identity_once` proves the 54-file union without duplicates; `test_shard_completion_requires_its_exact_members_and_every_eof_arm` rejects missing, duplicate, reordered and changed identities and missing EOF arms. The workflow test requires all four non-fail-fast matrix entries and distinct artifact names | | Metadata-only HEVC acquisition does not apply the complete-decoder acceptance predicate | The existing `packet_native` mode selects that predicate only for C2. `test_online_census_releases_each_owned_media_file_and_keeps_receipts` preserves metadata-only cleanup and false full-coverage flags on its two-file fixture; the complete-roster/EOF tests retain the 54-file C2 denominator. The command boundary owns runtime mode selection | | HEVC C2 keeps container follow-ups separate from completed pixel comparisons and never treats a decoder, clock or pixel failure as one | Closed `PACKET_TOOLING_REFUSALS`, zero native output before classification, and `test_container_follow_ups_preserve_all_identities_without_pixel_credit`; the regression preserves all 54 identities and rejects missing EOF arms, decoder errors, clock errors and pixel mismatches. Runtime tool results require boundary validation | diff --git a/docs/agents/containers.md b/docs/agents/containers.md index 433084374..bc025c21c 100644 --- a/docs/agents/containers.md +++ b/docs/agents/containers.md @@ -4,6 +4,7 @@ ISO BMFF, Matroska, MPEG-TS, and AVI. | Rule | Enforced by | |---|---| +| Rewritten FLAC STREAMINFO bounds remain legal even without an internal block | `Survey::finish` handles the sole terminal block explicitly and constructs `Plan` with checked `u16` bounds, so serialization cannot truncate a sentinel. `a_single_repaired_frame_has_legal_representable_bounds` covers tiny and maximum-sized terminal blocks and the empty survey. The type bounds field width; runtime validation and the regression enforce RFC 9639's minimum and ordering constraints | | A copied-audio presentation control retains the independent input before a CoreAV-written intermediate and derives terminal trim from that input's edit | `matroska_copy_bitrate_keeps_complete_native_files` decodes both original MP4 tracks before replacement, reads the declared `elst` presentation, and checks the intermediate against the original PCM or its declared Opus prefix before checking the final output. Both source-construction paths also expose the pinned oracle's displaced samples. A production type cannot prove an external decoder's complete PCM identity, so the regression owns this check | | Copied-audio bitrate normalization distinguishes absent and explicit-zero requests and preserves unproved descriptions or MP3 clocks without new refusals | The shared policy type separates source-clock hints from explicit requests; the writer owns a spare-work-only MP3 header pass. Complete-file oracle controls cover default, CBR and VBR paths; synthetic fallback and descriptor/version tests pin unchanged output for unproved cases. Codec labels alone cannot prove a packet's complete frame clock | | An audio-bearing input-seek summary reports the edit's presentation origin, not the earlier decode access point | `clip::input_seek` uses the requested start for both the edit origin and `snapped_start`; `audio_bearing_input_seek_plans_complete_samples_before_writing` checks that summary alongside the retained sample ranges and serialized edits. A rational type cannot distinguish these two source-clock positions | diff --git a/docs/agents/ffmpeg-cli.md b/docs/agents/ffmpeg-cli.md index 1e9639c93..4119639fa 100644 --- a/docs/agents/ffmpeg-cli.md +++ b/docs/agents/ffmpeg-cli.md @@ -36,7 +36,7 @@ Ordinary CI uses `scripts/install-ci-oracle-ffmpeg.sh` with a SHA256-keyed archi | The map-order and first-input-copy oracle regressions own exclusive per-invocation directories and remove only their own artifacts on exit | `oracle_scratch` creates process/call-qualified paths with `create_dir`, and `FixtureDir` owns cleanup. `oracle_scratch_owns_isolated_directories_until_drop` rejects the former shared-path deletion and proves independent lifetimes. The legacy generated-fixture directory contract retains its existing owner and is not changed by this regression-fixture lifetime fix | | AAC packet identity cannot qualify an explicit concat filter when the complete stream-copy output differs from the pin | The option parser retains its bitstream-filter refusal. `ffmpeg_aac_filter` requires strict-safe refusal without destination/staging changes and compares complete real-fallback output bytes with the pin for AAC, video-only and unsupported variants. It removes no video hashes, sizes or bitrate fields to establish acceptance; codec-local identity is not whole-output equality | | Input seeking selects compatibility clip admission, not ordinary gapless clipping, and unsupported source metadata or combined bounds refuse before publication | `plan` refuses unsupported audio-only/bound/metadata cases and `write` uses `clip_mp4_input_seek` only when input `-ss` is present. `ffmpeg_input_seek` covers zero/nonzero seek, reordering, AV/audio/video selection, fractional endpoints, tail copies, source titles/handlers/volume, destination/staging preservation and original-argument fallback. Its bounded AV controls and two closed-GOP C18 witnesses compare complete records, pixels, AAC PCM and independent ISO layout/presentation checks. Codec display fields cannot establish retained packet bounds or presentation metadata | -| A compatibility fixture's intentional refusal is not a native pass and must prove destination preservation and exact pinned fallback | `audio_copy_check.py` binds the AC-3/E-AC-3 concat fixtures to their timing refusal, while FLAC/ALAC retain native packet and decode checks. `test_audio_copy_check.py` pins that scope and rejects wrong reasons, stale references and failed or unequal fallback. Types cannot prove an external program's exit, filesystem effects or bytes | +| A compatibility fixture's intentional refusal is not a native pass and must prove destination preservation and exact pinned fallback | `audio_copy_check.py` binds the AC-3/E-AC-3 concat fixtures to their timing refusal. FLAC concat compares every decoded packet after its RFC header rewrite; other lossless copies retain packet identity. `test_audio_copy_check.py` pins that scope and rejects changed PCM, wrong reasons, stale references and failed or unequal fallback. Types cannot prove an external program's exit, filesystem effects or bytes | | Compatibility concat admits several audio edits only for the qualified H.264 and primed AAC class, preserving each input's presentation rather than claiming exact FFmpeg concat clocks | `join_mp4` reuses parsed single-description AAC-LC configuration and requires an integral skipped prefix of at least its configured frame length. Writer regressions and `ffmpeg_concat_primed` independently check stored packets, edit windows, continuous raw audio and complete metadata; `concat_audio_gaps_are_refused_with_exact_pinned_fallback` retains unprimed gap refusals. A source edit enum cannot establish the constructed timeline | | H.264 compatibility concat writes avc1 with configuration-only parameter sets and keeps every real oracle difference explicit | `join::avc::Sample` validates NAL framing during staged copying and refuses in-sample parameter sets; the existing configuration comparison refuses differing avcC records. The shared `concat_output` comparator checks every packet, decoded output, format and stream field, and stored mdhd, permitting only asserted byte-identical oracle SPS/PPS insertions and named metadata/layout differences. Hostile in-band copies prove atomic refusal and exact fallback. External media requires runtime validation rather than a codec label | | Restoring H.264 concat does not weaken unrelated timing/filter refusals or change frozen instruction-guard inputs | Unedited, video-only gap, lossless audio and four unfiltered controls retain native success. `ffmpeg_concat_primed` separately qualifies twelve primed recipes and four additional controls, with in-band atomic refusal and exact fallback. Unprimed multi-edit audio and explicit filters remain refused. C02 retains its unchanged command and baseline, and `test_bench_guard` pins its command outside `REQUIRED_REFUSALS`. A Rust type cannot constrain Python guard registration or an external oracle | diff --git a/docs/design/concat-avc1.md b/docs/design/concat-avc1.md index b54ba32ba..5f525bf51 100644 --- a/docs/design/concat-avc1.md +++ b/docs/design/concat-avc1.md @@ -29,7 +29,7 @@ This plan and its implementation share one PR. The plan is the first commit. Top RFC 9639 Section 8.2 Table 3 defines STREAMINFO statistics for the complete FLAC stream. For the qualified sample-clock inputs, its sample count is the combined decoded sample count. Its MD5 is zero, meaning unknown. Block and frame bounds combine all inputs, including a formerly terminal short block that becomes internal after joining. The [native clock policy](concat-primed-aac.md#clock-and-layout-rules) defines unknown totals and retained bounds when clocks do not establish those statistics. Keeping the first input's total and MD5, as FFmpeg 9.0.2 does, would describe different audio. ALAC retains the maximum frame-byte bound across inputs and actual packets, with average bitrate zero, meaning unknown. -The generated FLAC inputs use fixed blocking. Their copied frame numbers restart at the second input, and a formerly final short block becomes internal. The output intentionally retains this MP4 packet-copy deviation shared with FFmpeg 9.0.2, contrary to the contiguous fixed-block stream model in RFC 9639 Sections 8.2, 9.1 and 9.1.5. It is not a claim of a conforming standalone FLAC stream. Rewriting the headers and their checksums would change packet bytes and is separate work. +The generated FLAC inputs use fixed blocking. FFmpeg 9.0.2 copies their headers, so frame numbers restart at the second input and a formerly final short block becomes internal. The [FLAC header follow-up](flac-concat-headers.md) repairs qualified compatibility concat streams by rewriting every frame to variable blocking with cumulative sample numbers and updated CRC-8 and CRC-16. Subframe bytes and decoded PCM are unchanged. This deliberately differs from the oracle's packets to satisfy RFC 9639 Sections 8.2, 9.1 and 9.1.5. Native `join` retains its opaque packet-copy policy. The compatibility caller supplies the format encoder identifier through `JoinCompatibility`. The CLI extracts that identifier from the existing WAV metadata owner instead of adding another version literal. The source's visual compressor name identifies copied video. Native joins also retain that source compressor name. diff --git a/docs/design/flac-concat-headers.md b/docs/design/flac-concat-headers.md new file mode 100644 index 000000000..a372bd0ae --- /dev/null +++ b/docs/design/flac-concat-headers.md @@ -0,0 +1,21 @@ +# FLAC concat frame headers + +`coreav ffmpeg -f concat -i list.txt -c copy output.mp4` must describe one FLAC stream, rather than several independently numbered streams in one track. RFC 9639 Sections 8.2, 9.1 and 9.1.5 require one blocking strategy, contiguous coded numbers and valid block-size bounds. FFmpeg 9.0.2 retains the input headers, including a short internal block and restarted frame numbers. + +## Plan + +Survey bounded frame headers during the existing first packet listing, before building the output tables. Reuse the audio crate's RFC parser and read at most 16 bytes per packet, without adding a listing or changing the work-limit admission. When the joined headers already describe a continuous stream, keep the packet bytes. Otherwise rewrite every header to variable blocking with the cumulative sample number. Copy subframes through the existing fixed window and update CRC-8 and CRC-16 without buffering or decoding frames. Preserve an invalid frame's CRC-16 error syndrome rather than concealing corruption. + +The survey and table passes share each input's packet-work budget. The rewritten sizes determine sample tables and chunk offsets before publication. A changed input or a size disagreement fails the temporary output. Single-input copies and the native `join` command retain their current packet-copy behavior. Unparseable headers retain packet copy rather than adding a new native refusal; the conformance guarantee concerns valid FLAC inputs with established sample clocks. Existing refusals for internal blocks below 16 samples are unchanged, because headers cannot repair those blocks. + +## Metadata + +RFC 9639 Section 8.2 Table 3 describes the output, not its first input. Set the total to the sum of frame block sizes, and MD5 to zero, meaning unknown. Set frame-byte bounds from the rewritten sample sizes. Block bounds include every non-final block and the largest block. Variable blocking permits conservative bounds; if the measured bounds are equal, widen the declared range so STREAMINFO does not describe fixed blocking. Keep sample rate, channel count and bit depth. + +## Verification + +Use generated PCM to property-test header rewriting across coded-number lengths, uncommon header fields and copy-buffer splits. Decode rewritten frames and compare every sample with the input PCM in order. Check cumulative sample numbers, both CRCs and STREAMINFO. The pinned-oracle concat regression must retain complete container and video checks while proving the intentional FLAC packet difference and equal decoded PCM. Keep the measurement rosters, baselines and divergence registry unchanged. + +The registered packet-copy oracles remain frozen, so they still report rewritten FLAC packets as different. Current writer CI alone uses `tools/compat-corpus/flac_concat_ci.py` for C02 variants 0 through 5 on `av_flac.mp4`, bound to SHA-256 `c52b70aa98caee8e674fd722390dbce2e0640a6e436d9ae2e3c29688049f9de0`. `scripts/flac_concat_fixture.py` is the sole producer of these identified fixture bytes: it pins FFmpeg CPU flags and count and libx264's scalar, single-thread encoder. Both finite two-second inputs are encoded completely. Automatic CPU dispatch can change H.264 packets without changing the advertised encoder settings, so a pinned binary alone does not establish this whole-file identity. A Linux regression regenerates the fixture under conflicting CPU-count and dispatch requests and requires the same full-file digest. All six cases must exist and be written. Every command and input list is checked before the frozen checker groups byte-identical outputs. + +The adapter requires exactly the two original 174-packet identity failures; renewed frozen byte agreement or any other failure is an error. It verifies the frozen owners' checksums, actual input binding, both CRCs, cumulative output numbering and STREAMINFO, then compares canonical packet identities with only the blocking bit, coded number and CRC bytes removed. Separately decoded, complete input PCM concatenated twice must equal the complete output PCM, with CRC errors fatal. The unchanged frozen oracle still checks all other packet fields, timing, decoded audio, video and container evidence. Invalid proof fails rather than suppressing a diagnostic. Native join checks and registered measurement workflows do not load this adapter; current-CI success is not registered packet-identity agreement. diff --git a/docs/design/join.md b/docs/design/join.md index e436b2edc..8d3f63c25 100644 --- a/docs/design/join.md +++ b/docs/design/join.md @@ -85,7 +85,7 @@ the same configuration differ in declared bit rates); accepting different codec configurations with several sample entries (ISO/IEC 14496-12 allows it, but many players read only the first). -FLAC block-size bounds are statistics rather than decoder configuration and may differ between inputs. When the media timescale equals the configured sample rate, joined STREAMINFO combines every input's bounds and representable formerly terminal internal blocks, counts the complete output samples, and writes an unknown MD5. Native packet copy keeps admission when those statistics cannot be established: a different clock or a total outside 36 bits writes total zero, and an unproved or unrepresentable internal block retains the combined declared block bounds. RFC 9639 section 8.2 Table 3 defines zero as an unknown total or MD5, but provides no unknown block bound. Retained bounds do not prove an internal short block conforms. These outputs retain the [packet-header deviation](concat-avc1.md), not standalone FLAC conformance. ALAC combines maximum frame bytes and writes an unknown average bitrate. Clipping and splitting retain their excerpt metadata policy. +FLAC block-size bounds are statistics rather than decoder configuration and may differ between inputs. When the media timescale equals the configured sample rate, joined STREAMINFO combines every input's bounds and representable formerly terminal internal blocks, counts the complete output samples, and writes an unknown MD5. Native packet copy keeps admission when those statistics cannot be established: a different clock or a total outside 36 bits writes total zero, and an unproved or unrepresentable internal block retains the combined declared block bounds. RFC 9639 section 8.2 Table 3 defines zero as an unknown total or MD5, but provides no unknown block bound. Retained bounds do not prove an internal short block conforms. Native `join` retains opaque packet copying, not standalone FLAC conformance. Compatibility concat instead [rewrites qualified discontinuous FLAC headers](flac-concat-headers.md) to variable blocking and cumulative sample numbers, derives block bounds and total samples from those headers, and recomputes frame-byte bounds after rewriting. A continuous stream or a single-input list keeps its packet bytes. ALAC combines maximum frame bytes and writes an unknown average bitrate. Clipping and splitting retain their excerpt metadata policy. The output keeps input 1's visual compressor name through the existing bounded sample-entry scan. Its media header duration is the sum of stored decode durations, independent of composition delay and edit endpoints. This preserves the presented tail on a join-to-clip round trip without inflating `mdhd`. diff --git a/scripts/audio_copy_check.py b/scripts/audio_copy_check.py index 448601627..f8b35a91a 100755 --- a/scripts/audio_copy_check.py +++ b/scripts/audio_copy_check.py @@ -17,13 +17,15 @@ - clip: the output's audio packets are a run of the source's, in order; - split: the segments' audio packets, in order, are the source's; - join, and "coreav ffmpeg -f concat -c copy", of a file with itself: the - output's audio packets are the source's twice; + output's audio packets are the source's twice, except that FLAC concat + rewrites frame headers and retains every decoded packet's PCM; - every output decodes without an error. AC-3 and E-AC-3 compatibility concat cases refuse, preserve the destination and use byte-identical pinned fallback for their multi-edit -timelines. FLAC and ALAC concat run natively and retain all packet and -decode checks above, with their complete output checked separately. +timelines. FLAC and ALAC concat run natively, with their complete output +checked separately. FLAC concat uses variable blocking and cumulative +sample numbers as RFC 9639 requires, unlike the pinned oracle's packets. Clips, segments and joins change the edit lists, so their packets are compared with edit lists ignored. The binaries are black boxes; no @@ -200,6 +202,25 @@ def check_packets(tools, source, outputs, problems, expected): f"source's {len(a)}") +def check_flac_concat(tools, source, output, problems): + same_stream(problems, tools.ffprobe, source, output) + decodes(problems, tools.ffmpeg, output) + stored_durations(problems, tools, output) + + def decoded_packets(path): + result = run([tools.ffmpeg, "-v", "error", "-ignore_editlist", "1", *AUDIO_DECODE_INPUT, + "-i", str(path), "-map", "0:a:0", "-f", "framemd5", "-"]) + if result.returncode or result.stderr: + raise RuntimeError("FLAC concat PCM comparison failed to decode") + return [tuple(field.strip() for field in line.split(",")[3:]) + for line in result.stdout.decode().splitlines() if line and not line.startswith("#")] + + expected = decoded_packets(source) + actual = decoded_packets(output) + if not expected or actual != expected * 2: + problems.append("FLAC concat decoded packets differ from the source repeated twice") + + def check_concat_refusal(tools, args, output, done, problems, reason): if done.returncode != 4 or done.stdout or done.stderr.strip() != reason: problems.append("concat must refuse with exit 4 and its exact reason") @@ -258,7 +279,7 @@ def cases(source, out): listing.write_text(f"file '{source}'\nfile '{source}'\n") yield "ffmpeg -f concat -c copy", ["ffmpeg", "-v", "error", "-f", "concat", "-safe", "0", "-i", str(listing), "-c", "copy", str(concat)], lambda: [concat], \ - "refused" if stem in CONCAT_REFUSALS else "twice" + "refused" if stem in CONCAT_REFUSALS else "flac-concat" if stem == "av_flac.mp4" else "twice" def main(): @@ -298,6 +319,8 @@ def main(): problems.append(f"exit {done.returncode}: {done.stderr.decode(errors='replace').strip()[:500]}") elif kind == "copy": check_copy(tools, source, outputs()[0], problems) + elif kind == "flac-concat": + check_flac_concat(tools, source, outputs()[0], problems) else: check_packets(tools, source, outputs(), problems, EXPECTED[kind]) except (subprocess.TimeoutExpired, RuntimeError, KeyError, IndexError, ValueError) as error: diff --git a/scripts/flac_concat_fixture.py b/scripts/flac_concat_fixture.py new file mode 100644 index 000000000..0e5b1ba54 --- /dev/null +++ b/scripts/flac_concat_fixture.py @@ -0,0 +1,30 @@ +"""The deterministic, full-file-identified FLAC concat fixture for current CI.""" +import argparse +from pathlib import Path +import subprocess +import sys + +sys.path.insert(0, str(Path(__file__).resolve().parent)) + +import test_oracle + + +def command(ffmpeg, output): + return [str(ffmpeg), '-v', 'error', '-y', '-cpuflags', '0', '-cpucount', '1', + '-f', 'lavfi', '-i', 'testsrc2=size=320x240:rate=25:duration=2', + '-f', 'lavfi', '-i', 'sine=frequency=440:duration=2', + '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-g', '25', + '-threads:v', '1', '-x264-params', 'asm=0', + '-c:a', 'flac', '-strict', '-2', str(output)] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('output') + args = parser.parse_args() + tools = test_oracle.require() + subprocess.run(command(tools.ffmpeg, args.output), check=True, timeout=120) + + +if __name__ == '__main__': + main() diff --git a/scripts/gen-fixtures.sh b/scripts/gen-fixtures.sh index b93c99aee..f55a7236e 100755 --- a/scripts/gen-fixtures.sh +++ b/scripts/gen-fixtures.sh @@ -617,7 +617,12 @@ try_gen codec_eac3_stereo.mp4 "${KAUD[@]}" -c:a eac3 try_gen codec_eac3_51.mp4 "${KAUD[@]}" "${KSIX[@]}" -c:a eac3 try_gen codec_flac.mp4 "${KAUD[@]}" -c:a flac -strict -2 # Video beside each audio codec the clip, split and join writers copy. -for codec in flac alac ac3 eac3; do +if python3 -I "$SCRIPTS/flac_concat_fixture.py" "$OUT/av_flac.mp4" 2> "$OUT/av_flac.mp4.log"; then + rm -f "$OUT/av_flac.mp4.log"; generated av_flac.mp4 +else + rm -f "$OUT/av_flac.mp4"; skip av_flac.mp4 'ffmpeg-failed(see-av_flac.mp4.log)' +fi +for codec in alac ac3 eac3; do try_gen "av_$codec.mp4" "${SRC[@]}" "${VID[@]}" -c:a "$codec" -strict -2 -shortest done # The pinned FFmpeg's MP4 muxer refuses libvorbis packets straight from the diff --git a/scripts/tests/test_audio_copy_check.py b/scripts/tests/test_audio_copy_check.py index 4184eebcc..90d456506 100644 --- a/scripts/tests/test_audio_copy_check.py +++ b/scripts/tests/test_audio_copy_check.py @@ -20,7 +20,7 @@ def test_only_ac3_and_eac3_concat_fixtures_require_refusal(self): with self.subTest(codec=codec): cases = list(checker.cases(out / f"av_{codec}.mp4", out)) self.assertEqual(next(c[3] for c in cases if c[0] == "join"), "twice") - self.assertEqual(cases[-1][3], "refused" if codec in {"ac3", "eac3"} else "twice") + self.assertEqual(cases[-1][3], "refused" if codec in {"ac3", "eac3"} else "flac-concat" if codec == "flac" else "twice") self.assertTrue(all(c[3] != "refused" for c in cases[:-1])) def test_unexpected_native_success_wrong_reason_and_published_output_fail(self): @@ -105,6 +105,23 @@ def test_lossless_concat_keeps_the_complete_packet_contract(self): self.assertFalse(checker.EXPECTED["twice"](packets, packets)) self.assertFalse(checker.EXPECTED["twice"](packets, packets + list(reversed(packets)))) + def test_flac_concat_requires_every_original_pcm_packet_twice(self): + source = b"#format: frame checksums\n0, 0, 0, 32, 128, first\n0, 32, 32, 16, 64, second\n" + repeated = source + source + for output, success in [(repeated, True), (source, False), + (repeated.replace(b"second", b"changed", 1), False), + (b"", False)]: + with self.subTest(output=output): + tools = SimpleNamespace(ffmpeg="controlled-ffmpeg", ffprobe="controlled-ffprobe") + results = [subprocess.CompletedProcess([], 0, data, b"") for data in (source, output)] + problems = [] + with mock.patch.object(checker, "run", side_effect=results), \ + mock.patch.object(checker, "same_stream"), \ + mock.patch.object(checker, "decodes"), \ + mock.patch.object(checker, "stored_durations"): + checker.check_flac_concat(tools, Path("source.mp4"), Path("output.mp4"), problems) + self.assertEqual(not problems, success) + def test_each_fixture_requires_its_own_reason(self): self.assertEqual(checker.CONCAT_REFUSALS, { "av_ac3.mp4": checker.CONCAT_REASON, diff --git a/tools/compat-corpus/flac_concat_ci.py b/tools/compat-corpus/flac_concat_ci.py new file mode 100644 index 000000000..93b471cce --- /dev/null +++ b/tools/compat-corpus/flac_concat_ci.py @@ -0,0 +1,279 @@ +"""Current fixture CI policy. Never use this adapter to judge a frozen round.""" +import argparse +import contextlib +import hashlib +import json +import os +from pathlib import Path +import sys +import tempfile + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] +sys.path[:0] = [str(HERE), str(ROOT / 'scripts')] + +import copy_check +import decoded_output +import join_check +from oracle_audio import INPUT_ARGS as AUDIO_DECODE_INPUT + +LEGACY_ORACLE = copy_check.oracle +MAX_PACKET = 16 << 20 +MAX_PACKETS = 1 << 16 +FROZEN = ('scripts/clip_check.py', 'scripts/join_check.py', 'tools/compat-corpus/copy_check.py') +FIXTURE = 'av_flac.mp4' +FIXTURE_SHA256 = 'c52b70aa98caee8e674fd722390dbce2e0640a6e436d9ae2e3c29688049f9de0' +VARIANTS = ( + ('-f', 'concat', '-i', 'INPUT', '-c', 'copy', 'OUTPUT'), + ('-f', 'concat', '-safe', '0', '-i', 'INPUT', '-c', 'copy', 'OUTPUT'), + ('-safe', '0', '-f', 'concat', '-i', 'INPUT', '-c', 'copy', 'OUTPUT'), + ('-y', '-f', 'concat', '-i', 'INPUT', '-c', 'copy', 'OUTPUT'), + ('-f', 'concat', '-i', 'INPUT', '-vcodec', 'copy', '-acodec', 'copy', 'OUTPUT'), + ('-y', '-safe', '0', '-f', 'concat', '-i', 'INPUT', '-c', 'copy', 'OUTPUT'), +) +FROZEN_DIFFERENCES = [ + 'audio: 174 packets differ, first (input, packet) [(0, 0), (0, 1), (0, 2)]', + "audio: 174 packets differ from ffmpeg concat's, first 0", +] + + +def frozen_check(root=ROOT): + expected = json.loads((root / 'docs/measurements/compat-0.1.0/judging.json').read_text())['files'] + if any(hashlib.sha256((root / name).read_bytes()).hexdigest() != expected[name] for name in FROZEN): + raise ValueError('stage=frozen_mismatch') + + +def crc(data, width, polynomial): + value = 0 + mask = (1 << width) - 1 + for byte in data: + value ^= byte << (width - 8) + for _ in range(8): + value = ((value << 1) ^ (polynomial if value & (1 << (width - 1)) else 0)) & mask + return value + + +def frame(data): + if not 8 <= len(data) <= MAX_PACKET or data[0] != 255 or data[1] & 254 != 248: + raise ValueError('FLAC frame extent or sync') + block_code, rate_code = data[2] >> 4, data[2] & 15 + if data[3] & 1 or data[3] >> 4 > 10 or (data[3] >> 1) & 7 == 3 or block_code == 0 or rate_code == 15: + raise ValueError('FLAC reserved field') + first = data[4] + count = 1 if first < 128 else next((n for n in range(2, 8) if first >> (8 - n) == (1 << n) - 1 + and not first & (1 << (7 - n))), 0) + if count == 0 or 4 + count > len(data) - 3: + raise ValueError('FLAC coded number') + number = first if count == 1 else first & ((1 << (7 - count)) - 1) + for byte in data[5:4 + count]: + if byte & 192 != 128: + raise ValueError('FLAC coded number continuation') + number = number * 64 + (byte & 63) + minimum = 0 if count == 1 else 128 if count == 2 else 1 << (5 * count - 4) + variable = bool(data[1] & 1) + if number < minimum or number >= 1 << (36 if variable else 31): + raise ValueError('FLAC coded number range') + number_end = at = 4 + count + if block_code in (6, 7): + width = block_code - 5 + block = int.from_bytes(data[at:at + width], 'big') + 1 + at += width + else: + block = 192 if block_code == 1 else 576 << (block_code - 2) if block_code <= 5 else 1 << block_code + if not 1 <= block <= 65535: + raise ValueError('FLAC block size') + rate = (None, 88200, 176400, 192000, 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000)[rate_code] if rate_code < 12 else None + if rate_code >= 12: + width = 1 if rate_code == 12 else 2 + rate = int.from_bytes(data[at:at + width], 'big') * (1000 if rate_code == 12 else 10 if rate_code == 14 else 1) + if rate == 0: + raise ValueError('FLAC sample rate') + at += width + if at >= len(data) - 2 or at >= 16 or crc(data[:at], 8, 7) != data[at]: + raise ValueError('FLAC header CRC') + if crc(data, 16, 0x8005): + raise ValueError('FLAC frame CRC') + canonical = data[:1] + bytes([data[1] & 254]) + data[2:4] + b'\0' + data[number_end:at] + data[at + 1:-2] + channels = (data[3] >> 4) + 1 if data[3] >> 4 <= 7 else 2 + bits = (None, 8, 12, None, 16, 20, 24, 32)[data[3] >> 1 & 7] + return {'block': block, 'number': number, 'variable': variable, 'size': len(data), + 'rate': rate, 'channels': channels, 'bits': bits, + 'canonical': canonical} + + +def stream_info(data): + if len(data) != 34: + raise ValueError('FLAC STREAMINFO length') + packed = int.from_bytes(data[10:18], 'big') + return {'minimum': int.from_bytes(data[:2], 'big'), 'maximum': int.from_bytes(data[2:4], 'big'), + 'min_frame': int.from_bytes(data[4:7], 'big'), 'max_frame': int.from_bytes(data[7:10], 'big'), + 'format': packed >> 36, 'samples': packed & ((1 << 36) - 1), 'md5': data[18:]} + + +def validate_stream(frames, info, expected_format): + rate, channels, bits = info['format'] >> 8, (info['format'] >> 5 & 7) + 1, (info['format'] & 31) + 1 + if (not frames or not 16 <= info['minimum'] <= info['maximum'] <= 65535 + or info['format'] != expected_format or rate == 0 or bits < 4): + raise ValueError('FLAC output STREAMINFO') + strategy, nominal, samples = frames[0]['variable'], frames[0]['block'], 0 + for index, item in enumerate(frames): + final = index + 1 == len(frames) + if (item['variable'] != strategy or item['number'] != (samples if strategy else index) + or item['rate'] not in (None, rate) or item['bits'] not in (None, bits) or item['channels'] != channels + or item['block'] > info['maximum'] or not final and item['block'] < info['minimum'] + or not strategy and (item['block'] > nominal or not final and item['block'] != nominal)): + raise ValueError('FLAC output continuity or block bounds') + samples += item['block'] + sizes = [item['size'] for item in frames] + minimum, maximum = (value if value < 1 << 24 else 0 for value in (min(sizes), max(sizes))) + if (strategy and info['minimum'] == info['maximum'] or samples >= 1 << 36 or info['samples'] != samples + or info['md5'] != bytes(16) or info['min_frame'] != minimum or info['max_frame'] != maximum): + raise ValueError('FLAC output statistics') + + +def packet_key(packet): + return int(packet['pos']), int(packet['size']), packet['data_hash'] + + +def stream_projection(ffprobe, path, index, reader, output, expected_format): + description = join_check.cc.ffprobe_json(ffprobe, '-select_streams', str(index), '-show_streams', '-show_data', path) + streams = description.get('streams', []) + if len(streams) != 1 or streams[0].get('codec_name') != 'flac': + return None + packets = reader(ffprobe, path, index, raw=True) + if not packets or len(packets) > MAX_PACKETS: + raise ValueError('FLAC packet count') + frames, projection = [], {} + with open(path, 'rb') as source: + extent = os.fstat(source.fileno()).st_size + previous_end = total = 0 + for packet in packets: + offset, size, digest = packet_key(packet) + total += size + if not 0 < size <= MAX_PACKET or total > 128 << 20 or offset < previous_end or offset + size > extent: + raise ValueError('FLAC packet extent') + source.seek(offset) + data = source.read(size) + if len(data) != size or digest != 'SHA256:' + hashlib.sha256(data).hexdigest(): + raise ValueError('FLAC packet identity') + item = frame(data) + canonical = item.pop('canonical') + frames.append(item) + projection[packet_key(packet)] = (len(canonical), 'SHA256:' + hashlib.sha256(canonical).hexdigest()) + previous_end = offset + size + if Path(path).resolve() == output: + info = stream_info(decoded_output.hexdump(streams[0].get('extradata', ''))) + validate_stream(frames, info, expected_format) + return projection + + +@contextlib.contextmanager +def projected_packets(output, expected_format): + reader = join_check.cc.packets + cache = {} + + def packets(ffprobe, path, index, raw=False): + key = str(Path(path).resolve()), index + if key not in cache: + cache[key] = stream_projection(ffprobe, path, index, reader, Path(output).resolve(), expected_format) + rows = reader(ffprobe, path, index, raw=raw) + projection = cache[key] + if projection is None: + return rows + result = [] + for packet in rows: + size, digest = projection[packet_key(packet)] + result.append({**packet, 'size': str(size), 'data_hash': digest}) + return result + + join_check.cc.packets = packets + try: + yield + finally: + join_check.cc.packets = reader + + +def qualified_format(record, tools): + if record.get('job') != 'join' or Path(record['source']).name != FIXTURE: + return None + if (record.get('id') != 'C02' or record.get('status') != 'written' + or tuple(record['args']) not in VARIANTS): + raise ValueError('stage=unlisted_flac_concat_case') + source = Path(record['source']) + if source.stat().st_size > 128 << 20 or copy_check.digest([source]) != FIXTURE_SHA256: + raise ValueError('stage=flac_fixture_digest') + streams = copy_check.streams(tools['ffprobe'], record['source']) or [] + audio = [stream for stream in streams if stream.get('codec_type') == 'audio'] + video = [stream for stream in streams if stream.get('codec_type') == 'video'] + if len(streams) != 2 or len(audio) != 1 or len(video) != 1 or audio[0].get('codec_name') != 'flac': + raise ValueError('stage=flac_fixture_streams') + listing = Path(record['input']) + line = "file '" + source.name + "'\n" + if listing.stat().st_size > 4096 or listing.read_text() != line * 2: + raise ValueError('stage=flac_fixture_list') + listed = listing.parent / source.name + if (not os.path.samefile(source, listed) + and (max(source.stat().st_size, listed.stat().st_size) > 128 << 20 + or copy_check.digest([source]) != copy_check.digest([listed]))): + raise ValueError('stage=flac_fixture_binding') + inputs = copy_check.entry(record, None, [])['inputs'] + if len(inputs) != 2 or any(Path(item['path']).resolve() != listed.resolve() + or item['audio_stream'] != audio[0]['index'] + or item['video_stream'] != video[0]['index'] for item in inputs): + raise ValueError('stage=flac_fixture_inputs') + config = join_check.cc.extradata(tools['ffprobe'], record['source'], audio[0]['index']) + return stream_info(decoded_output.hexdump(config))['format'] + + +def pcm_proof(record, tools): + limit = 16 << 20 + with tempfile.TemporaryDirectory(prefix='flac-concat-ci-') as temporary: + decoded = [] + for index, source in enumerate((record['source'], record['output'])): + path = Path(temporary) / str(index) + command = [tools['ffmpeg'], '-v', 'error', '-nostdin', '-xerror', '-err_detect', 'crccheck+explode', + '-ignore_editlist', '1', *AUDIO_DECODE_INPUT, '-i', str(source), '-map', '0:a:0', + '-c:a', 'pcm_s32le', '-f', 's32le', '-fs', str(limit), '-'] + code, _, error = decoded_output.invoke(command, os.environ.copy(), output=path) + if code != 0 or error or not 0 < path.stat().st_size < limit: + raise ValueError('stage=flac_pcm_decode') + decoded.append(path.read_bytes()) + if decoded[1] != decoded[0] * 2: + raise ValueError('stage=flac_concatenated_input_pcm') + + +def exact_cases(records): + cases = [record for record in records if record.get('job') == 'join' + and Path(record['source']).name == FIXTURE] + if (len(cases) != len(VARIANTS) or {tuple(record['args']) for record in cases} != set(VARIANTS) + or any(record.get('id') != 'C02' or record.get('status') != 'written' for record in cases)): + raise ValueError('stage=flac_exact_six_cases') + return cases + + +def current_oracle(record, tools): + expected_format = qualified_format(record, tools) + if expected_format is None: + return LEGACY_ORACLE(record, tools) + original = LEGACY_ORACLE(record, tools) + if original != FROZEN_DIFFERENCES: + return original or ['stage=flac_frozen_identity_returned'] + pcm_proof(record, tools) + with projected_packets(record['output'], expected_format): + return LEGACY_ORACLE(record, tools) + + +def main(): + frozen_check() + parser = argparse.ArgumentParser(add_help=False) + parser.add_argument('--records', required=True) + parser.add_argument('--ffprobe', default='ffprobe') + args, _ = parser.parse_known_args() + for record in exact_cases(json.loads(Path(args.records).read_text())): + qualified_format(record, {'ffprobe': args.ffprobe}) + copy_check.oracle = current_oracle + return copy_check.main() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/compat-corpus/tests/test_flac_concat_ci.py b/tools/compat-corpus/tests/test_flac_concat_ci.py new file mode 100644 index 000000000..2de68d62d --- /dev/null +++ b/tools/compat-corpus/tests/test_flac_concat_ci.py @@ -0,0 +1,280 @@ +import copy +from fractions import Fraction +import hashlib +import json +import os +from pathlib import Path +import sys +import subprocess +import tempfile +import unittest +from unittest import mock + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +import flac_concat_ci as checker +import flac_concat_fixture + + +def packet(number, variable=False, block=32, body=bytes(6)): + if number < 128: + coded = bytes([number]) + else: + count = next(n for n in range(2, 8) if number < 1 << (5 * n + 1)) + coded = bytes([(255 << (8 - count) & 255) | (number >> (6 * (count - 1)))]) + coded += bytes(128 | (number >> (6 * n) & 63) for n in reversed(range(count - 1))) + header = bytes([255, 248 | variable, 0x6a, 0x18]) + coded + bytes([block - 1]) + header += bytes([checker.crc(header, 8, 7)]) + data = header + body + return data + checker.crc(data, 16, 0x8005).to_bytes(2, 'big') + + +def config(samples=128, minimum=16, maximum=32, frame_size=15): + packed = 48000 << 44 | 1 << 41 | 15 << 36 | samples + return (minimum.to_bytes(2, 'big') + maximum.to_bytes(2, 'big') + + frame_size.to_bytes(3, 'big') * 2 + packed.to_bytes(8, 'big') + bytes(16)) + + +def dump(data): + lines = [] + for at in range(0, len(data), 16): + part = data[at:at + 16] + words = ' '.join(part[n:n + 2].hex() for n in range(0, len(part), 2)) + lines.append(f'{at:08x}: {words:<39} .') + return '\n' + '\n'.join(lines) + + +def rows(path, packets): + path.write_bytes(b''.join(packets)) + result, offset = [], 0 + for index, data in enumerate(packets): + result.append({'pos': str(offset), 'size': str(len(data)), 'data_hash': 'SHA256:' + hashlib.sha256(data).hexdigest(), + 'pts': index * 32, 'dts': index * 32, 'duration': 32, 'flags': 'K_'}) + offset += len(data) + return result + + +class HeaderProof(unittest.TestCase): + def test_rfc_header_crc_and_canonical_subframes(self): + self.assertEqual(checker.crc(bytes.fromhex('fff869180000'), 8, 7), 0xbf) + original = checker.frame(packet(0))['canonical'] + for number in [0, 127, 128, 2048, 65536, 1 << 21, 1 << 26, 1 << 31, (1 << 36) - 1]: + data = packet(number, True) + parsed = checker.frame(data) + self.assertEqual(parsed['number'], number) + self.assertEqual(parsed['canonical'], original) + self.assertEqual(checker.crc(data, 16, 0x8005), 0) + changed = checker.frame(packet(0, True, body=b'\0\0\1\0\0\0')) + self.assertNotEqual(changed['canonical'], original) + + def test_bad_crcs_noncanonical_numbers_and_extents_fail(self): + good = packet(0) + bad_header = bytearray(good) + bad_header[6] ^= 1 + bad_header[-2:] = checker.crc(bad_header[:-2], 16, 0x8005).to_bytes(2, 'big') + noncanonical = good[:4] + b'\xc0\x80' + good[5:6] + noncanonical += bytes([checker.crc(noncanonical, 8, 7)]) + bytes(6) + noncanonical += checker.crc(noncanonical, 16, 0x8005).to_bytes(2, 'big') + for bad in [*([good[:end] for end in range(8)]), bytes(bad_header), noncanonical, + good[:-1] + bytes([good[-1] ^ 1])]: + with self.subTest(data=bad), self.assertRaises(ValueError): + checker.frame(bad) + + def test_output_sequence_and_every_streaminfo_statistic_are_required(self): + frames = [checker.frame(packet(number, True)) for number in [0, 32, 64, 96]] + info = checker.stream_info(config()) + checker.validate_stream(frames, info, info['format']) + for name, value in [('minimum', 32), ('maximum', 31), ('samples', 64), + ('min_frame', 14), ('max_frame', 16), ('md5', b'x' * 16), ('format', 0)]: + with self.subTest(field=name), self.assertRaises(ValueError): + checker.validate_stream(frames, {**info, name: value}, info['format']) + for name, value in [('number', 0), ('variable', False), ('block', 1)]: + bad = copy.deepcopy(frames) + bad[2][name] = value + with self.subTest(frame_field=name), self.assertRaises(ValueError): + checker.validate_stream(bad, info, info['format']) + fixed = [checker.frame(packet(number)) for number in [0, 1]] + checker.validate_stream(fixed, checker.stream_info(config(64, 32, 32)), info['format']) + + +class ScopedProjection(unittest.TestCase): + def test_exact_six_cases_reject_missing_duplicate_unlisted_or_refused_variants(self): + cases = [{'id': 'C02', 'job': 'join', 'status': 'written', 'source': checker.FIXTURE, 'args': list(args)} + for args in checker.VARIANTS] + self.assertEqual(checker.exact_cases(cases), cases) + for bad in [[], cases[:-1], cases + cases[:1], cases[:-1] + cases[:1], + [{**row, 'id': 'C03'} for row in cases], + [{**row, 'status': 'refused'} for row in cases], + [{**row, 'args': ['-hide_banner'] + row['args']} for row in cases]]: + with self.subTest(cases=bad), self.assertRaisesRegex(ValueError, 'exact_six'): + checker.exact_cases(bad) + + def test_fixture_identity_is_not_a_filename_or_command_pattern(self): + with tempfile.TemporaryDirectory() as temporary: + source = Path(temporary) / checker.FIXTURE + source.write_bytes(b'wrong fixture') + record = {'id': 'C02', 'job': 'join', 'status': 'written', + 'source': str(source), 'args': list(checker.VARIANTS[0])} + with self.assertRaisesRegex(ValueError, 'fixture_digest'): + checker.qualified_format(record, {}) + for field, value in [('id', 'C03'), ('status', 'refused'), + ('args', ['-hide_banner'] + record['args'])]: + with self.subTest(field=field), self.assertRaisesRegex(ValueError, 'unlisted'): + checker.qualified_format({**record, field: value}, {}) + + def test_every_binding_is_checked_before_frozen_output_grouping(self): + cases = [{'id': 'C02', 'job': 'join', 'status': 'written', 'source': checker.FIXTURE, 'args': list(args)} + for args in checker.VARIANTS] + with tempfile.TemporaryDirectory() as temporary: + records = Path(temporary) / 'records.json' + records.write_text(json.dumps(cases)) + with mock.patch.object(sys, 'argv', ['checker', '--records', str(records), '--ffprobe', 'qualified']), \ + mock.patch.object(checker, 'frozen_check'), \ + mock.patch.object(checker, 'qualified_format', side_effect=[123] * 5 + [ValueError('last binding')]) as qualify, \ + mock.patch.object(checker.copy_check, 'main') as grouped: + with self.assertRaisesRegex(ValueError, 'last binding'): + checker.main() + self.assertEqual(qualify.call_args_list, + [mock.call(record, {'ffprobe': 'qualified'}) for record in cases]) + grouped.assert_not_called() + + def test_frozen_agreement_or_other_failure_cannot_be_adapted(self): + with mock.patch.object(checker, 'qualified_format', return_value=123), \ + mock.patch.object(checker, 'pcm_proof') as pcm, \ + mock.patch.object(checker, 'projected_packets') as projection: + for problems in [[], ['video differs'], checker.FROZEN_DIFFERENCES + ['timing differs']]: + with mock.patch.object(checker, 'LEGACY_ORACLE', return_value=problems): + self.assertTrue(checker.current_oracle({}, {})) + pcm.assert_not_called() + projection.assert_not_called() + + def test_pcm_must_equal_both_complete_inputs_and_decoder_failure_is_fatal(self): + for change in ('none', 'sample', 'short', 'empty', 'decode', 'stderr', 'timeout'): + with self.subTest(change=change): + def invoke(command, env, output): + self.assertIn('-xerror', command) + self.assertIn('crccheck+explode', command) + self.assertLess(command.index('-ignore_editlist'), command.index('-i')) + data = b'abcd' if command[command.index('-i') + 1] == 'input' else b'abcdabcd' + if output.name == '1': + data = {'sample': b'abcdabce', 'short': b'abcd', 'empty': b''}.get(change, data) + output.write_bytes(data) + return (None if change == 'timeout' else 1 if change == 'decode' else 0, + b'', b'bad decode' if change == 'stderr' else b'') + with mock.patch.object(checker.decoded_output, 'invoke', side_effect=invoke): + if change == 'none': + checker.pcm_proof({'source': 'input', 'output': 'output'}, {'ffmpeg': 'ffmpeg'}) + else: + with self.assertRaisesRegex(ValueError, 'flac_.*pcm'): + checker.pcm_proof({'source': 'input', 'output': 'output'}, {'ffmpeg': 'ffmpeg'}) + + def exercise(self, change=None): + with tempfile.TemporaryDirectory() as temporary: + source, output = (Path(temporary) / name for name in ('source', 'output')) + inputs = rows(source, [packet(0), packet(1)]) + data = [packet(n, True) for n in [0, 32, 64, 96]] + if change == 'subframe': + data[2] = packet(64, True, body=b'\0\0\1\0\0\0') + if change == 'restart': + data[2] = packet(0, True) + outputs = rows(output, data) + if change == 'timing': + outputs[2]['pts'] += 99 + if change == 'receipt': + outputs[2]['data_hash'] = 'SHA256:wrong' + original_rows = copy.deepcopy(outputs) + + def reader(ffprobe, path, index, raw=False): + return copy.deepcopy(outputs if Path(path) == output else inputs) + + info = checker.stream_info(config()) + description = {'streams': [{'codec_name': 'flac', 'extradata': dump(config())}]} + with mock.patch.object(checker.join_check.cc, 'packets', reader), \ + mock.patch.object(checker.join_check.cc, 'ffprobe_json', return_value=description): + self.assertEqual(checker.join_check.cc.packets('probe', output, 0), original_rows) + with checker.projected_packets(output, info['format']): + normalized = checker.join_check.cc.packets('probe', output, 0) + original = checker.join_check.cc.packets('probe', source, 0) + for old, new in zip(original_rows, normalized): + self.assertEqual({k: v for k, v in old.items() if k not in ('size', 'data_hash')}, + {k: v for k, v in new.items() if k not in ('size', 'data_hash')}) + counts, found = {'agree': 0, 'disagree': 0}, [] + checker.join_check.compare_packets('audio', [original, original], + [Fraction(0), Fraction(64, 48000)], normalized, Fraction(1, 48000), counts, found, {}) + self.assertIs(checker.join_check.cc.packets, reader) + return found + + def test_only_valid_header_differences_are_projected(self): + self.assertEqual(self.exercise(), []) + self.assertTrue(self.exercise('subframe')) + self.assertTrue(self.exercise('timing')) + for change in ('restart', 'receipt'): + with self.subTest(change=change), self.assertRaises(ValueError): + self.exercise(change) + + def test_non_flac_and_other_jobs_keep_the_legacy_judgment(self): + reader = lambda *args, **kwargs: [{'size': '7', 'data_hash': 'unchanged', 'pts': 123}] + with mock.patch.object(checker.join_check.cc, 'packets', reader), \ + mock.patch.object(checker.join_check.cc, 'ffprobe_json', return_value={'streams': [{'codec_name': 'aac'}]}): + with checker.projected_packets('output', 0): + self.assertEqual(checker.join_check.cc.packets('probe', 'path', 0), reader()) + record, tools = {'job': 'remux'}, object() + with mock.patch.object(checker, 'LEGACY_ORACLE', return_value=['original failure']) as legacy: + self.assertEqual(checker.current_oracle(record, tools), ['original failure']) + legacy.assert_called_once_with(record, tools) + + def test_the_frozen_owners_cannot_change_under_the_adapter(self): + checker.frozen_check() + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + expected = {} + for name in checker.FROZEN: + path = root / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(name) + expected[name] = hashlib.sha256(path.read_bytes()).hexdigest() + registration = root / 'docs/measurements/compat-0.1.0/judging.json' + registration.parent.mkdir(parents=True) + registration.write_text(json.dumps({'files': expected})) + checker.frozen_check(root) + (root / checker.FROZEN[0]).write_text('changed') + with self.assertRaisesRegex(ValueError, 'frozen_mismatch'): + checker.frozen_check(root) + + def test_only_current_writer_ci_uses_the_adapter(self): + workflow = (checker.ROOT / '.github/workflows/ci.yml').read_text() + self.assertEqual(workflow.count('python3 -I tools/compat-corpus/flac_concat_ci.py --records'), 1) + self.assertIn('python3 -I tools/compat-corpus/copy_check.py --records', workflow) + frozen = (checker.ROOT / '.github/workflows/compat-0.1.0.yml').read_text() + self.assertNotIn('flac_concat_ci', frozen) + + +@unittest.skipUnless(os.environ.get('COREAV_CURRENT_CI_ORACLE') == '1', 'needs generated current-CI records') +class GeneratedRecords(unittest.TestCase): + @unittest.skipUnless(sys.platform == 'linux', 'the fixture digest uses the pinned Linux oracle') + def test_fixture_generation_pins_override_cpu_requests_and_match_the_digest(self): + import test_oracle + tools = test_oracle.require() + with tempfile.TemporaryDirectory() as temporary: + for index, (count, flags) in enumerate(((1, 'sse2'), (8, 'ssse3'))): + output = Path(temporary) / f'{index}.mp4' + command = flac_concat_fixture.command(tools.ffmpeg, output) + command[1:1] = ['-cpucount', str(count), '-cpuflags', flags] + at = command.index('-c:v') + command[at:at] = ['-x264-params', f'asm={flags}'] + done = subprocess.run(command, capture_output=True, timeout=120, check=False) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertEqual(hashlib.sha256(output.read_bytes()).hexdigest(), checker.FIXTURE_SHA256) + + def test_all_six_flac_concat_variants_keep_the_complete_oracle(self): + import test_oracle + tools = test_oracle.require() + programs = {'ffmpeg': str(tools.ffmpeg), 'ffprobe': str(tools.ffprobe), 'mediainfo': None} + records = json.loads((Path(os.environ['COREAV_FFMPEG_OUT']) / 'records.json').read_text()) + selected = [record for record in records if record.get('status') == 'written' + and checker.qualified_format(record, programs) is not None] + self.assertEqual(len(selected), 6) + checker.exact_cases(selected) + for record in selected: + with self.subTest(args=record['args']): + self.assertEqual(checker.LEGACY_ORACLE(record, programs), checker.FROZEN_DIFFERENCES) + self.assertEqual(checker.current_oracle(record, programs), [])