v4l2-tracer: fix expected frame length calculation
cover v4l2-tracer: fix expected frame length calculation
This series fixes the calculation of expected frame lengths in v4l2-tracer. Previously, the calculation assumed that the stride equals the width and that chroma padding follows the end of the plane, which could result in incorrect lengths.
Patch 1/2: add plane_bytesperline to trace_context
- Introduces plane_bytesperline to store the stride of each plane, providing the necessary data to compute accurate frame lengths.
Patch 2/2: calculate expected length using v4l2-fwht info
- Computes the frame length per plane correctly, fixing previous inaccuracies.
Together, these patches ensure that v4l2-tracer calculates expected frame lengths accurately for all supported pixel formats.
Sarah Gershuni (2):
add plane_bytesperline to trace_context
calculate expected length using v4l2-fwht info
utils/v4l2-tracer/trace-helper.cpp | 55 ++++++++++++++++++++++-------- utils/v4l2-tracer/trace.h | 1 + 2 files changed, 42 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 add plane_bytesperline to trace_context
Add plane_bytesperline array to trace_context to store stride per plane for multiplanar formats. Update g_fmt_setup_trace to initialize the array.
--- utils/v4l2-tracer/trace-helper.cpp | 15 +++++++++++++-- utils/v4l2-tracer/trace.h | 1 + 2 files changed, 14 insertions(+), 2 deletions(-)
utils/v4l2-tracer/trace-helper.cpp +13 -2
@@ -399,16 +399,27 @@ void g_fmt_setup_trace(struct v4l2_format *format) ctx_trace.width = format->fmt.pix.width; ctx_trace.height = format->fmt.pix.height; ctx_trace.pixelformat = format->fmt.pix.pixelformat; + ctx_trace.plane_bytesperline[0] = format->fmt.pix.bytesperline; } if (format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { ctx_trace.width = format->fmt.pix_mp.width; ctx_trace.height = format->fmt.pix_mp.height; ctx_trace.pixelformat = format->fmt.pix_mp.pixelformat; + + for (unsigned i = 0; i < format->fmt.pix_mp.num_planes; i++){ + ctx_trace.plane_bytesperline[i] = format->fmt.pix_mp.plane_fmt[i].bytesperline; + } } - if (format->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) + if (format->type == V4L2_BUF_TYPE_VIDEO_OUTPUT){ ctx_trace.compression_format = format->fmt.pix.pixelformat; - if (format->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) + ctx_trace.plane_bytesperline[0] = format->fmt.pix.bytesperline; + } + if (format->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE){ ctx_trace.compression_format = format->fmt.pix_mp.pixelformat; + for (unsigned i = 0; i < format->fmt.pix_mp.num_planes; i++){ + ctx_trace.plane_bytesperline[i] = format->fmt.pix_mp.plane_fmt[i].bytesperline; + } + } } void s_fmt_setup(struct v4l2_format *format)
utils/v4l2-tracer/trace.h +1 -0
@@ -32,6 +32,7 @@ struct trace_context {__u32 pixelformat; std::string media_device;__u32 compression_format; +__u32 plane_bytesperline[VIDEO_MAX_PLANES ];union {struct h264_info h264; } fmt;
Signature · 1 line
2.43.0
PATCH 2/2 calculate expected length using v4l2-fwht info
In reply to Sarah GershuniUpdate get_expected_length_trace to calculate the expected buffer length based on v4l2_fwht_find_pixfmt info.
--- utils/v4l2-tracer/trace-helper.cpp | 40 +++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-)
utils/v4l2-tracer/trace-helper.cpp +28 -12
@@ -6,6 +6,11 @@ #include "trace.h" #include <math.h> +extern "C" { +#include "codec-v4l2-fwht.h" +} + + struct trace_context ctx_trace = {}; bool is_video_or_media_device(const char *path)
@@ -233,18 +238,29 @@ void print_buffers_trace(void) unsigned get_expected_length_trace() { - /* - * TODO: this assumes that the stride is equal to the real width and that the - * padding follows the end of the chroma plane. It could be improved by - * following the model in v4l2-ctl-streaming.cpp read_write_padded_frame() - */ - unsigned expected_length = ctx_trace.width * ctx_trace.height; - if (ctx_trace.pixelformat == V4L2_PIX_FMT_NV12 || ctx_trace.pixelformat == V4L2_PIX_FMT_YUV420) { - expected_length *= 3; - expected_length /= 2; - expected_length += (expected_length % 2); - } - return expected_length; + const auto *info = v4l2_fwht_find_pixfmt(ctx_trace.pixelformat); + if (!info) + return 0; + + unsigned coded_height = ctx_trace.height; + unsigned expected = 0; + + for (unsigned plane_idx = 0; plane_idx < info->planes_num; ++plane_idx) { + unsigned stride = ctx_trace.plane_bytesperline[plane_idx]; + + bool is_chroma = (plane_idx == 1 || plane_idx == 2); + unsigned h_div = is_chroma ? info->height_div : 1; + + if (info->planes_num == 3 && plane_idx == 1) + stride /= 2; + + if (plane_idx == 1 && + (info->id == V4L2_PIX_FMT_NV24 || info->id == V4L2_PIX_FMT_NV42)) + stride *= 2; + + expected += stride * (coded_height / h_div); + } + return expected; } void s_ext_ctrls_setup(struct v4l2_ext_controls *ext_controls)
Signature · 1 line
2.43.0
Re: [PATCH v4l-utils 2/2] calculate expected length using v4l2-fwht info
Hi Sarah,
Quoted 39 lines · Sarah Gershuni
Update
get_expected_length_traceto calculate the expected buffer length based onv4l2_fwht_find_pixfmtinfo.Signed-off-by Sarah Gershuni <[email protected]>--- utils/v4l2-tracer/trace-helper.cpp | 40 +++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-)utils/v4l2-tracer/trace-helper.cpp +5
-11@@ -6,6 +6,11 @@ #include "trace.h" #include <math.h> +extern "C" { +#include "codec-v4l2-fwht.h" +} + + struct trace_context ctx_trace = {}; bool is_video_or_media_device(const char *path)@@ -233,18 +238,29 @@ void print_buffers_trace(void) unsigned get_expected_length_trace() { - /* - * TODO: this assumes that the stride is equal to the real width and that the - * padding follows the end of the chroma plane. It could be improved by - * following the model in v4l2-ctl-streaming.cpp read_write_padded_frame() - */ - unsigned expected_length = ctx_trace.width * ctx_trace.height; - if (ctx_trace.pixelformat == V4L2_PIX_FMT_NV12 || ctx_trace.pixelformat == V4L2_PIX_FMT_YUV420) { - expected_length *= 3; - expected_length /= 2; - expected_length += (expected_length % 2); - }
utils/v4l2-tracer/trace-helper.cpp void print_buffers_trace(void) · line 249
Quoted 10 lines
utils/v4l2-tracer/trace-helper.cpp +9
-1- return expected_length; + const auto *info = v4l2_fwht_find_pixfmt(ctx_trace.pixelformat); + if (!info) + return 0; + + unsigned coded_height = ctx_trace.height; + unsigned expected = 0; + + for (unsigned plane_idx = 0; plane_idx < info->planes_num; ++plane_idx) { + unsigned stride = ctx_trace.plane_bytesperline[plane_idx];
Ah, this does not work. All the pixelformats that v4l2_fwht_find_pixfmt knows are all single buffer formats, i.e. all planes are combined in a single buffer. So there is also just one bytesperline value.
I know, it's very confusing: the _MPLANE buffer types should really be called _MBUFFER in hindsight. I.e. each plane has its own buffer as opposed to concatenating all planes in a single buffer.
I'm not sure what problem you are attempting to fix, but I don't think this does what you want it to do.
Although patch 1/2 looks fine, I'll drop both patches since it makes no sense in combination with this patch.
It really only makes sense for multiplanar pixelformats like V4L2_PIX_FMT_NV12M, V4L2_PIX_FMT_YUV420M, etc., but that's not what this second patch deals with.
Regards,
Hans
Quoted 17 lines
utils/v4l2-tracer/trace-helper.cpp +14
-0+ + bool is_chroma = (plane_idx == 1 || plane_idx == 2); + unsigned h_div = is_chroma ? info->height_div : 1; + + if (info->planes_num == 3 && plane_idx == 1) + stride /= 2; + + if (plane_idx == 1 && + (info->id == V4L2_PIX_FMT_NV24 || info->id == V4L2_PIX_FMT_NV42)) + stride *= 2; + + expected += stride * (coded_height / h_div); + } + return expected; } void s_ext_ctrls_setup(struct v4l2_ext_controls *ext_controls)
Re: [PATCH v4l-utils 2/2] calculate expected length using v4l2-fwht info
Hi,
Thanks for your review.
This patch was intended to address the TODO in get_expected_length_trace(), which assumes that the stride equals the real width and that padding follows the end of the chroma plane. I adjusted the implementation for single-buffer formats and sent a V2.
Regards,
Sarah
בתאריך יום ג׳, 17 במרץ 2026 ב-14:10 מאת Hans Verkuil <[email protected]>:
Quoted 93 lines
Hi Sarah,
Quoted 39 lines · Sarah Gershuni
Update
get_expected_length_traceto calculate the expected buffer length based onv4l2_fwht_find_pixfmtinfo.Signed-off-by Sarah Gershuni <[email protected]>--- utils/v4l2-tracer/trace-helper.cpp | 40 +++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-)utils/v4l2-tracer/trace-helper.cpp +5
-11@@ -6,6 +6,11 @@ #include "trace.h" #include <math.h> +extern "C" { +#include "codec-v4l2-fwht.h" +} + + struct trace_context ctx_trace = {}; bool is_video_or_media_device(const char *path)@@ -233,18 +238,29 @@ void print_buffers_trace(void) unsigned get_expected_length_trace() { - /* - * TODO: this assumes that the stride is equal to the real width and that the - * padding follows the end of the chroma plane. It could be improved by - * following the model in v4l2-ctl-streaming.cpp read_write_padded_frame() - */ - unsigned expected_length = ctx_trace.width * ctx_trace.height; - if (ctx_trace.pixelformat == V4L2_PIX_FMT_NV12 || ctx_trace.pixelformat == V4L2_PIX_FMT_YUV420) { - expected_length *= 3; - expected_length /= 2; - expected_length += (expected_length % 2); - }utils/v4l2-tracer/trace-helper.cpp void print_buffers_trace(void) · line 249
Quoted 10 lines
utils/v4l2-tracer/trace-helper.cpp +9
-1- return expected_length; + const auto *info = v4l2_fwht_find_pixfmt(ctx_trace.pixelformat); + if (!info) + return 0; + + unsigned coded_height = ctx_trace.height; + unsigned expected = 0; + + for (unsigned plane_idx = 0; plane_idx < info->planes_num; ++plane_idx) { + unsigned stride = ctx_trace.plane_bytesperline[plane_idx];Ah, this does not work. All the pixelformats that
v4l2_fwht_find_pixfmtknows are all single buffer formats, i.e. all planes are combined in a single buffer. So there is also just one bytesperline value.I know, it's very confusing: the
_MPLANEbuffer types should really be called_MBUFFERin hindsight. I.e. each plane has its own buffer as opposed to concatenating all planes in a single buffer.I'm not sure what problem you are attempting to fix, but I don't think this does what you want it to do.
Although patch 1/2 looks fine, I'll drop both patches since it makes no sense in combination with this patch.
It really only makes sense for multiplanar pixelformats like
V4L2_PIX_FMT_NV12M,V4L2_PIX_FMT_YUV420M, etc., but that's not what this second patch deals with.Regards,
Hans
Quoted 17 lines
utils/v4l2-tracer/trace-helper.cpp +14
-0+ + bool is_chroma = (plane_idx == 1 || plane_idx == 2); + unsigned h_div = is_chroma ? info->height_div : 1; + + if (info->planes_num == 3 && plane_idx == 1) + stride /= 2; + + if (plane_idx == 1 && + (info->id == V4L2_PIX_FMT_NV24 || info->id == V4L2_PIX_FMT_NV42)) + stride *= 2; + + expected += stride * (coded_height / h_div); + } + return expected; } void s_ext_ctrls_setup(struct v4l2_ext_controls *ext_controls)