Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion crates/coreav-audio/src/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
3 changes: 3 additions & 0 deletions crates/coreav-audio/src/flac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
142 changes: 142 additions & 0 deletions crates/coreav-audio/src/flac/rewrite.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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<usize> {
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<u32> {
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());
}
}
}
}
37 changes: 37 additions & 0 deletions crates/coreav-cli/tests/ffmpeg_concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
};
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 = [
Expand Down
72 changes: 41 additions & 31 deletions crates/coreav-cli/tests/support/concat_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -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,
Expand Down
Loading
Loading