aboutsummaryrefslogtreecommitdiffstats
path: root/src/images/SkImageDecoder_libpvjpeg.cpp
blob: b98763e6c99fafb0ba41fc146cd05bd2036304e4 (plain)
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
#include "SkImageDecoder.h"
#include "SkColor.h"
#include "SkColorPriv.h"
#include "SkDither.h"
#include "SkMath.h"
#include "SkStream.h"
#include "SkTemplates.h"
#include "SkUtils.h"

extern void ValidateHeap();

class SkPVJPEGImageDecoder : public SkImageDecoder {
protected:
    virtual bool onDecode(SkStream* stream, SkBitmap* bm,
                          SkBitmap::Config pref, Mode);

private:
    enum {
        STORAGE_SIZE = 8 * 1024
    };
    char    fStorage[STORAGE_SIZE];
};

SkImageDecoder* SkImageDecoder_PVJPEG_Factory(SkStream* stream)
{
    return SkNEW(SkPVJPEGImageDecoder);
}

#include "pvjpgdecoderinterface.h"
#include "pvjpgdecoder_factory.h"

class AutoPVDelete {
public:
    AutoPVDelete(PVJpgDecoderInterface* codec) : fCodec(codec) {}
    ~AutoPVDelete() {
        fCodec->Reset();
        PVJpgDecoderFactory::DeletePVJpgDecoder(fCodec);
    }
private:    
    PVJpgDecoderInterface* fCodec;
};

class MyObserver : public MPVJpegDecObserver {
public:
    MyObserver() : fCount(0) {}
    ~MyObserver() {
        if (fCount != 0) {
            SkDebugf("--- pvjpeg left %d allocations\n", fCount);
        }
    }

	virtual void allocateBuffer(uint8* &buffer, int32 buffersize) {
        ++fCount;
        // we double the allocation to work around bug when height is odd
        buffer = (uint8*)sk_malloc_throw(buffersize << 1);
        SkDebugf("---  pvjpeg alloc [%d] %d addr=%p\n", fCount, buffersize, buffer);
    }
    
	virtual void deallocateBuffer(uint8 *buffer) {
        SkDebugf("--- pvjpeg free [%d] addr=%p\n", fCount, buffer);
        --fCount;
        sk_free(buffer);
    }

private:
    int fCount;
};

static void check_status(TPvJpgDecStatus status) {
    if (TPVJPGDEC_SUCCESS != status) {
        SkDEBUGF(("--- pvjpeg status %d\n", status));
    }
}

static bool getFrame(PVJpgDecoderInterface* codec, SkBitmap* bitmap,
                     SkBitmap::Config prefConfig, SkImageDecoder::Mode mode) {
    TPvJpgDecInfo info;
    TPvJpgDecStatus status = codec->GetInfo(&info);
    if (status != TPVJPGDEC_SUCCESS)
        return false;

    int width = info.iWidth[0];
    int height = info.iHeight[0];

    bitmap->setConfig(SkBitmap::kRGB_565_Config, width, height);
    bitmap->setIsOpaque(true);

    if (SkImageDecoder::kDecodeBounds_Mode == mode) {
        return true;
    }
    
    SkASSERT(info.iNumComponent == 3);

    TPvJpgDecOutputFmt  format;
    format.iColorFormat = TPV_COLORFMT_RGB16;
    format.iCropped.topLeftX = 0;
    format.iCropped.topLeftY = 0;
    format.iCropped.bottomRightX = width - 1;
    format.iCropped.bottomRightY = height - 1;
    format.iOutputPitch = bitmap->rowBytes() >> 1;
    status = codec->SetOutput(&format);
    if (status != TPVJPGDEC_SUCCESS) {
        SkDebugf("--- PV SetOutput failed %d\n", status);
        return false;
    }

    TPvJpgDecFrame frame;
    uint8*         ptrs[3];
    int32          widths[3], heights[3];
    sk_bzero(ptrs, sizeof(ptrs));
    frame.ptr = ptrs;
    frame.iWidth = widths;
    frame.iHeight = heights;
    
    status = codec->GetFrame(&frame);
    if (status != TPVJPGDEC_SUCCESS) {
        SkDebugf("--- PV GetFrame failed %d\n", status);
        return false;
    }

    bitmap->allocPixels();
    memcpy(bitmap->getPixels(), ptrs[0], bitmap->getSize());
    return true;
}

class OsclCleanupper {
public:
    OsclCleanupper() {
        OsclBase::Init();
        OsclErrorTrap::Init();
        OsclMem::Init();
    }
    ~OsclCleanupper() {
        OsclMem::Cleanup();
        OsclErrorTrap::Cleanup();
        OsclBase::Cleanup();
    }
};

bool SkPVJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
                                    SkBitmap::Config prefConfig, Mode mode)
{
    // do I need this guy?
    OsclCleanupper oc;
    
    PVJpgDecoderInterface*  codec = PVJpgDecoderFactory::CreatePVJpgDecoder();
    TPvJpgDecStatus         status = codec->Init();
    check_status(status);

    MyObserver      observer;   // must create before autopvdelete
    AutoPVDelete    ad(codec);
    
    status = codec->SetObserver(&observer);
    check_status(status);
    
    char*   storage = fStorage;
    int32   bytesInStorage = 0;
    for (;;)
    {
        int32 bytesRead = stream->read(storage + bytesInStorage,
                                       STORAGE_SIZE - bytesInStorage);
        if (bytesRead <= 0) {
            SkDEBUGF(("SkPVJPEGImageDecoder: stream read returned %d\n", bytesRead));
            return false;
        }
        
        // update bytesInStorage to account for the read()
        bytesInStorage += bytesRead;
        SkASSERT(bytesInStorage <= STORAGE_SIZE);
        
        // now call Decode to eat some of the bytes
        int32 consumed = bytesInStorage;
        status = codec->Decode((uint8*)storage, &consumed);

        SkASSERT(bytesInStorage >= consumed);
        bytesInStorage -= consumed;
        // now bytesInStorage is the remaining unread bytes
        if (bytesInStorage > 0) { // slide the leftovers to the beginning
            SkASSERT(storage == fStorage);
            SkASSERT(consumed >= 0 && bytesInStorage >= 0);
            SkASSERT((size_t)(consumed + bytesInStorage) <= sizeof(fStorage));
            SkASSERT(sizeof(fStorage) == STORAGE_SIZE);
       //     SkDebugf("-- memmov srcOffset=%d, numBytes=%d\n", consumed, bytesInStorage);
            memmove(storage, storage + consumed, bytesInStorage);
        }
        
        switch (status) {
        case TPVJPGDEC_SUCCESS:
            SkDEBUGF(("SkPVJPEGImageDecoder::Decode returned success?\n");)
            return false;
        case TPVJPGDEC_FRAME_READY:
        case TPVJPGDEC_DONE:
            return getFrame(codec, decodedBitmap, prefConfig, mode);
        case TPVJPGDEC_FAIL:
        case TPVJPGDEC_INVALID_MEMORY:
        case TPVJPGDEC_INVALID_PARAMS:
        case TPVJPGDEC_NO_IMAGE_DATA:
            SkDEBUGF(("SkPVJPEGImageDecoder: failed to decode err=%d\n", status);)
            return false;
        case TPVJPGDEC_WAITING_FOR_INPUT:
            break;  // loop around and eat more from the stream
        }
    }
    return false;
}