| 1 |
/* |
| 2 |
* Copyright 2024 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/codec/SkCrabbyAvifCodec.h" |
| 9 |
|
| 10 |
#include "include/codec/SkAndroidCodec.h" |
| 11 |
#include "include/codec/SkAvifDecoder.h" |
| 12 |
#include "include/codec/SkCodec.h" |
| 13 |
#include "include/codec/SkCodecAnimation.h" |
| 14 |
#include "include/core/SkColorType.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkSize.h" |
| 17 |
#include "include/core/SkStream.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "include/private/SkGainmapInfo.h" |
| 20 |
#include "include/private/base/SkMutex.h" |
| 21 |
#include "modules/skcms/skcms.h" |
| 22 |
#include "src/codec/SkCodecPriv.h" |
| 23 |
#include "src/core/SkStreamPriv.h" |
| 24 |
|
| 25 |
#include <cstdint> |
| 26 |
#include <cstring> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
#include "avif/avif.h" |
| 30 |
#include "avif/libavif_compat.h" |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
template <typename NumeratorType> |
| 35 |
float FractionToFloat(NumeratorType numerator, uint32_t denominator) { |
| 36 |
// First cast to double and not float because uint32_t->float conversion can |
| 37 |
// cause precision loss. |
| 38 |
return static_cast<double>(numerator) / denominator; |
| 39 |
} |
| 40 |
|
| 41 |
sk_sp<SkColorSpace> AltImageColorSpace(const crabbyavif::avifGainMap& gain_map, |
| 42 |
const crabbyavif::avifImage& image) { |
| 43 |
sk_sp<SkColorSpace> color_space = nullptr; |
| 44 |
if (!gain_map.altICC.size) { |
| 45 |
return nullptr; |
| 46 |
} |
| 47 |
if (image.icc.size == gain_map.altICC.size && |
| 48 |
memcmp(gain_map.altICC.data, image.icc.data, gain_map.altICC.size) == 0) { |
| 49 |
// Same ICC as the base image, no need to specify it. |
| 50 |
return nullptr; |
| 51 |
} |
| 52 |
skcms_ICCProfile icc_profile; |
| 53 |
if (!skcms_Parse(gain_map.altICC.data, gain_map.altICC.size, &icc_profile)) { |
| 54 |
return nullptr; |
| 55 |
} |
| 56 |
return SkColorSpace::Make(icc_profile); |
| 57 |
} |
| 58 |
|
| 59 |
bool PopulateGainmapInfo(const crabbyavif::avifGainMap& gain_map, |
| 60 |
const crabbyavif::avifImage& image, |
| 61 |
SkGainmapInfo* info) { |
| 62 |
if (gain_map.baseHdrHeadroom.d == 0 || gain_map.alternateHdrHeadroom.d == 0) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
const float base_headroom = |
| 66 |
std::exp2(FractionToFloat(gain_map.baseHdrHeadroom.n, gain_map.baseHdrHeadroom.d)); |
| 67 |
const float alternate_headroom = std::exp2( |
| 68 |
FractionToFloat(gain_map.alternateHdrHeadroom.n, gain_map.alternateHdrHeadroom.d)); |
| 69 |
const bool base_is_hdr = base_headroom > alternate_headroom; |
| 70 |
info->fDisplayRatioSdr = base_is_hdr ? alternate_headroom : base_headroom; |
| 71 |
info->fDisplayRatioHdr = base_is_hdr ? base_headroom : alternate_headroom; |
| 72 |
info->fBaseImageType = |
| 73 |
base_is_hdr ? SkGainmapInfo::BaseImageType::kHDR : SkGainmapInfo::BaseImageType::kSDR; |
| 74 |
for (int i = 0; i < 3; ++i) { |
| 75 |
if (gain_map.gainMapMin[i].d == 0 || gain_map.gainMapMax[i].d == 0 || |
| 76 |
gain_map.gainMapGamma[i].d == 0 || gain_map.baseOffset[i].d == 0 || |
| 77 |
gain_map.alternateOffset[i].d == 0 || gain_map.gainMapGamma[i].n == 0) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
const float min_log2 = FractionToFloat(gain_map.gainMapMin[i].n, gain_map.gainMapMin[i].d); |
| 81 |
const float max_log2 = FractionToFloat(gain_map.gainMapMax[i].n, gain_map.gainMapMax[i].d); |
| 82 |
info->fGainmapRatioMin[i] = std::exp2(min_log2); |
| 83 |
info->fGainmapRatioMax[i] = std::exp2(max_log2); |
| 84 |
// Numerator and denominator intentionally swapped to get 1.0/gamma. |
| 85 |
info->fGainmapGamma[i] = |
| 86 |
FractionToFloat(gain_map.gainMapGamma[i].d, gain_map.gainMapGamma[i].n); |
| 87 |
const float base_offset = |
| 88 |
FractionToFloat(gain_map.baseOffset[i].n, gain_map.baseOffset[i].d); |
| 89 |
const float alternate_offset = |
| 90 |
FractionToFloat(gain_map.alternateOffset[i].n, gain_map.alternateOffset[i].d); |
| 91 |
info->fEpsilonSdr[i] = base_is_hdr ? alternate_offset : base_offset; |
| 92 |
info->fEpsilonHdr[i] = base_is_hdr ? base_offset : alternate_offset; |
| 93 |
} |
| 94 |
if (!gain_map.useBaseColorSpace) { |
| 95 |
info->fGainmapMathColorSpace = AltImageColorSpace(gain_map, image); |
| 96 |
} |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
SkEncodedOrigin ComputeSkEncodedOrigin(const crabbyavif::avifImage& image) { |
| 101 |
// |angle| * 90 specifies the angle of anti-clockwise rotation in degrees. |
| 102 |
// Legal values: [0-3]. |
| 103 |
const int angle = |
| 104 |
((image.transformFlags & crabbyavif::AVIF_TRANSFORM_IROT) && image.irot.angle <= 3) |
| 105 |
? image.irot.angle |
| 106 |
: 0; |
| 107 |
// |axis| specifies how the mirroring is performed. |
| 108 |
// -1: No mirroring. |
| 109 |
// 0: The top and bottom parts of the image are exchanged. |
| 110 |
// 1: The left and right parts of the image are exchanged. |
| 111 |
const int axis = |
| 112 |
((image.transformFlags & crabbyavif::AVIF_TRANSFORM_IMIR) && image.imir.axis <= 1) |
| 113 |
? image.imir.axis |
| 114 |
: -1; |
| 115 |
// The first dimension is axis (with an offset of 1). The second dimension |
| 116 |
// is angle. |
| 117 |
const SkEncodedOrigin kAxisAngleToSkEncodedOrigin[3][4] = { |
| 118 |
// No mirroring. |
| 119 |
{kTopLeft_SkEncodedOrigin, |
| 120 |
kLeftBottom_SkEncodedOrigin, |
| 121 |
kBottomRight_SkEncodedOrigin, |
| 122 |
kRightTop_SkEncodedOrigin}, |
| 123 |
// Top-to-bottom mirroring. Change Top<->Bottom in the first row. |
| 124 |
{kBottomLeft_SkEncodedOrigin, |
| 125 |
kLeftTop_SkEncodedOrigin, |
| 126 |
kTopRight_SkEncodedOrigin, |
| 127 |
kRightBottom_SkEncodedOrigin}, |
| 128 |
// Left-to-right mirroring. Change Left<->Right in the first row. |
| 129 |
{kTopRight_SkEncodedOrigin, |
| 130 |
kRightBottom_SkEncodedOrigin, |
| 131 |
kBottomLeft_SkEncodedOrigin, |
| 132 |
kLeftTop_SkEncodedOrigin}, |
| 133 |
}; |
| 134 |
return kAxisAngleToSkEncodedOrigin[axis + 1][angle]; |
| 135 |
} |
| 136 |
|
| 137 |
} // namespace |
| 138 |
|
| 139 |
void AvifDecoderDeleter::operator()(crabbyavif::avifDecoder* decoder) const { |
| 140 |
if (decoder != nullptr) { |
| 141 |
crabbyavif::avifDecoderDestroy(decoder); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
bool SkCrabbyAvifCodec::IsAvif(const void* buffer, size_t bytesRead) { |
| 146 |
crabbyavif::avifROData avifData = {static_cast<const uint8_t*>(buffer), bytesRead}; |
| 147 |
return crabbyavif::avifPeekCompatibleFileType(&avifData) == crabbyavif::CRABBY_AVIF_TRUE; |
| 148 |
} |
| 149 |
|
| 150 |
std::unique_ptr<SkCodec> SkCrabbyAvifCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 151 |
Result* result, |
| 152 |
bool gainmapOnly /*=false*/) { |
| 153 |
SkASSERT(result); |
| 154 |
if (!stream) { |
| 155 |
*result = SkCodec::kInvalidInput; |
| 156 |
return nullptr; |
| 157 |
} |
| 158 |
|
| 159 |
// CrabbyAvif needs a contiguous data buffer. |
| 160 |
sk_sp<SkData> data = nullptr; |
| 161 |
if (stream->getMemoryBase()) { |
| 162 |
// It is safe to make without copy because we'll hold onto the stream. |
| 163 |
data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength()); |
| 164 |
} else { |
| 165 |
data = SkStreamPriv::CopyStreamToData(stream.get()); |
| 166 |
// If we are forced to copy the stream to a data, we can go ahead and |
| 167 |
// delete the stream. |
| 168 |
stream.reset(nullptr); |
| 169 |
} |
| 170 |
return SkCrabbyAvifCodec::MakeFromData(std::move(stream), std::move(data), result, gainmapOnly); |
| 171 |
} |
| 172 |
|
| 173 |
std::unique_ptr<SkCodec> SkCrabbyAvifCodec::MakeFromData(std::unique_ptr<SkStream> stream, |
| 174 |
sk_sp<const SkData> data, |
| 175 |
Result* result, |
| 176 |
bool gainmapOnly /*=false*/) { |
| 177 |
SkASSERT(result); |
| 178 |
|
| 179 |
AvifDecoder avifDecoder(crabbyavif::avifDecoderCreate()); |
| 180 |
if (avifDecoder == nullptr) { |
| 181 |
*result = SkCodec::kInternalError; |
| 182 |
return nullptr; |
| 183 |
} |
| 184 |
|
| 185 |
// Ignore XMP and Exif to ensure that avifDecoderParse() isn't waiting for |
| 186 |
// some tiny Exif payload hiding at the end of a file. |
| 187 |
avifDecoder->ignoreXMP = crabbyavif::CRABBY_AVIF_TRUE; |
| 188 |
avifDecoder->ignoreExif = crabbyavif::CRABBY_AVIF_TRUE; |
| 189 |
|
| 190 |
// Disable strict mode. This allows some AVIF files in the wild that are |
| 191 |
// technically invalid according to the specification because they were |
| 192 |
// created with older tools but can be decoded and rendered without any |
| 193 |
// issues. |
| 194 |
avifDecoder->strictFlags = crabbyavif::AVIF_STRICT_DISABLED; |
| 195 |
|
| 196 |
// Disable support for sample transforms. Android pipeline does not support |
| 197 |
// these images. |
| 198 |
avifDecoder->allowSampleTransform = crabbyavif::CRABBY_AVIF_FALSE; |
| 199 |
|
| 200 |
// Android uses MediaCodec for decoding the underlying image. So there is no |
| 201 |
// need to set maxThreads since MediaCodec doesn't allow explicit setting |
| 202 |
// of threads. |
| 203 |
|
| 204 |
if (gainmapOnly) { |
| 205 |
avifDecoder->imageContentToDecode = crabbyavif::AVIF_IMAGE_CONTENT_GAIN_MAP; |
| 206 |
} |
| 207 |
|
| 208 |
crabbyavif::avifResult res = |
| 209 |
crabbyavif::avifDecoderSetIOMemory(avifDecoder.get(), data->bytes(), data->size()); |
| 210 |
if (res != crabbyavif::AVIF_RESULT_OK) { |
| 211 |
*result = SkCodec::kInternalError; |
| 212 |
return nullptr; |
| 213 |
} |
| 214 |
|
| 215 |
res = crabbyavif::avifDecoderParse(avifDecoder.get()); |
| 216 |
if (res != crabbyavif::AVIF_RESULT_OK) { |
| 217 |
*result = SkCodec::kInvalidInput; |
| 218 |
return nullptr; |
| 219 |
} |
| 220 |
|
| 221 |
// CrabbyAvif uses MediaCodec, which always sets bitsPerComponent to 8. |
| 222 |
const int bitsPerComponent = 8; |
| 223 |
SkEncodedInfo::Color color; |
| 224 |
SkEncodedInfo::Alpha alpha; |
| 225 |
if (avifDecoder->alphaPresent && !gainmapOnly) { |
| 226 |
color = SkEncodedInfo::kRGBA_Color; |
| 227 |
alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 228 |
} else { |
| 229 |
color = SkEncodedInfo::kRGB_Color; |
| 230 |
alpha = SkEncodedInfo::kOpaque_Alpha; |
| 231 |
} |
| 232 |
if (gainmapOnly && !avifDecoder->image->gainMap) { |
| 233 |
*result = SkCodec::kInvalidInput; |
| 234 |
return nullptr; |
| 235 |
} |
| 236 |
crabbyavif::avifImage* image = |
| 237 |
gainmapOnly ? avifDecoder->image->gainMap->image : avifDecoder->image; |
| 238 |
auto width = image->width; |
| 239 |
auto height = image->height; |
| 240 |
if (image->transformFlags & crabbyavif::AVIF_TRANSFORM_CLAP) { |
| 241 |
crabbyavif::avifCropRect rect; |
| 242 |
if (crabbyavif::crabby_avifCropRectConvertCleanApertureBox( |
| 243 |
&rect, &image->clap, width, height, image->yuvFormat, nullptr)) { |
| 244 |
width = rect.width; |
| 245 |
height = rect.height; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
std::unique_ptr<SkCodecs::ColorProfile> profile; |
| 250 |
if (image->icc.size > 0) { |
| 251 |
auto icc = SkData::MakeWithCopy(image->icc.data, image->icc.size); |
| 252 |
profile = SkCodecs::ColorProfile::MakeICCProfile(std::move(icc)); |
| 253 |
} else if (image->transferCharacteristics == crabbyavif::AVIF_TRANSFER_CHARACTERISTICS_PQ || |
| 254 |
image->transferCharacteristics == crabbyavif::AVIF_TRANSFER_CHARACTERISTICS_HLG) { |
| 255 |
// Do not set matrix_coefficients here because cicp_get_sk_color_space in SkAndroidCodec.cpp |
| 256 |
// fails if matrix_coeffieicnts is not zero: |
| 257 |
// https://skia.googlesource.com/skia/+/33b2d3333755ac5ce21495959c2d4bb11f299f8b/src/codec/SkAndroidCodec.cpp#186 |
| 258 |
profile = SkCodecs::ColorProfile::MakeCICP( |
| 259 |
image->colorPrimaries, image->transferCharacteristics, 0, |
| 260 |
image->yuvRange == crabbyavif::AVIF_RANGE_FULL); |
| 261 |
} |
| 262 |
if (profile && profile->dataSpace() != SkCodecs::ColorProfile::DataSpace::kRGB) { |
| 263 |
profile = nullptr; |
| 264 |
} |
| 265 |
|
| 266 |
SkEncodedInfo info = SkEncodedInfo::Make( |
| 267 |
width, height, color, alpha, bitsPerComponent, std::move(profile), image->depth); |
| 268 |
bool animation = avifDecoder->imageCount > 1; |
| 269 |
*result = kSuccess; |
| 270 |
SkEncodedImageFormat format = |
| 271 |
avifDecoder->compressionFormat == crabbyavif::COMPRESSION_FORMAT_AVIF |
| 272 |
? SkEncodedImageFormat::kAVIF |
| 273 |
: SkEncodedImageFormat::kHEIF; |
| 274 |
const SkEncodedOrigin origin = ComputeSkEncodedOrigin(*image); |
| 275 |
return std::unique_ptr<SkCodec>(new SkCrabbyAvifCodec(std::move(info), |
| 276 |
std::move(stream), |
| 277 |
std::move(data), |
| 278 |
std::move(avifDecoder), |
| 279 |
origin, |
| 280 |
animation, |
| 281 |
gainmapOnly, |
| 282 |
format)); |
| 283 |
} |
| 284 |
|
| 285 |
SkCrabbyAvifCodec::SkCrabbyAvifCodec(SkEncodedInfo&& info, |
| 286 |
std::unique_ptr<SkStream> stream, |
| 287 |
sk_sp<const SkData> data, |
| 288 |
AvifDecoder avifDecoder, |
| 289 |
SkEncodedOrigin origin, |
| 290 |
bool useAnimation, |
| 291 |
bool gainmapOnly, |
| 292 |
SkEncodedImageFormat format) |
| 293 |
: SkScalingCodec(std::move(info), skcms_PixelFormat_RGBA_8888, std::move(stream), origin) |
| 294 |
, fData(std::move(data)) |
| 295 |
, fAvifDecoder(std::move(avifDecoder)) |
| 296 |
, fUseAnimation(useAnimation) |
| 297 |
, fGainmapOnly(gainmapOnly) |
| 298 |
, fFormat(format) {} |
| 299 |
|
| 300 |
int SkCrabbyAvifCodec::onGetFrameCount() { |
| 301 |
if (!fUseAnimation) { |
| 302 |
return 1; |
| 303 |
} |
| 304 |
|
| 305 |
if (fFrameHolder.size() == 0) { |
| 306 |
if (fAvifDecoder->imageCount <= 1) { |
| 307 |
fUseAnimation = false; |
| 308 |
return 1; |
| 309 |
} |
| 310 |
fFrameHolder.reserve(fAvifDecoder->imageCount); |
| 311 |
for (int i = 0; i < fAvifDecoder->imageCount; i++) { |
| 312 |
Frame* frame = fFrameHolder.appendNewFrame(fAvifDecoder->alphaPresent == |
| 313 |
crabbyavif::CRABBY_AVIF_TRUE); |
| 314 |
frame->setXYWH(0, 0, fAvifDecoder->image->width, fAvifDecoder->image->height); |
| 315 |
frame->setDisposalMethod(SkCodecAnimation::DisposalMethod::kKeep); |
| 316 |
crabbyavif::avifImageTiming timing; |
| 317 |
avifDecoderNthImageTiming(fAvifDecoder.get(), i, &timing); |
| 318 |
frame->setDuration(timing.duration * 1000); |
| 319 |
frame->setRequiredFrame(SkCodec::kNoFrame); |
| 320 |
frame->setHasAlpha(fAvifDecoder->alphaPresent == crabbyavif::CRABBY_AVIF_TRUE); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return fFrameHolder.size(); |
| 325 |
} |
| 326 |
|
| 327 |
const SkFrame* SkCrabbyAvifCodec::FrameHolder::onGetFrame(int i) const { |
| 328 |
return static_cast<const SkFrame*>(this->frame(i)); |
| 329 |
} |
| 330 |
|
| 331 |
SkCrabbyAvifCodec::Frame* SkCrabbyAvifCodec::FrameHolder::appendNewFrame(bool hasAlpha) { |
| 332 |
const int i = this->size(); |
| 333 |
fFrames.emplace_back(i, |
| 334 |
hasAlpha ? SkEncodedInfo::kUnpremul_Alpha : SkEncodedInfo::kOpaque_Alpha); |
| 335 |
return &fFrames[i]; |
| 336 |
} |
| 337 |
|
| 338 |
const SkCrabbyAvifCodec::Frame* SkCrabbyAvifCodec::FrameHolder::frame(int i) const { |
| 339 |
SkASSERT(i >= 0 && i < this->size()); |
| 340 |
return &fFrames[i]; |
| 341 |
} |
| 342 |
|
| 343 |
bool SkCrabbyAvifCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const { |
| 344 |
if (i >= fFrameHolder.size()) { |
| 345 |
return false; |
| 346 |
} |
| 347 |
|
| 348 |
const Frame* frame = fFrameHolder.frame(i); |
| 349 |
if (!frame) { |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
if (frameInfo) { |
| 354 |
frame->fillIn(frameInfo, true); |
| 355 |
} |
| 356 |
|
| 357 |
return true; |
| 358 |
} |
| 359 |
|
| 360 |
int SkCrabbyAvifCodec::onGetRepetitionCount() { |
| 361 |
return (fAvifDecoder->repetitionCount < 0) ? kRepetitionCountInfinite |
| 362 |
: fAvifDecoder->repetitionCount; |
| 363 |
} |
| 364 |
|
| 365 |
SkCodec::IsAnimated SkCrabbyAvifCodec::onIsAnimated() { |
| 366 |
if (!fUseAnimation || fAvifDecoder->imageCount <= 1) { |
| 367 |
return IsAnimated::kNo; |
| 368 |
} |
| 369 |
return IsAnimated::kYes; |
| 370 |
} |
| 371 |
|
| 372 |
bool SkCrabbyAvifCodec::conversionSupported(const SkImageInfo& dstInfo, |
| 373 |
bool srcIsOpaque, |
| 374 |
bool needsColorXform) { |
| 375 |
return dstInfo.colorType() == kRGBA_8888_SkColorType || |
| 376 |
dstInfo.colorType() == kBGRA_8888_SkColorType || |
| 377 |
dstInfo.colorType() == kRGBA_1010102_SkColorType || |
| 378 |
dstInfo.colorType() == kRGBA_F16_SkColorType || |
| 379 |
dstInfo.colorType() == kRGB_565_SkColorType; |
| 380 |
} |
| 381 |
|
| 382 |
SkCodec::Result SkCrabbyAvifCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 383 |
void* dst, |
| 384 |
size_t dstRowBytes, |
| 385 |
const Options& options, |
| 386 |
int* rowsDecoded) { |
| 387 |
// Creating multiple MediaCodec instances can lead to binder starvation |
| 388 |
// issues (e.g. b/447869238) on Android. Guard access to this function with |
| 389 |
// a static lock so that a single app process can only create one MediaCodec |
| 390 |
// instance at a time. |
| 391 |
static SkMutex mutex; |
| 392 |
SkAutoMutexExclusive lock(mutex); |
| 393 |
switch (dstInfo.colorType()) { |
| 394 |
case kRGBA_8888_SkColorType: |
| 395 |
case kBGRA_8888_SkColorType: |
| 396 |
case kRGB_565_SkColorType: |
| 397 |
fAvifDecoder->androidMediaCodecOutputColorFormat = |
| 398 |
crabbyavif::ANDROID_MEDIA_CODEC_OUTPUT_COLOR_FORMAT_YUV420_FLEXIBLE; |
| 399 |
break; |
| 400 |
case kRGBA_F16_SkColorType: |
| 401 |
case kRGBA_1010102_SkColorType: |
| 402 |
fAvifDecoder->androidMediaCodecOutputColorFormat = |
| 403 |
crabbyavif::ANDROID_MEDIA_CODEC_OUTPUT_COLOR_FORMAT_P010; |
| 404 |
break; |
| 405 |
default: |
| 406 |
return kUnimplemented; |
| 407 |
} |
| 408 |
|
| 409 |
crabbyavif::avifResult result = |
| 410 |
crabbyavif::avifDecoderNthImage(fAvifDecoder.get(), options.fFrameIndex); |
| 411 |
if (result != crabbyavif::AVIF_RESULT_OK) { |
| 412 |
return kInvalidInput; |
| 413 |
} |
| 414 |
if (fGainmapOnly && !fAvifDecoder->image->gainMap) { |
| 415 |
return kInvalidInput; |
| 416 |
} |
| 417 |
|
| 418 |
// At this point we have the decoded image. Now we have to perform cropping, subset computation |
| 419 |
// and scaling. The right order of these operations is: |
| 420 |
// 1) Cropping (as described by the CleanAperture property). This has to be the first step to |
| 421 |
// ensure that we don't accidentally expose the non-cropped portions of the image to the |
| 422 |
// subsequent operations. |
| 423 |
// 2) Subset computation (as requested by options.fSubset). |
| 424 |
// 3) Scaling (to match dstInfo.dimensions() if necessary). This has to be the last step to |
| 425 |
// ensure that we never fill in more pixels than what is requested by dstInfo.dimensions(). |
| 426 |
|
| 427 |
crabbyavif::avifImage* image = |
| 428 |
fGainmapOnly ? fAvifDecoder->image->gainMap->image : fAvifDecoder->image; |
| 429 |
using AvifImagePtr = |
| 430 |
std::unique_ptr<crabbyavif::avifImage, decltype(&crabbyavif::crabby_avifImageDestroy)>; |
| 431 |
|
| 432 |
// cropped_image is a view into the underlying image. It can be safely deleted once the pixels |
| 433 |
// are converted into RGB (or when it goes out of scope in one of the error paths). |
| 434 |
AvifImagePtr cropped_image{nullptr, crabbyavif::crabby_avifImageDestroy}; |
| 435 |
if (image->transformFlags & crabbyavif::AVIF_TRANSFORM_CLAP) { |
| 436 |
crabbyavif::avifCropRect rect; |
| 437 |
if (crabbyavif::crabby_avifCropRectConvertCleanApertureBox( |
| 438 |
&rect, &image->clap, image->width, image->height, image->yuvFormat, nullptr)) { |
| 439 |
cropped_image.reset(crabbyavif::crabby_avifImageCreateEmpty()); |
| 440 |
result = crabbyavif::crabby_avifImageSetViewRect(cropped_image.get(), image, &rect); |
| 441 |
if (result != crabbyavif::AVIF_RESULT_OK) { |
| 442 |
return kInvalidInput; |
| 443 |
} |
| 444 |
image = cropped_image.get(); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
AvifImagePtr subset_image{nullptr, crabbyavif::crabby_avifImageDestroy}; |
| 449 |
if (options.fSubset) { |
| 450 |
const crabbyavif::avifCropRect rect{ |
| 451 |
.x = static_cast<uint32_t>(options.fSubset->x()), |
| 452 |
.y = static_cast<uint32_t>(options.fSubset->y()), |
| 453 |
.width = static_cast<uint32_t>(options.fSubset->width()), |
| 454 |
.height = static_cast<uint32_t>(options.fSubset->height())}; |
| 455 |
subset_image.reset(crabbyavif::crabby_avifImageCreateEmpty()); |
| 456 |
result = crabbyavif::crabby_avifImageSetViewRect(subset_image.get(), image, &rect); |
| 457 |
if (result != crabbyavif::AVIF_RESULT_OK) { |
| 458 |
return kInvalidInput; |
| 459 |
} |
| 460 |
image = subset_image.get(); |
| 461 |
} |
| 462 |
|
| 463 |
AvifImagePtr scaled_image{nullptr, crabbyavif::crabby_avifImageDestroy}; |
| 464 |
if (dstInfo.width() != image->width || dstInfo.height() != image->height) { |
| 465 |
// |image| contains plane pointers which point to Android MediaCodec's buffers. Those |
| 466 |
// buffers are read-only and hence we cannot scale in place. Make a copy of the image and |
| 467 |
// scale the copied image. |
| 468 |
scaled_image.reset(crabbyavif::crabby_avifImageCreateEmpty()); |
| 469 |
result = crabbyavif::crabby_avifImageCopy( |
| 470 |
scaled_image.get(), image, crabbyavif::AVIF_PLANES_ALL); |
| 471 |
if (result != crabbyavif::AVIF_RESULT_OK) { |
| 472 |
return kInvalidInput; |
| 473 |
} |
| 474 |
image = scaled_image.get(); |
| 475 |
result = crabbyavif::avifImageScale( |
| 476 |
image, dstInfo.width(), dstInfo.height(), &fAvifDecoder->diag); |
| 477 |
if (result != crabbyavif::AVIF_RESULT_OK) { |
| 478 |
return kInvalidInput; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
crabbyavif::avifRGBImage rgbImage; |
| 483 |
crabbyavif::avifRGBImageSetDefaults(&rgbImage, image); |
| 484 |
|
| 485 |
switch (dstInfo.colorType()) { |
| 486 |
case kRGBA_8888_SkColorType: |
| 487 |
rgbImage.depth = 8; |
| 488 |
rgbImage.format = crabbyavif::AVIF_RGB_FORMAT_RGBA; |
| 489 |
break; |
| 490 |
case kBGRA_8888_SkColorType: |
| 491 |
rgbImage.depth = 8; |
| 492 |
rgbImage.format = crabbyavif::AVIF_RGB_FORMAT_BGRA; |
| 493 |
break; |
| 494 |
case kRGBA_F16_SkColorType: |
| 495 |
rgbImage.depth = 16; |
| 496 |
rgbImage.format = crabbyavif::AVIF_RGB_FORMAT_RGBA; |
| 497 |
rgbImage.isFloat = crabbyavif::CRABBY_AVIF_TRUE; |
| 498 |
break; |
| 499 |
case kRGBA_1010102_SkColorType: |
| 500 |
rgbImage.depth = 10; |
| 501 |
rgbImage.format = crabbyavif::AVIF_RGB_FORMAT_RGBA1010102; |
| 502 |
break; |
| 503 |
case kRGB_565_SkColorType: |
| 504 |
rgbImage.depth = 8; |
| 505 |
rgbImage.format = crabbyavif::AVIF_RGB_FORMAT_RGB565; |
| 506 |
break; |
| 507 |
default: |
| 508 |
// Not reached because of the checks in conversionSupported(). |
| 509 |
return kUnimplemented; |
| 510 |
} |
| 511 |
|
| 512 |
rgbImage.pixels = static_cast<uint8_t*>(dst); |
| 513 |
rgbImage.rowBytes = dstRowBytes; |
| 514 |
rgbImage.chromaUpsampling = crabbyavif::AVIF_CHROMA_UPSAMPLING_FASTEST; |
| 515 |
rgbImage.alphaPremultiplied = (dstInfo.alphaType() == kPremul_SkAlphaType) |
| 516 |
? crabbyavif::CRABBY_AVIF_TRUE |
| 517 |
: crabbyavif::CRABBY_AVIF_FALSE; |
| 518 |
|
| 519 |
result = crabbyavif::avifImageYUVToRGB(image, &rgbImage); |
| 520 |
if (result != crabbyavif::AVIF_RESULT_OK) { |
| 521 |
return kInvalidInput; |
| 522 |
} |
| 523 |
|
| 524 |
*rowsDecoded = image->height; |
| 525 |
return kSuccess; |
| 526 |
} |
| 527 |
|
| 528 |
bool SkCrabbyAvifCodec::onGetGainmapCodec(SkGainmapInfo* info, |
| 529 |
std::unique_ptr<SkCodec>* gainmapCodec) { |
| 530 |
if (!gainmapCodec || !info || !fAvifDecoder->image || !fAvifDecoder->image->gainMap || |
| 531 |
!PopulateGainmapInfo(*fAvifDecoder->image->gainMap, *fAvifDecoder->image, info)) { |
| 532 |
return false; |
| 533 |
} |
| 534 |
Result result; |
| 535 |
*gainmapCodec = SkCrabbyAvifCodec::MakeFromData( |
| 536 |
/*stream=*/nullptr, fData, &result, /*gainmapOnly=*/true); |
| 537 |
return static_cast<bool>(*gainmapCodec); |
| 538 |
} |
| 539 |
|
| 540 |
namespace SkAvifDecoder { |
| 541 |
namespace CrabbyAvif { |
| 542 |
|
| 543 |
bool IsAvif(const void* data, size_t len) { return SkCrabbyAvifCodec::IsAvif(data, len); } |
| 544 |
|
| 545 |
std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream, |
| 546 |
SkCodec::Result* outResult, |
| 547 |
SkCodecs::DecodeContext) { |
| 548 |
SkCodec::Result resultStorage; |
| 549 |
if (!outResult) { |
| 550 |
outResult = &resultStorage; |
| 551 |
} |
| 552 |
return SkCrabbyAvifCodec::MakeFromStream(std::move(stream), outResult); |
| 553 |
} |
| 554 |
|
| 555 |
std::unique_ptr<SkCodec> Decode(sk_sp<const SkData> data, |
| 556 |
SkCodec::Result* outResult, |
| 557 |
SkCodecs::DecodeContext) { |
| 558 |
if (!data) { |
| 559 |
if (outResult) { |
| 560 |
*outResult = SkCodec::kInvalidInput; |
| 561 |
} |
| 562 |
return nullptr; |
| 563 |
} |
| 564 |
return Decode(SkMemoryStream::Make(std::move(data)), outResult, nullptr); |
| 565 |
} |
| 566 |
|
| 567 |
} // namespace CrabbyAvif |
| 568 |
} // namespace SkAvifDecoder |