Skip to content

Commit 9fad61b

Browse files
更新typing和示例
1 parent c0bc7dd commit 9fad61b

File tree

6 files changed

+146
-74
lines changed

6 files changed

+146
-74
lines changed

README.md

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,52 @@ pip install pylibde265
5353

5454
```python
5555
"""
56-
to run this example, you need install pylibde265 and matplotlib and colour-science package.
56+
to run this example, you need install pylibde265 and matplotlib
5757
---
5858
here is my environment:
59-
matplotlib==3.10.0
60-
colour-science==0.4.6
59+
matplotlib==3.10.1
6160
"""
6261

63-
import pylibde265.pyde265
62+
import pylibde265.de265
6463
import matplotlib.pyplot as plt
65-
import colour
6664
import os
67-
68-
print(dir(pylibde265.pyde265))
69-
print(f"libde265 version: {pylibde265.pyde265.get_version()}")
65+
import numpy as np
66+
67+
def ycbcr_to_rgb(ycbcr_image):
68+
"""
69+
YCbCr --> RGB (BT.601)
70+
---
71+
72+
args:
73+
ycbcr_image: numpy.ndarray (height, width, 3) [0, 255]
74+
75+
return:
76+
numpy.ndarray (height, width, 3) [0, 255]
77+
"""
78+
ycbcr = ycbcr_image.astype(np.float32)
79+
height, width, _ = ycbcr.shape
80+
81+
inv_matrix = np.array([
82+
[1.164, 0.0, 1.596],
83+
[1.164, -0.813, -0.391],
84+
[1.164, 2.018, 0.0]
85+
])
86+
shift = np.array([16.0, 128.0, 128.0])
87+
88+
ycbcr_shifted = ycbcr - shift
89+
rgb_linear = np.dot(ycbcr_shifted, inv_matrix.T)
90+
rgb = np.clip(rgb_linear, 0, 255).astype(np.uint8)
91+
92+
return rgb
93+
94+
print(dir(pylibde265.de265))
95+
print(f"libde265 version: {pylibde265.de265.get_version()}")
96+
print(f"pylibde265 version: {pylibde265.__version__}")
7097

7198
VEDIO_PATH = "./multimedia/video/Kinkaku-ji.h265"
7299
NUMBER_OF_THREADS = os.cpu_count()
73100

74-
decoder = pylibde265.pyde265.decoder(NUMBER_OF_THREADS)
101+
decoder = pylibde265.de265.decoder(NUMBER_OF_THREADS)
75102

76103
error = decoder.load(VEDIO_PATH)
77104
frame = 0
@@ -81,21 +108,15 @@ for image_martix in decoder.decode():
81108
print(f"frame ------{frame}------")
82109
print(f"width: {decoder.w} height: {decoder.h}")
83110
print(f"chroma: {decoder.chroma} bps: {decoder.bps}")
84-
print(f"pts: {decoder.pts} ttd: {decoder.ttd} ttd_max: {decoder.ttd_max}")
85-
86-
image_martix = colour.YCbCr_to_RGB(
87-
image_martix,
88-
in_bits=8,
89-
in_int=True,
90-
in_legal=True,
91-
out_bits=8,
92-
out_legal=True,
93-
)
111+
print(f"pts: {decoder.pts} matrix_coeff: {decoder.matrix_coeff}")
112+
print(f"current TID {decoder.get_current_TID()} / {decoder.get_highest_TID()}")
113+
114+
115+
image_martix = ycbcr_to_rgb(image_martix)
94116
plt.imshow(image_martix)
95117
plt.show()
96118

97119
break
98-
99120
```
100121

101122
![example_preview.png](./multimedia/image/example.png)

running_example.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
11
"""
2-
to run this example, you need install pylibde265 and matplotlib and colour-science package.
2+
to run this example, you need install pylibde265 and matplotlib
33
---
44
here is my environment:
5-
matplotlib==3.10.0
6-
colour-science==0.4.6
5+
matplotlib==3.10.1
76
"""
87

