forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

sample.rs168 lines, 4.6 KiBHistoryRawDownload
1//! The sample type a decoded picture is stored in: `u8` for 8-bit video,
2//! `u16` for High 10. The 8-bit instantiation is the original decoder and is
3//! kept byte-identical; `u16` shares all parsing and reconstruction and swaps
4//! only the SIMD-specialised kernels for plain scalar ones.
5
6use crate::deblock::{
7 EdgeParams, filter_chroma_h_edge8, filter_chroma_line, filter_luma_h_edge16, filter_luma_line,
8};
9use crate::decoder::{SliceCtx, compensate8, compensate16};
10use crate::dpb::Picture;
11use ec_core::error::Result;
12
13/// A picture sample.
14pub(crate) trait Sample:
15 Copy + Default + PartialEq + std::fmt::Debug + Send + Sync + 'static
16{
17 /// True for the 8-bit instantiation.
18 const IS_8: bool;
19 /// Bytes per sample in an output frame.
20 const BYTES: usize;
21 /// Append `src` to an output plane, little-endian, shifted left by `shift`.
22 fn extend_bytes(out: &mut Vec<u8>, src: &[Self], shift: u32);
23 fn to_i32(self) -> i32;
24 /// Truncating conversion; the value is known to be in range.
25 fn from_i32(v: i32) -> Self;
26 /// `Clip3(0, max, v)`; the 8-bit instantiation ignores `max`.
27 fn clip(v: i32, max: i32) -> Self;
28
29 /// Motion compensation of one partition (clause 8.4.2).
30 #[allow(clippy::too_many_arguments)]
31 fn compensate(
32 pic: &mut Picture<Self>,
33 refs: &[Picture<Self>],
34 ctx: &SliceCtx,
35 mb_x: usize,
36 mb_y: usize,
37 px: usize,
38 py: usize,
39 pw: usize,
40 ph: usize,
41 mv: [[i16; 2]; 2],
42 ref_idx: [i8; 2],
43 ) -> Result<()>;
44
45 /// Filter 16 luma samples across a horizontal edge whose q0 row starts at
46 /// `q0_row` (a uniform boundary strength). The scalar form here is the
47 /// definition; `u8` overrides it with the SIMD kernel.
48 fn filter_luma_h_edge16(
49 data: &mut [Self],
50 max: i32,
51 q0_row: usize,
52 stride: usize,
53 e: &EdgeParams,
54 ) {
55 for k in 0..16 {
56 filter_luma_line(data, max, q0_row + k, stride, e);
57 }
58 }
59
60 /// Eight-sample chroma counterpart of [`Sample::filter_luma_h_edge16`].
61 fn filter_chroma_h_edge8(
62 data: &mut [Self],
63 max: i32,
64 q0_row: usize,
65 stride: usize,
66 e: &EdgeParams,
67 ) {
68 for k in 0..8 {
69 filter_chroma_line(data, max, q0_row + k, stride, e);
70 }
71 }
72}
73
74impl Sample for u8 {
75 const IS_8: bool = true;
76 const BYTES: usize = 1;
77 #[inline]
78 fn extend_bytes(out: &mut Vec<u8>, src: &[u8], _shift: u32) {
79 out.extend_from_slice(src);
80 }
81 #[inline(always)]
82 fn to_i32(self) -> i32 {
83 i32::from(self)
84 }
85 #[inline(always)]
86 fn from_i32(v: i32) -> u8 {
87 v as u8
88 }
89 #[inline(always)]
90 fn clip(v: i32, _max: i32) -> u8 {
91 v.clamp(0, 255) as u8
92 }
93 #[allow(clippy::too_many_arguments)]
94 fn compensate(
95 pic: &mut Picture<u8>,
96 refs: &[Picture<u8>],
97 ctx: &SliceCtx,
98 mb_x: usize,
99 mb_y: usize,
100 px: usize,
101 py: usize,
102 pw: usize,
103 ph: usize,
104 mv: [[i16; 2]; 2],
105 ref_idx: [i8; 2],
106 ) -> Result<()> {
107 compensate8(pic, refs, ctx, mb_x, mb_y, px, py, pw, ph, mv, ref_idx)
108 }
109 #[inline]
110 fn filter_luma_h_edge16(
111 data: &mut [u8],
112 _max: i32,
113 q0_row: usize,
114 stride: usize,
115 e: &EdgeParams,
116 ) {
117 filter_luma_h_edge16(data, q0_row, stride, e);
118 }
119 #[inline]
120 fn filter_chroma_h_edge8(
121 data: &mut [u8],
122 _max: i32,
123 q0_row: usize,
124 stride: usize,
125 e: &EdgeParams,
126 ) {
127 filter_chroma_h_edge8(data, q0_row, stride, e);
128 }
129}
130
131impl Sample for u16 {
132 const IS_8: bool = false;
133 const BYTES: usize = 2;
134 #[inline]
135 fn extend_bytes(out: &mut Vec<u8>, src: &[u16], shift: u32) {
136 for &v in src {
137 out.extend_from_slice(&(v << shift).to_le_bytes());
138 }
139 }
140 #[inline(always)]
141 fn to_i32(self) -> i32 {
142 i32::from(self)
143 }
144 #[inline(always)]
145 fn from_i32(v: i32) -> u16 {
146 v as u16
147 }
148 #[allow(clippy::too_many_arguments)]
149 fn compensate(
150 pic: &mut Picture<u16>,
151 refs: &[Picture<u16>],
152 ctx: &SliceCtx,
153 mb_x: usize,
154 mb_y: usize,
155 px: usize,
156 py: usize,
157 pw: usize,
158 ph: usize,
159 mv: [[i16; 2]; 2],
160 ref_idx: [i8; 2],
161 ) -> Result<()> {
162 compensate16(pic, refs, ctx, mb_x, mb_y, px, py, pw, ph, mv, ref_idx)
163 }
164 #[inline(always)]
165 fn clip(v: i32, max: i32) -> u16 {
166 v.clamp(0, max) as u16
167 }
168}