11 #include <QMutexLocker>
16 #include "libmythbase/mythconfig.h"
23 #include "libavcodec/avcodec.h"
24 #include "libavutil/imgutils.h"
25 #include "libavformat/avformat.h"
32 case FMT_YV12:
return AV_PIX_FMT_YUV420P;
38 case FMT_NV12:
return AV_PIX_FMT_NV12;
39 case FMT_P010:
return AV_PIX_FMT_P010;
40 case FMT_P016:
return AV_PIX_FMT_P016;
54 case FMT_BGRA:
return AV_PIX_FMT_BGRA;
58 case FMT_YUY2:
return AV_PIX_FMT_UYVY422;
60 case FMT_VTB:
return AV_PIX_FMT_VIDEOTOOLBOX;
64 case FMT_DXVA2:
return AV_PIX_FMT_DXVA2_VLD;
65 case FMT_MMAL:
return AV_PIX_FMT_MMAL;
69 return AV_PIX_FMT_NONE;
76 case AV_PIX_FMT_YUVJ420P:
77 case AV_PIX_FMT_YUV420P:
return FMT_YV12;
83 case AV_PIX_FMT_NV12:
return FMT_NV12;
84 case AV_PIX_FMT_P010:
return FMT_P010;
85 case AV_PIX_FMT_P016:
return FMT_P016;
86 case AV_PIX_FMT_YUVJ422P:
93 case AV_PIX_FMT_YUVJ444P:
100 case AV_PIX_FMT_UYVY422:
return FMT_YUY2;
104 case AV_PIX_FMT_BGRA:
return FMT_BGRA;
106 case AV_PIX_FMT_MMAL:
return FMT_MMAL;
108 case AV_PIX_FMT_VIDEOTOOLBOX:
return FMT_VTB;
110 case AV_PIX_FMT_DXVA2_VLD:
return FMT_DXVA2;
123 case AVCOL_TRC_BT2020_10:
151 size_t b0 = tf->buf[0]->size;
152 size_t b1 = tf->buf[1]->size;
153 size_t b2 = tf->buf[2]->size;
156 size_t b0a = ((b0 + 15) / 16) * 16;
157 size_t b1a = ((b1 + 15) / 16) * 16;
158 size_t b2a = ((b2 + 15) / 16) * 16;
161 uint8_t *tbuf{
new uint8_t[b0a + b1a + b2a]{} };
164 tf->data[1] = tbuf + b0a;
165 tf->data[2] = tbuf + b0a + b1a;
166 memcpy(tf->data[0], tf->buf[0]->data, b0);
167 memcpy(tf->data[1], tf->buf[1]->data, b1);
168 memcpy(tf->data[2], tf->buf[2]->data, b2);
173 tf->width, tf->height);
175 mythframe.
m_offsets[1] = tf->data[1] - tf->data[0];
176 mythframe.
m_offsets[2] = tf->data[2] - tf->data[0];
177 mythframe.
m_pitches[0] = tf->linesize[0];
178 mythframe.
m_pitches[1] = tf->linesize[1];
179 mythframe.
m_pitches[2] = tf->linesize[2];
188 memcpy(
Frame->data[0], tf->data[0], b0);
189 memcpy(
Frame->data[1], tf->data[1], b1);
190 memcpy(
Frame->data[2], tf->data[2], b2);
201 if (Fmt == AV_PIX_FMT_NONE)
227 m_size = av_image_get_buffer_size(Fmt, Width, Height, IMAGE_ALIGN);
235 int Width,
int Height)
237 int newwidth = Width;
238 #ifdef Q_PROCESSOR_ARM
249 if (FromFmt == AV_PIX_FMT_YUV420P && ToFmt == AV_PIX_FMT_BGRA)
250 newwidth = Width - 1;
253 newwidth, Height, ToFmt, SWS_FAST_BILINEAR,
254 nullptr,
nullptr,
nullptr);
257 sws_scale(
m_swsctx, From->data, From->linesize, 0, Height, To->data, To->linesize);
258 return SizeData(Width, Height, ToFmt);
268 unsigned char*
Buffer, AVPixelFormat Fmt)
275 av_image_fill_arrays(To->data, To->linesize,
Buffer, Fmt, From->
m_width, From->
m_height, IMAGE_ALIGN);
289 const AVCodec* Codec,
292 if (Stream ==
nullptr || Stream->codecpar ==
nullptr)
295 AVCodecContext* avctx =
m_streamMap.value(Stream,
nullptr);
296 if (avctx ==
nullptr)
302 else if (Codec ==
nullptr)
304 Codec = avcodec_find_decoder(Stream->codecpar->codec_id);
305 if (Codec ==
nullptr)
307 LOG(VB_GENERAL, LOG_WARNING, QString(
"avcodec_find_decoder fail for %1")
308 .arg(Stream->codecpar->codec_id));
312 avctx = avcodec_alloc_context3(Codec);
313 if (avctx !=
nullptr && avcodec_parameters_to_context(avctx, Stream->codecpar) < 0)
314 avcodec_free_context(&avctx);
316 if (avctx !=
nullptr)
318 avctx->pkt_timebase = Stream->time_base;
340 avcodec_flush_buffers(avctx);
341 avcodec_free_context(&avctx);
348 QMap<const AVStream*, AVCodecContext*>::iterator i =
m_streamMap.begin();
351 const AVStream *stream = i.key();
359 const int probeBufferSize = 8 * 1024;
360 const AVInputFormat *fmt =
nullptr;
362 memset(&probe, 0,
sizeof(AVProbeData));
364 probe.buf =
new unsigned char[probeBufferSize + AVPROBE_PADDING_SIZE];
365 probe.buf_size = probeBufferSize;
366 memset(probe.buf, 0, probeBufferSize + AVPROBE_PADDING_SIZE);
367 av_log_set_level(AV_LOG_FATAL);
371 if (
m_errorCode == 0 && !infile.open(QIODevice::ReadOnly))
374 int64_t leng = infile.read(
reinterpret_cast<char*
>(probe.buf), probeBufferSize);
375 probe.buf_size =
static_cast<int>(leng);
377 fmt = av_probe_input_format(&probe,
static_cast<int>(
true));
381 AVFormatContext *ctx =
nullptr;
384 ctx = avformat_alloc_context();
388 m_errorCode = avformat_find_stream_info(ctx,
nullptr);
392 for (
uint ix = 0; ix < ctx->nb_streams; ix++)
394 AVStream *stream = ctx->streams[ix];
395 if (stream ==
nullptr)
397 AVCodecParameters *codecpar = stream->codecpar;
398 const AVCodecDescriptor* desc =
nullptr;
400 info.m_codecType =
' ';
401 if (codecpar !=
nullptr)
403 desc = avcodec_descriptor_get(codecpar->codec_id);
404 switch (codecpar->codec_type)
406 case AVMEDIA_TYPE_VIDEO:
407 info.m_codecType =
'V';
409 case AVMEDIA_TYPE_AUDIO:
410 info.m_codecType =
'A';
412 case AVMEDIA_TYPE_SUBTITLE:
413 info.m_codecType =
'S';
420 info.m_codecName = desc->name;
421 info.m_duration = stream->duration * stream->time_base.num / stream->time_base.den;
422 if (
info.m_codecType ==
'V')
424 if (codecpar !=
nullptr)
426 info.m_width = codecpar->width;
427 info.m_height = codecpar->height;
428 info.m_SampleAspectRatio =
static_cast<float>(codecpar->sample_aspect_ratio.num)
429 /
static_cast<float>(codecpar->sample_aspect_ratio.den);
430 switch (codecpar->field_order)
432 case AV_FIELD_PROGRESSIVE:
433 info.m_fieldOrder =
"PR";
436 info.m_fieldOrder =
"TT";
439 info.m_fieldOrder =
"BB";
442 info.m_fieldOrder =
"TB";
445 info.m_fieldOrder =
"BT";
451 info.m_frameRate =
static_cast<float>(stream->r_frame_rate.num)
452 /
static_cast<float>(stream->r_frame_rate.den);
453 info.m_avgFrameRate =
static_cast<float>(stream->avg_frame_rate.num)
454 /
static_cast<float>(stream->avg_frame_rate.den);
456 if (
info.m_codecType ==
'A')
457 info.m_channels = codecpar->ch_layout.nb_channels;
468 m_errorMsg =
"av_probe_input_format returned no result";
476 LOG(VB_GENERAL, LOG_ERR,
477 QString(
"MythStreamInfoList failed for %1. Error code:%2 Message:%3")
484 avformat_close_input(&ctx);
485 avformat_free_context(ctx);