| 1 |
// Copyright (c) 2018-2023, 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 |
|
| 10 |
use crate::api::color::*; |
| 11 |
use crate::api::config::EncoderConfig; |
| 12 |
use crate::api::context::RcData; |
| 13 |
use crate::api::util::*; |
| 14 |
use crate::encoder::*; |
| 15 |
use crate::frame::*; |
| 16 |
use crate::util::Pixel; |
| 17 |
|
| 18 |
use bitstream_io::{BigEndian, BitWrite, BitWriter}; |
| 19 |
use crossbeam::channel::{Receiver, Sender}; |
| 20 |
use thiserror::Error; |
| 21 |
|
| 22 |
use std::io; |
| 23 |
use std::sync::Arc; |
| 24 |
|
| 25 |
/// An error returned from the `send` methods. |
| 26 |
/// |
| 27 |
/// The message could not be sent because the channel is disconnected. |
| 28 |
/// |
| 29 |
/// The error contains the message so it can be recovered. |
| 30 |
#[derive(PartialEq, Eq, Clone, Copy, Error)] |
| 31 |
#[error("sending on a disconnected channel")] |
| 32 |
pub struct SendError<T>(pub T); |
| 33 |
|
| 34 |
/// An error returned from the `try_send` methods. |
| 35 |
/// |
| 36 |
/// The error contains the message being sent so it can be recovered. |
| 37 |
#[derive(PartialEq, Eq, Clone, Copy, Error)] |
| 38 |
pub enum TrySendError<T> { |
| 39 |
/// The message could not be sent because the channel is full. |
| 40 |
#[error("sending on a full channel")] |
| 41 |
Full(T), |
| 42 |
|
| 43 |
/// The message could not be sent because the channel is disconnected. |
| 44 |
#[error("sending on a disconnected channel")] |
| 45 |
Disconnected(T), |
| 46 |
} |
| 47 |
|
| 48 |
/// An error returned from the `recv` methods. |
| 49 |
/// |
| 50 |
/// A message could not be received because the channel is empty and disconnected. |
| 51 |
/// |
| 52 |
#[derive(PartialEq, Eq, Clone, Copy, Debug, Error)] |
| 53 |
#[error("receiving on an empty and disconnected channel")] |
| 54 |
pub struct RecvError; |
| 55 |
|
| 56 |
/// An error returned from the `try_recv` methods. |
| 57 |
/// |
| 58 |
#[derive(PartialEq, Eq, Clone, Copy, Debug, Error)] |
| 59 |
pub enum TryRecvError { |
| 60 |
/// A message could not be received because the channel is empty. |
| 61 |
#[error("receiving on an empty channel")] |
| 62 |
Empty, |
| 63 |
|
| 64 |
/// The message could not be received because the channel is empty and disconnected. |
| 65 |
#[error("receiving on an empty and disconnected channel")] |
| 66 |
Disconnected, |
| 67 |
} |
| 68 |
|
| 69 |
impl<T> SendError<T> { |
| 70 |
fn from(value: crossbeam::channel::SendError<T>) -> Self { |
| 71 |
Self(value.0) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
impl<T> TrySendError<T> { |
| 76 |
fn from(value: crossbeam::channel::TrySendError<T>) -> Self { |
| 77 |
use crossbeam::channel::TrySendError::*; |
| 78 |
match value { |
| 79 |
Full(v) => TrySendError::Full(v), |
| 80 |
Disconnected(v) => TrySendError::Disconnected(v), |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
impl RecvError { |
| 85 |
fn from(_: crossbeam::channel::RecvError) -> Self { |
| 86 |
RecvError |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl TryRecvError { |
| 91 |
fn from(value: crossbeam::channel::TryRecvError) -> Self { |
| 92 |
use crossbeam::channel::TryRecvError::*; |
| 93 |
match value { |
| 94 |
Empty => TryRecvError::Empty, |
| 95 |
Disconnected => TryRecvError::Disconnected, |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/// Endpoint to send previous-pass statistics data |
| 101 |
pub struct RcDataSender { |
| 102 |
pub(crate) sender: Sender<RcData>, |
| 103 |
pub(crate) limit: u64, |
| 104 |
pub(crate) count: u64, |
| 105 |
} |
| 106 |
|
| 107 |
impl RcDataSender { |
| 108 |
pub(crate) fn new(limit: u64, sender: Sender<RcData>) -> RcDataSender { |
| 109 |
Self { sender, limit, count: 0 } |
| 110 |
} |
| 111 |
|
| 112 |
/// # Errors |
| 113 |
/// |
| 114 |
/// - `TrySendError::Full` if the message could not be sent because the channel is full. |
| 115 |
/// - `TrySendError::Disconnected` if the message could not be sent |
| 116 |
/// because the channel is disconnected. |
| 117 |
pub fn try_send( |
| 118 |
&mut self, data: RcData, |
| 119 |
) -> Result<(), TrySendError<RcData>> { |
| 120 |
if self.limit <= self.count { |
| 121 |
Err(TrySendError::Full(data)) |
| 122 |
} else { |
| 123 |
let r = self.sender.try_send(data).map_err(TrySendError::from); |
| 124 |
if r.is_ok() { |
| 125 |
self.count += 1; |
| 126 |
} |
| 127 |
r |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/// # Errors |
| 132 |
/// |
| 133 |
/// - `SendError` if the message could not be sent because the channel is disconnected. |
| 134 |
pub fn send(&mut self, data: RcData) -> Result<(), SendError<RcData>> { |
| 135 |
if self.limit <= self.count { |
| 136 |
Err(SendError(data)) |
| 137 |
} else { |
| 138 |
let r = self.sender.send(data).map_err(SendError::from); |
| 139 |
if r.is_ok() { |
| 140 |
self.count += 1; |
| 141 |
} |
| 142 |
r |
| 143 |
} |
| 144 |
} |
| 145 |
pub fn len(&self) -> usize { |
| 146 |
self.sender.len() |
| 147 |
} |
| 148 |
pub fn is_empty(&self) -> bool { |
| 149 |
self.sender.is_empty() |
| 150 |
} |
| 151 |
|
| 152 |
// TODO: proxy more methods |
| 153 |
} |
| 154 |
|
| 155 |
/// Endpoint to receive current-pass statistics data |
| 156 |
pub struct RcDataReceiver(pub(crate) Receiver<RcData>); |
| 157 |
|
| 158 |
impl RcDataReceiver { |
| 159 |
/// Attempts to receive a message from the channel without blocking. |
| 160 |
/// |
| 161 |
/// This method will either receive a message from the channel immediately or return an error |
| 162 |
/// if the channel is empty. |
| 163 |
/// |
| 164 |
/// If called on a zero-capacity channel, this method will receive a message only if there |
| 165 |
/// happens to be a send operation on the other side of the channel at the same time. |
| 166 |
/// |
| 167 |
/// # Errors |
| 168 |
/// |
| 169 |
/// - `TryRecvError::Empty` if the channel is currently empty. |
| 170 |
/// - `TryRecvError::Disconnected` if the channel is empty and has been disconnected. |
| 171 |
pub fn try_recv(&self) -> Result<RcData, TryRecvError> { |
| 172 |
self.0.try_recv().map_err(TryRecvError::from) |
| 173 |
} |
| 174 |
|
| 175 |
/// Blocks the current thread until a message is received or the channel is empty and |
| 176 |
/// disconnected. |
| 177 |
/// |
| 178 |
/// If the channel is empty and not disconnected, this call will block until the receive |
| 179 |
/// operation can proceed. If the channel is empty and becomes disconnected, this call will |
| 180 |
/// wake up and return an error. |
| 181 |
/// |
| 182 |
/// If called on a zero-capacity channel, this method will wait for a send operation to appear |
| 183 |
/// on the other side of the channel. |
| 184 |
/// |
| 185 |
/// # Errors |
| 186 |
/// |
| 187 |
/// - `RecvError` if the channel is empty and has been disconnected. |
| 188 |
pub fn recv(&self) -> Result<RcData, RecvError> { |
| 189 |
self.0.recv().map_err(RecvError::from) |
| 190 |
} |
| 191 |
pub fn len(&self) -> usize { |
| 192 |
self.0.len() |
| 193 |
} |
| 194 |
pub fn is_empty(&self) -> bool { |
| 195 |
self.0.is_empty() |
| 196 |
} |
| 197 |
pub fn iter<'a>(&'a self) -> impl Iterator<Item = RcData> + 'a { |
| 198 |
self.0.iter() |
| 199 |
} |
| 200 |
|
| 201 |
pub const fn summary_size(&self) -> usize { |
| 202 |
crate::rate::TWOPASS_HEADER_SZ |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
pub type PassDataChannel = (RcDataSender, RcDataReceiver); |
| 207 |
|
| 208 |
pub type FrameInput<T> = (Option<Arc<Frame<T>>>, Option<FrameParameters>); |
| 209 |
|
| 210 |
/// Endpoint to send frames |
| 211 |
pub struct FrameSender<T: Pixel> { |
| 212 |
sender: Sender<FrameInput<T>>, |
| 213 |
config: Arc<EncoderConfig>, |
| 214 |
limit: u64, |
| 215 |
count: u64, |
| 216 |
} |
| 217 |
|
| 218 |
// Proxy the crossbeam Sender |
| 219 |
// |
| 220 |
// TODO: enforce the limit |
| 221 |
impl<T: Pixel> FrameSender<T> { |
| 222 |
pub(crate) fn new( |
| 223 |
limit: u64, sender: Sender<FrameInput<T>>, config: Arc<EncoderConfig>, |
| 224 |
) -> FrameSender<T> { |
| 225 |
Self { sender, config, limit, count: 0 } |
| 226 |
} |
| 227 |
|
| 228 |
/// # Errors |
| 229 |
/// |
| 230 |
/// - `TrySendError::Full` if the message could not be sent because the channel is full. |
| 231 |
/// - `TrySendError::Disconnected` if the message could not be sent |
| 232 |
/// because the channel is disconnected. |
| 233 |
pub fn try_send<F: IntoFrame<T>>( |
| 234 |
&mut self, frame: F, |
| 235 |
) -> Result<(), TrySendError<FrameInput<T>>> { |
| 236 |
if self.limit <= self.count { |
| 237 |
Err(TrySendError::Full(frame.into())) |
| 238 |
} else { |
| 239 |
let r = self.sender.try_send(frame.into()).map_err(TrySendError::from); |
| 240 |
if r.is_ok() { |
| 241 |
self.count += 1; |
| 242 |
} |
| 243 |
r |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/// # Errors |
| 248 |
/// |
| 249 |
/// - `SendError` if the message could not be sent because the channel is disconnected. |
| 250 |
pub fn send<F: IntoFrame<T>>( |
| 251 |
&mut self, frame: F, |
| 252 |
) -> Result<(), SendError<FrameInput<T>>> { |
| 253 |
if self.limit <= self.count { |
| 254 |
Err(SendError(frame.into())) |
| 255 |
} else { |
| 256 |
let r = self.sender.send(frame.into()).map_err(SendError::from); |
| 257 |
if r.is_ok() { |
| 258 |
self.count += 1; |
| 259 |
} |
| 260 |
r |
| 261 |
} |
| 262 |
} |
| 263 |
pub fn len(&self) -> usize { |
| 264 |
self.sender.len() |
| 265 |
} |
| 266 |
pub fn is_empty(&self) -> bool { |
| 267 |
self.sender.is_empty() |
| 268 |
} |
| 269 |
// TODO: proxy more methods |
| 270 |
} |
| 271 |
|
| 272 |
// Frame factory |
| 273 |
impl<T: Pixel> FrameSender<T> { |
| 274 |
/// Helper to create a new frame with the current encoder configuration |
| 275 |
#[inline] |
| 276 |
pub fn new_frame(&self) -> Frame<T> { |
| 277 |
Frame::new( |
| 278 |
self.config.width, |
| 279 |
self.config.height, |
| 280 |
self.config.chroma_sampling, |
| 281 |
) |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/// Endpoint to receive packets |
| 286 |
pub struct PacketReceiver<T: Pixel> { |
| 287 |
pub(crate) receiver: Receiver<Packet<T>>, |
| 288 |
pub(crate) config: Arc<EncoderConfig>, |
| 289 |
} |
| 290 |
|
| 291 |
impl<T: Pixel> PacketReceiver<T> { |
| 292 |
/// Attempts to receive a message from the channel without blocking. |
| 293 |
/// |
| 294 |
/// This method will either receive a message from the channel immediately or return an error |
| 295 |
/// if the channel is empty. |
| 296 |
/// |
| 297 |
/// If called on a zero-capacity channel, this method will receive a message only if there |
| 298 |
/// happens to be a send operation on the other side of the channel at the same time. |
| 299 |
/// |
| 300 |
/// # Errors |
| 301 |
/// |
| 302 |
/// - `TryRecvError::Empty` if the channel is currently empty. |
| 303 |
/// - `TryRecvError::Disconnected` if the channel is empty and has been disconnected. |
| 304 |
pub fn try_recv(&self) -> Result<Packet<T>, TryRecvError> { |
| 305 |
self.receiver.try_recv().map_err(TryRecvError::from) |
| 306 |
} |
| 307 |
/// Blocks the current thread until a message is received or the channel is empty and |
| 308 |
/// disconnected. |
| 309 |
/// |
| 310 |
/// If the channel is empty and not disconnected, this call will block until the receive |
| 311 |
/// operation can proceed. If the channel is empty and becomes disconnected, this call will |
| 312 |
/// wake up and return an error. |
| 313 |
/// |
| 314 |
/// If called on a zero-capacity channel, this method will wait for a send operation to appear |
| 315 |
/// on the other side of the channel. |
| 316 |
/// |
| 317 |
/// # Errors |
| 318 |
/// |
| 319 |
/// - `RecvError` if the channel is empty and has been disconnected. |
| 320 |
pub fn recv(&self) -> Result<Packet<T>, RecvError> { |
| 321 |
self.receiver.recv().map_err(RecvError::from) |
| 322 |
} |
| 323 |
pub fn len(&self) -> usize { |
| 324 |
self.receiver.len() |
| 325 |
} |
| 326 |
pub fn is_empty(&self) -> bool { |
| 327 |
self.receiver.is_empty() |
| 328 |
} |
| 329 |
pub fn iter<'a>(&'a self) -> impl Iterator<Item = Packet<T>> + 'a { |
| 330 |
self.receiver.iter() |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
impl<T: Pixel> PacketReceiver<T> { |
| 335 |
/// Produces a sequence header matching the current encoding context. |
| 336 |
/// |
| 337 |
/// Its format is compatible with the AV1 Matroska and ISOBMFF specification. |
| 338 |
/// Note that the returned header does not include any config OBUs which are |
| 339 |
/// required for some uses. See [the specification]. |
| 340 |
/// |
| 341 |
/// [the specification]: |
| 342 |
/// https://aomediacodec.github.io/av1-isobmff/#av1codecconfigurationbox-section |
| 343 |
/// |
| 344 |
/// # Panics |
| 345 |
/// |
| 346 |
/// Panics if the header cannot be written in memory. This is unrecoverable, |
| 347 |
/// and usually indicates the system is out of memory. |
| 348 |
#[inline] |
| 349 |
pub fn container_sequence_header(&self) -> Vec<u8> { |
| 350 |
fn sequence_header_inner(seq: &Sequence) -> io::Result<Vec<u8>> { |
| 351 |
let mut buf = Vec::new(); |
| 352 |
|
| 353 |
{ |
| 354 |
let mut bw = BitWriter::endian(&mut buf, BigEndian); |
| 355 |
bw.write_bit(true)?; // marker |
| 356 |
bw.write::<7, u8>(1)?; // version |
| 357 |
bw.write::<3, u8>(seq.profile)?; |
| 358 |
bw.write::<5, u8>(31)?; // level |
| 359 |
bw.write_bit(false)?; // tier |
| 360 |
bw.write_bit(seq.bit_depth > 8)?; // high_bitdepth |
| 361 |
bw.write_bit(seq.bit_depth == 12)?; // twelve_bit |
| 362 |
bw.write_bit(seq.chroma_sampling == ChromaSampling::Cs400)?; // monochrome |
| 363 |
bw.write_bit(seq.chroma_sampling != ChromaSampling::Cs444)?; // chroma_subsampling_x |
| 364 |
bw.write_bit(seq.chroma_sampling == ChromaSampling::Cs420)?; // chroma_subsampling_y |
| 365 |
bw.write::<2, u8>(0)?; // chroma_sample_position |
| 366 |
bw.write::<3, u8>(0)?; // reserved |
| 367 |
bw.write_bit(false)?; // initial_presentation_delay_present |
| 368 |
|
| 369 |
bw.write::<4, u8>(0)?; // reserved |
| 370 |
} |
| 371 |
|
| 372 |
Ok(buf) |
| 373 |
} |
| 374 |
|
| 375 |
let seq = Sequence::new(&self.config); |
| 376 |
|
| 377 |
sequence_header_inner(&seq).unwrap() |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
/// A channel modeling an encoding process |
| 382 |
pub type VideoDataChannel<T> = (FrameSender<T>, PacketReceiver<T>); |