Debian Codegraph @

src/metadata.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 <QMediaPlayer>
22 #include <QGuiApplication>
23 #include <QThread>
24 #include <QMap>
25 #include <QJsonDocument>
26 #include <QJsonValue>
27 #ifndef Q_OS_WASM
28 # include <QProcess>
29 #endif
30
31 #include "metadata.hpp"
32 #include "tools.hpp"
33 #include "log.hpp"
34
35
36 MetaData::MetaData()
37 {
38 }
39
40 static int haveFFprobe = -1;
41 static QMap<QUrl, MetaData> cache;
42
43 bool MetaData::detectCached(const QUrl& url, QString* errMsg)
44 {
45 /* Try to find the url in the cache */
46 *this = cache[url];
47 if (!this->url.isEmpty())
48 return true;
49
50 /* Detection via file name hints - this sets default modes that can be overridden per stream */
51 InputMode defaultInputMode = Input_Unknown;
52 SurroundMode defaultSurroundMode = Surround_Unknown;
53 QString fileName = url.fileName();
54 QString extension = getExtension(fileName);
55 if (extension == "jps" || extension == "pns") {
56 defaultInputMode = Input_Right_Left;
57 } else if (extension == "mpo") {
58 defaultInputMode = Input_Top_Bottom; // this was converted; see digestiblemedia
59 } else {
60 /* Try to guess the input mode from a marker contained in the file name.
61 * This should be compatible to the Bino 1.x naming conventions. */
62 QString marker = fileName.left(fileName.lastIndexOf('.'));
63 marker = marker.right(marker.length() - marker.lastIndexOf('-') - 1);
64 marker = marker.toLower();
65 if (marker == "lr")
66 defaultInputMode = Input_Left_Right;
67 else if (marker == "rl")
68 defaultInputMode = Input_Right_Left;
69 else if (marker == "lrh" || marker == "lrq")
70 defaultInputMode = Input_Left_Right_Half;
71 else if (marker == "rlh" || marker == "rlq")
72 defaultInputMode = Input_Right_Left_Half;
73 else if (marker == "tb" || marker == "ab")
74 defaultInputMode = Input_Top_Bottom;
75 else if (marker == "bt" || marker == "ba")
76 defaultInputMode = Input_Bottom_Top;
77 else if (marker == "tbh" || marker == "abq")
78 defaultInputMode = Input_Top_Bottom_Half;
79 else if (marker == "bth" || marker == "baq")
80 defaultInputMode = Input_Bottom_Top_Half;
81 else if (marker == "2d")
82 defaultInputMode = Input_Mono;
83 }
84 if (defaultInputMode != Input_Unknown) {
85 LOG_DEBUG("guessing input mode %s from file name %s", inputModeToString(defaultInputMode), qPrintable(fileName));
86 }
87 int i360 = fileName.indexOf("360");
88 if (i360 >= 0 && (i360 == 0 || !fileName[i360 - 1].isLetterOrNumber()) && !fileName[i360 + 3].isLetterOrNumber()) {
89 defaultSurroundMode = Surround_360;
90 } else {
91 int i180 = fileName.indexOf("180");
92 if (i180 >= 0 && (i180 == 0 || !fileName[i180 - 1].isLetterOrNumber()) && !fileName[i180 + 3].isLetterOrNumber())
93 defaultSurroundMode = Surround_180;
94 }
95 if (defaultSurroundMode != Surround_Unknown) {
96 LOG_DEBUG("guessing surround mode %s from file name %s", surroundModeToString(defaultSurroundMode), qPrintable(fileName));
97 }
98
99 /* Detection via QMediaPlayer */
100 QMediaPlayer player;
101 bool failure = false;
102 bool available = false;
103 bool didFFprobe = false;
104 QString errorMessage;
105 player.connect(&player, &QMediaPlayer::errorOccurred,
106 [&](QMediaPlayer::Error, const QString& errorString) {
107 errorMessage = errorString;
108 LOG_WARNING("%s", qPrintable(tr("Cannot get meta data from %1: %2").arg(player.source().toString()).arg(errorString)));
109 failure = true;
110 });
111 player.connect(&player, &QMediaPlayer::metaDataChanged, [&]() { available = true; });
112 player.setSource(url);
113 do {
114 // while waiting for the QMediaPlayer meta data to arrive, run ffprobe in parallel
115 if (!didFFprobe) {
116 detectViaFFprobe(url, defaultInputMode, defaultSurroundMode);
117 didFFprobe = true;
118 }
119 QGuiApplication::processEvents();
120 }
121 while (!failure && !available);
122 if (failure) {
123 if (errMsg)
124 *errMsg = errorMessage;
125 return false;
126 }
127
128 this->url = url;
129 global = player.metaData();
130 videoTracks = player.videoTracks();
131 audioTracks = player.audioTracks();
132 subtitleTracks = player.subtitleTracks();
133 inputModes.resize(videoTracks.size(), defaultInputMode);
134 surroundModes.resize(videoTracks.size(), defaultSurroundMode);
135
136 cache.insert(url, *this);
137 return true;
138 }
139
140 void MetaData::detectViaFFprobe(const QUrl& url, InputMode& defaultInputMode, SurroundMode& defaultSurroundMode)
141 {
142 #ifndef Q_OS_WASM
143 /* Detection via ffprobe (if available) */
144 if (haveFFprobe == -1) {
145 // check once whether we can run ffprobe
146 QProcess prc;
147 prc.startCommand("ffprobe --help");
148 prc.waitForFinished(1000);
149 haveFFprobe = (prc.exitStatus() == QProcess::NormalExit && prc.exitCode() == 0);
150 LOG_DEBUG("ffprobe available: %d (exit status %d, exit code %d)", haveFFprobe,
151 prc.exitStatus() == QProcess::NormalExit ? 0 : 1, prc.exitCode());
152 }
153 if (haveFFprobe) {
154 QProcess prc;
155 prc.setProgram("ffprobe");
156 QStringList args;
157 args.append("-of");
158 args.append("json");
159 args.append("-show_format");
160 args.append("-show_streams");
161 args.append(url.toString());
162 prc.setArguments(args);
163 prc.start();
164 prc.waitForFinished(5000);
165 if (prc.exitStatus() == QProcess::NormalExit && prc.exitCode() == 0) {
166 QJsonParseError err;
167 QJsonDocument doc = QJsonDocument::fromJson(prc.readAllStandardOutput(), &err);
168 if (doc.isNull()) {
169 LOG_DEBUG("cannot parse ffprobe json output: %s", qPrintable(err.errorString()));
170 } else {
171 // First check 3dtv.at conventions. These are not interpreted by FFmpeg.
172 // See https://www.3dtv.at/Knowhow/StereoWmvSpec_en.aspx
173 QString layout = doc["format"]["tags"]["StereoscopicLayout"].toString();
174 QString halfWidth = doc["format"]["tags"]["StereoscopicHalfWidth"].toString();
175 QString halfHeight = doc["format"]["tags"]["StereoscopicHalfHeight"].toString();
176 if (layout == "SideBySideRF")
177 defaultInputMode = (halfWidth == "1") ? Input_Right_Left_Half : Input_Right_Left;
178 else if (layout == "SideBySideLF")
179 defaultInputMode = (halfWidth == "1") ? Input_Left_Right_Half : Input_Left_Right;
180 else if (layout == "OverUnderRT")
181 defaultInputMode = (halfHeight == "1") ? Input_Bottom_Top_Half : Input_Bottom_Top;
182 else if (layout == "OverUnderLT")
183 defaultInputMode = (halfHeight == "1") ? Input_Top_Bottom_Half : Input_Top_Bottom;
184 // FFmpeg metadata, per stream, see libavutil/stereo3d.h and libavutil/spherical.h
185 QJsonValue streamArray = doc["streams"];
186 if (!streamArray.isNull() && !streamArray.isUndefined()) {
187 for (int i = 0; ; i++) {
188 QString codecType = streamArray[i]["codec_type"].toString();
189 if (codecType.isEmpty()) {
190 break;
191 } else if (codecType == "video") {
192 inputModes.append(defaultInputMode);
193 surroundModes.append(defaultSurroundMode);
194 QJsonValue sideDataArray = streamArray[i]["side_data_list"];
195 if (!sideDataArray.isNull() && !sideDataArray.isUndefined()) {
196 for (int j = 0; ; j++) {
197 QString type = sideDataArray[j]["side_data_type"].toString();
198 if (type.isEmpty()) {
199 break;
200 } else if (type == "Stereo 3D") {
201 QString layout = sideDataArray[j]["type"].toString();
202 QString inverted = sideDataArray[j]["inverted"].toString();
203 if (layout == "top and bottom") {
204 inputModes.last() = (inverted == "1" ? Input_Bottom_Top : Input_Top_Bottom);
205 } else if (layout == "side by side") {
206 inputModes.last() = (inverted == "1" ? Input_Right_Left : Input_Left_Right);
207 } else if (layout == "frame alternate") {
208 inputModes.last() = (inverted == "1" ? Input_Alternating_RL : Input_Alternating_LR);
209 } else if (layout == "2D") {
210 inputModes.last() = Input_Mono;
211 }
212 } else if (type == "Spherical Mapping") {
213 QString proj = sideDataArray[j]["projection"].toString();
214 if (proj == "equirectangular") {
215 surroundModes.last() = Surround_360;
216 } else if (proj == "half equirectangular") {
217 surroundModes.last() = Surround_180;
218 }
219 }
220 }
221 }
222 LOG_DEBUG("meta data from ffprobe for video stream %d: input mode %s, surround mode %s",
223 int(inputModes.size() - 1),
224 inputModeToString(inputModes.last()), surroundModeToString(surroundModes.last()));
225 }
226 }
227 }
228 }
229 } else {
230 LOG_DEBUG("cannot run ffprobe: exit status %d, exit code %d",
231 prc.exitStatus() == QProcess::NormalExit ? 0 : 1, prc.exitCode());
232 }
233 }
234 #endif
235 }