forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-02-17 557c24d082b6ecb9bfe5407b77ae43fa7650a5dc
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*------------------------------------------------------------------------------
--                                                                            --
--       This software is confidential and proprietary and may be used        --
--        only as expressly authorized by a licensing agreement from          --
--                                                                            --
--                            Rockchip Products .                             --
--                                                                            --
--                   (C) COPYRIGHT 2014 ROCKCHIP PRODUCTS                     --
--                            ALL RIGHTS RESERVED                             --
--                                                                            --
--                 The entire notice above must be reproduced                 --
--                  on all copies and should not be removed.                  --
--                                                                            --
--------------------------------------------------------------------------------
--
--  Description : jpeg scan decode source
--
------------------------------------------------------------------------------
--
--
--
 
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
 
    Table of contents
 
    1. Include headers
    2. External compiler flags
    3. Module defines
    4. Local function prototypes
    5. Functions
        - JpegDecDecodeScan
        - JpegDecDecodeScanHeader
        - JpegDecDecodeBaselineScan
        - JpegDecDecodeProgressiveScan
        - JpegDecDecodeBaselineBlock
 
------------------------------------------------------------------------------*/
 
/*------------------------------------------------------------------------------
    1. Include headers
------------------------------------------------------------------------------*/
#include "jpegdecutils.h"
#include "jpegdecinternal.h"
#include "jpegdecmarkers.h"
#include "jpegdecscan.h"
#include "dwl.h"
#include "allocator_drm.h"
 
#define ALOGV printf
#define ALOGD printf
#define ALOGI printf
#define ALOGW printf
#define ALOGE printf
 
 
/*------------------------------------------------------------------------------
    2. External compiler flags
--------------------------------------------------------------------------------
 
_DTRACE:   define this flag to print trace information to stdout
 
--------------------------------------------------------------------------------
    3. Module defines
------------------------------------------------------------------------------*/
 
/*------------------------------------------------------------------------------
    4. Local function prototypes
------------------------------------------------------------------------------*/
JpegDecRet JpegDecDecodeScanHeader(JpegDecContainer *);
 
/*------------------------------------------------------------------------------
    5. Functions
------------------------------------------------------------------------------*/
 
/*------------------------------------------------------------------------------
 
        Function name: JpegDecDecodeScan
 
        Functional description:
          Handles the top level control of scan decoding
 
        Inputs:
          JpegDecContainer *pDecData      Pointer to JpegDecContainer structure
 
        Outputs:
          OK  (0)
          NOK (-1)
 
------------------------------------------------------------------------------*/
JpegDecRet JpegDecDecodeScan(JpegDecContainer * pDecData)
{
    JpegDecRet retCode; /* Returned code container */
 
    retCode = JPEGDEC_ERROR;
 
    retCode = JpegDecDecodeScanHeader(pDecData);
    if (retCode != JPEGDEC_OK) {
        return (retCode);
    }
 
    JPEGDEC_TRACE_INTERNAL(("SCAN: Allocate residual buffer\n"));
 
    /* Allocate buffers if not done already */
    if (!pDecData->info.allocated) {
        retCode = JpegDecAllocateResidual(pDecData);
        if (retCode != JPEGDEC_OK) {
            JPEGDEC_TRACE_INTERNAL(("SCAN: ALLOCATE ERROR\n"));
 
            return (retCode);
        }
        /* update */
        pDecData->info.allocated = 1;
    } else {
#if 0
        int timeout = 0;
 
        if (pDecData->asicBuff.outLumaBuffer.phy_addr)
           allocator_drm.free(pDecData->ctx, &pDecData->asicBuff.outLumaBuffer); 
            //VPUFreeLinear(&pDecData->asicBuff.outLumaBuffer);
 
        while (VPUMallocLinear(&pDecData->asicBuff.outLumaBuffer, (pDecData->image.sizeLuma + (pDecData->image.sizeChroma)))) {
            usleep(5000);
            timeout++;
            if (timeout > 0xFF) {
                ALOGE("MJPEG Malloc outbuf Failed \n");
                return (JPEGDEC_MEMFAIL);
            }
        }
#endif
        pDecData->asicBuff.outLumaBuffer = *pDecData->pictureMem;
        /* luma bus address to output */
        pDecData->info.outLuma = pDecData->asicBuff.outLumaBuffer;
 
        if (pDecData->image.sizeChroma) {
            pDecData->asicBuff.outChromaBuffer.phy_addr = pDecData->asicBuff.outLumaBuffer.phy_addr + (pDecData->image.sizeLuma << 10);
 
            if (pDecData->info.operationType != JPEGDEC_BASELINE) {
                pDecData->asicBuff.outChromaBuffer2.phy_addr = pDecData->asicBuff.outChromaBuffer.phy_addr + (pDecData->image.sizeChroma / 2);
            } else {
                pDecData->asicBuff.outChromaBuffer2.phy_addr = 0;
            }
 
            // chroma bus address to output
            pDecData->info.outChroma = pDecData->asicBuff.outChromaBuffer;
            pDecData->info.outChroma2 = pDecData->asicBuff.outChromaBuffer2;
        }
    }
 
    JPEGDEC_TRACE_INTERNAL(("SCAN: Scan rlc data\n"));
    JPEGDEC_TRACE_INTERNAL(("SCAN: MODE: %d\n", pDecData->frame.codingType));
 
    return (JPEGDEC_OK);
 
}
 
