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