From 43c4bbd13b4a623f2f5ef34911c1c8cdd275b059 Mon Sep 17 00:00:00 2001 From: Bo Li Date: Sun, 4 Jan 2026 00:55:58 +0800 Subject: [PATCH] fix: remove duplicate function and use specific exception types - Remove duplicate _split_yuv420_planes function in hevc_feature_decoder_mv.py - Replace broad `except Exception:` with specific types (ValueError, TypeError, OSError, IndexError, AttributeError, cv2.error) in hevc_feature_decoder.py and hevc_feature_decoder_mv.py - This improves debuggability and allows KeyboardInterrupt/SystemExit to propagate --- dataloader/hevc_feature_decoder.py | 20 ++++++------- dataloader/hevc_feature_decoder_mv.py | 43 +++++---------------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/dataloader/hevc_feature_decoder.py b/dataloader/hevc_feature_decoder.py index 9f88830..1c80e6a 100644 --- a/dataloader/hevc_feature_decoder.py +++ b/dataloader/hevc_feature_decoder.py @@ -145,7 +145,7 @@ def _parse_fraction(frac: Optional[str]) -> float: a = float(a.strip()); b = float(b.strip()) return 0.0 if b == 0 else a / b return float(frac) - except Exception: + except (ValueError, TypeError, AttributeError): return 0.0 @@ -188,10 +188,10 @@ def _get(d, *keys): continue try: packets_pts.append(int(v)) - except Exception: + except (ValueError, TypeError): try: packets_pts.append(int(float(v))) - except Exception: + except (ValueError, TypeError): pass if not packets_pts: packets_pts = list(range(len(packets_list))) @@ -208,7 +208,7 @@ def _get(d, *keys): nbf = _get(viddict, "nb_frames", "@nb_frames") try: self.nb_frames = int(nbf) - except Exception: + except (ValueError, TypeError): self.nb_frames = len(packets_list) if packets_list else 0 self.width = int(_get(viddict, "width", "@width")) @@ -271,7 +271,7 @@ def close(self): self._proc = None if hasattr(self, 'DEVNULL') and self.DEVNULL: try: self.DEVNULL.close() - except Exception: pass + except OSError: pass self.DEVNULL = None def _terminate(self, timeout=1.0): @@ -386,11 +386,11 @@ def _is_i_like(self, frame_type: int, ref_off_L0: np.ndarray, ref_off_L1: np.nda _itypes_env = os.environ.get('UMT_HEVC_I_TYPES', '0') try: _itypes = {int(x) for x in _itypes_env.replace(' ', '').split(',') if x != ''} - except Exception: + except ValueError: _itypes = {0} if int(frame_type) in _itypes: return True - except Exception: + except (ValueError, TypeError): pass if STRICT_I: # In strict mode, do NOT use the fallback; rely only on frame_type flag. @@ -494,7 +494,7 @@ def _readFrame(self): # More explicit labels: common mapping 0=IDR, 1=CRA(IRAP), 2=P, 3=B _ft_map = {0: "IDR", 1: "CRA", 2: "P", 3: "B"} ft_str = _ft_map.get(_ft, str(_ft)) - except Exception: + except (ValueError, TypeError): ft_str = "NA" # optional I cache key (Y-only) to help upper layers deduplicate compute @@ -512,7 +512,7 @@ def _readFrame(self): else: _y_small = cv2.resize(cv2.cvtColor(rgb, cv2.COLOR_BGR2YUV)[:,:,0], (8, 8), interpolation=cv2.INTER_AREA) _frame_hash = hashlib.md5(_y_small.tobytes()).hexdigest() - except Exception: + except (cv2.error, ValueError, TypeError): _frame_hash = None self._lastmeta = { @@ -711,7 +711,7 @@ def read_first_gop_tensors(self, gop_size=12, need_i_rgb=True, mv_level='L0', try: _mvmax = float(max(np.abs(mv_x_L0).max(), np.abs(mv_y_L0).max())) _refmax = int(np.max(ref_off_L0)) - except Exception: + except (ValueError, TypeError): _mvmax, _refmax = -1.0, -1 _ft_id = meta.get('frame_type') _ft_str = meta.get('frame_type_str') diff --git a/dataloader/hevc_feature_decoder_mv.py b/dataloader/hevc_feature_decoder_mv.py index b2d6047..14e0224 100644 --- a/dataloader/hevc_feature_decoder_mv.py +++ b/dataloader/hevc_feature_decoder_mv.py @@ -94,35 +94,6 @@ def _split_yuv420_planes(buf: bytes, H: int, W: int, layout: str): raise ValueError(layout) -# ---------------- YUV plane parsers ---------------- -def _split_yuv420_planes(buf: bytes, H: int, W: int, layout: str): - """Return Y (H,W), U (H/2,W/2), V (H/2,W/2) for layout in {i420,yv12,nv12,nv21}.""" - nY = H*W - nUV = (H//2)*(W//2) - arr = np.frombuffer(buf, dtype=np.uint8) - if layout in ("i420","yv12"): - Y = arr[:nY].reshape(H, W) - UV = arr[nY:] - # planar U and V (each nUV) - U_planar, V_planar = (UV[:nUV], UV[nUV:]) if layout=="i420" else (UV[nUV:], UV[:nUV]) - U = U_planar.reshape(H//2, W//2) - V = V_planar.reshape(H//2, W//2) - return Y, U, V - elif layout in ("nv12","nv21"): - Y = arr[:nY].reshape(H, W) - UVint = arr[nY:].reshape(H//2, W) # interleaved per row: UVUV or VUVU - U = np.empty((H//2, W//2), dtype=np.uint8) - V = np.empty((H//2, W//2), dtype=np.uint8) - if layout == "nv12": # UVUV... - U[:] = UVint[:, 0::2] - V[:] = UVint[:, 1::2] - else: # nv21: VUVU... - V[:] = UVint[:, 0::2] - U[:] = UVint[:, 1::2] - return Y, U, V - else: - raise ValueError(layout) - # ---------------- manual YUV->BGR with matrix/range ---------------- def _upsample_uv(U, V, H, W): # nearest-neighbor 2x upsample @@ -210,7 +181,7 @@ def _auto_pick_color(yuv_buf: bytes, H: int, W: int): best_mse = mse best = (lay, mat, rng) picked_bgr = bgr - except Exception: + except (ValueError, IndexError): continue if best is None: # 回退 @@ -255,9 +226,9 @@ def __init__(self, video, parallel=1, hevc_bin=None): def close(self): if self.proc and self.proc.poll() is None: try: self.proc.stdin.close() - except Exception: pass + except OSError: pass try: self.proc.stdout.close() - except Exception: pass + except OSError: pass self.proc.terminate() self.proc = None @@ -442,7 +413,7 @@ def _parse_fraction(frac: Optional[str]) -> float: a = float(a.strip()); b = float(b.strip()) return 0.0 if b == 0 else a / b return float(frac) - except Exception: + except (ValueError, TypeError, AttributeError): return 0.0 @@ -485,10 +456,10 @@ def _get(d, *keys): continue try: packets_pts.append(int(v)) - except Exception: + except (ValueError, TypeError): try: packets_pts.append(int(float(v))) - except Exception: + except (ValueError, TypeError): pass if not packets_pts: packets_pts = list(range(len(packets_list))) @@ -505,7 +476,7 @@ def _get(d, *keys): nbf = _get(viddict, "nb_frames", "@nb_frames") try: self.nb_frames = int(nbf) - except Exception: + except (ValueError, TypeError): self.nb_frames = len(packets_list) if packets_list else 0 self.width = int(_get(viddict, "width", "@width"))