Skip to content

Crash when accessing stream in separate threads after upgrade to ffmpeg 8.1 #2247

@daveisfera

Description

@daveisfera

Ran into an issue when upgrading to ffmpeg 8.1 and was able to calling stream.decode() in separate threads. Worked with 8.0 but crashes on 8.1.

Here's an example reproducer:

from queue import Queue
from threading import Thread

from av import __version__, library_versions, open as av_open

"""Reproducer: concurrent decode() on the same AVCodecContext segfaults
under PyAV 17.0.1 + ffmpeg 8.1, but is tolerated by ffmpeg 8.0.
"""

URL = "https://test-streams.mux.dev/x36xhzz/url_6/193039199_mp4_h264_aac_hq_7.m3u8"


def main() -> None:
    print("av", __version__)
    print("library_versions", library_versions)

    container = av_open(URL)
    stream = container.streams.video[0]
    stream.codec_context.thread_count = 1
    stream.codec_context.thread_type = "NONE"

    q: Queue = Queue(maxsize=64)

    def decode_worker() -> None:
        while True:
            pkt = q.get()
            if pkt is None:
                return
            for _ in stream.decode(pkt):
                pass

    worker = Thread(target=decode_worker, daemon=True)
    worker.start()

    # Demux thread: hand most packets to the worker, but every Nth one decode
    # *here* — racing the worker on the same codec context. This is the same
    # pattern as _dump_packet running concurrently with _threaded_av_decode.
    n = 0
    for packet in container.demux(stream):
        if packet.pts is None:
            continue
        n += 1
        if n % 7 == 0:
            for _ in stream.decode(packet):
                pass
        else:
            q.put(packet)

    q.put(None)
    worker.join()
    container.close()
    print(f"done packets={n}")


if __name__ == "__main__":
    main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions