Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qffmpegcodec.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "qloggingcategory.h"
6
8
9static Q_LOGGING_CATEGORY(qLcPlaybackEngineCodec, "qt.multimedia.playbackengine.codec");
10
11namespace QFFmpeg {
12
13Codec::Data::Data(AVCodecContextUPtr context, AVStream *stream, AVFormatContext *formatContext,
14 std::unique_ptr<QFFmpeg::HWAccel> hwAccel)
15 : context(std::move(context)), stream(stream), hwAccel(std::move(hwAccel))
16{
17 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
18 pixelAspectRatio = av_guess_sample_aspect_ratio(formatContext, stream, nullptr);
19}
20
21QMaybe<Codec> Codec::create(AVStream *stream, AVFormatContext *formatContext)
22{
23 if (!stream)
24 return { "Invalid stream" };
25
26 const AVCodec *decoder = nullptr;
27 std::unique_ptr<QFFmpeg::HWAccel> hwAccel;
28
29 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
30 std::tie(decoder, hwAccel) = HWAccel::findDecoderWithHwAccel(stream->codecpar->codec_id);
31
32 if (!decoder)
33 decoder = QFFmpeg::findAVDecoder(stream->codecpar->codec_id);
34
35 if (!decoder)
36 return { "Failed to find a valid FFmpeg decoder" };
37
38 qCDebug(qLcPlaybackEngineCodec) << "found decoder" << decoder->name << "for id" << decoder->id;
39
40 AVCodecContextUPtr context(avcodec_alloc_context3(decoder));
41 if (!context)
42 return { "Failed to allocate a FFmpeg codec context" };
43
44 if (hwAccel)
45 context->hw_device_ctx = av_buffer_ref(hwAccel->hwDeviceContextAsBuffer());
46
47 if (context->codec_type != AVMEDIA_TYPE_AUDIO && context->codec_type != AVMEDIA_TYPE_VIDEO
48 && context->codec_type != AVMEDIA_TYPE_SUBTITLE) {
49 return { "Unknown codec type" };
50 }
51
52 int ret = avcodec_parameters_to_context(context.get(), stream->codecpar);
53 if (ret < 0)
54 return { "Failed to set FFmpeg codec parameters" };
55
56 // ### This still gives errors about wrong HW formats (as we accept all of them)
57 // But it would be good to get so we can filter out pixel format we don't support natively
58 context->get_format = QFFmpeg::getFormat;
59
60 /* Init the decoder, with reference counting and threading */
62 av_dict_set(opts, "refcounted_frames", "1", 0);
63 av_dict_set(opts, "threads", "auto", 0);
64 applyExperimentalCodecOptions(decoder, opts);
65
66 ret = avcodec_open2(context.get(), decoder, opts);
67 if (ret < 0)
68 return QString("Failed to open FFmpeg codec context " + err2str(ret));
69
70 return Codec(new Data(std::move(context), stream, formatContext, std::move(hwAccel)));
71}
72
73AVRational Codec::pixelAspectRatio(AVFrame *frame) const
74{
75 // does the same as av_guess_sample_aspect_ratio, but more efficient
76 return d->pixelAspectRatio.num && d->pixelAspectRatio.den ? d->pixelAspectRatio
77 : frame->sample_aspect_ratio;
78}
79
81
82} // namespace QFFmpeg
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
AVPixelFormat getFormat(AVCodecContext *codecContext, const AVPixelFormat *suggestedFormats)
QString err2str(int errnum)
Definition qffmpeg_p.h:64
const AVCodec * findAVDecoder(AVCodecID codecId, const std::optional< AVHWDeviceType > &deviceType, const std::optional< PixelOrSampleFormat > &format)
Definition qffmpeg.cpp:427
std::unique_ptr< AVCodecContext, AVDeleter< decltype(&avcodec_free_context), &avcodec_free_context > > AVCodecContextUPtr
Definition qffmpeg_p.h:144
void applyExperimentalCodecOptions(const AVCodec *codec, AVDictionary **opts)
Definition qffmpeg.cpp:467
Combined button and popup list for selecting options.
static void * context
EGLStreamKHR stream
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
return ret
QFrame frame
[0]