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        return WebPDemuxGetI(mDemux, WEBP_FF_CANVAS_WIDTH);
36    }
37
38    virtual int getHeight() const {
39        return WebPDemuxGetI(mDemux, WEBP_FF_CANVAS_HEIGHT);
40    }
41
42    virtual bool isOpaque() const {
43        return !(mFormatFlags & ALPHA_FLAG);
44    }
45
46    virtual int getFrameCount() const {
47        return WebPDemuxGetI(mDemux, WEBP_FF_FRAME_COUNT);
48    }
49
50    virtual int getDefaultLoopCount() const {
51        return mLoopCount;
52    }
53
54    virtual FrameSequenceState* createState() const;
55
56    WebPDemuxer* getDemuxer() const { return mDemux; }
57
58    bool isKeyFrame(size_t frameNr) const { return mIsKeyFrame[frameNr]; }
59
60private:
61    void constructDependencyChain();
62
63    WebPData mData;
64    WebPDemuxer* mDemux;
65    int mLoopCount;
66    uint32_t mFormatFlags;
67    // mIsKeyFrame[i] is true if ith canvas can be constructed without decoding any prior frames.
68    bool* mIsKeyFrame;
69};
70
71// Produces frames of a possibly-animated WebP file for display.
72class FrameSequenceState_webp : public FrameSequenceState {
73public:
74    FrameSequenceState_webp(const FrameSequence_webp& frameSequence);
75    virtual ~FrameSequenceState_webp();
76
77    // Returns frame's delay time in milliseconds.
78    virtual long drawFrame(int frameNr,
79            Color8888* outputPtr, int outputPixelStride, int previousFrameNr);
80
81private:
82    void initializeFrame(const WebPIterator& currIter, Color8888* currBuffer, int currStride,
83            const WebPIterator& prevIter, const Color8888* prevBuffer, int prevStride);
84    bool decodeFrame(const WebPIterator& iter, Color8888* currBuffer, int currStride,
85            const WebPIterator& prevIter, const Color8888* prevBuffer, int prevStride);
86
87    const FrameSequence_webp& mFrameSequence;
88    WebPDecoderConfig mDecoderConfig;
89    Color8888* mPreservedBuffer;
90};
91
92#endif //RASTERMILL_FRAMESQUENCE_WEBP_H
93