Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
358
363
367QMediaFormat::QMediaFormat(const QMediaFormat &other) noexcept = default;
368
377QMediaFormat &QMediaFormat::operator=(const QMediaFormat &other) noexcept = default;
378
389// Properties
445{
446 return QPlatformMediaIntegration::instance()->formatInfo()->isSupported(*this, mode);
447}
448
454{
455 return QMimeDatabase().mimeTypeForName(QString::fromLatin1(mimeTypeForFormat[fmt + 1]));
456}
457
495{
496 return QPlatformMediaIntegration::instance()->formatInfo()->supportedFileFormats(*this, m);
497}
498
521{
522 return QPlatformMediaIntegration::instance()->formatInfo()->supportedVideoCodecs(*this, m);
523}
524
547{
548 return QPlatformMediaIntegration::instance()->formatInfo()->supportedAudioCodecs(*this, m);
549}
550
560{
561 constexpr const char *descriptions[QMediaFormat::LastFileFormat + 2] = {
562 "Unspecified",
563 "WMV",
564 "AVI",
565 "Matroska",
566 "MPEG-4",
567 "Ogg",
568 "QuickTime",
569 "WebM",
570 // Audio Formats
571 "MPEG-4 Audio",
572 "AAC",
573 "WMA",
574 "MP3",
575 "FLAC",
576 "Wave"
577 };
578 return QString::fromUtf8(descriptions[int(fileFormat) + 1]);
579}
580
590{
591 constexpr const char *descriptions[] = {
592 "Invalid",
593 "MP3",
594 "AAC",
595 "AC3",
596 "EAC3",
597 "FLAC",
598 "DolbyTrueHD",
599 "Opus",
600 "Vorbis",
601 "Wave",
602 "WMA",
603 "ALAC",
604 };
605 return QString::fromUtf8(descriptions[int(codec) + 1]);
606}
607
617{
618 constexpr const char *descriptions[] = {
619 "Invalid",
620 "MPEG1",
621 "MPEG2",
622 "MPEG4",
623 "H264",
624 "H265",
625 "VP8",
626 "VP9",
627 "AV1",
628 "Theora",
629 "WMV",
630 "MotionJPEG"
631 };
632 return QString::fromUtf8(descriptions[int(codec) + 1]);
633}
634
644{
645 constexpr const char *descriptions[QMediaFormat::LastFileFormat + 2] = {
646 "Unspecified File Format",
647 "Windows Media Video",
648 "Audio Video Interleave",
649 "Matroska Multimedia Container",
650 "MPEG-4 Video Container",
651 "Ogg",
652 "QuickTime Container",
653 "WebM",
654 // Audio Formats
655 "MPEG-4 Audio",
656 "AAC",
657 "Windows Media Audio",
658 "MP3",
659 "Free Lossless Audio Codec (FLAC)",
660 "Wave File"
661 };
662 return QString::fromUtf8(descriptions[int(fileFormat) + 1]);
663}
664
674{
675 constexpr const char *descriptions[] = {
676 "Unspecified Audio Codec",
677 "MP3",
678 "Advanced Audio Codec (AAC)",
679 "Dolby Digital (AC3)",
680 "Dolby Digital Plus (E-AC3)",
681 "Free Lossless Audio Codec (FLAC)",
682 "Dolby True HD",
683 "Opus",
684 "Vorbis",
685 "Wave",
686 "Windows Media Audio",
687 "Apple Lossless Audio Codec (ALAC)",
688 };
689 return QString::fromUtf8(descriptions[int(codec) + 1]);
690}
691
701{
702 constexpr const char *descriptions[] = {
703 "Unspecified Video Codec",
704 "MPEG-1 Video",
705 "MPEG-2 Video",
706 "MPEG-4 Video",
707 "H.264",
708 "H.265",
709 "VP8",
710 "VP9",
711 "AV1",
712 "Theora",
713 "Windows Media Video",
714 "MotionJPEG"
715 };
716 return QString::fromUtf8(descriptions[int(codec) + 1]);
717}
718
731{
732 Q_ASSERT(!d);
733 return fmt == other.fmt &&
734 audio == other.audio &&
735 video == other.video;
736}
737
768{
769 const bool requiresVideo = (flags & ResolveFlags::RequiresVideo) != 0;
770
771 if (!requiresVideo)
773
774 // need to adjust the format. Priority is given first to file format, then video codec, then audio codec
775
776 QMediaFormat nullFormat;
777 auto supportedFormats = nullFormat.supportedFileFormats(QMediaFormat::Encode);
778 auto supportedAudioCodecs = nullFormat.supportedAudioCodecs(QMediaFormat::Encode);
779 auto supportedVideoCodecs = nullFormat.supportedVideoCodecs(QMediaFormat::Encode);
780
781 auto bestSupportedFileFormat = [&](QMediaFormat::AudioCodec audio = QMediaFormat::AudioCodec::Unspecified,
783 {
786 f.setVideoCodec(video);
787 auto supportedFormats = f.supportedFileFormats(QMediaFormat::Encode);
788 auto *list = (flags == NoFlags) ? audioFormatPriorityList : videoFormatPriorityList;
790 if (supportedFormats.contains(*list))
791 break;
792 ++list;
793 }
794 return *list;
795 };
796
797 // reset format if it does not support video when video is required
798 if (requiresVideo && this->supportedVideoCodecs(QMediaFormat::Encode).isEmpty())
800
801 // reset non supported formats and codecs
802 if (!supportedFormats.contains(fmt))
804 if (!supportedAudioCodecs.contains(audio))
806 if (!requiresVideo || !supportedVideoCodecs.contains(video))
808
809 if (requiresVideo) {
810 // try finding a file format that is supported
812 fmt = bestSupportedFileFormat(audio, video);
813 // try without the audio codec
815 fmt = bestSupportedFileFormat(QMediaFormat::AudioCodec::Unspecified, video);
816 }
817 // try without the video codec
819 fmt = bestSupportedFileFormat(audio);
820 // give me a format that's supported
822 fmt = bestSupportedFileFormat();
823 // still nothing? Give up
825 *this = {};
826 return;
827 }
828
829 // find a working video codec
830 if (requiresVideo) {
831 // reset the audio codec, so that we won't throw away the video codec
832 // if it is supported (choosing the specified video codec has higher
833 // priority than the specified audio codec)
834 auto a = audio;
836 auto videoCodecs = this->supportedVideoCodecs(QMediaFormat::Encode);
837 if (!videoCodecs.contains(video)) {
838 // not supported, try to find a replacement
839 auto *list = videoPriorityList;
841 if (videoCodecs.contains(*list))
842 break;
843 ++list;
844 }
845 video = *list;
846 }
847 audio = a;
848 } else {
850 }
851
852 // and a working audio codec
853 auto audioCodecs = this->supportedAudioCodecs(QMediaFormat::Encode);
854 if (!audioCodecs.contains(audio)) {
855 auto *list = audioPriorityList;
857 if (audioCodecs.contains(*list))
858 break;
859 ++list;
860 }
861 audio = *list;
862 }
863}
864
882
883#include "moc_qmediaformat.cpp"
\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
void setAudioCodec(AudioCodec codec)
Sets the audio codec to codec.
~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
static QPlatformMediaIntegration * instance()
\inmodule QtCore
Definition qshareddata.h:19
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
Combined button and popup list for selecting options.
QMediaFormat::AudioCodec codec
QMediaFormat::FileFormat fileFormat
GLenum mode
const GLfloat * m
GLboolean GLboolean GLboolean GLboolean a
[7]
GLfloat GLfloat f
GLbitfield flags
GLint GLsizei GLsizei GLenum format
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
QVideoFrameFormat::PixelFormat fmt
QList< int > list
[14]
QSharedPointer< T > other(t)
[5]