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 "private/qmultimedia_enum_to_string_converter_p.h"
8
9#include <QtCore/qmimedatabase.h>
10
11QT_BEGIN_NAMESPACE
12
13// clang-format off
14
15struct DescriptionRole{};
16
17QT_MM_MAKE_STRING_RESOLVER(QMediaFormat::FileFormat, QtMultimediaPrivate::EnumName,
18 (QMediaFormat::UnspecifiedFormat, "Unspecified")
19 (QMediaFormat::WMV, "WMV")
20 (QMediaFormat::AVI, "AVI")
21 (QMediaFormat::Matroska, "Matroska")
22 (QMediaFormat::MPEG4, "MPEG-4")
23 (QMediaFormat::Ogg, "Ogg")
24 (QMediaFormat::QuickTime, "QuickTime")
25 (QMediaFormat::WebM, "WebM")
26 // Audio Formats
27 (QMediaFormat::Mpeg4Audio, "MPEG-4 Audio")
28 (QMediaFormat::AAC, "AAC")
29 (QMediaFormat::WMA, "WMA")
30 (QMediaFormat::MP3, "MP3")
31 (QMediaFormat::FLAC, "FLAC")
32 (QMediaFormat::Wave, "Wave")
33 );
34
35QT_MM_MAKE_STRING_RESOLVER(QMediaFormat::FileFormat, DescriptionRole,
36 (QMediaFormat::UnspecifiedFormat, "Unspecified File Format")
37 (QMediaFormat::WMV, "Windows Media Video")
38 (QMediaFormat::AVI, "Audio Video Interleave")
39 (QMediaFormat::Matroska, "Matroska Multimedia Container")
40 (QMediaFormat::MPEG4, "MPEG-4 Video Container")
41 (QMediaFormat::Ogg, "Ogg")
42 (QMediaFormat::QuickTime, "QuickTime Container")
43 (QMediaFormat::WebM, "WebM")
44 // Audio Formats
45 (QMediaFormat::Mpeg4Audio, "MPEG-4 Audio")
46 (QMediaFormat::AAC, "AAC")
47 (QMediaFormat::WMA, "Windows Media Audio")
48 (QMediaFormat::MP3, "MP3")
49 (QMediaFormat::FLAC, "Free Lossless Audio Codec (FLAC)")
50 (QMediaFormat::Wave, "Wave File")
51 );
52
53QT_MM_MAKE_STRING_RESOLVER(QMediaFormat::AudioCodec, QtMultimediaPrivate::EnumName,
54 (QMediaFormat::AudioCodec::Unspecified, "Unspecified")
55 (QMediaFormat::AudioCodec::MP3, "MP3")
56 (QMediaFormat::AudioCodec::AAC, "AAC")
57 (QMediaFormat::AudioCodec::AC3, "AC3")
58 (QMediaFormat::AudioCodec::EAC3, "EAC3")
59 (QMediaFormat::AudioCodec::FLAC, "FLAC")
60 (QMediaFormat::AudioCodec::DolbyTrueHD, "DolbyTrueHD")
61 (QMediaFormat::AudioCodec::Opus, "Opus")
62 (QMediaFormat::AudioCodec::Vorbis, "Vorbis")
63 (QMediaFormat::AudioCodec::Wave, "Wave")
64 (QMediaFormat::AudioCodec::WMA, "WMA")
65 (QMediaFormat::AudioCodec::ALAC, "ALAC")
66 );
67
68QT_MM_MAKE_STRING_RESOLVER(QMediaFormat::AudioCodec, DescriptionRole,
69 (QMediaFormat::AudioCodec::Unspecified, "Unspecified Audio Codec")
70 (QMediaFormat::AudioCodec::MP3, "MP3")
71 (QMediaFormat::AudioCodec::AAC, "Advanced Audio Codec (AAC)")
72 (QMediaFormat::AudioCodec::AC3, "Dolby Digital (AC3)")
73 (QMediaFormat::AudioCodec::EAC3, "Dolby Digital Plus (E-AC3)")
74 (QMediaFormat::AudioCodec::FLAC, "Free Lossless Audio Codec (FLAC)")
75 (QMediaFormat::AudioCodec::DolbyTrueHD, "Dolby True HD")
76 (QMediaFormat::AudioCodec::Opus, "Opus")
77 (QMediaFormat::AudioCodec::Vorbis, "Vorbis")
78 (QMediaFormat::AudioCodec::Wave, "Wave")
79 (QMediaFormat::AudioCodec::WMA, "Windows Media Audio")
80 (QMediaFormat::AudioCodec::ALAC, "Apple Lossless Audio Codec (ALAC)")
81 );
82
83
84QT_MM_MAKE_STRING_RESOLVER(QMediaFormat::VideoCodec, QtMultimediaPrivate::EnumName,
85 (QMediaFormat::VideoCodec::Unspecified, "Unspecified")
86 (QMediaFormat::VideoCodec::MPEG1, "MPEG1")
87 (QMediaFormat::VideoCodec::MPEG2, "MPEG2")
88 (QMediaFormat::VideoCodec::MPEG4, "MPEG4")
89 (QMediaFormat::VideoCodec::H264, "H264")
90 (QMediaFormat::VideoCodec::H265, "H265")
91 (QMediaFormat::VideoCodec::VP8, "VP8")
92 (QMediaFormat::VideoCodec::VP9, "VP9")
93 (QMediaFormat::VideoCodec::AV1, "AV1")
94 (QMediaFormat::VideoCodec::Theora, "Theora")
95 (QMediaFormat::VideoCodec::WMV, "WMV")
96 (QMediaFormat::VideoCodec::MotionJPEG, "MotionJPEG")
97 );
98
99QT_MM_MAKE_STRING_RESOLVER(QMediaFormat::VideoCodec, DescriptionRole,
100 (QMediaFormat::VideoCodec::Unspecified, "Unspecified Video Codec")
101 (QMediaFormat::VideoCodec::MPEG1, "MPEG-1 Video")
102 (QMediaFormat::VideoCodec::MPEG2, "MPEG-2 Video")
103 (QMediaFormat::VideoCodec::MPEG4, "MPEG-4 Video")
104 (QMediaFormat::VideoCodec::H264, "H.264")
105 (QMediaFormat::VideoCodec::H265, "H.265")
106 (QMediaFormat::VideoCodec::VP8, "VP8")
107 (QMediaFormat::VideoCodec::VP9, "VP9")
108 (QMediaFormat::VideoCodec::AV1, "AV1")
109 (QMediaFormat::VideoCodec::Theora, "Theora")
110 (QMediaFormat::VideoCodec::WMV, "Windows Media Video")
111 (QMediaFormat::VideoCodec::MotionJPEG, "MotionJPEG")
112 );
113// clang-format on
114
115/*!
116 \class QMediaFormat
117 \ingroup multimedia
118 \inmodule QtMultimedia
119 \brief Describes an encoding format for a multimedia file or stream.
120 \since 6.2
121
122 QMediaFormat describes an encoding format for a multimedia file or stream.
123
124 You can check whether a certain media format can be used for encoding
125 or decoding using QMediaFormat.
126*/
127
128/*!
129 \qmlvaluetype mediaFormat
130 \ingroup qmlvaluetypes
131 \since 6.2
132 //! \nativetype QMediaFormat
133 \brief MediaFormat describes the format of a media file.
134 \inqmlmodule QtMultimedia
135 \ingroup multimedia_qml
136
137 The MediaFormat type describes the format of a media file. It contains
138 three properties that describe the file type and the audio and video codecs
139 that are being used.
140
141 MediaFormat can be used to specify the type of file that should be created
142 by a MediaRecorder. The snippet below shows an example that sets up the
143 recorder to create an mpeg4 video with AAC encoded audio and H265 video:
144
145 \qml
146 CaptureSession {
147 ... // setup inputs
148 MediaRecorder {
149 mediaFormat {
150 fileFormat: MediaFormat.MPEG4
151 audioCodec: MediaFormat.AudioCodec.AAC
152 videoCodec: MediaFormat.VideoCodec.H265
153 }
154 }
155 }
156 \endqml
157
158 If the specified mediaFormat is not supported, the MediaRecorder will automatically try
159 to find the best possible replacement format and use that instead.
160
161 \sa MediaRecorder, CaptureSession
162*/
163
164namespace {
165
166const char *mimeTypeForFormat[QMediaFormat::LastFileFormat + 2] =
167{
168 "",
169 "video/x-ms-wmv",
170 "video/x-msvideo",
171 "video/x-matroska",
172 "video/mp4",
173 "video/ogg",
174 "video/quicktime",
175 "video/webm",
176 // Audio Formats
177 "audio/mp4",
178 "audio/aac",
179 "audio/x-ms-wma",
180 "audio/mpeg",
181 "audio/flac",
182 "audio/wav"
183};
184
185constexpr QMediaFormat::FileFormat videoFormatPriorityList[] =
186{
187 QMediaFormat::MPEG4,
188 QMediaFormat::QuickTime,
189 QMediaFormat::AVI,
190 QMediaFormat::WebM,
191 QMediaFormat::WMV,
192 QMediaFormat::Matroska,
193 QMediaFormat::Ogg,
194 QMediaFormat::UnspecifiedFormat
195};
196
197constexpr QMediaFormat::FileFormat audioFormatPriorityList[] =
198{
199 QMediaFormat::Mpeg4Audio,
200 QMediaFormat::MP3,
201 QMediaFormat::WMA,
202 QMediaFormat::FLAC,
203 QMediaFormat::Wave,
204 QMediaFormat::UnspecifiedFormat
205};
206
207constexpr QMediaFormat::AudioCodec audioPriorityList[] =
208{
209 QMediaFormat::AudioCodec::AAC,
210 QMediaFormat::AudioCodec::MP3,
211 QMediaFormat::AudioCodec::AC3,
212 QMediaFormat::AudioCodec::Opus,
213 QMediaFormat::AudioCodec::EAC3,
214 QMediaFormat::AudioCodec::DolbyTrueHD,
215 QMediaFormat::AudioCodec::WMA,
216 QMediaFormat::AudioCodec::FLAC,
217 QMediaFormat::AudioCodec::Vorbis,
218 QMediaFormat::AudioCodec::Wave,
219 QMediaFormat::AudioCodec::Unspecified
220};
221
222constexpr QMediaFormat::VideoCodec videoPriorityList[] =
223{
224 QMediaFormat::VideoCodec::H265,
225 QMediaFormat::VideoCodec::VP9,
226 QMediaFormat::VideoCodec::H264,
227 QMediaFormat::VideoCodec::AV1,
228 QMediaFormat::VideoCodec::VP8,
229 QMediaFormat::VideoCodec::WMV,
230 QMediaFormat::VideoCodec::Theora,
231 QMediaFormat::VideoCodec::MPEG4,
232 QMediaFormat::VideoCodec::MPEG2,
233 QMediaFormat::VideoCodec::MPEG1,
234 QMediaFormat::VideoCodec::MotionJPEG,
235};
236
237}
238
239class QMediaFormatPrivate : public QSharedData
240{};
241
242QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QMediaFormatPrivate);
243
244/*! \enum QMediaFormat::FileFormat
245
246 Describes the container format used in a multimedia file or stream.
247
248 \value WMA
249 \l {Windows Media Audio}
250 \value AAC
251 \l{Advanced Audio Coding}
252 \value Matroska
253 \l{Matroska (MKV)}
254 \value WMV
255 \l{Windows Media Video}
256 \value MP3
257 \l{MPEG-1 Audio Layer III or MPEG-2 Audio Layer III}
258 \value Wave
259 \l{Waveform Audio File Format}
260 \value Ogg
261 \l{Ogg}
262 \value MPEG4
263 \l{MPEG-4}
264 \value AVI
265 \l{Audio Video Interleave}
266 \value QuickTime
267 \l{QuickTime}
268 \value WebM
269 \l{WebM}
270 \value Mpeg4Audio
271 \l{MPEG-4 Part 3 or MPEG-4 Audio (formally ISO/IEC 14496-3)}
272 \value FLAC
273 \l{Free Lossless Audio Codec}
274 \value UnspecifiedFormat
275 The format is unspecified.
276
277 \omitvalue LastFileFormat
278*/
279
280/*! \qmlproperty enumeration QtMultimedia::mediaFormat::fileFormat
281
282 Describes the container format used in a multimedia file or stream.
283 It can take one of the following values:
284
285 \table
286 \header \li Property value
287 \li Description
288 \row \li MediaFormat.WMA
289 \li \l{Windows Media Audio}
290 \row \li MediaFormat.AAC
291 \li \l{Advanced Audio Coding}
292 \row \li MediaFormat.Matroska
293 \li \l{Matroska (MKV)}
294 \row \li MediaFormat.WMV
295 \li \l{Windows Media Video}
296 \row \li MediaFormat.MP3
297 \li \l{MPEG-1 Audio Layer III or MPEG-2 Audio Layer III}
298 \row \li MediaFormat.Wave
299 \li \l{Waveform Audio File Format}
300 \row \li MediaFormat.Ogg
301 \li \l{Ogg}
302 \row \li MediaFormat.MPEG4
303 \li \l{MPEG-4}
304 \row \li MediaFormat.AVI
305 \li \l{Audio Video Interleave}
306 \row \li MediaFormat.QuickTime
307 \li \l{QuickTime}
308 \row \li MediaFormat.WebM
309 \li \l{WebM}
310 \row \li MediaFormat.Mpeg4Audio
311 \li \l{MPEG-4 Part 3 or MPEG-4 Audio (formally ISO/IEC 14496-3)}
312 \row \li MediaFormat.FLAC
313 \li \l{Free Lossless Audio Codec}
314 \row \li MediaFormat.UnspecifiedFormat
315 \li The format is unspecified.
316 \endtable
317*/
318
319/*! \enum QMediaFormat::AudioCodec
320
321 Describes the audio codec used in multimedia file or stream.
322
323 \value WMA
324 \l {Windows Media Audio}
325 \value AC3
326 \l {Dolby Digital}
327 \value AAC
328 \l{Advanced Audio Coding}
329 \value ALAC
330 \l{Apple Lossless Audio Codec}
331 \value DolbyTrueHD
332 \l{Dolby TrueHD}
333 \value EAC3
334 \l {Dolby Digital Plus (EAC3)}
335 \value MP3
336 \l{MPEG-1 Audio Layer III or MPEG-2 Audio Layer III}
337 \value Wave
338 \l{Waveform Audio File Format}
339 \value Vorbis
340 \l{Ogg Vorbis}
341 \value FLAC
342 \l{Free Lossless Audio Codec}
343 \value Opus
344 \l{Opus Audio Format}
345 \value Unspecified
346 Unspecified codec
347
348 \omitvalue LastAudioCodec
349*/
350
351/*! \qmlproperty enumeration QtMultimedia::mediaFormat::audioCodec
352
353 Describes the audio codec used in multimedia file or stream.
354 It can take one of the following values:
355
356 \table
357 \header \li Property value
358 \li Description
359 \row \li MediaFormat.WMA
360 \li \l {Windows Media Audio}
361 \row \li MediaFormat.AC3
362 \li \l {Dolby Digital}
363 \row \li MediaFormat.AAC
364 \li \l{Advanced Audio Coding}
365 \row \li MediaFormat.ALAC
366 \li \l{Apple Lossless Audio Codec}
367 \row \li MediaFormat.DolbyTrueHD
368 \li \l{Dolby TrueHD}
369 \row \li MediaFormat.EAC3
370 \li \l {Dolby Digital Plus (EAC3)}
371 \row \li MediaFormat.MP3
372 \li \l{MPEG-1 Audio Layer III or MPEG-2 Audio Layer III}
373 \row \li MediaFormat.Wave
374 \li \l{Waveform Audio File Format}
375 \row \li MediaFormat.Vorbis
376 \li \l{Ogg Vorbis}
377 \row \li MediaFormat.FLAC
378 \li \l{Free Lossless Audio Codec}
379 \row \li MediaFormat.Opus
380 \li \l{Opus Audio Format}
381 \row \li MediaFormat.Unspecified
382 \li Unspecified codec
383 \endtable
384*/
385
386/*! \enum QMediaFormat::VideoCodec
387
388 Describes the video coded used in multimedia file or stream.
389
390 \value VP8
391 \l{VP8}
392 \value MPEG2
393 \l{MPEG-2}
394 \value MPEG1
395 \l{MPEG-1}
396 \value WMV
397 \l{Windows Media Video}
398 \value H265
399 \l{High Efficiency Video Coding (HEVC)}
400 \value H264
401 \l{Advanced Video Coding}
402 \value MPEG4
403 \l{MPEG-4}
404 \value AV1
405 \l{AOMedia Video 1}
406 \value MotionJPEG
407 \l{MotionJPEG}
408 \value VP9
409 \l{VP9}
410 \value Theora
411 \l{Theora}
412 \value Unspecified
413 Video codec not specified
414
415 \omitvalue LastVideoCodec
416*/
417
418/*! \qmlproperty enumeration QtMultimedia::mediaFormat::videoCodec
419
420 Describes the video codec used in multimedia file or stream.
421 It can take one of the following values:
422
423 \table
424 \header \li Property value
425 \li Description
426 \row \li MediaFormat.VP8
427 \li \l{VP8}
428 \row \li MediaFormat.MPEG2
429 \li \l{MPEG-2}
430 \row \li MediaFormat.MPEG1
431 \li \l{MPEG-1}
432 \row \li MediaFormat.WMV
433 \li \l{Windows Media Video}
434 \row \li MediaFormat.H265
435 \li \l{High Efficiency Video Coding (HEVC)}
436 \row \li MediaFormat.H264
437 \li \l{Advanced Video Coding}
438 \row \li MediaFormat.MPEG4
439 \li \l{MPEG-4}
440 \row \li MediaFormat.AV1
441 \li \l{AOMedia Video 1}
442 \row \li MediaFormat.MotionJPEG
443 \li \l{MotionJPEG}
444 \row \li MediaFormat.VP9
445 \li \l{VP9}
446 \row \li MediaFormat.Theora
447 \li \l{Theora}
448 \row \li MediaFormat.Unspecified
449 \li Video codec not specified
450 \endtable
451*/
452
453// these are non inline to make a possible future addition of a d pointer binary compatible
454
455/*!
456 Constructs a QMediaFormat object for \a format.
457*/
458QMediaFormat::QMediaFormat(FileFormat format)
459 : fmt(format)
460{
461}
462
463/*!
464 Destroys the QMediaFormat object.
465*/
466QMediaFormat::~QMediaFormat() = default;
467
468/*!
469 Constructs a QMediaFormat object by copying from \a other.
470*/
471QMediaFormat::QMediaFormat(const QMediaFormat &other) noexcept = default;
472
473/*!
474 \fn void QMediaFormat::swap(QMediaFormat &other) noexcept
475
476 Swaps the media format with \a other.
477*/
478/*!
479 Copies \a other into this QMediaFormat object.
480*/
481QMediaFormat &QMediaFormat::operator=(const QMediaFormat &other) noexcept = default;
482
483/*! \fn QMediaFormat::QMediaFormat(QMediaFormat &&other)
484
485 Constructs a QMediaFormat objects by moving from \a other.
486*/
487
488/*! \fn QMediaFormat &QMediaFormat::operator=(QMediaFormat &&other)
489
490 Moves \a other into this QMediaFormat objects.
491*/
492
493// Properties
494/*! \property QMediaFormat::fileFormat
495
496 \brief The file (container) format of the media.
497
498 \sa QMediaFormat::FileFormat
499*/
500
501/*! \property QMediaFormat::audioCodec
502
503 \brief The audio codec of the media.
504
505 \sa QMediaFormat::AudioCodec
506*/
507
508/*! \property QMediaFormat::videoCodec
509
510 \brief The video codec of the media.
511
512 \sa QMediaFormat::VideoCodec
513*/
514
515/*! \fn void QMediaFormat::setVideoCodec(VideoCodec codec)
516
517 Sets the video codec to \a codec.
518
519 \sa videoCodec(), QMediaFormat::VideoCodec
520*/
521
522/*! \fn QMediaFormat::VideoCodec QMediaFormat::videoCodec() const
523
524 Returns the video codec used in this format.
525
526 \sa setVideoCodec(), QMediaFormat::VideoCodec
527*/
528
529/*! \fn void QMediaFormat::setAudioCodec(AudioCodec codec)
530
531 Sets the audio codec to \a codec.
532
533 \sa audioCodec(), QMediaFormat::AudioCodec
534*/
535
536/*! \fn QMediaFormat::AudioCodec QMediaFormat::audioCodec() const
537
538 Returns the audio codec used in this format.
539
540 \sa setAudioCodec(), QMediaFormat::AudioCodec
541*/
542
543/*!
544 Returns \c true if Qt Multimedia can encode or decode this format,
545 depending on \a mode.
546*/
547
548bool QMediaFormat::isSupported(ConversionMode mode) const
549{
550 return QPlatformMediaIntegration::instance()->formatInfo()->isSupported(format: *this, m: mode);
551}
552
553/*!
554 Returns the \l{MIME type} for the file format used in this media format.
555*/
556
557QMimeType QMediaFormat::mimeType() const
558{
559 return QMimeDatabase().mimeTypeForName(nameOrAlias: QString::fromLatin1(ba: mimeTypeForFormat[fmt + 1]));
560}
561
562/*!
563 \enum QMediaFormat::ConversionMode
564
565 In many cases, systems have asymmetric capabilities and can often decode more formats
566 or codecs than can be encoded. This enum describes the requested conversion mode to
567 be used when checking whether a certain file format or codec is supported.
568
569 \value Encode
570 Used to check whether a certain file format or codec can be encoded.
571 \value Decode
572 Used to check whether a certain file format or codec can be decoded.
573
574 \sa supportedFileFormats, supportedAudioCodecs, supportedVideoCodecs
575*/
576
577/*!
578 \qmlmethod list<FileFormat> QtMultimedia::mediaFormat::supportedFileFormats(conversionMode)
579 Returns a list of file formats for the audio and video
580 codec indicated by \a{conversionMode}.
581
582 To get all supported file formats, run this query on a default constructed MediaFormat. To
583 get a list of file formats supporting a specific combination of an audio and video codec,
584 you can set the audioCodec and videoCodec properties before running this query.
585
586 \sa QMediaFormat::ConversionMode
587*/
588
589/*!
590 Returns a list of file formats for the audio and video
591 codec indicated by \a{m}.
592
593 To get all supported file formats, run this query on a default constructed
594 QMediaFormat.
595
596 \sa QMediaFormat::ConversionMode
597*/
598QList<QMediaFormat::FileFormat> QMediaFormat::supportedFileFormats(QMediaFormat::ConversionMode m)
599{
600 return QPlatformMediaIntegration::instance()->formatInfo()->supportedFileFormats(constraints: *this, m);
601}
602
603/*!
604 \qmlmethod list<VideoCodec> QtMultimedia::mediaFormat::supportedVideoCodecs(conversionMode)
605 Returns a list of video codecs for the chosen file format and
606 audio codec (\a conversionMode).
607
608 To get all supported video codecs, run this query on a default constructed MediaFormat. To
609 get a list of supported video codecs for a specific combination of a file format and an audio
610 codec, you can set the fileFormat and audioCodec properties before running this query.
611
612 \sa QMediaFormat::ConversionMode
613*/
614
615/*!
616 Returns a list of video codecs for the chosen file format and
617 audio codec (\a m).
618
619 To get all supported video codecs, run this query on a default constructed
620 MediaFormat.
621
622 \sa QMediaFormat::ConversionMode
623*/
624QList<QMediaFormat::VideoCodec> QMediaFormat::supportedVideoCodecs(QMediaFormat::ConversionMode m)
625{
626 return QPlatformMediaIntegration::instance()->formatInfo()->supportedVideoCodecs(constraints: *this, m);
627}
628
629/*!
630 \qmlmethod list<AudioCodec> QtMultimedia::mediaFormat::supportedAudioFormats(conversionMode)
631 Returns a list of audio codecs for the chosen file format and
632 video codec (\a conversionMode).
633
634 To get all supported audio codecs, run this query on a default constructed MediaFormat. To get
635 a list of supported audio codecs for a specific combination of a file format and a video codec,
636 you can set the fileFormat and videoCodec properties before running this query.
637
638 \sa QMediaFormat::ConversionMode
639*/
640
641/*!
642 Returns a list of audio codecs for the chosen file format and
643 video codec (\a m).
644
645 To get all supported audio codecs, run this query on a default constructed
646 QMediaFormat.
647
648 \sa QMediaFormat::ConversionMode
649*/
650QList<QMediaFormat::AudioCodec> QMediaFormat::supportedAudioCodecs(QMediaFormat::ConversionMode m)
651{
652 return QPlatformMediaIntegration::instance()->formatInfo()->supportedAudioCodecs(constraints: *this, m);
653}
654
655/*!
656 \qmlmethod string QtMultimedia::mediaFormat::fileFormatName(fileFormat)
657 Returns a string based name for \a fileFormat.
658*/
659
660/*!
661 Returns a string based name for \a fileFormat.
662*/
663QString QMediaFormat::fileFormatName(QMediaFormat::FileFormat fileFormat)
664{
665 return QtMultimediaPrivate::StringResolver<QMediaFormat::FileFormat>::toQString(arg: fileFormat)
666 .value_or(QStringLiteral("Unknown File Format"));
667}
668
669/*!
670 \qmlmethod string QtMultimedia::mediaFormat::audioCodecName(codec)
671 Returns a string based name for \a codec.
672*/
673
674/*!
675 Returns a string based name for \a codec.
676*/
677QString QMediaFormat::audioCodecName(QMediaFormat::AudioCodec codec)
678{
679 return QtMultimediaPrivate::StringResolver<QMediaFormat::AudioCodec>::toQString(arg: codec).value_or(
680 QStringLiteral("Unknown Audio Codec"));
681}
682
683/*!
684 \qmlmethod string QtMultimedia::mediaFormat::videoCodecName(codec)
685 Returns a string based name for \a codec.
686*/
687
688/*!
689 Returns a string based name for \a codec.
690*/
691QString QMediaFormat::videoCodecName(QMediaFormat::VideoCodec codec)
692{
693 return QtMultimediaPrivate::StringResolver<QMediaFormat::VideoCodec>::toQString(arg: codec).value_or(
694 QStringLiteral("Unknown Video Codec"));
695}
696
697/*!
698 \qmlmethod string QtMultimedia::mediaFormat::fileFormatDescription(fileFormat)
699 Returns a description for \a fileFormat.
700*/
701
702/*!
703 Returns a description for \a fileFormat.
704*/
705QString QMediaFormat::fileFormatDescription(QMediaFormat::FileFormat fileFormat)
706{
707 return QtMultimediaPrivate::StringResolver<QMediaFormat::FileFormat,
708 DescriptionRole>::toQString(arg: fileFormat)
709 .value_or(QStringLiteral("Unknown File Format"));
710}
711
712/*!
713 \qmlmethod string QtMultimedia::mediaFormat::audioCodecDescription(codec)
714 Returns a description for \a codec.
715*/
716
717/*!
718 Returns a description for \a codec.
719*/
720QString QMediaFormat::audioCodecDescription(QMediaFormat::AudioCodec codec)
721{
722 return QtMultimediaPrivate::StringResolver<QMediaFormat::AudioCodec,
723 DescriptionRole>::toQString(arg: codec)
724 .value_or(QStringLiteral("Unknown Audio Codec"));
725}
726
727/*!
728 \qmlmethod string QtMultimedia::mediaFormat::videoCodecDescription(codec)
729 Returns a description for \a codec.
730*/
731
732/*!
733 Returns a description for \a codec.
734*/
735QString QMediaFormat::videoCodecDescription(QMediaFormat::VideoCodec codec)
736{
737 return QtMultimediaPrivate::StringResolver<QMediaFormat::VideoCodec,
738 DescriptionRole>::toQString(arg: codec)
739 .value_or(QStringLiteral("Unknown Video Codec"));
740}
741
742/*!
743 \fn bool QMediaFormat::operator!=(const QMediaFormat &other) const
744
745 Returns \c true if \a other is not equal to the current media format,
746 otherwise returns \c false.
747*/
748
749/*!
750 Returns \c true if \a other is equal to the current media format, otherwise
751 returns \c false.
752*/
753bool QMediaFormat::operator==(const QMediaFormat &other) const
754{
755 Q_ASSERT(!d);
756 return fmt == other.fmt &&
757 audio == other.audio &&
758 video == other.video;
759}
760
761/*!
762 \enum QMediaFormat::ResolveFlags
763
764 Describes the requirements for resolving a suitable format for
765 QMediaRecorder.
766
767 \value NoFlags
768 No requirements
769 \value RequiresVideo
770 A video codec is required
771
772 \sa resolveForEncoding()
773*/
774
775/*!
776 Resolves the format, based on \a flags, to a format that is supported by
777 QMediaRecorder.
778
779 This method tries to find the best possible match for unspecified settings.
780 Settings that are not supported by the recorder will be modified to the closest
781 match that is supported.
782
783 When resolving, priority is given in the following order:
784 \list 1
785 \li File format
786 \li Video codec
787 \li Audio codec
788 \endlist
789*/
790void QMediaFormat::resolveForEncoding(ResolveFlags flags)
791{
792 const bool requiresVideo = (flags & ResolveFlags::RequiresVideo) != 0;
793
794 if (!requiresVideo)
795 video = VideoCodec::Unspecified;
796
797 // need to adjust the format. Priority is given first to file format, then video codec, then audio codec
798
799 QMediaFormat nullFormat;
800 auto supportedFormats = nullFormat.supportedFileFormats(m: QMediaFormat::Encode);
801 auto supportedAudioCodecs = nullFormat.supportedAudioCodecs(m: QMediaFormat::Encode);
802 auto supportedVideoCodecs = nullFormat.supportedVideoCodecs(m: QMediaFormat::Encode);
803
804 auto bestSupportedFileFormat = [&](QMediaFormat::AudioCodec audio = QMediaFormat::AudioCodec::Unspecified,
805 QMediaFormat::VideoCodec video = QMediaFormat::VideoCodec::Unspecified)
806 {
807 QMediaFormat f;
808 f.setAudioCodec(audio);
809 f.setVideoCodec(video);
810 auto supportedFormats = f.supportedFileFormats(m: QMediaFormat::Encode);
811 auto *list = (flags == NoFlags) ? audioFormatPriorityList : videoFormatPriorityList;
812 while (*list != QMediaFormat::UnspecifiedFormat) {
813 if (supportedFormats.contains(t: *list))
814 break;
815 ++list;
816 }
817 return *list;
818 };
819
820 // reset format if it does not support video when video is required
821 if (requiresVideo && this->supportedVideoCodecs(m: QMediaFormat::Encode).isEmpty())
822 fmt = QMediaFormat::UnspecifiedFormat;
823
824 // reset non supported formats and codecs
825 if (!supportedFormats.contains(t: fmt))
826 fmt = QMediaFormat::UnspecifiedFormat;
827 if (!supportedAudioCodecs.contains(t: audio))
828 audio = QMediaFormat::AudioCodec::Unspecified;
829 if (!requiresVideo || !supportedVideoCodecs.contains(t: video))
830 video = QMediaFormat::VideoCodec::Unspecified;
831
832 if (requiresVideo) {
833 // try finding a file format that is supported
834 if (fmt == QMediaFormat::UnspecifiedFormat)
835 fmt = bestSupportedFileFormat(audio, video);
836 // try without the audio codec
837 if (fmt == QMediaFormat::UnspecifiedFormat)
838 fmt = bestSupportedFileFormat(QMediaFormat::AudioCodec::Unspecified, video);
839 }
840 // try without the video codec
841 if (fmt == QMediaFormat::UnspecifiedFormat)
842 fmt = bestSupportedFileFormat(audio);
843 // give me a format that's supported
844 if (fmt == QMediaFormat::UnspecifiedFormat)
845 fmt = bestSupportedFileFormat();
846 // still nothing? Give up
847 if (fmt == QMediaFormat::UnspecifiedFormat) {
848 *this = {};
849 return;
850 }
851
852 // find a working video codec
853 if (requiresVideo) {
854 // reset the audio codec, so that we won't throw away the video codec
855 // if it is supported (choosing the specified video codec has higher
856 // priority than the specified audio codec)
857 auto a = audio;
858 audio = QMediaFormat::AudioCodec::Unspecified;
859 auto videoCodecs = this->supportedVideoCodecs(m: QMediaFormat::Encode);
860 if (!videoCodecs.contains(t: video)) {
861 // not supported, try to find a replacement
862 auto *list = videoPriorityList;
863 while (*list != QMediaFormat::VideoCodec::Unspecified) {
864 if (videoCodecs.contains(t: *list))
865 break;
866 ++list;
867 }
868 video = *list;
869 }
870 audio = a;
871 } else {
872 video = QMediaFormat::VideoCodec::Unspecified;
873 }
874
875 // and a working audio codec
876 auto audioCodecs = this->supportedAudioCodecs(m: QMediaFormat::Encode);
877 if (!audioCodecs.contains(t: audio)) {
878 auto *list = audioPriorityList;
879 while (*list != QMediaFormat::AudioCodec::Unspecified) {
880 if (audioCodecs.contains(t: *list))
881 break;
882 ++list;
883 }
884 audio = *list;
885 }
886}
887
888/*!
889 \variable QMediaFormat::audio
890 \internal
891*/
892/*!
893 \variable QMediaFormat::d
894 \internal
895*/
896/*!
897 \variable QMediaFormat::video
898 \internal
899*/
900/*!
901 \variable QMediaFormat::fmt
902 \internal
903*/
904QT_END_NAMESPACE
905
906#include "moc_qmediaformat.cpp"
907

source code of qtmultimedia/src/multimedia/qmediaformat.cpp