11 #include <QMutexLocker>
15 #include "libmythbase/mythconfig.h"
22 #include "libavcodec/avcodec.h"
23 #include "libavutil/imgutils.h"
24 #include "libavformat/avformat.h"
31 case FMT_YV12:
return AV_PIX_FMT_YUV420P;
37 case FMT_NV12:
return AV_PIX_FMT_NV12;
38 case FMT_P010:
return AV_PIX_FMT_P010;
39 case FMT_P016:
return AV_PIX_FMT_P016;
53 case FMT_BGRA:
return AV_PIX_FMT_BGRA;
57 case FMT_YUY2:
return AV_PIX_FMT_UYVY422;
59 case FMT_VTB:
return AV_PIX_FMT_VIDEOTOOLBOX;
63 case FMT_DXVA2:
return AV_PIX_FMT_DXVA2_VLD;
64 case FMT_MMAL:
return AV_PIX_FMT_MMAL;
68 return AV_PIX_FMT_NONE;
75 case AV_PIX_FMT_YUVJ420P:
76 case AV_PIX_FMT_YUV420P:
return FMT_YV12;
82 case AV_PIX_FMT_NV12:
return FMT_NV12;
83 case AV_PIX_FMT_P010:
return FMT_P010;
84 case AV_PIX_FMT_P016:
return FMT_P016;
85 case AV_PIX_FMT_YUVJ422P:
92 case AV_PIX_FMT_YUVJ444P:
99 case AV_PIX_FMT_UYVY422:
return FMT_YUY2;
103 case AV_PIX_FMT_BGRA:
return FMT_BGRA;
105 case AV_PIX_FMT_MMAL:
return FMT_MMAL;
107 case AV_PIX_FMT_VIDEOTOOLBOX:
return FMT_VTB;
109 case AV_PIX_FMT_DXVA2_VLD:
return FMT_DXVA2;
122 case AVCOL_TRC_BT2020_10:
150 size_t b0 = tf->buf[0]->size;
151 size_t b1 = tf->buf[1]->size;
152 size_t b2 = tf->buf[2]->size;
155 size_t b0a = ((b0 + 15) / 16) * 16;
156 size_t b1a = ((b1 + 15) / 16) * 16;
157 size_t b2a = ((b2 + 15) / 16) * 16;
160 uint8_t *tbuf{
new uint8_t[b0a + b1a + b2a]{} };
163 tf->data[1] = tbuf + b0a;
164 tf->data[2] = tbuf + b0a + b1a;
165 memcpy(tf->data[0], tf->buf[0]->data, b0);
166 memcpy(tf->data[1], tf->buf[1]->data, b1);
167 memcpy(tf->data[2], tf->buf[2]->data, b2);
172 tf->width, tf->height);
174 mythframe.
m_offsets[1] = tf->data[1] - tf->data[0];
175 mythframe.
m_offsets[2] = tf->data[2] - tf->data[0];
176 mythframe.
m_pitches[0] = tf->linesize[0];
177 mythframe.
m_pitches[1] = tf->linesize[1];
178 mythframe.
m_pitches[2] = tf->linesize[2];
187 memcpy(
Frame->data[0], tf->data[0], b0);
188 memcpy(
Frame->data[1], tf->data[1], b1);
189 memcpy(
Frame->data[2], tf->data[2], b2);
200 if (Fmt == AV_PIX_FMT_NONE)
226 m_size = av_image_get_buffer_size(Fmt, Width, Height, IMAGE_ALIGN);
234 int Width,
int Height)
236 int newwidth = Width;
237 #ifdef Q_PROCESSOR_ARM
248 if (FromFmt == AV_PIX_FMT_YUV420P && ToFmt == AV_PIX_FMT_BGRA)
249 newwidth = Width - 1;
252 newwidth, Height, ToFmt, SWS_FAST_BILINEAR,
253 nullptr,
nullptr,
nullptr);
256 sws_scale(
m_swsctx, From->data, From->linesize, 0, Height, To->data, To->linesize);
257 return SizeData(Width, Height, ToFmt);
267 unsigned char*
Buffer, AVPixelFormat Fmt)
274 av_image_fill_arrays(To->data, To->linesize,
Buffer, Fmt, From->
m_width, From->
m_height, IMAGE_ALIGN);
288 const AVCodec* Codec,
292 AVCodecContext* avctx =
m_streamMap.value(Stream,
nullptr);
295 if (Stream ==
nullptr || Stream->codecpar ==
nullptr)
305 Codec = avcodec_find_decoder(Stream->codecpar->codec_id);
309 LOG(VB_GENERAL, LOG_WARNING, QString(
"avcodec_find_decoder fail for %1")
310 .arg(Stream->codecpar->codec_id));
314 avctx = avcodec_alloc_context3(Codec);
315 if (avcodec_parameters_to_context(avctx, Stream->codecpar) < 0)
316 avcodec_free_context(&avctx);
320 avctx->pkt_timebase = Stream->time_base;
342 avcodec_flush_buffers(avctx);
343 avcodec_free_context(&avctx);
350 QMap<const AVStream*, AVCodecContext*>::iterator i =
m_streamMap.begin();
353 const AVStream *stream = i.key();
361 const int probeBufferSize = 8 * 1024;
362 const AVInputFormat *fmt =
nullptr;
364 memset(&probe, 0,
sizeof(AVProbeData));
366 probe.buf =
new unsigned char[probeBufferSize + AVPROBE_PADDING_SIZE];
367 probe.buf_size = probeBufferSize;
368 memset(probe.buf, 0, probeBufferSize + AVPROBE_PADDING_SIZE);
369 av_log_set_level(AV_LOG_FATAL);
373 if (
m_errorCode == 0 && !infile.open(QIODevice::ReadOnly))
376 int64_t leng = infile.read(
reinterpret_cast<char*
>(probe.buf), probeBufferSize);
377 probe.buf_size =
static_cast<int>(leng);
379 fmt = av_probe_input_format(&probe,
static_cast<int>(
true));
383 AVFormatContext *ctx =
nullptr;
386 ctx = avformat_alloc_context();
390 m_errorCode = avformat_find_stream_info(ctx,
nullptr);
394 for (
uint ix = 0; ix < ctx->nb_streams; ix++)
396 AVStream *stream = ctx->streams[ix];
397 if (stream ==
nullptr)
399 AVCodecParameters *codecpar = stream->codecpar;
400 const AVCodecDescriptor* desc =
nullptr;
402 info.m_codecType =
' ';
403 if (codecpar !=
nullptr)
405 desc = avcodec_descriptor_get(codecpar->codec_id);
406 switch (codecpar->codec_type)
408 case AVMEDIA_TYPE_VIDEO:
409 info.m_codecType =
'V';
411 case AVMEDIA_TYPE_AUDIO:
412 info.m_codecType =
'A';
414 case AVMEDIA_TYPE_SUBTITLE:
415 info.m_codecType =
'S';
422 info.m_codecName = desc->name;
423 info.m_duration = stream->duration * stream->time_base.num / stream->time_base.den;
424 if (
info.m_codecType ==
'V')
426 if (codecpar !=
nullptr)
428 info.m_width = codecpar->width;
429 info.m_height = codecpar->height;
430 info.m_SampleAspectRatio =
static_cast<float>(codecpar->sample_aspect_ratio.num)
431 /
static_cast<float>(codecpar->sample_aspect_ratio.den);
432 switch (codecpar->field_order)
434 case AV_FIELD_PROGRESSIVE:
435 info.m_fieldOrder =
"PR";
438 info.m_fieldOrder =
"TT";
441 info.m_fieldOrder =
"BB";
444 info.m_fieldOrder =
"TB";
447 info.m_fieldOrder =
"BT";
453 info.m_frameRate =
static_cast<float>(stream->r_frame_rate.num)
454 /
static_cast<float>(stream->r_frame_rate.den);
455 info.m_avgFrameRate =
static_cast<float>(stream->avg_frame_rate.num)
456 /
static_cast<float>(stream->avg_frame_rate.den);
458 if (
info.m_codecType ==
'A')
459 info.m_channels = codecpar->ch_layout.nb_channels;
470 m_errorMsg =
"av_probe_input_format returned no result";
482 LOG(VB_GENERAL, LOG_ERR,
483 QString(
"MythStreamInfoList failed for %1. Error code:%2 Message:%3")
490 avformat_close_input(&ctx);
491 avformat_free_context(ctx);