From 54399431210348c98ca0a13d963c63d9cf985e77 Mon Sep 17 00:00:00 2001 From: Henrik Gramner Date: Fri, 12 May 2023 13:16:26 +0200 Subject: [PATCH 1/4] Eliminate duplicate and constant intra_edge table entries Reduces the table size by around 3 kB. --- src/decode.c | 52 +++++++++++++++++------------------- src/intra_edge.c | 69 ++++++++++++++---------------------------------- src/intra_edge.h | 20 +++++++++----- 3 files changed, 58 insertions(+), 83 deletions(-) diff --git a/src/decode.c b/src/decode.c index 4d1ad555f..db06ed6e5 100644 --- a/src/decode.c +++ b/src/decode.c @@ -2390,19 +2390,19 @@ static int decode_sb(Dav1dTaskContext *const t, const enum BlockLevel bl, if (bl == BL_8X8) { const EdgeTip *const tip = (const EdgeTip *) node; assert(hsz == 1); - if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[0])) + if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, EDGE_ALL_TR_AND_BL)) return -1; const enum Filter2d tl_filter = t->tl_4x4_filter; t->bx++; - if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[1])) + if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[0])) return -1; t->bx--; t->by++; - if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[2])) + if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[1])) return -1; t->bx++; t->tl_4x4_filter = tl_filter; - if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[3])) + if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[2])) return -1; t->bx--; t->by--; @@ -2435,56 +2435,52 @@ static int decode_sb(Dav1dTaskContext *const t, const enum BlockLevel bl, } break; case PARTITION_T_TOP_SPLIT: { - const EdgeBranch *const branch = (const EdgeBranch *) node; - if (decode_b(t, bl, b[0], PARTITION_T_TOP_SPLIT, branch->tts[0])) + if (decode_b(t, bl, b[0], PARTITION_T_TOP_SPLIT, EDGE_ALL_TR_AND_BL)) return -1; t->bx += hsz; - if (decode_b(t, bl, b[0], PARTITION_T_TOP_SPLIT, branch->tts[1])) + if (decode_b(t, bl, b[0], PARTITION_T_TOP_SPLIT, node->v[1])) return -1; t->bx -= hsz; t->by += hsz; - if (decode_b(t, bl, b[1], PARTITION_T_TOP_SPLIT, branch->tts[2])) + if (decode_b(t, bl, b[1], PARTITION_T_TOP_SPLIT, node->h[1])) return -1; t->by -= hsz; break; } case PARTITION_T_BOTTOM_SPLIT: { - const EdgeBranch *const branch = (const EdgeBranch *) node; - if (decode_b(t, bl, b[0], PARTITION_T_BOTTOM_SPLIT, branch->tbs[0])) + if (decode_b(t, bl, b[0], PARTITION_T_BOTTOM_SPLIT, node->h[0])) return -1; t->by += hsz; - if (decode_b(t, bl, b[1], PARTITION_T_BOTTOM_SPLIT, branch->tbs[1])) + if (decode_b(t, bl, b[1], PARTITION_T_BOTTOM_SPLIT, node->v[0])) return -1; t->bx += hsz; - if (decode_b(t, bl, b[1], PARTITION_T_BOTTOM_SPLIT, branch->tbs[2])) + if (decode_b(t, bl, b[1], PARTITION_T_BOTTOM_SPLIT, 0)) return -1; t->bx -= hsz; t->by -= hsz; break; } case PARTITION_T_LEFT_SPLIT: { - const EdgeBranch *const branch = (const EdgeBranch *) node; - if (decode_b(t, bl, b[0], PARTITION_T_LEFT_SPLIT, branch->tls[0])) + if (decode_b(t, bl, b[0], PARTITION_T_LEFT_SPLIT, EDGE_ALL_TR_AND_BL)) return -1; t->by += hsz; - if (decode_b(t, bl, b[0], PARTITION_T_LEFT_SPLIT, branch->tls[1])) + if (decode_b(t, bl, b[0], PARTITION_T_LEFT_SPLIT, node->h[1])) return -1; t->by -= hsz; t->bx += hsz; - if (decode_b(t, bl, b[1], PARTITION_T_LEFT_SPLIT, branch->tls[2])) + if (decode_b(t, bl, b[1], PARTITION_T_LEFT_SPLIT, node->v[1])) return -1; t->bx -= hsz; break; } case PARTITION_T_RIGHT_SPLIT: { - const EdgeBranch *const branch = (const EdgeBranch *) node; - if (decode_b(t, bl, b[0], PARTITION_T_RIGHT_SPLIT, branch->trs[0])) + if (decode_b(t, bl, b[0], PARTITION_T_RIGHT_SPLIT, node->v[0])) return -1; t->bx += hsz; - if (decode_b(t, bl, b[1], PARTITION_T_RIGHT_SPLIT, branch->trs[1])) + if (decode_b(t, bl, b[1], PARTITION_T_RIGHT_SPLIT, node->h[0])) return -1; t->by += hsz; - if (decode_b(t, bl, b[1], PARTITION_T_RIGHT_SPLIT, branch->trs[2])) + if (decode_b(t, bl, b[1], PARTITION_T_RIGHT_SPLIT, 0)) return -1; t->by -= hsz; t->bx -= hsz; @@ -2492,34 +2488,34 @@ static int decode_sb(Dav1dTaskContext *const t, const enum BlockLevel bl, } case PARTITION_H4: { const EdgeBranch *const branch = (const EdgeBranch *) node; - if (decode_b(t, bl, b[0], PARTITION_H4, branch->h4[0])) + if (decode_b(t, bl, b[0], PARTITION_H4, node->h[0])) return -1; t->by += hsz >> 1; - if (decode_b(t, bl, b[0], PARTITION_H4, branch->h4[1])) + if (decode_b(t, bl, b[0], PARTITION_H4, branch->h4)) return -1; t->by += hsz >> 1; - if (decode_b(t, bl, b[0], PARTITION_H4, branch->h4[2])) + if (decode_b(t, bl, b[0], PARTITION_H4, EDGE_ALL_LEFT_HAS_BOTTOM)) return -1; t->by += hsz >> 1; if (t->by < f->bh) - if (decode_b(t, bl, b[0], PARTITION_H4, branch->h4[3])) + if (decode_b(t, bl, b[0], PARTITION_H4, node->h[1])) return -1; t->by -= hsz * 3 >> 1; break; } case PARTITION_V4: { const EdgeBranch *const branch = (const EdgeBranch *) node; - if (decode_b(t, bl, b[0], PARTITION_V4, branch->v4[0])) + if (decode_b(t, bl, b[0], PARTITION_V4, node->v[0])) return -1; t->bx += hsz >> 1; - if (decode_b(t, bl, b[0], PARTITION_V4, branch->v4[1])) + if (decode_b(t, bl, b[0], PARTITION_V4, branch->v4)) return -1; t->bx += hsz >> 1; - if (decode_b(t, bl, b[0], PARTITION_V4, branch->v4[2])) + if (decode_b(t, bl, b[0], PARTITION_V4, EDGE_ALL_TOP_HAS_RIGHT)) return -1; t->bx += hsz >> 1; if (t->bx < f->bw) - if (decode_b(t, bl, b[0], PARTITION_V4, branch->v4[3])) + if (decode_b(t, bl, b[0], PARTITION_V4, node->v[1])) return -1; t->bx -= hsz * 3 >> 1; break; diff --git a/src/intra_edge.c b/src/intra_edge.c index 684d113fa..2e4e68d53 100644 --- a/src/intra_edge.c +++ b/src/intra_edge.c @@ -44,66 +44,37 @@ static void init_edges(EdgeNode *const node, const enum EdgeFlags edge_flags) { node->o = edge_flags; + node->h[0] = edge_flags | EDGE_ALL_LEFT_HAS_BOTTOM; + node->v[0] = edge_flags | EDGE_ALL_TOP_HAS_RIGHT; -#define ALL_FL(t) (EDGE_I444_##t | EDGE_I422_##t | EDGE_I420_##t) if (bl == BL_8X8) { EdgeTip *const nt = (EdgeTip *) node; - node->h[0] = edge_flags | ALL_FL(LEFT_HAS_BOTTOM); - node->h[1] = edge_flags & (ALL_FL(LEFT_HAS_BOTTOM) | + node->h[1] = edge_flags & (EDGE_ALL_LEFT_HAS_BOTTOM | EDGE_I420_TOP_HAS_RIGHT); - - node->v[0] = edge_flags | ALL_FL(TOP_HAS_RIGHT); - node->v[1] = edge_flags & (ALL_FL(TOP_HAS_RIGHT) | + node->v[1] = edge_flags & (EDGE_ALL_TOP_HAS_RIGHT | EDGE_I420_LEFT_HAS_BOTTOM | EDGE_I422_LEFT_HAS_BOTTOM); - nt->split[0] = ALL_FL(TOP_HAS_RIGHT) | ALL_FL(LEFT_HAS_BOTTOM); - nt->split[1] = (edge_flags & ALL_FL(TOP_HAS_RIGHT)) | + nt->split[0] = (edge_flags & EDGE_ALL_TOP_HAS_RIGHT) | EDGE_I422_LEFT_HAS_BOTTOM; - nt->split[2] = edge_flags | EDGE_I444_TOP_HAS_RIGHT; - nt->split[3] = edge_flags & (EDGE_I420_TOP_HAS_RIGHT | + nt->split[1] = edge_flags | EDGE_I444_TOP_HAS_RIGHT; + nt->split[2] = edge_flags & (EDGE_I420_TOP_HAS_RIGHT | EDGE_I420_LEFT_HAS_BOTTOM | EDGE_I422_LEFT_HAS_BOTTOM); } else { EdgeBranch *const nwc = (EdgeBranch *) node; - node->h[0] = edge_flags | ALL_FL(LEFT_HAS_BOTTOM); - node->h[1] = edge_flags & ALL_FL(LEFT_HAS_BOTTOM); - - node->v[0] = edge_flags | ALL_FL(TOP_HAS_RIGHT); - node->v[1] = edge_flags & ALL_FL(TOP_HAS_RIGHT); - - nwc->h4[0] = edge_flags | ALL_FL(LEFT_HAS_BOTTOM); - nwc->h4[1] = - nwc->h4[2] = ALL_FL(LEFT_HAS_BOTTOM); - nwc->h4[3] = edge_flags & ALL_FL(LEFT_HAS_BOTTOM); - if (bl == BL_16X16) - nwc->h4[1] |= edge_flags & EDGE_I420_TOP_HAS_RIGHT; - - nwc->v4[0] = edge_flags | ALL_FL(TOP_HAS_RIGHT); - nwc->v4[1] = - nwc->v4[2] = ALL_FL(TOP_HAS_RIGHT); - nwc->v4[3] = edge_flags & ALL_FL(TOP_HAS_RIGHT); - if (bl == BL_16X16) - nwc->v4[1] |= edge_flags & (EDGE_I420_LEFT_HAS_BOTTOM | - EDGE_I422_LEFT_HAS_BOTTOM); - - nwc->tls[0] = ALL_FL(TOP_HAS_RIGHT) | ALL_FL(LEFT_HAS_BOTTOM); - nwc->tls[1] = edge_flags & ALL_FL(LEFT_HAS_BOTTOM); - nwc->tls[2] = edge_flags & ALL_FL(TOP_HAS_RIGHT); + node->h[1] = edge_flags & EDGE_ALL_LEFT_HAS_BOTTOM; + node->v[1] = edge_flags & EDGE_ALL_TOP_HAS_RIGHT; - nwc->trs[0] = edge_flags | ALL_FL(TOP_HAS_RIGHT); - nwc->trs[1] = edge_flags | ALL_FL(LEFT_HAS_BOTTOM); - nwc->trs[2] = 0; - - nwc->tts[0] = ALL_FL(TOP_HAS_RIGHT) | ALL_FL(LEFT_HAS_BOTTOM); - nwc->tts[1] = edge_flags & ALL_FL(TOP_HAS_RIGHT); - nwc->tts[2] = edge_flags & ALL_FL(LEFT_HAS_BOTTOM); - - nwc->tbs[0] = edge_flags | ALL_FL(LEFT_HAS_BOTTOM); - nwc->tbs[1] = edge_flags | ALL_FL(TOP_HAS_RIGHT); - nwc->tbs[2] = 0; + nwc->h4 = EDGE_ALL_LEFT_HAS_BOTTOM; + nwc->v4 = EDGE_ALL_TOP_HAS_RIGHT; + if (bl == BL_16X16) { + nwc->h4 |= edge_flags & EDGE_I420_TOP_HAS_RIGHT; + nwc->v4 |= edge_flags & (EDGE_I420_LEFT_HAS_BOTTOM | + EDGE_I422_LEFT_HAS_BOTTOM); + } } } @@ -114,17 +85,17 @@ static void init_mode_node(EdgeBranch *const nwc, const int left_has_bottom) { init_edges(&nwc->node, bl, - (top_has_right ? ALL_FL(TOP_HAS_RIGHT) : 0) | - (left_has_bottom ? ALL_FL(LEFT_HAS_BOTTOM) : 0)); + (top_has_right ? EDGE_ALL_TOP_HAS_RIGHT : 0) | + (left_has_bottom ? EDGE_ALL_LEFT_HAS_BOTTOM : 0)); if (bl == BL_16X16) { for (int n = 0; n < 4; n++) { EdgeTip *const nt = mem->nt++; nwc->split[n] = &nt->node; init_edges(&nt->node, bl + 1, ((n == 3 || (n == 1 && !top_has_right)) ? 0 : - ALL_FL(TOP_HAS_RIGHT)) | + EDGE_ALL_TOP_HAS_RIGHT) | (!(n == 0 || (n == 2 && left_has_bottom)) ? 0 : - ALL_FL(LEFT_HAS_BOTTOM))); + EDGE_ALL_LEFT_HAS_BOTTOM)); } } else { for (int n = 0; n < 4; n++) { diff --git a/src/intra_edge.h b/src/intra_edge.h index 8759095e1..720f12ab2 100644 --- a/src/intra_edge.h +++ b/src/intra_edge.h @@ -31,12 +31,20 @@ #include enum EdgeFlags { - EDGE_I444_TOP_HAS_RIGHT = 1 << 0, - EDGE_I422_TOP_HAS_RIGHT = 1 << 1, - EDGE_I420_TOP_HAS_RIGHT = 1 << 2, + EDGE_I444_TOP_HAS_RIGHT = 1 << 0, + EDGE_I422_TOP_HAS_RIGHT = 1 << 1, + EDGE_I420_TOP_HAS_RIGHT = 1 << 2, EDGE_I444_LEFT_HAS_BOTTOM = 1 << 3, EDGE_I422_LEFT_HAS_BOTTOM = 1 << 4, EDGE_I420_LEFT_HAS_BOTTOM = 1 << 5, + EDGE_ALL_TOP_HAS_RIGHT = EDGE_I444_TOP_HAS_RIGHT | + EDGE_I422_TOP_HAS_RIGHT | + EDGE_I420_TOP_HAS_RIGHT, + EDGE_ALL_LEFT_HAS_BOTTOM = EDGE_I444_LEFT_HAS_BOTTOM | + EDGE_I422_LEFT_HAS_BOTTOM | + EDGE_I420_LEFT_HAS_BOTTOM, + EDGE_ALL_TR_AND_BL = EDGE_ALL_TOP_HAS_RIGHT | + EDGE_ALL_LEFT_HAS_BOTTOM, }; typedef struct EdgeNode { @@ -45,13 +53,13 @@ typedef struct EdgeNode { typedef struct EdgeTip { EdgeNode node; - uint8_t /* enum EdgeFlags */ split[4]; + uint8_t /* enum EdgeFlags */ split[3]; } EdgeTip; typedef struct EdgeBranch { EdgeNode node; - uint8_t /* enum EdgeFlags */ tts[3], tbs[3], tls[3], trs[3], h4[4], v4[4]; - EdgeNode *split[4]; + uint8_t /* enum EdgeFlags */ h4, v4; + const EdgeNode *split[4]; } EdgeBranch; void dav1d_init_mode_tree(EdgeNode *const root, EdgeTip *const nt, From d4d5612645fac495164fee8b53ebc0d2fd46dbf4 Mon Sep 17 00:00:00 2001 From: Frank Bossen Date: Sun, 17 Mar 2024 12:53:16 -0400 Subject: [PATCH 2/4] Port C code to Rust --- src/decode.rs | 58 +++++++++++++++++---------------- src/intra_edge.rs | 83 ++++++++++++++--------------------------------- 2 files changed, 54 insertions(+), 87 deletions(-) diff --git a/src/decode.rs b/src/decode.rs index fcd0d75e4..b9d3fafe4 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -3575,16 +3575,16 @@ unsafe fn decode_sb( None => { let tip = intra_edge.tip(sb128, edge_index); assert!(hsz == 1); - decode_b(c, t, f, bl, BS_4x4, bp, tip.split[0])?; + decode_b(c, t, f, bl, BS_4x4, bp, EdgeFlags::EDGE_ALL_TR_AND_BL)?; let tl_filter = t.tl_4x4_filter; t.bx += 1; - decode_b(c, t, f, bl, BS_4x4, bp, tip.split[1])?; + decode_b(c, t, f, bl, BS_4x4, bp, tip.split[0])?; t.bx -= 1; t.by += 1; - decode_b(c, t, f, bl, BS_4x4, bp, tip.split[2])?; + decode_b(c, t, f, bl, BS_4x4, bp, tip.split[1])?; t.bx += 1; t.tl_4x4_filter = tl_filter; - decode_b(c, t, f, bl, BS_4x4, bp, tip.split[3])?; + decode_b(c, t, f, bl, BS_4x4, bp, tip.split[2])?; t.bx -= 1; t.by -= 1; if cfg!(target_arch = "x86_64") && t.frame_thread.pass != 0 { @@ -3612,68 +3612,70 @@ unsafe fn decode_sb( } } PARTITION_T_TOP_SPLIT => { - let branch = intra_edge.branch(sb128, edge_index); - decode_b(c, t, f, bl, b[0], bp, branch.tts[0])?; + let node = intra_edge.node(sb128, edge_index); + decode_b(c, t, f, bl, b[0], bp, EdgeFlags::EDGE_ALL_TR_AND_BL)?; t.bx += hsz; - decode_b(c, t, f, bl, b[0], bp, branch.tts[1])?; + decode_b(c, t, f, bl, b[0], bp, node.v[1])?; t.bx -= hsz; t.by += hsz; - decode_b(c, t, f, bl, b[1], bp, branch.tts[2])?; + decode_b(c, t, f, bl, b[1], bp, node.h[1])?; t.by -= hsz; } PARTITION_T_BOTTOM_SPLIT => { - let branch = intra_edge.branch(sb128, edge_index); - decode_b(c, t, f, bl, b[0], bp, branch.tbs[0])?; + 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[1], bp, branch.tbs[1])?; + decode_b(c, t, f, bl, b[1], bp, node.v[0])?; t.bx += hsz; - decode_b(c, t, f, bl, b[1], bp, branch.tbs[2])?; + decode_b(c, t, f, bl, b[1], bp, EdgeFlags::EDGE_NONE)?; t.bx -= hsz; t.by -= hsz; } PARTITION_T_LEFT_SPLIT => { - let branch = intra_edge.branch(sb128, edge_index); - decode_b(c, t, f, bl, b[0], bp, branch.tls[0])?; + let node = intra_edge.node(sb128, edge_index); + decode_b(c, t, f, bl, b[0], bp, EdgeFlags::EDGE_ALL_TR_AND_BL)?; t.by += hsz; - decode_b(c, t, f, bl, b[0], bp, branch.tls[1])?; + decode_b(c, t, f, bl, b[0], bp, node.h[1])?; t.by -= hsz; t.bx += hsz; - decode_b(c, t, f, bl, b[1], bp, branch.tls[2])?; + decode_b(c, t, f, bl, b[1], bp, node.v[1])?; t.bx -= hsz; } PARTITION_T_RIGHT_SPLIT => { - let branch = intra_edge.branch(sb128, edge_index); - decode_b(c, t, f, bl, b[0], bp, branch.trs[0])?; + 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[1], bp, branch.trs[1])?; + decode_b(c, t, f, bl, b[1], bp, node.h[0])?; t.by += hsz; - decode_b(c, t, f, bl, b[1], bp, (*branch).trs[2])?; + decode_b(c, t, f, bl, b[1], bp, EdgeFlags::EDGE_NONE)?; t.by -= hsz; t.bx -= hsz; } PARTITION_H4 => { + let node = intra_edge.node(sb128, edge_index); let branch = intra_edge.branch(sb128, edge_index); - decode_b(c, t, f, bl, b[0], bp, branch.h4[0])?; + decode_b(c, t, f, bl, b[0], bp, node.h[0])?; t.by += hsz >> 1; - decode_b(c, t, f, bl, b[0], bp, branch.h4[1])?; + decode_b(c, t, f, bl, b[0], bp, branch.h4)?; t.by += hsz >> 1; - decode_b(c, t, f, bl, b[0], bp, branch.h4[2])?; + decode_b(c, t, f, bl, b[0], bp, EdgeFlags::LEFT_HAS_BOTTOM)?; t.by += hsz >> 1; if t.by < f.bh { - decode_b(c, t, f, bl, b[0], bp, branch.h4[3])?; + decode_b(c, t, f, bl, b[0], bp, node.h[1])?; } t.by -= hsz * 3 >> 1; } PARTITION_V4 => { + let node = intra_edge.node(sb128, edge_index); let branch = intra_edge.branch(sb128, edge_index); - decode_b(c, t, f, bl, b[0], bp, branch.v4[0])?; + decode_b(c, t, f, bl, b[0], bp, node.v[0])?; t.bx += hsz >> 1; - decode_b(c, t, f, bl, b[0], bp, branch.v4[1])?; + decode_b(c, t, f, bl, b[0], bp, branch.v4)?; t.bx += hsz >> 1; - decode_b(c, t, f, bl, b[0], bp, branch.v4[2])?; + decode_b(c, t, f, bl, b[0], bp, EdgeFlags::TOP_HAS_RIGHT)?; t.bx += hsz >> 1; if t.bx < f.bw { - decode_b(c, t, f, bl, b[0], bp, branch.v4[3])?; + decode_b(c, t, f, bl, b[0], bp, node.v[1])?; } t.bx -= hsz * 3 >> 1; } diff --git a/src/intra_edge.rs b/src/intra_edge.rs index 442ebe439..aeebcd93b 100644 --- a/src/intra_edge.rs +++ b/src/intra_edge.rs @@ -18,18 +18,23 @@ bitflags! { } impl EdgeFlags { - const LEFT_HAS_BOTTOM: Self = Self::union_all([ + pub const LEFT_HAS_BOTTOM: Self = Self::union_all([ Self::I444_LEFT_HAS_BOTTOM, Self::I422_LEFT_HAS_BOTTOM, Self::I420_LEFT_HAS_BOTTOM, ]); - const TOP_HAS_RIGHT: Self = Self::union_all([ + pub const TOP_HAS_RIGHT: Self = Self::union_all([ Self::I444_TOP_HAS_RIGHT, Self::I422_TOP_HAS_RIGHT, Self::I420_TOP_HAS_RIGHT, ]); + pub const EDGE_ALL_TR_AND_BL: Self = + Self::union_all([Self::LEFT_HAS_BOTTOM, Self::TOP_HAS_RIGHT]); + + pub const EDGE_NONE: Self = Self::empty(); + pub const fn union_all(flags: [Self; N]) -> Self { let mut i = 0; let mut output = Self::empty(); @@ -99,19 +104,15 @@ pub struct EdgeNode { #[repr(C)] pub struct EdgeTip { pub node: EdgeNode, - pub split: [EdgeFlags; B], + pub split: [EdgeFlags; 3], } #[repr(C)] pub struct EdgeBranch { pub node: EdgeNode, - pub tts: [EdgeFlags; 3], - pub tbs: [EdgeFlags; 3], - pub tls: [EdgeFlags; 3], - pub trs: [EdgeFlags; 3], - pub h4: [EdgeFlags; 4], - pub v4: [EdgeFlags; 4], - pub split: [EdgeIndex; B], + pub h4: EdgeFlags, + pub v4: EdgeFlags, + pub split: [EdgeIndex; 4], } impl EdgeTip { @@ -135,7 +136,6 @@ impl EdgeTip { let node = EdgeNode { o, h, v }; let split = [ - EdgeFlags::all(), edge_flags .intersection(EdgeFlags::TOP_HAS_RIGHT) .union(EdgeFlags::I422_LEFT_HAS_BOTTOM), @@ -164,51 +164,20 @@ impl EdgeBranch { ]; let node = EdgeNode { o, h, v }; - let h4 = [ - edge_flags.union(EdgeFlags::LEFT_HAS_BOTTOM), - EdgeFlags::LEFT_HAS_BOTTOM.union( - edge_flags - .intersection(EdgeFlags::I420_TOP_HAS_RIGHT) - .select(matches!(bl, BlockLevel::Bl16x16)), - ), - EdgeFlags::LEFT_HAS_BOTTOM, - edge_flags.intersection(EdgeFlags::LEFT_HAS_BOTTOM), - ]; - - let v4 = [ - edge_flags.union(EdgeFlags::TOP_HAS_RIGHT), - EdgeFlags::TOP_HAS_RIGHT.union( - edge_flags - .intersection(EdgeFlags::union_all([ - EdgeFlags::I420_LEFT_HAS_BOTTOM, - EdgeFlags::I422_LEFT_HAS_BOTTOM, - ])) - .select(matches!(bl, BlockLevel::Bl16x16)), - ), - EdgeFlags::TOP_HAS_RIGHT, - edge_flags.intersection(EdgeFlags::TOP_HAS_RIGHT), - ]; + let h4 = EdgeFlags::LEFT_HAS_BOTTOM.union( + edge_flags + .intersection(EdgeFlags::I420_TOP_HAS_RIGHT) + .select(matches!(bl, BlockLevel::Bl16x16)), + ); - let tls = [ - EdgeFlags::all(), - edge_flags.intersection(EdgeFlags::LEFT_HAS_BOTTOM), - edge_flags.intersection(EdgeFlags::TOP_HAS_RIGHT), - ]; - let trs = [ - edge_flags.union(EdgeFlags::TOP_HAS_RIGHT), - edge_flags.union(EdgeFlags::LEFT_HAS_BOTTOM), - EdgeFlags::empty(), - ]; - let tts = [ - EdgeFlags::all(), - edge_flags.intersection(EdgeFlags::TOP_HAS_RIGHT), - edge_flags.intersection(EdgeFlags::LEFT_HAS_BOTTOM), - ]; - let tbs = [ - edge_flags.union(EdgeFlags::LEFT_HAS_BOTTOM), - edge_flags.union(EdgeFlags::TOP_HAS_RIGHT), - EdgeFlags::empty(), - ]; + let v4 = EdgeFlags::TOP_HAS_RIGHT.union( + edge_flags + .intersection(EdgeFlags::union_all([ + EdgeFlags::I420_LEFT_HAS_BOTTOM, + EdgeFlags::I422_LEFT_HAS_BOTTOM, + ])) + .select(matches!(bl, BlockLevel::Bl16x16)), + ); let split = [EdgeIndex::root(); 4]; @@ -216,10 +185,6 @@ impl EdgeBranch { node, h4, v4, - tls, - trs, - tts, - tbs, split, } } From d358e4e60397c7d575f1a9cef6c58096a271000e Mon Sep 17 00:00:00 2001 From: Frank Bossen Date: Sun, 17 Mar 2024 15:16:53 -0400 Subject: [PATCH 3/4] Fix style issues Remove `EDGE_` prefix from constant names and use `EdgeFlags::union_all` in conjunction with constants. --- src/decode.rs | 10 +++++----- src/intra_edge.rs | 15 ++++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/decode.rs b/src/decode.rs index b9d3fafe4..f8b297510 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -3575,7 +3575,7 @@ unsafe fn decode_sb( None => { let tip = intra_edge.tip(sb128, edge_index); assert!(hsz == 1); - decode_b(c, t, f, bl, BS_4x4, bp, EdgeFlags::EDGE_ALL_TR_AND_BL)?; + decode_b(c, t, f, bl, BS_4x4, bp, EdgeFlags::ALL_TR_AND_BL)?; let tl_filter = t.tl_4x4_filter; t.bx += 1; decode_b(c, t, f, bl, BS_4x4, bp, tip.split[0])?; @@ -3613,7 +3613,7 @@ unsafe fn decode_sb( } PARTITION_T_TOP_SPLIT => { let node = intra_edge.node(sb128, edge_index); - decode_b(c, t, f, bl, b[0], bp, EdgeFlags::EDGE_ALL_TR_AND_BL)?; + decode_b(c, t, f, bl, b[0], bp, EdgeFlags::ALL_TR_AND_BL)?; t.bx += hsz; decode_b(c, t, f, bl, b[0], bp, node.v[1])?; t.bx -= hsz; @@ -3627,13 +3627,13 @@ unsafe fn decode_sb( t.by += hsz; decode_b(c, t, f, bl, b[1], bp, node.v[0])?; t.bx += hsz; - decode_b(c, t, f, bl, b[1], bp, EdgeFlags::EDGE_NONE)?; + decode_b(c, t, f, bl, b[1], bp, EdgeFlags::NONE)?; t.bx -= hsz; t.by -= hsz; } PARTITION_T_LEFT_SPLIT => { let node = intra_edge.node(sb128, edge_index); - decode_b(c, t, f, bl, b[0], bp, EdgeFlags::EDGE_ALL_TR_AND_BL)?; + decode_b(c, t, f, bl, b[0], bp, EdgeFlags::ALL_TR_AND_BL)?; t.by += hsz; decode_b(c, t, f, bl, b[0], bp, node.h[1])?; t.by -= hsz; @@ -3647,7 +3647,7 @@ unsafe fn decode_sb( t.bx += hsz; decode_b(c, t, f, bl, b[1], bp, node.h[0])?; t.by += hsz; - decode_b(c, t, f, bl, b[1], bp, EdgeFlags::EDGE_NONE)?; + decode_b(c, t, f, bl, b[1], bp, EdgeFlags::NONE)?; t.by -= hsz; t.bx -= hsz; } diff --git a/src/intra_edge.rs b/src/intra_edge.rs index aeebcd93b..2fad695a3 100644 --- a/src/intra_edge.rs +++ b/src/intra_edge.rs @@ -30,10 +30,9 @@ impl EdgeFlags { Self::I420_TOP_HAS_RIGHT, ]); - pub const EDGE_ALL_TR_AND_BL: Self = - Self::union_all([Self::LEFT_HAS_BOTTOM, Self::TOP_HAS_RIGHT]); + pub const ALL_TR_AND_BL: Self = Self::union_all([Self::LEFT_HAS_BOTTOM, Self::TOP_HAS_RIGHT]); - pub const EDGE_NONE: Self = Self::empty(); + pub const NONE: Self = Self::empty(); pub const fn union_all(flags: [Self; N]) -> Self { let mut i = 0; @@ -164,20 +163,22 @@ impl EdgeBranch { ]; let node = EdgeNode { o, h, v }; - let h4 = EdgeFlags::LEFT_HAS_BOTTOM.union( + let h4 = EdgeFlags::union_all([ + EdgeFlags::LEFT_HAS_BOTTOM, edge_flags .intersection(EdgeFlags::I420_TOP_HAS_RIGHT) .select(matches!(bl, BlockLevel::Bl16x16)), - ); + ]); - let v4 = EdgeFlags::TOP_HAS_RIGHT.union( + let v4 = EdgeFlags::union_all([ + EdgeFlags::TOP_HAS_RIGHT, edge_flags .intersection(EdgeFlags::union_all([ EdgeFlags::I420_LEFT_HAS_BOTTOM, EdgeFlags::I422_LEFT_HAS_BOTTOM, ])) .select(matches!(bl, BlockLevel::Bl16x16)), - ); + ]); let split = [EdgeIndex::root(); 4]; From 1453ee265d39862afbde6911d2814cb1cea34e7a Mon Sep 17 00:00:00 2001 From: Frank Bossen Date: Tue, 19 Mar 2024 05:17:06 -0400 Subject: [PATCH 4/4] Address PR comments --- src/decode.rs | 12 ++++----- src/intra_edge.rs | 63 ++++++++++++++++++++++------------------------- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/src/decode.rs b/src/decode.rs index f8b297510..bbaaab4c1 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -3627,7 +3627,7 @@ unsafe fn decode_sb( t.by += hsz; decode_b(c, t, f, bl, b[1], bp, node.v[0])?; t.bx += hsz; - decode_b(c, t, f, bl, b[1], bp, EdgeFlags::NONE)?; + decode_b(c, t, f, bl, b[1], bp, EdgeFlags::empty())?; t.bx -= hsz; t.by -= hsz; } @@ -3647,18 +3647,18 @@ unsafe fn decode_sb( t.bx += hsz; decode_b(c, t, f, bl, b[1], bp, node.h[0])?; t.by += hsz; - decode_b(c, t, f, bl, b[1], bp, EdgeFlags::NONE)?; + decode_b(c, t, f, bl, b[1], bp, EdgeFlags::empty())?; t.by -= hsz; t.bx -= hsz; } PARTITION_H4 => { - let node = intra_edge.node(sb128, edge_index); let branch = intra_edge.branch(sb128, edge_index); + let node = &branch.node; decode_b(c, t, f, bl, b[0], bp, node.h[0])?; t.by += hsz >> 1; decode_b(c, t, f, bl, b[0], bp, branch.h4)?; t.by += hsz >> 1; - decode_b(c, t, f, bl, b[0], bp, EdgeFlags::LEFT_HAS_BOTTOM)?; + decode_b(c, t, f, bl, b[0], bp, EdgeFlags::ALL_LEFT_HAS_BOTTOM)?; t.by += hsz >> 1; if t.by < f.bh { decode_b(c, t, f, bl, b[0], bp, node.h[1])?; @@ -3666,13 +3666,13 @@ unsafe fn decode_sb( t.by -= hsz * 3 >> 1; } PARTITION_V4 => { - let node = intra_edge.node(sb128, edge_index); let branch = intra_edge.branch(sb128, edge_index); + let node = &branch.node; decode_b(c, t, f, bl, b[0], bp, node.v[0])?; t.bx += hsz >> 1; decode_b(c, t, f, bl, b[0], bp, branch.v4)?; t.bx += hsz >> 1; - decode_b(c, t, f, bl, b[0], bp, EdgeFlags::TOP_HAS_RIGHT)?; + decode_b(c, t, f, bl, b[0], bp, EdgeFlags::ALL_TOP_HAS_RIGHT)?; t.bx += hsz >> 1; if t.bx < f.bw { decode_b(c, t, f, bl, b[0], bp, node.v[1])?; diff --git a/src/intra_edge.rs b/src/intra_edge.rs index 2fad695a3..ac97d3f1a 100644 --- a/src/intra_edge.rs +++ b/src/intra_edge.rs @@ -18,21 +18,20 @@ bitflags! { } impl EdgeFlags { - pub const LEFT_HAS_BOTTOM: Self = Self::union_all([ + pub const ALL_LEFT_HAS_BOTTOM: Self = Self::union_all([ Self::I444_LEFT_HAS_BOTTOM, Self::I422_LEFT_HAS_BOTTOM, Self::I420_LEFT_HAS_BOTTOM, ]); - pub const TOP_HAS_RIGHT: Self = Self::union_all([ + pub const ALL_TOP_HAS_RIGHT: Self = Self::union_all([ Self::I444_TOP_HAS_RIGHT, Self::I422_TOP_HAS_RIGHT, Self::I420_TOP_HAS_RIGHT, ]); - pub const ALL_TR_AND_BL: Self = Self::union_all([Self::LEFT_HAS_BOTTOM, Self::TOP_HAS_RIGHT]); - - pub const NONE: Self = Self::empty(); + pub const ALL_TR_AND_BL: Self = + Self::union_all([Self::ALL_LEFT_HAS_BOTTOM, Self::ALL_TOP_HAS_RIGHT]); pub const fn union_all(flags: [Self; N]) -> Self { let mut i = 0; @@ -111,23 +110,23 @@ pub struct EdgeBranch { pub node: EdgeNode, pub h4: EdgeFlags, pub v4: EdgeFlags, - pub split: [EdgeIndex; 4], + pub split: [EdgeIndex; B], } impl EdgeTip { const fn new(edge_flags: EdgeFlags) -> Self { let o = edge_flags; let h = [ - edge_flags.union(EdgeFlags::LEFT_HAS_BOTTOM), + edge_flags.union(EdgeFlags::ALL_LEFT_HAS_BOTTOM), edge_flags.intersection(EdgeFlags::union_all([ - EdgeFlags::LEFT_HAS_BOTTOM, + EdgeFlags::ALL_LEFT_HAS_BOTTOM, EdgeFlags::I420_TOP_HAS_RIGHT, ])), ]; let v = [ - edge_flags.union(EdgeFlags::TOP_HAS_RIGHT), + edge_flags.union(EdgeFlags::ALL_TOP_HAS_RIGHT), edge_flags.intersection(EdgeFlags::union_all([ - EdgeFlags::TOP_HAS_RIGHT, + EdgeFlags::ALL_TOP_HAS_RIGHT, EdgeFlags::I420_LEFT_HAS_BOTTOM, EdgeFlags::I422_LEFT_HAS_BOTTOM, ])), @@ -136,7 +135,7 @@ impl EdgeTip { let split = [ edge_flags - .intersection(EdgeFlags::TOP_HAS_RIGHT) + .intersection(EdgeFlags::ALL_TOP_HAS_RIGHT) .union(EdgeFlags::I422_LEFT_HAS_BOTTOM), edge_flags.union(EdgeFlags::I444_TOP_HAS_RIGHT), edge_flags.intersection(EdgeFlags::union_all([ @@ -154,31 +153,27 @@ impl EdgeBranch { const fn new(edge_flags: EdgeFlags, bl: BlockLevel) -> Self { let o = edge_flags; let h = [ - edge_flags.union(EdgeFlags::LEFT_HAS_BOTTOM), - edge_flags.intersection(EdgeFlags::LEFT_HAS_BOTTOM), + edge_flags.union(EdgeFlags::ALL_LEFT_HAS_BOTTOM), + edge_flags.intersection(EdgeFlags::ALL_LEFT_HAS_BOTTOM), ]; let v = [ - edge_flags.union(EdgeFlags::TOP_HAS_RIGHT), - edge_flags.intersection(EdgeFlags::TOP_HAS_RIGHT), + edge_flags.union(EdgeFlags::ALL_TOP_HAS_RIGHT), + edge_flags.intersection(EdgeFlags::ALL_TOP_HAS_RIGHT), ]; let node = EdgeNode { o, h, v }; - let h4 = EdgeFlags::union_all([ - EdgeFlags::LEFT_HAS_BOTTOM, - edge_flags - .intersection(EdgeFlags::I420_TOP_HAS_RIGHT) - .select(matches!(bl, BlockLevel::Bl16x16)), - ]); + let h4 = edge_flags + .intersection(EdgeFlags::I420_TOP_HAS_RIGHT) + .select(matches!(bl, BlockLevel::Bl16x16)) + .union(EdgeFlags::ALL_LEFT_HAS_BOTTOM); - let v4 = EdgeFlags::union_all([ - EdgeFlags::TOP_HAS_RIGHT, - edge_flags - .intersection(EdgeFlags::union_all([ - EdgeFlags::I420_LEFT_HAS_BOTTOM, - EdgeFlags::I422_LEFT_HAS_BOTTOM, - ])) - .select(matches!(bl, BlockLevel::Bl16x16)), - ]); + let v4 = edge_flags + .intersection(EdgeFlags::union_all([ + EdgeFlags::I420_LEFT_HAS_BOTTOM, + EdgeFlags::I422_LEFT_HAS_BOTTOM, + ])) + .select(matches!(bl, BlockLevel::Bl16x16)) + .union(EdgeFlags::ALL_TOP_HAS_RIGHT); let split = [EdgeIndex::root(); 4]; @@ -235,8 +230,8 @@ impl ) -> (Self, EdgeIndices) { let mut branch = EdgeBranch::new( EdgeFlags::union_all([ - EdgeFlags::TOP_HAS_RIGHT.select(top_has_right), - EdgeFlags::LEFT_HAS_BOTTOM.select(left_has_bottom), + EdgeFlags::ALL_TOP_HAS_RIGHT.select(top_has_right), + EdgeFlags::ALL_LEFT_HAS_BOTTOM.select(left_has_bottom), ]), bl, ); @@ -247,8 +242,8 @@ impl indices.tip = next; branch.split[n as usize] = tip; self.tip[tip.index as usize] = EdgeTip::new(EdgeFlags::union_all([ - EdgeFlags::TOP_HAS_RIGHT.select(!(n == 3 || (n == 1 && !top_has_right))), - EdgeFlags::LEFT_HAS_BOTTOM.select(n == 0 || (n == 2 && left_has_bottom)), + EdgeFlags::ALL_TOP_HAS_RIGHT.select(!(n == 3 || (n == 1 && !top_has_right))), + EdgeFlags::ALL_LEFT_HAS_BOTTOM.select(n == 0 || (n == 2 && left_has_bottom)), ])); n += 1; }