GitCorner

Code

ossartcraftfilmcraft๐ŸŒ
f67d38b35cc5
use super::convert::{
    Matrix, Planes444, covers, rgba_to_yuv420_8, rgba_to_yuv420_10, rgba_to_yuv422_10, rgbf_to_yuv420_8, rgbf_to_yuv420_10, rgbf_to_yuv422_10, to_yuv444_10,
};
use super::format::{ColorSignal, DEFAULT_CRF, ExportSettings, Format, H264Pass};
use super::registry::{AudioEncoder, EncodedPacket, EncoderFrame, PrepareAhead, VideoEncoder};
use super::stats::{Stage, timed};
use super::{BitrateMode, ExportError, FrameRate, H264Profile, Result, SampleEntry, SampleFormat};
use std::sync::{Arc, Mutex, PoisonError};

#[path = "mjpeg.rs"]
mod mjpeg;

struct MjpegEncoder {
    w: u16,
    h: u16,
    quality: u8,
    rate: FrameRate,
}

impl VideoEncoder for MjpegEncoder {
    fn sample_entry(&self) -> SampleEntry {
        SampleEntry::jpeg(self.w, self.h)
    }
    fn timescale(&self) -> u32 {
        self.rate.num as u32
    }
    fn encode(&mut self, f: &EncoderFrame) -> Result<Vec<EncodedPacket>> {
        let out = mjpeg::encode(f.rgba, f.width, f.height, self.quality).map_err(ExportError::Encode)?;
        Ok(vec![EncodedPacket { data: out, key: true, duration: self.rate.den as u32, composition_offset: 0 }])
    }
    fn flush(&mut self) -> Result<Vec<EncodedPacket>> {
        Ok(Vec::new())
    }
}

pub(crate) fn mjpeg_factory(format: Format, w: u32, h: u32, rate: FrameRate, s: &ExportSettings) -> Option<Result<Box<dyn VideoEncoder>>> {
    (format == Format::Mjpeg).then(|| Ok(Box::new(MjpegEncoder { w: w as u16, h: h as u16, quality: s.quality.clamp(1, 100), rate }) as Box<dyn VideoEncoder>))
}

/// AAC-LC (our encoder), 320 kbps stereo by default.
struct AacEncoder {
    enc: craft_audio::aac::Encoder,
    rate: u32,
    channels: u32,
}

impl AudioEncoder for AacEncoder {
    fn sample_entry(&self) -> SampleEntry {
        SampleEntry::aac(self.enc.audio_specific_config(), self.channels, self.rate)
    }
    fn encode(&mut self, planar: &[Vec<f32>]) -> Result<Vec<Vec<u8>>> {
        let refs: Vec<&[f32]> = if planar.len() == 6 {
            // ours: L, R, C, LFE, Ls, Rs โ†’ AAC channel configuration 6: C, L, R, Ls, Rs, LFE
            [2usize, 0, 1, 4, 5, 3].iter().map(|&c| planar[c].as_slice()).collect()
        } else {
            planar.iter().map(Vec::as_slice).collect()
        };
        Ok(self.enc.encode(&refs))
    }
    fn flush(&mut self) -> Result<Vec<Vec<u8>>> {
        Ok(self.enc.flush())
    }
    fn priming(&self) -> u32 {
        self.enc.priming_samples()
    }
    fn frame_size(&self) -> u32 {
        1024
    }
}

/// FLAC (our encoder) in MP4 / QuickTime: one frame of 4096 samples per access unit.
pub(crate) struct FlacAudio {
    enc: craft_audio::flac::Encoder,
    format: SampleFormat,
    /// Samples of the frame `flush` returns (shorter than a full block).
    tail: u32,
}

