forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

vp9.rs197 lines, 7.8 KiBHistoryRawDownload
1//! The VP9 software seat: one span of `ec-vp9` through the same [`Render`] door
2//! every other seat's pictures go through, and the same way a refusal is named
3//! on stderr. Export reads the same [`Stream`] to decode its sources.
4//!
5//! `ec-vp9` takes one demuxed block (a frame or a superframe) at a time, which
6//! is exactly what the demuxer hands out, so unlike AV1 there is no chunking:
7//! every access unit goes straight into the decoder. A hidden frame comes back
8//! as `None` and takes no display slot, and a shown one (including a
9//! `show_existing_frame`) comes back as the picture it displays. VP9 has no
10//! reorder delay, so nothing is held back at the end of the stream.
11
12use std::sync::mpsc::SyncSender;
13
14use ec_vp9::decode::{Decoder, Picture};
15
16use super::{Abort, DecodeFailure, Frame, I420, LATE_RUN, Render, skip_for_speed};
17use crate::demux::Demuxer;
18use crate::project::Speed;
19
20/// One VP9 decode, pulled a picture at a time: the decoder and the display
21/// index of the next shown picture.
22pub(crate) struct Stream {
23 decoder: Decoder,
24 /// Signed, for the same reason the AV1 span's is: the landing sync sample
25 /// can sit inside what an mp4's edit list trims, i.e. before frame 0.
26 index: i64,
27}
28
29impl Stream {
30 /// A decoder positioned at the sync sample at or before `start_frame`.
31 pub(crate) fn open(demuxer: &mut Demuxer, start_frame: u32) -> Self {
32 Self {
33 decoder: Decoder::new(),
34 index: demuxer.seek_to_sync_at_or_before(start_frame),
35 }
36 }
37
38 /// The next shown picture, narrowed into `out`, with its display index.
39 /// `None` at end of stream.
40 pub(crate) fn next(
41 &mut self,
42 demuxer: &mut Demuxer,
43 out: &mut I420,
44 ) -> Result<Option<i64>, String> {
45 loop {
46 let au = match demuxer.next_access_unit() {
47 Ok(Some(au)) => au,
48 // The demuxer is dry: every shown frame has already been returned.
49 Ok(None) => return Ok(None),
50 Err(e) => return Err(format!("demux error: {e}")),
51 };
52 let picture = match self.decoder.decode(&au) {
53 Ok(Some(picture)) => picture,
54 // A hidden frame: a reference, with no display slot of its own.
55 Ok(None) => continue,
56 Err(e) => {
57 return Err(format!(
58 "software VP9 decode failed at frame {}: {e}",
59 self.index
60 ));
61 }
62 };
63 if !carried(&picture) {
64 return Err(format!(
65 "software VP9 frame {} is {}-bit, and only 8 or 10 bits is carried",
66 self.index, picture.bit_depth,
67 ));
68 }
69 narrow(&picture, out);
70 let due = self.index;
71 self.index += 1;
72 return Ok(Some(due));
73 }
74 }
75}
76
77/// The VP9 software seat over `[start_frame, end_frame)`. A span decodes from
78/// the sync sample at or before `start_frame`; pictures before the target are
79/// decoded (the target references them) but never sent.
80///
81/// A refusal out of `ec-vp9`, or a picture outside what edith carries (anything
82/// but 8 or 10 bits), fails the span in words: named on stderr, the
83/// pictures decoded so far stand, and no garbage is sent.
84// The eight inputs every software span takes (`run_vp8` has the same shape); a
85// one-off struct would only ever be built and taken apart here.
86#[allow(clippy::too_many_arguments)]
87pub(super) fn run_vp9(
88 demuxer: &mut Demuxer,
89 tx: &SyncSender<Frame>,
90 start_frame: u32,
91 end_frame: u32,
92 render: &mut Render,
93 abort: &Abort,
94 speed: Speed,
95 failure: &DecodeFailure,
96) {
97 let mut stream = Stream::open(demuxer, start_frame);
98 let mut out = I420::default();
99 let mut skipped = 0;
100 while !abort.hit() {
101 let due = match stream.next(demuxer, &mut out) {
102 Ok(Some(due)) => due,
103 Ok(None) => break,
104 Err(e) => {
105 failure.set(e);
106 break;
107 }
108 };
109 if due < i64::from(start_frame) {
110 continue;
111 }
112 let due = due as u32;
113 // The three ends [`super::run`] reaches per picture: late for the
114 // playhead (one in [`LATE_RUN`] still goes through), redundant by
115 // construction at speed, or a conversion and a send.
116 let sent = if abort.late(due) {
117 if skipped < LATE_RUN {
118 skipped += 1;
119 false
120 } else {
121 skipped = 0;
122 send(&out, due, render, tx)
123 }
124 } else if skip_for_speed(speed, start_frame, due, end_frame) {
125 // Redundant by construction, not by lateness (see `skip_for_speed`).
126 false
127 } else {
128 skipped = 0;
129 send(&out, due, render, tx)
130 };
131 // The consumer went away, or the range is out: nothing left to decode for.
132 if sent || i64::from(due) + 1 >= i64::from(end_frame) {
133 break;
134 }
135 }
136}
137
138/// Whether edith carries this picture's bit depth: 8 or 10 bits. Profile 1 and 3
139/// (4:4:4, 4:2:2, 4:4:0) pictures are carried too, their chroma reduced to 4:2:0 by
140/// [`narrow`]; 12-bit is refused at open by the demuxer (`vp9_bit_depth`), and this
141/// is the same line drawn on the decoded picture.
142fn carried(picture: &Picture) -> bool {
143 matches!(picture.bit_depth, 8 | 10)
144}
145
146/// One [`Picture`] down to the tightly packed 8-bit I420 every converter here
147/// takes, by the same rule [`super::narrow_av1`] applies: a 10-bit sample
148/// downshifts by two, the truncation the hardware seat's P010 read-back does.
149/// Chroma that is not 4:2:0 is reduced to it by averaging the source samples each
150/// output sample covers (a 2x2 box where the source is subsampled less, so a 4:4:4
151/// chroma plane averages 2x2 and a 4:2:2 one averages 1x2 vertically).
152fn narrow(picture: &Picture, out: &mut I420) {
153 let shift = picture.bit_depth.saturating_sub(8);
154 let plane = |src: &[u16], dst: &mut Vec<u8>| {
155 dst.clear();
156 dst.extend(src.iter().map(|&sample| (sample >> shift) as u8));
157 };
158 let (w, h) = (usize::from(picture.width), usize::from(picture.height));
159 plane(&picture.y[..w * h], &mut out.y);
160 let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
161 let (ssx, ssy) = (usize::from(picture.subsampling_x), usize::from(picture.subsampling_y));
162 if (ssx, ssy) == (1, 1) {
163 plane(&picture.u, &mut out.u);
164 plane(&picture.v, &mut out.v);
165 } else {
166 let stride = picture.uv_stride;
167 let (sw, sh) = ((w + ssx) >> ssx, (h + ssy) >> ssy);
168 let reduce = |src: &[u16], dst: &mut Vec<u8>| {
169 dst.clear();
170 for oy in 0..ch {
171 let (y0, y1) = if ssy == 0 { (2 * oy, (2 * oy + 2).min(sh)) } else { (oy, oy + 1) };
172 for ox in 0..cw {
173 let (x0, x1) = if ssx == 0 { (2 * ox, (2 * ox + 2).min(sw)) } else { (ox, ox + 1) };
174 let (mut sum, mut n) = (0u32, 0u32);
175 for y in y0..y1 {
176 for x in x0..x1 {
177 sum += u32::from(src[y * stride + x]);
178 n += 1;
179 }
180 }
181 dst.push(((sum / n) >> shift) as u8);
182 }
183 }
184 };
185 reduce(&picture.u, &mut out.u);
186 reduce(&picture.v, &mut out.v);
187 }
188 out.width = u32::from(picture.width);
189 out.height = u32::from(picture.height);
190}
191
192/// Queues one narrowed picture through the shared [`Render`]. `true` when the
193/// consumer went away, so the span stops rather than keeps decoding into a void.
194fn send(out: &I420, due: u32, render: &mut Render, tx: &SyncSender<Frame>) -> bool {
195 let frame = render.frame(due, &out.y, &out.u, &out.v, out.width, out.height);
196 tx.send(frame).is_err()
197}