// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Coda multi - standard codec IP - JPEG support functions
*
* Copyright ( C ) 2014 Philipp Zabel , Pengutronix
*/
#include <linux/unaligned.h>
#include <linux/irqreturn.h>
#include <linux/kernel.h>
#include <linux/ktime.h>
#include <linux/slab.h>
#include <linux/swab.h>
#include <linux/videodev2.h>
#include <media/v4l2-common.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-jpeg.h>
#include <media/v4l2-mem2mem.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-dma-contig.h>
#include "coda.h"
#include "trace.h"
#define SOI_MARKER 0 xffd8
#define APP9_MARKER 0 xffe9
#define DRI_MARKER 0 xffdd
#define DQT_MARKER 0 xffdb
#define DHT_MARKER 0 xffc4
#define SOF_MARKER 0 xffc0
#define SOS_MARKER 0 xffda
#define EOI_MARKER 0 xffd9
enum {
CODA9_JPEG_FORMAT_420,
CODA9_JPEG_FORMAT_422,
CODA9_JPEG_FORMAT_224,
CODA9_JPEG_FORMAT_444,
CODA9_JPEG_FORMAT_400,
};
struct coda_huff_tab {
u8 luma_dc[16 + 12 ];
u8 chroma_dc[16 + 12 ];
u8 luma_ac[16 + 162 ];
u8 chroma_ac[16 + 162 ];
/* DC Luma, DC Chroma, AC Luma, AC Chroma */
s16 min[4 * 16 ];
s16 max[4 * 16 ];
s8 ptr[4 * 16 ];
};
#define CODA9_JPEG_ENC_HUFF_DATA_SIZE (256 + 256 + 16 + 16 )
/*
* Typical Huffman tables for 8 - bit precision luminance and
* chrominance from JPEG ITU - T . 81 ( ISO / IEC 10918 - 1 ) Annex K . 3
*/
static const unsigned char luma_dc[16 + 12 ] = {
/* bits */
0 x00, 0 x01, 0 x05, 0 x01, 0 x01, 0 x01, 0 x01, 0 x01,
0 x01, 0 x00, 0 x00, 0 x00, 0 x00, 0 x00, 0 x00, 0 x00,
/* values */
0 x00, 0 x01, 0 x02, 0 x03, 0 x04, 0 x05, 0 x06, 0 x07,
0 x08, 0 x09, 0 x0a, 0 x0b,
};
static const unsigned char chroma_dc[16 + 12 ] = {
/* bits */
0 x00, 0 x03, 0 x01, 0 x01, 0 x01, 0 x01, 0 x01, 0 x01,
0 x01, 0 x01, 0 x01, 0 x00, 0 x00, 0 x00, 0 x00, 0 x00,
/* values */
0 x00, 0 x01, 0 x02, 0 x03, 0 x04, 0 x05, 0 x06, 0 x07,
0 x08, 0 x09, 0 x0a, 0 x0b,
};
static const unsigned char luma_ac[16 + 162 + 2 ] = {
/* bits */
0 x00, 0 x02, 0 x01, 0 x03, 0 x03, 0 x02, 0 x04, 0 x03,
0 x05, 0 x05, 0 x04, 0 x04, 0 x00, 0 x00, 0 x01, 0 x7d,
/* values */
0 x01, 0 x02, 0 x03, 0 x00, 0 x04, 0 x11, 0 x05, 0 x12,
0 x21, 0 x31, 0 x41, 0 x06, 0 x13, 0 x51, 0 x61, 0 x07,
0 x22, 0 x71, 0 x14, 0 x32, 0 x81, 0 x91, 0 xa1, 0 x08,
0 x23, 0 x42, 0 xb1, 0 xc1, 0 x15, 0 x52, 0 xd1, 0 xf0,
0 x24, 0 x33, 0 x62, 0 x72, 0 x82, 0 x09, 0 x0a, 0 x16,
0 x17, 0 x18, 0 x19, 0 x1a, 0 x25, 0 x26, 0 x27, 0 x28,
0 x29, 0 x2a, 0 x34, 0 x35, 0 x36, 0 x37, 0 x38, 0 x39,
0 x3a, 0 x43, 0 x44, 0 x45, 0 x46, 0 x47, 0 x48, 0 x49,
0 x4a, 0 x53, 0 x54, 0 x55, 0 x56, 0 x57, 0 x58, 0 x59,
0 x5a, 0 x63, 0 x64, 0 x65, 0 x66, 0 x67, 0 x68, 0 x69,
0 x6a, 0 x73, 0 x74, 0 x75, 0 x76, 0 x77, 0 x78, 0 x79,
0 x7a, 0 x83, 0 x84, 0 x85, 0 x86, 0 x87, 0 x88, 0 x89,
0 x8a, 0 x92, 0 x93, 0 x94, 0 x95, 0 x96, 0 x97, 0 x98,
0 x99, 0 x9a, 0 xa2, 0 xa3, 0 xa4, 0 xa5, 0 xa6, 0 xa7,
0 xa8, 0 xa9, 0 xaa, 0 xb2, 0 xb3, 0 xb4, 0 xb5, 0 xb6,
0 xb7, 0 xb8, 0 xb9, 0 xba, 0 xc2, 0 xc3, 0 xc4, 0 xc5,
0 xc6, 0 xc7, 0 xc8, 0 xc9, 0 xca, 0 xd2, 0 xd3, 0 xd4,
0 xd5, 0 xd6, 0 xd7, 0 xd8, 0 xd9, 0 xda, 0 xe1, 0 xe2,
0 xe3, 0 xe4, 0 xe5, 0 xe6, 0 xe7, 0 xe8, 0 xe9, 0 xea,
0 xf1, 0 xf2, 0 xf3, 0 xf4, 0 xf5, 0 xf6, 0 xf7, 0 xf8,
0 xf9, 0 xfa, /* padded to 32-bit */
};
static const unsigned char chroma_ac[16 + 162 + 2 ] = {
/* bits */
0 x00, 0 x02, 0 x01, 0 x02, 0 x04, 0 x04, 0 x03, 0 x04,
0 x07, 0 x05, 0 x04, 0 x04, 0 x00, 0 x01, 0 x02, 0 x77,
/* values */
0 x00, 0 x01, 0 x02, 0 x03, 0 x11, 0 x04, 0 x05, 0 x21,
0 x31, 0 x06, 0 x12, 0 x41, 0 x51, 0 x07, 0 x61, 0 x71,
0 x13, 0 x22, 0 x32, 0 x81, 0 x08, 0 x14, 0 x42, 0 x91,
0 xa1, 0 xb1, 0 xc1, 0 x09, 0 x23, 0 x33, 0 x52, 0 xf0,
0 x15, 0 x62, 0 x72, 0 xd1, 0 x0a, 0 x16, 0 x24, 0 x34,
0 xe1, 0 x25, 0 xf1, 0 x17, 0 x18, 0 x19, 0 x1a, 0 x26,
0 x27, 0 x28, 0 x29, 0 x2a, 0 x35, 0 x36, 0 x37, 0 x38,
0 x39, 0 x3a, 0 x43, 0 x44, 0 x45, 0 x46, 0 x47, 0 x48,
0 x49, 0 x4a, 0 x53, 0 x54, 0 x55, 0 x56, 0 x57, 0 x58,
0 x59, 0 x5a, 0 x63, 0 x64, 0 x65, 0 x66, 0 x67, 0 x68,
0 x69, 0 x6a, 0 x73, 0 x74, 0 x75, 0 x76, 0 x77, 0 x78,
0 x79, 0 x7a, 0 x82, 0 x83, 0 x84, 0 x85, 0 x86, 0 x87,
0 x88, 0 x89, 0 x8a, 0 x92, 0 x93, 0 x94, 0 x95, 0 x96,
0 x97, 0 x98, 0 x99, 0 x9a, 0 xa2, 0 xa3, 0 xa4, 0 xa5,
0 xa6, 0 xa7, 0 xa8, 0 xa9, 0 xaa, 0 xb2, 0 xb3, 0 xb4,
0 xb5, 0 xb6, 0 xb7, 0 xb8, 0 xb9, 0 xba, 0 xc2, 0 xc3,
0 xc4, 0 xc5, 0 xc6, 0 xc7, 0 xc8, 0 xc9, 0 xca, 0 xd2,
0 xd3, 0 xd4, 0 xd5, 0 xd6, 0 xd7, 0 xd8, 0 xd9, 0 xda,
0 xe2, 0 xe3, 0 xe4, 0 xe5, 0 xe6, 0 xe7, 0 xe8, 0 xe9,
0 xea, 0 xf2, 0 xf3, 0 xf4, 0 xf5, 0 xf6, 0 xf7, 0 xf8,
0 xf9, 0 xfa, /* padded to 32-bit */
};
/*
* Quantization tables for luminance and chrominance components in
* zig - zag scan order from the Freescale i . MX VPU libraries
*/
static unsigned char luma_q[64 ] = {
0 x06, 0 x04, 0 x04, 0 x04, 0 x05, 0 x04, 0 x06, 0 x05,
0 x05, 0 x06, 0 x09, 0 x06, 0 x05, 0 x06, 0 x09, 0 x0b,
0 x08, 0 x06, 0 x06, 0 x08, 0 x0b, 0 x0c, 0 x0a, 0 x0a,
0 x0b, 0 x0a, 0 x0a, 0 x0c, 0 x10, 0 x0c, 0 x0c, 0 x0c,
0 x0c, 0 x0c, 0 x0c, 0 x10, 0 x0c, 0 x0c, 0 x0c, 0 x0c,
0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c,
0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c,
0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c,
};
static unsigned char chroma_q[64 ] = {
0 x07, 0 x07, 0 x07, 0 x0d, 0 x0c, 0 x0d, 0 x18, 0 x10,
0 x10, 0 x18, 0 x14, 0 x0e, 0 x0e, 0 x0e, 0 x14, 0 x14,
0 x0e, 0 x0e, 0 x0e, 0 x0e, 0 x14, 0 x11, 0 x0c, 0 x0c,
0 x0c, 0 x0c, 0 x0c, 0 x11, 0 x11, 0 x0c, 0 x0c, 0 x0c,
0 x0c, 0 x0c, 0 x0c, 0 x11, 0 x0c, 0 x0c, 0 x0c, 0 x0c,
0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c,
0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c,
0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c, 0 x0c,
};
static const unsigned char width_align[] = {
[CODA9_JPEG_FORMAT_420] = 16 ,
[CODA9_JPEG_FORMAT_422] = 16 ,
[CODA9_JPEG_FORMAT_224] = 8 ,
[CODA9_JPEG_FORMAT_444] = 8 ,
[CODA9_JPEG_FORMAT_400] = 8 ,
};
static const unsigned char height_align[] = {
[CODA9_JPEG_FORMAT_420] = 16 ,
[CODA9_JPEG_FORMAT_422] = 8 ,
[CODA9_JPEG_FORMAT_224] = 16 ,
[CODA9_JPEG_FORMAT_444] = 8 ,
[CODA9_JPEG_FORMAT_400] = 8 ,
};
static int coda9_jpeg_chroma_format(u32 pixfmt)
{
switch (pixfmt) {
case V4L2_PIX_FMT_YUV420:
case V4L2_PIX_FMT_NV12:
return CODA9_JPEG_FORMAT_420;
case V4L2_PIX_FMT_YUV422P:
return CODA9_JPEG_FORMAT_422;
case V4L2_PIX_FMT_YUV444:
return CODA9_JPEG_FORMAT_444;
case V4L2_PIX_FMT_GREY:
return CODA9_JPEG_FORMAT_400;
}
return -EINVAL;
}
struct coda_memcpy_desc {
int offset;
const void *src;
size_t len;
};
static void coda_memcpy_parabuf(void *parabuf,
const struct coda_memcpy_desc *desc)
{
u32 *dst = parabuf + desc->offset;
const u32 *src = desc->src;
int len = desc->len / 4 ;
int i;
for (i = 0 ; i < len; i += 2 ) {
dst[i + 1 ] = swab32(src[i]);
dst[i] = swab32(src[i + 1 ]);
}
}
int coda_jpeg_write_tables(struct coda_ctx *ctx)
{
int i;
static const struct coda_memcpy_desc huff[8 ] = {
{ 0 , luma_dc, sizeof (luma_dc) },
{ 32 , luma_ac, sizeof (luma_ac) },
{ 216 , chroma_dc, sizeof (chroma_dc) },
{ 248 , chroma_ac, sizeof (chroma_ac) },
};
struct coda_memcpy_desc qmat[3 ] = {
{ 512 , ctx->params.jpeg_qmat_tab[0 ], 64 },
{ 576 , ctx->params.jpeg_qmat_tab[1 ], 64 },
{ 640 , ctx->params.jpeg_qmat_tab[1 ], 64 },
};
/* Write huffman tables to parameter memory */
for (i = 0 ; i < ARRAY_SIZE(huff); i++)
coda_memcpy_parabuf(ctx->parabuf.vaddr, huff + i);
/* Write Q-matrix to parameter memory */
for (i = 0 ; i < ARRAY_SIZE(qmat); i++)
coda_memcpy_parabuf(ctx->parabuf.vaddr, qmat + i);
return 0 ;
}
bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb)
{
void *vaddr = vb2_plane_vaddr(vb, 0 );
u16 soi, eoi;
int len, i;
soi = be16_to_cpup((__be16 *)vaddr);
if (soi != SOI_MARKER)
return false ;
len = vb2_get_plane_payload(vb, 0 );
vaddr += len - 2 ;
for (i = 0 ; i < 32 ; i++) {
eoi = be16_to_cpup((__be16 *)(vaddr - i));
if (eoi == EOI_MARKER) {
if (i > 0 )
vb2_set_plane_payload(vb, 0 , len - i);
return true ;
}
}
return false ;
}
static int coda9_jpeg_gen_dec_huff_tab(struct coda_ctx *ctx, int tab_num);
int coda_jpeg_decode_header(struct coda_ctx *ctx, struct vb2_buffer *vb)
{
struct coda_dev *dev = ctx->dev;
u8 *buf = vb2_plane_vaddr(vb, 0 );
size_t len = vb2_get_plane_payload(vb, 0 );
struct v4l2_jpeg_scan_header scan_header;
struct v4l2_jpeg_reference quantization_tables[4 ] = { };
struct v4l2_jpeg_reference huffman_tables[4 ] = { };
struct v4l2_jpeg_header header = {
.scan = &scan_header,
.quantization_tables = quantization_tables,
.huffman_tables = huffman_tables,
};
struct coda_q_data *q_data_src;
struct coda_huff_tab *huff_tab;
int i, j, ret;
ret = v4l2_jpeg_parse_header(buf, len, &header);
if (ret < 0 ) {
v4l2_err(&dev->v4l2_dev, "failed to parse JPEG header: %pe\n" ,
ERR_PTR(ret));
return ret;
}
ctx->params.jpeg_restart_interval = header.restart_interval;
/* check frame header */
if (header.frame.height > ctx->codec->max_h ||
header.frame.width > ctx->codec->max_w) {
v4l2_err(&dev->v4l2_dev, "invalid dimensions: %dx%d\n" ,
header.frame.width, header.frame.height);
return -EINVAL;
}
q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
if (header.frame.height != q_data_src->height ||
header.frame.width != q_data_src->width) {
v4l2_err(&dev->v4l2_dev,
"dimensions don't match format: %dx%d\n" ,
header.frame.width, header.frame.height);
return -EINVAL;
}
if (header.frame.num_components != 3 ) {
v4l2_err(&dev->v4l2_dev,
"unsupported number of components: %d\n" ,
header.frame.num_components);
return -EINVAL;
}
/* install quantization tables */
if (quantization_tables[3 ].start) {
v4l2_err(&dev->v4l2_dev,
"only 3 quantization tables supported\n" );
return -EINVAL;
}
for (i = 0 ; i < 3 ; i++) {
if (!quantization_tables[i].start)
continue ;
if (quantization_tables[i].length != 64 ) {
v4l2_err(&dev->v4l2_dev,
"only 8-bit quantization tables supported\n" );
continue ;
}
if (!ctx->params.jpeg_qmat_tab[i]) {
ctx->params.jpeg_qmat_tab[i] = kmalloc(64 , GFP_KERNEL);
if (!ctx->params.jpeg_qmat_tab[i])
return -ENOMEM;
}
memcpy(ctx->params.jpeg_qmat_tab[i],
quantization_tables[i].start, 64 );
}
/* install Huffman tables */
for (i = 0 ; i < 4 ; i++) {
if (!huffman_tables[i].start) {
v4l2_err(&dev->v4l2_dev, "missing Huffman table\n" );
return -EINVAL;
}
/* AC tables should be between 17 -> 178, DC between 17 -> 28 */
if (huffman_tables[i].length < 17 ||
huffman_tables[i].length > 178 ||
((i & 2 ) == 0 && huffman_tables[i].length > 28 )) {
v4l2_err(&dev->v4l2_dev,
"invalid Huffman table %d length: %zu\n" ,
i, huffman_tables[i].length);
return -EINVAL;
}
}
huff_tab = ctx->params.jpeg_huff_tab;
if (!huff_tab) {
huff_tab = kzalloc(sizeof (struct coda_huff_tab), GFP_KERNEL);
if (!huff_tab)
return -ENOMEM;
ctx->params.jpeg_huff_tab = huff_tab;
}
memset(huff_tab, 0 , sizeof (*huff_tab));
memcpy(huff_tab->luma_dc, huffman_tables[0 ].start, huffman_tables[0 ].length);
memcpy(huff_tab->chroma_dc, huffman_tables[1 ].start, huffman_tables[1 ].length);
memcpy(huff_tab->luma_ac, huffman_tables[2 ].start, huffman_tables[2 ].length);
memcpy(huff_tab->chroma_ac, huffman_tables[3 ].start, huffman_tables[3 ].length);
/* check scan header */
for (i = 0 ; i < scan_header.num_components; i++) {
struct v4l2_jpeg_scan_component_spec *scan_component;
scan_component = &scan_header.component[i];
for (j = 0 ; j < header.frame.num_components; j++) {
if (header.frame.component[j].component_identifier ==
scan_component->component_selector)
break ;
}
if (j == header.frame.num_components)
continue ;
ctx->params.jpeg_huff_dc_index[j] =
scan_component->dc_entropy_coding_table_selector;
ctx->params.jpeg_huff_ac_index[j] =
scan_component->ac_entropy_coding_table_selector;
}
/* Generate Huffman table information */
for (i = 0 ; i < 4 ; i++)
coda9_jpeg_gen_dec_huff_tab(ctx, i);
/* start of entropy coded segment */
ctx->jpeg_ecs_offset = header.ecs_offset;
switch (header.frame.subsampling) {
case V4L2_JPEG_CHROMA_SUBSAMPLING_420:
case V4L2_JPEG_CHROMA_SUBSAMPLING_422:
ctx->params.jpeg_chroma_subsampling = header.frame.subsampling;
break ;
default :
v4l2_err(&dev->v4l2_dev, "chroma subsampling not supported: %d" ,
header.frame.subsampling);
return -EINVAL;
}
return 0 ;
}
static inline void coda9_jpeg_write_huff_values(struct coda_dev *dev, u8 *bits,
int num_values)
{
s8 *values = (s8 *)(bits + 16 );
int huff_length, i;
for (huff_length = 0 , i = 0 ; i < 16 ; i++)
huff_length += bits[i];
for (i = huff_length; i < num_values; i++)
values[i] = -1 ;
for (i = 0 ; i < num_values; i++)
coda_write(dev, (s32)values[i], CODA9_REG_JPEG_HUFF_DATA);
}
static void coda9_jpeg_dec_huff_setup(struct coda_ctx *ctx)
{
struct coda_huff_tab *huff_tab = ctx->params.jpeg_huff_tab;
struct coda_dev *dev = ctx->dev;
s16 *huff_min = huff_tab->min;
s16 *huff_max = huff_tab->max;
s8 *huff_ptr = huff_tab->ptr;
int i;
/* MIN Tables */
coda_write(dev, 0 x003, CODA9_REG_JPEG_HUFF_CTRL);
coda_write(dev, 0 x000, CODA9_REG_JPEG_HUFF_ADDR);
for (i = 0 ; i < 4 * 16 ; i++)
coda_write(dev, (s32)huff_min[i], CODA9_REG_JPEG_HUFF_DATA);
/* MAX Tables */
coda_write(dev, 0 x403, CODA9_REG_JPEG_HUFF_CTRL);
coda_write(dev, 0 x440, CODA9_REG_JPEG_HUFF_ADDR);
for (i = 0 ; i < 4 * 16 ; i++)
coda_write(dev, (s32)huff_max[i], CODA9_REG_JPEG_HUFF_DATA);
/* PTR Tables */
coda_write(dev, 0 x803, CODA9_REG_JPEG_HUFF_CTRL);
coda_write(dev, 0 x880, CODA9_REG_JPEG_HUFF_ADDR);
for (i = 0 ; i < 4 * 16 ; i++)
coda_write(dev, (s32)huff_ptr[i], CODA9_REG_JPEG_HUFF_DATA);
/* VAL Tables: DC Luma, DC Chroma, AC Luma, AC Chroma */
coda_write(dev, 0 xc03, CODA9_REG_JPEG_HUFF_CTRL);
coda9_jpeg_write_huff_values(dev, huff_tab->luma_dc, 12 );
coda9_jpeg_write_huff_values(dev, huff_tab->chroma_dc, 12 );
coda9_jpeg_write_huff_values(dev, huff_tab->luma_ac, 162 );
coda9_jpeg_write_huff_values(dev, huff_tab->chroma_ac, 162 );
coda_write(dev, 0 x000, CODA9_REG_JPEG_HUFF_CTRL);
}
static inline void coda9_jpeg_write_qmat_tab(struct coda_dev *dev,
u8 *qmat, int index)
{
int i;
coda_write(dev, index | 0 x3, CODA9_REG_JPEG_QMAT_CTRL);
for (i = 0 ; i < 64 ; i++)
coda_write(dev, qmat[i], CODA9_REG_JPEG_QMAT_DATA);
coda_write(dev, 0 , CODA9_REG_JPEG_QMAT_CTRL);
}
static void coda9_jpeg_qmat_setup(struct coda_ctx *ctx)
{
struct coda_dev *dev = ctx->dev;
int *qmat_index = ctx->params.jpeg_qmat_index;
u8 **qmat_tab = ctx->params.jpeg_qmat_tab;
coda9_jpeg_write_qmat_tab(dev, qmat_tab[qmat_index[0 ]], 0 x00);
coda9_jpeg_write_qmat_tab(dev, qmat_tab[qmat_index[1 ]], 0 x40);
coda9_jpeg_write_qmat_tab(dev, qmat_tab[qmat_index[2 ]], 0 x80);
}
static void coda9_jpeg_dec_bbc_gbu_setup(struct coda_ctx *ctx,
struct vb2_buffer *buf, u32 ecs_offset)
{
struct coda_dev *dev = ctx->dev;
int page_ptr, word_ptr, bit_ptr;
u32 bbc_base_addr, end_addr;
int bbc_cur_pos;
int ret, val;
bbc_base_addr = vb2_dma_contig_plane_dma_addr(buf, 0 );
end_addr = bbc_base_addr + vb2_get_plane_payload(buf, 0 );
page_ptr = ecs_offset / 256 ;
word_ptr = (ecs_offset % 256 ) / 4 ;
if (page_ptr & 1 )
word_ptr += 64 ;
bit_ptr = (ecs_offset % 4 ) * 8 ;
if (word_ptr & 1 )
bit_ptr += 32 ;
word_ptr &= ~0 x1;
coda_write(dev, end_addr, CODA9_REG_JPEG_BBC_WR_PTR);
coda_write(dev, bbc_base_addr, CODA9_REG_JPEG_BBC_BAS_ADDR);
/* Leave 3 256-byte page margin to avoid a BBC interrupt */
coda_write(dev, end_addr + 256 * 3 + 256 , CODA9_REG_JPEG_BBC_END_ADDR);
val = DIV_ROUND_UP(vb2_plane_size(buf, 0 ), 256 ) + 3 ;
coda_write(dev, BIT(31 ) | val, CODA9_REG_JPEG_BBC_STRM_CTRL);
bbc_cur_pos = page_ptr;
coda_write(dev, bbc_cur_pos, CODA9_REG_JPEG_BBC_CUR_POS);
coda_write(dev, bbc_base_addr + (bbc_cur_pos << 8 ),
CODA9_REG_JPEG_BBC_EXT_ADDR);
coda_write(dev, (bbc_cur_pos & 1 ) << 6 , CODA9_REG_JPEG_BBC_INT_ADDR);
coda_write(dev, 64 , CODA9_REG_JPEG_BBC_DATA_CNT);
coda_write(dev, 0 , CODA9_REG_JPEG_BBC_COMMAND);
do {
ret = coda_read(dev, CODA9_REG_JPEG_BBC_BUSY);
} while (ret == 1 );
bbc_cur_pos++;
coda_write(dev, bbc_cur_pos, CODA9_REG_JPEG_BBC_CUR_POS);
coda_write(dev, bbc_base_addr + (bbc_cur_pos << 8 ),
CODA9_REG_JPEG_BBC_EXT_ADDR);
coda_write(dev, (bbc_cur_pos & 1 ) << 6 , CODA9_REG_JPEG_BBC_INT_ADDR);
coda_write(dev, 64 , CODA9_REG_JPEG_BBC_DATA_CNT);
coda_write(dev, 0 , CODA9_REG_JPEG_BBC_COMMAND);
do {
ret = coda_read(dev, CODA9_REG_JPEG_BBC_BUSY);
} while (ret == 1 );
bbc_cur_pos++;
coda_write(dev, bbc_cur_pos, CODA9_REG_JPEG_BBC_CUR_POS);
coda_write(dev, 1 , CODA9_REG_JPEG_BBC_CTRL);
coda_write(dev, 0 , CODA9_REG_JPEG_GBU_TT_CNT);
coda_write(dev, word_ptr, CODA9_REG_JPEG_GBU_WD_PTR);
coda_write(dev, 0 , CODA9_REG_JPEG_GBU_BBSR);
coda_write(dev, 127 , CODA9_REG_JPEG_GBU_BBER);
if (page_ptr & 1 ) {
coda_write(dev, 0 , CODA9_REG_JPEG_GBU_BBIR);
coda_write(dev, 0 , CODA9_REG_JPEG_GBU_BBHR);
} else {
coda_write(dev, 64 , CODA9_REG_JPEG_GBU_BBIR);
coda_write(dev, 64 , CODA9_REG_JPEG_GBU_BBHR);
}
coda_write(dev, 4 , CODA9_REG_JPEG_GBU_CTRL);
coda_write(dev, bit_ptr, CODA9_REG_JPEG_GBU_FF_RPTR);
coda_write(dev, 3 , CODA9_REG_JPEG_GBU_CTRL);
}
static const int bus_req_num[] = {
[CODA9_JPEG_FORMAT_420] = 2 ,
[CODA9_JPEG_FORMAT_422] = 3 ,
[CODA9_JPEG_FORMAT_224] = 3 ,
[CODA9_JPEG_FORMAT_444] = 4 ,
[CODA9_JPEG_FORMAT_400] = 4 ,
};
#define MCU_INFO(mcu_block_num, comp_num, comp0_info, comp1_info, comp2_info) \
(((mcu_block_num) << CODA9_JPEG_MCU_BLOCK_NUM_OFFSET) | \
((comp_num) << CODA9_JPEG_COMP_NUM_OFFSET) | \
((comp0_info) << CODA9_JPEG_COMP0_INFO_OFFSET) | \
((comp1_info) << CODA9_JPEG_COMP1_INFO_OFFSET) | \
((comp2_info) << CODA9_JPEG_COMP2_INFO_OFFSET))
static const u32 mcu_info[] = {
[CODA9_JPEG_FORMAT_420] = MCU_INFO(6 , 3 , 10 , 5 , 5 ),
[CODA9_JPEG_FORMAT_422] = MCU_INFO(4 , 3 , 9 , 5 , 5 ),
[CODA9_JPEG_FORMAT_224] = MCU_INFO(4 , 3 , 6 , 5 , 5 ),
[CODA9_JPEG_FORMAT_444] = MCU_INFO(3 , 3 , 5 , 5 , 5 ),
[CODA9_JPEG_FORMAT_400] = MCU_INFO(1 , 1 , 5 , 0 , 0 ),
};
/*
* Convert Huffman table specifcations to tables of codes and code lengths .
* For reference , see JPEG ITU - T . 81 ( ISO / IEC 10918 - 1 ) [ 1 ]
*
* [ 1 ] https : //www.w3.org/Graphics/JPEG/itu-t81.pdf
*/
static int coda9_jpeg_gen_enc_huff_tab(struct coda_ctx *ctx, int tab_num,
int *ehufsi, int *ehufco)
{
int i, j, k, lastk, si, code, maxsymbol;
const u8 *bits, *huffval;
struct {
int size[256 ];
int code[256 ];
} *huff;
static const unsigned char *huff_tabs[4 ] = {
luma_dc, luma_ac, chroma_dc, chroma_ac,
};
int ret = -EINVAL;
huff = kzalloc(sizeof (*huff), GFP_KERNEL);
if (!huff)
return -ENOMEM;
bits = huff_tabs[tab_num];
huffval = huff_tabs[tab_num] + 16 ;
maxsymbol = tab_num & 1 ? 256 : 16 ;
/* Figure C.1 - Generation of table of Huffman code sizes */
k = 0 ;
for (i = 1 ; i <= 16 ; i++) {
j = bits[i - 1 ];
if (k + j > maxsymbol)
goto out;
while (j--)
huff->size[k++] = i;
}
lastk = k;
/* Figure C.2 - Generation of table of Huffman codes */
k = 0 ;
code = 0 ;
si = huff->size[0 ];
while (k < lastk) {
while (huff->size[k] == si) {
huff->code[k++] = code;
code++;
}
if (code >= (1 << si))
goto out;
code <<= 1 ;
si++;
}
/* Figure C.3 - Ordering procedure for encoding procedure code tables */
for (k = 0 ; k < lastk; k++) {
i = huffval[k];
if (i >= maxsymbol || ehufsi[i])
goto out;
ehufco[i] = huff->code[k];
ehufsi[i] = huff->size[k];
}
ret = 0 ;
out:
kfree(huff);
return ret;
}
#define DC_TABLE_INDEX0 0
#define AC_TABLE_INDEX0 1
#define DC_TABLE_INDEX1 2
#define AC_TABLE_INDEX1 3
static u8 *coda9_jpeg_get_huff_bits(struct coda_ctx *ctx, int tab_num)
{
struct coda_huff_tab *huff_tab = ctx->params.jpeg_huff_tab;
if (!huff_tab)
return NULL;
switch (tab_num) {
case DC_TABLE_INDEX0: return huff_tab->luma_dc;
case AC_TABLE_INDEX0: return huff_tab->luma_ac;
case DC_TABLE_INDEX1: return huff_tab->chroma_dc;
case AC_TABLE_INDEX1: return huff_tab->chroma_ac;
}
return NULL;
}
static int coda9_jpeg_gen_dec_huff_tab(struct coda_ctx *ctx, int tab_num)
{
int ptr_cnt = 0 , huff_code = 0 , zero_flag = 0 , data_flag = 0 ;
u8 *huff_bits;
s16 *huff_max;
s16 *huff_min;
s8 *huff_ptr;
int ofs;
int i;
huff_bits = coda9_jpeg_get_huff_bits(ctx, tab_num);
if (!huff_bits)
return -EINVAL;
/* DC/AC Luma, DC/AC Chroma -> DC Luma/Chroma, AC Luma/Chroma */
ofs = ((tab_num & 1 ) << 1 ) | ((tab_num >> 1 ) & 1 );
ofs *= 16 ;
huff_ptr = ctx->params.jpeg_huff_tab->ptr + ofs;
huff_max = ctx->params.jpeg_huff_tab->max + ofs;
huff_min = ctx->params.jpeg_huff_tab->min + ofs;
for (i = 0 ; i < 16 ; i++) {
if (huff_bits[i]) {
huff_ptr[i] = ptr_cnt;
ptr_cnt += huff_bits[i];
huff_min[i] = huff_code;
huff_max[i] = huff_code + (huff_bits[i] - 1 );
data_flag = 1 ;
zero_flag = 0 ;
} else {
huff_ptr[i] = -1 ;
huff_min[i] = -1 ;
huff_max[i] = -1 ;
zero_flag = 1 ;
}
if (data_flag == 1 ) {
if (zero_flag == 1 )
huff_code <<= 1 ;
else
huff_code = (huff_max[i] + 1 ) << 1 ;
}
}
return 0 ;
}
static int coda9_jpeg_load_huff_tab(struct coda_ctx *ctx)
{
struct {
int size[4 ][256 ];
int code[4 ][256 ];
} *huff;
u32 *huff_data;
int i, j;
int ret;
huff = kzalloc(sizeof (*huff), GFP_KERNEL);
if (!huff)
return -ENOMEM;
/* Generate all four (luma/chroma DC/AC) code/size lookup tables */
for (i = 0 ; i < 4 ; i++) {
ret = coda9_jpeg_gen_enc_huff_tab(ctx, i, huff->size[i],
huff->code[i]);
if (ret)
goto out;
}
if (!ctx->params.jpeg_huff_data) {
ctx->params.jpeg_huff_data =
kzalloc(sizeof (u32) * CODA9_JPEG_ENC_HUFF_DATA_SIZE,
GFP_KERNEL);
if (!ctx->params.jpeg_huff_data) {
ret = -ENOMEM;
goto out;
}
}
huff_data = ctx->params.jpeg_huff_data;
for (j = 0 ; j < 4 ; j++) {
/* Store Huffman lookup tables in AC0, AC1, DC0, DC1 order */
int t = (j == 0 ) ? AC_TABLE_INDEX0 :
(j == 1 ) ? AC_TABLE_INDEX1 :
(j == 2 ) ? DC_TABLE_INDEX0 :
DC_TABLE_INDEX1;
/* DC tables only have 16 entries */
int len = (j < 2 ) ? 256 : 16 ;
for (i = 0 ; i < len; i++) {
if (huff->size[t][i] == 0 && huff->code[t][i] == 0 )
*(huff_data++) = 0 ;
else
*(huff_data++) =
((huff->size[t][i] - 1 ) << 16 ) |
huff->code[t][i];
}
}
ret = 0 ;
out:
kfree(huff);
return ret;
}
static void coda9_jpeg_write_huff_tab(struct coda_ctx *ctx)
{
struct coda_dev *dev = ctx->dev;
u32 *huff_data = ctx->params.jpeg_huff_data;
int i;
/* Write Huffman size/code lookup tables in AC0, AC1, DC0, DC1 order */
coda_write(dev, 0 x3, CODA9_REG_JPEG_HUFF_CTRL);
for (i = 0 ; i < CODA9_JPEG_ENC_HUFF_DATA_SIZE; i++)
coda_write(dev, *(huff_data++), CODA9_REG_JPEG_HUFF_DATA);
coda_write(dev, 0 x0, CODA9_REG_JPEG_HUFF_CTRL);
}
static inline void coda9_jpeg_write_qmat_quotients(struct coda_dev *dev,
u8 *qmat, int index)
{
int i;
coda_write(dev, index | 0 x3, CODA9_REG_JPEG_QMAT_CTRL);
for (i = 0 ; i < 64 ; i++)
coda_write(dev, 0 x80000 / qmat[i], CODA9_REG_JPEG_QMAT_DATA);
coda_write(dev, index, CODA9_REG_JPEG_QMAT_CTRL);
}
static void coda9_jpeg_load_qmat_tab(struct coda_ctx *ctx)
{
struct coda_dev *dev = ctx->dev;
u8 *luma_tab;
u8 *chroma_tab;
luma_tab = ctx->params.jpeg_qmat_tab[0 ];
if (!luma_tab)
luma_tab = luma_q;
chroma_tab = ctx->params.jpeg_qmat_tab[1 ];
if (!chroma_tab)
chroma_tab = chroma_q;
coda9_jpeg_write_qmat_quotients(dev, luma_tab, 0 x00);
coda9_jpeg_write_qmat_quotients(dev, chroma_tab, 0 x40);
coda9_jpeg_write_qmat_quotients(dev, chroma_tab, 0 x80);
}
struct coda_jpeg_stream {
u8 *curr;
u8 *end;
};
static inline int coda_jpeg_put_byte(u8 byte, struct coda_jpeg_stream *stream)
{
if (stream->curr >= stream->end)
return -EINVAL;
*stream->curr++ = byte;
return 0 ;
}
static inline int coda_jpeg_put_word(u16 word, struct coda_jpeg_stream *stream)
{
if (stream->curr + sizeof (__be16) > stream->end)
return -EINVAL;
put_unaligned_be16(word, stream->curr);
stream->curr += sizeof (__be16);
return 0 ;
}
static int coda_jpeg_put_table(u16 marker, u8 index, const u8 *table,
size_t len, struct coda_jpeg_stream *stream)
{
int i, ret;
ret = coda_jpeg_put_word(marker, stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_put_word(3 + len, stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_put_byte(index, stream);
for (i = 0 ; i < len && ret == 0 ; i++)
ret = coda_jpeg_put_byte(table[i], stream);
return ret;
}
static int coda_jpeg_define_quantization_table(struct coda_ctx *ctx, u8 index,
struct coda_jpeg_stream *stream)
{
return coda_jpeg_put_table(DQT_MARKER, index,
ctx->params.jpeg_qmat_tab[index], 64 ,
stream);
}
static int coda_jpeg_define_huffman_table(u8 index, const u8 *table, size_t len,
struct coda_jpeg_stream *stream)
{
return coda_jpeg_put_table(DHT_MARKER, index, table, len, stream);
}
static int coda9_jpeg_encode_header(struct coda_ctx *ctx, int len, u8 *buf)
{
struct coda_jpeg_stream stream = { buf, buf + len };
struct coda_q_data *q_data_src;
int chroma_format, comp_num;
int i, ret, pad;
q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
chroma_format = coda9_jpeg_chroma_format(q_data_src->fourcc);
if (chroma_format < 0 )
return 0 ;
/* Start Of Image */
ret = coda_jpeg_put_word(SOI_MARKER, &stream);
if (ret < 0 )
return ret;
/* Define Restart Interval */
if (ctx->params.jpeg_restart_interval) {
ret = coda_jpeg_put_word(DRI_MARKER, &stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_put_word(4 , &stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_put_word(ctx->params.jpeg_restart_interval,
&stream);
if (ret < 0 )
return ret;
}
/* Define Quantization Tables */
ret = coda_jpeg_define_quantization_table(ctx, 0 x00, &stream);
if (ret < 0 )
return ret;
if (chroma_format != CODA9_JPEG_FORMAT_400) {
ret = coda_jpeg_define_quantization_table(ctx, 0 x01, &stream);
if (ret < 0 )
return ret;
}
/* Define Huffman Tables */
ret = coda_jpeg_define_huffman_table(0 x00, luma_dc, 16 + 12 , &stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_define_huffman_table(0 x10, luma_ac, 16 + 162 , &stream);
if (ret < 0 )
return ret;
if (chroma_format != CODA9_JPEG_FORMAT_400) {
ret = coda_jpeg_define_huffman_table(0 x01, chroma_dc, 16 + 12 ,
&stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_define_huffman_table(0 x11, chroma_ac, 16 + 162 ,
&stream);
if (ret < 0 )
return ret;
}
/* Start Of Frame */
ret = coda_jpeg_put_word(SOF_MARKER, &stream);
if (ret < 0 )
return ret;
comp_num = (chroma_format == CODA9_JPEG_FORMAT_400) ? 1 : 3 ;
ret = coda_jpeg_put_word(8 + comp_num * 3 , &stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_put_byte(0 x08, &stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_put_word(q_data_src->height, &stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_put_word(q_data_src->width, &stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_put_byte(comp_num, &stream);
if (ret < 0 )
return ret;
for (i = 0 ; i < comp_num; i++) {
static unsigned char subsampling[5 ][3 ] = {
[CODA9_JPEG_FORMAT_420] = { 0 x22, 0 x11, 0 x11 },
[CODA9_JPEG_FORMAT_422] = { 0 x21, 0 x11, 0 x11 },
[CODA9_JPEG_FORMAT_224] = { 0 x12, 0 x11, 0 x11 },
[CODA9_JPEG_FORMAT_444] = { 0 x11, 0 x11, 0 x11 },
[CODA9_JPEG_FORMAT_400] = { 0 x11 },
};
/* Component identifier, matches SOS */
ret = coda_jpeg_put_byte(i + 1 , &stream);
if (ret < 0 )
return ret;
ret = coda_jpeg_put_byte(subsampling[chroma_format][i],
&stream);
if (ret < 0 )
return ret;
/* Chroma table index */
ret = coda_jpeg_put_byte((i == 0 ) ? 0 : 1 , &stream);
if (ret < 0 )
return ret;
}
/* Pad to multiple of 8 bytes */
pad = (stream.curr - buf) % 8 ;
if (pad) {
pad = 8 - pad;
while (pad--) {
ret = coda_jpeg_put_byte(0 x00, &stream);
if (ret < 0 )
return ret;
}
}
return stream.curr - buf;
}
/*
* Scale quantization table using nonlinear scaling factor
* u8 qtab [ 64 ] , scale [ 50 , 190 ]
*/
static void coda_scale_quant_table(u8 *q_tab, int scale)
{
unsigned int temp;
int i;
for (i = 0 ; i < 64 ; i++) {
temp = DIV_ROUND_CLOSEST((unsigned int )q_tab[i] * scale, 100 );
if (temp <= 0 )
temp = 1 ;
if (temp > 255 )
temp = 255 ;
q_tab[i] = (unsigned char )temp;
}
}
void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality)
{
unsigned int scale;
ctx->params.jpeg_quality = quality;
/* Clip quality setting to [5,100] interval */
if (quality > 100 )
quality = 100 ;
if (quality < 5 )
quality = 5 ;
/*
* Non - linear scaling factor :
* [ 5 , 50 ] - > [ 1000 . . 100 ] , [ 51 , 100 ] - > [ 98 . . 0 ]
*/
if (quality < 50 )
scale = 5000 / quality;
else
scale = 200 - 2 * quality;
if (ctx->params.jpeg_qmat_tab[0 ]) {
memcpy(ctx->params.jpeg_qmat_tab[0 ], luma_q, 64 );
coda_scale_quant_table(ctx->params.jpeg_qmat_tab[0 ], scale);
}
if (ctx->params.jpeg_qmat_tab[1 ]) {
memcpy(ctx->params.jpeg_qmat_tab[1 ], chroma_q, 64 );
coda_scale_quant_table(ctx->params.jpeg_qmat_tab[1 ], scale);
}
}
/*
* Encoder context operations
*/
static int coda9_jpeg_start_encoding(struct coda_ctx *ctx)
{
struct coda_dev *dev = ctx->dev;
int ret;
ret = coda9_jpeg_load_huff_tab(ctx);
if (ret < 0 ) {
v4l2_err(&dev->v4l2_dev, "error loading Huffman tables\n" );
return ret;
}
if (!ctx->params.jpeg_qmat_tab[0 ]) {
ctx->params.jpeg_qmat_tab[0 ] = kmalloc(64 , GFP_KERNEL);
if (!ctx->params.jpeg_qmat_tab[0 ])
return -ENOMEM;
}
if (!ctx->params.jpeg_qmat_tab[1 ]) {
ctx->params.jpeg_qmat_tab[1 ] = kmalloc(64 , GFP_KERNEL);
if (!ctx->params.jpeg_qmat_tab[1 ])
return -ENOMEM;
}
coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
return 0 ;
}
static int coda9_jpeg_prepare_encode(struct coda_ctx *ctx)
{
struct coda_q_data *q_data_src;
struct vb2_v4l2_buffer *src_buf, *dst_buf;
struct coda_dev *dev = ctx->dev;
u32 start_addr, end_addr;
u16 aligned_width, aligned_height;
bool chroma_interleave;
int chroma_format;
int header_len;
int ret;
ktime_t timeout;
src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
if (vb2_get_plane_payload(&src_buf->vb2_buf, 0 ) == 0 )
vb2_set_plane_payload(&src_buf->vb2_buf, 0 ,
vb2_plane_size(&src_buf->vb2_buf, 0 ));
src_buf->sequence = ctx->osequence;
dst_buf->sequence = ctx->osequence;
ctx->osequence++;
src_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
src_buf->flags &= ~V4L2_BUF_FLAG_PFRAME;
coda_set_gdi_regs(ctx);
start_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0 );
end_addr = start_addr + vb2_plane_size(&dst_buf->vb2_buf, 0 );
chroma_format = coda9_jpeg_chroma_format(q_data_src->fourcc);
if (chroma_format < 0 )
return chroma_format;
/* Round image dimensions to multiple of MCU size */
aligned_width = round_up(q_data_src->width, width_align[chroma_format]);
aligned_height = round_up(q_data_src->height,
height_align[chroma_format]);
if (aligned_width != q_data_src->bytesperline) {
v4l2_err(&dev->v4l2_dev, "wrong stride: %d instead of %d\n" ,
aligned_width, q_data_src->bytesperline);
}
header_len =
coda9_jpeg_encode_header(ctx,
vb2_plane_size(&dst_buf->vb2_buf, 0 ),
vb2_plane_vaddr(&dst_buf->vb2_buf, 0 ));
if (header_len < 0 )
return header_len;
coda_write(dev, start_addr + header_len, CODA9_REG_JPEG_BBC_BAS_ADDR);
coda_write(dev, end_addr, CODA9_REG_JPEG_BBC_END_ADDR);
coda_write(dev, start_addr + header_len, CODA9_REG_JPEG_BBC_WR_PTR);
coda_write(dev, start_addr + header_len, CODA9_REG_JPEG_BBC_RD_PTR);
coda_write(dev, 0 , CODA9_REG_JPEG_BBC_CUR_POS);
/* 64 words per 256-byte page */
coda_write(dev, 64 , CODA9_REG_JPEG_BBC_DATA_CNT);
coda_write(dev, start_addr, CODA9_REG_JPEG_BBC_EXT_ADDR);
coda_write(dev, 0 , CODA9_REG_JPEG_BBC_INT_ADDR);
coda_write(dev, 0 , CODA9_REG_JPEG_GBU_BT_PTR);
coda_write(dev, 0 , CODA9_REG_JPEG_GBU_WD_PTR);
coda_write(dev, 0 , CODA9_REG_JPEG_GBU_BBSR);
coda_write(dev, BIT(31 ) | ((end_addr - start_addr - header_len) / 256 ),
CODA9_REG_JPEG_BBC_STRM_CTRL);
coda_write(dev, 0 , CODA9_REG_JPEG_GBU_CTRL);
coda_write(dev, 0 , CODA9_REG_JPEG_GBU_FF_RPTR);
coda_write(dev, 127 , CODA9_REG_JPEG_GBU_BBER);
coda_write(dev, 64 , CODA9_REG_JPEG_GBU_BBIR);
coda_write(dev, 64 , CODA9_REG_JPEG_GBU_BBHR);
chroma_interleave = (q_data_src->fourcc == V4L2_PIX_FMT_NV12);
coda_write(dev, CODA9_JPEG_PIC_CTRL_TC_DIRECTION |
CODA9_JPEG_PIC_CTRL_ENCODER_EN, CODA9_REG_JPEG_PIC_CTRL);
coda_write(dev, 0 , CODA9_REG_JPEG_SCL_INFO);
coda_write(dev, chroma_interleave, CODA9_REG_JPEG_DPB_CONFIG);
coda_write(dev, ctx->params.jpeg_restart_interval,
CODA9_REG_JPEG_RST_INTVAL);
coda_write(dev, 1 , CODA9_REG_JPEG_BBC_CTRL);
coda_write(dev, bus_req_num[chroma_format], CODA9_REG_JPEG_OP_INFO);
coda9_jpeg_write_huff_tab(ctx);
coda9_jpeg_load_qmat_tab(ctx);
if (ctx->params.rot_mode & CODA_ROT_90) {
aligned_width = aligned_height;
aligned_height = q_data_src->bytesperline;
if (chroma_format == CODA9_JPEG_FORMAT_422)
chroma_format = CODA9_JPEG_FORMAT_224;
else if (chroma_format == CODA9_JPEG_FORMAT_224)
chroma_format = CODA9_JPEG_FORMAT_422;
}
/* These need to be multiples of MCU size */
coda_write(dev, aligned_width << 16 | aligned_height,
CODA9_REG_JPEG_PIC_SIZE);
coda_write(dev, ctx->params.rot_mode ?
(CODA_ROT_MIR_ENABLE | ctx->params.rot_mode) : 0 ,
CODA9_REG_JPEG_ROT_INFO);
coda_write(dev, mcu_info[chroma_format], CODA9_REG_JPEG_MCU_INFO);
coda_write(dev, 1 , CODA9_GDI_CONTROL);
timeout = ktime_add_us(ktime_get(), 100000 );
do {
ret = coda_read(dev, CODA9_GDI_STATUS);
if (ktime_compare(ktime_get(), timeout) > 0 ) {
v4l2_err(&dev->v4l2_dev, "timeout waiting for GDI\n" );
return -ETIMEDOUT;
}
} while (!ret);
coda_write(dev, (chroma_format << 17 ) | (chroma_interleave << 16 ) |
q_data_src->bytesperline, CODA9_GDI_INFO_CONTROL);
/* The content of this register seems to be irrelevant: */
coda_write(dev, aligned_width << 16 | aligned_height,
CODA9_GDI_INFO_PIC_SIZE);
coda_write_base(ctx, q_data_src, src_buf, CODA9_GDI_INFO_BASE_Y);
coda_write(dev, 0 , CODA9_REG_JPEG_DPB_BASE00);
coda_write(dev, 0 , CODA9_GDI_CONTROL);
coda_write(dev, 1 , CODA9_GDI_PIC_INIT_HOST);
coda_write(dev, 1 , CODA9_GDI_WPROT_ERR_CLR);
coda_write(dev, 0 , CODA9_GDI_WPROT_RGN_EN);
trace_coda_jpeg_run(ctx, src_buf);
coda_write(dev, 1 , CODA9_REG_JPEG_PIC_START);
return 0 ;
}
static void coda9_jpeg_finish_encode(struct coda_ctx *ctx)
{
struct vb2_v4l2_buffer *src_buf, *dst_buf;
struct coda_dev *dev = ctx->dev;
u32 wr_ptr, start_ptr;
u32 err_mb;
if (ctx->aborting) {
coda_write(ctx->dev, 0 , CODA9_REG_JPEG_BBC_FLUSH_CMD);
return ;
}
/*
* Lock to make sure that an encoder stop command running in parallel
* will either already have marked src_buf as last , or it will wake up
* the capture queue after the buffers are returned .
*/
mutex_lock(&ctx->wakeup_mutex);
src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
trace_coda_jpeg_done(ctx, dst_buf);
/*
* Set plane payload to the number of bytes written out
* by the JPEG processing unit
*/
start_ptr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0 );
wr_ptr = coda_read(dev, CODA9_REG_JPEG_BBC_WR_PTR);
vb2_set_plane_payload(&dst_buf->vb2_buf, 0 , wr_ptr - start_ptr);
err_mb = coda_read(dev, CODA9_REG_JPEG_PIC_ERRMB);
if (err_mb)
coda_dbg(1 , ctx, "ERRMB: 0x%x\n" , err_mb);
coda_write(dev, 0 , CODA9_REG_JPEG_BBC_FLUSH_CMD);
dst_buf->flags &= ~(V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_LAST);
dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
dst_buf->flags |= src_buf->flags & V4L2_BUF_FLAG_LAST;
v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false );
v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
coda_m2m_buf_done(ctx, dst_buf, err_mb ? VB2_BUF_STATE_ERROR :
VB2_BUF_STATE_DONE);
mutex_unlock(&ctx->wakeup_mutex);
coda_dbg(1 , ctx, "job finished: encoded frame (%u)%s\n" ,
dst_buf->sequence,
(dst_buf->flags & V4L2_BUF_FLAG_LAST) ? " (last)" : "" );
/*
* Reset JPEG processing unit after each encode run to work
* around hangups when switching context between encoder and
* decoder .
*/
coda_hw_reset(ctx);
}
static void coda9_jpeg_encode_timeout(struct coda_ctx *ctx)
{
struct coda_dev *dev = ctx->dev;
u32 end_addr, wr_ptr;
/* Handle missing BBC overflow interrupt via timeout */
end_addr = coda_read(dev, CODA9_REG_JPEG_BBC_END_ADDR);
wr_ptr = coda_read(dev, CODA9_REG_JPEG_BBC_WR_PTR);
if (wr_ptr >= end_addr - 256 ) {
v4l2_err(&dev->v4l2_dev, "JPEG too large for capture buffer\n" );
coda9_jpeg_finish_encode(ctx);
return ;
}
coda_hw_reset(ctx);
}
static void coda9_jpeg_release(struct coda_ctx *ctx)
{
int i;
if (ctx->params.jpeg_qmat_tab[0 ] == luma_q)
ctx->params.jpeg_qmat_tab[0 ] = NULL;
if (ctx->params.jpeg_qmat_tab[1 ] == chroma_q)
ctx->params.jpeg_qmat_tab[1 ] = NULL;
for (i = 0 ; i < 3 ; i++)
kfree(ctx->params.jpeg_qmat_tab[i]);
kfree(ctx->params.jpeg_huff_data);
kfree(ctx->params.jpeg_huff_tab);
}
const struct coda_context_ops coda9_jpeg_encode_ops = {
.queue_init = coda_encoder_queue_init,
.start_streaming = coda9_jpeg_start_encoding,
.prepare_run = coda9_jpeg_prepare_encode,
.finish_run = coda9_jpeg_finish_encode,
.run_timeout = coda9_jpeg_encode_timeout,
.release = coda9_jpeg_release,
};
/*
* Decoder context operations
*/
static int coda9_jpeg_start_decoding(struct coda_ctx *ctx)
{
ctx->params.jpeg_qmat_index[0 ] = 0 ;
ctx->params.jpeg_qmat_index[1 ] = 1 ;
ctx->params.jpeg_qmat_index[2 ] = 1 ;
ctx->params.jpeg_qmat_tab[0 ] = luma_q;
ctx->params.jpeg_qmat_tab[1 ] = chroma_q;
/* nothing more to do here */
/* TODO: we could already scan the first header to get the chroma
* format .
*/
return 0 ;
}
static int coda9_jpeg_prepare_decode(struct coda_ctx *ctx)
{
struct coda_dev *dev = ctx->dev;
int aligned_width, aligned_height;
int chroma_format;
int ret;
u32 val, dst_fourcc;
struct coda_q_data *q_data_src, *q_data_dst;
struct vb2_v4l2_buffer *src_buf, *dst_buf;
int chroma_interleave;
int scl_hor_mode, scl_ver_mode;
src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
dst_fourcc = q_data_dst->fourcc;
scl_hor_mode = coda_jpeg_scale(q_data_src->width, q_data_dst->width);
scl_ver_mode = coda_jpeg_scale(q_data_src->height, q_data_dst->height);
if (vb2_get_plane_payload(&src_buf->vb2_buf, 0 ) == 0 )
vb2_set_plane_payload(&src_buf->vb2_buf, 0 ,
vb2_plane_size(&src_buf->vb2_buf, 0 ));
chroma_format = coda9_jpeg_chroma_format(q_data_dst->fourcc);
if (chroma_format < 0 )
return chroma_format;
ret = coda_jpeg_decode_header(ctx, &src_buf->vb2_buf);
if (ret < 0 ) {
src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
return ret;
}
/* Round image dimensions to multiple of MCU size */
aligned_width = round_up(q_data_src->width, width_align[chroma_format]);
aligned_height = round_up(q_data_src->height, height_align[chroma_format]);
if (aligned_width != q_data_dst->bytesperline) {
v4l2_err(&dev->v4l2_dev, "stride mismatch: %d != %d\n" ,
aligned_width, q_data_dst->bytesperline);
}
coda_set_gdi_regs(ctx);
val = ctx->params.jpeg_huff_ac_index[0 ] << 12 |
ctx->params.jpeg_huff_ac_index[1 ] << 11 |
ctx->params.jpeg_huff_ac_index[2 ] << 10 |
ctx->params.jpeg_huff_dc_index[0 ] << 9 |
ctx->params.jpeg_huff_dc_index[1 ] << 8 |
ctx->params.jpeg_huff_dc_index[2 ] << 7 ;
if (ctx->params.jpeg_huff_tab)
val |= CODA9_JPEG_PIC_CTRL_USER_HUFFMAN_EN;
coda_write(dev, val, CODA9_REG_JPEG_PIC_CTRL);
coda_write(dev, aligned_width << 16 | aligned_height,
CODA9_REG_JPEG_PIC_SIZE);
chroma_interleave = (dst_fourcc == V4L2_PIX_FMT_NV12);
coda_write(dev, 0 , CODA9_REG_JPEG_ROT_INFO);
coda_write(dev, bus_req_num[chroma_format], CODA9_REG_JPEG_OP_INFO);
coda_write(dev, mcu_info[chroma_format], CODA9_REG_JPEG_MCU_INFO);
if (scl_hor_mode || scl_ver_mode)
val = CODA9_JPEG_SCL_ENABLE | (scl_hor_mode << 2 ) | scl_ver_mode;
else
val = 0 ;
coda_write(dev, val, CODA9_REG_JPEG_SCL_INFO);
coda_write(dev, chroma_interleave, CODA9_REG_JPEG_DPB_CONFIG);
coda_write(dev, ctx->params.jpeg_restart_interval,
CODA9_REG_JPEG_RST_INTVAL);
if (ctx->params.jpeg_huff_tab)
coda9_jpeg_dec_huff_setup(ctx);
coda9_jpeg_qmat_setup(ctx);
coda9_jpeg_dec_bbc_gbu_setup(ctx, &src_buf->vb2_buf,
ctx->jpeg_ecs_offset);
coda_write(dev, 0 , CODA9_REG_JPEG_RST_INDEX);
coda_write(dev, 0 , CODA9_REG_JPEG_RST_COUNT);
coda_write(dev, 0 , CODA9_REG_JPEG_DPCM_DIFF_Y);
coda_write(dev, 0 , CODA9_REG_JPEG_DPCM_DIFF_CB);
coda_write(dev, 0 , CODA9_REG_JPEG_DPCM_DIFF_CR);
coda_write(dev, 0 , CODA9_REG_JPEG_ROT_INFO);
coda_write(dev, 1 , CODA9_GDI_CONTROL);
do {
ret = coda_read(dev, CODA9_GDI_STATUS);
} while (!ret);
val = (chroma_format << 17 ) | (chroma_interleave << 16 ) |
q_data_dst->bytesperline;
if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
val |= 3 << 20 ;
coda_write(dev, val, CODA9_GDI_INFO_CONTROL);
coda_write(dev, aligned_width << 16 | aligned_height,
CODA9_GDI_INFO_PIC_SIZE);
coda_write_base(ctx, q_data_dst, dst_buf, CODA9_GDI_INFO_BASE_Y);
coda_write(dev, 0 , CODA9_REG_JPEG_DPB_BASE00);
coda_write(dev, 0 , CODA9_GDI_CONTROL);
coda_write(dev, 1 , CODA9_GDI_PIC_INIT_HOST);
trace_coda_jpeg_run(ctx, src_buf);
coda_write(dev, 1 , CODA9_REG_JPEG_PIC_START);
return 0 ;
}
static void coda9_jpeg_finish_decode(struct coda_ctx *ctx)
{
struct coda_dev *dev = ctx->dev;
struct vb2_v4l2_buffer *dst_buf, *src_buf;
struct coda_q_data *q_data_dst;
u32 err_mb;
err_mb = coda_read(dev, CODA9_REG_JPEG_PIC_ERRMB);
if (err_mb)
v4l2_err(&dev->v4l2_dev, "ERRMB: 0x%x\n" , err_mb);
coda_write(dev, 0 , CODA9_REG_JPEG_BBC_FLUSH_CMD);
/*
* Lock to make sure that a decoder stop command running in parallel
* will either already have marked src_buf as last , or it will wake up
* the capture queue after the buffers are returned .
*/
mutex_lock(&ctx->wakeup_mutex);
src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
dst_buf->sequence = ctx->osequence++;
trace_coda_jpeg_done(ctx, dst_buf);
dst_buf->flags &= ~(V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_LAST);
dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
dst_buf->flags |= src_buf->flags & V4L2_BUF_FLAG_LAST;
v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false );
q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
vb2_set_plane_payload(&dst_buf->vb2_buf, 0 , q_data_dst->sizeimage);
v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
coda_m2m_buf_done(ctx, dst_buf, err_mb ? VB2_BUF_STATE_ERROR :
VB2_BUF_STATE_DONE);
mutex_unlock(&ctx->wakeup_mutex);
coda_dbg(1 , ctx, "job finished: decoded frame (%u)%s\n" ,
dst_buf->sequence,
(dst_buf->flags & V4L2_BUF_FLAG_LAST) ? " (last)" : "" );
/*
* Reset JPEG processing unit after each decode run to work
* around hangups when switching context between encoder and
* decoder .
*/
coda_hw_reset(ctx);
}
const struct coda_context_ops coda9_jpeg_decode_ops = {
.queue_init = coda_encoder_queue_init, /* non-bitstream operation */
.start_streaming = coda9_jpeg_start_decoding,
.prepare_run = coda9_jpeg_prepare_decode,
.finish_run = coda9_jpeg_finish_decode,
.release = coda9_jpeg_release,
};
irqreturn_t coda9_jpeg_irq_handler(int irq, void *data)
{
struct coda_dev *dev = data;
struct coda_ctx *ctx;
int status;
int err_mb;
status = coda_read(dev, CODA9_REG_JPEG_PIC_STATUS);
if (status == 0 )
return IRQ_HANDLED;
coda_write(dev, status, CODA9_REG_JPEG_PIC_STATUS);
if (status & CODA9_JPEG_STATUS_OVERFLOW)
v4l2_err(&dev->v4l2_dev, "JPEG overflow\n" );
if (status & CODA9_JPEG_STATUS_BBC_INT)
v4l2_err(&dev->v4l2_dev, "JPEG BBC interrupt\n" );
if (status & CODA9_JPEG_STATUS_ERROR) {
v4l2_err(&dev->v4l2_dev, "JPEG error\n" );
err_mb = coda_read(dev, CODA9_REG_JPEG_PIC_ERRMB);
if (err_mb) {
v4l2_err(&dev->v4l2_dev,
"ERRMB: 0x%x: rst idx %d, mcu pos (%d,%d)\n" ,
err_mb, err_mb >> 24 , (err_mb >> 12 ) & 0 xfff,
err_mb & 0 xfff);
}
}
ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
if (!ctx) {
v4l2_err(&dev->v4l2_dev,
"Instance released before the end of transaction\n" );
mutex_unlock(&dev->coda_mutex);
return IRQ_HANDLED;
}
complete(&ctx->completion);
return IRQ_HANDLED;
}
Messung V0.5 in Prozent C=95 H=92 G=93
¤ Dauer der Verarbeitung: 0.22 Sekunden
(vorverarbeitet am 2026-10-11)
¤
*© Formatika GbR, Deutschland