impl FlacAudio {
    pub(crate) fn new(sample_rate: u32, channels: u32, s: &ExportSettings) -> Result<Self> {
        let format = s.audio.sample_format();
        let ch = u8::try_from(channels).map_err(|_| ExportError::Unsupported("FLAC: too many audio channels".into()))?;
        let mut cfg = craft_audio::flac::EncoderConfig::new(sample_rate, ch, format.bits() as u8);
        cfg.level = s.audio.flac_level.min(8);
        let enc = craft_audio::flac::Encoder::new(cfg).map_err(|e| ExportError::Unsupported(format!("FLAC: {e}")))?;
        Ok(FlacAudio { enc, format, tail: 0 })
    }
}

impl AudioEncoder for FlacAudio {
    fn sample_entry(&self) -> SampleEntry {
        SampleEntry::flac(&self.enc.streaminfo())
    }
    fn encode(&mut self, planar: &[Vec<f32>]) -> Result<Vec<Vec<u8>>> {
        let ints: Vec<Vec<i32>> = planar.iter().map(|c| c.iter().map(|&s| self.format.quantise(s)).collect()).collect();
        let refs: Vec<&[i32]> = ints.iter().map(Vec::as_slice).collect();
        self.enc.encode_frames(&refs).map_err(|e| ExportError::Encode(format!("FLAC: {e}")))
    }
    fn flush(&mut self) -> Result<Vec<Vec<u8>>> {
        Ok(self.flush_timed()?.into_iter().map(|(au, _)| au).collect())
    }
    fn flush_timed(&mut self) -> Result<Vec<(Vec<u8>, u32)>> {
        self.tail = self.enc.pending_samples() as u32;
        let last = self.enc.finish();
        Ok(if last.is_empty() { Vec::new() } else { vec![(last, self.tail)] })
    }
    fn priming(&self) -> u32 {
        0
    }
    fn frame_size(&self) -> u32 {
        4096
    }
    fn final_sample_entry(&self) -> Option<SampleEntry> {
        Some(SampleEntry::flac(&self.enc.streaminfo()))
    }
}

pub(crate) fn aac_factory(_format: Format, sample_rate: u32, channels: u32, s: &ExportSettings) -> Option<Result<Box<dyn AudioEncoder>>> {
    // AAC caps a frame at 6144 bits per channel (ISO/IEC 14496-3 ยง4.5.3.2): 6 bits per sample, so
    // 264.6 kbps for stereo at 22.05 kHz; a higher setting is capped instead of refused
    let max = (6 * sample_rate as u64 * channels.max(1) as u64).min(u32::MAX as u64) as u32;
    let bps = (s.audio.bitrate_kbps.clamp(32, 512) * 1000).min(max);
    Some(
        craft_audio::aac::Encoder::new(craft_audio::aac::EncoderConfig::cbr(sample_rate, channels as usize, bps))
            .map(|enc| Box::new(AacEncoder { enc, rate: sample_rate, channels }) as Box<dyn AudioEncoder>)
            .map_err(|e| ExportError::Encode(e.to_string())),
    )
}

/// ProRes 422 encoder (HQ unless the settings pick another flavour): sRGB/709 RGBA8 โ†’ 10-bit limited-range BT.709 4:2:2.
struct ProResEncoder {
    enc: craft_video::prores::Encoder,
    profile: craft_video::prores::Profile,
    /// Code the frames' alpha channel (4444 profiles with Include Alpha Channel).
    alpha: bool,
    w: u32,
    h: u32,
    rate: FrameRate,
    signal: ColorSignal,
}

