aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Zabel <p.zabel@pengutronix.de>2014-10-02 13:08:31 -0400
committerMauro Carvalho Chehab <mchehab@osg.samsung.com>2014-10-28 13:03:16 -0400
commitcb1d3a336371e35c3920cc50a701c5403c255644 (patch)
tree3648fc5a63f7d62132147cfad6eb9417759ac186
parent0ab54524f3ff6463b389cbd160c8efcf64c46865 (diff)
[media] coda: add CODA7541 JPEG support
This patch adds JPEG encoding and decoding support for CODA7541, using the BIT processor. Separate JPEG encoder and decoder video devices are created due to different streaming behaviour and different supported pixel formats. The hardware can not change subsampling on the fly, but encode and decode 4:2:2 subsampled JPEG images from and into this format. The CODA7541 JPEG decoder uses the bitstream buffer and thus can run without new buffers queued if there is a buffer in the bitstream. Since there is no standard way to store the colorspace used in JPEGs, and to make v4l2-compliance happy, the JPEG format always reports V4L2_COLORSPACE_JPEG. Signed-off-by: Lucas Stach <l.stach@pengutronix.de> Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de> Signed-off-by: Kamil Debski <k.debski@samsung.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
-rw-r--r--drivers/media/platform/coda/Makefile2
-rw-r--r--drivers/media/platform/coda/coda-bit.c103
-rw-r--r--drivers/media/platform/coda/coda-common.c112
-rw-r--r--drivers/media/platform/coda/coda-jpeg.c225
-rw-r--r--drivers/media/platform/coda/coda.h8
5 files changed, 398 insertions, 52 deletions
diff --git a/drivers/media/platform/coda/Makefile b/drivers/media/platform/coda/Makefile
index 3543291e6273..25ce15561695 100644
--- a/drivers/media/platform/coda/Makefile
+++ b/drivers/media/platform/coda/Makefile
@@ -1,3 +1,3 @@
1coda-objs := coda-common.o coda-bit.o coda-h264.o 1coda-objs := coda-common.o coda-bit.o coda-h264.o coda-jpeg.o
2 2
3obj-$(CONFIG_VIDEO_CODA) += coda.o 3obj-$(CONFIG_VIDEO_CODA) += coda.o
diff --git a/drivers/media/platform/coda/coda-bit.c b/drivers/media/platform/coda/coda-bit.c
index 746a6158ccd0..931248dc60f9 100644
--- a/drivers/media/platform/coda/coda-bit.c
+++ b/drivers/media/platform/coda/coda-bit.c
@@ -691,6 +691,7 @@ static int coda_start_encoding(struct coda_ctx *ctx)
691 struct vb2_buffer *buf; 691 struct vb2_buffer *buf;
692 int gamma, ret, value; 692 int gamma, ret, value;
693 u32 dst_fourcc; 693 u32 dst_fourcc;
694 u32 stride;
694 695
695 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 696 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
696 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 697 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
@@ -710,6 +711,14 @@ static int coda_start_encoding(struct coda_ctx *ctx)
710 return -EFAULT; 711 return -EFAULT;
711 } 712 }
712 713
714 if (dst_fourcc == V4L2_PIX_FMT_JPEG) {
715 if (!ctx->params.jpeg_qmat_tab[0])
716 ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
717 if (!ctx->params.jpeg_qmat_tab[1])
718 ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
719 coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
720 }
721
713 mutex_lock(&dev->coda_mutex); 722 mutex_lock(&dev->coda_mutex);
714 723
715 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); 724 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
@@ -765,6 +774,8 @@ static int coda_start_encoding(struct coda_ctx *ctx)
765 << CODA_PICHEIGHT_OFFSET; 774 << CODA_PICHEIGHT_OFFSET;
766 } 775 }
767 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE); 776 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
777 if (dst_fourcc == V4L2_PIX_FMT_JPEG)
778 ctx->params.framerate = 0;
768 coda_write(dev, ctx->params.framerate, 779 coda_write(dev, ctx->params.framerate,
769 CODA_CMD_ENC_SEQ_SRC_F_RATE); 780 CODA_CMD_ENC_SEQ_SRC_F_RATE);
770 781
@@ -798,6 +809,16 @@ static int coda_start_encoding(struct coda_ctx *ctx)
798 } 809 }
799 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA); 810 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
800 break; 811 break;
812 case V4L2_PIX_FMT_JPEG:
813 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_PARA);
814 coda_write(dev, ctx->params.jpeg_restart_interval,
815 CODA_CMD_ENC_SEQ_JPG_RST_INTERVAL);
816 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_EN);
817 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_SIZE);
818 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_OFFSET);
819
820 coda_jpeg_write_tables(ctx);
821 break;
801 default: 822 default:
802 v4l2_err(v4l2_dev, 823 v4l2_err(v4l2_dev,
803 "dst format (0x%08x) invalid.\n", dst_fourcc); 824 "dst format (0x%08x) invalid.\n", dst_fourcc);
@@ -805,28 +826,36 @@ static int coda_start_encoding(struct coda_ctx *ctx)
805 goto out; 826 goto out;
806 } 827 }
807 828
808 switch (ctx->params.slice_mode) { 829 /*
809 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE: 830 * slice mode and GOP size registers are used for thumb size/offset
810 value = 0; 831 * in JPEG mode
811 break; 832 */
812 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB: 833 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
813 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) 834 switch (ctx->params.slice_mode) {
814 << CODA_SLICING_SIZE_OFFSET; 835 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
815 value |= (1 & CODA_SLICING_UNIT_MASK) 836 value = 0;
816 << CODA_SLICING_UNIT_OFFSET; 837 break;
817 value |= 1 & CODA_SLICING_MODE_MASK; 838 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
818 break; 839 value = (ctx->params.slice_max_mb &
819 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES: 840 CODA_SLICING_SIZE_MASK)
820 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) 841 << CODA_SLICING_SIZE_OFFSET;
821 << CODA_SLICING_SIZE_OFFSET; 842 value |= (1 & CODA_SLICING_UNIT_MASK)
822 value |= (0 & CODA_SLICING_UNIT_MASK) 843 << CODA_SLICING_UNIT_OFFSET;
823 << CODA_SLICING_UNIT_OFFSET; 844 value |= 1 & CODA_SLICING_MODE_MASK;
824 value |= 1 & CODA_SLICING_MODE_MASK; 845 break;
825 break; 846 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
847 value = (ctx->params.slice_max_bits &
848 CODA_SLICING_SIZE_MASK)
849 << CODA_SLICING_SIZE_OFFSET;
850 value |= (0 & CODA_SLICING_UNIT_MASK)
851 << CODA_SLICING_UNIT_OFFSET;
852 value |= 1 & CODA_SLICING_MODE_MASK;
853 break;
854 }
855 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
856 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
857 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
826 } 858 }
827 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
828 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
829 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
830 859
831 if (ctx->params.bitrate) { 860 if (ctx->params.bitrate) {
832 /* Rate control enabled */ 861 /* Rate control enabled */
@@ -917,19 +946,24 @@ static int coda_start_encoding(struct coda_ctx *ctx)
917 goto out; 946 goto out;
918 } 947 }
919 948
920 if (dev->devtype->product == CODA_960) 949 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
921 ctx->num_internal_frames = 4; 950 if (dev->devtype->product == CODA_960)
922 else 951 ctx->num_internal_frames = 4;
923 ctx->num_internal_frames = 2; 952 else
924 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc); 953 ctx->num_internal_frames = 2;
925 if (ret < 0) { 954 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
926 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n"); 955 if (ret < 0) {
927 goto out; 956 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
957 goto out;
958 }
959 stride = q_data_src->bytesperline;
960 } else {
961 ctx->num_internal_frames = 0;
962 stride = 0;
928 } 963 }
929
930 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM); 964 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
931 coda_write(dev, q_data_src->bytesperline, 965 coda_write(dev, stride, CODA_CMD_SET_FRAME_BUF_STRIDE);
932 CODA_CMD_SET_FRAME_BUF_STRIDE); 966
933 if (dev->devtype->product == CODA_7541) { 967 if (dev->devtype->product == CODA_7541) {
934 coda_write(dev, q_data_src->bytesperline, 968 coda_write(dev, q_data_src->bytesperline,
935 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE); 969 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
@@ -1104,6 +1138,9 @@ static int coda_prepare_encode(struct coda_ctx *ctx)
1104 case V4L2_PIX_FMT_MPEG4: 1138 case V4L2_PIX_FMT_MPEG4:
1105 quant_param = ctx->params.mpeg4_intra_qp; 1139 quant_param = ctx->params.mpeg4_intra_qp;
1106 break; 1140 break;
1141 case V4L2_PIX_FMT_JPEG:
1142 quant_param = 30;
1143 break;
1107 default: 1144 default:
1108 v4l2_warn(&ctx->dev->v4l2_dev, 1145 v4l2_warn(&ctx->dev->v4l2_dev,
1109 "cannot set intra qp, fmt not supported\n"); 1146 "cannot set intra qp, fmt not supported\n");
@@ -1315,6 +1352,8 @@ static int __coda_start_decoding(struct coda_ctx *ctx)
1315 if ((dev->devtype->product == CODA_7541) || 1352 if ((dev->devtype->product == CODA_7541) ||
1316 (dev->devtype->product == CODA_960)) 1353 (dev->devtype->product == CODA_960))
1317 val |= CODA_REORDER_ENABLE; 1354 val |= CODA_REORDER_ENABLE;
1355 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG)
1356 val |= CODA_NO_INT_ENABLE;
1318 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION); 1357 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1319 1358
1320 ctx->params.codec_mode = ctx->codec->mode; 1359 ctx->params.codec_mode = ctx->codec->mode;
diff --git a/drivers/media/platform/coda/coda-common.c b/drivers/media/platform/coda/coda-common.c
index 7fc0dc06298f..76b80d27a135 100644
--- a/drivers/media/platform/coda/coda-common.c
+++ b/drivers/media/platform/coda/coda-common.c
@@ -139,6 +139,10 @@ static const struct coda_fmt coda_formats[] = {
139 .name = "MPEG4 Encoded Stream", 139 .name = "MPEG4 Encoded Stream",
140 .fourcc = V4L2_PIX_FMT_MPEG4, 140 .fourcc = V4L2_PIX_FMT_MPEG4,
141 }, 141 },
142 {
143 .name = "JPEG Encoded Images",
144 .fourcc = V4L2_PIX_FMT_JPEG,
145 },
142}; 146};
143 147
144#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \ 148#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
@@ -159,8 +163,10 @@ static const struct coda_codec codadx6_codecs[] = {
159static const struct coda_codec coda7_codecs[] = { 163static const struct coda_codec coda7_codecs[] = {
160 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720), 164 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
161 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720), 165 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
166 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
162 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088), 167 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
163 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088), 168 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
169 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
164}; 170};
165 171
166static const struct coda_codec coda9_codecs[] = { 172static const struct coda_codec coda9_codecs[] = {
@@ -193,6 +199,21 @@ static const struct coda_video_device coda_bit_encoder = {
193 }, 199 },
194}; 200};
195 201
202static const struct coda_video_device coda_bit_jpeg_encoder = {
203 .name = "coda-jpeg-encoder",
204 .type = CODA_INST_ENCODER,
205 .ops = &coda_bit_encode_ops,
206 .src_formats = {
207 V4L2_PIX_FMT_YUV420,
208 V4L2_PIX_FMT_YVU420,
209 V4L2_PIX_FMT_NV12,
210 V4L2_PIX_FMT_YUV422P,
211 },
212 .dst_formats = {
213 V4L2_PIX_FMT_JPEG,
214 },
215};
216
196static const struct coda_video_device coda_bit_decoder = { 217static const struct coda_video_device coda_bit_decoder = {
197 .name = "coda-decoder", 218 .name = "coda-decoder",
198 .type = CODA_INST_DECODER, 219 .type = CODA_INST_DECODER,
@@ -208,11 +229,28 @@ static const struct coda_video_device coda_bit_decoder = {
208 }, 229 },
209}; 230};
210 231
232static const struct coda_video_device coda_bit_jpeg_decoder = {
233 .name = "coda-jpeg-decoder",
234 .type = CODA_INST_DECODER,
235 .ops = &coda_bit_decode_ops,
236 .src_formats = {
237 V4L2_PIX_FMT_JPEG,
238 },
239 .dst_formats = {
240 V4L2_PIX_FMT_YUV420,
241 V4L2_PIX_FMT_YVU420,
242 V4L2_PIX_FMT_NV12,
243 V4L2_PIX_FMT_YUV422P,
244 },
245};
246
211static const struct coda_video_device *codadx6_video_devices[] = { 247static const struct coda_video_device *codadx6_video_devices[] = {
212 &coda_bit_encoder, 248 &coda_bit_encoder,
213}; 249};
214 250
215static const struct coda_video_device *coda7_video_devices[] = { 251static const struct coda_video_device *coda7_video_devices[] = {
252 &coda_bit_jpeg_encoder,
253 &coda_bit_jpeg_decoder,
216 &coda_bit_encoder, 254 &coda_bit_encoder,
217 &coda_bit_decoder, 255 &coda_bit_decoder,
218}; 256};
@@ -395,7 +433,10 @@ static int coda_g_fmt(struct file *file, void *priv,
395 f->fmt.pix.bytesperline = q_data->bytesperline; 433 f->fmt.pix.bytesperline = q_data->bytesperline;
396 434
397 f->fmt.pix.sizeimage = q_data->sizeimage; 435 f->fmt.pix.sizeimage = q_data->sizeimage;
398 f->fmt.pix.colorspace = ctx->colorspace; 436 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
437 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
438 else
439 f->fmt.pix.colorspace = ctx->colorspace;
399 440
400 return 0; 441 return 0;
401} 442}
@@ -453,7 +494,10 @@ static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
453 case V4L2_PIX_FMT_YUV420: 494 case V4L2_PIX_FMT_YUV420:
454 case V4L2_PIX_FMT_YVU420: 495 case V4L2_PIX_FMT_YVU420:
455 case V4L2_PIX_FMT_NV12: 496 case V4L2_PIX_FMT_NV12:
456 /* Frame stride must be multiple of 8, but 16 for h.264 */ 497 /*
498 * Frame stride must be at least multiple of 8,
499 * but multiple of 16 for h.264 or JPEG 4:2:x
500 */
457 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16); 501 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
458 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * 502 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
459 f->fmt.pix.height * 3 / 2; 503 f->fmt.pix.height * 3 / 2;
@@ -463,9 +507,11 @@ static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
463 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * 507 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
464 f->fmt.pix.height * 2; 508 f->fmt.pix.height * 2;
465 break; 509 break;
510 case V4L2_PIX_FMT_JPEG:
511 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
512 /* fallthrough */
466 case V4L2_PIX_FMT_H264: 513 case V4L2_PIX_FMT_H264:
467 case V4L2_PIX_FMT_MPEG4: 514 case V4L2_PIX_FMT_MPEG4:
468 case V4L2_PIX_FMT_JPEG:
469 f->fmt.pix.bytesperline = 0; 515 f->fmt.pix.bytesperline = 0;
470 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE; 516 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
471 break; 517 break;
@@ -538,8 +584,12 @@ static int coda_try_fmt_vid_out(struct file *file, void *priv,
538 if (ret < 0) 584 if (ret < 0)
539 return ret; 585 return ret;
540 586
541 if (!f->fmt.pix.colorspace) 587 if (!f->fmt.pix.colorspace) {
542 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; 588 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
589 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
590 else
591 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
592 }
543 593
544 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 594 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
545 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc); 595 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
@@ -883,6 +933,7 @@ static int coda_job_ready(void *m2m_priv)
883 933
884 if (ctx->hold || 934 if (ctx->hold ||
885 ((ctx->inst_type == CODA_INST_DECODER) && 935 ((ctx->inst_type == CODA_INST_DECODER) &&
936 !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
886 (coda_get_bitstream_payload(ctx) < 512) && 937 (coda_get_bitstream_payload(ctx) < 512) &&
887 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) { 938 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
888 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 939 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
@@ -1057,7 +1108,7 @@ static void coda_buf_queue(struct vb2_buffer *vb)
1057 * In the decoder case, immediately try to copy the buffer into the 1108 * In the decoder case, immediately try to copy the buffer into the
1058 * bitstream ringbuffer and mark it as ready to be dequeued. 1109 * bitstream ringbuffer and mark it as ready to be dequeued.
1059 */ 1110 */
1060 if (q_data->fourcc == V4L2_PIX_FMT_H264 && 1111 if (ctx->inst_type == CODA_INST_DECODER &&
1061 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1112 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1062 /* 1113 /*
1063 * For backwards compatibility, queuing an empty buffer marks 1114 * For backwards compatibility, queuing an empty buffer marks
@@ -1120,12 +1171,13 @@ static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1120 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev; 1171 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1121 struct coda_q_data *q_data_src, *q_data_dst; 1172 struct coda_q_data *q_data_src, *q_data_dst;
1122 struct vb2_buffer *buf; 1173 struct vb2_buffer *buf;
1123 u32 dst_fourcc;
1124 int ret = 0; 1174 int ret = 0;
1125 1175
1126 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 1176 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1127 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1177 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1128 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) { 1178 if (q_data_src->fourcc == V4L2_PIX_FMT_H264 ||
1179 (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
1180 ctx->dev->devtype->product == CODA_7541)) {
1129 /* copy the buffers that where queued before streamon */ 1181 /* copy the buffers that where queued before streamon */
1130 mutex_lock(&ctx->bitstream_mutex); 1182 mutex_lock(&ctx->bitstream_mutex);
1131 coda_fill_bitstream(ctx); 1183 coda_fill_bitstream(ctx);
@@ -1156,13 +1208,12 @@ static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1156 if (!(ctx->streamon_out & ctx->streamon_cap)) 1208 if (!(ctx->streamon_out & ctx->streamon_cap))
1157 return 0; 1209 return 0;
1158 1210
1159 /* Allow decoder device_run with no new buffers queued */ 1211 /* Allow BIT decoder device_run with no new buffers queued */
1160 if (ctx->inst_type == CODA_INST_DECODER) 1212 if (ctx->inst_type == CODA_INST_DECODER)
1161 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true); 1213 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1162 1214
1163 ctx->gopcounter = ctx->params.gop_size - 1; 1215 ctx->gopcounter = ctx->params.gop_size - 1;
1164 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 1216 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1165 dst_fourcc = q_data_dst->fourcc;
1166 1217
1167 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc, 1218 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1168 q_data_dst->fourcc); 1219 q_data_dst->fourcc);
@@ -1172,6 +1223,10 @@ static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1172 goto err; 1223 goto err;
1173 } 1224 }
1174 1225
1226 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1227 ctx->params.gop_size = 1;
1228 ctx->gopcounter = ctx->params.gop_size - 1;
1229
1175 ret = ctx->ops->start_streaming(ctx); 1230 ret = ctx->ops->start_streaming(ctx);
1176 if (ctx->inst_type == CODA_INST_DECODER) { 1231 if (ctx->inst_type == CODA_INST_DECODER) {
1177 if (ret == -EAGAIN) 1232 if (ret == -EAGAIN)
@@ -1320,6 +1375,12 @@ static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1320 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB: 1375 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1321 ctx->params.intra_refresh = ctrl->val; 1376 ctx->params.intra_refresh = ctrl->val;
1322 break; 1377 break;
1378 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1379 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1380 break;
1381 case V4L2_CID_JPEG_RESTART_INTERVAL:
1382 ctx->params.jpeg_restart_interval = ctrl->val;
1383 break;
1323 default: 1384 default:
1324 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1385 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1325 "Invalid control, id=%d, val=%d\n", 1386 "Invalid control, id=%d, val=%d\n",
@@ -1381,6 +1442,14 @@ static void coda_encode_ctrls(struct coda_ctx *ctx)
1381 1920 * 1088 / 256, 1, 0); 1442 1920 * 1088 / 256, 1, 0);
1382} 1443}
1383 1444
1445static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1446{
1447 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1448 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1449 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1450 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1451}
1452
1384static int coda_ctrls_setup(struct coda_ctx *ctx) 1453static int coda_ctrls_setup(struct coda_ctx *ctx)
1385{ 1454{
1386 v4l2_ctrl_handler_init(&ctx->ctrls, 2); 1455 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
@@ -1389,8 +1458,12 @@ static int coda_ctrls_setup(struct coda_ctx *ctx)
1389 V4L2_CID_HFLIP, 0, 1, 1, 0); 1458 V4L2_CID_HFLIP, 0, 1, 1, 0);
1390 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1459 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1391 V4L2_CID_VFLIP, 0, 1, 1, 0); 1460 V4L2_CID_VFLIP, 0, 1, 1, 0);
1392 if (ctx->inst_type == CODA_INST_ENCODER) 1461 if (ctx->inst_type == CODA_INST_ENCODER) {
1393 coda_encode_ctrls(ctx); 1462 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1463 coda_jpeg_encode_ctrls(ctx);
1464 else
1465 coda_encode_ctrls(ctx);
1466 }
1394 1467
1395 if (ctx->ctrls.error) { 1468 if (ctx->ctrls.error) {
1396 v4l2_err(&ctx->dev->v4l2_dev, 1469 v4l2_err(&ctx->dev->v4l2_dev,
@@ -1548,16 +1621,17 @@ static int coda_open(struct file *file)
1548 1621
1549 ctx->fh.ctrl_handler = &ctx->ctrls; 1622 ctx->fh.ctrl_handler = &ctx->ctrls;
1550 1623
1551 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE, 1624 ret = coda_alloc_context_buf(ctx, &ctx->parabuf,
1552 "parabuf"); 1625 CODA_PARA_BUF_SIZE, "parabuf");
1553 if (ret < 0) { 1626 if (ret < 0) {
1554 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf"); 1627 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1555 goto err_dma_alloc; 1628 goto err_dma_alloc;
1556 } 1629 }
1557 1630
1558 ctx->bitstream.size = CODA_MAX_FRAME_SIZE; 1631 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1559 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev, 1632 ctx->bitstream.vaddr = dma_alloc_writecombine(
1560 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL); 1633 &dev->plat_dev->dev, ctx->bitstream.size,
1634 &ctx->bitstream.paddr, GFP_KERNEL);
1561 if (!ctx->bitstream.vaddr) { 1635 if (!ctx->bitstream.vaddr) {
1562 v4l2_err(&dev->v4l2_dev, 1636 v4l2_err(&dev->v4l2_dev,
1563 "failed to allocate bitstream ringbuffer"); 1637 "failed to allocate bitstream ringbuffer");
@@ -1625,8 +1699,10 @@ static int coda_release(struct file *file)
1625 list_del(&ctx->list); 1699 list_del(&ctx->list);
1626 coda_unlock(ctx); 1700 coda_unlock(ctx);
1627 1701
1628 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size, 1702 if (ctx->bitstream.vaddr) {
1629 ctx->bitstream.vaddr, ctx->bitstream.paddr); 1703 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1704 ctx->bitstream.vaddr, ctx->bitstream.paddr);
1705 }
1630 if (ctx->dev->devtype->product == CODA_DX6) 1706 if (ctx->dev->devtype->product == CODA_DX6)
1631 coda_free_aux_buf(dev, &ctx->workbuf); 1707 coda_free_aux_buf(dev, &ctx->workbuf);
1632 1708
diff --git a/drivers/media/platform/coda/coda-jpeg.c b/drivers/media/platform/coda/coda-jpeg.c
new file mode 100644
index 000000000000..967b0159c8b9
--- /dev/null
+++ b/drivers/media/platform/coda/coda-jpeg.c
@@ -0,0 +1,225 @@
1/*
2 * Coda multi-standard codec IP - JPEG support functions
3 *
4 * Copyright (C) 2014 Philipp Zabel, Pengutronix
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/swab.h>
14
15#include "coda.h"
16
17/*
18 * Typical Huffman tables for 8-bit precision luminance and
19 * chrominance from JPEG ITU-T.81 (ISO/IEC 10918-1) Annex K.3
20 */
21
22static const unsigned char luma_dc_bits[16] = {
23 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
24 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25};
26
27static const unsigned char luma_dc_value[12] = {
28 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29 0x08, 0x09, 0x0a, 0x0b,
30};
31
32static const unsigned char chroma_dc_bits[16] = {
33 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
34 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
35};
36
37static const unsigned char chroma_dc_value[12] = {
38 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
39 0x08, 0x09, 0x0a, 0x0b,
40};
41
42static const unsigned char luma_ac_bits[16] = {
43 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
44 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
45};
46
47static const unsigned char luma_ac_value[162 + 2] = {
48 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
49 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
50 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
51 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
52 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
53 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
54 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
55 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
56 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
57 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
58 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
59 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
60 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
61 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
62 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
63 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
64 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
65 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
66 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
67 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
68 0xf9, 0xfa, /* padded to 32-bit */
69};
70
71static const unsigned char chroma_ac_bits[16] = {
72 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
73 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
74};
75
76static const unsigned char chroma_ac_value[162 + 2] = {
77 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
78 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
79 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
80 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
81 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
82 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
83 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
84 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
85 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
86 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
87 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
88 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
89 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
90 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
91 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
92 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
93 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
94 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
95 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
96 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
97 0xf9, 0xfa, /* padded to 32-bit */
98};
99
100/*
101 * Quantization tables for luminance and chrominance components in
102 * zig-zag scan order from the Freescale i.MX VPU libaries
103 */
104
105static unsigned char luma_q[64] = {
106 0x06, 0x04, 0x04, 0x04, 0x05, 0x04, 0x06, 0x05,
107 0x05, 0x06, 0x09, 0x06, 0x05, 0x06, 0x09, 0x0b,
108 0x08, 0x06, 0x06, 0x08, 0x0b, 0x0c, 0x0a, 0x0a,
109 0x0b, 0x0a, 0x0a, 0x0c, 0x10, 0x0c, 0x0c, 0x0c,
110 0x0c, 0x0c, 0x0c, 0x10, 0x0c, 0x0c, 0x0c, 0x0c,
111 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
112 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
113 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
114};
115
116static unsigned char chroma_q[64] = {
117 0x07, 0x07, 0x07, 0x0d, 0x0c, 0x0d, 0x18, 0x10,
118 0x10, 0x18, 0x14, 0x0e, 0x0e, 0x0e, 0x14, 0x14,
119 0x0e, 0x0e, 0x0e, 0x0e, 0x14, 0x11, 0x0c, 0x0c,
120 0x0c, 0x0c, 0x0c, 0x11, 0x11, 0x0c, 0x0c, 0x0c,
121 0x0c, 0x0c, 0x0c, 0x11, 0x0c, 0x0c, 0x0c, 0x0c,
122 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
123 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
124 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
125};
126
127struct coda_memcpy_desc {
128 int offset;
129 const void *src;
130 size_t len;
131};
132
133static void coda_memcpy_parabuf(void *parabuf,
134 const struct coda_memcpy_desc *desc)
135{
136 u32 *dst = parabuf + desc->offset;
137 const u32 *src = desc->src;
138 int len = desc->len / 4;
139 int i;
140
141 for (i = 0; i < len; i += 2) {
142 dst[i + 1] = swab32(src[i]);
143 dst[i] = swab32(src[i + 1]);
144 }
145}
146
147int coda_jpeg_write_tables(struct coda_ctx *ctx)
148{
149 int i;
150 static const struct coda_memcpy_desc huff[8] = {
151 { 0, luma_dc_bits, sizeof(luma_dc_bits) },
152 { 16, luma_dc_value, sizeof(luma_dc_value) },
153 { 32, luma_ac_bits, sizeof(luma_ac_bits) },
154 { 48, luma_ac_value, sizeof(luma_ac_value) },
155 { 216, chroma_dc_bits, sizeof(chroma_dc_bits) },
156 { 232, chroma_dc_value, sizeof(chroma_dc_value) },
157 { 248, chroma_ac_bits, sizeof(chroma_ac_bits) },
158 { 264, chroma_ac_value, sizeof(chroma_ac_value) },
159 };
160 struct coda_memcpy_desc qmat[3] = {
161 { 512, ctx->params.jpeg_qmat_tab[0], 64 },
162 { 576, ctx->params.jpeg_qmat_tab[1], 64 },
163 { 640, ctx->params.jpeg_qmat_tab[1], 64 },
164 };
165
166 /* Write huffman tables to parameter memory */
167 for (i = 0; i < ARRAY_SIZE(huff); i++)
168 coda_memcpy_parabuf(ctx->parabuf.vaddr, huff + i);
169
170 /* Write Q-matrix to parameter memory */
171 for (i = 0; i < ARRAY_SIZE(qmat); i++)
172 coda_memcpy_parabuf(ctx->parabuf.vaddr, qmat + i);
173
174 return 0;
175}
176
177/*
178 * Scale quantization table using nonlinear scaling factor
179 * u8 qtab[64], scale [50,190]
180 */
181static void coda_scale_quant_table(u8 *q_tab, int scale)
182{
183 unsigned int temp;
184 int i;
185
186 for (i = 0; i < 64; i++) {
187 temp = DIV_ROUND_CLOSEST((unsigned int)q_tab[i] * scale, 100);
188 if (temp <= 0)
189 temp = 1;
190 if (temp > 255)
191 temp = 255;
192 q_tab[i] = (unsigned char)temp;
193 }
194}
195
196void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality)
197{
198 unsigned int scale;
199
200 ctx->params.jpeg_quality = quality;
201
202 /* Clip quality setting to [5,100] interval */
203 if (quality > 100)
204 quality = 100;
205 if (quality < 5)
206 quality = 5;
207
208 /*
209 * Non-linear scaling factor:
210 * [5,50] -> [1000..100], [51,100] -> [98..0]
211 */
212 if (quality < 50)
213 scale = 5000 / quality;
214 else
215 scale = 200 - 2 * quality;
216
217 if (ctx->params.jpeg_qmat_tab[0]) {
218 memcpy(ctx->params.jpeg_qmat_tab[0], luma_q, 64);
219 coda_scale_quant_table(ctx->params.jpeg_qmat_tab[0], scale);
220 }
221 if (ctx->params.jpeg_qmat_tab[1]) {
222 memcpy(ctx->params.jpeg_qmat_tab[1], chroma_q, 64);
223 coda_scale_quant_table(ctx->params.jpeg_qmat_tab[1], scale);
224 }
225}
diff --git a/drivers/media/platform/coda/coda.h b/drivers/media/platform/coda/coda.h
index 07eaf58303ef..c14dee82891f 100644
--- a/drivers/media/platform/coda/coda.h
+++ b/drivers/media/platform/coda/coda.h
@@ -69,7 +69,7 @@ struct coda_aux_buf {
69 69
70struct coda_dev { 70struct coda_dev {
71 struct v4l2_device v4l2_dev; 71 struct v4l2_device v4l2_dev;
72 struct video_device vfd[3]; 72 struct video_device vfd[5];
73 struct platform_device *plat_dev; 73 struct platform_device *plat_dev;
74 const struct coda_devtype *devtype; 74 const struct coda_devtype *devtype;
75 75
@@ -118,6 +118,9 @@ struct coda_params {
118 u8 mpeg4_inter_qp; 118 u8 mpeg4_inter_qp;
119 u8 gop_size; 119 u8 gop_size;
120 int intra_refresh; 120 int intra_refresh;
121 u8 jpeg_quality;
122 u8 jpeg_restart_interval;
123 u8 *jpeg_qmat_tab[3];
121 int codec_mode; 124 int codec_mode;
122 int codec_mode_aux; 125 int codec_mode_aux;
123 enum v4l2_mpeg_video_multi_slice_mode slice_mode; 126 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
@@ -288,6 +291,9 @@ void coda_bit_stream_end_flag(struct coda_ctx *ctx);
288 291
289int coda_h264_padding(int size, char *p); 292int coda_h264_padding(int size, char *p);
290 293
294int coda_jpeg_write_tables(struct coda_ctx *ctx);
295void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
296
291extern const struct coda_context_ops coda_bit_encode_ops; 297extern const struct coda_context_ops coda_bit_encode_ops;
292extern const struct coda_context_ops coda_bit_decode_ops; 298extern const struct coda_context_ops coda_bit_decode_ops;
293 299