Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions av/container/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ def set_chapters(self, chapters):
with cython.nogil:
_free_chapters(self.ptr)

if count == 0:
self.ptr.nb_chapters = 0
self.ptr.chapters = cython.NULL
return

ch_array = cython.cast(
AVChapterPtrPtr,
lib.av_malloc(count * cython.sizeof(cython.pointer[lib.AVChapter])),
Expand Down
19 changes: 14 additions & 5 deletions av/container/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@ def __cinit__(self, *args, **kwargs):
c_options: cython.pointer[cython.pointer[lib.AVDictionary]] = cython.NULL
base_dict: Dictionary
stream_dict: Dictionary
if self.options or self.stream_options:
nb_streams_before: cython.uint = self.ptr.nb_streams
if self.stream_options and nb_streams_before == 0:
raise ValueError(
"stream_options were provided, but this format does not expose "
"its streams before avformat_find_stream_info (e.g. MPEG). "
"Per-stream options cannot be applied."
)
# Only allocate c_options when streams are already known.
if (self.options or self.stream_options) and nb_streams_before > 0:
base_dict = Dictionary(self.options)
c_options = cython.cast(
cython.pointer[cython.pointer[lib.AVDictionary]],
malloc(self.ptr.nb_streams * cython.sizeof(cython.p_void)),
malloc(nb_streams_before * cython.sizeof(cython.p_void)),
)
for i in range(self.ptr.nb_streams):
for i in range(nb_streams_before):
c_options[i] = cython.NULL
if i < len(self.stream_options) and self.stream_options:
stream_dict = base_dict.copy()
Expand All @@ -56,9 +64,8 @@ def __cinit__(self, *args, **kwargs):
self.set_timeout(None)
self.err_check(ret)

# Clean up all of our options.
if c_options:
for i in range(self.ptr.nb_streams):
for i in range(nb_streams_before):
lib.av_dict_free(cython.address(c_options[i]))
free(c_options)

Expand Down Expand Up @@ -144,6 +151,8 @@ def demux(self, *args, **kwargs):
self._assert_open()

streams: list[Stream] = self.streams.get(*args, **kwargs)
if self.ptr.nb_streams == 0:
return
include_stream: cython.pointer[cython.bint] = cython.cast(
cython.pointer[cython.bint],
malloc(self.ptr.nb_streams * cython.sizeof(bint)),
Expand Down
8 changes: 8 additions & 0 deletions tests/test_chapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ def test_set_chapters() -> None:
with av.open(path) as container:
container.set_chapters(chapters)
assert container.chapters() == chapters


def test_set_chapters_empty() -> None:
path = fate_suite("vorbis/vorbis_chapter_extension_demo.ogg")
with av.open(path) as container:
assert len(container.chapters()) == 4
container.set_chapters([])
assert container.chapters() == []
Loading