9-
import pylibde265.pyde265
8+
import pylibde265.de265
109
import matplotlib.pyplot as plt
11-
import colour
1210
import os
13-
from pyinstrument import Profiler
11+
import numpy as np
1412

15-
profiler = Profiler()
16-
profiler.start()
13+
def ycbcr_to_rgb(ycbcr_image):
14+
"""
15+
YCbCr --> RGB (BT.601)
16+
---
17+
18+
args:
19+
ycbcr_image: numpy.ndarray (height, width, 3) [0, 255]
20+
21+
return:
22+
numpy.ndarray (height, width, 3) [0, 255]
23+
"""
24+
ycbcr = ycbcr_image.astype(np.float32)
25+
height, width, _ = ycbcr.shape
26+
27+
inv_matrix = np.array([
28+
[1.164, 0.0, 1.596],
29+
[1.164, -0.813, -0.391],
30+
[1.164, 2.018, 0.0]
31+
])
32+
shift = np.array([16.0, 128.0, 128.0])
33+
34+
ycbcr_shifted = ycbcr - shift
35+
rgb_linear = np.dot(ycbcr_shifted, inv_matrix.T)
36+
rgb = np.clip(rgb_linear, 0, 255).astype(np.uint8)
37+
38+
return rgb
1739

18-
print(dir(pylibde265.pyde265))
19-
print(f"libde265 version: {pylibde265.pyde265.get_version()}")
40+
print(dir(pylibde265.de265))
41+
print(f"libde265 version: {pylibde265.de265.get_version()}")
42+
print(f"pylibde265 version: {pylibde265.__version__}")
2043

2144
VEDIO_PATH = "./multimedia/video/Kinkaku-ji.h265"
2245
NUMBER_OF_THREADS = os.cpu_count()
2346

24-
decoder = pylibde265.pyde265.decoder(NUMBER_OF_THREADS)
47+
decoder = pylibde265.de265.decoder(NUMBER_OF_THREADS)
2548

2649
error = decoder.load(VEDIO_PATH)
2750
frame = 0
@@ -31,20 +54,12 @@
3154
print(f"frame ------{frame}------")
3255
print(f"width: {decoder.w} height: {decoder.h}")
3356
print(f"chroma: {decoder.chroma} bps: {decoder.bps}")
34-
print(f"pts: {decoder.pts} ttd: {decoder.ttd} ttd_max: {decoder.ttd_max}")
35-
36-
image_martix = colour.YCbCr_to_RGB(
37-
image_martix,
38-
in_bits=8,
39-
in_int=True,
40-
in_legal=True,
41-
out_bits=8,
42-
out_legal=True,
43-
)
44-
# plt.imshow(image_martix)
45-
# plt.show()
46-
47-
break
48-
49-
profiler.stop()
50-
print(profiler.output_text(unicode=True, color=True))
57+
print(f"pts: {decoder.pts} matrix_coeff: {decoder.matrix_coeff}")
58+
print(f"current TID {decoder.get_current_TID()} / {decoder.get_highest_TID()}")
59+
60+
61+
image_martix = ycbcr_to_rgb(image_martix)
62+
plt.imshow(image_martix)
63+
plt.show()
64+
65+
break

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@
4646
ext_modules=cythonize(ext_modules, nthreads=os.cpu_count(), annotate=True),
4747
data_files=[
4848
("", ["./libde265/build/libde265/Release/libde265.dll"]),
49-
("Lib/site-packages/pylibde265", ["typing/pyde265.pyi"]),
49+
("Lib/site-packages/pylibde265", ["typing/de265.pyi"]),
5050
],
5151
)

src/pylibde265/de265.pyx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@ from cython.parallel import prange
44
import numpy as np
55
cimport numpy as cnp
66
# from scipy.ndimage import zoom
7+
from typing import Union
78
from pylibde265 cimport de265
89

9-
def get_version()->str:
10+
def get_version() -> str:
1011
return de265.de265_get_version().decode('ascii')
1112

