diff --git a/drivers/media/platform/apple/jpeg/Kconfig b/drivers/media/platform/apple/jpeg/Kconfig index bc72a64f3d06b..1a1784735bfc1 100644 --- a/drivers/media/platform/apple/jpeg/Kconfig +++ b/drivers/media/platform/apple/jpeg/Kconfig @@ -1,27 +1,31 @@ # SPDX-License-Identifier: GPL-2.0-only config VIDEO_APPLE_JPEG - tristate "Apple Silicon JPEG encoder" + tristate "Apple Silicon JPEG encoder and decoder" depends on VIDEO_DEV depends on ARCH_APPLE || (COMPILE_TEST && 64BIT) depends on OF_ADDRESS && IOMMU_API && PM + select V4L2_JPEG_HELPER select V4L2_MEM2MEM_DEV select VIDEOBUF2_DMA_CONTIG help - V4L2 memory-to-memory baseline JPEG encoder for the JPEG engine - in M1 Pro/Max/Ultra, M2 and later Apple SoCs and the A18 Pro. - It takes full-range NV12 (4:2:0) or NV16 (4:2:2) frames. The + V4L2 memory-to-memory baseline JPEG encoder (and, where verified, + decoder) for the JPEG engine in M1 Pro/Max/Ultra, M2 and later + Apple SoCs and the A18 Pro. + The encoder takes full-range NV12 (4:2:0) or NV16 (4:2:2). The device compatible selects the register layout and address width; - unrecognized revisions are refused. + unrecognized revisions are refused. On M4 Pro/Max (and M4) and + A18 Pro engines a second video device decodes baseline JPEG to + NV12, NV16 or NV24. To compile this driver as a module, choose M here: the module will be called apple-jpeg. config VIDEO_APPLE_JPEG_KUNIT_TEST - tristate "KUnit tests for the Apple JPEG encoder" if !KUNIT_ALL_TESTS + tristate "KUnit tests for the Apple JPEG encoder and decoder" if !KUNIT_ALL_TESTS depends on KUNIT && VIDEO_APPLE_JPEG default KUNIT_ALL_TESTS help - Check engine identification, the quantisation tables and every - register write of 4:2:0 and 4:2:2 encode jobs. No hardware is - needed. + Check engine identification, the quantisation tables, every + register write of 4:2:0 and 4:2:2 encode jobs, the decoder's JPEG + header checks and its job programming. No hardware is needed. diff --git a/drivers/media/platform/apple/jpeg/apple-jpeg-drv.c b/drivers/media/platform/apple/jpeg/apple-jpeg-drv.c index 349890ba78a29..d211fca3363ed 100644 --- a/drivers/media/platform/apple/jpeg/apple-jpeg-drv.c +++ b/drivers/media/platform/apple/jpeg/apple-jpeg-drv.c @@ -1,12 +1,14 @@ // SPDX-License-Identifier: GPL-2.0-only /* - * Apple Silicon JPEG encoder, V4L2 memory-to-memory. + * Apple Silicon JPEG encoder and decoder, V4L2 memory-to-memory. * - * Takes full-range YUV in one buffer (NV12 for 4:2:0, NV16 for 4:2:2; NV24 - * for 4:4:4 and GREY where the engine's job layout has them) and - * produces baseline JPEG with the standard Huffman tables. One job runs at - * a time; the engine is reset before each job and the completion interrupt - * ends it. Register layout: see apple-jpeg-hw.h. + * The encoder takes full-range YUV in one buffer (NV12 for 4:2:0, NV16 for + * 4:2:2; NV24 for 4:4:4 and GREY where the engine's job layout has them) + * and produces baseline JPEG with the standard Huffman tables. The decoder, + * a second video device on the same engine, takes baseline JPEG and + * produces NV12, NV16 or NV24. One job runs at a time; the engine is reset + * before each job and the completion interrupt ends it. Register layout: + * see apple-jpeg-hw.h. */ #include #include @@ -28,6 +30,7 @@ #include #include #include +#include #include #include @@ -48,6 +51,7 @@ struct apple_jpeg { struct mutex lock; /* serialises ioctls and queue state */ struct v4l2_device v4l2_dev; struct video_device vdev; + struct video_device dec_vdev; /* registered when the layout decodes */ struct v4l2_m2m_dev *m2m_dev; int irq; @@ -60,9 +64,17 @@ struct apple_jpeg { struct apple_jpeg_write writes[APPLE_JPEG_MAX_WRITES]; }; +/* A decoder OUTPUT buffer: the JPEG and what its headers said. */ +struct apple_jpeg_dec_buf { + struct v4l2_m2m_buffer m2m; /* first: the m2m and vb2 views */ + struct apple_jpeg_dec_header hdr; + int error; /* the headers were refused */ +}; + struct apple_jpeg_ctx { struct v4l2_fh fh; struct apple_jpeg *jpeg; + bool dec; /* a decoder context */ struct v4l2_ctrl_handler hdl; struct v4l2_ctrl *quality; struct v4l2_ctrl *restart; @@ -76,8 +88,24 @@ struct apple_jpeg_ctx { * context, which hold only the queue mutex. */ spinlock_t drain_lock; + + /* + * Decoder: the stream the CAPTURE format is for. A source buffer with + * other headers stalls the queue until the client has read the new + * format, which @stalled says it should (the event has been sent). + */ + struct apple_jpeg_dec_header cfg; + bool cfg_valid; + bool stalled; + bool change_last; /* the next CAPTURE buffer ends the old stream */ + bool cap_restart; /* no decode until CAPTURE restarts */ }; +static struct apple_jpeg_dec_buf *aj_dec_buf(struct vb2_v4l2_buffer *vbuf) +{ + return container_of(vbuf, struct apple_jpeg_dec_buf, m2m.vb); +} + static struct apple_jpeg_ctx *aj_file_to_ctx(struct file *file) { return container_of(file_to_v4l2_fh(file), struct apple_jpeg_ctx, fh); @@ -145,9 +173,11 @@ static void aj_finish(struct apple_jpeg *jpeg, struct apple_jpeg_ctx *ctx, int e src = v4l2_m2m_src_buf_remove(m2m); dst = v4l2_m2m_dst_buf_remove(m2m); if (src && dst) { - if (!err && (jpeg->bytes < 4 || jpeg->bytes > vb2_plane_size(&dst->vb2_buf, 0))) + u32 bytes = ctx->dec ? ctx->dst.plane_fmt[0].sizeimage : jpeg->bytes; + + if (!err && (bytes < 4 || bytes > vb2_plane_size(&dst->vb2_buf, 0))) err = -EIO; - vb2_set_plane_payload(&dst->vb2_buf, 0, err ? 0 : jpeg->bytes); + vb2_set_plane_payload(&dst->vb2_buf, 0, err ? 0 : bytes); v4l2_m2m_buf_copy_metadata(src, dst); dst->sequence = ctx->sequence++; dst->flags |= V4L2_BUF_FLAG_KEYFRAME; @@ -248,9 +278,34 @@ static enum apple_jpeg_subsampling aj_subsampling(const struct apple_jpeg *jpeg, return AJ_SUBSAMPLING_420; } -static void aj_device_run(void *priv) +/* Start a built job, or fail it; called with hw_lock held, which it drops. */ +static void aj_start(struct apple_jpeg *jpeg, struct apple_jpeg_ctx *ctx, int n, + unsigned long flags) + __releases(&jpeg->hw_lock) +{ + if (n < 0) { + spin_unlock_irqrestore(&jpeg->hw_lock, flags); + aj_finish(jpeg, ctx, n); + return; + } + jpeg->active = ctx; + /* + * Arm the watchdog before the start command, so that the completion + * cannot precede it, and give this job its full timeout even if an + * earlier job's expiry is still pending. + */ + jpeg->deadline = jiffies + msecs_to_jiffies(AJ_JOB_TIMEOUT_MS); + mod_delayed_work(system_percpu_wq, &jpeg->watchdog, + msecs_to_jiffies(AJ_JOB_TIMEOUT_MS)); + /* Buffers must be visible before the start command, the last write. */ + dma_wmb(); + for (int i = 0; i < n; i++) + writel(jpeg->writes[i].value, jpeg->regs + jpeg->writes[i].offset); + spin_unlock_irqrestore(&jpeg->hw_lock, flags); +} + +static void aj_enc_run(struct apple_jpeg_ctx *ctx) { - struct apple_jpeg_ctx *ctx = priv; struct apple_jpeg *jpeg = ctx->jpeg; struct vb2_v4l2_buffer *src, *dst; struct apple_jpeg_layout layout; @@ -300,29 +355,160 @@ static void aj_device_run(void *priv) } n = apple_jpeg_build_job(jpeg->variant->layout, &job, jpeg->writes, ARRAY_SIZE(jpeg->writes)); - if (n < 0) { + aj_start(jpeg, ctx, n, flags); +} + +/* The raw decode outputs, in apple_jpeg_subsampling order. */ +static const u32 aj_dec_fmts[] = { + V4L2_PIX_FMT_NV12, V4L2_PIX_FMT_NV16, V4L2_PIX_FMT_NV24, +}; + +static enum apple_jpeg_subsampling aj_dec_out(u32 fourcc) +{ + for (unsigned int s = 0; s < ARRAY_SIZE(aj_dec_fmts); s++) + if (aj_dec_fmts[s] == fourcc) + return s; + return AJ_SUBSAMPLING_420; +} + +static bool aj_dec_same(const struct apple_jpeg_dec_header *a, + const struct apple_jpeg_dec_header *b) +{ + return a->width == b->width && a->height == b->height && a->coded == b->coded; +} + +static void aj_dec_run(struct apple_jpeg_ctx *ctx) +{ + struct apple_jpeg *jpeg = ctx->jpeg; + struct vb2_v4l2_buffer *src, *dst; + struct apple_jpeg_dec_layout layout; + struct apple_jpeg_dec_job job; + struct apple_jpeg_dec_buf *buf; + unsigned long flags; + dma_addr_t out; + int n, ret; + + src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); + dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); + buf = aj_dec_buf(src); + ret = pm_runtime_resume_and_get(jpeg->dev); + if (ret < 0) { + pm_runtime_get_noresume(jpeg->dev); + aj_finish(jpeg, ctx, ret); + return; + } + out = vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0); + job = (struct apple_jpeg_dec_job){ + .width = buf->hdr.width, + .height = buf->hdr.height, + .coded = buf->hdr.coded, + .out = aj_dec_out(ctx->dst.pixelformat), + .input = vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0), + .input_size = vb2_plane_size(&src->vb2_buf, 0), + .luma = out, + .output_end = out + vb2_plane_size(&dst->vb2_buf, 0) - 1, + }; + apple_jpeg_dec_layout(job.width, job.height, job.coded, job.out, &layout); + /* The CAPTURE format must be this JPEG's, and the buffer must hold it. */ + if (!aj_dec_same(&buf->hdr, &ctx->cfg) || + layout.bytesperline != ctx->dst.plane_fmt[0].bytesperline || + layout.sizeimage != ctx->dst.plane_fmt[0].sizeimage || + layout.sizeimage > vb2_plane_size(&dst->vb2_buf, 0)) { + aj_finish(jpeg, ctx, -ENOSPC); + return; + } + job.chroma = out + layout.chroma_offset; + job.luma_stride = layout.bytesperline; + job.chroma_stride = layout.chroma_stride; + + spin_lock_irqsave(&jpeg->hw_lock, flags); + ret = jpeg->broken ? -EIO : aj_reset(jpeg); + if (ret) { + jpeg->broken = true; spin_unlock_irqrestore(&jpeg->hw_lock, flags); - aj_finish(jpeg, ctx, n); + dev_err_ratelimited(jpeg->dev, "engine reset failed: %d\n", ret); + aj_finish(jpeg, ctx, -EIO); return; } - jpeg->active = ctx; - /* - * Arm the watchdog before the start command, so that the completion - * cannot precede it, and give this job its full timeout even if an - * earlier job's expiry is still pending. - */ - jpeg->deadline = jiffies + msecs_to_jiffies(AJ_JOB_TIMEOUT_MS); - mod_delayed_work(system_percpu_wq, &jpeg->watchdog, - msecs_to_jiffies(AJ_JOB_TIMEOUT_MS)); - /* Buffers must be visible before the start command, the last write. */ - dma_wmb(); - for (int i = 0; i < n; i++) - writel(jpeg->writes[i].value, jpeg->regs + jpeg->writes[i].offset); - spin_unlock_irqrestore(&jpeg->hw_lock, flags); + n = apple_jpeg_build_decode(jpeg->variant, &job, jpeg->writes, ARRAY_SIZE(jpeg->writes)); + aj_start(jpeg, ctx, n, flags); +} + +static const struct v4l2_event aj_src_change = { + .type = V4L2_EVENT_SOURCE_CHANGE, + .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION, +}; + +/* An empty CAPTURE buffer that ends the stream before a resolution change. */ +static void aj_dec_change_done(struct vb2_v4l2_buffer *vbuf) +{ + vb2_set_plane_payload(&vbuf->vb2_buf, 0, 0); + vbuf->field = V4L2_FIELD_NONE; + vbuf->flags |= V4L2_BUF_FLAG_LAST; + vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE); +} + +/* + * A resolution change ends the decoded stream: the next CAPTURE buffer + * comes back empty with LAST, and no job runs until the client restarts + * CAPTURE with the new format. Unlike a drain it does not stop the m2m + * context, so a STOP the client sends meanwhile still drains the JPEGs + * after the change. + */ +static void aj_dec_end_stream(struct apple_jpeg_ctx *ctx) +{ + struct vb2_v4l2_buffer *dst; + unsigned long flags; + + spin_lock_irqsave(&ctx->drain_lock, flags); + ctx->cap_restart = true; + dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); + if (dst) + aj_dec_change_done(dst); + else + ctx->change_last = true; + spin_unlock_irqrestore(&ctx->drain_lock, flags); +} + +/* + * A decode runs once the CAPTURE format matches the next JPEG. When it does + * not, tell the client once and wait for it to take the new format. + */ +static int aj_job_ready(void *priv) +{ + struct apple_jpeg_ctx *ctx = priv; + struct vb2_v4l2_buffer *src; + struct apple_jpeg_dec_buf *buf; + + if (!ctx->dec) + return 1; + src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); + if (!src || ctx->cap_restart) + return 0; + buf = aj_dec_buf(src); + if (ctx->cfg_valid && aj_dec_same(&buf->hdr, &ctx->cfg)) + return 1; + if (!ctx->stalled) { + ctx->stalled = true; + aj_dec_end_stream(ctx); + v4l2_event_queue_fh(&ctx->fh, &aj_src_change); + } + return 0; +} + +static void aj_device_run(void *priv) +{ + struct apple_jpeg_ctx *ctx = priv; + + if (ctx->dec) + aj_dec_run(ctx); + else + aj_enc_run(ctx); } static const struct v4l2_m2m_ops aj_m2m_ops = { .device_run = aj_device_run, + .job_ready = aj_job_ready, }; /* ---- formats ---------------------------------------------------------- */ @@ -562,6 +748,305 @@ static const struct v4l2_ioctl_ops aj_ioctl_ops = { .vidioc_unsubscribe_event = v4l2_event_unsubscribe, }; +/* ---- decoder formats -------------------------------------------------- */ + +/* The output a JPEG decodes to unless the client picks another. */ +static enum apple_jpeg_subsampling aj_dec_native(enum apple_jpeg_subsampling coded) +{ + return coded == AJ_SUBSAMPLING_GREY ? AJ_SUBSAMPLING_420 : coded; +} + +/* The stream the formats describe: the parsed one, else the OUTPUT size. */ +static struct apple_jpeg_dec_header aj_dec_stream(const struct apple_jpeg_ctx *ctx) +{ + if (ctx->cfg_valid) + return ctx->cfg; + return (struct apple_jpeg_dec_header){ + .width = ctx->src.width, .height = ctx->src.height, + .coded = AJ_SUBSAMPLING_420, + }; +} + +/* + * Raw CAPTURE: the MCU-padded (coded) size, which the engine writes; the + * visible size is the compose rectangle. JPEG is full-range BT.601. + */ +static void aj_dec_fill_cap(const struct apple_jpeg_ctx *ctx, struct v4l2_pix_format_mplane *pix) +{ + const struct apple_jpeg_variant *v = ctx->jpeg->variant; + struct apple_jpeg_dec_header hdr = aj_dec_stream(ctx); + enum apple_jpeg_subsampling out = aj_dec_out(pix->pixelformat); + struct apple_jpeg_dec_layout l; + + if (aj_dec_fmts[out] != pix->pixelformat || !apple_jpeg_can_decode(v, hdr.coded, out)) + out = aj_dec_native(hdr.coded); + apple_jpeg_dec_layout(hdr.width, hdr.height, hdr.coded, out, &l); + memset(pix, 0, sizeof(*pix)); + pix->pixelformat = aj_dec_fmts[out]; + pix->width = l.coded_width; + pix->height = l.coded_height; + pix->field = V4L2_FIELD_NONE; + pix->num_planes = 1; + pix->plane_fmt[0].bytesperline = l.bytesperline; + pix->plane_fmt[0].sizeimage = l.sizeimage; + pix->colorspace = V4L2_COLORSPACE_SRGB; + pix->ycbcr_enc = V4L2_YCBCR_ENC_601; + pix->quantization = V4L2_QUANTIZATION_FULL_RANGE; + pix->xfer_func = V4L2_XFER_FUNC_SRGB; +} + +/* JPEG OUTPUT: the size is a hint until the headers are parsed. */ +static void aj_dec_fill_out(struct v4l2_pix_format_mplane *pix) +{ + u32 w = clamp_t(u32, pix->width ?: 1920, 1, AJ_MAX_SIZE); + u32 h = clamp_t(u32, pix->height ?: 1080, 1, AJ_MAX_SIZE); + u32 keep = pix->plane_fmt[0].sizeimage; + + memset(pix, 0, sizeof(*pix)); + pix->pixelformat = V4L2_PIX_FMT_JPEG; + pix->width = w; + pix->height = h; + pix->field = V4L2_FIELD_NONE; + pix->num_planes = 1; + /* + * A whole JPEG must fit one buffer, and the size is only a hint: budget + * 3 bytes per pixel and at least 8 MiB, so that clients which size the + * buffers from the first frame survive a larger one later. + */ + pix->plane_fmt[0].sizeimage = clamp_t(u64, max_t(u64, keep, 3ULL * w * h), + SZ_8M, APPLE_JPEG_CODED_LIMIT); + pix->plane_fmt[0].sizeimage = PAGE_ALIGN(pix->plane_fmt[0].sizeimage); + pix->colorspace = V4L2_COLORSPACE_SRGB; + pix->ycbcr_enc = V4L2_YCBCR_ENC_601; + pix->quantization = V4L2_QUANTIZATION_FULL_RANGE; + pix->xfer_func = V4L2_XFER_FUNC_SRGB; +} + +/* Take the headers of the next JPEG as the stream, with its native output. */ +static void aj_dec_take(struct apple_jpeg_ctx *ctx, const struct apple_jpeg_dec_header *hdr) +{ + ctx->cfg = *hdr; + ctx->cfg_valid = true; + ctx->stalled = false; + ctx->dst.pixelformat = aj_dec_fmts[aj_dec_native(hdr->coded)]; + aj_dec_fill_cap(ctx, &ctx->dst); +} + +/* + * After a source change event, every CAPTURE format query answers for the + * new stream; called with the queue lock held. + */ +static void aj_dec_adopt(struct apple_jpeg_ctx *ctx) +{ + struct vb2_v4l2_buffer *src; + + if (!ctx->stalled) + return; + src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); + if (!src) + return; + aj_dec_take(ctx, &aj_dec_buf(src)->hdr); + v4l2_m2m_try_schedule(ctx->fh.m2m_ctx); +} + +static int aj_dec_querycap(struct file *file, void *priv, struct v4l2_capability *cap) +{ + strscpy(cap->driver, AJ_NAME, sizeof(cap->driver)); + strscpy(cap->card, "Apple JPEG decoder", sizeof(cap->card)); + return 0; +} + +/* Clients learn the size from the headers: they wait for the source change. */ +static int aj_dec_enum_fmt_out(struct file *file, void *priv, struct v4l2_fmtdesc *f) +{ + if (f->index) + return -EINVAL; + f->pixelformat = V4L2_PIX_FMT_JPEG; + f->flags = V4L2_FMT_FLAG_COMPRESSED | V4L2_FMT_FLAG_DYN_RESOLUTION; + return 0; +} + +/* The stream's native output first, then the conversions the engine does. */ +static int aj_dec_enum_fmt_cap(struct file *file, void *priv, struct v4l2_fmtdesc *f) +{ + struct apple_jpeg_ctx *ctx = aj_file_to_ctx(file); + struct apple_jpeg_dec_header hdr; + enum apple_jpeg_subsampling native; + unsigned int n = 0; + + aj_dec_adopt(ctx); + hdr = aj_dec_stream(ctx); + native = aj_dec_native(hdr.coded); + + if (!f->index) { + f->pixelformat = aj_dec_fmts[native]; + return 0; + } + for (unsigned int s = 0; s < ARRAY_SIZE(aj_dec_fmts); s++) { + if (s == native || + !apple_jpeg_can_decode(ctx->jpeg->variant, hdr.coded, s)) + continue; + if (++n == f->index) { + f->pixelformat = aj_dec_fmts[s]; + return 0; + } + } + return -EINVAL; +} + +static int aj_dec_enum_framesizes(struct file *file, void *priv, struct v4l2_frmsizeenum *fs) +{ + if (fs->index || (fs->pixel_format != V4L2_PIX_FMT_JPEG && + aj_dec_fmts[aj_dec_out(fs->pixel_format)] != fs->pixel_format)) + return -EINVAL; + fs->type = V4L2_FRMSIZE_TYPE_STEPWISE; + fs->stepwise = (struct v4l2_frmsize_stepwise){ 1, AJ_MAX_SIZE, 1, 1, AJ_MAX_SIZE, 1 }; + return 0; +} + +static int aj_dec_g_fmt_cap(struct file *file, void *priv, struct v4l2_format *f) +{ + struct apple_jpeg_ctx *ctx = aj_file_to_ctx(file); + + aj_dec_adopt(ctx); + f->fmt.pix_mp = ctx->dst; + return 0; +} + +static int aj_dec_try_fmt_out(struct file *file, void *priv, struct v4l2_format *f) +{ + aj_dec_fill_out(&f->fmt.pix_mp); + return 0; +} + +static int aj_dec_try_fmt_cap(struct file *file, void *priv, struct v4l2_format *f) +{ + struct apple_jpeg_ctx *ctx = aj_file_to_ctx(file); + + aj_dec_adopt(ctx); + aj_dec_fill_cap(ctx, &f->fmt.pix_mp); + return 0; +} + +static int aj_dec_s_fmt_out(struct file *file, void *priv, struct v4l2_format *f) +{ + struct apple_jpeg_ctx *ctx = aj_file_to_ctx(file); + + if (vb2_is_busy(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx))) + return -EBUSY; + aj_dec_fill_out(&f->fmt.pix_mp); + ctx->src = f->fmt.pix_mp; + if (!ctx->cfg_valid && !vb2_is_busy(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx))) + aj_dec_fill_cap(ctx, &ctx->dst); + return 0; +} + +static int aj_dec_s_fmt_cap(struct file *file, void *priv, struct v4l2_format *f) +{ + struct apple_jpeg_ctx *ctx = aj_file_to_ctx(file); + + if (vb2_is_busy(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx))) + return -EBUSY; + aj_dec_adopt(ctx); + aj_dec_fill_cap(ctx, &f->fmt.pix_mp); + ctx->dst = f->fmt.pix_mp; + return 0; +} + +/* The visible image within the MCU-padded CAPTURE buffer. */ +static int aj_dec_g_selection(struct file *file, void *priv, struct v4l2_selection *s) +{ + struct apple_jpeg_ctx *ctx = aj_file_to_ctx(file); + struct apple_jpeg_dec_header hdr; + + if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) + return -EINVAL; + aj_dec_adopt(ctx); + hdr = aj_dec_stream(ctx); + switch (s->target) { + case V4L2_SEL_TGT_COMPOSE: + case V4L2_SEL_TGT_COMPOSE_DEFAULT: + case V4L2_SEL_TGT_COMPOSE_BOUNDS: + s->r = (struct v4l2_rect){ 0, 0, hdr.width, hdr.height }; + return 0; + case V4L2_SEL_TGT_COMPOSE_PADDED: + s->r = (struct v4l2_rect){ 0, 0, ctx->dst.width, ctx->dst.height }; + return 0; + default: + return -EINVAL; + } +} + +/* As for the encoder's STOP: see aj_encoder_cmd. */ +static int aj_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *dc) +{ + struct apple_jpeg_ctx *ctx = aj_file_to_ctx(file); + unsigned long flags; + int ret; + + ret = v4l2_m2m_ioctl_try_decoder_cmd(file, priv, dc); + if (ret) + return ret; + spin_lock_irqsave(&ctx->drain_lock, flags); + ret = v4l2_m2m_decoder_cmd(file, ctx->fh.m2m_ctx, dc); + if (!ret) { + if (dc->cmd == V4L2_DEC_CMD_START) { + ctx->cap_restart = false; + ctx->change_last = false; + } else if (v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) { + v4l2_event_queue_fh(&ctx->fh, &aj_eos); + } + } + spin_unlock_irqrestore(&ctx->drain_lock, flags); + if (!ret && dc->cmd == V4L2_DEC_CMD_START) { + vb2_clear_last_buffer_dequeued(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)); + v4l2_m2m_try_schedule(ctx->fh.m2m_ctx); + } + return ret; +} + +static int aj_dec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub) +{ + switch (sub->type) { + case V4L2_EVENT_EOS: + return v4l2_event_subscribe(fh, sub, 0, NULL); + case V4L2_EVENT_SOURCE_CHANGE: + return v4l2_src_change_event_subscribe(fh, sub); + default: + return v4l2_ctrl_subscribe_event(fh, sub); + } +} + +static const struct v4l2_ioctl_ops aj_dec_ioctl_ops = { + .vidioc_querycap = aj_dec_querycap, + .vidioc_enum_fmt_vid_out = aj_dec_enum_fmt_out, + .vidioc_enum_fmt_vid_cap = aj_dec_enum_fmt_cap, + .vidioc_enum_framesizes = aj_dec_enum_framesizes, + .vidioc_g_fmt_vid_out_mplane = aj_g_fmt_out, + .vidioc_g_fmt_vid_cap_mplane = aj_dec_g_fmt_cap, + .vidioc_try_fmt_vid_out_mplane = aj_dec_try_fmt_out, + .vidioc_try_fmt_vid_cap_mplane = aj_dec_try_fmt_cap, + .vidioc_s_fmt_vid_out_mplane = aj_dec_s_fmt_out, + .vidioc_s_fmt_vid_cap_mplane = aj_dec_s_fmt_cap, + .vidioc_g_selection = aj_dec_g_selection, + + .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, + .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, + .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, + .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, + .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, + .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, + .vidioc_streamon = v4l2_m2m_ioctl_streamon, + .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, + + .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd, + .vidioc_decoder_cmd = aj_decoder_cmd, + + .vidioc_subscribe_event = aj_dec_subscribe_event, + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, +}; + /* ---- queues ----------------------------------------------------------- */ static struct v4l2_pix_format_mplane *aj_q_fmt(struct apple_jpeg_ctx *ctx, u32 type) @@ -588,9 +1073,31 @@ static int aj_queue_setup(struct vb2_queue *q, unsigned int *nbufs, unsigned int return 0; } +/* A JPEG to decode: the client's payload, its headers checked now. */ +static int aj_dec_buf_prepare_out(struct vb2_buffer *vb) +{ + struct apple_jpeg_dec_buf *buf = aj_dec_buf(to_vb2_v4l2_buffer(vb)); + unsigned long len = vb2_get_plane_payload(vb, 0); + void *data = vb2_plane_vaddr(vb, 0); + + /* The decoder consumes a padded input buffer from the plane base. */ + if (vb->planes[0].data_offset || (vb2_dma_contig_plane_dma_addr(vb, 0) & 31)) + return -EINVAL; + if (!len || len > vb2_plane_size(vb, 0) || !data) + return -EINVAL; + buf->error = apple_jpeg_parse_header(data, len, &buf->hdr); + if (!buf->error && (buf->hdr.width > AJ_MAX_SIZE || buf->hdr.height > AJ_MAX_SIZE)) + buf->error = -EOPNOTSUPP; + return 0; +} + static int aj_buf_prepare(struct vb2_buffer *vb) { - struct v4l2_pix_format_mplane *pix = aj_q_fmt(vb2_get_drv_priv(vb->vb2_queue), vb->type); + struct apple_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); + struct v4l2_pix_format_mplane *pix = aj_q_fmt(ctx, vb->type); + + if (ctx->dec && V4L2_TYPE_IS_OUTPUT(vb->type)) + return aj_dec_buf_prepare_out(vb); for (unsigned int i = 0; i < pix->num_planes; i++) { if (vb2_plane_size(vb, i) < pix->plane_fmt[i].sizeimage) @@ -612,8 +1119,30 @@ static void aj_buf_queue(struct vb2_buffer *vb) struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); unsigned long flags; + /* A JPEG the engine cannot decode fails at once, without a CAPTURE buffer. */ + if (ctx->dec && V4L2_TYPE_IS_OUTPUT(vb->type) && aj_dec_buf(vbuf)->error) { + v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); + return; + } + /* + * Until CAPTURE streams, a JPEG with new headers at the head of the + * queue sets the stream at once: no job can run to notice it, and the + * client waits for the event. Later ones wait for their turn. + */ + if (ctx->dec && V4L2_TYPE_IS_OUTPUT(vb->type) && + !vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)) && + !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) && + (!ctx->cfg_valid || !aj_dec_same(&aj_dec_buf(vbuf)->hdr, &ctx->cfg))) { + aj_dec_take(ctx, &aj_dec_buf(vbuf)->hdr); + v4l2_event_queue_fh(&ctx->fh, &aj_src_change); + } + spin_lock_irqsave(&ctx->drain_lock, flags); if (!V4L2_TYPE_IS_OUTPUT(vb->type) && vb2_is_streaming(vb->vb2_queue) && + ctx->change_last) { + ctx->change_last = false; + aj_dec_change_done(vbuf); + } else if (!V4L2_TYPE_IS_OUTPUT(vb->type) && vb2_is_streaming(vb->vb2_queue) && v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) { vb2_set_plane_payload(vb, 0, 0); vbuf->field = V4L2_FIELD_NONE; @@ -647,7 +1176,30 @@ static void aj_stop_streaming(struct vb2_queue *q) break; v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); } - v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q); + if (ctx->dec && ctx->cap_restart && !V4L2_TYPE_IS_OUTPUT(q->type)) { + struct v4l2_m2m_ctx *m2m = ctx->fh.m2m_ctx; + struct vb2_v4l2_buffer *last = m2m->last_src_buf; + bool draining = m2m->is_draining, next_last = m2m->next_buf_last; + + /* + * The client restarts CAPTURE for a resolution change: a STOP it + * sent before still applies to the JPEGs after the change + * (GStreamer drains at EOS while the change is pending). + */ + v4l2_m2m_update_stop_streaming_state(m2m, q); + m2m->is_draining = draining; + m2m->last_src_buf = last; + m2m->next_buf_last = next_last; + } else { + v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q); + } + /* A seek drops the JPEG that stalled; a CAPTURE restart ends the change. */ + if (V4L2_TYPE_IS_OUTPUT(q->type)) { + ctx->stalled = false; + } else { + ctx->change_last = false; + ctx->cap_restart = false; + } if (V4L2_TYPE_IS_OUTPUT(q->type) && v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) v4l2_event_queue_fh(&ctx->fh, &aj_eos); } @@ -665,7 +1217,8 @@ static int aj_init_queue(struct apple_jpeg_ctx *ctx, struct vb2_queue *q, u32 ty q->type = type; q->io_modes = VB2_MMAP | VB2_DMABUF; q->drv_priv = ctx; - q->buf_struct_size = sizeof(struct v4l2_m2m_buffer); + q->buf_struct_size = ctx->dec && V4L2_TYPE_IS_OUTPUT(type) ? + sizeof(struct apple_jpeg_dec_buf) : sizeof(struct v4l2_m2m_buffer); q->ops = &aj_qops; q->mem_ops = &vb2_dma_contig_memops; q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; @@ -702,19 +1255,26 @@ static int aj_open(struct file *file) if (!ctx) return -ENOMEM; ctx->jpeg = jpeg; + ctx->dec = video_devdata(file) == &jpeg->dec_vdev; spin_lock_init(&ctx->drain_lock); ctx->src.width = 1920; ctx->src.height = 1080; - ctx->src.pixelformat = V4L2_PIX_FMT_NV12; - aj_fill_raw(jpeg, &ctx->src); - aj_fill_coded(jpeg, &ctx->dst, &ctx->src, 0); v4l2_fh_init(&ctx->fh, video_devdata(file)); v4l2_ctrl_handler_init(&ctx->hdl, 2); - ctx->quality = v4l2_ctrl_new_std(&ctx->hdl, &aj_ctrl_ops, - V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 100, 1, 75); - ctx->restart = v4l2_ctrl_new_std(&ctx->hdl, &aj_ctrl_ops, - V4L2_CID_JPEG_RESTART_INTERVAL, 0, U16_MAX, 1, 0); + if (ctx->dec) { + aj_dec_fill_out(&ctx->src); + ctx->dst.pixelformat = V4L2_PIX_FMT_NV12; + aj_dec_fill_cap(ctx, &ctx->dst); + } else { + ctx->src.pixelformat = V4L2_PIX_FMT_NV12; + aj_fill_raw(jpeg, &ctx->src); + aj_fill_coded(jpeg, &ctx->dst, &ctx->src, 0); + ctx->quality = v4l2_ctrl_new_std(&ctx->hdl, &aj_ctrl_ops, + V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 100, 1, 75); + ctx->restart = v4l2_ctrl_new_std(&ctx->hdl, &aj_ctrl_ops, + V4L2_CID_JPEG_RESTART_INTERVAL, 0, U16_MAX, 1, 0); + } if (ctx->hdl.error) { ret = ctx->hdl.error; goto err_ctrls; @@ -950,13 +1510,33 @@ static int aj_probe(struct platform_device *pdev) vdev->vfl_dir = VFL_DIR_M2M; vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; video_set_drvdata(vdev, jpeg); - return video_register_device(vdev, VFL_TYPE_VIDEO, APPLE_M2M_VIDEO_NR); + ret = video_register_device(vdev, VFL_TYPE_VIDEO, APPLE_M2M_VIDEO_NR); + if (ret) + return ret; + + if (!apple_jpeg_can_decode(jpeg->variant, AJ_SUBSAMPLING_420, AJ_SUBSAMPLING_420)) + return 0; + vdev = &jpeg->dec_vdev; + strscpy(vdev->name, AJ_NAME "-dec", sizeof(vdev->name)); + vdev->fops = &aj_fops; + vdev->ioctl_ops = &aj_dec_ioctl_ops; + vdev->release = video_device_release_empty; + vdev->lock = &jpeg->lock; + vdev->v4l2_dev = &jpeg->v4l2_dev; + vdev->vfl_dir = VFL_DIR_M2M; + vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; + video_set_drvdata(vdev, jpeg); + ret = video_register_device(vdev, VFL_TYPE_VIDEO, APPLE_M2M_VIDEO_NR); + if (ret) + video_unregister_device(&jpeg->vdev); + return ret; } static void aj_remove(struct platform_device *pdev) { struct apple_jpeg *jpeg = platform_get_drvdata(pdev); + video_unregister_device(&jpeg->dec_vdev); video_unregister_device(&jpeg->vdev); } @@ -988,5 +1568,5 @@ static struct platform_driver aj_driver = { }; module_platform_driver(aj_driver); -MODULE_DESCRIPTION("Apple Silicon JPEG encoder"); +MODULE_DESCRIPTION("Apple Silicon JPEG encoder and decoder"); MODULE_LICENSE("GPL"); diff --git a/drivers/media/platform/apple/jpeg/apple-jpeg-hw.c b/drivers/media/platform/apple/jpeg/apple-jpeg-hw.c index 0c0f85d323f3a..d56ab06bd1847 100644 --- a/drivers/media/platform/apple/jpeg/apple-jpeg-hw.c +++ b/drivers/media/platform/apple/jpeg/apple-jpeg-hw.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only /* - * Apple Silicon JPEG engine: encode job programming, without hardware access + * Apple Silicon JPEG engine: encode and decode job programming, without hardware access * so that KUnit covers all of it. * * Quantisation tables are the ITU-T T.81 Annex K tables scaled with the @@ -15,6 +15,7 @@ #include #include +#include #include "apple-jpeg-hw.h" @@ -543,6 +544,7 @@ const struct apple_jpeg_variant apple_jpeg_t6041 = { .hwrev_mask = AJ_HWREV_MASK, .hwrev = AJ_HWREV_T6041, .int_done_mask = BIT(31) | BIT(30), + .decode = true, }; EXPORT_SYMBOL_IF_KUNIT(apple_jpeg_t6041); @@ -575,6 +577,7 @@ const struct apple_jpeg_variant apple_jpeg_t8140 = { .hwrev_mask = AJ_HWREV_MASK, .hwrev = AJ_HWREV_T8140, .int_done_mask = AJ_INT_STATUS_DONE, + .decode = true, }; EXPORT_SYMBOL_IF_KUNIT(apple_jpeg_t8140); @@ -915,3 +918,249 @@ int apple_jpeg_build_job(const struct apple_jpeg_reg_layout *layout, return l.error ?: (l.n <= max ? l.n : -ENOSPC); } EXPORT_SYMBOL_IF_KUNIT(apple_jpeg_build_job); + +/* + * Decode output programming, from the public m1n1 JPEG experiment + * (proxyclient/experiments/jpeg.py): per JPEG sampling and planar output + * format, the tile of each output plane in one MCU (8-byte columns and + * rows, plane 1 holding interleaved CbCr) and the chroma resampling. + */ +struct aj_dec_mode { + u8 tile0_w, tile0_h; + u8 tile1_w, tile1_h; + u8 halve_h:1, halve_v:1, double_h:1, double_v:1; +}; + +/* [out][coded]; 4:0:0 JPEGs use the 4:4:4 row. */ +static const struct aj_dec_mode aj_dec_modes[3][3] = { + [AJ_SUBSAMPLING_420] = { + [AJ_SUBSAMPLING_420] = { 2, 16, 2, 8 }, + [AJ_SUBSAMPLING_422] = { 2, 8, 2, 4, .halve_v = 1 }, + [AJ_SUBSAMPLING_444] = { 2, 8, 2, 4, .halve_h = 1, .halve_v = 1 }, + }, + [AJ_SUBSAMPLING_422] = { + [AJ_SUBSAMPLING_420] = { 2, 16, 2, 16, .double_v = 1 }, + [AJ_SUBSAMPLING_422] = { 2, 8, 2, 8 }, + [AJ_SUBSAMPLING_444] = { 2, 8, 2, 8, .halve_h = 1 }, + }, + [AJ_SUBSAMPLING_444] = { + [AJ_SUBSAMPLING_420] = { 2, 16, 4, 16, .double_h = 1, .double_v = 1 }, + [AJ_SUBSAMPLING_422] = { 2, 8, 4, 8, .double_h = 1 }, + [AJ_SUBSAMPLING_444] = { 1, 8, 2, 8 }, + }, +}; + +static const u8 aj_dec_codec[] = { + [AJ_SUBSAMPLING_420] = AJ_CODEC_420, + [AJ_SUBSAMPLING_422] = AJ_CODEC_422, + [AJ_SUBSAMPLING_444] = AJ_CODEC_444, + [AJ_SUBSAMPLING_GREY] = AJ_CODEC_GREY, +}; + +static const u8 aj_dec_format[] = { + [AJ_SUBSAMPLING_420] = AJ_DEC_FMT_420, + [AJ_SUBSAMPLING_422] = AJ_DEC_FMT_422, + [AJ_SUBSAMPLING_444] = AJ_DEC_FMT_444, +}; + +/* YCbCr to RGB, unused for YUV output; the reset value the experiment sets. */ +static const u32 aj_dec_matrix[11] = { + 0x100, 0x0, 0x167, 0x100, 0xffffffa8, 0xffffff49, 0x100, 0x1c6, 0x0, 0x0, 0xffffff80, +}; + +/* + * Check that a JPEG is one the engine decodes: baseline (SOF0), 8-bit, + * YCbCr 4:2:0, 4:2:2, 4:4:4 or greyscale, one interleaved scan of every + * component with tables the engine can select. The engine parses the + * tables and restart markers itself. + */ +int apple_jpeg_parse_header(void *buf, size_t len, struct apple_jpeg_dec_header *hdr) +{ + struct v4l2_jpeg_scan_header scan = {}; + struct v4l2_jpeg_header h = { .scan = &scan }; + const struct v4l2_jpeg_frame_header *f = &h.frame; + int ret; + + ret = v4l2_jpeg_parse_header(buf, len, &h); + if (ret < 0) + return ret; + if (!h.sof.start || !h.sos.start || h.ecs_offset >= len) + return -EINVAL; + /* The segment references start after the two marker bytes. */ + if (h.sof.start[-1] != 0xc0 || f->precision != 8) + return -EOPNOTSUPP; + if (f->num_components != 1 && f->num_components != 3) + return -EOPNOTSUPP; + if (f->num_components == 3 && h.app14_tf == V4L2_JPEG_APP14_TF_CMYK_RGB) + return -EOPNOTSUPP; + switch (f->subsampling) { + case V4L2_JPEG_CHROMA_SUBSAMPLING_420: + hdr->coded = AJ_SUBSAMPLING_420; + break; + case V4L2_JPEG_CHROMA_SUBSAMPLING_422: + hdr->coded = AJ_SUBSAMPLING_422; + break; + case V4L2_JPEG_CHROMA_SUBSAMPLING_444: + hdr->coded = AJ_SUBSAMPLING_444; + break; + case V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY: + hdr->coded = AJ_SUBSAMPLING_GREY; + break; + default: + return -EOPNOTSUPP; + } + if (scan.num_components != f->num_components) + return -EOPNOTSUPP; + for (unsigned int i = 0; i < f->num_components; i++) { + if (f->component[i].quantization_table_selector > 3 || + scan.component[i].component_selector != f->component[i].component_identifier || + scan.component[i].dc_entropy_coding_table_selector > 1 || + scan.component[i].ac_entropy_coding_table_selector > 1) + return -EOPNOTSUPP; + } + hdr->width = f->width; + hdr->height = f->height; + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(apple_jpeg_parse_header); + +/* + * Decode is programmed for the wide-address layout and enabled where it + * was verified: T6041 and T8140. The T6050 job layout is not supported. + */ +bool apple_jpeg_can_decode(const struct apple_jpeg_variant *variant, + enum apple_jpeg_subsampling coded, + enum apple_jpeg_subsampling out) +{ + const struct apple_jpeg_reg_layout *layout = variant->layout; + + if (!variant->decode || layout->t6050_job || !layout->decode_block_base) + return false; + return coded <= AJ_SUBSAMPLING_GREY && out <= AJ_SUBSAMPLING_444; +} +EXPORT_SYMBOL_IF_KUNIT(apple_jpeg_can_decode); + +void apple_jpeg_dec_layout(u32 width, u32 height, enum apple_jpeg_subsampling coded, + enum apple_jpeg_subsampling out, struct apple_jpeg_dec_layout *l) +{ + l->coded_width = ALIGN(width, 16); + l->coded_height = ALIGN(height, apple_jpeg_mcu_rows(coded)); + l->bytesperline = l->coded_width; + l->chroma_offset = l->bytesperline * l->coded_height; + switch (out) { + case AJ_SUBSAMPLING_420: + l->chroma_stride = l->bytesperline; + l->sizeimage = l->chroma_offset + l->chroma_stride * l->coded_height / 2; + break; + case AJ_SUBSAMPLING_444: + l->chroma_stride = 2 * l->bytesperline; + l->sizeimage = l->chroma_offset + l->chroma_stride * l->coded_height; + break; + default: + l->chroma_stride = l->bytesperline; + l->sizeimage = l->chroma_offset + l->chroma_stride * l->coded_height; + break; + } +} +EXPORT_SYMBOL_IF_KUNIT(apple_jpeg_dec_layout); + +/* Every register write of one decode, in order, ending with the start command. */ +int apple_jpeg_build_decode(const struct apple_jpeg_variant *variant, + const struct apple_jpeg_dec_job *job, + struct apple_jpeg_write *w, unsigned int max) +{ + const struct apple_jpeg_reg_layout *layout = variant->layout; + u32 width = job->width, height = job->height, base = layout->decode_block_base; + u64 addr_mask = DMA_BIT_MASK(layout->dma_addr_bits), input_end; + struct aj_list l = { .w = w, .max = max }; + const struct aj_dec_mode *m; + u32 mcu_w, mcu_h, mcus_w, mcus_h, right, bottom, cwidth, crows; + + if (!width || !height || width > U16_MAX || height > U16_MAX) + return -EINVAL; + if (!apple_jpeg_can_decode(variant, job->coded, job->out)) + return -EOPNOTSUPP; + m = &aj_dec_modes[job->out][min(job->coded, AJ_SUBSAMPLING_444)]; + mcu_w = apple_jpeg_mcu_cols(job->coded); + mcu_h = apple_jpeg_mcu_rows(job->coded); + mcus_w = DIV_ROUND_UP(width, mcu_w); + mcus_h = DIV_ROUND_UP(height, mcu_h); + right = width - (mcus_w - 1) * mcu_w; + bottom = height - (mcus_h - 1) * mcu_h; + cwidth = job->out == AJ_SUBSAMPLING_444 ? 2 * width : ALIGN(width, 2); + crows = job->out == AJ_SUBSAMPLING_420 ? DIV_ROUND_UP(height, 2) : height; + + if (job->luma_stride < ALIGN(width, 16) || job->chroma_stride < cwidth) + return -EINVAL; + if ((job->luma | job->chroma | job->input) & 31 || job->input_size < 4) + return -EINVAL; + if (job->output_end < job->chroma || job->chroma < job->luma) + return -EINVAL; + if (check_add_overflow(job->input, (u64)job->input_size - 1, &input_end) || + ((job->input | input_end | job->luma | job->chroma | job->output_end) & ~addr_mask)) + return -ERANGE; + + aj_put(&l, AJ_MODE, AJ_MODE_SETUP); + aj_put(&l, AJ_MAIN_RUN, 0); + aj_defaults(&l, layout, false); + aj_put(&l, AJ_MODE, AJ_MODE_OPEN); + + aj_put(&l, AJ_DECODE_ENABLE, 1); + aj_put(&l, AJ_CODEC_MODE, 0); + aj_put(&l, AJ_ENCODE_ENABLE, 0); + aj_put(&l, AJ_CODEC, aj_dec_codec[job->coded]); + aj_put(&l, base + AJ_DEC_PIXEL_FORMAT, aj_dec_format[job->out]); + + aj_put(&l, AJ_PX_USE_PLANE1, 1); + aj_put(&l, AJ_PX_PLANE0_WIDTH_M1, width - 1); + aj_put(&l, AJ_PX_PLANE0_HEIGHT_M1, height - 1); + aj_put(&l, AJ_PX_PLANE1_WIDTH_M1, cwidth - 1); + aj_put(&l, AJ_PX_PLANE1_HEIGHT_M1, crows - 1); + aj_put(&l, layout->timeout, apple_jpeg_timeout(width, height)); + aj_put(&l, layout->pipe_limit0, 0x1f); + aj_put(&l, layout->pipe_limit1, 1); + + aj_put(&l, base + AJ_DEC_MCUS_W, mcus_w); + aj_put(&l, base + AJ_DEC_MCUS_H, mcus_h); + aj_put(&l, base + AJ_DEC_RIGHT_PIXELS, right); + aj_put(&l, base + AJ_DEC_BOTTOM_PIXELS, bottom); + aj_put(&l, base + AJ_DEC_RIGHT_SAMPLES, DIV_ROUND_UP(right, mcu_w / 8)); + aj_put(&l, base + AJ_DEC_BOTTOM_SAMPLES, DIV_ROUND_UP(bottom, mcu_h / 8)); + + aj_put(&l, AJ_PX_MCUS_H, mcus_h); + aj_put(&l, AJ_PX_MCUS_W, DIV_ROUND_UP(width, job->out == AJ_SUBSAMPLING_444 ? + mcu_w : 16)); + aj_put(&l, AJ_PX_PLANE0_TILE_H, m->tile0_w); + aj_put(&l, AJ_PX_PLANE0_TILE_V, m->tile0_h); + aj_put(&l, AJ_PX_PLANE1_TILE_H, m->tile1_w); + aj_put(&l, AJ_PX_PLANE1_TILE_V, m->tile1_h); + aj_put(&l, AJ_CHROMA_HALVE_H, m->halve_h && job->coded != AJ_SUBSAMPLING_GREY); + aj_put(&l, AJ_CHROMA_HALVE_V, m->halve_v); + aj_put(&l, AJ_CHROMA_DOUBLE_H, m->double_h); + aj_put(&l, AJ_CHROMA_DOUBLE_V, m->double_v); + + for (unsigned int i = 0; i < ARRAY_SIZE(aj_dec_matrix); i++) + aj_put(&l, layout->matrix_base + 4 * i, aj_dec_matrix[i]); + aj_put(&l, base + AJ_DEC_SCALE, 0); + + aj_put_addr(&l, layout, layout->input_plane0, job->input); + aj_put_addr(&l, layout, layout->input_plane1, job->input); + aj_put_addr(&l, layout, layout->input_end, input_end); + aj_put_addr(&l, layout, layout->output_start0, job->luma); + aj_put_addr(&l, layout, layout->output_start1, job->chroma); + aj_put_addr(&l, layout, layout->output_end, job->output_end); + aj_put(&l, AJ_PX_PLANE0_STRIDE, job->luma_stride); + aj_put(&l, AJ_PX_PLANE1_STRIDE, job->chroma_stride); + + aj_put(&l, layout->prefetch_read, 0); + aj_put(&l, layout->prefetch_write, 1); + + aj_put(&l, AJ_MODE, AJ_MODE_DECODE); + aj_put(&l, AJ_DEC_STREAM_CTRL, AJ_DEC_STREAM_CTRL_BASELINE); + aj_put(&l, AJ_JPEG_IO_FLAGS, AJ_IO_FLAGS_ORDER | AJ_IO_FLAGS_DECODE | + AJ_IO_FLAGS_FROM_STREAM); + aj_put(&l, AJ_MAIN_RUN, 1); + aj_put(&l, AJ_COMMAND, 1); + return l.error ?: (l.n <= max ? l.n : -ENOSPC); +} +EXPORT_SYMBOL_IF_KUNIT(apple_jpeg_build_decode); diff --git a/drivers/media/platform/apple/jpeg/apple-jpeg-hw.h b/drivers/media/platform/apple/jpeg/apple-jpeg-hw.h index 45860e465b413..2336f8634144e 100644 --- a/drivers/media/platform/apple/jpeg/apple-jpeg-hw.h +++ b/drivers/media/platform/apple/jpeg/apple-jpeg-hw.h @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * Apple Silicon JPEG engine: register map and encode job programming. + * Apple Silicon JPEG engine: register map, encode and decode job programming. * * Offsets follow the public m1n1 map (proxyclient/m1n1/hw/jpeg.py, MIT) for * the control and 0x1000 blocks. T8140 widens every DMA address to a 64-bit @@ -28,6 +28,7 @@ #define AJ_MODE_OPEN 0x17f #define AJ_MODE_IDLE 0x743 #define AJ_MODE_ENCODE 0x74f +#define AJ_MODE_DECODE 0x757 #define AJ_DEBUG_STATUS 0x00c #define AJ_IRQ_MASK 0x020 #define AJ_STATUS 0x024 @@ -41,6 +42,10 @@ #define AJ_CODEC_MODE 0x02c /* 1 encode */ #define AJ_DECODE_ENABLE 0x034 #define AJ_ENCODE_ENABLE 0x038 +#define AJ_CHROMA_HALVE_H 0x03c +#define AJ_CHROMA_HALVE_V 0x044 +#define AJ_CHROMA_DOUBLE_H 0x04c +#define AJ_CHROMA_DOUBLE_V 0x054 #define AJ_PX_USE_PLANE1 0x058 #define AJ_PX_MCUS_W 0x05c #define AJ_PX_MCUS_H 0x060 @@ -59,6 +64,18 @@ #define AJ_T6050_PRE_ENCODE 0x1dc /* T6050: cleared before MODE_ENCODE */ #define AJ_T6050_ENC_CTRL 0x334 #define AJ_T6050_ENC_CTRL_ENCODE 0x35 +/* Decode geometry and output format, from layout->decode_block_base. */ +#define AJ_DEC_MCUS_W 0x00 +#define AJ_DEC_MCUS_H 0x04 +#define AJ_DEC_RIGHT_PIXELS 0x08 +#define AJ_DEC_BOTTOM_PIXELS 0x0c +#define AJ_DEC_RIGHT_SAMPLES 0x10 +#define AJ_DEC_BOTTOM_SAMPLES 0x14 +#define AJ_DEC_SCALE 0x18 +#define AJ_DEC_PIXEL_FORMAT 0x1c +#define AJ_DEC_FMT_444 0 /* Y plane, CbCr plane */ +#define AJ_DEC_FMT_422 1 +#define AJ_DEC_FMT_420 2 #define AJ_JPEG_IO_FLAGS 0x1000 #define AJ_IO_FLAGS_ORDER (BIT(4) | BIT(5)) #define AJ_IO_FLAGS_422 1 @@ -66,6 +83,8 @@ #define AJ_IO_FLAGS_444 0 #define AJ_IO_FLAGS_GREY 3 #define AJ_IO_FLAGS_T6050 BIT(7) +#define AJ_IO_FLAGS_DECODE BIT(3) +#define AJ_IO_FLAGS_FROM_STREAM 7 /* decode: sampling from the frame header */ #define AJ_COMMAND 0x1004 #define AJ_QTBL_SEL 0x100c #define AJ_QTBL_SEL_LUMA_CHROMA 0x14 /* Y: table 0, Cb and Cr: table 1 */ @@ -78,6 +97,8 @@ #define AJ_JPEG_WIDTH 0x101c #define AJ_COMPRESSED_BYTES 0x1020 #define AJ_OUTPUT_FLAGS 0x1024 +#define AJ_DEC_STREAM_CTRL 0x1028 +#define AJ_DEC_STREAM_CTRL_BASELINE 0x400 #define AJ_INT_STATUS 0x102c #define AJ_INT_STATUS_DONE BIT(31) #define AJ_BITSTREAM_CORRUPTION 0x1030 @@ -134,6 +155,7 @@ struct apple_jpeg_variant { u32 hwrev_mask; u32 hwrev; u32 int_done_mask; + bool decode; /* decode verified on this engine */ }; extern const struct apple_jpeg_variant apple_jpeg_t6000; @@ -272,4 +294,60 @@ int apple_jpeg_build_job(const struct apple_jpeg_reg_layout *layout, const struct apple_jpeg_job *job, struct apple_jpeg_write *w, unsigned int max); +/* + * Decode: the engine parses the whole baseline JPEG (tables, restart + * markers) from the input buffer itself; the driver only checks the + * envelope and programs the geometry. Output is Y then interleaved CbCr in + * one buffer, as for the encoder's input. + */ +struct apple_jpeg_dec_job { + u32 width; + u32 height; + enum apple_jpeg_subsampling coded; /* the JPEG's sampling */ + enum apple_jpeg_subsampling out; /* the raw output's */ + u64 input; + u32 input_size; /* the whole input buffer, not just the payload */ + u64 luma; + u64 chroma; + u32 luma_stride; + u32 chroma_stride; + u64 output_end; /* inclusive */ +}; + +/* MCU width of a JPEG's sampling: 16 for 4:2:0 and 4:2:2, else 8. */ +static inline u32 apple_jpeg_mcu_cols(enum apple_jpeg_subsampling s) +{ + return s < AJ_SUBSAMPLING_444 ? 16 : 8; +} + +/* + * Raw decode output. The engine writes whole MCUs, so the buffer carries + * the MCU-padded image: @coded_height rows of luma, then the chroma rows. + */ +struct apple_jpeg_dec_layout { + u32 coded_width; + u32 coded_height; + u32 bytesperline; + u32 chroma_stride; + u32 chroma_offset; + u32 sizeimage; +}; + +/* What a decode needs from a JPEG's headers. */ +struct apple_jpeg_dec_header { + u16 width; + u16 height; + enum apple_jpeg_subsampling coded; +}; + +int apple_jpeg_parse_header(void *buf, size_t len, struct apple_jpeg_dec_header *hdr); +void apple_jpeg_dec_layout(u32 width, u32 height, enum apple_jpeg_subsampling coded, + enum apple_jpeg_subsampling out, struct apple_jpeg_dec_layout *l); +bool apple_jpeg_can_decode(const struct apple_jpeg_variant *variant, + enum apple_jpeg_subsampling coded, + enum apple_jpeg_subsampling out); +int apple_jpeg_build_decode(const struct apple_jpeg_variant *variant, + const struct apple_jpeg_dec_job *job, + struct apple_jpeg_write *w, unsigned int max); + #endif /* APPLE_JPEG_HW_H */ diff --git a/drivers/media/platform/apple/jpeg/apple-jpeg-test.c b/drivers/media/platform/apple/jpeg/apple-jpeg-test.c index 461d6ab9979d0..c491075820b2f 100644 --- a/drivers/media/platform/apple/jpeg/apple-jpeg-test.c +++ b/drivers/media/platform/apple/jpeg/apple-jpeg-test.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-2.0-only -/* KUnit tests for Apple JPEG identity matching and encode job programming. */ +/* KUnit tests for Apple JPEG identity matching, encode and decode job programming. */ #include #include @@ -622,6 +622,274 @@ static void apple_jpeg_coded_size_test(struct kunit *test) (u32)PAGE_ALIGN(1627406336)); } +/* ---- decode ------------------------------------------------------------ */ + +/* + * A minimal JPEG header: SOI, SOF (marker @sof), SOS, two bytes of entropy + * data and EOI. @ids gives each component's identifier and H/V factors. + */ +static size_t apple_jpeg_test_hdr(u8 *b, u8 sof, u8 precision, u16 w, u16 h, u8 nf, + const u8 (*comp)[2], u8 ns, u8 tdta) +{ + size_t n = 0; + + b[n++] = 0xff; b[n++] = 0xd8; + b[n++] = 0xff; b[n++] = sof; + b[n++] = 0; b[n++] = 8 + 3 * nf; + b[n++] = precision; + b[n++] = h >> 8; b[n++] = h; + b[n++] = w >> 8; b[n++] = w; + b[n++] = nf; + for (unsigned int i = 0; i < nf; i++) { + b[n++] = comp[i][0]; + b[n++] = comp[i][1]; + b[n++] = i ? 1 : 0; + } + b[n++] = 0xff; b[n++] = 0xda; + b[n++] = 0; b[n++] = 6 + 2 * ns; + b[n++] = ns; + for (unsigned int i = 0; i < ns; i++) { + b[n++] = comp[i][0]; + b[n++] = i ? tdta : 0; + } + b[n++] = 0; b[n++] = 63; b[n++] = 0; + b[n++] = 0x12; b[n++] = 0x34; + b[n++] = 0xff; b[n++] = 0xd9; + return n; +} + +static void apple_jpeg_parse_test(struct kunit *test) +{ + static const u8 c420[3][2] = { { 1, 0x22 }, { 2, 0x11 }, { 3, 0x11 } }; + static const u8 c422[3][2] = { { 1, 0x21 }, { 2, 0x11 }, { 3, 0x11 } }; + static const u8 c444[3][2] = { { 1, 0x11 }, { 2, 0x11 }, { 3, 0x11 } }; + static const u8 c411[3][2] = { { 1, 0x41 }, { 2, 0x11 }, { 3, 0x11 } }; + static const u8 cmyk[4][2] = { { 1, 0x11 }, { 2, 0x11 }, { 3, 0x11 }, { 4, 0x11 } }; + struct apple_jpeg_dec_header hdr; + u8 *b = kunit_kzalloc(test, 128, GFP_KERNEL); + size_t n; + + KUNIT_ASSERT_NOT_NULL(test, b); + n = apple_jpeg_test_hdr(b, 0xc0, 8, 1920, 1080, 3, c420, 3, 0x11); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), 0); + KUNIT_EXPECT_EQ(test, hdr.width, 1920); + KUNIT_EXPECT_EQ(test, hdr.height, 1080); + KUNIT_EXPECT_EQ(test, hdr.coded, AJ_SUBSAMPLING_420); + + n = apple_jpeg_test_hdr(b, 0xc0, 8, 643, 479, 3, c422, 3, 0x11); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), 0); + KUNIT_EXPECT_EQ(test, hdr.coded, AJ_SUBSAMPLING_422); + n = apple_jpeg_test_hdr(b, 0xc0, 8, 33, 17, 3, c444, 3, 0x11); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), 0); + KUNIT_EXPECT_EQ(test, hdr.coded, AJ_SUBSAMPLING_444); + n = apple_jpeg_test_hdr(b, 0xc0, 8, 642, 482, 1, c444, 1, 0); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), 0); + KUNIT_EXPECT_EQ(test, hdr.coded, AJ_SUBSAMPLING_GREY); + + /* Progressive, 8-bit extended, 12-bit, 4:1:1, four components. */ + n = apple_jpeg_test_hdr(b, 0xc2, 8, 640, 480, 3, c420, 3, 0x11); + KUNIT_EXPECT_LT(test, apple_jpeg_parse_header(b, n, &hdr), 0); + n = apple_jpeg_test_hdr(b, 0xc1, 8, 640, 480, 3, c420, 3, 0x11); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), -EOPNOTSUPP); + n = apple_jpeg_test_hdr(b, 0xc1, 12, 640, 480, 3, c420, 3, 0x11); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), -EOPNOTSUPP); + n = apple_jpeg_test_hdr(b, 0xc0, 8, 640, 480, 3, c411, 3, 0x11); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), -EOPNOTSUPP); + n = apple_jpeg_test_hdr(b, 0xc0, 8, 640, 480, 4, cmyk, 4, 0x11); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), -EOPNOTSUPP); + /* A scan of one of three components, or a third Huffman table. */ + n = apple_jpeg_test_hdr(b, 0xc0, 8, 640, 480, 3, c420, 1, 0x11); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), -EOPNOTSUPP); + n = apple_jpeg_test_hdr(b, 0xc0, 8, 640, 480, 3, c420, 3, 0x21); + KUNIT_EXPECT_EQ(test, apple_jpeg_parse_header(b, n, &hdr), -EOPNOTSUPP); + /* Cut before the scan header, and not a JPEG at all. */ + n = apple_jpeg_test_hdr(b, 0xc0, 8, 640, 480, 3, c420, 3, 0x11); + KUNIT_EXPECT_LT(test, apple_jpeg_parse_header(b, 21, &hdr), 0); + b[0] = 0; + KUNIT_EXPECT_LT(test, apple_jpeg_parse_header(b, n, &hdr), 0); +} + +static void apple_jpeg_dec_layout_test(struct kunit *test) +{ + static const struct { + u32 w, h; + enum apple_jpeg_subsampling coded, out; + u32 cw, ch, cstride, coff, size; + } cases[] = { + { 1920, 1080, AJ_SUBSAMPLING_420, AJ_SUBSAMPLING_420, 1920, 1088, 1920, + 1920 * 1088, 1920 * 1088 * 3 / 2 }, + { 642, 482, AJ_SUBSAMPLING_420, AJ_SUBSAMPLING_420, 656, 496, 656, + 656 * 496, 656 * 496 * 3 / 2 }, + { 643, 479, AJ_SUBSAMPLING_422, AJ_SUBSAMPLING_422, 656, 480, 656, + 656 * 480, 656 * 480 * 2 }, + { 642, 482, AJ_SUBSAMPLING_444, AJ_SUBSAMPLING_444, 656, 488, 1312, + 656 * 488, 656 * 488 * 3 }, + { 1920, 1080, AJ_SUBSAMPLING_GREY, AJ_SUBSAMPLING_420, 1920, 1080, 1920, + 1920 * 1080, 1920 * 1080 * 3 / 2 }, + { 1920, 1080, AJ_SUBSAMPLING_420, AJ_SUBSAMPLING_444, 1920, 1088, 3840, + 1920 * 1088, 1920 * 1088 * 3 }, + }; + struct apple_jpeg_dec_layout l; + + for (unsigned int i = 0; i < ARRAY_SIZE(cases); i++) { + apple_jpeg_dec_layout(cases[i].w, cases[i].h, cases[i].coded, cases[i].out, &l); + KUNIT_EXPECT_EQ(test, l.coded_width, cases[i].cw); + KUNIT_EXPECT_EQ(test, l.coded_height, cases[i].ch); + KUNIT_EXPECT_EQ(test, l.bytesperline, cases[i].cw); + KUNIT_EXPECT_EQ(test, l.chroma_stride, cases[i].cstride); + KUNIT_EXPECT_EQ(test, l.chroma_offset, cases[i].coff); + KUNIT_EXPECT_EQ(test, l.sizeimage, cases[i].size); + } +} + +static struct apple_jpeg_dec_job apple_jpeg_test_dec_job(void) +{ + struct apple_jpeg_dec_layout l; + struct apple_jpeg_dec_job job = { + .width = 1920, .height = 1080, + .coded = AJ_SUBSAMPLING_420, .out = AJ_SUBSAMPLING_420, + .input = 0x10000100000ULL, .input_size = 0x800000, + .luma = 0x10001000000ULL, + }; + + apple_jpeg_dec_layout(job.width, job.height, job.coded, job.out, &l); + job.chroma = job.luma + l.chroma_offset; + job.luma_stride = l.bytesperline; + job.chroma_stride = l.chroma_stride; + job.output_end = job.luma + l.sizeimage - 1; + return job; +} + +static void apple_jpeg_decode_job_test(struct kunit *test) +{ + struct apple_jpeg_write *w = kunit_kcalloc(test, APPLE_JPEG_MAX_WRITES, sizeof(*w), + GFP_KERNEL); + const struct apple_jpeg_reg_layout *rl = apple_jpeg_t8140.layout; + struct apple_jpeg_dec_job job = apple_jpeg_test_dec_job(); + u32 base = rl->decode_block_base; + int n; + + KUNIT_ASSERT_NOT_NULL(test, w); + n = apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, APPLE_JPEG_MAX_WRITES); + KUNIT_ASSERT_GT(test, n, 0); + KUNIT_EXPECT_EQ(test, w[0].offset, (u32)AJ_MODE); + KUNIT_EXPECT_EQ(test, w[0].value, (u32)AJ_MODE_SETUP); + KUNIT_EXPECT_EQ(test, w[n - 2].offset, (u32)AJ_MAIN_RUN); + KUNIT_EXPECT_EQ(test, w[n - 2].value, 1U); + KUNIT_EXPECT_EQ(test, w[n - 1].offset, (u32)AJ_COMMAND); + KUNIT_EXPECT_EQ(test, w[n - 1].value, 1U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_MODE), (u32)AJ_MODE_DECODE); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_DECODE_ENABLE), 1U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_ENCODE_ENABLE), 0U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_CODEC_MODE), 0U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_CODEC), (u32)AJ_CODEC_420); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, base + AJ_DEC_PIXEL_FORMAT), + (u32)AJ_DEC_FMT_420); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, base + AJ_DEC_MCUS_W), 120U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, base + AJ_DEC_MCUS_H), 68U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, base + AJ_DEC_BOTTOM_PIXELS), 8U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, base + AJ_DEC_BOTTOM_SAMPLES), 4U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, base + AJ_DEC_SCALE), 0U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_PX_MCUS_W), 120U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_PX_MCUS_H), 68U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_PX_PLANE0_TILE_V), 16U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_PX_PLANE1_TILE_V), 8U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_PX_PLANE1_HEIGHT_M1), 539U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_PX_PLANE0_STRIDE), 1920U); + /* Wide addresses: low then high word. */ + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, rl->input_plane0), 0x00100000U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, rl->input_plane0 + 4), 0x100U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, rl->input_end), 0x008fffffU); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, rl->output_start1), + lower_32_bits(job.chroma)); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, rl->output_end), + lower_32_bits(job.output_end)); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, rl->pipe_limit0), 0x1fU); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, rl->prefetch_write), 1U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_DEC_STREAM_CTRL), + (u32)AJ_DEC_STREAM_CTRL_BASELINE); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_JPEG_IO_FLAGS), 0x3fU); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, rl->tiling), 0U); + + /* 4:2:2 at an odd width: the half-filled chroma column counts. */ + job.width = 643; + job.height = 479; + job.coded = AJ_SUBSAMPLING_422; + job.out = AJ_SUBSAMPLING_422; + job.luma_stride = 656; + job.chroma_stride = 656; + n = apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, APPLE_JPEG_MAX_WRITES); + KUNIT_ASSERT_GT(test, n, 0); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, base + AJ_DEC_RIGHT_PIXELS), 3U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, base + AJ_DEC_RIGHT_SAMPLES), 2U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_PX_PLANE1_WIDTH_M1), 643U); + + /* 4:4:4 to NV12: chroma halved both ways, 8-pixel MCUs. */ + job.coded = AJ_SUBSAMPLING_444; + job.out = AJ_SUBSAMPLING_420; + n = apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, APPLE_JPEG_MAX_WRITES); + KUNIT_ASSERT_GT(test, n, 0); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_CODEC), (u32)AJ_CODEC_444); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, base + AJ_DEC_MCUS_W), 81U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_PX_MCUS_W), 41U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_CHROMA_HALVE_H), 1U); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_CHROMA_HALVE_V), 1U); + + /* Greyscale: no horizontal halving of the absent chroma. */ + job.coded = AJ_SUBSAMPLING_GREY; + n = apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, APPLE_JPEG_MAX_WRITES); + KUNIT_ASSERT_GT(test, n, 0); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_CODEC), (u32)AJ_CODEC_GREY); + KUNIT_EXPECT_EQ(test, apple_jpeg_last(w, n, AJ_CHROMA_HALVE_H), 0U); +} + +static void apple_jpeg_decode_reject_test(struct kunit *test) +{ + struct apple_jpeg_write *w = kunit_kcalloc(test, APPLE_JPEG_MAX_WRITES, sizeof(*w), + GFP_KERNEL); + struct apple_jpeg_dec_job job; + + KUNIT_ASSERT_NOT_NULL(test, w); + /* Decode is enabled only where it was verified. */ + KUNIT_EXPECT_TRUE(test, apple_jpeg_can_decode(&apple_jpeg_t8140, AJ_SUBSAMPLING_420, + AJ_SUBSAMPLING_420)); + KUNIT_EXPECT_TRUE(test, apple_jpeg_can_decode(&apple_jpeg_t6041, AJ_SUBSAMPLING_GREY, + AJ_SUBSAMPLING_444)); + KUNIT_EXPECT_FALSE(test, apple_jpeg_can_decode(&apple_jpeg_t6041, AJ_SUBSAMPLING_420, + AJ_SUBSAMPLING_GREY)); + KUNIT_EXPECT_FALSE(test, apple_jpeg_can_decode(&apple_jpeg_t6050, AJ_SUBSAMPLING_420, + AJ_SUBSAMPLING_420)); + KUNIT_EXPECT_FALSE(test, apple_jpeg_can_decode(&apple_jpeg_t6000, AJ_SUBSAMPLING_420, + AJ_SUBSAMPLING_420)); + KUNIT_EXPECT_FALSE(test, apple_jpeg_can_decode(&apple_jpeg_t8112, AJ_SUBSAMPLING_420, + AJ_SUBSAMPLING_420)); + + job = apple_jpeg_test_dec_job(); + KUNIT_EXPECT_EQ(test, apple_jpeg_build_decode(&apple_jpeg_t6050, &job, w, + APPLE_JPEG_MAX_WRITES), -EOPNOTSUPP); + job.input |= 4; + KUNIT_EXPECT_EQ(test, apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, + APPLE_JPEG_MAX_WRITES), -EINVAL); + job = apple_jpeg_test_dec_job(); + job.luma_stride = 1904; + KUNIT_EXPECT_EQ(test, apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, + APPLE_JPEG_MAX_WRITES), -EINVAL); + job = apple_jpeg_test_dec_job(); + job.input_size = 0; + KUNIT_EXPECT_EQ(test, apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, + APPLE_JPEG_MAX_WRITES), -EINVAL); + job = apple_jpeg_test_dec_job(); + job.output_end = job.chroma - 1; + KUNIT_EXPECT_EQ(test, apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, + APPLE_JPEG_MAX_WRITES), -EINVAL); + job = apple_jpeg_test_dec_job(); + job.width = 0; + KUNIT_EXPECT_EQ(test, apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, + APPLE_JPEG_MAX_WRITES), -EINVAL); + job = apple_jpeg_test_dec_job(); + KUNIT_EXPECT_EQ(test, apple_jpeg_build_decode(&apple_jpeg_t8140, &job, w, 8), -ENOSPC); +} + static struct kunit_case apple_jpeg_cases[] = { KUNIT_CASE(apple_jpeg_hwrev_test), KUNIT_CASE(apple_jpeg_layout_test), @@ -637,6 +905,10 @@ static struct kunit_case apple_jpeg_cases[] = { KUNIT_CASE(apple_jpeg_job_reject_test), KUNIT_CASE(apple_jpeg_raw_layout_test), KUNIT_CASE(apple_jpeg_coded_size_test), + KUNIT_CASE(apple_jpeg_parse_test), + KUNIT_CASE(apple_jpeg_dec_layout_test), + KUNIT_CASE(apple_jpeg_decode_job_test), + KUNIT_CASE(apple_jpeg_decode_reject_test), { } }; @@ -646,6 +918,6 @@ static struct kunit_suite apple_jpeg_suite = { }; kunit_test_suite(apple_jpeg_suite); -MODULE_DESCRIPTION("KUnit tests for the Apple JPEG encoder"); +MODULE_DESCRIPTION("KUnit tests for the Apple JPEG encoder and decoder"); MODULE_LICENSE("GPL"); MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");