Opened 7 months ago

Last modified 4 months ago

#7757 new defect

mpegtsenc: invalid computed DTS values (HEVC)

Reported by: Ace17 Owned by:
Priority: normal Component: avformat
Version: unspecified Keywords: hevc dts mpegts
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Summary of the bug:

With some HEVC bitstreams (embedded in the example below), the input AVPacket DTS/PTS are ignored, and the output stream is missing some DTS values, and repeating some other ones.

I would have expected a steady DTS increase, and PTS=DTS.
(please note that in this stream, the display order matches the coding order, although B-frames are used).

How to reproduce:

$ gcc -o muxer hevc_mux_issue.c `pkg-config libavformat libavcodec libavutil --cflags --libs`
$ ./muxer > out.ts
$ ffprobe -show_packets out.ts | grep dts= -B 2

Result:

ffprobe version 4.1-1 Copyright (c) 2007-2018 the FFmpeg developers
  built with gcc 8 (Debian 8.2.0-12)
  configuration: --prefix=/usr --extra-version=1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Input #0, mpegts, from 'yo.ts':
  Duration: 00:00:00.44, start: 0.000000, bitrate: 51 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: hevc (Main) ([6][0][0][0] / 0x0006), yuv420p(tv), 1920x1080 [SAR 1:1 DAR 16:9], 50 fps, 12.50 tbr, 90k tbn, 50 tbc
pts=0
pts_time=0.000000
dts=N/A
--
pts=3600
pts_time=0.040000
dts=3600
--
pts=7200
pts_time=0.080000
dts=3600
--
pts=10800
pts_time=0.120000
dts=10800
--
pts=14400
pts_time=0.160000
dts=10800
--
pts=18000
pts_time=0.200000
dts=18000
--
pts=21600
pts_time=0.240000
dts=18000
--
pts=25200
pts_time=0.280000
dts=25200
--
pts=28800
pts_time=0.320000
dts=25200
--
pts=32400
pts_time=0.360000
dts=32400
$

// hevc_mux_issue.c
#include <assert.h>
#include <stdio.h>

#include <libavformat/avformat.h>

typedef struct
{
  int size;
  const char* data;
} AccessUnit;

