2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
4 * Rick Chang <rick.chang@mediatek.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/videodev2.h>
19 #include "mtk_jpeg_parse.h"
27 struct mtk_jpeg_stream {
33 static int read_byte(struct mtk_jpeg_stream *stream)
35 if (stream->curr >= stream->size)
37 return stream->addr[stream->curr++];
40 static int read_word_be(struct mtk_jpeg_stream *stream, u32 *word)
45 byte = read_byte(stream);
49 byte = read_byte(stream);
52 *word = (u32)byte | temp;
57 static void read_skip(struct mtk_jpeg_stream *stream, long len)
65 static bool mtk_jpeg_do_parse(struct mtk_jpeg_dec_param *param, u8 *src_addr_va,
69 struct mtk_jpeg_stream stream;
71 stream.addr = src_addr_va;
72 stream.size = src_size;
79 byte = read_byte(&stream);
85 byte = read_byte(&stream);
96 if (read_word_be(&stream, &word))
100 if (read_byte(&stream) == -1)
103 if (read_word_be(&stream, &word))
107 if (read_word_be(&stream, &word))
111 param->comp_num = read_byte(&stream);
112 if (param->comp_num != 1 && param->comp_num != 3)
115 for (i = 0; i < param->comp_num; i++) {
116 param->comp_id[i] = read_byte(&stream);
117 if (param->comp_id[i] == -1)
121 byte = read_byte(&stream);
124 param->sampling_w[i] = (byte >> 4) & 0x0F;
125 param->sampling_h[i] = byte & 0x0F;
127 param->qtbl_num[i] = read_byte(&stream);
128 if (param->qtbl_num[i] == -1)
132 notfound = !(i == param->comp_num);
134 case RST ... RST + 7:
140 if (read_word_be(&stream, &word))
142 length = (long)word - 2;
143 read_skip(&stream, length);
151 bool mtk_jpeg_parse(struct mtk_jpeg_dec_param *param, u8 *src_addr_va,
154 if (!mtk_jpeg_do_parse(param, src_addr_va, src_size))
156 if (mtk_jpeg_dec_fill_param(param))