Skip to content

Commit cdd32eb

Browse files
committed
Set immutable properties
1 parent 4e9c7d6 commit cdd32eb

File tree

8 files changed

+43
-33
lines changed

8 files changed

+43
-33
lines changed

av/_hwdevice_registry.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

av/codec/hwaccel.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ cdef class HWAccel:
1919
cdef public bint allow_software_fallback
2020
cdef public dict options
2121
cdef public int flags
22-
cdef public str output_format
23-
cdef public int device_id
22+
cdef str _output_format
23+
cdef int _device_id

av/codec/hwaccel.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ def __init__(
130130
output_format = output_format.lower()
131131
if output_format not in {"sw", "hw"}:
132132
raise ValueError("output_format must be 'sw' or 'hw'")
133-
self.output_format = output_format
133+
self._output_format = output_format
134134

135-
self.device_id = 0
135+
self._device_id = 0
136136
if self._device_type == HWDeviceType.cuda:
137137
if device:
138138
try:
139-
self.device_id = int(device)
139+
self._device_id = int(device)
140140
except ValueError:
141-
self.device_id = 0
141+
self._device_id = 0
142142

143143
self._device = device
144144
self.allow_software_fallback = allow_software_fallback
@@ -178,6 +178,14 @@ def _initialize_hw_context(self, codec: Codec):
178178
)
179179
)
180180

181+
@property
182+
def device_id(self) -> int:
183+
return self._device_id
184+
185+
@property
186+
def output_format(self) -> str:
187+
return self._output_format
188+
181189
def create(self, codec: Codec):
182190
"""Create a new hardware accelerator context with the given codec"""
183191
if self.ptr:

av/codec/hwaccel.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ class HWConfig:
3737
def is_supported(self) -> bool: ...
3838

3939
class HWAccel:
40-
output_format: Literal["sw", "hw"]
4140
options: dict[str, object]
42-
device_id: int
4341

42+
@property
43+
def output_format(self) -> Literal["sw", "hw"]: ...
44+
@property
45+
def device_id(self) -> int: ...
4446
def __init__(
4547
self,
4648
device_type: str | HWDeviceType,

av/video/codeccontext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ def _transfer_hwframe(self, frame: Frame):
128128
return frame
129129

130130
if self.hwaccel_ctx.output_format == "hw":
131-
frame.device_id = int(getattr(self.hwaccel_ctx, "device_id", 0))
131+
vframe = cython.cast(VideoFrame, frame)
132+
vframe._device_id = int(getattr(self.hwaccel_ctx, "device_id", 0))
132133
return frame
133134

134135
frame_sw: Frame = self._alloc_next_frame()

av/video/frame.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ from av.video.reformatter cimport VideoReformatter
77

88

99
cdef class CudaContext:
10-
cdef public int device_id
11-
cdef public bool primary_ctx
10+
cdef int _device_id
11+
cdef bool _primary_ctx
1212
cdef lib.AVBufferRef* _device_ref
1313
cdef dict _frames_cache
1414

@@ -26,7 +26,7 @@ cdef class VideoFrame(Frame):
2626
cdef uint8_t *_buffer
2727
cdef object _np_buffer
2828

29-
cdef public int device_id
29+
cdef int _device_id
3030
cdef VideoReformatter reformatter
3131
cdef readonly VideoFormat format
3232

av/video/frame.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
from cython.cimports.dlpack import DLManagedTensor, kDLCPU, kDLCUDA, kDLUInt
2020
from cython.cimports.libc.stdint import int64_t, uint8_t
2121

22-
import av._hwdevice_registry as _hwreg
23-
2422

2523
@cython.cclass
2624
class CudaContext:
2725
def __cinit__(self, device_id=0, primary_ctx=True):
28-
self.device_id = int(device_id)
29-
self.primary_ctx = bool(primary_ctx)
26+
self._device_id = int(device_id)
27+
self._primary_ctx = bool(primary_ctx)
3028
self._device_ref = cython.NULL
3129
self._frames_cache = {}
3230

@@ -46,6 +44,14 @@ def __dealloc__(self):
4644
lib.av_buffer_unref(cython.address(ref))
4745
self._device_ref = cython.NULL
4846

47+
@property
48+
def device_id(self) -> int:
49+
return self._device_id
50+
51+
@property
52+
def primary_ctx(self) -> bool:
53+
return self._primary_ctx
54+
4955
@cython.cfunc
5056
def _get_device_ref(self) -> cython.pointer[lib.AVBufferRef]:
5157
device_ref: cython.pointer[lib.AVBufferRef] = self._device_ref
@@ -69,11 +75,6 @@ def _get_device_ref(self) -> cython.pointer[lib.AVBufferRef]:
6975
)
7076
)
7177

72-
_hwreg.register_cuda_hwdevice_data_ptr(
73-
cython.cast(cython.size_t, device_ref.data),
74-
self.device_id,
75-
)
76-
7778
self._device_ref = device_ref
7879
return device_ref
7980

@@ -425,6 +426,10 @@ def __repr__(self):
425426
f"{self.width}x{self.height} at 0x{id(self):x}>"
426427
)
427428

429+
@property
430+
def device_id(self) -> int:
431+
return self._device_id
432+
428433
@property
429434
def planes(self):
430435
"""

av/video/frame.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class PictureType(IntEnum):
2929
BI = 7
3030

3131
class CudaContext:
32+
@property
33+
def device_id(self) -> int: ...
34+
@property
35+
def primary_ctx(self) -> bool: ...
3236
def __init__(self, device_id: int = 0, primary_ctx: bool = True) -> None: ...
3337

3438
class VideoFrame(Frame):
@@ -48,6 +52,8 @@ class VideoFrame(Frame):
4852
def interlaced_frame(self) -> bool: ...
4953
@property
5054
def rotation(self) -> int: ...
55+
@property
56+
def device_id(self) -> int: ...
5157
def __init__(
5258
self, width: int = 0, height: int = 0, format: str = "yuv420p"
5359
) -> None: ...

0 commit comments

Comments
 (0)