1 use nihav_core::frame::*;
2 use nihav_codec_support::codecs::MV;
3 use super::super::SimpleFrame;
5 macro_rules! module_selector {
6 ($( ($cond:meta, $module:ident) ),*) => {
7 module_selector!(list; r#false; $(($cond, $module)),*);
9 (list; $nocond:meta; ($ccar:meta, $carmod:ident), $(($condcdr:meta, $cdrmod:ident)),*) => {
10 module_selector!(single; $nocond; $ccar; $carmod);
11 module_selector!(list; any($nocond, $ccar); $(($condcdr, $cdrmod)),*);
13 (list; $nocond:meta; ($yescond:meta, $module:ident)) => {
14 module_selector!(single; $nocond; $yescond; $module);
16 (list; $_:meta; ) => {};
17 (single; $nocond:meta; $yescond:meta; $module:ident) => {
18 #[cfg(all(not($nocond), $yescond))]
20 #[cfg(all(not($nocond), $yescond))]
26 (all(feature = "simd", target_arch = "x86_64"), x86),
27 (debug_assertions, debug),
28 (not(debug_assertions), release)
31 type MCFunc = fn (dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, h: usize);
33 fn clip_u8(val: i16) -> u8 { val.max(0).min(255) as u8 }
36 fn register_simd(&mut self);
47 pub fn new() -> Self {
49 let blk = std::mem::MaybeUninit::uninit();
55 #[allow(clippy::type_complexity)]
57 avg_buf: NAVideoBufferRef<u8>,
58 pub put_block_weighted: [fn (dst: &mut [u8], stride: usize, src: &[u8], h: usize, wparams: [i8; 3]); 4],
59 pub put_block_weighted2: [fn (dst: &mut [u8], stride: usize, src0: &[u8], src1: &[u8], h: usize, wparams: [i8; 5]); 4],
60 pub chroma_interp: [fn (dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, dx: u16, dy: u16, h: usize); 3],
61 avg: [fn (dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bh: usize); 4],
68 pub fn new(avg_buf: NAVideoBufferRef<u8>) -> Self {
71 put_block_weighted: [put_blk_w_2, put_blk_w_4, put_blk_w_8, put_blk_w_16],
72 put_block_weighted2: [put_blk_w2_2, put_blk_w2_4, put_blk_w2_8, put_blk_w2_16],
73 chroma_interp: [chroma_interp_2, chroma_interp_4, chroma_interp_8],
74 avg: [avg_2, avg_4, avg_8, avg_16],
80 pub fn set_dimensions(&mut self, width: usize, height: usize) {
84 pub fn do_mc(&mut self, frm: &mut NASimpleVideoFrame<u8>, refpic: &SimpleFrame, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV) {
85 let mut ebuf = [0u8; 22 * 22];
88 let mode = ((mv.x & 3) + (mv.y & 3) * 4) as usize;
89 let pre = if mode != 0 { 2isize } else { 0 };
90 let post = if mode != 0 { 3isize } else { 0 };
91 let (yw, yh) = (self.width, self.height);
92 let src = refpic.data;
93 let systride = refpic.stride[0];
94 let src_x = (xpos as isize) + (mvx as isize);
95 let src_y = (ypos as isize) + (mvy as isize);
96 let (ysrc, ystride) = if (src_x - pre < 0) || (src_x + (w as isize) + post > (yw as isize)) || (src_y - pre < 0) || (src_y + (h as isize) + post > (yh as isize)) {
97 let add = (pre + post) as usize;
98 edge_emu_sf(refpic, src_x - pre, src_y - pre, yw, yh, w + add, h + add, &mut ebuf, 22, 0);
101 (&src[refpic.offset[0] + ((src_x - pre) as usize) + ((src_y - pre) as usize) * systride..], systride)
103 let wmode = match w {
108 (H264_LUMA_INTERP[wmode][mode])(&mut frm.data[frm.offset[0] + xpos + ypos * frm.stride[0]..], frm.stride[0], ysrc, ystride, h);
110 let (cw, ch) = (self.width >> 1, self.height >> 1);
113 let dx = (mv.x & 7) as u16;
114 let dy = (mv.y & 7) as u16;
115 let src_x = ((xpos >> 1) as isize) + (mvx as isize);
116 let src_y = ((ypos >> 1) as isize) + (mvy as isize);
117 let suoff = refpic.offset[1];
118 let svoff = refpic.offset[2];
119 let sustride = refpic.stride[1];
120 let svstride = refpic.stride[2];
123 let (csrc, cstride) = if (src_x < 0) || (src_x + (cbw as isize) + 1 > (cw as isize)) || (src_y < 0) || (src_y + (cbh as isize) + 1 > (ch as isize)) {
124 let aw = (cw + 7) & !7;
125 let ah = (ch + 7) & !7;
126 edge_emu_sf(refpic, src_x, src_y, aw, ah, cbw+1, cbh+1, &mut ebuf, 18, 1);
127 edge_emu_sf(refpic, src_x, src_y, aw, ah, cbw+1, cbh+1, &mut ebuf[9..], 18, 2);
128 ([&ebuf, &ebuf[9..]], [18, 18])
130 ([&src[suoff + (src_x as usize) + (src_y as usize) * sustride..],
131 &src[svoff + (src_x as usize) + (src_y as usize) * svstride..]],
132 [sustride, svstride])
135 let off = frm.offset[chroma] + xpos / 2 + (ypos / 2) * frm.stride[chroma];
136 (self.chroma_interp[wmode])(&mut frm.data[off..], frm.stride[chroma], csrc[chroma - 1], cstride[chroma - 1], dx, dy, cbh);
140 pub fn mc_blocks(&mut self, dst: &mut McBlock, refpic: &SimpleFrame, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV) {
141 let mode = ((mv.x & 3) + (mv.y & 3) * 4) as usize;
143 let pre = if mode != 0 { 2 } else { 0 };
144 let post = if mode != 0 { 3 } else { 0 };
145 let (width, height) = (self.width, self.height);
146 let sx = (xpos as isize) + ((mv.x >> 2) as isize);
147 let sy = (ypos as isize) + ((mv.y >> 2) as isize);
149 const EBUF_STRIDE: usize = 32;
150 let mut ebuf = [0u8; EBUF_STRIDE * (16 + 2 + 3)];
152 let wmode = match w {
157 if (sx - pre < 0) || (sx + (w as isize) + post > (width as isize)) ||
158 (sy - pre < 0) || (sy + (h as isize) + post > (height as isize)) {
159 let edge = (pre + post) as usize;
160 edge_emu_sf(refpic, sx - pre, sy - pre, width, height, w + edge, h + edge,
161 &mut ebuf, EBUF_STRIDE, 0);
162 (H264_LUMA_INTERP[wmode][mode])(&mut dst.y, 16, &ebuf, EBUF_STRIDE, h);
164 let sstride = refpic.stride[0];
165 let soff = refpic.offset[0];
166 let sbuf = refpic.data;
167 let saddr = soff + ((sx - pre) as usize) + ((sy - pre) as usize) * sstride;
168 (H264_LUMA_INTERP[wmode][mode])(&mut dst.y, 16, &sbuf[saddr..], sstride, h);
171 let (cw, ch) = (self.width >> 1, self.height >> 1);
174 let dx = (mv.x & 7) as u16;
175 let dy = (mv.y & 7) as u16;
176 let src_x = ((xpos >> 1) as isize) + (mvx as isize);
177 let src_y = ((ypos >> 1) as isize) + (mvy as isize);
178 let suoff = refpic.offset[1];
179 let svoff = refpic.offset[2];
180 let sustride = refpic.stride[1];
181 let svstride = refpic.stride[2];
182 let src = refpic.data;
185 let (csrc, cstride) = if (src_x < 0) || (src_x + (cbw as isize) + 1 > (cw as isize)) || (src_y < 0) || (src_y + (cbh as isize) + 1 > (ch as isize)) {
186 let aw = (cw + 7) & !7;
187 let ah = (ch + 7) & !7;
188 edge_emu_sf(refpic, src_x, src_y, aw, ah, cbw+1, cbh+1, &mut ebuf, 18, 1);
189 edge_emu_sf(refpic, src_x, src_y, aw, ah, cbw+1, cbh+1, &mut ebuf[9..], 18, 2);
190 ([&ebuf, &ebuf[9..]], [18, 18])
192 ([&src[suoff + (src_x as usize) + (src_y as usize) * sustride..],
193 &src[svoff + (src_x as usize) + (src_y as usize) * svstride..]],
194 [sustride, svstride])
196 (self.chroma_interp[wmode])(&mut dst.u, 16, csrc[0], cstride[0], dx, dy, cbh);
197 (self.chroma_interp[wmode])(&mut dst.v, 16, csrc[1], cstride[1], dx, dy, cbh);
200 pub fn do_mc_avg(&mut self, frm: &mut NASimpleVideoFrame<u8>, refpic: &SimpleFrame, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV) {
201 let mut abuf = self.avg_buf.clone();
202 let stride_y = abuf.get_stride(0);
203 let stride_c = abuf.get_stride(1);
204 let off_y = abuf.get_offset(0);
205 let off_u = abuf.get_offset(1);
206 let off_v = abuf.get_offset(2);
207 let data = abuf.get_data_mut().unwrap();
208 let mut afrm = NASimpleVideoFrame {
209 width: [64, 32, 32, 0],
210 height: [64, 32, 32, 0],
212 stride: [stride_y, stride_c, stride_c, 0],
213 offset: [off_y, off_u, off_v, 0],
217 let amv = MV { x: mv.x + (xpos as i16) * 4, y: mv.y + (ypos as i16) * 4 };
218 self.do_mc(&mut afrm, refpic, 0, 0, w, h, amv);
219 let wsize = match w {
225 let src = self.avg_buf.get_data();
227 let shift = if comp == 0 { 0 } else { 1 };
228 let sstride = self.avg_buf.get_stride(comp);
229 let soff = self.avg_buf.get_offset(comp);
230 (self.avg[wsize - shift])(&mut frm.data[frm.offset[comp] + (xpos >> shift) + (ypos >> shift) * frm.stride[comp]..], frm.stride[comp], &src[soff..], sstride, h >> shift);
234 pub fn gray_block(&mut self, frm: &mut NASimpleVideoFrame<u8>, x: usize, y: usize, w: usize, h: usize) {
235 let yoff = frm.offset[0] + x + y * frm.stride[0];
236 let coff = [frm.offset[1] + x / 2 + y / 2 * frm.stride[1],
237 frm.offset[2] + x / 2 + y / 2 * frm.stride[2]];
238 for row in frm.data[yoff..].chunks_mut(frm.stride[0]).take(h) {
239 for el in row[..w].iter_mut() {
244 for row in frm.data[coff[chroma]..].chunks_mut(frm.stride[chroma + 1]).take(h / 2) {
245 for el in row[..w / 2].iter_mut() {
253 fn edge_emu_sf(src: &SimpleFrame, xpos: isize, ypos: isize, w: usize, h: usize, bw: usize, bh: usize, dst: &mut [u8], dstride: usize, comp: usize) {
254 let stride = src.stride[comp];
255 let offs = src.offset[comp];
256 let framebuf = src.data;
260 if (y as isize) + ypos < 0 { srcy = 0; }
261 else if (y as isize) + ypos >= (h as isize) { srcy = h - 1; }
262 else { srcy = ((y as isize) + ypos) as usize; }
266 if (x as isize) + xpos < 0 { srcx = 0; }
267 else if (x as isize) + xpos >= (w as isize) { srcx = w - 1; }
268 else { srcx = ((x as isize) + xpos) as usize; }
269 dst[x + y * dstride] = framebuf[offs + srcx + srcy * stride];
274 fn avg(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize) {
275 for (dline, sline) in dst.chunks_mut(dstride).zip(src.chunks(sstride)).take(bh) {
276 for (dst, src) in dline.iter_mut().zip(sline.iter()).take(bw) {
277 *dst = ((u16::from(*dst) + u16::from(*src) + 1) >> 1) as u8;
282 fn avg_2(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bh: usize) {
283 let _ = src[sstride + 1];
284 let _ = dst[dstride + 1];
285 dst[0] = ((u16::from(dst[0]) + u16::from(src[0]) + 1) >> 1) as u8;
286 dst[1] = ((u16::from(dst[1]) + u16::from(src[1]) + 1) >> 1) as u8;
287 dst[dstride] = ((u16::from(dst[dstride]) + u16::from(src[sstride]) + 1) >> 1) as u8;
288 dst[dstride + 1] = ((u16::from(dst[dstride + 1]) + u16::from(src[sstride + 1]) + 1) >> 1) as u8;
290 let _ = src[sstride * 3 + 1];
291 let _ = dst[dstride * 3 + 1];
292 dst[dstride * 2] = ((u16::from(dst[dstride * 2]) + u16::from(src[sstride * 2]) + 1) >> 1) as u8;
293 dst[dstride * 2 + 1] = ((u16::from(dst[dstride * 2 + 1]) + u16::from(src[sstride * 2 + 1]) + 1) >> 1) as u8;
294 dst[dstride * 3] = ((u16::from(dst[dstride * 3]) + u16::from(src[sstride * 3]) + 1) >> 1) as u8;
295 dst[dstride * 3 + 1] = ((u16::from(dst[dstride * 3 + 1]) + u16::from(src[sstride * 3 + 1]) + 1) >> 1) as u8;
298 fn avg_4(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bh: usize) {
299 avg(dst, dstride, src, sstride, 4, bh);
301 fn avg_8(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bh: usize) {
302 avg(dst, dstride, src, sstride, 8, bh);
304 fn avg_16(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bh: usize) {
305 avg(dst, dstride, src, sstride, 16, bh);
308 fn put_block_weighted(dst: &mut [u8], stride: usize, src: &[u8], w: usize, h: usize, wparams: [i8; 3]) {
309 let weight = i16::from(wparams[0]);
310 let offset = i16::from(wparams[1]);
311 let wshift = wparams[2] as u8;
312 let bias = (1 << wshift) >> 1;
314 for (drow, srow) in dst.chunks_mut(stride).zip(src.chunks_exact(16)).take(h) {
315 for (dst, &src) in drow[..w].iter_mut().zip(srow.iter()) {
316 *dst = clip_u8(((i16::from(src) * weight + bias) >> wshift) + offset);
321 fn put_blk_w_2(dst: &mut [u8], stride: usize, src: &[u8], h: usize, wparams: [i8; 3]) {
322 put_block_weighted(dst, stride, src, 2, h, wparams);
324 fn put_blk_w_4(dst: &mut [u8], stride: usize, src: &[u8], h: usize, wparams: [i8; 3]) {
325 put_block_weighted(dst, stride, src, 4, h, wparams);
327 fn put_blk_w_8(dst: &mut [u8], stride: usize, src: &[u8], h: usize, wparams: [i8; 3]) {
328 put_block_weighted(dst, stride, src, 8, h, wparams);
330 fn put_blk_w_16(dst: &mut [u8], stride: usize, src: &[u8], h: usize, wparams: [i8; 3]) {
331 put_block_weighted(dst, stride, src, 16, h, wparams);
334 fn put_block_weighted2(dst: &mut [u8], stride: usize, src0: &[u8], src1: &[u8], w: usize, h: usize, wparams: [i8; 5]) {
335 let weight0 = i16::from(wparams[0]);
336 let offset0 = i16::from(wparams[1]);
337 let weight1 = i16::from(wparams[2]);
338 let offset1 = i16::from(wparams[3]);
339 let wshift = (wparams[4] as u8) + 1;
340 let offset = (offset0 + offset1 + 1) >> 1;
341 let bias = (1 << wshift) >> 1;
343 for (drow, (srow0, srow1)) in dst.chunks_mut(stride).zip(src0.chunks_exact(16).zip(src1.chunks_exact(16))).take(h) {
344 for (dst, (&src0, &src1)) in drow[..w].iter_mut().zip(srow0.iter().zip(srow1.iter())) {
345 *dst = clip_u8(((i16::from(src0) * weight0 + i16::from(src1) * weight1 + bias) >> wshift) + offset);
350 fn put_blk_w2_2(dst: &mut [u8], stride: usize, src0: &[u8], src1: &[u8], h: usize, wparams: [i8; 5]) {
351 let weight0 = i16::from(wparams[0]);
352 let offset0 = i16::from(wparams[1]);
353 let weight1 = i16::from(wparams[2]);
354 let offset1 = i16::from(wparams[3]);
355 let wshift = (wparams[4] as u8) + 1;
356 let offset = (offset0 + offset1 + 1) >> 1;
357 let bias = (1 << wshift) >> 1;
359 let _ = src0[16 + 1];
360 let _ = src1[16 + 1];
361 let _ = dst[stride + 1];
362 dst[0] = clip_u8(((i16::from(src0[ 0]) * weight0 + i16::from(src1[ 0]) * weight1 + bias) >> wshift) + offset);
363 dst[1] = clip_u8(((i16::from(src0[ 1]) * weight0 + i16::from(src1[ 1]) * weight1 + bias) >> wshift) + offset);
364 dst[stride] = clip_u8(((i16::from(src0[16]) * weight0 + i16::from(src1[16]) * weight1 + bias) >> wshift) + offset);
365 dst[stride + 1] = clip_u8(((i16::from(src0[17]) * weight0 + i16::from(src1[17]) * weight1 + bias) >> wshift) + offset);
367 let _ = src0[16 * 3 + 1];
368 let _ = src1[16 * 3 + 1];
369 let _ = dst[stride * 3 + 1];
370 dst[stride * 2] = clip_u8(((i16::from(src0[32]) * weight0 + i16::from(src1[32]) * weight1 + bias) >> wshift) + offset);
371 dst[stride * 2 + 1] = clip_u8(((i16::from(src0[33]) * weight0 + i16::from(src1[33]) * weight1 + bias) >> wshift) + offset);
372 dst[stride * 3] = clip_u8(((i16::from(src0[48]) * weight0 + i16::from(src1[48]) * weight1 + bias) >> wshift) + offset);
373 dst[stride * 3 + 1] = clip_u8(((i16::from(src0[49]) * weight0 + i16::from(src1[49]) * weight1 + bias) >> wshift) + offset);
376 fn put_blk_w2_4(dst: &mut [u8], stride: usize, src0: &[u8], src1: &[u8], h: usize, wparams: [i8; 5]) {
377 put_block_weighted2(dst, stride, src0, src1, 4, h, wparams);
379 fn put_blk_w2_8(dst: &mut [u8], stride: usize, src0: &[u8], src1: &[u8], h: usize, wparams: [i8; 5]) {
380 put_block_weighted2(dst, stride, src0, src1, 8, h, wparams);
382 fn put_blk_w2_16(dst: &mut [u8], stride: usize, src0: &[u8], src1: &[u8], h: usize, wparams: [i8; 5]) {
383 put_block_weighted2(dst, stride, src0, src1, 16, h, wparams);