1b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi/*
2b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi * Copyright (C) 2013 The Android Open Source Project
3b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi *
4b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi * Licensed under the Apache License, Version 2.0 (the "License");
5b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi * you may not use this file except in compliance with the License.
6b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi * You may obtain a copy of the License at
7b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi *
8b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi *      http://www.apache.org/licenses/LICENSE-2.0
9b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi *
10b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi * Unless required by applicable law or agreed to in writing, software
11b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi * distributed under the License is distributed on an "AS IS" BASIS,
12b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi * See the License for the specific language governing permissions and
14b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi * limitations under the License.
15b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi */
16b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
17b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#include <string.h>
18b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#include "JNIHelpers.h"
19b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#include "utils/log.h"
20b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#include "utils/math.h"
21b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#include "webp/format_constants.h"
22b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
23b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#include "FrameSequence_webp.h"
24b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
25b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#define WEBP_DEBUG 0
26b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
27b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi////////////////////////////////////////////////////////////////////////////////
28b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi// Frame sequence
29b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi////////////////////////////////////////////////////////////////////////////////
30b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
31b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic uint32_t GetLE32(const uint8_t* const data) {
32b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    return MKFOURCC(data[0], data[1], data[2], data[3]);
33b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
34b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
35b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi// Returns true if the frame covers full canvas.
36b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic bool isFullFrame(const WebPIterator& frame, int canvasWidth, int canvasHeight) {
37b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    return (frame.width == canvasWidth && frame.height == canvasHeight);
38b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
39b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
409b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi// Returns true if the rectangle defined by 'frame' contains pixel (x, y).
419b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshistatic bool FrameContainsPixel(const WebPIterator& frame, int x, int y) {
429b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi    const int left = frame.x_offset;
439b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi    const int right = left + frame.width;
449b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi    const int top = frame.y_offset;
459b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi    const int bottom = top + frame.height;
469b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi    return x >= left && x < right && y >= top && y < bottom;
479b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi}
489b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi
49b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi// Construct mIsKeyFrame array.
50b34f1da83570613bb349f8026d4325552ac495edUrvang Joshivoid FrameSequence_webp::constructDependencyChain() {
51b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const size_t frameCount = getFrameCount();
52b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mIsKeyFrame = new bool[frameCount];
53b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasWidth = getWidth();
54b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasHeight = getHeight();
55b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
56b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPIterator prev;
57b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPIterator curr;
58b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
59b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // Note: WebPDemuxGetFrame() uses base-1 counting.
60b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    int ok = WebPDemuxGetFrame(mDemux, 1, &curr);
61b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    ALOG_ASSERT(ok, "Could not retrieve frame# 0");
62b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mIsKeyFrame[0] = true;  // 0th frame is always a key frame.
63b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    for (size_t i = 1; i < frameCount; i++) {
64b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        prev = curr;
65b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        ok = WebPDemuxGetFrame(mDemux, i + 1, &curr);  // Get ith frame.
66b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        ALOG_ASSERT(ok, "Could not retrieve frame# %d", i);
67b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
68b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        if ((!curr.has_alpha || curr.blend_method == WEBP_MUX_NO_BLEND) &&
69b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                isFullFrame(curr, canvasWidth, canvasHeight)) {
70b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            mIsKeyFrame[i] = true;
71b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        } else {
72b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            mIsKeyFrame[i] = (prev.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) &&
73b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    (isFullFrame(prev, canvasWidth, canvasHeight) || mIsKeyFrame[i - 1]);
74b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        }
75b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
76b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPDemuxReleaseIterator(&prev);
77b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPDemuxReleaseIterator(&curr);
78b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
79b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#if WEBP_DEBUG
80b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    ALOGD("Dependency chain:");
81b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    for (size_t i = 0; i < frameCount; i++) {
82b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        ALOGD("Frame# %zu: %s", i, mIsKeyFrame[i] ? "Key frame" : "NOT a key frame");
83b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
84b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#endif
85b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
86b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
87b34f1da83570613bb349f8026d4325552ac495edUrvang JoshiFrameSequence_webp::FrameSequence_webp(Stream* stream) {
886a61141137c7a46d747aa611c9caf62436bc119fChris Craik    if (stream->getRawBuffer() != NULL) {
896a61141137c7a46d747aa611c9caf62436bc119fChris Craik        mData.size = stream->getRawBufferSize();
906a61141137c7a46d747aa611c9caf62436bc119fChris Craik        mData.bytes = stream->getRawBufferAddr();
916a61141137c7a46d747aa611c9caf62436bc119fChris Craik        mRawByteBuffer = stream->getRawBuffer();
926a61141137c7a46d747aa611c9caf62436bc119fChris Craik    } else {
936a61141137c7a46d747aa611c9caf62436bc119fChris Craik        // Read RIFF header to get file size.
946a61141137c7a46d747aa611c9caf62436bc119fChris Craik        uint8_t riff_header[RIFF_HEADER_SIZE];
956a61141137c7a46d747aa611c9caf62436bc119fChris Craik        if (stream->read(riff_header, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE) {
966a61141137c7a46d747aa611c9caf62436bc119fChris Craik            ALOGE("WebP header load failed");
976a61141137c7a46d747aa611c9caf62436bc119fChris Craik            return;
986a61141137c7a46d747aa611c9caf62436bc119fChris Craik        }
996a61141137c7a46d747aa611c9caf62436bc119fChris Craik        mData.size = CHUNK_HEADER_SIZE + GetLE32(riff_header + TAG_SIZE);
1006a61141137c7a46d747aa611c9caf62436bc119fChris Craik        mData.bytes = new uint8_t[mData.size];
1016a61141137c7a46d747aa611c9caf62436bc119fChris Craik        memcpy((void*)mData.bytes, riff_header, RIFF_HEADER_SIZE);
1026a61141137c7a46d747aa611c9caf62436bc119fChris Craik
1036a61141137c7a46d747aa611c9caf62436bc119fChris Craik        // Read rest of the bytes.
1046a61141137c7a46d747aa611c9caf62436bc119fChris Craik        void* remaining_bytes = (void*)(mData.bytes + RIFF_HEADER_SIZE);
1056a61141137c7a46d747aa611c9caf62436bc119fChris Craik        size_t remaining_size = mData.size - RIFF_HEADER_SIZE;
1066a61141137c7a46d747aa611c9caf62436bc119fChris Craik        if (stream->read(remaining_bytes, remaining_size) != remaining_size) {
1076a61141137c7a46d747aa611c9caf62436bc119fChris Craik            ALOGE("WebP full load failed");
1086a61141137c7a46d747aa611c9caf62436bc119fChris Craik            return;
1096a61141137c7a46d747aa611c9caf62436bc119fChris Craik        }
110b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
111b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
112b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // Construct demux.
113b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mDemux = WebPDemux(&mData);
114b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    if (!mDemux) {
115b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        ALOGE("Parsing of WebP container file failed");
116b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        return;
117b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
118b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mLoopCount = WebPDemuxGetI(mDemux, WEBP_FF_LOOP_COUNT);
119b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mFormatFlags = WebPDemuxGetI(mDemux, WEBP_FF_FORMAT_FLAGS);
120b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#if WEBP_DEBUG
121b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    ALOGD("FrameSequence_webp created with size = %d x %d, number of frames = %d, flags = 0x%X",
122b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi          getWidth(), getHeight(), getFrameCount(), mFormatFlags);
123b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#endif
124b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    constructDependencyChain();
125b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
126b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
127b34f1da83570613bb349f8026d4325552ac495edUrvang JoshiFrameSequence_webp::~FrameSequence_webp() {
128b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPDemuxDelete(mDemux);
129b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    delete[] mIsKeyFrame;
1306a61141137c7a46d747aa611c9caf62436bc119fChris Craik    if (mRawByteBuffer == NULL) {
1316a61141137c7a46d747aa611c9caf62436bc119fChris Craik        delete[] mData.bytes;
1326a61141137c7a46d747aa611c9caf62436bc119fChris Craik    }
133b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
134b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
135b34f1da83570613bb349f8026d4325552ac495edUrvang JoshiFrameSequenceState* FrameSequence_webp::createState() const {
136b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    return new FrameSequenceState_webp(*this);
137b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
138b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
139b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi////////////////////////////////////////////////////////////////////////////////
140b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi// draw helpers
141b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi////////////////////////////////////////////////////////////////////////////////
142b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
143b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic bool willBeCleared(const WebPIterator& iter) {
144b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    return iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND;
145b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
146b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
147b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi// return true if area of 'target' completely covers area of 'covered'
148b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic bool checkIfCover(const WebPIterator& target, const WebPIterator& covered) {
149b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int covered_x_max = covered.x_offset + covered.width;
150b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int target_x_max = target.x_offset + target.width;
151b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int covered_y_max = covered.y_offset + covered.height;
152b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int target_y_max = target.y_offset + target.height;
153b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    return target.x_offset <= covered.x_offset
154b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi           && covered_x_max <= target_x_max
155b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi           && target.y_offset <= covered.y_offset
156b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi           && covered_y_max <= target_y_max;
157b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
158b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
159b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi// Clear all pixels in a line to transparent.
160b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic void clearLine(Color8888* dst, int width) {
161b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    memset(dst, 0, width * sizeof(*dst));  // Note: Assumes TRANSPARENT == 0x0.
162b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
163b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
164b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi// Copy all pixels from 'src' to 'dst'.
165b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic void copyFrame(const Color8888* src, int srcStride, Color8888* dst, int dstStride,
166b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        int width, int height) {
167b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    for (int y = 0; y < height; y++) {
168b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        memcpy(dst, src, width * sizeof(*dst));
169b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        src += srcStride;
170b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        dst += dstStride;
171b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
172b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
173b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
174b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi////////////////////////////////////////////////////////////////////////////////
175b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi// Frame sequence state
176b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi////////////////////////////////////////////////////////////////////////////////
177b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
178b34f1da83570613bb349f8026d4325552ac495edUrvang JoshiFrameSequenceState_webp::FrameSequenceState_webp(const FrameSequence_webp& frameSequence) :
179b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        mFrameSequence(frameSequence) {
180b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPInitDecoderConfig(&mDecoderConfig);
181b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mDecoderConfig.output.is_external_memory = 1;
182b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mDecoderConfig.output.colorspace = MODE_rgbA;  // Pre-multiplied alpha mode.
183b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasWidth = mFrameSequence.getWidth();
184b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasHeight = mFrameSequence.getHeight();
185b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mPreservedBuffer = new Color8888[canvasWidth * canvasHeight];
186b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
187b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
188b34f1da83570613bb349f8026d4325552ac495edUrvang JoshiFrameSequenceState_webp::~FrameSequenceState_webp() {
189b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    delete[] mPreservedBuffer;
190b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
191b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
192b34f1da83570613bb349f8026d4325552ac495edUrvang Joshivoid FrameSequenceState_webp::initializeFrame(const WebPIterator& currIter, Color8888* currBuffer,
193b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        int currStride, const WebPIterator& prevIter, const Color8888* prevBuffer, int prevStride) {
194b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasWidth = mFrameSequence.getWidth();
195b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasHeight = mFrameSequence.getHeight();
196b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const bool currFrameIsKeyFrame = mFrameSequence.isKeyFrame(currIter.frame_num - 1);
197b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
198b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    if (currFrameIsKeyFrame) {  // Clear canvas.
199b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        for (int y = 0; y < canvasHeight; y++) {
200b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            Color8888* dst = currBuffer + y * currStride;
201b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            clearLine(dst, canvasWidth);
202b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        }
203b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    } else {
204b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        // Preserve previous frame as starting state of current frame.
205b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        copyFrame(prevBuffer, prevStride, currBuffer, currStride, canvasWidth, canvasHeight);
206b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
207b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        // Dispose previous frame rectangle to Background if needed.
208b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        bool prevFrameCompletelyCovered =
209b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                (!currIter.has_alpha || currIter.blend_method == WEBP_MUX_NO_BLEND) &&
210b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                checkIfCover(currIter, prevIter);
211b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        if ((prevIter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) &&
212b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                !prevFrameCompletelyCovered) {
213b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            Color8888* dst = currBuffer + prevIter.x_offset + prevIter.y_offset * currStride;
214b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            for (int j = 0; j < prevIter.height; j++) {
215b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                clearLine(dst, prevIter.width);
216b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                dst += currStride;
217b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            }
218b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        }
219b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
220b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
221b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
222b34f1da83570613bb349f8026d4325552ac495edUrvang Joshibool FrameSequenceState_webp::decodeFrame(const WebPIterator& currIter, Color8888* currBuffer,
223b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        int currStride, const WebPIterator& prevIter, const Color8888* prevBuffer, int prevStride) {
224b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    Color8888* dst = currBuffer + currIter.x_offset + currIter.y_offset * currStride;
225b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mDecoderConfig.output.u.RGBA.rgba = (uint8_t*)dst;
226b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mDecoderConfig.output.u.RGBA.stride = currStride * 4;
227b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    mDecoderConfig.output.u.RGBA.size = mDecoderConfig.output.u.RGBA.stride * currIter.height;
228b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
229b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const WebPData& currFrame = currIter.fragment;
230b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    if (WebPDecode(currFrame.bytes, currFrame.size, &mDecoderConfig) != VP8_STATUS_OK) {
231b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        return false;
232b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
233b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
234b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasWidth = mFrameSequence.getWidth();
235b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasHeight = mFrameSequence.getHeight();
236b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const bool currFrameIsKeyFrame = mFrameSequence.isKeyFrame(currIter.frame_num - 1);
237b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // During the decoding of current frame, we may have set some pixels to be transparent
238b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // (i.e. alpha < 255). However, the value of each of these pixels should have been determined
239b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // by blending it against the value of that pixel in the previous frame if WEBP_MUX_BLEND was
240b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // specified. So, we correct these pixels based on disposal method of the previous frame and
241b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // the previous frame buffer.
242b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    if (currIter.blend_method == WEBP_MUX_BLEND && !currFrameIsKeyFrame) {
243b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        if (prevIter.dispose_method == WEBP_MUX_DISPOSE_NONE) {
244b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            for (int y = 0; y < currIter.height; y++) {
245b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                const int canvasY = currIter.y_offset + y;
246b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                for (int x = 0; x < currIter.width; x++) {
247b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    const int canvasX = currIter.x_offset + x;
248b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    Color8888& currPixel = currBuffer[canvasY * currStride + canvasX];
249b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    // FIXME: Use alpha-blending when alpha is between 0 and 255.
250b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    if (!(currPixel & COLOR_8888_ALPHA_MASK)) {
251b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                        const Color8888 prevPixel = prevBuffer[canvasY * prevStride + canvasX];
252b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                        currPixel = prevPixel;
253b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    }
254b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                }
255b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            }
256b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        } else {  // prevIter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND
257b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            // Need to restore transparent pixels to as they were just after frame initialization.
258b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            // That is:
259b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            //   * Transparent if it belongs to previous frame rectangle <-- This is a no-op.
260b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            //   * Pixel in the previous canvas otherwise <-- Need to restore.
261b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            for (int y = 0; y < currIter.height; y++) {
262b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                const int canvasY = currIter.y_offset + y;
263b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                for (int x = 0; x < currIter.width; x++) {
264b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    const int canvasX = currIter.x_offset + x;
265b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    Color8888& currPixel = currBuffer[canvasY * currStride + canvasX];
266b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    // FIXME: Use alpha-blending when alpha is between 0 and 255.
2679b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi                    if (!(currPixel & COLOR_8888_ALPHA_MASK)
2689b38510c08561d2c71cc2aa2ea5432e46d27e82fUrvang Joshi                            && !FrameContainsPixel(prevIter, canvasX, canvasY)) {
269b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                        const Color8888 prevPixel = prevBuffer[canvasY * prevStride + canvasX];
270b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                        currPixel = prevPixel;
271b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                    }
272b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                }
273b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            }
274b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        }
275b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
276b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    return true;
277b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
278b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
279b34f1da83570613bb349f8026d4325552ac495edUrvang Joshilong FrameSequenceState_webp::drawFrame(int frameNr,
280b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        Color8888* outputPtr, int outputPixelStride, int previousFrameNr) {
281b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPDemuxer* demux = mFrameSequence.getDemuxer();
282b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    ALOG_ASSERT(demux, "Cannot drawFrame, mDemux is NULL");
283b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
284b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#if WEBP_DEBUG
285b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    ALOGD("  drawFrame called for frame# %d, previous frame# %d", frameNr, previousFrameNr);
286b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#endif
287b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
288b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasWidth = mFrameSequence.getWidth();
289b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int canvasHeight = mFrameSequence.getHeight();
290b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
291b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // Find the first frame to be decoded.
292b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    int start = max(previousFrameNr + 1, 0);
293b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    int earliestRequired = frameNr;
294b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    while (earliestRequired > start) {
295b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        if (mFrameSequence.isKeyFrame(earliestRequired)) {
296b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            start = earliestRequired;
297b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            break;
298b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        }
299b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        earliestRequired--;
300b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
301b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
302b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPIterator currIter;
303b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPIterator prevIter;
304b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    int ok = WebPDemuxGetFrame(demux, start, &currIter);  // Get frame number 'start - 1'.
305b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    ALOG_ASSERT(ok, "Could not retrieve frame# %d", start - 1);
306b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
307b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // Use preserve buffer only if needed.
308b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    Color8888* prevBuffer = (frameNr == 0) ? outputPtr : mPreservedBuffer;
309b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    int prevStride = (frameNr == 0) ? outputPixelStride : canvasWidth;
310b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    Color8888* currBuffer = outputPtr;
311b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    int currStride = outputPixelStride;
312b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
313b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    for (int i = start; i <= frameNr; i++) {
314b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        prevIter = currIter;
315b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        ok = WebPDemuxGetFrame(demux, i + 1, &currIter);  // Get ith frame.
316b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        ALOG_ASSERT(ok, "Could not retrieve frame# %d", i);
317b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#if WEBP_DEBUG
318b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        ALOGD("      producing frame %d (has_alpha = %d, dispose = %s, blend = %s, duration = %d)",
319b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi              i, currIter.has_alpha,
320b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi              (currIter.dispose_method == WEBP_MUX_DISPOSE_NONE) ? "none" : "background",
321b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi              (currIter.blend_method == WEBP_MUX_BLEND) ? "yes" : "no", currIter.duration);
322b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#endif
323b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        // We swap the prev/curr buffers as we go.
324b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        Color8888* tmpBuffer = prevBuffer;
325b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        prevBuffer = currBuffer;
326b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        currBuffer = tmpBuffer;
327b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
328b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        int tmpStride = prevStride;
329b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        prevStride = currStride;
330b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        currStride = tmpStride;
331b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
332b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#if WEBP_DEBUG
333b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        ALOGD("            prev = %p, curr = %p, out = %p, tmp = %p",
334b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi              prevBuffer, currBuffer, outputPtr, mPreservedBuffer);
335b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#endif
336b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        // Process this frame.
337b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        initializeFrame(currIter, currBuffer, currStride, prevIter, prevBuffer, prevStride);
338b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
339b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        if (i == frameNr || !willBeCleared(currIter)) {
340b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            if (!decodeFrame(currIter, currBuffer, currStride, prevIter, prevBuffer, prevStride)) {
341b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                ALOGE("Error decoding frame# %d", i);
342b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi                return -1;
343b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            }
344b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        }
345b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
346b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
347b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    if (outputPtr != currBuffer) {
348b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        copyFrame(currBuffer, currStride, outputPtr, outputPixelStride, canvasWidth, canvasHeight);
349b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    }
350b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
351b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    // Return last frame's delay.
352b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int frameCount = mFrameSequence.getFrameCount();
353b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int lastFrame = (frameNr + frameCount - 1) % frameCount;
354b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    ok = WebPDemuxGetFrame(demux, lastFrame, &currIter);
355b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    ALOG_ASSERT(ok, "Could not retrieve frame# %d", lastFrame - 1);
356b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const int lastFrameDelay = currIter.duration;
357b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
358b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPDemuxReleaseIterator(&currIter);
359b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    WebPDemuxReleaseIterator(&prevIter);
360b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
361b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    return lastFrameDelay;
362b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
363b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
364b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi////////////////////////////////////////////////////////////////////////////////
365b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi// Registry
366b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi////////////////////////////////////////////////////////////////////////////////
367b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
368b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi#include "Registry.h"
369b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
370b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic bool isWebP(void* header, int header_size) {
371b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    const uint8_t* const header_str = (const uint8_t*)header;
372b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    return (header_size >= RIFF_HEADER_SIZE) &&
373b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            !memcmp("RIFF", header_str, 4) &&
374b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi            !memcmp("WEBP", header_str + 8, 4);
375b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
376b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
3776a61141137c7a46d747aa611c9caf62436bc119fChris Craikstatic bool acceptsWebPBuffer() {
3786a61141137c7a46d747aa611c9caf62436bc119fChris Craik    return true;
3796a61141137c7a46d747aa611c9caf62436bc119fChris Craik}
3806a61141137c7a46d747aa611c9caf62436bc119fChris Craik
381b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic FrameSequence* createFramesequence(Stream* stream) {
382b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi    return new FrameSequence_webp(stream);
383b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi}
384b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
385b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic RegistryEntry gEntry = {
386b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        RIFF_HEADER_SIZE,
387b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        isWebP,
388b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        createFramesequence,
389b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi        NULL,
3906a61141137c7a46d747aa611c9caf62436bc119fChris Craik        acceptsWebPBuffer,
391b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi};
392b34f1da83570613bb349f8026d4325552ac495edUrvang Joshistatic Registry gRegister(gEntry);
393b34f1da83570613bb349f8026d4325552ac495edUrvang Joshi
394