forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

encode_threads.rs198 lines, 7.4 KiBHistoryRawDownload
1//! The wavefront is deterministic: one clip encodes to the same bytes at 1, 2, 4
2//! and 6 worker threads, for both the intra-only stream (every picture an IDR) and
3//! the IDR+P stream, and the P stream decodes to the encoder's own reconstruction.
4//!
5//! Rows are dealt to workers round-robin, and each worker reads the row above only
6//! through the snapshots of finished CTBs, so the thread count changes the schedule
7//! and nothing else. A test that only checked one thread count would miss a read
8//! that reaches past what a worker has imported, so this compares several.
9
10use ec_core::frame::{PixelFormat, Plane, VideoFrame};
11use ec_h265::Decoder;
12use ec_h265::encoder::{Encoder, EncoderConfig, RateControl};
13
14/// A 320x240 clip of a textured pattern that moves a few samples per picture, so
15/// the P pictures find real motion and code both merge and AMVP units.
16fn moving_clip(frames: usize, w: usize, h: usize) -> Vec<VideoFrame> {
17 let (cw, ch) = (w / 2, h / 2);
18 (0..frames)
19 .map(|t| {
20 let mut y = vec![0u8; w * h];
21 for r in 0..h {
22 for c in 0..w {
23 let (x, yy) = (c + 3 * t, r + t);
24 // A ramp with a hashed texture, so no two blocks are alike.
25 let mut v = (x * 7 + yy * 5) as u32;
26 v ^= (x as u32).wrapping_mul(0x9e37) ^ (yy as u32).wrapping_mul(0x85eb);
27 y[r * w + c] = (v.wrapping_mul(2_654_435_761) >> 24) as u8;
28 }
29 }
30 let chroma = |scale: usize| -> Vec<u8> {
31 (0..cw * ch)
32 .map(|i| {
33 let (c, r) = (i % cw, i / cw);
34 ((c * scale + r * 3 + t * 2) % 251) as u8
35 })
36 .collect()
37 };
38 VideoFrame::try_new(
39 PixelFormat::I420,
40 w as u32,
41 h as u32,
42 vec![
43 Plane::new(y, w),
44 Plane::new(chroma(5), cw),
45 Plane::new(chroma(9), cw),
46 ],
47 )
48 .expect("I420 planes at the stated size")
49 })
50 .collect()
51}
52
53/// The access units of `frames` coded with `threads` workers at `keyint`, and the
54/// reconstruction of every picture.
55fn encode_at(frames: &[VideoFrame], keyint: usize, threads: usize) -> (Vec<u8>, Vec<u8>) {
56 let mut cfg = EncoderConfig::new(320, 240);
57 cfg.rate_control = RateControl::ConstantQp(30);
58 cfg.keyint = keyint;
59 cfg.keep_recon = true;
60 cfg.threads = threads;
61 let enc = Encoder::new(cfg).expect("320x240 encoder");
62 let coded = enc.encode_sequence(frames).expect("sequence encodes");
63 let mut stream = Vec::new();
64 let mut recon = Vec::new();
65 for pic in &coded {
66 stream.extend_from_slice(&pic.au);
67 if let Some(r) = &pic.recon {
68 for p in &r.planes {
69 recon.extend_from_slice(&p.data);
70 }
71 }
72 }
73 (stream, recon)
74}
75
76/// Encode `frames` at 1 thread, then at 2, 4 and 6 threads, and require the same
77/// access units and reconstruction every time.
78fn assert_identical_across_threads(frames: &[VideoFrame], keyint: usize, label: &str) {
79 let (one, recon_one) = encode_at(frames, keyint, 1);
80 for threads in [2, 4, 6] {
81 let (many, recon_many) = encode_at(frames, keyint, threads);
82 assert_eq!(
83 one.len(),
84 many.len(),
85 "{label}, {threads} threads: stream length differs from 1 thread"
86 );
87 assert!(
88 one == many,
89 "{label}, {threads} threads: stream bytes differ from 1 thread"
90 );
91 assert!(
92 recon_one == recon_many,
93 "{label}, {threads} threads: reconstruction differs from 1 thread"
94 );
95 }
96}
97
98#[test]
99fn wavefront_intra_pictures_are_identical_at_1_2_4_and_6_threads() {
100 // keyint 1: every picture is an IDR, so only the intra wavefront runs.
101 let frames = moving_clip(4, 320, 240);
102 assert_identical_across_threads(&frames, 1, "intra-only");
103}
104
105#[test]
106fn wavefront_p_pictures_are_identical_at_1_2_4_and_6_threads() {
107 // keyint 4: IDR pictures plus P pictures, so both wavefronts run.
108 let frames = moving_clip(9, 320, 240);
109 assert_identical_across_threads(&frames, 4, "IDR+P");
110}
111
112#[test]
113fn wavefront_p_stream_decodes_to_the_encoder_reconstruction() {
114 let frames = moving_clip(9, 320, 240);
115 let (stream, recon) = encode_at(&frames, 4, 4);
116 let mut dec = Decoder::new();
117 dec.decode_annex_b(&stream).expect("the stream decodes");
118 dec.flush().expect("the stream flushes");
119 let mut decoded = Vec::new();
120 while let Some(frame) = dec.next_frame() {
121 for plane in [&frame.picture.y, &frame.picture.cb, &frame.picture.cr] {
122 decoded.extend(plane.iter().map(|&v| v as u8));
123 }
124 }
125 assert_eq!(decoded.len(), recon.len(), "decoded picture count differs");
126 assert!(
127 decoded == recon,
128 "decoder output differs from the encoder reconstruction"
129 );
130}
131
132#[test]
133fn sequence_encoder_matches_encode_sequence_and_forces_idrs() {
134 let frames = moving_clip(9, 320, 240);
135 let (want, _) = encode_at(&frames, 4, 1);
136 let make = || {
137 let mut cfg = EncoderConfig::new(320, 240);
138 cfg.rate_control = RateControl::ConstantQp(30);
139 cfg.keyint = 4;
140 cfg.keep_recon = true;
141 cfg.threads = 1;
142 ec_h265::SequenceEncoder::new(Encoder::new(cfg).expect("encoder")).expect("sequence")
143 };
144 let mut seq = make();
145 let got: Vec<u8> = frames
146 .iter()
147 .flat_map(|f| seq.encode(f, false).expect("encode").au)
148 .collect();
149 assert!(
150 got == want,
151 "push-style stream differs from encode_sequence"
152 );
153 let mut seq = make();
154 let mut stream = Vec::new();
155 let mut recon = Vec::new();
156 for (t, f) in frames.iter().enumerate() {
157 let pic = seq.encode(f, t == 2).expect("encode");
158 stream.extend_from_slice(&pic.au);
159 for p in &pic.recon.expect("recon").planes {
160 recon.extend_from_slice(&p.data);
161 }
162 }
163 let mut dec = Decoder::new();
164 dec.decode_annex_b(&stream).expect("the stream decodes");
165 dec.flush().expect("the stream flushes");
166 let mut decoded = Vec::new();
167 while let Some(frame) = dec.next_frame() {
168 for plane in [&frame.picture.y, &frame.picture.cb, &frame.picture.cr] {
169 decoded.extend(plane.iter().map(|&v| v as u8));
170 }
171 }
172 assert!(
173 decoded == recon,
174 "forced-IDR stream does not decode to the reconstruction"
175 );
176}
177
178#[test]
179fn end_picture_outputs_each_access_unit_cropped() {
180 let frames = moving_clip(4, 200, 120);
181 let mut cfg = EncoderConfig::new(200, 120);
182 cfg.rate_control = RateControl::ConstantQp(30);
183 cfg.keyint = 4;
184 cfg.keep_recon = true;
185 cfg.threads = 1;
186 let mut seq = ec_h265::SequenceEncoder::new(Encoder::new(cfg).expect("encoder")).expect("sequence");
187 let mut dec = Decoder::new();
188 for f in &frames {
189 let pic = seq.encode(f, false).expect("encode");
190 dec.decode_annex_b(&pic.au).expect("decode");
191 dec.end_picture();
192 let out = dec.next_frame().expect("a frame per access unit").cropped();
193 assert_eq!((out.width, out.height), (200, 120));
194 let recon = pic.recon.expect("recon");
195 let y: Vec<u8> = out.y.iter().map(|&v| v as u8).collect();
196 assert!(*y == *recon.planes[0].data, "cropped luma differs from the reconstruction");
197 }
198}