12-
def get_error_text(err_number:int)->str:
13+
def get_error_text(err_number: int) -> str:
1314
return de265.de265_get_error_text(err_number).decode('ascii')
1415

15-
def isOk(err_number:int)->bool:
16+
def isOk(err_number: int) -> bool:
1617
return de265.de265_isOK(err_number)
1718

18-
def set_verbosity(level:int)->None:
19+
def set_verbosity(level: int) -> None:
1920
de265.de265_set_verbosity(level)
2021

2122

2223
cdef class decoder(object):
2324
cdef int threads
2425
cdef int buffer_size
2526
cdef de265.de265_decoder_context* ctx
26-
cdef readonly int w,h,chroma,bps,pts,ttd_max,ttd
27+
cdef readonly int w,h,chroma,bps,pts,matrix_coeff
2728
cdef readonly int wC,hC
2829

2930
def __cinit__(self,int threads,int buffer_size=102400):
@@ -85,8 +86,7 @@ cdef class decoder(object):
8586
self.chroma = de265.de265_get_chroma_format(image_ptr)
8687
self.bps = de265.de265_get_bits_per_pixel(image_ptr,0)
8788
self.pts = de265.de265_get_image_PTS(image_ptr)
88-
self.ttd_max = de265.de265_get_highest_TID(self.ctx)
89-
self.ttd = de265.de265_get_current_TID(self.ctx)
89+
self.matrix_coeff = de265.de265_get_image_matrix_coefficients(image_ptr)
9090

9191
if self.chroma == 1: # 4:2:0
9292
self.wC = self.w // 2
@@ -134,3 +134,28 @@ cdef class decoder(object):
134134
def free_image(self):
135135
de265.de265_release_next_picture(self.ctx)
136136

137+
def get_highest_TID(self) -> int:
138+
return de265.de265_get_highest_TID(self.ctx)
139+
140+
def get_current_TID(self) -> int:
141+
return de265.de265_get_current_TID(self.ctx)
142+
143+
def set_limit_TID(self, ttd: int):
144+
de265.de265_set_limit_TID(self.ctx,ttd)
145+
146+
def set_framerate_ratio(self, percent: int):
147+
# 0 ~ 100 (%)
148+
de265.de265_set_framerate_ratio(self.ctx, percent)
149+
150+
def change_framerate(self, more_vs_less: int) -> int:
151+
# 1: more, -1: less
152+
return de265.de265_change_framerate(self.ctx, more_vs_less)
153+
154+
def set_parameter(self, param: int, value: Union[int, bool]):
155+
if type(value) == bool:
156+
de265.de265_set_parameter_bool(self.ctx, param, value)
157+
elif type(value) == int:
158+
de265.de265_set_parameter_int(self.ctx, param, value)
159+
160+
def get_parameter(self, param: int) -> bool:
161+
return bool(de265.de265_get_parameter_bool(self.ctx, param))

typing/de265.pyi

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Union
2+
3+
def get_version() -> str: ...
4+
def get_error_text(err_number: int) -> str: ...
5+
def isOk(err_number: int) -> bool: ...
6+
def set_verbosity(level: int) -> None: ...
7+
8+
class decoder:
9+
w: int
10+
h: int
11+
chroma: int
12+
bps: int
13+
pts: int
14+
matrix_coeff: int
15+
16+
wC: int
17+
hC: int
18+
19+
def __init__(self, threads: int, buffer_size: int) -> None: ...
20+
def load(self, vedio_path: str) -> int: ...
21+
def decode(self) -> None: ...
22+
def free_image(self) -> None: ...
23+
def get_highest_TID(self) -> int: ...
24+
def get_current_TID(self) -> int: ...
25+
def set_limit_TID(self, ttd: int): ...
26+
def set_framerate_ratio(self, percent: int): ...
27+
def change_framerate(self, more_vs_less: int) -> int: ...
28+
def set_parameter(self, param: int, value: Union[int, bool]): ...
29+
def get_parameter(self, param: int) -> bool: ...

typing/pyde265.pyi

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

0 commit comments

Comments
 (0)