6#include <QtCore/qdebug.h>
7#include <QtCore/qloggingcategory.h>
8#include <QtCore/qscopeguard.h>
11#include <libavutil/pixdesc.h>
12#include <libavutil/samplefmt.h>
13#include <libavutil/error.h>
16#include <libavutil/hwcontext_videotoolbox.h>
28 if (codec.type() == AVMEDIA_TYPE_VIDEO) {
29 auto checkFormat = [format](AVPixelFormat f) {
return f == format; };
30 return findAVPixelFormat(codec, checkFormat).has_value();
33 if (codec.type() == AVMEDIA_TYPE_AUDIO) {
34 const auto sampleFormats = codec.sampleFormats();
35 return hasValue(sampleFormats, AVSampleFormat(format));
43 const auto desc = av_pix_fmt_desc_get(format);
44 return desc && (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
49 if (codec.isExperimental()) {
50 qCWarning(qLcFFmpegUtils) <<
"Applying the option 'strict -2' for the experimental codec"
51 << codec.name() <<
". it's unlikely to work properly";
52 av_dict_set(opts,
"strict",
"-2", 0);
59 case AV_HWDEVICE_TYPE_VIDEOTOOLBOX:
60 return AV_PIX_FMT_VIDEOTOOLBOX;
61 case AV_HWDEVICE_TYPE_VAAPI:
62 return AV_PIX_FMT_VAAPI;
63 case AV_HWDEVICE_TYPE_MEDIACODEC:
64 return AV_PIX_FMT_MEDIACODEC;
65 case AV_HWDEVICE_TYPE_CUDA:
66 return AV_PIX_FMT_CUDA;
67 case AV_HWDEVICE_TYPE_VDPAU:
68 return AV_PIX_FMT_VDPAU;
69 case AV_HWDEVICE_TYPE_OPENCL:
70 return AV_PIX_FMT_OPENCL;
71 case AV_HWDEVICE_TYPE_QSV:
72 return AV_PIX_FMT_QSV;
73 case AV_HWDEVICE_TYPE_D3D11VA:
74 return AV_PIX_FMT_D3D11;
75#if QT_FFMPEG_HAS_D3D12VA
76 case AV_HWDEVICE_TYPE_D3D12VA:
77 return AV_PIX_FMT_D3D12;
79 case AV_HWDEVICE_TYPE_DXVA2:
80 return AV_PIX_FMT_DXVA2_VLD;
81 case AV_HWDEVICE_TYPE_DRM:
82 return AV_PIX_FMT_DRM_PRIME;
83#if QT_FFMPEG_HAS_VULKAN
84 case AV_HWDEVICE_TYPE_VULKAN:
85 return AV_PIX_FMT_VULKAN;
88 return AV_PIX_FMT_NONE;
94 QScopeGuard freeData([&sideData]() { av_free(sideData.data); });
95#if QT_FFMPEG_STREAM_SIDE_DATA_DEPRECATED
96 AVPacketSideData *result = av_packet_side_data_add(
97 &stream->codecpar->coded_side_data,
98 &stream->codecpar->nb_coded_side_data,
112 qWarning() <<
"Adding stream side data is not supported for FFmpeg < 6.1";
122#if QT_FFMPEG_STREAM_SIDE_DATA_DEPRECATED
123 return av_packet_side_data_get(stream->codecpar->coded_side_data,
124 stream->codecpar->nb_coded_side_data, type);
126 auto checkType = [type](
const auto &item) {
return item.type == type; };
127 const auto end = stream->side_data + stream->nb_side_data;
128 const auto found = std::find_if(stream->side_data, end, checkType);
129 return found == end ?
nullptr : found;
134 const AVAudioFormat &outputFormat)
136 SwrContext *resampler =
nullptr;
137#if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT
139#if QT_FFMPEG_SWR_CONST_CH_LAYOUT
140 using AVChannelLayoutPrm =
const AVChannelLayout*;
142 using AVChannelLayoutPrm = AVChannelLayout*;
145 swr_alloc_set_opts2(&resampler,
146 const_cast<AVChannelLayoutPrm>(&outputFormat.channelLayout),
147 outputFormat.sampleFormat,
148 outputFormat.sampleRate,
149 const_cast<AVChannelLayoutPrm>(&inputFormat.channelLayout),
150 inputFormat.sampleFormat,
151 inputFormat.sampleRate,
157 resampler = swr_alloc_set_opts(
nullptr,
158 outputFormat.channelLayoutMask,
159 outputFormat.sampleFormat,
160 outputFormat.sampleRate,
161 inputFormat.channelLayoutMask,
162 inputFormat.sampleFormat,
163 inputFormat.sampleRate,
173 qCWarning(qLcFFmpegUtils) <<
"Failed to initialize audio resampler:" << error;
176 return SwrContextUPtr(resampler);
181 case AVCOL_TRC_BT709:
183 case AVCOL_TRC_BT1361_ECG:
184 case AVCOL_TRC_BT2020_10:
185 case AVCOL_TRC_BT2020_12:
186 case AVCOL_TRC_SMPTE240M:
187 return QVideoFrameFormat::ColorTransfer_BT709;
188 case AVCOL_TRC_GAMMA22:
189 case AVCOL_TRC_SMPTE428:
190 case AVCOL_TRC_IEC61966_2_1:
191 case AVCOL_TRC_IEC61966_2_4:
192 return QVideoFrameFormat::ColorTransfer_Gamma22;
193 case AVCOL_TRC_GAMMA28:
194 return QVideoFrameFormat::ColorTransfer_Gamma28;
195 case AVCOL_TRC_SMPTE170M:
196 return QVideoFrameFormat::ColorTransfer_BT601;
197 case AVCOL_TRC_LINEAR:
198 return QVideoFrameFormat::ColorTransfer_Linear;
199 case AVCOL_TRC_SMPTE2084:
200 return QVideoFrameFormat::ColorTransfer_ST2084;
201 case AVCOL_TRC_ARIB_STD_B67:
202 return QVideoFrameFormat::ColorTransfer_STD_B67;
206 return QVideoFrameFormat::ColorTransfer_Unknown;
212 case QVideoFrameFormat::ColorTransfer_BT709:
213 return AVCOL_TRC_BT709;
214 case QVideoFrameFormat::ColorTransfer_BT601:
215 return AVCOL_TRC_BT709;
216 case QVideoFrameFormat::ColorTransfer_Linear:
217 return AVCOL_TRC_SMPTE2084;
218 case QVideoFrameFormat::ColorTransfer_Gamma22:
219 return AVCOL_TRC_GAMMA22;
220 case QVideoFrameFormat::ColorTransfer_Gamma28:
221 return AVCOL_TRC_GAMMA28;
222 case QVideoFrameFormat::ColorTransfer_ST2084:
223 return AVCOL_TRC_SMPTE2084;
224 case QVideoFrameFormat::ColorTransfer_STD_B67:
225 return AVCOL_TRC_ARIB_STD_B67;
227 return AVCOL_TRC_UNSPECIFIED;
233 switch (colorSpace) {
235 case AVCOL_SPC_UNSPECIFIED:
236 case AVCOL_SPC_RESERVED:
238 case AVCOL_SPC_SMPTE240M:
239 case AVCOL_SPC_YCGCO:
240 case AVCOL_SPC_SMPTE2085:
241 case AVCOL_SPC_CHROMA_DERIVED_NCL:
242 case AVCOL_SPC_CHROMA_DERIVED_CL:
243 case AVCOL_SPC_ICTCP:
244 return QVideoFrameFormat::ColorSpace_Undefined;
246 return QVideoFrameFormat::ColorSpace_AdobeRgb;
247 case AVCOL_SPC_BT709:
248 return QVideoFrameFormat::ColorSpace_BT709;
249 case AVCOL_SPC_BT470BG:
250 case AVCOL_SPC_SMPTE170M:
251 return QVideoFrameFormat::ColorSpace_BT601;
252 case AVCOL_SPC_BT2020_NCL:
253 case AVCOL_SPC_BT2020_CL:
254 return QVideoFrameFormat::ColorSpace_BT2020;
260 switch (colorSpace) {
261 case QVideoFrameFormat::ColorSpace_BT601:
262 return AVCOL_SPC_BT470BG;
263 case QVideoFrameFormat::ColorSpace_BT709:
264 return AVCOL_SPC_BT709;
265 case QVideoFrameFormat::ColorSpace_AdobeRgb:
266 return AVCOL_SPC_RGB;
267 case QVideoFrameFormat::ColorSpace_BT2020:
268 return AVCOL_SPC_BT2020_CL;
270 return AVCOL_SPC_UNSPECIFIED;
276 switch (colorRange) {
277 case AVCOL_RANGE_MPEG:
278 return QVideoFrameFormat::ColorRange_Video;
279 case AVCOL_RANGE_JPEG:
280 return QVideoFrameFormat::ColorRange_Full;
282 return QVideoFrameFormat::ColorRange_Unknown;
288 switch (colorRange) {
289 case QVideoFrameFormat::ColorRange_Video:
290 return AVCOL_RANGE_MPEG;
291 case QVideoFrameFormat::ColorRange_Full:
292 return AVCOL_RANGE_JPEG;
294 return AVCOL_RANGE_UNSPECIFIED;
301 if (!frame->hw_frames_ctx)
304 const auto *frameCtx =
reinterpret_cast<AVHWFramesContext *>(frame->hw_frames_ctx->data);
308 return frameCtx->device_ctx;
312 AVPixelFormat dstPixFmt, SwsFlags conversionType)
316 sws_getContext(srcSize.width(), srcSize.height(), srcPixFmt, dstSize.width(),
317 dstSize.height(), dstPixFmt, conversionType,
nullptr,
nullptr,
nullptr);
320 qCWarning(qLcFFmpegUtils) <<
"Cannot create sws context for:\n"
321 <<
"srcSize:" << srcSize
322 <<
"srcPixFmt:" << srcPixFmt
323 <<
"dstSize:" << dstSize
324 <<
"dstPixFmt:" << dstPixFmt
325 <<
"conversionType:" << conversionType;
327 return SwsContextUPtr(result);
348 dbg << value.num <<
"/" << value.den;
355 auto freeBuffer = QScopeGuard([&] {
359 int status = av_dict_get_string(&dict, &buffer,
'=',
',');
360 if (status < 0 || !buffer)
361 return dbg <<
"Failed to print AVDictionary";
369 const AVDictionary *rawDict = dict.opts;
371 return dbg << *rawDict;
373 return dbg <<
"Empty AVDictionaryHolder";
383 char errBuf[AV_ERROR_MAX_STRING_SIZE];
384 dbg << av_make_error_string(errBuf, AV_ERROR_MAX_STRING_SIZE, qToUnderlying(error));
388#if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT
389QDebug operator<<(QDebug dbg,
const AVChannelLayout &layout)
392 dbg <<
"nb_channels:" << layout.nb_channels;
393 dbg <<
", order:" << layout.order;
395 if (layout.order == AV_CHANNEL_ORDER_NATIVE || layout.order == AV_CHANNEL_ORDER_AMBISONIC)
396 dbg <<
", mask:" << Qt::bin << layout.u.mask << Qt::dec;
397 else if (layout.order == AV_CHANNEL_ORDER_CUSTOM && layout.u.map)
398 dbg <<
", id: " << layout.u.map->id;
406#if QT_FFMPEG_HAS_AVCODEC_GET_SUPPORTED_CONFIG
407QDebug operator<<(QDebug dbg,
const AVCodecConfig value)
410 case AV_CODEC_CONFIG_CHANNEL_LAYOUT:
411 dbg <<
"AV_CODEC_CONFIG_CHANNEL_LAYOUT";
413 case AV_CODEC_CONFIG_COLOR_RANGE:
414 dbg <<
"AV_CODEC_CONFIG_COLOR_RANGE";
416 case AV_CODEC_CONFIG_COLOR_SPACE:
417 dbg <<
"AV_CODEC_CONFIG_COLOR_SPACE";
419 case AV_CODEC_CONFIG_FRAME_RATE:
420 dbg <<
"AV_CODEC_CONFIG_FRAME_RATE";
422 case AV_CODEC_CONFIG_PIX_FORMAT:
423 dbg <<
"AV_CODEC_CONFIG_PIX_FORMAT";
425 case AV_CODEC_CONFIG_SAMPLE_FORMAT:
426 dbg <<
"AV_CODEC_CONFIG_SAMPLE_FORMAT";
428 case AV_CODEC_CONFIG_SAMPLE_RATE:
429 dbg <<
"AV_CODEC_CONFIG_SAMPLE_RATE";
432 dbg <<
"<UNKNOWN_CODEC_CONFIG>";
AVColorSpace toAvColorSpace(QVideoFrameFormat::ColorSpace colorSpace)
SwsContextUPtr createSwsContext(const QSize &srcSize, AVPixelFormat srcPixFmt, const QSize &dstSize, AVPixelFormat dstPixFmt, SwsFlags conversionType)
void applyExperimentalCodecOptions(const Codec &codec, AVDictionary **opts)
bool isHwPixelFormat(AVPixelFormat format)
AVPacketSideData * addStreamSideData(AVStream *stream, AVPacketSideData sideData)
AVHWDeviceContext * avFrameDeviceContext(const AVFrame *frame)
AVColorTransferCharacteristic toAvColorTransfer(QVideoFrameFormat::ColorTransfer colorTrc)
QVideoFrameFormat::ColorSpace fromAvColorSpace(AVColorSpace colorSpace)
bool isAVFormatSupported(const Codec &codec, PixelOrSampleFormat format)
std::conditional_t< QT_FFMPEG_AVIO_WRITE_CONST, const uint8_t *, uint8_t * > AvioWriteBufferType
QVideoFrameFormat::ColorTransfer fromAvColorTransfer(AVColorTransferCharacteristic colorTrc)
QVideoFrameFormat::ColorRange fromAvColorRange(AVColorRange colorRange)
SwrContextUPtr createResampleContext(const AVAudioFormat &inputFormat, const AVAudioFormat &outputFormat)
AVPixelFormat pixelFormatForHwDevice(AVHWDeviceType deviceType)
const AVPacketSideData * streamSideData(const AVStream *stream, AVPacketSideDataType type)
AVColorRange toAvColorRange(QVideoFrameFormat::ColorRange colorRange)
Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core")
QDebug operator<<(QDebug dbg, const QFFmpeg::AVDictionaryHolder &dict)
QDebug operator<<(QDebug dbg, QFFmpeg::AVError error)
QDebug operator<<(QDebug dbg, const QFileInfo &fi)