Krita Source Code Documentation
Loading...
Searching...
No Matches
KisLibavEncoderContext.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 */
4#ifndef KISLIBAVENCODERCONTEXT
5#define KISLIBAVENCODERCONTEXT
6
7#include <QImage>
8#include <QString>
9
10#include <klocalizedstring.h>
11
12#include <kis_debug.h>
13
14extern "C" {
15#include <libavutil/imgutils.h>
16#include <libavutil/pixfmt.h>
17#include <libswscale/swscale.h>
18}
19
21{
22 Q_DISABLE_COPY_MOVE(KisLibavEncoderContext)
23public:
24 explicit KisLibavEncoderContext(QString *outErrorMessage = nullptr)
25 : m_outErrorMessage(outErrorMessage)
26 {
27 }
28
30 {
31 sws_freeContext(m_swsContext);
32 };
33
34 bool convertFrame(QImage &inOutImage, AVPixelFormat &outPixelFormat) const
35 {
36 switch (inOutImage.format()) {
37 case QImage::Format_RGB32:
38 outPixelFormat = AV_PIX_FMT_BGR0;
39 break;
40 case QImage::Format_ARGB32:
41 outPixelFormat = AV_PIX_FMT_BGRA;
42 break;
43 default:
44 // The above are the only formats I can get the the recorder to
45 // produce, so I'm not gonna get experimental with this.
46 outPixelFormat = AV_PIX_FMT_BGRA;
47 inOutImage = inOutImage.convertToFormat(QImage::Format_ARGB32);
48 if (inOutImage.isNull()) {
49 warnFile << "Frame conversion from" << inOutImage.format() << "failed";
50 return false;
51 }
52 break;
53 }
54 return true;
55 }
56
57 int getSwsFlags(const QString &scaleFilter) const
58 {
59 // These are the values provided by KisDlgAnimationRenderer.
60 if (scaleFilter.isEmpty() || scaleFilter == QStringLiteral("bilinear")) {
61 return SWS_FAST_BILINEAR;
62 } else if (scaleFilter == QStringLiteral("bicubic")) {
63 return SWS_BICUBIC;
64 } else if (scaleFilter == QStringLiteral("lanczos")) {
65 return SWS_LANCZOS;
66 } else if (scaleFilter == QStringLiteral("neighbor")) {
67 return 0;
68 } else if (scaleFilter == QStringLiteral("spline")) {
69 return SWS_SPLINE;
70 } else {
71 warnFile.nospace() << "Unhandled scale filter '" << scaleFilter << "'";
72 return SWS_FAST_BILINEAR;
73 }
74 }
75
76 SwsContext *getSwsContextFor(int inputWidth,
77 int inputHeight,
78 AVPixelFormat inputFormat,
79 int outputWidth,
80 int outputHeight,
81 AVPixelFormat outputFormat,
82 int flags)
83 {
84 return sws_getCachedContext(m_swsContext,
85 inputWidth,
86 inputHeight,
87 inputFormat,
88 outputWidth,
89 outputHeight,
90 outputFormat,
91 flags,
92 nullptr,
93 nullptr,
94 nullptr);
95 }
96
97 void setInternalErrorMessage(const QString &detail)
98 {
99 warnFile << "Media encoder error:" << detail;
100 // Internal encoder errors are only really useful for developers,
101 // so there's no point in translating them.
102 setErrorMessage(i18n("Internal error (%1)", detail));
103 }
104
105 void setErrorMessage(const QString &errorMessage)
106 {
107 if (m_outErrorMessage) {
108 *m_outErrorMessage = errorMessage;
109 }
110 }
111
112private:
114 SwsContext *m_swsContext = nullptr;
115};
116
117#endif
void setErrorMessage(const QString &errorMessage)
KisLibavEncoderContext(QString *outErrorMessage=nullptr)
SwsContext * getSwsContextFor(int inputWidth, int inputHeight, AVPixelFormat inputFormat, int outputWidth, int outputHeight, AVPixelFormat outputFormat, int flags)
void setInternalErrorMessage(const QString &detail)
int getSwsFlags(const QString &scaleFilter) const
bool convertFrame(QImage &inOutImage, AVPixelFormat &outPixelFormat) const
#define warnFile
Definition kis_debug.h:99