2018-02-22 22:52:49 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2018 F4HKW //
|
|
|
|
// for F4EXB / SDRAngel //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation as version 3 of the License, or //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "datvideorender.h"
|
|
|
|
|
|
|
|
DATVideoRender::DATVideoRender(QWidget * parent):
|
2018-03-11 16:39:02 +01:00
|
|
|
TVScreen(true, parent)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
installEventFilter(this);
|
2019-03-19 23:12:54 +01:00
|
|
|
m_blnIsFullScreen = false;
|
|
|
|
m_blnRunning = false;
|
|
|
|
|
|
|
|
m_blnIsFFMPEGInitialized = false;
|
|
|
|
m_blnIsOpen = false;
|
2019-03-20 00:07:05 +01:00
|
|
|
m_formatCtx = nullptr;
|
|
|
|
m_videoDecoderCtx = nullptr;
|
2019-03-19 23:12:54 +01:00
|
|
|
m_objSwsCtx = nullptr;
|
|
|
|
m_audioBuffer.resize(1<<14);
|
|
|
|
m_audioBufferFill = 0;
|
|
|
|
m_audioFifo = nullptr;
|
2019-03-20 00:07:05 +01:00
|
|
|
m_videoStreamIndex = -1;
|
2019-03-19 23:12:54 +01:00
|
|
|
m_audioStreamIndex = -1;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
|
|
|
m_intCurrentRenderWidth=-1;
|
|
|
|
m_intCurrentRenderHeight=-1;
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
m_frame=nullptr;
|
|
|
|
m_frameCount=-1;
|
2018-02-22 22:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DATVideoRender::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::MouseButtonRelease)
|
|
|
|
{
|
|
|
|
SetFullScreen(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// standard event processing
|
|
|
|
return QObject::eventFilter(obj, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DATVideoRender::SetFullScreen(bool blnFullScreen)
|
|
|
|
{
|
2019-03-17 01:36:44 +01:00
|
|
|
if (m_blnIsFullScreen == blnFullScreen) {
|
2018-02-22 22:52:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (blnFullScreen == true)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
setWindowFlags(Qt::Window);
|
|
|
|
setWindowState(Qt::WindowFullScreen);
|
|
|
|
show();
|
|
|
|
m_blnIsFullScreen=true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setWindowFlags(Qt::Widget);
|
|
|
|
setWindowState(Qt::WindowNoState);
|
|
|
|
show();
|
|
|
|
m_blnIsFullScreen=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ReadFunction(void *opaque, uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
QIODevice* objStream = reinterpret_cast<QIODevice*>(opaque);
|
|
|
|
int intNbBytes = objStream->read((char*)buf, buf_size);
|
|
|
|
return intNbBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t SeekFunction(void* opaque, int64_t offset, int whence)
|
|
|
|
{
|
|
|
|
QIODevice* objStream = reinterpret_cast<QIODevice*>(opaque);
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (whence == AVSEEK_SIZE) {
|
2018-02-22 22:52:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (objStream->isSequential()) {
|
2018-02-22 22:52:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (objStream->seek(offset) == false) {
|
2018-02-22 22:52:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return objStream->pos();
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:04:45 +01:00
|
|
|
void DATVideoRender::ResetMetaData()
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
MetaData.CodecID=-1;
|
|
|
|
MetaData.PID=-1;
|
|
|
|
MetaData.Program="";
|
|
|
|
MetaData.Stream="";
|
|
|
|
MetaData.Width=-1;
|
|
|
|
MetaData.Height=-1;
|
|
|
|
MetaData.BitRate=-1;
|
|
|
|
MetaData.Channels=-1;
|
|
|
|
MetaData.CodecDescription= "";
|
|
|
|
|
2018-02-26 01:04:45 +01:00
|
|
|
MetaData.OK_Decoding=false;
|
|
|
|
MetaData.OK_TransportStream=false;
|
|
|
|
MetaData.OK_VideoStream=false;
|
|
|
|
|
|
|
|
emit onMetaDataChanged(&MetaData);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DATVideoRender::InitializeFFMPEG()
|
|
|
|
{
|
|
|
|
ResetMetaData();
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (m_blnIsFFMPEGInitialized) {
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
avcodec_register_all();
|
|
|
|
av_register_all();
|
|
|
|
av_log_set_level(AV_LOG_FATAL);
|
|
|
|
//av_log_set_level(AV_LOG_ERROR);
|
|
|
|
|
|
|
|
m_blnIsFFMPEGInitialized=true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DATVideoRender::PreprocessStream()
|
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
AVDictionary *opts = nullptr;
|
|
|
|
AVCodec *videoCodec = nullptr;
|
|
|
|
AVCodec *audioCodec = nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
|
|
|
int intRet=-1;
|
2019-03-20 00:07:05 +01:00
|
|
|
char *buffer=nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
|
|
|
//Identify stream
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (avformat_find_stream_info(m_formatCtx, nullptr) < 0)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
avformat_close_input(&m_formatCtx);
|
|
|
|
m_formatCtx = nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
qDebug() << "DATVideoProcess::PreprocessStream cannot find stream info";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Find video stream
|
2019-03-20 00:07:05 +01:00
|
|
|
intRet = av_find_best_stream(m_formatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
|
2018-02-22 22:52:49 +01:00
|
|
|
|
|
|
|
if (intRet < 0)
|
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
avformat_close_input(&m_formatCtx);
|
2018-02-22 22:52:49 +01:00
|
|
|
qDebug() << "DATVideoProcess::PreprocessStream cannot find video stream";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
m_videoStreamIndex = intRet;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-19 23:12:54 +01:00
|
|
|
//Find audio stream
|
2019-03-20 00:07:05 +01:00
|
|
|
intRet = av_find_best_stream(m_formatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
|
2019-03-19 23:12:54 +01:00
|
|
|
|
|
|
|
if (intRet < 0) {
|
|
|
|
qDebug() << "DATVideoProcess::PreprocessStream cannot find audio stream";
|
|
|
|
}
|
|
|
|
|
|
|
|
m_audioStreamIndex = intRet;
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
// Prepare Video Codec and extract meta data
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2018-06-13 00:20:51 +02:00
|
|
|
// FIXME: codec is depreecated but replacement fails
|
2019-03-20 00:07:05 +01:00
|
|
|
// AVCodecParameters *parms = m_formatCtx->streams[m_videoStreamIndex]->codecpar;
|
|
|
|
// m_videoDecoderCtx = new AVCodecContext();
|
|
|
|
// avcodec_parameters_to_context(m_videoDecoderCtx, parms);
|
|
|
|
m_videoDecoderCtx = m_formatCtx->streams[m_videoStreamIndex]->codec;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
|
|
|
//Meta Data
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
MetaData.PID = m_formatCtx->streams[m_videoStreamIndex]->id;
|
|
|
|
MetaData.CodecID = m_videoDecoderCtx->codec_id;
|
2018-02-26 01:04:45 +01:00
|
|
|
MetaData.OK_TransportStream = true;
|
2019-03-20 00:07:05 +01:00
|
|
|
MetaData.Program = "";
|
|
|
|
MetaData.Stream = "";
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (m_formatCtx->programs)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
buffer = nullptr;
|
|
|
|
av_dict_get_string(m_formatCtx->programs[m_videoStreamIndex]->metadata, &buffer, ':', '\n');
|
2019-03-17 01:36:44 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (buffer != nullptr) {
|
|
|
|
MetaData.Program = QString("%1").arg(buffer);
|
2018-02-22 22:52:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
buffer = nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
av_dict_get_string(m_formatCtx->streams[m_videoStreamIndex]->metadata, &buffer, ':', '\n');
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (buffer != nullptr) {
|
|
|
|
MetaData.Stream = QString("%1").arg(buffer);
|
2018-02-22 22:52:49 +01:00
|
|
|
}
|
|
|
|
|
2018-02-26 01:04:45 +01:00
|
|
|
emit onMetaDataChanged(&MetaData);
|
|
|
|
|
2018-02-22 22:52:49 +01:00
|
|
|
//Decoder
|
2019-03-20 00:07:05 +01:00
|
|
|
videoCodec = avcodec_find_decoder(m_videoDecoderCtx->codec_id);
|
2019-03-17 01:36:44 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (videoCodec == nullptr)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
avformat_close_input(&m_formatCtx);
|
|
|
|
m_formatCtx = nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
qDebug() << "DATVideoProcess::PreprocessStream cannot find associated video CODEC";
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
av_dict_set(&opts, "refcounted_frames", "1", 0);
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (avcodec_open2(m_videoDecoderCtx, videoCodec, &opts) < 0)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
avformat_close_input(&m_formatCtx);
|
|
|
|
m_formatCtx=nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
qDebug() << "DATVideoProcess::PreprocessStream cannot open associated video CODEC";
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Allocate Frame
|
2019-03-20 00:07:05 +01:00
|
|
|
m_frame = av_frame_alloc();
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (!m_frame)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
avformat_close_input(&m_formatCtx);
|
|
|
|
m_formatCtx=nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
|
|
|
qDebug() << "DATVideoProcess::PreprocessStream cannot allocate frame";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
m_frameCount = 0;
|
|
|
|
MetaData.Width = m_videoDecoderCtx->width;
|
|
|
|
MetaData.Height = m_videoDecoderCtx->height;
|
|
|
|
MetaData.BitRate = m_videoDecoderCtx->bit_rate;
|
|
|
|
MetaData.Channels = m_videoDecoderCtx->channels;
|
|
|
|
MetaData.CodecDescription = QString("%1").arg(videoCodec->long_name);
|
2018-02-26 01:04:45 +01:00
|
|
|
MetaData.OK_VideoStream = true;
|
|
|
|
|
|
|
|
emit onMetaDataChanged(&MetaData);
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
// Prepare Audio Codec
|
|
|
|
|
|
|
|
if (m_audioStreamIndex >= 0)
|
|
|
|
{
|
|
|
|
m_audioDecoderCtx = m_formatCtx->streams[m_audioStreamIndex]->codec;
|
|
|
|
|
|
|
|
audioCodec = avcodec_find_decoder(m_audioDecoderCtx->codec_id);
|
|
|
|
|
|
|
|
if (audioCodec == nullptr)
|
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::PreprocessStream cannot find associated audio CODEC";
|
|
|
|
m_audioStreamIndex = -1; // invalidate audio
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (avcodec_open2(m_audioDecoderCtx, audioCodec, nullptr) < 0)
|
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::PreprocessStream cannot open associated audio CODEC";
|
|
|
|
m_audioStreamIndex = -1; // invalidate audio
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 22:52:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DATVideoRender::OpenStream(DATVideostream *objDevice)
|
|
|
|
{
|
|
|
|
int intIOBufferSize = 32768;
|
2019-03-17 01:36:44 +01:00
|
|
|
unsigned char * ptrIOBuffer = nullptr;
|
|
|
|
AVIOContext * objIOCtx = nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if(m_blnRunning) {
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (objDevice == nullptr)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-17 01:36:44 +01:00
|
|
|
qDebug() << "DATVideoProcess::OpenStream QIODevice is nullptr";
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (m_blnIsOpen)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::OpenStream already open";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (objDevice->bytesAvailable() <= 0)
|
2018-02-26 01:04:45 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::OpenStream no data available";
|
2019-03-17 01:36:44 +01:00
|
|
|
MetaData.OK_Data = false;
|
2018-02-26 01:04:45 +01:00
|
|
|
emit onMetaDataChanged(&MetaData);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Only once execution
|
2019-03-17 01:36:44 +01:00
|
|
|
m_blnRunning = true;
|
2018-02-26 01:04:45 +01:00
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
MetaData.OK_Data = true;
|
2018-02-26 01:04:45 +01:00
|
|
|
emit onMetaDataChanged(&MetaData);
|
|
|
|
|
2018-02-22 22:52:49 +01:00
|
|
|
InitializeFFMPEG();
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (!m_blnIsFFMPEGInitialized)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::OpenStream FFMPEG not initialized";
|
2019-03-17 01:36:44 +01:00
|
|
|
m_blnRunning = false;
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (!objDevice->open(QIODevice::ReadOnly))
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::OpenStream cannot open QIODevice";
|
2019-03-17 01:36:44 +01:00
|
|
|
m_blnRunning = false;
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Connect QIODevice to FFMPEG Reader
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
m_formatCtx = avformat_alloc_context();
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (m_formatCtx == nullptr)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::OpenStream cannot alloc format FFMPEG context";
|
2019-03-17 01:36:44 +01:00
|
|
|
m_blnRunning = false;
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-13 00:20:51 +02:00
|
|
|
ptrIOBuffer = (unsigned char *)av_malloc(intIOBufferSize+ AV_INPUT_BUFFER_PADDING_SIZE);
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
objIOCtx = avio_alloc_context(ptrIOBuffer,
|
|
|
|
intIOBufferSize,
|
|
|
|
0,
|
|
|
|
reinterpret_cast<void *>(objDevice),
|
|
|
|
&ReadFunction,
|
|
|
|
nullptr,
|
|
|
|
&SeekFunction);
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
m_formatCtx->pb = objIOCtx;
|
|
|
|
m_formatCtx->flags |= AVFMT_FLAG_CUSTOM_IO;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (avformat_open_input(&m_formatCtx, nullptr , nullptr, nullptr) < 0)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::OpenStream cannot open stream";
|
2018-02-26 01:04:45 +01:00
|
|
|
m_blnRunning=false;
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (!PreprocessStream())
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2018-02-26 01:04:45 +01:00
|
|
|
m_blnRunning=false;
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_blnIsOpen=true;
|
|
|
|
m_blnRunning=false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DATVideoRender::RenderStream()
|
|
|
|
{
|
|
|
|
AVPacket objPacket;
|
|
|
|
int intGotFrame;
|
|
|
|
bool blnNeedRenderingSetup;
|
|
|
|
|
2018-02-26 01:04:45 +01:00
|
|
|
if(!m_blnIsOpen)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2018-02-26 01:04:45 +01:00
|
|
|
qDebug() << "DATVideoProcess::RenderStream Stream not open";
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if(m_blnRunning) {
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Only once execution
|
|
|
|
m_blnRunning=true;
|
|
|
|
|
|
|
|
//********** Rendering **********
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (av_read_frame(m_formatCtx, &objPacket) < 0)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::RenderStream reading packet error";
|
|
|
|
m_blnRunning=false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Video channel
|
2019-03-20 00:07:05 +01:00
|
|
|
if (objPacket.stream_index == m_videoStreamIndex)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
memset(m_frame, 0, sizeof(AVFrame));
|
|
|
|
av_frame_unref(m_frame);
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
intGotFrame = 0;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (new_decode(m_videoDecoderCtx, m_frame, &intGotFrame, &objPacket) < 0)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
qDebug() << "DATVideoProcess::RenderStream decoding video packet error";
|
|
|
|
m_blnRunning = false;
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (intGotFrame)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
//Rendering and RGB Converter setup
|
2019-03-20 00:07:05 +01:00
|
|
|
blnNeedRenderingSetup=(m_frameCount==0);
|
2019-03-17 01:36:44 +01:00
|
|
|
blnNeedRenderingSetup|=(m_objSwsCtx==nullptr);
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if ((m_intCurrentRenderWidth!=m_frame->width) || (m_intCurrentRenderHeight!=m_frame->height)) {
|
2018-02-22 22:52:49 +01:00
|
|
|
blnNeedRenderingSetup=true;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (blnNeedRenderingSetup)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-17 01:36:44 +01:00
|
|
|
if (m_objSwsCtx != nullptr)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
sws_freeContext(m_objSwsCtx);
|
2019-03-17 01:36:44 +01:00
|
|
|
m_objSwsCtx=nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Convertisseur YUV -> RGB
|
|
|
|
m_objSwsCtx = sws_alloc_context();
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
av_opt_set_int(m_objSwsCtx,"srcw",m_frame->width,0);
|
|
|
|
av_opt_set_int(m_objSwsCtx,"srch",m_frame->height,0);
|
|
|
|
av_opt_set_int(m_objSwsCtx,"src_format",m_frame->format,0);
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
av_opt_set_int(m_objSwsCtx,"dstw",m_frame->width,0);
|
|
|
|
av_opt_set_int(m_objSwsCtx,"dsth",m_frame->height,0);
|
2018-02-22 22:52:49 +01:00
|
|
|
av_opt_set_int(m_objSwsCtx,"dst_format",AV_PIX_FMT_RGB24 ,0);
|
|
|
|
|
2018-02-26 01:04:45 +01:00
|
|
|
av_opt_set_int(m_objSwsCtx,"sws_flag", SWS_FAST_BILINEAR /* SWS_BICUBIC*/,0);
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (sws_init_context(m_objSwsCtx, nullptr, nullptr) < 0)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::RenderStream cannont init video data converter";
|
2019-03-17 01:36:44 +01:00
|
|
|
m_objSwsCtx=nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
m_blnRunning=false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if ((m_intCurrentRenderHeight>0) && (m_intCurrentRenderWidth>0))
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
//av_freep(&m_pbytDecodedData[0]);
|
|
|
|
//av_freep(&m_pintDecodedLineSize[0]);
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (av_image_alloc(m_pbytDecodedData, m_pintDecodedLineSize,m_frame->width, m_frame->height, AV_PIX_FMT_RGB24, 1)<0)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::RenderStream cannont init video image buffer";
|
|
|
|
sws_freeContext(m_objSwsCtx);
|
2019-03-17 01:36:44 +01:00
|
|
|
m_objSwsCtx=nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
m_blnRunning=false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Rendering device setup
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
resizeTVScreen(m_frame->width,m_frame->height);
|
2019-03-17 01:36:44 +01:00
|
|
|
update();
|
|
|
|
resetImage();
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
m_intCurrentRenderWidth=m_frame->width;
|
|
|
|
m_intCurrentRenderHeight=m_frame->height;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
MetaData.Width = m_frame->width;
|
|
|
|
MetaData.Height = m_frame->height;
|
2019-03-17 01:36:44 +01:00
|
|
|
MetaData.OK_Decoding = true;
|
|
|
|
emit onMetaDataChanged(&MetaData);
|
2018-02-22 22:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Frame rendering
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (sws_scale(m_objSwsCtx, m_frame->data, m_frame->linesize, 0, m_frame->height, m_pbytDecodedData, m_pintDecodedLineSize)<0)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::RenderStream error converting video frame to RGB";
|
|
|
|
m_blnRunning=false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderImage(m_pbytDecodedData[0]);
|
2019-03-20 00:07:05 +01:00
|
|
|
av_frame_unref(m_frame);
|
|
|
|
m_frameCount ++;
|
2018-02-22 22:52:49 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-20 00:07:05 +01:00
|
|
|
// Audio channel
|
2019-03-19 23:12:54 +01:00
|
|
|
else if (objPacket.stream_index == m_audioStreamIndex)
|
|
|
|
{
|
|
|
|
}
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2018-06-13 00:20:51 +02:00
|
|
|
av_packet_unref(&objPacket);
|
2018-02-22 22:52:49 +01:00
|
|
|
|
|
|
|
//********** Rendering **********
|
|
|
|
|
|
|
|
m_blnRunning=false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DATVideoRender::CloseStream(QIODevice *objDevice)
|
|
|
|
{
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (m_blnRunning) {
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (!objDevice)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-17 01:36:44 +01:00
|
|
|
qDebug() << "DATVideoProcess::CloseStream QIODevice is nullptr";
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (!m_blnIsOpen)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2018-02-26 01:04:45 +01:00
|
|
|
qDebug() << "DATVideoProcess::CloseStream Stream not open";
|
2018-02-22 22:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (!m_formatCtx)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
qDebug() << "DATVideoProcess::CloseStream FFMEG Context is not initialized";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:04:45 +01:00
|
|
|
//Only once execution
|
|
|
|
m_blnRunning=true;
|
|
|
|
|
2018-03-04 02:35:15 +01:00
|
|
|
// maybe done in the avcodec_close
|
2019-03-20 00:07:05 +01:00
|
|
|
// avformat_close_input(&m_formatCtx);
|
|
|
|
// m_formatCtx=nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (m_videoDecoderCtx)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
avcodec_close(m_videoDecoderCtx);
|
|
|
|
m_videoDecoderCtx = nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 00:07:05 +01:00
|
|
|
if (m_frame)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
2019-03-20 00:07:05 +01:00
|
|
|
av_frame_unref(m_frame);
|
|
|
|
av_frame_free(&m_frame);
|
2018-02-22 22:52:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (m_objSwsCtx != nullptr)
|
2018-02-22 22:52:49 +01:00
|
|
|
{
|
|
|
|
sws_freeContext(m_objSwsCtx);
|
2019-03-17 01:36:44 +01:00
|
|
|
m_objSwsCtx = nullptr;
|
2018-02-22 22:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
objDevice->close();
|
|
|
|
m_blnIsOpen=false;
|
|
|
|
m_blnRunning=false;
|
|
|
|
m_intCurrentRenderWidth=-1;
|
|
|
|
m_intCurrentRenderHeight=-1;
|
|
|
|
|
2018-02-26 01:04:45 +01:00
|
|
|
ResetMetaData();
|
|
|
|
emit onMetaDataChanged(&MetaData);
|
|
|
|
|
2018-02-22 22:52:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-13 00:20:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replacement of deprecated avcodec_decode_video2 with the same signature
|
|
|
|
* https://blogs.gentoo.org/lu_zero/2016/03/29/new-avcodec-api/
|
|
|
|
*/
|
|
|
|
int DATVideoRender::new_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*got_frame = 0;
|
|
|
|
|
2019-03-17 01:36:44 +01:00
|
|
|
if (pkt)
|
|
|
|
{
|
2018-06-13 00:20:51 +02:00
|
|
|
ret = avcodec_send_packet(avctx, pkt);
|
|
|
|
// In particular, we don't expect AVERROR(EAGAIN), because we read all
|
|
|
|
// decoded frames with avcodec_receive_frame() until done.
|
2019-03-17 01:36:44 +01:00
|
|
|
if (ret < 0) {
|
2018-06-13 00:20:51 +02:00
|
|
|
return ret == AVERROR_EOF ? 0 : ret;
|
2019-03-17 01:36:44 +01:00
|
|
|
}
|
2018-06-13 00:20:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = avcodec_receive_frame(avctx, frame);
|
2019-03-17 01:36:44 +01:00
|
|
|
|
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
|
2018-06-13 00:20:51 +02:00
|
|
|
return ret;
|
2019-03-17 01:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= 0) {
|
2018-06-13 00:20:51 +02:00
|
|
|
*got_frame = 1;
|
2019-03-17 01:36:44 +01:00
|
|
|
}
|
2018-06-13 00:20:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|