Skip to content

fix: use real MJPG decoded size instead of negotiated resolution - #529

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
add-uos:cherry-pick/eagle-e03bbbf
Oct 12, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
add-uos:cherry-pick/eagle-e03bbbf

Conversation

@add-uos

@add-uos add-uos commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

fix: use real MJPG decoded size instead of negotiated resolution

Do not preset decoder width/height before avcodec_open2(); let FFmpeg
parse the real size from the JPEG SOF header. Copy decoded frames using
the actual sw_frame/picture dimensions and realloc buffers when needed.
Add jpeg_get_decoded_size() and trim yuv_frame to the real decoded size.

不在 avcodec_open2() 前预设解码器宽高,改由 FFmpeg 从 JPEG SOF 头解析
真实尺寸。按实际解码帧尺寸拷贝并在需要时 realloc 缓冲。新增
jpeg_get_decoded_size(),并在首帧后将 yuv_frame 收紧到真实解码尺寸。

Log: 修复MJPG解码尺寸与协商分辨率不一致导致的解码失败和画面错位
PMS: BUG-372317
Influence: 修复相机协商与实际分辨率不一致导致的解码失败、画面错位及内存浪费。

Summary by Sourcery

Use JPEG-decoded dimensions throughout MJPG processing to correctly handle cameras whose actual frame size differs from the negotiated resolution.

Bug Fixes:

  • Use the actual dimensions decoded from MJPG JPEG frames instead of the negotiated V4L2 resolution, preventing decode failures and image misalignment when they differ.

Enhancements:

  • Resize decoded-frame buffers to match the real JPEG dimensions and expose the decoded size to callers.

Do not preset decoder width/height before avcodec_open2(); let FFmpeg
parse the real size from the JPEG SOF header. Copy decoded frames using
the actual sw_frame/picture dimensions and realloc buffers when needed.
Add jpeg_get_decoded_size() and trim yuv_frame to the real decoded size.

不在 avcodec_open2() 前预设解码器宽高,改由 FFmpeg 从 JPEG SOF 头解析
真实尺寸。按实际解码帧尺寸拷贝并在需要时 realloc 缓冲。新增
jpeg_get_decoded_size(),并在首帧后将 yuv_frame 收紧到真实解码尺寸。

Log: 修复MJPG解码尺寸与协商分辨率不一致导致的解码失败和画面错位
PMS: BUG-372317
Influence: 修复相机协商与实际分辨率不一致导致的解码失败、画面错位及内存浪费。
@sourcery-ai

sourcery-ai Bot commented Oct 12, 2026

Copy link
Copy Markdown

Sorry @add-uos, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 3 days and 7 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Oct 12, 2026

Copy link
Copy Markdown

Reviewer's Guide

MJPG decoding now lets FFmpeg parse the JPEG SOF dimensions, copies frames using the actual decoded geometry, and propagates that geometry to V4L2 frames while resizing buffers to avoid decode failures, image misalignment, and unnecessary memory usage when negotiated and encoded resolutions differ.

Sequence diagram for MJPG decoding with actual JPEG dimensions

sequenceDiagram
    participant V4L2 as V4L2 frame decoder
    participant JPEG as JPEG decoder
    participant FFmpeg
    participant Frame as yuv_frame buffer

    V4L2->>JPEG: jpeg_decode(out_buf, in_buf, size)
    JPEG->>FFmpeg: Decode JPEG without preset width/height
    FFmpeg-->>JPEG: Decoded frame with width, height, format
    JPEG->>Frame: av_image_copy_to_buffer(actual width, actual height)
    JPEG-->>V4L2: Decode result
    V4L2->>JPEG: jpeg_get_decoded_size(real_w, real_h)
    JPEG-->>V4L2: Return decoded dimensions
    alt negotiated size differs
        V4L2->>V4L2: Update frame width and height
    end
    alt buffer size differs
        V4L2->>Frame: realloc(actual width * actual height * 3 / 2)
    end
Loading

File-Level Changes

Change Details Files
Derive JPEG dimensions from the decoded frame rather than the negotiated V4L2 resolution.
  • Stop pre-populating FFmpeg decoder width and height before opening the codec.
  • Capture decoded dimensions from the swscale frame or decoded picture and expose them through a new accessor.
  • Use actual dimensions and pixel format when copying decoded data into the temporary buffer, growing it when required.
libcam/libcam_v4l2core/jpeg_decoder.c
libcam/libcam_v4l2core/jpeg_decoder.h
Propagate the real MJPG dimensions to frame consumers and resize output storage accordingly.
  • Track the allocated capacity of each decoded YUV buffer.
  • After decoding, update frame width and height from the JPEG decoder's reported dimensions.
  • Reallocate the YUV buffer to the real decoded image size and fail cleanly if allocation fails.
  • Reset buffer-capacity metadata during frame cleanup.
libcam/libcam_v4l2core/frame_decoder.c
libcam/libcam_v4l2core/gviewv4l2core.h

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@add-uos

add-uos commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit e0bd925 into linuxdeepin:release/eagle Oct 12, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants