Debian Codegraph @

src/videoframe.cpp

raw
1 /*
2 * This file is part of Bino, a 3D video player.
3 *
4 * Copyright (C) 2022, 2023, 2024, 2025
5 * Martin Lambers <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "videoframe.hpp"
22 #include "log.hpp"
23
24
25 VideoFrame::VideoFrame()
26 {
27 update(Input_Unknown, Surround_Unknown, QVideoFrame(), false);
28 }
29
30 bool VideoFrame::isValid() const
31 {
32 return (qframe.isValid() && qframe.pixelFormat() != QVideoFrameFormat::Format_Invalid);
33 }
34
35 // from qtmultimedia/src/multimedia/shaders/qvideotexturehelper.cpp
36 static float linearToPQ(float sig)
37 {
38 const float m1 = 1305.0f / 8192.0f;
39 const float m2 = 2523.0f / 32.0f;
40 const float c1 = 107.0f / 128.0f;
41 const float c2 = 2413.0f / 128.0f;
42 const float c3 = 2392.0f / 128.0f;
43 const float SDR_LEVEL = 100.f;
44 sig *= SDR_LEVEL / 10000.0f;
45 float psig = powf(sig, m1);
46 float num = c1 + c2 * psig;
47 float den = 1.0f + c3 * psig;
48 return powf(num / den, m2);
49 }
50
51 // from qtmultimedia/src/multimedia/shaders/qvideotexturehelper.cpp
52 static float linearToHLG(float sig)
53 {
54 const float a = 0.17883277f;
55 const float b = 0.28466892f; // = 1 - 4a
56 const float c = 0.55991073f; // = 0.5 - a ln(4a)
57 if (sig < 1.0f / 12.0f)
58 return sqrtf(3.0f * sig);
59 return a * logf(12.0f * sig - b) + c;
60 }
61
62 void VideoFrame::update(InputMode im, SurroundMode sm, const QVideoFrame& frame, bool newSrc)
63 {
64 if (qframe.isMapped())
65 qframe.unmap();
66 qframe = frame;
67
68 if (isValid()) {
69 subtitle = qframe.subtitleText();
70 width = qframe.width();
71 height = qframe.height();
72 aspectRatio = float(width) / height;
73 LOG_FIREHOSE("videoframe receives new %dx%d frame with pixel format %s", width, height,
74 qPrintable(QVideoFrameFormat::pixelFormatToString(qframe.pixelFormat())));
75 if (im == Input_Unknown) {
76 if (width % 2 == 0 && aspectRatio >= 3.0f)
77 im = Input_Left_Right;
78 else if (height % 2 == 0 && aspectRatio < 1.0f)
79 im = Input_Top_Bottom;
80 else
81 im = Input_Mono;
82 LOG_FIREHOSE("videoframe guesses input mode from aspect ratio %g: %s", aspectRatio, inputModeToString(im));
83 }
84 inputMode = im;
85 if (sm == Surround_Unknown) {
86 if (width == height && (inputMode == Input_Top_Bottom || inputMode == Input_Bottom_Top))
87 sm = Surround_360;
88 else if (width == 2 * height && inputMode == Input_Mono)
89 sm = Surround_360;
90 else if (width == 2 * height && (inputMode == Input_Top_Bottom_Half || inputMode == Input_Bottom_Top_Half))
91 sm = Surround_360;
92 else if (width == 2 * height && (inputMode == Input_Left_Right_Half || inputMode == Input_Right_Left_Half))
93 sm = Surround_360;
94 else if (width == 4 * height && (inputMode == Input_Left_Right || inputMode == Input_Right_Left))
95 sm = Surround_360;
96 else if (2 * width == height && (inputMode == Input_Top_Bottom || inputMode == Input_Bottom_Top))
97 sm = Surround_180;
98 else if (width == height && inputMode == Input_Mono)
99 sm = Surround_180;
100 else if (width == height && (inputMode == Input_Top_Bottom_Half || inputMode == Input_Bottom_Top_Half))
101 sm = Surround_180;
102 else if (width == height && (inputMode == Input_Left_Right_Half || inputMode == Input_Right_Left_Half))
103 sm = Surround_180;
104 else if (width == 2 * height && (inputMode == Input_Left_Right || inputMode == Input_Right_Left))
105 sm = Surround_180;
106 else
107 sm = Surround_Off;
108 LOG_FIREHOSE("videoframe guesses surround mode %s from frame size", surroundModeToString(sm));
109 }
110 surroundMode = sm;
111 bool fallbackToImage = true;
112 if ( qframe.pixelFormat() == QVideoFrameFormat::Format_ARGB8888
113 || qframe.pixelFormat() == QVideoFrameFormat::Format_ARGB8888_Premultiplied
114 || qframe.pixelFormat() == QVideoFrameFormat::Format_XRGB8888
115 || qframe.pixelFormat() == QVideoFrameFormat::Format_BGRA8888
116 || qframe.pixelFormat() == QVideoFrameFormat::Format_BGRA8888_Premultiplied
117 || qframe.pixelFormat() == QVideoFrameFormat::Format_BGRX8888
118 || qframe.pixelFormat() == QVideoFrameFormat::Format_ABGR8888
119 || qframe.pixelFormat() == QVideoFrameFormat::Format_XBGR8888
120 || qframe.pixelFormat() == QVideoFrameFormat::Format_RGBA8888
121 || qframe.pixelFormat() == QVideoFrameFormat::Format_RGBX8888
122 || qframe.pixelFormat() == QVideoFrameFormat::Format_YUV420P
123 || qframe.pixelFormat() == QVideoFrameFormat::Format_YUV422P
124 || qframe.pixelFormat() == QVideoFrameFormat::Format_YV12
125 || qframe.pixelFormat() == QVideoFrameFormat::Format_NV12
126 || qframe.pixelFormat() == QVideoFrameFormat::Format_P010
127 || qframe.pixelFormat() == QVideoFrameFormat::Format_P016
128 || qframe.pixelFormat() == QVideoFrameFormat::Format_Y8
129 || qframe.pixelFormat() == QVideoFrameFormat::Format_Y16) {
130 fallbackToImage = false;
131 }
132 if (fallbackToImage) {
133 if (newSrc) {
134 LOG_WARNING("%s", qPrintable(tr("Pixel format %1 is not hardware accelerated!")
135 .arg(QVideoFrameFormat::pixelFormatToString(qframe.pixelFormat()))));
136 }
137 storage = Storage_Image;
138 pixelFormat = QVideoFrameFormat::pixelFormatFromImageFormat(QImage::Format_RGB32);
139 colorRangeSmall = false;
140 colorSpace = CS_AdobeRgb;
141 image = qframe.toImage();
142 image.convertTo(QImage::Format_RGB32);
143 } else {
144 storage = Storage_Mapped;
145 pixelFormat = qframe.pixelFormat();
146 // Heuristic used in qtmultimedia/src/multimedia/video/qvideotexturehelper.cpp:
147 colorSpace = (qframe.surfaceFormat().frameHeight() > 576 ? CS_BT709 : CS_BT601);
148 switch (qframe.surfaceFormat().colorSpace()) {
149 case QVideoFrameFormat::ColorSpace_Undefined:
150 // nothing to do: default was already guessed
151 break;
152 case QVideoFrameFormat::ColorSpace_BT601:
153 colorSpace = CS_BT601;
154 break;
155 case QVideoFrameFormat::ColorSpace_BT709:
156 colorSpace = CS_BT709;
157 break;
158 case QVideoFrameFormat::ColorSpace_AdobeRgb:
159 colorSpace = CS_AdobeRgb;
160 break;
161 case QVideoFrameFormat::ColorSpace_BT2020:
162 colorSpace = CS_BT2020;
163 break;
164 }
165 colorRangeSmall = true;
166 switch (qframe.surfaceFormat().colorRange()) {
167 case QVideoFrameFormat::ColorRange_Unknown:
168 case QVideoFrameFormat::ColorRange_Video:
169 // nothing to do: default value already set
170 break;
171 case QVideoFrameFormat::ColorRange_Full:
172 colorRangeSmall = false;
173 break;
174 }
175 colorTransfer = CT_NOOP; // color was already transferred by color space conversion
176 masteringWhite = 1.0f;
177 switch (qframe.surfaceFormat().colorTransfer()) {
178 case QVideoFrameFormat::ColorTransfer_Unknown:
179 case QVideoFrameFormat::ColorTransfer_BT709:
180 case QVideoFrameFormat::ColorTransfer_BT601:
181 case QVideoFrameFormat::ColorTransfer_Linear:
182 case QVideoFrameFormat::ColorTransfer_Gamma22:
183 case QVideoFrameFormat::ColorTransfer_Gamma28:
184 // nothing to do: default was already set
185 break;
186 case QVideoFrameFormat::ColorTransfer_ST2084:
187 colorTransfer = CT_ST2084;
188 masteringWhite = linearToPQ(qframe.surfaceFormat().maxLuminance() / 100.0f);
189 break;
190 case QVideoFrameFormat::ColorTransfer_STD_B67:
191 colorTransfer = CT_STD_B67;
192 masteringWhite = linearToHLG(qframe.surfaceFormat().maxLuminance() / 100.0f);
193 break;
194 }
195 qframe.map(QVideoFrame::ReadOnly);
196 planeCount = qframe.planeCount();
197 for (int p = 0; p < planeCount; p++) {
198 bytesPerLine[p] = qframe.bytesPerLine(p);
199 bytesPerPlane[p] = qframe.mappedBytes(p);
200 mappedBits[p] = qframe.bits(p);
201 }
202 }
203 subtitle = qframe.subtitleText();
204 subtitle.replace(QLatin1Char('\n'), QChar::LineSeparator); // qvideoframe.cpp does this
205 } else {
206 // Synthesize a black frame
207 inputMode = Input_Unknown;
208 surroundMode = Surround_Off;
209 width = 1;
210 height = 1;
211 storage = Storage_Image;
212 image = QImage(width, height, QImage::Format_RGB32);
213 image.fill(0);
214 aspectRatio = 1.0f;
215 subtitle = QString();
216 }
217 }
218
219 void VideoFrame::reUpdate()
220 {
221 update(inputMode, surroundMode, qframe, false);
222 }
223
224 void VideoFrame::invalidate()
225 {
226 if (isValid())
227 update(Input_Unknown, Surround_Unknown, QVideoFrame(), false);
228 }
229
230 QDataStream &operator<<(QDataStream& ds, const VideoFrame& f)
231 {
232 ds << static_cast<int>(f.inputMode);
233 ds << static_cast<int>(f.surroundMode);
234 ds << f.subtitle;
235 ds << f.width;
236 ds << f.height;
237 ds << f.aspectRatio;
238 switch (f.storage) {
239 case VideoFrame::Storage_Mapped:
240 case VideoFrame::Storage_Copied:
241 ds << static_cast<int>(VideoFrame::Storage_Copied);
242 ds << static_cast<int>(f.pixelFormat);
243 ds << f.colorRangeSmall;
244 ds << static_cast<int>(f.colorSpace);
245 ds << static_cast<int>(f.colorTransfer);
246 ds << f.masteringWhite;
247 ds << f.planeCount;
248 for (int p = 0; p < f.planeCount; p++) {
249 ds << f.bytesPerLine[p];
250 ds << f.bytesPerPlane[p];
251 if (f.storage == VideoFrame::Storage_Mapped)
252 ds.writeRawData(reinterpret_cast<const char*>(f.mappedBits[p]), f.bytesPerPlane[p]);
253 else
254 ds.writeRawData(reinterpret_cast<const char*>(f.bits[p].data()), f.bytesPerPlane[p]);
255 }
256 break;
257 case VideoFrame::Storage_Image:
258 ds.writeRawData(reinterpret_cast<const char*>(f.image.bits()), f.image.sizeInBytes());
259 break;
260 }
261 return ds;
262 }
263
264 QDataStream &operator>>(QDataStream& ds, VideoFrame& f)
265 {
266 int tmp;
267
268 ds >> tmp;
269 f.inputMode = static_cast<InputMode>(tmp);
270 ds >> tmp;
271 f.surroundMode = static_cast<SurroundMode>(tmp);
272 ds >> f.subtitle;
273 ds >> f.width;
274 ds >> f.height;
275 ds >> f.aspectRatio;
276 ds >> tmp;
277 f.storage = static_cast<enum VideoFrame::Storage>(tmp);
278 switch (f.storage) {
279 case VideoFrame::Storage_Mapped: // cannot happen, see above
280 case VideoFrame::Storage_Copied:
281 f.image = QImage();
282 ds >> tmp;
283 f.pixelFormat = static_cast<QVideoFrameFormat::PixelFormat>(tmp);
284 ds >> f.colorRangeSmall;
285 ds >> tmp;
286 f.colorSpace = static_cast<enum VideoFrame::ColorSpace>(tmp);
287 ds >> tmp;
288 f.colorTransfer = static_cast<enum VideoFrame::ColorTransfer>(tmp);
289 ds >> f.masteringWhite;
290 ds >> f.planeCount;
291 for (int p = 0; p < 3; p++) {
292 if (p < f.planeCount) {
293 ds >> f.bytesPerLine[p];
294 ds >> f.bytesPerPlane[p];
295 f.bits[p].resize(f.bytesPerPlane[p]);
296 ds.readRawData(reinterpret_cast<char*>(f.bits[p].data()), f.bytesPerPlane[p]);
297 } else {
298 f.bytesPerLine[p] = 0;
299 f.bytesPerPlane[p] = 0;
300 f.bits[p].clear();
301 }
302 f.mappedBits[p] = nullptr;
303 }
304 break;
305 case VideoFrame::Storage_Image:
306 f.pixelFormat = QVideoFrameFormat::pixelFormatFromImageFormat(QImage::Format_RGB32);
307 f.colorRangeSmall = false;
308 f.colorSpace = VideoFrame::CS_AdobeRgb;
309 f.planeCount = 0;
310 for (int p = 0; p < 3; p++) {
311 f.bytesPerLine[p] = 0;
312 f.bytesPerPlane[p] = 0;
313 f.mappedBits[p] = nullptr;
314 f.bits[p].clear();
315 }
316 f.image = QImage(f.width, f.height, QImage::Format_RGB32);
317 ds.readRawData(reinterpret_cast<char*>(f.image.bits()), f.image.sizeInBytes());
318 break;
319 }
320 return ds;
321 }