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
5 changes: 5 additions & 0 deletions .github/workflows/h264-encoder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
- crates/coreav-transcode/**
- crates/coreav-frame/**
- scripts/native_h264_cli.py
- scripts/track_boxes.py
- crates/coreav-kernels-h264/**
- crates/coreav-h264/**
- scripts/h264_g1*
Expand Down Expand Up @@ -38,6 +39,7 @@ on:
- crates/coreav-transcode/**
- crates/coreav-frame/**
- scripts/native_h264_cli.py
- scripts/track_boxes.py
- crates/coreav-kernels-h264/**
- crates/coreav-h264/**
- scripts/h264_g1*
Expand Down Expand Up @@ -94,6 +96,8 @@ jobs:
cp target/release/coreav "$RUNNER_TEMP/default-base"
cargo build --locked --release -p coreav-cli --features native-h264
cp target/release/coreav "$RUNNER_TEMP/external-base"
cargo build --locked --release -p coreav-cli --features native-h264-encoder,native-h264
cp target/release/coreav "$RUNNER_TEMP/native-base"
- name: Keep native encoding out of default and release builds
run: |
set -euo pipefail
Expand Down Expand Up @@ -132,6 +136,7 @@ jobs:
--default-candidate "$RUNNER_TEMP/default-candidate" \
--baseline-default "$RUNNER_TEMP/default-base" \
--baseline-external "$RUNNER_TEMP/external-base" \
--baseline-native "$RUNNER_TEMP/native-base" \
--driver target/release/examples/p \
--ffmpeg "$RUNNER_TEMP/cli-oracles/ffmpeg" \
--output "$RUNNER_TEMP/native-cli"
Expand Down
17 changes: 12 additions & 5 deletions crates/coreav-cli/src/ffmpeg/native_h264.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ pub(super) fn source(report: &Report, index: usize) -> bool {
&& matches!(field("pix_fmt"), Some(Value::Str(s)) if s == "yuv420p")
&& dimension("width", 1920)
&& dimension("height", 1080)
&& stream
.r_frame_rate()
.is_some_and(|(n, d)| d.checked_mul(30) == Some(n))
&& stream.r_frame_rate().is_some()
}

/// Tries the opt-in encoder without changing the command used on fallback.
Expand Down Expand Up @@ -160,8 +158,6 @@ mod tests {
("field_order", Value::Na),
("pix_fmt", Value::Str("yuv420p10le".into())),
("pix_fmt", Value::Str("yuv444p".into())),
("r_frame_rate", Value::Str("30000/1001".into())),
("r_frame_rate", Value::Str("3002/100".into())),
("r_frame_rate", Value::Str("0/0".into())),
] {
let mut changed = report.clone();
Expand All @@ -174,6 +170,17 @@ mod tests {
}
assert!(!source(&changed, 0), "{name}: {value:?}");
}
for rate in ["25/1", "24000/1001", "30000/1001", "1000/33"] {
let mut changed = report.clone();
if let Some(stream) = changed.streams.first_mut() {
for (name, field) in &mut stream.entries {
if *name == "r_frame_rate" {
*field = Value::Str(rate.into());
}
}
}
assert!(source(&changed, 0), "{rate}");
}
}

fn args(extra: &str) -> Vec<String> {
Expand Down
19 changes: 16 additions & 3 deletions crates/coreav-h264-encoder/examples/p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
decoded,
deblocking,
interval,
metadata @ ..,
] = args.as_slice()
else {
return Err(
"usage: p WIDTH HEIGHT QP_OR_crf:N FRAMES INPUT OUTPUT RECON DECODED DEBLOCKING INTERVAL".into(),
"usage: p WIDTH HEIGHT QP_OR_crf:N FRAMES INPUT OUTPUT RECON DECODED DEBLOCKING INTERVAL [FPS_NUM FPS_DEN SAR_NUM SAR_DEN]".into(),
);
};
let deblocking = if deblocking.parse::<bool>()? {
Expand All @@ -35,9 +36,21 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
let width = width.parse::<usize>()?;
let height = height.parse::<usize>()?;
let frames = frames.parse::<u64>()?;
let (frame_rate, sample_aspect_ratio) = match metadata {
[] => (Default::default(), Default::default()),
[num, den, sar_num, sar_den] => (
coreav_h264_encoder::FrameRate::new(num.parse()?, den.parse()?)?,
if sar_num == "0" && sar_den == "1" {
Default::default()
} else {
coreav_h264_encoder::SampleAspectRatio::new(sar_num.parse()?, sar_den.parse()?)?
},
),
_ => return Err("expected four optional timing and aspect terms".into()),
};
let settings = Settings {
frame_rate: Default::default(),
sample_aspect_ratio: Default::default(),
frame_rate,
sample_aspect_ratio,
width,
height,
rate_mode: match qp.strip_prefix("crf:") {
Expand Down
116 changes: 109 additions & 7 deletions crates/coreav-transcode/src/native_h264.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Bounded native H.264 access units for the existing transcode spool and muxers.

use coreav_frame::{ChromaLocation, ColorInfo, PixelFormat, Pixels};
use coreav_h264_encoder::{Colour, Deblocking, Gop, Limits, Plane, Settings};
use coreav_h264_encoder::{Colour, Deblocking, Gop, Limits, Plane, SampleAspectRatio, Settings};

use crate::Error;
use crate::encoder::{EncoderSettings, Sink};
Expand All @@ -25,6 +25,24 @@ pub(crate) fn color(color: ColorInfo) -> ColorInfo {
}
}

pub(crate) fn aspect(value: Option<(u64, u64)>) -> Result<SampleAspectRatio, Error> {
let Some((mut n, mut d)) = value.filter(|(n, d)| *n > 0 && *d > 0 && n != d) else {
return Ok(SampleAspectRatio::default());
};
let (mut a, mut b) = (n, d);
while b != 0 {
(a, b) = (b, a % b);
}
n /= a;
d /= a;
let invalid = || Error::NativeIneligible("the sample aspect does not fit native H.264");
SampleAspectRatio::new(
u32::try_from(n).map_err(|_| invalid())?,
u32::try_from(d).map_err(|_| invalid())?,
)
.map_err(ineligible)
}

pub(crate) struct Encoder<S> {
encoder: coreav_h264_encoder::Encoder,
sink: S,
Expand All @@ -39,17 +57,23 @@ impl<S: Sink> Encoder<S> {
max_bytes: u64,
sink: S,
) -> Result<Self, Error> {
if settings.format != PixelFormat::YUV420P
|| settings.sample_aspect.is_some_and(|(n, d)| n != d)
{
if settings.format != PixelFormat::YUV420P {
return Err(Error::NativeIneligible(
"native H.264 needs square 8-bit 4:2:0 pixels",
"native H.264 needs 8-bit 4:2:0 pixels",
));
}
let c = settings.color;
let settings_native = Settings {
frame_rate: Default::default(),
sample_aspect_ratio: Default::default(),
frame_rate: coreav_h264_encoder::FrameRate::new(
settings.frame_rate.num,
settings.frame_rate.den,
)
.map_err(ineligible)?,
sample_aspect_ratio: aspect(
settings
.sample_aspect
.map(|(n, d)| (u64::from(n), u64::from(d))),
)?,
width: settings.width as usize,
height: settings.height as usize,
rate_mode: request.rate,
Expand Down Expand Up @@ -234,9 +258,87 @@ mod tests {
sample_aspect: Some((4, 3)),
..settings(1)
};
assert!(Encoder::new(&settings, request, 1_000_000, Bytes::default()).is_ok());
let settings = EncoderSettings {
sample_aspect: Some((65536, 65535)),
..settings
};
assert!(matches!(
Encoder::new(&settings, request, 1_000_000, Bytes::default()),
Err(Error::NativeIneligible(_))
));
}

#[test]
fn sample_aspect_is_exact_and_preserves_unspecified_and_square_bytes() {
for value in [
None,
Some((0, 1)),
Some((1, 0)),
Some((1, 1)),
Some((u64::MAX, u64::MAX)),
] {
assert_eq!(aspect(value).unwrap(), SampleAspectRatio::default());
}
assert_eq!(
aspect(Some((16 * 1_000_000_000, 15 * 1_000_000_000))).unwrap(),
SampleAspectRatio::new(16, 15).unwrap()
);
assert!(aspect(Some((u64::MAX, u64::MAX - 1))).is_err());
assert!(aspect(Some((65536, 65535))).is_err());
let mut previous = None;
for sample_aspect in [None, Some((0, 1)), Some((1, 1)), Some((16, 16))] {
let settings = EncoderSettings {
sample_aspect,
..settings(1)
};
let mut encoder = Encoder::new(
&settings,
NativeH264 {
rate: RateMode::Crf(23),
},
1_000_000,
Bytes::default(),
)
.unwrap();
encoder.send_frame(Pixels::Cpu(picture())).unwrap();
let bytes = encoder.finish().0;
if let Some(previous) = &previous {
assert_eq!(&bytes, previous);
}
previous = Some(bytes);
}
}

#[test]
fn fractional_rate_and_declared_aspect_are_thread_independent() {
let mut previous = None;
for threads in [0, 1, 2, 8] {
let settings = EncoderSettings {
frame_rate: FrameRate {
num: 30000,
den: 1001,
},
sample_aspect: Some((16, 15)),
..settings(threads)
};
let mut encoder = Encoder::new(
&settings,
NativeH264 {
rate: RateMode::FixedQp(28),
},
1_000_000,
Bytes::default(),
)
.unwrap();
for _ in 0..3 {
encoder.send_frame(Pixels::Cpu(picture())).unwrap();
}
let bytes = encoder.finish().0;
if let Some(previous) = &previous {
assert_eq!(&bytes, previous);
}
previous = Some(bytes);
}
}
}
Loading
Loading