1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ImageLogging.h" // Must appear first
#include "nsJXLDecoder.h"
#include "AnimationParams.h"
#include "mozilla/CheckedInt.h"
#include "RasterImage.h"
#include "SurfacePipeFactory.h"
#include "mozilla/Vector.h"
using namespace mozilla::gfx;
namespace mozilla::image {
static LazyLogModule sJXLLog("JXLDecoder");
nsJXLDecoder::nsJXLDecoder(RasterImage* aImage)
: Decoder(aImage),
mLexer(Transition::ToUnbuffered(State::FINISHED_JXL_DATA, State::JXL_DATA,
SIZE_MAX),
Transition::To(State::DRAIN_FRAMES, 0)) {
MOZ_LOG(sJXLLog, LogLevel::Debug,
("[this=%p] nsJXLDecoder::nsJXLDecoder", this));
}
nsresult nsJXLDecoder::InitInternal() {
bool premultiply = !(GetSurfaceFlags() & SurfaceFlags::NO_PREMULTIPLY_ALPHA);
mDecoder.reset(jxl_decoder_new(IsMetadataDecode(), premultiply));
return NS_OK;
}
nsJXLDecoder::~nsJXLDecoder() {
MOZ_LOG(sJXLLog, LogLevel::Debug,
("[this=%p] nsJXLDecoder::~nsJXLDecoder", this));
}
LexerResult nsJXLDecoder::DoDecode(SourceBufferIterator& aIterator,
IResumable* aOnResume) {
MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
return mLexer.Lex(aIterator, aOnResume,
[this](State aState, const char* aData, size_t aLength) {
switch (aState) {
case State::JXL_DATA:
return ReadJXLData(aData, aLength);
case State::DRAIN_FRAMES:
return DrainFrames();
case State::FINISHED_JXL_DATA:
return FinishedJXLData();
}
MOZ_CRASH("Unknown State");
});
}
LexerTransition<nsJXLDecoder::State> nsJXLDecoder::ReadJXLData(
const char* aData, size_t aLength) {
MOZ_ASSERT(mDecoder);
const uint8_t* currentData = reinterpret_cast<const uint8_t*>(aData);
size_t currentLength = aLength;
while (true) {
JxlDecoderStatus status = ProcessInput(¤tData, ¤tLength);
switch (status) {
case JxlDecoderStatus::Ok: {
if (!HasSize()) {
JxlBasicInfo basicInfo = jxl_decoder_get_basic_info(mDecoder.get());
if (!basicInfo.valid) {
if (currentLength == 0) {
return Transition::ContinueUnbuffered(State::JXL_DATA);
} else {
break;
}
}
if (basicInfo.width > INT32_MAX || basicInfo.height > INT32_MAX) {
return Transition::TerminateFailure();
}
PostSize(basicInfo.width, basicInfo.height);
if (basicInfo.has_alpha) {
PostHasTransparency();
}
if (!basicInfo.is_animated) {
PostFrameCount(1);
if (IsMetadataDecode()) {
return Transition::TerminateSuccess();
}
}
}
// Handle animation metadata when first frame header is available.
if (jxl_decoder_is_frame_ready(mDecoder.get()) && !HasAnimation()) {
JxlBasicInfo basicInfo = jxl_decoder_get_basic_info(mDecoder.get());
if (basicInfo.is_animated) {
JxlFrameInfo frameInfo = jxl_decoder_get_frame_info(mDecoder.get());
PostIsAnimated(
FrameTimeout::FromRawMilliseconds(frameInfo.duration_ms));
PostLoopCount(
(basicInfo.num_loops == 0 || basicInfo.num_loops > INT32_MAX)
? -1
: static_cast<int32_t>(basicInfo.num_loops));
if (IsMetadataDecode()) {
return Transition::TerminateSuccess();
}
}
}
switch (HandleFrameOutput()) {
case FrameOutputResult::BufferAllocated:
break;
case FrameOutputResult::FrameAdvanced:
return Transition::ContinueUnbufferedAfterYield(
State::JXL_DATA, aLength - currentLength);
case FrameOutputResult::DecodeComplete:
return Transition::TerminateSuccess();
case FrameOutputResult::NoOutput:
if (currentLength == 0) {
return Transition::ContinueUnbuffered(State::JXL_DATA);
}
break;
case FrameOutputResult::Error:
return Transition::TerminateFailure();
}
break;
}
case JxlDecoderStatus::NeedMoreData: {
if (currentLength == 0) {
return Transition::ContinueUnbuffered(State::JXL_DATA);
}
break;
}
case JxlDecoderStatus::Error:
return Transition::TerminateFailure();
}
}
}
JxlDecoderStatus nsJXLDecoder::ProcessInput(const uint8_t** aData,
size_t* aLength) {
uint8_t* bufferPtr = mPixelBuffer.empty() ? nullptr : mPixelBuffer.begin();
size_t bufferLen = mPixelBuffer.length();
return jxl_decoder_process_data(mDecoder.get(), aData, aLength, bufferPtr,
bufferLen);
}
nsJXLDecoder::FrameOutputResult nsJXLDecoder::HandleFrameOutput() {
bool frameNeedsBuffer = jxl_decoder_is_frame_ready(mDecoder.get());
// Allocate buffer for frame rendering.
if (frameNeedsBuffer && mPixelBuffer.empty()) {
OrientedIntSize size = Size();
CheckedInt<size_t> bufferSize =
CheckedInt<size_t>(size.width) * size.height * 4;
if (!bufferSize.isValid() || !mPixelBuffer.resize(bufferSize.value())) {
MOZ_LOG(sJXLLog, LogLevel::Error,
("[this=%p] nsJXLDecoder::HandleFrameOutput -- "
"failed to allocate pixel buffer\n",
this));
return FrameOutputResult::Error;
}
return FrameOutputResult::BufferAllocated;
}
// Frame rendering complete. The pixel buffer has been filled by
// jxl_decoder_process_data. Send it through the surface pipeline.
if (!frameNeedsBuffer && !mPixelBuffer.empty()) {
nsresult rv = ProcessFrame(mPixelBuffer);
if (NS_FAILED(rv)) {
return FrameOutputResult::Error;
}
bool hasMoreFrames = jxl_decoder_has_more_frames(mDecoder.get());
if (IsFirstFrameDecode() || !HasAnimation() || !hasMoreFrames) {
PostFrameCount(mFrameIndex + 1);
PostDecodeDone();
return FrameOutputResult::DecodeComplete;
}
mFrameIndex++;
mPixelBuffer.clear();
return FrameOutputResult::FrameAdvanced;
}
return FrameOutputResult::NoOutput;
}
LexerTransition<nsJXLDecoder::State> nsJXLDecoder::DrainFrames() {
// Called via the truncated transition when the source is complete. Since
// jxl-rs buffers all input data internally, it may be able to produce
// remaining frames without additional source bytes.
while (true) {
const uint8_t* noData = nullptr;
size_t noLength = 0;
JxlDecoderStatus status = ProcessInput(&noData, &noLength);
switch (status) {
case JxlDecoderStatus::Ok: {
if (!HasSize()) {
return Transition::TerminateFailure();
}
switch (HandleFrameOutput()) {
case FrameOutputResult::BufferAllocated:
break;
case FrameOutputResult::FrameAdvanced:
return Transition::ToAfterYield(State::DRAIN_FRAMES);
case FrameOutputResult::DecodeComplete:
case FrameOutputResult::NoOutput:
return Transition::TerminateSuccess();
case FrameOutputResult::Error:
return Transition::TerminateFailure();
}
break;
}
case JxlDecoderStatus::NeedMoreData:
return Transition::TerminateSuccess();
case JxlDecoderStatus::Error:
return Transition::TerminateFailure();
}
}
}
LexerTransition<nsJXLDecoder::State> nsJXLDecoder::FinishedJXLData() {
MOZ_ASSERT_UNREACHABLE("Read the entire address space?");
return Transition::TerminateFailure();
}
nsresult nsJXLDecoder::ProcessFrame(Vector<uint8_t>& aPixelBuffer) {
MOZ_ASSERT(HasSize());
MOZ_ASSERT(mDecoder);
JxlBasicInfo basicInfo = jxl_decoder_get_basic_info(mDecoder.get());
OrientedIntSize size = Size();
Maybe<AnimationParams> animParams;
if (HasAnimation()) {
JxlFrameInfo frameInfo = jxl_decoder_get_frame_info(mDecoder.get());
if (!frameInfo.frame_duration_valid) {
return NS_ERROR_FAILURE;
}
// jxl-rs renders complete frames, replacing the previous frame entirely.
animParams.emplace(FullFrame().ToUnknownRect(),
FrameTimeout::FromRawMilliseconds(frameInfo.duration_ms),
mFrameIndex, BlendMethod::SOURCE, DisposalMethod::KEEP);
}
SurfaceFormat inFormat = SurfaceFormat::R8G8B8A8;
SurfaceFormat outFormat =
basicInfo.has_alpha ? SurfaceFormat::OS_RGBA : SurfaceFormat::OS_RGBX;
SurfacePipeFlags pipeFlags = SurfacePipeFlags();
Maybe<SurfacePipe> pipe = SurfacePipeFactory::CreateSurfacePipe(
this, size, OutputSize(), FullFrame(), inFormat, outFormat, animParams,
nullptr, pipeFlags);
if (!pipe) {
return NS_ERROR_FAILURE;
}
uint8_t* currentRow = aPixelBuffer.begin();
for (int32_t y = 0; y < size.height; ++y) {
WriteState result =
pipe->WriteBuffer(reinterpret_cast<uint32_t*>(currentRow));
if (result == WriteState::FAILURE) {
return NS_ERROR_FAILURE;
}
currentRow += size.width * 4;
}
if (Maybe<SurfaceInvalidRect> invalidRect = pipe->TakeInvalidRect()) {
PostInvalidation(invalidRect->mInputSpaceRect,
Some(invalidRect->mOutputSpaceRect));
}
PostFrameStop(basicInfo.has_alpha ? Opacity::SOME_TRANSPARENCY
: Opacity::FULLY_OPAQUE);
return NS_OK;
}
} // namespace mozilla::image
|