From: Kostya Shishkov Date: Wed, 15 May 2019 09:33:18 +0000 (+0200) Subject: duck: fix some clippy warnings X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=8a7352c04dd848eea6519f0d775d98d547b90a39;p=nihav.git duck: fix some clippy warnings --- diff --git a/nihav-duck/src/codecs/dkadpcm.rs b/nihav-duck/src/codecs/dkadpcm.rs index b5871ab..d339556 100644 --- a/nihav-duck/src/codecs/dkadpcm.rs +++ b/nihav-duck/src/codecs/dkadpcm.rs @@ -16,15 +16,15 @@ impl IMAState { } } fn reset(&mut self, predictor: i16, step: u8) { - self.predictor = predictor as i32; + self.predictor = i32::from(predictor); self.step = step.min(IMA_MAX_STEP) as usize; } fn expand_sample(&mut self, nibble: u8) -> i16 { let istep = (self.step as isize) + (IMA_STEPS[nibble as usize] as isize); let sign = (nibble & 8) != 0; - let diff = (((2 * (nibble & 7) + 1) as i32) * IMA_STEP_TABLE[self.step]) >> 3; + let diff = (i32::from(2 * (nibble & 7) + 1) * IMA_STEP_TABLE[self.step]) >> 3; let sample = if !sign { self.predictor + diff } else { self.predictor - diff }; - self.predictor = sample.max(std::i16::MIN as i32).min(std::i16::MAX as i32); + self.predictor = sample.max(i32::from(std::i16::MIN)).min(i32::from(std::i16::MAX)); self.step = istep.max(0).min(IMA_MAX_STEP as isize) as usize; self.predictor as i16 } @@ -100,7 +100,7 @@ impl NADecoder for DuckADPCMDecoder { self.ch_state[0].reset(sumpred, sumstep); self.ch_state[1].reset(diffpred, diffstep); let mut last_nib = 0; - let mut diff_val: i32 = diffpred as i32; + let mut diff_val: i32 = i32::from(diffpred); for x in (0..out_block_len).step_by(2) { let nib0; let nib1; @@ -118,9 +118,9 @@ impl NADecoder for DuckADPCMDecoder { nib1 = b0 & 0xF; nib2 = b0 >> 4; } - let sum0 = self.ch_state[0].expand_sample(nib0) as i32; - let diff = self.ch_state[1].expand_sample(nib1) as i32; - let sum1 = self.ch_state[0].expand_sample(nib2) as i32; + let sum0 = i32::from(self.ch_state[0].expand_sample(nib0)); + let diff = i32::from(self.ch_state[1].expand_sample(nib1)); + let sum1 = i32::from(self.ch_state[0].expand_sample(nib2)); diff_val = (diff_val + diff) >> 1; dst[off0 + x + 0] = (sum0 + diff_val) as i16; dst[off1 + x + 0] = (sum0 - diff_val) as i16; diff --git a/nihav-duck/src/codecs/mod.rs b/nihav-duck/src/codecs/mod.rs index ccd5f36..c614b87 100644 --- a/nihav-duck/src/codecs/mod.rs +++ b/nihav-duck/src/codecs/mod.rs @@ -55,7 +55,7 @@ const DUCK_CODECS: &[DecoderInfo] = &[ ]; pub fn duck_register_all_codecs(rd: &mut RegisteredDecoders) { - for decoder in DUCK_CODECS.into_iter() { + for decoder in DUCK_CODECS.iter() { rd.add_decoder(decoder.clone()); } } diff --git a/nihav-duck/src/codecs/truemotion1.rs b/nihav-duck/src/codecs/truemotion1.rs index c01fcaa..44ff668 100644 --- a/nihav-duck/src/codecs/truemotion1.rs +++ b/nihav-duck/src/codecs/truemotion1.rs @@ -203,7 +203,7 @@ impl TM1Decoder { let cfdt = DUCK_C_FAT_DELTAS[delta_set]; let vec = DUCK_VECTABLES[table_idx - 1]; - let mut vec_iter = vec.into_iter(); + let mut vec_iter = vec.iter(); for i in 0..256 { let len = (*vec_iter.next().unwrap() as usize) >> 1; for j in 0..len { @@ -315,9 +315,9 @@ impl TM1Decoder { _ => unreachable!(), }; } else { - let cur = (dst[off + x + 0] as u32) | ((dst[off + x + 1] as u32) << 16); + let cur = u32::from(dst[off + x + 0]) | (u32::from(dst[off + x + 1]) << 16); self.vert_pred[(x >> 1) + 0] = cur; - let cur = (dst[off + x + 2] as u32) | ((dst[off + x + 3] as u32) << 16); + let cur = u32::from(dst[off + x + 2]) | (u32::from(dst[off + x + 3]) << 16); hor_pred = cur.wrapping_sub(self.vert_pred[(x >> 1) + 1]); self.vert_pred[(x >> 1) + 1] = cur; } @@ -490,15 +490,15 @@ impl TM1Decoder { _ => unreachable!(), }; } else { - let cur = (dst[off + x*4 + 0] as u32) - | ((dst[off + x*4 + 1] as u32) << 8) - | ((dst[off + x*4 + 2] as u32) << 16) - | ((dst[off + x*4 + 3] as u32) << 24); + let cur = u32::from(dst[off + x*4 + 0]) + | (u32::from(dst[off + x*4 + 1]) << 8) + | (u32::from(dst[off + x*4 + 2]) << 16) + | (u32::from(dst[off + x*4 + 3]) << 24); self.vert_pred[x + 0] = cur; - let cur = (dst[off + x*4 + 4] as u32) - | ((dst[off + x*4 + 5] as u32) << 8) - | ((dst[off + x*4 + 6] as u32) << 16) - | ((dst[off + x*4 + 7] as u32) << 24); + let cur = u32::from(dst[off + x*4 + 4]) + | (u32::from(dst[off + x*4 + 5]) << 8) + | (u32::from(dst[off + x*4 + 6]) << 16) + | (u32::from(dst[off + x*4 + 7]) << 24); hor_pred = cur.wrapping_sub(self.vert_pred[x + 1]); self.vert_pred[x + 1] = cur; } @@ -605,9 +605,7 @@ impl NADecoder for TM1Decoder { if is_intra || is_sprite { let fmt = if is_24bit { BGR0_FORMAT } else { RGB555_FORMAT }; let myinfo = NAVideoInfo::new(out_width, height, false, fmt); - let bufret = alloc_video_buffer(myinfo, 2); - if let Err(_) = bufret { return Err(DecoderError::InvalidData); } - let bufinfo = bufret.unwrap(); + let bufinfo = alloc_video_buffer(myinfo, 2)?; self.lastframe.reset(); if !is_24bit { self.lastframe.set16(bufinfo.get_vbuf16().unwrap()); @@ -640,17 +638,15 @@ impl NADecoder for TM1Decoder { } else { return Err(DecoderError::MissingReference); } - } else { - if let Some(mut buf) = self.lastframe.get24() { - let stride = buf.get_stride(0); - { - let data = buf.get_data_mut().unwrap(); - self.decode_24bit(data.as_mut_slice(), stride, out_width, height, &mut mask, &mut index)?; - } - bufinfo = NABufferType::VideoPacked(buf); - } else { - return Err(DecoderError::MissingReference); + } else if let Some(mut buf) = self.lastframe.get24() { + let stride = buf.get_stride(0); + { + let data = buf.get_data_mut().unwrap(); + self.decode_24bit(data.as_mut_slice(), stride, out_width, height, &mut mask, &mut index)?; } + bufinfo = NABufferType::VideoPacked(buf); + } else { + return Err(DecoderError::MissingReference); } let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); diff --git a/nihav-duck/src/codecs/truemotion2.rs b/nihav-duck/src/codecs/truemotion2.rs index 2e0ff5e..ecfce30 100644 --- a/nihav-duck/src/codecs/truemotion2.rs +++ b/nihav-duck/src/codecs/truemotion2.rs @@ -288,20 +288,20 @@ impl DeltaState { } fn recalc_y(&mut self, dst: &[u8], yoff: usize, ystride: usize, last: &mut [i32]) { let src = &dst[yoff+3..]; - self.dy[0] = (src[ystride * 0] as i32) - last[3]; - self.dy[1] = (src[ystride * 1] as i32) - (src[ystride * 0] as i32); - self.dy[2] = (src[ystride * 2] as i32) - (src[ystride * 1] as i32); - self.dy[3] = (src[ystride * 3] as i32) - (src[ystride * 2] as i32); + self.dy[0] = i32::from(src[ystride * 0]) - last[3]; + self.dy[1] = i32::from(src[ystride * 1]) - i32::from(src[ystride * 0]); + self.dy[2] = i32::from(src[ystride * 2]) - i32::from(src[ystride * 1]); + self.dy[3] = i32::from(src[ystride * 3]) - i32::from(src[ystride * 2]); let src = &dst[yoff + 3 * ystride..]; for x in 0..4 { - last[x] = src[x] as i32; + last[x] = i32::from(src[x]); } } fn recalc_c(&mut self, dst: &[i16], coff: usize, cstride: usize, idx: usize, last: &mut [i32]) { - self.dc[idx][0] = (dst[coff + 1] as i32) - last[1]; - self.dc[idx][1] = (dst[coff + 1 + cstride] as i32) - (dst[coff + 1] as i32); - last[0] = dst[coff + cstride + 0] as i32; - last[1] = dst[coff + cstride + 1] as i32; + self.dc[idx][0] = i32::from(dst[coff + 1]) - last[1]; + self.dc[idx][1] = i32::from(dst[coff + 1 + cstride]) - i32::from(dst[coff + 1]); + last[0] = i32::from(dst[coff + cstride + 0]); + last[1] = i32::from(dst[coff + cstride + 1]); } } @@ -318,15 +318,9 @@ impl TM2Frame { fn alloc(width: usize, height: usize) -> Self { let ystride = (width + 3) & !3; let ysize = ystride * ((height + 3) & !3); - let mut ydata = Vec::with_capacity(ysize); - ydata.resize(ysize, 0); let cstride = ystride >> 1; let csize = cstride * (((height + 3) & !3) >> 1); - let mut udata = Vec::with_capacity(csize); - udata.resize(csize, 0); - let mut vdata = Vec::with_capacity(csize); - vdata.resize(csize, 0); - Self { ydata, udata, vdata, ystride, cstride } + Self { ydata: vec![0; ysize], udata: vec![0; csize], vdata: vec![0; csize], ystride, cstride } } } @@ -357,12 +351,9 @@ impl TM2Decoder { let mut ydeltas: [i32; 16] = [0; 16]; let mut cdeltas: [[i32; 4]; 2] = [[0; 4]; 2]; - let mut lasty: Vec = Vec::with_capacity(self.width + 1); - lasty.resize(self.width + 1, 0); - let mut lastu: Vec = Vec::with_capacity(self.width/2 + 1); - lastu.resize(self.width/2 + 1, 0); - let mut lastv: Vec = Vec::with_capacity(self.width/2 + 1); - lastv.resize(self.width/2 + 1, 0); + let mut lasty: Vec = vec![0; self.width + 1]; + let mut lastu: Vec = vec![0; self.width/2 + 1]; + let mut lastv: Vec = vec![0; self.width/2 + 1]; for by in 0..bh { let mut dstate = DeltaState::default(); for bx in 0..bw { @@ -530,7 +521,7 @@ impl TM2Decoder { for y in 0..self.height { let out = &mut dst[off..]; for (x, pic) in out.chunks_exact_mut(3).take(self.width).enumerate() { - let y = self.cur_frame.ydata[ysrc + x] as i16; + let y = i16::from(self.cur_frame.ydata[ysrc + x]); let u = self.cur_frame.udata[csrc + (x >> 1)]; let v = self.cur_frame.vdata[csrc + (x >> 1)]; pic[offs[0]] = (y + u).max(0).min(255) as u8; @@ -574,9 +565,7 @@ impl NADecoder for TM2Decoder { } let myinfo = NAVideoInfo::new(self.width, self.height, false, RGB24_FORMAT); - let bufret = alloc_video_buffer(myinfo, 2); - if let Err(_) = bufret { return Err(DecoderError::InvalidData); } - let bufinfo = bufret.unwrap(); + let bufinfo = alloc_video_buffer(myinfo, 2)?; let mut buf = bufinfo.get_vbuf().unwrap(); let is_intra = self.decode_blocks()?; diff --git a/nihav-duck/src/codecs/truemotion2x.rs b/nihav-duck/src/codecs/truemotion2x.rs index 12366f5..16d1125 100644 --- a/nihav-duck/src/codecs/truemotion2x.rs +++ b/nihav-duck/src/codecs/truemotion2x.rs @@ -69,25 +69,25 @@ impl Deltas { fn get_int(&mut self, br: &mut ByteReader) -> DecoderResult { let b = self.get_val(br)?; if b != self.vq_esc - 1 { - return Ok(Self::remap(b as u16)); + return Ok(Self::remap(u16::from(b))); } let mut run = 0; let mut val; - let mut pow = self.vq_esc as u16; + let mut pow = u16::from(self.vq_esc); loop { let b = self.get_val(br)?; run += 1; if b != self.vq_esc - 1 { - val = (b as u16) * pow; + val = u16::from(b) * pow; break; } - pow *= self.vq_esc as u16; + pow *= u16::from(self.vq_esc); } for _ in 0..run { - pow /= self.vq_esc as u16; - let b = self.get_val(br)? as u16; - val += pow * (b as u16); + pow /= u16::from(self.vq_esc); + let b = u16::from(self.get_val(br)?); + val += pow * b; } Ok(Self::remap(val)) } @@ -678,9 +678,7 @@ impl NADecoder for TM2XDecoder { self.decode_frame(&src[12..][..data_size])?; let myinfo = self.info.get_properties().get_video_info().unwrap(); - let bufret = alloc_video_buffer(myinfo, 2); - if let Err(_) = bufret { return Err(DecoderError::InvalidData); } - let bufinfo = bufret.unwrap(); + let bufinfo = alloc_video_buffer(myinfo, 2)?; let mut buf = bufinfo.get_vbuf().unwrap(); self.output_frame(&mut buf); diff --git a/nihav-duck/src/codecs/truemotionrt.rs b/nihav-duck/src/codecs/truemotionrt.rs index d7b777f..ba2ff17 100644 --- a/nihav-duck/src/codecs/truemotionrt.rs +++ b/nihav-duck/src/codecs/truemotionrt.rs @@ -60,9 +60,7 @@ impl NADecoder for TMRTDecoder { let height = (hdr[5] as usize) | ((hdr[6] as usize) << 8); let myinfo = NAVideoInfo::new(width, height, false, YUV410_FORMAT); - let bufret = alloc_video_buffer(myinfo, 2); - if let Err(_) = bufret { return Err(DecoderError::InvalidData); } - let bufinfo = bufret.unwrap(); + let bufinfo = alloc_video_buffer(myinfo, 2)?; let mut buf = bufinfo.get_vbuf().unwrap(); let mut br = BitReader::new(&src[hdr_size..], src.len() - hdr_size, BitReaderMode::LE);