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>
21namespace ranges = QtMultimediaPrivate::ranges;
29 if (codec.type() == AVMEDIA_TYPE_VIDEO) {
30 auto checkFormat = [format](AVPixelFormat f) {
return f == format; };
31 return findAVPixelFormat(codec, checkFormat).has_value();
34 if (codec.type() == AVMEDIA_TYPE_AUDIO) {
35 const auto sampleFormats = codec.sampleFormats();
36 return hasValue(sampleFormats, AVSampleFormat(format));
44 const auto desc = av_pix_fmt_desc_get(format);
45 return desc && (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0;
50 if (codec.isExperimental()) {
51 qCWarning(qLcFFmpegUtils) <<
"Applying the option 'strict -2' for the experimental codec"
52 << codec.name() <<
". it's unlikely to work properly";
53 av_dict_set(opts,
"strict",
"-2", 0);
60 case AV_HWDEVICE_TYPE_VIDEOTOOLBOX:
61 return AV_PIX_FMT_VIDEOTOOLBOX;
62 case AV_HWDEVICE_TYPE_VAAPI:
63 return AV_PIX_FMT_VAAPI;
64 case AV_HWDEVICE_TYPE_MEDIACODEC:
65 return AV_PIX_FMT_MEDIACODEC;
66 case AV_HWDEVICE_TYPE_CUDA:
67 return AV_PIX_FMT_CUDA;
68 case AV_HWDEVICE_TYPE_VDPAU:
69 return AV_PIX_FMT_VDPAU;
70 case AV_HWDEVICE_TYPE_OPENCL:
71 return AV_PIX_FMT_OPENCL;
72 case AV_HWDEVICE_TYPE_QSV:
73 return AV_PIX_FMT_QSV;
74 case AV_HWDEVICE_TYPE_D3D11VA:
75 return AV_PIX_FMT_D3D11;
76#if QT_FFMPEG_HAS_D3D12VA
77 case AV_HWDEVICE_TYPE_D3D12VA:
78 return AV_PIX_FMT_D3D12;
80 case AV_HWDEVICE_TYPE_DXVA2:
81 return AV_PIX_FMT_DXVA2_VLD;
82 case AV_HWDEVICE_TYPE_DRM:
83 return AV_PIX_FMT_DRM_PRIME;
84#if QT_FFMPEG_HAS_VULKAN
85 case AV_HWDEVICE_TYPE_VULKAN:
86 return AV_PIX_FMT_VULKAN;
89 return AV_PIX_FMT_NONE;
95 QScopeGuard freeData([&sideData]() { av_free(sideData.data); });
96#if QT_FFMPEG_STREAM_SIDE_DATA_DEPRECATED
97 AVPacketSideData *result = av_packet_side_data_add(
98 &stream->codecpar->coded_side_data,
99 &stream->codecpar->nb_coded_side_data,
113 qWarning() <<
"Adding stream side data is not supported for FFmpeg < 6.1";
123#if QT_FFMPEG_STREAM_SIDE_DATA_DEPRECATED
124 return av_packet_side_data_get(stream->codecpar->coded_side_data,
125 stream->codecpar->nb_coded_side_data, type);
127 const auto end = stream->side_data + stream->nb_side_data;
128 const auto found = std::find_if(stream->side_data, end, [&](
const auto &item) {
129 return item.type == type;
131 return found == end ?
nullptr : found;
136 const AVAudioFormat &outputFormat)
138 SwrContext *resampler =
nullptr;
139#if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT
141#if QT_FFMPEG_SWR_CONST_CH_LAYOUT
142 using AVChannelLayoutPrm =
const AVChannelLayout*;
144 using AVChannelLayoutPrm = AVChannelLayout*;
147 swr_alloc_set_opts2(&resampler,
148 const_cast<AVChannelLayoutPrm>(&outputFormat.channelLayout),
149 outputFormat.sampleFormat,
150 outputFormat.sampleRate,
151 const_cast<AVChannelLayoutPrm>(&inputFormat.channelLayout),
152 inputFormat.sampleFormat,
153 inputFormat.sampleRate,
159 resampler = swr_alloc_set_opts(
nullptr,
160 outputFormat.channelLayoutMask,
161 outputFormat.sampleFormat,
162 outputFormat.sampleRate,
163 inputFormat.channelLayoutMask,
164 inputFormat.sampleFormat,
165 inputFormat.sampleRate,
175 qCWarning(qLcFFmpegUtils) <<
"Failed to initialize audio resampler:" << error;
178 return SwrContextUPtr(resampler);
183 case AVCOL_TRC_BT709:
185 case AVCOL_TRC_BT1361_ECG:
186 case AVCOL_TRC_BT2020_10:
187 case AVCOL_TRC_BT2020_12:
188 case AVCOL_TRC_SMPTE240M:
189 return QVideoFrameFormat::ColorTransfer_BT709;
190 case AVCOL_TRC_GAMMA22:
191 case AVCOL_TRC_SMPTE428:
192 case AVCOL_TRC_IEC61966_2_1:
193 case AVCOL_TRC_IEC61966_2_4:
194 return QVideoFrameFormat::ColorTransfer_Gamma22;
195 case AVCOL_TRC_GAMMA28:
196 return QVideoFrameFormat::ColorTransfer_Gamma28;
197 case AVCOL_TRC_SMPTE170M:
198 return QVideoFrameFormat::ColorTransfer_BT601;
199 case AVCOL_TRC_LINEAR:
200 return QVideoFrameFormat::ColorTransfer_Linear;
201 case AVCOL_TRC_SMPTE2084:
202 return QVideoFrameFormat::ColorTransfer_ST2084;
203 case AVCOL_TRC_ARIB_STD_B67:
204 return QVideoFrameFormat::ColorTransfer_STD_B67;
208 return QVideoFrameFormat::ColorTransfer_Unknown;
214 case QVideoFrameFormat::ColorTransfer_BT709:
215 return AVCOL_TRC_BT709;
216 case QVideoFrameFormat::ColorTransfer_BT601:
217 return AVCOL_TRC_BT709;
218 case QVideoFrameFormat::ColorTransfer_Linear:
219 return AVCOL_TRC_SMPTE2084;
220 case QVideoFrameFormat::ColorTransfer_Gamma22:
221 return AVCOL_TRC_GAMMA22;
222 case QVideoFrameFormat::ColorTransfer_Gamma28:
223 return AVCOL_TRC_GAMMA28;
224 case QVideoFrameFormat::ColorTransfer_ST2084:
225 return AVCOL_TRC_SMPTE2084;
226 case QVideoFrameFormat::ColorTransfer_STD_B67:
227 return AVCOL_TRC_ARIB_STD_B67;
229 return AVCOL_TRC_UNSPECIFIED;
235 switch (colorSpace) {
237 case AVCOL_SPC_UNSPECIFIED:
238 case AVCOL_SPC_RESERVED:
240 case AVCOL_SPC_SMPTE240M:
241 case AVCOL_SPC_YCGCO:
242 case AVCOL_SPC_SMPTE2085:
243 case AVCOL_SPC_CHROMA_DERIVED_NCL:
244 case AVCOL_SPC_CHROMA_DERIVED_CL:
245 case AVCOL_SPC_ICTCP:
246 return QVideoFrameFormat::ColorSpace_Undefined;
248 return QVideoFrameFormat::ColorSpace_AdobeRgb;
249 case AVCOL_SPC_BT709:
250 return QVideoFrameFormat::ColorSpace_BT709;
251 case AVCOL_SPC_BT470BG:
252 case AVCOL_SPC_SMPTE170M:
253 return QVideoFrameFormat::ColorSpace_BT601;
254 case AVCOL_SPC_BT2020_NCL:
255 case AVCOL_SPC_BT2020_CL:
256 return QVideoFrameFormat::ColorSpace_BT2020;
262 switch (colorSpace) {
263 case QVideoFrameFormat::ColorSpace_BT601:
264 return AVCOL_SPC_BT470BG;
265 case QVideoFrameFormat::ColorSpace_BT709:
266 return AVCOL_SPC_BT709;
267 case QVideoFrameFormat::ColorSpace_AdobeRgb:
268 return AVCOL_SPC_RGB;
269 case QVideoFrameFormat::ColorSpace_BT2020:
270 return AVCOL_SPC_BT2020_CL;
272 return AVCOL_SPC_UNSPECIFIED;
278 switch (colorRange) {
279 case AVCOL_RANGE_MPEG:
280 return QVideoFrameFormat::ColorRange_Video;
281 case AVCOL_RANGE_JPEG:
282 return QVideoFrameFormat::ColorRange_Full;
284 return QVideoFrameFormat::ColorRange_Unknown;
290 switch (colorRange) {
291 case QVideoFrameFormat::ColorRange_Video:
292 return AVCOL_RANGE_MPEG;
293 case QVideoFrameFormat::ColorRange_Full:
294 return AVCOL_RANGE_JPEG;
296 return AVCOL_RANGE_UNSPECIFIED;
303 if (!frame->hw_frames_ctx)
306 const auto *frameCtx =
reinterpret_cast<AVHWFramesContext *>(frame->hw_frames_ctx->data);
310 return frameCtx->device_ctx;
314 AVPixelFormat dstPixFmt, SwsFlags conversionType)
318 sws_getContext(srcSize.width(), srcSize.height(), srcPixFmt, dstSize.width(),
319 dstSize.height(), dstPixFmt, conversionType,
nullptr,
nullptr,
nullptr);
322 qCWarning(qLcFFmpegUtils) <<
"Cannot create sws context for:\n"
323 <<
"srcSize:" << srcSize
324 <<
"srcPixFmt:" << srcPixFmt
325 <<
"dstSize:" << dstSize
326 <<
"dstPixFmt:" << dstPixFmt
327 <<
"conversionType:" << conversionType;
329 return SwsContextUPtr(result);
350 dbg << value.num <<
"/" << value.den;
357 auto freeBuffer = QScopeGuard([&] {
361 int status = av_dict_get_string(&dict, &buffer,
'=',
',');
362 if (status < 0 || !buffer)
363 return dbg <<
"Failed to print AVDictionary";
371 const AVDictionary *rawDict = dict.opts;
373 return dbg << *rawDict;
375 return dbg <<
"Empty AVDictionaryHolder";
385 char errBuf[AV_ERROR_MAX_STRING_SIZE];
386 dbg << av_make_error_string(errBuf, AV_ERROR_MAX_STRING_SIZE, qToUnderlying(error));
390#if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT
391QDebug operator<<(QDebug dbg,
const AVChannelLayout &layout)
394 dbg <<
"nb_channels:" << layout.nb_channels;
395 dbg <<
", order:" << layout.order;
397 if (layout.order == AV_CHANNEL_ORDER_NATIVE || layout.order == AV_CHANNEL_ORDER_AMBISONIC)
398 dbg <<
", mask:" << Qt::bin << layout.u.mask << Qt::dec;
399 else if (layout.order == AV_CHANNEL_ORDER_CUSTOM && layout.u.map)
400 dbg <<
", id: " << layout.u.map->id;
408#if QT_FFMPEG_HAS_AVCODEC_GET_SUPPORTED_CONFIG
409QDebug operator<<(QDebug dbg,
const AVCodecConfig value)
412 case AV_CODEC_CONFIG_CHANNEL_LAYOUT:
413 dbg <<
"AV_CODEC_CONFIG_CHANNEL_LAYOUT";
415 case AV_CODEC_CONFIG_COLOR_RANGE:
416 dbg <<
"AV_CODEC_CONFIG_COLOR_RANGE";
418 case AV_CODEC_CONFIG_COLOR_SPACE:
419 dbg <<
"AV_CODEC_CONFIG_COLOR_SPACE";
421 case AV_CODEC_CONFIG_FRAME_RATE:
422 dbg <<
"AV_CODEC_CONFIG_FRAME_RATE";
424 case AV_CODEC_CONFIG_PIX_FORMAT:
425 dbg <<
"AV_CODEC_CONFIG_PIX_FORMAT";
427 case AV_CODEC_CONFIG_SAMPLE_FORMAT:
428 dbg <<
"AV_CODEC_CONFIG_SAMPLE_FORMAT";
430 case AV_CODEC_CONFIG_SAMPLE_RATE:
431 dbg <<
"AV_CODEC_CONFIG_SAMPLE_RATE";
434 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)
std::conditional_t< QT_FFMPEG_AVIO_WRITE_CONST, const uint8_t *, uint8_t * > AvioWriteBufferType
QVideoFrameFormat::ColorSpace fromAvColorSpace(AVColorSpace colorSpace)
bool isAVFormatSupported(const Codec &codec, PixelOrSampleFormat format)
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)
QDebug operator<<(QDebug dbg, const QFFmpeg::AVDictionaryHolder &dict)
QDebug operator<<(QDebug dbg, QFFmpeg::AVError error)
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)