|
| 1 | +# Soothe - testing framework for encoders quality |
| 2 | +# Copyright (C) 2020, Fluendo, S.A. |
| 3 | +# Author: Pablo Marcos Oltra <[email protected]>, Fluendo, S.A. |
| 4 | +# Copyright (C) 2025, Igalia, S.L. |
| 5 | +# Author: Stéphane Cerveau <[email protected]> |
| 6 | +# |
| 7 | +# This library is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public License |
| 9 | +# as published by the Free Software Foundation, either version 3 |
| 10 | +# of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This library is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public |
| 18 | +# License along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +"""Module with the decoder abstract class and the list of available decoders""" |
| 21 | + |
| 22 | + |
| 23 | +from abc import ABC, abstractmethod |
| 24 | +from functools import lru_cache |
| 25 | +from shutil import which |
| 26 | +from typing import List, Optional, Type |
| 27 | + |
| 28 | +from .codec import Codec, OutputFormat |
| 29 | +from .utils import normalize_binary_cmd |
| 30 | + |
| 31 | + |
| 32 | +class Decoder(ABC): |
| 33 | + """Base class for decoders""" |
| 34 | + |
| 35 | + name = "" |
| 36 | + codec = Codec.NONE |
| 37 | + description = "" |
| 38 | + binary = "" |
| 39 | + is_reference = False |
| 40 | + outputs_y4m = False # True if decoder outputs Y4M, False for raw YUV |
| 41 | + |
| 42 | + def __init__(self) -> None: |
| 43 | + if self.binary: |
| 44 | + self.binary = normalize_binary_cmd(self.binary) |
| 45 | + |
| 46 | + @abstractmethod |
| 47 | + def decode( |
| 48 | + self, |
| 49 | + input_filepath: str, |
| 50 | + output_filepath: str, |
| 51 | + output_format: OutputFormat, |
| 52 | + timeout: int, |
| 53 | + verbose: bool, |
| 54 | + ) -> str: |
| 55 | + """Decodes input_filepath in output_filepath""" |
| 56 | + raise Exception("Not implemented") |
| 57 | + |
| 58 | + @lru_cache(maxsize=128) |
| 59 | + def check(self, verbose: bool) -> bool: |
| 60 | + """Checks whether the decoder can be run""" |
| 61 | + if hasattr(self, "binary") and self.binary: |
| 62 | + try: |
| 63 | + path = which(self.binary) |
| 64 | + if verbose and not path: |
| 65 | + print(f"Binary {self.binary} can't be found to be " |
| 66 | + f"executed") |
| 67 | + |
| 68 | + return path is not None |
| 69 | + except Exception: |
| 70 | + return False |
| 71 | + return True |
| 72 | + |
| 73 | + def __str__(self) -> str: |
| 74 | + return f" {self.name}: {self.description}" |
| 75 | + |
| 76 | + |
| 77 | +DECODERS: List[Decoder] = [] |
| 78 | + |
| 79 | + |
| 80 | +def register_decoder(cls: Type[Decoder]) -> Type[Decoder]: |
| 81 | + """Register a new decoder implementation""" |
| 82 | + DECODERS.append(cls()) |
| 83 | + DECODERS.sort(key=lambda dec: dec.name) |
| 84 | + return cls |
| 85 | + |
| 86 | + |
| 87 | +def get_reference_decoder_for_codec(codec: Codec) -> Optional["Decoder"]: |
| 88 | + """Find the reference decoder for a specific codec""" |
| 89 | + |
| 90 | + reference_decoders = [d for d in DECODERS if d.codec == codec and |
| 91 | + d.is_reference] |
| 92 | + |
| 93 | + if not reference_decoders: |
| 94 | + return None |
| 95 | + if len(reference_decoders) > 1: |
| 96 | + print(f"Multiple reference decoders found for codec {codec.name}") |
| 97 | + |
| 98 | + return reference_decoders[0] |
0 commit comments