Skip to content

Commit

Permalink
mod intra_edge: Make fully safe and const fn-initialized (#762)
Browse files Browse the repository at this point in the history
* Fixes #756.
  • Loading branch information
kkysen authored Feb 28, 2024
2 parents 08f6b7c + 0c1bb15 commit 82062a9
Show file tree
Hide file tree
Showing 4 changed files with 389 additions and 233 deletions.
54 changes: 32 additions & 22 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ use crate::src::internal::Rav1dTaskContext;
use crate::src::internal::Rav1dTaskContext_scratch_pal;
use crate::src::internal::Rav1dTileState;
use crate::src::internal::ScalableMotionParams;
use crate::src::intra_edge::EdgeBranch;
use crate::src::intra_edge::EdgeFlags;
use crate::src::intra_edge::EdgeNode;
use crate::src::intra_edge::EdgeTip;
use crate::src::intra_edge::EdgeIndex;
use crate::src::intra_edge::IntraEdges;
use crate::src::intra_edge::EDGE_I444_TOP_HAS_RIGHT;
use crate::src::ipred::rav1d_intra_pred_dsp_init;
use crate::src::levels::mv;
Expand Down Expand Up @@ -3492,16 +3491,25 @@ unsafe fn decode_sb(
t: &mut Rav1dTaskContext,
f: &mut Rav1dFrameData,
bl: BlockLevel,
node: *const EdgeNode,
edge_index: EdgeIndex,
) -> Result<(), ()> {
let ts = &mut *t.ts;
let hsz = 16 >> bl;
let have_h_split = f.bw > t.bx + hsz;
let have_v_split = f.bh > t.by + hsz;

let sb128 = f.seq_hdr().sb128 != 0;
let intra_edge = &IntraEdges::DEFAULT;

if !have_h_split && !have_v_split {
assert!(bl < BL_8X8);
return decode_sb(c, t, f, bl + 1, (*(node as *const EdgeBranch)).split[0]);
return decode_sb(
c,
t,
f,
bl + 1,
intra_edge.branch(sb128, edge_index).split[0],
);
}

let frame_hdr = &***f.frame_hdr.as_ref().unwrap();
Expand Down Expand Up @@ -3554,26 +3562,26 @@ unsafe fn decode_sb(

match bp {
PARTITION_NONE => {
let node = &*node;
let node = intra_edge.node(sb128, edge_index);
decode_b(c, t, f, bl, b[0], bp, node.o)?;
}
PARTITION_H => {
let node = &*node;
let node = intra_edge.node(sb128, edge_index);
decode_b(c, t, f, bl, b[0], bp, node.h[0])?;
t.by += hsz;
decode_b(c, t, f, bl, b[0], bp, node.h[1])?;
t.by -= hsz;
}
PARTITION_V => {
let node = &*node;
let node = intra_edge.node(sb128, edge_index);
decode_b(c, t, f, bl, b[0], bp, node.v[0])?;
t.bx += hsz;
decode_b(c, t, f, bl, b[0], bp, node.v[1])?;
t.bx -= hsz;
}
PARTITION_SPLIT => {
if bl == BL_8X8 {
let tip = &*(node as *const EdgeTip);
let tip = intra_edge.tip(sb128, edge_index);
assert!(hsz == 1);
decode_b(c, t, f, bl, BS_4x4, bp, tip.split[0])?;
let tl_filter = t.tl_4x4_filter;
Expand All @@ -3596,7 +3604,7 @@ unsafe fn decode_sb(
(((ts.frame_thread[p].cf as uintptr_t) + 63) & !63) as *mut DynCoef;
}
} else {
let branch = &*(node as *const EdgeBranch);
let branch = intra_edge.branch(sb128, edge_index);
decode_sb(c, t, f, bl + 1, branch.split[0])?;
t.bx += hsz;
decode_sb(c, t, f, bl + 1, branch.split[1])?;
Expand All @@ -3610,7 +3618,7 @@ unsafe fn decode_sb(
}
}
PARTITION_T_TOP_SPLIT => {
let branch = &*(node as *const EdgeBranch);
let branch = intra_edge.branch(sb128, edge_index);
decode_b(c, t, f, bl, b[0], bp, branch.tts[0])?;
t.bx += hsz;
decode_b(c, t, f, bl, b[0], bp, branch.tts[1])?;
Expand All @@ -3620,7 +3628,7 @@ unsafe fn decode_sb(
t.by -= hsz;
}
PARTITION_T_BOTTOM_SPLIT => {
let branch = &*(node as *const EdgeBranch);
let branch = intra_edge.branch(sb128, edge_index);
decode_b(c, t, f, bl, b[0], bp, branch.tbs[0])?;
t.by += hsz;
decode_b(c, t, f, bl, b[1], bp, branch.tbs[1])?;
Expand All @@ -3630,7 +3638,7 @@ unsafe fn decode_sb(
t.by -= hsz;
}
PARTITION_T_LEFT_SPLIT => {
let branch = &*(node as *const EdgeBranch);
let branch = intra_edge.branch(sb128, edge_index);
decode_b(c, t, f, bl, b[0], bp, branch.tls[0])?;
t.by += hsz;
decode_b(c, t, f, bl, b[0], bp, branch.tls[1])?;
Expand All @@ -3640,7 +3648,7 @@ unsafe fn decode_sb(
t.bx -= hsz;
}
PARTITION_T_RIGHT_SPLIT => {
let branch = &*(node as *const EdgeBranch);
let branch = intra_edge.branch(sb128, edge_index);
decode_b(c, t, f, bl, b[0], bp, branch.trs[0])?;
t.bx += hsz;
decode_b(c, t, f, bl, b[1], bp, branch.trs[1])?;
Expand All @@ -3650,7 +3658,7 @@ unsafe fn decode_sb(
t.bx -= hsz;
}
PARTITION_H4 => {
let branch = &*(node as *const EdgeBranch);
let branch = intra_edge.branch(sb128, edge_index);
decode_b(c, t, f, bl, b[0], bp, branch.h4[0])?;
t.by += hsz >> 1;
decode_b(c, t, f, bl, b[0], bp, branch.h4[1])?;
Expand All @@ -3663,7 +3671,7 @@ unsafe fn decode_sb(
t.by -= hsz * 3 >> 1;
}
PARTITION_V4 => {
let branch = &*(node as *const EdgeBranch);
let branch = intra_edge.branch(sb128, edge_index);
decode_b(c, t, f, bl, b[0], bp, branch.v4[0])?;
t.bx += hsz >> 1;
decode_b(c, t, f, bl, b[0], bp, branch.v4[1])?;
Expand Down Expand Up @@ -3704,13 +3712,14 @@ unsafe fn decode_sb(

assert!(bl < BL_8X8);
if is_split {
let branch = &*(node as *const EdgeBranch);
let branch = intra_edge.branch(sb128, edge_index);
bp = PARTITION_SPLIT;
decode_sb(c, t, f, bl + 1, branch.split[0])?;
t.bx += hsz;
decode_sb(c, t, f, bl + 1, branch.split[1])?;
t.bx -= hsz;
} else {
let node = intra_edge.node(sb128, edge_index);
bp = PARTITION_H;
decode_b(
c,
Expand All @@ -3719,7 +3728,7 @@ unsafe fn decode_sb(
bl,
dav1d_block_sizes[bl as usize][bp as usize][0],
bp,
(*node).h[0],
node.h[0],
)?;
}
} else {
Expand Down Expand Up @@ -3753,13 +3762,14 @@ unsafe fn decode_sb(

assert!(bl < BL_8X8);
if is_split {
let branch = &*(node as *const EdgeBranch);
let branch = intra_edge.branch(sb128, edge_index);
bp = PARTITION_SPLIT;
decode_sb(c, t, f, bl + 1, branch.split[0])?;
t.by += hsz;
decode_sb(c, t, f, bl + 1, branch.split[2])?;
t.by -= hsz;
} else {
let node = intra_edge.node(sb128, edge_index);
bp = PARTITION_V;
decode_b(
c,
Expand All @@ -3768,7 +3778,7 @@ unsafe fn decode_sb(
bl,
dav1d_block_sizes[bl as usize][bp as usize][0],
bp,
(*node).v[0],
node.v[0],
)?;
}
}
Expand Down Expand Up @@ -4101,7 +4111,7 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(
if c.flush.load(Ordering::Acquire) != 0 {
return Err(());
}
decode_sb(c, t, f, root_bl, c.intra_edge.root(root_bl))?;
decode_sb(c, t, f, root_bl, EdgeIndex::root())?;
if t.bx & 16 != 0 || f.seq_hdr().sb128 != 0 {
t.a = (t.a).offset(1);
}
Expand Down Expand Up @@ -4208,7 +4218,7 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(
read_restoration_info(t, f, lr, p, frame_type);
}
}
decode_sb(c, t, f, root_bl, c.intra_edge.root(root_bl))?;
decode_sb(c, t, f, root_bl, EdgeIndex::root())?;
if t.bx & 16 != 0 || f.seq_hdr().sb128 != 0 {
t.a = (t.a).offset(1);
t.lf_mask = (t.lf_mask).offset(1);
Expand Down
27 changes: 0 additions & 27 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ use crate::src::error::Rav1dResult;
use crate::src::filmgrain::Rav1dFilmGrainDSPContext;
use crate::src::filmgrain::GRAIN_HEIGHT;
use crate::src::filmgrain::GRAIN_WIDTH;
use crate::src::intra_edge::EdgeBranch;
use crate::src::intra_edge::EdgeFlags;
use crate::src::intra_edge::EdgeNode;
use crate::src::intra_edge::EdgeTip;
use crate::src::ipred::Rav1dIntraPredDSPContext;
use crate::src::itx::Rav1dInvTxfmDSPContext;
use crate::src::levels::Av1Block;
use crate::src::levels::BlockLevel;
use crate::src::levels::BlockSize;
use crate::src::levels::Filter2d;
use crate::src::levels::BL_128X128;
use crate::src::levels::BL_64X64;
use crate::src::lf_mask::Av1Filter;
use crate::src::lf_mask::Av1FilterLUT;
use crate::src::lf_mask::Av1Restoration;
Expand Down Expand Up @@ -205,24 +199,6 @@ pub(crate) struct Rav1dContext_refs {
pub refpoc: [c_uint; 7],
}

#[repr(C)]
pub struct Rav1dContext_intra_edge {
pub branch_sb128: [EdgeBranch; 85],
pub branch_sb64: [EdgeBranch; 21],
pub tip_sb128: [EdgeTip; 256],
pub tip_sb64: [EdgeTip; 64],
}

impl Rav1dContext_intra_edge {
pub fn root(&self, bl: BlockLevel) -> &EdgeNode {
match bl {
BL_128X128 => &self.branch_sb128[0].node,
BL_64X64 => &self.branch_sb64[0].node,
_ => unreachable!(),
}
}
}

pub(crate) enum Rav1dContextTaskType {
/// Worker thread in a multi-threaded context.
Worker(JoinHandle<()>),
Expand Down Expand Up @@ -289,9 +265,6 @@ pub struct Rav1dContext {
pub(crate) dsp: [Rav1dDSPContext; 3], /* 8, 10, 12 bits/component */
pub(crate) refmvs_dsp: Rav1dRefmvsDSPContext,

// tree to keep track of which edges are available
pub(crate) intra_edge: Rav1dContext_intra_edge,

pub(crate) allocator: Rav1dPicAllocator,
pub(crate) apply_grain: bool,
pub(crate) operating_point: c_int,
Expand Down
Loading

0 comments on commit 82062a9

Please sign in to comment.