Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qmediaformat.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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
4#include "qmediaformat.h"
5#include "private/qplatformmediaintegration_p.h"
6#include "private/qplatformmediaformatinfo_p.h"
7#include <QtCore/qmimedatabase.h>
8
10
29
60namespace {
61
62const char *mimeTypeForFormat[QMediaFormat::LastFileFormat + 2] =
63{
64 "",
65 "video/x-ms-wmv",
66 "video/x-msvideo",
67 "video/x-matroska",
68 "video/mp4",
69 "video/ogg",
70 "video/quicktime",
71 "video/webm",
72 // Audio Formats
73 "audio/mp4",
74 "audio/aac",
75 "audio/x-ms-wma",
76 "audio/mpeg",
77 "audio/flac",
78 "audio/wav"
79};
80
81constexpr QMediaFormat::FileFormat videoFormatPriorityList[] =
82{
91};
92
93constexpr QMediaFormat::FileFormat audioFormatPriorityList[] =
94{
101};
102
103constexpr QMediaFormat::AudioCodec audioPriorityList[] =
104{
116};
117
118constexpr QMediaFormat::VideoCodec videoPriorityList[] =
119{
131};
132
133}
134
136{};
137
139
349// these are non inline to make a possible future addition of a d pointer binary compatible
350
355 : fmt(format)
356{
357}
358
363
367QMediaFormat::QMediaFormat(const QMediaFormat &other) noexcept = default;
368
377QMediaFormat &QMediaFormat::operator=(const QMediaFormat &other) noexcept = default;
378
389// Properties
445{
447}
448
454{
455 return QMimeDatabase().mimeTypeForName(QString::fromLatin1(mimeTypeForFormat[fmt + 1]));
456}
457
459{
462 result = pi->formatInfo();
463 return result;
464}
465
503{
504 auto *fi = formatInfo();
505 return fi != nullptr ? fi->supportedFileFormats(*this, m) : QList<QMediaFormat::FileFormat>{};
506}
507
530{
531 auto *fi = formatInfo();
532 return fi != nullptr ? fi->supportedVideoCodecs(*this, m) : QList<QMediaFormat::VideoCodec>{};
533}
534
557{
558 auto *fi = formatInfo();
559 return fi != nullptr ? fi->supportedAudioCodecs(*this, m) : QList<QMediaFormat::AudioCodec>{};
560}
561
571{
572 constexpr const char *descriptions[QMediaFormat::LastFileFormat + 2] = {
573 "Unspecified",
574 "WMV",
575 "AVI",
576 "Matroska",
577 "MPEG-4",
578 "Ogg",
579 "QuickTime",
580 "WebM",
581 // Audio Formats
582 "MPEG-4 Audio",
583 "AAC",
584 "WMA",
585 "MP3",
586 "FLAC",
587 "Wave"
588 };
589 return QString::fromUtf8(descriptions[int(fileFormat) + 1]);
590}
591
601{
602 constexpr const char *descriptions[] = {
603 "Invalid",
604 "MP3",
605 "AAC",
606 "AC3",
607 "EAC3",
608 "FLAC",
609 "DolbyTrueHD",
610 "Opus",
611 "Vorbis",
612 "Wave",
613 "WMA",
614 "ALAC",
615 };
616 return QString::fromUtf8(descriptions[int(codec) + 1]);
617}
618
628{
629 constexpr const char *descriptions[] = {
630 "Invalid",
631 "MPEG1",
632 "MPEG2",
633 "MPEG4",
634 "H264",
635 "H265",
636 "VP8",
637 "VP9",
638 "AV1",
639 "Theora",
640 "WMV",
641 "MotionJPEG"
642 };
643 return QString::fromUtf8(descriptions[int(codec) + 1]);
644}
645
655{
656 constexpr const char *descriptions[QMediaFormat::LastFileFormat + 2] = {
657 "Unspecified File Format",
658 "Windows Media Video",
659 "Audio Video Interleave",
660 "Matroska Multimedia Container",
661 "MPEG-4 Video Container",
662 "Ogg",
663 "QuickTime Container",
664 "WebM",
665 // Audio Formats
666 "MPEG-4 Audio",
667 "AAC",
668 "Windows Media Audio",
669 "MP3",
670 "Free Lossless Audio Codec (FLAC)",
671 "Wave File"
672 };
673 return QString::fromUtf8(descriptions[int(fileFormat) + 1]);
674}
675
685{
686 constexpr const char *descriptions[] = {
687 "Unspecified Audio Codec",
688 "MP3",
689 "Advanced Audio Codec (AAC)",
690 "Dolby Digital (AC3)",
691 "Dolby Digital Plus (E-AC3)",
692 "Free Lossless Audio Codec (FLAC)",
693 "Dolby True HD",
694 "Opus",
695 "Vorbis",
696 "Wave",
697 "Windows Media Audio",
698 "Apple Lossless Audio Codec (ALAC)",
699 };
700 return QString::fromUtf8(descriptions[int(codec) + 1]);
701}
702
712{
713 constexpr const char *descriptions[] = {
714 "Unspecified Video Codec",
715 "MPEG-1 Video",
716 "MPEG-2 Video",
717 "MPEG-4 Video",
718 "H.264",
719 "H.265",
720 "VP8",
721 "VP9",
722 "AV1",
723 "Theora",
724 "Windows Media Video",
725 "MotionJPEG"
726 };
727 return QString::fromUtf8(descriptions[int(codec) + 1]);
728}
729
742{
743 Q_ASSERT(!d);
744 return fmt == other.fmt &&
745 audio == other.audio &&
746 video == other.video;
747}
748
779{
780 const bool requiresVideo = (flags & ResolveFlags::RequiresVideo) != 0;
781
782 if (!requiresVideo)
784
785 // need to adjust the format. Priority is given first to file format, then video codec, then audio codec
786
787 QMediaFormat nullFormat;
788 auto supportedFormats = nullFormat.supportedFileFormats(QMediaFormat::Encode);
791
792 auto bestSupportedFileFormat = [&](QMediaFormat::AudioCodec audio = QMediaFormat::AudioCodec::Unspecified,
794 {
796 f.setAudioCodec(audio);
797 f.setVideoCodec(video);
798 auto supportedFormats = f.supportedFileFormats(QMediaFormat::Encode);
799 auto *list = (flags == NoFlags) ? audioFormatPriorityList : videoFormatPriorityList;
801 if (supportedFormats.contains(*list))
802 break;
803 ++list;
804 }
805 return *list;
806 };
807
808 // reset format if it does not support video when video is required
809 if (requiresVideo && this->supportedVideoCodecs(QMediaFormat::Encode).isEmpty())
811
812 // reset non supported formats and codecs
813 if (!supportedFormats.contains(fmt))
815 if (!supportedAudioCodecs.contains(audio))
817 if (!requiresVideo || !supportedVideoCodecs.contains(video))
819
820 if (requiresVideo) {
821 // try finding a file format that is supported
823 fmt = bestSupportedFileFormat(audio, video);
824 // try without the audio codec
826 fmt = bestSupportedFileFormat(QMediaFormat::AudioCodec::Unspecified, video);
827 }
828 // try without the video codec
830 fmt = bestSupportedFileFormat(audio);
831 // give me a format that's supported
833 fmt = bestSupportedFileFormat();
834 // still nothing? Give up
836 *this = {};
837 return;
838 }
839
840 // find a working video codec
841 if (requiresVideo) {
842 // reset the audio codec, so that we won't throw away the video codec
843 // if it is supported (choosing the specified video codec has higher
844 // priority than the specified audio codec)
845 auto a = audio;
847 auto videoCodecs = this->supportedVideoCodecs(QMediaFormat::Encode);
848 if (!videoCodecs.contains(video)) {
849 // not supported, try to find a replacement
850 auto *list = videoPriorityList;
852 if (videoCodecs.contains(*list))
853 break;
854 ++list;
855 }
856 video = *list;
857 }
858 audio = a;
859 } else {
861 }
862
863 // and a working audio codec
864 auto audioCodecs = this->supportedAudioCodecs(QMediaFormat::Encode);
865 if (!audioCodecs.contains(audio)) {
866 auto *list = audioPriorityList;
868 if (audioCodecs.contains(*list))
869 break;
870 ++list;
871 }
872 audio = *list;
873 }
874}
875
893
894#include "moc_qmediaformat.cpp"
Definition qlist.h:74
\inmodule QtMultimedia
QMediaFormat & operator=(const QMediaFormat &other) noexcept
Copies other into this QMediaFormat object.
static Q_INVOKABLE QString videoCodecName(VideoCodec codec)
\qmlmethod string QtMultimedia::mediaFormat::videoCodecName(codec) Returns a string based name for co...
void resolveForEncoding(ResolveFlags flags)
Resolves the format, based on flags, to a format that is supported by QMediaRecorder.
AudioCodec
\qmlproperty enumeration QtMultimedia::mediaFormat::fileFormat
static Q_INVOKABLE QString audioCodecName(AudioCodec codec)
\qmlmethod string QtMultimedia::mediaFormat::audioCodecName(codec) Returns a string based name for co...
Q_INVOKABLE QList< VideoCodec > supportedVideoCodecs(ConversionMode m)
\qmlmethod list<VideoCodec> QtMultimedia::mediaFormat::supportedVideoCodecs(conversionMode) Returns a...
FileFormat
Describes the container format used in a multimedia file or stream.
Q_INVOKABLE bool isSupported(ConversionMode mode) const
Returns true if Qt Multimedia can encode or decode this format, depending on mode.
FileFormat fmt
VideoCodec video
QExplicitlySharedDataPointer< QMediaFormatPrivate > d
QMediaFormat(FileFormat format=UnspecifiedFormat)
\qmlproperty enumeration QtMultimedia::mediaFormat::videoCodec
~QMediaFormat()
Destroys the QMediaFormat object.
ConversionMode
In many cases, systems have asymmetric capabilities and can often decode more formats or codecs than ...
static Q_INVOKABLE QString videoCodecDescription(QMediaFormat::VideoCodec codec)
\qmlmethod string QtMultimedia::mediaFormat::videoCodecDescription(codec) Returns a description for c...
VideoCodec
\qmlproperty enumeration QtMultimedia::mediaFormat::audioCodec
QMimeType mimeType() const
Returns the \l{MIME type} for the file format used in this media format.
static Q_INVOKABLE QString fileFormatDescription(QMediaFormat::FileFormat fileFormat)
\qmlmethod string QtMultimedia::mediaFormat::fileFormatDescription(fileFormat) Returns a description ...
Q_INVOKABLE QList< AudioCodec > supportedAudioCodecs(ConversionMode m)
\qmlmethod list<AudioCodec> QtMultimedia::mediaFormat::supportedAudioFormats(conversionMode) Returns ...
ResolveFlags
Describes the requirements for resolving a suitable format for QMediaRecorder.
static Q_INVOKABLE QString audioCodecDescription(QMediaFormat::AudioCodec codec)
\qmlmethod string QtMultimedia::mediaFormat::audioCodecDescription(codec) Returns a description for c...
FileFormat fileFormat
The file (container) format of the media.
Q_INVOKABLE QList< FileFormat > supportedFileFormats(ConversionMode m)
\qmlmethod list<FileFormat> QtMultimedia::mediaFormat::supportedFileFormats(conversionMode) Returns a...
AudioCodec audio
bool operator==(const QMediaFormat &other) const
Returns true if other is equal to the current media format, otherwise returns false.
static Q_INVOKABLE QString fileFormatName(FileFormat fileFormat)
\qmlmethod string QtMultimedia::mediaFormat::fileFormatName(fileFormat) Returns a string based name f...
\inmodule QtCore
QMimeType mimeTypeForName(const QString &nameOrAlias) const
Returns a MIME type for nameOrAlias or an invalid one if none found.
\inmodule QtCore
Definition qmimetype.h:25
bool isSupported(const QMediaFormat &format, QMediaFormat::ConversionMode m) const
static QPlatformMediaIntegration * instance()
virtual QPlatformMediaFormatInfo * formatInfo()=0
\inmodule QtCore
Definition qshareddata.h:19
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5857
double pi
[0]
Combined button and popup list for selecting options.
QMediaFormat::AudioCodec codec
QMediaFormat::FileFormat fileFormat
static QPlatformMediaFormatInfo * formatInfo()
GLenum mode
const GLfloat * m
GLboolean GLboolean GLboolean GLboolean a
[7]
GLfloat GLfloat f
GLbitfield flags
GLint GLsizei GLsizei GLenum format
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
QVideoFrameFormat::PixelFormat fmt
QList< int > list
[14]
QFileInfo fi("c:/temp/foo")
[newstuff]
QSharedPointer< T > other(t)
[5]