Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / media / platform / mtk-jpeg / mtk_jpeg_dec_parse.c
blobb95c45791c29edc5a80caba27e2c84ea00f718a3
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
5 * Rick Chang <rick.chang@mediatek.com>
6 */
8 #include <linux/kernel.h>
9 #include <linux/videodev2.h>
11 #include "mtk_jpeg_dec_parse.h"
13 #define TEM 0x01
14 #define SOF0 0xc0
15 #define RST 0xd0
16 #define SOI 0xd8
17 #define EOI 0xd9
19 struct mtk_jpeg_stream {
20 u8 *addr;
21 u32 size;
22 u32 curr;
25 static int read_byte(struct mtk_jpeg_stream *stream)
27 if (stream->curr >= stream->size)
28 return -1;
29 return stream->addr[stream->curr++];
32 static int read_word_be(struct mtk_jpeg_stream *stream, u32 *word)
34 u32 temp;
35 int byte;
37 byte = read_byte(stream);
38 if (byte == -1)
39 return -1;
40 temp = byte << 8;
41 byte = read_byte(stream);
42 if (byte == -1)
43 return -1;
44 *word = (u32)byte | temp;
46 return 0;
49 static void read_skip(struct mtk_jpeg_stream *stream, long len)
51 if (len <= 0)
52 return;
53 while (len--)
54 read_byte(stream);
57 static bool mtk_jpeg_do_parse(struct mtk_jpeg_dec_param *param, u8 *src_addr_va,
58 u32 src_size)
60 bool notfound = true;
61 struct mtk_jpeg_stream stream;
63 stream.addr = src_addr_va;
64 stream.size = src_size;
65 stream.curr = 0;
67 while (notfound) {
68 int i, length, byte;
69 u32 word;
71 byte = read_byte(&stream);
72 if (byte == -1)
73 return false;
74 if (byte != 0xff)
75 continue;
77 byte = read_byte(&stream);
78 while (byte == 0xff);
79 if (byte == -1)
80 return false;
81 if (byte == 0)
82 continue;
84 length = 0;
85 switch (byte) {
86 case SOF0:
87 /* length */
88 if (read_word_be(&stream, &word))
89 break;
91 /* precision */
92 if (read_byte(&stream) == -1)
93 break;
95 if (read_word_be(&stream, &word))
96 break;
97 param->pic_h = word;
99 if (read_word_be(&stream, &word))
100 break;
101 param->pic_w = word;
103 param->comp_num = read_byte(&stream);
104 if (param->comp_num != 1 && param->comp_num != 3)
105 break;
107 for (i = 0; i < param->comp_num; i++) {
108 param->comp_id[i] = read_byte(&stream);
109 if (param->comp_id[i] == -1)
110 break;
112 /* sampling */
113 byte = read_byte(&stream);
114 if (byte == -1)
115 break;
116 param->sampling_w[i] = (byte >> 4) & 0x0F;
117 param->sampling_h[i] = byte & 0x0F;
119 param->qtbl_num[i] = read_byte(&stream);
120 if (param->qtbl_num[i] == -1)
121 break;
124 notfound = !(i == param->comp_num);
125 break;
126 case RST ... RST + 7:
127 case SOI:
128 case EOI:
129 case TEM:
130 break;
131 default:
132 if (read_word_be(&stream, &word))
133 break;
134 length = (long)word - 2;
135 read_skip(&stream, length);
136 break;
140 return !notfound;
143 bool mtk_jpeg_parse(struct mtk_jpeg_dec_param *param, u8 *src_addr_va,
144 u32 src_size)
146 if (!mtk_jpeg_do_parse(param, src_addr_va, src_size))
147 return false;
148 if (mtk_jpeg_dec_fill_param(param))
149 return false;
151 return true;