impl VideoEncoder for ProResEncoder {
    fn sample_entry(&self) -> SampleEntry {
        let mut e = SampleEntry::prores(filmcraft_isobmff::FourCc(self.profile.fourcc()), self.w as u16, self.h as u16);
        self.signal.apply_to(&mut e, true);
        e
    }
    fn timescale(&self) -> u32 {
        self.rate.num as u32
    }
    fn encode(&mut self, f: &EncoderFrame) -> Result<Vec<EncodedPacket>> {
        let chroma = self.profile.chroma();
        let mut fr = craft_video::prores::Frame::new(f.width, f.height, chroma, 10, self.alpha).map_err(|e| ExportError::Encode(e.to_string()))?;
        let (w, h) = (f.width as usize, f.height as usize);
        timed(Stage::Convert, || match (chroma, f.hdr) {
            (craft_video::prores::ChromaFormat::Yuv444, hdr) => {
                let (kr, kb) = if hdr.is_some() { self.signal.kr_kb() } else { (0.2126, 0.0722) };
                to_yuv444_10(f.rgba, hdr, w, h, Matrix::new(kr, kb), Planes444 { y: &mut fr.y, cb: &mut fr.cb, cr: &mut fr.cr, alpha: fr.alpha.as_deref_mut() })
            }
            (_, Some(rgb)) => {
                let (kr, kb) = self.signal.kr_kb();
                rgbf_to_yuv422_10(rgb, w, h, Matrix::new(kr, kb), &mut fr.y, &mut fr.cb, &mut fr.cr)
            }
            (_, None) => rgba_to_yuv422_10(f.rgba, w, h, &mut fr.y, &mut fr.cb, &mut fr.cr),
        });
        let data = self.enc.encode(&fr).map_err(|e| ExportError::Encode(e.to_string()))?;
        Ok(vec![EncodedPacket { data, key: true, duration: self.rate.den as u32, composition_offset: 0 }])
    }
    fn flush(&mut self) -> Result<Vec<EncodedPacket>> {
        Ok(Vec::new())
    }
}

/// The ProRes profile named by [`ExportSettings::prores_profile`].
pub fn prores_profile(name: &str) -> craft_video::prores::Profile {
    use craft_video::prores::Profile;
    match name.to_ascii_lowercase().as_str() {
        "proxy" => Profile::Proxy,
        "lt" => Profile::Lt,
        "standard" | "422" => Profile::Standard,
        "4444" => Profile::P4444,
        "4444xq" | "4444 xq" | "4444-xq" | "xq" => Profile::P4444Xq,
        _ => Profile::Hq,
    }
}

pub(crate) fn prores_factory(format: Format, w: u32, h: u32, rate: FrameRate, s: &ExportSettings) -> Option<Result<Box<dyn VideoEncoder>>> {
    (format == Format::ProRes).then(|| {
        let profile = prores_profile(&s.prores_profile);
        let mut cfg = craft_video::prores::EncoderConfig::new(profile, w, h);
        if s.signal.is_hdr() {
            cfg.color = craft_video::prores::ColorInfo { primaries: s.signal.primaries, transfer: s.signal.transfer, matrix: s.signal.matrix };
        }
        let alpha = s.keeps_alpha();
        cfg.encode_alpha = alpha;
        let enc = craft_video::prores::Encoder::with_config(cfg);
        Ok(Box::new(ProResEncoder { enc, profile, alpha, w, h, rate, signal: s.signal }) as Box<dyn VideoEncoder>)
    })
}

/// The DNxHR profile named by [`ExportSettings::dnx_profile`].
pub fn dnx_profile(name: &str) -> craft_video::dnx::Profile {
    use craft_video::dnx::Profile;
    match name.to_ascii_lowercase().as_str() {
        "lb" => Profile::Lb,
        "sq" => Profile::Sq,
        "hqx" => Profile::Hqx,
        _ => Profile::Hq,
    }
}

/// DNxHR encoder (RGBA8 or HDR floats โ†’ BT.709 / BT.2020 limited-range 4:2:2; 8-bit for
/// LB/SQ/HQ, 10-bit for HQX).
struct DnxEncoder {
    enc: craft_video::dnx::Encoder,
    w: u32,
    h: u32,
    rate: FrameRate,
    signal: ColorSignal,
}

