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>
13#include <gst/gstversion.h>
14#include <common/qgst_handle_types_p.h>
15#include <common/qgstutils_p.h>
16#include <qgstreamerformatinfo_p.h>
22 using namespace std::string_view_literals;
23 Q_ASSERT(!tag.empty());
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 };
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 };
49 qCritical() <<
"cannot parse orientation: {}" << tag;
50 return { QtVideo::Rotation::None,
false };
57#ifdef __cpp_lib_constexpr_algorithms
58# define constexpr_lookup constexpr
60# define constexpr_lookup
63struct MetadataKeyValuePair
66 QMediaMetaData::Key key;
69constexpr const char *toTag(
const char *t)
73constexpr const char *toTag(
const MetadataKeyValuePair &kv)
78constexpr QMediaMetaData::Key toKey(QMediaMetaData::Key k)
82constexpr QMediaMetaData::Key toKey(
const MetadataKeyValuePair &kv)
87constexpr auto compareByKey = [](
const auto &lhs,
const auto &rhs) {
88 return toKey(lhs) < toKey(rhs);
91constexpr auto compareByTag = [](
const auto &lhs,
const auto &rhs) {
92 return std::strcmp(toTag(lhs), toTag(rhs)) < 0;
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 },
105 { GST_TAG_LANGUAGE_CODE, QMediaMetaData::Language },
107 { GST_TAG_ORGANIZATION, QMediaMetaData::Publisher },
108 { GST_TAG_COPYRIGHT, QMediaMetaData::Copyright },
111 { GST_TAG_DURATION, QMediaMetaData::Duration },
114 { GST_TAG_BITRATE, QMediaMetaData::AudioBitRate },
115 { GST_TAG_AUDIO_CODEC, QMediaMetaData::AudioCodec },
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 },
123 { GST_TAG_PREVIEW_IMAGE, QMediaMetaData::ThumbnailImage },
124 { GST_TAG_IMAGE, QMediaMetaData::CoverArtImage },
127 {
"resolution", QMediaMetaData::Resolution },
128 { GST_TAG_IMAGE_ORIENTATION, QMediaMetaData::Orientation },
131 { GST_TAG_VIDEO_CODEC, QMediaMetaData::VideoCodec },
134 { GST_TAG_PERFORMER, QMediaMetaData::LeadPerformer },
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);
146 auto array = gstTagToMetaDataKey;
147 std::sort(array.begin(), array.end(), compareByKey);
153QMediaMetaData::Key tagToKey(
const char *tag)
156 return QMediaMetaData::Key(-1);
159 auto foundIterator =
std::lower_bound(gstTagToMetaDataKey.begin(), gstTagToMetaDataKey.end(),
161 if (std::strcmp(foundIterator->tag, tag) == 0)
162 return foundIterator->key;
164 return QMediaMetaData::Key(-1);
167const char *keyToTag(QMediaMetaData::Key key)
170 auto foundIterator =
std::lower_bound(metaDataKeyToGstTag.begin(), metaDataKeyToGstTag.end(),
172 if (foundIterator->key == key)
173 return foundIterator->tag;
178#undef constexpr_lookup
180QDateTime parseDate(
const GDate *date)
182 if (!g_date_valid(date))
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());
191QDateTime parseDate(
const GValue &val)
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);
198QDateTime parseDateTime(
const GstDateTime *dateTime)
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;
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);
214 QDate(year, month, day),
215 QTime(hour, minute, second),
216 QTimeZone(tz * 60 * 60),
220QDateTime parseDateTime(
const GValue &val)
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);
227QImage parseImage(
const GValue &val)
229 Q_ASSERT(G_VALUE_TYPE(&val) == GST_TYPE_SAMPLE);
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);
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);
251std::optional<
double> parseFractionAsDouble(
const GValue &val)
253 Q_ASSERT(G_VALUE_TYPE(&val) == GST_TYPE_FRACTION);
255 int nom = gst_value_get_fraction_numerator(&val);
256 int denom = gst_value_get_fraction_denominator(&val);
259 return double(nom) /
double(denom);
262constexpr std::string_view extendedComment{ GST_TAG_EXTENDED_COMMENT };
264void addTagsFromExtendedComment(
const GstTagList *list,
const gchar *tag, QMediaMetaData &metadata)
266 using namespace Qt::Literals;
267 assert(tag == extendedComment);
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);
273 const QLatin1StringView strValue{ g_value_get_string(value) };
275 auto equalIndex = strValue.indexOf(QLatin1StringView(
"="));
276 if (equalIndex == -1) {
277 qDebug() <<
"Cannot parse GST_TAG_EXTENDED_COMMENT entry: " << value;
281 const QLatin1StringView key = strValue.first(equalIndex);
282 const QLatin1StringView valueString = strValue.last(strValue.size() - equalIndex - 1);
284 if (key ==
"DURATION"_L1) {
285 QGstDateTimeHandle duration{
286 gst_date_time_new_from_iso8601_string(valueString.data()),
287 QGstDateTimeHandle::HasRef,
291 using namespace std::chrono;
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()));
298 metadata.insert(QMediaMetaData::Duration,
299 QVariant::fromValue(round<milliseconds>(chronoDuration).count()));
305void addTagToMetaData(
const GstTagList *list,
const gchar *tag,
void *userdata)
307 QMediaMetaData &metadata = *
reinterpret_cast<QMediaMetaData *>(userdata);
309 QMediaMetaData::Key key = tagToKey(tag);
310 if (key == QMediaMetaData::Key::Date)
313 if (key == QMediaMetaData::Key(-1)) {
314 if (tag == extendedComment)
315 addTagsFromExtendedComment(list, tag, metadata);
321 gst_tag_list_copy_value(&val, list, tag);
323 GType type = G_VALUE_TYPE(&val);
325 if (
auto entryCount = gst_tag_list_get_tag_size(list, tag) != 0; entryCount != 1)
326 qWarning() <<
"addTagToMetaData: invaled entry count for" << tag <<
"-" << entryCount;
328 if (type == G_TYPE_STRING) {
329 const gchar *str_value = g_value_get_string(&val);
332 case QMediaMetaData::Language: {
333 metadata.insert(key, QVariant::fromValue(QGstUtils::codeToLanguage(str_value)));
336 case QMediaMetaData::Orientation: {
338 metadata.insert(key, QVariant::fromValue(result.rotation));
342 metadata.insert(key, QString::fromUtf8(str_value));
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);
361 metadata.insert(key, date);
363 }
else if (type == GST_TYPE_DATE_TIME) {
364 QDateTime date = parseDateTime(val);
366 metadata.insert(key, parseDateTime(val));
367 }
else if (type == GST_TYPE_SAMPLE) {
368 QImage image = parseImage(val);
370 metadata.insert(key, image);
371 }
else if (type == GST_TYPE_FRACTION) {
372 std::optional<
double> fraction = parseFractionAsDouble(val);
375 metadata.insert(key, *fraction);
386 extendMetaDataFromTagList(m, handle);
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);
408 auto readDate = [&]() -> std::optional<QDateTime> {
410 gst_tag_list_get_date(handle.get(), GST_TAG_DATE, &dateHandle);
412 QDateTime ret = parseDate(dateHandle);
413 g_date_free(dateHandle);
420 std::optional<QDateTime> date = readDateTime();
425 metadata.insert(QMediaMetaData::Key::Date, *date);
427 gst_tag_list_foreach(handle.get(),
reinterpret_cast<GstTagForeachFunc>(&addTagToMetaData),
434 gst_tag_setter_reset_tags(element);
436 for (QMediaMetaData::Key key : metadata.keys()) {
437 const char *tagName = keyToTag(key);
440 const QVariant &tagValue = metadata.value(key);
442 auto setTag = [&](
const auto &value) {
443 gst_tag_setter_add_tags(element, GST_TAG_MERGE_REPLACE, tagName, value,
nullptr);
446 switch (tagValue.typeId()) {
447 case QMetaType::QString:
448 setTag(tagValue.toString().toUtf8().constData());
451 case QMetaType::LongLong:
452 setTag(tagValue.toInt());
454 case QMetaType::Double:
455 setTag(tagValue.toDouble());
458 case QMetaType::QDateTime: {
463 QDateTime date = tagValue.toDateTime();
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,
472 gst_tag_setter_add_tags(element, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
473 dateTime.get(),
nullptr);
476 case QMetaType::QDate: {
477 QDate date = tagValue.toDate();
479 QUniqueGDateHandle dateHandle{
480 g_date_new_dmy(date.day(), GDateMonth(date.month()), date.year()),
483 gst_tag_setter_add_tags(element, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, dateHandle.get(),
488 if (tagValue.typeId() == qMetaTypeId<QLocale::Language>()) {
489 QByteArray language = QLocale::languageToCode(tagValue.value<QLocale::Language>(),
490 QLocale::ISO639Part2)
492 setTag(language.constData());
503 GstTagSetter *tagSetter = qGstSafeCast<GstTagSetter>(element.element());
505 applyMetaDataToTagSetter(metadata, tagSetter);
507 qWarning() <<
"applyMetaDataToTagSetter failed: element not a GstTagSetter"
513 GstIterator *elements = gst_bin_iterate_all_by_interface(bin.bin(), GST_TYPE_TAG_SETTER);
516 while (gst_iterator_next(elements, &item) == GST_ITERATOR_OK) {
517 GstElement *element =
static_cast<GstElement *>(g_value_get_object(&item));
521 GstTagSetter *tagSetter = qGstSafeCast<GstTagSetter>(element);
524 applyMetaDataToTagSetter(metadata, tagSetter);
527 gst_iterator_free(elements);
532 QGstStructureView structure = caps.at(0);
534 QMediaFormat::FileFormat fileFormat = QGstreamerFormatInfo::fileFormatForCaps(structure);
535 if (fileFormat != QMediaFormat::FileFormat::UnspecifiedFormat) {
537 metadata.insert(QMediaMetaData::FileFormat, fileFormat);
541 QMediaFormat::AudioCodec audioCodec = QGstreamerFormatInfo::audioCodecForCaps(structure);
542 if (audioCodec != QMediaFormat::AudioCodec::Unspecified) {
544 metadata.insert(QMediaMetaData::AudioCodec, QVariant::fromValue(audioCodec));
548 QMediaFormat::VideoCodec videoCodec = QGstreamerFormatInfo::videoCodecForCaps(structure);
549 if (videoCodec != QMediaFormat::VideoCodec::Unspecified) {
551 metadata.insert(QMediaMetaData::VideoCodec, QVariant::fromValue(videoCodec));
552 std::optional<
float> framerate = structure[
"framerate"].getFraction();
554 metadata.insert(QMediaMetaData::VideoFrameRate, *framerate);
556 QSize resolution = structure.resolution();
557 if (resolution.isValid())
558 metadata.insert(QMediaMetaData::Resolution, resolution);
565 extendMetaDataFromCaps(metadata, caps);