1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Coda multi-standard codec IP - JPEG support functions
5 * Copyright (C) 2014 Philipp Zabel, Pengutronix
8 #include <asm/unaligned.h>
9 #include <linux/irqreturn.h>
10 #include <linux/kernel.h>
11 #include <linux/ktime.h>
12 #include <linux/slab.h>
13 #include <linux/swab.h>
14 #include <linux/videodev2.h>
16 #include <media/v4l2-common.h>
17 #include <media/v4l2-fh.h>
18 #include <media/v4l2-jpeg.h>
19 #include <media/v4l2-mem2mem.h>
20 #include <media/videobuf2-core.h>
21 #include <media/videobuf2-dma-contig.h>
26 #define SOI_MARKER 0xffd8
27 #define APP9_MARKER 0xffe9
28 #define DRI_MARKER 0xffdd
29 #define DQT_MARKER 0xffdb
30 #define DHT_MARKER 0xffc4
31 #define SOF_MARKER 0xffc0
32 #define SOS_MARKER 0xffda
33 #define EOI_MARKER 0xffd9
36 CODA9_JPEG_FORMAT_420,
37 CODA9_JPEG_FORMAT_422,
38 CODA9_JPEG_FORMAT_224,
39 CODA9_JPEG_FORMAT_444,
40 CODA9_JPEG_FORMAT_400,
43 struct coda_huff_tab {
45 u8 chroma_dc[16 + 12];
47 u8 chroma_ac[16 + 162];
49 /* DC Luma, DC Chroma, AC Luma, AC Chroma */
55 #define CODA9_JPEG_ENC_HUFF_DATA_SIZE (256 + 256 + 16 + 16)
58 * Typical Huffman tables for 8-bit precision luminance and
59 * chrominance from JPEG ITU-T.81 (ISO/IEC 10918-1) Annex K.3
62 static const unsigned char luma_dc[16 + 12] = {
64 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
65 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
68 0x08, 0x09, 0x0a, 0x0b,
71 static const unsigned char chroma_dc[16 + 12] = {
73 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
74 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
77 0x08, 0x09, 0x0a, 0x0b,
80 static const unsigned char luma_ac[16 + 162 + 2] = {
82 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
83 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
85 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
86 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
87 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
88 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
89 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
90 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
91 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
92 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
93 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
94 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
95 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
96 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
97 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
98 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
99 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
100 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
101 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
102 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
103 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
104 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
105 0xf9, 0xfa, /* padded to 32-bit */
108 static const unsigned char chroma_ac[16 + 162 + 2] = {
110 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
111 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
113 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
114 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
115 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
116 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
117 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
118 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
119 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
120 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
121 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
122 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
123 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
124 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
125 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
126 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
127 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
128 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
129 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
130 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
131 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
132 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
133 0xf9, 0xfa, /* padded to 32-bit */
137 * Quantization tables for luminance and chrominance components in
138 * zig-zag scan order from the Freescale i.MX VPU libraries
141 static unsigned char luma_q[64] = {
142 0x06, 0x04, 0x04, 0x04, 0x05, 0x04, 0x06, 0x05,
143 0x05, 0x06, 0x09, 0x06, 0x05, 0x06, 0x09, 0x0b,
144 0x08, 0x06, 0x06, 0x08, 0x0b, 0x0c, 0x0a, 0x0a,
145 0x0b, 0x0a, 0x0a, 0x0c, 0x10, 0x0c, 0x0c, 0x0c,
146 0x0c, 0x0c, 0x0c, 0x10, 0x0c, 0x0c, 0x0c, 0x0c,
147 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
148 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
149 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
152 static unsigned char chroma_q[64] = {
153 0x07, 0x07, 0x07, 0x0d, 0x0c, 0x0d, 0x18, 0x10,
154 0x10, 0x18, 0x14, 0x0e, 0x0e, 0x0e, 0x14, 0x14,
155 0x0e, 0x0e, 0x0e, 0x0e, 0x14, 0x11, 0x0c, 0x0c,
156 0x0c, 0x0c, 0x0c, 0x11, 0x11, 0x0c, 0x0c, 0x0c,
157 0x0c, 0x0c, 0x0c, 0x11, 0x0c, 0x0c, 0x0c, 0x0c,
158 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
159 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
160 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
163 static const unsigned char width_align[] = {
164 [CODA9_JPEG_FORMAT_420] = 16,
165 [CODA9_JPEG_FORMAT_422] = 16,
166 [CODA9_JPEG_FORMAT_224] = 8,
167 [CODA9_JPEG_FORMAT_444] = 8,
168 [CODA9_JPEG_FORMAT_400] = 8,
171 static const unsigned char height_align[] = {
172 [CODA9_JPEG_FORMAT_420] = 16,
173 [CODA9_JPEG_FORMAT_422] = 8,
174 [CODA9_JPEG_FORMAT_224] = 16,
175 [CODA9_JPEG_FORMAT_444] = 8,
176 [CODA9_JPEG_FORMAT_400] = 8,
179 static int coda9_jpeg_chroma_format(u32 pixfmt)
182 case V4L2_PIX_FMT_YUV420:
183 case V4L2_PIX_FMT_NV12:
184 return CODA9_JPEG_FORMAT_420;
185 case V4L2_PIX_FMT_YUV422P:
186 return CODA9_JPEG_FORMAT_422;
187 case V4L2_PIX_FMT_YUV444:
188 return CODA9_JPEG_FORMAT_444;
189 case V4L2_PIX_FMT_GREY:
190 return CODA9_JPEG_FORMAT_400;
195 struct coda_memcpy_desc {
201 static void coda_memcpy_parabuf(void *parabuf,
202 const struct coda_memcpy_desc *desc)
204 u32 *dst = parabuf + desc->offset;
205 const u32 *src = desc->src;
206 int len = desc->len / 4;
209 for (i = 0; i < len; i += 2) {
210 dst[i + 1] = swab32(src[i]);
211 dst[i] = swab32(src[i + 1]);
215 int coda_jpeg_write_tables(struct coda_ctx *ctx)
218 static const struct coda_memcpy_desc huff[8] = {
219 { 0, luma_dc, sizeof(luma_dc) },
220 { 32, luma_ac, sizeof(luma_ac) },
221 { 216, chroma_dc, sizeof(chroma_dc) },
222 { 248, chroma_ac, sizeof(chroma_ac) },
224 struct coda_memcpy_desc qmat[3] = {
225 { 512, ctx->params.jpeg_qmat_tab[0], 64 },
226 { 576, ctx->params.jpeg_qmat_tab[1], 64 },
227 { 640, ctx->params.jpeg_qmat_tab[1], 64 },
230 /* Write huffman tables to parameter memory */
231 for (i = 0; i < ARRAY_SIZE(huff); i++)
232 coda_memcpy_parabuf(ctx->parabuf.vaddr, huff + i);
234 /* Write Q-matrix to parameter memory */
235 for (i = 0; i < ARRAY_SIZE(qmat); i++)
236 coda_memcpy_parabuf(ctx->parabuf.vaddr, qmat + i);
241 bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb)
243 void *vaddr = vb2_plane_vaddr(vb, 0);
247 soi = be16_to_cpup((__be16 *)vaddr);
248 if (soi != SOI_MARKER)
251 len = vb2_get_plane_payload(vb, 0);
253 for (i = 0; i < 32; i++) {
254 eoi = be16_to_cpup((__be16 *)(vaddr - i));
255 if (eoi == EOI_MARKER) {
257 vb2_set_plane_payload(vb, 0, len - i);
265 static int coda9_jpeg_gen_dec_huff_tab(struct coda_ctx *ctx, int tab_num);
267 int coda_jpeg_decode_header(struct coda_ctx *ctx, struct vb2_buffer *vb)
269 struct coda_dev *dev = ctx->dev;
270 u8 *buf = vb2_plane_vaddr(vb, 0);
271 size_t len = vb2_get_plane_payload(vb, 0);
272 struct v4l2_jpeg_scan_header scan_header;
273 struct v4l2_jpeg_reference quantization_tables[4] = { };
274 struct v4l2_jpeg_reference huffman_tables[4] = { };
275 struct v4l2_jpeg_header header = {
276 .scan = &scan_header,
277 .quantization_tables = quantization_tables,
278 .huffman_tables = huffman_tables,
280 struct coda_q_data *q_data_src;
281 struct coda_huff_tab *huff_tab;
284 ret = v4l2_jpeg_parse_header(buf, len, &header);
286 v4l2_err(&dev->v4l2_dev, "failed to parse JPEG header: %pe\n",
291 ctx->params.jpeg_restart_interval = header.restart_interval;
293 /* check frame header */
294 if (header.frame.height > ctx->codec->max_h ||
295 header.frame.width > ctx->codec->max_w) {
296 v4l2_err(&dev->v4l2_dev, "invalid dimensions: %dx%d\n",
297 header.frame.width, header.frame.height);
301 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
302 if (header.frame.height != q_data_src->height ||
303 header.frame.width != q_data_src->width) {
304 v4l2_err(&dev->v4l2_dev,
305 "dimensions don't match format: %dx%d\n",
306 header.frame.width, header.frame.height);
310 if (header.frame.num_components != 3) {
311 v4l2_err(&dev->v4l2_dev,
312 "unsupported number of components: %d\n",
313 header.frame.num_components);
317 /* install quantization tables */
318 if (quantization_tables[3].start) {
319 v4l2_err(&dev->v4l2_dev,
320 "only 3 quantization tables supported\n");
323 for (i = 0; i < 3; i++) {
324 if (!quantization_tables[i].start)
326 if (quantization_tables[i].length != 64) {
327 v4l2_err(&dev->v4l2_dev,
328 "only 8-bit quantization tables supported\n");
331 if (!ctx->params.jpeg_qmat_tab[i]) {
332 ctx->params.jpeg_qmat_tab[i] = kmalloc(64, GFP_KERNEL);
333 if (!ctx->params.jpeg_qmat_tab[i])
336 memcpy(ctx->params.jpeg_qmat_tab[i],
337 quantization_tables[i].start, 64);
340 /* install Huffman tables */
341 for (i = 0; i < 4; i++) {
342 if (!huffman_tables[i].start) {
343 v4l2_err(&dev->v4l2_dev, "missing Huffman table\n");
346 /* AC tables should be between 17 -> 178, DC between 17 -> 28 */
347 if (huffman_tables[i].length < 17 ||
348 huffman_tables[i].length > 178 ||
349 ((i & 2) == 0 && huffman_tables[i].length > 28)) {
350 v4l2_err(&dev->v4l2_dev,
351 "invalid Huffman table %d length: %zu\n",
352 i, huffman_tables[i].length);
356 huff_tab = ctx->params.jpeg_huff_tab;
358 huff_tab = kzalloc(sizeof(struct coda_huff_tab), GFP_KERNEL);
361 ctx->params.jpeg_huff_tab = huff_tab;
364 memset(huff_tab, 0, sizeof(*huff_tab));
365 memcpy(huff_tab->luma_dc, huffman_tables[0].start, huffman_tables[0].length);
366 memcpy(huff_tab->chroma_dc, huffman_tables[1].start, huffman_tables[1].length);
367 memcpy(huff_tab->luma_ac, huffman_tables[2].start, huffman_tables[2].length);
368 memcpy(huff_tab->chroma_ac, huffman_tables[3].start, huffman_tables[3].length);
370 /* check scan header */
371 for (i = 0; i < scan_header.num_components; i++) {
372 struct v4l2_jpeg_scan_component_spec *scan_component;
374 scan_component = &scan_header.component[i];
375 for (j = 0; j < header.frame.num_components; j++) {
376 if (header.frame.component[j].component_identifier ==
377 scan_component->component_selector)
380 if (j == header.frame.num_components)
383 ctx->params.jpeg_huff_dc_index[j] =
384 scan_component->dc_entropy_coding_table_selector;
385 ctx->params.jpeg_huff_ac_index[j] =
386 scan_component->ac_entropy_coding_table_selector;
389 /* Generate Huffman table information */
390 for (i = 0; i < 4; i++)
391 coda9_jpeg_gen_dec_huff_tab(ctx, i);
393 /* start of entropy coded segment */
394 ctx->jpeg_ecs_offset = header.ecs_offset;
396 switch (header.frame.subsampling) {
397 case V4L2_JPEG_CHROMA_SUBSAMPLING_420:
398 case V4L2_JPEG_CHROMA_SUBSAMPLING_422:
399 ctx->params.jpeg_chroma_subsampling = header.frame.subsampling;
402 v4l2_err(&dev->v4l2_dev, "chroma subsampling not supported: %d",
403 header.frame.subsampling);
410 static inline void coda9_jpeg_write_huff_values(struct coda_dev *dev, u8 *bits,
413 s8 *values = (s8 *)(bits + 16);
416 for (huff_length = 0, i = 0; i < 16; i++)
417 huff_length += bits[i];
418 for (i = huff_length; i < num_values; i++)
420 for (i = 0; i < num_values; i++)
421 coda_write(dev, (s32)values[i], CODA9_REG_JPEG_HUFF_DATA);
424 static int coda9_jpeg_dec_huff_setup(struct coda_ctx *ctx)
426 struct coda_huff_tab *huff_tab = ctx->params.jpeg_huff_tab;
427 struct coda_dev *dev = ctx->dev;
428 s16 *huff_min = huff_tab->min;
429 s16 *huff_max = huff_tab->max;
430 s8 *huff_ptr = huff_tab->ptr;
434 coda_write(dev, 0x003, CODA9_REG_JPEG_HUFF_CTRL);
435 coda_write(dev, 0x000, CODA9_REG_JPEG_HUFF_ADDR);
436 for (i = 0; i < 4 * 16; i++)
437 coda_write(dev, (s32)huff_min[i], CODA9_REG_JPEG_HUFF_DATA);
440 coda_write(dev, 0x403, CODA9_REG_JPEG_HUFF_CTRL);
441 coda_write(dev, 0x440, CODA9_REG_JPEG_HUFF_ADDR);
442 for (i = 0; i < 4 * 16; i++)
443 coda_write(dev, (s32)huff_max[i], CODA9_REG_JPEG_HUFF_DATA);
446 coda_write(dev, 0x803, CODA9_REG_JPEG_HUFF_CTRL);
447 coda_write(dev, 0x880, CODA9_REG_JPEG_HUFF_ADDR);
448 for (i = 0; i < 4 * 16; i++)
449 coda_write(dev, (s32)huff_ptr[i], CODA9_REG_JPEG_HUFF_DATA);
451 /* VAL Tables: DC Luma, DC Chroma, AC Luma, AC Chroma */
452 coda_write(dev, 0xc03, CODA9_REG_JPEG_HUFF_CTRL);
453 coda9_jpeg_write_huff_values(dev, huff_tab->luma_dc, 12);
454 coda9_jpeg_write_huff_values(dev, huff_tab->chroma_dc, 12);
455 coda9_jpeg_write_huff_values(dev, huff_tab->luma_ac, 162);
456 coda9_jpeg_write_huff_values(dev, huff_tab->chroma_ac, 162);
457 coda_write(dev, 0x000, CODA9_REG_JPEG_HUFF_CTRL);
461 static inline void coda9_jpeg_write_qmat_tab(struct coda_dev *dev,
466 coda_write(dev, index | 0x3, CODA9_REG_JPEG_QMAT_CTRL);
467 for (i = 0; i < 64; i++)
468 coda_write(dev, qmat[i], CODA9_REG_JPEG_QMAT_DATA);
469 coda_write(dev, 0, CODA9_REG_JPEG_QMAT_CTRL);
472 static void coda9_jpeg_qmat_setup(struct coda_ctx *ctx)
474 struct coda_dev *dev = ctx->dev;
475 int *qmat_index = ctx->params.jpeg_qmat_index;
476 u8 **qmat_tab = ctx->params.jpeg_qmat_tab;
478 coda9_jpeg_write_qmat_tab(dev, qmat_tab[qmat_index[0]], 0x00);
479 coda9_jpeg_write_qmat_tab(dev, qmat_tab[qmat_index[1]], 0x40);
480 coda9_jpeg_write_qmat_tab(dev, qmat_tab[qmat_index[2]], 0x80);
483 static void coda9_jpeg_dec_bbc_gbu_setup(struct coda_ctx *ctx,
484 struct vb2_buffer *buf, u32 ecs_offset)
486 struct coda_dev *dev = ctx->dev;
487 int page_ptr, word_ptr, bit_ptr;
488 u32 bbc_base_addr, end_addr;
492 bbc_base_addr = vb2_dma_contig_plane_dma_addr(buf, 0);
493 end_addr = bbc_base_addr + vb2_get_plane_payload(buf, 0);
495 page_ptr = ecs_offset / 256;
496 word_ptr = (ecs_offset % 256) / 4;
499 bit_ptr = (ecs_offset % 4) * 8;
504 coda_write(dev, end_addr, CODA9_REG_JPEG_BBC_WR_PTR);
505 coda_write(dev, bbc_base_addr, CODA9_REG_JPEG_BBC_BAS_ADDR);
507 /* Leave 3 256-byte page margin to avoid a BBC interrupt */
508 coda_write(dev, end_addr + 256 * 3 + 256, CODA9_REG_JPEG_BBC_END_ADDR);
509 val = DIV_ROUND_UP(vb2_plane_size(buf, 0), 256) + 3;
510 coda_write(dev, BIT(31) | val, CODA9_REG_JPEG_BBC_STRM_CTRL);
512 bbc_cur_pos = page_ptr;
513 coda_write(dev, bbc_cur_pos, CODA9_REG_JPEG_BBC_CUR_POS);
514 coda_write(dev, bbc_base_addr + (bbc_cur_pos << 8),
515 CODA9_REG_JPEG_BBC_EXT_ADDR);
516 coda_write(dev, (bbc_cur_pos & 1) << 6, CODA9_REG_JPEG_BBC_INT_ADDR);
517 coda_write(dev, 64, CODA9_REG_JPEG_BBC_DATA_CNT);
518 coda_write(dev, 0, CODA9_REG_JPEG_BBC_COMMAND);
520 ret = coda_read(dev, CODA9_REG_JPEG_BBC_BUSY);
524 coda_write(dev, bbc_cur_pos, CODA9_REG_JPEG_BBC_CUR_POS);
525 coda_write(dev, bbc_base_addr + (bbc_cur_pos << 8),
526 CODA9_REG_JPEG_BBC_EXT_ADDR);
527 coda_write(dev, (bbc_cur_pos & 1) << 6, CODA9_REG_JPEG_BBC_INT_ADDR);
528 coda_write(dev, 64, CODA9_REG_JPEG_BBC_DATA_CNT);
529 coda_write(dev, 0, CODA9_REG_JPEG_BBC_COMMAND);
531 ret = coda_read(dev, CODA9_REG_JPEG_BBC_BUSY);
535 coda_write(dev, bbc_cur_pos, CODA9_REG_JPEG_BBC_CUR_POS);
536 coda_write(dev, 1, CODA9_REG_JPEG_BBC_CTRL);
538 coda_write(dev, 0, CODA9_REG_JPEG_GBU_TT_CNT);
539 coda_write(dev, word_ptr, CODA9_REG_JPEG_GBU_WD_PTR);
540 coda_write(dev, 0, CODA9_REG_JPEG_GBU_BBSR);
541 coda_write(dev, 127, CODA9_REG_JPEG_GBU_BBER);
543 coda_write(dev, 0, CODA9_REG_JPEG_GBU_BBIR);
544 coda_write(dev, 0, CODA9_REG_JPEG_GBU_BBHR);
546 coda_write(dev, 64, CODA9_REG_JPEG_GBU_BBIR);
547 coda_write(dev, 64, CODA9_REG_JPEG_GBU_BBHR);
549 coda_write(dev, 4, CODA9_REG_JPEG_GBU_CTRL);
550 coda_write(dev, bit_ptr, CODA9_REG_JPEG_GBU_FF_RPTR);
551 coda_write(dev, 3, CODA9_REG_JPEG_GBU_CTRL);
554 static const int bus_req_num[] = {
555 [CODA9_JPEG_FORMAT_420] = 2,
556 [CODA9_JPEG_FORMAT_422] = 3,
557 [CODA9_JPEG_FORMAT_224] = 3,
558 [CODA9_JPEG_FORMAT_444] = 4,
559 [CODA9_JPEG_FORMAT_400] = 4,
562 #define MCU_INFO(mcu_block_num, comp_num, comp0_info, comp1_info, comp2_info) \
563 (((mcu_block_num) << CODA9_JPEG_MCU_BLOCK_NUM_OFFSET) | \
564 ((comp_num) << CODA9_JPEG_COMP_NUM_OFFSET) | \
565 ((comp0_info) << CODA9_JPEG_COMP0_INFO_OFFSET) | \
566 ((comp1_info) << CODA9_JPEG_COMP1_INFO_OFFSET) | \
567 ((comp2_info) << CODA9_JPEG_COMP2_INFO_OFFSET))
569 static const u32 mcu_info[] = {
570 [CODA9_JPEG_FORMAT_420] = MCU_INFO(6, 3, 10, 5, 5),
571 [CODA9_JPEG_FORMAT_422] = MCU_INFO(4, 3, 9, 5, 5),
572 [CODA9_JPEG_FORMAT_224] = MCU_INFO(4, 3, 6, 5, 5),
573 [CODA9_JPEG_FORMAT_444] = MCU_INFO(3, 3, 5, 5, 5),
574 [CODA9_JPEG_FORMAT_400] = MCU_INFO(1, 1, 5, 0, 0),
578 * Convert Huffman table specifcations to tables of codes and code lengths.
579 * For reference, see JPEG ITU-T.81 (ISO/IEC 10918-1) [1]
581 * [1] https://www.w3.org/Graphics/JPEG/itu-t81.pdf
583 static int coda9_jpeg_gen_enc_huff_tab(struct coda_ctx *ctx, int tab_num,
584 int *ehufsi, int *ehufco)
586 int i, j, k, lastk, si, code, maxsymbol;
587 const u8 *bits, *huffval;
592 static const unsigned char *huff_tabs[4] = {
593 luma_dc, luma_ac, chroma_dc, chroma_ac,
597 huff = kzalloc(sizeof(*huff), GFP_KERNEL);
601 bits = huff_tabs[tab_num];
602 huffval = huff_tabs[tab_num] + 16;
604 maxsymbol = tab_num & 1 ? 256 : 16;
606 /* Figure C.1 - Generation of table of Huffman code sizes */
608 for (i = 1; i <= 16; i++) {
610 if (k + j > maxsymbol)
617 /* Figure C.2 - Generation of table of Huffman codes */
622 while (huff->size[k] == si) {
623 huff->code[k++] = code;
626 if (code >= (1 << si))
632 /* Figure C.3 - Ordering procedure for encoding procedure code tables */
633 for (k = 0; k < lastk; k++) {
635 if (i >= maxsymbol || ehufsi[i])
637 ehufco[i] = huff->code[k];
638 ehufsi[i] = huff->size[k];
647 #define DC_TABLE_INDEX0 0
648 #define AC_TABLE_INDEX0 1
649 #define DC_TABLE_INDEX1 2
650 #define AC_TABLE_INDEX1 3
652 static u8 *coda9_jpeg_get_huff_bits(struct coda_ctx *ctx, int tab_num)
654 struct coda_huff_tab *huff_tab = ctx->params.jpeg_huff_tab;
660 case DC_TABLE_INDEX0: return huff_tab->luma_dc;
661 case AC_TABLE_INDEX0: return huff_tab->luma_ac;
662 case DC_TABLE_INDEX1: return huff_tab->chroma_dc;
663 case AC_TABLE_INDEX1: return huff_tab->chroma_ac;
669 static int coda9_jpeg_gen_dec_huff_tab(struct coda_ctx *ctx, int tab_num)
671 int ptr_cnt = 0, huff_code = 0, zero_flag = 0, data_flag = 0;
679 huff_bits = coda9_jpeg_get_huff_bits(ctx, tab_num);
683 /* DC/AC Luma, DC/AC Chroma -> DC Luma/Chroma, AC Luma/Chroma */
684 ofs = ((tab_num & 1) << 1) | ((tab_num >> 1) & 1);
687 huff_ptr = ctx->params.jpeg_huff_tab->ptr + ofs;
688 huff_max = ctx->params.jpeg_huff_tab->max + ofs;
689 huff_min = ctx->params.jpeg_huff_tab->min + ofs;
691 for (i = 0; i < 16; i++) {
693 huff_ptr[i] = ptr_cnt;
694 ptr_cnt += huff_bits[i];
695 huff_min[i] = huff_code;
696 huff_max[i] = huff_code + (huff_bits[i] - 1);
706 if (data_flag == 1) {
710 huff_code = (huff_max[i] + 1) << 1;
717 static int coda9_jpeg_load_huff_tab(struct coda_ctx *ctx)
727 huff = kzalloc(sizeof(*huff), GFP_KERNEL);
731 /* Generate all four (luma/chroma DC/AC) code/size lookup tables */
732 for (i = 0; i < 4; i++) {
733 ret = coda9_jpeg_gen_enc_huff_tab(ctx, i, huff->size[i],
739 if (!ctx->params.jpeg_huff_data) {
740 ctx->params.jpeg_huff_data =
741 kzalloc(sizeof(u32) * CODA9_JPEG_ENC_HUFF_DATA_SIZE,
743 if (!ctx->params.jpeg_huff_data) {
748 huff_data = ctx->params.jpeg_huff_data;
750 for (j = 0; j < 4; j++) {
751 /* Store Huffman lookup tables in AC0, AC1, DC0, DC1 order */
752 int t = (j == 0) ? AC_TABLE_INDEX0 :
753 (j == 1) ? AC_TABLE_INDEX1 :
754 (j == 2) ? DC_TABLE_INDEX0 :
756 /* DC tables only have 16 entries */
757 int len = (j < 2) ? 256 : 16;
759 for (i = 0; i < len; i++) {
760 if (huff->size[t][i] == 0 && huff->code[t][i] == 0)
764 ((huff->size[t][i] - 1) << 16) |
775 static void coda9_jpeg_write_huff_tab(struct coda_ctx *ctx)
777 struct coda_dev *dev = ctx->dev;
778 u32 *huff_data = ctx->params.jpeg_huff_data;
781 /* Write Huffman size/code lookup tables in AC0, AC1, DC0, DC1 order */
782 coda_write(dev, 0x3, CODA9_REG_JPEG_HUFF_CTRL);
783 for (i = 0; i < CODA9_JPEG_ENC_HUFF_DATA_SIZE; i++)
784 coda_write(dev, *(huff_data++), CODA9_REG_JPEG_HUFF_DATA);
785 coda_write(dev, 0x0, CODA9_REG_JPEG_HUFF_CTRL);
788 static inline void coda9_jpeg_write_qmat_quotients(struct coda_dev *dev,
793 coda_write(dev, index | 0x3, CODA9_REG_JPEG_QMAT_CTRL);
794 for (i = 0; i < 64; i++)
795 coda_write(dev, 0x80000 / qmat[i], CODA9_REG_JPEG_QMAT_DATA);
796 coda_write(dev, index, CODA9_REG_JPEG_QMAT_CTRL);
799 static void coda9_jpeg_load_qmat_tab(struct coda_ctx *ctx)
801 struct coda_dev *dev = ctx->dev;
805 luma_tab = ctx->params.jpeg_qmat_tab[0];
809 chroma_tab = ctx->params.jpeg_qmat_tab[1];
811 chroma_tab = chroma_q;
813 coda9_jpeg_write_qmat_quotients(dev, luma_tab, 0x00);
814 coda9_jpeg_write_qmat_quotients(dev, chroma_tab, 0x40);
815 coda9_jpeg_write_qmat_quotients(dev, chroma_tab, 0x80);
818 struct coda_jpeg_stream {
823 static inline int coda_jpeg_put_byte(u8 byte, struct coda_jpeg_stream *stream)
825 if (stream->curr >= stream->end)
828 *stream->curr++ = byte;
833 static inline int coda_jpeg_put_word(u16 word, struct coda_jpeg_stream *stream)
835 if (stream->curr + sizeof(__be16) > stream->end)
838 put_unaligned_be16(word, stream->curr);
839 stream->curr += sizeof(__be16);
844 static int coda_jpeg_put_table(u16 marker, u8 index, const u8 *table,
845 size_t len, struct coda_jpeg_stream *stream)
849 ret = coda_jpeg_put_word(marker, stream);
852 ret = coda_jpeg_put_word(3 + len, stream);
855 ret = coda_jpeg_put_byte(index, stream);
856 for (i = 0; i < len && ret == 0; i++)
857 ret = coda_jpeg_put_byte(table[i], stream);
862 static int coda_jpeg_define_quantization_table(struct coda_ctx *ctx, u8 index,
863 struct coda_jpeg_stream *stream)
865 return coda_jpeg_put_table(DQT_MARKER, index,
866 ctx->params.jpeg_qmat_tab[index], 64,
870 static int coda_jpeg_define_huffman_table(u8 index, const u8 *table, size_t len,
871 struct coda_jpeg_stream *stream)
873 return coda_jpeg_put_table(DHT_MARKER, index, table, len, stream);
876 static int coda9_jpeg_encode_header(struct coda_ctx *ctx, int len, u8 *buf)
878 struct coda_jpeg_stream stream = { buf, buf + len };
879 struct coda_q_data *q_data_src;
880 int chroma_format, comp_num;
883 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
884 chroma_format = coda9_jpeg_chroma_format(q_data_src->fourcc);
885 if (chroma_format < 0)
889 ret = coda_jpeg_put_word(SOI_MARKER, &stream);
893 /* Define Restart Interval */
894 if (ctx->params.jpeg_restart_interval) {
895 ret = coda_jpeg_put_word(DRI_MARKER, &stream);
898 ret = coda_jpeg_put_word(4, &stream);
901 ret = coda_jpeg_put_word(ctx->params.jpeg_restart_interval,
907 /* Define Quantization Tables */
908 ret = coda_jpeg_define_quantization_table(ctx, 0x00, &stream);
911 if (chroma_format != CODA9_JPEG_FORMAT_400) {
912 ret = coda_jpeg_define_quantization_table(ctx, 0x01, &stream);
917 /* Define Huffman Tables */
918 ret = coda_jpeg_define_huffman_table(0x00, luma_dc, 16 + 12, &stream);
921 ret = coda_jpeg_define_huffman_table(0x10, luma_ac, 16 + 162, &stream);
924 if (chroma_format != CODA9_JPEG_FORMAT_400) {
925 ret = coda_jpeg_define_huffman_table(0x01, chroma_dc, 16 + 12,
929 ret = coda_jpeg_define_huffman_table(0x11, chroma_ac, 16 + 162,
936 ret = coda_jpeg_put_word(SOF_MARKER, &stream);
939 comp_num = (chroma_format == CODA9_JPEG_FORMAT_400) ? 1 : 3;
940 ret = coda_jpeg_put_word(8 + comp_num * 3, &stream);
943 ret = coda_jpeg_put_byte(0x08, &stream);
946 ret = coda_jpeg_put_word(q_data_src->height, &stream);
949 ret = coda_jpeg_put_word(q_data_src->width, &stream);
952 ret = coda_jpeg_put_byte(comp_num, &stream);
955 for (i = 0; i < comp_num; i++) {
956 static unsigned char subsampling[5][3] = {
957 [CODA9_JPEG_FORMAT_420] = { 0x22, 0x11, 0x11 },
958 [CODA9_JPEG_FORMAT_422] = { 0x21, 0x11, 0x11 },
959 [CODA9_JPEG_FORMAT_224] = { 0x12, 0x11, 0x11 },
960 [CODA9_JPEG_FORMAT_444] = { 0x11, 0x11, 0x11 },
961 [CODA9_JPEG_FORMAT_400] = { 0x11 },
964 /* Component identifier, matches SOS */
965 ret = coda_jpeg_put_byte(i + 1, &stream);
968 ret = coda_jpeg_put_byte(subsampling[chroma_format][i],
972 /* Chroma table index */
973 ret = coda_jpeg_put_byte((i == 0) ? 0 : 1, &stream);
978 /* Pad to multiple of 8 bytes */
979 pad = (stream.curr - buf) % 8;
983 ret = coda_jpeg_put_byte(0x00, &stream);
989 return stream.curr - buf;
993 * Scale quantization table using nonlinear scaling factor
994 * u8 qtab[64], scale [50,190]
996 static void coda_scale_quant_table(u8 *q_tab, int scale)
1001 for (i = 0; i < 64; i++) {
1002 temp = DIV_ROUND_CLOSEST((unsigned int)q_tab[i] * scale, 100);
1007 q_tab[i] = (unsigned char)temp;
1011 void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality)
1015 ctx->params.jpeg_quality = quality;
1017 /* Clip quality setting to [5,100] interval */
1024 * Non-linear scaling factor:
1025 * [5,50] -> [1000..100], [51,100] -> [98..0]
1028 scale = 5000 / quality;
1030 scale = 200 - 2 * quality;
1032 if (ctx->params.jpeg_qmat_tab[0]) {
1033 memcpy(ctx->params.jpeg_qmat_tab[0], luma_q, 64);
1034 coda_scale_quant_table(ctx->params.jpeg_qmat_tab[0], scale);
1036 if (ctx->params.jpeg_qmat_tab[1]) {
1037 memcpy(ctx->params.jpeg_qmat_tab[1], chroma_q, 64);
1038 coda_scale_quant_table(ctx->params.jpeg_qmat_tab[1], scale);
1043 * Encoder context operations
1046 static int coda9_jpeg_start_encoding(struct coda_ctx *ctx)
1048 struct coda_dev *dev = ctx->dev;
1051 ret = coda9_jpeg_load_huff_tab(ctx);
1053 v4l2_err(&dev->v4l2_dev, "error loading Huffman tables\n");
1056 if (!ctx->params.jpeg_qmat_tab[0])
1057 ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
1058 if (!ctx->params.jpeg_qmat_tab[1])
1059 ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
1060 coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
1065 static int coda9_jpeg_prepare_encode(struct coda_ctx *ctx)
1067 struct coda_q_data *q_data_src;
1068 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1069 struct coda_dev *dev = ctx->dev;
1070 u32 start_addr, end_addr;
1071 u16 aligned_width, aligned_height;
1072 bool chroma_interleave;
1078 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1079 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1080 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1082 if (vb2_get_plane_payload(&src_buf->vb2_buf, 0) == 0)
1083 vb2_set_plane_payload(&src_buf->vb2_buf, 0,
1084 vb2_plane_size(&src_buf->vb2_buf, 0));
1086 src_buf->sequence = ctx->osequence;
1087 dst_buf->sequence = ctx->osequence;
1090 src_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1091 src_buf->flags &= ~V4L2_BUF_FLAG_PFRAME;
1093 coda_set_gdi_regs(ctx);
1095 start_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1096 end_addr = start_addr + vb2_plane_size(&dst_buf->vb2_buf, 0);
1098 chroma_format = coda9_jpeg_chroma_format(q_data_src->fourcc);
1099 if (chroma_format < 0)
1100 return chroma_format;
1102 /* Round image dimensions to multiple of MCU size */
1103 aligned_width = round_up(q_data_src->width, width_align[chroma_format]);
1104 aligned_height = round_up(q_data_src->height,
1105 height_align[chroma_format]);
1106 if (aligned_width != q_data_src->bytesperline) {
1107 v4l2_err(&dev->v4l2_dev, "wrong stride: %d instead of %d\n",
1108 aligned_width, q_data_src->bytesperline);
1112 coda9_jpeg_encode_header(ctx,
1113 vb2_plane_size(&dst_buf->vb2_buf, 0),
1114 vb2_plane_vaddr(&dst_buf->vb2_buf, 0));
1118 coda_write(dev, start_addr + header_len, CODA9_REG_JPEG_BBC_BAS_ADDR);
1119 coda_write(dev, end_addr, CODA9_REG_JPEG_BBC_END_ADDR);
1120 coda_write(dev, start_addr + header_len, CODA9_REG_JPEG_BBC_WR_PTR);
1121 coda_write(dev, start_addr + header_len, CODA9_REG_JPEG_BBC_RD_PTR);
1122 coda_write(dev, 0, CODA9_REG_JPEG_BBC_CUR_POS);
1123 /* 64 words per 256-byte page */
1124 coda_write(dev, 64, CODA9_REG_JPEG_BBC_DATA_CNT);
1125 coda_write(dev, start_addr, CODA9_REG_JPEG_BBC_EXT_ADDR);
1126 coda_write(dev, 0, CODA9_REG_JPEG_BBC_INT_ADDR);
1128 coda_write(dev, 0, CODA9_REG_JPEG_GBU_BT_PTR);
1129 coda_write(dev, 0, CODA9_REG_JPEG_GBU_WD_PTR);
1130 coda_write(dev, 0, CODA9_REG_JPEG_GBU_BBSR);
1131 coda_write(dev, BIT(31) | ((end_addr - start_addr - header_len) / 256),
1132 CODA9_REG_JPEG_BBC_STRM_CTRL);
1133 coda_write(dev, 0, CODA9_REG_JPEG_GBU_CTRL);
1134 coda_write(dev, 0, CODA9_REG_JPEG_GBU_FF_RPTR);
1135 coda_write(dev, 127, CODA9_REG_JPEG_GBU_BBER);
1136 coda_write(dev, 64, CODA9_REG_JPEG_GBU_BBIR);
1137 coda_write(dev, 64, CODA9_REG_JPEG_GBU_BBHR);
1139 chroma_interleave = (q_data_src->fourcc == V4L2_PIX_FMT_NV12);
1140 coda_write(dev, CODA9_JPEG_PIC_CTRL_TC_DIRECTION |
1141 CODA9_JPEG_PIC_CTRL_ENCODER_EN, CODA9_REG_JPEG_PIC_CTRL);
1142 coda_write(dev, 0, CODA9_REG_JPEG_SCL_INFO);
1143 coda_write(dev, chroma_interleave, CODA9_REG_JPEG_DPB_CONFIG);
1144 coda_write(dev, ctx->params.jpeg_restart_interval,
1145 CODA9_REG_JPEG_RST_INTVAL);
1146 coda_write(dev, 1, CODA9_REG_JPEG_BBC_CTRL);
1148 coda_write(dev, bus_req_num[chroma_format], CODA9_REG_JPEG_OP_INFO);
1150 coda9_jpeg_write_huff_tab(ctx);
1151 coda9_jpeg_load_qmat_tab(ctx);
1153 if (ctx->params.rot_mode & CODA_ROT_90) {
1154 aligned_width = aligned_height;
1155 aligned_height = q_data_src->bytesperline;
1156 if (chroma_format == CODA9_JPEG_FORMAT_422)
1157 chroma_format = CODA9_JPEG_FORMAT_224;
1158 else if (chroma_format == CODA9_JPEG_FORMAT_224)
1159 chroma_format = CODA9_JPEG_FORMAT_422;
1161 /* These need to be multiples of MCU size */
1162 coda_write(dev, aligned_width << 16 | aligned_height,
1163 CODA9_REG_JPEG_PIC_SIZE);
1164 coda_write(dev, ctx->params.rot_mode ?
1165 (CODA_ROT_MIR_ENABLE | ctx->params.rot_mode) : 0,
1166 CODA9_REG_JPEG_ROT_INFO);
1168 coda_write(dev, mcu_info[chroma_format], CODA9_REG_JPEG_MCU_INFO);
1170 coda_write(dev, 1, CODA9_GDI_CONTROL);
1171 timeout = ktime_add_us(ktime_get(), 100000);
1173 ret = coda_read(dev, CODA9_GDI_STATUS);
1174 if (ktime_compare(ktime_get(), timeout) > 0) {
1175 v4l2_err(&dev->v4l2_dev, "timeout waiting for GDI\n");
1180 coda_write(dev, (chroma_format << 17) | (chroma_interleave << 16) |
1181 q_data_src->bytesperline, CODA9_GDI_INFO_CONTROL);
1182 /* The content of this register seems to be irrelevant: */
1183 coda_write(dev, aligned_width << 16 | aligned_height,
1184 CODA9_GDI_INFO_PIC_SIZE);
1186 coda_write_base(ctx, q_data_src, src_buf, CODA9_GDI_INFO_BASE_Y);
1188 coda_write(dev, 0, CODA9_REG_JPEG_DPB_BASE00);
1189 coda_write(dev, 0, CODA9_GDI_CONTROL);
1190 coda_write(dev, 1, CODA9_GDI_PIC_INIT_HOST);
1192 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
1193 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
1195 trace_coda_jpeg_run(ctx, src_buf);
1197 coda_write(dev, 1, CODA9_REG_JPEG_PIC_START);
1202 static void coda9_jpeg_finish_encode(struct coda_ctx *ctx)
1204 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1205 struct coda_dev *dev = ctx->dev;
1206 u32 wr_ptr, start_ptr;
1209 if (ctx->aborting) {
1210 coda_write(ctx->dev, 0, CODA9_REG_JPEG_BBC_FLUSH_CMD);
1215 * Lock to make sure that an encoder stop command running in parallel
1216 * will either already have marked src_buf as last, or it will wake up
1217 * the capture queue after the buffers are returned.
1219 mutex_lock(&ctx->wakeup_mutex);
1220 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1221 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1223 trace_coda_jpeg_done(ctx, dst_buf);
1226 * Set plane payload to the number of bytes written out
1227 * by the JPEG processing unit
1229 start_ptr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1230 wr_ptr = coda_read(dev, CODA9_REG_JPEG_BBC_WR_PTR);
1231 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr);
1233 err_mb = coda_read(dev, CODA9_REG_JPEG_PIC_ERRMB);
1235 coda_dbg(1, ctx, "ERRMB: 0x%x\n", err_mb);
1237 coda_write(dev, 0, CODA9_REG_JPEG_BBC_FLUSH_CMD);
1239 dst_buf->flags &= ~(V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_LAST);
1240 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1241 dst_buf->flags |= src_buf->flags & V4L2_BUF_FLAG_LAST;
1243 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false);
1245 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1246 coda_m2m_buf_done(ctx, dst_buf, err_mb ? VB2_BUF_STATE_ERROR :
1247 VB2_BUF_STATE_DONE);
1248 mutex_unlock(&ctx->wakeup_mutex);
1250 coda_dbg(1, ctx, "job finished: encoded frame (%u)%s\n",
1252 (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? " (last)" : "");
1255 * Reset JPEG processing unit after each encode run to work
1256 * around hangups when switching context between encoder and
1262 static void coda9_jpeg_encode_timeout(struct coda_ctx *ctx)
1264 struct coda_dev *dev = ctx->dev;
1265 u32 end_addr, wr_ptr;
1267 /* Handle missing BBC overflow interrupt via timeout */
1268 end_addr = coda_read(dev, CODA9_REG_JPEG_BBC_END_ADDR);
1269 wr_ptr = coda_read(dev, CODA9_REG_JPEG_BBC_WR_PTR);
1270 if (wr_ptr >= end_addr - 256) {
1271 v4l2_err(&dev->v4l2_dev, "JPEG too large for capture buffer\n");
1272 coda9_jpeg_finish_encode(ctx);
1279 static void coda9_jpeg_release(struct coda_ctx *ctx)
1283 if (ctx->params.jpeg_qmat_tab[0] == luma_q)
1284 ctx->params.jpeg_qmat_tab[0] = NULL;
1285 if (ctx->params.jpeg_qmat_tab[1] == chroma_q)
1286 ctx->params.jpeg_qmat_tab[1] = NULL;
1287 for (i = 0; i < 3; i++)
1288 kfree(ctx->params.jpeg_qmat_tab[i]);
1289 kfree(ctx->params.jpeg_huff_data);
1290 kfree(ctx->params.jpeg_huff_tab);
1293 const struct coda_context_ops coda9_jpeg_encode_ops = {
1294 .queue_init = coda_encoder_queue_init,
1295 .start_streaming = coda9_jpeg_start_encoding,
1296 .prepare_run = coda9_jpeg_prepare_encode,
1297 .finish_run = coda9_jpeg_finish_encode,
1298 .run_timeout = coda9_jpeg_encode_timeout,
1299 .release = coda9_jpeg_release,
1303 * Decoder context operations
1306 static int coda9_jpeg_start_decoding(struct coda_ctx *ctx)
1308 ctx->params.jpeg_qmat_index[0] = 0;
1309 ctx->params.jpeg_qmat_index[1] = 1;
1310 ctx->params.jpeg_qmat_index[2] = 1;
1311 ctx->params.jpeg_qmat_tab[0] = luma_q;
1312 ctx->params.jpeg_qmat_tab[1] = chroma_q;
1313 /* nothing more to do here */
1315 /* TODO: we could already scan the first header to get the chroma
1322 static int coda9_jpeg_prepare_decode(struct coda_ctx *ctx)
1324 struct coda_dev *dev = ctx->dev;
1325 int aligned_width, aligned_height;
1328 u32 val, dst_fourcc;
1329 struct coda_q_data *q_data_src, *q_data_dst;
1330 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1331 int chroma_interleave;
1332 int scl_hor_mode, scl_ver_mode;
1334 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1335 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1336 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1337 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1338 dst_fourcc = q_data_dst->fourcc;
1340 scl_hor_mode = coda_jpeg_scale(q_data_src->width, q_data_dst->width);
1341 scl_ver_mode = coda_jpeg_scale(q_data_src->height, q_data_dst->height);
1343 if (vb2_get_plane_payload(&src_buf->vb2_buf, 0) == 0)
1344 vb2_set_plane_payload(&src_buf->vb2_buf, 0,
1345 vb2_plane_size(&src_buf->vb2_buf, 0));
1347 chroma_format = coda9_jpeg_chroma_format(q_data_dst->fourcc);
1348 if (chroma_format < 0)
1349 return chroma_format;
1351 ret = coda_jpeg_decode_header(ctx, &src_buf->vb2_buf);
1353 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1354 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1355 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1356 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1361 /* Round image dimensions to multiple of MCU size */
1362 aligned_width = round_up(q_data_src->width, width_align[chroma_format]);
1363 aligned_height = round_up(q_data_src->height, height_align[chroma_format]);
1364 if (aligned_width != q_data_dst->bytesperline) {
1365 v4l2_err(&dev->v4l2_dev, "stride mismatch: %d != %d\n",
1366 aligned_width, q_data_dst->bytesperline);
1369 coda_set_gdi_regs(ctx);
1371 val = ctx->params.jpeg_huff_ac_index[0] << 12 |
1372 ctx->params.jpeg_huff_ac_index[1] << 11 |
1373 ctx->params.jpeg_huff_ac_index[2] << 10 |
1374 ctx->params.jpeg_huff_dc_index[0] << 9 |
1375 ctx->params.jpeg_huff_dc_index[1] << 8 |
1376 ctx->params.jpeg_huff_dc_index[2] << 7;
1377 if (ctx->params.jpeg_huff_tab)
1378 val |= CODA9_JPEG_PIC_CTRL_USER_HUFFMAN_EN;
1379 coda_write(dev, val, CODA9_REG_JPEG_PIC_CTRL);
1381 coda_write(dev, aligned_width << 16 | aligned_height,
1382 CODA9_REG_JPEG_PIC_SIZE);
1384 chroma_interleave = (dst_fourcc == V4L2_PIX_FMT_NV12);
1385 coda_write(dev, 0, CODA9_REG_JPEG_ROT_INFO);
1386 coda_write(dev, bus_req_num[chroma_format], CODA9_REG_JPEG_OP_INFO);
1387 coda_write(dev, mcu_info[chroma_format], CODA9_REG_JPEG_MCU_INFO);
1388 if (scl_hor_mode || scl_ver_mode)
1389 val = CODA9_JPEG_SCL_ENABLE | (scl_hor_mode << 2) | scl_ver_mode;
1392 coda_write(dev, val, CODA9_REG_JPEG_SCL_INFO);
1393 coda_write(dev, chroma_interleave, CODA9_REG_JPEG_DPB_CONFIG);
1394 coda_write(dev, ctx->params.jpeg_restart_interval,
1395 CODA9_REG_JPEG_RST_INTVAL);
1397 if (ctx->params.jpeg_huff_tab) {
1398 ret = coda9_jpeg_dec_huff_setup(ctx);
1400 v4l2_err(&dev->v4l2_dev,
1401 "failed to set up Huffman tables: %d\n", ret);
1406 coda9_jpeg_qmat_setup(ctx);
1408 coda9_jpeg_dec_bbc_gbu_setup(ctx, &src_buf->vb2_buf,
1409 ctx->jpeg_ecs_offset);
1411 coda_write(dev, 0, CODA9_REG_JPEG_RST_INDEX);
1412 coda_write(dev, 0, CODA9_REG_JPEG_RST_COUNT);
1414 coda_write(dev, 0, CODA9_REG_JPEG_DPCM_DIFF_Y);
1415 coda_write(dev, 0, CODA9_REG_JPEG_DPCM_DIFF_CB);
1416 coda_write(dev, 0, CODA9_REG_JPEG_DPCM_DIFF_CR);
1418 coda_write(dev, 0, CODA9_REG_JPEG_ROT_INFO);
1420 coda_write(dev, 1, CODA9_GDI_CONTROL);
1422 ret = coda_read(dev, CODA9_GDI_STATUS);
1425 val = (chroma_format << 17) | (chroma_interleave << 16) |
1426 q_data_dst->bytesperline;
1427 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
1429 coda_write(dev, val, CODA9_GDI_INFO_CONTROL);
1431 coda_write(dev, aligned_width << 16 | aligned_height,
1432 CODA9_GDI_INFO_PIC_SIZE);
1434 coda_write_base(ctx, q_data_dst, dst_buf, CODA9_GDI_INFO_BASE_Y);
1436 coda_write(dev, 0, CODA9_REG_JPEG_DPB_BASE00);
1437 coda_write(dev, 0, CODA9_GDI_CONTROL);
1438 coda_write(dev, 1, CODA9_GDI_PIC_INIT_HOST);
1440 trace_coda_jpeg_run(ctx, src_buf);
1442 coda_write(dev, 1, CODA9_REG_JPEG_PIC_START);
1447 static void coda9_jpeg_finish_decode(struct coda_ctx *ctx)
1449 struct coda_dev *dev = ctx->dev;
1450 struct vb2_v4l2_buffer *dst_buf, *src_buf;
1451 struct coda_q_data *q_data_dst;
1454 err_mb = coda_read(dev, CODA9_REG_JPEG_PIC_ERRMB);
1456 v4l2_err(&dev->v4l2_dev, "ERRMB: 0x%x\n", err_mb);
1458 coda_write(dev, 0, CODA9_REG_JPEG_BBC_FLUSH_CMD);
1461 * Lock to make sure that a decoder stop command running in parallel
1462 * will either already have marked src_buf as last, or it will wake up
1463 * the capture queue after the buffers are returned.
1465 mutex_lock(&ctx->wakeup_mutex);
1466 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1467 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1468 dst_buf->sequence = ctx->osequence++;
1470 trace_coda_jpeg_done(ctx, dst_buf);
1472 dst_buf->flags &= ~(V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_LAST);
1473 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1474 dst_buf->flags |= src_buf->flags & V4L2_BUF_FLAG_LAST;
1476 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false);
1478 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1479 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, q_data_dst->sizeimage);
1481 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1482 coda_m2m_buf_done(ctx, dst_buf, err_mb ? VB2_BUF_STATE_ERROR :
1483 VB2_BUF_STATE_DONE);
1485 mutex_unlock(&ctx->wakeup_mutex);
1487 coda_dbg(1, ctx, "job finished: decoded frame (%u)%s\n",
1489 (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? " (last)" : "");
1492 * Reset JPEG processing unit after each decode run to work
1493 * around hangups when switching context between encoder and
1499 const struct coda_context_ops coda9_jpeg_decode_ops = {
1500 .queue_init = coda_encoder_queue_init, /* non-bitstream operation */
1501 .start_streaming = coda9_jpeg_start_decoding,
1502 .prepare_run = coda9_jpeg_prepare_decode,
1503 .finish_run = coda9_jpeg_finish_decode,
1504 .release = coda9_jpeg_release,
1507 irqreturn_t coda9_jpeg_irq_handler(int irq, void *data)
1509 struct coda_dev *dev = data;
1510 struct coda_ctx *ctx;
1514 status = coda_read(dev, CODA9_REG_JPEG_PIC_STATUS);
1517 coda_write(dev, status, CODA9_REG_JPEG_PIC_STATUS);
1519 if (status & CODA9_JPEG_STATUS_OVERFLOW)
1520 v4l2_err(&dev->v4l2_dev, "JPEG overflow\n");
1522 if (status & CODA9_JPEG_STATUS_BBC_INT)
1523 v4l2_err(&dev->v4l2_dev, "JPEG BBC interrupt\n");
1525 if (status & CODA9_JPEG_STATUS_ERROR) {
1526 v4l2_err(&dev->v4l2_dev, "JPEG error\n");
1528 err_mb = coda_read(dev, CODA9_REG_JPEG_PIC_ERRMB);
1530 v4l2_err(&dev->v4l2_dev,
1531 "ERRMB: 0x%x: rst idx %d, mcu pos (%d,%d)\n",
1532 err_mb, err_mb >> 24, (err_mb >> 12) & 0xfff,
1537 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1539 v4l2_err(&dev->v4l2_dev,
1540 "Instance released before the end of transaction\n");
1541 mutex_unlock(&dev->coda_mutex);
1545 complete(&ctx->completion);