Skip to content

Writing packets to data stream broken in av>=17 #2223

@oakaigh

Description

@oakaigh

PyAV version: 17.0.0

The issue appears to be caused by zero-copying introduced in v17; It does not appear in v16.

Steps to reproduce:

  • Python:

    import json
    import av
    import numpy
    
    container = av.open("/tmp/sample.mp4", format="mp4", mode="w")
    stream = container.add_data_stream("bin_data")
    vstream = container.add_stream("h264")
    
    for i in range(10):
        packet = av.Packet(json.dumps("This may not work", ensure_ascii=False).encode("utf-8"))
        if i % 2 == 0:
            # works fine when literal; possible memory mgmt issues?
            packet = av.Packet(b"This works fine")    
        vframe = av.VideoFrame.from_ndarray(numpy.random.randint(0, 255, size=(120, 120, 3), dtype=numpy.uint8))
        packet.pts = i
        packet.dts = i
        packet.duration = 1
        packet.stream = stream
        container.mux(packet)
        container.mux(vstream.encode(vframe))
    
    container.close()
  • Shell:

    ffprobe -show_packets -select_streams d -show_data /tmp/sample.mp4 
    Output
    ffprobe version 5.1.8-0+deb12u1 Copyright (c) 2007-2025 the FFmpeg developers
      built with gcc 12 (Debian 12.2.0-14+deb12u1)
      configuration: --prefix=/usr --extra-version=0+deb12u1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libglslang --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librist --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --disable-sndio --enable-libjxl --enable-pocketsphinx --enable-librsvg --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-librav1e --enable-shared
      libavutil      57. 28.100 / 57. 28.100
      libavcodec     59. 37.100 / 59. 37.100
      libavformat    59. 27.100 / 59. 27.100
      libavdevice    59.  7.100 / 59.  7.100
      libavfilter     8. 44.100 /  8. 44.100
      libswscale      6.  7.100 /  6.  7.100
      libswresample   4.  7.100 /  4.  7.100
      libpostproc    56.  6.100 / 56.  6.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/tmp/sample.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        encoder         : Lavf62.3.100
      Duration: 00:00:00.00, start: 0.000000, bitrate: 7656 kb/s
      Stream #0:0[0x1](und): Data: bin_data (gpmd / 0x646D7067), 12240 kb/s
        Metadata:
          handler_name    : GoPro MET
    �[0;33mUnsupported codec with id 98314 for input stream 0
    �[0m[PACKET]
    codec_type=data
    stream_index=0
    pts=0
    pts_time=0.000000
    dts=0
    dts_time=0.000000
    duration=1
    duration_time=0.000011
    size=15
    pos=48
    flags=K_
    data=
    00000000: 5468 6973 2077 6f72 6b73 2066 696e 65    This works fine
    
    [/PACKET]
    [PACKET]
    codec_type=data
    stream_index=0
    pts=1
    pts_time=0.000011
    dts=1
    dts_time=0.000011
    duration=1
    duration_time=0.000011
    size=19
    pos=63
    flags=K_
    data=
    00000000: e0de d63f bb7f 0000 0200 0000 0000 0000  ...?............
    00000010: 2096 ef                                   ..
    
    [/PACKET]
    [PACKET]
    codec_type=data
    stream_index=0
    pts=2
    pts_time=0.000022
    dts=2
    dts_time=0.000022
    duration=1
    duration_time=0.000011
    size=15
    pos=82
    flags=K_
    data=
    00000000: 5468 6973 2077 6f72 6b73 2066 696e 65    This works fine
    
    [/PACKET]
    [PACKET]
    codec_type=data
    stream_index=0
    pts=3
    pts_time=0.000033
    dts=3
    dts_time=0.000033
    duration=1
    duration_time=0.000011
    size=19
    pos=97
    flags=K_
    data=
    00000000: 2039 d93f bb7f 0000 0200 0000 0000 0000   9.?............
    00000010: 2096 ef                                   ..
    
    [/PACKET]
    [PACKET]
    codec_type=data
    stream_index=0
    pts=4
    pts_time=0.000044
    dts=4
    dts_time=0.000044
    duration=1
    duration_time=0.000011
    size=15
    pos=116
    flags=K_
    data=
    00000000: 5468 6973 2077 6f72 6b73 2066 696e 65    This works fine
    
    [/PACKET]
    [PACKET]
    codec_type=data
    stream_index=0
    pts=5
    pts_time=0.000056
    dts=5
    dts_time=0.000056
    duration=1
    duration_time=0.000011
    size=19
    pos=131
    flags=K_
    data=
    00000000: 103a d93f bb7f 0000 0200 0000 0000 0000  .:.?............
    00000010: 2096 ef                                   ..
    
    [/PACKET]
    [PACKET]
    codec_type=data
    stream_index=0
    pts=6
    pts_time=0.000067
    dts=6
    dts_time=0.000067
    duration=1
    duration_time=0.000011
    size=15
    pos=150
    flags=K_
    data=
    00000000: 5468 6973 2077 6f72 6b73 2066 696e 65    This works fine
    
    [/PACKET]
    [PACKET]
    codec_type=data
    stream_index=0
    pts=7
    pts_time=0.000078
    dts=7
    dts_time=0.000078
    duration=1
    duration_time=0.000011
    size=19
    pos=165
    flags=K_
    data=
    00000000: b030 d93f bb7f 0000 0200 0000 0000 0000  .0.?............
    00000010: 2096 ef                                   ..
    
    [/PACKET]
    [PACKET]
    codec_type=data
    stream_index=0
    pts=8
    pts_time=0.000089
    dts=8
    dts_time=0.000089
    duration=1
    duration_time=0.000011
    size=15
    pos=184
    flags=K_
    data=
    00000000: 5468 6973 2077 6f72 6b73 2066 696e 65    This works fine
    
    [/PACKET]
    [PACKET]
    codec_type=data
    stream_index=0
    pts=9
    pts_time=0.000100
    dts=9
    dts_time=0.000100
    duration=1
    duration_time=0.000011
    size=19
    pos=199
    flags=K_
    data=
    00000000: 2254 6869 7320 6d61 7920 6e6f 7420 776f  "This may not wo
    00000010: 726b 22                                  rk"
    
    [/PACKET]
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions