xref: /rockchip-linux_mpp/mpp/codec/dec/jpeg/jpegd_parser.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2  *
3  * Copyright 2015 Rockchip Electronics Co. LTD
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #define MODULE_TAG "jpegd_parser"
19 
20 #include <string.h>
21 
22 #include "mpp_env.h"
23 #include "mpp_mem.h"
24 #include "mpp_soc.h"
25 #include "mpp_debug.h"
26 #include "mpp_bitread.h"
27 #include "mpp_packet_impl.h"
28 
29 #include "jpegd_api.h"
30 #include "jpegd_parser.h"
31 #include "mpp_dec_cb_param.h"
32 
33 RK_U32 jpegd_debug = 0x0;
34 
35 /* return the 8 bit start code value and update the search
36    state. Return 0 if no start code found */
jpegd_find_marker(const RK_U8 ** pbuf_ptr,const RK_U8 * buf_end)37 static RK_U8 jpegd_find_marker(const RK_U8 **pbuf_ptr, const RK_U8 *buf_end)
38 {
39     const RK_U8 *buf_ptr = NULL;
40     RK_U8 val = 0;
41     RK_U8 start_code = 0xff;
42     RK_U32 strm_len = buf_end - *pbuf_ptr + 1;
43 
44     while (*pbuf_ptr < buf_end) {
45         buf_ptr = memchr(*pbuf_ptr, start_code, strm_len);
46 
47         if (!buf_ptr) {
48             mpp_err("Start codec not found!\n");
49             return 0;
50         }
51 
52         RK_U8 marker = *(buf_ptr + 1);
53         if (marker >= 0xc0 && marker <= 0xfe) {
54             val = *(buf_ptr + 1);
55             jpegd_dbg_marker("find_marker skipped %d bytes\n", buf_ptr - *pbuf_ptr);
56             *pbuf_ptr = buf_ptr;
57             return val;
58         } else {
59             jpegd_dbg_marker("0x%x is not a marker\n", marker);
60             (*pbuf_ptr)++;
61         }
62     }
63     return 0;
64 }
65 
jpegd_find_eoi(const RK_U8 ** pbuf_ptr,const RK_U8 * buf_end)66 static MPP_RET jpegd_find_eoi(const RK_U8 **pbuf_ptr, const RK_U8 *buf_end)
67 {
68     const RK_U8 *buf_ptr = NULL;
69     RK_S32 eoi = 0xffd9;
70     RK_U32 strm_len = buf_end - *pbuf_ptr + 1;
71 
72     if (*pbuf_ptr >= buf_end) {
73         mpp_err("buf ptr %p is overflow the buf end %p.", *pbuf_ptr, buf_end);
74         return MPP_NOK;
75     }
76 
77     buf_ptr = memchr(*pbuf_ptr, eoi, strm_len);
78 
79     if (buf_ptr && (buf_end > buf_ptr)) {
80         return MPP_OK;
81     }
82 
83     return MPP_NOK;
84 }
85 
jpeg_judge_yuv_mode(JpegdCtx * ctx)86 static MPP_RET jpeg_judge_yuv_mode(JpegdCtx *ctx)
87 {
88     MPP_RET ret = MPP_OK;
89     JpegdSyntax *s = ctx->syntax;
90 
91     /*  check input format */
92     if (s->nb_components == 3) {
93         if (s->h_count[0] == 2 && s->v_count[0] == 2 &&
94             s->h_count[1] == 1 && s->v_count[1] == 1 &&
95             s->h_count[2] == 1 && s->v_count[2] == 1) {
96             jpegd_dbg_marker("YCbCr Format: YUV420(2*2:1*1:1*1)\n");
97             s->yuv_mode = JPEGDEC_YUV420;
98             s->output_fmt = MPP_FMT_YUV420SP;
99         } else if (s->h_count[0] == 2 && s->v_count[0] == 1 &&
100                    s->h_count[1] == 1 && s->v_count[1] == 1 &&
101                    s->h_count[2] == 1 && s->v_count[2] == 1) {
102             jpegd_dbg_marker("YCbCr Format: YUV422(2*1:1*1:1*1)\n");
103             s->yuv_mode = JPEGDEC_YUV422;
104             s->output_fmt = MPP_FMT_YUV422SP;
105 
106             /* check if fill needed */
107             if ((s->height & 0xf) && ((s->height & 0xf) <= 8)) {
108                 s->fill_bottom = 1;
109             }
110         } else if (s->h_count[0] == 1 && s->v_count[0] == 2 &&
111                    s->h_count[1] == 1 && s->v_count[1] == 1 &&
112                    s->h_count[2] == 1 && s->v_count[2] == 1) {
113             jpegd_dbg_marker("YCbCr Format: YUV440(1*2:1*1:1*1)\n");
114             s->yuv_mode = JPEGDEC_YUV440;
115             s->output_fmt = MPP_FMT_YUV440SP;
116 
117             /* check if fill needed */
118             if ((s->width & 0xf) && ((s->width & 0xf) <= 8)) {
119                 s->fill_right = 1;
120             }
121         } else if (s->h_count[0] == 1 && s->v_count[0] == 1 &&
122                    s->h_count[1] == 1 && s->v_count[1] == 1 &&
123                    s->h_count[2] == 1 && s->v_count[2] == 1) {
124             jpegd_dbg_marker("YCbCr Format: YUV444(1*1:1*1:1*1)\n");
125             s->yuv_mode = JPEGDEC_YUV444;
126             s->output_fmt = MPP_FMT_YUV444SP;
127 
128             /* check if fill needed */
129             if ((s->width & 0xf) && ((s->width & 0xf) <= 8)) {
130                 s->fill_right = 1;
131             }
132 
133             if ((s->height & 0xf) && ((s->height & 0xf) <= 8)) {
134                 s->fill_bottom = 1;
135             }
136         } else if (s->h_count[0] == 4 && s->v_count[0] == 1 &&
137                    s->h_count[1] == 1 && s->v_count[1] == 1 &&
138                    s->h_count[2] == 1 && s->v_count[2] == 1) {
139             jpegd_dbg_marker("YCbCr Format: YUV411(4*1:1*1:1*1)\n");
140             s->yuv_mode = JPEGDEC_YUV411;
141             s->output_fmt = MPP_FMT_YUV411SP;
142 
143             /* check if fill needed */
144             if ((s->height & 0xf) && ((s->height & 0xf) <= 8)) {
145                 s->fill_bottom = 1;
146             }
147         } else {
148             mpp_err_f("Unsupported YCbCr Format: (%d*%d:%d*%d:%d*%d)\n",
149                       s->h_count[0], s->v_count[0],
150                       s->h_count[1], s->v_count[1],
151                       s->h_count[2], s->v_count[2]);
152             ret = MPP_ERR_STREAM;
153         }
154     } else if (s->nb_components == 1) {
155         if (s->h_count[0] == s->v_count[0] && s->h_count[0] != 0) {
156             s->yuv_mode = JPEGDEC_YUV400;
157             s->output_fmt = MPP_FMT_YUV400;
158             /* check if fill needed */
159             if ((s->width & 0xf) && ((s->width & 0xf) <= 8)) {
160                 s->fill_right = 1;
161             }
162 
163             if ((s->height & 0xf) && ((s->height & 0xf) <= 8)) {
164                 s->fill_bottom = 1;
165             }
166         } else {
167             mpp_err_f("unsupported format(%d*%d)\n", s->h_count[0],
168                       s->v_count[0]);
169             ret = MPP_ERR_STREAM;
170         }
171     } else {
172         mpp_err_f("unsupported format, nb_components=%d\n", s->nb_components);
173         ret = MPP_ERR_STREAM;
174     }
175 
176     return ret;
177 }
178 
jpegd_read_len(BitReadCtx_t * gb)179 static inline RK_U16 jpegd_read_len(BitReadCtx_t *gb)
180 {
181     RK_U8 lh, ll;
182     READ_BITS(gb, 8, &lh);
183     READ_BITS(gb, 8, &ll);
184     return (((RK_U16)lh) << 8) | ((RK_U16)ll);
185 
186 __BITREAD_ERR:
187     return 0;
188 }
189 
jpegd_skip_section(JpegdCtx * ctx)190 static MPP_RET jpegd_skip_section(JpegdCtx *ctx)
191 {
192     BitReadCtx_t *gb = ctx->bit_ctx;
193     RK_U16 len = 0;
194 
195     if (gb->bytes_left_ < 2)
196         return MPP_ERR_READ_BIT;
197     len = jpegd_read_len(gb);
198     if (len < 2 /* invalid marker */ || (RK_U32)len - 2 > gb->bytes_left_) {
199         /* too short length or bytes is not enough */
200         return MPP_ERR_READ_BIT;
201     }
202     if (len > 2)
203         SKIP_BITS(gb, (len - 2) * 8);
204 
205     return MPP_OK;
206 
207 __BITREAD_ERR:
208     return MPP_NOK;
209 }
210 
jpegd_decode_dht(JpegdCtx * ctx)211 static MPP_RET jpegd_decode_dht(JpegdCtx *ctx)
212 {
213     MPP_RET ret = MPP_NOK;
214     BitReadCtx_t *gb = ctx->bit_ctx;
215     JpegdSyntax *syntax = ctx->syntax;
216     RK_U32 len, num, value;
217     RK_U32 table_type, table_id;
218     RK_U32  i, code_max;
219 
220     len = jpegd_read_len(gb);
221     len -= 2; /* Huffman Table Length */
222 
223     if (len > gb->bytes_left_) {
224         mpp_err_f("dht: len %d is too large\n", len);
225         return MPP_ERR_STREAM;
226     }
227     jpegd_dbg_marker("dht: huffman tables length=%d\n", len);
228 
229     while (len > 0) {
230         if (len < MAX_HUFFMAN_CODE_BIT_LENGTH + 1) {
231             mpp_err_f("dht: len %d is too small\n", len);
232             return MPP_ERR_STREAM;
233         }
234 
235         READ_BITS(gb, 4, &table_type); /* 0 - DC; 1 - AC */
236         if (table_type >= HUFFMAN_TABLE_TYPE_BUTT) {
237             mpp_err_f("table type %d error\n", table_type);
238             return MPP_ERR_STREAM;
239         }
240 
241         READ_BITS(gb, 4, &table_id);
242         if (table_id >= HUFFMAN_TABLE_ID_TWO) {
243             mpp_err_f("table id %d is unsupported for baseline\n", table_id);
244             return MPP_ERR_STREAM;
245         }
246 
247         num = 0;
248         if (table_type == HUFFMAN_TABLE_TYPE_DC) {
249             DcTable *ptr = &(syntax->dc_table[table_id]);
250             for (i = 0; i < MAX_HUFFMAN_CODE_BIT_LENGTH; i++) {
251                 READ_BITS(gb, 8, &value);
252                 ptr->bits[i] = value;
253                 num += value;
254             }
255 
256             ptr->actual_length = num;
257         } else {
258             AcTable *ptr = &(syntax->ac_table[table_id]);
259             for (i = 0; i < MAX_HUFFMAN_CODE_BIT_LENGTH; i++) {
260                 READ_BITS(gb, 8, &value);
261                 ptr->bits[i] = value;
262                 num += value;
263             }
264 
265             ptr->actual_length = num;
266         }
267 
268         len -= 17;
269         if (len < num ||
270             (num > MAX_DC_HUFFMAN_TABLE_LENGTH && table_type == HUFFMAN_TABLE_TYPE_DC) ||
271             (num > MAX_AC_HUFFMAN_TABLE_LENGTH && table_type == HUFFMAN_TABLE_TYPE_AC)) {
272             mpp_err_f("table type %d, code word number %d error\n", table_type, num);
273             return MPP_ERR_STREAM;
274         }
275 
276         code_max = 0;
277         if (table_type == HUFFMAN_TABLE_TYPE_DC) {
278             DcTable *ptr = &(syntax->dc_table[table_id]);
279 
280             syntax->htbl_entry |= 1 << (table_id * 2);
281 
282             for (i = 0; i < num; i++) {
283                 READ_BITS(gb, 8, &value);
284                 ptr->vals[i] = value;
285                 if (ptr->vals[i] > code_max)
286                     code_max = ptr->vals[i];
287             }
288         } else {
289             AcTable *ptr = &(syntax->ac_table[table_id]);
290 
291             syntax->htbl_entry |= 1 << ((table_id * 2) + 1);
292 
293             for (i = 0; i < num; i++) {
294                 READ_BITS(gb, 8, &value);
295                 ptr->vals[i] = value;
296                 if (ptr->vals[i] > code_max)
297                     code_max = ptr->vals[i];
298             }
299         }
300         len -= num;
301 
302         jpegd_dbg_marker("dht: type=%d id=%d code_word_num=%d, code_max=%d, len=%d\n",
303                          table_type, table_id, num, code_max, len);
304     }
305     ret = MPP_OK;
306 
307 __BITREAD_ERR:
308     if (ret != MPP_OK)
309         jpegd_dbg_syntax("bit read error!\n");
310 
311     return ret;
312 }
313 
314 /* quantize tables */
jpegd_decode_dqt(JpegdCtx * ctx)315 static MPP_RET jpegd_decode_dqt(JpegdCtx *ctx)
316 {
317     MPP_RET ret = MPP_NOK;
318     BitReadCtx_t *gb = ctx->bit_ctx;
319     JpegdSyntax *syntax = ctx->syntax;
320     RK_U32 len;
321     int index, i;
322     RK_U16 value;
323 
324     len = jpegd_read_len(gb);
325     len -= 2; /* quantize tables length */
326 
327     if (len > gb->bytes_left_) {
328         mpp_err_f("dqt: len %d is too large\n", len);
329         return MPP_ERR_STREAM;
330     }
331 
332     while (len >= 65) {
333         RK_U16 pr;
334         READ_BITS(gb, 4, &pr);
335         if (pr > 1) {
336             mpp_err_f("dqt: invalid precision\n");
337             return MPP_ERR_STREAM;
338         }
339 
340         READ_BITS(gb, 4, &index);
341         if (index >= QUANTIZE_TABLE_ID_BUTT) {
342             mpp_err_f("dqt: invalid quantize tables ID\n");
343             return MPP_ERR_STREAM;
344         }
345         jpegd_dbg_marker("quantize tables ID=%d\n", index);
346 
347         /* read quant table */
348         for (i = 0; i < QUANTIZE_TABLE_LENGTH; i++) {
349             READ_BITS(gb, pr ? 16 : 8, &value);
350             syntax->quant_matrixes[index][i] = value;
351         }
352         syntax->qtbl_entry++;
353         if (syntax->qtbl_entry > MAX_COMPONENTS)
354             mpp_err_f("%d entries qtbl is not supported\n", syntax->qtbl_entry);
355 
356         if (jpegd_debug & JPEGD_DBG_TABLE) {
357             /* debug code */
358             mpp_log("******Start to print quantize table %d******\n", index);
359 
360             for (i = 0; i < QUANTIZE_TABLE_LENGTH; i += 8) {
361                 mpp_log("%2d~%2d 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
362                         i, i + 7,
363                         syntax->quant_matrixes[index][i + 0],
364                         syntax->quant_matrixes[index][i + 1],
365                         syntax->quant_matrixes[index][i + 2],
366                         syntax->quant_matrixes[index][i + 3],
367                         syntax->quant_matrixes[index][i + 4],
368                         syntax->quant_matrixes[index][i + 5],
369                         syntax->quant_matrixes[index][i + 7],
370                         syntax->quant_matrixes[index][i + 7]);
371             }
372             mpp_log("******Quantize table %d End******\n", index);
373         }
374 
375         // XXX FIXME fine-tune, and perhaps add dc too
376         syntax->qscale[index] = MPP_MAX(syntax->quant_matrixes[index][1],
377                                         syntax->quant_matrixes[index][8]) >> 1;
378 
379         jpegd_dbg_marker("qscale[%d]: %d\n", index, syntax->qscale[index]);
380         len -= 1 + 64 * (1 + pr);
381     }
382     ret = MPP_OK;
383 
384 __BITREAD_ERR:
385     if (ret != MPP_OK)
386         jpegd_dbg_syntax("bit read error!\n");
387 
388     return ret;
389 }
390 
jpegd_decode_sof(JpegdCtx * ctx)391 static MPP_RET jpegd_decode_sof(JpegdCtx *ctx)
392 {
393     MPP_RET ret = MPP_NOK;
394     BitReadCtx_t *gb = ctx->bit_ctx;
395     JpegdSyntax *syntax = ctx->syntax;
396     RK_U32 len, bits, i;
397     RK_U32 width, height;
398     RK_U32 nb_components, value;
399 
400     len = jpegd_read_len(gb);
401     if (len > gb->bytes_left_) {
402         mpp_err_f("len %d is too large\n", len);
403         return MPP_ERR_STREAM;
404     }
405 
406     READ_BITS(gb, 8, &bits);
407     if (bits > 16 || bits < 1) {
408         /* usually bits is 8 */
409         mpp_err_f("bits %d is invalid\n", bits);
410         return MPP_ERR_STREAM;
411     }
412     syntax->sample_precision = bits;
413 
414     READ_BITS(gb, 16, &height);
415     READ_BITS(gb, 16, &width);
416     syntax->height = height;
417     syntax->width = width;
418     syntax->hor_stride = MPP_ALIGN(width, 16);
419     syntax->ver_stride = MPP_ALIGN(height, 16);
420 
421     jpegd_dbg_marker("sof0: picture: %dx%d, stride: %dx%d\n", width, height,
422                      syntax->hor_stride, syntax->ver_stride);
423 
424     READ_BITS(gb, 8 , &nb_components);
425     if ((nb_components != 1) && (nb_components != MAX_COMPONENTS)) {
426         mpp_err_f("components number %d error\n", nb_components);
427         return MPP_ERR_STREAM;
428     }
429 
430     if (len != (8 + (3 * nb_components)))
431         mpp_err_f("decode_sof0: error, len(%d) mismatch nb_components(%d)\n",
432                   len, nb_components);
433 
434     syntax->nb_components = nb_components;
435     syntax->h_max = 1;
436     syntax->v_max = 1;
437     for (i = 0; i < nb_components; i++) {
438         /* component id */
439         READ_BITS(gb, 8 , &value);
440         syntax->component_id[i] = value - 1; /* start from zero */
441 
442         READ_BITS(gb, 4, &value);
443         syntax->h_count[i] = value;  /* Horizontal sampling factor */
444 
445         READ_BITS(gb, 4, &value);
446         syntax->v_count[i] = value; /* Vertical sampling factor */
447 
448         if (!syntax->h_count[i] || !syntax->v_count[i]) {
449             mpp_err_f("Invalid sampling factor in component %d %d:%d\n",
450                       i, syntax->h_count[i], syntax->v_count[i]);
451             return MPP_ERR_STREAM;
452         }
453 
454         /* compute hmax and vmax (only used in interleaved case) */
455         if (syntax->h_count[i] > syntax->h_max)
456             syntax->h_max = syntax->h_count[i];
457         if (syntax->v_count[i] > syntax->v_max)
458             syntax->v_max = syntax->v_count[i];
459 
460         /* Quantization table destination selector */
461         READ_BITS(gb, 8, &value);
462         syntax->quant_index[i] = value;
463 
464         if (syntax->quant_index[i] >= QUANTIZE_TABLE_ID_BUTT) {
465             mpp_err_f("quant_index %d is invalid\n", syntax->quant_index[i]);
466             return MPP_ERR_STREAM;
467         }
468 
469         jpegd_dbg_marker("component %d %d:%d id: %d quant_id:%d\n",
470                          i, syntax->h_count[i], syntax->v_count[i],
471                          syntax->component_id[i], syntax->quant_index[i]);
472     }
473 
474     /* judge yuv mode from sampling factor */
475     ret = jpeg_judge_yuv_mode(ctx);
476 
477 __BITREAD_ERR:
478     if (ret != MPP_OK)
479         jpegd_dbg_syntax("bit read error!\n");
480 
481     return ret;
482 }
483 
jpegd_decode_sos(JpegdCtx * ctx)484 static MPP_RET jpegd_decode_sos(JpegdCtx *ctx)
485 {
486     MPP_RET ret = MPP_NOK;
487     BitReadCtx_t *gb = ctx->bit_ctx;
488     JpegdSyntax *syntax = ctx->syntax;
489     RK_U32 len, nb_components, value;
490     RK_U32 id, i, index;
491 
492     len = jpegd_read_len(gb);
493     if (len > gb->bytes_left_) {
494         mpp_err_f("len %d is too large\n", len);
495         return MPP_ERR_STREAM;
496     }
497     syntax->sos_len = len; /* used for calculating stream offset */
498 
499     READ_BITS(gb, 8, &nb_components);
500     if ((nb_components != 1) && (nb_components != MAX_COMPONENTS)) {
501         mpp_err_f("decode_sos: nb_components %d unsupported\n", nb_components);
502         return MPP_ERR_STREAM;
503     }
504 
505     if (len != 6 + 2 * nb_components) {
506         mpp_err_f("decode_sos: invalid len (%d), nb_components:%d\n",
507                   len, nb_components);
508         return MPP_ERR_STREAM;
509     }
510     syntax->qtable_cnt = nb_components;
511 
512     for (i = 0; i < nb_components; i++) {
513         READ_BITS(gb, 8, &value);
514         id = value - 1;
515         jpegd_dbg_marker("sos component: %d\n", id);
516 
517         /* find component index */
518         for (index = 0; index < syntax->nb_components; index++)
519             if (id == syntax->component_id[index])
520                 break;
521 
522         if (index == syntax->nb_components) {
523             mpp_err_f("decode_sos: index(%d) out of components\n", index);
524             return MPP_ERR_STREAM;
525         }
526 
527         READ_BITS(gb, 4, &value);
528         syntax->dc_index[i] = value;
529 
530         READ_BITS(gb, 4, &value);
531         syntax->ac_index[i] = value;
532 
533         jpegd_dbg_marker("component:%d, dc_index:%d, ac_index:%d\n",
534                          id, syntax->dc_index[i], syntax->ac_index[i]);
535 
536         if (syntax->dc_index[i] > HUFFMAN_TABLE_ID_ONE ||
537             syntax->ac_index[i] > HUFFMAN_TABLE_ID_ONE) {
538             /* for baseline */
539             mpp_err_f("Huffman table id error\n");
540             return MPP_ERR_STREAM;
541         }
542     }
543 
544     READ_BITS(gb, 8, &value);
545     syntax->scan_start = value;
546 
547     READ_BITS(gb, 8, &value);
548     syntax->scan_end = value;
549 
550     READ_BITS(gb, 4, &value);
551     syntax->prev_shift = value;
552 
553     READ_BITS(gb, 4, &value);
554     syntax->point_transform = value;
555 
556     if (syntax->scan_start != 0 || syntax->scan_end != 0x3F ||
557         syntax->prev_shift != 0 || syntax->point_transform != 0) {
558         /* for baseline */
559         mpp_err_f("unsupported sos parameter: scan_start:%d,\n"
560                   "\t\tscan_end:%d, prev_shift:%d, point_transform:%d\n",
561                   syntax->scan_start, syntax->scan_end,
562                   syntax->prev_shift, syntax->point_transform);
563         return MPP_ERR_STREAM;
564     }
565     ret = MPP_OK;
566 
567 __BITREAD_ERR:
568     if (ret != MPP_OK)
569         jpegd_dbg_syntax("bit read error!\n");
570 
571     return ret;
572 }
573 
jpegd_decode_dri(JpegdCtx * ctx)574 static MPP_RET jpegd_decode_dri(JpegdCtx *ctx)
575 {
576     MPP_RET ret = MPP_NOK;
577     BitReadCtx_t *gb = ctx->bit_ctx;
578     JpegdSyntax *s = ctx->syntax;
579     RK_U32 len;
580 
581     len = jpegd_read_len(gb);
582     if (len != DRI_MARKER_LENGTH) {
583         mpp_err_f("DRI length %d error\n", len);
584         return MPP_ERR_STREAM;
585     }
586 
587     READ_BITS(gb, 16, &s->restart_interval);
588     jpegd_dbg_marker("restart interval: %d\n", s->restart_interval);
589 
590     ret = MPP_OK;
591 
592 __BITREAD_ERR:
593     if (ret != MPP_OK)
594         jpegd_dbg_syntax("bit read error!\n");
595 
596     return ret;
597 }
598 
jpegd_setup_default_dht(JpegdCtx * ctx)599 static MPP_RET jpegd_setup_default_dht(JpegdCtx *ctx)
600 {
601     jpegd_dbg_func("enter\n");
602     JpegdSyntax *s = ctx->syntax;
603     AcTable *ac_ptr = NULL;
604     DcTable *dc_ptr = NULL;
605     const RK_U8 *bits_tmp = NULL;
606     const RK_U8 *val_tmp = NULL;
607     RK_U32 tmp_len = 0;
608     RK_U32 i,  k;
609 
610     /* Set up the standard Huffman tables (cf. JPEG standard section K.3)
611      * IMPORTANT: these are only valid for 8-bit data precision!
612      */
613     static const RK_U8 bits_dc_luminance[MAX_HUFFMAN_CODE_BIT_LENGTH] =
614     {  0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
615 
616     /* luminance and chrominance all use it */
617     static const RK_U8 val_dc[MAX_DC_HUFFMAN_TABLE_LENGTH] =
618     { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
619 
620     static const RK_U8 bits_dc_chrominance[MAX_HUFFMAN_CODE_BIT_LENGTH] =
621     { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
622 
623     static const RK_U8 bits_ac_luminance[MAX_HUFFMAN_CODE_BIT_LENGTH] =
624     { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
625 
626     /* 162 Bytes */
627     static const RK_U8 val_ac_luminance[MAX_AC_HUFFMAN_TABLE_LENGTH] = {
628         0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
629         0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
630         0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
631         0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
632         0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
633         0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
634         0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
635         0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
636         0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
637         0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
638         0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
639         0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
640         0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
641         0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
642         0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
643         0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
644         0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
645         0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
646         0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
647         0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
648         0xf9, 0xfa
649     };
650 
651     static const RK_U8 bits_ac_chrominance[MAX_HUFFMAN_CODE_BIT_LENGTH] =
652     { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
653 
654     static const RK_U8 val_ac_chrominance[MAX_AC_HUFFMAN_TABLE_LENGTH] = {
655         0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
656         0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
657         0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
658         0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
659         0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
660         0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
661         0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
662         0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
663         0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
664         0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
665         0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
666         0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
667         0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
668         0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
669         0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
670         0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
671         0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
672         0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
673         0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
674         0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
675         0xf9, 0xfa
676     };
677 
678     const RK_U8 *bits_table[4] = {
679         bits_ac_luminance,
680         bits_ac_chrominance,
681         bits_dc_luminance,
682         bits_dc_chrominance
683     };
684 
685     const RK_U8 *val_table[4] = {
686         val_ac_luminance,
687         val_ac_chrominance,
688         val_dc,
689         val_dc
690     };
691 
692     /* AC Table */
693     for (k = 0; k < 2; k++) {
694         ac_ptr = &(s->ac_table[k]);
695         bits_tmp = bits_table[k];
696         val_tmp = val_table[k];
697 
698         tmp_len = 0;
699         for (i = 0; i < MAX_HUFFMAN_CODE_BIT_LENGTH; i++) {
700             /* read in the values of list BITS */
701             tmp_len += ac_ptr->bits[i] = bits_tmp[i];
702         }
703 
704         ac_ptr->actual_length = tmp_len;  /* set the table length */
705         for (i = 0; i < tmp_len; i++) {
706             /* read in the HUFFVALs */
707             ac_ptr->vals[i] = val_tmp[i];
708         }
709     }
710 
711     /* DC Table */
712     for (k = 0; k < 2; k++) {
713         dc_ptr = &(s->dc_table[k]);
714         bits_tmp = bits_table[k + 2];
715         val_tmp = val_table[k + 2];
716 
717         tmp_len = 0;
718         for (i = 0; i < MAX_HUFFMAN_CODE_BIT_LENGTH; i++) {
719             /* read in the values of list BITS */
720             tmp_len += dc_ptr->bits[i] = bits_tmp[i];
721         }
722 
723         dc_ptr->actual_length = tmp_len;  /* set the table length */
724         for (i = 0; i < tmp_len; i++) {
725             /* read in the HUFFVALs */
726             dc_ptr->vals[i] = val_tmp[i];
727         }
728     }
729 
730     jpegd_dbg_func("exit\n");
731     return MPP_OK;
732 }
733 
jpegd_decode_frame(JpegdCtx * ctx)734 static MPP_RET jpegd_decode_frame(JpegdCtx *ctx)
735 {
736     jpegd_dbg_func("enter\n");
737     MPP_RET ret = MPP_OK;
738     const RK_U8 *const buf = ctx->buffer;
739     RK_U32 strm_len = ctx->streamLength;
740     BitReadCtx_t *gb = ctx->bit_ctx;
741     JpegdSyntax *syntax = ctx->syntax;
742     RK_S32 start_code = 0xffd8;
743 
744     const RK_U8 *buf_ptr = buf;
745     const RK_U8 *const buf_end = buf + strm_len;
746 
747     syntax->htbl_entry = 0;
748     syntax->qtbl_entry = 0;
749 
750     if (strm_len < 8 || !memchr(buf_ptr, start_code, 8)) {
751         // not jpeg
752         ret = MPP_ERR_STREAM;
753         goto fail;
754     }
755 
756     while (buf_ptr < buf_end) {
757         /* find start marker */
758         start_code = jpegd_find_marker(&buf_ptr, buf_end);
759         if (start_code <= 0) {
760             jpegd_dbg_marker("start code not found\n");
761             ret = MPP_ERR_STREAM;
762             break;
763         } else {
764             buf_ptr += 2;
765         }
766 
767         jpegd_dbg_marker("marker = 0x%x, avail_size_in_buf = %d\n",
768                          start_code, buf_end - buf_ptr);
769         ctx->start_code = start_code;
770 
771         /* setup bit read context */
772         mpp_set_bitread_ctx(gb, (RK_U8 *)buf_ptr, buf_end - buf_ptr);
773 
774         /* process markers */
775         if (start_code >= RST0 && start_code <= RST7) {
776             /* nothing to do with RSTn */
777             jpegd_dbg_syntax("restart marker: %d\n", start_code & 0x0f);
778         }
779 
780         if (start_code > SOF0 && start_code <= SOF15 && start_code != DHT) {
781             mpp_err_f("Only baseline DCT is supported, unsupported entropy encoding 0x%x", start_code);
782             ret = MPP_ERR_STREAM;
783             goto fail;
784         }
785 
786         switch (start_code) {
787         case SOI:
788             /* nothing to do on SOI */
789             syntax->dht_found = 0;
790             syntax->eoi_found = 0;
791             syntax->sof0_found = 0;
792             syntax->qtable_cnt = 0;
793             syntax->qtbl_entry = 0;
794             syntax->htbl_entry = 0;
795             break;
796         case DHT:
797             if ((ret = jpegd_decode_dht(ctx)) != MPP_OK) {
798                 mpp_err_f("huffman table decode error\n");
799                 goto fail;
800             }
801             syntax->dht_found = 1;
802             break;
803         case DQT:
804             if ((ret = jpegd_decode_dqt(ctx)) != MPP_OK) {
805                 mpp_err_f("quantize tables decode error\n");
806                 goto fail;
807             }
808             break;
809         case SOF0:
810             if ((ret = jpegd_decode_sof(ctx)) != MPP_OK) {
811                 mpp_err_f("sof0 decode error\n");
812                 goto fail;
813             }
814             if (ctx->syntax->sample_precision != 8) {
815                 mpp_err_f("Illegal sample precision %d.\n\
816                     For baseline, it should be 8\n", ctx->syntax->sample_precision);
817                 goto fail;
818             }
819 
820             syntax->sof0_found = 1;
821             break;
822         case EOI:
823             syntax->eoi_found = 1;
824             jpegd_dbg_marker("still exists %d bytes behind EOI marker\n",
825                              buf_end - buf_ptr);
826             goto done;
827             break;
828         case SOS:
829             if (!syntax->sof0_found) {
830                 mpp_err_f("Warning: only support baseline type\n");
831                 goto fail;
832             }
833 
834             if ((ret = jpegd_decode_sos(ctx)) != MPP_OK) {
835                 mpp_err_f("sos decode error\n");
836                 goto fail;
837             }
838 
839             /* stream behind SOS is decoded by hardware */
840             syntax->strm_offset = buf_ptr - buf + syntax->sos_len;
841             syntax->cur_pos = (RK_U8 *)buf + syntax->strm_offset;
842             syntax->pkt_len = ctx->streamLength;
843             jpegd_dbg_marker("This packet owns %d bytes with length %zu\n"
844                              "\t\thas been decoded %d bytes by software\n"
845                              "\t\tbuf_ptr:%p, buf:%p, sos_len:%d\n"
846                              "\t\thardware start address:%p",
847                              mpp_packet_get_size(ctx->input_packet),
848                              syntax->pkt_len,
849                              syntax->strm_offset, buf_ptr, buf,
850                              syntax->sos_len, syntax->cur_pos);
851 
852             if (syntax->strm_offset >= ctx->streamLength) {
853                 mpp_err_f("stream offset %d is larger than buffer size %d\n",
854                           syntax->strm_offset, ctx->streamLength);
855                 ret = MPP_ERR_UNKNOW;
856                 goto fail;
857             }
858 
859             if ((syntax->strm_offset + 2) < ctx->streamLength &&
860                 buf_ptr[syntax->sos_len] == 0xff && buf_ptr[syntax->sos_len + 1] == 0xd8) {
861                 jpegd_dbg_marker("Encontered SOI again, parse again!\n");
862                 break;
863             }
864             if (!ctx->scan_all_marker) {
865                 jpegd_dbg_marker("just scan parts of markers!\n");
866                 goto done;
867             }
868 
869             break;
870         case DRI:
871             if ((ret = jpegd_decode_dri(ctx)) != MPP_OK) {
872                 mpp_err_f("dri decode error\n");
873                 goto fail;
874             }
875             break;
876         default:
877             jpegd_dbg_marker("unhandled coding type(0x%x) in switch..case..\n",
878                              start_code);
879             if ((ret = jpegd_skip_section(ctx)) != MPP_OK) {
880                 jpegd_dbg_marker("Fail to skip section 0xFF%02x!\n",
881                                  start_code);
882                 goto fail;
883             }
884             break;
885         }
886 
887         buf_ptr = ctx->bit_ctx->data_;
888     }
889 
890 done:
891     if (!syntax->dht_found) {
892         jpegd_dbg_marker("sorry, DHT is not found!\n");
893         jpegd_setup_default_dht(ctx);
894         syntax->htbl_entry = 0x0f;
895     }
896     if (!syntax->sof0_found) {
897         mpp_err_f("sof marker not found!\n");
898         ret = MPP_ERR_STREAM;
899     }
900     if (!syntax->eoi_found) {
901         if (MPP_OK != jpegd_find_eoi(&buf_ptr, buf_end)) {
902             mpp_err_f("EOI marker not found!\n");
903             ret = MPP_ERR_STREAM;
904         }
905     }
906     jpegd_dbg_func("exit\n");
907     return ret;
908 
909 fail:
910     ret = MPP_ERR_STREAM;
911     jpegd_dbg_func("exit\n");
912     return ret;
913 }
914 
915 static MPP_RET
jpegd_split_frame(RK_U8 * src,RK_U32 src_size,RK_U8 * dst,RK_U32 dst_size,RK_U32 * copy_length)916 jpegd_split_frame(RK_U8 *src, RK_U32 src_size,
917                   RK_U8 *dst, RK_U32 dst_size, RK_U32 *copy_length)
918 {
919     jpegd_dbg_func("enter\n");
920     MPP_RET ret = MPP_OK;
921     if (NULL == src || NULL == dst || src_size <= 0) {
922         mpp_err_f("NULL pointer or wrong src_size(%d)", src_size);
923         return MPP_ERR_NULL_PTR;
924     }
925     RK_U8 *tmp;
926     RK_U32 str_size = (src_size + 255) & (~255);
927 
928     if (src[6] == 0x41 && src[7] == 0x56 && src[8] == 0x49 && src[9] == 0x31) {
929         //distinguish 310 from 210 camera
930         RK_U32     i;
931         RK_U32 copy_len = 0;
932         tmp = src;
933         jpegd_dbg_parser("distinguish 310 from 210 camera");
934 
935         for (i = 0; i < src_size - 4; i++) {
936             if (tmp[i] == 0xff) {
937                 if (tmp[i + 1] == 0x00 && tmp[i + 2] == 0xff && ((tmp[i + 3] & 0xf0) == 0xd0))
938                     i += 2;
939             }
940             *dst++ = tmp[i];
941             copy_len++;
942         }
943         for (; i < src_size; i++) {
944             *dst++ = tmp[i];
945             copy_len++;
946         }
947         if (copy_len < src_size)
948             memset(dst, 0, src_size - copy_len);
949         *copy_length = copy_len;
950     } else {
951         memcpy(dst, src, src_size);
952         memset(dst + src_size, 0, str_size > dst_size ? dst_size - src_size : str_size - src_size);
953         *copy_length = src_size;
954     }
955 
956     jpegd_dbg_func("exit\n");
957     return ret;
958 }
959 
jpegd_prepare(void * ctx,MppPacket pkt,HalDecTask * task)960 static MPP_RET jpegd_prepare(void *ctx, MppPacket pkt, HalDecTask *task)
961 {
962     jpegd_dbg_func("enter\n");
963     MPP_RET ret = MPP_OK;
964     JpegdCtx *JpegCtx = (JpegdCtx *)ctx;
965     if (!JpegCtx->copy_flag) {
966         /* no need to copy stream, handle packet from upper application directly*/
967         JpegCtx->input_packet = pkt;
968     }
969 
970     MppPacket input_packet = JpegCtx->input_packet;
971     RK_U32 copy_length = 0;
972     void *base = mpp_packet_get_pos(pkt);
973     RK_U8 *pos = base;
974     RK_U32 pkt_length = (RK_U32)mpp_packet_get_length(pkt);
975     RK_U32 eos = (pkt_length) ? (mpp_packet_get_eos(pkt)) : (1);
976 
977     JpegCtx->pts = mpp_packet_get_pts(pkt);
978 
979     task->valid = 0;
980     task->flags.eos = eos;
981     JpegCtx->eos = eos;
982 
983     jpegd_dbg_parser("pkt_length %d eos %d\n", pkt_length, eos);
984 
985     if (!pkt_length) {
986         jpegd_dbg_parser("it is end of stream.");
987         return ret;
988     }
989 
990     if (pkt_length > JpegCtx->bufferSize) {
991         jpegd_dbg_parser("Huge Frame(%d Bytes)! bufferSize:%d",
992                          pkt_length, JpegCtx->bufferSize);
993         mpp_free(JpegCtx->recv_buffer);
994         JpegCtx->recv_buffer = NULL;
995 
996         JpegCtx->recv_buffer = mpp_calloc(RK_U8, pkt_length + 1024);
997         if (NULL == JpegCtx->recv_buffer) {
998             mpp_err_f("no memory!");
999             return MPP_ERR_NOMEM;
1000         }
1001 
1002         JpegCtx->bufferSize = pkt_length + 1024;
1003     }
1004 
1005     if (JpegCtx->copy_flag)
1006         jpegd_split_frame(base, pkt_length, JpegCtx->recv_buffer,
1007                           JpegCtx->bufferSize, &copy_length);
1008 
1009     pos += pkt_length;
1010     mpp_packet_set_pos(pkt, pos);
1011     if (copy_length != pkt_length) {
1012         jpegd_dbg_parser("packet prepare, pkt_length:%d, copy_length:%d\n",
1013                          pkt_length, copy_length);
1014     }
1015 
1016     /* debug information */
1017     if (jpegd_debug & JPEGD_DBG_IO) {
1018         static FILE *jpg_file;
1019         static char name[32];
1020 
1021         snprintf(name, sizeof(name) - 1, "/data/input%02d.jpg",
1022                  JpegCtx->input_jpeg_count);
1023         jpg_file = fopen(name, "wb+");
1024         if (jpg_file) {
1025             jpegd_dbg_io("frame_%02d input jpeg(%d Bytes) saving to %s\n",
1026                          JpegCtx->input_jpeg_count, pkt_length, name);
1027             fwrite(base, pkt_length, 1, jpg_file);
1028             fclose(jpg_file);
1029             JpegCtx->input_jpeg_count++;
1030         }
1031     }
1032 
1033     if (JpegCtx->copy_flag) {
1034         mpp_packet_set_data(input_packet, JpegCtx->recv_buffer);
1035         mpp_packet_set_size(input_packet, pkt_length);
1036         mpp_packet_set_length(input_packet, pkt_length);
1037         memcpy(base, JpegCtx->recv_buffer, pkt_length);
1038     }
1039 
1040     JpegCtx->streamLength = pkt_length;
1041     task->input_packet = input_packet;
1042     task->valid = 1;
1043     jpegd_dbg_parser("input_packet:%p, recv_buffer:%p, pkt_length:%d",
1044                      input_packet,
1045                      JpegCtx->recv_buffer, pkt_length);
1046 
1047     jpegd_dbg_func("exit\n");
1048     return ret;
1049 }
1050 
jpegd_allocate_frame(JpegdCtx * ctx)1051 static MPP_RET jpegd_allocate_frame(JpegdCtx *ctx)
1052 {
1053     jpegd_dbg_func("enter\n");
1054     JpegdSyntax *s = ctx->syntax;
1055     MppBufSlots slots = ctx->frame_slots;
1056     MppFrame output = ctx->output_frame;
1057     RK_S32 slot_idx = ctx->frame_slot_index;
1058 
1059     if (slot_idx == -1) {
1060         RK_U32 value;
1061         MppFrameFormat fmt = MPP_FMT_YUV420SP;
1062 
1063         switch (s->yuv_mode) {
1064         case JPEGDEC_YUV420: {
1065             fmt = MPP_FMT_YUV420SP;
1066         } break;
1067         case JPEGDEC_YUV422: {
1068             fmt = MPP_FMT_YUV422SP;
1069         } break;
1070         case JPEGDEC_YUV444: {
1071             fmt = MPP_FMT_YUV444SP;
1072         } break;
1073         case JPEGDEC_YUV400: {
1074             fmt = MPP_FMT_YUV400;
1075         } break;
1076         default : {
1077             fmt = MPP_FMT_YUV420SP;
1078         } break;
1079         }
1080 
1081         mpp_frame_set_fmt(output, fmt);
1082         mpp_frame_set_width(output, s->width);
1083         mpp_frame_set_height(output, s->height);
1084         mpp_frame_set_hor_stride(output, s->hor_stride);
1085         mpp_frame_set_ver_stride(output, s->ver_stride);
1086         mpp_frame_set_pts(output, ctx->pts);
1087 
1088         if (ctx->eos)
1089             mpp_frame_set_eos(output, 1);
1090 
1091         mpp_buf_slot_get_unused(slots, &slot_idx);
1092         ctx->frame_slot_index = slot_idx;
1093         jpegd_dbg_parser("frame_slot_index:%d\n", slot_idx);
1094 
1095         value = 2;
1096         mpp_slots_set_prop(slots, SLOTS_NUMERATOR, &value);
1097         value = 1;
1098         mpp_slots_set_prop(slots, SLOTS_DENOMINATOR, &value);
1099         if (mpp_buf_slot_set_prop(slots, slot_idx, SLOT_FRAME, output))
1100             return MPP_ERR_VALUE;
1101         mpp_buf_slot_set_flag(slots, slot_idx, SLOT_CODEC_USE);
1102         mpp_buf_slot_set_flag(slots, slot_idx, SLOT_HAL_OUTPUT);
1103     }
1104 
1105     jpegd_dbg_func("exit\n");
1106     return MPP_OK;
1107 }
1108 
jpegd_update_frame(JpegdCtx * ctx)1109 static MPP_RET jpegd_update_frame(JpegdCtx *ctx)
1110 {
1111     jpegd_dbg_func("enter\n");
1112 
1113     mpp_buf_slot_clr_flag(ctx->frame_slots, ctx->frame_slot_index,
1114                           SLOT_CODEC_USE);
1115     ctx->frame_slot_index = -1;
1116 
1117     jpegd_dbg_func("exit\n");
1118     return MPP_OK;
1119 }
1120 
jpegd_parse(void * ctx,HalDecTask * task)1121 static MPP_RET jpegd_parse(void *ctx, HalDecTask *task)
1122 {
1123     jpegd_dbg_func("enter\n");
1124     MPP_RET ret = MPP_OK;
1125     JpegdCtx *JpegCtx = (JpegdCtx *)ctx;
1126     task->valid = 0;
1127 
1128     JpegCtx->buffer = (RK_U8 *)mpp_packet_get_data(JpegCtx->input_packet);
1129 
1130     memset(JpegCtx->syntax, 0, sizeof(JpegdSyntax));
1131 
1132     ret = jpegd_decode_frame(JpegCtx);
1133     if (MPP_OK == ret) {
1134         if (jpegd_allocate_frame(JpegCtx))
1135             return MPP_ERR_VALUE;
1136 
1137         task->syntax.data = (void *)JpegCtx->syntax;
1138         task->syntax.number = sizeof(JpegdSyntax);
1139         task->output = JpegCtx->frame_slot_index;
1140         task->valid = 1;
1141 
1142         jpegd_update_frame(JpegCtx);
1143     } else
1144         task->flags.parse_err = 1;
1145 
1146     jpegd_dbg_func("exit\n");
1147     return ret;
1148 }
1149 
jpegd_deinit(void * ctx)1150 static MPP_RET jpegd_deinit(void *ctx)
1151 {
1152     jpegd_dbg_func("enter\n");
1153     JpegdCtx *JpegCtx = (JpegdCtx *)ctx;
1154 
1155     if (JpegCtx->recv_buffer) {
1156         mpp_free(JpegCtx->recv_buffer);
1157         JpegCtx->recv_buffer = NULL;
1158     }
1159 
1160     if (JpegCtx->output_frame) {
1161         mpp_frame_deinit(&JpegCtx->output_frame);
1162     }
1163 
1164     if (JpegCtx->copy_flag) {
1165         if (JpegCtx->input_packet) {
1166             mpp_packet_deinit(&JpegCtx->input_packet);
1167         }
1168     } else {
1169         JpegCtx->input_packet = NULL;
1170     }
1171 
1172     if (JpegCtx->bit_ctx) {
1173         mpp_free(JpegCtx->bit_ctx);
1174         JpegCtx->bit_ctx = NULL;
1175     }
1176 
1177     if (JpegCtx->syntax) {
1178         mpp_free(JpegCtx->syntax);
1179         JpegCtx->syntax = NULL;
1180     }
1181 
1182     JpegCtx->pts = 0;
1183     JpegCtx->eos = 0;
1184     JpegCtx->input_jpeg_count = 0;
1185 
1186     jpegd_dbg_func("exit\n");
1187     return 0;
1188 }
1189 
jpegd_init(void * ctx,ParserCfg * parser_cfg)1190 static MPP_RET jpegd_init(void *ctx, ParserCfg *parser_cfg)
1191 {
1192     JpegdCtx *JpegCtx = (JpegdCtx *)ctx;
1193     const MppDecHwCap *hw_info = parser_cfg->hw_info;
1194 
1195     jpegd_dbg_func("enter\n");
1196 
1197     if (NULL == JpegCtx) {
1198         JpegCtx = (JpegdCtx *)mpp_calloc(JpegdCtx, 1);
1199         if (NULL == JpegCtx) {
1200             mpp_err_f("NULL pointer");
1201             return MPP_ERR_NULL_PTR;
1202         }
1203     }
1204 
1205     mpp_env_get_u32("jpegd_debug", &jpegd_debug, 0);
1206     // mpp only support baseline
1207     JpegCtx->scan_all_marker = 0;
1208 
1209     if (hw_info && hw_info->cap_hw_jpg_fix) {
1210         /*
1211          * no need to copy stream when decoding jpeg;
1212          * just scan parts of markers to reduce CPU's occupancy
1213          */
1214         JpegCtx->copy_flag = 0;
1215     } else {
1216         // TODO: do not copy if input provides valid fd and virtual ptr
1217         JpegCtx->copy_flag = 1;
1218     }
1219 
1220     JpegCtx->frame_slots = parser_cfg->frame_slots;
1221     JpegCtx->packet_slots = parser_cfg->packet_slots;
1222     JpegCtx->frame_slot_index = -1;
1223     mpp_buf_slot_setup(JpegCtx->frame_slots, 1);
1224 
1225     JpegCtx->recv_buffer = mpp_calloc(RK_U8, JPEGD_STREAM_BUFF_SIZE);
1226     if (NULL == JpegCtx->recv_buffer) {
1227         mpp_err_f("no memory!");
1228         return MPP_ERR_NOMEM;
1229     }
1230     JpegCtx->bufferSize = JPEGD_STREAM_BUFF_SIZE;
1231     if (JpegCtx->copy_flag) {
1232         mpp_packet_init(&JpegCtx->input_packet,
1233                         JpegCtx->recv_buffer, JPEGD_STREAM_BUFF_SIZE);
1234     } else {
1235         JpegCtx->input_packet = NULL;
1236     }
1237 
1238     mpp_frame_init(&JpegCtx->output_frame);
1239     if (!JpegCtx->output_frame) {
1240         mpp_err_f("Failed to allocate output frame buffer");
1241         return MPP_ERR_NOMEM;
1242     }
1243 
1244     JpegCtx->bit_ctx = mpp_calloc(BitReadCtx_t, 1);
1245     if (JpegCtx->bit_ctx == NULL) {
1246         mpp_err_f("allocate bit_ctx failed\n");
1247         return MPP_ERR_MALLOC;
1248     }
1249 
1250     JpegCtx->syntax = mpp_calloc(JpegdSyntax, 1);
1251     if (JpegCtx->syntax == NULL) {
1252         mpp_err_f("allocate syntax failed\n");
1253         return MPP_ERR_MALLOC;
1254     }
1255     memset(JpegCtx->syntax, 0, sizeof(JpegdSyntax));
1256 
1257     JpegCtx->pts = 0;
1258     JpegCtx->eos = 0;
1259     JpegCtx->input_jpeg_count = 0;
1260 
1261     jpegd_dbg_func("exit\n");
1262     return MPP_OK;
1263 }
1264 
jpegd_flush(void * ctx)1265 static MPP_RET jpegd_flush(void *ctx)
1266 {
1267     jpegd_dbg_func("enter\n");
1268     JpegdCtx *JpegCtx = (JpegdCtx *)ctx;
1269     (void)JpegCtx;
1270     jpegd_dbg_func("exit\n");
1271     return MPP_OK;
1272 }
1273 
jpegd_reset(void * ctx)1274 static MPP_RET jpegd_reset(void *ctx)
1275 {
1276     jpegd_dbg_func("enter\n");
1277     JpegdCtx *JpegCtx = (JpegdCtx *)ctx;
1278 
1279     (void)JpegCtx;
1280 
1281     jpegd_dbg_func("exit\n");
1282     return MPP_OK;
1283 }
1284 
jpegd_control(void * ctx,MpiCmd cmd,void * param)1285 static MPP_RET jpegd_control(void *ctx, MpiCmd cmd, void *param)
1286 {
1287     jpegd_dbg_func("enter\n");
1288     MPP_RET ret = MPP_OK;
1289     (void) ctx;
1290     (void) cmd;
1291     (void) param;
1292     jpegd_dbg_func("exit\n");
1293     return ret;
1294 }
1295 
jpegd_callback(void * decoder,void * err_info)1296 static MPP_RET jpegd_callback(void *decoder, void *err_info)
1297 {
1298     JpegdCtx *JpegCtx = (JpegdCtx *)decoder;
1299     DecCbHalDone *ctx = (DecCbHalDone *)err_info;
1300     HalDecTask *task_dec = (HalDecTask *)ctx->task;
1301     RK_U32 task_err = task_dec->flags.parse_err;
1302     RK_U32 hw_dec_err = ctx->hard_err;
1303     RK_S32 output = task_dec->output;
1304     RK_U32 err_mark = 0;
1305     MppFrame frame = NULL;
1306 
1307     if (output >= 0)
1308         mpp_buf_slot_get_prop(JpegCtx->frame_slots, output, SLOT_FRAME_PTR, &frame);
1309 
1310     if (!frame)
1311         goto __RETURN;
1312 
1313     /* check and mark current frame */
1314     if (task_err)
1315         err_mark |= MPP_FRAME_ERR_DEC_INVALID;
1316     else if (hw_dec_err)
1317         err_mark |= MPP_FRAME_ERR_DEC_HW_ERR;
1318     if (err_mark)
1319         mpp_frame_set_errinfo(frame, err_mark);
1320 
1321 __RETURN:
1322     return MPP_OK;
1323 }
1324 
1325 const ParserApi api_jpegd_parser = {
1326     .name = "jpegd_parse",
1327     .coding = MPP_VIDEO_CodingMJPEG,
1328     .ctx_size = sizeof(JpegdCtx),
1329     .flag = 0,
1330     .init = jpegd_init,
1331     .deinit = jpegd_deinit,
1332     .prepare = jpegd_prepare,
1333     .parse = jpegd_parse,
1334     .reset = jpegd_reset,
1335     .flush = jpegd_flush,
1336     .control = jpegd_control,
1337     .callback = jpegd_callback,
1338 };
1339 
1340 
1341