/*------------------------------------------------------------------------------
 
        Function name: JpegDecDecodeScanHeader
 
        Functional description:
          Decodes scan headers
 
        Inputs:
          JpegDecContainer *pDecData       Pointer to JpegDecContainer structure
 
        Outputs:
          OK  (0)
          NOK (-1)
 
------------------------------------------------------------------------------*/
JpegDecRet JpegDecDecodeScanHeader(JpegDecContainer * pDecData)
{
    RK_U32 i;
    RK_U32 tmp;
 
    StreamStorage *pStream = &(pDecData->stream);
 
    pDecData->scan.Ls = JpegDecGet2Bytes(pStream);
 
    /* check if there is enough data */
    if (((pStream->readBits / 8) + pDecData->scan.Ls) > pStream->streamLength)
        return (JPEGDEC_STRM_ERROR);
 
    pDecData->scan.Ns = JpegDecGetByte(pStream);
 
    pDecData->info.fillX = pDecData->info.fillY = 0;
    if (pDecData->scan.Ns == 1) {
        /* Reset to non-interleaved baseline operation type */
        if (pDecData->info.operationType == JPEGDEC_BASELINE &&
            pDecData->info.yCbCrMode != JPEGDEC_YUV400)
            pDecData->info.operationType = JPEGDEC_NONINTERLEAVED;
 
        tmp = JpegDecGetByte(&(pDecData->stream));
        pDecData->frame.cIndex = tmp - 1;
        pDecData->info.componentId = pDecData->frame.cIndex;
        pDecData->scan.Cs[pDecData->frame.cIndex] = tmp;
        tmp = JpegDecGetByte(pStream);
        pDecData->scan.Td[pDecData->frame.cIndex] = tmp >> 4;
        pDecData->scan.Ta[pDecData->frame.cIndex] = tmp & 0x0F;
 
        /* check/update component info */
        if (pDecData->frame.Nf == 3) {
            pDecData->info.fillRight = 0;
            pDecData->info.fillBottom = 0;
            pDecData->info.
            pfNeeded[pDecData->scan.Cs[pDecData->frame.cIndex] - 1] = 0;
            if (pDecData->scan.Cs[pDecData->frame.cIndex] == 2 ||
                pDecData->scan.Cs[pDecData->frame.cIndex] == 3) {
                if (pDecData->info.operationType == JPEGDEC_PROGRESSIVE ||
                    pDecData->info.operationType == JPEGDEC_NONINTERLEAVED ||
                    pDecData->info.nonInterleavedScanReady) {
                    if (pDecData->info.yCbCrModeOrig == JPEGDEC_YUV420) {
                        pDecData->info.X = pDecData->frame.hwX >> 1;
                        pDecData->info.Y = pDecData->frame.hwY >> 1;
                    } else if (pDecData->info.yCbCrModeOrig == JPEGDEC_YUV422) {
                        pDecData->info.X = pDecData->frame.hwX >> 1;
                    } else if (pDecData->info.yCbCrModeOrig == JPEGDEC_YUV440) {
                        pDecData->info.Y = pDecData->frame.hwY >> 1;
                    } else {
                        pDecData->info.yCbCrMode = 0;
                        return (JPEGDEC_UNSUPPORTED);
                    }
                }
 
                pDecData->info.yCbCrMode = 0;
            } else if (pDecData->scan.Cs[pDecData->frame.cIndex] == 1) { /* YCbCr 4:2:0 */
                pDecData->info.X = pDecData->frame.hwX;
                pDecData->info.Y = pDecData->frame.hwY;
                if (pDecData->info.yCbCrMode == JPEGDEC_YUV420) {
                    pDecData->info.yCbCrMode = 1;
                } else if (pDecData->info.yCbCrMode == JPEGDEC_YUV422) {
                    pDecData->info.yCbCrMode = 0;
                    if (pDecData->frame.cIndex == 0) {
                        JPEGDEC_TRACE_INTERNAL(("SCAN: #YUV422 FLAG\n"));
                        pDecData->info.yCbCr422 = 1;
                    }
                } else if (pDecData->info.yCbCrMode == JPEGDEC_YUV444) {
                    pDecData->info.yCbCrMode = 0;
                    return (JPEGDEC_UNSUPPORTED);
                }
            } else {
                pDecData->info.yCbCrMode = 0;
                return (JPEGDEC_UNSUPPORTED);
            }
 
            if (pDecData->info.X & 0xF) {
                pDecData->info.X += 8;
                pDecData->info.fillX = 1;
            } else if ((pDecData->scan.Cs[pDecData->frame.cIndex] == 1 ||
                        pDecData->info.yCbCrModeOrig == JPEGDEC_YUV440) &&
                       (pDecData->frame.X & 0xF) && (pDecData->frame.X & 0xF) <= 8) {
                pDecData->info.fillRight = 1;
            }
 
            if (pDecData->info.Y & 0xF) {
                pDecData->info.Y += 8;
                pDecData->info.fillY = 1;
            } else if ((pDecData->scan.Cs[pDecData->frame.cIndex] == 1 ||
                        pDecData->info.yCbCrModeOrig == JPEGDEC_YUV422) &&
                       (pDecData->frame.Y & 0xF) && (pDecData->frame.Y & 0xF) <= 8) {
                pDecData->info.fillBottom = 1;
            }
        } else if (pDecData->frame.Nf == 1) {
            JPEGDEC_TRACE_INTERNAL(("SCAN: #YUV422 FLAG\n"));
            pDecData->info.yCbCr422 = 0;
        }
 
        /* decoding info */
        if (pDecData->info.operationType == JPEGDEC_PROGRESSIVE ||
            pDecData->info.operationType == JPEGDEC_NONINTERLEAVED) {
            pDecData->info.yCbCrMode = 0;
        }
    } else {
        for (i = 0; i < pDecData->scan.Ns; i++) {
            pDecData->scan.Cs[i] = JpegDecGetByte(&(pDecData->stream));
            tmp = JpegDecGetByte(pStream);
            pDecData->scan.Td[i] = tmp >> 4;    /* which DC table */
            pDecData->scan.Ta[i] = tmp & 0x0F;  /* which AC table */
            pDecData->info.pfNeeded[i] = 1;
        }
        pDecData->info.X = pDecData->frame.hwX;
        pDecData->info.Y = pDecData->frame.hwY;
        pDecData->frame.cIndex = 0;
        pDecData->info.yCbCrMode = pDecData->info.yCbCrModeOrig;
    }
 
    pDecData->scan.Ss = JpegDecGetByte(pStream);
    pDecData->scan.Se = JpegDecGetByte(pStream);
    tmp = JpegDecGetByte(pStream);
    pDecData->scan.Ah = tmp >> 4;
    pDecData->scan.Al = tmp & 0x0F;
 
    if (pDecData->frame.codingType == SOF0) {
        /* baseline */
        if (pDecData->scan.Ss != 0)
            return (JPEGDEC_UNSUPPORTED);
        if (pDecData->scan.Se != 63)
            return (JPEGDEC_UNSUPPORTED);
        if (pDecData->scan.Ah != 0)
            return (JPEGDEC_UNSUPPORTED);
        if (pDecData->scan.Al != 0)
            return (JPEGDEC_UNSUPPORTED);
 
        /* update scan decoding parameters */
        /* interleaved/non-interleaved */
        if (pDecData->info.operationType == JPEGDEC_BASELINE)
            pDecData->info.nonInterleaved = 0;
        else
            pDecData->info.nonInterleaved = 1;
        /* decoding info */
        if ((pDecData->frame.Nf == 3 && pDecData->scan.Ns == 1) ||
            (pDecData->frame.Nf == 1 && pDecData->scan.Ns == 1))
            pDecData->info.amountOfQTables = 1;
        else
            pDecData->info.amountOfQTables = 3;
    }
 
    if (pDecData->frame.codingType == SOF2) {
        /* progressive */
        if (pDecData->scan.Ss == 0 && pDecData->scan.Se != 0)
            return (JPEGDEC_UNSUPPORTED);
        if (pDecData->scan.Ah < 0 || pDecData->scan.Ah > 13)
            return (JPEGDEC_UNSUPPORTED);
        if (pDecData->scan.Al < 0 || pDecData->scan.Al > 13)
            return (JPEGDEC_UNSUPPORTED);
 
        /* update scan decoding parameters */
        /* TODO! What if 2 components, possible??? */
        /* interleaved/non-interleaved */
        if (pDecData->scan.Ns == 1) {
            pDecData->info.nonInterleaved = 1;
            /* component ID */
            pDecData->info.componentId = pDecData->frame.cIndex;
            pDecData->info.amountOfQTables = 1;
        } else {
            pDecData->info.nonInterleaved = 0;
            /* component ID ==> set to luma ==> interleaved */
            pDecData->info.componentId = 0;
            pDecData->info.amountOfQTables = 3;
        }
 
    }
 
    return (JPEGDEC_OK);
}