impl VideoEncoder for DnxEncoder {
    fn sample_entry(&self) -> SampleEntry {
        let mut e = SampleEntry::dnx(filmcraft_isobmff::FourCc(*b"AVdh"), self.w as u16, self.h as u16);
        self.signal.apply_to(&mut e, true);
        e
    }
    fn timescale(&self) -> u32 {
        self.rate.num as u32
    }
    fn encode(&mut self, f: &EncoderFrame) -> Result<Vec<EncodedPacket>> {
        let mut fr = craft_video::dnx::Frame::new(f.width, f.height, craft_video::dnx::ChromaFormat::Yuv422, 10, false);
        match f.hdr {
            Some(rgb) => {
                let (kr, kb) = self.signal.kr_kb();
                rgbf_to_yuv422_10(rgb, f.width as usize, f.height as usize, Matrix::new(kr, kb), &mut fr.y, &mut fr.cb, &mut fr.cr)
            }
            None => rgba_to_yuv422_10(f.rgba, f.width as usize, f.height as usize, &mut fr.y, &mut fr.cb, &mut fr.cr),
        }
        // the encoder rescales 10-bit input to its coded depth
        let data = self.enc.encode(&fr).map_err(|e| ExportError::Encode(e.to_string()))?;
        Ok(vec![EncodedPacket { data, key: true, duration: self.rate.den as u32, composition_offset: 0 }])
    }
    fn flush(&mut self) -> Result<Vec<EncodedPacket>> {
        Ok(Vec::new())
    }
}

pub(crate) fn dnx_factory(format: Format, w: u32, h: u32, rate: FrameRate, s: &ExportSettings) -> Option<Result<Box<dyn VideoEncoder>>> {
    (format == Format::DnxHr).then(|| {
        let mut cfg = craft_video::dnx::EncoderConfig::new(dnx_profile(&s.dnx_profile), w, h);
        if s.signal.primaries == 9 {
            cfg.color_volume = craft_video::dnx::ColorVolume::Bt2020Ncl;
        }
        let enc = craft_video::dnx::Encoder::with_config(cfg).map_err(|e| ExportError::Encode(e.to_string()))?;
        Ok(Box::new(DnxEncoder { enc, w, h, rate, signal: s.signal }) as Box<dyn VideoEncoder>)
    })
}

/// The APV profile named by [`ExportSettings::apv_profile`].
pub fn apv_profile(name: &str) -> craft_video::apv::Profile {
    use craft_video::apv::Profile;
    match name.to_ascii_lowercase().replace([' ', '_'], "-").trim_start_matches("apv-").trim_start_matches("apv") {
        "422-12" | "42212" => Profile::P422_12,
        "444-10" | "44410" => Profile::P444_10,
        "444-12" | "44412" => Profile::P444_12,
        "4444-10" | "444410" => Profile::P4444_10,
        "4444-12" | "444412" => Profile::P4444_12,
        "400-10" | "40010" => Profile::P400_10,
        _ => Profile::P422_10,
    }
}

/// APV encoder (RFC 9924: 10/12-bit 4:2:2 or 4:4:4 intra).
struct ApvEncoder {
    enc: craft_video::apv::Encoder,
    chroma: craft_video::apv::ChromaFormat,
    w: u32,
    h: u32,
    rate: FrameRate,
    signal: ColorSignal,
    /// Scratch chroma planes for the 4:0:0 luma conversion, reused across frames.
    cb422: Vec<u16>,
    cr422: Vec<u16>,
}

