Skip to contentSkip to conversation

utils: add v4l2-tracer utility

PATCH Superseded Deborah Brouwer 2 participants 3 messages Sep 17, 2022 – Sep 27, 2022 Media (V4L/DVB)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 utils: add v4l2-tracer utility

The v4l2-tracer utility traces, records and replays userspace applications that implement the v4l2 memory-to-memory stateless video decoder interface. The trace function intercepts and copies all system calls, stateless codec controls and encoded data to a json-formatted trace file. The retrace function independently reads and replays the json trace file. The json trace file can be retraced independently from its original userspace environment and can be edited to inject errors to test a driver's error handling abilities.

The v4l2-tracer currently supports VP8, H264 and FWHT formats.

---
This patch supercedes:
[RFC,1/2] utils: add stateless tracer utility
[RFC,2/2] utils: add stateless retracer utility

Changes since RFC:
- combined tracer/retracer into one utility
- added h264 and fwht formats
- removed hard-coded link to shared library
- added help options and man page
- reduced json-c version requirements to 0.15

Examples:

Trace VP8 decoding:

v4l2-tracer trace -- gst-launch-1.0 -- filesrc location=test-25fps.vp8 ! parsebin ! v4l2slvp8dec ! videocodectestsink

Trace H264 decoding:

v4l2-tracer trace -y -- gst-launch-1.0 -- filesrc location=test-25fps.h264 ! parsebin ! v4l2slh264dec ! videocodectestsink

Trace FWHT decoding:

  v4l2-tracer trace -- v4l2-ctl -d9 --stream-mmap
  --stream-out-mmap --stream-from-hdr test-25fps.fwht
  --stream-to out.yuv

