2021-05-09 20:52:26 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//
|
|
|
|
// EmulationStation Desktop Edition
|
|
|
|
// VideoFFmpegComponent.cpp
|
|
|
|
//
|
|
|
|
// Video player based on FFmpeg.
|
|
|
|
//
|
|
|
|
|
2021-08-18 16:52:41 +00:00
|
|
|
#define DEBUG_VIDEO false
|
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
#include "components/VideoFFmpegComponent.h"
|
|
|
|
|
|
|
|
#include "AudioManager.h"
|
|
|
|
#include "Settings.h"
|
|
|
|
#include "Window.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "resources/TextureResource.h"
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-08-18 16:52:41 +00:00
|
|
|
#include <algorithm>
|
2021-07-04 09:36:16 +00:00
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
enum AVHWDeviceType VideoFFmpegComponent::sDeviceType = AV_HWDEVICE_TYPE_NONE;
|
|
|
|
enum AVPixelFormat VideoFFmpegComponent::sPixelFormat = AV_PIX_FMT_NONE;
|
|
|
|
std::vector<std::string> VideoFFmpegComponent::sHWDecodedVideos;
|
|
|
|
std::vector<std::string> VideoFFmpegComponent::sSWDecodedVideos;
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
VideoFFmpegComponent::VideoFFmpegComponent(Window* window)
|
|
|
|
: VideoComponent(window)
|
|
|
|
, mFrameProcessingThread(nullptr)
|
|
|
|
, mFormatContext(nullptr)
|
|
|
|
, mVideoStream(nullptr)
|
|
|
|
, mAudioStream(nullptr)
|
|
|
|
, mVideoCodec(nullptr)
|
|
|
|
, mAudioCodec(nullptr)
|
2021-07-14 17:13:25 +00:00
|
|
|
, mHardwareCodec(nullptr)
|
|
|
|
, mHwContext(nullptr)
|
2021-07-07 18:31:46 +00:00
|
|
|
, mVideoCodecContext(nullptr)
|
|
|
|
, mAudioCodecContext(nullptr)
|
|
|
|
, mVBufferSrcContext(nullptr)
|
|
|
|
, mVBufferSinkContext(nullptr)
|
|
|
|
, mVFilterGraph(nullptr)
|
|
|
|
, mVFilterInputs(nullptr)
|
|
|
|
, mVFilterOutputs(nullptr)
|
|
|
|
, mABufferSrcContext(nullptr)
|
|
|
|
, mABufferSinkContext(nullptr)
|
|
|
|
, mAFilterGraph(nullptr)
|
|
|
|
, mAFilterInputs(nullptr)
|
|
|
|
, mAFilterOutputs(nullptr)
|
|
|
|
, mVideoTargetQueueSize(0)
|
|
|
|
, mAudioTargetQueueSize(0)
|
2021-09-18 07:53:26 +00:00
|
|
|
, mVideoTimeBase(0.0l)
|
2021-07-07 18:31:46 +00:00
|
|
|
, mAccumulatedTime(0)
|
|
|
|
, mStartTimeAccumulation(false)
|
|
|
|
, mDecodedFrame(false)
|
|
|
|
, mEndOfVideo(false)
|
2021-05-09 20:52:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
VideoFFmpegComponent::~VideoFFmpegComponent() { stopVideo(); }
|
2021-05-09 20:52:26 +00:00
|
|
|
|
|
|
|
void VideoFFmpegComponent::setResize(float width, float height)
|
|
|
|
{
|
|
|
|
// This resize function is used when stretching videos to full screen in the video screensaver.
|
2021-08-17 16:41:45 +00:00
|
|
|
mTargetSize = glm::vec2{width, height};
|
2021-05-09 20:52:26 +00:00
|
|
|
mTargetIsMax = false;
|
|
|
|
mStaticImage.setResize(width, height);
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoFFmpegComponent::setMaxSize(float width, float height)
|
|
|
|
{
|
|
|
|
// This resize function is used in most instances, such as non-stretched video screensaver
|
|
|
|
// and the gamelist videos.
|
2021-08-17 16:41:45 +00:00
|
|
|
mTargetSize = glm::vec2{width, height};
|
2021-05-09 20:52:26 +00:00
|
|
|
mTargetIsMax = true;
|
|
|
|
mStaticImage.setMaxSize(width, height);
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoFFmpegComponent::resize()
|
|
|
|
{
|
|
|
|
if (!mTexture)
|
|
|
|
return;
|
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
const glm::vec2 textureSize{static_cast<float>(mVideoWidth), static_cast<float>(mVideoHeight)};
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
if (textureSize == glm::vec2{})
|
2021-05-09 20:52:26 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (mTargetIsMax) {
|
|
|
|
mSize = textureSize;
|
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::vec2 resizeScale{(mTargetSize.x / mSize.x), (mTargetSize.y / mSize.y)};
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
if (resizeScale.x < resizeScale.y) {
|
|
|
|
mSize.x *= resizeScale.x;
|
|
|
|
mSize.y *= resizeScale.x;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-08-16 16:25:01 +00:00
|
|
|
mSize.x *= resizeScale.y;
|
|
|
|
mSize.y *= resizeScale.y;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
mSize.y = std::round(mSize.y);
|
2021-08-16 16:25:01 +00:00
|
|
|
mSize.x = (mSize.y / textureSize.y) * textureSize.x;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If both components are set, we just stretch.
|
|
|
|
// If no components are set, we don't resize at all.
|
2021-08-17 16:41:45 +00:00
|
|
|
mSize = mTargetSize == glm::vec2{} ? textureSize : mTargetSize;
|
2021-05-09 20:52:26 +00:00
|
|
|
|
|
|
|
// If only one component is set, we resize in a way that maintains aspect ratio.
|
2021-08-16 16:25:01 +00:00
|
|
|
if (!mTargetSize.x && mTargetSize.y) {
|
|
|
|
mSize.y = std::round(mTargetSize.y);
|
|
|
|
mSize.x = (mSize.y / textureSize.y) * textureSize.x;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
2021-08-16 16:25:01 +00:00
|
|
|
else if (mTargetSize.x && !mTargetSize.y) {
|
|
|
|
mSize.y = std::round((mTargetSize.x / textureSize.x) * textureSize.y);
|
|
|
|
mSize.x = (mSize.y / textureSize.y) * textureSize.x;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSizeChanged();
|
|
|
|
}
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void VideoFFmpegComponent::render(const glm::mat4& parentTrans)
|
2021-05-09 20:52:26 +00:00
|
|
|
{
|
|
|
|
VideoComponent::render(parentTrans);
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::mat4 trans{parentTrans * getTransform()};
|
2021-05-09 20:52:26 +00:00
|
|
|
GuiComponent::renderChildren(trans);
|
|
|
|
|
|
|
|
if (mIsPlaying && mFormatContext) {
|
|
|
|
unsigned int color;
|
2021-05-29 08:58:51 +00:00
|
|
|
if (mDecodedFrame && mFadeIn < 1) {
|
2021-05-09 20:52:26 +00:00
|
|
|
const unsigned int fadeIn = static_cast<int>(mFadeIn * 255.0f);
|
2021-07-07 18:31:46 +00:00
|
|
|
color =
|
|
|
|
Renderer::convertRGBAToABGR((fadeIn << 24) | (fadeIn << 16) | (fadeIn << 8) | 255);
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
color = 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
Renderer::Vertex vertices[4];
|
|
|
|
Renderer::setMatrix(parentTrans);
|
|
|
|
|
|
|
|
// Render the black rectangle behind the video.
|
|
|
|
if (mVideoRectangleCoords.size() == 4) {
|
|
|
|
Renderer::drawRect(mVideoRectangleCoords[0], mVideoRectangleCoords[1],
|
2021-07-07 18:31:46 +00:00
|
|
|
mVideoRectangleCoords[2], mVideoRectangleCoords[3], // Line break.
|
|
|
|
0x000000FF, 0x000000FF);
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format off
|
2021-08-18 16:52:41 +00:00
|
|
|
vertices[0] = {{0.0f, 0.0f }, {0.0f, 0.0f}, color};
|
|
|
|
vertices[1] = {{0.0f, mSize.y}, {0.0f, 1.0f}, color};
|
|
|
|
vertices[2] = {{mSize.x, 0.0f }, {1.0f, 0.0f}, color};
|
|
|
|
vertices[3] = {{mSize.x, mSize.y}, {1.0f, 1.0f}, color};
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format on
|
2021-05-09 20:52:26 +00:00
|
|
|
|
|
|
|
// Round vertices.
|
|
|
|
for (int i = 0; i < 4; i++)
|
2021-08-16 16:25:01 +00:00
|
|
|
vertices[i].pos = glm::round(vertices[i].pos);
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-11 15:35:55 +00:00
|
|
|
// This is needed to avoid a slight gap before the video starts playing.
|
2021-05-09 20:52:26 +00:00
|
|
|
if (!mDecodedFrame)
|
|
|
|
return;
|
|
|
|
|
2021-05-11 15:35:55 +00:00
|
|
|
mPictureMutex.lock();
|
|
|
|
|
2021-05-19 17:47:40 +00:00
|
|
|
if (!mOutputPicture.hasBeenRendered) {
|
|
|
|
// Copy the contents of mOutputPicture to a temporary vector in order to call
|
|
|
|
// initFromPixels() only after the mutex unlock. This significantly reduces the
|
|
|
|
// lock waits in outputFrames().
|
|
|
|
size_t pictureSize = mOutputPicture.pictureRGBA.size();
|
|
|
|
std::vector<uint8_t> tempPictureRGBA(pictureSize);
|
|
|
|
int pictureWidth = 0;
|
|
|
|
int pictureHeight = 0;
|
|
|
|
|
|
|
|
if (pictureSize > 0) {
|
2021-07-07 18:31:46 +00:00
|
|
|
tempPictureRGBA.insert(tempPictureRGBA.begin(), mOutputPicture.pictureRGBA.begin(),
|
|
|
|
mOutputPicture.pictureRGBA.end());
|
2021-05-19 17:47:40 +00:00
|
|
|
pictureWidth = mOutputPicture.width;
|
|
|
|
pictureHeight = mOutputPicture.height;
|
|
|
|
|
|
|
|
mOutputPicture.hasBeenRendered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPictureMutex.unlock();
|
2021-05-11 15:35:55 +00:00
|
|
|
|
2021-05-19 17:47:40 +00:00
|
|
|
if (pictureSize > 0) {
|
|
|
|
// Build a texture for the video frame.
|
|
|
|
mTexture->initFromPixels(&tempPictureRGBA.at(0), pictureWidth, pictureHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mPictureMutex.unlock();
|
|
|
|
}
|
2021-05-11 15:35:55 +00:00
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
mTexture->bind();
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(USE_OPENGL_21)
|
2021-05-16 11:12:31 +00:00
|
|
|
// Render scanlines if this option is enabled. However, if this is the media viewer
|
|
|
|
// or the video screensaver, then skip this as the scanline rendering is then handled
|
|
|
|
// in those modules as a postprocessing step.
|
|
|
|
if ((!mScreensaverMode && !mMediaViewerMode) &&
|
2021-07-07 18:31:46 +00:00
|
|
|
Settings::getInstance()->getBool("GamelistVideoScanlines"))
|
2021-05-09 20:52:26 +00:00
|
|
|
vertices[0].shaders = Renderer::SHADER_SCANLINES;
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2021-05-09 20:52:26 +00:00
|
|
|
|
|
|
|
// Render it.
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
Renderer::drawTriangleStrips(&vertices[0], 4, trans);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VideoComponent::renderSnapshot(parentTrans);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
void VideoFFmpegComponent::updatePlayer()
|
2021-05-09 20:52:26 +00:00
|
|
|
{
|
|
|
|
if (mPause || !mFormatContext)
|
|
|
|
return;
|
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
// Output any audio that has been added by the processing thread.
|
|
|
|
mAudioMutex.lock();
|
|
|
|
if (mOutputAudio.size()) {
|
2021-05-29 09:48:33 +00:00
|
|
|
AudioManager::getInstance()->processStream(&mOutputAudio.at(0),
|
2021-07-07 18:31:46 +00:00
|
|
|
static_cast<unsigned int>(mOutputAudio.size()));
|
2021-05-29 08:58:51 +00:00
|
|
|
mOutputAudio.clear();
|
|
|
|
}
|
|
|
|
mAudioMutex.unlock();
|
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
if (mIsActuallyPlaying && mStartTimeAccumulation) {
|
2021-07-07 18:31:46 +00:00
|
|
|
mAccumulatedTime +=
|
|
|
|
static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
|
|
std::chrono::high_resolution_clock::now() - mTimeReference)
|
|
|
|
.count()) /
|
|
|
|
1000000000.0l;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mTimeReference = std::chrono::high_resolution_clock::now();
|
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
if (!mFrameProcessingThread) {
|
|
|
|
AudioManager::getInstance()->unmuteStream();
|
2021-05-12 20:49:24 +00:00
|
|
|
mFrameProcessingThread =
|
2021-07-07 18:31:46 +00:00
|
|
|
std::make_unique<std::thread>(&VideoFFmpegComponent::frameProcessing, this);
|
2021-05-29 08:58:51 +00:00
|
|
|
}
|
2021-05-11 15:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoFFmpegComponent::frameProcessing()
|
|
|
|
{
|
2021-05-29 08:58:51 +00:00
|
|
|
mWindow->increaseVideoPlayerCount();
|
2021-05-11 15:35:55 +00:00
|
|
|
|
2021-07-09 17:47:33 +00:00
|
|
|
bool videoFilter = false;
|
|
|
|
bool audioFilter = false;
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
videoFilter = setupVideoFilters();
|
|
|
|
|
|
|
|
if (mAudioCodecContext)
|
|
|
|
audioFilter = setupAudioFilters();
|
|
|
|
|
|
|
|
while (mIsPlaying && !mPause && videoFilter && (!mAudioCodecContext || audioFilter)) {
|
|
|
|
readFrames();
|
|
|
|
getProcessedFrames();
|
2021-05-11 15:35:55 +00:00
|
|
|
outputFrames();
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-11 15:35:55 +00:00
|
|
|
// This 1 ms wait makes sure that the thread does not consume all available CPU cycles.
|
|
|
|
SDL_Delay(1);
|
|
|
|
}
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
if (videoFilter) {
|
|
|
|
avfilter_inout_free(&mVFilterInputs);
|
|
|
|
avfilter_inout_free(&mVFilterOutputs);
|
|
|
|
avfilter_free(mVBufferSrcContext);
|
|
|
|
avfilter_free(mVBufferSinkContext);
|
|
|
|
avfilter_graph_free(&mVFilterGraph);
|
|
|
|
mVBufferSrcContext = nullptr;
|
|
|
|
mVBufferSinkContext = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audioFilter) {
|
|
|
|
avfilter_inout_free(&mAFilterInputs);
|
|
|
|
avfilter_inout_free(&mAFilterOutputs);
|
|
|
|
avfilter_free(mABufferSrcContext);
|
|
|
|
avfilter_free(mABufferSinkContext);
|
|
|
|
avfilter_graph_free(&mAFilterGraph);
|
|
|
|
mABufferSrcContext = nullptr;
|
|
|
|
mABufferSinkContext = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWindow->decreaseVideoPlayerCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoFFmpegComponent::setupVideoFilters()
|
|
|
|
{
|
|
|
|
int returnValue = 0;
|
2021-05-29 09:27:04 +00:00
|
|
|
char errorMessage[512];
|
2021-08-17 16:41:45 +00:00
|
|
|
const enum AVPixelFormat outPixFormats[] = {AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE};
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
mVFilterInputs = avfilter_inout_alloc();
|
|
|
|
mVFilterOutputs = avfilter_inout_alloc();
|
|
|
|
|
|
|
|
if (!(mVFilterGraph = avfilter_graph_alloc())) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupVideoFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't allocate filter graph";
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit the libavfilter video processing to two additional threads.
|
|
|
|
// Not sure why the actual thread count is one less than specified.
|
|
|
|
mVFilterGraph->nb_threads = 3;
|
|
|
|
|
|
|
|
const AVFilter* bufferSrc = avfilter_get_by_name("buffer");
|
|
|
|
if (!bufferSrc) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupVideoFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't find \"buffer\" filter";
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AVFilter* bufferSink = avfilter_get_by_name("buffersink");
|
|
|
|
if (!bufferSink) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupVideoFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't find \"buffersink\" filter";
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some codecs such as H.264 need the width to be in increments of 16 pixels.
|
|
|
|
int width = mVideoCodecContext->width;
|
|
|
|
int height = mVideoCodecContext->height;
|
|
|
|
int modulo = mVideoCodecContext->width % 16;
|
|
|
|
|
|
|
|
if (modulo > 0)
|
|
|
|
width += 16 - modulo;
|
|
|
|
|
|
|
|
std::string filterArguments =
|
2021-07-07 18:31:46 +00:00
|
|
|
"width=" + std::to_string(width) + ":" + "height=" + std::to_string(height) +
|
|
|
|
":pix_fmt=" + av_get_pix_fmt_name(mVideoCodecContext->pix_fmt) +
|
|
|
|
":time_base=" + std::to_string(mVideoStream->time_base.num) + "/" +
|
|
|
|
std::to_string(mVideoStream->time_base.den) +
|
|
|
|
":sar=" + std::to_string(mVideoCodecContext->sample_aspect_ratio.num) + "/" +
|
|
|
|
std::to_string(mVideoCodecContext->sample_aspect_ratio.den);
|
|
|
|
|
|
|
|
returnValue = avfilter_graph_create_filter(&mVBufferSrcContext, bufferSrc, "in",
|
|
|
|
filterArguments.c_str(), nullptr, mVFilterGraph);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupVideoFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't create filter instance for buffer source: "
|
|
|
|
<< av_make_error_string(errorMessage, sizeof(errorMessage), returnValue);
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
returnValue = avfilter_graph_create_filter(&mVBufferSinkContext, bufferSink, "out", nullptr,
|
|
|
|
nullptr, mVFilterGraph);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupVideoFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't create filter instance for buffer sink: "
|
|
|
|
<< av_make_error_string(errorMessage, sizeof(errorMessage), returnValue);
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Endpoints for the filter graph.
|
|
|
|
mVFilterInputs->name = av_strdup("out");
|
|
|
|
mVFilterInputs->filter_ctx = mVBufferSinkContext;
|
|
|
|
mVFilterInputs->pad_idx = 0;
|
|
|
|
mVFilterInputs->next = nullptr;
|
|
|
|
|
|
|
|
mVFilterOutputs->name = av_strdup("in");
|
|
|
|
mVFilterOutputs->filter_ctx = mVBufferSrcContext;
|
|
|
|
mVFilterOutputs->pad_idx = 0;
|
|
|
|
mVFilterOutputs->next = nullptr;
|
|
|
|
|
|
|
|
std::string filterDescription;
|
|
|
|
|
|
|
|
// Whether to upscale the frame rate to 60 FPS.
|
|
|
|
if (Settings::getInstance()->getBool("VideoUpscaleFrameRate")) {
|
|
|
|
if (modulo > 0)
|
2021-07-07 18:31:46 +00:00
|
|
|
filterDescription = "scale=width=" + std::to_string(width) +
|
|
|
|
":height=" + std::to_string(height) + ",fps=fps=60,";
|
2021-05-29 08:58:51 +00:00
|
|
|
else
|
|
|
|
filterDescription = "fps=fps=60,";
|
|
|
|
|
|
|
|
// The "framerate" filter is a more advanced way to upscale the frame rate using
|
|
|
|
// interpolation. However I have not been able to get this to work with slice
|
|
|
|
// threading so the performance is poor. As such it's disabled for now.
|
2021-07-07 18:31:46 +00:00
|
|
|
// if (modulo > 0)
|
|
|
|
// filterDescription =
|
|
|
|
// "scale=width=" + std::to_string(width) +
|
|
|
|
// ":height=" + std::to_string(height) +
|
|
|
|
// ",framerate=fps=60,";
|
|
|
|
// else
|
|
|
|
// filterDescription = "framerate=fps=60,";
|
2021-05-29 08:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filterDescription += "format=pix_fmts=" + std::string(av_get_pix_fmt_name(outPixFormats[0]));
|
|
|
|
|
|
|
|
returnValue = avfilter_graph_parse_ptr(mVFilterGraph, filterDescription.c_str(),
|
2021-07-07 18:31:46 +00:00
|
|
|
&mVFilterInputs, &mVFilterOutputs, nullptr);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupVideoFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't add graph filter: "
|
|
|
|
<< av_make_error_string(errorMessage, sizeof(errorMessage), returnValue);
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
returnValue = avfilter_graph_config(mVFilterGraph, nullptr);
|
|
|
|
|
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupVideoFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't configure graph: "
|
|
|
|
<< av_make_error_string(errorMessage, sizeof(errorMessage), returnValue);
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoFFmpegComponent::setupAudioFilters()
|
|
|
|
{
|
|
|
|
int returnValue = 0;
|
2021-05-29 09:27:04 +00:00
|
|
|
char errorMessage[512];
|
2021-08-17 16:41:45 +00:00
|
|
|
const int outSampleRates[] = {AudioManager::getInstance()->sAudioFormat.freq, -1};
|
|
|
|
const enum AVSampleFormat outSampleFormats[] = {AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE};
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
mAFilterInputs = avfilter_inout_alloc();
|
|
|
|
mAFilterOutputs = avfilter_inout_alloc();
|
|
|
|
|
|
|
|
if (!(mAFilterGraph = avfilter_graph_alloc())) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupAudioFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't allocate filter graph";
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit the libavfilter audio processing to one additional thread.
|
|
|
|
// Not sure why the actual thread count is one less than specified.
|
|
|
|
mAFilterGraph->nb_threads = 2;
|
|
|
|
|
|
|
|
const AVFilter* bufferSrc = avfilter_get_by_name("abuffer");
|
|
|
|
if (!bufferSrc) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupAudioFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't find \"abuffer\" filter";
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AVFilter* bufferSink = avfilter_get_by_name("abuffersink");
|
|
|
|
if (!bufferSink) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupAudioFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't find \"abuffersink\" filter";
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char channelLayout[512];
|
2021-07-07 18:31:46 +00:00
|
|
|
av_get_channel_layout_string(channelLayout, sizeof(channelLayout), mAudioCodecContext->channels,
|
|
|
|
mAudioCodecContext->channel_layout);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
std::string filterArguments =
|
2021-07-07 18:31:46 +00:00
|
|
|
"time_base=" + std::to_string(mAudioStream->time_base.num) + "/" +
|
|
|
|
std::to_string(mAudioStream->time_base.den) +
|
|
|
|
":sample_rate=" + std::to_string(mAudioCodecContext->sample_rate) +
|
|
|
|
":sample_fmt=" + av_get_sample_fmt_name(mAudioCodecContext->sample_fmt) +
|
|
|
|
":channel_layout=" + channelLayout;
|
|
|
|
|
|
|
|
returnValue = avfilter_graph_create_filter(&mABufferSrcContext, bufferSrc, "in",
|
|
|
|
filterArguments.c_str(), nullptr, mAFilterGraph);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupAudioFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't create filter instance for buffer source: "
|
|
|
|
<< av_make_error_string(errorMessage, sizeof(errorMessage), returnValue);
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
returnValue = avfilter_graph_create_filter(&mABufferSinkContext, bufferSink, "out", nullptr,
|
|
|
|
nullptr, mAFilterGraph);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupAudioFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't create filter instance for buffer sink: "
|
|
|
|
<< av_make_error_string(errorMessage, sizeof(errorMessage), returnValue);
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Endpoints for the filter graph.
|
|
|
|
mAFilterInputs->name = av_strdup("out");
|
|
|
|
mAFilterInputs->filter_ctx = mABufferSinkContext;
|
|
|
|
mAFilterInputs->pad_idx = 0;
|
|
|
|
mAFilterInputs->next = nullptr;
|
|
|
|
|
|
|
|
mAFilterOutputs->name = av_strdup("in");
|
|
|
|
mAFilterOutputs->filter_ctx = mABufferSrcContext;
|
|
|
|
mAFilterOutputs->pad_idx = 0;
|
|
|
|
mAFilterOutputs->next = nullptr;
|
|
|
|
|
|
|
|
std::string filterDescription =
|
2021-07-07 18:31:46 +00:00
|
|
|
"aresample=" + std::to_string(outSampleRates[0]) + "," +
|
|
|
|
"aformat=sample_fmts=" + av_get_sample_fmt_name(outSampleFormats[0]) +
|
|
|
|
":channel_layouts=stereo,"
|
|
|
|
"asetnsamples=n=1024:p=0";
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
returnValue = avfilter_graph_parse_ptr(mAFilterGraph, filterDescription.c_str(),
|
2021-07-07 18:31:46 +00:00
|
|
|
&mAFilterInputs, &mAFilterOutputs, nullptr);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupAudioFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't add graph filter: "
|
|
|
|
<< av_make_error_string(errorMessage, sizeof(errorMessage), returnValue);
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
returnValue = avfilter_graph_config(mAFilterGraph, nullptr);
|
|
|
|
|
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::setupAudioFilters(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't configure graph: "
|
|
|
|
<< av_make_error_string(errorMessage, sizeof(errorMessage), returnValue);
|
2021-05-29 08:58:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoFFmpegComponent::readFrames()
|
|
|
|
{
|
2021-05-29 08:58:51 +00:00
|
|
|
int readFrameReturn = 0;
|
|
|
|
|
|
|
|
// It's not clear if this can actually happen in practise, but in theory we could
|
|
|
|
// continue to load frames indefinitely and run out of memory if invalid PTS values
|
|
|
|
// are presented by FFmpeg.
|
|
|
|
if (mVideoFrameQueue.size() > 300 || mAudioFrameQueue.size() > 600)
|
|
|
|
return;
|
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
if (mVideoCodecContext && mFormatContext) {
|
2021-09-19 12:37:10 +00:00
|
|
|
if (static_cast<int>(mVideoFrameQueue.size()) < mVideoTargetQueueSize ||
|
|
|
|
(mAudioStreamIndex >= 0 &&
|
|
|
|
static_cast<int>(mAudioFrameQueue.size()) < mAudioTargetQueueSize)) {
|
2021-06-11 15:02:06 +00:00
|
|
|
while ((readFrameReturn = av_read_frame(mFormatContext, mPacket)) >= 0) {
|
2021-05-09 20:52:26 +00:00
|
|
|
if (mPacket->stream_index == mVideoStreamIndex) {
|
|
|
|
if (!avcodec_send_packet(mVideoCodecContext, mPacket) &&
|
2021-07-07 18:31:46 +00:00
|
|
|
!avcodec_receive_frame(mVideoCodecContext, mVideoFrame)) {
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-07-09 17:47:33 +00:00
|
|
|
int returnValue = 0;
|
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
if (mSWDecoder) {
|
2021-07-09 17:47:33 +00:00
|
|
|
returnValue = av_buffersrc_add_frame_flags(
|
2021-07-14 17:13:25 +00:00
|
|
|
mVBufferSrcContext, mVideoFrame, AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT);
|
2021-07-09 17:47:33 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-07-14 17:13:25 +00:00
|
|
|
AVFrame* destFrame = nullptr;
|
|
|
|
destFrame = av_frame_alloc();
|
|
|
|
|
|
|
|
if (mVideoFrame->format == sPixelFormat) {
|
|
|
|
if (av_hwframe_transfer_data(destFrame, mVideoFrame, 0) < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::readFrames(): "
|
|
|
|
"Couldn't transfer decoded video frame to "
|
|
|
|
"system memory";
|
|
|
|
av_frame_free(&destFrame);
|
|
|
|
av_packet_unref(mPacket);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
destFrame->pts = mVideoFrame->pts;
|
|
|
|
destFrame->pkt_dts = mVideoFrame->pkt_dts;
|
|
|
|
destFrame->pict_type = mVideoFrame->pict_type;
|
|
|
|
destFrame->chroma_location = mVideoFrame->chroma_location;
|
|
|
|
destFrame->pkt_pos = mVideoFrame->pkt_pos;
|
|
|
|
destFrame->pkt_duration = mVideoFrame->pkt_duration;
|
|
|
|
destFrame->pkt_size = mVideoFrame->pkt_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::readFrames(): "
|
|
|
|
"Couldn't decode video frame";
|
|
|
|
}
|
|
|
|
|
|
|
|
returnValue = av_buffersrc_add_frame_flags(
|
|
|
|
mVBufferSrcContext, destFrame, AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT);
|
|
|
|
av_frame_free(&destFrame);
|
2021-07-09 17:47:33 +00:00
|
|
|
}
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::readFrames(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't add video frame to buffer source";
|
2021-05-29 08:58:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
av_packet_unref(mPacket);
|
|
|
|
break;
|
|
|
|
}
|
2021-07-14 17:13:25 +00:00
|
|
|
else {
|
|
|
|
av_packet_unref(mPacket);
|
|
|
|
}
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
else if (mPacket->stream_index == mAudioStreamIndex) {
|
|
|
|
if (!avcodec_send_packet(mAudioCodecContext, mPacket) &&
|
2021-07-07 18:31:46 +00:00
|
|
|
!avcodec_receive_frame(mAudioCodecContext, mAudioFrame)) {
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
// We have an audio frame that needs conversion and resampling.
|
2021-07-07 18:31:46 +00:00
|
|
|
int returnValue = av_buffersrc_add_frame_flags(
|
|
|
|
mABufferSrcContext, mAudioFrame, AV_BUFFERSRC_FLAG_KEEP_REF);
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
if (returnValue < 0) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::readFrames(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Couldn't add audio frame to buffer source";
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
av_packet_unref(mPacket);
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-14 17:13:25 +00:00
|
|
|
else {
|
|
|
|
av_packet_unref(mPacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Ignore any stream that is not video or audio.
|
|
|
|
av_packet_unref(mPacket);
|
2021-05-29 08:58:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
if (readFrameReturn < 0)
|
|
|
|
mEndOfVideo = true;
|
|
|
|
}
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
void VideoFFmpegComponent::getProcessedFrames()
|
|
|
|
{
|
|
|
|
// Video frames.
|
|
|
|
while (av_buffersink_get_frame(mVBufferSinkContext, mVideoFrameResampled) >= 0) {
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
// Save the frame into the queue for later processing.
|
|
|
|
VideoFrame currFrame;
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
currFrame.width = mVideoFrameResampled->width;
|
|
|
|
currFrame.height = mVideoFrameResampled->height;
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
mVideoFrameResampled->best_effort_timestamp = mVideoFrameResampled->pkt_dts;
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
// The PTS value is the presentation time, i.e. the time stamp when the frame
|
|
|
|
// (picture) should be displayed. The packet DTS value is used for the basis of
|
|
|
|
// the calculation as per the recommendation in the FFmpeg documentation for
|
|
|
|
// the av_read_frame function.
|
2021-07-07 18:31:46 +00:00
|
|
|
double pts =
|
|
|
|
static_cast<double>(mVideoFrameResampled->pkt_dts) * av_q2d(mVideoStream->time_base);
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
// Needs to be adjusted if changing the rate?
|
|
|
|
double frameDuration = static_cast<double>(mVideoFrameResampled->pkt_duration) *
|
2021-07-07 18:31:46 +00:00
|
|
|
av_q2d(mVideoStream->time_base);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
currFrame.pts = pts;
|
|
|
|
currFrame.frameDuration = frameDuration;
|
|
|
|
|
|
|
|
int bufferSize = mVideoFrameResampled->width * mVideoFrameResampled->height * 4;
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
currFrame.frameRGBA.insert(currFrame.frameRGBA.begin(), &mVideoFrameResampled->data[0][0],
|
|
|
|
&mVideoFrameResampled->data[0][bufferSize]);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
mVideoFrameQueue.push(currFrame);
|
|
|
|
av_frame_unref(mVideoFrameResampled);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Audio frames.
|
|
|
|
// When resampling, we may not always get a frame returned from the sink as there may not
|
|
|
|
// have been enough data available to the filter.
|
2021-07-07 18:31:46 +00:00
|
|
|
while (mAudioCodecContext &&
|
|
|
|
av_buffersink_get_frame(mABufferSinkContext, mAudioFrameResampled) >= 0) {
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
AudioFrame currFrame;
|
2021-07-04 09:36:16 +00:00
|
|
|
AVRational timeBase;
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
mAudioFrameResampled->best_effort_timestamp = mAudioFrameResampled->pts;
|
|
|
|
|
|
|
|
timeBase.num = 1;
|
|
|
|
timeBase.den = mAudioFrameResampled->sample_rate;
|
|
|
|
|
|
|
|
double pts = mAudioFrameResampled->pts * av_q2d(timeBase);
|
|
|
|
currFrame.pts = pts;
|
|
|
|
|
|
|
|
int bufferSize = mAudioFrameResampled->nb_samples * mAudioFrameResampled->channels *
|
2021-07-07 18:31:46 +00:00
|
|
|
av_get_bytes_per_sample(AV_SAMPLE_FMT_FLT);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
currFrame.resampledData.insert(currFrame.resampledData.begin(),
|
2021-07-07 18:31:46 +00:00
|
|
|
&mAudioFrameResampled->data[0][0],
|
|
|
|
&mAudioFrameResampled->data[0][bufferSize]);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
mAudioFrameQueue.push(currFrame);
|
|
|
|
av_frame_unref(mAudioFrameResampled);
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:08:45 +00:00
|
|
|
void VideoFFmpegComponent::outputFrames()
|
2021-05-09 20:52:26 +00:00
|
|
|
{
|
|
|
|
// Check if we should start counting the time (i.e. start playing the video).
|
|
|
|
// The audio stream controls when the playback and time counting starts, assuming
|
|
|
|
// there is an audio track.
|
|
|
|
if (!mAudioCodecContext || (mAudioCodecContext && !mAudioFrameQueue.empty())) {
|
|
|
|
if (!mStartTimeAccumulation) {
|
|
|
|
mTimeReference = std::chrono::high_resolution_clock::now();
|
|
|
|
mStartTimeAccumulation = true;
|
|
|
|
mIsActuallyPlaying = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
// Process the audio frames that have a PTS value below mAccumulatedTime (plus a small
|
2021-05-12 20:55:00 +00:00
|
|
|
// buffer to avoid underflows).
|
2021-05-09 20:52:26 +00:00
|
|
|
while (!mAudioFrameQueue.empty()) {
|
2021-05-12 20:55:00 +00:00
|
|
|
if (mAudioFrameQueue.front().pts < mAccumulatedTime + AUDIO_BUFFER) {
|
2021-05-09 20:52:26 +00:00
|
|
|
// Enable only when needed, as this generates a lot of debug output.
|
2021-07-04 09:36:16 +00:00
|
|
|
if (DEBUG_VIDEO) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogDebug) << "Processing audio frame with PTS: "
|
|
|
|
<< mAudioFrameQueue.front().pts;
|
|
|
|
LOG(LogDebug) << "Total audio frames processed / audio frame queue size: "
|
|
|
|
<< mAudioFrameCount << " / "
|
|
|
|
<< std::to_string(mAudioFrameQueue.size());
|
2021-07-04 09:36:16 +00:00
|
|
|
}
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-16 11:12:31 +00:00
|
|
|
bool outputSound = false;
|
|
|
|
|
|
|
|
if ((!mScreensaverMode && !mMediaViewerMode) &&
|
2021-07-07 18:31:46 +00:00
|
|
|
Settings::getInstance()->getBool("GamelistVideoAudio"))
|
2021-05-16 11:12:31 +00:00
|
|
|
outputSound = true;
|
2021-07-07 18:31:46 +00:00
|
|
|
else if (mScreensaverMode && Settings::getInstance()->getBool("ScreensaverVideoAudio"))
|
2021-05-16 11:12:31 +00:00
|
|
|
outputSound = true;
|
2021-07-07 18:31:46 +00:00
|
|
|
else if (mMediaViewerMode && Settings::getInstance()->getBool("MediaViewerVideoAudio"))
|
2021-05-16 11:12:31 +00:00
|
|
|
outputSound = true;
|
|
|
|
|
|
|
|
if (outputSound) {
|
2021-05-29 08:58:51 +00:00
|
|
|
// The audio is output to AudioManager from updatePlayer() in the main thread.
|
|
|
|
mAudioMutex.lock();
|
|
|
|
|
|
|
|
mOutputAudio.insert(mOutputAudio.end(),
|
2021-07-07 18:31:46 +00:00
|
|
|
mAudioFrameQueue.front().resampledData.begin(),
|
|
|
|
mAudioFrameQueue.front().resampledData.end());
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
mAudioMutex.unlock();
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
mAudioFrameQueue.pop();
|
|
|
|
mAudioFrameCount++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
// Process all available video frames that have a PTS value below mAccumulatedTime.
|
2021-05-11 15:35:55 +00:00
|
|
|
// But if more than one frame is processed here, it means that the computer can't
|
|
|
|
// keep up for some reason.
|
2021-05-09 20:52:26 +00:00
|
|
|
while (mIsActuallyPlaying && !mVideoFrameQueue.empty()) {
|
|
|
|
if (mVideoFrameQueue.front().pts < mAccumulatedTime) {
|
|
|
|
// Enable only when needed, as this generates a lot of debug output.
|
2021-07-04 09:36:16 +00:00
|
|
|
if (DEBUG_VIDEO) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogDebug) << "Processing video frame with PTS: "
|
|
|
|
<< mVideoFrameQueue.front().pts;
|
|
|
|
LOG(LogDebug) << "Total video frames processed / video frame queue size: "
|
|
|
|
<< mVideoFrameCount << " / "
|
|
|
|
<< std::to_string(mVideoFrameQueue.size());
|
2021-07-04 09:36:16 +00:00
|
|
|
}
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-11 15:35:55 +00:00
|
|
|
mPictureMutex.lock();
|
|
|
|
|
2021-05-19 17:47:40 +00:00
|
|
|
// Give some leeway for frames that have not yet been rendered but that have pts
|
|
|
|
// values with a time difference relative to the frame duration that is under a
|
|
|
|
// certain threshold. In this case, give the renderer an additional chance to output
|
|
|
|
// the frames. If the difference exceeds the threshold though, then skip them as
|
|
|
|
// otherwise videos would just slow down instead of skipping frames when the computer
|
|
|
|
// can't keep up. This approach primarily decreases stuttering for videos with frame
|
2021-05-29 08:58:51 +00:00
|
|
|
// rates close to, or at, the rendering frame rate, for example 59.94 and 60 FPS.
|
|
|
|
if (mDecodedFrame && !mOutputPicture.hasBeenRendered) {
|
2021-05-19 17:47:40 +00:00
|
|
|
double timeDifference = mAccumulatedTime - mVideoFrameQueue.front().pts -
|
2021-07-07 18:31:46 +00:00
|
|
|
mVideoFrameQueue.front().frameDuration * 2.0l;
|
2021-05-19 17:47:40 +00:00
|
|
|
if (timeDifference < mVideoFrameQueue.front().frameDuration) {
|
|
|
|
mPictureMutex.unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 15:35:55 +00:00
|
|
|
mOutputPicture.pictureRGBA.clear();
|
|
|
|
mOutputPicture.pictureRGBA.insert(mOutputPicture.pictureRGBA.begin(),
|
2021-07-07 18:31:46 +00:00
|
|
|
mVideoFrameQueue.front().frameRGBA.begin(),
|
|
|
|
mVideoFrameQueue.front().frameRGBA.end());
|
2021-05-11 15:35:55 +00:00
|
|
|
|
|
|
|
mOutputPicture.width = mVideoFrameQueue.front().width;
|
|
|
|
mOutputPicture.height = mVideoFrameQueue.front().height;
|
2021-05-19 17:47:40 +00:00
|
|
|
mOutputPicture.hasBeenRendered = false;
|
2021-05-11 15:35:55 +00:00
|
|
|
|
|
|
|
mPictureMutex.unlock();
|
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
mVideoFrameQueue.pop();
|
|
|
|
mVideoFrameCount++;
|
2021-05-19 17:47:40 +00:00
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
mDecodedFrame = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoFFmpegComponent::calculateBlackRectangle()
|
|
|
|
{
|
|
|
|
// Calculate the position and size for the black rectangle that will be rendered behind
|
|
|
|
// videos. If the option to display pillarboxes (and letterboxes) is enabled, then this
|
|
|
|
// would extend to the entire md_video area (if above the threshold as defined below) or
|
|
|
|
// otherwise it will exactly match the video size. The reason to add a black rectangle
|
|
|
|
// behind videos in this second instance is that the scanline rendering will make the
|
|
|
|
// video partially transparent so this may avoid some unforseen issues with some themes.
|
2021-08-17 16:41:45 +00:00
|
|
|
if (mVideoAreaPos != glm::vec2{} && mVideoAreaSize != glm::vec2{}) {
|
2021-05-09 20:52:26 +00:00
|
|
|
mVideoRectangleCoords.clear();
|
|
|
|
|
|
|
|
if (Settings::getInstance()->getBool("GamelistVideoPillarbox")) {
|
|
|
|
float rectHeight;
|
|
|
|
float rectWidth;
|
|
|
|
// Video is in landscape orientation.
|
2021-08-16 16:25:01 +00:00
|
|
|
if (mSize.x > mSize.y) {
|
2021-05-09 20:52:26 +00:00
|
|
|
// Checking the Y size should not normally be required as landscape format
|
|
|
|
// should mean the height can't be higher than the max size defined by the
|
|
|
|
// theme. But as the height in mSize is provided by FFmpeg in integer format
|
|
|
|
// and then scaled, there could be rounding errors that make the video height
|
|
|
|
// slightly higher than allowed. It's only a single pixel or a few pixels, but
|
|
|
|
// it's still visible for some videos.
|
2021-08-16 16:25:01 +00:00
|
|
|
if (mSize.y < mVideoAreaSize.y && mSize.y / mVideoAreaSize.y < 0.90f)
|
|
|
|
rectHeight = mVideoAreaSize.y;
|
2021-05-09 20:52:26 +00:00
|
|
|
else
|
2021-08-16 16:25:01 +00:00
|
|
|
rectHeight = mSize.y;
|
2021-05-09 20:52:26 +00:00
|
|
|
// Don't add a black border that is too narrow, that's what the 0.85 constant
|
|
|
|
// takes care of.
|
2021-08-16 16:25:01 +00:00
|
|
|
if (mSize.x < mVideoAreaSize.x && mSize.x / mVideoAreaSize.x < 0.85f)
|
|
|
|
rectWidth = mVideoAreaSize.x;
|
2021-05-09 20:52:26 +00:00
|
|
|
else
|
2021-08-16 16:25:01 +00:00
|
|
|
rectWidth = mSize.x;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
// Video is in portrait orientation (or completely square).
|
|
|
|
else {
|
2021-08-16 16:25:01 +00:00
|
|
|
rectWidth = mVideoAreaSize.x;
|
|
|
|
rectHeight = mSize.y;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
// Populate the rectangle coordinates to be used in render().
|
2021-08-16 16:25:01 +00:00
|
|
|
mVideoRectangleCoords.push_back(std::round(mVideoAreaPos.x - rectWidth * mOrigin.x));
|
|
|
|
mVideoRectangleCoords.push_back(std::round(mVideoAreaPos.y - rectHeight * mOrigin.y));
|
2021-05-09 20:52:26 +00:00
|
|
|
mVideoRectangleCoords.push_back(std::round(rectWidth));
|
|
|
|
mVideoRectangleCoords.push_back(std::round(rectHeight));
|
|
|
|
}
|
|
|
|
// If the option to display pillarboxes is disabled, then make the rectangle equivalent
|
|
|
|
// to the size of the video.
|
|
|
|
else {
|
2021-08-16 16:25:01 +00:00
|
|
|
mVideoRectangleCoords.push_back(std::round(mPosition.x - mSize.x * mOrigin.x));
|
|
|
|
mVideoRectangleCoords.push_back(std::round(mPosition.y - mSize.y * mOrigin.y));
|
|
|
|
mVideoRectangleCoords.push_back(std::round(mSize.x));
|
|
|
|
mVideoRectangleCoords.push_back(std::round(mSize.y));
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
void VideoFFmpegComponent::detectHWDecoder()
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::detectHWDecoder(): Using hardware decoder VideoToolbox";
|
|
|
|
sDeviceType = AV_HWDEVICE_TYPE_VIDEOTOOLBOX;
|
|
|
|
return;
|
|
|
|
#elif defined(_WIN64)
|
|
|
|
bool hasDXVA2 = false;
|
|
|
|
bool hasD3D11VA = false;
|
|
|
|
|
|
|
|
AVBufferRef* testContext = nullptr;
|
|
|
|
AVHWDeviceType tempDevice = AV_HWDEVICE_TYPE_NONE;
|
|
|
|
|
|
|
|
while ((tempDevice = av_hwdevice_iterate_types(tempDevice)) != AV_HWDEVICE_TYPE_NONE) {
|
|
|
|
// The Direct3D 11 decoder detection seems to cause stability issues on some machines
|
|
|
|
// so disabling it for now.
|
|
|
|
if (tempDevice == AV_HWDEVICE_TYPE_DXVA2) {
|
|
|
|
// if (tempDevice == AV_HWDEVICE_TYPE_DXVA2 || tempDevice == AV_HWDEVICE_TYPE_D3D11VA) {
|
|
|
|
if (av_hwdevice_ctx_create(&testContext, tempDevice, nullptr, nullptr, 0) >= 0) {
|
|
|
|
if (tempDevice == AV_HWDEVICE_TYPE_DXVA2)
|
|
|
|
hasDXVA2 = true;
|
|
|
|
else
|
|
|
|
hasD3D11VA = true;
|
|
|
|
}
|
|
|
|
av_buffer_unref(&testContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prioritize DXVA2.
|
|
|
|
if (hasDXVA2) {
|
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::detectHWDecoder(): Using hardware decoder DXVA2";
|
|
|
|
sDeviceType = AV_HWDEVICE_TYPE_DXVA2;
|
|
|
|
}
|
|
|
|
else if (hasD3D11VA) {
|
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::detectHWDecoder(): Using hardware decoder D3D11VA";
|
|
|
|
sDeviceType = AV_HWDEVICE_TYPE_D3D11VA;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogWarning) << "VideoFFmpegComponent::detectHWDecoder(): Unable to detect any usable "
|
|
|
|
"hardware decoder";
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// This would mostly be Linux, but possibly also BSD Unix.
|
|
|
|
|
|
|
|
bool hasVAAPI = false;
|
|
|
|
bool hasVDPAU = false;
|
|
|
|
|
|
|
|
AVBufferRef* testContext = nullptr;
|
|
|
|
AVHWDeviceType tempDevice = AV_HWDEVICE_TYPE_NONE;
|
|
|
|
|
|
|
|
while ((tempDevice = av_hwdevice_iterate_types(tempDevice)) != AV_HWDEVICE_TYPE_NONE) {
|
|
|
|
if (tempDevice == AV_HWDEVICE_TYPE_VDPAU || tempDevice == AV_HWDEVICE_TYPE_VAAPI) {
|
|
|
|
if (av_hwdevice_ctx_create(&testContext, tempDevice, nullptr, nullptr, 0) >= 0) {
|
|
|
|
if (tempDevice == AV_HWDEVICE_TYPE_VAAPI)
|
|
|
|
hasVAAPI = true;
|
|
|
|
else
|
|
|
|
hasVDPAU = true;
|
|
|
|
}
|
|
|
|
av_buffer_unref(&testContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prioritize VAAPI.
|
|
|
|
if (hasVAAPI) {
|
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::detectHWDecoder(): Using hardware decoder VAAPI";
|
|
|
|
sDeviceType = AV_HWDEVICE_TYPE_VAAPI;
|
|
|
|
}
|
|
|
|
else if (hasVDPAU) {
|
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::detectHWDecoder(): Using hardware decoder VDPAU";
|
|
|
|
sDeviceType = AV_HWDEVICE_TYPE_VDPAU;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogWarning) << "VideoFFmpegComponent::detectHWDecoder(): Unable to detect any "
|
|
|
|
"usable hardware decoder";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoFFmpegComponent::decoderInitHW()
|
|
|
|
{
|
|
|
|
// This should only be required the first time any video is played.
|
|
|
|
if (sDeviceType == AV_HWDEVICE_TYPE_NONE)
|
|
|
|
detectHWDecoder();
|
|
|
|
|
|
|
|
// If there is no device, the detection failed.
|
|
|
|
if (sDeviceType == AV_HWDEVICE_TYPE_NONE)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the hardware decoding of the file was previously unsuccessful during the program
|
|
|
|
// session, then don't attempt it again.
|
|
|
|
if (std::find(sSWDecodedVideos.begin(), sSWDecodedVideos.end(), mVideoPath) !=
|
|
|
|
sSWDecodedVideos.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 50 is just an arbitrary number so we don't potentially get stuck in an endless loop.
|
|
|
|
for (int i = 0; i < 50; i++) {
|
|
|
|
const AVCodecHWConfig* config = avcodec_get_hw_config(mHardwareCodec, i);
|
|
|
|
if (!config) {
|
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::decoderInitHW(): Hardware decoder \""
|
|
|
|
<< av_hwdevice_get_type_name(sDeviceType)
|
|
|
|
<< "\" does not seem to support codec \"" << mHardwareCodec->name << "\"";
|
|
|
|
}
|
|
|
|
else if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
|
|
|
|
config->device_type == sDeviceType) {
|
|
|
|
sPixelFormat = config->pix_fmt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the pixel format is not set properly, then hardware decoding won't work for the file.
|
|
|
|
if (sPixelFormat == AV_PIX_FMT_NONE)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (av_hwdevice_ctx_create(&mHwContext, sDeviceType, nullptr, nullptr, 0) < 0) {
|
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::decoderInitHW(): Unable to open hardware device \""
|
|
|
|
<< av_hwdevice_get_type_name(sDeviceType) << "\"";
|
|
|
|
av_buffer_unref(&mHwContext);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback function for AVCodecContext.
|
|
|
|
// clang-format off
|
|
|
|
auto formatFunc =
|
|
|
|
[](AVCodecContext* ctx, const enum AVPixelFormat* pix_fmts) -> enum AVPixelFormat {
|
|
|
|
|
|
|
|
const enum AVPixelFormat* pixelFormats;
|
|
|
|
|
|
|
|
for (pixelFormats = pix_fmts; *pixelFormats != -1; pixelFormats++)
|
|
|
|
if (*pixelFormats == sPixelFormat)
|
|
|
|
return static_cast<enum AVPixelFormat>(sPixelFormat);
|
|
|
|
|
|
|
|
return AV_PIX_FMT_NONE;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if the video can actually be hardware decoded (unless this has already been done).
|
|
|
|
if (std::find(sHWDecodedVideos.begin(), sHWDecodedVideos.end(), mVideoPath) ==
|
|
|
|
sHWDecodedVideos.end()) {
|
|
|
|
|
|
|
|
// clang-format on
|
|
|
|
AVCodecContext* checkCodecContext = avcodec_alloc_context3(mHardwareCodec);
|
|
|
|
|
|
|
|
if (avcodec_parameters_to_context(checkCodecContext, mVideoStream->codecpar)) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't fill the video codec context parameters for file \""
|
|
|
|
<< mVideoPath << "\"";
|
|
|
|
avcodec_free_context(&checkCodecContext);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bool onlySWDecode = false;
|
|
|
|
|
|
|
|
checkCodecContext->get_format = formatFunc;
|
|
|
|
checkCodecContext->hw_device_ctx = av_buffer_ref(mHwContext);
|
|
|
|
|
|
|
|
if (avcodec_open2(checkCodecContext, mHardwareCodec, nullptr)) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't initialize the video codec context for file \""
|
|
|
|
<< mVideoPath << "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
AVPacket* checkPacket = av_packet_alloc();
|
|
|
|
int readFrameReturn = 0;
|
|
|
|
|
|
|
|
while ((readFrameReturn = av_read_frame(mFormatContext, checkPacket)) == 0) {
|
|
|
|
if (checkPacket->stream_index != mVideoStreamIndex)
|
|
|
|
av_packet_unref(checkPacket);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Supplying a packet to the decoder will cause an immediate error for some videos
|
|
|
|
// while others will require that one or several frame receive attempts are performed
|
|
|
|
// before we get a definitive result. On error we fall back to the software decoder.
|
|
|
|
if (readFrameReturn == 0 && checkPacket->stream_index == mVideoStreamIndex) {
|
|
|
|
if (avcodec_send_packet(checkCodecContext, checkPacket) < 0) {
|
|
|
|
// Save the file path to the list of videos that require software decoding
|
|
|
|
// so we don't have to check it again during the program session.
|
|
|
|
sSWDecodedVideos.emplace_back(mVideoPath);
|
|
|
|
onlySWDecode = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
AVFrame* checkFrame;
|
|
|
|
checkFrame = av_frame_alloc();
|
|
|
|
|
|
|
|
onlySWDecode = true;
|
|
|
|
|
|
|
|
// For some videos we need to process at least one extra frame to verify
|
|
|
|
// that the hardware encoder can actually be used, otherwise the fallback
|
|
|
|
// to software decoding would take place when it's not necessary.
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
if (avcodec_receive_frame(checkCodecContext, checkFrame) < 0) {
|
|
|
|
av_packet_unref(checkPacket);
|
|
|
|
while (av_read_frame(mFormatContext, checkPacket) == 0) {
|
|
|
|
if (checkPacket->stream_index != mVideoStreamIndex)
|
|
|
|
av_packet_unref(checkPacket);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
avcodec_send_packet(checkCodecContext, checkPacket);
|
|
|
|
av_packet_unref(checkPacket);
|
|
|
|
|
|
|
|
if (avcodec_receive_frame(checkCodecContext, checkFrame) == 0) {
|
|
|
|
onlySWDecode = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
onlySWDecode = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
onlySWDecode = false;
|
|
|
|
}
|
|
|
|
av_packet_unref(checkPacket);
|
|
|
|
av_frame_unref(checkFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&checkFrame);
|
|
|
|
|
|
|
|
if (onlySWDecode == false) {
|
|
|
|
// Save the file path to the list of videos that work with hardware
|
|
|
|
// decoding so we don't have to check it again during the program session.
|
|
|
|
sHWDecodedVideos.emplace_back(mVideoPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_packet_free(&checkPacket);
|
|
|
|
avcodec_free_context(&checkCodecContext);
|
|
|
|
|
|
|
|
// Seek back to the start position of the file.
|
|
|
|
av_seek_frame(mFormatContext, -1, 0, AVSEEK_FLAG_ANY);
|
|
|
|
|
|
|
|
if (onlySWDecode)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The hardware decoding check passed successfully or it was done previously for the file.
|
|
|
|
// Now perform the real setup.
|
|
|
|
mVideoCodecContext = avcodec_alloc_context3(mHardwareCodec);
|
|
|
|
|
|
|
|
if (!mVideoCodecContext) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't allocate video codec context for file \""
|
|
|
|
<< mVideoPath << "\"";
|
|
|
|
avcodec_free_context(&mVideoCodecContext);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avcodec_parameters_to_context(mVideoCodecContext, mVideoStream->codecpar)) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't fill the video codec context parameters for file \""
|
|
|
|
<< mVideoPath << "\"";
|
|
|
|
avcodec_free_context(&mVideoCodecContext);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mVideoCodecContext->get_format = formatFunc;
|
|
|
|
mVideoCodecContext->hw_device_ctx = av_buffer_ref(mHwContext);
|
|
|
|
|
|
|
|
if (avcodec_open2(mVideoCodecContext, mHardwareCodec, nullptr)) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't initialize the video codec context for file \""
|
|
|
|
<< mVideoPath << "\"";
|
|
|
|
avcodec_free_context(&mVideoCodecContext);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
void VideoFFmpegComponent::startVideo()
|
|
|
|
{
|
|
|
|
if (!mFormatContext) {
|
2021-07-14 17:13:25 +00:00
|
|
|
mHardwareCodec = nullptr;
|
|
|
|
mHwContext = nullptr;
|
2021-05-11 15:35:55 +00:00
|
|
|
mFrameProcessingThread = nullptr;
|
2021-05-09 20:52:26 +00:00
|
|
|
mVideoWidth = 0;
|
|
|
|
mVideoHeight = 0;
|
|
|
|
mAccumulatedTime = 0;
|
|
|
|
mStartTimeAccumulation = false;
|
2021-07-14 17:13:25 +00:00
|
|
|
mSWDecoder = true;
|
2021-05-09 20:52:26 +00:00
|
|
|
mDecodedFrame = false;
|
|
|
|
mEndOfVideo = false;
|
|
|
|
mVideoFrameCount = 0;
|
|
|
|
mAudioFrameCount = 0;
|
2021-05-19 17:47:40 +00:00
|
|
|
mOutputPicture = {};
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-06-22 16:17:06 +00:00
|
|
|
// Get an empty texture for rendering the video.
|
|
|
|
mTexture = TextureResource::get("");
|
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
// This is used for the audio and video synchronization.
|
|
|
|
mTimeReference = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
// Clear the video and audio frame queues.
|
|
|
|
std::queue<VideoFrame>().swap(mVideoFrameQueue);
|
|
|
|
std::queue<AudioFrame>().swap(mAudioFrameQueue);
|
|
|
|
|
|
|
|
std::string filePath = "file:" + mVideoPath;
|
|
|
|
|
|
|
|
// This will disable the FFmpeg logging, so comment this out if debug info is needed.
|
|
|
|
av_log_set_callback(nullptr);
|
|
|
|
|
|
|
|
// File operations and basic setup.
|
|
|
|
|
|
|
|
if (avformat_open_input(&mFormatContext, filePath.c_str(), nullptr, nullptr)) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't open video file \""
|
|
|
|
<< mVideoPath << "\"";
|
2021-05-09 20:52:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avformat_find_stream_info(mFormatContext, nullptr)) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't read stream information from video file \""
|
|
|
|
<< mVideoPath << "\"";
|
2021-05-09 20:52:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mVideoStreamIndex = -1;
|
|
|
|
mAudioStreamIndex = -1;
|
|
|
|
|
|
|
|
// Video stream setup.
|
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
#if defined(_RPI_)
|
|
|
|
bool hwDecoding = false;
|
|
|
|
#else
|
|
|
|
bool hwDecoding = Settings::getInstance()->getBool("VideoHardwareDecoding");
|
|
|
|
#endif
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
mVideoStreamIndex =
|
2021-07-14 17:13:25 +00:00
|
|
|
av_find_best_stream(mFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, &mHardwareCodec, 0);
|
2021-05-09 20:52:26 +00:00
|
|
|
|
|
|
|
if (mVideoStreamIndex < 0) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't retrieve video stream for file \""
|
|
|
|
<< mVideoPath << "\"";
|
2021-07-14 17:13:25 +00:00
|
|
|
avformat_close_input(&mFormatContext);
|
|
|
|
avformat_free_context(mFormatContext);
|
2021-05-09 20:52:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mVideoStream = mFormatContext->streams[mVideoStreamIndex];
|
|
|
|
mVideoWidth = mFormatContext->streams[mVideoStreamIndex]->codecpar->width;
|
|
|
|
mVideoHeight = mFormatContext->streams[mVideoStreamIndex]->codecpar->height;
|
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
<< "Playing video \"" << mVideoPath << "\" (codec: "
|
|
|
|
<< avcodec_get_name(
|
|
|
|
mFormatContext->streams[mVideoStreamIndex]->codecpar->codec_id)
|
|
|
|
<< ", decoder: " << (hwDecoding ? "hardware" : "software") << ")";
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
if (hwDecoding)
|
|
|
|
mSWDecoder = decoderInitHW();
|
|
|
|
else
|
|
|
|
mSWDecoder = true;
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
if (mSWDecoder) {
|
|
|
|
// The hardware decoder initialization failed, which can happen for a number of reasons.
|
|
|
|
if (hwDecoding) {
|
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::startVideo(): Hardware decoding failed, "
|
|
|
|
"falling back to software decoder";
|
|
|
|
}
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
mVideoCodec =
|
|
|
|
const_cast<AVCodec*>(avcodec_find_decoder(mVideoStream->codecpar->codec_id));
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
if (!mVideoCodec) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't find a suitable video codec for file \""
|
|
|
|
<< mVideoPath << "\"";
|
|
|
|
return;
|
|
|
|
}
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
mVideoCodecContext = avcodec_alloc_context3(mVideoCodec);
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
if (!mVideoCodecContext) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't allocate video codec context for file \""
|
|
|
|
<< mVideoPath << "\"";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED)
|
|
|
|
mVideoCodecContext->flags |= AV_CODEC_FLAG_TRUNCATED;
|
|
|
|
|
|
|
|
if (avcodec_parameters_to_context(mVideoCodecContext, mVideoStream->codecpar)) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't fill the video codec context parameters for file \""
|
|
|
|
<< mVideoPath << "\"";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avcodec_open2(mVideoCodecContext, mVideoCodec, nullptr)) {
|
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't initialize the video codec context for file \""
|
|
|
|
<< mVideoPath << "\"";
|
|
|
|
return;
|
|
|
|
}
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
// Audio stream setup, optional as some videos do not have any audio tracks.
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
mAudioStreamIndex =
|
|
|
|
av_find_best_stream(mFormatContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
|
2021-05-09 20:52:26 +00:00
|
|
|
|
|
|
|
if (mAudioStreamIndex < 0) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogDebug) << "VideoFFmpegComponent::startVideo(): "
|
2021-07-14 17:13:25 +00:00
|
|
|
"File does not seem to contain any audio streams";
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mAudioStreamIndex >= 0) {
|
|
|
|
mAudioStream = mFormatContext->streams[mAudioStreamIndex];
|
2021-07-07 18:31:46 +00:00
|
|
|
mAudioCodec =
|
|
|
|
const_cast<AVCodec*>(avcodec_find_decoder(mAudioStream->codecpar->codec_id));
|
2021-05-09 20:52:26 +00:00
|
|
|
|
|
|
|
if (!mAudioCodec) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogError) << "Couldn't find a suitable audio codec for file \"" << mVideoPath
|
|
|
|
<< "\"";
|
2021-05-09 20:52:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAudioCodecContext = avcodec_alloc_context3(mAudioCodec);
|
|
|
|
|
|
|
|
if (mAudioCodec->capabilities & AV_CODEC_CAP_TRUNCATED)
|
|
|
|
mAudioCodecContext->flags |= AV_CODEC_FLAG_TRUNCATED;
|
|
|
|
|
|
|
|
// Some formats want separate stream headers.
|
|
|
|
if (mAudioCodecContext->flags & AVFMT_GLOBALHEADER)
|
|
|
|
mAudioCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
|
|
|
|
if (avcodec_parameters_to_context(mAudioCodecContext, mAudioStream->codecpar)) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't fill the audio codec context parameters for file \""
|
|
|
|
<< mVideoPath << "\"";
|
2021-05-09 20:52:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avcodec_open2(mAudioCodecContext, mAudioCodec, nullptr)) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogError) << "VideoFFmpegComponent::startVideo(): "
|
|
|
|
"Couldn't initialize the audio codec context for file \""
|
|
|
|
<< mVideoPath << "\"";
|
2021-05-09 20:52:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mVideoTimeBase = 1.0l / av_q2d(mVideoStream->avg_frame_rate);
|
2021-05-29 08:58:51 +00:00
|
|
|
|
|
|
|
// Set some reasonable target queue sizes (buffers).
|
|
|
|
mVideoTargetQueueSize = static_cast<int>(av_q2d(mVideoStream->avg_frame_rate) / 2.0l);
|
2021-07-07 18:31:46 +00:00
|
|
|
if (mAudioStreamIndex >= 0)
|
2021-05-29 08:58:51 +00:00
|
|
|
mAudioTargetQueueSize = mAudioStream->codecpar->channels * 15;
|
2021-05-09 20:52:26 +00:00
|
|
|
else
|
2021-05-29 08:58:51 +00:00
|
|
|
mAudioTargetQueueSize = 30;
|
2021-05-09 20:52:26 +00:00
|
|
|
|
|
|
|
mPacket = av_packet_alloc();
|
|
|
|
mVideoFrame = av_frame_alloc();
|
2021-05-29 08:58:51 +00:00
|
|
|
mVideoFrameResampled = av_frame_alloc();
|
2021-05-09 20:52:26 +00:00
|
|
|
mAudioFrame = av_frame_alloc();
|
2021-05-29 08:58:51 +00:00
|
|
|
mAudioFrameResampled = av_frame_alloc();
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
// Resize the video surface, which is needed both for the gamelist view and for
|
|
|
|
// the video screeensaver.
|
2021-05-09 20:52:26 +00:00
|
|
|
resize();
|
|
|
|
|
|
|
|
// Calculate pillarbox/letterbox sizes.
|
|
|
|
calculateBlackRectangle();
|
|
|
|
|
|
|
|
mIsPlaying = true;
|
2021-05-29 08:58:51 +00:00
|
|
|
mFadeIn = 0.0f;
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoFFmpegComponent::stopVideo()
|
|
|
|
{
|
|
|
|
mIsPlaying = false;
|
|
|
|
mIsActuallyPlaying = false;
|
|
|
|
mStartDelayed = false;
|
|
|
|
mPause = false;
|
|
|
|
mEndOfVideo = false;
|
2021-06-22 16:17:06 +00:00
|
|
|
mTexture.reset();
|
2021-05-09 20:52:26 +00:00
|
|
|
|
2021-05-11 15:35:55 +00:00
|
|
|
if (mFrameProcessingThread) {
|
2021-05-29 08:58:51 +00:00
|
|
|
if (mWindow->getVideoPlayerCount() == 0)
|
|
|
|
AudioManager::getInstance()->muteStream();
|
2021-05-11 15:35:55 +00:00
|
|
|
// Wait for the thread execution to complete.
|
|
|
|
mFrameProcessingThread->join();
|
2021-05-12 20:49:24 +00:00
|
|
|
mFrameProcessingThread.reset();
|
2021-05-29 08:58:51 +00:00
|
|
|
mOutputAudio.clear();
|
2021-05-11 15:35:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
// Clear the video and audio frame queues.
|
|
|
|
std::queue<VideoFrame>().swap(mVideoFrameQueue);
|
|
|
|
std::queue<AudioFrame>().swap(mAudioFrameQueue);
|
|
|
|
|
2021-07-14 17:13:25 +00:00
|
|
|
// Clear the audio buffer.
|
|
|
|
AudioManager::getInstance()->clearStream();
|
|
|
|
|
2021-05-09 20:52:26 +00:00
|
|
|
if (mFormatContext) {
|
|
|
|
av_frame_free(&mVideoFrame);
|
2021-05-29 08:58:51 +00:00
|
|
|
av_frame_free(&mVideoFrameResampled);
|
2021-05-09 20:52:26 +00:00
|
|
|
av_frame_free(&mAudioFrame);
|
2021-05-29 08:58:51 +00:00
|
|
|
av_frame_free(&mAudioFrameResampled);
|
2021-05-09 20:52:26 +00:00
|
|
|
av_packet_unref(mPacket);
|
|
|
|
av_packet_free(&mPacket);
|
2021-07-14 17:13:25 +00:00
|
|
|
av_buffer_unref(&mHwContext);
|
2021-05-09 20:52:26 +00:00
|
|
|
avcodec_free_context(&mVideoCodecContext);
|
|
|
|
avcodec_free_context(&mAudioCodecContext);
|
|
|
|
avformat_close_input(&mFormatContext);
|
|
|
|
avformat_free_context(mFormatContext);
|
|
|
|
mVideoCodecContext = nullptr;
|
|
|
|
mAudioCodecContext = nullptr;
|
|
|
|
mFormatContext = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoFFmpegComponent::pauseVideo()
|
|
|
|
{
|
2021-05-29 08:58:51 +00:00
|
|
|
if (mPause && mWindow->getVideoPlayerCount() == 0)
|
|
|
|
AudioManager::getInstance()->muteStream();
|
2021-05-09 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoFFmpegComponent::handleLooping()
|
|
|
|
{
|
|
|
|
if (mIsPlaying && mEndOfVideo) {
|
|
|
|
// If the screensaver video swap time is set to 0, it means we should
|
|
|
|
// skip to the next game when the video has finished playing.
|
|
|
|
if (mScreensaverMode &&
|
2021-07-07 18:31:46 +00:00
|
|
|
Settings::getInstance()->getInt("ScreensaverSwapVideoTimeout") == 0) {
|
2021-05-09 20:52:26 +00:00
|
|
|
mWindow->screensaverTriggerNextGame();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stopVideo();
|
|
|
|
startVideo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|