1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef RASTERMILL_FRAMESQUENCE_WEBP_H
18#define RASTERMILL_FRAMESQUENCE_WEBP_H
19
20#include "config.h"
21#include "webp/decode.h"
22#include "webp/demux.h"
23
24#include "Stream.h"
25#include "Color.h"
26#include "FrameSequence.h"
27
28// Parser for a possibly-animated WebP bitstream.
29class FrameSequence_webp : public FrameSequence {
30public:
31    FrameSequence_webp(Stream* stream);
32    virtual ~FrameSequence_webp();
33
34    virtual int getWidth() const {
35        if (!mDemux) {
36            return 0;
37        }
38        return WebPDemuxGetI(mDemux, WEBP_FF_CANVAS_WIDTH);
39    }
40
41    virtual int getHeight() const {
42        if (!mDemux) {
43            return 0;
44        }
45        return WebPDemuxGetI(mDemux, WEBP_FF_CANVAS_HEIGHT);
46    }
47
48    virtual bool isOpaque() const {
49        return !(mFormatFlags & ALPHA_FLAG);
50    }
51
52    virtual int getFrameCount() const {
53        if (!mDemux) {
54            return 0;
55        }
56        return WebPDemuxGetI(mDemux, WEBP_FF_FRAME_COUNT);
57    }
58
59    virtual int getDefaultLoopCount() const {
60        return mLoopCount;
61    }
62
63    virtual jobject getRawByteBuffer() const {
64        return mRawByteBuffer;
65    }
66
67    virtual FrameSequenceState* createState() const;
68
69    WebPDemuxer* getDemuxer() const { return mDemux; }
70
71    bool isKeyFrame(size_t frameNr) const { return mIsKeyFrame[frameNr]; }
72
73private:
74    void constructDependencyChain();
75
76    WebPData mData;
77    WebPDemuxer* mDemux;
78    int mLoopCount;
79    uint32_t mFormatFlags;
80    // mIsKeyFrame[i] is true if ith canvas can be constructed without decoding any prior frames.
81    bool* mIsKeyFrame;
82    jobject mRawByteBuffer = nullptr;
83};
84
85// Produces frames of a possibly-animated WebP file for display.
86class FrameSequenceState_webp : public FrameSequenceState {
87public:
88    FrameSequenceState_webp(const FrameSequence_webp& frameSequence);
89    virtual ~FrameSequenceState_webp();
90
91    // Returns frame's delay time in milliseconds.
92    virtual long drawFrame(int frameNr,
93            Color8888* outputPtr, int outputPixelStride, int previousFrameNr);
94
95private:
96    void initializeFrame(const WebPIterator& currIter, Color8888* currBuffer, int currStride,
97            const WebPIterator& prevIter, const Color8888* prevBuffer, int prevStride);
98    bool decodeFrame(const WebPIterator& iter, Color8888* currBuffer, int currStride,
99            const WebPIterator& prevIter, const Color8888* prevBuffer, int prevStride);
100
101    const FrameSequence_webp& mFrameSequence;
102    WebPDecoderConfig mDecoderConfig;
103    Color8888* mPreservedBuffer;
104};
105
106#endif //RASTERMILL_FRAMESQUENCE_WEBP_H
107