Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/climatebenchpress/compressor/compressors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__all__ = [
"BitRound",
"BitRoundPco",
"FFmpeg",
"Jpeg2000",
"Sperr",
"StochRound",
Expand All @@ -14,6 +15,7 @@
from . import abc as abc
from .bitround import BitRound
from .bitround_pco import BitRoundPco
from .ffmpeg import FFmpeg
from .jpeg2000 import Jpeg2000
from .sperr import Sperr
from .stochround import StochRound
Expand Down
75 changes: 75 additions & 0 deletions src/climatebenchpress/compressor/compressors/ffmpeg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
__all__ = ["FFmpeg"]

import numpy as np
import numcodecs.astype
import numcodecs.compat
import numcodecs_wasm_fixed_offset_scale
import numcodecs_wasm_round
from numcodecs.abc import Codec
from numcodecs_combinators.stack import CodecStack

from .abc import Compressor


class FFmpeg(Compressor):
name = "ffmpeg"
description = "FFmpeg"

@staticmethod
def abs_bound_codec(
error_bound,
*,
data_min=None,
data_max=None,
**kwargs,
):
assert data_min is not None, "data_min must be provided"
assert data_max is not None, "data_max must be provided"

max_pixel_val = 2**12 - 1 # maximum pixel value for our integer encoding.

data_range = data_max - data_min

return CodecStack(
# increase precision for better rounding during linear quantization
numcodecs.astype.AsType(
encode_dtype="float64",
decode_dtype="float32",
),
# remap from [min, max] to [0, max_pixel_val]
numcodecs_wasm_fixed_offset_scale.FixedOffsetScale(
offset=data_min,
scale=data_range / max_pixel_val,
),
# round and truncate to integer values
numcodecs_wasm_round.Round(precision=1),
numcodecs.astype.AsType(
encode_dtype="uint32",
decode_dtype="float64",
),
# apply the ffmpeg codec
FFmpegCodec(),
)


class FFmpegCodec(Codec):
codec_id = "ffmpeg"

def __init__(self, *args, **kwargs):
# handle config
pass

def encode(self, buf) -> bytes:
a = numcodecs.compat.ensure_ndarray(buf)

Check failure on line 63 in src/climatebenchpress/compressor/compressors/ffmpeg.py

View workflow job for this annotation

GitHub Actions / Check

Ruff (F841)

src/climatebenchpress/compressor/compressors/ffmpeg.py:63:9: F841 Local variable `a` is assigned to but never used

# use ffmpeg to encode the array as a video

return b"encoded"

def decode(self, buf, out=None):
b = numcodecs.compat.ensure_bytes(buf)

Check failure on line 70 in src/climatebenchpress/compressor/compressors/ffmpeg.py

View workflow job for this annotation

GitHub Actions / Check

Ruff (F841)

src/climatebenchpress/compressor/compressors/ffmpeg.py:70:9: F841 Local variable `b` is assigned to but never used

# use ffmpeg to decode the data
decoded = np.array()

return numcodecs.compat.ndarray_copy(decoded, out)
Loading