Qt 6.x
The Qt SDK
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,
14 std::unique_ptr<QFFmpeg::HWAccel> hwAccel)
15 : context(std::move(context)), stream(stream), hwAccel(std::move(hwAccel))
16{
17}
18
19Codec::Data::~Data()
20{
21 // TODO: investigate if we can remove avcodec_close
22 // FFmpeg doc says that avcodec_free_context is enough
23 avcodec_close(context.get());
24}
25
26QMaybe<Codec> Codec::create(AVStream *stream)
27{
28 if (!stream)
29 return { "Invalid stream" };
30
31 const AVCodec *decoder = nullptr;
32 std::unique_ptr<QFFmpeg::HWAccel> hwAccel;
33
34 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
35 std::tie(decoder, hwAccel) = HWAccel::findDecoderWithHwAccel(stream->codecpar->codec_id);
36
37 if (!decoder)
38 decoder = QFFmpeg::findAVDecoder(stream->codecpar->codec_id);
39
40 if (!decoder)
41 return { "Failed to find a valid FFmpeg decoder" };
42
43 qCDebug(qLcPlaybackEngineCodec) << "found decoder" << decoder->name << "for id" << decoder->id;
44
45 AVCodecContextUPtr context(avcodec_alloc_context3(decoder));
46 if (!context)
47 return { "Failed to allocate a FFmpeg codec context" };
48
49 if (hwAccel)
50 context->hw_device_ctx = av_buffer_ref(hwAccel->hwDeviceContextAsBuffer());
51
52 if (context->codec_type != AVMEDIA_TYPE_AUDIO && context->codec_type != AVMEDIA_TYPE_VIDEO
53 && context->codec_type != AVMEDIA_TYPE_SUBTITLE) {
54 return { "Unknown codec type" };
55 }
56
57 int ret = avcodec_parameters_to_context(context.get(), stream->codecpar);
58 if (ret < 0)
59 return { "Failed to set FFmpeg codec parameters" };
60
61 // ### This still gives errors about wrong HW formats (as we accept all of them)
62 // But it would be good to get so we can filter out pixel format we don't support natively
63 context->get_format = QFFmpeg::getFormat;
64
65 /* Init the decoder, with reference counting and threading */
67 av_dict_set(opts, "refcounted_frames", "1", 0);
68 av_dict_set(opts, "threads", "auto", 0);
69
70 ret = avcodec_open2(context.get(), decoder, opts);
71 if (ret < 0)
72 return "Failed to open FFmpeg codec context " + err2str(ret);
73
74 return Codec(new Data(std::move(context), stream, std::move(hwAccel)));
75}
76
78
79} // namespace QFFmpeg
AVPixelFormat getFormat(AVCodecContext *codecContext, const AVPixelFormat *suggestedFormats)
std::unique_ptr< AVCodecContext, AVDeleter< decltype(&avcodec_free_context), &avcodec_free_context > > AVCodecContextUPtr
Definition qffmpeg_p.h:129
QString err2str(int errnum)
Definition qffmpeg_p.h:56
const AVCodec * findAVDecoder(AVCodecID codecId, const std::optional< AVHWDeviceType > &deviceType, const std::optional< PixelOrSampleFormat > &format)
Definition qffmpeg.cpp:270
Combined button and popup list for selecting options.
static void * context
EGLStreamKHR stream
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
return ret