Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Opaque as shorthand for Box<dyn Any + Send> #2640

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rav1e"
version = "0.4.0"
version = "0.5.0-alpha"
authors = ["Thomas Daede <tdaede@xiph.org>"]
edition = "2018"
build = "build.rs"
Expand Down
4 changes: 2 additions & 2 deletions src/api/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use crate::activity::ActivityMask;
use crate::api::lookahead::*;
use crate::api::{EncoderConfig, EncoderStatus, FrameType, Packet};
use crate::api::{EncoderConfig, EncoderStatus, FrameType, Opaque, Packet};
use crate::color::ChromaSampling::Cs400;
use crate::cpu_features::CpuFeatureLevel;
use crate::dist::get_satd;
Expand Down Expand Up @@ -256,7 +256,7 @@ pub(crate) struct ContextInner<T: Pixel> {
/// The next `output_frameno` to be computed by lookahead.
next_lookahead_output_frameno: u64,
/// Optional opaque to be sent back to the user
opaque_q: BTreeMap<u64, Box<dyn std::any::Any + Send>>,
opaque_q: BTreeMap<u64, Opaque>,
}

impl<T: Pixel> ContextInner<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ fn send_frame_kf<T: Pixel>(ctx: &mut Context<T>, keyframe: bool) {
let frame_type_override =
if keyframe { FrameTypeOverride::Key } else { FrameTypeOverride::No };

let opaque = Some(Box::new(keyframe) as Box<dyn std::any::Any + Send>);
let opaque = Some(Opaque::new(keyframe));

let fp = FrameParameters { frame_type_override, opaque };

Expand Down
26 changes: 25 additions & 1 deletion src/api/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,35 @@ use crate::serialize::{Deserialize, Serialize};
use crate::stats::EncoderStats;
use crate::util::Pixel;

use std::any::Any;
use std::fmt;
use std::sync::Arc;

use thiserror::*;

/// Opaque type to be passed from Frame to Packet
#[derive(Debug)]
pub struct Opaque(Box<dyn Any + Send + Sync>);

impl Opaque {
/// Wrap a type in the opaque struct
pub fn new<T: Any + Send + Sync>(t: T) -> Self {
Opaque(Box::new(t) as Box<dyn Any + Send + Sync>)
}

/// Attempt to downcast the opaque to a concrete type.
pub fn downcast<T: Any + Send + Sync>(self) -> Result<Box<T>, Opaque> {
if self.0.is::<T>() {
unsafe {
let raw: *mut (dyn Any + Send + Sync) = Box::into_raw(self.0);
Ok(Box::from_raw(raw as *mut T))
}
} else {
Err(self)
}
}
}

// TODO: use the num crate?
/// A rational number.
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -181,7 +205,7 @@ pub struct Packet<T: Pixel> {
pub enc_stats: EncoderStats,
/// Optional user-provided opaque data
#[cfg_attr(feature = "serialize", serde(skip))]
pub opaque: Option<Box<dyn std::any::Any + Send>>,
pub opaque: Option<Opaque>,
}

impl<T: Pixel> PartialEq for Packet<T> {
Expand Down
8 changes: 3 additions & 5 deletions src/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct FrameOpaque {
}

unsafe impl Send for FrameOpaque {}
unsafe impl Sync for FrameOpaque {}

impl Default for FrameOpaque {
fn default() -> Self {
Expand Down Expand Up @@ -180,7 +181,7 @@ impl EncContext {
}
fn send_frame(
&mut self, frame: Option<FrameInternal>, frame_type: FrameTypeOverride,
opaque: Option<Box<dyn std::any::Any + Send>>,
opaque: Option<rav1e::Opaque>,
) -> Result<(), rav1e::EncoderStatus> {
let info =
rav1e::FrameParameters { frame_type_override: frame_type, opaque };
Expand Down Expand Up @@ -1062,10 +1063,7 @@ pub unsafe extern fn rav1e_send_frame(
let maybe_opaque = if frame.is_null() {
None
} else {
(*frame)
.opaque
.take()
.map(|o| Box::new(o) as Box<dyn std::any::Any + Send>)
(*frame).opaque.take().map(|o| rav1e::Opaque::new(o))
};

let ret = (*ctx)
Expand Down
3 changes: 2 additions & 1 deletion src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use num_derive::FromPrimitive;

use crate::api::Opaque;
use crate::context::SB_SIZE;
use crate::mc::SUBPEL_FILTER_SIZE;
use crate::util::*;
Expand Down Expand Up @@ -39,7 +40,7 @@ pub struct FrameParameters {
/// Force emitted frame to be of the type selected
pub frame_type_override: FrameTypeOverride,
/// Output the provided data in the matching encoded Packet
pub opaque: Option<Box<dyn std::any::Any + Send>>,
pub opaque: Option<Opaque>,
}

pub use v_frame::frame::Frame;
Expand Down