Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • P PyAV
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 37
    • Issues 37
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 26
    • Merge requests 26
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • PyAV
  • PyAV
  • Merge requests
  • !476

add xdecode for reuse Frame object

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed magicbear requested to merge github/fork/magicbear/decode-reuse-frame into develop 6 years ago
  • Overview 0
  • Commits 4
  • Pipelines 0
  • Changes 6

add xdecode function for decoding to reuse Frame Object, prevent python GC using large of memory.

demo:

import av

in_container = av.open(file="test.flv", format='flv')

for packet in in_container.demux():
	if packet.stream.type == "video":
		for frame in packet.stream.codec_context.xdecode(packet):
			print(frame, frame.format)
	elif packet.stream.type == "audio":
		for frame in packet.stream.codec_context.xdecode(packet):
			print(frame, frame.format)
Compare
  • develop (base)

and
  • latest version
    13f7cbd9
    4 commits, 2 years ago

6 files
+ 72
- 2

    Preferences

    File browser
    Compare changes
a‎v‎
au‎dio‎
fram‎e.pyx‎ +2 -1
co‎dec‎
conte‎xt.pyx‎ +37 -0
cont‎ainer‎
inpu‎t.pyx‎ +17 -0
vi‎deo‎
fram‎e.pyx‎ +2 -1
packe‎t.pyx‎ +7 -0
strea‎m.pyx‎ +7 -0
av/audio/frame.pyx
+ 2
- 1
  • View file @ 13f7cbd9


@@ -89,7 +89,8 @@ cdef class AudioFrame(Frame):
cdef _init_user_attributes(self):
self.layout = get_audio_layout(0, self.ptr.channel_layout)
self.format = get_audio_format(<lib.AVSampleFormat>self.ptr.format)
self._init_planes(AudioPlane)
if self.planes is None:
self._init_planes(AudioPlane)
def __repr__(self):
return '<av.%s %d, pts=%s, %d samples at %dHz, %s, %s at 0x%x>' % (
av/codec/context.pyx
+ 37
- 0
  • View file @ 13f7cbd9


@@ -265,6 +265,25 @@ cdef class CodecContext(object):
break
return out
def _send_packet_and_recv_reuse(self, Packet packet):
cdef Frame frame
cdef int res
with nogil:
res = lib.avcodec_send_packet(self.ptr, &packet.struct if packet is not None else NULL)
err_check(res)
while True:
frame = self._recv_frame()
if frame:
self._setup_decoded_frame(frame, packet)
yield frame
self._next_frame = frame
else:
break
return
cdef _prepare_frames_for_encode(self, Frame frame):
return [frame]
@@ -357,6 +376,24 @@ cdef class CodecContext(object):
res.append(frame)
return res
def xdecode(self, packet=None):
"""Decode a list of :class:`.Frame` from the given :class:`.Packet`.
If the packet is None, the buffers will be flushed. This is useful if
you do not want the library to automatically re-order frames for you
(if they are encoded with a codec that has B-frames).
"""
if not self.codec.ptr:
raise ValueError('cannot decode unknown codec')
self.open(strict=False)
for frame in self._send_packet_and_recv_reuse(packet):
yield frame
return
cdef _setup_decoded_frame(self, Frame frame, Packet packet):
# Propagate our manual times.
av/container/input.pyx
+ 17
- 0
  • View file @ 13f7cbd9


@@ -163,6 +163,23 @@ cdef class InputContainer(Container):
for frame in packet.decode():
yield frame
def xdecode(self, *args, **kwargs):
"""xdecode(streams=None, video=None, audio=None, subtitles=None, data=None)
Yields a series of :class:`.Frame` from the given set of streams::
for frame in container.decode():
# Do something with `frame`.
.. seealso:: :meth:`.StreamContainer.get` for the interpretation of
the arguments.
"""
id(kwargs) # Avoid Cython bug; see demux().
for packet in self.demux(*args, **kwargs):
for frame in packet.xdecode():
yield frame
def seek(self, offset, whence='time', backward=True, any_frame=False, stream=None):
"""Seek to a (key)frame nearsest to the given timestamp.
av/video/frame.pyx
+ 2
- 1
  • View file @ 13f7cbd9


@@ -96,7 +96,8 @@ cdef class VideoFrame(Frame):
cdef _init_user_attributes(self):
self.format = get_video_format(<lib.AVPixelFormat>self.ptr.format, self.ptr.width, self.ptr.height)
self._init_planes(VideoPlane)
if self.planes is None:
self._init_planes(VideoPlane)
def __dealloc__(self):
# The `self._buffer` member is only set if *we* allocated the buffer in `_init`,
av/packet.pyx
+ 7
- 0
  • View file @ 13f7cbd9


@@ -101,6 +101,13 @@ cdef class Packet(Buffer):
"""
return self._stream.decode(self)
def xdecode(self):
"""
Send the packet's data to the decoder and return a list of
:class:`.AudioFrame`, :class:`.VideoFrame` or :class:`.SubtitleSet`.
"""
return self._stream.xdecode(self)
@deprecation.method
def decode_one(self):
"""
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference: PyAV-Org/PyAV!481
Source branch: github/fork/magicbear/decode-reuse-frame

Menu

Explore Projects Groups Snippets