Skip to content

Commit b17f940

Browse files
lgeigerWyattBlue
andcommitted
Use AVPacket directly
Co-authored-by: WyattBlue <wyattblue@auto-editor.com>
1 parent 6e91c93 commit b17f940

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

av/container/input.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,8 @@ def demux(self, *args, **kwargs):
153153

154154
i: cython.uint
155155
packet: Packet
156-
read_packet: Packet
156+
read_packet: cython.pointer[lib.AVPacket]
157157
ret: cython.int
158-
159158
self.set_timeout(self.read_timeout)
160159
try:
161160
for i in range(self.ptr.nb_streams):
@@ -167,30 +166,34 @@ def demux(self, *args, **kwargs):
167166
include_stream[i] = True
168167

169168
# Pre-allocate a AVPacket that is reused as the read buffer.
170-
read_packet = Packet()
169+
with cython.nogil:
170+
read_packet = lib.av_packet_alloc()
171+
if read_packet == cython.NULL:
172+
raise MemoryError("Could not allocate packet")
173+
171174
while True:
172175
# Reset the read buffer
173176
with cython.nogil:
174-
lib.av_packet_unref(read_packet.ptr)
177+
lib.av_packet_unref(read_packet)
175178
try:
176179
self.start_timeout()
177180
with cython.nogil:
178-
ret = lib.av_read_frame(self.ptr, read_packet.ptr)
181+
ret = lib.av_read_frame(self.ptr, read_packet)
179182
self.err_check(ret)
180183
except EOFError:
181184
break
182185

183-
if include_stream[read_packet.ptr.stream_index]:
186+
if include_stream[read_packet.stream_index]:
184187
# If AVFMTCTX_NOHEADER is set in ctx_flags, then new streams
185188
# may also appear in av_read_frame().
186189
# http://ffmpeg.org/doxygen/trunk/structAVFormatContext.html
187190
# TODO: find better way to handle this
188-
if read_packet.ptr.stream_index < len(self.streams):
191+
if read_packet.stream_index < len(self.streams):
189192
# Move the encoded data out of the read buffer into a
190193
# fresh Packet for the caller.
191194
packet = Packet()
192195
with cython.nogil:
193-
lib.av_packet_move_ref(packet.ptr, read_packet.ptr)
196+
lib.av_packet_move_ref(packet.ptr, read_packet)
194197
packet._stream = self.streams[packet.ptr.stream_index]
195198
# Keep track of this so that remuxing is easier.
196199
packet.ptr.time_base = packet._stream.ptr.time_base
@@ -207,6 +210,8 @@ def demux(self, *args, **kwargs):
207210
finally:
208211
self.set_timeout(None)
209212
free(include_stream)
213+
if read_packet != cython.NULL:
214+
lib.av_packet_free(cython.address(read_packet))
210215

211216
def decode(self, *args, **kwargs):
212217
"""decode(streams=None, video=None, audio=None, subtitles=None, data=None)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def parse_cflags(raw_flags):
202202
compiler_directives=compiler_directives,
203203
build_dir="src",
204204
include_path=["include"],
205+
annotate=True,
205206
)
206207

207208

0 commit comments

Comments
 (0)