From: Kostya Shishkov Date: Fri, 8 Nov 2024 18:28:09 +0000 (+0100) Subject: nihav_mpeg: fix or update clippy warnings X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=d1d0b8bb78a91ff0bd5fe5502484aaae6a5659de;p=nihav.git nihav_mpeg: fix or update clippy warnings --- diff --git a/nihav-mpeg/src/codecs/aac/coeff_read.rs b/nihav-mpeg/src/codecs/aac/coeff_read.rs index 81a1d36..8fc4853 100644 --- a/nihav-mpeg/src/codecs/aac/coeff_read.rs +++ b/nihav-mpeg/src/codecs/aac/coeff_read.rs @@ -69,24 +69,23 @@ fn decode_quads(br: &mut BitReader, cb: &Codebook, unsigned: bool, scale: f for out in dst.chunks_mut(4) { let cw = br.read_cb(cb)? as usize; if unsigned { - for i in 0..4 { - let val = AAC_QUADS[cw][i]; + for (&val, dst) in AAC_QUADS[cw].iter().zip(out.iter_mut()) { if val != 0 { - if br.read_bool()? { - out[i] = -CBRT_TAB[val as usize] * scale; - } else { - out[i] = CBRT_TAB[val as usize] * scale; - } + *dst = if br.read_bool()? { + -CBRT_TAB[val as usize] * scale + } else { + CBRT_TAB[val as usize] * scale + }; } } } else { - for i in 0..4 { - let val = AAC_QUADS[cw][i] - 1; - if val < 0 { - out[i] = -CBRT_TAB[val.unsigned_abs() as usize] * scale; - } else { - out[i] = CBRT_TAB[val.unsigned_abs() as usize] * scale; - } + for (&qval, dst) in AAC_QUADS[cw].iter().zip(out.iter_mut()) { + let val = qval - 1; + *dst = if val < 0 { + -CBRT_TAB[val.unsigned_abs() as usize] * scale + } else { + CBRT_TAB[val.unsigned_abs() as usize] * scale + }; } } } @@ -157,7 +156,7 @@ pub fn decode_scale_factor_data(br: &mut BitReader, scales: &mut [[u8; MAX_SFBS] if (sfb_cb[g][sfb] == INTENSITY_HCB) || (sfb_cb[g][sfb] == INTENSITY_HCB2) { let diff = i16::from(br.read_cb(&codebooks.scale_cb)?); scf_intensity += diff; - validate!((scf_intensity >= INTENSITY_SCALE_MIN) && (scf_intensity < INTENSITY_SCALE_MIN + 256)); + validate!((INTENSITY_SCALE_MIN..INTENSITY_SCALE_MIN + 256).contains(&scf_intensity)); scales[g][sfb] = (scf_intensity - INTENSITY_SCALE_MIN) as u8; } else if sfb_cb[g][sfb] == NOISE_HCB { if noise_pcm_flag { @@ -166,11 +165,11 @@ pub fn decode_scale_factor_data(br: &mut BitReader, scales: &mut [[u8; MAX_SFBS] } else { scf_noise += i16::from(br.read_cb(&codebooks.scale_cb)?); } - validate!((scf_noise >= NOISE_SCALE_MIN) && (scf_noise < NOISE_SCALE_MIN + 256)); + validate!((NOISE_SCALE_MIN..NOISE_SCALE_MIN + 256).contains(&scf_noise)); scales[g][sfb] = (scf_noise - NOISE_SCALE_MIN) as u8; } else { scf_normal += i16::from(br.read_cb(&codebooks.scale_cb)?); - validate!((scf_normal >= 0) && (scf_normal < 255)); + validate!((0..255).contains(&scf_normal)); scales[g][sfb] = scf_normal as u8; } } diff --git a/nihav-mpeg/src/codecs/aac/info.rs b/nihav-mpeg/src/codecs/aac/info.rs index 648b356..a9607d6 100644 --- a/nihav-mpeg/src/codecs/aac/info.rs +++ b/nihav-mpeg/src/codecs/aac/info.rs @@ -106,13 +106,12 @@ impl M4AInfo { } } fn read_object_type(br: &mut BitReader) -> DecoderResult { - let otypeidx; - if br.peek(5) == 31 { + let otypeidx = if br.peek(5) == 31 { br.skip(5)?; - otypeidx = (br.read(6)? as usize) + 32; - } else { - otypeidx = br.read(5)? as usize; - } + (br.read(6)? as usize) + 32 + } else { + br.read(5)? as usize + }; if otypeidx >= M4A_TYPES.len() { Ok(M4AType::Unknown) } else { @@ -146,12 +145,9 @@ impl M4AInfo { if (self.otype == M4AType::SBR) || (self.otype == M4AType::PS) { let ext_srate = Self::read_sampling_frequency(&mut br)?; self.otype = Self::read_object_type(&mut br)?; - let ext_chans; - if self.otype == M4AType::ER_BSAC { - ext_chans = Self::read_channel_config(&mut br)?; - } else { - ext_chans = 0; - } + let ext_chans = if self.otype == M4AType::ER_BSAC { + Self::read_channel_config(&mut br)? + } else { 0 }; self.sbr_ps_info = Some((ext_srate, ext_chans)); } diff --git a/nihav-mpeg/src/codecs/aac/mod.rs b/nihav-mpeg/src/codecs/aac/mod.rs index e37e812..fb15d95 100644 --- a/nihav-mpeg/src/codecs/aac/mod.rs +++ b/nihav-mpeg/src/codecs/aac/mod.rs @@ -11,6 +11,7 @@ mod coeff_read; use coeff_read::*; mod info; use info::*; +#[allow(clippy::needless_range_loop)] mod sbr; use sbr::{SBRHeader, SBRCodebooks, SBRState, SBRChannel, SBRDSP, sbr_read_sce, sbr_read_cpe}; #[allow(clippy::excessive_precision)] @@ -481,9 +482,7 @@ impl DSP { self.ew_buf[w * 128 + i] += src[i] * short_win[i]; } } else { // to be left-windowed - for i in 0..128 { - self.ew_buf[i] = src[i]; - } + self.ew_buf[..128].copy_from_slice(&src[..128]); } for i in 0..128 { self.ew_buf[w * 128 + i + 128] += src[i + 128] * short_win[127 - i]; @@ -505,55 +504,45 @@ impl DSP { } }, EIGHT_SHORT_SEQUENCE => { - for i in 0..SHORT_WIN_POINT0 { - dst[i] = delay[i]; - } + dst[..SHORT_WIN_POINT0].copy_from_slice(&delay[..SHORT_WIN_POINT0]); for i in SHORT_WIN_POINT0..SHORT_WIN_POINT1 { let j = i - SHORT_WIN_POINT0; dst[i] = delay[i] + self.ew_buf[j] * left_short_win[j]; } - for i in SHORT_WIN_POINT1..1024 { - let j = i - SHORT_WIN_POINT0; - dst[i] = self.ew_buf[j]; - } + dst[SHORT_WIN_POINT1..1024].copy_from_slice( + &self.ew_buf[SHORT_WIN_POINT1-SHORT_WIN_POINT0..1024-SHORT_WIN_POINT0]); }, LONG_STOP_SEQUENCE => { - for i in 0..SHORT_WIN_POINT0 { - dst[i] = delay[i]; - } + dst[..SHORT_WIN_POINT0].copy_from_slice(&delay[..SHORT_WIN_POINT0]); for i in SHORT_WIN_POINT0..SHORT_WIN_POINT1 { dst[i] = delay[i] + self.tmp[i] * left_short_win[i - SHORT_WIN_POINT0]; } - for i in SHORT_WIN_POINT1..1024 { - dst[i] = self.tmp[i]; - } + dst[SHORT_WIN_POINT1..1024].copy_from_slice(&self.tmp[SHORT_WIN_POINT1..1024]); }, _ => unreachable!(""), }; // save delay match seq { ONLY_LONG_SEQUENCE | LONG_STOP_SEQUENCE => { - for i in 0..1024 { - delay[i] = self.tmp[i + 1024] * long_win[1023 - i]; + for (dst, (&src, &win)) in delay.iter_mut() + .zip(self.tmp[1024..].iter().zip(long_win.iter().rev())) { + *dst = src * win; } }, EIGHT_SHORT_SEQUENCE => { - for i in 0..SHORT_WIN_POINT1 { // last part is already windowed - delay[i] = self.ew_buf[i + 512+64]; - } - for i in SHORT_WIN_POINT1..1024 { - delay[i] = 0.0; + // last part is already windowed + delay[..SHORT_WIN_POINT1].copy_from_slice(&self.ew_buf[512+64..][..SHORT_WIN_POINT1]); + for el in delay[SHORT_WIN_POINT1..].iter_mut() { + *el = 0.0; } }, LONG_START_SEQUENCE => { - for i in 0..SHORT_WIN_POINT0 { - delay[i] = self.tmp[i + 1024]; - } + delay[..SHORT_WIN_POINT0].copy_from_slice(&self.tmp[1024..][..SHORT_WIN_POINT0]); for i in SHORT_WIN_POINT0..SHORT_WIN_POINT1 { delay[i] = self.tmp[i + 1024] * short_win[127 - (i - SHORT_WIN_POINT0)]; } - for i in SHORT_WIN_POINT1..1024 { - delay[i] = 0.0; + for el in delay[SHORT_WIN_POINT1..].iter_mut() { + *el = 0.0; } }, _ => unreachable!(""), diff --git a/nihav-mpeg/src/codecs/aac/sbr/bs.rs b/nihav-mpeg/src/codecs/aac/sbr/bs.rs index dbce727..6513517 100644 --- a/nihav-mpeg/src/codecs/aac/sbr/bs.rs +++ b/nihav-mpeg/src/codecs/aac/sbr/bs.rs @@ -225,6 +225,7 @@ fn read_invf(br: &mut BitReader, chan: &mut SBRChannel, state: &SBRState) -> Dec } Ok(()) } +#[allow(clippy::collapsible_else_if)] fn read_envelope(br: &mut BitReader, chan: &mut SBRChannel, coupled: bool, cbs: &SBRCodebooks, state: &SBRState) -> DecoderResult<()> { let (f_cb, t_cb) = if coupled { if chan.amp_res { diff --git a/nihav-mpeg/src/codecs/aac/sbr/mod.rs b/nihav-mpeg/src/codecs/aac/sbr/mod.rs index d2477b9..cfd0c2c 100644 --- a/nihav-mpeg/src/codecs/aac/sbr/mod.rs +++ b/nihav-mpeg/src/codecs/aac/sbr/mod.rs @@ -348,6 +348,7 @@ fn generate_vdk(v_dk: &mut [usize], k0: usize, k1: usize, num_bands: usize) { } } +#[allow(clippy::comparison_chain)] fn calculate_master_frequencies(f: &mut [usize; SBR_BANDS], k0: usize, k2: usize, hdr: &SBRHeader) -> usize { if hdr.freq_scale == 0 { let (dk, num_bands) = if !hdr.alter_scale { diff --git a/nihav-mpeg/src/codecs/aac/tools.rs b/nihav-mpeg/src/codecs/aac/tools.rs index 17c4042..e00a85b 100644 --- a/nihav-mpeg/src/codecs/aac/tools.rs +++ b/nihav-mpeg/src/codecs/aac/tools.rs @@ -108,12 +108,11 @@ impl TNSCoeffs { // convert to LPC coefficients let mut b: [f32; TNS_MAX_ORDER + 1] = [0.0; TNS_MAX_ORDER + 1]; for m in 1..=self.order { + #[allow(clippy::needless_range_loop)] for i in 1..m { b[i] = self.coef[i - 1] + tmp[m - 1] * self.coef[m - i - 1]; } - for i in 1..m { - self.coef[i - 1] = b[i]; - } + self.coef[..m-1].copy_from_slice(&b[1..m]); self.coef[m - 1] = tmp[m - 1]; } } @@ -153,20 +152,20 @@ impl TNSData { let mut state = [0.0f32; 64]; let mut sidx = 32; if !self.coeffs[w][f].direction { - for m in start..end { + for coef in coeffs[start..end].iter_mut() { for i in 0..order { - coeffs[m] -= state[(sidx + i) & 63] * lpc[i]; + *coef -= state[(sidx + i) & 63] * lpc[i]; } sidx = (sidx + 63) & 63; - state[sidx] = coeffs[m]; + state[sidx] = *coef; } } else { - for m in (start..end).rev() { + for coef in coeffs[start..end].iter_mut().rev() { for i in 0..order { - coeffs[m] -= state[(sidx + i) & 63] * lpc[i]; + *coef -= state[(sidx + i) & 63] * lpc[i]; } sidx = (sidx + 63) & 63; - state[sidx] = coeffs[m]; + state[sidx] = *coef; } } } diff --git a/nihav-mpeg/src/codecs/mod.rs b/nihav-mpeg/src/codecs/mod.rs index 9d48808..62184b1 100644 --- a/nihav-mpeg/src/codecs/mod.rs +++ b/nihav-mpeg/src/codecs/mod.rs @@ -13,10 +13,7 @@ macro_rules! validate { mod mpeg4asp; #[cfg(feature="decoder_aac")] -#[allow(clippy::comparison_chain)] #[allow(clippy::excessive_precision)] -#[allow(clippy::manual_memcpy)] -#[allow(clippy::useless_let_if_seq)] mod aac; #[cfg(feature="decoder_mpa")] #[allow(clippy::excessive_precision)] diff --git a/nihav-mpeg/src/codecs/mpeg4asp/bitstream.rs b/nihav-mpeg/src/codecs/mpeg4asp/bitstream.rs index b82226b..ac3f79b 100644 --- a/nihav-mpeg/src/codecs/mpeg4asp/bitstream.rs +++ b/nihav-mpeg/src/codecs/mpeg4asp/bitstream.rs @@ -488,6 +488,7 @@ pub struct FrameDecoder { cbs: Tables, } +#[allow(clippy::needless_late_init)] impl FrameDecoder { pub fn new() -> Self { Self { @@ -685,6 +686,8 @@ unimplemented!(); self.slice_start = 0; } + #[allow(clippy::collapsible_if)] + #[allow(clippy::collapsible_else_if)] pub fn decode_mb(&mut self, src: &[u8], mb: &mut Macroblock, colo_not_coded: bool, mb_x: usize, pred: &mut PredState, qinfo: &QuantInfo) -> DecoderResult<()> { let mut br = BitReader::new(src, BitReaderMode::BE); br.skip(self.bitpos as u32)?; @@ -831,7 +834,7 @@ unimplemented!(); /* ignore alpha part for gray shape */ let quant = self.quant as i8 + dquant; - validate!(quant >= 0 && quant < 64); + validate!((0..64).contains(&quant)); let quant = quant as u8; mb.quant = quant; diff --git a/nihav-mpeg/src/codecs/mpeg4asp/dsp.rs b/nihav-mpeg/src/codecs/mpeg4asp/dsp.rs index 3353275..1367bd6 100644 --- a/nihav-mpeg/src/codecs/mpeg4asp/dsp.rs +++ b/nihav-mpeg/src/codecs/mpeg4asp/dsp.rs @@ -272,6 +272,7 @@ impl DSP { } } +#[allow(clippy::needless_late_init)] pub fn dequant_intra_block(block: &mut [i16; 64], ac_scale: u8, qinfo: &QuantInfo) { if qinfo.quant_type { for (coef, &weight) in block.iter_mut().zip(qinfo.intra_mat.iter()).skip(1) { @@ -297,6 +298,7 @@ pub fn dequant_intra_block(block: &mut [i16; 64], ac_scale: u8, qinfo: &QuantInf } } +#[allow(clippy::needless_late_init)] pub fn dequant_inter(mb: &mut Macroblock, qinfo: &QuantInfo) { let ac_scale = mb.quant; let mut cbp = mb.cbp; diff --git a/nihav-mpeg/src/codecs/mpegaudio/mp2code.rs b/nihav-mpeg/src/codecs/mpegaudio/mp2code.rs index 1c08a4e..b9a5d22 100644 --- a/nihav-mpeg/src/codecs/mpegaudio/mp2code.rs +++ b/nihav-mpeg/src/codecs/mpegaudio/mp2code.rs @@ -27,18 +27,18 @@ fn read3samples(br: &mut BitReader, qbits: i8, scale: f32, band: usize, dst: &mu let samplecode = br.read(-qbits as u8)? as usize; match qbits { -5 => { - for i in 0..3 { - dst[i][band] = unquant(GROUP3[samplecode][i], 3, scale); + for (i, &level) in GROUP3[samplecode].iter().enumerate() { + dst[i][band] = unquant(level, 3, scale); } }, -7 => { - for i in 0..3 { - dst[i][band] = unquant(GROUP5[samplecode][i], 5, scale); + for (i, &level) in GROUP5[samplecode].iter().enumerate() { + dst[i][band] = unquant(level, 5, scale); } }, -10 => { - for i in 0..3 { - dst[i][band] = unquant(GROUP9[samplecode][i], 9, scale); + for (i, &level) in GROUP9[samplecode].iter().enumerate() { + dst[i][band] = unquant(level, 9, scale); } }, _ => unreachable!(), @@ -159,10 +159,10 @@ impl MP2Data { bound -= 1; } - for band in 0..sblimit { - self.alloc[0][band] = br.read(ba_bits[band])? as u8; + for (band, &bits) in ba_bits[..sblimit].iter().enumerate() { + self.alloc[0][band] = br.read(bits)? as u8; if band < bound { - self.alloc[1][band] = br.read(ba_bits[band])? as u8; + self.alloc[1][band] = br.read(bits)? as u8; } else { self.alloc[1][band] = self.alloc[0][band]; } diff --git a/nihav-mpeg/src/codecs/mpegaudio/mp3code.rs b/nihav-mpeg/src/codecs/mpegaudio/mp3code.rs index c00696c..2ee78e5 100644 --- a/nihav-mpeg/src/codecs/mpegaudio/mp3code.rs +++ b/nihav-mpeg/src/codecs/mpegaudio/mp3code.rs @@ -234,6 +234,7 @@ pub struct MP3Data { tmp: [f32; SAMPLES / 2], } +#[allow(clippy::needless_range_loop)] impl MP3Data { pub fn new() -> Self { Self { @@ -534,6 +535,7 @@ impl MP3Data { } Ok(()) } + #[allow(clippy::manual_range_contains)] pub fn synth(&mut self, coeffs: &mut [[f32; SAMPLES]; 2], output: &mut [[[f32; 32]; 36]; 2], mode: u8, mode_ext: u8) { let channels = if mode == 3 { 1 } else { 2 }; let granules = if self.mpeg1 { 2 } else { 1 }; diff --git a/nihav-mpeg/src/lib.rs b/nihav-mpeg/src/lib.rs index 5e16125..d3a00e9 100644 --- a/nihav-mpeg/src/lib.rs +++ b/nihav-mpeg/src/lib.rs @@ -3,11 +3,6 @@ extern crate nihav_core; extern crate nihav_codec_support; #[cfg(feature="decoders")] -#[allow(clippy::collapsible_if)] -#[allow(clippy::collapsible_else_if)] -#[allow(clippy::needless_range_loop)] -#[allow(clippy::needless_late_init)] -#[allow(clippy::manual_range_contains)] #[allow(clippy::upper_case_acronyms)] mod codecs;