Debian Codegraph @

.cargo/vendor/rav1e/src/api/channel/mod.rs

raw
1 // Copyright (c) 2018-2022, The rav1e contributors. All rights reserved
2 //
3 // This source code is subject to the terms of the BSD 2 Clause License and
4 // the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
5 // was not distributed with this source code in the LICENSE file, you can
6 // obtain it at www.aomedia.org/license/software. If the Alliance for Open
7 // Media Patent License 1.0 was not distributed with this source code in the
8 // PATENTS file, you can obtain it at www.aomedia.org/license/patent.
9 #![allow(missing_docs)]
10
11 use crate::api::config::*;
12 use crate::api::context::RcData;
13 use crate::api::internal::ContextInner;
14 use crate::api::util::*;
15
16 use crossbeam::channel::*;
17
18 use crate::rate::RCState;
19 use crate::util::Pixel;
20
21 use rayon::ThreadPool;
22 use std::sync::Arc;
23
24 mod data;
25 pub use data::{
26 FrameInput, FrameSender, PacketReceiver, PassDataChannel, RcDataReceiver,
27 RcDataSender, RecvError, SendError, TryRecvError, TrySendError,
28 VideoDataChannel,
29 };
30
31 mod by_gop;
32
33 impl Config {
34 pub(crate) fn setup<T: Pixel>(
35 &self,
36 ) -> Result<(ContextInner<T>, Option<Arc<ThreadPool>>), InvalidConfig> {
37 self.validate()?;
38 let inner = self.new_inner()?;
39
40 let pool = self.new_thread_pool();
41
42 Ok((inner, pool))
43 }
44 }
45
46 impl Config {
47 /// Create a single pass encoder channel
48 ///
49 /// Drop the `FrameSender<T>` endpoint to flush the encoder.
50 ///
51 /// # Errors
52 ///
53 /// - Returns `InvalidConfig` if the configuration is invalid.
54 pub fn new_channel<T: Pixel>(
55 &self,
56 ) -> Result<VideoDataChannel<T>, InvalidConfig> {
57 let rc = &self.rate_control;
58
59 if rc.emit_pass_data || rc.summary.is_some() {
60 return Err(InvalidConfig::RateControlConfigurationMismatch);
61 }
62 let v = if self.slots > 1 {
63 self.new_by_gop_channel(self.slots)?
64 } else {
65 self.new_channel_internal()?.0
66 };
67
68 Ok(v)
69 }
70
71 /// Create a first pass encoder channel
72 ///
73 /// The pass data information is emitted through this channel.
74 ///
75 /// Drop the `FrameSender<T>` endpoint to flush the encoder.
76 /// The last buffer in the `PassDataReceiver` is the summary of the whole
77 /// encoding process.
78 ///
79 /// # Errors
80 ///
81 /// - Returns `InvalidConfig` if the configuration is invalid.
82 ///
83 /// # Panics
84 ///
85 /// - If the channel cannot be created. An error should be raised before this,
86 /// so a panic indicates a development error.
87 pub fn new_firstpass_channel<T: Pixel>(
88 &self,
89 ) -> Result<(VideoDataChannel<T>, RcDataReceiver), InvalidConfig> {
90 let rc = &self.rate_control;
91
92 if !rc.emit_pass_data {
93 return Err(InvalidConfig::RateControlConfigurationMismatch);
94 }
95
96 if self.slots > 1 {
97 log::warn!(
98 "Parallel gop encoding does not support multi pass rate control"
99 );
100 }
101
102 let (v, (_, r)) = self.new_channel_internal()?;
103
104 Ok((v, r.unwrap()))
105 }
106
107 /// Create a second pass encoder channel
108 ///
109 /// The encoding process require both frames and pass data to progress.
110 ///
111 /// Drop the `FrameSender<T>` endpoint to flush the encoder.
112 ///
113 /// # Errors
114 ///
115 /// - Returns `InvalidConfig` if the configuration is invalid.
116 ///
117 /// # Panics
118 ///
119 /// - If the channel cannot be created. An error should be raised before this,
120 /// so a panic indicates a development error.
121 pub fn new_secondpass_channel<T: Pixel>(
122 &self,
123 ) -> Result<(VideoDataChannel<T>, RcDataSender), InvalidConfig> {
124 let rc = &self.rate_control;
125 if rc.emit_pass_data || rc.summary.is_none() {
126 return Err(InvalidConfig::RateControlConfigurationMismatch);
127 }
128
129 if self.slots > 1 {
130 log::warn!(
131 "Parallel gop encoding does not support multi pass rate control"
132 );
133 }
134
135 let (v, (s, _)) = self.new_channel_internal()?;
136
137 Ok((v, s.unwrap()))
138 }
139
140 /// Create a multipass encoder channel
141 ///
142 /// The `PacketReceiver<T>` may block if not enough pass statistics data
143 /// are sent through the `PassDataSender` endpoint
144 ///
145 /// Drop the `FrameSender<T>` endpoint to flush the encoder.
146 /// The last buffer in the `PassDataReceiver` is the summary of the whole
147 /// encoding process.
148 ///
149 /// # Errors
150 ///
151 /// - Returns `InvalidConfig` if the configuration is invalid.
152 ///
153 /// # Panics
154 ///
155 /// - If the channel cannot be created. An error should be raised before this,
156 /// so a panic indicates a development error.
157 pub fn new_multipass_channel<T: Pixel>(
158 &self,
159 ) -> Result<(VideoDataChannel<T>, PassDataChannel), InvalidConfig> {
160 let rc = &self.rate_control;
161 if rc.summary.is_none() || !rc.emit_pass_data {
162 return Err(InvalidConfig::RateControlConfigurationMismatch);
163 }
164
165 if self.slots > 1 {
166 log::warn!(
167 "Parallel gop encoding does not support multi pass rate control"
168 );
169 }
170
171 let (v, (s, r)) = self.new_channel_internal()?;
172
173 Ok((v, (s.unwrap(), r.unwrap())))
174 }
175 }
176
177 trait RcFirstPass {
178 fn send_pass_data(&mut self, rc_state: &mut RCState);
179 fn send_pass_summary(&mut self, rc_state: &mut RCState);
180 }
181
182 trait RcSecondPass {
183 fn feed_pass_data<T: Pixel>(
184 &mut self, inner: &mut ContextInner<T>,
185 ) -> Result<(), ()>;
186 }
187
188 impl RcFirstPass for Sender<RcData> {
189 fn send_pass_data(&mut self, rc_state: &mut RCState) {
190 if let Some(data) = rc_state.emit_frame_data() {
191 let data = data.to_vec().into_boxed_slice();
192 self.send(RcData::Frame(data)).unwrap();
193 } else {
194 unreachable!(
195 "The encoder received more frames than its internal
196 limit allows"
197 );
198 }
199 }
200 fn send_pass_summary(&mut self, rc_state: &mut RCState) {
201 let data = rc_state.emit_summary();
202 let data = data.to_vec().into_boxed_slice();
203 self.send(RcData::Summary(data)).unwrap();
204 }
205 }
206
207 impl RcFirstPass for Option<Sender<RcData>> {
208 fn send_pass_data(&mut self, rc_state: &mut RCState) {
209 if let Some(s) = self.as_mut() {
210 s.send_pass_data(rc_state)
211 }
212 }
213 fn send_pass_summary(&mut self, rc_state: &mut RCState) {
214 if let Some(s) = self.as_mut() {
215 s.send_pass_summary(rc_state)
216 }
217 }
218 }
219
220 impl RcSecondPass for Receiver<RcData> {
221 fn feed_pass_data<T: Pixel>(
222 &mut self, inner: &mut ContextInner<T>,
223 ) -> Result<(), ()> {
224 while inner.rc_state.twopass_in_frames_needed() > 0
225 && !inner.done_processing()
226 {
227 if let Ok(RcData::Frame(data)) = self.recv() {
228 inner
229 .rc_state
230 .parse_frame_data_packet(data.as_ref())
231 .unwrap_or_else(|_| todo!("Error reporting"));
232 } else {
233 todo!("Error reporting");
234 }
235 }
236
237 Ok(())
238 }
239 }
240
241 impl RcSecondPass for Option<Receiver<RcData>> {
242 fn feed_pass_data<T: Pixel>(
243 &mut self, inner: &mut ContextInner<T>,
244 ) -> Result<(), ()> {
245 match self.as_mut() {
246 Some(s) => s.feed_pass_data(inner),
247 None => Ok(()),
248 }
249 }
250 }
251
252 impl Config {
253 #[allow(clippy::type_complexity)]
254 fn new_channel_internal<T: Pixel>(
255 &self,
256 ) -> Result<
257 (VideoDataChannel<T>, (Option<RcDataSender>, Option<RcDataReceiver>)),
258 InvalidConfig,
259 > {
260 // The inner context is already configured to use the summary at this point.
261 let (mut inner, pool) = self.setup()?;
262
263 // TODO: make it user-settable
264 let input_len = self.enc.speed_settings.rdo_lookahead_frames as usize * 2;
265
266 let (send_frame, receive_frame) = bounded(input_len);
267 let (send_packet, receive_packet) = unbounded();
268
269 let rc = &self.rate_control;
270
271 let (mut send_rc_pass1, rc_data_receiver) = if rc.emit_pass_data {
272 let (send_rc_pass1, receive_rc_pass1) = unbounded();
273 (Some(send_rc_pass1), Some(RcDataReceiver(receive_rc_pass1)))
274 } else {
275 (None, None)
276 };
277
278 let (rc_data_sender, mut receive_rc_pass2, frame_limit) = if rc
279 .summary
280 .is_some()
281 {
282 let (frame_limit, pass_limit) =
283 rc.summary.as_ref().map(|s| (s.ntus as u64, s.total as u64)).unwrap();
284
285 inner.limit = Some(frame_limit);
286
287 let (send_rc_pass2, receive_rc_pass2) = unbounded();
288
289 (
290 Some(RcDataSender::new(pass_limit, send_rc_pass2)),
291 Some(receive_rc_pass2),
292 frame_limit,
293 )
294 } else {
295 (None, None, i32::MAX as u64)
296 };
297
298 let config = Arc::new(self.enc.clone());
299
300 let channel = (
301 FrameSender::new(frame_limit, send_frame, config.clone()),
302 PacketReceiver { receiver: receive_packet, config },
303 );
304
305 let pass_channel = (rc_data_sender, rc_data_receiver);
306
307 let run = move || {
308 for f in receive_frame.iter() {
309 // info!("frame in {}", inner.frame_count);
310 while !inner.needs_more_fi_lookahead() {
311 receive_rc_pass2.feed_pass_data(&mut inner).unwrap();
312 // needs_more_fi_lookahead() should guard for missing output_frameno
313 // already.
314 //
315 // this call should return either Ok or Err(Encoded)
316 let has_pass_data = match inner.receive_packet() {
317 Ok(p) => {
318 send_packet.send(p).unwrap();
319 true
320 }
321 Err(EncoderStatus::Encoded) => true,
322 Err(EncoderStatus::NotReady) => todo!("Error reporting"),
323 _ => unreachable!(),
324 };
325 if has_pass_data {
326 send_rc_pass1.send_pass_data(&mut inner.rc_state);
327 }
328 }
329
330 let (frame, params) = f;
331 let _ = inner.send_frame(frame, params); // TODO make sure it cannot fail.
332 }
333
334 inner.limit = Some(inner.frame_count);
335 let _ = inner.send_frame(None, None);
336
337 loop {
338 receive_rc_pass2.feed_pass_data(&mut inner).unwrap();
339 let r = inner.receive_packet();
340 let has_pass_data = match r {
341 Ok(p) => {
342 // warn!("Sending out {}", p.input_frameno);
343 send_packet.send(p).unwrap();
344 true
345 }
346 Err(EncoderStatus::LimitReached) => break,
347 Err(EncoderStatus::Encoded) => true,
348 Err(EncoderStatus::NotReady) => todo!("Error reporting"),
349 _ => unreachable!(),
350 };
351
352 if has_pass_data {
353 send_rc_pass1.send_pass_data(&mut inner.rc_state);
354 }
355 }
356
357 send_rc_pass1.send_pass_summary(&mut inner.rc_state);
358 };
359
360 if let Some(pool) = pool {
361 pool.spawn(run);
362 } else {
363 rayon::spawn(run);
364 }
365
366 Ok((channel, pass_channel))
367 }
368 }