| 1 |
/* |
| 2 |
* Copyright (c) 2010-2022 Belledonne Communications SARL. |
| 3 |
* |
| 4 |
* This file is part of mediastreamer2 |
| 5 |
* (see https://gitlab.linphone.org/BC/public/mediastreamer2). |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Affero General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Affero General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Affero General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <TargetConditionals.h> |
| 22 |
#include <array> |
| 23 |
#include <sstream> |
| 24 |
#include <unordered_map> |
| 25 |
|
| 26 |
#include <VideoToolbox/VTVideoEncoderList.h> |
| 27 |
|
| 28 |
#include "videotoolbox-h264-utilities.h" |
| 29 |
#include "videotoolbox-h265-utilities.h" |
| 30 |
|
| 31 |
#include "videotoolbox-utils.h" |
| 32 |
|
| 33 |
using namespace std; |
| 34 |
|
| 35 |
namespace mediastreamer { |
| 36 |
|
| 37 |
static unordered_map<OSStatus, string> _errorMsg = { |
| 38 |
{noErr, "no error"}, |
| 39 |
{kCVReturnAllocationFailed, "return allocation failed"}, |
| 40 |
{kVTPropertyNotSupportedErr, "property not supported"}, |
| 41 |
{kVTPropertyReadOnlyErr, "read only error"}, |
| 42 |
{kVTParameterErr, "parameter error"}, |
| 43 |
{kVTInvalidSessionErr, "invalid session"}, |
| 44 |
{kVTAllocationFailedErr, "allocation failed"}, |
| 45 |
{kVTPixelTransferNotSupportedErr, "pixel transfer not supported"}, |
| 46 |
{kVTCouldNotFindVideoDecoderErr, "could not find video decoder"}, |
| 47 |
{kVTCouldNotCreateInstanceErr, "could not create instance"}, |
| 48 |
{kVTCouldNotFindVideoEncoderErr, "could not find video encoder"}, |
| 49 |
{kVTVideoDecoderBadDataErr, "bad data"}, |
| 50 |
{kVTVideoDecoderUnsupportedDataFormatErr, "unsupported data format"}, |
| 51 |
{kVTVideoDecoderMalfunctionErr, "decoder malfunction"}, |
| 52 |
{kVTVideoEncoderMalfunctionErr, "encoder mulfunction"}, |
| 53 |
{kVTVideoDecoderNotAvailableNowErr, "decoder not available now"}, |
| 54 |
{kVTImageRotationNotSupportedErr, "image rotation not supported"}, |
| 55 |
{kVTVideoEncoderNotAvailableNowErr, "encoder not available now"}, |
| 56 |
{kVTFormatDescriptionChangeNotSupportedErr, "format description change not supported"}, |
| 57 |
{kVTInsufficientSourceColorDataErr, "insufficient source color data"}, |
| 58 |
{kVTCouldNotCreateColorCorrectionDataErr, "could not create color correction data"}, |
| 59 |
{kVTColorSyncTransformConvertFailedErr, "color sync transform convert failed"}, |
| 60 |
{kVTVideoDecoderAuthorizationErr, "video decoder authorization error"}, |
| 61 |
{kVTVideoEncoderAuthorizationErr, "video encoder authorization error"}, |
| 62 |
{kVTColorCorrectionPixelTransferFailedErr, "color correction pixel transfer failed"}, |
| 63 |
{kVTMultiPassStorageIdentifierMismatchErr, "multi-pass storage identifier mismatch"}, |
| 64 |
{kVTMultiPassStorageInvalidErr, "multi-pass storage invalid"}, |
| 65 |
{kVTFrameSiloInvalidTimeStampErr, "frame silo invalid timestamp"}, |
| 66 |
{kVTFrameSiloInvalidTimeRangeErr, "frame silo invalid time range"}, |
| 67 |
{kVTCouldNotFindTemporalFilterErr, "could not find temporal filter"}, |
| 68 |
{kVTPixelTransferNotPermittedErr, "pixel transfer not permitted"}, |
| 69 |
{kVTColorCorrectionImageRotationFailedErr, "color correction image rotation failed"}, |
| 70 |
{-17690 /* kVTVideoDecoderRemovedErr */, "video decoder removed"}}; |
| 71 |
|
| 72 |
std::string toString(::OSStatus status) { |
| 73 |
ostringstream message; |
| 74 |
unordered_map<OSStatus, string>::const_iterator it = _errorMsg.find(status); |
| 75 |
if (it != _errorMsg.cend()) { |
| 76 |
message << it->second; |
| 77 |
} else { |
| 78 |
message << "unknown error"; |
| 79 |
} |
| 80 |
message << " [osstatus=" << status << "]"; |
| 81 |
return message.str(); |
| 82 |
} |
| 83 |
|
| 84 |
bool VideoToolboxUtilities::encoderIsAvailable() const { |
| 85 |
if (_codecAvailability.empty()) loadCodecAvailability(); |
| 86 |
return _codecAvailability.at(getCodecType()); |
| 87 |
} |
| 88 |
|
| 89 |
void VideoToolboxUtilities::getParameterSets(const CMFormatDescriptionRef format, MSQueue *outPs) const { |
| 90 |
size_t offset = 0; |
| 91 |
size_t parameterSetsCount; |
| 92 |
do { |
| 93 |
const uint8_t *parameterSet; |
| 94 |
size_t parameterSetSize; |
| 95 |
getParameterSet(format, offset++, parameterSet, parameterSetSize, parameterSetsCount); |
| 96 |
|
| 97 |
mblk_t *nalu = allocb(parameterSetSize, 0); |
| 98 |
memcpy(nalu->b_wptr, parameterSet, parameterSetSize); |
| 99 |
nalu->b_wptr += parameterSetSize; |
| 100 |
ms_queue_put(outPs, nalu); |
| 101 |
} while (offset < parameterSetsCount); |
| 102 |
} |
| 103 |
|
| 104 |
CMFormatDescriptionRef VideoToolboxUtilities::createFormatDescription(const H26xParameterSetsStore &psStore) const { |
| 105 |
MSQueue parameterSets; |
| 106 |
ms_queue_init(¶meterSets); |
| 107 |
|
| 108 |
try { |
| 109 |
psStore.fetchAllPs(¶meterSets); |
| 110 |
|
| 111 |
vector<const uint8_t *> ptrs; |
| 112 |
vector<size_t> sizes; |
| 113 |
for (const mblk_t *ps = ms_queue_peek_first(¶meterSets); !ms_queue_end(¶meterSets, ps); |
| 114 |
ps = ms_queue_next(¶meterSets, ps)) { |
| 115 |
ptrs.push_back(ps->b_rptr); |
| 116 |
sizes.push_back(msgdsize(ps)); |
| 117 |
} |
| 118 |
|
| 119 |
CMFormatDescriptionRef formatDesc = createFormatDescription(ptrs.size(), ptrs.data(), sizes.data()); |
| 120 |
ms_queue_flush(¶meterSets); |
| 121 |
return formatDesc; |
| 122 |
} catch (const AppleOSError &e) { |
| 123 |
ms_queue_flush(¶meterSets); |
| 124 |
throw; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
VideoToolboxUtilities *VideoToolboxUtilities::create(const std::string &mime) { |
| 129 |
if (mime == "video/avc") { |
| 130 |
return new VideoToolboxH264Utilities(); |
| 131 |
} else if (mime == "video/hevc") { |
| 132 |
return new VideoToolboxH265Utilities(); |
| 133 |
} else { |
| 134 |
throw invalid_argument(mime + " not supported"); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void VideoToolboxUtilities::loadCodecAvailability() { |
| 139 |
array<CMVideoCodecType, 2> codecTypes = {{kCMVideoCodecType_H264, kCMVideoCodecType_HEVC}}; |
| 140 |
CFArrayRef encoderLists = nullptr; |
| 141 |
VTCopyVideoEncoderList(nullptr, &encoderLists); |
| 142 |
for (auto codecType : codecTypes) { |
| 143 |
_codecAvailability[codecType] = false; |
| 144 |
for (CFIndex i = 0; i < CFArrayGetCount(encoderLists); i++) { |
| 145 |
CMVideoCodecType codecType2; |
| 146 |
auto dict = static_cast<const CFDictionaryRef>(CFArrayGetValueAtIndex(encoderLists, i)); |
| 147 |
auto codecTypeNumber = |
| 148 |
static_cast<const CFNumberRef>(CFDictionaryGetValue(dict, kVTVideoEncoderList_CodecType)); |
| 149 |
bool isHardwareAccelerated = CFDictionaryGetValue(dict, kVTVideoEncoderList_IsHardwareAccelerated); |
| 150 |
CFStringRef encoderName = (CFStringRef)CFDictionaryGetValue(dict, kVTVideoEncoderList_EncoderName); |
| 151 |
CFNumberGetValue(codecTypeNumber, kCFNumberIntType, &codecType2); |
| 152 |
if (codecType == codecType2) { |
| 153 |
#if !TARGET_OS_IPHONE |
| 154 |
if (isHardwareAccelerated) { |
| 155 |
_codecAvailability[codecType] = true; |
| 156 |
break; |
| 157 |
} else { |
| 158 |
ms_message("VideoToolbox: hardware encoding not supported for [%s]", |
| 159 |
CFStringGetCStringPtr(encoderName, kCFStringEncodingUTF8)); |
| 160 |
} |
| 161 |
#else |
| 162 |
/* Real iOS devices do not report hardware acceleration. Assume it is.*/ |
| 163 |
_codecAvailability[codecType] = true; |
| 164 |
#endif |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
CFRelease(encoderLists); |
| 169 |
} |
| 170 |
|
| 171 |
std::map<CMVideoCodecType, bool> VideoToolboxUtilities::_codecAvailability; |
| 172 |
|
| 173 |
} // namespace mediastreamer |