Skip to content

Commit

Permalink
将plane直接写入np缓冲区以减少开销
Browse files Browse the repository at this point in the history
  • Loading branch information
Puiching-Memory committed Feb 2, 2025
1 parent fdffa4d commit f5fe1ea
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cache/
libde265/
*.egg-info
*.whl
*.html
.coverage
.pytest_cache
src/pylibde265/pyde265.c
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ where = ["src"]

[project]
name = "pylibde265"
#version = "0.0.1"
dynamic = ["version"]
authors = [
{ name="梦归云帆", email="[email protected]" },
]
description = "python binding implementation of libde265, based on cython"
keywords = ["libde265", "h265", "vedio", "decode", "mp4"]
description = "Decode HEVC(H.265) video in python"
keywords = ["libde265", "h265", "vedio", "decode", "mp4", "hevc", "python"]
readme = "README.md"
requires-python = ">=3.9"
dependencies = [
Expand Down
4 changes: 2 additions & 2 deletions running_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
out_bits=8,
out_legal=True,
)
plt.imshow(image_martix)
plt.show()
#plt.imshow(image_martix)
#plt.show()

break

Expand Down
8 changes: 2 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@
Extension(
"pylibde265.pyde265",
sources=["./src/pylibde265/pyde265.pyx"],
include_dirs=[
"./libde265/build/",
"./libde265/libde265/",
np.get_include()
],
include_dirs=["./libde265/build/", "./libde265/libde265/", np.get_include()],
library_dirs=["./libde265/build/libde265/Release/"],
libraries=["de265"],
)
]

setup(
name="pylibde265",
ext_modules=cythonize(ext_modules),
ext_modules=cythonize(ext_modules, nthreads=os.cpu_count(), annotate=True),
data_files=[
("", ["./libde265/build/libde265/Release/libde265.dll"]),
("Lib/site-packages/pylibde265", ["typing/pyde265.pyi"]),
Expand Down
2 changes: 1 addition & 1 deletion src/pylibde265/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#from .decode import *
__version__ = '0.0.2'
__version__ = '0.0.3'
3 changes: 1 addition & 2 deletions src/pylibde265/pyde265.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,4 @@ cdef extern from "de265.h" nogil:
# An implicit free call is made in de265_free_decoder().
# Returns false if library was not initialized before, or if 'free' was called
# more often than 'init'.
cdef de265_error de265_free()

cdef de265_error de265_free()
21 changes: 12 additions & 9 deletions src/pylibde265/pyde265.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# cython:language_level=3

from libc.stdint cimport uint32_t,int64_t,uint8_t
from libc.stdio cimport printf
import numpy as np
Expand Down Expand Up @@ -48,12 +46,14 @@ cdef class decoder(object):

return dec_error

cdef decode_frame(self):
cdef cnp.ndarray decode_frame(self):
cdef int more = 1
cdef const uint8_t* bufferY = NULL
cdef const uint8_t* bufferCb = NULL
cdef const uint8_t* bufferCr = NULL
cdef int outstride = 0
cdef int ystride = 0, cstride = 0
cdef cnp.ndarray image
cdef int i, j

while more > 0:
more = 0
Expand Down Expand Up @@ -88,17 +88,20 @@ cdef class decoder(object):
else: # chroma==0
raise ValueError(f"unsupport chroma format:{self.chroma}")

bufferY = pyde265.de265_get_image_plane(image_ptr,0,&outstride)
bufferCb = pyde265.de265_get_image_plane(image_ptr,1,&outstride)
bufferCr = pyde265.de265_get_image_plane(image_ptr,2,&outstride)

bufferY = pyde265.de265_get_image_plane(image_ptr,0,&ystride)
bufferCb = pyde265.de265_get_image_plane(image_ptr,1,&cstride)
bufferCr = pyde265.de265_get_image_plane(image_ptr,2,&cstride)
planeY = np.frombuffer(bufferY[0:self.h*self.w], dtype='uint8').reshape((self.h, self.w))
planeCb = np.frombuffer(bufferCb[0:self.hC*self.wC], dtype='uint8').reshape((self.hC, self.wC))
planeCb = zoom(planeCb,(self.w//self.wC,self.h//self.hC),order=0)
planeCr = np.frombuffer(bufferCr[0:self.hC*self.wC], dtype='uint8').reshape((self.hC, self.wC))
planeCr = zoom(planeCr,(self.w//self.wC,self.h//self.hC),order=0)

image = np.dstack((planeY,planeCb,planeCr))
image = np.empty((self.h, self.w, 3), dtype=np.uint8)
image[:, :, 0] = planeY
image[:, :, 1] = planeCb
image[:, :, 2] = planeCr

return image

Expand Down

0 comments on commit f5fe1ea

Please sign in to comment.