1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
5 * Rick Chang <rick.chang@mediatek.com>
8 #include <linux/kernel.h>
9 #include <linux/videodev2.h>
11 #include "mtk_jpeg_dec_parse.h"
19 struct mtk_jpeg_stream {
25 static int read_byte(struct mtk_jpeg_stream *stream)
27 if (stream->curr >= stream->size)
29 return stream->addr[stream->curr++];
32 static int read_word_be(struct mtk_jpeg_stream *stream, u32 *word)
37 byte = read_byte(stream);
41 byte = read_byte(stream);
44 *word = (u32)byte | temp;
49 static void read_skip(struct mtk_jpeg_stream *stream, long len)
57 static bool mtk_jpeg_do_parse(struct mtk_jpeg_dec_param *param, u8 *src_addr_va,
61 struct mtk_jpeg_stream stream;
63 stream.addr = src_addr_va;
64 stream.size = src_size;
71 byte = read_byte(&stream);
77 byte = read_byte(&stream);
88 if (read_word_be(&stream, &word))
92 if (read_byte(&stream) == -1)
95 if (read_word_be(&stream, &word))
99 if (read_word_be(&stream, &word))
103 param->comp_num = read_byte(&stream);
104 if (param->comp_num != 1 && param->comp_num != 3)
107 for (i = 0; i < param->comp_num; i++) {
108 param->comp_id[i] = read_byte(&stream);
109 if (param->comp_id[i] == -1)
113 byte = read_byte(&stream);
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)
124 notfound = !(i == param->comp_num);
126 case RST ... RST + 7:
132 if (read_word_be(&stream, &word))
134 length = (long)word - 2;
135 read_skip(&stream, length);
143 bool mtk_jpeg_parse(struct mtk_jpeg_dec_param *param, u8 *src_addr_va,
146 if (!mtk_jpeg_do_parse(param, src_addr_va, src_size))
148 if (mtk_jpeg_dec_fill_param(param))