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 | |
4 | #include "playbackengine/qffmpegvideorenderer_p.h" |
5 | #include "qffmpegvideobuffer_p.h" |
6 | #include "qvideosink.h" |
7 | #include "private/qvideoframe_p.h" |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | namespace QFFmpeg { |
12 | |
13 | VideoRenderer::VideoRenderer(const TimeController &tc, QVideoSink *sink, |
14 | const VideoTransformation &transform) |
15 | : Renderer(tc), m_sink(sink), m_transform(transform) |
16 | { |
17 | } |
18 | |
19 | void VideoRenderer::setOutput(QVideoSink *sink, bool cleanPrevSink) |
20 | { |
21 | setOutputInternal(actual&: m_sink, desired: sink, changeHandler: [=](QVideoSink *prev) { |
22 | if (!prev) |
23 | return; |
24 | |
25 | if (sink) |
26 | sink->setVideoFrame(prev->videoFrame()); |
27 | |
28 | if (cleanPrevSink) |
29 | prev->setVideoFrame({}); |
30 | }); |
31 | } |
32 | |
33 | VideoRenderer::RenderingResult VideoRenderer::renderInternal(Frame frame) |
34 | { |
35 | if (!m_sink) |
36 | return {}; |
37 | |
38 | if (!frame.isValid()) { |
39 | m_sink->setVideoFrame({}); |
40 | return {}; |
41 | } |
42 | |
43 | // qCDebug(qLcVideoRenderer) << "RHI:" << accel.isNull() << accel.rhi() << sink->rhi(); |
44 | |
45 | const auto codec = frame.codec(); |
46 | Q_ASSERT(codec); |
47 | |
48 | #ifdef Q_OS_ANDROID |
49 | // QTBUG-108446 |
50 | // In general case, just creation of frames context is not correct since |
51 | // frames may require additional specific data for hw contexts, so |
52 | // just setting of hw_frames_ctx is not enough. |
53 | // TODO: investigate the case in order to remove or fix the code. |
54 | if (codec->hwAccel() && !frame.avFrame()->hw_frames_ctx) { |
55 | HWAccel *hwaccel = codec->hwAccel(); |
56 | AVFrame *avframe = frame.avFrame(); |
57 | if (!hwaccel->hwFramesContext()) |
58 | hwaccel->createFramesContext(AVPixelFormat(avframe->format), |
59 | { avframe->width, avframe->height }); |
60 | |
61 | if (hwaccel->hwFramesContext()) |
62 | avframe->hw_frames_ctx = av_buffer_ref(hwaccel->hwFramesContextAsBuffer()); |
63 | } |
64 | #endif |
65 | |
66 | const auto pixelAspectRatio = codec->pixelAspectRatio(frame: frame.avFrame()); |
67 | auto buffer = std::make_unique<QFFmpegVideoBuffer>(args: frame.takeAVFrame(), args: pixelAspectRatio); |
68 | QVideoFrameFormat format(buffer->size(), buffer->pixelFormat()); |
69 | format.setColorSpace(buffer->colorSpace()); |
70 | format.setColorTransfer(buffer->colorTransfer()); |
71 | format.setColorRange(buffer->colorRange()); |
72 | format.setMaxLuminance(buffer->maxNits()); |
73 | format.setRotation(m_transform.rotation); |
74 | format.setMirrored(m_transform.mirrorredHorizontallyAfterRotation); |
75 | QVideoFrame videoFrame = QVideoFramePrivate::createFrame(buffer: std::move(buffer), format); |
76 | videoFrame.setStartTime(frame.pts()); |
77 | videoFrame.setEndTime(frame.end()); |
78 | m_sink->setVideoFrame(videoFrame); |
79 | |
80 | return {}; |
81 | } |
82 | |
83 | } // namespace QFFmpeg |
84 | |
85 | QT_END_NAMESPACE |
86 | |
87 | #include "moc_qffmpegvideorenderer_p.cpp" |
88 | |