From 9f41bb02ec5e7007b03a35eacf6d3ef379208e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:13:49 +0300 Subject: [PATCH] S12: mpeg2 motion kind is a MotionKind enum instead of u8 consts (same six kinds, same arms; DualField arm explicit instead of the catch-all) --- crates/ec-mpeg2/src/decoder.rs | 57 ++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/crates/ec-mpeg2/src/decoder.rs b/crates/ec-mpeg2/src/decoder.rs index fbf65def..8b67f8ac 100644 --- a/crates/ec-mpeg2/src/decoder.rs +++ b/crates/ec-mpeg2/src/decoder.rs @@ -87,12 +87,16 @@ struct PicState { second_field: bool, } -const MV_FRAME: u8 = 0; -const MV_FIELD_IN_FRAME: u8 = 1; -const MV_DUAL_FRAME: u8 = 2; -const MV_FIELD16: u8 = 3; -const MV_16X8: u8 = 4; -const MV_DUAL_FIELD: u8 = 5; +#[derive(Clone, Copy, Default, PartialEq, Eq)] +enum MotionKind { + #[default] + Frame, + FieldInFrame, + DualFrame, + Field16, + Mv16x8, + DualField, +} #[derive(Clone, Copy, Default)] struct Motion { @@ -100,7 +104,7 @@ struct Motion { mv: [[[i32; 2]; 2]; 2], sel: [[u8; 2]; 2], dmv: [i32; 2], - kind: u8, + kind: MotionKind, fwd: bool, bwd: bool, } @@ -767,7 +771,7 @@ impl Core { // Same directions and vectors as the previous macroblock, // predicted frame-wise (field-wise from the same-parity // field in a field picture). - m.kind = if field_pic { MV_FIELD16 } else { MV_FRAME }; + m.kind = if field_pic { MotionKind::Field16 } else { MotionKind::Frame }; for s2 in 0..2 { m.mv[s2][0] = pmv[0][s2]; m.mv[s2][1] = pmv[0][s2]; @@ -778,7 +782,7 @@ impl Core { pmv = [[[0; 2]; 2]; 2]; m = Motion::default(); m.fwd = true; - m.kind = if field_pic { MV_FIELD16 } else { MV_FRAME }; + m.kind = if field_pic { MotionKind::Field16 } else { MotionKind::Frame }; let sel = st.parity as u8; m.sel = [[sel; 2]; 2]; motion = m; @@ -834,19 +838,19 @@ impl Core { } m.kind = if field_pic { match kind_code { - 1 => MV_FIELD16, - 2 => MV_16X8, - _ => MV_DUAL_FIELD, + 1 => MotionKind::Field16, + 2 => MotionKind::Mv16x8, + _ => MotionKind::DualField, } } else { match kind_code { - 1 => MV_FIELD_IN_FRAME, - 2 => MV_FRAME, - _ => MV_DUAL_FRAME, + 1 => MotionKind::FieldInFrame, + 2 => MotionKind::Frame, + _ => MotionKind::DualFrame, } }; if intra && hdr.concealment_motion_vectors { - m.kind = if field_pic { MV_FIELD16 } else { MV_FRAME }; + m.kind = if field_pic { MotionKind::Field16 } else { MotionKind::Frame }; } // Motion vectors. if m.fwd || (intra && hdr.concealment_motion_vectors) { @@ -869,7 +873,7 @@ impl Core { // No motion compensation: zero vector. pmv = [[[0; 2]; 2]; 2]; m.fwd = true; - m.kind = if field_pic { MV_FIELD16 } else { MV_FRAME }; + m.kind = if field_pic { MotionKind::Field16 } else { MotionKind::Frame }; m.sel = [[st.parity as u8; 2]; 2]; m.mv = [[[0; 2]; 2]; 2]; } @@ -1007,18 +1011,18 @@ impl Core { let mvv = |r: usize| (m.mv[s][r][0], m.mv[s][r][1]); let ptype = hdr.picture_type; match m.kind { - MV_FRAME => { + MotionKind::Frame => { let (x, y) = mvv(0); push(Part { dst0: 0, step: 1, rows: 16, src: self.ref_for(ptype, s, None, st, cur), field: None, src_row: (mb_y * 16) as i32, mvx: x, mvy: y, avg: false }); } - MV_FIELD_IN_FRAME => { + MotionKind::FieldInFrame => { for r in 0..2 { let (x, y) = mvv(r); let f = m.sel[s][r]; push(Part { dst0: r, step: 2, rows: 8, src: self.ref_for(ptype, s, Some(f), st, cur), field: Some(f), src_row: (mb_y * 8) as i32, mvx: x, mvy: y, avg: false }); } } - MV_DUAL_FRAME => { + MotionKind::DualFrame => { let (x, y) = mvv(0); for p in 0..2usize { let mm = if (p == 0) == hdr.top_field_first { 1 } else { 3 }; @@ -1030,20 +1034,19 @@ impl Core { push(Part { dst0: p, step: 2, rows: 8, src, field: Some(1 - p as u8), src_row: (mb_y * 8) as i32, mvx: ox, mvy: oy, avg: true }); } } - MV_FIELD16 => { + MotionKind::Field16 => { let (x, y) = mvv(0); let f = m.sel[s][0]; push(Part { dst0: 0, step: 1, rows: 16, src: self.ref_for(ptype, s, Some(f), st, cur), field: Some(f), src_row: (mb_y * 16) as i32, mvx: x, mvy: y, avg: false }); } - MV_16X8 => { + MotionKind::Mv16x8 => { for r in 0..2 { let (x, y) = mvv(r); let f = m.sel[s][r]; push(Part { dst0: 8 * r, step: 1, rows: 8, src: self.ref_for(ptype, s, Some(f), st, cur), field: Some(f), src_row: (mb_y * 16 + 8 * r) as i32, mvx: x, mvy: y, avg: false }); } } - _ => { - // MV_DUAL_FIELD + MotionKind::DualField => { let (x, y) = mvv(0); let p = st.parity as u8; let e = if p == 0 { -1 } else { 1 }; @@ -1217,10 +1220,10 @@ fn read_motion( s: usize, ) -> std::result::Result<(), SliceError> { let field_pic = hdr.structure != Structure::Frame; - let dual = matches!(m.kind, MV_DUAL_FRAME | MV_DUAL_FIELD); - let field_fmt = field_pic || m.kind != MV_FRAME; + let dual = matches!(m.kind, MotionKind::DualFrame | MotionKind::DualField); + let field_fmt = field_pic || m.kind != MotionKind::Frame; let frame_field = !field_pic && field_fmt; - let n = if matches!(m.kind, MV_FIELD_IN_FRAME | MV_16X8) { 2 } else { 1 }; + let n = if matches!(m.kind, MotionKind::FieldInFrame | MotionKind::Mv16x8) { 2 } else { 1 }; let shift = if mpeg1 && hdr.full_pel[s] { 1 } else { 0 }; for r in 0..n { if field_fmt && !dual {