forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

vp8.rs163 lines, 6.3 KiBHistoryRawDownload
1//! The VP8 software seat: one span of `ec-vp8` through the same loop
2//! [`super::run_vp9`] drives `ec-vp9` through, and the same door -- the
3//! [`Render`] every other seat's pictures go through, the same speed and
4//! lateness rules, the same way a refusal is named on stderr. Export reads the
5//! same [`Stream`] to decode its sources.
6//!
7//! `ec-vp8` takes one demuxed block (one complete frame) at a time, which is
8//! exactly what the demuxer hands out, so every access unit goes straight into
9//! the decoder. A shown frame comes back as the picture it displays. A hidden
10//! frame (an alt-ref or golden update that is not shown) comes back as `None`:
11//! unlike VP9's, a hidden VP8 frame is a block of its own in WebM and mp4, so it
12//! takes a container slot and sends nothing into it. VP8 has no reorder delay and
13//! no `show_existing_frame`, so nothing is held back at the end of the stream.
14//!
15//! Seeking has only one kind of landing point: a key frame. The demuxer's sync
16//! samples are exactly those, so a span starts at the key frame at or before
17//! the target, decodes through it, and sends nothing before the target.
18
19use std::sync::mpsc::SyncSender;
20
21use ec_vp8::decode::{Decoder, Picture};
22
23use super::{Abort, DecodeFailure, Frame, I420, LATE_RUN, Render, skip_for_speed};
24use crate::demux::Demuxer;
25use crate::project::Speed;
26
27/// One VP8 decode, pulled a picture at a time: the decoder and the display index
28/// of the next slot. A hidden frame still takes a slot, so the index moves past
29/// it without a picture.
30pub(crate) struct Stream {
31 decoder: Decoder,
32 /// Signed, for the same reason the other spans' is: the landing sync sample
33 /// can sit inside what an mp4's edit list trims, i.e. before frame 0.
34 index: i64,
35}
36
37impl Stream {
38 /// A decoder positioned at the sync sample at or before `start_frame`.
39 pub(crate) fn open(demuxer: &mut Demuxer, start_frame: u32) -> Self {
40 Self {
41 decoder: Decoder::new(),
42 index: demuxer.seek_to_sync_at_or_before(start_frame),
43 }
44 }
45
46 /// The next slot that shows a picture, copied into `out`, with its display
47 /// index. `None` at end of stream.
48 pub(crate) fn next(
49 &mut self,
50 demuxer: &mut Demuxer,
51 out: &mut I420,
52 ) -> Result<Option<i64>, String> {
53 loop {
54 let frame = match demuxer.next_access_unit() {
55 Ok(Some(frame)) => frame,
56 // The demuxer is dry: every shown frame has already been returned.
57 Ok(None) => return Ok(None),
58 Err(e) => return Err(format!("demux error: {e}")),
59 };
60 let picture = match self.decoder.decode(&frame) {
61 Ok(Some(picture)) => picture,
62 // A hidden frame: the sample still owns its slot on the container's
63 // timeline (the count `frame_count` and every seek index are made
64 // of), it just has no picture to show, so the slot is passed over.
65 Ok(None) => {
66 self.index += 1;
67 continue;
68 }
69 Err(e) => {
70 return Err(format!(
71 "software VP8 decode failed at frame {}: {e}",
72 self.index
73 ));
74 }
75 };
76 copy(&picture, out);
77 let due = self.index;
78 self.index += 1;
79 return Ok(Some(due));
80 }
81 }
82}
83
84/// The VP8 software seat over `[start_frame, end_frame)`. A span decodes from
85/// the key frame at or before `start_frame`; pictures before the target are
86/// decoded (the target references them) but never sent.
87///
88/// A refusal out of `ec-vp8` fails the span in words: named on stderr, the
89/// pictures decoded so far stand, and no garbage is sent.
90// The eight inputs every software span takes (`run_vp9` has the same shape); a
91// one-off struct would only ever be built and taken apart here.
92#[allow(clippy::too_many_arguments)]
93pub(super) fn run_vp8(
94 demuxer: &mut Demuxer,
95 tx: &SyncSender<Frame>,
96 start_frame: u32,
97 end_frame: u32,
98 render: &mut Render,
99 abort: &Abort,
100 speed: Speed,
101 failure: &DecodeFailure,
102) {
103 let mut stream = Stream::open(demuxer, start_frame);
104 let mut out = I420::default();
105 let mut skipped = 0;
106 while !abort.hit() {
107 let due = match stream.next(demuxer, &mut out) {
108 Ok(Some(due)) => due,
109 Ok(None) => break,
110 Err(e) => {
111 failure.set(e);
112 break;
113 }
114 };
115 if due < i64::from(start_frame) {
116 continue;
117 }
118 let due = due as u32;
119 // The three ends [`super::run`] reaches per picture: late for the
120 // playhead (one in [`LATE_RUN`] still goes through), redundant by
121 // construction at speed, or a send.
122 let sent = if abort.late(due) {
123 if skipped < LATE_RUN {
124 skipped += 1;
125 false
126 } else {
127 skipped = 0;
128 send(&out, due, render, tx)
129 }
130 } else if skip_for_speed(speed, start_frame, due, end_frame) {
131 // Redundant by construction, not by lateness (see `skip_for_speed`).
132 false
133 } else {
134 skipped = 0;
135 send(&out, due, render, tx)
136 };
137 // The consumer went away, or the range is out: nothing left to decode for.
138 if sent || i64::from(due) + 1 >= i64::from(end_frame) {
139 break;
140 }
141 }
142}
143
144/// `ec-vp8` hands back 8-bit 4:2:0 planes already cropped to the frame, which
145/// is the tightly packed I420 every converter here takes -- no narrowing, only
146/// the copy that lets the planes outlive the decoder's buffer.
147fn copy(picture: &Picture, out: &mut I420) {
148 out.width = u32::from(picture.width);
149 out.height = u32::from(picture.height);
150 out.y.clear();
151 out.y.extend_from_slice(&picture.y);
152 out.u.clear();
153 out.u.extend_from_slice(&picture.u);
154 out.v.clear();
155 out.v.extend_from_slice(&picture.v);
156}
157
158/// Queues one picture through the shared [`Render`]. `true` when the consumer
159/// went away, so the span stops rather than keeps decoding into a void.
160fn send(out: &I420, due: u32, render: &mut Render, tx: &SyncSender<Frame>) -> bool {
161 let frame = render.frame(due, &out.y, &out.u, &out.v, out.width, out.height);
162 tx.send(frame).is_err()
163}