impl VideoEncoder for ApvEncoder {
    fn sample_entry(&self) -> SampleEntry {
        let apvc = filmcraft_isobmff::ApvConfig::parse(&self.enc.decoder_config_record()).unwrap_or_default();
        let mut e = SampleEntry::apv(apvc, self.w as u16, self.h as u16);
        self.signal.apply_to(&mut e, true);
        e
    }
    fn timescale(&self) -> u32 {
        self.rate.num as u32
    }
    /// Converts `f` into the profile's planes (4:4:4 at full chroma resolution, 4:2:2, or 4:0:0
    /// luma only), BT.709 for SDR and the signal's matrix for HDR, and codes one access unit.
    /// A picture buffer shorter than `width` x `height` is an error.
    fn encode(&mut self, f: &EncoderFrame) -> Result<Vec<EncodedPacket>> {
        use craft_video::apv::ChromaFormat as Cf;
        let (w, h) = (f.width as usize, f.height as usize);
        let short = |what: &str, got: usize| ExportError::Encode(format!("a {w}x{h} picture needs {what}, got {got}"));
        match f.hdr {
            Some(rgb) if !covers(rgb.len(), w, h, 3) => return Err(short("w*h*3 floats", rgb.len())),
            None if !covers(f.rgba.len(), w, h, 4) => return Err(short("w*h*4 bytes", f.rgba.len())),
            _ => {}
        }
        let mut fr = craft_video::apv::Frame::new(f.width, f.height, self.chroma, 10, self.chroma == Cf::Yuv4444);
        let (kr, kb) = if f.hdr.is_some() { self.signal.kr_kb() } else { (0.2126, 0.0722) };
        let chroma = self.chroma;
        let (cb422, cr422) = (&mut self.cb422, &mut self.cr422);
        timed(Stage::Convert, || match chroma {
            Cf::Yuv444 | Cf::Yuv4444 => to_yuv444_10(
                f.rgba,
                f.hdr,
                w,
                h,
                Matrix::new(kr, kb),
                Planes444 { y: &mut fr.y, cb: &mut fr.cb, cr: &mut fr.cr, alpha: fr.alpha.as_deref_mut() },
            ),
            Cf::Yuv422 => match f.hdr {
                Some(rgb) => rgbf_to_yuv422_10(rgb, w, h, Matrix::new(kr, kb), &mut fr.y, &mut fr.cb, &mut fr.cr),
                None => rgba_to_yuv422_10(f.rgba, w, h, &mut fr.y, &mut fr.cb, &mut fr.cr),
            },
            Cf::Monochrome => {
                // the 4:2:2 converters want chroma planes; only the luma is coded
                let n = w.div_ceil(2).saturating_mul(h);
                cb422.resize(n, 0);
                cr422.resize(n, 0);
                match f.hdr {
                    Some(rgb) => rgbf_to_yuv422_10(rgb, w, h, Matrix::new(kr, kb), &mut fr.y, cb422, cr422),
                    None => rgba_to_yuv422_10(f.rgba, w, h, &mut fr.y, cb422, cr422),
                }
            }
        });
        let data = self.enc.encode_raw_au(&fr).map_err(|e| ExportError::Encode(e.to_string()))?;
        Ok(vec![EncodedPacket { data, key: true, duration: self.rate.den as u32, composition_offset: 0 }])
    }
    fn flush(&mut self) -> Result<Vec<EncodedPacket>> {
        Ok(Vec::new())
    }
}

pub(crate) fn apv_factory(format: Format, w: u32, h: u32, rate: FrameRate, s: &ExportSettings) -> Option<Result<Box<dyn VideoEncoder>>> {
    (format == Format::Apv).then(|| {
        let profile = apv_profile(&s.apv_profile);
        let mut cfg = craft_video::apv::EncoderConfig::new(profile, w, h);
        if s.signal.is_hdr() {
            cfg.color = craft_video::apv::ColorInfo { primaries: s.signal.primaries, transfer: s.signal.transfer, matrix: s.signal.matrix, full_range: false };
        }
        let chroma = cfg.chroma;
        let enc = craft_video::apv::Encoder::with_config(cfg).map_err(|e| ExportError::Encode(e.to_string()))?;
        Ok(Box::new(ApvEncoder { enc, chroma, w, h, rate, signal: s.signal, cb422: Vec::new(), cr422: Vec::new() }) as Box<dyn VideoEncoder>)
    })
}

