Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgstreamermetadata.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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 <QtMultimedia/qmediametadata.h>
6#include <QtMultimedia/qtvideo.h>
7#include <QtCore/qdebug.h>
8#include <QtCore/qdatetime.h>
9#include <QtCore/qlocale.h>
10#include <QtCore/qtimezone.h>
11#include <QtGui/qimage.h>
12
13#include <gst/gstversion.h>
14#include <common/qgst_handle_types_p.h>
15#include <common/qgstutils_p.h>
16#include <qgstreamerformatinfo_p.h>
17
19
20RotationResult parseRotationTag(std::string_view tag)
21{
22 using namespace std::string_view_literals;
23 Q_ASSERT(!tag.empty());
24
25 if (tag[0] == 'r') {
26 if (tag == "rotate-90"sv)
27 return { QtVideo::Rotation::Clockwise90, false };
28 if (tag == "rotate-180"sv)
29 return { QtVideo::Rotation::Clockwise180, false };
30 if (tag == "rotate-270"sv)
31 return { QtVideo::Rotation::Clockwise270, false };
32 if (tag == "rotate-0"sv)
33 return { QtVideo::Rotation::None, false };
34 }
35 if (tag[0] == 'f') {
36 // To flip by horizontal axis is the same as to mirror by vertical axis
37 // and rotate by 180 degrees.
38
39 if (tag == "flip-rotate-90"sv)
40 return { QtVideo::Rotation::Clockwise270, true };
41 if (tag == "flip-rotate-180"sv)
42 return { QtVideo::Rotation::None, true };
43 if (tag == "flip-rotate-270"sv)
44 return { QtVideo::Rotation::Clockwise90, true };
45 if (tag == "flip-rotate-0"sv)
46 return { QtVideo::Rotation::Clockwise180, true };
47 }
48
49 qCritical() << "cannot parse orientation: {}" << tag;
50 return { QtVideo::Rotation::None, false };
51}
52
53namespace {
54
56
57#ifdef __cpp_lib_constexpr_algorithms
58# define constexpr_lookup constexpr
59#else
60# define constexpr_lookup /*constexpr*/
61#endif
62
63struct MetadataKeyValuePair
64{
65 const char *tag;
66 QMediaMetaData::Key key;
67};
68
69constexpr const char *toTag(const char *t)
70{
71 return t;
72}
73constexpr const char *toTag(const MetadataKeyValuePair &kv)
74{
75 return kv.tag;
76}
77
78constexpr QMediaMetaData::Key toKey(QMediaMetaData::Key k)
79{
80 return k;
81}
82constexpr QMediaMetaData::Key toKey(const MetadataKeyValuePair &kv)
83{
84 return kv.key;
85}
86
87constexpr auto compareByKey = [](const auto &lhs, const auto &rhs) {
88 return toKey(lhs) < toKey(rhs);
89};
90
91constexpr auto compareByTag = [](const auto &lhs, const auto &rhs) {
92 return std::strcmp(toTag(lhs), toTag(rhs)) < 0;
93};
94
95constexpr_lookup auto makeLookupTable()
96{
97 std::array<MetadataKeyValuePair, 22> lookupTable{ {
98 { GST_TAG_TITLE, QMediaMetaData::Title },
99 { GST_TAG_COMMENT, QMediaMetaData::Comment },
100 { GST_TAG_DESCRIPTION, QMediaMetaData::Description },
101 { GST_TAG_GENRE, QMediaMetaData::Genre },
102 { GST_TAG_DATE_TIME, QMediaMetaData::Date },
103 { GST_TAG_DATE, QMediaMetaData::Date },
104
105 { GST_TAG_LANGUAGE_CODE, QMediaMetaData::Language },
106
107 { GST_TAG_ORGANIZATION, QMediaMetaData::Publisher },
108 { GST_TAG_COPYRIGHT, QMediaMetaData::Copyright },
109
110 // Media
111 { GST_TAG_DURATION, QMediaMetaData::Duration },
112
113 // Audio
114 { GST_TAG_BITRATE, QMediaMetaData::AudioBitRate },
115 { GST_TAG_AUDIO_CODEC, QMediaMetaData::AudioCodec },
116
117 // Music
118 { GST_TAG_ALBUM, QMediaMetaData::AlbumTitle },
119 { GST_TAG_ALBUM_ARTIST, QMediaMetaData::AlbumArtist },
120 { GST_TAG_ARTIST, QMediaMetaData::ContributingArtist },
121 { GST_TAG_TRACK_NUMBER, QMediaMetaData::TrackNumber },
122
123 { GST_TAG_PREVIEW_IMAGE, QMediaMetaData::ThumbnailImage },
124 { GST_TAG_IMAGE, QMediaMetaData::CoverArtImage },
125
126 // Image/Video
127 { "resolution", QMediaMetaData::Resolution },
128 { GST_TAG_IMAGE_ORIENTATION, QMediaMetaData::Orientation },
129
130 // Video
131 { GST_TAG_VIDEO_CODEC, QMediaMetaData::VideoCodec },
132
133 // Movie
134 { GST_TAG_PERFORMER, QMediaMetaData::LeadPerformer },
135 } };
136
137 std::sort(lookupTable.begin(), lookupTable.end(),
138 [](const MetadataKeyValuePair &lhs, const MetadataKeyValuePair &rhs) {
139 return std::string_view(lhs.tag) < std::string_view(rhs.tag);
140 });
141 return lookupTable;
142}
143
144constexpr_lookup auto gstTagToMetaDataKey = makeLookupTable();
145constexpr_lookup auto metaDataKeyToGstTag = [] {
146 auto array = gstTagToMetaDataKey;
147 std::sort(array.begin(), array.end(), compareByKey);
148 return array;
149}();
150
151} // namespace MetadataLookupImpl
152
153QMediaMetaData::Key tagToKey(const char *tag)
154{
155 if (tag == nullptr)
156 return QMediaMetaData::Key(-1);
157
158 using namespace MetadataLookupImpl;
159 auto foundIterator = std::lower_bound(gstTagToMetaDataKey.begin(), gstTagToMetaDataKey.end(),
160 tag, compareByTag);
161 if (std::strcmp(foundIterator->tag, tag) == 0)
162 return foundIterator->key;
163
164 return QMediaMetaData::Key(-1);
165}
166
167const char *keyToTag(QMediaMetaData::Key key)
168{
169 using namespace MetadataLookupImpl;
170 auto foundIterator = std::lower_bound(metaDataKeyToGstTag.begin(), metaDataKeyToGstTag.end(),
171 key, compareByKey);
172 if (foundIterator->key == key)
173 return foundIterator->tag;
174
175 return nullptr;
176}
177
178#undef constexpr_lookup
179
180QDateTime parseDate(const GDate *date)
181{
182 if (!g_date_valid(date))
183 return {};
184
185 int year = g_date_get_year(date);
186 int month = g_date_get_month(date);
187 int day = g_date_get_day(date);
188 return QDateTime(QDate(year, month, day), QTime());
189}
190
191QDateTime parseDate(const GValue &val)
192{
193 Q_ASSERT(G_VALUE_TYPE(&val) == G_TYPE_DATE);
194 const GDate *date = (const GDate *)g_value_get_boxed(&val);
195 return parseDate(date);
196}
197
198QDateTime parseDateTime(const GstDateTime *dateTime)
199{
200 int year = gst_date_time_has_year(dateTime) ? gst_date_time_get_year(dateTime) : 0;
201 int month = gst_date_time_has_month(dateTime) ? gst_date_time_get_month(dateTime) : 0;
202 int day = gst_date_time_has_day(dateTime) ? gst_date_time_get_day(dateTime) : 0;
203 int hour = 0;
204 int minute = 0;
205 int second = 0;
206 float tz = 0;
207 if (gst_date_time_has_time(dateTime)) {
208 hour = gst_date_time_get_hour(dateTime);
209 minute = gst_date_time_get_minute(dateTime);
210 second = gst_date_time_get_second(dateTime);
211 tz = gst_date_time_get_time_zone_offset(dateTime);
212 }
213 return QDateTime{
214 QDate(year, month, day),
215 QTime(hour, minute, second),
216 QTimeZone(tz * 60 * 60),
217 };
218}
219
220QDateTime parseDateTime(const GValue &val)
221{
222 Q_ASSERT(G_VALUE_TYPE(&val) == GST_TYPE_DATE_TIME);
223 const GstDateTime *dateTime = (const GstDateTime *)g_value_get_boxed(&val);
224 return parseDateTime(dateTime);
225}
226
227QImage parseImage(const GValue &val)
228{
229 Q_ASSERT(G_VALUE_TYPE(&val) == GST_TYPE_SAMPLE);
230
231 GstSample *sample = (GstSample *)g_value_get_boxed(&val);
232 GstCaps *caps = gst_sample_get_caps(sample);
233 if (caps && !gst_caps_is_empty(caps)) {
234 GstStructure *structure = gst_caps_get_structure(caps, 0);
235 const gchar *name = gst_structure_get_name(structure);
236 if (QByteArray(name).startsWith("image/")) {
237 GstBuffer *buffer = gst_sample_get_buffer(sample);
238 if (buffer) {
239 GstMapInfo info;
240 gst_buffer_map(buffer, &info, GST_MAP_READ);
241 QImage image = QImage::fromData(info.data, info.size, name);
242 gst_buffer_unmap(buffer, &info);
243 return image;
244 }
245 }
246 }
247
248 return {};
249}
250
251std::optional<double> parseFractionAsDouble(const GValue &val)
252{
253 Q_ASSERT(G_VALUE_TYPE(&val) == GST_TYPE_FRACTION);
254
255 int nom = gst_value_get_fraction_numerator(&val);
256 int denom = gst_value_get_fraction_denominator(&val);
257 if (denom == 0)
258 return std::nullopt;
259 return double(nom) / double(denom);
260}
261
262constexpr std::string_view extendedComment{ GST_TAG_EXTENDED_COMMENT };
263
264void addTagsFromExtendedComment(const GstTagList *list, const gchar *tag, QMediaMetaData &metadata)
265{
266 using namespace Qt::Literals;
267 assert(tag == extendedComment);
268
269 int entryCount = gst_tag_list_get_tag_size(list, tag);
270 for (int i = 0; i != entryCount; ++i) {
271 const GValue *value = gst_tag_list_get_value_index(list, tag, i);
272
273 const QLatin1StringView strValue{ g_value_get_string(value) };
274
275 auto equalIndex = strValue.indexOf(QLatin1StringView("="));
276 if (equalIndex == -1) {
277 qDebug() << "Cannot parse GST_TAG_EXTENDED_COMMENT entry: " << value;
278 continue;
279 }
280
281 const QLatin1StringView key = strValue.first(equalIndex);
282 const QLatin1StringView valueString = strValue.last(strValue.size() - equalIndex - 1);
283
284 if (key == "DURATION"_L1) {
285 QGstDateTimeHandle duration{
286 gst_date_time_new_from_iso8601_string(valueString.data()),
287 QGstDateTimeHandle::HasRef,
288 };
289
290 if (duration) {
291 using namespace std::chrono;
292
293 auto chronoDuration = hours(gst_date_time_get_hour(duration.get()))
294 + minutes(gst_date_time_get_minute(duration.get()))
295 + seconds(gst_date_time_get_second(duration.get()))
296 + microseconds(gst_date_time_get_microsecond(duration.get()));
297
298 metadata.insert(QMediaMetaData::Duration,
299 QVariant::fromValue(round<milliseconds>(chronoDuration).count()));
300 }
301 }
302 }
303}
304
305void addTagToMetaData(const GstTagList *list, const gchar *tag, void *userdata)
306{
307 QMediaMetaData &metadata = *reinterpret_cast<QMediaMetaData *>(userdata);
308
309 QMediaMetaData::Key key = tagToKey(tag);
310 if (key == QMediaMetaData::Key::Date)
311 return; // date/datetime are handled on a higher layer
312
313 if (key == QMediaMetaData::Key(-1)) {
314 if (tag == extendedComment)
315 addTagsFromExtendedComment(list, tag, metadata);
316
317 return;
318 }
319
320 GValue val{};
321 gst_tag_list_copy_value(&val, list, tag);
322
323 GType type = G_VALUE_TYPE(&val);
324
325 if (auto entryCount = gst_tag_list_get_tag_size(list, tag) != 0; entryCount != 1)
326 qWarning() << "addTagToMetaData: invaled entry count for" << tag << "-" << entryCount;
327
328 if (type == G_TYPE_STRING) {
329 const gchar *str_value = g_value_get_string(&val);
330
331 switch (key) {
332 case QMediaMetaData::Language: {
333 metadata.insert(key, QVariant::fromValue(QGstUtils::codeToLanguage(str_value)));
334 break;
335 }
336 case QMediaMetaData::Orientation: {
337 RotationResult result = parseRotationTag(str_value);
338 metadata.insert(key, QVariant::fromValue(result.rotation));
339 break;
340 }
341 default:
342 metadata.insert(key, QString::fromUtf8(str_value));
343 break;
344 };
345 } else if (type == G_TYPE_INT) {
346 metadata.insert(key, g_value_get_int(&val));
347 } else if (type == G_TYPE_UINT) {
348 metadata.insert(key, g_value_get_uint(&val));
349 } else if (type == G_TYPE_LONG) {
350 metadata.insert(key, qint64(g_value_get_long(&val)));
351 } else if (type == G_TYPE_BOOLEAN) {
352 metadata.insert(key, g_value_get_boolean(&val));
353 } else if (type == G_TYPE_CHAR) {
354 metadata.insert(key, g_value_get_schar(&val));
355 } else if (type == G_TYPE_DOUBLE) {
356 metadata.insert(key, g_value_get_double(&val));
357 } else if (type == G_TYPE_DATE) {
358 if (!metadata.keys().contains(key)) {
359 QDateTime date = parseDate(val);
360 if (date.isValid())
361 metadata.insert(key, date);
362 }
363 } else if (type == GST_TYPE_DATE_TIME) {
364 QDateTime date = parseDateTime(val);
365 if (date.isValid())
366 metadata.insert(key, parseDateTime(val));
367 } else if (type == GST_TYPE_SAMPLE) {
368 QImage image = parseImage(val);
369 if (!image.isNull())
370 metadata.insert(key, image);
371 } else if (type == GST_TYPE_FRACTION) {
372 std::optional<double> fraction = parseFractionAsDouble(val);
373
374 if (fraction)
375 metadata.insert(key, *fraction);
376 }
377
378 g_value_unset(&val);
379}
380
381} // namespace
382
383QMediaMetaData taglistToMetaData(const QGstTagListHandle &handle)
384{
386 extendMetaDataFromTagList(m, handle);
387 return m;
388}
389
390void extendMetaDataFromTagList(QMediaMetaData &metadata, const QGstTagListHandle &handle)
391{
392 if (handle) {
393 // gstreamer has both GST_TAG_DATE_TIME and GST_TAG_DATE tags.
394 // if both are present, we use GST_TAG_DATE_TIME, else we fall back to GST_TAG_DATE
395
396 auto readDateTime = [&]() -> std::optional<QDateTime> {
397 GstDateTime *dateTimeHandle{};
398 gst_tag_list_get_date_time(handle.get(), GST_TAG_DATE_TIME, &dateTimeHandle);
399 if (dateTimeHandle) {
400 QDateTime ret = parseDateTime(dateTimeHandle);
401 gst_date_time_unref(dateTimeHandle);
402 if (ret.isValid())
403 return ret;
404 }
405 return std::nullopt;
406 };
407
408 auto readDate = [&]() -> std::optional<QDateTime> {
409 GDate *dateHandle{};
410 gst_tag_list_get_date(handle.get(), GST_TAG_DATE, &dateHandle);
411 if (dateHandle) {
412 QDateTime ret = parseDate(dateHandle);
413 g_date_free(dateHandle);
414 if (ret.isValid())
415 return ret;
416 }
417 return std::nullopt;
418 };
419
420 std::optional<QDateTime> date = readDateTime();
421 if (!date)
422 date = readDate();
423
424 if (date)
425 metadata.insert(QMediaMetaData::Key::Date, *date);
426
427 gst_tag_list_foreach(handle.get(), reinterpret_cast<GstTagForeachFunc>(&addTagToMetaData),
428 &metadata);
429 }
430}
431
432static void applyMetaDataToTagSetter(const QMediaMetaData &metadata, GstTagSetter *element)
433{
434 gst_tag_setter_reset_tags(element);
435
436 for (QMediaMetaData::Key key : metadata.keys()) {
437 const char *tagName = keyToTag(key);
438 if (!tagName)
439 continue;
440 const QVariant &tagValue = metadata.value(key);
441
442 auto setTag = [&](const auto &value) {
443 gst_tag_setter_add_tags(element, GST_TAG_MERGE_REPLACE, tagName, value, nullptr);
444 };
445
446 switch (tagValue.typeId()) {
447 case QMetaType::QString:
448 setTag(tagValue.toString().toUtf8().constData());
449 break;
450 case QMetaType::Int:
451 case QMetaType::LongLong:
452 setTag(tagValue.toInt());
453 break;
454 case QMetaType::Double:
455 setTag(tagValue.toDouble());
456 break;
457
458 case QMetaType::QDateTime: {
459 // tagName does not properly disambiguate between GST_TAG_DATE_TIME and
460 // GST_TAG_DATE, as both map to QMediaMetaData::Key::Date. so we set it accordingly to
461 // the QVariant.
462
463 QDateTime date = tagValue.toDateTime();
464
465 QGstGstDateTimeHandle dateTime{
466 gst_date_time_new(date.offsetFromUtc() / 60. / 60., date.date().year(),
467 date.date().month(), date.date().day(), date.time().hour(),
468 date.time().minute(), date.time().second()),
469 QGstGstDateTimeHandle::HasRef,
470 };
471
472 gst_tag_setter_add_tags(element, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
473 dateTime.get(), nullptr);
474 break;
475 }
476 case QMetaType::QDate: {
477 QDate date = tagValue.toDate();
478
479 QUniqueGDateHandle dateHandle{
480 g_date_new_dmy(date.day(), GDateMonth(date.month()), date.year()),
481 };
482
483 gst_tag_setter_add_tags(element, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, dateHandle.get(),
484 nullptr);
485 break;
486 }
487 default: {
488 if (tagValue.typeId() == qMetaTypeId<QLocale::Language>()) {
489 QByteArray language = QLocale::languageToCode(tagValue.value<QLocale::Language>(),
490 QLocale::ISO639Part2)
491 .toUtf8();
492 setTag(language.constData());
493 }
494
495 break;
496 }
497 }
498 }
499}
500
501void applyMetaDataToTagSetter(const QMediaMetaData &metadata, const QGstElement &element)
502{
503 GstTagSetter *tagSetter = qGstSafeCast<GstTagSetter>(element.element());
504 if (tagSetter)
505 applyMetaDataToTagSetter(metadata, tagSetter);
506 else
507 qWarning() << "applyMetaDataToTagSetter failed: element not a GstTagSetter"
508 << element.name();
509}
510
511void applyMetaDataToTagSetter(const QMediaMetaData &metadata, const QGstBin &bin)
512{
513 GstIterator *elements = gst_bin_iterate_all_by_interface(bin.bin(), GST_TYPE_TAG_SETTER);
514 GValue item = {};
515
516 while (gst_iterator_next(elements, &item) == GST_ITERATOR_OK) {
517 GstElement *element = static_cast<GstElement *>(g_value_get_object(&item));
518 if (!element)
519 continue;
520
521 GstTagSetter *tagSetter = qGstSafeCast<GstTagSetter>(element);
522
523 if (tagSetter)
524 applyMetaDataToTagSetter(metadata, tagSetter);
525 }
526
527 gst_iterator_free(elements);
528}
529
530void extendMetaDataFromCaps(QMediaMetaData &metadata, const QGstCaps &caps)
531{
532 QGstStructureView structure = caps.at(0);
533
534 QMediaFormat::FileFormat fileFormat = QGstreamerFormatInfo::fileFormatForCaps(structure);
535 if (fileFormat != QMediaFormat::FileFormat::UnspecifiedFormat) {
536 // Container caps
537 metadata.insert(QMediaMetaData::FileFormat, fileFormat);
538 return;
539 }
540
541 QMediaFormat::AudioCodec audioCodec = QGstreamerFormatInfo::audioCodecForCaps(structure);
542 if (audioCodec != QMediaFormat::AudioCodec::Unspecified) {
543 // Audio stream caps
544 metadata.insert(QMediaMetaData::AudioCodec, QVariant::fromValue(audioCodec));
545 return;
546 }
547
548 QMediaFormat::VideoCodec videoCodec = QGstreamerFormatInfo::videoCodecForCaps(structure);
549 if (videoCodec != QMediaFormat::VideoCodec::Unspecified) {
550 // Video stream caps
551 metadata.insert(QMediaMetaData::VideoCodec, QVariant::fromValue(videoCodec));
552 std::optional<float> framerate = structure["framerate"].getFraction();
553 if (framerate)
554 metadata.insert(QMediaMetaData::VideoFrameRate, *framerate);
555
556 QSize resolution = structure.resolution();
557 if (resolution.isValid())
558 metadata.insert(QMediaMetaData::Resolution, resolution);
559 }
560}
561
563{
564 QMediaMetaData metadata;
565 extendMetaDataFromCaps(metadata, caps);
566 return metadata;
567}
568
569QT_END_NAMESPACE
\inmodule QtMultimedia
void extendMetaDataFromTagList(QMediaMetaData &metadata, const QGstTagListHandle &handle)
QMediaMetaData taglistToMetaData(const QGstTagListHandle &handle)
QMediaMetaData capsToMetaData(const QGstCaps &caps)
QT_BEGIN_NAMESPACE RotationResult parseRotationTag(std::string_view tag)
#define constexpr_lookup
void applyMetaDataToTagSetter(const QMediaMetaData &metadata, const QGstElement &element)
void applyMetaDataToTagSetter(const QMediaMetaData &metadata, const QGstBin &bin)
static void applyMetaDataToTagSetter(const QMediaMetaData &metadata, GstTagSetter *element)
void extendMetaDataFromCaps(QMediaMetaData &metadata, const QGstCaps &caps)