| 1 |
/* |
| 2 |
* Copyright 2022 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/SkAvifCodec.h" |
| 9 |
|
| 10 |
#include "include/codec/SkAvifDecoder.h" |
| 11 |
#include "include/codec/SkCodec.h" |
| 12 |
#include "include/codec/SkCodecAnimation.h" |
| 13 |
#include "include/core/SkColorType.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkSize.h" |
| 16 |
#include "include/core/SkStream.h" |
| 17 |
#include "include/core/SkTypes.h" |
| 18 |
#include "modules/skcms/skcms.h" |
| 19 |
#include "src/codec/SkCodecPriv.h" |
| 20 |
#include "src/core/SkStreamPriv.h" |
| 21 |
|
| 22 |
#include <cstdint> |
| 23 |
#include <cstring> |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
#include "avif/avif.h" |
| 27 |
|
| 28 |
void AvifDecoderDeleter::operator()(avifDecoder* decoder) const { |
| 29 |
if (decoder != nullptr) { |
| 30 |
avifDecoderDestroy(decoder); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
bool SkAvifCodec::IsAvif(const void* buffer, size_t bytesRead) { |
| 35 |
avifROData avifData = {static_cast<const uint8_t*>(buffer), bytesRead}; |
| 36 |
bool isAvif = avifPeekCompatibleFileType(&avifData) == AVIF_TRUE; |
| 37 |
if (isAvif) return true; |
| 38 |
// Peeking sometimes fails if the ftyp box is too large. Check the signature |
| 39 |
// just to be sure. |
| 40 |
const char* bytes = static_cast<const char*>(buffer); |
| 41 |
isAvif = bytesRead >= 12 && !memcmp(&bytes[4], "ftyp", 4) && |
| 42 |
(!memcmp(&bytes[8], "avif", 4) || !memcmp(&bytes[8], "avis", 4)); |
| 43 |
return isAvif; |
| 44 |
} |
| 45 |
|
| 46 |
std::unique_ptr<SkCodec> SkAvifCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 47 |
Result* result) { |
| 48 |
SkASSERT(result); |
| 49 |
if (!stream) { |
| 50 |
*result = SkCodec::kInvalidInput; |
| 51 |
return nullptr; |
| 52 |
} |
| 53 |
AvifDecoder avifDecoder(avifDecoderCreate()); |
| 54 |
if (avifDecoder == nullptr) { |
| 55 |
*result = SkCodec::kInternalError; |
| 56 |
return nullptr; |
| 57 |
} |
| 58 |
avifDecoder->ignoreXMP = AVIF_TRUE; |
| 59 |
avifDecoder->ignoreExif = AVIF_TRUE; |
| 60 |
avifDecoder->allowProgressive = AVIF_FALSE; |
| 61 |
avifDecoder->allowIncremental = AVIF_FALSE; |
| 62 |
avifDecoder->strictFlags = AVIF_STRICT_DISABLED; |
| 63 |
// TODO(vigneshv): Enable threading based on number of CPU cores available. |
| 64 |
avifDecoder->maxThreads = 1; |
| 65 |
|
| 66 |
// libavif needs a contiguous data buffer. |
| 67 |
sk_sp<SkData> data = nullptr; |
| 68 |
if (stream->getMemoryBase()) { |
| 69 |
// It is safe to make without copy because we'll hold onto the stream. |
| 70 |
data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength()); |
| 71 |
} else { |
| 72 |
data = SkStreamPriv::CopyStreamToData(stream.get()); |
| 73 |
// If we are forced to copy the stream to a data, we can go ahead and |
| 74 |
// delete the stream. |
| 75 |
stream.reset(nullptr); |
| 76 |
} |
| 77 |
|
| 78 |
avifResult res = avifDecoderSetIOMemory(avifDecoder.get(), data->bytes(), data->size()); |
| 79 |
if (res != AVIF_RESULT_OK) { |
| 80 |
*result = SkCodec::kInternalError; |
| 81 |
return nullptr; |
| 82 |
} |
| 83 |
|
| 84 |
res = avifDecoderParse(avifDecoder.get()); |
| 85 |
if (res != AVIF_RESULT_OK) { |
| 86 |
*result = SkCodec::kInvalidInput; |
| 87 |
return nullptr; |
| 88 |
} |
| 89 |
|
| 90 |
std::unique_ptr<SkCodecs::ColorProfile> profile; |
| 91 |
// TODO(vigneshv): Get ICC Profile from the avif decoder. |
| 92 |
|
| 93 |
const int bitsPerComponent = avifDecoder->image->depth > 8 ? 16 : 8; |
| 94 |
SkEncodedInfo::Color color; |
| 95 |
SkEncodedInfo::Alpha alpha; |
| 96 |
if (avifDecoder->alphaPresent) { |
| 97 |
color = SkEncodedInfo::kRGBA_Color; |
| 98 |
alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 99 |
} else { |
| 100 |
color = SkEncodedInfo::kRGB_Color; |
| 101 |
alpha = SkEncodedInfo::kOpaque_Alpha; |
| 102 |
} |
| 103 |
SkEncodedInfo info = SkEncodedInfo::Make(avifDecoder->image->width, |
| 104 |
avifDecoder->image->height, |
| 105 |
color, |
| 106 |
alpha, |
| 107 |
bitsPerComponent, |
| 108 |
std::move(profile), |
| 109 |
avifDecoder->image->depth); |
| 110 |
bool animation = avifDecoder->imageCount > 1; |
| 111 |
*result = kSuccess; |
| 112 |
return std::unique_ptr<SkCodec>(new SkAvifCodec(std::move(info), |
| 113 |
std::move(stream), |
| 114 |
std::move(data), |
| 115 |
std::move(avifDecoder), |
| 116 |
kDefault_SkEncodedOrigin, |
| 117 |
animation)); |
| 118 |
} |
| 119 |
|
| 120 |
SkAvifCodec::SkAvifCodec(SkEncodedInfo&& info, |
| 121 |
std::unique_ptr<SkStream> stream, |
| 122 |
sk_sp<const SkData> data, |
| 123 |
AvifDecoder avifDecoder, |
| 124 |
SkEncodedOrigin origin, |
| 125 |
bool useAnimation) |
| 126 |
: INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, std::move(stream), origin) |
| 127 |
, fData(std::move(data)) |
| 128 |
, fAvifDecoder(std::move(avifDecoder)) |
| 129 |
, fUseAnimation(useAnimation) {} |
| 130 |
|
| 131 |
int SkAvifCodec::onGetFrameCount() { |
| 132 |
if (!fUseAnimation) { |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
if (fFrameHolder.size() == 0) { |
| 137 |
if (fAvifDecoder->imageCount <= 1) { |
| 138 |
fUseAnimation = false; |
| 139 |
return 1; |
| 140 |
} |
| 141 |
fFrameHolder.reserve(fAvifDecoder->imageCount); |
| 142 |
for (int i = 0; i < fAvifDecoder->imageCount; i++) { |
| 143 |
Frame* frame = fFrameHolder.appendNewFrame(fAvifDecoder->alphaPresent == AVIF_TRUE); |
| 144 |
frame->setXYWH(0, 0, fAvifDecoder->image->width, fAvifDecoder->image->height); |
| 145 |
frame->setDisposalMethod(SkCodecAnimation::DisposalMethod::kKeep); |
| 146 |
avifImageTiming timing; |
| 147 |
avifDecoderNthImageTiming(fAvifDecoder.get(), i, &timing); |
| 148 |
frame->setDuration(timing.duration * 1000); |
| 149 |
frame->setRequiredFrame(SkCodec::kNoFrame); |
| 150 |
frame->setHasAlpha(fAvifDecoder->alphaPresent == AVIF_TRUE); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return fFrameHolder.size(); |
| 155 |
} |
| 156 |
|
| 157 |
const SkFrame* SkAvifCodec::FrameHolder::onGetFrame(int i) const { |
| 158 |
return static_cast<const SkFrame*>(this->frame(i)); |
| 159 |
} |
| 160 |
|
| 161 |
SkAvifCodec::Frame* SkAvifCodec::FrameHolder::appendNewFrame(bool hasAlpha) { |
| 162 |
const int i = this->size(); |
| 163 |
fFrames.emplace_back(i, |
| 164 |
hasAlpha ? SkEncodedInfo::kUnpremul_Alpha : SkEncodedInfo::kOpaque_Alpha); |
| 165 |
return &fFrames[i]; |
| 166 |
} |
| 167 |
|
| 168 |
const SkAvifCodec::Frame* SkAvifCodec::FrameHolder::frame(int i) const { |
| 169 |
SkASSERT(i >= 0 && i < this->size()); |
| 170 |
return &fFrames[i]; |
| 171 |
} |
| 172 |
|
| 173 |
bool SkAvifCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const { |
| 174 |
if (i >= fFrameHolder.size()) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
const Frame* frame = fFrameHolder.frame(i); |
| 179 |
if (!frame) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
if (frameInfo) { |
| 184 |
frame->fillIn(frameInfo, true); |
| 185 |
} |
| 186 |
|
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
int SkAvifCodec::onGetRepetitionCount() { return kRepetitionCountInfinite; } |
| 191 |
|
| 192 |
SkCodec::IsAnimated SkAvifCodec::onIsAnimated() { |
| 193 |
if (!fUseAnimation || fAvifDecoder->imageCount <= 1) { |
| 194 |
return IsAnimated::kNo; |
| 195 |
} |
| 196 |
return IsAnimated::kYes; |
| 197 |
} |
| 198 |
|
| 199 |
SkCodec::Result SkAvifCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 200 |
void* dst, |
| 201 |
size_t dstRowBytes, |
| 202 |
const Options& options, |
| 203 |
int* rowsDecoded) { |
| 204 |
if (options.fSubset) { |
| 205 |
return kUnimplemented; |
| 206 |
} |
| 207 |
|
| 208 |
const SkColorType dstColorType = dstInfo.colorType(); |
| 209 |
if (dstColorType != kRGBA_8888_SkColorType |
| 210 |
&& dstColorType != kBGRA_8888_SkColorType |
| 211 |
&& dstColorType != kRGBA_F16_SkColorType) { |
| 212 |
// TODO(vigneshv): Check if more color types need to be supported. |
| 213 |
// Currently android supports at least RGB565 which is not |
| 214 |
// supported here. |
| 215 |
return kUnimplemented; |
| 216 |
} |
| 217 |
|
| 218 |
avifResult result = avifDecoderNthImage(fAvifDecoder.get(), options.fFrameIndex); |
| 219 |
if (result != AVIF_RESULT_OK) { |
| 220 |
return kInvalidInput; |
| 221 |
} |
| 222 |
|
| 223 |
if (this->dimensions() != dstInfo.dimensions()) { |
| 224 |
result = avifImageScale( |
| 225 |
fAvifDecoder->image, dstInfo.width(), dstInfo.height(), &fAvifDecoder->diag); |
| 226 |
if (result != AVIF_RESULT_OK) { |
| 227 |
return kInvalidInput; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
avifRGBImage rgbImage; |
| 232 |
avifRGBImageSetDefaults(&rgbImage, fAvifDecoder->image); |
| 233 |
|
| 234 |
if (dstColorType == kRGBA_8888_SkColorType) { |
| 235 |
rgbImage.depth = 8; |
| 236 |
} else if (dstColorType == kBGRA_8888_SkColorType){ |
| 237 |
rgbImage.depth = 8; |
| 238 |
rgbImage.format = AVIF_RGB_FORMAT_BGRA; |
| 239 |
} else if (dstColorType == kRGBA_F16_SkColorType) { |
| 240 |
rgbImage.depth = 16; |
| 241 |
rgbImage.isFloat = AVIF_TRUE; |
| 242 |
} |
| 243 |
|
| 244 |
rgbImage.pixels = static_cast<uint8_t*>(dst); |
| 245 |
rgbImage.rowBytes = dstRowBytes; |
| 246 |
rgbImage.chromaUpsampling = AVIF_CHROMA_UPSAMPLING_FASTEST; |
| 247 |
|
| 248 |
result = avifImageYUVToRGB(fAvifDecoder->image, &rgbImage); |
| 249 |
if (result != AVIF_RESULT_OK) { |
| 250 |
return kInvalidInput; |
| 251 |
} |
| 252 |
|
| 253 |
*rowsDecoded = fAvifDecoder->image->height; |
| 254 |
return kSuccess; |
| 255 |
} |
| 256 |
|
| 257 |
namespace SkAvifDecoder { |
| 258 |
namespace LibAvif { |
| 259 |
|
| 260 |
bool IsAvif(const void* data, size_t len) { |
| 261 |
return SkAvifCodec::IsAvif(data, len); |
| 262 |
} |
| 263 |
|
| 264 |
std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream, |
| 265 |
SkCodec::Result* outResult, |
| 266 |
SkCodecs::DecodeContext) { |
| 267 |
SkCodec::Result resultStorage; |
| 268 |
if (!outResult) { |
| 269 |
outResult = &resultStorage; |
| 270 |
} |
| 271 |
return SkAvifCodec::MakeFromStream(std::move(stream), outResult); |
| 272 |
} |
| 273 |
|
| 274 |
std::unique_ptr<SkCodec> Decode(sk_sp<const SkData> data, |
| 275 |
SkCodec::Result* outResult, |
| 276 |
SkCodecs::DecodeContext) { |
| 277 |
if (!data) { |
| 278 |
if (outResult) { |
| 279 |
*outResult = SkCodec::kInvalidInput; |
| 280 |
} |
| 281 |
return nullptr; |
| 282 |
} |
| 283 |
return Decode(SkMemoryStream::Make(std::move(data)), outResult, nullptr); |
| 284 |
} |
| 285 |
|
| 286 |
} // namespace LibAvif |
| 287 |
} // namespace SkAvifDecoder |