4#include <QtMultimedia/private/qavfcamerautility_p.h>
5#include <QtMultimedia/private/qavfcameradebug_p.h>
7#include <QtCore/qvector.h>
8#include <private/qmultimediautils_p.h>
9#include <private/qcameradevice_p.h>
10#include <QtMultimedia/private/qavfhelpers_p.h>
17#include <AudioToolbox/AudioToolbox.h>
23AVFPSRange qt_connection_framerates(AVCaptureConnection *videoConnection)
25 Q_ASSERT(videoConnection);
31 if (videoConnection.supportsVideoMinFrameDuration) {
32 const CMTime cmMin = videoConnection.videoMinFrameDuration;
33 if (CMTIME_IS_VALID(cmMin)) {
34 if (
const Float64 minSeconds = CMTimeGetSeconds(cmMin))
35 newRange.second = 1. / minSeconds;
39 if (videoConnection.supportsVideoMaxFrameDuration) {
40 const CMTime cmMax = videoConnection.videoMaxFrameDuration;
41 if (CMTIME_IS_VALID(cmMax)) {
42 if (
const Float64 maxSeconds = CMTimeGetSeconds(cmMax))
43 newRange.first = 1. / maxSeconds;
52inline bool qt_area_sane(
const QSize &size)
54 return !size.isNull() && size.isValid()
55 &&
std::numeric_limits<
int>::max() / size.width() >= size.height();
58template <
template <
typename...>
class Comp>
61 bool operator() (AVCaptureDeviceFormat *f1, AVCaptureDeviceFormat *f2)
const
64 const QSize r1(qt_device_format_resolution(f1));
65 const QSize r2(qt_device_format_resolution(f2));
67 const Comp<
std::tuple<
int,
int>> op = {};
68 return op(
std::make_tuple(r1.width(), r1.height()),
69 std::make_tuple(r2.width(), r2.height()));
73struct FormatHasNoFPSRange
75 bool operator() (AVCaptureDeviceFormat *format)
const
78 return !format.videoSupportedFrameRateRanges || !format.videoSupportedFrameRateRanges.count;
82Float64 qt_find_min_framerate_distance(AVCaptureDeviceFormat *format, Float64 fps)
84 Q_ASSERT(format && format.videoSupportedFrameRateRanges
85 && format.videoSupportedFrameRateRanges.count);
87 AVFrameRateRange *range = [format.videoSupportedFrameRateRanges objectAtIndex:0];
88 Float64 distance = qAbs(range.maxFrameRate - fps);
89 for (NSUInteger i = 1, e = format.videoSupportedFrameRateRanges.count; i < e; ++i) {
90 range = [format.videoSupportedFrameRateRanges objectAtIndex:i];
91 distance = qMin(distance, qAbs(range.maxFrameRate - fps));
101 const QCameraFormat &cameraFormat,
102 const std::function<
bool(uint32_t)> &cvFormatValidator)
104 const auto cameraFormatPrivate = QCameraFormatPrivate::handle(cameraFormat);
105 if (!cameraFormatPrivate)
108 const auto requiredCvPixFormat = QAVFHelpers::toCVPixelFormat(cameraFormatPrivate->pixelFormat,
109 cameraFormatPrivate->colorRange);
111 if (requiredCvPixFormat == CvPixelFormatInvalid)
114 AVCaptureDeviceFormat *newFormat = nil;
115 Float64 newFormatMaxFrameRate = {};
116 NSArray<AVCaptureDeviceFormat *> *formats = captureDevice.formats;
117 for (AVCaptureDeviceFormat *format in formats) {
118 CMFormatDescriptionRef formatDesc = format.formatDescription;
119 CMVideoDimensions dim = CMVideoFormatDescriptionGetDimensions(formatDesc);
120 FourCharCode cvPixFormat = CMVideoFormatDescriptionGetCodecType(formatDesc);
122 if (cvPixFormat != requiredCvPixFormat)
125 if (cameraFormatPrivate->resolution != QSize(dim.width, dim.height))
128 if (cvFormatValidator && !cvFormatValidator(cvPixFormat))
131 const float epsilon = 0.001f;
132 for (AVFrameRateRange *frameRateRange in format.videoSupportedFrameRateRanges) {
133 if (frameRateRange.minFrameRate >= cameraFormatPrivate->minFrameRate - epsilon
134 && frameRateRange.maxFrameRate <= cameraFormatPrivate->maxFrameRate + epsilon
135 && newFormatMaxFrameRate < frameRateRange.maxFrameRate) {
137 newFormatMaxFrameRate = frameRateRange.maxFrameRate;
147 Q_ASSERT(captureDevice);
149 QVector<AVCaptureDeviceFormat *> formats;
151 if (!captureDevice.formats || !captureDevice.formats.count)
154 formats.reserve(captureDevice.formats.count);
155 for (AVCaptureDeviceFormat *format in captureDevice.formats) {
156 const QSize resolution(qt_device_format_resolution(format));
157 if (resolution.isNull() || !resolution.isValid())
165 std::sort(formats.begin(), formats.end(), ByResolution<std::less>());
167 QSize size(qt_device_format_resolution(formats[0]));
168 FourCharCode codec = CMVideoFormatDescriptionGetCodecType(formats[0].formatDescription);
170 for (
int i = 1; i < formats.size(); ++i) {
171 const QSize nextSize(qt_device_format_resolution(formats[i]));
172 if (nextSize == size) {
175 formats[last] = formats[i];
178 formats[last] = formats[i];
181 codec = CMVideoFormatDescriptionGetCodecType(formats[i].formatDescription);
183 formats.resize(last + 1);
190 if (!format || !format.formatDescription)
193 const CMVideoDimensions res = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
194 return QSize(res.width, res.height);
202 const CMVideoDimensions hrDim(format.highResolutionStillImageDimensions);
203 res.setWidth(hrDim.width);
204 res.setHeight(hrDim.height);
213 QVector<AVFPSRange> qtRanges;
215 if (!format.videoSupportedFrameRateRanges || !format.videoSupportedFrameRateRanges.count)
218 qtRanges.reserve(format.videoSupportedFrameRateRanges.count);
219 for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges)
220 qtRanges << AVFPSRange(range.minFrameRate, range.maxFrameRate);
229 if (!format.formatDescription) {
230 qCDebug(qLcCamera) << Q_FUNC_INFO <<
"no format description found";
234 const CMVideoDimensions res = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
235 const CGSize resPAR = CMVideoFormatDescriptionGetPresentationDimensions(format.formatDescription,
true,
false);
237 if (qAbs(resPAR.width - res.width) < 1.) {
242 if (!res.width || !resPAR.width)
245 auto frac = qRealToFraction(resPAR.width > res.width ? res.width / qreal(resPAR.width)
246 : resPAR.width / qreal(res.width));
248 return QSize(frac.numerator, frac.denominator);
252 const QSize &request,
256 Q_ASSERT(captureDevice);
257 Q_ASSERT(!request.isNull() && request.isValid());
259 if (!captureDevice.formats || !captureDevice.formats.count)
262 QVector<AVCaptureDeviceFormat *> formats(qt_unique_device_formats(captureDevice, filter));
264 for (
int i = 0; i < formats.size(); ++i) {
265 AVCaptureDeviceFormat *format = formats[i];
266 if (qt_device_format_resolution(format) == request)
269 if (stillImage && qt_device_format_high_resolution(format) == request)
273 if (!qt_area_sane(request))
276 typedef std::pair<QSize, AVCaptureDeviceFormat *> FormatPair;
278 QVector<FormatPair> pairs;
279 pairs.reserve(formats.size());
281 for (
int i = 0; i < formats.size(); ++i) {
282 AVCaptureDeviceFormat *format = formats[i];
283 const QSize res(qt_device_format_resolution(format));
284 if (!res.isNull() && res.isValid() && qt_area_sane(res))
285 pairs << FormatPair(res, format);
286 const QSize highRes(qt_device_format_high_resolution(format));
287 if (stillImage && !highRes.isNull() && highRes.isValid() && qt_area_sane(highRes))
288 pairs << FormatPair(highRes, format);
294 AVCaptureDeviceFormat *best = pairs[0].second;
295 QSize next(pairs[0].first);
296 int wDiff = qAbs(request.width() - next.width());
297 int hDiff = qAbs(request.height() - next.height());
298 const int area = request.width() * request.height();
299 int areaDiff = qAbs(area - next.width() * next.height());
300 for (
int i = 1; i < pairs.size(); ++i) {
301 next = pairs[i].first;
302 const int newWDiff = qAbs(next.width() - request.width());
303 const int newHDiff = qAbs(next.height() - request.height());
304 const int newAreaDiff = qAbs(area - next.width() * next.height());
306 if ((newWDiff < wDiff && newHDiff < hDiff)
307 || ((newWDiff <= wDiff || newHDiff <= hDiff) && newAreaDiff <= areaDiff)) {
310 best = pairs[i].second;
311 areaDiff = newAreaDiff;
322 Q_ASSERT(captureDevice);
325 const qreal epsilon = 0.1;
327 QVector<AVCaptureDeviceFormat *>sorted(qt_unique_device_formats(captureDevice, filter));
329 std::sort(sorted.begin(), sorted.end(), ByResolution<std::greater>());
331 sorted.erase(std::remove_if(sorted.begin(), sorted.end(), FormatHasNoFPSRange()), sorted.end());
336 for (
int i = 0; i < sorted.size(); ++i) {
337 AVCaptureDeviceFormat *format = sorted[i];
338 for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
339 if (range.maxFrameRate - range.minFrameRate < epsilon) {
341 if (qAbs(fps - range.maxFrameRate) < epsilon)
345 if (fps >= range.minFrameRate && fps <= range.maxFrameRate)
350 Float64 distance = qt_find_min_framerate_distance(sorted[0], fps);
351 AVCaptureDeviceFormat *match = sorted[0];
352 for (
int i = 1; i < sorted.size(); ++i) {
353 const Float64 newDistance = qt_find_min_framerate_distance(sorted[i], fps);
354 if (newDistance < distance) {
355 distance = newDistance;
365 Q_ASSERT(format && format.videoSupportedFrameRateRanges
366 && format.videoSupportedFrameRateRanges.count);
368 const qreal epsilon = 0.1;
370 for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
371 if (range.maxFrameRate - range.minFrameRate < epsilon) {
373 if (qAbs(fps - range.maxFrameRate) < epsilon)
377 if (fps >= range.minFrameRate && fps <= range.maxFrameRate)
381 AVFrameRateRange *match = [format.videoSupportedFrameRateRanges objectAtIndex:0];
382 Float64 distance = qAbs(match.maxFrameRate - fps);
383 for (NSUInteger i = 1, e = format.videoSupportedFrameRateRanges.count; i < e; ++i) {
384 AVFrameRateRange *range = [format.videoSupportedFrameRateRanges objectAtIndex:i];
385 const Float64 newDistance = qAbs(range.maxFrameRate - fps);
386 if (newDistance < distance) {
387 distance = newDistance;
397 if (format && fps > qreal(0)) {
398 const qreal epsilon = 0.1;
399 for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
400 if (fps >= range.minFrameRate - epsilon && fps <= range.maxFrameRate + epsilon)
413 if (![f1.mediaType isEqualToString:f2.mediaType])
416 return CMFormatDescriptionEqual(f1.formatDescription, f2.formatDescription);
421 static bool firstSet =
true;
423 if (!captureDevice || !format)
426 if (qt_formats_are_equal(captureDevice.activeFormat, format)) {
439 const AVFConfigurationLock lock(captureDevice);
441 qWarning(
"Failed to set active format (lock failed)");
448 fps = qt_current_framerates(captureDevice, nil);
450 captureDevice.activeFormat = format;
453 qt_set_framerate_limits(captureDevice, nil, fps.first, fps.second);
460 Q_ASSERT(videoConnection);
462 if (minFPS < 0. || maxFPS < 0. || (maxFPS && maxFPS < minFPS)) {
463 qCDebug(qLcCamera) << Q_FUNC_INFO <<
"invalid framerates (min, max):"
468 CMTime minDuration = kCMTimeInvalid;
470 if (!videoConnection.supportsVideoMinFrameDuration)
471 qCDebug(qLcCamera) << Q_FUNC_INFO <<
"maximum framerate is not supported";
473 minDuration = CMTimeMake(1, maxFPS);
475 if (videoConnection.supportsVideoMinFrameDuration)
476 videoConnection.videoMinFrameDuration = minDuration;
478 CMTime maxDuration = kCMTimeInvalid;
480 if (!videoConnection.supportsVideoMaxFrameDuration)
481 qCDebug(qLcCamera) << Q_FUNC_INFO <<
"minimum framerate is not supported";
483 maxDuration = CMTimeMake(1, minFPS);
485 if (videoConnection.supportsVideoMaxFrameDuration)
486 videoConnection.videoMaxFrameDuration = maxDuration;
494 if (range.maxFrameRate - range.minFrameRate < 0.1) {
496 return range.minFrameDuration;
499 if (fps <= range.minFrameRate)
500 return range.maxFrameDuration;
501 if (fps >= range.maxFrameRate)
502 return range.minFrameDuration;
504 auto frac = qRealToFraction(1. / fps);
505 return CMTimeMake(frac.numerator, frac.denominator);
510 Q_ASSERT(captureDevice);
511 if (!captureDevice.activeFormat) {
512 qCDebug(qLcCamera) << Q_FUNC_INFO <<
"no active capture device format";
516 if (minFPS < 0. || maxFPS < 0. || (maxFPS && maxFPS < minFPS)) {
517 qCDebug(qLcCamera) << Q_FUNC_INFO <<
"invalid framerates (min, max):"
522 CMTime minFrameDuration = kCMTimeInvalid;
523 CMTime maxFrameDuration = kCMTimeInvalid;
524 if (maxFPS || minFPS) {
525 AVFrameRateRange *range = qt_find_supported_framerate_range(captureDevice.activeFormat,
526 maxFPS ? maxFPS : minFPS);
528 qCDebug(qLcCamera) << Q_FUNC_INFO <<
"no framerate range found, (min, max):"
534 minFrameDuration = qt_adjusted_frame_duration(range, maxFPS);
536 maxFrameDuration = qt_adjusted_frame_duration(range, minFPS);
539 const AVFConfigurationLock lock(captureDevice);
541 qCDebug(qLcCamera) << Q_FUNC_INFO <<
"failed to lock for configuration";
551 [captureDevice setActiveVideoMinFrameDuration:minFrameDuration];
552 [captureDevice setActiveVideoMaxFrameDuration:maxFrameDuration];
553#elif defined(Q_OS_MACOS)
554 if (CMTIME_IS_INVALID(minFrameDuration)
555 && CMTIME_IS_INVALID(maxFrameDuration)) {
556 AVFrameRateRange *range = captureDevice.activeFormat.videoSupportedFrameRateRanges.firstObject;
557 minFrameDuration = range.minFrameDuration;
558 maxFrameDuration = range.maxFrameDuration;
561 if (CMTIME_IS_VALID(minFrameDuration))
562 [captureDevice setActiveVideoMinFrameDuration:minFrameDuration];
564 if (CMTIME_IS_VALID(maxFrameDuration))
565 [captureDevice setActiveVideoMaxFrameDuration:maxFrameDuration];
570 qreal minFPS, qreal maxFPS)
572 Q_UNUSED(videoConnection);
573 Q_ASSERT(captureDevice);
574 qt_set_framerate_limits(captureDevice, minFPS, maxFPS);
579 Q_UNUSED(videoConnection);
580 Q_ASSERT(captureDevice);
583 const CMTime minDuration = captureDevice.activeVideoMinFrameDuration;
584 if (CMTIME_IS_VALID(minDuration)) {
585 if (
const Float64 minSeconds = CMTimeGetSeconds(minDuration))
586 fps.second = 1. / minSeconds;
589 const CMTime maxDuration = captureDevice.activeVideoMaxFrameDuration;
590 if (CMTIME_IS_VALID(maxDuration)) {
591 if (
const Float64 maxSeconds = CMTimeGetSeconds(maxDuration))
592 fps.first = 1. / maxSeconds;
600 UInt32 format = codecId;
602 OSStatus err = AudioFormatGetPropertyInfo(
603 kAudioFormatProperty_AvailableEncodeSampleRates,
611 UInt32 numRanges = size /
sizeof(AudioValueRange);
612 QList<AudioValueRange> result;
613 result.resize(numRanges);
615 err = AudioFormatGetProperty(kAudioFormatProperty_AvailableEncodeSampleRates,
620 return err == noErr ? result : QList<AudioValueRange>{};
625 UInt32 format = codecId;
627 OSStatus err = AudioFormatGetPropertyInfo(
628 kAudioFormatProperty_AvailableEncodeBitRates,
636 UInt32 numRanges = size /
sizeof(AudioValueRange);
637 QList<AudioValueRange> result;
638 result.resize(numRanges);
640 err = AudioFormatGetProperty(kAudioFormatProperty_AvailableEncodeBitRates,
645 return err == noErr ? result : QList<AudioValueRange>{};
650 AudioStreamBasicDescription sf = {};
651 sf.mFormatID = codecId;
653 OSStatus err = AudioFormatGetPropertyInfo(
654 kAudioFormatProperty_AvailableEncodeNumberChannels,
667 UInt32 numCounts = size /
sizeof(UInt32);
668 QList<UInt32> channelCounts;
669 channelCounts.resize(numCounts);
671 err = AudioFormatGetProperty(kAudioFormatProperty_AvailableEncodeNumberChannels,
675 channelCounts.data());
677 return channelCounts;
684 AudioStreamBasicDescription sf = {};
685 sf.mFormatID = codecId;
686 sf.mChannelsPerFrame = noChannels;
688 OSStatus err = AudioFormatGetPropertyInfo(
689 kAudioFormatProperty_AvailableEncodeChannelLayoutTags,
697 UInt32 noTags = (UInt32)size /
sizeof(UInt32);
698 QList<AudioChannelLayoutTag> tagsArr;
699 tagsArr.resize(noTags);
701 err = AudioFormatGetProperty(kAudioFormatProperty_AvailableEncodeChannelLayoutTags,
709 QList<UInt32> result;
710 for (
const AudioChannelLayoutTag &item : tagsArr)
711 result.push_back(item);
QSize qt_device_format_resolution(AVCaptureDeviceFormat *format)
bool qt_formats_are_equal(AVCaptureDeviceFormat *f1, AVCaptureDeviceFormat *f2)
AVFPSRange qt_current_framerates(AVCaptureDevice *captureDevice, AVCaptureConnection *videoConnection)
std::optional< QList< UInt32 > > qt_supported_channel_counts_for_format(int codecId)
QVector< AVCaptureDeviceFormat * > qt_unique_device_formats(AVCaptureDevice *captureDevice, FourCharCode filter)
void qt_set_framerate_limits(AVCaptureDevice *captureDevice, AVCaptureConnection *videoConnection, qreal minFPS, qreal maxFPS)
QVector< AVFPSRange > qt_device_format_framerates(AVCaptureDeviceFormat *format)
QSize qt_device_format_high_resolution(AVCaptureDeviceFormat *format)
QList< AudioValueRange > qt_supported_sample_rates_for_format(int codecId)
QList< UInt32 > qt_supported_channel_layout_tags_for_format(int codecId, int noChannels)
AVCaptureDeviceFormat * qt_find_best_framerate_match(AVCaptureDevice *captureDevice, FourCharCode filter, Float64 fps)
QList< AudioValueRange > qt_supported_bit_rates_for_format(int codecId)
bool qt_set_active_format(AVCaptureDevice *captureDevice, AVCaptureDeviceFormat *format, bool preserveFps)
AVFrameRateRange * qt_find_supported_framerate_range(AVCaptureDeviceFormat *format, Float64 fps)
void qt_set_framerate_limits(AVCaptureConnection *videoConnection, qreal minFPS, qreal maxFPS)
QSize qt_device_format_pixel_aspect_ratio(AVCaptureDeviceFormat *format)
AVCaptureDeviceFormat * qt_find_best_resolution_match(AVCaptureDevice *captureDevice, const QSize &request, FourCharCode filter, bool stillImage)
CMTime qt_adjusted_frame_duration(AVFrameRateRange *range, qreal fps)
bool qt_format_supports_framerate(AVCaptureDeviceFormat *format, qreal fps)
AVCaptureDeviceFormat * qt_convert_to_capture_device_format(AVCaptureDevice *captureDevice, const QCameraFormat &cameraFormat, const std::function< bool(uint32_t)> &cvFormatValidator)
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")