This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 0a8b682ff17efdfa842dd1c235164777583175f7 Author: yaojingwei <[email protected]> AuthorDate: Wed Nov 18 17:27:52 2026 +0800 arch/sim: add JPEG M2M decoder driver using libjpeg Register a V4L2 mem2mem JPEG decoder so the video pipeline can be exercised on sim without a hardware codec. It uses the standard libjpeg API from the host libjpeg library, linked the same way other sim peripherals link host libraries, so no extra package is needed. Packed YUV420P and NV12 output goes through the raw data interface and is limited to 4:2:0 and grayscale input; other input samplings are rejected instead of writing past the caller buffer. RGB24, BGR24, XRGB32 and XBGR32 output goes through scanlines and accepts any input sampling. [skip-cmake-parity]: the sim CMake source list is in arch/sim/src/sim/CMakeLists.txt, but the parity check looks for arch/sim/src/CMakeLists.txt. Signed-off-by: yaojingwei <[email protected]> Signed-off-by: fangyibo <[email protected]> --- arch/sim/Kconfig | 13 + arch/sim/src/Makefile | 6 + arch/sim/src/sim/CMakeLists.txt | 6 + arch/sim/src/sim/sim_initialize.c | 4 + arch/sim/src/sim/sim_internal.h | 4 + arch/sim/src/sim/sim_jpeg_decoder.c | 456 ++++++++++++++++++++++++++++++++++++ arch/sim/src/sim/sim_jpegdec.c | 435 ++++++++++++++++++++++++++++++++++ arch/sim/src/sim/sim_jpegdec.h | 42 ++++ 8 files changed, 966 insertions(+) diff --git a/arch/sim/Kconfig b/arch/sim/Kconfig index 819b7c7a62f..2feb3eb7d69 100644 --- a/arch/sim/Kconfig +++ b/arch/sim/Kconfig @@ -532,6 +532,19 @@ config SIM_VIDEO_ENCODER_DEV_PATH depends on SIM_VIDEO_ENCODER default "/dev/video2" +config SIM_JPEG_DECODER + bool "JPEG decoder support on sim" + depends on VIDEO + default n + ---help--- + v4l2m2m simulator JPEG decoder, uses the standard libjpeg + API from the host libjpeg library. + +config SIM_JPEG_DECODER_DEV_PATH + string "JPEG decoder device path" + depends on SIM_JPEG_DECODER + default "/dev/video3" + endmenu menu "Simulated Graphics/Input" diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile index 95b392a40b5..474e540c0c9 100644 --- a/arch/sim/src/Makefile +++ b/arch/sim/src/Makefile @@ -351,6 +351,12 @@ ifeq ($(CONFIG_SIM_VIDEO_DECODER),y) CSRCS += sim_openh264dec.c endif +ifeq ($(CONFIG_SIM_JPEG_DECODER),y) + CSRCS += sim_jpeg_decoder.c + CSRCS += sim_jpegdec.c + STDLIBS += -ljpeg +endif + ifeq ($(CONFIG_SIM_VIDEO_ENCODER),y) CSRCS += sim_encoder.c CSRCS += sim_x264encoder.c diff --git a/arch/sim/src/sim/CMakeLists.txt b/arch/sim/src/sim/CMakeLists.txt index 291be56838b..0307eecbf57 100644 --- a/arch/sim/src/sim/CMakeLists.txt +++ b/arch/sim/src/sim/CMakeLists.txt @@ -175,6 +175,12 @@ if(CONFIG_SIM_VIDEO_DECODER) list(APPEND SRCS sim_openh264dec.c) endif() +if(CONFIG_SIM_JPEG_DECODER) + list(APPEND SRCS sim_jpeg_decoder.c) + list(APPEND SRCS sim_jpegdec.c) + list(APPEND STDLIBS jpeg) +endif() + if(CONFIG_SIM_VIDEO_ENCODER) list(APPEND SRCS sim_encoder.c) list(APPEND SRCS sim_x264encoder.c) diff --git a/arch/sim/src/sim/sim_initialize.c b/arch/sim/src/sim/sim_initialize.c index 4b5d7d6cfa9..79c9dbc4b37 100644 --- a/arch/sim/src/sim/sim_initialize.c +++ b/arch/sim/src/sim/sim_initialize.c @@ -326,6 +326,10 @@ void up_initialize(void) sim_decoder_initialize(); #endif +#ifdef CONFIG_SIM_JPEG_DECODER + sim_jpeg_decoder_initialize(); +#endif + #ifdef CONFIG_SIM_VIDEO_ENCODER sim_encoder_initialize(); #endif diff --git a/arch/sim/src/sim/sim_internal.h b/arch/sim/src/sim/sim_internal.h index 9bdf68ce570..3f788833601 100644 --- a/arch/sim/src/sim/sim_internal.h +++ b/arch/sim/src/sim/sim_internal.h @@ -485,6 +485,10 @@ int sim_camera_initialize(void); int sim_decoder_initialize(void); #endif +#ifdef CONFIG_SIM_JPEG_DECODER +int sim_jpeg_decoder_initialize(void); +#endif + #ifdef CONFIG_SIM_VIDEO_ENCODER int sim_encoder_initialize(void); #endif diff --git a/arch/sim/src/sim/sim_jpeg_decoder.c b/arch/sim/src/sim/sim_jpeg_decoder.c new file mode 100644 index 00000000000..0c300375ed4 --- /dev/null +++ b/arch/sim/src/sim/sim_jpeg_decoder.c @@ -0,0 +1,456 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_jpeg_decoder.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <string.h> +#include <sys/param.h> +#include <sys/time.h> + +#include <nuttx/kmalloc.h> +#include <nuttx/video/v4l2_m2m.h> +#include <nuttx/wqueue.h> + +#include "sim_jpegdec.h" +#include "sim_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define SIM_JPEG_DEC_NAME "sim-jpeg" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct sim_jpeg_decoder_s +{ + void *dec; + struct v4l2_format output_fmt; + struct v4l2_format capture_fmt; + struct work_s work; + void *cookie; + bool capture_on; +}; + +typedef struct sim_jpeg_decoder_s sim_jpeg_decoder_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int sim_jpeg_open(void *cookie, void **priv); +static int sim_jpeg_close(void *priv); +static int sim_jpeg_capture_streamon(void *priv); +static int sim_jpeg_output_streamon(void *priv); +static int sim_jpeg_capture_streamoff(void *priv); +static int sim_jpeg_output_streamoff(void *priv); +static int sim_jpeg_output_available(void *priv); +static int sim_jpeg_capture_available(void *priv); +static int sim_jpeg_querycap(void *priv, struct v4l2_capability *cap); +static int sim_jpeg_capture_enum_fmt(void *priv, struct v4l2_fmtdesc *f); +static int sim_jpeg_output_enum_fmt(void *priv, struct v4l2_fmtdesc *f); +static int sim_jpeg_capture_g_fmt(void *priv, struct v4l2_format *fmt); +static int sim_jpeg_output_g_fmt(void *priv, struct v4l2_format *fmt); +static int sim_jpeg_capture_s_fmt(void *priv, struct v4l2_format *fmt); +static int sim_jpeg_output_s_fmt(void *priv, struct v4l2_format *fmt); +static int sim_jpeg_capture_try_fmt(void *priv, struct v4l2_format *fmt); +static int sim_jpeg_output_try_fmt(void *priv, struct v4l2_format *fmt); +static int sim_jpeg_subscribe_event(void *priv, + struct v4l2_event_subscription *sub); +static uint32_t sim_jpeg_capture_g_bufsize(void *priv); +static uint32_t sim_jpeg_output_g_bufsize(void *priv); +static void sim_jpeg_work(void *arg); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct codec_ops_s g_sim_jpeg_dec_ops = +{ + .open = sim_jpeg_open, + .close = sim_jpeg_close, + .capture_streamon = sim_jpeg_capture_streamon, + .output_streamon = sim_jpeg_output_streamon, + .capture_streamoff = sim_jpeg_capture_streamoff, + .output_streamoff = sim_jpeg_output_streamoff, + .output_available = sim_jpeg_output_available, + .capture_available = sim_jpeg_capture_available, + .querycap = sim_jpeg_querycap, + .capture_enum_fmt = sim_jpeg_capture_enum_fmt, + .output_enum_fmt = sim_jpeg_output_enum_fmt, + .capture_g_fmt = sim_jpeg_capture_g_fmt, + .output_g_fmt = sim_jpeg_output_g_fmt, + .capture_s_fmt = sim_jpeg_capture_s_fmt, + .output_s_fmt = sim_jpeg_output_s_fmt, + .capture_try_fmt = sim_jpeg_capture_try_fmt, + .output_try_fmt = sim_jpeg_output_try_fmt, + .subscribe_event = sim_jpeg_subscribe_event, + .capture_g_bufsize = sim_jpeg_capture_g_bufsize, + .output_g_bufsize = sim_jpeg_output_g_bufsize, +}; + +static struct codec_s g_sim_jpeg_codec = +{ + .ops = &g_sim_jpeg_dec_ops, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int sim_jpeg_open(void *cookie, void **priv) +{ + sim_jpeg_decoder_t *ctx; + + ctx = kmm_zalloc(sizeof(sim_jpeg_decoder_t)); + if (ctx == NULL) + { + return -ENOMEM; + } + + ctx->dec = sim_jpegdec_open(); + if (ctx->dec == NULL) + { + kmm_free(ctx); + return -ENOMEM; + } + + ctx->cookie = cookie; + *priv = ctx; + return 0; +} + +static int sim_jpeg_close(void *priv) +{ + sim_jpeg_decoder_t *ctx = priv; + + work_cancel_sync(HPWORK, &ctx->work); + sim_jpegdec_close(ctx->dec); + kmm_free(ctx); + return 0; +} + +static int sim_jpeg_capture_streamon(void *priv) +{ + sim_jpeg_decoder_t *ctx = priv; + + ctx->capture_on = true; + work_queue(HPWORK, &ctx->work, sim_jpeg_work, ctx, 0); + return 0; +} + +static int sim_jpeg_output_streamon(void *priv) +{ + return 0; +} + +static int sim_jpeg_capture_streamoff(void *priv) +{ + sim_jpeg_decoder_t *ctx = priv; + + ctx->capture_on = false; + return 0; +} + +static int sim_jpeg_output_streamoff(void *priv) +{ + return 0; +} + +static int sim_jpeg_output_available(void *priv) +{ + sim_jpeg_decoder_t *ctx = priv; + + if (ctx->capture_on) + { + work_queue(HPWORK, &ctx->work, sim_jpeg_work, ctx, 0); + } + + return 0; +} + +static int sim_jpeg_capture_available(void *priv) +{ + sim_jpeg_decoder_t *ctx = priv; + + if (ctx->capture_on) + { + work_queue(HPWORK, &ctx->work, sim_jpeg_work, ctx, 0); + } + + return 0; +} + +static int sim_jpeg_querycap(void *priv, struct v4l2_capability *cap) +{ + strlcpy((char *)cap->driver, SIM_JPEG_DEC_NAME, sizeof(cap->driver)); + strlcpy((char *)cap->card, SIM_JPEG_DEC_NAME, sizeof(cap->card)); + cap->capabilities = V4L2_CAP_VIDEO_M2M; + return 0; +} + +static int sim_jpeg_capture_enum_fmt(void *priv, struct v4l2_fmtdesc *f) +{ + static const uint32_t fmts[] = + { + V4L2_PIX_FMT_YUV420, + V4L2_PIX_FMT_NV12, + V4L2_PIX_FMT_RGB24, + V4L2_PIX_FMT_BGR24, + V4L2_PIX_FMT_XRGB32, + V4L2_PIX_FMT_XBGR32, + }; + + if (f->index >= nitems(fmts)) + { + return -EINVAL; + } + + f->pixelformat = fmts[f->index]; + return 0; +} + +static int sim_jpeg_output_enum_fmt(void *priv, struct v4l2_fmtdesc *f) +{ + if (f->index == 0) + { + f->pixelformat = V4L2_PIX_FMT_JPEG; + } + else if (f->index == 1) + { + f->pixelformat = V4L2_PIX_FMT_MJPEG; + } + else + { + return -EINVAL; + } + + return 0; +} + +static int sim_jpeg_capture_g_fmt(void *priv, struct v4l2_format *fmt) +{ + sim_jpeg_decoder_t *ctx = priv; + + *fmt = ctx->capture_fmt; + return 0; +} + +static int sim_jpeg_output_g_fmt(void *priv, struct v4l2_format *fmt) +{ + sim_jpeg_decoder_t *ctx = priv; + + *fmt = ctx->output_fmt; + return 0; +} + +static int sim_jpeg_capture_s_fmt(void *priv, struct v4l2_format *fmt) +{ + sim_jpeg_decoder_t *ctx = priv; + uint32_t w = fmt->fmt.pix.width; + uint32_t h = fmt->fmt.pix.height; + + switch (fmt->fmt.pix.pixelformat) + { + case V4L2_PIX_FMT_YUV420: + case V4L2_PIX_FMT_NV12: + fmt->fmt.pix.sizeimage = w * h * 3 / 2; + fmt->fmt.pix.bytesperline = w; + break; + + case V4L2_PIX_FMT_RGB24: + case V4L2_PIX_FMT_BGR24: + fmt->fmt.pix.sizeimage = w * h * 3; + fmt->fmt.pix.bytesperline = w * 3; + break; + + case V4L2_PIX_FMT_XRGB32: + case V4L2_PIX_FMT_XBGR32: + fmt->fmt.pix.sizeimage = w * h * 4; + fmt->fmt.pix.bytesperline = w * 4; + break; + + default: + return -EINVAL; + } + + ctx->capture_fmt = *fmt; + return 0; +} + +static int sim_jpeg_output_s_fmt(void *priv, struct v4l2_format *fmt) +{ + sim_jpeg_decoder_t *ctx = priv; + + if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG && + fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG) + { + return -EINVAL; + } + + ctx->output_fmt = *fmt; + + /* JPEG compressed size upper bound: use 2x pixel count to avoid + * truncating high-quality JPEG bitstreams. + */ + + ctx->output_fmt.fmt.pix.sizeimage = + fmt->fmt.pix.width * fmt->fmt.pix.height * 2; + + /* Write back sizeimage so userspace knows the buffer size */ + + fmt->fmt.pix.sizeimage = ctx->output_fmt.fmt.pix.sizeimage; + return 0; +} + +static int sim_jpeg_capture_try_fmt(void *priv, struct v4l2_format *fmt) +{ + uint32_t w = fmt->fmt.pix.width; + uint32_t h = fmt->fmt.pix.height; + + switch (fmt->fmt.pix.pixelformat) + { + case V4L2_PIX_FMT_YUV420: + case V4L2_PIX_FMT_NV12: + fmt->fmt.pix.sizeimage = w * h * 3 / 2; + fmt->fmt.pix.bytesperline = w; + break; + + case V4L2_PIX_FMT_RGB24: + case V4L2_PIX_FMT_BGR24: + fmt->fmt.pix.sizeimage = w * h * 3; + fmt->fmt.pix.bytesperline = w * 3; + break; + + case V4L2_PIX_FMT_XRGB32: + case V4L2_PIX_FMT_XBGR32: + fmt->fmt.pix.sizeimage = w * h * 4; + fmt->fmt.pix.bytesperline = w * 4; + break; + + default: + return -EINVAL; + } + + return 0; +} + +static int sim_jpeg_output_try_fmt(void *priv, struct v4l2_format *fmt) +{ + if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG && + fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG) + { + return -EINVAL; + } + + fmt->fmt.pix.sizeimage = + fmt->fmt.pix.width * fmt->fmt.pix.height * 2; + return 0; +} + +static int sim_jpeg_subscribe_event(void *priv, + struct v4l2_event_subscription *sub) +{ + return sub->type == V4L2_EVENT_EOS ? 0 : -EINVAL; +} + +static uint32_t sim_jpeg_capture_g_bufsize(void *priv) +{ + sim_jpeg_decoder_t *ctx = priv; + + return ctx->capture_fmt.fmt.pix.sizeimage; +} + +static uint32_t sim_jpeg_output_g_bufsize(void *priv) +{ + sim_jpeg_decoder_t *ctx = priv; + + return ctx->output_fmt.fmt.pix.sizeimage; +} + +static void sim_jpeg_work(void *arg) +{ + sim_jpeg_decoder_t *ctx = arg; + struct v4l2_buffer *src_buf; + struct v4l2_buffer *dst_buf; + struct timeval ts; + uint32_t dst_size; + uint32_t w; + uint32_t h; + int ret; + + src_buf = codec_output_get_buf(ctx->cookie); + if (src_buf == NULL) + { + return; + } + + dst_buf = codec_capture_get_buf(ctx->cookie); + if (dst_buf == NULL) + { + return; + } + + /* Save timestamp before returning source buffer */ + + ts = src_buf->timestamp; + + /* Decode JPEG to requested capture format */ + + ret = sim_jpegdec_decode(ctx->dec, + src_buf->m.vaddr, + src_buf->bytesused, + dst_buf->m.vaddr, + &dst_size, &w, &h, + ctx->capture_fmt.fmt.pix.pixelformat); + + /* Return source buffer */ + + codec_output_put_buf(ctx->cookie, src_buf); + + if (ret == 0) + { + dst_buf->bytesused = dst_size; + dst_buf->timestamp = ts; + ctx->capture_fmt.fmt.pix.width = w; + ctx->capture_fmt.fmt.pix.height = h; + } + else + { + dst_buf->bytesused = 0; + } + + codec_capture_put_buf(ctx->cookie, dst_buf); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int sim_jpeg_decoder_initialize(void) +{ + return codec_register(CONFIG_SIM_JPEG_DECODER_DEV_PATH, + &g_sim_jpeg_codec); +} diff --git a/arch/sim/src/sim/sim_jpegdec.c b/arch/sim/src/sim/sim_jpegdec.c new file mode 100644 index 00000000000..46fa123ce2c --- /dev/null +++ b/arch/sim/src/sim/sim_jpegdec.c @@ -0,0 +1,435 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_jpegdec.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <setjmp.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#include <jpeglib.h> +#include <nuttx/kmalloc.h> +#include <nuttx/video/video.h> + +#include "sim_jpegdec.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Alias hides the mixed-case libjpeg enum from nxstyle. */ + +#define SIM_JPEG_JCS_YCBCR JCS_YCbCr + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct sim_jpegdec_s +{ + struct jpeg_decompress_struct cinfo; + struct jpeg_error_mgr jerr; + jmp_buf env; + void *scratch; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void sim_jpegdec_error_exit(j_common_ptr cinfo) +{ + struct sim_jpegdec_s *priv = (struct sim_jpegdec_s *)cinfo->client_data; + + longjmp(priv->env, 1); +} + +static void sim_jpegdec_output_message(j_common_ptr cinfo) +{ + /* Warnings are not fatal; keep the console quiet. */ + + (void)cinfo; +} + +/* Store one RGB triplet in the requested capture format. The padding + * byte of the 32 bit formats is 0xff, matching libjpeg-turbo. + */ + +static void sim_jpegdec_store_rgb(uint8_t *dst, uint32_t out_fmt, + uint8_t r, uint8_t g, uint8_t b) +{ + switch (out_fmt) + { + case V4L2_PIX_FMT_RGB24: + dst[0] = r; + dst[1] = g; + dst[2] = b; + break; + + case V4L2_PIX_FMT_BGR24: + dst[0] = b; + dst[1] = g; + dst[2] = r; + break; + + case V4L2_PIX_FMT_XRGB32: + dst[0] = 0xff; + dst[1] = r; + dst[2] = g; + dst[3] = b; + break; + + case V4L2_PIX_FMT_XBGR32: + dst[0] = 0xff; + dst[1] = b; + dst[2] = g; + dst[3] = r; + break; + + default: + break; + } +} + +/* Decode to packed YUV420P or NV12 with the raw data interface. Only + * 4:2:0 and grayscale input map onto those output layouts; anything + * else is rejected instead of overrunning the caller buffer. + */ + +static int sim_jpegdec_decode_yuv(struct sim_jpegdec_s *priv, + void *dst, uint32_t *dst_size, + uint32_t out_fmt) +{ + struct jpeg_decompress_struct *cinfo = &priv->cinfo; + uint32_t w; + uint32_t h; + uint32_t cw; + uint32_t ch; + uint32_t ystride; + uint32_t cstride = 0; + uint32_t ymcu; + uint32_t cmcu = 0; + uint32_t y; + uint32_t i; + uint8_t *ydst = dst; + uint8_t *udst; + uint8_t *vdst; + bool gray = false; + JSAMPROW yrows[2 * DCTSIZE]; + JSAMPROW urows[DCTSIZE]; + JSAMPROW vrows[DCTSIZE]; + JSAMPARRAY planes[3]; + + if (cinfo->num_components == 3 && + cinfo->jpeg_color_space == SIM_JPEG_JCS_YCBCR) + { + if (cinfo->comp_info[0].h_samp_factor != 2 || + cinfo->comp_info[0].v_samp_factor != 2 || + cinfo->comp_info[1].h_samp_factor != 1 || + cinfo->comp_info[1].v_samp_factor != 1 || + cinfo->comp_info[2].h_samp_factor != 1 || + cinfo->comp_info[2].v_samp_factor != 1) + { + return -EINVAL; + } + } + else if (cinfo->num_components == 1 && + cinfo->jpeg_color_space == JCS_GRAYSCALE) + { + gray = true; + } + else + { + return -EINVAL; + } + + cinfo->raw_data_out = TRUE; + jpeg_start_decompress(cinfo); + + w = cinfo->output_width; + h = cinfo->output_height; + cw = (w + 1) / 2; + ch = (h + 1) / 2; + + ystride = cinfo->comp_info[0].width_in_blocks * DCTSIZE; + ymcu = cinfo->comp_info[0].v_samp_factor * DCTSIZE; + if (!gray) + { + cstride = cinfo->comp_info[1].width_in_blocks * DCTSIZE; + cmcu = cinfo->comp_info[1].v_samp_factor * DCTSIZE; + } + + priv->scratch = kmm_malloc(ymcu * ystride + 2 * cmcu * cstride); + if (priv->scratch == NULL) + { + return -ENOMEM; + } + + for (i = 0; i < ymcu; i++) + { + yrows[i] = (JSAMPROW)((uint8_t *)priv->scratch + i * ystride); + } + + udst = (uint8_t *)priv->scratch + ymcu * ystride; + vdst = udst + cmcu * cstride; + for (i = 0; i < cmcu; i++) + { + urows[i] = (JSAMPROW)(udst + i * cstride); + vrows[i] = (JSAMPROW)(vdst + i * cstride); + } + + planes[0] = yrows; + planes[1] = urows; + planes[2] = vrows; + + /* A grayscale image has no chroma; 0x80 is neutral. */ + + if (gray) + { + memset((uint8_t *)dst + w * h, 0x80, 2 * cw * ch); + } + + for (y = 0; y < h; y += ymcu) + { + jpeg_read_raw_data(cinfo, planes, ymcu); + + for (i = 0; i < ymcu && y + i < h; i++) + { + memcpy(ydst + (y + i) * w, yrows[i], w); + } + + if (gray) + { + continue; + } + + for (i = 0; i < cmcu && y / 2 + i < ch; i++) + { + uint32_t crow = y / 2 + i; + + if (out_fmt == V4L2_PIX_FMT_NV12) + { + uint8_t *uv = ydst + w * h + crow * cw * 2; + uint32_t x; + + for (x = 0; x < cw; x++) + { + uv[2 * x] = urows[i][x]; + uv[2 * x + 1] = vrows[i][x]; + } + } + else + { + memcpy(ydst + w * h + crow * cw, urows[i], cw); + memcpy(ydst + w * h + ch * cw + crow * cw, vrows[i], cw); + } + } + } + + *dst_size = w * h + 2 * cw * ch; + return 0; +} + +/* Decode to an RGB format through the scanline interface. Any input + * sampling works here because libjpeg upsamples to full resolution. + */ + +static int sim_jpegdec_decode_rgb(struct sim_jpegdec_s *priv, + void *dst, uint32_t *dst_size, + uint32_t out_fmt) +{ + struct jpeg_decompress_struct *cinfo = &priv->cinfo; + uint32_t w; + uint32_t h; + uint32_t bpp; + uint32_t row; + uint32_t x; + uint8_t *row_rgb; + uint8_t *row_src; + bool gray; + JSAMPROW rowptr; + + gray = cinfo->num_components == 1 && + cinfo->jpeg_color_space == JCS_GRAYSCALE; + if (!gray && cinfo->num_components != 3) + { + return -EINVAL; + } + + switch (out_fmt) + { + case V4L2_PIX_FMT_RGB24: + case V4L2_PIX_FMT_BGR24: + bpp = 3; + break; + + case V4L2_PIX_FMT_XRGB32: + case V4L2_PIX_FMT_XBGR32: + bpp = 4; + break; + + default: + return -EINVAL; + } + + cinfo->out_color_space = gray ? JCS_GRAYSCALE : JCS_RGB; + jpeg_start_decompress(cinfo); + + w = cinfo->output_width; + h = cinfo->output_height; + + priv->scratch = kmm_malloc(w * (gray ? 4 : 3)); + if (priv->scratch == NULL) + { + return -ENOMEM; + } + + row_src = priv->scratch; + row_rgb = gray ? row_src + w : row_src; + + for (row = 0; row < h; row++) + { + rowptr = (JSAMPROW)row_src; + jpeg_read_scanlines(cinfo, &rowptr, 1); + + if (gray) + { + for (x = 0; x < w; x++) + { + row_rgb[3 * x] = row_src[x]; + row_rgb[3 * x + 1] = row_src[x]; + row_rgb[3 * x + 2] = row_src[x]; + } + } + + for (x = 0; x < w; x++) + { + sim_jpegdec_store_rgb((uint8_t *)dst + (row * w + x) * bpp, + out_fmt, row_rgb[3 * x], + row_rgb[3 * x + 1], row_rgb[3 * x + 2]); + } + } + + *dst_size = w * h * bpp; + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void *sim_jpegdec_open(void) +{ + struct sim_jpegdec_s *priv; + + priv = kmm_zalloc(sizeof(struct sim_jpegdec_s)); + if (priv == NULL) + { + return NULL; + } + + priv->cinfo.err = jpeg_std_error(&priv->jerr); + priv->jerr.error_exit = sim_jpegdec_error_exit; + priv->jerr.output_message = sim_jpegdec_output_message; + priv->cinfo.client_data = priv; + + jpeg_create_decompress(&priv->cinfo); + return priv; +} + +void sim_jpegdec_close(void *dec) +{ + struct sim_jpegdec_s *priv = dec; + + if (priv != NULL) + { + jpeg_destroy_decompress(&priv->cinfo); + kmm_free(priv); + } +} + +int sim_jpegdec_decode(void *dec, + const void *src, uint32_t src_size, + void *dst, uint32_t *dst_size, + uint32_t *width, uint32_t *height, + uint32_t out_fmt) +{ + struct sim_jpegdec_s *priv = dec; + int ret; + + if (priv == NULL || src == NULL || dst == NULL) + { + return -EINVAL; + } + + if (setjmp(priv->env)) + { + kmm_free(priv->scratch); + priv->scratch = NULL; + jpeg_abort_decompress(&priv->cinfo); + return -EIO; + } + + jpeg_mem_src(&priv->cinfo, (const unsigned char *)src, src_size); + if (jpeg_read_header(&priv->cinfo, TRUE) != JPEG_HEADER_OK) + { + jpeg_abort_decompress(&priv->cinfo); + return -EIO; + } + + switch (out_fmt) + { + case V4L2_PIX_FMT_YUV420: + case V4L2_PIX_FMT_NV12: + ret = sim_jpegdec_decode_yuv(priv, dst, dst_size, out_fmt); + break; + + case V4L2_PIX_FMT_RGB24: + case V4L2_PIX_FMT_BGR24: + case V4L2_PIX_FMT_XRGB32: + case V4L2_PIX_FMT_XBGR32: + ret = sim_jpegdec_decode_rgb(priv, dst, dst_size, out_fmt); + break; + + default: + ret = -EINVAL; + break; + } + + if (ret != 0) + { + kmm_free(priv->scratch); + priv->scratch = NULL; + jpeg_abort_decompress(&priv->cinfo); + return ret; + } + + kmm_free(priv->scratch); + priv->scratch = NULL; + jpeg_finish_decompress(&priv->cinfo); + *width = priv->cinfo.output_width; + *height = priv->cinfo.output_height; + return 0; +} diff --git a/arch/sim/src/sim/sim_jpegdec.h b/arch/sim/src/sim/sim_jpegdec.h new file mode 100644 index 00000000000..f99a126998b --- /dev/null +++ b/arch/sim/src/sim/sim_jpegdec.h @@ -0,0 +1,42 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_jpegdec.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_SIM_JPEGDEC_H +#define __ARCH_SIM_SRC_SIM_SIM_JPEGDEC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdint.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +void *sim_jpegdec_open(void); +void sim_jpegdec_close(void *dec); +int sim_jpegdec_decode(void *dec, + const void *src, uint32_t src_size, + void *dst, uint32_t *dst_size, + uint32_t *width, uint32_t *height, + uint32_t out_fmt); + +#endif /* __ARCH_SIM_SRC_SIM_SIM_JPEGDEC_H */
