This is an automated email from the git hooks/post-receive script. Git pushed a commit to branch release/9.1 in repository ffmpeg.
commit 1ad290946b7e6d84141592dcb6e9ff51f2d1218c Author: Michael Niedermayer <[email protected]> AuthorDate: Fri Oct 2 03:20:55 2026 +0200 Commit: Michael Niedermayer <[email protected]> CommitDate: Thu Nov 19 14:55:53 2026 +0100 swscale/ops_dispatch: do not rely on linesize padding in frames from the caller Size the lines of caller frames from their width instead, plus for the input the 16 bytes of padding AVFrame.data requires. Buffers allocated by the graph are padded for the backend and keep using their linesize. For widths that are not a multiple of the block size this costs up to 2% with the x86 backend. The interlaced case was reported, the separatefields and crop cases were found during triage of the report. Fixes: out of array read Fixes: out of array write Fixes: edlOwd4qVGBw Regression since: db2bc11a97 Found-by: mzfr <[email protected]> Suggested-by: Niklas Haas <[email protected]> Replicated through UnModified FFmpeg (cherry picked from commit 458e9f79c63ec354f00cf868ce03e2b8a9e5b18e) Signed-off-by: Michael Niedermayer <[email protected]> --- libswscale/format.c | 1 + libswscale/format.h | 6 ++++++ libswscale/graph.c | 17 ++++++++++++++--- libswscale/ops_dispatch.c | 16 +++++++++------- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/libswscale/format.c b/libswscale/format.c index 0d2a07b5bc..72d8bdb272 100644 --- a/libswscale/format.c +++ b/libswscale/format.c @@ -740,6 +740,7 @@ void ff_sws_frame_from_avframe(SwsFrame *dst, const AVFrame *src) for (int i = 0; i < FF_ARRAY_ELEMS(dst->data); i++) { dst->data[i] = src->data[i]; dst->linesize[i] = src->linesize[i]; + dst->padding[i] = 0; } } diff --git a/libswscale/format.h b/libswscale/format.h index 8174cd1891..60cc1cc2ee 100644 --- a/libswscale/format.h +++ b/libswscale/format.h @@ -210,6 +210,12 @@ typedef struct SwsFrame { int width, height; enum AVPixelFormat format; + /** + * Number of bytes after the last pixel of each line that may be read + * (input frames) or written (output frames). + */ + int padding[4]; + /** * Pointer to the original AVFrame, if there is a 1:1 correspondence. **/ diff --git a/libswscale/graph.c b/libswscale/graph.c index bdf934ed5e..2e977cc25f 100644 --- a/libswscale/graph.c +++ b/libswscale/graph.c @@ -55,8 +55,8 @@ int ff_sws_pass_aligned_width(const SwsPass *pass, int width) } /* Allocates (or refs) one buffer per plane */ -static int frame_alloc_planes_ref(AVFrame *dst, const AVFrame *src, - const int plane_copy[4]) +static int frame_alloc_planes_ref(AVFrame *dst, int padding[4], + const AVFrame *src, const int plane_copy[4]) { int ret = av_image_check_size2(dst->width, dst->height, INT64_MAX, dst->format, 0, NULL); @@ -99,6 +99,11 @@ static int frame_alloc_planes_ref(AVFrame *dst, const AVFrame *src, dst->buf[i] = buf; } + for (int i = 0; i < 4 && dst->data[i]; i++) { + padding[i] = dst->linesize[i] - + av_image_get_linesize(dst->format, dst->width, i); + } + return 0; } @@ -140,6 +145,7 @@ static int pass_alloc_output(SwsPass *pass) avframe->height = buffer->height; int ret; + int padding[4] = {0}; #if CONFIG_SWSCALE_VULKAN const SwsGraph *graph = pass->graph; @@ -160,7 +166,7 @@ static int pass_alloc_output(SwsPass *pass) src = pass->input->output->avframe; avframe->format = pass->format; - ret = frame_alloc_planes_ref(avframe, src, buffer->plane_copy); + ret = frame_alloc_planes_ref(avframe, padding, src, buffer->plane_copy); if (ret < 0) { av_frame_free(&avframe); return ret; @@ -171,6 +177,7 @@ done: #endif buffer->avframe = avframe; ff_sws_frame_from_avframe(&buffer->frame, avframe); + memcpy(buffer->frame.padding, padding, sizeof(padding)); return 0; } @@ -1040,6 +1047,10 @@ int ff_sws_graph_run(SwsGraph *graph, const AVFrame *dst, const AVFrame *src) get_field(graph, &graph->dst, dst, &dst_field); get_field(graph, &graph->src, src, &src_field); + /* AVFrame.data requires 16 bytes of padding after the planes for reading */ + for (int i = 0; i < 4; i++) + src_field.padding[i] = 16; + for (int i = 0; i < graph->num_passes; i++) { const SwsPass *pass = graph->passes[i]; graph->exec.pass = pass; diff --git a/libswscale/ops_dispatch.c b/libswscale/ops_dispatch.c index 21e5535f14..c973424aa9 100644 --- a/libswscale/ops_dispatch.c +++ b/libswscale/ops_dispatch.c @@ -224,12 +224,11 @@ static int op_pass_setup(const SwsFrame *out, const SwsFrame *in, size_t safe_blocks = num_blocks; for (int i = 0; i < p->planes_in; i++) { const int idx = p->idx_in[i]; - size_t input_bytes = in->linesize[idx]; - if (p->filter_size_h && float_in) { - /* Floating point inputs may contain NaN / Infinity in the padding */ - const int plane_w = AV_CEIL_RSHIFT(in->width, exec->in_sub_x[i]); - input_bytes = pixel_bytes(plane_w, p->pixel_bits_in, AV_ROUND_UP); - } + const int plane_w = AV_CEIL_RSHIFT(in->width, exec->in_sub_x[i]); + size_t input_bytes = pixel_bytes(plane_w, p->pixel_bits_in, AV_ROUND_UP); + /* Floating point inputs may contain NaN / Infinity in the padding */ + if (!p->filter_size_h || !float_in) + input_bytes += in->padding[idx]; size_t safe_bytes = safe_bytes_pad(input_bytes, comp->over_read[i]); size_t safe_blocks_in; @@ -256,7 +255,10 @@ static int op_pass_setup(const SwsFrame *out, const SwsFrame *in, for (int i = 0; i < p->planes_out; i++) { const int idx = p->idx_out[i]; - size_t safe_bytes = safe_bytes_pad(out->linesize[idx], comp->over_write[i]); + const int plane_w = AV_CEIL_RSHIFT(out->width, exec->out_sub_x[i]); + size_t output_bytes = pixel_bytes(plane_w, p->pixel_bits_out, AV_ROUND_UP) + + out->padding[idx]; + size_t safe_bytes = safe_bytes_pad(output_bytes, comp->over_write[i]); size_t safe_blocks_out = safe_bytes / exec->block_size_out[i]; if (safe_blocks_out < num_blocks) { p->memcpy_out = true; -- To stop receiving notification emails like this one, please contact [email protected]. _______________________________________________ ffmpeg-cvslog mailing list -- [email protected] To unsubscribe send an email to [email protected]