Retrace:
v4l2-tracer retrace 79568_trace.json

 configure.ac                         |   12 +
 utils/Makefile.am                    |    5 +
 utils/common/v4l2-info.cpp           |    7 +-
 utils/common/v4l2-info.h             |    8 +
 utils/v4l2-tracer/.gitignore         |   12 +
 utils/v4l2-tracer/Makefile.am        |   23 +
 utils/v4l2-tracer/libtracer.cpp      |  209 +++++
 utils/v4l2-tracer/libtracer.h        |   35 +
 utils/v4l2-tracer/retrace-fwht.cpp   |   54 ++
 utils/v4l2-tracer/retrace-fwht.h     |   11 +
 utils/v4l2-tracer/retrace-h264.cpp   |  446 +++++++++
 utils/v4l2-tracer/retrace-h264.h     |   16 +
 utils/v4l2-tracer/retrace-helper.cpp |  157 ++++
 utils/v4l2-tracer/retrace-helper.h   |   44 +
 utils/v4l2-tracer/retrace-vp8.cpp    |  288 ++++++
 utils/v4l2-tracer/retrace-vp8.h      |   11 +
 utils/v4l2-tracer/retracer.cpp       | 1281 ++++++++++++++++++++++++++
 utils/v4l2-tracer/trace-fwht.cpp     |   24 +
 utils/v4l2-tracer/trace-fwht.h       |   11 +
 utils/v4l2-tracer/trace-h264.cpp     |  281 ++++++
 utils/v4l2-tracer/trace-h264.h       |   18 +
 utils/v4l2-tracer/trace-helper.cpp   |  667 ++++++++++++++
 utils/v4l2-tracer/trace-helper.h     |   81 ++
 utils/v4l2-tracer/trace-info.cpp     |  460 +++++++++
 utils/v4l2-tracer/trace-info.h       |   93 ++
 utils/v4l2-tracer/trace-vp8.cpp      |  183 ++++
 utils/v4l2-tracer/trace-vp8.h        |   11 +
 utils/v4l2-tracer/trace.cpp          |  582 ++++++++++++
 utils/v4l2-tracer/trace.h            |   17 +
 utils/v4l2-tracer/v4l2-tracer.1.in   |  109 +++
 utils/v4l2-tracer/v4l2-tracer.cpp    |  181 ++++
 utils/v4l2-tracer/v4l2-tracer.h      |   39 +
 32 files changed, 5370 insertions(+), 6 deletions(-)
 create mode 100644 utils/v4l2-tracer/.gitignore
 create mode 100644 utils/v4l2-tracer/Makefile.am
 create mode 100644 utils/v4l2-tracer/libtracer.cpp
 create mode 100644 utils/v4l2-tracer/libtracer.h
 create mode 100644 utils/v4l2-tracer/retrace-fwht.cpp
 create mode 100644 utils/v4l2-tracer/retrace-fwht.h
 create mode 100644 utils/v4l2-tracer/retrace-h264.cpp
 create mode 100644 utils/v4l2-tracer/retrace-h264.h
 create mode 100644 utils/v4l2-tracer/retrace-helper.cpp
 create mode 100644 utils/v4l2-tracer/retrace-helper.h
 create mode 100644 utils/v4l2-tracer/retrace-vp8.cpp
 create mode 100644 utils/v4l2-tracer/retrace-vp8.h
 create mode 100644 utils/v4l2-tracer/retracer.cpp
 create mode 100644 utils/v4l2-tracer/trace-fwht.cpp
 create mode 100644 utils/v4l2-tracer/trace-fwht.h
 create mode 100644 utils/v4l2-tracer/trace-h264.cpp
 create mode 100644 utils/v4l2-tracer/trace-h264.h
 create mode 100644 utils/v4l2-tracer/trace-helper.cpp
 create mode 100644 utils/v4l2-tracer/trace-helper.h
 create mode 100644 utils/v4l2-tracer/trace-info.cpp
 create mode 100644 utils/v4l2-tracer/trace-info.h
 create mode 100644 utils/v4l2-tracer/trace-vp8.cpp
 create mode 100644 utils/v4l2-tracer/trace-vp8.h
 create mode 100644 utils/v4l2-tracer/trace.cpp
 create mode 100644 utils/v4l2-tracer/trace.h
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.1.in
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.cpp
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.h
configure.ac +12 -0
index 05298981..39d83579 100644
@@ -32,6 +32,8 @@ AC_CONFIG_FILES([Makefile
 	utils/v4l2-ctl/Makefile
 	utils/v4l2-dbg/Makefile
 	utils/v4l2-sysfs-path/Makefile
+	utils/v4l2-tracer/Makefile
+	utils/v4l2-tracer/v4l2-tracer.1
 	utils/qv4l2/Makefile
 	utils/libcecutil/Makefile
 	utils/cec-ctl/Makefile
@@ -311,6 +313,15 @@ AS_IF([test "x$with_libudev" != xno -o "x$enable_libdvbv5" != xno],
 
 AC_SUBST([JPEG_LIBS])
 
+JSONC_VERSION_REQUIRED="0.15";
+PKG_CHECK_MODULES(JSONC, [json-c >= $JSONC_VERSION_REQUIRED], [jsonc_pkgconfig=yes], [jsonc_pkgconfig=no])
+AC_SUBST([JSONC_CFLAGS])
+AC_SUBST([JSONC_LIBS])
+AM_CONDITIONAL([HAVE_JSONC], [test x$jsonc_pkgconfig = xyes])
+if test "x$jsonc_pkgconfig" = "xno"; then
+    AC_MSG_WARN(json-c $JSONC_VERSION_REQUIRED or higher required for v4l2-tracer)
+fi
+
 # Check for pthread
 
 AS_IF([test x$enable_shared != xno],
@@ -645,6 +656,7 @@ compile time options summary
     QT version                 : $QT_VERSION
     ALSA support               : $USE_ALSA
     SDL support		       : $sdl_pc
+    JSON-C                     : $jsonc_pkgconfig >= $JSONC_VERSION_REQUIRED
 
     build dynamic libs         : $enable_shared
     build static libs          : $enable_static
utils/Makefile.am +5 -0
index 0e68a612..6f59515e 100644
@@ -15,6 +15,11 @@ SUBDIRS = \
 	cec-follower \
 	rds-ctl
 
+if HAVE_JSONC
+SUBDIRS += \
+	v4l2-tracer
+endif
+
 if WITH_LIBDVBV5
 SUBDIRS += \
 	dvb
utils/common/v4l2-info.cpp +1 -6
index c4dbb14e..98122b8b 100644
@@ -16,12 +16,7 @@ static std::string num2s(unsigned num, bool is_hex = true)
 	return buf;
 }
 
-struct flag_def {
-	unsigned flag;
-	const char *str;
-};
-
-static std::string flags2s(unsigned val, const flag_def *def)
+std::string flags2s(unsigned int val, const flag_def *def)
 {
 	std::string s;
 
utils/common/v4l2-info.h +8 -0
index 35237853..8eb14bc4 100644
@@ -11,6 +11,14 @@
 #include <linux/videodev2.h>
 #include <linux/v4l2-subdev.h>
 
+struct flag_def {
+	unsigned int flag;
+	const char *str;
+};
+
+/* Return a comma-separated string of flags or hex value if unknown */
+std::string flags2s(unsigned int val, const flag_def *def);
+
 /* Print capability information */
 void v4l2_info_capability(const v4l2_capability &cap);
 void v4l2_info_subdev_capability(const v4l2_subdev_capability &subdevcap);
utils/v4l2-tracer/.gitignore new file +12 -0
new file mode 100644
index 00000000..b9e273a7
@@ -0,0 +1,12 @@
+*.json
+*.yuv
+*.webm
+*.h264
+*.ivf
+*.vp8
+*.vp9
+*.fwht
+*.mp4
+v4l2-tracer
+.clang-tidy
+v4l2-tracer.1
utils/v4l2-tracer/Makefile.am new file +23 -0
new file mode 100644
index 00000000..d37558a5
@@ -0,0 +1,23 @@
+if HAVE_JSONC
+
+lib_LTLIBRARIES = libtracer.la
+libtracer_la_SOURCES = libtracer.cpp trace.cpp trace-vp8.cpp trace-h264.cpp trace-fwht.cpp \
+trace-helper.cpp trace-info.cpp $(top_srcdir)/utils/common/v4l2-info.cpp \
+$(top_srcdir)/utils/common/media-info.cpp
+
+libtracer_la_CPPFLAGS = -I$(top_srcdir)/utils/common $(JSONC_CFLAGS)
+libtracer_la_LDFLAGS = -avoid-version -module -shared -export-dynamic -ldl $(JSONC_LIBS)
+libtracer_la_LIBTOOLFLAGS = --tag=disable-static
+
+bin_PROGRAMS = v4l2-tracer
+man_MANS = v4l2-tracer.1
+
+v4l2_tracer_SOURCES = v4l2-tracer.cpp retracer.cpp trace-info.cpp retrace-helper.cpp retrace-vp8.cpp \
+retrace-h264.cpp retrace-fwht.cpp  $(top_srcdir)/utils/common/v4l2-info.cpp
+
+v4l2_tracer_CPPFLAGS = -I$(top_srcdir)/utils/common -DLIBTRACER_PATH=$(libdir) $(JSONC_CFLAGS)
+v4l2_tracer_LDFLAGS = -lrt -lpthread $(JSONC_LIBS)
+
+EXTRA_DIST = v4l2-tracer.1
+
+endif
utils/v4l2-tracer/libtracer.cpp new file +209 -0

209 lines not on this page · raw message

utils/v4l2-tracer/libtracer.h new file +35 -0
new file mode 100644
index 00000000..f528fa8f
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef LIBTRACER_H
+#define LIBTRACER_H
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <math.h> /* for pow */
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <config.h>
+#include <pthread.h>
+#include <unistd.h> /* for close */
+#include <linux/dma-buf.h>
+#include <json-c/json.h>
+#include <unordered_map>
+#include <algorithm> /* for std::find */
+#include <list>
+#include <v4l2-info.h>
+#include "trace.h"
+#include "trace-info.h"
+#include "trace-helper.h"
+#include "trace-vp8.h"
+#include "trace-h264.h"
+#include "trace-fwht.h"
+
+#endif
utils/v4l2-tracer/retrace-fwht.cpp new file +54 -0

54 lines not on this page · raw message

utils/v4l2-tracer/retrace-fwht.h new file +11 -0
new file mode 100644
index 00000000..db814f03
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef RETRACE_FWHT_H
+#define RETRACE_FWHT_H
+
+struct v4l2_ctrl_fwht_params *retrace_v4l2_ctrl_fwht_params(json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/retrace-h264.cpp new file +446 -0

446 lines not on this page · raw message

utils/v4l2-tracer/retrace-h264.h new file +16 -0
new file mode 100644
index 00000000..37dbdd87
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef RETRACE_H264_H
+#define RETRACE_H264_H
+
+struct v4l2_ctrl_h264_sps *retrace_v4l2_ctrl_h264_sps_pointer(json_object *ctrl_obj);
+struct v4l2_ctrl_h264_pps *retrace_v4l2_ctrl_h264_pps_pointer(json_object *ctrl_obj);
+struct v4l2_ctrl_h264_scaling_matrix *retrace_v4l2_ctrl_h264_scaling_matrix_pointer(json_object *ctrl_obj);
+struct v4l2_ctrl_h264_pred_weights *retrace_v4l2_ctrl_h264_pred_weights_pointer(json_object *ctrl_obj);
+struct v4l2_ctrl_h264_slice_params *retrace_v4l2_ctrl_h264_slice_params_pointer(json_object *h264_weight_factors_obj);
+struct v4l2_ctrl_h264_decode_params *retrace_v4l2_ctrl_h264_decode_params(json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/retrace-helper.cpp new file +157 -0

157 lines not on this page · raw message

utils/v4l2-tracer/retrace-helper.h new file +44 -0

44 lines not on this page · raw message

utils/v4l2-tracer/retrace-vp8.cpp new file +288 -0

288 lines not on this page · raw message

utils/v4l2-tracer/retrace-vp8.h new file +11 -0
new file mode 100644
index 00000000..a5bea25e
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef RETRACE_VP8_H
+#define RETRACE_VP8_H
+
+struct v4l2_ctrl_vp8_frame *retrace_v4l2_ctrl_vp8_frame_pointer(json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/retracer.cpp new file +1281 -0

1,281 lines not on this page · raw message

utils/v4l2-tracer/trace-fwht.cpp new file +24 -0
new file mode 100644
index 00000000..4d31df51
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "libtracer.h"
+
+void trace_v4l2_ctrl_fwht_params(void *p_fwht_params, json_object *ctrl_obj)
+{
+	json_object *fwht_params_obj = json_object_new_object();
+	struct v4l2_ctrl_fwht_params *fwht_params = static_cast<struct v4l2_ctrl_fwht_params*>(p_fwht_params);
+
+	json_object_object_add(fwht_params_obj, "backward_ref_ts", json_object_new_uint64(fwht_params->backward_ref_ts));
+	json_object_object_add(fwht_params_obj, "version", json_object_new_int64(fwht_params->version));
+	json_object_object_add(fwht_params_obj, "width", json_object_new_int64(fwht_params->width));
+	json_object_object_add(fwht_params_obj, "height", json_object_new_int64(fwht_params->height));
+	json_object_object_add(fwht_params_obj, "flags", json_object_new_int64(fwht_params->flags));
+	json_object_object_add(fwht_params_obj, "colorspace", json_object_new_int64(fwht_params->colorspace));
+	json_object_object_add(fwht_params_obj, "xfer_func", json_object_new_int64(fwht_params->xfer_func));
+	json_object_object_add(fwht_params_obj, "ycbcr_enc", json_object_new_int64(fwht_params->ycbcr_enc));
+	json_object_object_add(fwht_params_obj, "quantization", json_object_new_int64(fwht_params->quantization));
+
+	json_object_object_add(ctrl_obj, "v4l2_ctrl_fwht_params", fwht_params_obj);
+}
utils/v4l2-tracer/trace-fwht.h new file +11 -0
new file mode 100644
index 00000000..566a3eca
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef TRACE_FWHT_H
+#define TRACE_FWHT_H
+
+void trace_v4l2_ctrl_fwht_params(void *p_fwht_params, json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/trace-h264.cpp new file +281 -0

281 lines not on this page · raw message

utils/v4l2-tracer/trace-h264.h new file +18 -0
new file mode 100644
index 00000000..fea0f598
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef TRACE_H264_H
+#define TRACE_H264_H
+
+void trace_v4l2_ctrl_h264_decode_mode(struct v4l2_ext_control ctrl, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_start_code(struct v4l2_ext_control ctrl, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_sps(void *p_h264_sps, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_pps(void *p_h264_pps, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_scaling_matrix(void *p_h264_scaling_matrix, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_pred_weights(void *p_h264_pred_weights, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_slice_params(void *p_h264_slice_params, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_decode_params(void *p_h264_decode_params, json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/trace-helper.cpp new file +667 -0

11 lines not on this page · raw message

utils/v4l2-tracer/trace-helper.h new file +81 -0

81 lines not on this page · raw message

utils/v4l2-tracer/trace-info.cpp new file +460 -0

460 lines not on this page · raw message

utils/v4l2-tracer/trace-info.h new file +93 -0

93 lines not on this page · raw message

utils/v4l2-tracer/trace-vp8.cpp new file +183 -0

183 lines not on this page · raw message

utils/v4l2-tracer/trace-vp8.h new file +11 -0
new file mode 100644
index 00000000..96187eea

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/trace.cpp new file +582 -0

582 lines not on this page · raw message

utils/v4l2-tracer/trace.h new file +17 -0
new file mode 100644
index 00000000..9b431fea

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/v4l2-tracer.1.in new file +109 -0

109 lines not on this page · raw message

utils/v4l2-tracer/v4l2-tracer.cpp new file +181 -0

181 lines not on this page · raw message

utils/v4l2-tracer/v4l2-tracer.h new file +39 -0
new file mode 100644
index 00000000..7b280b4f

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.37.3

Part of the diff is not shown here. View the raw message.

#1 lore raw

Hi Deb,

Quoted 10 lines · Deborah Brouwer

The v4l2-tracer utility traces, records and replays userspace applications that implement the v4l2 memory-to-memory stateless video decoder interface. The trace function intercepts and copies all system calls, stateless codec controls and encoded data to a json-formatted trace file. The retrace function independently reads and replays the json trace file. The json trace file can be retraced independently from its original userspace environment and can be edited to inject errors to test a driver's error handling abilities.

The v4l2-tracer currently supports VP8, H264 and FWHT formats.

Thank you for working on this, very nice!

A more in-depth review will follow (hopefully next week), but I did have one high-level naming question:

Quoted 37 lines

---
This patch supercedes:
[RFC,1/2] utils: add stateless tracer utility
[RFC,2/2] utils: add stateless retracer utility

Changes since RFC:
- combined tracer/retracer into one utility
- added h264 and fwht formats
- removed hard-coded link to shared library
- added help options and man page
- reduced json-c version requirements to 0.15

Examples:

Trace VP8 decoding:

v4l2-tracer trace -- gst-launch-1.0 -- filesrc location=test-25fps.vp8 ! parsebin ! v4l2slvp8dec ! videocodectestsink

Trace H264 decoding:

v4l2-tracer trace -y -- gst-launch-1.0 -- filesrc location=test-25fps.h264 ! parsebin ! v4l2slh264dec ! videocodectestsink

Trace FWHT decoding:

  v4l2-tracer trace -- v4l2-ctl -d9 --stream-mmap
  --stream-out-mmap --stream-from-hdr test-25fps.fwht
  --stream-to out.yuv

Retrace:
v4l2-tracer retrace 79568_trace.json

Quoted 8 lines
 configure.ac                         |   12 +
 utils/Makefile.am                    |    5 +
 utils/common/v4l2-info.cpp           |    7 +-
 utils/common/v4l2-info.h             |    8 +
 utils/v4l2-tracer/.gitignore         |   12 +
 utils/v4l2-tracer/Makefile.am        |   23 +
 utils/v4l2-tracer/libtracer.cpp      |  209 +++++
 utils/v4l2-tracer/libtracer.h        |   35 +

If I understood it right, this will install a libtracer library?

In that case, the library name is much too generic, it should be renamed to libv4l2tracer.

Regards,

Hans

Quoted 54 lines
 utils/v4l2-tracer/retrace-fwht.cpp   |   54 ++
 utils/v4l2-tracer/retrace-fwht.h     |   11 +
 utils/v4l2-tracer/retrace-h264.cpp   |  446 +++++++++
 utils/v4l2-tracer/retrace-h264.h     |   16 +
 utils/v4l2-tracer/retrace-helper.cpp |  157 ++++
 utils/v4l2-tracer/retrace-helper.h   |   44 +
 utils/v4l2-tracer/retrace-vp8.cpp    |  288 ++++++
 utils/v4l2-tracer/retrace-vp8.h      |   11 +
 utils/v4l2-tracer/retracer.cpp       | 1281 ++++++++++++++++++++++++++
 utils/v4l2-tracer/trace-fwht.cpp     |   24 +
 utils/v4l2-tracer/trace-fwht.h       |   11 +
 utils/v4l2-tracer/trace-h264.cpp     |  281 ++++++
 utils/v4l2-tracer/trace-h264.h       |   18 +
 utils/v4l2-tracer/trace-helper.cpp   |  667 ++++++++++++++
 utils/v4l2-tracer/trace-helper.h     |   81 ++
 utils/v4l2-tracer/trace-info.cpp     |  460 +++++++++
 utils/v4l2-tracer/trace-info.h       |   93 ++
 utils/v4l2-tracer/trace-vp8.cpp      |  183 ++++
 utils/v4l2-tracer/trace-vp8.h        |   11 +
 utils/v4l2-tracer/trace.cpp          |  582 ++++++++++++
 utils/v4l2-tracer/trace.h            |   17 +
 utils/v4l2-tracer/v4l2-tracer.1.in   |  109 +++
 utils/v4l2-tracer/v4l2-tracer.cpp    |  181 ++++
 utils/v4l2-tracer/v4l2-tracer.h      |   39 +
 32 files changed, 5370 insertions(+), 6 deletions(-)
 create mode 100644 utils/v4l2-tracer/.gitignore
 create mode 100644 utils/v4l2-tracer/Makefile.am
 create mode 100644 utils/v4l2-tracer/libtracer.cpp
 create mode 100644 utils/v4l2-tracer/libtracer.h
 create mode 100644 utils/v4l2-tracer/retrace-fwht.cpp
 create mode 100644 utils/v4l2-tracer/retrace-fwht.h
 create mode 100644 utils/v4l2-tracer/retrace-h264.cpp
 create mode 100644 utils/v4l2-tracer/retrace-h264.h
 create mode 100644 utils/v4l2-tracer/retrace-helper.cpp
 create mode 100644 utils/v4l2-tracer/retrace-helper.h
 create mode 100644 utils/v4l2-tracer/retrace-vp8.cpp
 create mode 100644 utils/v4l2-tracer/retrace-vp8.h
 create mode 100644 utils/v4l2-tracer/retracer.cpp
 create mode 100644 utils/v4l2-tracer/trace-fwht.cpp
 create mode 100644 utils/v4l2-tracer/trace-fwht.h
 create mode 100644 utils/v4l2-tracer/trace-h264.cpp
 create mode 100644 utils/v4l2-tracer/trace-h264.h
 create mode 100644 utils/v4l2-tracer/trace-helper.cpp
 create mode 100644 utils/v4l2-tracer/trace-helper.h
 create mode 100644 utils/v4l2-tracer/trace-info.cpp
 create mode 100644 utils/v4l2-tracer/trace-info.h
 create mode 100644 utils/v4l2-tracer/trace-vp8.cpp
 create mode 100644 utils/v4l2-tracer/trace-vp8.h
 create mode 100644 utils/v4l2-tracer/trace.cpp
 create mode 100644 utils/v4l2-tracer/trace.h
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.1.in
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.cpp
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.h
#2 lore raw
In reply to Deborah Brouwer

Hi Deb,

I finally have some time for a review!

Some high-level comments first:

I mentioned already that libtracer should be renamed since the name is too generic. libv4l2tracer would make more sense.

I also see that the README needs updating. Esp. by mentioning which json package needs to be installed (libjson-c-dev for debian).

v4l2-tracer --help shows:

v4l2-tracer usage:
v4l2-tracer {trace|retrace}
v4l2-tracer trace [trace options] -- <tracee> v4l2-tracer retrace [retrace options] -- <trace_file>.json

This definitely needs to be expanded to show the options as well.

I'm not sure what the purpose of the first line is: "v4l2-tracer {trace|retrace}"; it seems superfluous given the following two lines.

Quoted 189 lines · Deborah Brouwer

The v4l2-tracer utility traces, records and replays userspace applications that implement the v4l2 memory-to-memory stateless video decoder interface. The trace function intercepts and copies all system calls, stateless codec controls and encoded data to a json-formatted trace file. The retrace function independently reads and replays the json trace file. The json trace file can be retraced independently from its original userspace environment and can be edited to inject errors to test a driver's error handling abilities.

The v4l2-tracer currently supports VP8, H264 and FWHT formats.

---
This patch supercedes:
[RFC,1/2] utils: add stateless tracer utility
[RFC,2/2] utils: add stateless retracer utility

Changes since RFC:
- combined tracer/retracer into one utility
- added h264 and fwht formats
- removed hard-coded link to shared library
- added help options and man page
- reduced json-c version requirements to 0.15

Examples:

Trace VP8 decoding:

v4l2-tracer trace -- gst-launch-1.0 -- filesrc location=test-25fps.vp8 ! parsebin ! v4l2slvp8dec ! videocodectestsink

Trace H264 decoding:

v4l2-tracer trace -y -- gst-launch-1.0 -- filesrc location=test-25fps.h264 ! parsebin ! v4l2slh264dec ! videocodectestsink

Trace FWHT decoding:

  v4l2-tracer trace -- v4l2-ctl -d9 --stream-mmap
  --stream-out-mmap --stream-from-hdr test-25fps.fwht
  --stream-to out.yuv

Retrace:
v4l2-tracer retrace 79568_trace.json

 configure.ac                         |   12 +
 utils/Makefile.am                    |    5 +
 utils/common/v4l2-info.cpp           |    7 +-
 utils/common/v4l2-info.h             |    8 +
 utils/v4l2-tracer/.gitignore         |   12 +
 utils/v4l2-tracer/Makefile.am        |   23 +
 utils/v4l2-tracer/libtracer.cpp      |  209 +++++
 utils/v4l2-tracer/libtracer.h        |   35 +
 utils/v4l2-tracer/retrace-fwht.cpp   |   54 ++
 utils/v4l2-tracer/retrace-fwht.h     |   11 +
 utils/v4l2-tracer/retrace-h264.cpp   |  446 +++++++++
 utils/v4l2-tracer/retrace-h264.h     |   16 +
 utils/v4l2-tracer/retrace-helper.cpp |  157 ++++
 utils/v4l2-tracer/retrace-helper.h   |   44 +
 utils/v4l2-tracer/retrace-vp8.cpp    |  288 ++++++
 utils/v4l2-tracer/retrace-vp8.h      |   11 +
 utils/v4l2-tracer/retracer.cpp       | 1281 ++++++++++++++++++++++++++
 utils/v4l2-tracer/trace-fwht.cpp     |   24 +
 utils/v4l2-tracer/trace-fwht.h       |   11 +
 utils/v4l2-tracer/trace-h264.cpp     |  281 ++++++
 utils/v4l2-tracer/trace-h264.h       |   18 +
 utils/v4l2-tracer/trace-helper.cpp   |  667 ++++++++++++++
 utils/v4l2-tracer/trace-helper.h     |   81 ++
 utils/v4l2-tracer/trace-info.cpp     |  460 +++++++++
 utils/v4l2-tracer/trace-info.h       |   93 ++
 utils/v4l2-tracer/trace-vp8.cpp      |  183 ++++
 utils/v4l2-tracer/trace-vp8.h        |   11 +
 utils/v4l2-tracer/trace.cpp          |  582 ++++++++++++
 utils/v4l2-tracer/trace.h            |   17 +
 utils/v4l2-tracer/v4l2-tracer.1.in   |  109 +++
 utils/v4l2-tracer/v4l2-tracer.cpp    |  181 ++++
 utils/v4l2-tracer/v4l2-tracer.h      |   39 +
 32 files changed, 5370 insertions(+), 6 deletions(-)
 create mode 100644 utils/v4l2-tracer/.gitignore
 create mode 100644 utils/v4l2-tracer/Makefile.am
 create mode 100644 utils/v4l2-tracer/libtracer.cpp
 create mode 100644 utils/v4l2-tracer/libtracer.h
 create mode 100644 utils/v4l2-tracer/retrace-fwht.cpp
 create mode 100644 utils/v4l2-tracer/retrace-fwht.h
 create mode 100644 utils/v4l2-tracer/retrace-h264.cpp
 create mode 100644 utils/v4l2-tracer/retrace-h264.h
 create mode 100644 utils/v4l2-tracer/retrace-helper.cpp
 create mode 100644 utils/v4l2-tracer/retrace-helper.h
 create mode 100644 utils/v4l2-tracer/retrace-vp8.cpp
 create mode 100644 utils/v4l2-tracer/retrace-vp8.h
 create mode 100644 utils/v4l2-tracer/retracer.cpp
 create mode 100644 utils/v4l2-tracer/trace-fwht.cpp
 create mode 100644 utils/v4l2-tracer/trace-fwht.h
 create mode 100644 utils/v4l2-tracer/trace-h264.cpp
 create mode 100644 utils/v4l2-tracer/trace-h264.h
 create mode 100644 utils/v4l2-tracer/trace-helper.cpp
 create mode 100644 utils/v4l2-tracer/trace-helper.h
 create mode 100644 utils/v4l2-tracer/trace-info.cpp
 create mode 100644 utils/v4l2-tracer/trace-info.h
 create mode 100644 utils/v4l2-tracer/trace-vp8.cpp
 create mode 100644 utils/v4l2-tracer/trace-vp8.h
 create mode 100644 utils/v4l2-tracer/trace.cpp
 create mode 100644 utils/v4l2-tracer/trace.h
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.1.in
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.cpp
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.h
configure.ac +12 -0
index 05298981..39d83579 100644
@@ -32,6 +32,8 @@ AC_CONFIG_FILES([Makefile
 	utils/v4l2-ctl/Makefile
 	utils/v4l2-dbg/Makefile
 	utils/v4l2-sysfs-path/Makefile
+	utils/v4l2-tracer/Makefile
+	utils/v4l2-tracer/v4l2-tracer.1
 	utils/qv4l2/Makefile
 	utils/libcecutil/Makefile
 	utils/cec-ctl/Makefile
@@ -311,6 +313,15 @@ AS_IF([test "x$with_libudev" != xno -o "x$enable_libdvbv5" != xno],
 
 AC_SUBST([JPEG_LIBS])
 
+JSONC_VERSION_REQUIRED="0.15";
+PKG_CHECK_MODULES(JSONC, [json-c >= $JSONC_VERSION_REQUIRED], [jsonc_pkgconfig=yes], [jsonc_pkgconfig=no])
+AC_SUBST([JSONC_CFLAGS])
+AC_SUBST([JSONC_LIBS])
+AM_CONDITIONAL([HAVE_JSONC], [test x$jsonc_pkgconfig = xyes])
+if test "x$jsonc_pkgconfig" = "xno"; then
+    AC_MSG_WARN(json-c $JSONC_VERSION_REQUIRED or higher required for v4l2-tracer)
+fi
+
 # Check for pthread
 
 AS_IF([test x$enable_shared != xno],
@@ -645,6 +656,7 @@ compile time options summary
     QT version                 : $QT_VERSION
     ALSA support               : $USE_ALSA
     SDL support		       : $sdl_pc
+    JSON-C                     : $jsonc_pkgconfig >= $JSONC_VERSION_REQUIRED
 
     build dynamic libs         : $enable_shared
     build static libs          : $enable_static
utils/Makefile.am +5 -0
index 0e68a612..6f59515e 100644
@@ -15,6 +15,11 @@ SUBDIRS = \
 	cec-follower \
 	rds-ctl
 
+if HAVE_JSONC
+SUBDIRS += \
+	v4l2-tracer
+endif
+
 if WITH_LIBDVBV5
 SUBDIRS += \
 	dvb
utils/common/v4l2-info.cpp +1 -6
index c4dbb14e..98122b8b 100644
@@ -16,12 +16,7 @@ static std::string num2s(unsigned num, bool is_hex = true)
 	return buf;
 }
 
-struct flag_def {
-	unsigned flag;
-	const char *str;
-};
-
-static std::string flags2s(unsigned val, const flag_def *def)
+std::string flags2s(unsigned int val, const flag_def *def)
 {
 	std::string s;
 
utils/common/v4l2-info.h +1 -0
index 35237853..8eb14bc4 100644
@@ -11,6 +11,14 @@
 #include <linux/videodev2.h>
 #include <linux/v4l2-subdev.h>
 
+struct flag_def {

utils/common/v4l2-info.h line 24

Quoted 10 lines
utils/common/v4l2-info.h +7 -0
+	unsigned int flag;
+	const char *str;
+};
+
+/* Return a comma-separated string of flags or hex value if unknown */
+std::string flags2s(unsigned int val, const flag_def *def);
+
 /* Print capability information */
 void v4l2_info_capability(const v4l2_capability &cap);
 void v4l2_info_subdev_capability(const v4l2_subdev_capability &subdevcap);

Split off these v4l2-info changes into a separate patch. I'll just take that as-is, since it makes sense.

Quoted 6 lines
utils/v4l2-tracer/.gitignore new file +0 -0
new file mode 100644
index 00000000..b9e273a7

utils/v4l2-tracer/.gitignore line 9

Quoted 9 lines
utils/v4l2-tracer/.gitignore new file +9 -0
@@ -0,0 +1,12 @@
+*.json
+*.yuv
+*.webm
+*.h264
+*.ivf
+*.vp8
+*.vp9
+*.fwht
+*.mp4

Why are these files ignored? They are not generated during compilation, so I don't think these belong here.

utils/v4l2-tracer/.gitignore line 11

Quoted 2 lines
utils/v4l2-tracer/.gitignore new file +2 -0
+v4l2-tracer
+.clang-tidy

Same for this .clang-tidy.

Quoted 103 lines
utils/v4l2-tracer/.gitignore new file +1 -0
+v4l2-tracer.1
utils/v4l2-tracer/Makefile.am new file +23 -0
new file mode 100644
index 00000000..d37558a5
@@ -0,0 +1,23 @@
+if HAVE_JSONC
+
+lib_LTLIBRARIES = libtracer.la
+libtracer_la_SOURCES = libtracer.cpp trace.cpp trace-vp8.cpp trace-h264.cpp trace-fwht.cpp \
+trace-helper.cpp trace-info.cpp $(top_srcdir)/utils/common/v4l2-info.cpp \
+$(top_srcdir)/utils/common/media-info.cpp
+
+libtracer_la_CPPFLAGS = -I$(top_srcdir)/utils/common $(JSONC_CFLAGS)
+libtracer_la_LDFLAGS = -avoid-version -module -shared -export-dynamic -ldl $(JSONC_LIBS)
+libtracer_la_LIBTOOLFLAGS = --tag=disable-static
+
+bin_PROGRAMS = v4l2-tracer
+man_MANS = v4l2-tracer.1
+
+v4l2_tracer_SOURCES = v4l2-tracer.cpp retracer.cpp trace-info.cpp retrace-helper.cpp retrace-vp8.cpp \
+retrace-h264.cpp retrace-fwht.cpp  $(top_srcdir)/utils/common/v4l2-info.cpp
+
+v4l2_tracer_CPPFLAGS = -I$(top_srcdir)/utils/common -DLIBTRACER_PATH=$(libdir) $(JSONC_CFLAGS)
+v4l2_tracer_LDFLAGS = -lrt -lpthread $(JSONC_LIBS)
+
+EXTRA_DIST = v4l2-tracer.1
+
+endif
utils/v4l2-tracer/libtracer.cpp new file +67 -0
new file mode 100644
index 00000000..38b7cbc6
@@ -0,0 +1,209 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "libtracer.h"
+
+int open(const char *path, int oflag, ...)
+{
+	errno = 0;
+	mode_t mode = 0;
+
+	if (oflag & O_CREAT) {
+		va_list ap;
+		va_start(ap, oflag);
+		mode = va_arg(ap, PROMOTED_MODE_T);
+		va_end(ap);
+	}
+
+	int (*original_open)(const char *path, int oflag, ...);
+	original_open = (int (*)(const char*, int, ...)) dlsym(RTLD_NEXT, "open");
+	int fd = (*original_open)(path, oflag, mode);
+
+	if (is_video_or_media_device(path)) {
+		if (!options_are_set())
+			set_options();
+		add_device(fd, path);
+		trace_open(fd, path, oflag, mode, false);
+	}
+
+	return fd;
+}
+
+int open64(const char *path, int oflag, ...)
+{
+	errno = 0;
+	mode_t mode = 0;
+
+	if (oflag & O_CREAT) {
+		va_list ap;
+		va_start(ap, oflag);
+		mode = va_arg(ap, PROMOTED_MODE_T);
+		va_end(ap);
+	}
+
+	int (*original_open64)(const char *path, int oflag, ...);
+	original_open64 = (int (*)(const char*, int, ...)) dlsym(RTLD_NEXT, "open64");
+	int fd = (*original_open64)(path, oflag, mode);
+
+	if (is_video_or_media_device(path)) {
+		if (!options_are_set())
+			set_options();
+		add_device(fd, path);
+		trace_open(fd, path, oflag, mode, true);
+	}
+
+	return fd;
+}
+
+int close(int fd)
+{
+	errno = 0;
+	std::string path = get_device(fd);
+
+	/* Only trace the close if a corresponding open was also traced. */
+	if (!path.empty()) {
+		json_object *close_obj = json_object_new_object();

utils/v4l2-tracer/libtracer.cpp line 77

Quoted 10 lines
utils/v4l2-tracer/libtracer.cpp new file +10 -0
+		json_object_object_add(close_obj, "syscall_str", json_object_new_string("close"));
+		json_object_object_add(close_obj, "syscall", json_object_new_int(LIBTRACER_SYSCALL_CLOSE));
+		json_object_object_add(close_obj, "fd", json_object_new_int(fd));
+		json_object_object_add(close_obj, "path", json_object_new_string(path.c_str()));
+		write_json_object_to_json_file(close_obj);
+		json_object_put(close_obj);
+		remove_device(fd);
+
+		/* If we removed the last device, close the json trace file. */
+		if(!count_devices())

Add space after 'if'.

Quoted 72 lines
utils/v4l2-tracer/libtracer.cpp new file +72 -0
+			close_json_file();
+	}
+
+	int (*original_close)(int fd);
+	original_close = (int (*)(int)) dlsym(RTLD_NEXT, "close");
+
+	return (*original_close)(fd);
+}
+
+void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
+{
+	errno = 0;
+	void *(*original_mmap)(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
+	original_mmap = (void*(*)(void*, size_t, int, int, int, off_t)) dlsym(RTLD_NEXT, "mmap");
+	void *buf_address_pointer = (*original_mmap)(addr, len, prot, flags, fildes, off);
+
+	set_buffer_address_trace(fildes, off, (unsigned long) buf_address_pointer);
+
+	if (buffer_in_trace_context(fildes, off))
+		trace_mmap(addr, len, prot, flags, fildes, off, (unsigned long) buf_address_pointer, false);
+
+	return buf_address_pointer;
+}
+
+void *mmap64(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
+{
+	errno = 0;
+	void *(*original_mmap64)(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
+	original_mmap64 = (void*(*)(void*, size_t, int, int, int, off_t)) dlsym(RTLD_NEXT, "mmap64");
+	void *buf_address_pointer = (*original_mmap64)(addr, len, prot, flags, fildes, off);
+
+	set_buffer_address_trace(fildes, off, (unsigned long) buf_address_pointer);
+
+	if (buffer_in_trace_context(fildes, off))
+		trace_mmap(addr, len, prot, flags, fildes, off, (unsigned long) buf_address_pointer, true);
+
+	return buf_address_pointer;
+}
+
+int munmap(void *start, size_t length)
+{
+	errno = 0;
+	int(*original_munmap)(void *start, size_t length);
+	original_munmap = (int(*)(void *, size_t)) dlsym(RTLD_NEXT, "munmap");
+	int ret = (*original_munmap)(start, length);
+
+	/* Only trace the unmapping if the original mapping was traced. */
+	if (!buffer_is_mapped((unsigned long) start))
+		return ret;
+
+	json_object *munmap_obj = json_object_new_object();
+	json_object_object_add(munmap_obj, "syscall_str", json_object_new_string("munmap"));
+	json_object_object_add(munmap_obj, "syscall", json_object_new_int(LIBTRACER_SYSCALL_MUNMAP));
+
+	if (errno) {
+		json_object_object_add(munmap_obj, "errno", json_object_new_int(errno));
+		json_object_object_add(munmap_obj, "errno_str", json_object_new_string(strerror(errno)));
+	}
+
+	json_object *munmap_args = json_object_new_object();
+	json_object_object_add(munmap_args, "start", json_object_new_int64((int64_t)start));
+	json_object_object_add(munmap_args, "length", json_object_new_uint64(length));
+	json_object_object_add(munmap_obj, "munmap_args", munmap_args);
+
+	write_json_object_to_json_file(munmap_obj);
+	json_object_put(munmap_obj);
+
+	return ret;
+}
+
+int ioctl(int fd, unsigned long request, ...)
+{

utils/v4l2-tracer/libtracer.cpp line 159

Quoted 10 lines
utils/v4l2-tracer/libtracer.cpp new file +10 -0
+	errno = 0;
+	va_list ap;
+	va_start(ap, request);
+	void *arg = va_arg(ap, void *);
+	va_end(ap);
+
+	int (*original_ioctl)(int fd, unsigned long request, ...);
+	original_ioctl = (int (*)(int, long unsigned int, ...)) dlsym(RTLD_NEXT, "ioctl");
+
+	if((request == VIDIOC_S_EXT_CTRLS) && (_IOC_TYPE(request) == 'V'))

Add space after 'if'.

Quoted 98 lines
utils/v4l2-tracer/libtracer.cpp new file +50 -0
+		s_ext_ctrls_setup(static_cast<struct v4l2_ext_controls*>(arg));
+
+	if ((request == VIDIOC_QBUF) && (_IOC_TYPE(request) == 'V'))
+		qbuf_setup(static_cast<struct v4l2_buffer*>(arg));
+
+	if ((request == VIDIOC_STREAMOFF) && (_IOC_TYPE(request) == 'V'))
+		streamoff_cleanup(*(static_cast<v4l2_buf_type*>(arg)));
+
+	json_object *ioctl_obj = json_object_new_object();
+	json_object_object_add(ioctl_obj, "syscall_str", json_object_new_string("ioctl"));
+	json_object_object_add(ioctl_obj, "syscall", json_object_new_int(LIBTRACER_SYSCALL_IOCTL));
+	json_object_object_add(ioctl_obj, "fd", json_object_new_int(fd));
+	json_object_object_add(ioctl_obj, "request", json_object_new_uint64(request));
+	json_object_object_add(ioctl_obj, "request_str", json_object_new_string(get_ioctl_request_str(request).c_str()));
+
+	/* Trace the ioctl arguments provided by userspace. */
+	json_object *ioctl_args_userspace = trace_ioctl_args(fd, request, arg);
+	if (json_object_object_length(ioctl_args_userspace))
+		json_object_object_add(ioctl_obj, "ioctl_args_from_userspace", ioctl_args_userspace);
+
+	/* Make the original ioctl call. */
+	int ret = (*original_ioctl)(fd, request, arg);
+
+	if (errno) {
+		json_object_object_add(ioctl_obj, "errno", json_object_new_int(errno));
+		json_object_object_add(ioctl_obj, "errno_str", json_object_new_string(strerror(errno)));
+	}
+
+	/* Also trace the ioctl arguments as modified by the driver. */
+	json_object *ioctl_args_driver = trace_ioctl_args(fd, request, arg, false);
+	if (json_object_object_length(ioctl_args_driver))
+		json_object_object_add(ioctl_obj, "ioctl_args_from_driver", ioctl_args_driver);
+
+	write_json_object_to_json_file(ioctl_obj);
+	json_object_put(ioctl_obj);
+
+	if ((request == VIDIOC_G_FMT) && (_IOC_TYPE(request) == 'V'))
+		g_fmt_setup_trace(static_cast<struct v4l2_format*>(arg));
+
+	if ((request == VIDIOC_S_FMT) && (_IOC_TYPE(request) == 'V'))
+		s_fmt_setup(static_cast<struct v4l2_format*>(arg));
+
+	if ((request == VIDIOC_EXPBUF) && (_IOC_TYPE(request) == 'V'))
+		expbuf_setup(fd, static_cast<struct v4l2_exportbuffer*>(arg));
+
+	if ((request == VIDIOC_QUERYBUF) && (_IOC_TYPE(request) == 'V'))
+		querybuf_setup(fd, static_cast<struct v4l2_buffer*>(arg));
+
+	return ret;
+}
utils/v4l2-tracer/libtracer.h new file +35 -0
new file mode 100644
index 00000000..f528fa8f
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef LIBTRACER_H
+#define LIBTRACER_H
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <math.h> /* for pow */
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <config.h>
+#include <pthread.h>
+#include <unistd.h> /* for close */
+#include <linux/dma-buf.h>
+#include <json-c/json.h>
+#include <unordered_map>
+#include <algorithm> /* for std::find */
+#include <list>
+#include <v4l2-info.h>
+#include "trace.h"
+#include "trace-info.h"
+#include "trace-helper.h"
+#include "trace-vp8.h"
+#include "trace-h264.h"
+#include "trace-fwht.h"
+
+#endif
utils/v4l2-tracer/retrace-fwht.cpp new file +1 -0
new file mode 100644
index 00000000..000b8ba3
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: GPL-2.0-only */

utils/v4l2-tracer/retrace-fwht.cpp line 11

Quoted 10 lines
utils/v4l2-tracer/retrace-fwht.cpp new file +10 -0
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "v4l2-tracer.h"
+
+struct v4l2_ctrl_fwht_params *retrace_v4l2_ctrl_fwht_params(json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_fwht_params *fwht_params;
+	fwht_params = (struct v4l2_ctrl_fwht_params *) malloc(sizeof(v4l2_ctrl_fwht_params));

Just use calloc() to avoid the memset below.

Quoted 30 lines
utils/v4l2-tracer/retrace-fwht.cpp new file +30 -0
+	memset(fwht_params, 0, sizeof(v4l2_ctrl_fwht_params));
+
+	json_object *fwht_params_obj;
+	json_object_object_get_ex(ctrl_obj, "v4l2_ctrl_fwht_params", &fwht_params_obj);
+
+	json_object *backward_ref_ts_obj;
+	json_object_object_get_ex(fwht_params_obj, "backward_ref_ts", &backward_ref_ts_obj);
+	fwht_params->backward_ref_ts = json_object_get_uint64(backward_ref_ts_obj);
+
+	json_object *version_obj;
+	json_object_object_get_ex(fwht_params_obj, "version", &version_obj);
+	fwht_params->version = json_object_get_int(version_obj);
+
+	json_object *width_obj;
+	json_object_object_get_ex(fwht_params_obj, "width", &width_obj);
+	fwht_params->width = json_object_get_int(width_obj);
+
+	json_object *height_obj;
+	json_object_object_get_ex(fwht_params_obj, "height", &height_obj);
+	fwht_params->height = json_object_get_int(height_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(fwht_params_obj, "flags", &flags_obj);
+	fwht_params->flags = json_object_get_int(flags_obj);
+
+	json_object *colorspace_obj;
+	json_object_object_get_ex(fwht_params_obj, "colorspace", &colorspace_obj);
+	fwht_params->colorspace = json_object_get_int(colorspace_obj);
+
+	json_object *xfer_func_obj;

utils/v4l2-tracer/retrace-fwht.cpp line 51

Quoted 10 lines
utils/v4l2-tracer/retrace-fwht.cpp new file +10 -0
+	json_object_object_get_ex(fwht_params_obj, "xfer_func", &xfer_func_obj);
+	fwht_params->xfer_func = json_object_get_int(xfer_func_obj);
+
+	json_object *ycbcr_enc_obj;
+	json_object_object_get_ex(fwht_params_obj, "ycbcr_enc", &ycbcr_enc_obj);
+	fwht_params->ycbcr_enc = json_object_get_int(ycbcr_enc_obj);
+
+	json_object *quantization_obj;
+	json_object_object_get_ex(fwht_params_obj, "quantization", &quantization_obj);
+	fwht_params->quantization = json_object_get_int(quantization_obj);

This code looks like it can easily be generated, or, alternatively, put in a #define or helper function to simplify this.

E.g.:

static __u32 get_u32(json_object *in, const char *name)

{
	json_object *obj;

	json_object_object_get_ex(in, name, &obj);
	return json_object_get_int(obj);
}

...

	fwht_params->quantization = get_u32(fwht_params_obj, "quantization");

I would really try generating this from the header. I would actually implement both helpers and autogenerate. The helpers will help reduce the amount of code you need to generate.

Quoted 105 lines
utils/v4l2-tracer/retrace-fwht.cpp new file +3 -0
+
+	return fwht_params;
+}
utils/v4l2-tracer/retrace-fwht.h new file +11 -0
new file mode 100644
index 00000000..db814f03
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef RETRACE_FWHT_H
+#define RETRACE_FWHT_H
+
+struct v4l2_ctrl_fwht_params *retrace_v4l2_ctrl_fwht_params(json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/retrace-h264.cpp new file +79 -0
new file mode 100644
index 00000000..cb766d34
@@ -0,0 +1,446 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "v4l2-tracer.h"
+
+struct v4l2_ctrl_h264_sps *retrace_v4l2_ctrl_h264_sps_pointer(json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_h264_sps *h264_sps;
+	h264_sps = (struct v4l2_ctrl_h264_sps *) malloc(sizeof(v4l2_ctrl_h264_sps));
+	memset(h264_sps, 0, sizeof(v4l2_ctrl_h264_sps));
+
+	json_object *h264_sps_obj;
+	json_object_object_get_ex(ctrl_obj, "v4l2_ctrl_h264_sps", &h264_sps_obj);
+
+	json_object *profile_idc;
+	json_object_object_get_ex(h264_sps_obj, "profile_idc", &profile_idc);
+	h264_sps->profile_idc = json_object_get_int(profile_idc);
+
+	json_object *constraint_set_flags;
+	json_object_object_get_ex(h264_sps_obj, "constraint_set_flags", &constraint_set_flags);
+	h264_sps->constraint_set_flags = json_object_get_int(constraint_set_flags);
+
+	json_object *level_idc;
+	json_object_object_get_ex(h264_sps_obj, "level_idc", &level_idc);
+	h264_sps->level_idc = json_object_get_int(level_idc);
+
+	json_object *seq_parameter_set_id;
+	json_object_object_get_ex(h264_sps_obj, "seq_parameter_set_id", &seq_parameter_set_id);
+	h264_sps->seq_parameter_set_id = json_object_get_int(seq_parameter_set_id);
+
+	json_object *chroma_format_idc;
+	json_object_object_get_ex(h264_sps_obj, "chroma_format_idc", &chroma_format_idc);
+	h264_sps->chroma_format_idc = json_object_get_int(chroma_format_idc);
+
+	json_object *bit_depth_luma_minus8;
+	json_object_object_get_ex(h264_sps_obj, "bit_depth_luma_minus8", &bit_depth_luma_minus8);
+	h264_sps->bit_depth_luma_minus8 = json_object_get_int(bit_depth_luma_minus8);
+
+	json_object *bit_depth_chroma_minus8;
+	json_object_object_get_ex(h264_sps_obj, "bit_depth_chroma_minus8", &bit_depth_chroma_minus8);
+	h264_sps->bit_depth_chroma_minus8 = json_object_get_int(bit_depth_chroma_minus8);
+
+	json_object *log2_max_frame_num_minus4;
+	json_object_object_get_ex(h264_sps_obj, "log2_max_frame_num_minus4", &log2_max_frame_num_minus4);
+	h264_sps->log2_max_frame_num_minus4 = json_object_get_int(log2_max_frame_num_minus4);
+
+	json_object *pic_order_cnt_type;
+	json_object_object_get_ex(h264_sps_obj, "pic_order_cnt_type", &pic_order_cnt_type);
+	h264_sps->pic_order_cnt_type = json_object_get_int(pic_order_cnt_type);
+
+	json_object *log2_max_pic_order_cnt_lsb_minus4;
+	json_object_object_get_ex(h264_sps_obj, "log2_max_pic_order_cnt_lsb_minus4", &log2_max_pic_order_cnt_lsb_minus4);
+	h264_sps->log2_max_pic_order_cnt_lsb_minus4 = json_object_get_int(log2_max_pic_order_cnt_lsb_minus4);
+
+	json_object *max_num_ref_frames;
+	json_object_object_get_ex(h264_sps_obj, "max_num_ref_frames", &max_num_ref_frames);
+	h264_sps->max_num_ref_frames = json_object_get_int(max_num_ref_frames);
+
+	json_object *num_ref_frames_in_pic_order_cnt_cycle;
+	json_object_object_get_ex(h264_sps_obj, "num_ref_frames_in_pic_order_cnt_cycle", &num_ref_frames_in_pic_order_cnt_cycle);
+	h264_sps->num_ref_frames_in_pic_order_cnt_cycle = json_object_get_int(num_ref_frames_in_pic_order_cnt_cycle);
+
+	/* 	__s32 offset_for_ref_frame[255] */
+	json_object *offset_for_ref_frame;
+	json_object_object_get_ex(h264_sps_obj, "offset_for_ref_frame", &offset_for_ref_frame);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_sps->offset_for_ref_frame); i++)
+		h264_sps->offset_for_ref_frame[i] = json_object_get_int(json_object_array_get_idx(offset_for_ref_frame, i));
+
+	json_object *offset_for_non_ref_pic;
+	json_object_object_get_ex(h264_sps_obj, "offset_for_non_ref_pic", &offset_for_non_ref_pic);
+	h264_sps->offset_for_non_ref_pic = json_object_get_int(offset_for_non_ref_pic);
+
+	json_object *offset_for_top_to_bottom_field;
+	json_object_object_get_ex(h264_sps_obj, "offset_for_top_to_bottom_field", &offset_for_top_to_bottom_field);
+	h264_sps->offset_for_top_to_bottom_field = json_object_get_int(offset_for_top_to_bottom_field);
+
+	json_object *pic_width_in_mbs_minus1;

utils/v4l2-tracer/retrace-h264.cpp line 89

Quoted 10 lines
utils/v4l2-tracer/retrace-h264.cpp new file +10 -0
+	json_object_object_get_ex(h264_sps_obj, "pic_width_in_mbs_minus1", &pic_width_in_mbs_minus1);
+	h264_sps->pic_width_in_mbs_minus1 = json_object_get_int(pic_width_in_mbs_minus1);
+
+	json_object *pic_height_in_map_units_minus1;
+	json_object_object_get_ex(h264_sps_obj, "pic_height_in_map_units_minus1", &pic_height_in_map_units_minus1);
+	h264_sps->pic_height_in_map_units_minus1 = json_object_get_int(pic_height_in_map_units_minus1);
+
+	json_object *flags;
+	json_object_object_get_ex(h264_sps_obj, "flags", &flags);
+	h264_sps->flags = json_object_get_int(flags);

Doing this all manually doesn't scale, and will all too easily lead to mistakes.

Quoted 391 lines
utils/v4l2-tracer/retrace-h264.cpp new file +357 -0
+
+	return h264_sps;
+}
+
+struct v4l2_ctrl_h264_pps *retrace_v4l2_ctrl_h264_pps_pointer(json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_h264_pps *h264_pps;
+	h264_pps = (struct v4l2_ctrl_h264_pps *) malloc(sizeof(v4l2_ctrl_h264_pps));
+	memset(h264_pps, 0, sizeof(v4l2_ctrl_h264_pps));
+
+	json_object *h264_pps_obj;
+	json_object_object_get_ex(ctrl_obj, "v4l2_ctrl_h264_pps", &h264_pps_obj);
+
+	json_object *pic_parameter_set_id;
+	json_object_object_get_ex(h264_pps_obj, "pic_parameter_set_id", &pic_parameter_set_id);
+	h264_pps->pic_parameter_set_id = json_object_get_int(pic_parameter_set_id);
+
+	json_object *seq_parameter_set_id;
+	json_object_object_get_ex(h264_pps_obj, "seq_parameter_set_id", &seq_parameter_set_id);
+	h264_pps->seq_parameter_set_id = json_object_get_int(seq_parameter_set_id);
+
+	json_object *num_slice_groups_minus1;
+	json_object_object_get_ex(h264_pps_obj, "num_slice_groups_minus1", &num_slice_groups_minus1);
+	h264_pps->num_slice_groups_minus1 = json_object_get_int(num_slice_groups_minus1);
+
+	json_object *num_ref_idx_l0_default_active_minus1;
+	json_object_object_get_ex(h264_pps_obj, "num_ref_idx_l0_default_active_minus1", &num_ref_idx_l0_default_active_minus1);
+	h264_pps->num_ref_idx_l0_default_active_minus1 = json_object_get_int(num_ref_idx_l0_default_active_minus1);
+
+	json_object *num_ref_idx_l1_default_active_minus1;
+	json_object_object_get_ex(h264_pps_obj, "num_ref_idx_l1_default_active_minus1", &num_ref_idx_l1_default_active_minus1);
+	h264_pps->num_ref_idx_l1_default_active_minus1 = json_object_get_int(num_ref_idx_l1_default_active_minus1);
+
+	json_object *weighted_bipred_idc;
+	json_object_object_get_ex(h264_pps_obj, "weighted_bipred_idc", &weighted_bipred_idc);
+	h264_pps->weighted_bipred_idc = json_object_get_int(weighted_bipred_idc);
+
+	json_object *pic_init_qp_minus26;
+	json_object_object_get_ex(h264_pps_obj, "pic_init_qp_minus26", &pic_init_qp_minus26);
+	h264_pps->pic_init_qp_minus26 = json_object_get_int(pic_init_qp_minus26);
+
+	json_object *pic_init_qs_minus26;
+	json_object_object_get_ex(h264_pps_obj, "pic_init_qs_minus26", &pic_init_qs_minus26);
+	h264_pps->pic_init_qs_minus26 = json_object_get_int(pic_init_qs_minus26);
+
+	json_object *chroma_qp_index_offset;
+	json_object_object_get_ex(h264_pps_obj, "chroma_qp_index_offset", &chroma_qp_index_offset);
+	h264_pps->chroma_qp_index_offset = json_object_get_int(chroma_qp_index_offset);
+
+	json_object *second_chroma_qp_index_offset;
+	json_object_object_get_ex(h264_pps_obj, "second_chroma_qp_index_offset", &second_chroma_qp_index_offset);
+	h264_pps->second_chroma_qp_index_offset = json_object_get_int(second_chroma_qp_index_offset);
+
+	json_object *flags;
+	json_object_object_get_ex(h264_pps_obj, "flags", &flags);
+	h264_pps->flags = json_object_get_int(flags);
+
+	return h264_pps;
+}
+
+struct v4l2_ctrl_h264_scaling_matrix *retrace_v4l2_ctrl_h264_scaling_matrix_pointer(json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_h264_scaling_matrix *h264_scaling_matrix;
+	h264_scaling_matrix = (struct v4l2_ctrl_h264_scaling_matrix *) malloc(sizeof(v4l2_ctrl_h264_scaling_matrix));
+	memset(h264_scaling_matrix, 0, sizeof(v4l2_ctrl_h264_scaling_matrix));
+
+	json_object *h264_scaling_matrix_obj;
+	json_object_object_get_ex(ctrl_obj, "v4l2_ctrl_h264_scaling_matrix", &h264_scaling_matrix_obj);
+
+	int count = 0;
+
+	/* __u8 scaling_list_4x4[6][16] */
+	json_object *scaling_list_4x4;
+	json_object_object_get_ex(h264_scaling_matrix_obj, "scaling_list_4x4", &scaling_list_4x4);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_scaling_matrix->scaling_list_4x4); i++)
+		for (size_t j = 0; j < ARRAY_SIZE(h264_scaling_matrix->scaling_list_4x4[0]); j++)
+			h264_scaling_matrix->scaling_list_4x4[i][j] = json_object_get_int(json_object_array_get_idx(scaling_list_4x4, count++));
+
+	/* __u8 scaling_list_8x8[6][64] */
+	count = 0;
+	json_object *scaling_list_8x8;
+	json_object_object_get_ex(h264_scaling_matrix_obj, "scaling_list_8x8", &scaling_list_8x8);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_scaling_matrix->scaling_list_8x8); i++)
+		for (size_t j = 0; j < ARRAY_SIZE(h264_scaling_matrix->scaling_list_8x8[0]); j++)
+			h264_scaling_matrix->scaling_list_8x8[i][j] = json_object_get_int(json_object_array_get_idx(scaling_list_8x8, count++));
+
+	return h264_scaling_matrix;
+}
+
+struct v4l2_h264_weight_factors retrace_v4l2_h264_weight_factors(json_object *h264_weight_factors_obj)
+{
+	struct v4l2_h264_weight_factors h264_weight_factors;
+	memset(&h264_weight_factors, 0, sizeof(h264_weight_factors));
+
+	json_object *luma_weight;
+	json_object_object_get_ex(h264_weight_factors_obj, "luma_weight", &luma_weight);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_weight_factors.luma_weight); i++)
+		h264_weight_factors.luma_weight[i] = json_object_get_int(json_object_array_get_idx(luma_weight, i));
+
+	json_object *luma_offset;
+	json_object_object_get_ex(h264_weight_factors_obj, "luma_offset", &luma_offset);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_weight_factors.luma_offset); i++)
+		h264_weight_factors.luma_offset[i] = json_object_get_int(json_object_array_get_idx(luma_offset, i));
+
+	int count = 0;
+	/* __s16 chroma_weight[32][2] */
+	json_object *chroma_weight;
+	json_object_object_get_ex(h264_weight_factors_obj, "chroma_weight", &chroma_weight);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_weight_factors.chroma_weight); i++) {
+		for (size_t j = 0; j < ARRAY_SIZE(h264_weight_factors.chroma_weight[0]); j++) {
+			int value = json_object_get_int(json_object_array_get_idx(chroma_weight, count++));
+			h264_weight_factors.chroma_weight[i][j] = (__s16) value;
+		}
+	}
+
+	/* __s16 chroma_offset[32][2] */
+	count = 0;
+	json_object *chroma_offset;
+	json_object_object_get_ex(h264_weight_factors_obj, "chroma_offset", &chroma_offset);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_weight_factors.chroma_offset); i++) {
+		for (size_t j = 0; j < ARRAY_SIZE(h264_weight_factors.chroma_offset[0]); j++) {
+			int value = json_object_get_int(json_object_array_get_idx(chroma_offset, count++));
+			h264_weight_factors.chroma_offset[i][j] = (__s16) value;
+		}
+	}
+
+	return h264_weight_factors;
+}
+
+struct v4l2_ctrl_h264_pred_weights *retrace_v4l2_ctrl_h264_pred_weights_pointer(json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_h264_pred_weights *h264_pred_weights;
+	h264_pred_weights = (struct v4l2_ctrl_h264_pred_weights *) malloc(sizeof(v4l2_ctrl_h264_pred_weights));
+	memset(h264_pred_weights, 0, sizeof(v4l2_ctrl_h264_pred_weights));
+
+	json_object *h264_pred_weights_obj;
+	json_object_object_get_ex(ctrl_obj, "v4l2_ctrl_h264_pred_weights", &h264_pred_weights_obj);
+
+	json_object *luma_log2_weight_denom;
+	json_object_object_get_ex(h264_pred_weights_obj, "luma_log2_weight_denom", &luma_log2_weight_denom);
+	h264_pred_weights->luma_log2_weight_denom = json_object_get_int(luma_log2_weight_denom);
+
+	json_object *chroma_log2_weight_denom;
+	json_object_object_get_ex(h264_pred_weights_obj, "chroma_log2_weight_denom", &chroma_log2_weight_denom);
+	h264_pred_weights->chroma_log2_weight_denom = json_object_get_int(chroma_log2_weight_denom);
+
+	json_object *weight_factors;
+	json_object_object_get_ex(h264_pred_weights_obj, "weight_factors", &weight_factors);
+
+	/* struct v4l2_h264_weight_factors weight_factors[2] */
+	for (size_t i = 0; i < ARRAY_SIZE(h264_pred_weights->weight_factors); i++)
+		h264_pred_weights->weight_factors[i] = retrace_v4l2_h264_weight_factors(json_object_array_get_idx(weight_factors, i));
+
+	return h264_pred_weights;
+}
+
+struct v4l2_h264_reference retrace_v4l2_h264_reference(json_object *h264_reference_obj)
+{
+	struct v4l2_h264_reference h264_reference;
+	memset(&h264_reference, 0, sizeof(v4l2_h264_reference));
+
+	json_object *fields_obj;
+	json_object_object_get_ex(h264_reference_obj, "fields", &fields_obj);
+	h264_reference.fields = json_object_get_int(fields_obj);
+
+	json_object *index_obj;
+	json_object_object_get_ex(h264_reference_obj, "index", &index_obj);
+	h264_reference.index = json_object_get_int(index_obj);
+
+	return h264_reference;
+}
+
+struct v4l2_ctrl_h264_slice_params *retrace_v4l2_ctrl_h264_slice_params_pointer(json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_h264_slice_params *h264_slice_params;
+	h264_slice_params = (struct v4l2_ctrl_h264_slice_params *) malloc(sizeof(v4l2_ctrl_h264_slice_params));
+	memset(h264_slice_params, 0, sizeof(v4l2_ctrl_h264_slice_params));
+
+	json_object *h264_slice_params_obj;
+	json_object_object_get_ex(ctrl_obj, "v4l2_ctrl_h264_slice_params", &h264_slice_params_obj);
+
+	json_object *header_bit_size_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "header_bit_size", &header_bit_size_obj);
+	h264_slice_params->header_bit_size = json_object_get_int(header_bit_size_obj);
+
+	json_object *first_mb_in_slice_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "first_mb_in_slice", &first_mb_in_slice_obj);
+	h264_slice_params->first_mb_in_slice = json_object_get_int(first_mb_in_slice_obj);
+
+	json_object *slice_type_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "slice_type", &slice_type_obj);
+	h264_slice_params->slice_type = json_object_get_int(slice_type_obj);
+
+	json_object *colour_plane_id_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "colour_plane_id", &colour_plane_id_obj);
+	h264_slice_params->colour_plane_id = json_object_get_int(colour_plane_id_obj);
+
+	json_object *redundant_pic_cnt_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "redundant_pic_cnt", &redundant_pic_cnt_obj);
+	h264_slice_params->redundant_pic_cnt = json_object_get_int(redundant_pic_cnt_obj);
+
+	json_object *cabac_init_idc_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "cabac_init_idc", &cabac_init_idc_obj);
+	h264_slice_params->cabac_init_idc = json_object_get_int(cabac_init_idc_obj);
+
+	json_object *slice_qp_delta_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "slice_qp_delta", &slice_qp_delta_obj);
+	h264_slice_params->slice_qp_delta = json_object_get_int(slice_qp_delta_obj);
+
+	json_object *slice_qs_delta_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "slice_qs_delta", &slice_qs_delta_obj);
+	h264_slice_params->slice_qs_delta = json_object_get_int(slice_qs_delta_obj);
+
+	json_object *disable_deblocking_filter_idc_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "disable_deblocking_filter_idc", &disable_deblocking_filter_idc_obj);
+	h264_slice_params->disable_deblocking_filter_idc = json_object_get_int(disable_deblocking_filter_idc_obj);
+
+	json_object *slice_alpha_c0_offset_div2_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "slice_alpha_c0_offset_div2", &slice_alpha_c0_offset_div2_obj);
+	h264_slice_params->slice_alpha_c0_offset_div2 = json_object_get_int(slice_alpha_c0_offset_div2_obj);
+
+	json_object *slice_beta_offset_div2_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "slice_beta_offset_div2", &slice_beta_offset_div2_obj);
+	h264_slice_params->slice_beta_offset_div2 = json_object_get_int(slice_beta_offset_div2_obj);
+
+	json_object *num_ref_idx_l0_active_minus1_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "num_ref_idx_l0_active_minus1", &num_ref_idx_l0_active_minus1_obj);
+	h264_slice_params->num_ref_idx_l0_active_minus1 = json_object_get_int(num_ref_idx_l0_active_minus1_obj);
+
+	json_object *num_ref_idx_l1_active_minus1_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "num_ref_idx_l1_active_minus1", &num_ref_idx_l1_active_minus1_obj);
+	h264_slice_params->num_ref_idx_l1_active_minus1 = json_object_get_int(num_ref_idx_l1_active_minus1_obj);
+
+	/*	struct v4l2_h264_reference ref_pic_list0[V4L2_H264_REF_LIST_LEN] */
+	json_object *ref_pic_list0_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "ref_pic_list0", &ref_pic_list0_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_slice_params->ref_pic_list0); i++)
+		h264_slice_params->ref_pic_list0[i] = retrace_v4l2_h264_reference(json_object_array_get_idx(ref_pic_list0_obj, i));
+
+	/* struct v4l2_h264_reference ref_pic_list1[V4L2_H264_REF_LIST_LEN] */
+	json_object *ref_pic_list1_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "ref_pic_list1", &ref_pic_list1_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_slice_params->ref_pic_list1); i++)
+		h264_slice_params->ref_pic_list1[i] = retrace_v4l2_h264_reference(json_object_array_get_idx(ref_pic_list1_obj, i));
+
+	json_object *flags_obj;
+	json_object_object_get_ex(h264_slice_params_obj, "flags", &flags_obj);
+	h264_slice_params->flags = json_object_get_int(flags_obj);
+
+	return h264_slice_params;
+}
+
+struct v4l2_h264_dpb_entry retrace_h264_dpb_entry(json_object *h264_dpb_entry_obj)
+{
+	struct v4l2_h264_dpb_entry entry;
+	memset(&entry, 0, sizeof(v4l2_h264_dpb_entry));
+
+	json_object *reference_ts_obj;
+	json_object_object_get_ex(h264_dpb_entry_obj, "reference_ts", &reference_ts_obj);
+	entry.reference_ts = json_object_get_int(reference_ts_obj);
+
+	json_object *pic_num_obj;
+	json_object_object_get_ex(h264_dpb_entry_obj, "pic_num", &pic_num_obj);
+	entry.pic_num = json_object_get_int(pic_num_obj);
+
+	json_object *frame_num_obj;
+	json_object_object_get_ex(h264_dpb_entry_obj, "frame_num", &frame_num_obj);
+	entry.frame_num = json_object_get_int(frame_num_obj);
+
+	json_object *fields_obj;
+	json_object_object_get_ex(h264_dpb_entry_obj, "fields", &fields_obj);
+	entry.fields = json_object_get_int(fields_obj);
+
+	json_object *top_field_order_cnt_obj;
+	json_object_object_get_ex(h264_dpb_entry_obj, "top_field_order_cnt", &top_field_order_cnt_obj);
+	entry.top_field_order_cnt = json_object_get_int(top_field_order_cnt_obj);
+
+	json_object *bottom_field_order_cnt_obj;
+	json_object_object_get_ex(h264_dpb_entry_obj, "bottom_field_order_cnt", &bottom_field_order_cnt_obj);
+	entry.bottom_field_order_cnt = json_object_get_int(bottom_field_order_cnt_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(h264_dpb_entry_obj, "flags", &flags_obj);
+	entry.flags = json_object_get_int(flags_obj);
+
+	return entry;
+}
+
+struct v4l2_ctrl_h264_decode_params *retrace_v4l2_ctrl_h264_decode_params(json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_h264_decode_params *h264_decode_params;
+	h264_decode_params = (struct v4l2_ctrl_h264_decode_params *) malloc(sizeof(v4l2_ctrl_h264_decode_params));
+	memset(h264_decode_params, 0, sizeof(v4l2_ctrl_h264_decode_params));
+
+	json_object *h264_decode_params_obj;
+	json_object_object_get_ex(ctrl_obj, "v4l2_ctrl_h264_decode_params", &h264_decode_params_obj);
+
+	/* struct v4l2_h264_dpb_entry dpb[V4L2_H264_NUM_DPB_ENTRIES]; */
+	json_object *dpb_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "dpb", &dpb_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_decode_params->dpb); i++)
+		h264_decode_params->dpb[i] = retrace_h264_dpb_entry(json_object_array_get_idx(dpb_obj, i));
+
+	json_object *nal_ref_idc_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "nal_ref_idc", &nal_ref_idc_obj);
+	h264_decode_params->nal_ref_idc = json_object_get_int(nal_ref_idc_obj);
+
+	json_object *frame_num_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "frame_num", &frame_num_obj);
+	h264_decode_params->frame_num = json_object_get_int(frame_num_obj);
+
+	json_object *top_field_order_cnt_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "top_field_order_cnt", &top_field_order_cnt_obj);
+	h264_decode_params->top_field_order_cnt = json_object_get_int(top_field_order_cnt_obj);
+
+	json_object *bottom_field_order_cnt_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "bottom_field_order_cnt", &bottom_field_order_cnt_obj);
+	h264_decode_params->bottom_field_order_cnt = json_object_get_int(bottom_field_order_cnt_obj);
+
+	json_object *idr_pic_id_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "idr_pic_id", &idr_pic_id_obj);
+	h264_decode_params->idr_pic_id = json_object_get_int(idr_pic_id_obj);
+
+	json_object *pic_order_cnt_lsb_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "pic_order_cnt_lsb", &pic_order_cnt_lsb_obj);
+	h264_decode_params->pic_order_cnt_lsb = json_object_get_int(pic_order_cnt_lsb_obj);
+
+	json_object *delta_pic_order_cnt_bottom_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "delta_pic_order_cnt_bottom", &delta_pic_order_cnt_bottom_obj);
+	h264_decode_params->delta_pic_order_cnt_bottom = json_object_get_int(delta_pic_order_cnt_bottom_obj);
+
+	json_object *delta_pic_order_cnt0_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "delta_pic_order_cnt0", &delta_pic_order_cnt0_obj);
+	h264_decode_params->delta_pic_order_cnt0 = json_object_get_int(delta_pic_order_cnt0_obj);
+
+	json_object *delta_pic_order_cnt1_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "delta_pic_order_cnt1", &delta_pic_order_cnt1_obj);
+	h264_decode_params->delta_pic_order_cnt1 = json_object_get_int(delta_pic_order_cnt1_obj);
+
+	json_object *dec_ref_pic_marking_bit_size_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "dec_ref_pic_marking_bit_size", &dec_ref_pic_marking_bit_size_obj);
+	h264_decode_params->dec_ref_pic_marking_bit_size = json_object_get_int(dec_ref_pic_marking_bit_size_obj);
+
+	json_object *pic_order_cnt_bit_size_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "pic_order_cnt_bit_size", &pic_order_cnt_bit_size_obj);
+	h264_decode_params->pic_order_cnt_bit_size = json_object_get_int(pic_order_cnt_bit_size_obj);
+
+	json_object *slice_group_change_cycle_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "slice_group_change_cycle", &slice_group_change_cycle_obj);
+	h264_decode_params->slice_group_change_cycle = json_object_get_int(slice_group_change_cycle_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(h264_decode_params_obj, "flags", &flags_obj);
+	h264_decode_params->flags = json_object_get_int(flags_obj);
+
+	return h264_decode_params;
+}
utils/v4l2-tracer/retrace-h264.h new file +16 -0
new file mode 100644
index 00000000..37dbdd87
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef RETRACE_H264_H
+#define RETRACE_H264_H
+
+struct v4l2_ctrl_h264_sps *retrace_v4l2_ctrl_h264_sps_pointer(json_object *ctrl_obj);
+struct v4l2_ctrl_h264_pps *retrace_v4l2_ctrl_h264_pps_pointer(json_object *ctrl_obj);
+struct v4l2_ctrl_h264_scaling_matrix *retrace_v4l2_ctrl_h264_scaling_matrix_pointer(json_object *ctrl_obj);
+struct v4l2_ctrl_h264_pred_weights *retrace_v4l2_ctrl_h264_pred_weights_pointer(json_object *ctrl_obj);
+struct v4l2_ctrl_h264_slice_params *retrace_v4l2_ctrl_h264_slice_params_pointer(json_object *h264_weight_factors_obj);
+struct v4l2_ctrl_h264_decode_params *retrace_v4l2_ctrl_h264_decode_params(json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/retrace-helper.cpp new file +6 -0
new file mode 100644
index 00000000..48c6bb67
@@ -0,0 +1,157 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "v4l2-tracer.h"

utils/v4l2-tracer/retrace-helper.cpp line 16

Quoted 10 lines
utils/v4l2-tracer/retrace-helper.cpp new file +10 -0
+
+static struct retrace_context ctx_retrace = {
+	.lock = PTHREAD_MUTEX_INITIALIZER
+};
+
+bool buffer_in_retrace_context(int fd, __u32 offset)
+{
+	bool buffer_in_retrace_context = false;
+	pthread_mutex_lock(&ctx_retrace.lock);
+	for (auto it = ctx_retrace.buffers.begin(); it != ctx_retrace.buffers.end(); ++it) {

You can write this as:

for (auto &b : ctx_retrace.buffers)

Quoted 4 lines
utils/v4l2-tracer/retrace-helper.cpp new file +4 -0
+		if ((it->fd == fd) && (it->offset == offset)) {
+			buffer_in_retrace_context = true;
+			break;
+		}

utils/v4l2-tracer/retrace-helper.cpp line 30

Quoted 10 lines
utils/v4l2-tracer/retrace-helper.cpp new file +10 -0
+	}
+	pthread_mutex_unlock(&ctx_retrace.lock);
+	return buffer_in_retrace_context;
+}
+
+void add_buffer_retrace(int fd, __u32 offset)
+{
+	pthread_mutex_lock(&ctx_retrace.lock);
+	struct buffer_retrace buf;
+	memset(&buf, 0, sizeof(buffer_retrace));

You can avoid the memset by doing:

	struct buffer_retrace buf = {};
Quoted 76 lines
utils/v4l2-tracer/retrace-helper.cpp new file +76 -0
+	buf.fd = fd;
+	buf.offset = offset;
+	ctx_retrace.buffers.push_front(buf);
+	pthread_mutex_unlock(&ctx_retrace.lock);
+}
+
+void set_buffer_address_retrace(int fd, __u32 offset, long address_trace, long address_retrace)
+{
+	pthread_mutex_lock(&ctx_retrace.lock);
+	for (auto it = ctx_retrace.buffers.begin(); it != ctx_retrace.buffers.end(); ++it) {
+		if ((it->fd == fd) && (it->offset == offset)) {
+			it->address_trace = address_trace;
+			it->address_retrace = address_retrace;
+			break;
+		}
+	}
+	pthread_mutex_unlock(&ctx_retrace.lock);
+}
+
+long get_retrace_address_from_trace_address(long address_trace)
+{
+	long address_retrace = 0;
+
+	pthread_mutex_lock(&ctx_retrace.lock);
+	for (auto it = ctx_retrace.buffers.cbegin(); it != ctx_retrace.buffers.cend(); ++it) {
+		if (it->address_trace == address_trace) {
+			address_retrace = it->address_retrace;
+			break;
+		}
+	}
+	pthread_mutex_unlock(&ctx_retrace.lock);
+
+	return address_retrace;
+}
+
+void print_buffers_retrace(void)
+{
+	pthread_mutex_lock(&ctx_retrace.lock);
+	for (auto it = ctx_retrace.buffers.cbegin(); it != ctx_retrace.buffers.cend(); ++it) {
+		fprintf(stderr, "fd: %d, offset: %d, address_trace:%ld, address_retrace:%ld\n",
+		        it->fd, it->offset, it->address_trace, it->address_retrace);
+	}
+	pthread_mutex_unlock(&ctx_retrace.lock);
+}
+
+/*
+ * Create a new file descriptor entry in retrace context.
+ * Add both the fd from the trace context and the corresponding fd from the retrace context.
+ */
+void add_fd(int fd_trace, int fd_retrace)
+{
+	std::pair<int, int> new_pair;
+	new_pair = std::make_pair(fd_trace, fd_retrace);
+	pthread_mutex_lock(&ctx_retrace.lock);
+	ctx_retrace.retrace_fds.insert(new_pair);
+	pthread_mutex_unlock(&ctx_retrace.lock);
+}
+
+int get_fd_retrace_from_fd_trace(int fd_trace)
+{
+	int fd_retrace = 0;
+	std::unordered_map<int, int>::const_iterator it;
+
+	pthread_mutex_lock(&ctx_retrace.lock);
+	it = ctx_retrace.retrace_fds.find(fd_trace);
+	if (it != ctx_retrace.retrace_fds.end())
+		fd_retrace = it->second;
+	pthread_mutex_unlock(&ctx_retrace.lock);
+
+	return fd_retrace;
+}
+
+/* Using a file descriptor from the trace, find and remove an fd entry from the retrace context.*/
+void remove_fd(int fd_trace)
+{
+	pthread_mutex_lock(&ctx_retrace.lock);

utils/v4l2-tracer/retrace-helper.cpp line 116

Quoted 10 lines
utils/v4l2-tracer/retrace-helper.cpp new file +10 -0
+	ctx_retrace.retrace_fds.erase(fd_trace);
+	pthread_mutex_unlock(&ctx_retrace.lock);
+}
+
+void print_fds(void)
+{
+	pthread_mutex_lock(&ctx_retrace.lock);
+	if (ctx_retrace.retrace_fds.empty())
+		fprintf(stderr, "all devices closed\n");
+	for ( auto it = ctx_retrace.retrace_fds.cbegin(); it != ctx_retrace.retrace_fds.cend(); ++it )

Remove the starting and ending space. You don't use that style elsewhere, so it is odd to see the extra spaces appearing here.

Quoted 15 lines
utils/v4l2-tracer/retrace-helper.cpp new file +15 -0
+		fprintf(stderr, "fd_trace: %d, fd_retrace: %d\n", it->first, it->second);
+	pthread_mutex_unlock(&ctx_retrace.lock);
+}
+
+void set_pixelformat_retrace(__u32 width, __u32 height, __u32 pixelformat)
+{
+	pthread_mutex_lock(&ctx_retrace.lock);
+	ctx_retrace.width = width;
+	ctx_retrace.height = height;
+	ctx_retrace.pixelformat = pixelformat;
+	pthread_mutex_unlock(&ctx_retrace.lock);
+}
+
+unsigned get_expected_length_retrace(void)
+{

utils/v4l2-tracer/retrace-helper.cpp line 141

Quoted 10 lines
utils/v4l2-tracer/retrace-helper.cpp new file +10 -0
+	unsigned width;
+	unsigned height;
+	unsigned pixelformat;
+	unsigned expected_length;
+
+	pthread_mutex_lock(&ctx_retrace.lock);
+	width = ctx_retrace.width;
+	height = ctx_retrace.height;
+	pixelformat = ctx_retrace.pixelformat;
+	pthread_mutex_unlock(&ctx_retrace.lock);

I'd just write:

	unsigned width = ctx_retrace.width;

etc. Rather than separately declaring these variables.

Quoted 1,681 lines
utils/v4l2-tracer/retrace-helper.cpp new file +16 -0
+
+	expected_length = width * height;
+	if (pixelformat == V4L2_PIX_FMT_NV12 || pixelformat == V4L2_PIX_FMT_YUV420) {
+		expected_length *= 3;
+		expected_length /= 2;
+		expected_length += (expected_length % 2);
+	}
+	return expected_length;
+}
+
+void print_context(void)
+{
+	print_fds();
+	print_buffers_retrace();
+	fprintf(stderr, "\n");
+}
utils/v4l2-tracer/retrace-helper.h new file +44 -0
new file mode 100644
index 00000000..0507f4eb
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef RETRACE_HELPER_H
+#define RETRACE_HELPER_H
+
+struct buffer_retrace {
+	int fd;
+	__u32 offset;
+	long address_trace;
+	long address_retrace;
+};
+
+struct retrace_context {
+	__u32 width;
+	__u32 height;
+	__u32 pixelformat;
+	pthread_mutex_t lock;
+
+	/* Key is a file descriptor from the trace, value is the corresponding fd in the retrace context. */
+	std::unordered_map<int, int> retrace_fds;
+
+	/* List of output and capture buffers being retraced. */
+	std::list<struct buffer_retrace> buffers;
+};
+
+bool buffer_in_retrace_context(int fd, __u32 offset = 0);
+void add_buffer_retrace(int fd, __u32 offset = 0);
+
+void set_buffer_address_retrace(int fd, __u32 offset, long address_trace, long address_retrace);
+long get_retrace_address_from_trace_address(long address_trace);
+
+void add_fd(int fd_trace, int fd_retrace);
+int get_fd_retrace_from_fd_trace(int fd_trace);
+void remove_fd(int fd_trace);
+
+void set_pixelformat_retrace(__u32 width, __u32 height, __u32 pixelformat);
+unsigned get_expected_length_retrace(void);
+
+void print_context(void);
+
+#endif
utils/v4l2-tracer/retrace-vp8.cpp new file +288 -0
new file mode 100644
index 00000000..efb8b243
@@ -0,0 +1,288 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "v4l2-tracer.h"
+
+struct v4l2_vp8_loop_filter retrace_v4l2_vp8_loop_filter(json_object *lf_obj)
+{
+	struct v4l2_vp8_loop_filter lf;
+	memset(&lf, 0, sizeof(lf));
+
+	/* __s8 ref_frm_delta[4] */
+	json_object *ref_frm_delta_obj;
+	json_object_object_get_ex(lf_obj, "ref_frm_delta", &ref_frm_delta_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(lf.ref_frm_delta); i++)
+		lf.ref_frm_delta[i] = (__s8) json_object_get_int(json_object_array_get_idx(ref_frm_delta_obj, i));
+
+	/* __s8 mb_mode_delta[4] */
+	json_object *mb_mode_delta_obj;
+	json_object_object_get_ex(lf_obj, "mb_mode_delta", &mb_mode_delta_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(lf.mb_mode_delta); i++)
+		lf.mb_mode_delta[i] = (__s8) json_object_get_int(json_object_array_get_idx(mb_mode_delta_obj, i));
+
+	json_object *sharpness_level_obj;
+	json_object_object_get_ex(lf_obj, "sharpness_level", &sharpness_level_obj);
+	lf.sharpness_level = json_object_get_int(sharpness_level_obj);
+
+	json_object *level_obj;
+	json_object_object_get_ex(lf_obj, "level", &level_obj);
+	lf.level = json_object_get_int(level_obj);
+
+	json_object *padding_obj;
+	json_object_object_get_ex(lf_obj, "padding", &padding_obj);
+	lf.padding = json_object_get_int(padding_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(lf_obj, "flags", &flags_obj);
+	lf.flags = json_object_get_int(flags_obj);
+
+	return lf;
+}
+
+struct v4l2_vp8_segment retrace_v4l2_vp8_segment(json_object *segment_obj)
+{
+	struct v4l2_vp8_segment segment;
+	memset(&segment, 0, sizeof(v4l2_vp8_segment));
+
+	/*	__s8 quant_update[4] */
+	json_object *quant_update_obj;
+	json_object_object_get_ex(segment_obj, "quant_update", &quant_update_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(segment.quant_update); i++)
+		segment.quant_update[i] = (__s8) json_object_get_int(json_object_array_get_idx(quant_update_obj, i));
+
+	/* __s8 lf_update[4] */
+	json_object *lf_update_obj;
+	json_object_object_get_ex(segment_obj, "lf_update", &lf_update_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(segment.lf_update); i++)
+		segment.lf_update[i] = (__s8) json_object_get_int(json_object_array_get_idx(lf_update_obj, i));
+
+	/* __u8 segment_probs[3] */
+	json_object *segment_probs_obj;
+	json_object_object_get_ex(segment_obj, "segment_probs", &segment_probs_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(segment.segment_probs); i++)
+		segment.segment_probs[i] = json_object_get_int(json_object_array_get_idx(segment_probs_obj, i));
+
+	json_object *padding_obj;
+	json_object_object_get_ex(segment_obj, "padding", &padding_obj);
+	segment.padding = json_object_get_int(padding_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(segment_obj, "flags", &flags_obj);
+	segment.flags = json_object_get_int(flags_obj);
+
+	return segment;
+}
+
+struct v4l2_vp8_quantization retrace_v4l2_vp8_quantization(json_object *quant_obj)
+{
+	struct v4l2_vp8_quantization quant;
+	memset(&quant, 0, sizeof(quant));
+
+	json_object *y_ac_qi_obj;
+	json_object_object_get_ex(quant_obj, "y_ac_qi", &y_ac_qi_obj);
+	quant.y_ac_qi = json_object_get_int(y_ac_qi_obj);
+
+	json_object *y_dc_delta_obj;
+	json_object_object_get_ex(quant_obj, "y_dc_delta", &y_dc_delta_obj);
+	quant.y_dc_delta = (__s8) json_object_get_int(y_dc_delta_obj);
+
+	json_object *y2_dc_delta_obj;
+	json_object_object_get_ex(quant_obj, "y2_dc_delta", &y2_dc_delta_obj);
+	quant.y2_dc_delta = (__s8) json_object_get_int(y2_dc_delta_obj);
+
+	json_object *y2_ac_delta_obj;
+	json_object_object_get_ex(quant_obj, "y2_ac_delta", &y2_ac_delta_obj);
+	quant.y2_ac_delta = (__s8) json_object_get_int(y2_ac_delta_obj);
+
+	json_object *uv_dc_delta_obj;
+	json_object_object_get_ex(quant_obj, "uv_dc_delta", &uv_dc_delta_obj);
+	quant.uv_dc_delta = (__s8) json_object_get_int(uv_dc_delta_obj);
+
+	json_object *uv_ac_delta_obj;
+	json_object_object_get_ex(quant_obj, "uv_ac_delta", &uv_ac_delta_obj);
+	quant.uv_ac_delta = (__s8) json_object_get_int(uv_ac_delta_obj);
+
+	json_object *padding_obj;
+	json_object_object_get_ex(quant_obj, "padding", &padding_obj);
+	quant.padding = json_object_get_int(padding_obj);
+
+	return quant;
+}
+
+struct v4l2_vp8_entropy retrace_v4l2_vp8_entropy(json_object *entropy_obj)
+{
+	struct v4l2_vp8_entropy entropy;
+	memset(&entropy, 0, sizeof(entropy));
+
+	int count = 0;
+
+	/* __u8 coeff_probs[4][8][3][V4L2_VP8_COEFF_PROB_CNT] */
+	json_object *coeff_probs_obj;
+	json_object_object_get_ex(entropy_obj, "coeff_probs", &coeff_probs_obj);
+
+	for (size_t i = 0; i < ARRAY_SIZE(entropy.coeff_probs); i++)
+		for (size_t j = 0; j < ARRAY_SIZE(entropy.coeff_probs[0]); j++)
+			for (size_t k = 0; k < ARRAY_SIZE(entropy.coeff_probs[0][0]); k++)
+				for (size_t l = 0; l < V4L2_VP8_COEFF_PROB_CNT; l++)
+					entropy.coeff_probs[i][j][k][l] = json_object_get_int(json_object_array_get_idx(coeff_probs_obj, count++));
+
+	/* __u8 y_mode_probs[4] */
+	json_object *y_mode_probs_obj;
+	json_object_object_get_ex(entropy_obj, "y_mode_probs", &y_mode_probs_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(entropy.y_mode_probs); i++)
+		entropy.y_mode_probs[i] = json_object_get_int(json_object_array_get_idx(y_mode_probs_obj, i));
+
+	/* __u8 uv_mode_probs[3] */
+	json_object *uv_mode_probs_obj;
+	json_object_object_get_ex(entropy_obj, "uv_mode_probs", &uv_mode_probs_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(entropy.uv_mode_probs); i++)
+		entropy.uv_mode_probs[i] = json_object_get_int(json_object_array_get_idx(uv_mode_probs_obj, i));
+
+	/*  __u8 mv_probs[2][V4L2_VP8_MV_PROB_CNT] */
+	count = 0;
+	json_object *mv_probs_obj;
+	json_object_object_get_ex(entropy_obj, "mv_probs", &mv_probs_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(entropy.mv_probs); i++)
+		for (size_t j = 0; j < V4L2_VP8_MV_PROB_CNT; j++)
+			entropy.mv_probs[i][j] = json_object_get_int(json_object_array_get_idx(mv_probs_obj, count++));
+
+	/* __u8 padding[3] */
+	json_object *padding_obj;
+	json_object_object_get_ex(entropy_obj, "padding", &padding_obj);
+	for (size_t i = 0; i < ARRAY_SIZE(entropy.padding); i++)
+		entropy.padding[i] = json_object_get_int(json_object_array_get_idx(padding_obj, i));
+
+	return entropy;
+}
+
+struct v4l2_vp8_entropy_coder_state retrace_v4l2_vp8_entropy_coder_state(json_object *coder_state_obj)
+{
+	struct v4l2_vp8_entropy_coder_state coder_state;
+	memset(&coder_state, 0, sizeof(coder_state));
+
+	json_object *range_obj;
+	json_object_object_get_ex(coder_state_obj, "range", &range_obj);
+	coder_state.range = json_object_get_int(range_obj);
+
+	json_object *value_obj;
+	json_object_object_get_ex(coder_state_obj, "value", &value_obj);
+	coder_state.value = json_object_get_int(value_obj);
+
+	json_object *bit_count_obj;
+	json_object_object_get_ex(coder_state_obj, "bit_count", &bit_count_obj);
+	coder_state.bit_count = json_object_get_int(bit_count_obj);
+
+	json_object *padding_obj;
+	json_object_object_get_ex(coder_state_obj, "padding", &padding_obj);
+	coder_state.padding = json_object_get_int(padding_obj);
+
+	return coder_state;
+}
+
+struct v4l2_ctrl_vp8_frame *retrace_v4l2_ctrl_vp8_frame_pointer(json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_vp8_frame *vp8_frame_pointer = (struct v4l2_ctrl_vp8_frame *) malloc(sizeof(v4l2_ctrl_vp8_frame));
+	memset(vp8_frame_pointer, 0, sizeof(v4l2_ctrl_vp8_frame));
+
+	json_object *vp8_frame_obj;
+	json_object_object_get_ex(ctrl_obj, "v4l2_ctrl_vp8_frame", &vp8_frame_obj);
+
+	/* struct v4l2_vp8_segment segment */
+	json_object *segment_obj;
+	json_object_object_get_ex(vp8_frame_obj, "segment", &segment_obj);
+	vp8_frame_pointer->segment = retrace_v4l2_vp8_segment(segment_obj);
+
+	/* struct v4l2_vp8_loop_filter lf */
+	json_object *lf_obj;
+	json_object_object_get_ex(vp8_frame_obj, "lf", &lf_obj);
+	vp8_frame_pointer->lf = retrace_v4l2_vp8_loop_filter(lf_obj);
+
+	/* struct v4l2_vp8_quantization quant */
+	json_object *quant_obj;
+	json_object_object_get_ex(vp8_frame_obj, "quant", &quant_obj);
+	vp8_frame_pointer->quant = retrace_v4l2_vp8_quantization(quant_obj);
+
+	/* struct v4l2_vp8_entropy entropy */
+	json_object *entropy_obj;
+	json_object_object_get_ex(vp8_frame_obj, "entropy", &entropy_obj);
+	vp8_frame_pointer->entropy = retrace_v4l2_vp8_entropy(entropy_obj);
+
+	/* struct v4l2_vp8_entropy_coder_state coder_state */
+	json_object *coder_state_obj;
+	json_object_object_get_ex(vp8_frame_obj, "coder_state", &coder_state_obj);
+	vp8_frame_pointer->coder_state = retrace_v4l2_vp8_entropy_coder_state(coder_state_obj);
+
+	json_object *width_obj;
+	json_object_object_get_ex(vp8_frame_obj, "width", &width_obj);
+	vp8_frame_pointer->width = json_object_get_int(width_obj);
+
+	json_object *height_obj;
+	json_object_object_get_ex(vp8_frame_obj, "height", &height_obj);
+	vp8_frame_pointer->height = json_object_get_int(height_obj);
+
+	json_object *horizontal_scale_obj;
+	json_object_object_get_ex(vp8_frame_obj, "horizontal_scale", &horizontal_scale_obj);
+	vp8_frame_pointer->horizontal_scale = json_object_get_int(horizontal_scale_obj);
+
+	json_object *vertical_scale_obj;
+	json_object_object_get_ex(vp8_frame_obj, "vertical_scale", &vertical_scale_obj);
+	vp8_frame_pointer->vertical_scale = json_object_get_int(vertical_scale_obj);
+
+	json_object *version_obj;
+	json_object_object_get_ex(vp8_frame_obj, "version", &version_obj);
+	vp8_frame_pointer->version = json_object_get_int(version_obj);
+
+	json_object *prob_skip_false_obj;
+	json_object_object_get_ex(vp8_frame_obj, "prob_skip_false", &prob_skip_false_obj);
+	vp8_frame_pointer->prob_skip_false = json_object_get_int(prob_skip_false_obj);
+
+	json_object *prob_intra_obj;
+	json_object_object_get_ex(vp8_frame_obj, "prob_intra", &prob_intra_obj);
+	vp8_frame_pointer->prob_intra = json_object_get_int(prob_intra_obj);
+
+	json_object *prob_last_obj;
+	json_object_object_get_ex(vp8_frame_obj, "prob_last", &prob_last_obj);
+	vp8_frame_pointer->prob_last = json_object_get_int(prob_last_obj);
+
+	json_object *prob_gf_obj;
+	json_object_object_get_ex(vp8_frame_obj, "prob_gf", &prob_gf_obj);
+	vp8_frame_pointer->prob_gf = json_object_get_int(prob_gf_obj);
+
+	json_object *num_dct_parts_obj;
+	json_object_object_get_ex(vp8_frame_obj, "num_dct_parts", &num_dct_parts_obj);
+	vp8_frame_pointer->num_dct_parts = json_object_get_int(num_dct_parts_obj);
+
+	json_object *first_part_size_obj;
+	json_object_object_get_ex(vp8_frame_obj, "first_part_size", &first_part_size_obj);
+	vp8_frame_pointer->first_part_size = json_object_get_int(first_part_size_obj);
+
+	json_object *first_part_header_bits_obj;
+	json_object_object_get_ex(vp8_frame_obj, "first_part_header_bits", &first_part_header_bits_obj);
+	vp8_frame_pointer->first_part_header_bits = json_object_get_int(first_part_header_bits_obj);
+
+	/* __u32 dct_part_sizes[8] */
+	json_object *dct_part_sizes_obj;
+	json_object_object_get_ex(vp8_frame_obj, "dct_part_sizes", &dct_part_sizes_obj);
+	for (int i = 0; i < 8; i++)
+		vp8_frame_pointer->dct_part_sizes[i] = json_object_get_int(json_object_array_get_idx(dct_part_sizes_obj, i));
+
+	json_object *last_frame_ts_obj;
+	json_object_object_get_ex(vp8_frame_obj, "last_frame_ts", &last_frame_ts_obj);
+	vp8_frame_pointer->last_frame_ts = json_object_get_int(last_frame_ts_obj);
+
+	json_object *golden_frame_ts_obj;
+	json_object_object_get_ex(vp8_frame_obj, "golden_frame_ts", &golden_frame_ts_obj);
+	vp8_frame_pointer->golden_frame_ts = json_object_get_int(golden_frame_ts_obj);
+
+	json_object *alt_frame_ts_obj;
+	json_object_object_get_ex(vp8_frame_obj, "alt_frame_ts", &alt_frame_ts_obj);
+	vp8_frame_pointer->alt_frame_ts = json_object_get_int(alt_frame_ts_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(vp8_frame_obj, "flags", &flags_obj);
+	vp8_frame_pointer->flags = json_object_get_int(flags_obj);
+
+	return vp8_frame_pointer;
+}
utils/v4l2-tracer/retrace-vp8.h new file +11 -0
new file mode 100644
index 00000000..a5bea25e
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef RETRACE_VP8_H
+#define RETRACE_VP8_H
+
+struct v4l2_ctrl_vp8_frame *retrace_v4l2_ctrl_vp8_frame_pointer(json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/retracer.cpp new file +1281 -0
new file mode 100644
index 00000000..0f179710
@@ -0,0 +1,1281 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "v4l2-tracer.h"
+
+bool verbose = false;
+std::string retrace_filename;
+std::string dev_path_video;
+std::string dev_path_media;
+
+enum Option_Retracer {
+    OptSetDevice_Retracer = 'd',
+    OptSetMediaDevice_Retracer = 'm',
+    OptVerbose_Retracer = 'v',
+};
+
+static struct option long_options_retracer[] = {
+    { "device", required_argument, nullptr, OptSetDevice_Retracer },
+    { "media", required_argument, nullptr, OptSetMediaDevice_Retracer },
+    { "verbose", no_argument, nullptr, OptVerbose_Retracer },
+    { nullptr, 0, nullptr, 0 }
+};
+
+char short_options_retracer[] = {
+    OptSetDevice_Retracer, ':',
+    OptSetMediaDevice_Retracer, ':',
+    OptVerbose_Retracer
+};
+
+void retrace_mmap(json_object *mmap_obj, bool is_mmap64)
+{
+	json_object *mmap_args_obj;
+	json_object_object_get_ex(mmap_obj, "mmap_args", &mmap_args_obj);
+
+	json_object *len_obj;
+	json_object_object_get_ex(mmap_args_obj, "len", &len_obj);
+	size_t len = (size_t) json_object_get_int(len_obj);
+
+	json_object *prot_obj;
+	json_object_object_get_ex(mmap_args_obj, "prot", &prot_obj);
+	int prot = json_object_get_int(prot_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(mmap_args_obj, "flags", &flags_obj);
+	int flags = json_object_get_int(flags_obj);
+
+	json_object *fildes_obj;
+	json_object_object_get_ex(mmap_args_obj, "fildes", &fildes_obj);
+	int fd_trace = json_object_get_int(fildes_obj);
+
+	json_object *off_obj;
+	json_object_object_get_ex(mmap_args_obj, "off", &off_obj);
+	off_t off = (off_t) json_object_get_int64(off_obj);
+
+	int fd_retrace = get_fd_retrace_from_fd_trace(fd_trace);
+
+	/* Only retrace mmap calls that map a buffer. */
+	if (!buffer_in_retrace_context(fd_retrace, off))
+		return;
+
+	void *buf_address_retrace_pointer;
+	if (is_mmap64)
+		buf_address_retrace_pointer = mmap64(0, len, prot, flags, fd_retrace, off);
+	else
+		buf_address_retrace_pointer = mmap(0, len, prot, flags, fd_retrace, off);
+
+	if (buf_address_retrace_pointer == MAP_FAILED) {
+		if (is_mmap64)
+			perror("mmap64");
+		else
+			perror("mmap");
+		print_context();
+		exit(EXIT_FAILURE);
+	}
+
+	/*
+	 * Get and store the original trace address so that it can be matched
+	 * with the munmap address later.
+	 */
+	json_object *buffer_address_obj;
+	json_object_object_get_ex(mmap_obj, "buffer_address", &buffer_address_obj);
+	long buf_address_trace = json_object_get_int64(buffer_address_obj);
+	set_buffer_address_retrace(fd_retrace, off, buf_address_trace, (long) buf_address_retrace_pointer);
+
+	if (verbose || (errno != 0)) {
+		if (is_mmap64)
+			perror("mmap64");
+		else
+			perror("mmap");
+		fprintf(stderr, "fd: %d, offset: %ld\n", fd_retrace, off);
+		print_context();
+	}
+}
+
+void retrace_munmap(json_object *syscall_obj)
+{
+	json_object *munmap_args_obj;
+	json_object_object_get_ex(syscall_obj, "munmap_args", &munmap_args_obj);
+
+	json_object *start_obj;
+	json_object_object_get_ex(munmap_args_obj, "start", &start_obj);
+	long start = json_object_get_int64(start_obj);
+
+	json_object *length_obj;
+	json_object_object_get_ex(munmap_args_obj, "length", &length_obj);
+	size_t length = (size_t) json_object_get_int(length_obj);
+
+	long buffer_address_retrace = get_retrace_address_from_trace_address(start);
+
+	if (buffer_address_retrace < 0)
+		return;
+
+	munmap((void *)buffer_address_retrace, length);
+
+	if (verbose || (errno != 0)) {
+		perror("munmap");
+		fprintf(stderr, "unmapped: %ld\n", buffer_address_retrace);
+		fprintf(stderr, "\n");
+	}
+}
+
+void retrace_open(json_object *jobj, bool is_open64)
+{
+	json_object *fd_trace_obj;
+	json_object_object_get_ex(jobj, "fd", &fd_trace_obj);
+	int fd_trace = json_object_get_int(fd_trace_obj);
+
+	json_object *open_args_obj;
+	json_object_object_get_ex(jobj, "open_args", &open_args_obj);
+
+	json_object *path_obj;
+	json_object_object_get_ex(open_args_obj, "path", &path_obj);
+	std::string path = json_object_get_string(path_obj);
+
+	json_object *oflag_obj;
+	json_object_object_get_ex(open_args_obj, "oflag", &oflag_obj);
+	int oflag = json_object_get_int(oflag_obj);
+
+	json_object *mode_obj;
+	json_object_object_get_ex(open_args_obj, "mode", &mode_obj);
+	int mode = json_object_get_int(mode_obj);
+
+	/* If a device is provided on the command line, use it instead of the device from the trace file. */
+	if ((path.find("video") != path.npos) && !dev_path_video.empty())
+		path = dev_path_video;
+
+	if ((path.find("media") != path.npos) && !dev_path_media.empty())
+		path = dev_path_media;
+
+	int fd_retrace = 0;
+	if (is_open64)
+		fd_retrace = open64(path.c_str(), oflag, mode);
+	else
+		fd_retrace = open(path.c_str(), oflag, mode);
+
+	if (fd_retrace <= 0) {
+		fprintf(stderr, "Cannot open: %s\n", path.c_str());
+		exit(fd_retrace);
+	}
+
+	add_fd(fd_trace, fd_retrace);
+
+	if (verbose || (errno != 0)) {
+		if (is_open64)
+			perror("open64");
+		else
+			perror("open");
+		fprintf(stderr, "path: %s \n", path.c_str());
+		print_context();
+	}
+}
+
+void retrace_close(json_object *jobj)
+{
+	json_object *fd_trace_obj;
+	json_object_object_get_ex(jobj, "fd", &fd_trace_obj);
+	int fd_retrace = get_fd_retrace_from_fd_trace(json_object_get_int(fd_trace_obj));
+
+	/* Only close devices that were opened in the retrace context. */
+	if (fd_retrace) {
+		close(fd_retrace);
+		remove_fd(json_object_get_int(fd_trace_obj));
+
+		if (verbose || (errno != 0)) {
+			perror("close");
+			fprintf(stderr, "fd: %d\n\n", fd_retrace);
+			print_context();
+		}
+	}
+}
+
+struct v4l2_requestbuffers retrace_v4l2_requestbuffers(json_object *ioctl_args)
+{
+	struct v4l2_requestbuffers request_buffers;
+	memset(&request_buffers, 0, sizeof(v4l2_requestbuffers));
+
+	json_object *requestbuffers_obj;
+	json_object_object_get_ex(ioctl_args, "v4l2_requestbuffers", &requestbuffers_obj);
+
+	json_object *count_obj;
+	json_object_object_get_ex(requestbuffers_obj, "count", &count_obj);
+	request_buffers.count = json_object_get_int(count_obj);
+
+	json_object *type_obj;
+	json_object_object_get_ex(requestbuffers_obj, "type", &type_obj);
+	request_buffers.type = json_object_get_int(type_obj);
+
+	json_object *memory_obj;
+	json_object_object_get_ex(requestbuffers_obj, "memory", &memory_obj);
+	request_buffers.memory = json_object_get_int(memory_obj);
+
+	json_object *capabilities_obj;
+	json_object_object_get_ex(requestbuffers_obj, "capabilities", &capabilities_obj);
+	request_buffers.capabilities = json_object_get_int(capabilities_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(requestbuffers_obj, "flags", &flags_obj);
+	request_buffers.flags = json_object_get_int(flags_obj);
+
+	return request_buffers;
+}
+
+void retrace_vidioc_reqbufs(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_requestbuffers request_buffers = retrace_v4l2_requestbuffers(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_REQBUFS, &request_buffers);
+
+	if (verbose || (errno != 0)) {
+		perror("VIDIOC_REQBUFS");
+		fprintf(stderr, "type: %s, request_buffers.count: %d\n",
+		        buftype2s(request_buffers.type).c_str(), request_buffers.count);
+		print_context();
+	}
+}
+
+struct v4l2_plane *retrace_v4l2_plane(json_object *plane_obj, __u32 memory)
+{
+	struct v4l2_plane *pl = (struct v4l2_plane *) calloc(1, sizeof(v4l2_plane));
+
+	json_object *bytesused_obj;
+	json_object_object_get_ex(plane_obj, "bytesused", &bytesused_obj);
+	pl->bytesused = (__u32) json_object_get_uint64(bytesused_obj);
+
+	json_object *length_obj;
+	json_object_object_get_ex(plane_obj, "length", &length_obj);
+	pl->length = (__u32) json_object_get_uint64(length_obj);
+
+	json_object *m_obj;
+	json_object_object_get_ex(plane_obj, "m", &m_obj);
+	if (memory == V4L2_MEMORY_MMAP) {
+		json_object *mem_offset_obj;
+		json_object_object_get_ex(m_obj, "mem_offset", &mem_offset_obj);
+		pl->m.mem_offset = (__u32) json_object_get_int64(mem_offset_obj);
+	}
+
+	json_object *data_offset_obj;
+	json_object_object_get_ex(plane_obj, "data_offset", &data_offset_obj);
+	pl->data_offset = (__u32) json_object_get_int64(data_offset_obj);
+
+	return pl;
+}
+
+struct v4l2_buffer *retrace_v4l2_buffer(json_object *ioctl_args)
+{
+	struct v4l2_buffer *buf = (struct v4l2_buffer *) calloc(1, sizeof(struct v4l2_buffer));
+
+	json_object *buf_obj;
+	json_object_object_get_ex(ioctl_args, "v4l2_buffer", &buf_obj);
+
+	json_object *index_obj;
+	json_object_object_get_ex(buf_obj, "index", &index_obj);
+	buf->index = (__u32) json_object_get_uint64(index_obj);
+
+	json_object *type_obj;
+	json_object_object_get_ex(buf_obj, "type", &type_obj);
+	buf->type = (__u32) json_object_get_uint64(type_obj);
+
+	json_object *bytesused_obj;
+	json_object_object_get_ex(buf_obj, "bytesused", &bytesused_obj);
+	buf->bytesused = (__u32) json_object_get_uint64(bytesused_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(buf_obj, "flags", &flags_obj);
+	buf->flags = (__u32) json_object_get_uint64(flags_obj);
+
+	json_object *field_obj;
+	json_object_object_get_ex(buf_obj, "field", &field_obj);
+	buf->field = (__u32) json_object_get_uint64(field_obj);
+
+	json_object *timestamp_obj;
+	json_object_object_get_ex(buf_obj, "timestamp", &timestamp_obj);
+
+	struct timeval tv;
+	memset(&tv, 0, sizeof(timeval));
+	json_object *tv_sec_obj;
+	json_object_object_get_ex(timestamp_obj, "tv_sec", &tv_sec_obj);
+	tv.tv_sec = json_object_get_int64(tv_sec_obj);
+	json_object *tv_usec_obj;
+	json_object_object_get_ex(timestamp_obj, "tv_usec", &tv_usec_obj);
+	tv.tv_usec = json_object_get_int64(tv_usec_obj);
+	buf->timestamp = tv;
+
+	json_object *sequence_obj;
+	json_object_object_get_ex(buf_obj, "sequence", &sequence_obj);
+	buf->sequence = (__u32) json_object_get_uint64(sequence_obj);
+
+	json_object *memory_obj;
+	json_object_object_get_ex(buf_obj, "memory", &memory_obj);
+	buf->memory = (__u32) json_object_get_uint64(memory_obj);
+
+	json_object *length_obj;
+	json_object_object_get_ex(buf_obj, "length", &length_obj);
+	buf->length = (__u32) json_object_get_uint64(length_obj);
+
+	json_object *m_obj;
+	json_object_object_get_ex(buf_obj, "m", &m_obj);
+
+	/* multi-planar API */
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		json_object *planes_obj;
+		json_object_object_get_ex(m_obj, "planes", &planes_obj);
+		json_object *plane_obj = json_object_array_get_idx(planes_obj, 0); /* TODO add planes > 0 */
+		buf->m.planes = retrace_v4l2_plane(plane_obj, buf->memory);
+	}
+
+	/* single-planar API */
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
+
+		switch (buf->memory) {
+		case V4L2_MEMORY_MMAP: {
+			json_object *offset_obj;
+			json_object_object_get_ex(m_obj, "offset", &offset_obj);
+			buf->m.offset = (__u32) json_object_get_uint64(offset_obj);
+			break;
+		}
+		case V4L2_MEMORY_USERPTR:
+		case V4L2_MEMORY_DMABUF:
+			break;
+		}
+	}
+
+	if (buf->flags & V4L2_BUF_FLAG_REQUEST_FD) {
+		json_object *request_fd_obj;
+		json_object_object_get_ex(buf_obj, "request_fd", &request_fd_obj);
+		buf->request_fd = (__s32) get_fd_retrace_from_fd_trace(json_object_get_int(request_fd_obj));
+	}
+
+	return buf;
+}
+
+void retrace_vidioc_querybuf(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_buffer *buf = retrace_v4l2_buffer(ioctl_args_user);
+
+	ioctl(fd_retrace, VIDIOC_QUERYBUF, buf);
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		if (buf->m.planes != nullptr) {
+			free(buf->m.planes);
+		}
+	}
+
+	/*
+	 * For single-planar API, use QUERYBUF as an opportunity
+	 * to store the buffer in the retrace context for future access.
+	 */
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
+		switch (buf->memory) {
+		case V4L2_MEMORY_MMAP: {
+			if (!buffer_in_retrace_context(fd_retrace, buf->m.offset))
+				add_buffer_retrace(fd_retrace, buf->m.offset);
+			break;
+		}
+		case V4L2_MEMORY_USERPTR:
+		case V4L2_MEMORY_DMABUF:
+		default:
+			break;
+		}
+	}
+
+	if (verbose || (errno != 0)) {
+		perror("VIDIOC_QUERYBUF");
+		fprintf(stderr, "buf->type: %s, buf->index: %d, fd_retrace: %d\n",
+		        buftype2s(buf->type).c_str(), buf->index, fd_retrace);
+		print_context();
+	}
+
+	free(buf);
+}
+
+void retrace_vidioc_qbuf(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_buffer *buf = retrace_v4l2_buffer(ioctl_args_user);
+
+	ioctl(fd_retrace, VIDIOC_QBUF, buf);
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		if (buf->m.planes != nullptr) {
+			free(buf->m.planes);
+		}
+	}
+
+	if (verbose || (errno != 0)) {
+		perror("VIDIOC_QBUF");
+		fprintf(stderr, "buf->type: %s, buf->index: %d, fd_retrace: %d, \n",
+		        buftype2s(buf->type).c_str(), buf->index, fd_retrace);
+		print_context();
+	}
+
+	free(buf);
+}
+
+struct v4l2_exportbuffer retrace_v4l2_exportbuffer(json_object *ioctl_args)
+{
+	struct v4l2_exportbuffer export_buffer;
+	memset(&export_buffer, 0, sizeof(v4l2_exportbuffer));
+
+	json_object *exportbuffer_obj;
+	json_object_object_get_ex(ioctl_args, "v4l2_exportbuffer", &exportbuffer_obj);
+
+	json_object *type_obj;
+	json_object_object_get_ex(exportbuffer_obj, "type", &type_obj);
+	export_buffer.type = json_object_get_int(type_obj);
+
+	json_object *index_obj;
+	json_object_object_get_ex(exportbuffer_obj, "index", &index_obj);
+	export_buffer.index = json_object_get_int(index_obj);
+
+	json_object *plane_obj;
+	json_object_object_get_ex(exportbuffer_obj, "plane", &plane_obj);
+	export_buffer.plane = json_object_get_int(plane_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(exportbuffer_obj, "flags", &flags_obj);
+	export_buffer.flags = json_object_get_int(flags_obj);
+
+	json_object *fd_obj;
+	json_object_object_get_ex(exportbuffer_obj, "fd", &fd_obj);
+	export_buffer.fd = json_object_get_int(fd_obj);
+
+	return export_buffer;
+}
+
+void retrace_vidioc_expbuf(int fd_retrace, json_object *ioctl_args_user, json_object *ioctl_args_driver)
+{
+	struct v4l2_exportbuffer export_buffer;
+	memset(&export_buffer, 0, sizeof(v4l2_exportbuffer));
+
+	export_buffer = retrace_v4l2_exportbuffer(ioctl_args_user);
+	ioctl(fd_retrace, VIDIOC_EXPBUF, &export_buffer);
+
+	int buf_fd_retrace = export_buffer.fd;
+
+	/*
+	 * For multi-planar API use EXPBUF as an opportunity
+	 * to store the buffer in the retrace context for future access.
+	 */
+	if ((export_buffer.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) ||
+	    (export_buffer.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)) {
+			if (!buffer_in_retrace_context(buf_fd_retrace))
+				add_buffer_retrace(buf_fd_retrace);
+	}
+
+	/*
+	 * Get the export buffer file descriptor as provided by the driver in the original trace context.
+	 * Then associate this original file descriptor with the current file descriptor in the retrace context.
+	 */
+	memset(&export_buffer, 0, sizeof(v4l2_exportbuffer));
+	export_buffer = retrace_v4l2_exportbuffer(ioctl_args_driver);
+	int buf_fd_trace = export_buffer.fd;
+	add_fd(buf_fd_trace, buf_fd_retrace);
+
+	if (verbose || (errno != 0)) {
+		perror("VIDIOC_EXPBUF");
+		fprintf(stderr, "type: %s \n", buftype2s(export_buffer.type).c_str());
+		fprintf(stderr, "index: %d, fd: %d\n", export_buffer.index, buf_fd_retrace);
+		print_context();
+	}
+}
+
+void retrace_vidioc_dqbuf(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_buffer *buf = retrace_v4l2_buffer(ioctl_args_user);
+
+	struct pollfd *pfds = (struct pollfd *) calloc(1, sizeof(struct pollfd));
+	if (pfds == NULL)
+		exit(EXIT_FAILURE);
+	pfds[0].fd = fd_retrace;
+	pfds[0].events = POLLIN;
+	poll(pfds, 1, 5000);
+	free(pfds);
+
+	ioctl(fd_retrace, VIDIOC_DQBUF, buf);
+
+	if (verbose || (errno != 0)) {
+		perror("VIDIOC_DQBUF");
+		fprintf(stderr, "fd_retrace: %d\n", fd_retrace);
+		fprintf(stderr, "buf->index: %d\n", buf->index);
+		fprintf(stderr, "buf->type: %s,\n", buftype2s(buf->type).c_str());
+		fprintf(stderr, "buf->bytesused: %u, \n", buf->bytesused);
+		fprintf(stderr, "buf->flags: %u\n", buf->flags);
+		fprintf(stderr, "buf->field: %u, buf->request_fd: %d\n", buf->field, buf->request_fd);
+		fprintf(stderr, "buf->request_fd: %d\n", buf->request_fd);
+		print_context();
+	}
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		if (buf->m.planes != nullptr) {
+			free(buf->m.planes);
+		}
+	}
+
+	free(buf);
+}
+
+void retrace_vidioc_streamon(int fd_retrace, json_object *ioctl_args)
+{
+	json_object *buf_type_obj;
+	json_object_object_get_ex(ioctl_args, "buf_type", &buf_type_obj);
+	v4l2_buf_type buf_type = (v4l2_buf_type) json_object_get_int(buf_type_obj);
+
+	ioctl(fd_retrace, VIDIOC_STREAMON, &buf_type);
+
+	if (verbose || (errno != 0)) {
+		perror("VIDIOC_STREAMON");
+		fprintf(stderr, "buftype: %s\n\n", buftype2s(buf_type).c_str());
+	}
+}
+
+void retrace_vidioc_streamoff(int fd_retrace, json_object *ioctl_args)
+{
+	json_object *buf_type_obj;
+	json_object_object_get_ex(ioctl_args, "buf_type", &buf_type_obj);
+	v4l2_buf_type buf_type = (v4l2_buf_type) json_object_get_int(buf_type_obj);
+
+	ioctl(fd_retrace, VIDIOC_STREAMOFF, &buf_type);
+
+	if (verbose || (errno != 0)) {
+		perror("VIDIOC_STREAMOFF");
+		fprintf(stderr, "buftype: %s\n", buftype2s(buf_type).c_str());
+		fprintf(stderr, "\n");
+	}
+}
+
+struct v4l2_plane_pix_format get_v4l2_plane_pix_format(json_object *pix_mp_obj, int plane)
+{
+	std::string key;
+	struct v4l2_plane_pix_format plane_fmt;
+	memset(&plane_fmt, 0, sizeof(v4l2_plane_pix_format));
+
+	json_object *plane_fmt_obj, *sizeimage_obj, *bytesperline_obj;
+
+	key = "v4l2_plane_pix_format_";
+	key += std::to_string(plane);
+	json_object_object_get_ex(pix_mp_obj, key.c_str(), &plane_fmt_obj);
+
+	json_object_object_get_ex(plane_fmt_obj, "sizeimage", &sizeimage_obj);
+	plane_fmt.sizeimage = json_object_get_int64(sizeimage_obj);
+
+	json_object_object_get_ex(plane_fmt_obj, "bytesperline", &bytesperline_obj);
+	plane_fmt.bytesperline = json_object_get_int64(bytesperline_obj);
+
+	return plane_fmt;
+}
+
+struct v4l2_pix_format_mplane retrace_v4l2_pix_format_mplane(json_object *v4l2_format_obj)
+{
+	struct v4l2_pix_format_mplane pix_mp;
+	memset(&pix_mp, 0, sizeof(v4l2_pix_format_mplane));
+
+	json_object *pix_mp_obj;
+	json_object_object_get_ex(v4l2_format_obj, "v4l2_pix_format_mplane", &pix_mp_obj);
+
+	json_object *width_obj;
+	json_object_object_get_ex(pix_mp_obj, "width", &width_obj);
+	pix_mp.width = json_object_get_int(width_obj);
+
+	json_object *height_obj;
+	json_object_object_get_ex(pix_mp_obj, "height", &height_obj);
+	pix_mp.height = json_object_get_int(height_obj);
+
+	json_object *pixelformat_obj;
+	json_object_object_get_ex(pix_mp_obj, "pixelformat", &pixelformat_obj);
+	pix_mp.pixelformat = json_object_get_int(pixelformat_obj);
+
+	json_object *field_obj;
+	json_object_object_get_ex(pix_mp_obj, "field", &field_obj);
+	pix_mp.field = json_object_get_int(field_obj);
+
+	json_object *colorspace_obj;
+	json_object_object_get_ex(pix_mp_obj, "colorspace", &colorspace_obj);
+	pix_mp.colorspace = json_object_get_int(colorspace_obj);
+
+	json_object *num_planes_obj;
+	json_object_object_get_ex(pix_mp_obj, "num_planes", &num_planes_obj);
+	pix_mp.num_planes = json_object_get_int(num_planes_obj);
+
+	for (int i = 0; i < pix_mp.num_planes; i++)
+		pix_mp.plane_fmt[i] = get_v4l2_plane_pix_format(pix_mp_obj, i);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(pix_mp_obj, "flags", &flags_obj);
+	pix_mp.flags = json_object_get_int(flags_obj);
+
+	json_object *ycbcr_enc_obj;
+	json_object_object_get_ex(pix_mp_obj, "ycbcr_enc", &ycbcr_enc_obj);
+	pix_mp.ycbcr_enc = json_object_get_int(ycbcr_enc_obj);
+
+	json_object *quantization_obj;
+	json_object_object_get_ex(pix_mp_obj, "quantization", &quantization_obj);
+	pix_mp.quantization = json_object_get_int(quantization_obj);
+
+	json_object *xfer_func_obj;
+	json_object_object_get_ex(pix_mp_obj, "xfer_func", &xfer_func_obj);
+	pix_mp.xfer_func = json_object_get_int(xfer_func_obj);
+
+	return pix_mp;
+}
+
+struct v4l2_format retrace_v4l2_format(json_object *ioctl_args)
+{
+	struct v4l2_format format;
+	memset(&format, 0, sizeof(format));
+
+	json_object *v4l2_format_obj;
+	json_object_object_get_ex(ioctl_args, "v4l2_format", &v4l2_format_obj);
+
+	json_object *type_obj;
+	json_object_object_get_ex(v4l2_format_obj, "type", &type_obj);
+	format.type = json_object_get_int(type_obj);
+
+	switch (format.type) {
+	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
+	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
+	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
+	case V4L2_BUF_TYPE_VBI_CAPTURE:
+	case V4L2_BUF_TYPE_VBI_OUTPUT:
+	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
+	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
+	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
+		break;
+	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
+	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
+		format.fmt.pix_mp = retrace_v4l2_pix_format_mplane(v4l2_format_obj);
+		break;
+	case V4L2_BUF_TYPE_SDR_CAPTURE:
+	case V4L2_BUF_TYPE_SDR_OUTPUT:
+	case V4L2_BUF_TYPE_META_CAPTURE:
+	case V4L2_BUF_TYPE_META_OUTPUT:
+		break;
+	}
+
+	return format;
+}
+
+void retrace_vidioc_g_fmt(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_format format;
+	memset(&format, 0, sizeof(format));
+
+	format = retrace_v4l2_format(ioctl_args_user);
+
+	ioctl(fd_retrace, VIDIOC_G_FMT, &format);
+
+	if (format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		set_pixelformat_retrace(format.fmt.pix.width, format.fmt.pix.height, format.fmt.pix.pixelformat);
+	if (format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+		set_pixelformat_retrace(format.fmt.pix_mp.width, format.fmt.pix_mp.height, format.fmt.pix_mp.pixelformat);
+
+	if (verbose || (errno != 0))
+		perror("VIDIOC_G_FMT");
+}
+
+void retrace_vidioc_s_fmt(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_format format;
+	memset(&format, 0, sizeof(format));
+
+	format = retrace_v4l2_format(ioctl_args_user);
+
+	ioctl(fd_retrace, VIDIOC_S_FMT, &format);
+
+	if (verbose || (errno != 0)) {
+		perror("VIDIOC_S_FMT");
+		fprintf(stderr, "%s\n", buftype2s(format.type).c_str());
+		fprintf(stderr, "format.fmt.pix_mp.pixelformat: %s\n\n",
+		        fcc2s(format.fmt.pix_mp.pixelformat).c_str());
+	}
+}
+
+int retrace_v4l2_ext_control_value(json_object *ctrl_obj)
+{
+	json_object *value_obj;
+	json_object_object_get_ex(ctrl_obj, "value", &value_obj);
+	return json_object_get_int(value_obj);
+}
+
+struct v4l2_ext_control retrace_v4l2_ext_control(json_object *ext_controls_obj, int ctrl_idx)
+{
+	struct v4l2_ext_control ctrl;
+	memset(&ctrl, 0, sizeof(v4l2_ext_control));
+
+	std::string unique_key_for_control = "v4l2_ext_control_";
+	unique_key_for_control += std::to_string(ctrl_idx);
+
+	json_object *ctrl_obj;
+	json_object_object_get_ex(ext_controls_obj, unique_key_for_control.c_str(), &ctrl_obj);
+
+	json_object *id_obj;
+	json_object_object_get_ex(ctrl_obj, "id", &id_obj);
+	ctrl.id = json_object_get_int64(id_obj);
+
+	json_object *size_obj;
+	json_object_object_get_ex(ctrl_obj, "size", &size_obj);
+	ctrl.size = json_object_get_int64(size_obj);
+
+	if ((ctrl.id & V4L2_CID_CODEC_STATELESS_BASE) == V4L2_CID_CODEC_STATELESS_BASE) {
+		switch (ctrl.id) {
+		case V4L2_CID_STATELESS_VP8_FRAME:
+			ctrl.ptr = retrace_v4l2_ctrl_vp8_frame_pointer(ctrl_obj);
+			break;
+		case V4L2_CID_STATELESS_H264_DECODE_MODE:
+		case V4L2_CID_STATELESS_H264_START_CODE:
+			ctrl.value = retrace_v4l2_ext_control_value(ctrl_obj);
+			break;
+		case V4L2_CID_STATELESS_H264_SPS:
+			ctrl.ptr = retrace_v4l2_ctrl_h264_sps_pointer(ctrl_obj);
+			break;
+		case V4L2_CID_STATELESS_H264_PPS:
+			ctrl.ptr = retrace_v4l2_ctrl_h264_pps_pointer(ctrl_obj);
+			break;
+		case V4L2_CID_STATELESS_H264_SCALING_MATRIX:
+			ctrl.ptr = retrace_v4l2_ctrl_h264_scaling_matrix_pointer(ctrl_obj);
+			break;
+		case V4L2_CID_STATELESS_H264_PRED_WEIGHTS:
+			ctrl.ptr = retrace_v4l2_ctrl_h264_pred_weights_pointer(ctrl_obj);
+			break;
+		case V4L2_CID_STATELESS_H264_SLICE_PARAMS:
+			ctrl.ptr = retrace_v4l2_ctrl_h264_slice_params_pointer(ctrl_obj);
+			break;
+		case V4L2_CID_STATELESS_H264_DECODE_PARAMS:
+			ctrl.ptr = retrace_v4l2_ctrl_h264_decode_params(ctrl_obj);
+			break;
+		case V4L2_CID_STATELESS_FWHT_PARAMS:
+			ctrl.ptr = retrace_v4l2_ctrl_fwht_params(ctrl_obj);
+		default:
+			break;
+		}
+	}
+	return ctrl;
+}
+
+struct v4l2_ext_control *retrace_v4l2_ext_control_array_pointer(json_object *ext_controls_obj, int count)
+{
+	struct v4l2_ext_control *ctrl_array_pointer = (struct v4l2_ext_control *) calloc(count, sizeof(v4l2_ext_control));
+
+	for (int i = 0; i < count; i++)
+		ctrl_array_pointer[i] = retrace_v4l2_ext_control(ext_controls_obj, i);
+
+	return ctrl_array_pointer;
+}
+
+void retrace_vidioc_s_ext_ctrls(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_ext_controls ext_controls;
+	memset(&ext_controls, 0, sizeof(ext_controls));
+
+	json_object *ext_controls_obj;
+	json_object_object_get_ex(ioctl_args, "v4l2_ext_controls", &ext_controls_obj);
+
+	json_object *which_obj;
+	json_object_object_get_ex(ext_controls_obj, "which", &which_obj);
+	ext_controls.which = json_object_get_int(which_obj);
+
+	json_object *count_obj;
+	json_object_object_get_ex(ext_controls_obj, "count", &count_obj);
+	ext_controls.count = json_object_get_int(count_obj);
+
+	/* request_fd is only valid for V4L2_CTRL_WHICH_REQUEST_VAL */
+	if (ext_controls.which == V4L2_CTRL_WHICH_REQUEST_VAL) {
+		json_object *request_fd_obj;
+		json_object_object_get_ex(ext_controls_obj, "request_fd", &request_fd_obj);
+		int request_fd_trace = json_object_get_int(request_fd_obj);
+		ext_controls.request_fd = get_fd_retrace_from_fd_trace(request_fd_trace);
+	}
+
+	ext_controls.controls = retrace_v4l2_ext_control_array_pointer(ext_controls_obj, ext_controls.count);
+
+	ioctl(fd_retrace, VIDIOC_S_EXT_CTRLS, &ext_controls);
+
+	if (verbose || (errno != 0))
+		perror("VIDIOC_S_EXT_CTRLS");
+
+	/* Free controls working backwards from the end of the controls array. */
+	for (int i = ((int) ext_controls.count - 1); i >= 0 ; i--) {
+		if (ext_controls.controls[i].ptr != nullptr)
+			free(ext_controls.controls[i].ptr);
+	}
+
+	if (ext_controls.controls != nullptr)
+		free(ext_controls.controls);
+}
+
+void retrace_query_ext_ctrl(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_query_ext_ctrl query_ext_ctrl;
+	memset(&query_ext_ctrl, 0, sizeof(v4l2_query_ext_ctrl));
+
+	json_object *query_ext_ctrl_obj;
+	json_object_object_get_ex(ioctl_args, "v4l2_query_ext_ctrl", &query_ext_ctrl_obj);
+
+	json_object *id_obj;
+	json_object_object_get_ex(query_ext_ctrl_obj, "id", &id_obj);
+	query_ext_ctrl.id = json_object_get_int64(id_obj);
+
+	ioctl(fd_retrace, VIDIOC_QUERY_EXT_CTRL, &query_ext_ctrl);
+
+	if (verbose) {
+		perror("VIDIOC_QUERY_EXT_CTRL");
+		fprintf(stderr, "id: %u\n\n", query_ext_ctrl.id);
+	}
+}
+
+void retrace_vidioc_enum_fmt(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_fmtdesc fmtdesc;
+	memset(&fmtdesc, 0, sizeof(v4l2_fmtdesc));
+
+	json_object *v4l2_fmtdesc_obj;
+	json_object_object_get_ex(ioctl_args, "v4l2_fmtdesc", &v4l2_fmtdesc_obj);
+
+	json_object *index_obj;
+	json_object_object_get_ex(v4l2_fmtdesc_obj, "index", &index_obj);
+	fmtdesc.index = json_object_get_int(index_obj);
+
+	json_object *type_obj;
+	json_object_object_get_ex(v4l2_fmtdesc_obj, "type", &type_obj);
+	fmtdesc.type = json_object_get_int(type_obj);
+
+	json_object *mbus_code_obj;
+	json_object_object_get_ex(v4l2_fmtdesc_obj, "mbus_code", &mbus_code_obj);
+	fmtdesc.mbus_code = json_object_get_int64(mbus_code_obj);
+
+	ioctl(fd_retrace, VIDIOC_ENUM_FMT, &fmtdesc);
+
+	if (verbose) {
+		perror("VIDIOC_ENUM_FMT");
+		fprintf(stderr, "index: %u\n", fmtdesc.index);
+		fprintf(stderr, "type: %u\n", fmtdesc.type);
+		fprintf(stderr, "flags: %u\n", fmtdesc.flags);
+		fprintf(stderr, "description: %s\n", fmtdesc.description);
+		fprintf(stderr, "pixelformat: %u\n", fmtdesc.pixelformat);
+		fprintf(stderr, "mbus_code: %u\n\n", fmtdesc.mbus_code);
+	}
+}
+
+void retrace_vidioc_querycap(int fd_retrace)
+{
+	struct v4l2_capability argp;
+	memset(&argp, 0, sizeof(v4l2_capability));
+
+	ioctl(fd_retrace, VIDIOC_QUERYCAP, &argp);
+
+	if (verbose || (errno != 0))
+		perror("VIDIOC_QUERYCAP");
+}
+
+void retrace_media_ioc_request_alloc(int fd_retrace, json_object *ioctl_args)
+{
+	/* Get the original request file descriptor from the original trace file. */
+	json_object *request_fd_trace_obj;
+	json_object_object_get_ex(ioctl_args, "request_fd", &request_fd_trace_obj);
+	int request_fd_trace = json_object_get_int(request_fd_trace_obj);
+
+	/* Allocate a request in the retrace context. */
+	__s32 request_fd_retrace = 0;
+	ioctl(fd_retrace, MEDIA_IOC_REQUEST_ALLOC, &request_fd_retrace);
+
+	/* Associate the original request file descriptor with the current request file descriptor. */
+	add_fd(request_fd_trace, request_fd_retrace);
+
+	if (verbose || (errno != 0))
+		perror("MEDIA_IOC_REQUEST_ALLOC");
+}
+
+void retrace_dma_buf_ioctl_sync(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct dma_buf_sync sync;
+	memset(&sync, 0, sizeof(dma_buf_sync));
+
+	json_object *sync_obj;
+	json_object_object_get_ex(ioctl_args_user, "dma_buf_sync",&sync_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(sync_obj, "flags",&flags_obj);
+	sync.flags = json_object_get_int(flags_obj);
+
+	ioctl(fd_retrace, DMA_BUF_IOCTL_SYNC, &sync);
+
+	if (verbose || (errno != 0))
+		perror("DMA_BUF_IOCTL_SYNC");
+}
+
+void retrace_ioctl_media(int fd_retrace, long request, json_object *ioctl_args_driver)
+{
+	switch (request){
+	case MEDIA_IOC_DEVICE_INFO:
+	case MEDIA_IOC_ENUM_ENTITIES:
+	case MEDIA_IOC_ENUM_LINKS:
+	case MEDIA_IOC_SETUP_LINK:
+	case MEDIA_IOC_G_TOPOLOGY: {
+		struct media_v2_topology top;
+		memset(&top, 0, sizeof(media_v2_topology));
+		ioctl(fd_retrace, MEDIA_IOC_G_TOPOLOGY, &top);
+		if (verbose || (errno != 0))
+			perror("MEDIA_IOC_G_TOPOLOGY");
+		break;
+	}
+	case MEDIA_IOC_REQUEST_ALLOC:
+		retrace_media_ioc_request_alloc(fd_retrace, ioctl_args_driver);
+		break;
+	case MEDIA_REQUEST_IOC_QUEUE: {
+		ioctl(fd_retrace, MEDIA_REQUEST_IOC_QUEUE);
+		if (verbose || (errno != 0))
+			perror("MEDIA_REQUEST_IOC_QUEUE");
+		break;
+	}
+	case MEDIA_REQUEST_IOC_REINIT: {
+		ioctl(fd_retrace, MEDIA_REQUEST_IOC_REINIT);
+		if (verbose || (errno != 0))
+			perror("MEDIA_REQUEST_IOC_REINIT");
+		break;
+	}
+	default:
+		break;
+	}
+}
+
+void retrace_ioctl_video(int fd_retrace, long request, json_object *ioctl_args_user, json_object *ioctl_args_driver)
+{
+	switch (request) {
+	case VIDIOC_QUERYCAP:
+		retrace_vidioc_querycap(fd_retrace);
+		break;
+	case VIDIOC_ENUM_FMT:
+		retrace_vidioc_enum_fmt(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_G_FMT:
+		retrace_vidioc_g_fmt(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_S_FMT:
+		retrace_vidioc_s_fmt(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_REQBUFS:
+		retrace_vidioc_reqbufs(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_QUERYBUF:
+		retrace_vidioc_querybuf(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_QBUF:
+		retrace_vidioc_qbuf(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_EXPBUF:
+		retrace_vidioc_expbuf(fd_retrace, ioctl_args_user, ioctl_args_driver);
+		break;
+	case VIDIOC_DQBUF:
+		retrace_vidioc_dqbuf(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_STREAMON:
+		retrace_vidioc_streamon(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_STREAMOFF:
+		retrace_vidioc_streamoff(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_S_EXT_CTRLS:
+		retrace_vidioc_s_ext_ctrls(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_QUERY_EXT_CTRL:
+		retrace_query_ext_ctrl(fd_retrace, ioctl_args_user);
+		break;
+	default:
+		break;
+	}
+}
+
+void retrace_ioctl(json_object *syscall_obj)
+{
+	int fd_retrace = 0;
+	__u8 ioctl_type = 0;
+	long request = 0;
+
+	json_object *fd_trace_obj;
+	json_object_object_get_ex(syscall_obj, "fd", &fd_trace_obj);
+	fd_retrace = get_fd_retrace_from_fd_trace(json_object_get_int(fd_trace_obj));
+
+	json_object *request_obj;
+	json_object_object_get_ex(syscall_obj, "request", &request_obj);
+	request = json_object_get_int64(request_obj);
+
+	json_object *ioctl_args_user;
+	json_object_object_get_ex(syscall_obj, "ioctl_args_from_userspace", &ioctl_args_user);
+
+	json_object *ioctl_args_driver;
+	json_object_object_get_ex(syscall_obj, "ioctl_args_from_driver", &ioctl_args_driver);
+
+	ioctl_type = _IOC_TYPE(request);
+	switch (ioctl_type) {
+	case 'V':
+		retrace_ioctl_video(fd_retrace, request, ioctl_args_user, ioctl_args_driver);
+		break;
+	case '|':
+		retrace_ioctl_media(fd_retrace, request, ioctl_args_driver);
+		break;
+	case 'b':
+		if (request == DMA_BUF_IOCTL_SYNC)
+			retrace_dma_buf_ioctl_sync(fd_retrace, ioctl_args_user);
+		break;
+	default:
+		break;
+	}
+}
+
+void write_to_output_buffer(unsigned char *buffer_pointer, int bytesused, json_object *mem_obj)
+{
+	std::string data;
+	int byteswritten = 0;
+	json_object *line_obj;
+	size_t number_of_lines;
+
+	json_object *mem_array_obj;
+	json_object_object_get_ex(mem_obj, "mem_array", &mem_array_obj);
+	number_of_lines = json_object_array_length(mem_array_obj);
+
+	for (long unsigned int i = 0; i < number_of_lines; i++) {
+		line_obj = json_object_array_get_idx(mem_array_obj, i);
+		data = json_object_get_string(line_obj);
+
+		for (long unsigned i = 0; i < data.length(); i++) {
+			if (std::isspace(data[i]))
+				continue;
+			try {
+				/* Two values from the string e.g. "D9" are needed to write one byte. */
+				*buffer_pointer = (char) std::stoi(data.substr(i,2), nullptr, 16);
+				buffer_pointer++;
+				i++;
+				byteswritten++;
+			} catch (std::invalid_argument& ia) {
+				fprintf(stderr, "Warning: \'%s\' is an invalid argument; %s line: %d.\n",
+				        data.substr(i,2).c_str(), __func__, __LINE__);
+			} catch (std::out_of_range& oor) {
+				fprintf(stderr, "Warning: \'%s\' is out of range; %s line: %d.\n",
+				        data.substr(i,2).c_str(), __func__, __LINE__);
+			}
+		}
+	}
+
+	if (verbose) {
+		fprintf(stderr, "\nWrite to Output Buffer\n");
+		fprintf(stderr, "bytesused: %d, byteswritten: %d\n", bytesused, byteswritten);
+		fprintf(stderr, "\n");
+	}
+}
+
+void write_decoded_frames_to_yuv_file_retrace(unsigned char *buffer_pointer, int bytesused)
+{
+	int byteswritten = 0;
+	int expected_length = (int) get_expected_length_retrace();
+
+	FILE *fp = fopen(retrace_filename.c_str(), "a");
+	for (int i = 0; i < bytesused; i++) {
+		if (i < expected_length) {
+			fwrite(&buffer_pointer[i], sizeof(unsigned char), 1, fp);
+			byteswritten++;
+		}
+	}
+	fclose(fp);
+
+	if (verbose){
+		fprintf(stderr, "\nWrite to File\n");
+		fprintf(stderr, "%s\n", retrace_filename.c_str());
+		fprintf(stderr, "buffer_pointer address: %ld, bytesused: %d, byteswritten: %d\n", (long) buffer_pointer, bytesused, byteswritten);
+		fprintf(stderr, "\n");
+		print_context();
+	}
+}
+
+void retrace_mem(json_object *mem_obj)
+{
+	json_object *type_obj;
+	json_object_object_get_ex(mem_obj, "type", &type_obj);
+	v4l2_buf_type type = (v4l2_buf_type) json_object_get_int(type_obj);
+
+	json_object *bytesused_obj;
+	json_object_object_get_ex(mem_obj, "bytesused", &bytesused_obj);
+	int bytesused = json_object_get_int64(bytesused_obj);
+	if (!bytesused)
+		return;
+
+	json_object *offset_obj;
+	json_object_object_get_ex(mem_obj, "offset", &offset_obj);
+	__u32 offset = json_object_get_int64(offset_obj);
+
+	json_object *address_obj;
+	json_object_object_get_ex(mem_obj, "address", &address_obj);
+	long buffer_address_trace = json_object_get_int64(address_obj);
+
+	long buffer_address_retrace = get_retrace_address_from_trace_address(buffer_address_trace);
+
+	unsigned char *buffer_pointer = (unsigned char *) buffer_address_retrace;
+
+	/* Get the encoded data from the json file and write it to output buffer memory. */
+	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
+		write_to_output_buffer(buffer_pointer, bytesused, mem_obj);
+
+	/* Get the decoded capture buffer from memory and write it to a binary yuv file. */
+	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		write_decoded_frames_to_yuv_file_retrace(buffer_pointer, bytesused);
+
+	if (verbose) {
+		fprintf(stderr, "\n%s\n", __func__);
+		fprintf(stderr, "%s, bytesused: %d, offset: %d, buffer_address_retrace: %ld\n", buftype2s(type).c_str(), bytesused, offset, buffer_address_retrace);
+		print_context();
+	}
+}
+
+void retrace_object(json_object *jobj)
+{
+	json_object *syscall_obj;
+	int ret = json_object_object_get_ex(jobj, "syscall", &syscall_obj);
+	int syscall = json_object_get_int(syscall_obj);
+
+	/* If the json object doesn't hold a syscall, check if it holds a memory dump. */
+	if (ret == 0) {
+		json_object *temp_obj;
+		if (json_object_object_get_ex(jobj, "mem_dump", &temp_obj))
+			retrace_mem(jobj);
+		return;
+	}
+
+	errno = 0;
+
+	switch (syscall) {
+	case LIBTRACER_SYSCALL_IOCTL:
+		retrace_ioctl(jobj);
+		break;
+	case LIBTRACER_SYSCALL_OPEN:
+		retrace_open(jobj, false);
+		break;
+	case LIBTRACER_SYSCALL_OPEN64:
+		retrace_open(jobj, true);
+		break;
+	case LIBTRACER_SYSCALL_CLOSE:
+		retrace_close(jobj);
+		break;
+	case LIBTRACER_SYSCALL_MMAP:
+		retrace_mmap(jobj, false);
+		break;
+	case LIBTRACER_SYSCALL_MMAP64:
+		retrace_mmap(jobj, true);
+		break;
+	case LIBTRACER_SYSCALL_MUNMAP:
+		retrace_munmap(jobj);
+		break;
+	default:
+		break;
+	}
+}
+
+void retrace_array(json_object *root_array_obj)
+{
+	json_object *jobj;
+	struct array_list *al = json_object_get_array(root_array_obj);
+	size_t json_objects_in_file = array_list_length(al);
+
+	for (size_t i = 0; i < json_objects_in_file; i++) {
+		jobj = (json_object *) array_list_get_idx(al, i);
+		retrace_object(jobj);
+	}
+}
+
+int get_options_retrace(int argc, char *argv[])
+{
+	int ch;
+	do {
+		ch = getopt_long(argc, argv, short_options_retracer, long_options_retracer, NULL);
+		switch (ch){
+		case OptSetDevice_Retracer: {
+			std::string device = optarg;
+			if (device[0] >= '0' && device[0] <= '9' && device.length() <= 3) {
+				static char newdev[20];
+				sprintf(newdev, "/dev/video%s", optarg);
+				dev_path_video = newdev;
+				fprintf(stderr, "Using: %s\n", dev_path_video.c_str());
+			} else {
+				return 1;
+			}
+			break;
+		}
+		case OptSetMediaDevice_Retracer: {
+			std::string device = optarg;
+			if (device[0] >= '0' && device[0] <= '9' && device.length() <= 3) {
+				static char newdev[20];
+				sprintf(newdev, "/dev/media%s", optarg);
+				device = newdev;
+				dev_path_media = newdev;
+				fprintf(stderr, "Using: %s\n", dev_path_media.c_str());
+			} else {
+				return 1;
+			}
+			break;
+		}
+		case OptVerbose_Retracer:
+			verbose = true;
+			break;
+		case '?':
+		case ':':
+			return 1;
+		default:
+			break;
+		}
+	} while (ch != -1);
+
+	return 0;
+}
+
+void print_help_retracer(void)
+{
+		fprintf(stderr, "v4l2-tracer retrace [retrace options] -- <trace_file>.json\n"
+	        "\t-d, --device <dev>   Use a different video device than specified in the trace file.\n"
+	        "\t                     <dev> must be a digit corresponding to an existing /dev/video<dev> \n"
+	        "\t-h, --help           Display retrace help.\n"
+	        "\t-m, --media <dev>    Use a different media device than specified in the trace file.\n"
+	        "\t                     <dev> must be a digit corresponding to an existing /dev/media<dev> \n"
+	        "\t-v, --verbose        Turn on verbose reporting\n\n");
+}
+
+int retracer(int argc, char *argv[])
+{
+	if ((get_options_retrace(argc, argv)) || (optind == argc)) {
+		print_help_retracer();
+		return 1;
+	}
+
+	std::string trace_filename = argv[optind];
+
+	if (trace_filename.substr(trace_filename.length()-4, trace_filename.npos) != "json") {
+		fprintf(stderr, "Trace file must be json-formatted: %s\n", trace_filename.c_str());
+		print_help_retracer();
+		return 1;
+	}
+
+	FILE *trace_file = fopen(trace_filename.c_str(), "r");
+	if (trace_file == NULL) {
+		fprintf(stderr, "Trace file error: %s\n", trace_filename.c_str());
+		return 1;
+	}
+	fclose(trace_file);
+
+	fprintf(stderr, "Retracing: %s\n", trace_filename.c_str());
+
+	/* Create file to hold the decoded frames.  Discard previous retraced file if any. */
+	retrace_filename = trace_filename;
+	retrace_filename = retrace_filename.replace(5, retrace_filename.npos, "_retrace");
+	retrace_filename += ".yuv";
+
+	/* Discard any previous retrace with same name. */
+	fclose(fopen(retrace_filename.c_str(), "w"));
+
+	json_object *root_array_obj = json_object_from_file(trace_filename.c_str());
+	retrace_array(root_array_obj);
+	json_object_put(root_array_obj);
+
+	fprintf(stderr, "Retracing complete in %s\n", retrace_filename.c_str());
+
+	return 0;
+}
utils/v4l2-tracer/trace-fwht.cpp new file +11 -0
new file mode 100644
index 00000000..4d31df51
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "libtracer.h"
+
+void trace_v4l2_ctrl_fwht_params(void *p_fwht_params, json_object *ctrl_obj)
+{
+	json_object *fwht_params_obj = json_object_new_object();
+	struct v4l2_ctrl_fwht_params *fwht_params = static_cast<struct v4l2_ctrl_fwht_params*>(p_fwht_params);

utils/v4l2-tracer/trace-fwht.cpp line 21

Quoted 10 lines
utils/v4l2-tracer/trace-fwht.cpp new file +10 -0
+
+	json_object_object_add(fwht_params_obj, "backward_ref_ts", json_object_new_uint64(fwht_params->backward_ref_ts));
+	json_object_object_add(fwht_params_obj, "version", json_object_new_int64(fwht_params->version));
+	json_object_object_add(fwht_params_obj, "width", json_object_new_int64(fwht_params->width));
+	json_object_object_add(fwht_params_obj, "height", json_object_new_int64(fwht_params->height));
+	json_object_object_add(fwht_params_obj, "flags", json_object_new_int64(fwht_params->flags));
+	json_object_object_add(fwht_params_obj, "colorspace", json_object_new_int64(fwht_params->colorspace));
+	json_object_object_add(fwht_params_obj, "xfer_func", json_object_new_int64(fwht_params->xfer_func));
+	json_object_object_add(fwht_params_obj, "ycbcr_enc", json_object_new_int64(fwht_params->ycbcr_enc));
+	json_object_object_add(fwht_params_obj, "quantization", json_object_new_int64(fwht_params->quantization));

This too looks like a nice candidate for code generation.

Why is this using json_object_new_int64() instead of json_object_new_int()?

I would also make a #define for this:

#define obj_add_u32(ptr, field) \
	json_object_object_add(ptr ## _obj, #field, json_object_new_int(ptr->field))

(I hope I got the syntax right, and feel free to change the naming)

That way you can just do:

obj_add_u32(fwht_params, version)

If this was generated, then I probably wouldn't use a helper define. But if you do it manually like it is now, then such a define prevents mistakes.

utils/v4l2-tracer/trace-info.cpp

Quoted 1,129 lines
utils/v4l2-tracer/trace-fwht.cpp new file +3 -0
+
+	json_object_object_add(ctrl_obj, "v4l2_ctrl_fwht_params", fwht_params_obj);
+}
utils/v4l2-tracer/trace-fwht.h new file +11 -0
new file mode 100644
index 00000000..566a3eca
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef TRACE_FWHT_H
+#define TRACE_FWHT_H
+
+void trace_v4l2_ctrl_fwht_params(void *p_fwht_params, json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/trace-h264.cpp new file +281 -0
new file mode 100644
index 00000000..d2465e0e
@@ -0,0 +1,281 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "libtracer.h"
+
+void trace_v4l2_ctrl_h264_decode_mode(struct v4l2_ext_control ctrl, json_object *ctrl_obj)
+{
+	json_object *h264_decode_mode_obj = json_object_new_object();
+	json_object_object_add(h264_decode_mode_obj, "value", json_object_new_int(ctrl.value));
+	json_object_object_add(h264_decode_mode_obj, "value_str", json_object_new_string(h264_decode_mode2s(ctrl.value).c_str()));
+	json_object_object_add(ctrl_obj, "V4L2_CID_STATELESS_H264_DECODE_MODE", h264_decode_mode_obj);
+}
+
+void trace_v4l2_ctrl_h264_start_code(struct v4l2_ext_control ctrl, json_object *ctrl_obj)
+{
+	json_object *h264_start_code_obj = json_object_new_object();
+	json_object_object_add(h264_start_code_obj, "value", json_object_new_int(ctrl.value));
+	json_object_object_add(h264_start_code_obj, "value_str", json_object_new_string(h264_start_code2s(ctrl.value).c_str()));
+	json_object_object_add(ctrl_obj, "V4L2_CID_STATELESS_H264_START_CODE", h264_start_code_obj);
+}
+
+void trace_v4l2_ctrl_h264_sps(void *p_h264_sps, json_object *ctrl_obj)
+{
+	json_object *h264_sps_obj = json_object_new_object();
+	struct v4l2_ctrl_h264_sps *h264_sps = static_cast<struct v4l2_ctrl_h264_sps*>(p_h264_sps);
+
+	json_object_object_add(h264_sps_obj, "profile_idc", json_object_new_int(h264_sps->profile_idc));
+	json_object_object_add(h264_sps_obj, "constraint_set_flags", json_object_new_int(h264_sps->constraint_set_flags));
+	json_object_object_add(h264_sps_obj, "level_idc", json_object_new_int(h264_sps->level_idc));
+	json_object_object_add(h264_sps_obj, "seq_parameter_set_id", json_object_new_int(h264_sps->seq_parameter_set_id));
+	json_object_object_add(h264_sps_obj, "chroma_format_idc", json_object_new_int(h264_sps->chroma_format_idc));
+	json_object_object_add(h264_sps_obj, "bit_depth_luma_minus8", json_object_new_int(h264_sps->bit_depth_luma_minus8));
+	json_object_object_add(h264_sps_obj, "bit_depth_chroma_minus8", json_object_new_int(h264_sps->bit_depth_chroma_minus8));
+	json_object_object_add(h264_sps_obj, "log2_max_frame_num_minus4", json_object_new_int(h264_sps->log2_max_frame_num_minus4));
+	json_object_object_add(h264_sps_obj, "pic_order_cnt_type", json_object_new_int(h264_sps->pic_order_cnt_type));
+	json_object_object_add(h264_sps_obj, "log2_max_pic_order_cnt_lsb_minus4", json_object_new_int(h264_sps->log2_max_pic_order_cnt_lsb_minus4));
+	json_object_object_add(h264_sps_obj, "max_num_ref_frames", json_object_new_int(h264_sps->max_num_ref_frames));
+	json_object_object_add(h264_sps_obj, "num_ref_frames_in_pic_order_cnt_cycle", json_object_new_int(h264_sps->num_ref_frames_in_pic_order_cnt_cycle));
+
+	/* 	__s32 offset_for_ref_frame[255] */
+	json_object *offset_for_ref_frame_obj =  json_object_new_array_ext(255);
+	for (size_t index = 0; index < ARRAY_SIZE(h264_sps->offset_for_ref_frame); index++)
+		json_object_array_add(offset_for_ref_frame_obj, json_object_new_int(h264_sps->offset_for_ref_frame[index]));
+
+	json_object_object_add(h264_sps_obj, "offset_for_ref_frame", offset_for_ref_frame_obj);
+	json_object_object_add(h264_sps_obj, "offset_for_non_ref_pic", json_object_new_int(h264_sps->offset_for_non_ref_pic));
+	json_object_object_add(h264_sps_obj, "offset_for_top_to_bottom_field", json_object_new_int(h264_sps->offset_for_top_to_bottom_field));
+	json_object_object_add(h264_sps_obj, "pic_width_in_mbs_minus1", json_object_new_int(h264_sps->pic_width_in_mbs_minus1));
+	json_object_object_add(h264_sps_obj, "pic_height_in_map_units_minus1", json_object_new_int(h264_sps->pic_height_in_map_units_minus1));
+	json_object_object_add(h264_sps_obj, "flags", json_object_new_uint64(h264_sps->flags));
+	json_object_object_add(h264_sps_obj, "flags_str", json_object_new_string(h264_sps_flag2s(h264_sps->flags).c_str()));
+
+	json_object_object_add(ctrl_obj, "v4l2_ctrl_h264_sps", h264_sps_obj);
+}
+
+void trace_v4l2_ctrl_h264_pps(void *p_h264_pps, json_object *ctrl_obj)
+{
+	json_object *h264_pps_obj = json_object_new_object();
+	struct v4l2_ctrl_h264_pps *h264_pps = static_cast<struct v4l2_ctrl_h264_pps*>(p_h264_pps);
+
+	json_object_object_add(h264_pps_obj, "pic_parameter_set_id", json_object_new_int(h264_pps->pic_parameter_set_id));
+	json_object_object_add(h264_pps_obj, "seq_parameter_set_id", json_object_new_int(h264_pps->seq_parameter_set_id));
+	json_object_object_add(h264_pps_obj, "num_slice_groups_minus1", json_object_new_int(h264_pps->num_slice_groups_minus1));
+	json_object_object_add(h264_pps_obj, "num_ref_idx_l0_default_active_minus1", json_object_new_int(h264_pps->num_ref_idx_l0_default_active_minus1));
+	json_object_object_add(h264_pps_obj, "num_ref_idx_l1_default_active_minus1", json_object_new_int(h264_pps->num_ref_idx_l1_default_active_minus1));
+	json_object_object_add(h264_pps_obj, "weighted_bipred_idc", json_object_new_int(h264_pps->weighted_bipred_idc));
+	json_object_object_add(h264_pps_obj, "pic_init_qp_minus26", json_object_new_int(h264_pps->pic_init_qp_minus26));
+	json_object_object_add(h264_pps_obj, "pic_init_qs_minus26", json_object_new_int(h264_pps->pic_init_qs_minus26));
+	json_object_object_add(h264_pps_obj, "chroma_qp_index_offset", json_object_new_int(h264_pps->chroma_qp_index_offset));
+	json_object_object_add(h264_pps_obj, "second_chroma_qp_index_offset", json_object_new_int(h264_pps->second_chroma_qp_index_offset));
+	json_object_object_add(h264_pps_obj, "flags", json_object_new_int(h264_pps->flags));
+	json_object_object_add(h264_pps_obj, "flags_str", json_object_new_string(h264_pps_flag2s(h264_pps->flags).c_str()));
+
+	json_object_object_add(ctrl_obj, "v4l2_ctrl_h264_pps", h264_pps_obj);
+}
+
+void trace_v4l2_ctrl_h264_scaling_matrix(void *p_h264_scaling_matrix, json_object *ctrl_obj)
+{
+	json_object *h264_scaling_matrix_obj = json_object_new_object();
+	struct v4l2_ctrl_h264_scaling_matrix *h264_scaling_matrix;
+	h264_scaling_matrix = static_cast<struct v4l2_ctrl_h264_scaling_matrix*>(p_h264_scaling_matrix);
+
+	/*__u8 scaling_list_4x4[6][16] */
+	json_object *scaling_list_4x4_obj = json_object_new_array();
+	for (size_t i = 0; i < ARRAY_SIZE(h264_scaling_matrix->scaling_list_4x4); i++)
+		for (size_t j = 0; j < ARRAY_SIZE(h264_scaling_matrix->scaling_list_4x4[0]); j++)
+			json_object_array_add(scaling_list_4x4_obj, json_object_new_int(h264_scaling_matrix->scaling_list_4x4[i][j]));
+
+	json_object_object_add(h264_scaling_matrix_obj, "scaling_list_4x4", scaling_list_4x4_obj);
+
+	/*	__u8 scaling_list_8x8[6][64] */
+	json_object *scaling_list_8x8_obj = json_object_new_array();
+	for (size_t i = 0; i < ARRAY_SIZE(h264_scaling_matrix->scaling_list_8x8); i++)
+		for (size_t j = 0; j < ARRAY_SIZE(h264_scaling_matrix->scaling_list_8x8[0]); j++)
+			json_object_array_add(scaling_list_8x8_obj, json_object_new_int(h264_scaling_matrix->scaling_list_8x8[i][j]));
+
+	json_object_object_add(h264_scaling_matrix_obj, "scaling_list_8x8", scaling_list_8x8_obj);
+
+	json_object_object_add(ctrl_obj, "v4l2_ctrl_h264_scaling_matrix", h264_scaling_matrix_obj);
+}
+
+json_object *trace_v4l2_h264_weight_factors(struct v4l2_h264_weight_factors wf)
+{
+	json_object *weight_factors_obj = json_object_new_object();
+
+	/*__s16 luma_weight[32] */
+	json_object *luma_weight_obj = json_object_new_array();
+	for (size_t i = 0; i < ARRAY_SIZE(wf.luma_weight); i++)
+			json_object_array_add(luma_weight_obj, json_object_new_int(wf.luma_weight[i]));
+	json_object_object_add(weight_factors_obj, "luma_weight", luma_weight_obj);
+
+	/* __s16 luma_offset[32] */
+	json_object *luma_offset_obj = json_object_new_array();
+	for (size_t i = 0; i < ARRAY_SIZE(wf.luma_offset); i++)
+			json_object_array_add(luma_offset_obj, json_object_new_int(wf.luma_offset[i]));
+	json_object_object_add(weight_factors_obj, "luma_offset", luma_offset_obj);
+
+	/* __s16 chroma_weight[32][2] */
+	json_object *chroma_weight_obj = json_object_new_array();
+	for (size_t i = 0; i < ARRAY_SIZE(wf.chroma_weight); i++)
+		for (size_t j = 0; j < ARRAY_SIZE(wf.chroma_weight[0]); j++)
+			json_object_array_add(chroma_weight_obj, json_object_new_int(wf.chroma_weight[i][j]));
+	json_object_object_add(weight_factors_obj, "chroma_weight", chroma_weight_obj);
+
+	/* __s16 chroma_offset[32][2] */
+	json_object *chroma_offset_obj = json_object_new_array();
+	for (size_t i = 0; i < ARRAY_SIZE(wf.chroma_offset); i++)
+		for (size_t j = 0; j < ARRAY_SIZE(wf.chroma_offset[0]); j++)
+				json_object_array_add(chroma_offset_obj, json_object_new_int(wf.chroma_offset[i][j]));
+	json_object_object_add(weight_factors_obj, "chroma_offset", chroma_offset_obj);
+
+	return weight_factors_obj;
+}
+
+void trace_v4l2_ctrl_h264_pred_weights(void *p_h264_pred_weights, json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_h264_pred_weights *h264_pred_weights;
+	h264_pred_weights = static_cast<struct v4l2_ctrl_h264_pred_weights*>(p_h264_pred_weights);
+
+	json_object *h264_pred_weights_obj = json_object_new_object();
+
+	json_object_object_add(h264_pred_weights_obj, "luma_log2_weight_denom",
+	                       json_object_new_int(h264_pred_weights->luma_log2_weight_denom));
+
+	json_object_object_add(h264_pred_weights_obj, "chroma_log2_weight_denom",
+	                       json_object_new_int(h264_pred_weights->chroma_log2_weight_denom));
+
+	/* struct v4l2_h264_weight_factors weight_factors[2] */
+	json_object *weight_factors_array_obj = json_object_new_array_ext(2);
+	for (size_t i = 0; i < ARRAY_SIZE(h264_pred_weights->weight_factors); i++) {
+		json_object *wf = trace_v4l2_h264_weight_factors(h264_pred_weights->weight_factors[i]);
+		json_object_array_add(weight_factors_array_obj, wf);
+	}
+	json_object_object_add(h264_pred_weights_obj, "weight_factors", weight_factors_array_obj);
+	json_object_object_add(ctrl_obj, "v4l2_ctrl_h264_pred_weights", h264_pred_weights_obj);
+}
+
+json_object *trace_v4l2_h264_reference(struct v4l2_h264_reference ref)
+{
+	json_object *h264_reference_obj = json_object_new_object();
+
+	json_object_object_add(h264_reference_obj, "fields", json_object_new_int(ref.fields));
+	json_object_object_add(h264_reference_obj, "index", json_object_new_int(ref.index));
+
+	return h264_reference_obj;
+}
+
+void trace_v4l2_ctrl_h264_slice_params(void *p_h264_slice_params, json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_h264_slice_params *h264_slice_params;
+	h264_slice_params = static_cast<struct v4l2_ctrl_h264_slice_params*>(p_h264_slice_params);
+
+	json_object *h264_slice_params_obj = json_object_new_object();
+
+	json_object_object_add(h264_slice_params_obj, "header_bit_size",
+	                       json_object_new_uint64(h264_slice_params->header_bit_size));
+	json_object_object_add(h264_slice_params_obj, "first_mb_in_slice",
+	                       json_object_new_uint64(h264_slice_params->first_mb_in_slice));
+	json_object_object_add(h264_slice_params_obj, "slice_type",
+	                       json_object_new_int(h264_slice_params->slice_type));
+	json_object_object_add(h264_slice_params_obj, "colour_plane_id",
+	                       json_object_new_int(h264_slice_params->colour_plane_id));
+	json_object_object_add(h264_slice_params_obj, "redundant_pic_cnt",
+	                       json_object_new_int(h264_slice_params->redundant_pic_cnt));
+	json_object_object_add(h264_slice_params_obj, "cabac_init_idc",
+	                       json_object_new_int(h264_slice_params->cabac_init_idc));
+	json_object_object_add(h264_slice_params_obj, "slice_qp_delta",
+	                       json_object_new_int(h264_slice_params->slice_qp_delta));
+	json_object_object_add(h264_slice_params_obj, "slice_qs_delta",
+	                       json_object_new_int(h264_slice_params->slice_qs_delta));
+	json_object_object_add(h264_slice_params_obj, "disable_deblocking_filter_idc",
+	                       json_object_new_int(h264_slice_params->disable_deblocking_filter_idc));
+	json_object_object_add(h264_slice_params_obj, "slice_alpha_c0_offset_div2",
+	                       json_object_new_int(h264_slice_params->slice_alpha_c0_offset_div2));
+	json_object_object_add(h264_slice_params_obj, "slice_beta_offset_div2",
+	                       json_object_new_int(h264_slice_params->slice_beta_offset_div2));
+	json_object_object_add(h264_slice_params_obj, "num_ref_idx_l0_active_minus1",
+	                       json_object_new_int(h264_slice_params->num_ref_idx_l0_active_minus1));
+	json_object_object_add(h264_slice_params_obj, "num_ref_idx_l1_active_minus1",
+	                       json_object_new_int(h264_slice_params->num_ref_idx_l1_active_minus1));
+
+	/* 	struct v4l2_h264_reference ref_pic_list0[V4L2_H264_REF_LIST_LEN] */
+	json_object *ref_pic_list0_obj = json_object_new_array();
+	for (size_t i = 0; i < ARRAY_SIZE(h264_slice_params->ref_pic_list0); i++)
+		json_object_array_add(ref_pic_list0_obj, trace_v4l2_h264_reference(h264_slice_params->ref_pic_list0[i]));
+	json_object_object_add(h264_slice_params_obj, "ref_pic_list0", ref_pic_list0_obj);
+
+	/* 	struct v4l2_h264_reference ref_pic_list1[V4L2_H264_REF_LIST_LEN] */
+	json_object *ref_pic_list1_obj = json_object_new_array();
+	for (size_t i = 0; i < ARRAY_SIZE(h264_slice_params->ref_pic_list1); i++) {
+		json_object_array_add(ref_pic_list1_obj, trace_v4l2_h264_reference(h264_slice_params->ref_pic_list1[i]));
+	}
+	json_object_object_add(h264_slice_params_obj, "ref_pic_list1", ref_pic_list1_obj);
+
+	json_object_object_add(h264_slice_params_obj, "flags", json_object_new_uint64(h264_slice_params->flags));
+	json_object_object_add(h264_slice_params_obj, "flags_str", json_object_new_string(h264_slice_flag2s(h264_slice_params->flags).c_str()));
+
+	json_object_object_add(ctrl_obj, "v4l2_ctrl_h264_slice_params", h264_slice_params_obj);
+}
+
+json_object *trace_v4l2_h264_dpb_entry(struct v4l2_h264_dpb_entry entry)
+{
+	json_object *h264_dpb_entry_obj = json_object_new_object();
+
+	json_object_object_add(h264_dpb_entry_obj, "reference_ts", json_object_new_uint64(entry.reference_ts));
+	json_object_object_add(h264_dpb_entry_obj, "pic_num", json_object_new_uint64(entry.pic_num));
+	json_object_object_add(h264_dpb_entry_obj, "frame_num", json_object_new_int(entry.frame_num));
+	json_object_object_add(h264_dpb_entry_obj, "fields", json_object_new_int(entry.fields));
+	json_object_object_add(h264_dpb_entry_obj, "top_field_order_cnt", json_object_new_int(entry.top_field_order_cnt));
+	json_object_object_add(h264_dpb_entry_obj, "bottom_field_order_cnt", json_object_new_int(entry.bottom_field_order_cnt));
+	json_object_object_add(h264_dpb_entry_obj, "flags", json_object_new_uint64(entry.flags));
+	json_object_object_add(h264_dpb_entry_obj, "flags_str", json_object_new_string(h264_dpb_entry_flag2s(entry.flags).c_str()));
+
+	return h264_dpb_entry_obj;
+}
+
+void trace_v4l2_ctrl_h264_decode_params(void *p_h264_decode_params, json_object *ctrl_obj)
+{
+	struct v4l2_ctrl_h264_decode_params *h264_decode_params;
+	h264_decode_params = static_cast<struct v4l2_ctrl_h264_decode_params*>(p_h264_decode_params);
+
+	json_object *h264_decode_params_obj = json_object_new_object();
+
+	/* 	struct v4l2_h264_dpb_entry dpb[V4L2_H264_NUM_DPB_ENTRIES] */
+	json_object *dpb_obj = json_object_new_array();
+	for (int i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++)
+		json_object_array_add(dpb_obj, trace_v4l2_h264_dpb_entry(h264_decode_params->dpb[i]));
+	json_object_object_add(h264_decode_params_obj, "dpb", dpb_obj);
+
+	json_object_object_add(h264_decode_params_obj, "nal_ref_idc",
+	                       json_object_new_int(h264_decode_params->nal_ref_idc));
+	json_object_object_add(h264_decode_params_obj, "frame_num",
+	                       json_object_new_int(h264_decode_params->frame_num));
+	json_object_object_add(h264_decode_params_obj, "top_field_order_cnt",
+	                       json_object_new_int(h264_decode_params->top_field_order_cnt));
+	json_object_object_add(h264_decode_params_obj, "bottom_field_order_cnt",
+	                       json_object_new_int(h264_decode_params->bottom_field_order_cnt));
+	json_object_object_add(h264_decode_params_obj, "idr_pic_id",
+	                       json_object_new_int(h264_decode_params->idr_pic_id));
+	json_object_object_add(h264_decode_params_obj, "pic_order_cnt_lsb",
+	                       json_object_new_int(h264_decode_params->pic_order_cnt_lsb));
+	json_object_object_add(h264_decode_params_obj, "delta_pic_order_cnt_bottom",
+	                       json_object_new_int(h264_decode_params->delta_pic_order_cnt_bottom));
+	json_object_object_add(h264_decode_params_obj, "delta_pic_order_cnt0",
+	                       json_object_new_int(h264_decode_params->delta_pic_order_cnt0));
+	json_object_object_add(h264_decode_params_obj, "delta_pic_order_cnt1",
+	                       json_object_new_int(h264_decode_params->delta_pic_order_cnt1));
+	json_object_object_add(h264_decode_params_obj, "dec_ref_pic_marking_bit_size",
+	                       json_object_new_uint64(h264_decode_params->dec_ref_pic_marking_bit_size));
+	json_object_object_add(h264_decode_params_obj, "pic_order_cnt_bit_size",
+	                       json_object_new_uint64(h264_decode_params->pic_order_cnt_bit_size));
+	json_object_object_add(h264_decode_params_obj, "slice_group_change_cycle",
+	                       json_object_new_uint64(h264_decode_params->slice_group_change_cycle));
+	json_object_object_add(h264_decode_params_obj, "flags", json_object_new_uint64(h264_decode_params->flags));
+	json_object_object_add(h264_decode_params_obj, "flags_str",
+						   json_object_new_string(h264_decode_params_flag2s(h264_decode_params->flags).c_str()));
+
+	json_object_object_add(ctrl_obj, "v4l2_ctrl_h264_decode_params", h264_decode_params_obj);
+}
utils/v4l2-tracer/trace-h264.h new file +18 -0
new file mode 100644
index 00000000..fea0f598
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef TRACE_H264_H
+#define TRACE_H264_H
+
+void trace_v4l2_ctrl_h264_decode_mode(struct v4l2_ext_control ctrl, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_start_code(struct v4l2_ext_control ctrl, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_sps(void *p_h264_sps, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_pps(void *p_h264_pps, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_scaling_matrix(void *p_h264_scaling_matrix, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_pred_weights(void *p_h264_pred_weights, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_slice_params(void *p_h264_slice_params, json_object *ctrl_obj);
+void trace_v4l2_ctrl_h264_decode_params(void *p_h264_decode_params, json_object *ctrl_obj);
+
+#endif
utils/v4l2-tracer/trace-helper.cpp new file +667 -0
new file mode 100644
index 00000000..06595529
@@ -0,0 +1,667 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "libtracer.h"
+
+struct trace_options options;
+
+struct trace_context ctx_trace = {
+	.lock = PTHREAD_MUTEX_INITIALIZER
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/trace-helper.h new file +81 -0
new file mode 100644
index 00000000..b8cb196a

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/trace-info.cpp new file +32 -0
new file mode 100644
index 00000000..dccfe6a2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

You want to use a define for this as well. Something like:

#define ID2STR(id) { id, #id }

This avoids accidental mismatches.

It would be even better if this could be generated from the header.

I think a lot of the code in this header can be autogenerated.

utils/v4l2-tracer/trace-info.cpp

Quoted 169 lines
utils/v4l2-tracer/trace-info.cpp new file +169 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Drop this, ASYNCIO is no longer in use.

utils/v4l2-tracer/trace-info.cpp

Quoted 5 lines
utils/v4l2-tracer/trace-info.cpp new file +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

A simple command to generate this would be:

grep V4L2_CAP_ include/linux/videodev2.h | sed 's/#define \([A-Z_0-9]*\).*/\t{ \1, "\1" },/' | grep -v V4L2_CAP_TIMEPERFRAME | grep -v V4L2_CAP_ASYNCIO

utils/v4l2-tracer/trace-info.cpp

Quoted 25 lines
utils/v4l2-tracer/trace-info.cpp new file +25 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

grep define.*V4L2_VP8_SEGMENT_FLAG_ include/linux/v4l2-controls.h | sed 's/#define \([A-Z_0-9]*\).*/\t{ \1, "\1" },/'

etc.

So you can make a shell/perl/python script that generates this straight from the headers.

utils/v4l2-tracer/v4l2-tracer.h

Quoted 1,492 lines
utils/v4l2-tracer/trace-info.cpp new file +229 -0

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/trace-info.h new file +93 -0
new file mode 100644
index 00000000..946e2334

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/trace-vp8.cpp new file +183 -0
new file mode 100644
index 00000000..11741134

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/trace-vp8.h new file +11 -0
new file mode 100644
index 00000000..96187eea

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/trace.cpp new file +582 -0
new file mode 100644
index 00000000..0e478a13

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/trace.h new file +17 -0
new file mode 100644
index 00000000..9b431fea

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/v4l2-tracer.1.in new file +109 -0
new file mode 100644
index 00000000..a287fb09

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/v4l2-tracer.cpp new file +181 -0
new file mode 100644
index 00000000..05c7c7d0

Lines of this file are not shown. View the raw message.

utils/v4l2-tracer/v4l2-tracer.h new file +39 -0
new file mode 100644
index 00000000..7b280b4f

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Note that it is OK to add autogeneration later, but I would definitely introduce helpers. The way it is now it is too easy to make mistakes.

Regards,

Hans

Part of the diff is not shown here. View the raw message.