11#include <QMutexLocker>
13#if QT_VERSION >= QT_VERSION_CHECK(6,5,0)
14#include <QtProcessorDetection>
19#include "libmythbase/mythconfig.h"
26#include "libavcodec/avcodec.h"
27#include "libavutil/imgutils.h"
28#include "libavformat/avformat.h"
35 case FMT_YV12:
return AV_PIX_FMT_YUV420P;
41 case FMT_NV12:
return AV_PIX_FMT_NV12;
42 case FMT_P010:
return AV_PIX_FMT_P010;
43 case FMT_P016:
return AV_PIX_FMT_P016;
57 case FMT_BGRA:
return AV_PIX_FMT_BGRA;
61 case FMT_YUY2:
return AV_PIX_FMT_UYVY422;
63 case FMT_VTB:
return AV_PIX_FMT_VIDEOTOOLBOX;
67 case FMT_DXVA2:
return AV_PIX_FMT_DXVA2_VLD;
68 case FMT_MMAL:
return AV_PIX_FMT_MMAL;
72 return AV_PIX_FMT_NONE;
79 case AV_PIX_FMT_YUVJ420P:
80 case AV_PIX_FMT_YUV420P:
return FMT_YV12;
86 case AV_PIX_FMT_NV12:
return FMT_NV12;
87 case AV_PIX_FMT_P010:
return FMT_P010;
88 case AV_PIX_FMT_P016:
return FMT_P016;
89 case AV_PIX_FMT_YUVJ422P:
96 case AV_PIX_FMT_YUVJ444P:
103 case AV_PIX_FMT_UYVY422:
return FMT_YUY2;
107 case AV_PIX_FMT_BGRA:
return FMT_BGRA;
109 case AV_PIX_FMT_MMAL:
return FMT_MMAL;
111 case AV_PIX_FMT_VIDEOTOOLBOX:
return FMT_VTB;
113 case AV_PIX_FMT_DXVA2_VLD:
return FMT_DXVA2;
126 case AVCOL_TRC_BT2020_10:
154 size_t b0 = tf->buf[0]->size;
155 size_t b1 = tf->buf[1]->size;
156 size_t b2 = tf->buf[2]->size;
159 size_t b0a = ((b0 + 15) / 16) * 16;
160 size_t b1a = ((b1 + 15) / 16) * 16;
161 size_t b2a = ((b2 + 15) / 16) * 16;
164 uint8_t *tbuf{
new uint8_t[b0a + b1a + b2a]{} };
167 tf->data[1] = tbuf + b0a;
168 tf->data[2] = tbuf + b0a + b1a;
169 memcpy(tf->data[0], tf->buf[0]->data, b0);
170 memcpy(tf->data[1], tf->buf[1]->data, b1);
171 memcpy(tf->data[2], tf->buf[2]->data, b2);
176 tf->width, tf->height);
178 mythframe.
m_offsets[1] = tf->data[1] - tf->data[0];
179 mythframe.
m_offsets[2] = tf->data[2] - tf->data[0];
180 mythframe.
m_pitches[0] = tf->linesize[0];
181 mythframe.
m_pitches[1] = tf->linesize[1];
182 mythframe.
m_pitches[2] = tf->linesize[2];
191 memcpy(
Frame->data[0], tf->data[0], b0);
192 memcpy(
Frame->data[1], tf->data[1], b1);
193 memcpy(
Frame->data[2], tf->data[2], b2);
204 if (Fmt == AV_PIX_FMT_NONE)
230 m_size = av_image_get_buffer_size(Fmt, Width, Height, IMAGE_ALIGN);
238 int Width,
int Height)
240 int newwidth = Width;
241#ifdef Q_PROCESSOR_ARM
252 if (FromFmt == AV_PIX_FMT_YUV420P && ToFmt == AV_PIX_FMT_BGRA)
253 newwidth = Width - 1;
256 newwidth, Height, ToFmt, SWS_FAST_BILINEAR,
257 nullptr,
nullptr,
nullptr);
260 sws_scale(
m_swsctx, From->data, From->linesize, 0, Height, To->data, To->linesize);
261 return SizeData(Width, Height, ToFmt);
271 unsigned char*
Buffer, AVPixelFormat Fmt)
278 av_image_fill_arrays(To->data, To->linesize,
Buffer, Fmt, From->
m_width, From->
m_height, IMAGE_ALIGN);
292 const AVCodec* Codec,
295 if (Stream ==
nullptr || Stream->codecpar ==
nullptr)
298 AVCodecContext* avctx =
m_streamMap.value(Stream,
nullptr);
299 if (avctx ==
nullptr)
305 else if (Codec ==
nullptr)
307 Codec = avcodec_find_decoder(Stream->codecpar->codec_id);
308 if (Codec ==
nullptr)
310 LOG(VB_GENERAL, LOG_WARNING, QString(
"avcodec_find_decoder fail for %1")
311 .arg(Stream->codecpar->codec_id));
315 avctx = avcodec_alloc_context3(Codec);
316 if (avctx !=
nullptr && avcodec_parameters_to_context(avctx, Stream->codecpar) < 0)
317 avcodec_free_context(&avctx);
319 if (avctx !=
nullptr)
321 avctx->pkt_timebase = Stream->time_base;
343 avcodec_flush_buffers(avctx);
344 avcodec_free_context(&avctx);
351 QMap<const AVStream*, AVCodecContext*>::iterator i =
m_streamMap.begin();
354 const AVStream *stream = i.key();
362 const int probeBufferSize = 8 * 1024;
363 const AVInputFormat *fmt =
nullptr;
365 memset(&probe, 0,
sizeof(AVProbeData));
367 probe.buf =
new unsigned char[probeBufferSize + AVPROBE_PADDING_SIZE];
368 probe.buf_size = probeBufferSize;
369 memset(probe.buf, 0, probeBufferSize + AVPROBE_PADDING_SIZE);
370 av_log_set_level(AV_LOG_FATAL);
374 if (
m_errorCode == 0 && !infile.open(QIODevice::ReadOnly))
377 int64_t leng = infile.read(
reinterpret_cast<char*
>(probe.buf), probeBufferSize);
378 probe.buf_size =
static_cast<int>(leng);
380 fmt = av_probe_input_format(&probe,
static_cast<int>(
true));
384 AVFormatContext *ctx =
nullptr;
387 ctx = avformat_alloc_context();
392 m_errorCode = avformat_find_stream_info(ctx,
nullptr);
396 for (
uint ix = 0; ix < ctx->nb_streams; ix++)
398 AVStream *stream = ctx->streams[ix];
399 if (stream ==
nullptr)
401 AVCodecParameters *codecpar = stream->codecpar;
402 const AVCodecDescriptor* desc =
nullptr;
404 info.m_codecType =
' ';
405 if (codecpar !=
nullptr)
407 desc = avcodec_descriptor_get(codecpar->codec_id);
408 switch (codecpar->codec_type)
410 case AVMEDIA_TYPE_VIDEO:
411 info.m_codecType =
'V';
413 case AVMEDIA_TYPE_AUDIO:
414 info.m_codecType =
'A';
416 case AVMEDIA_TYPE_SUBTITLE:
417 info.m_codecType =
'S';
424 info.m_codecName = desc->name;
425 info.m_duration = stream->duration * stream->time_base.num / stream->time_base.den;
426 if (
info.m_codecType ==
'V')
428 if (codecpar !=
nullptr)
430 info.m_width = codecpar->width;
431 info.m_height = codecpar->height;
432 info.m_SampleAspectRatio =
static_cast<float>(codecpar->sample_aspect_ratio.num)
433 /
static_cast<float>(codecpar->sample_aspect_ratio.den);
434 switch (codecpar->field_order)
436 case AV_FIELD_PROGRESSIVE:
437 info.m_fieldOrder =
"PR";
440 info.m_fieldOrder =
"TT";
443 info.m_fieldOrder =
"BB";
446 info.m_fieldOrder =
"TB";
449 info.m_fieldOrder =
"BT";
455 info.m_frameRate =
static_cast<float>(stream->r_frame_rate.num)
456 /
static_cast<float>(stream->r_frame_rate.den);
457 info.m_avgFrameRate =
static_cast<float>(stream->avg_frame_rate.num)
458 /
static_cast<float>(stream->avg_frame_rate.den);
460 if (
info.m_codecType ==
'A')
461 info.m_channels = codecpar->ch_layout.nb_channels;
472 m_errorMsg =
"av_probe_input_format returned no result";
480 LOG(VB_GENERAL, LOG_ERR,
481 QString(
"MythStreamInfoList failed for %1. Error code:%2 Message:%3")
488 avformat_close_input(&ctx);
489 avformat_free_context(ctx);
int Copy(AVFrame *To, const MythVideoFrame *From, unsigned char *Buffer, AVPixelFormat Fmt=AV_PIX_FMT_YUV420P)
Initialise AVFrame and copy contents of VideoFrame frame into it, performing any required conversion.
int SizeData(int Width, int Height, AVPixelFormat Fmt)
static void DeinterlaceAVFrame(AVFrame *Frame)
Deinterlace an AVFrame.
static VideoFrameType PixelFormatToFrameType(AVPixelFormat Fmt)
static MythHDR::HDRType FFmpegTransferToHDRType(int Transfer)
static int FillAVFrame(AVFrame *Frame, const MythVideoFrame *From, AVPixelFormat Fmt=AV_PIX_FMT_NONE)
Initialise AVFrame with content from MythVideoFrame.
static AVPixelFormat FrameTypeToPixelFormat(VideoFrameType Type)
AVCodecContext * FindCodecContext(const AVStream *Stream)
QRecursiveMutex m_mapLock
AVCodecContext * GetCodecContext(const AVStream *Stream, const AVCodec *Codec=nullptr, bool NullCodec=false)
void FreeCodecContext(const AVStream *Stream)
QMap< const AVStream *, AVCodecContext * > m_streamMap
Handles software based deinterlacing of video frames.
void Filter(MythVideoFrame *Frame, FrameScanType Scan, MythVideoProfile *Profile, bool Force=false)
Deinterlace Frame if needed.
QVector< MythStreamInfo > m_streamInfoList
MythStreamInfoList(const QString &filename)
static size_t GetBufferSize(VideoFrameType Type, int Width, int Height, int Aligned=MYTH_WIDTH_ALIGNMENT)
MythDeintType m_deinterlaceAllowed
MythDeintType m_deinterlaceSingle
static bool YUVFormat(VideoFrameType Type)
MTV_PUBLIC std::string av_make_error_stdstring_unknown(int errnum)
@ FMT_RGB32
endian dependent format, ARGB or BGRA
#define LOG(_MASK_, _LEVEL_, _QSTRING_)