修复:校验 V4L2 编码协商尺寸,避免送帧越界崩溃

This commit is contained in:
mofeng-git
2026-09-27 13:32:34 +08:00
parent f8fb64ad8c
commit 7e6dc91095
3 changed files with 46 additions and 0 deletions

View File

@@ -60,6 +60,7 @@ RUN dpkg --add-architecture arm64
# Install cross-compiler and native build tools
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
patch \
pkg-config \
cmake \
nasm \
@@ -250,6 +251,9 @@ RUN github_prefix="" \
&& make install \
&& rm -rf /tmp/opus /tmp/opus.tar.gz
# Reject raw formats smaller than the requested frame before any V4L2 upload.
COPY build/cross/patches/0001-v4l2-m2m-reject-undersized-encoder-format.patch /tmp/ffmpeg-v4l2-size.patch
# Download and build FFmpeg with RKMPP support
RUN mkdir -p /tmp/ffmpeg-build && cd /tmp/ffmpeg-build \
&& github_prefix="" \
@@ -261,6 +265,7 @@ RUN mkdir -p /tmp/ffmpeg-build && cd /tmp/ffmpeg-build \
&& git remote add origin "${github_prefix}https://github.com/nyanmisaka/ffmpeg-rockchip.git" \
&& git fetch --depth 1 origin ${FFMPEG_ROCKCHIP_REV} \
&& git checkout --detach FETCH_HEAD \
&& patch -p1 < /tmp/ffmpeg-v4l2-size.patch \
&& cd .. \
# Build RKMPP
&& mkdir -p rkmpp/build && cd rkmpp/build \

View File

@@ -60,6 +60,7 @@ RUN dpkg --add-architecture armhf
# Install cross-compiler and native build tools
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
patch \
pkg-config \
cmake \
nasm \
@@ -239,6 +240,9 @@ RUN github_prefix="" \
&& make install \
&& rm -rf /tmp/opus /tmp/opus.tar.gz
# Reject raw formats smaller than the requested frame before any V4L2 upload.
COPY build/cross/patches/0001-v4l2-m2m-reject-undersized-encoder-format.patch /tmp/ffmpeg-v4l2-size.patch
# Download and build FFmpeg with RKMPP support
RUN mkdir -p /tmp/ffmpeg-build && cd /tmp/ffmpeg-build \
&& github_prefix="" \
@@ -250,6 +254,7 @@ RUN mkdir -p /tmp/ffmpeg-build && cd /tmp/ffmpeg-build \
&& git remote add origin "${github_prefix}https://github.com/nyanmisaka/ffmpeg-rockchip.git" \
&& git fetch --depth 1 origin ${FFMPEG_ROCKCHIP_REV} \
&& git checkout --detach FETCH_HEAD \
&& patch -p1 < /tmp/ffmpeg-v4l2-size.patch \
&& cd .. \
# Build RKMPP
&& mkdir -p rkmpp/build && cd rkmpp/build \

View File

@@ -0,0 +1,36 @@
--- a/libavcodec/v4l2_m2m_enc.c
+++ b/libavcodec/v4l2_m2m_enc.c
@@ -338,6 +338,7 @@
V4L2m2mPriv *priv = avctx->priv_data;
enum AVPixelFormat pix_fmt_output;
uint32_t v4l2_fmt_output;
+ unsigned int output_width, output_height;
int ret;
ret = ff_v4l2_m2m_create_context(priv, &s);
@@ -370,6 +371,25 @@
v4l2_fmt_output = output->format.fmt.pix_mp.pixelformat;
else
v4l2_fmt_output = output->format.fmt.pix.pixelformat;
+
+ /* Drivers may clamp the requested raw format to their supported size.
+ * The software-frame upload still uses avctx/frame dimensions, so a
+ * smaller negotiated format would overrun the mapped output buffers.
+ * Allow upward alignment, but reject frames that do not fit.
+ */
+ if (V4L2_TYPE_IS_MULTIPLANAR(output->type)) {
+ output_width = output->format.fmt.pix_mp.width;
+ output_height = output->format.fmt.pix_mp.height;
+ } else {
+ output_width = output->format.fmt.pix.width;
+ output_height = output->format.fmt.pix.height;
+ }
+ if (output_width < avctx->width || output_height < avctx->height) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Encoder cannot fit requested %dx%d frame in negotiated %ux%u format\n",
+ avctx->width, avctx->height, output_width, output_height);
+ return AVERROR(EINVAL);
+ }
pix_fmt_output = ff_v4l2_format_v4l2_to_avfmt(v4l2_fmt_output, AV_CODEC_ID_RAWVIDEO);
if (pix_fmt_output != avctx->pix_fmt) {