2018-06-28 08:44:43 +00:00
|
|
|
#include <Limelight.h>
|
2018-07-18 03:00:16 +00:00
|
|
|
#include "ffmpeg.h"
|
2018-09-08 22:27:21 +00:00
|
|
|
#include "streaming/streamutils.h"
|
2019-01-20 07:05:56 +00:00
|
|
|
#include "streaming/session.h"
|
|
|
|
|
2018-10-13 00:59:53 +00:00
|
|
|
#include <h264_stream.h>
|
2018-07-08 04:52:20 +00:00
|
|
|
|
2019-02-13 03:58:36 +00:00
|
|
|
#include "ffmpeg-renderers/sdlvid.h"
|
|
|
|
|
2018-07-22 00:00:09 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
2018-07-18 03:00:16 +00:00
|
|
|
#include "ffmpeg-renderers/dxva2.h"
|
2018-07-13 09:28:10 +00:00
|
|
|
#endif
|
|
|
|
|
2018-07-22 00:00:09 +00:00
|
|
|
#ifdef Q_OS_DARWIN
|
2018-07-18 03:00:16 +00:00
|
|
|
#include "ffmpeg-renderers/vt.h"
|
2018-07-22 00:00:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBVA
|
2018-07-21 07:16:03 +00:00
|
|
|
#include "ffmpeg-renderers/vaapi.h"
|
|
|
|
#endif
|
|
|
|
|
2018-08-03 09:11:44 +00:00
|
|
|
#ifdef HAVE_LIBVDPAU
|
|
|
|
#include "ffmpeg-renderers/vdpau.h"
|
|
|
|
#endif
|
|
|
|
|
2018-08-03 04:37:46 +00:00
|
|
|
// This is gross but it allows us to use sizeof()
|
|
|
|
#include "ffmpeg_videosamples.cpp"
|
2018-07-13 09:28:10 +00:00
|
|
|
|
2018-10-13 00:59:53 +00:00
|
|
|
#define MAX_SPS_EXTRA_SIZE 16
|
|
|
|
|
2018-08-10 01:39:38 +00:00
|
|
|
#define FAILED_DECODES_RESET_THRESHOLD 20
|
|
|
|
|
2018-08-03 04:37:46 +00:00
|
|
|
bool FFmpegVideoDecoder::isHardwareAccelerated()
|
|
|
|
{
|
|
|
|
return m_HwDecodeCfg != nullptr;
|
|
|
|
}
|
2018-07-13 09:28:10 +00:00
|
|
|
|
2018-08-25 19:38:04 +00:00
|
|
|
int FFmpegVideoDecoder::getDecoderCapabilities()
|
|
|
|
{
|
2019-03-30 06:38:23 +00:00
|
|
|
return m_Renderer->getDecoderCapabilities();
|
2018-08-25 19:38:04 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 04:37:46 +00:00
|
|
|
enum AVPixelFormat FFmpegVideoDecoder::ffGetFormat(AVCodecContext* context,
|
|
|
|
const enum AVPixelFormat* pixFmts)
|
|
|
|
{
|
|
|
|
FFmpegVideoDecoder* decoder = (FFmpegVideoDecoder*)context->opaque;
|
|
|
|
const enum AVPixelFormat *p;
|
2018-07-13 09:28:10 +00:00
|
|
|
|
2018-08-03 04:37:46 +00:00
|
|
|
for (p = pixFmts; *p != -1; p++) {
|
|
|
|
// Only match our hardware decoding codec or SW pixel
|
|
|
|
// format (if not using hardware decoding). It's crucial
|
|
|
|
// to override the default get_format() which will try
|
|
|
|
// to gracefully fall back to software decode and break us.
|
|
|
|
if (*p == (decoder->m_HwDecodeCfg ?
|
|
|
|
decoder->m_HwDecodeCfg->pix_fmt :
|
|
|
|
context->pix_fmt)) {
|
|
|
|
return *p;
|
2018-07-13 09:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:37:46 +00:00
|
|
|
return AV_PIX_FMT_NONE;
|
2018-07-13 09:28:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 02:42:53 +00:00
|
|
|
FFmpegVideoDecoder::FFmpegVideoDecoder(bool testOnly)
|
2018-07-18 03:00:16 +00:00
|
|
|
: m_VideoDecoderCtx(nullptr),
|
|
|
|
m_DecodeBuffer(1024 * 1024, 0),
|
|
|
|
m_HwDecodeCfg(nullptr),
|
2018-08-10 01:39:38 +00:00
|
|
|
m_Renderer(nullptr),
|
2018-08-16 06:57:03 +00:00
|
|
|
m_ConsecutiveFailedDecodes(0),
|
2018-09-25 07:47:59 +00:00
|
|
|
m_Pacer(nullptr),
|
|
|
|
m_LastFrameNumber(0),
|
2018-10-13 00:59:53 +00:00
|
|
|
m_StreamFps(0),
|
2019-02-13 02:42:53 +00:00
|
|
|
m_NeedsSpsFixup(false),
|
|
|
|
m_TestOnly(testOnly)
|
2018-07-13 09:28:10 +00:00
|
|
|
{
|
2018-07-18 03:00:16 +00:00
|
|
|
av_init_packet(&m_Pkt);
|
2018-07-13 09:28:10 +00:00
|
|
|
|
2018-09-25 07:47:59 +00:00
|
|
|
SDL_zero(m_ActiveWndVideoStats);
|
|
|
|
SDL_zero(m_LastWndVideoStats);
|
|
|
|
SDL_zero(m_GlobalVideoStats);
|
|
|
|
|
2018-07-16 09:07:32 +00:00
|
|
|
// Use linear filtering when renderer scaling is required
|
|
|
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
|
2018-07-18 03:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FFmpegVideoDecoder::~FFmpegVideoDecoder()
|
2018-08-03 04:37:46 +00:00
|
|
|
{
|
|
|
|
reset();
|
2019-02-14 02:34:59 +00:00
|
|
|
|
|
|
|
// Set log level back to default.
|
|
|
|
// NB: We don't do this in reset() because we want
|
|
|
|
// to preserve the log level across reset() during
|
|
|
|
// test initialization.
|
|
|
|
av_log_set_level(AV_LOG_INFO);
|
2018-08-03 04:37:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 05:28:59 +00:00
|
|
|
IFFmpegRenderer* FFmpegVideoDecoder::getRenderer()
|
|
|
|
{
|
|
|
|
return m_Renderer;
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:37:46 +00:00
|
|
|
void FFmpegVideoDecoder::reset()
|
2018-07-18 03:00:16 +00:00
|
|
|
{
|
2018-08-16 06:57:03 +00:00
|
|
|
delete m_Pacer;
|
|
|
|
m_Pacer = nullptr;
|
|
|
|
|
2018-08-25 18:21:52 +00:00
|
|
|
// This must be called after deleting Pacer because it
|
|
|
|
// may be holding AVFrames to free in its destructor.
|
|
|
|
// However, it must be called before deleting the IFFmpegRenderer
|
|
|
|
// since the codec context may be referencing objects that we
|
|
|
|
// need to delete in the renderer destructor.
|
|
|
|
avcodec_free_context(&m_VideoDecoderCtx);
|
|
|
|
|
2019-02-13 02:43:38 +00:00
|
|
|
if (!m_TestOnly) {
|
|
|
|
Session::get()->getOverlayManager().setOverlayRenderer(nullptr);
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
delete m_Renderer;
|
2018-08-03 04:37:46 +00:00
|
|
|
m_Renderer = nullptr;
|
2018-09-25 07:47:59 +00:00
|
|
|
|
2019-02-13 02:42:53 +00:00
|
|
|
if (!m_TestOnly) {
|
|
|
|
logVideoStats(m_GlobalVideoStats, "Global video stats");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Test-only decoders can't have any frames submitted
|
|
|
|
SDL_assert(m_GlobalVideoStats.totalFrames == 0);
|
|
|
|
}
|
2018-07-18 03:00:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 06:57:03 +00:00
|
|
|
bool FFmpegVideoDecoder::completeInitialization(AVCodec* decoder, SDL_Window* window,
|
|
|
|
int videoFormat, int width, int height,
|
2019-02-13 02:42:53 +00:00
|
|
|
int maxFps, bool enableFramePacing, bool testFrame)
|
2018-07-18 03:00:16 +00:00
|
|
|
{
|
2019-02-13 02:42:53 +00:00
|
|
|
// In test-only mode, we should only see test frames
|
|
|
|
SDL_assert(!m_TestOnly || testFrame);
|
|
|
|
|
2018-12-25 20:57:00 +00:00
|
|
|
auto vsyncConstraint = m_Renderer->getFramePacingConstraint();
|
|
|
|
if (vsyncConstraint == IFFmpegRenderer::PACING_FORCE_OFF && enableFramePacing) {
|
2018-09-04 00:57:09 +00:00
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2018-12-25 20:57:00 +00:00
|
|
|
"Frame pacing is forcefully disabled by the active renderer");
|
|
|
|
enableFramePacing = false;
|
2018-09-04 00:57:09 +00:00
|
|
|
}
|
2018-12-25 20:57:00 +00:00
|
|
|
else if (vsyncConstraint == IFFmpegRenderer::PACING_FORCE_ON && !enableFramePacing) {
|
2018-09-08 22:27:21 +00:00
|
|
|
// FIXME: This duplicates logic in Session.cpp
|
|
|
|
int displayHz = StreamUtils::getDisplayRefreshRate(window);
|
|
|
|
if (displayHz + 5 >= maxFps) {
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2018-12-25 20:57:00 +00:00
|
|
|
"Frame pacing is forcefully enabled by the active renderer");
|
|
|
|
enableFramePacing = true;
|
2018-09-08 22:27:21 +00:00
|
|
|
}
|
2018-09-04 00:57:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 07:47:59 +00:00
|
|
|
m_StreamFps = maxFps;
|
2019-02-13 02:42:53 +00:00
|
|
|
|
|
|
|
// Don't bother initializing Pacer if we're not actually going to render
|
|
|
|
if (!testFrame) {
|
|
|
|
m_Pacer = new Pacer(m_Renderer, &m_ActiveWndVideoStats);
|
|
|
|
if (!m_Pacer->initialize(window, maxFps, enableFramePacing)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-16 06:57:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
m_VideoDecoderCtx = avcodec_alloc_context3(decoder);
|
|
|
|
if (!m_VideoDecoderCtx) {
|
2018-07-08 04:52:20 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Unable to allocate video decoder context");
|
2018-07-18 03:00:16 +00:00
|
|
|
return false;
|
2018-07-08 04:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:28:10 +00:00
|
|
|
// Always request low delay decoding
|
2018-07-18 03:00:16 +00:00
|
|
|
m_VideoDecoderCtx->flags |= AV_CODEC_FLAG_LOW_DELAY;
|
2018-07-13 09:28:10 +00:00
|
|
|
|
2018-09-02 22:45:29 +00:00
|
|
|
// Allow display of corrupt frames and frames missing references
|
|
|
|
m_VideoDecoderCtx->flags |= AV_CODEC_FLAG_OUTPUT_CORRUPT;
|
|
|
|
m_VideoDecoderCtx->flags2 |= AV_CODEC_FLAG2_SHOW_ALL;
|
|
|
|
|
2018-07-13 09:28:10 +00:00
|
|
|
// Enable slice multi-threading for software decoding
|
2018-07-18 03:00:16 +00:00
|
|
|
if (!m_HwDecodeCfg) {
|
|
|
|
m_VideoDecoderCtx->thread_type = FF_THREAD_SLICE;
|
|
|
|
m_VideoDecoderCtx->thread_count = qMin(MAX_SLICES, SDL_GetCPUCount());
|
2018-07-13 09:28:10 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No threading for HW decode
|
2018-07-18 03:00:16 +00:00
|
|
|
m_VideoDecoderCtx->thread_count = 1;
|
2018-07-13 09:28:10 +00:00
|
|
|
}
|
2018-07-08 04:52:20 +00:00
|
|
|
|
|
|
|
// Setup decoding parameters
|
2018-07-18 03:00:16 +00:00
|
|
|
m_VideoDecoderCtx->width = width;
|
|
|
|
m_VideoDecoderCtx->height = height;
|
|
|
|
m_VideoDecoderCtx->pix_fmt = AV_PIX_FMT_YUV420P; // FIXME: HDR
|
2018-08-03 04:37:46 +00:00
|
|
|
m_VideoDecoderCtx->get_format = ffGetFormat;
|
2018-07-08 04:52:20 +00:00
|
|
|
|
2018-07-13 09:28:10 +00:00
|
|
|
// Allow the renderer to attach data to this decoder
|
2018-07-18 03:00:16 +00:00
|
|
|
if (!m_Renderer->prepareDecoderContext(m_VideoDecoderCtx)) {
|
|
|
|
return false;
|
2018-07-13 09:28:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 06:24:44 +00:00
|
|
|
// Nobody must override our ffGetFormat
|
|
|
|
SDL_assert(m_VideoDecoderCtx->get_format == ffGetFormat);
|
|
|
|
|
2018-08-03 05:28:59 +00:00
|
|
|
// Stash a pointer to this object in the context
|
|
|
|
SDL_assert(m_VideoDecoderCtx->opaque == nullptr);
|
|
|
|
m_VideoDecoderCtx->opaque = this;
|
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
int err = avcodec_open2(m_VideoDecoderCtx, decoder, nullptr);
|
2018-07-08 04:52:20 +00:00
|
|
|
if (err < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Unable to open decoder for format: %x",
|
|
|
|
videoFormat);
|
2018-07-18 03:00:16 +00:00
|
|
|
return false;
|
2018-07-08 04:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 04:37:46 +00:00
|
|
|
// FFMpeg doesn't completely initialize the codec until the codec
|
|
|
|
// config data comes in. This would be too late for us to change
|
|
|
|
// our minds on the selected video codec, so we'll do a trial run
|
|
|
|
// now to see if things will actually work when the video stream
|
|
|
|
// comes in.
|
2019-02-13 02:42:53 +00:00
|
|
|
if (testFrame) {
|
2018-08-03 04:37:46 +00:00
|
|
|
if (videoFormat & VIDEO_FORMAT_MASK_H264) {
|
|
|
|
m_Pkt.data = (uint8_t*)k_H264TestFrame;
|
|
|
|
m_Pkt.size = sizeof(k_H264TestFrame);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_Pkt.data = (uint8_t*)k_HEVCTestFrame;
|
|
|
|
m_Pkt.size = sizeof(k_HEVCTestFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = avcodec_send_packet(m_VideoDecoderCtx, &m_Pkt);
|
|
|
|
if (err < 0) {
|
|
|
|
char errorstring[512];
|
|
|
|
av_strerror(err, errorstring, sizeof(errorstring));
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Test decode failed: %s", errorstring);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 00:59:53 +00:00
|
|
|
else {
|
|
|
|
if ((videoFormat & VIDEO_FORMAT_MASK_H264) &&
|
|
|
|
!(m_Renderer->getDecoderCapabilities() & CAPABILITY_REFERENCE_FRAME_INVALIDATION_AVC)) {
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Using H.264 SPS fixup");
|
|
|
|
m_NeedsSpsFixup = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_NeedsSpsFixup = false;
|
|
|
|
}
|
2019-02-13 02:43:38 +00:00
|
|
|
|
|
|
|
// Tell overlay manager to use this renderer
|
|
|
|
Session::get()->getOverlayManager().setOverlayRenderer(m_Renderer);
|
2018-10-13 00:59:53 +00:00
|
|
|
}
|
2018-08-03 04:37:46 +00:00
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
return true;
|
2018-07-08 04:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 07:47:59 +00:00
|
|
|
void FFmpegVideoDecoder::addVideoStats(VIDEO_STATS& src, VIDEO_STATS& dst)
|
|
|
|
{
|
|
|
|
dst.receivedFrames += src.receivedFrames;
|
|
|
|
dst.decodedFrames += src.decodedFrames;
|
|
|
|
dst.renderedFrames += src.renderedFrames;
|
2019-01-22 01:43:15 +00:00
|
|
|
dst.totalFrames += src.totalFrames;
|
2018-09-25 07:47:59 +00:00
|
|
|
dst.networkDroppedFrames += src.networkDroppedFrames;
|
|
|
|
dst.pacerDroppedFrames += src.pacerDroppedFrames;
|
|
|
|
dst.totalReassemblyTime += src.totalReassemblyTime;
|
|
|
|
dst.totalDecodeTime += src.totalDecodeTime;
|
2018-12-25 20:09:45 +00:00
|
|
|
dst.totalPacerTime += src.totalPacerTime;
|
2018-09-25 07:47:59 +00:00
|
|
|
dst.totalRenderTime += src.totalRenderTime;
|
|
|
|
|
|
|
|
Uint32 now = SDL_GetTicks();
|
|
|
|
|
|
|
|
// Initialize the measurement start point if this is the first video stat window
|
|
|
|
if (!dst.measurementStartTimestamp) {
|
|
|
|
dst.measurementStartTimestamp = src.measurementStartTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following code assumes the global measure was already started first
|
|
|
|
SDL_assert(dst.measurementStartTimestamp <= src.measurementStartTimestamp);
|
|
|
|
|
2019-01-22 01:43:15 +00:00
|
|
|
dst.totalFps = (float)dst.totalFrames / ((float)(now - dst.measurementStartTimestamp) / 1000);
|
2018-09-25 07:47:59 +00:00
|
|
|
dst.receivedFps = (float)dst.receivedFrames / ((float)(now - dst.measurementStartTimestamp) / 1000);
|
|
|
|
dst.decodedFps = (float)dst.decodedFrames / ((float)(now - dst.measurementStartTimestamp) / 1000);
|
|
|
|
dst.renderedFps = (float)dst.renderedFrames / ((float)(now - dst.measurementStartTimestamp) / 1000);
|
|
|
|
}
|
|
|
|
|
2019-01-20 07:05:56 +00:00
|
|
|
void FFmpegVideoDecoder::stringifyVideoStats(VIDEO_STATS& stats, char* output)
|
2018-09-25 07:47:59 +00:00
|
|
|
{
|
2019-01-20 07:05:56 +00:00
|
|
|
int offset = 0;
|
2018-09-25 07:47:59 +00:00
|
|
|
|
2019-01-20 07:05:56 +00:00
|
|
|
// Start with an empty string
|
|
|
|
output[offset] = 0;
|
|
|
|
|
|
|
|
if (stats.receivedFps > 0) {
|
|
|
|
offset += sprintf(&output[offset],
|
2019-01-22 01:43:15 +00:00
|
|
|
"Estimated host PC frame rate: %.2f FPS\n"
|
|
|
|
"Incoming frame rate from network: %.2f FPS\n"
|
2019-01-20 07:05:56 +00:00
|
|
|
"Decoding frame rate: %.2f FPS\n"
|
|
|
|
"Rendering frame rate: %.2f FPS\n",
|
2019-01-22 01:43:15 +00:00
|
|
|
stats.totalFps,
|
2019-01-20 07:05:56 +00:00
|
|
|
stats.receivedFps,
|
|
|
|
stats.decodedFps,
|
|
|
|
stats.renderedFps);
|
2018-09-25 07:47:59 +00:00
|
|
|
}
|
2019-01-20 07:05:56 +00:00
|
|
|
|
2018-09-25 07:47:59 +00:00
|
|
|
if (stats.renderedFrames != 0) {
|
2019-01-20 07:05:56 +00:00
|
|
|
offset += sprintf(&output[offset],
|
|
|
|
"Frames dropped by your network connection: %.2f%%\n"
|
2019-02-17 07:04:25 +00:00
|
|
|
"Frames dropped due to network jitter: %.2f%%\n"
|
2019-01-22 01:43:15 +00:00
|
|
|
"Average receive time: %.2f ms\n"
|
|
|
|
"Average decoding time: %.2f ms\n"
|
|
|
|
"Average frame queue delay: %.2f ms\n"
|
2019-01-23 04:55:58 +00:00
|
|
|
"Average rendering time (including monitor V-sync latency): %.2f ms\n",
|
2019-01-22 01:43:15 +00:00
|
|
|
(float)stats.networkDroppedFrames / stats.totalFrames * 100,
|
|
|
|
(float)stats.pacerDroppedFrames / stats.decodedFrames * 100,
|
|
|
|
(float)stats.totalReassemblyTime / stats.receivedFrames,
|
2019-01-20 07:05:56 +00:00
|
|
|
(float)stats.totalDecodeTime / stats.decodedFrames,
|
|
|
|
(float)stats.totalPacerTime / stats.renderedFrames,
|
2019-01-22 01:43:15 +00:00
|
|
|
(float)stats.totalRenderTime / stats.renderedFrames);
|
2019-01-20 07:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FFmpegVideoDecoder::logVideoStats(VIDEO_STATS& stats, const char* title)
|
|
|
|
{
|
|
|
|
if (stats.renderedFps > 0 || stats.renderedFrames != 0) {
|
|
|
|
char videoStatsStr[512];
|
|
|
|
stringifyVideoStats(stats, videoStatsStr);
|
|
|
|
|
2018-09-25 07:47:59 +00:00
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2019-01-20 07:05:56 +00:00
|
|
|
"%s", title);
|
2018-09-25 07:47:59 +00:00
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2019-01-20 07:05:56 +00:00
|
|
|
"----------------------------------------------------------\n%s",
|
|
|
|
videoStatsStr);
|
2018-09-25 07:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:57:16 +00:00
|
|
|
IFFmpegRenderer* FFmpegVideoDecoder::createAcceleratedRenderer(const AVCodecHWConfig* hwDecodeCfg)
|
|
|
|
{
|
|
|
|
if (!(hwDecodeCfg->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for acceleration types we support
|
|
|
|
switch (hwDecodeCfg->device_type) {
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
case AV_HWDEVICE_TYPE_DXVA2:
|
|
|
|
return new DXVA2Renderer();
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
case AV_HWDEVICE_TYPE_VIDEOTOOLBOX:
|
|
|
|
return VTRendererFactory::createRenderer();
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIBVA
|
|
|
|
case AV_HWDEVICE_TYPE_VAAPI:
|
|
|
|
return new VAAPIRenderer();
|
2018-08-03 09:11:44 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIBVDPAU
|
|
|
|
case AV_HWDEVICE_TYPE_VDPAU:
|
|
|
|
return new VDPAURenderer();
|
2018-08-03 04:57:16 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:37:46 +00:00
|
|
|
bool FFmpegVideoDecoder::initialize(
|
|
|
|
StreamingPreferences::VideoDecoderSelection vds,
|
|
|
|
SDL_Window* window,
|
|
|
|
int videoFormat,
|
|
|
|
int width,
|
|
|
|
int height,
|
2018-08-21 01:19:42 +00:00
|
|
|
int maxFps,
|
2018-12-25 20:57:00 +00:00
|
|
|
bool enableVsync,
|
|
|
|
bool enableFramePacing)
|
2018-08-03 04:37:46 +00:00
|
|
|
{
|
|
|
|
AVCodec* decoder;
|
|
|
|
|
2019-02-14 02:34:59 +00:00
|
|
|
// Increase log level until the first frame is decoded
|
2018-08-03 05:48:40 +00:00
|
|
|
av_log_set_level(AV_LOG_DEBUG);
|
|
|
|
|
2018-08-03 04:37:46 +00:00
|
|
|
if (videoFormat & VIDEO_FORMAT_MASK_H264) {
|
|
|
|
decoder = avcodec_find_decoder(AV_CODEC_ID_H264);
|
|
|
|
}
|
|
|
|
else if (videoFormat & VIDEO_FORMAT_MASK_H265) {
|
|
|
|
decoder = avcodec_find_decoder(AV_CODEC_ID_HEVC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Q_ASSERT(false);
|
|
|
|
decoder = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!decoder) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Unable to find decoder for format: %x",
|
|
|
|
videoFormat);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0;; i++) {
|
|
|
|
const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
|
|
|
|
if (!config || vds == StreamingPreferences::VDS_FORCE_SOFTWARE) {
|
|
|
|
// No matching hardware acceleration support.
|
|
|
|
// This is not an error.
|
|
|
|
m_HwDecodeCfg = nullptr;
|
|
|
|
m_Renderer = new SdlRenderer();
|
|
|
|
if (vds != StreamingPreferences::VDS_FORCE_HARDWARE &&
|
2018-08-21 01:19:42 +00:00
|
|
|
m_Renderer->initialize(window, videoFormat, width, height, maxFps, enableVsync) &&
|
2019-02-13 02:42:53 +00:00
|
|
|
completeInitialization(decoder, window, videoFormat, width, height, maxFps, enableFramePacing, m_TestOnly)) {
|
2018-08-03 04:37:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:57:16 +00:00
|
|
|
m_Renderer = createAcceleratedRenderer(config);
|
|
|
|
if (!m_Renderer) {
|
2018-08-03 04:37:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_HwDecodeCfg = config;
|
2018-08-19 07:59:04 +00:00
|
|
|
// Initialize the hardware codec and submit a test frame if the renderer needs it
|
2018-08-21 01:19:42 +00:00
|
|
|
if (m_Renderer->initialize(window, videoFormat, width, height, maxFps, enableVsync) &&
|
2019-02-13 02:42:53 +00:00
|
|
|
completeInitialization(decoder, window, videoFormat, width, height, maxFps, enableFramePacing, m_TestOnly || m_Renderer->needsTestFrame())) {
|
|
|
|
if (m_TestOnly) {
|
|
|
|
// This decoder is only for testing capabilities, so don't bother
|
|
|
|
// creating a usable renderer
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-19 07:59:04 +00:00
|
|
|
if (m_Renderer->needsTestFrame()) {
|
|
|
|
// The test worked, so now let's initialize it for real
|
|
|
|
reset();
|
|
|
|
if ((m_Renderer = createAcceleratedRenderer(config)) != nullptr &&
|
2018-08-21 01:19:42 +00:00
|
|
|
m_Renderer->initialize(window, videoFormat, width, height, maxFps, enableVsync) &&
|
2018-12-25 20:57:00 +00:00
|
|
|
completeInitialization(decoder, window, videoFormat, width, height, maxFps, enableFramePacing, false)) {
|
2018-08-19 07:59:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Decoder failed to initialize after successful test");
|
|
|
|
reset();
|
|
|
|
}
|
2018-08-03 04:37:46 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-08-19 07:59:04 +00:00
|
|
|
// No test required. Good to go now.
|
|
|
|
return true;
|
2018-08-03 04:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Failed to initialize or test frame failed, so keep looking
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 00:59:53 +00:00
|
|
|
void FFmpegVideoDecoder::writeBuffer(PLENTRY entry, int& offset)
|
|
|
|
{
|
|
|
|
if (m_NeedsSpsFixup && entry->bufferType == BUFFER_TYPE_SPS) {
|
|
|
|
const char naluHeader[] = {0x00, 0x00, 0x00, 0x01};
|
|
|
|
h264_stream_t* stream = h264_new();
|
|
|
|
int nalStart, nalEnd;
|
|
|
|
|
|
|
|
// Read the old NALU
|
|
|
|
find_nal_unit((uint8_t*)entry->data, entry->length, &nalStart, &nalEnd);
|
|
|
|
read_nal_unit(stream,
|
|
|
|
(unsigned char *)&entry->data[nalStart],
|
|
|
|
nalEnd - nalStart);
|
|
|
|
|
|
|
|
SDL_assert(nalStart == sizeof(naluHeader));
|
|
|
|
SDL_assert(nalEnd == entry->length);
|
|
|
|
|
|
|
|
// Fixup the SPS to what OS X needs to use hardware acceleration
|
|
|
|
stream->sps->num_ref_frames = 1;
|
|
|
|
stream->sps->vui.max_dec_frame_buffering = 1;
|
|
|
|
|
|
|
|
int initialOffset = offset;
|
|
|
|
|
|
|
|
// Copy the modified NALU data. This assumes a 3 byte prefix and
|
|
|
|
// begins writing from the 2nd byte, so we must write the data
|
|
|
|
// first, then go back and write the Annex B prefix.
|
|
|
|
offset += write_nal_unit(stream, (uint8_t*)&m_DecodeBuffer.data()[initialOffset + 3],
|
|
|
|
MAX_SPS_EXTRA_SIZE + entry->length - sizeof(naluHeader));
|
|
|
|
|
|
|
|
// Copy the NALU prefix over from the original SPS
|
|
|
|
memcpy(&m_DecodeBuffer.data()[initialOffset], naluHeader, sizeof(naluHeader));
|
|
|
|
offset += sizeof(naluHeader);
|
|
|
|
|
|
|
|
h264_free(stream);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Write the buffer as-is
|
|
|
|
memcpy(&m_DecodeBuffer.data()[offset],
|
|
|
|
entry->data,
|
|
|
|
entry->length);
|
|
|
|
offset += entry->length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
int FFmpegVideoDecoder::submitDecodeUnit(PDECODE_UNIT du)
|
2018-07-08 04:52:20 +00:00
|
|
|
{
|
|
|
|
PLENTRY entry = du->bufferList;
|
|
|
|
int err;
|
|
|
|
|
2019-02-13 02:42:53 +00:00
|
|
|
SDL_assert(!m_TestOnly);
|
|
|
|
|
2019-01-20 07:05:56 +00:00
|
|
|
if (!m_LastFrameNumber) {
|
|
|
|
m_ActiveWndVideoStats.measurementStartTimestamp = SDL_GetTicks();
|
|
|
|
m_LastFrameNumber = du->frameNumber;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Any frame number greater than m_LastFrameNumber + 1 represents a dropped frame
|
|
|
|
m_ActiveWndVideoStats.networkDroppedFrames += du->frameNumber - (m_LastFrameNumber + 1);
|
2019-01-22 01:43:15 +00:00
|
|
|
m_ActiveWndVideoStats.totalFrames += du->frameNumber - (m_LastFrameNumber + 1);
|
2019-01-20 07:05:56 +00:00
|
|
|
m_LastFrameNumber = du->frameNumber;
|
|
|
|
}
|
|
|
|
|
2018-09-25 07:47:59 +00:00
|
|
|
// Flip stats windows roughly every second
|
2019-01-20 07:05:56 +00:00
|
|
|
if (SDL_TICKS_PASSED(SDL_GetTicks(), m_ActiveWndVideoStats.measurementStartTimestamp + 1000)) {
|
|
|
|
// Update overlay stats if it's enabled
|
2019-02-13 02:43:38 +00:00
|
|
|
if (Session::get()->getOverlayManager().isOverlayEnabled(Overlay::OverlayDebug)) {
|
2019-01-20 07:05:56 +00:00
|
|
|
VIDEO_STATS lastTwoWndStats = {};
|
|
|
|
addVideoStats(m_LastWndVideoStats, lastTwoWndStats);
|
|
|
|
addVideoStats(m_ActiveWndVideoStats, lastTwoWndStats);
|
|
|
|
|
2019-02-13 02:43:38 +00:00
|
|
|
stringifyVideoStats(lastTwoWndStats, Session::get()->getOverlayManager().getOverlayText(Overlay::OverlayDebug));
|
|
|
|
Session::get()->getOverlayManager().setOverlayTextUpdated(Overlay::OverlayDebug);
|
2019-01-20 07:05:56 +00:00
|
|
|
}
|
2018-09-25 07:47:59 +00:00
|
|
|
|
|
|
|
// Accumulate these values into the global stats
|
|
|
|
addVideoStats(m_ActiveWndVideoStats, m_GlobalVideoStats);
|
|
|
|
|
|
|
|
// Move this window into the last window slot and clear it for next window
|
|
|
|
SDL_memcpy(&m_LastWndVideoStats, &m_ActiveWndVideoStats, sizeof(m_ActiveWndVideoStats));
|
|
|
|
SDL_zero(m_ActiveWndVideoStats);
|
|
|
|
m_ActiveWndVideoStats.measurementStartTimestamp = SDL_GetTicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ActiveWndVideoStats.receivedFrames++;
|
2019-01-22 01:43:15 +00:00
|
|
|
m_ActiveWndVideoStats.totalFrames++;
|
2018-09-25 07:47:59 +00:00
|
|
|
|
2018-10-13 00:59:53 +00:00
|
|
|
int requiredBufferSize = du->fullLength;
|
|
|
|
if (du->frameType == FRAME_TYPE_IDR) {
|
|
|
|
// Add some extra space in case we need to do an SPS fixup
|
|
|
|
requiredBufferSize += MAX_SPS_EXTRA_SIZE;
|
|
|
|
}
|
|
|
|
if (requiredBufferSize + AV_INPUT_BUFFER_PADDING_SIZE > m_DecodeBuffer.length()) {
|
|
|
|
m_DecodeBuffer = QByteArray(requiredBufferSize + AV_INPUT_BUFFER_PADDING_SIZE, 0);
|
2018-07-08 04:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
while (entry != nullptr) {
|
2018-10-13 00:59:53 +00:00
|
|
|
writeBuffer(entry, offset);
|
2018-07-08 04:52:20 +00:00
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
m_Pkt.data = reinterpret_cast<uint8_t*>(m_DecodeBuffer.data());
|
2018-10-13 00:59:53 +00:00
|
|
|
m_Pkt.size = offset;
|
2018-07-08 04:52:20 +00:00
|
|
|
|
2018-09-25 07:47:59 +00:00
|
|
|
m_ActiveWndVideoStats.totalReassemblyTime += LiGetMillis() - du->receiveTimeMs;
|
|
|
|
|
|
|
|
Uint32 beforeDecode = SDL_GetTicks();
|
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
err = avcodec_send_packet(m_VideoDecoderCtx, &m_Pkt);
|
2018-07-08 04:52:20 +00:00
|
|
|
if (err < 0) {
|
|
|
|
char errorstring[512];
|
|
|
|
av_strerror(err, errorstring, sizeof(errorstring));
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
2018-08-10 01:39:38 +00:00
|
|
|
"avcodec_send_packet() failed: %s", errorstring);
|
|
|
|
|
|
|
|
// If we've failed a bunch of decodes in a row, the decoder/renderer is
|
|
|
|
// clearly unhealthy, so let's generate a synthetic reset event to trigger
|
|
|
|
// the event loop to destroy and recreate the decoder.
|
|
|
|
if (++m_ConsecutiveFailedDecodes == FAILED_DECODES_RESET_THRESHOLD) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Resetting decoder due to consistent failure");
|
|
|
|
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = SDL_RENDER_DEVICE_RESET;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
}
|
|
|
|
|
2018-07-08 04:52:20 +00:00
|
|
|
return DR_NEED_IDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFrame* frame = av_frame_alloc();
|
|
|
|
if (!frame) {
|
|
|
|
// Failed to allocate a frame but we did submit,
|
|
|
|
// so we can return DR_OK
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to allocate frame");
|
|
|
|
return DR_OK;
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
err = avcodec_receive_frame(m_VideoDecoderCtx, frame);
|
2018-07-08 04:52:20 +00:00
|
|
|
if (err == 0) {
|
2018-08-10 01:39:38 +00:00
|
|
|
// Reset failed decodes count if we reached this far
|
|
|
|
m_ConsecutiveFailedDecodes = 0;
|
|
|
|
|
2019-02-14 02:34:59 +00:00
|
|
|
// Restore default log level after a successful decode
|
|
|
|
av_log_set_level(AV_LOG_INFO);
|
|
|
|
|
2018-12-25 20:09:45 +00:00
|
|
|
// Capture a frame timestamp to measuring pacing delay
|
|
|
|
frame->pts = SDL_GetTicks();
|
|
|
|
|
2018-09-25 07:47:59 +00:00
|
|
|
// Count time in avcodec_send_packet() and avcodec_receive_frame()
|
|
|
|
// as time spent decoding
|
|
|
|
m_ActiveWndVideoStats.totalDecodeTime += SDL_GetTicks() - beforeDecode;
|
|
|
|
m_ActiveWndVideoStats.decodedFrames++;
|
2019-01-22 04:27:37 +00:00
|
|
|
|
|
|
|
// Queue the frame for rendering (or render now if pacer is disabled)
|
|
|
|
m_Pacer->submitFrame(frame);
|
2018-07-08 04:52:20 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
av_frame_free(&frame);
|
2018-08-10 01:39:38 +00:00
|
|
|
|
|
|
|
char errorstring[512];
|
|
|
|
av_strerror(err, errorstring, sizeof(errorstring));
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"avcodec_receive_frame() failed: %s", errorstring);
|
|
|
|
|
|
|
|
if (++m_ConsecutiveFailedDecodes == FAILED_DECODES_RESET_THRESHOLD) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Resetting decoder due to consistent failure");
|
|
|
|
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = SDL_RENDER_DEVICE_RESET;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
}
|
2018-07-08 04:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return DR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called on main thread
|
2019-01-22 04:27:37 +00:00
|
|
|
void FFmpegVideoDecoder::renderFrame(SDL_UserEvent*)
|
2018-07-08 04:52:20 +00:00
|
|
|
{
|
2019-01-22 04:27:37 +00:00
|
|
|
SDL_assert(false);
|
2018-07-08 04:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called on main thread
|
2019-01-22 04:27:37 +00:00
|
|
|
void FFmpegVideoDecoder::dropFrame(SDL_UserEvent*)
|
2018-07-08 04:52:20 +00:00
|
|
|
{
|
2019-01-22 04:27:37 +00:00
|
|
|
SDL_assert(false);
|
2018-07-08 04:52:20 +00:00
|
|
|
}
|