/// H.264 (shared encoder): sRGB/709 RGBA8 โ†’ limited-range BT.709 4:2:0, 8-bit, or 10-bit for High 10
/// (HDR frames from their float pixels), length-prefixed samples with the `avcC` in the sample entry.
struct H264Encoder {
    enc: craft_video::h264::Encoder,
    w: u32,
    h: u32,
    rate: FrameRate,
    y: Vec<u8>,
    u: Vec<u8>,
    v: Vec<u8>,
    /// The 10-bit planes of High 10.
    y16: Vec<u16>,
    u16: Vec<u16>,
    v16: Vec<u16>,
    signal: ColorSignal,
    /// 8-bit 4:2:0 planes the render threads converted ahead ([`VideoEncoder::prepare_ahead`]), by
    /// picture index.
    ready: Arc<Mutex<std::collections::HashMap<u64, Yuv420>>>,
}

type Yuv420 = (Vec<u8>, Vec<u8>, Vec<u8>);

/// The encoder input of `f`: 8-bit limited-range 4:2:0 in the export's colour signal.
fn h264_input(f: &EncoderFrame, signal: ColorSignal, y: &mut Vec<u8>, u: &mut Vec<u8>, v: &mut Vec<u8>) {
    match f.hdr {
        Some(rgb) => {
            let (kr, kb) = signal.kr_kb();
            rgbf_to_yuv420_8(rgb, f.width as usize, f.height as usize, Matrix::new(kr, kb), y, u, v)
        }
        None => rgba_to_yuv420_8(f.rgba, f.width as usize, f.height as usize, y, u, v),
    }
}

/// The High 10 encoder input of `f`: 10-bit limited-range 4:2:0 in the export's colour signal
/// (HDR frames from their float pixels).
fn h264_input10(f: &EncoderFrame, signal: ColorSignal, y: &mut Vec<u16>, u: &mut Vec<u16>, v: &mut Vec<u16>) -> Result<()> {
    let (w, h) = (f.width as usize, f.height as usize);
    match f.hdr {
        Some(rgb) => {
            let (kr, kb) = signal.kr_kb();
            rgbf_to_yuv420_10(rgb, w, h, Matrix::new(kr, kb), y, u, v)
        }
        None => rgba_to_yuv420_10(f.rgba, w, h, y, u, v),
    }
}

impl H264Encoder {
    fn deep(&self) -> bool {
        self.enc.config().bit_depth > 8
    }

    /// Encode `f` at 10 bits (High 10), converting it on the encoder's threads.
    fn encode10(&mut self, f: &EncoderFrame) -> Result<Vec<craft_video::h264::Packet>> {
        let (enc, y, u, v, signal) = (&self.enc, &mut self.y16, &mut self.u16, &mut self.v16, self.signal);
        timed(Stage::Convert, || enc.install(|| h264_input10(f, signal, y, u, v)))?;
        let cw = (f.width as usize).div_ceil(2);
        let frame = craft_video::h264::YuvFrame16 { y: &self.y16, u: &self.u16, v: &self.v16, y_stride: f.width as usize, uv_stride: cw };
        self.enc.encode16(&frame, f.index as i64 * self.rate.den).map_err(|e| ExportError::Encode(e.to_string()))
    }

    fn packets(&self, ps: Vec<craft_video::h264::Packet>) -> Vec<EncodedPacket> {
        ps.into_iter()
            .map(|p| EncodedPacket { data: p.data, key: p.keyframe, duration: self.rate.den as u32, composition_offset: (p.pts - p.dts) as i32 })
            .collect()
    }
}

