xref: /OK3568_Linux_fs/external/mpp/mpp/hal/vpu/jpegd/hal_jpegd_common.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *
3  * Copyright 2017 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 #define MODULE_TAG "HAL_JPEGD_COMMON"
18 
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdint.h>
22 
23 #include "mpp_env.h"
24 #include "mpp_mem.h"
25 #include "mpp_debug.h"
26 #include "mpp_bitread.h"
27 #include "mpp_bitput.h"
28 
29 #include "hal_dec_task.h"
30 #include "jpegd_syntax.h"
31 #include "jpegd_api.h"
32 #include "hal_jpegd_common.h"
33 
34 static PpRgbCfg pp_rgb_cfgs[PP_RGB_CFG_LENTH] = {
35     //ffmpeg: rgb565be, bin(rrrr,rggg, gggb,bbbb) mem MSB-->LSB(gggb,bbbb, rrrr,rggg)
36     {
37         .fmt = MPP_FMT_RGB565, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
38         .out_endian = 1, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 1,
39         .r_padd = 0, .g_padd = 5, .b_padd = 11,
40         .r_dither = 2, .g_dither = 3, .b_dither = 2,
41         .r_mask = 0xf800f800, .g_mask = 0x07e007e0, .b_mask = 0x001f001f
42     },
43     //ffmpeg: bgr565be, bin(bbbb,bggg, gggr,rrrr) mem MSB-->LSB(gggr,rrrr, bbbb,bggg)
44     {
45         .fmt = MPP_FMT_BGR565, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
46         .out_endian = 1, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 1,
47         .r_padd = 11, .g_padd = 5, .b_padd = 0,
48         .r_dither = 2, .g_dither = 3, .b_dither = 2,
49         .r_mask = 0x001f001f, .g_mask = 0x07e007e0, .b_mask = 0xf800f800
50     },
51     //ffmpeg: rgb555be, bin(0rrr,rrgg, gggb,bbbb) mem MSB-->LSB(gggb,bbbb, 0rrr,rrgg)
52     {
53         .fmt = MPP_FMT_RGB555, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
54         .out_endian = 1, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 1,
55         .r_padd = 1, .g_padd = 6, .b_padd = 11,
56         .r_dither = 2, .g_dither = 2, .b_dither = 2,
57         .r_mask = 0x7c007c00, .g_mask = 0x03e003e0, .b_mask = 0x001f001f
58     },
59     //ffmpeg: bgr555be, bin(0bbb,bbgg, gggr,rrrr) mem MSB-->LSB(gggr,rrrr, 0bbb,bbgg)
60     {
61         .fmt = MPP_FMT_BGR555, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
62         .out_endian = 1, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 1,
63         .r_padd = 11, .g_padd = 6, .b_padd = 1,
64         .r_dither = 2, .g_dither = 2, .b_dither = 2,
65         .r_mask = 0x001f001f, .g_mask = 0x03e003e0, .b_mask = 0x7c007c00
66     },
67     //ffmpeg: rgb444be, bin(0000,rrrr, gggg,bbbb) mem MSB-->LSB(gggg,bbbb, 0000,rrrr)
68     {
69         .fmt = MPP_FMT_RGB444, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
70         .out_endian = 0, .swap_16 = 1, .swap_32 = 1, .rgb_in_32 = 1,
71         .r_padd = 12, .g_padd = 0, .b_padd = 4,
72         .r_dither = 1, .g_dither = 1, .b_dither = 1,
73         .r_mask = 0x000f000f, .g_mask = 0xf000f000, .b_mask = 0x0f000f00
74     },
75     //ffmpeg: bgr444be, bin(0000,bbbb, gggg,rrrr) mem MSB-->LSB(gggg,rrrr, 0000,bbbb)
76     {
77         .fmt = MPP_FMT_BGR444, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
78         .out_endian = 0, .swap_16 = 1, .swap_32 = 1, .rgb_in_32 = 1,
79         .r_padd = 4, .g_padd = 0, .b_padd = 12, .r_mask = 0x0f000f00,
80         .r_dither = 1, .g_dither = 1, .b_dither = 1,
81         .g_mask = 0xf000f000, .b_mask = 0x000f000f
82     },
83     //ffmpeg: argb, bin(aaaa,aaaa, rrrr,rrrr, gggg,gggg, bbbb,bbbb)
84     {
85         .fmt = MPP_FMT_ARGB8888, .pp_out_fmt = PP_OUT_FORMAT_ARGB,
86         .out_endian = 0, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 0,
87         .r_padd = 16, .g_padd = 8, .b_padd = 0,
88         .r_dither = 0, .g_dither = 0, .b_dither = 0,
89         .r_mask = 0x0000ff00 | 0xff,
90         .g_mask = 0x00ff0000 | 0xff,
91         .b_mask = 0xff000000 | 0xff
92     },
93     //ffmepg: rgba, bin(aaaa,aaaa, bbbb,bbbb, gggg,gggg, rrrr,rrrr)
94     {
95         .fmt = MPP_FMT_ABGR8888, .pp_out_fmt = PP_OUT_FORMAT_ARGB,
96         .out_endian = 0, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 0,
97         .r_padd = 0, .g_padd = 8, .b_padd = 16,
98         .r_dither = 0, .g_dither = 0, .b_dither = 0,
99         .r_mask = 0xff000000 | 0xff,
100         .g_mask = 0x00ff0000 | 0xff,
101         .b_mask = 0x0000ff00 | 0xff
102     },
103     //ffmpeg: bgra, bin(bbbb,bbbb, gggg,gggg, rrrr,rrrr, aaaa,aaaa)
104     {
105         .fmt = MPP_FMT_BGRA8888, .pp_out_fmt = PP_OUT_FORMAT_ARGB,
106         .out_endian = 0, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 0,
107         .r_padd = 8, .g_padd = 16, .b_padd = 24,
108         .r_dither = 0, .g_dither = 0, .b_dither = 0,
109         .r_mask = 0x00ff0000 | (0xff << 24),
110         .g_mask = 0x0000ff00 | (0xff << 24),
111         .b_mask = 0x000000ff | (0xff << 24)
112     },
113     //ffmpeg: rgba, bin(rrrr,rrrr, gggg,gggg, bbbb,bbbb, aaaa,aaaa)
114     {
115         .fmt = MPP_FMT_RGBA8888, .pp_out_fmt = PP_OUT_FORMAT_ARGB,
116         .out_endian = 0, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 0,
117         .r_padd = 24, .g_padd = 16, .b_padd = 8,
118         .r_dither = 0, .g_dither = 0, .b_dither = 0,
119         .r_mask = 0x000000ff | (0xff << 24),
120         .g_mask = 0x0000ff00 | (0xff << 24),
121         .b_mask = 0x00ff0000 | (0xff << 24)
122     },
123 };
124 
125 static PpRgbCfg pp_rgb_le_cfgs[PP_RGB_CFG_LENTH] = {
126     //ffmpeg: rgb565le, bin(gggb,bbbb, rrrr,rggg) mem MSB-->LSB(rrrr,rggg, gggb,bbbb)
127     {
128         .fmt = MPP_FMT_RGB565 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
129         .out_endian = 0, .swap_16 = 1, .swap_32 = 1, .rgb_in_32 = 1,
130         .r_padd = 0, .g_padd = 5, .b_padd = 11,
131         .r_dither = 2, .g_dither = 3, .b_dither = 2,
132         .r_mask = 0xf800f800, .g_mask = 0x07e007e0, .b_mask = 0x001f001f
133     },
134     //ffmpeg: bgr565le, bin(gggr,rrrr, bbbb,bggg) mem MSB-->LSB(bbbb,bggg, gggr,rrrr)
135     {
136         .fmt = MPP_FMT_BGR565 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
137         .out_endian = 0, .swap_16 = 1, .swap_32 = 1, .rgb_in_32 = 1,
138         .r_padd = 11, .g_padd = 5, .b_padd = 0,
139         .r_dither = 2, .g_dither = 3, .b_dither = 2,
140         .r_mask = 0x001f001f, .g_mask = 0x07e007e0, .b_mask = 0xf800f800
141     },
142     //ffmpeg: rgb555le, bin(gggb,bbbb, 0rrr,rrgg) mem MSB-->LSB(0rrr,rrgg, gggb,bbbb)
143     {
144         .fmt = MPP_FMT_RGB555 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
145         .out_endian = 0, .swap_16 = 1, .swap_32 = 1, .rgb_in_32 = 1,
146         .r_padd = 1, .g_padd = 6, .b_padd = 11,
147         .r_dither = 2, .g_dither = 2, .b_dither = 2,
148         .r_mask = 0x7c007c00, .g_mask = 0x03e003e0, .b_mask = 0x001f001f
149     },
150     //ffmpeg: bgr555le, bin(gggr,rrrr, 0bbb,bbgg) mem MSB-->LSB(0bbb,bbgg, gggr,rrrr)
151     {
152         .fmt = MPP_FMT_BGR555 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
153         .out_endian = 0, .swap_16 = 1, .swap_32 = 1, .rgb_in_32 = 1,
154         .r_padd = 11, .g_padd = 6, .b_padd = 1,
155         .r_dither = 2, .g_dither = 2, .b_dither = 2,
156         .r_mask = 0x001f001f, .g_mask = 0x03e003e0, .b_mask = 0x7c007c00
157     },
158     //ffmpeg: rgb444le, bin(gggg,bbbb, 0000,rrrr) mem MSB-->LSB(0000,rrrr, gggg,bbbb)
159     {
160         .fmt = MPP_FMT_RGB444 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
161         .out_endian = 1, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 1,
162         .r_padd = 12, .g_padd = 0, .b_padd = 4,
163         .r_dither = 1, .g_dither = 1, .b_dither = 1,
164         .r_mask = 0x000f000f, .g_mask = 0xf000f000, .b_mask = 0x0f000f00
165     },
166     //ffmpeg: bgr444le, bin(gggg,rrrr, 0000,bbbb) mem MSB-->LSB(0000,bbbb, gggg,rrrr)
167     {
168         .fmt = MPP_FMT_BGR444 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_RGB565,
169         .out_endian = 1, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 1,
170         .r_padd = 4, .g_padd = 0, .b_padd = 12, .r_mask = 0x0f000f00,
171         .r_dither = 1, .g_dither = 1, .b_dither = 1,
172         .g_mask = 0xf000f000, .b_mask = 0x000f000f
173     },
174     //in memory: [31:0] A:R:G:B 8:8:8:8 little endian
175     {
176         .fmt = MPP_FMT_ARGB8888 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_ARGB,
177         .out_endian = 0, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 0,
178         .r_padd = 8, .g_padd = 16, .b_padd = 24,
179         .r_dither = 0, .g_dither = 0, .b_dither = 0,
180         .r_mask = 0x00ff0000 | (0xff << 24),
181         .g_mask = 0x0000ff00 | (0xff << 24),
182         .b_mask = 0x000000ff | (0xff << 24)
183     },
184     //in memory: [31:0] A:B:G:R 8:8:8:8 little endian
185     {
186         .fmt = MPP_FMT_ABGR8888 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_ARGB,
187         .out_endian = 0, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 0,
188         .r_padd = 24, .g_padd = 16, .b_padd = 8,
189         .r_dither = 0, .g_dither = 0, .b_dither = 0,
190         .r_mask = 0x000000ff | (0xff << 24),
191         .g_mask = 0x0000ff00 | (0xff << 24),
192         .b_mask = 0x00ff0000 | (0xff << 24)
193     },
194     //in memory: [31:0] B:G:R:A 8:8:8:8 little endian
195     {
196         .fmt = MPP_FMT_BGRA8888 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_ARGB,
197         .out_endian = 0, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 0,
198         .r_padd = 16, .g_padd = 8, .b_padd = 0,
199         .r_dither = 0, .g_dither = 0, .b_dither = 0,
200         .r_mask = 0x0000ff00 | 0xff,
201         .g_mask = 0x00ff0000 | 0xff,
202         .b_mask = 0xff000000 | 0xff
203     },
204     //in memory: [31:0] R:G:B:A 8:8:8:8 little endian
205     {
206         .fmt = MPP_FMT_RGBA8888 | MPP_FRAME_FMT_LE_MASK, .pp_out_fmt = PP_OUT_FORMAT_ARGB,
207         .out_endian = 0, .swap_16 = 0, .swap_32 = 1, .rgb_in_32 = 0,
208         .r_padd = 0, .g_padd = 8, .b_padd = 16,
209         .r_dither = 0, .g_dither = 0, .b_dither = 0,
210         .r_mask = 0xff000000 | 0xff,
211         .g_mask = 0x00ff0000 | 0xff,
212         .b_mask = 0x0000ff00 | 0xff
213     },
214 };
215 
get_pp_rgb_Cfg(MppFrameFormat fmt)216 PpRgbCfg* get_pp_rgb_Cfg(MppFrameFormat fmt)
217 {
218     PpRgbCfg* cfg = NULL;
219     PpRgbCfg* cfg_array = NULL;
220     RK_U8 i = 0;
221 
222     if (MPP_FRAME_FMT_IS_LE(fmt))
223         cfg_array = pp_rgb_le_cfgs;
224     else
225         cfg_array = pp_rgb_cfgs;
226 
227     for (i = 0; i < PP_RGB_CFG_LENTH; i++) {
228         if (cfg_array[i].fmt ==  fmt) {
229             cfg = &cfg_array[i];
230             break;
231         }
232     }
233 
234     return cfg;
235 }
236 
jpegd_vdpu_tail_0xFF_patch(MppBuffer stream,RK_U32 length)237 RK_U32 jpegd_vdpu_tail_0xFF_patch(MppBuffer stream, RK_U32 length)
238 {
239     RK_U8 *p = mpp_buffer_get_ptr(stream);
240     RK_U8 *end = p + length;
241 
242     if (end[-1] == 0xD9 && end[-2] == 0xFF) {
243         end -= 2;
244 
245         do {
246             if (end[-1] == 0xFF) {
247                 end--;
248                 length--;
249                 continue;
250             }
251             break;
252         } while (1);
253 
254         end[0] = 0xff;
255         end[1] = 0xD9;
256     }
257 
258     return length;
259 }
260 
jpegd_write_qp_ac_dc_table(JpegdHalCtx * ctx,JpegdSyntax * syntax)261 void jpegd_write_qp_ac_dc_table(JpegdHalCtx *ctx,
262                                 JpegdSyntax*syntax)
263 {
264     jpegd_dbg_func("enter\n");
265     JpegdSyntax *s = syntax;
266     RK_U32 *base = (RK_U32 *)mpp_buffer_get_ptr(ctx->pTableBase);
267     RK_U8 table_tmp[QUANTIZE_TABLE_LENGTH] = {0};
268     RK_U32 idx, table_word = 0, table_value = 0;
269     RK_U32 shifter = 32;
270     AcTable *ac_ptr0 = NULL, *ac_ptr1 = NULL;
271     DcTable *dc_ptr0 = NULL, *dc_ptr1 = NULL;
272     RK_U32 i, j = 0;
273 
274     /* Quantize tables for all components
275      * length = 64 * 3  (Bytes)
276      */
277     for (j = 0; j < s->qtable_cnt; j++) {
278         idx = s->quant_index[j]; /* quantize table index used by j component */
279 
280         for (i = 0; i < QUANTIZE_TABLE_LENGTH; i++) {
281             table_tmp[zzOrder[i]] = (RK_U8) s->quant_matrixes[idx][i];
282         }
283 
284         /* could memcpy be OK?? */
285         for (i = 0; i < QUANTIZE_TABLE_LENGTH; i += 4) {
286             /* transfer to big endian */
287             table_word = (table_tmp[i] << 24) |
288                          (table_tmp[i + 1] << 16) |
289                          (table_tmp[i + 2] << 8) |
290                          table_tmp[i + 3];
291             *base = table_word;
292             base++;
293         }
294     }
295 
296     /* write AC and DC tables
297      * memory:  AC(Y) - AC(UV) - DC(Y) - DC(UV)
298      * length = 162   + 162    + 12    + 12   (Bytes)
299      */
300     {
301         /* this trick is done because hardware always wants
302          * luma table as ac hardware table 0 */
303         if (s->ac_index[0] == HUFFMAN_TABLE_ID_ZERO) {
304             /* Luma's AC uses Huffman table zero */
305             ac_ptr0 = &(s->ac_table[HUFFMAN_TABLE_ID_ZERO]);
306             ac_ptr1 = &(s->ac_table[HUFFMAN_TABLE_ID_ONE]);
307         } else {
308             ac_ptr0 = &(s->ac_table[HUFFMAN_TABLE_ID_ONE]);
309             ac_ptr1 = &(s->ac_table[HUFFMAN_TABLE_ID_ZERO]);
310         }
311 
312         /* write luma AC table */
313         for (i = 0; i < MAX_AC_HUFFMAN_TABLE_LENGTH; i++) {
314             if (i < ac_ptr0->actual_length)
315                 table_value = (RK_U8) ac_ptr0->vals[i];
316             else
317                 table_value = 0;
318 
319             /* transfer to big endian */
320             if (shifter == 32)
321                 table_word = (table_value << (shifter - 8));
322             else
323                 table_word |= (table_value << (shifter - 8));
324 
325             shifter -= 8;
326             if (shifter == 0) {
327                 /* write 4 Bytes(32 bit) */
328                 *base = table_word;
329                 base++;
330                 shifter = 32;
331             }
332         }
333 
334         /* write chroma AC table */
335         for (i = 0; i < MAX_AC_HUFFMAN_TABLE_LENGTH; i++) {
336             /* chroma's AC table must be zero for YUV400 */
337             if ((s->yuv_mode != JPEGDEC_YUV400) && (i < ac_ptr1->actual_length))
338                 table_value = (RK_U8) ac_ptr1->vals[i];
339             else
340                 table_value = 0;
341 
342             /* transfer to big endian */
343             if (shifter == 32)
344                 table_word = (table_value << (shifter - 8));
345             else
346                 table_word |= (table_value << (shifter - 8));
347 
348             shifter -= 8;
349             if (shifter == 0) {
350                 /* write 4 Bytes(32 bit) */
351                 *base = table_word;
352                 base++;
353                 shifter = 32;
354             }
355         }
356 
357         /* this trick is done because hardware always wants
358          * luma table as dc hardware table 0 */
359         if (s->dc_index[0] == HUFFMAN_TABLE_ID_ZERO) {
360             /* Luma's DC uses Huffman table zero */
361             dc_ptr0 = &(s->dc_table[HUFFMAN_TABLE_ID_ZERO]);
362             dc_ptr1 = &(s->dc_table[HUFFMAN_TABLE_ID_ONE]);
363         } else {
364             dc_ptr0 = &(s->dc_table[HUFFMAN_TABLE_ID_ONE]);
365             dc_ptr1 = &(s->dc_table[HUFFMAN_TABLE_ID_ZERO]);
366         }
367 
368         /* write luma DC table */
369         for (i = 0; i < MAX_DC_HUFFMAN_TABLE_LENGTH; i++) {
370             if (i < dc_ptr0->actual_length)
371                 table_value = (RK_U8) dc_ptr0->vals[i];
372             else
373                 table_value = 0;
374 
375             /* transfer to big endian */
376             if (shifter == 32)
377                 table_word = (table_value << (shifter - 8));
378             else
379                 table_word |= (table_value << (shifter - 8));
380 
381             shifter -= 8;
382             if (shifter == 0) {
383                 /* write 4 Bytes(32 bit) */
384                 *base = table_word;
385                 base++;
386                 shifter = 32;
387             }
388         }
389 
390         /* write chroma DC table */
391         for (i = 0; i < MAX_DC_HUFFMAN_TABLE_LENGTH; i++) {
392             /* chroma's DC table must be zero for YUV400 */
393             if ((s->yuv_mode != JPEGDEC_YUV400) && (i < dc_ptr1->actual_length))
394                 table_value = (RK_U8) dc_ptr1->vals[i];
395             else
396                 table_value = 0;
397 
398             /* transfer to big endian */
399             if (shifter == 32)
400                 table_word = (table_value << (shifter - 8));
401             else
402                 table_word |= (table_value << (shifter - 8));
403 
404             shifter -= 8;
405             if (shifter == 0) {
406                 /* write 4 Bytes(32 bit) */
407                 *base = table_word;
408                 base++;
409                 shifter = 32;
410             }
411         }
412     }
413 
414     /* four-byte padding zero */
415     for (i = 0; i < 4; i++) {
416         table_value = 0;
417 
418         if (shifter == 32)
419             table_word = (table_value << (shifter - 8));
420         else
421             table_word |= (table_value << (shifter - 8));
422 
423         shifter -= 8;
424 
425         if (shifter == 0) {
426             *base = table_word;
427             base++;
428             shifter = 32;
429         }
430     }
431     jpegd_dbg_func("exit\n");
432     return;
433 }
434 
jpegd_check_have_pp(JpegdHalCtx * ctx)435 void jpegd_check_have_pp(JpegdHalCtx *ctx)
436 {
437     ctx->codec_type = mpp_get_vcodec_type();
438     ctx->have_pp = ((ctx->dev_type == VPU_CLIENT_VDPU1) &&
439                     (ctx->codec_type & (1 << VPU_CLIENT_VDPU1_PP))) ||
440                    ((ctx->dev_type == VPU_CLIENT_VDPU2) &&
441                     (ctx->codec_type & (1 << VPU_CLIENT_VDPU2_PP)));
442 }
443 
jpegd_setup_output_fmt(JpegdHalCtx * ctx,JpegdSyntax * s,RK_S32 output)444 MPP_RET jpegd_setup_output_fmt(JpegdHalCtx *ctx, JpegdSyntax *s, RK_S32 output)
445 {
446     jpegd_dbg_func("enter\n");
447     RK_U32 pp_in_fmt = 0;
448     RK_U32 stride = 0;
449     PPInfo *pp_info = &ctx->pp_info;
450     MppClientType dev_type = ctx->dev_type;
451     MppFrame frm = NULL;
452     MPP_RET ret = MPP_OK;
453 
454     if (ctx->have_pp && ctx->set_output_fmt_flag &&
455         ctx->output_fmt != s->output_fmt) {
456         MppFrameFormat fmt = MPP_FMT_BUTT;
457 
458         /* Using pp to convert all format to yuv420sp */
459         switch (s->output_fmt) {
460         case MPP_FMT_YUV400:
461             pp_in_fmt = PP_IN_FORMAT_YUV400;
462             break;
463         case MPP_FMT_YUV420SP:
464             pp_in_fmt = PP_IN_FORMAT_YUV420SEMI;
465             break;
466         case MPP_FMT_YUV422SP:
467             pp_in_fmt = PP_IN_FORMAT_YUV422SEMI;
468             break;
469         case MPP_FMT_YUV440SP:
470             pp_in_fmt = PP_IN_FORMAT_YUV440SEMI;
471             break;
472         case MPP_FMT_YUV411SP:
473             pp_in_fmt = PP_IN_FORMAT_YUV411_SEMI;
474             break;
475         case MPP_FMT_YUV444SP:
476             pp_in_fmt = PP_IN_FORMAT_YUV444_SEMI;
477             break;
478         default:
479             jpegd_dbg_hal("other output format %d\n", s->output_fmt);
480             break;
481         }
482 
483         pp_info->pp_enable = 1;
484         pp_info->pp_in_fmt = pp_in_fmt;
485 
486         fmt = ctx->output_fmt;
487 
488         if (MPP_FRAME_FMT_IS_LE(fmt)) {
489             fmt &= MPP_FRAME_FMT_MASK;
490         }
491 
492         switch (fmt) {
493         case MPP_FMT_RGB565 :
494         case MPP_FMT_BGR565 :
495         case MPP_FMT_RGB555 :
496         case MPP_FMT_BGR555 :
497         case MPP_FMT_RGB444 :
498         case MPP_FMT_BGR444 : {
499             pp_info->pp_out_fmt = PP_OUT_FORMAT_RGB565;
500             stride = s->hor_stride * 2;
501         } break;
502         case MPP_FMT_ARGB8888 :
503         case MPP_FMT_ABGR8888 :
504         case MPP_FMT_RGBA8888 :
505         case MPP_FMT_BGRA8888 : {
506             pp_info->pp_out_fmt = PP_OUT_FORMAT_ARGB;
507             stride = s->hor_stride * 4;
508         } break;
509         default : {
510             pp_info->pp_out_fmt = PP_OUT_FORMAT_YUV420INTERLAVE;
511         } break;
512         }
513 
514         jpegd_dbg_hal("Post Process! pp_in_fmt:%d, pp_out_fmt:%d",
515                       pp_in_fmt, pp_info->pp_out_fmt);
516 
517         /* check and switch to dev with pp */
518         if (ctx->dev_type == VPU_CLIENT_VDPU1)
519             dev_type = VPU_CLIENT_VDPU1_PP;
520         else if (ctx->dev_type == VPU_CLIENT_VDPU2)
521             dev_type = VPU_CLIENT_VDPU2_PP;
522     } else {
523         /* keep original output format */
524         ctx->output_fmt = s->output_fmt;
525         pp_info->pp_enable = 0;
526 
527         /* check and switch to dev without pp */
528         if (ctx->dev_type == VPU_CLIENT_VDPU1_PP)
529             dev_type = VPU_CLIENT_VDPU1;
530         else if (ctx->dev_type == VPU_CLIENT_VDPU2_PP)
531             dev_type = VPU_CLIENT_VDPU2;
532     }
533 
534     mpp_assert(ctx->dev);
535     if (ctx->dev_type != dev_type && ctx->dev) {
536         MppDev dev = NULL;
537 
538         ret = mpp_dev_init(&dev, dev_type);
539         if (ret) {
540             mpp_err_f("dev type %x -> %x switch failed ret %d\n",
541                       ctx->dev_type, dev_type, ret);
542             return ret;
543         }
544 
545         mpp_dev_deinit(ctx->dev);
546         ctx->dev = dev;
547         ctx->dev_type = dev_type;
548 
549         jpegd_dbg_hal("mpp_dev_init success.\n");
550     }
551 
552     mpp_buf_slot_get_prop(ctx->frame_slots, output,
553                           SLOT_FRAME_PTR, &frm);
554     mpp_frame_set_fmt(frm, ctx->output_fmt);
555     mpp_frame_set_hor_stride_pixel(frm, s->hor_stride);
556     /* update RGBX format byte stride and pixel stride */
557     if (stride)
558         mpp_frame_set_hor_stride(frm, stride);
559 
560     jpegd_dbg_func("exit\n");
561     return ret;
562 }
563 
jpeg_image_check_size(RK_U32 hor_stride,RK_U32 ver_stride)564 MPP_RET jpeg_image_check_size(RK_U32 hor_stride, RK_U32 ver_stride)
565 {
566     MPP_RET ret = MPP_OK;
567 
568     if (hor_stride > MAX_WIDTH || ver_stride > MAX_HEIGHT ||
569         hor_stride < MIN_WIDTH || ver_stride < MIN_HEIGHT ||
570         hor_stride * ver_stride > MAX_STREAM_LENGTH) {
571         mpp_err_f("unsupported resolution: %dx%d\n", hor_stride, ver_stride);
572         ret = MPP_NOK;
573     }
574 
575     return ret;
576 }
577