const AccessUnit accessUnits[] =
{
  {443, "\x00\x00\x00\x01\x46\x01\x10\x00\x00\x00\x01\x40\x01\x0C\x04\xFF\xFF\x01\x60\x00\x00\x03\x00\xB0\x00\x00\x03\x00\x00\x03\x00\x7B\x00\x00\x1B\x02\x40\x00\x00\x00\x01\x42\x01\x04\x01\x60\x00\x00\x03\x00\xB0\x00\x00\x03\x00\x00\x03\x00\x7B\x00\x00\xA0\x03\xC0\x80\x11\x07\xCB\x94\x6E\x49\x0A\x5B\x46\x02\x0A\x00\x00\x03\x00\x02\x00\x00\x03\x00\x65\xF0\x05\xEF\x74\x90\x00\x3D\x09\x00\x00\x16\xE3\x60\x00\x07\xA1\x20\x00\x02\xDC\x6C\x15\x00\x03\xD0\x90\x00\x01\x6E\x36\x00\x00\x7A\x12\x00\x00\x2D\xC6\xC1\xC0\x00\xF4\x24\x00\x00\x5B\x8D\x88\x00\x1E\x84\x80\x00\x0B\x71\xB0\x20\x00\x00\x00\x01\x44\x01\xC0\x73\xC0\x33\x1A\x20\xA4\x11\xC4\x7C\x8A\x28\x93\x34\x6E\x6F\x91\x48\x49\x32\x72\x79\x3E\x4F\xC9\xFC\x9F\x93\xE4\xF2\x72\x64\x90\x8A\x20\x95\xAE\x4F\x27\xD7\xE4\xFE\xBF\x27\xD7\x93\xAC\x94\x45\x22\x52\x64\xE4\xF2\x7C\x9F\x93\xF9\x3F\x27\xC9\xE4\xE4\xC9\x21\x14\x42\x55\xAE\x4F\x27\xD7\xE4\xFE\xBF\x27\xD7\x93\xAC\x94\x45\x22\x52\x64\xE4\xF2\x7C\x9F\x93\xF9\x3F\x27\xC9\xE4\xE4\xC9\x24\x42\x55\xAE\x4F\x27\xD7\xE4\xFE\xBF\x27\xD7\x93\xAC\x95\x90\x00\x00\x01\x28\x01\xAF\x70\xDE\xA0\x51\x76\x3D\xB7\x4A\x51\x44\xE8\xD4\x2E\x91\x7F\xB8\xAD\xFE\x6B\xE7\x9A\xFD\xB9\xCF\x7B\x5F\x64\x17\xF0\x5F\xF9\x87\xA1\x05\x50\x18\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x1E\xE0\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\xFA\x80"},
  {80, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE0\x25\xE2\x32\x1B\x94\xC7\x2F\xA0\xC5\x71\x7C\x41\xD0\x09\x8F\x93\xA5\x31\x85\xAE\x9F\x17\xB4\x4E\x39\xAB\x2A\xEA\xAE\x7A\x25\x7E\x0E\xC8\x38\x64\xA0\x75\xDA\x32\x96\x43\xF6\x57\xE2\x41\x7B\x9E\x6A\x0E\x54\x73\x00\x00\x7D\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x10\x10"},
  {119, "\x00\x00\x00\x01\x46\x02\x50\x00\x00\x01\x02\x02\xE0\x45\x58\x88\x80\xFD\x26\x82\xED\x64\x0F\xAA\xAB\xAD\xB6\x49\x28\x65\x13\xF4\x79\x23\xBE\x59\x0E\x9D\x7A\x37\xB5\x5B\x6A\x0E\x44\x3A\x51\x95\xE5\x60\x1C\x37\x4E\x40\xAE\x31\x18\x6F\xD6\xA0\x69\x35\x1A\x41\x26\x08\xDF\x25\x7B\x73\x0B\x3E\x9A\xA3\x7C\x06\xCB\x64\x8B\x2D\x69\xDB\xEB\xA6\xAE\xBA\xA4\x17\xEA\x53\x6B\x76\x5A\x63\x20\x98\x7D\x1D\x1E\xE2\x85\xD3\x78\x36\x60\x00\x0A\xF0\x00\x00\x03\x00\x00\x03\x00\x00\x03\x01\x01"},
  {77, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE0\x67\xD6\x34\xF0\xFD\x25\xE1\x90\x01\xB3\x94\x07\x09\x5A\x9B\xAE\x3F\x05\xB9\x7A\xCF\x2D\xE0\x00\x00\x3E\xD3\xDA\xD0\x48\xBA\xB9\xB4\x90\x74\xDF\x2D\x4E\x55\x32\x82\x03\xDC\xE7\x4E\x11\xF2\xC0\x00\x01\x4A\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\xB7\x80"},
  {156, "\x00\x00\x00\x01\x46\x01\x30\x00\x00\x01\x02\x01\xD0\x21\xD1\x77\x67\xC3\xF1\x6C\x18\xE4\xCC\x6D\xC2\xFE\x9D\x9F\xB5\x89\x1D\x9B\xFC\x78\x3F\xD8\xDB\xD0\x70\x78\x53\xDB\xBB\xEE\xFA\xC0\xFD\xCF\xDD\xE0\x8F\x89\xB1\x3C\xBE\x7B\xDA\xCA\x7E\xE1\xDC\x3B\x7E\x40\xE7\xD2\xF2\x6B\x8D\x6A\x16\xEF\x54\x52\x41\x1A\x0A\x76\x2C\xDF\x6D\x15\x7B\xE8\xBC\xD5\x8A\xE9\x72\xAE\x58\x2F\x64\xEC\x99\xA4\xDF\xED\x19\x22\x03\xA5\xCA\xBA\x7F\xAF\xF4\x9E\x6D\xC6\xDB\x08\x5F\x4E\x10\x54\x93\x99\x00\xD3\x98\x34\xCA\xC9\x71\xF8\x7E\x5A\x33\x53\x7D\xF5\xC9\xF4\xB4\xC4\xCC\x40\xB0\x16\x20\x00\x02\xF0\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x05\x44"},
  {89, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE0\xA2\x75\x4C\xD0\xAC\xFD\x1F\x04\xA5\xAB\xBE\xB8\xAA\x38\xE8\x01\xA5\xEA\x60\x89\x0C\x00\x00\x03\x00\x00\x03\x00\x35\x75\x31\x08\xEF\x23\xCE\xC0\x94\x0A\x64\x58\xC7\x29\x34\xF1\x22\x21\x21\x2E\xE3\xB3\xB2\x6B\x9C\x36\x56\x56\xB3\x6A\x00\x00\x03\x00\xCF\x54\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x02\x5A"},
  {72, "\x00\x00\x00\x01\x46\x02\x50\x00\x00\x01\x02\x02\xE0\xC2\x55\x17\x34\x6C\x43\x45\xF6\x15\x25\x87\x70\x50\x53\x41\xF7\x10\x41\xD6\x74\xF2\xDD\x25\xDF\xA7\x47\xB9\x6D\xC4\xFF\xB1\x3D\xD4\xF8\xBA\x13\xB7\x1F\x75\x82\x24\x24\x80\x00\x00\xC6\x48\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x09\xD8"},
  {145, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE0\xE2\xF5\x44\x8D\x0F\xC0\xFD\x1A\x80\x0A\xB3\xD0\xB8\xF3\x5A\x00\x00\x30\xD2\x3F\xD3\x44\xDC\x00\x00\x03\x00\x00\x03\x00\x02\xB7\x56\xA0\x00\x00\x03\x00\x08\xD7\x60\x00\x00\x03\x00\x53\x13\x03\xC6\xA3\x77\xA0\x4C\x5F\xBD\x10\xEA\xBF\xC2\x63\x88\x78\x7D\xFD\xBD\x63\x67\x42\x3F\x33\x0F\xA5\x3A\x86\xD9\x6A\xC1\xD5\x8A\xA0\xD2\xCD\xB7\x7A\xFB\xFB\x48\x21\x92\xAA\x10\xB0\xCF\xA4\x9D\x89\x79\x49\x93\x4E\x88\xDB\x0B\xBC\x40\x96\x86\x63\x09\x84\x37\xF3\x40\x00\x00\x03\x01\x51\xD0\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x12\x10"},
  {126, "\x00\x00\x00\x01\x46\x01\x30\x00\x00\x01\x02\x01\xD0\x40\xB4\x54\x4D\x09\xC0\x55\x77\xBE\xE0\x38\x05\x9F\xE6\xC4\x18\x7B\x2A\x9A\x99\xA3\x0C\xA2\x30\x7D\x53\x28\x00\x00\x03\x00\x00\x03\x00\x0D\x77\x33\x11\x09\xDA\x3D\xBD\x91\x1F\xE6\x1F\xEC\x36\xB2\x20\xB4\xDF\x67\x41\xF5\x6B\x19\xB7\xBB\x2A\x8E\x50\x0C\x6E\x93\x6B\xB8\x6F\x71\x32\xF1\xE7\x27\xD4\x07\xF1\x54\x18\x00\xC3\xFE\xD0\x02\x9D\x62\xCC\x6E\xDA\x83\x66\x61\xC0\x83\xEC\x90\x00\x00\x03\x03\x8D\xA8\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x03\x1A"},
  {129, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE1\x23\x75\x44\x48\xD0\x4B\xFD\x1B\xA8\x44\x0F\x4C\xC0\x0C\xB0\x18\xF4\x01\xF4\x00\xA4\x00\xFF\x01\x26\x2A\xD4\x55\xC7\x80\xA2\x2A\x01\xF6\x10\xBE\xA3\xB6\x27\x18\xCF\xF9\x03\xA2\xC2\x97\xEB\xFA\xBE\x9B\xEC\xA2\x37\x8E\xF1\xAA\x68\x39\xFD\x6C\xA9\xE8\xA2\xA2\x43\x71\xBA\xB8\xFD\x86\x08\x0F\xFC\xBF\x07\x93\xAC\x22\x7B\x7A\x78\x65\xD7\xE8\xFB\x7B\x41\x76\xF4\xCF\x03\x58\x10\xE7\x5A\xE7\x59\xE0\x00\x00\x0D\xAF\x80\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x1F\x50"},
  {110, "\x00\x00\x00\x01\x46\x02\x50\x00\x00\x01\x02\x02\xE1\x42\xD5\x14\x8C\xD0\xDC\x3B\x82\x14\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x01\xA8\x5F\x7C\xF1\x3F\xA1\x9E\xF4\xD1\x18\x28\x53\xAB\xFF\x00\x16\xAF\x80\x4F\xFB\xF6\x26\xB9\xBB\xAF\x7E\x93\x59\x5B\x2C\x1F\x11\x50\x6A\xED\xDA\x32\x9B\x0B\xFF\xB4\x33\xB8\x7C\xF1\xBD\xBA\xBC\xFF\x6F\x68\x45\x88\x59\xB4\xAF\xBE\x3F\x00\x00\x03\x00\x07\xAB\x80\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x02\xCE"},
  {111, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE1\x63\x75\x44\x22\x34\x14\xC0\xFD\x1B\xAA\x0B\x2A\x4C\x6E\x86\x68\x5B\x8B\x81\xD6\xC1\x38\x05\xD8\xED\x3F\xC2\x8D\xFE\xA6\x26\x11\x10\x78\x0B\xD7\xF7\xA9\xBA\x9F\x01\x9D\x53\x70\x08\x51\x5B\xFB\xEC\xFE\x52\x5F\x9C\xE7\xC7\x86\xD7\xE5\x11\x73\xA9\x46\x39\x8E\xFB\xB6\xD1\xC6\xF7\x1F\x89\x8B\xD7\xCD\x83\xFE\xD4\x4B\x86\xE0\x00\x00\x2C\xAD\x40\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x22\x60"},
  {117, "\x00\x00\x00\x01\x46\x01\x30\x00\x00\x01\x02\x01\xD0\x60\xB4\x52\x08\xD0\x4B\xD1\x8B\x19\x5B\x1A\x67\x54\xE3\x71\xD5\xE8\x5B\x09\x1C\x1C\x10\x70\x44\xB0\xF4\x4B\xFF\x05\x39\xED\x88\x3D\xC5\x41\x1F\x83\x66\xEB\xB1\x9D\x2C\x70\xB1\x10\xD3\xFC\x1B\x30\x86\xF3\xC4\xDF\x08\x4F\x3E\x76\x24\xBC\xCF\xFF\xFE\x7F\x8B\x56\x28\xBB\x04\x99\xAF\xF9\x3B\x7D\x47\x0E\x07\xFA\xAE\x48\x0E\xE6\xA5\xE1\x69\x78\xF0\x00\x00\x0C\x0A\x7C\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x04\x7C"},
  {94, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE1\xA3\x75\x42\x08\x8D\x05\x30\xE8\x7E\xDA\xF6\xCF\xD8\x00\x02\x81\x06\x24\x31\x72\xC5\x33\xCF\x40\x83\xEE\x52\x1B\xB6\x0C\x3B\x26\xC5\xA7\xED\xF1\xAD\xDF\x4E\xF6\x12\xEA\x4F\x08\x31\x3C\x50\x05\xAC\x17\xE6\x3C\xCF\x7F\x92\x35\x76\x46\x0A\x86\xFA\xD0\xF8\x00\x00\x4F\xF3\xC0\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x28\x20"},
  {86, "\x00\x00\x00\x01\x46\x02\x50\x00\x00\x01\x02\x02\xE1\xC2\xD4\x92\x08\xCD\x04\x70\x33\x85\x76\xD8\xE8\x04\xA1\x4F\xF3\xC7\xF9\x16\xEB\x3C\xB7\xC5\xE3\xFA\x53\x3F\x68\x85\x2F\x4F\x3F\xFE\xEA\xB5\x5C\x3E\xD4\xA1\xC4\xD4\xB5\xE1\x0E\x43\x8D\x68\xE7\x45\xAE\xB8\x0F\x9A\x96\xC0\x00\x00\x1A\xD1\xFC\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x58\xC0"},
  {76, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE1\xE3\x75\x20\x82\x23\x41\x4C\xF7\x86\x06\x15\x95\x98\x3A\xF4\xBE\xB7\x9C\xAD\x19\x77\x02\x18\x71\x36\x05\x15\x1D\x47\xD0\xB7\xB7\xB9\xF2\x59\xFE\x98\xDD\x38\x8A\xD3\x40\xEC\xE0\x00\x00\x03\x01\x2F\x80\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x06\xCC"},
  {145, "\x00\x00\x00\x01\x46\x01\x30\x00\x00\x01\x02\x01\xD0\x80\xB4\x52\x08\xD0\x53\x5F\x4E\x76\x5E\x38\x17\x93\x8F\xCA\x05\xB8\xB4\x7F\x85\x8F\x0E\xC9\x72\x97\xDE\xD0\x62\x06\xDA\x30\xA5\xFD\xE9\xFE\x2C\x7F\x21\xF0\x28\x39\xAC\x8E\xA7\xF8\xD8\xA6\xFD\xD4\x95\xEE\x17\x2C\x9A\xF3\x09\x99\x90\x30\x7F\x8A\x7F\xC6\xFF\xC8\xFF\x91\x84\xB5\xBF\x5B\xEF\xB2\x2F\xB4\x87\x67\x3F\x67\x3D\x1F\x43\xA4\xFF\x69\x62\xB4\x8F\x12\x33\xDD\x2F\x3B\xB6\xBE\xED\x15\x75\x0E\x59\xEF\xF4\x8A\xFE\x90\x7F\xA3\x3F\xC7\x23\x2A\x1E\x00\x00\x03\x01\x2B\x24\x60\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x30\xA0"},
  {79, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE2\x23\x75\x42\x08\x8D\x05\x30\xBC\x71\xEE\x44\x60\xD1\x74\xBD\x87\xAE\x6B\xFB\xF0\x22\xAA\x74\x38\x47\xF5\xDC\x9E\xD0\xB6\x72\x62\xE2\x77\x85\xD8\xF7\x52\xB7\x35\x98\x86\x12\xFD\xC5\x93\x00\x00\x03\x00\x11\xCF\xBF\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x07\x84"},
  {83, "\x00\x00\x00\x01\x46\x02\x50\x00\x00\x01\x02\x02\xE2\x42\xD4\x92\x08\x8D\x05\x30\xBC\x71\xEE\x44\x5F\xF8\xA6\x4B\xA9\x7E\xD3\x42\xEB\xD7\x5B\xC9\xD8\xBF\x23\xAB\x5B\x7F\xFB\xA1\x27\xDB\x1F\x81\x0F\xF9\xA7\x43\xAB\x4B\xBB\xA9\x9C\x93\xE8\xD7\x3F\x30\x91\xB5\xB0\x00\x02\x1B\x9F\xA0\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x00\x3C\x20"},
  {77, "\x00\x00\x00\x01\x46\x03\x50\x00\x00\x01\x04\x03\xE2\x63\x75\x20\x82\x23\x41\x4C\xF7\x86\x05\x0F\x74\x4C\x30\xEB\xDD\xA7\x93\x20\x8E\xAD\x57\x36\xDC\xC7\x6D\x1F\xA4\xA1\xAC\xBF\xF3\x4E\x86\x43\xE8\xF2\x35\x07\xD3\xFD\x1D\xB6\x7F\xB0\x00\x00\x03\x00\xD4\x62\xC0\x00\x00\x03\x00\x00\x03\x00\x00\x03\x00\x05\xFC"},
};

int onWrite(void* opaque, uint8_t* buf, int len)
{
  fwrite(buf, 1, len, stdout);
  return len;
}

int main()
{
  AVFormatContext* ctx = NULL;
  avformat_alloc_output_context2(&ctx, NULL, "mpegts", NULL);
  assert(ctx);
  assert(!(ctx->oformat->flags & AVFMT_NOFILE));

  AVStream* stream = avformat_new_stream(ctx, NULL);
  assert(stream);

  uint8_t writeBuffer[1024];
  ctx->pb = avio_alloc_context(writeBuffer, (int)(sizeof writeBuffer), 1, NULL, NULL, &onWrite, NULL);
  assert(ctx->pb);

  int ret = avformat_write_header(ctx, NULL);
  assert(ret >= 0);

  for(int i=0;i < sizeof(accessUnits)/sizeof(*accessUnits);++i)
  {
    AVPacket pkt;
    av_init_packet(&pkt);

    const int64_t t = (i * 90000LL) / 50;
    pkt.data = (uint8_t*)accessUnits[i].data;
    pkt.size = accessUnits[i].size;
    pkt.pts = t;
    pkt.dts = t;

    int ret = av_interleaved_write_frame(ctx, &pkt);
    assert(ret >= 0);

    av_packet_unref(&pkt);

    ++i;
  }

  av_write_trailer(ctx);

  av_free(ctx->pb);
  avformat_free_context(ctx);

  return 0;
}

Attachments (1)

ordered.hevc (1.4 KB) - added by Ace17 6 months ago.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 6 months ago by Ace17

  • Summary changed from mpegts muxer HEVC: input DTS are ignored to mpegts muxer HEVC: invalid output DTS values

comment:2 Changed 6 months ago by cehoyos

  • Keywords gop removed
  • Version changed from 4.1 to unspecified

Is the issue reproducible with current FFmpeg git head?
Is the issue reproducible with ffmpeg, the application?

comment:3 Changed 6 months ago by Ace17

Is the issue reproducible with current FFmpeg git head?

Yes.
I just reproduced the issue with git commit d227ed5d598340e719eff7156b1aa0a4469e9a6a .

Is the issue reproducible with ffmpeg, the application?

No, because the ffmpeg application fails muxing the corresponding HEVC elementary stream.

bin$ ./ffmpeg -i /tmp/ordered.hevc -vcodec copy -y output.ts
ffmpeg version git-2019-03-12-d227ed5 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8 (Debian 8.2.0-21)
  configuration: --prefix=/tmp/ffmpeg-sysroot
  libavutil      56. 26.100 / 56. 26.100
  libavcodec     58. 47.103 / 58. 47.103
  libavformat    58. 26.101 / 58. 26.101
  libavdevice    58.  6.101 / 58.  6.101
  libavfilter     7. 48.100 /  7. 48.100
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
Input #0, hevc, from '/tmp/ordered.hevc':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: hevc (Main), yuv420p(tv), 1920x1080 [SAR 1:1 DAR 16:9], 50 fps, 50 tbr, 1200k tbn, 50 tbc
Output #0, mpegts, to 'output.ts':
  Metadata:
    encoder         : Lavf58.26.101
    Stream #0:0: Video: hevc (Main), yuv420p(tv), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 50 fps, 50 tbr, 90k tbn, 1200k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mpegts @ 0x5622429eddc0] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
[mpegts @ 0x5622429eddc0] first pts value must be set
av_interleaved_write_frame(): Invalid data found when processing input
frame=    1 fps=0.0 q=-1.0 Lsize=       0kB time=-00:00:00.01 bitrate=  -0.0kbits/s speed=N/A    
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Conversion failed!
bin$

The resulting output.ts is empty, but this is another issue.

comment:4 Changed 6 months ago by cehoyos

Why do you think this is another issue?

comment:5 Changed 6 months ago by Ace17

I seems so, because the mpegts muxer is under different conditions:

A) AVPacket.pts and AVPacket.dts are fully specified, av_interleaved_write_frame doesn't report any error, and the output transport stream has invalid DTS values.

B) AVPacket.pts and AVPacket.dst are not specified, av_interleaved_write_frame reports an error and no output file is produced.

Moreover, B) might be a lot harder to get to work, as it involves reconstructing the picture display order from the HEVC elementary stream, to compute a PTS and a DTS. In A), the PTS and DTS are provided by the program.

(Of course, I might be wrong about this, and both problematic behaviors might have the same root issue.)

Version 0, edited 6 months ago by Ace17 (next)

comment:6 Changed 6 months ago by cehoyos

Please attach the file ordered.hevc.

Changed 6 months ago by Ace17

comment:7 Changed 6 months ago by Ace17

Here you go.

comment:8 Changed 6 months ago by cehoyos

Is there a reason why you don't make sure pts > dts?
pkt.pts = t + 1;

comment:9 Changed 6 months ago by Ace17

Yes,
My real use-case involves a third-party hardware encoder, which provides, under some configurations, the exact PTS&DTS values than the (greatly simplified) example program I posted.

comment:10 Changed 6 months ago by cehoyos

Did you encode the frames that you provided with the hardware encoder?

comment:11 Changed 6 months ago by Ace17

Yes.
The bitstream and the PTS,DTS values all come from the same hardware encoder.

comment:12 Changed 4 months ago by Ace17

  • Keywords mpegtsenc mux gop added; mpegts removed
  • Summary changed from mpegts muxer HEVC: invalid output DTS values to mpegtsenc: invalid computed DTS values (HEVC)

comment:13 Changed 4 months ago by cehoyos

  • Keywords mpegts added; mpegtsenc mux gop removed
Note: See TracTickets for help on using tickets.