impl VideoEncoder for H264Encoder {
    fn sample_entry(&self) -> SampleEntry {
        let cfg = filmcraft_isobmff::AvcConfig::parse(&self.enc.avcc()).unwrap_or_else(|_| {
            let (sps, pps) = self.enc.sps_pps();
            filmcraft_isobmff::AvcConfig::new(vec![sps], vec![pps], 4)
        });
        let mut e = SampleEntry::avc(cfg, self.w as u16, self.h as u16);
        self.signal.apply_to(&mut e, false);
        e
    }
    fn timescale(&self) -> u32 {
        self.rate.num as u32
    }
    fn encode(&mut self, f: &EncoderFrame) -> Result<Vec<EncodedPacket>> {
        if self.deep() {
            let ps = self.encode10(f)?;
            return Ok(self.packets(ps));
        }
        let ready = {
            let mut ready = self.ready.lock().unwrap_or_else(PoisonError::into_inner);
            let mine = ready.remove(&f.index);
            // anything older belongs to a batch that was rendered and then not encoded
            ready.retain(|&i, _| i > f.index);
            mine
        };
        let (w, h) = (f.width as usize, f.height as usize);
        let chroma = w.div_ceil(2) * h.div_ceil(2);
        match ready.filter(|(y, u, v)| y.len() == w * h && u.len() == chroma && v.len() == chroma) {
            Some((y, u, v)) => (self.y, self.u, self.v) = (y, u, v),
            None => {
                let (enc, y, u, v, signal) = (&self.enc, &mut self.y, &mut self.u, &mut self.v, self.signal);
                // on the encoder's own threads: the global pool is busy rendering the next batch
                timed(Stage::Convert, || enc.install(|| h264_input(f, signal, y, u, v)));
            }
        }
        let cw = (f.width as usize).div_ceil(2);
        let frame = craft_video::h264::YuvFrame { y: &self.y, u: &self.u, v: &self.v, y_stride: f.width as usize, uv_stride: cw };
        let ps = self.enc.try_encode(&frame, f.index as i64 * self.rate.den).map_err(|e| ExportError::Encode(e.to_string()))?;
        Ok(self.packets(ps))
    }
    fn flush(&mut self) -> Result<Vec<EncodedPacket>> {
        let ps = self.enc.flush();
        Ok(self.packets(ps))
    }
    fn media_start(&self) -> Option<i64> {
        // With B-frames the first DTS is one frame before the first PTS.
        (self.enc.delay() > 0).then_some(self.rate.den)
    }
    fn pass_stats(&self) -> Option<craft_video::h264::PassStats> {
        self.enc.pass_stats()
    }
    fn prepare_ahead(&self) -> Option<PrepareAhead> {
        // High 10 converts in `encode`: the planes prepared ahead are 8-bit
        if self.deep() {
            return None;
        }
        let (ready, signal) = (self.ready.clone(), self.signal);
        Some(Arc::new(move |f: &EncoderFrame| {
            let (mut y, mut u, mut v) = (Vec::new(), Vec::new(), Vec::new());
            timed(Stage::Convert, || h264_input(f, signal, &mut y, &mut u, &mut v));
            ready.lock().unwrap_or_else(PoisonError::into_inner).insert(f.index, (y, u, v));
        }))
    }
}

/// Slices per H.264 picture: one per four macroblock rows. The encoder's default follows the core
/// count, which would make the stream (and every decoded picture) depend on the machine; this is
/// what it picks on a machine with enough cores.
pub(crate) fn h264_slices(height: u32) -> usize {
    (height.div_ceil(16) as usize).div_ceil(4).max(1)
}

pub(crate) fn h264_factory(format: Format, w: u32, h: u32, rate: FrameRate, s: &ExportSettings) -> Option<Result<Box<dyn VideoEncoder>>> {
    if format != Format::H264 {
        return None;
    }
    let mut cfg = craft_video::h264::EncoderConfig::new(w, h, rate.num as u32, rate.den as u32);
    // Keep the existing FilmCraft identification SEI string instead of the shared crate default.
    cfg.writer = format!("filmcraft-h264enc {}", env!("CARGO_PKG_VERSION"));
    // MXF carries the Annex B byte stream (ST 381-3) with in-band parameter sets
    cfg.format = if s.format.is_mxf() { craft_video::h264::PacketFormat::AnnexB } else { craft_video::h264::PacketFormat::LengthPrefixed };
    cfg.aud = s.format.is_mxf();
    cfg.keyint = s.keyframe_distance.filter(|k| *k > 0).unwrap_or_else(|| (rate.num as f64 / rate.den as f64 * 2.0).round().max(1.0) as u32);
    cfg.slices = h264_slices(h);
    let kbps = s.bitrate_kbps.max(100);
    let max = s.max_bitrate_kbps.filter(|m| *m >= kbps).unwrap_or_else(|| (u64::from(kbps) * 3 / 2).min(u64::from(u32::MAX)) as u32);
    cfg.rate = match s.bitrate_mode {
        BitrateMode::Cbr => craft_video::h264::RateControl::Cbr { kbps },
        // the encoder clamps to 0..=51; a NaN from a hand-edited preset becomes the default
        BitrateMode::Crf => craft_video::h264::RateControl::Crf(if s.crf.is_finite() { s.crf } else { DEFAULT_CRF }),
        _ => craft_video::h264::RateControl::Vbr { target_kbps: kbps, max_kbps: max },
    };
    cfg.pass = match &s.h264_pass {
        H264Pass::Single => craft_video::h264::Pass::Single,
        H264Pass::First => craft_video::h264::Pass::First,
        H264Pass::Second(st) => craft_video::h264::Pass::Second(st.clone()),
    };
    cfg.profile = match s.h264_profile {
        H264Profile::Baseline => craft_video::h264::Profile::Baseline,
        H264Profile::Main => craft_video::h264::Profile::Main,
        H264Profile::High => craft_video::h264::Profile::High,
        H264Profile::High10 => craft_video::h264::Profile::High10,
    };
    cfg.bit_depth = s.h264_profile.bit_depth();
    cfg.intra_only = s.h264_intra_only;
    cfg.level = s.h264_level;
    if let Some((n, d)) = s.pixel_aspect {
        cfg.sar = (n.clamp(1, 65535) as u16, d.clamp(1, 65535) as u16);
    }
    if s.signal.is_hdr() {
        cfg.color = craft_video::h264::ColorConfig { primaries: s.signal.primaries, transfer: s.signal.transfer, matrix: s.signal.matrix, full_range: false };
        if let Some((md, cll)) = s.signal.static_metadata() {
            let mut b = [0u8; 24];
            b.copy_from_slice(&md.to_bytes());
            cfg.mastering_display = Some(b);
            cfg.content_light = Some(cll);
        }
    }
    Some(
        craft_video::h264::Encoder::new_with_motion_search_starts(cfg, craft_video::h264::MotionSearchStarts::SeedAndNeighbour)
            .map(|enc| {
                let (y, u, v, y16, u16, v16) = Default::default();
                Box::new(H264Encoder { enc, w, h, rate, y, u, v, y16, u16, v16, signal: s.signal, ready: Default::default() }) as Box<dyn VideoEncoder>
            })
            .map_err(|e| ExportError::Encode(e.to_string())),
    )
}

#[cfg(test)]
mod mjpeg_adapter_tests {
    use super::*;

    #[test]
    fn mjpeg_packet_timing_and_quality_clamp_stay_in_the_adapter() {
        for rate in [FrameRate::FPS_24, FrameRate::FPS_29_97] {
            for (requested, clamped) in [(0, 1), (75, 75), (255, 100)] {
                let settings = ExportSettings { quality: requested, ..Default::default() };
                let mut encoder = mjpeg_factory(Format::Mjpeg, 1, 1, rate, &settings).unwrap().unwrap();
                assert_eq!(encoder.timescale(), rate.num as u32);
                let rgba = [25, 80, 195, 123];
                for index in [0, 1, 99] {
                    let packets = encoder.encode(&EncoderFrame { width: 1, height: 1, rgba: &rgba, hdr: None, index }).unwrap();
                    assert_eq!(packets.len(), 1);
                    let packet = &packets[0];
                    assert!(packet.key);
                    assert_eq!(packet.duration, rate.den as u32);
                    assert_eq!(packet.composition_offset, 0);
                    assert_eq!(packet.data, mjpeg::encode(&rgba, 1, 1, clamped).unwrap());
                }
                assert!(encoder.flush().unwrap().is_empty());
            }
        }
    }
}