Skip to contentSkip to conversation

media: v4l2-ctrls: Add average qp control

PATCH Superseded Ming Qian 3 participants 11 messages Apr 25, 2024 – Apr 30, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ming Qian

author maintainer
#0 lore raw

PATCH v2 1/3 media: v4l2-ctrls: Add average qp control

Add a control V4L2_CID_MPEG_VIDEO_AVERAGE_QP to report the average qp value of current encoded frame. the value applies to the last dequeued capture buffer.

---
v2
- improve document description according Hans's comments
- drop volatile flag

 Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst | 5 +++++
 drivers/media/v4l2-core/v4l2-ctrls-defs.c                 | 5 +++++
 include/uapi/linux/v4l2-controls.h                        | 2 ++
 3 files changed, 12 insertions(+)
Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst +5 -0
index 2a165ae063fb..7d82ab14b8ba 100644
@@ -1653,6 +1653,11 @@ enum v4l2_mpeg_video_h264_hierarchical_coding_type -
     Quantization parameter for a P frame for FWHT. Valid range: from 1
     to 31.
 
+``V4L2_CID_MPEG_VIDEO_AVERAGE_QP (integer)``
+    This read-only control returns the average QP value of the currently
+    encoded frame. The value applies to the last dequeued capture buffer
+    (VIDIOC_DQBUF). Applicable to encoders.
+
 .. raw:: latex
 
     \normalsize
drivers/media/v4l2-core/v4l2-ctrls-defs.c +5 -0
index 8696eb1cdd61..1ea52011247a 100644
@@ -970,6 +970,7 @@ const char *v4l2_ctrl_get_name(u32 id)
 	case V4L2_CID_MPEG_VIDEO_LTR_COUNT:			return "LTR Count";
 	case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:		return "Frame LTR Index";
 	case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:		return "Use LTR Frames";
+	case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:			return "Average QP Value";
 	case V4L2_CID_FWHT_I_FRAME_QP:				return "FWHT I-Frame QP Value";
 	case V4L2_CID_FWHT_P_FRAME_QP:				return "FWHT P-Frame QP Value";
 
@@ -1507,6 +1508,10 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
 		*max = 0xffffffffffffLL;
 		*step = 1;
 		break;
+	case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:
+		*type = V4L2_CTRL_TYPE_INTEGER;
+		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
+		break;
 	case V4L2_CID_PIXEL_RATE:
 		*type = V4L2_CTRL_TYPE_INTEGER64;
 		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
include/uapi/linux/v4l2-controls.h +2 -0
index 99c3f5e99da7..974fd254e573 100644
@@ -898,6 +898,8 @@ enum v4l2_mpeg_video_av1_level {
 	V4L2_MPEG_VIDEO_AV1_LEVEL_7_3 = 23
 };
 
+#define V4L2_CID_MPEG_VIDEO_AVERAGE_QP  (V4L2_CID_CODEC_BASE + 657)
+
 /*  MPEG-class control IDs specific to the CX2341x driver as defined by V4L2 */
 #define V4L2_CID_CODEC_CX2341X_BASE				(V4L2_CTRL_CLASS_CODEC | 0x1000)
 #define V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE		(V4L2_CID_CODEC_CX2341X_BASE+0)
Signature · 1 line
2.43.0-rc1
author maintainer
#1 lore raw

PATCH v2 2/3 media: amphion: Report the average QP of current encoded frame

Report the average QP value of current encoded frame via the control V4L2_CID_MPEG_VIDEO_AVERAGE_QP, the value applies to the last dequeued capture buffer.

---
v2
- drop the volatile flag

 drivers/media/platform/amphion/venc.c        |  4 ++++
 drivers/media/platform/amphion/vpu.h         |  1 +
 drivers/media/platform/amphion/vpu_defs.h    |  1 +
 drivers/media/platform/amphion/vpu_v4l2.c    | 16 ++++++++++++++++
 drivers/media/platform/amphion/vpu_v4l2.h    |  1 +
 drivers/media/platform/amphion/vpu_windsor.c |  2 ++
 6 files changed, 25 insertions(+)
drivers/media/platform/amphion/venc.c +4 -0
index 4eb57d793a9c..cdfaba9d107b 100644
@@ -680,6 +680,9 @@ static int venc_ctrl_init(struct vpu_inst *inst)
 			       ~(1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME),
 			       V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
 
+	v4l2_ctrl_new_std(&inst->ctrl_handler, NULL,
+			  V4L2_CID_MPEG_VIDEO_AVERAGE_QP, 0, 51, 1, 0);
+
 	if (inst->ctrl_handler.error) {
 		ret = inst->ctrl_handler.error;
 		v4l2_ctrl_handler_free(&inst->ctrl_handler);
@@ -819,6 +822,7 @@ static int venc_get_one_encoded_frame(struct vpu_inst *inst,
 	vbuf->field = inst->cap_format.field;
 	vbuf->flags |= frame->info.pic_type;
 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
+	vpu_set_buffer_average_qp(vbuf, frame->info.average_qp);
 	dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
 	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
 	venc->ready_count++;
drivers/media/platform/amphion/vpu.h +1 -0
index 0246cf0ac3a8..22f0da26ccec 100644
@@ -306,6 +306,7 @@ struct vpu_vb2_buffer {
 	dma_addr_t chroma_v;
 	unsigned int state;
 	u32 tag;
+	u32 average_qp;
 };
 
 void vpu_writel(struct vpu_dev *vpu, u32 reg, u32 val);
drivers/media/platform/amphion/vpu_defs.h +1 -0
index 7320852668d6..428d988cf2f7 100644
@@ -114,6 +114,7 @@ struct vpu_enc_pic_info {
 	u32 wptr;
 	u32 crc;
 	s64 timestamp;
+	u32 average_qp;
 };
 
 struct vpu_dec_codec_info {
drivers/media/platform/amphion/vpu_v4l2.c +16 -0
index c88738e8fff7..83db57bc80b7 100644
@@ -63,6 +63,13 @@ unsigned int vpu_get_buffer_state(struct vb2_v4l2_buffer *vbuf)
 	return vpu_buf->state;
 }
 
+void vpu_set_buffer_average_qp(struct vb2_v4l2_buffer *vbuf, u32 qp)
+{
+	struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf);
+
+	vpu_buf->average_qp = qp;
+}
+
 void vpu_v4l2_set_error(struct vpu_inst *inst)
 {
 	vpu_inst_lock(inst);
@@ -539,6 +546,15 @@ static void vpu_vb2_buf_finish(struct vb2_buffer *vb)
 	struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
 	struct vb2_queue *q = vb->vb2_queue;
 
+	if (V4L2_TYPE_IS_CAPTURE(vb->type)) {
+		struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf);
+		struct v4l2_ctrl *ctrl = v4l2_ctrl_find(&inst->ctrl_handler,
+							V4L2_CID_MPEG_VIDEO_AVERAGE_QP);
+
+		if (ctrl)
+			v4l2_ctrl_s_ctrl(ctrl, vpu_buf->average_qp);
+	}
+
 	if (vbuf->flags & V4L2_BUF_FLAG_LAST)
 		vpu_notify_eos(inst);
 
drivers/media/platform/amphion/vpu_v4l2.h +1 -0
index 60f43056a7a2..56f2939fa84d 100644
@@ -12,6 +12,7 @@ void vpu_inst_lock(struct vpu_inst *inst);
 void vpu_inst_unlock(struct vpu_inst *inst);
 void vpu_set_buffer_state(struct vb2_v4l2_buffer *vbuf, unsigned int state);
 unsigned int vpu_get_buffer_state(struct vb2_v4l2_buffer *vbuf);
+void vpu_set_buffer_average_qp(struct vb2_v4l2_buffer *vbuf, u32 qp);
 
 int vpu_v4l2_open(struct file *file, struct vpu_inst *inst);
 int vpu_v4l2_close(struct file *file);
drivers/media/platform/amphion/vpu_windsor.c +2 -0
index 5f1101d7cf9e..e7d37aa4b826 100644
@@ -499,6 +499,7 @@ struct windsor_pic_info {
 	u32 proc_dacc_rng_wr_cnt;
 	s32 tv_s;
 	u32 tv_ns;
+	u32 average_qp;
 };
 
 u32 vpu_windsor_get_data_size(void)
@@ -734,6 +735,7 @@ static void vpu_windsor_unpack_pic_info(struct vpu_rpc_event *pkt, void *data)
 	info->wptr = get_ptr(windsor->str_buff_wptr);
 	info->crc = windsor->frame_crc;
 	info->timestamp = timespec64_to_ns(&ts);
+	info->average_qp = windsor->average_qp;
 }
 
 static void vpu_windsor_unpack_mem_req(struct vpu_rpc_event *pkt, void *data)
Signature · 1 line
2.43.0-rc1
author maintainer
#2 lore raw

PATCH v2 3/3 media: amphion: Remove lock in s_ctrl callback

In reply to Ming Qian

There is no need to add lock in s_ctrl callback, it has been synchronized by the ctrl_handler's lock, otherwise it may led to deadlock if driver call v4l2_ctrl_s_ctrl().

---
 drivers/media/platform/amphion/vdec.c | 2 --
 drivers/media/platform/amphion/venc.c | 2 --
 2 files changed, 4 deletions(-)
drivers/media/platform/amphion/vdec.c +0 -2
index a57f9f4f3b87..6a38a0fa0e2d 100644
@@ -195,7 +195,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
 	struct vdec_t *vdec = inst->priv;
 	int ret = 0;
 
-	vpu_inst_lock(inst);
 	switch (ctrl->id) {
 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
 		vdec->params.display_delay_enable = ctrl->val;
@@ -207,7 +206,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
 		ret = -EINVAL;
 		break;
 	}
-	vpu_inst_unlock(inst);
 
 	return ret;
 }
drivers/media/platform/amphion/venc.c +0 -2
index cdfaba9d107b..351b4edc8742 100644
@@ -518,7 +518,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
 	struct venc_t *venc = inst->priv;
 	int ret = 0;
 
-	vpu_inst_lock(inst);
 	switch (ctrl->id) {
 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
 		venc->params.profile = ctrl->val;
@@ -579,7 +578,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
 		ret = -EINVAL;
 		break;
 	}
-	vpu_inst_unlock(inst);
 
 	return ret;
 }
Signature · 1 line
2.43.0-rc1
#3 lore raw

Re: [PATCH v2 3/3] media: amphion: Remove lock in s_ctrl callback

Hi Ming Qian,

W dniu 25.04.2024 o 08:50, Ming Qian pisze:

Quoted 15 lines

There is no need to add lock in s_ctrl callback, it has been synchronized by the ctrl_handler's lock, otherwise it may led to deadlock if driver call v4l2_ctrl_s_ctrl().

---
  drivers/media/platform/amphion/vdec.c | 2 --
  drivers/media/platform/amphion/venc.c | 2 --
  2 files changed, 4 deletions(-)
drivers/media/platform/amphion/vdec.c +0 -0
index a57f9f4f3b87..6a38a0fa0e2d 100644

drivers/media/platform/amphion/vdec.c static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl) · line 198

Quoted 4 lines
drivers/media/platform/amphion/vdec.c +0 -1
@@ -195,7 +195,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
  	struct vdec_t *vdec = inst->priv;
  	int ret = 0;
 
-	vpu_inst_lock(inst);

I assume that PATCH v2 2/3 might cause the said deadlock to happen? If so, maybe it would make more sense to make the current patch preceed
PATCH v2 2/3? Otherwise the kernel at PATCH v2 2/3 introduces a potential
deadlock.

Regards,

Andrzej

Quoted 31 lines
drivers/media/platform/amphion/vdec.c +0 -1
  	switch (ctrl->id) {
  	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
  		vdec->params.display_delay_enable = ctrl->val;
@@ -207,7 +206,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
  		ret = -EINVAL;
  		break;
  	}
-	vpu_inst_unlock(inst);
 
  	return ret;
  }
drivers/media/platform/amphion/venc.c +0 -2
index cdfaba9d107b..351b4edc8742 100644
@@ -518,7 +518,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
  	struct venc_t *venc = inst->priv;
  	int ret = 0;
 
-	vpu_inst_lock(inst);
  	switch (ctrl->id) {
  	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  		venc->params.profile = ctrl->val;
@@ -579,7 +578,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
  		ret = -EINVAL;
  		break;
  	}
-	vpu_inst_unlock(inst);
 
  	return ret;
  }
#4 lore raw

Re: [PATCH v2 3/3] media: amphion: Remove lock in s_ctrl callback

Hi Andrzej,

Quoted 27 lines

Hi Ming Qian,

W dniu 25.04.2024 o 08:50, Ming Qian pisze:

Quoted 15 lines

There is no need to add lock in s_ctrl callback, it has been synchronized by the ctrl_handler's lock, otherwise it may led to deadlock if driver call v4l2_ctrl_s_ctrl().

---
  drivers/media/platform/amphion/vdec.c | 2 --
  drivers/media/platform/amphion/venc.c | 2 --
  2 files changed, 4 deletions(-)
drivers/media/platform/amphion/vdec.c +0 -0
index a57f9f4f3b87..6a38a0fa0e2d 100644

drivers/media/platform/amphion/vdec.c static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl) · line 197

Quoted 3 lines
drivers/media/platform/amphion/vdec.c +0 -1
@@ -195,7 +195,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
      struct vdec_t *vdec = inst->priv;
      int ret = 0;
-    vpu_inst_lock(inst);

I assume that PATCH v2 2/3 might cause the said deadlock to happen? If so, maybe it would make more sense to make the current patch preceed
PATCH v2 2/3? Otherwise the kernel at PATCH v2 2/3 introduces a potential
deadlock.

Quoted 4 lines

Regards,

Andrzej

I actually discovered this problem when I was preparing the v2 2/3 patch.

But in the v2 2/3 patch, it tried to add a read-only ctrl, then I just unset the s_ctrl callback for the new added ctrl, the potential deadlock is caused by call the s_ctrl back in a locked environment, so after unset the s_ctrl callback, the 2/3 patch won't trigger the deadlock even if this patch is missing.

In order to avoid encountering similar problems in the future, that driver may set or get some ctrl, I added this patch.

Best regards,
Ming

Quoted 29 lines
Quoted 28 lines
drivers/media/platform/amphion/vdec.c +0 -1
      switch (ctrl->id) {
      case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
          vdec->params.display_delay_enable = ctrl->val;
@@ -207,7 +206,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
          ret = -EINVAL;
          break;
      }
-    vpu_inst_unlock(inst);
      return ret;
  }
drivers/media/platform/amphion/venc.c +0 -2
index cdfaba9d107b..351b4edc8742 100644
@@ -518,7 +518,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
      struct venc_t *venc = inst->priv;
      int ret = 0;
-    vpu_inst_lock(inst);
      switch (ctrl->id) {
      case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
          venc->params.profile = ctrl->val;
@@ -579,7 +578,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
          ret = -EINVAL;
          break;
      }
-    vpu_inst_unlock(inst);
      return ret;
  }
#5 lore raw

Re: [PATCH v2 3/3] media: amphion: Remove lock in s_ctrl callback

Hi Ming,

W dniu 30.04.2024 o 04:32, ming qian pisze:

Quoted 36 lines

Hi Andrzej,

Quoted 27 lines

Hi Ming Qian,

W dniu 25.04.2024 o 08:50, Ming Qian pisze:

Quoted 15 lines

There is no need to add lock in s_ctrl callback, it has been synchronized by the ctrl_handler's lock, otherwise it may led to deadlock if driver call v4l2_ctrl_s_ctrl().

---
  drivers/media/platform/amphion/vdec.c | 2 --
  drivers/media/platform/amphion/venc.c | 2 --

  2 files changed, 4 deletions(-)
drivers/media/platform/amphion/vdec.c +0 -0
index a57f9f4f3b87..6a38a0fa0e2d 100644

drivers/media/platform/amphion/vdec.c static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl) · line 197

Quoted 3 lines
drivers/media/platform/amphion/vdec.c +0 -1
@@ -195,7 +195,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
       struct vdec_t *vdec = inst->priv;
       int ret = 0;
-    vpu_inst_lock(inst);

I assume that PATCH v2 2/3 might cause the said deadlock to happen? If so, maybe it would make more sense to make the current patch preceed PATCH v2 2/3? Otherwise the kernel at PATCH v2 2/3 introduces a potential deadlock.

Quoted 4 lines

Regards,

Andrzej

I actually discovered this problem when I was preparing the v2 2/3 patch.

Quoted 8 lines

But in the v2 2/3 patch, it tried to add a read-only ctrl, then I just unset the s_ctrl callback for the new added ctrl, the potential deadlock is caused by call the s_ctrl back in a locked environment, so after unset the s_ctrl callback, the 2/3 patch won't trigger the deadlock even if this patch is missing.

In order to avoid encountering similar problems in the future, that driver may set or get some ctrl, I added this patch.

Do I understand you correctly that patch 2/3 is written in such a way that it does not introduce a deadlock, and you add patch 3/3 only to prevent future problems? If so, it seems to me that patch 3/3 could/should be separate from this series, as it does not quite match "Add average qp control".

Regards,

Andrzej

Quoted 33 lines

Best regards,
Ming

Quoted 29 lines
Quoted 28 lines
drivers/media/platform/amphion/vdec.c +0 -0
       switch (ctrl->id) {

      case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
          vdec->params.display_delay_enable = ctrl->val;

drivers/media/platform/amphion/vdec.c +0 -1
@@ -207,7 +206,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
           ret = -EINVAL;
           break;
       }
-    vpu_inst_unlock(inst);
       return ret;
   }
drivers/media/platform/amphion/venc.c +0 -1
index cdfaba9d107b..351b4edc8742 100644
@@ -518,7 +518,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
       struct venc_t *venc = inst->priv;
       int ret = 0;
-    vpu_inst_lock(inst);
       switch (ctrl->id) {

      case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
          venc->params.profile = ctrl->val;

drivers/media/platform/amphion/venc.c +0 -1
@@ -579,7 +578,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
           ret = -EINVAL;
           break;
       }
-    vpu_inst_unlock(inst);
       return ret;
   }
#6 lore raw

Re: [PATCH v2 3/3] media: amphion: Remove lock in s_ctrl callback

Hi Andrzej,

Quoted 49 lines

Hi Ming,

W dniu 30.04.2024 o 04:32, ming qian pisze:

Quoted 37 lines

Hi Andrzej,

Quoted 28 lines

Hi Ming Qian,

W dniu 25.04.2024 o 08:50, Ming Qian pisze:

Quoted 15 lines

There is no need to add lock in s_ctrl callback, it has been synchronized by the ctrl_handler's lock, otherwise it may led to deadlock if driver call v4l2_ctrl_s_ctrl().

---
  drivers/media/platform/amphion/vdec.c | 2 --
  drivers/media/platform/amphion/venc.c | 2 --
  2 files changed, 4 deletions(-)
drivers/media/platform/amphion/vdec.c +0 -0
index a57f9f4f3b87..6a38a0fa0e2d 100644

drivers/media/platform/amphion/vdec.c static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl) · line 197

Quoted 3 lines
drivers/media/platform/amphion/vdec.c +0 -1
@@ -195,7 +195,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
      struct vdec_t *vdec = inst->priv;
      int ret = 0;
-    vpu_inst_lock(inst);

I assume that PATCH v2 2/3 might cause the said deadlock to happen? If so, maybe it would make more sense to make the current patch preceed
PATCH v2 2/3? Otherwise the kernel at PATCH v2 2/3 introduces a
potential
deadlock.

Quoted 4 lines

Regards,

Andrzej

I actually discovered this problem when I was preparing the v2 2/3 patch.

Quoted 8 lines

But in the v2 2/3 patch, it tried to add a read-only ctrl, then I just unset the s_ctrl callback for the new added ctrl, the potential deadlock is caused by call the s_ctrl back in a locked environment, so after unset the s_ctrl callback, the 2/3 patch won't trigger the deadlock even if this patch is missing.

In order to avoid encountering similar problems in the future, that driver may set or get some ctrl, I added this patch.

Quoted 9 lines

Do I understand you correctly that patch 2/3 is written in such a way that it does not introduce a deadlock, and you add patch 3/3 only to prevent future problems? If so, it seems to me that patch 3/3 could/should be separate from this series, as it does not quite match "Add average qp control".

Regards,

Andrzej

You're right, when I prepare the 2/3 patch, I add the new ctrl just like the previous ctrl, but I got a deadlock. then I tried to fix a deadlock in 2/3 patch first, and make this patch again to avoid similar errors.

I put this patch to this series, as I think this better explains why I made this change.

Regards,
Ming

Quoted 34 lines
Quoted 33 lines

Best regards,
Ming

Quoted 29 lines
Quoted 28 lines
drivers/media/platform/amphion/vdec.c +0 -1
      switch (ctrl->id) {
      case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
          vdec->params.display_delay_enable = ctrl->val;
@@ -207,7 +206,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
          ret = -EINVAL;
          break;
      }
-    vpu_inst_unlock(inst);
      return ret;
  }
drivers/media/platform/amphion/venc.c +0 -2
index cdfaba9d107b..351b4edc8742 100644
@@ -518,7 +518,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
      struct venc_t *venc = inst->priv;
      int ret = 0;
-    vpu_inst_lock(inst);
      switch (ctrl->id) {
      case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
          venc->params.profile = ctrl->val;
@@ -579,7 +578,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
          ret = -EINVAL;
          break;
      }
-    vpu_inst_unlock(inst);
      return ret;
  }
#7 lore raw
In reply to Ming Qian

Hi Ming Qian,

W dniu 25.04.2024 o 08:50, Ming Qian pisze:

Quoted 20 lines

Add a control V4L2_CID_MPEG_VIDEO_AVERAGE_QP to report the average qp value of current encoded frame. the value applies to the last dequeued capture buffer.

---
v2
- improve document description according Hans's comments
- drop volatile flag

  Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst | 5 +++++
  drivers/media/v4l2-core/v4l2-ctrls-defs.c                 | 5 +++++
  include/uapi/linux/v4l2-controls.h                        | 2 ++
  3 files changed, 12 insertions(+)
Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst +0 -0
index 2a165ae063fb..7d82ab14b8ba 100644

Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst enum v4l2_mpeg_video_h264_hierarchical_coding_type - · line 1660

Quoted 8 lines
Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst +5 -0
@@ -1653,6 +1653,11 @@ enum v4l2_mpeg_video_h264_hierarchical_coding_type -
      Quantization parameter for a P frame for FWHT. Valid range: from 1
      to 31.
 
+``V4L2_CID_MPEG_VIDEO_AVERAGE_QP (integer)``
+    This read-only control returns the average QP value of the currently
+    encoded frame. The value applies to the last dequeued capture buffer
+    (VIDIOC_DQBUF). Applicable to encoders.
+

What is the intended range of the values? And why? Is your intention to make it a hardware-agnostic control or a hardware-specific control?

Regrds,

Andrzej

Quoted 39 lines
Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst +0 -0
  .. raw:: latex
 
      \normalsize
drivers/media/v4l2-core/v4l2-ctrls-defs.c +5 -0
index 8696eb1cdd61..1ea52011247a 100644
@@ -970,6 +970,7 @@ const char *v4l2_ctrl_get_name(u32 id)
  	case V4L2_CID_MPEG_VIDEO_LTR_COUNT:			return "LTR Count";
  	case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:		return "Frame LTR Index";
  	case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:		return "Use LTR Frames";
+	case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:			return "Average QP Value";
  	case V4L2_CID_FWHT_I_FRAME_QP:				return "FWHT I-Frame QP Value";
  	case V4L2_CID_FWHT_P_FRAME_QP:				return "FWHT P-Frame QP Value";
 
@@ -1507,6 +1508,10 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
  		*max = 0xffffffffffffLL;
  		*step = 1;
  		break;
+	case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:
+		*type = V4L2_CTRL_TYPE_INTEGER;
+		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
+		break;
  	case V4L2_CID_PIXEL_RATE:
  		*type = V4L2_CTRL_TYPE_INTEGER64;
  		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
include/uapi/linux/v4l2-controls.h +2 -0
index 99c3f5e99da7..974fd254e573 100644
@@ -898,6 +898,8 @@ enum v4l2_mpeg_video_av1_level {
  	V4L2_MPEG_VIDEO_AV1_LEVEL_7_3 = 23
  };
 
+#define V4L2_CID_MPEG_VIDEO_AVERAGE_QP  (V4L2_CID_CODEC_BASE + 657)
+
  /*  MPEG-class control IDs specific to the CX2341x driver as defined by V4L2 */
  #define V4L2_CID_CODEC_CX2341X_BASE				(V4L2_CTRL_CLASS_CODEC | 0x1000)
  #define V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE		(V4L2_CID_CODEC_CX2341X_BASE+0)
#8 lore raw

Hi Andrzej,

Quoted 35 lines

Hi Ming Qian,

W dniu 25.04.2024 o 08:50, Ming Qian pisze:

Quoted 24 lines

Add a control V4L2_CID_MPEG_VIDEO_AVERAGE_QP to report the average qp value of current encoded frame. the value applies to the last dequeued capture buffer.

---
v2
- improve document description according Hans's comments
- drop volatile flag

  Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst | 5 +++++
  drivers/media/v4l2-core/v4l2-ctrls-defs.c                 | 5 +++++
  include/uapi/linux/v4l2-controls.h                        | 2 ++
  3 files changed, 12 insertions(+)
Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst +0 -0
index 2a165ae063fb..7d82ab14b8ba 100644
@@ -1653,6 +1653,11 @@ enum 

v4l2_mpeg_video_h264_hierarchical_coding_type -
Quantization parameter for a P frame for FWHT. Valid range: from 1 to 31.
+``V4L2_CID_MPEG_VIDEO_AVERAGE_QP (integer)``

Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst enum v4l2_mpeg_video_h264_hierarchical_coding_type - · line 1660

Quoted 4 lines
Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst +4 -0
+    This read-only control returns the average QP value of the currently
+    encoded frame. The value applies to the last dequeued capture buffer
+    (VIDIOC_DQBUF). Applicable to encoders.
+

What is the intended range of the values? And why? Is your intention to make it a hardware-agnostic control or a hardware-specific control?

Quoted 4 lines

Regrds,

Andrzej

The value range depends on the format,
For H264, it's [V4L2_CID_MPEG_VIDEO_H264_MIN_QP, V4L2_CID_MPEG_VIDEO_H264_MIN_QP], usually this is [0, 51].
For HEVC, it's [V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP], usually it's from 0 to 51 for 8 bit and from 0 to 63 for 10 bit.
For H263 and MPEG4, it may be from 1 to 31.
For VPX, it may be from 1 to 128.

I think the driver that supports this ctrl can set an appropriate value range based on the format it supports.

Users also need to read the value of this ctrl according to the format they set.

Best regards,
Ming

Quoted 46 lines
Quoted 45 lines

.. raw:: latex
\normalsize

drivers/media/v4l2-core/v4l2-ctrls-defs.c +1 -0
index 8696eb1cdd61..1ea52011247a 100644
@@ -970,6 +970,7 @@ const char *v4l2_ctrl_get_name(u32 id)
      case V4L2_CID_MPEG_VIDEO_LTR_COUNT:            return "LTR Count";
      case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:        return "Frame 
 LTR Index";
      case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:        return "Use LTR 
 Frames";
+    case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:            return "Average 
 QP Value";

case V4L2_CID_FWHT_I_FRAME_QP: return "FWHT
I-Frame QP Value";
case V4L2_CID_FWHT_P_FRAME_QP: return "FWHT
P-Frame QP Value";

drivers/media/v4l2-core/v4l2-ctrls-defs.c +4 -0
@@ -1507,6 +1508,10 @@ void v4l2_ctrl_fill(u32 id, const char **name, 
 enum v4l2_ctrl_type *type,
          *max = 0xffffffffffffLL;
          *step = 1;
          break;
+    case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:
+        *type = V4L2_CTRL_TYPE_INTEGER;
+        *flags |= V4L2_CTRL_FLAG_READ_ONLY;
+        break;
      case V4L2_CID_PIXEL_RATE:
          *type = V4L2_CTRL_TYPE_INTEGER64;
          *flags |= V4L2_CTRL_FLAG_READ_ONLY;
include/uapi/linux/v4l2-controls.h +2 -0
index 99c3f5e99da7..974fd254e573 100644
@@ -898,6 +898,8 @@ enum v4l2_mpeg_video_av1_level {
      V4L2_MPEG_VIDEO_AV1_LEVEL_7_3 = 23
  };
+#define V4L2_CID_MPEG_VIDEO_AVERAGE_QP  (V4L2_CID_CODEC_BASE + 657)
+
  /*  MPEG-class control IDs specific to the CX2341x driver as defined 

by V4L2 */
#define V4L2_CID_CODEC_CX2341X_BASE
(V4L2_CTRL_CLASS_CODEC | 0x1000)
#define V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE
(V4L2_CID_CODEC_CX2341X_BASE+0)

#9 lore raw

Hi Ming,

W dniu 30.04.2024 o 04:20, ming qian pisze:

Quoted 50 lines

Hi Andrzej,

Quoted 34 lines

Hi Ming Qian,

W dniu 25.04.2024 o 08:50, Ming Qian pisze:

Quoted 20 lines

Add a control V4L2_CID_MPEG_VIDEO_AVERAGE_QP to report the average qp value of current encoded frame. the value applies to the last dequeued capture buffer.

---
v2
  - improve document description according Hans's comments
  - drop volatile flag

  Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst | 5 +++++ drivers/media/v4l2-core/v4l2-ctrls-defs.c                 | 5 +++++ include/uapi/linux/v4l2-controls.h                        | 2 ++

  3 files changed, 12 insertions(+)
Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst +0 -0
index 2a165ae063fb..7d82ab14b8ba 100644
@@ -1653,6 +1653,11 @@ enum v4l2_mpeg_video_h264_hierarchical_coding_type -
Quoted 7 lines

      Quantization parameter for a P frame for FWHT. Valid range: from 1 to 31.
+``V4L2_CID_MPEG_VIDEO_AVERAGE_QP (integer)``
+    This read-only control returns the average QP value of the currently +    encoded frame. The value applies to the last dequeued capture buffer +    (VIDIOC_DQBUF). Applicable to encoders.
+

What is the intended range of the values? And why? Is your intention to make it a hardware-agnostic control or a hardware-specific control?

Quoted 4 lines

Regrds,

Andrzej

The value range depends on the format,
For H264, it's [V4L2_CID_MPEG_VIDEO_H264_MIN_QP, V4L2_CID_MPEG_VIDEO_H264_MIN_QP], usually this is [0, 51].
For HEVC, it's [V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP], usually it's from 0 to 51 for 8 bit and from 0 to 63 for 10 bit.
For H263 and MPEG4, it may be from 1 to 31.
For VPX, it may be from 1 to 128.

Quoted 5 lines

I think the driver that supports this ctrl can set an appropriate value range based on the format it supports.

Users also need to read the value of this ctrl according to the format they set.

What happens if the user does not set the format or until they set it?

This looks like a contextual control, where the context is a cross product between setting or not setting the format and which particular format is actually set.

I don't want to voice strong opinions about whether this should or should not be accepted, but this kind of behavior should definitely be documented so that driver writers know how to implement this control. Right now reading the documentation in the *.rst file as a driver writer I would be puzzled. So, from a driver writer's perspective I'd expect these additions to the doc:

- the range of returned values
- the expected behavior when format has not been set
- the expected behavior when no frames have been processed yet (so there's no QP to be read back from the hardware)

Regards,

Andrzej

Quoted 42 lines

Best regards,
Ming

Quoted 38 lines
Quoted 37 lines

  .. raw:: latex
      \normalsize

drivers/media/v4l2-core/v4l2-ctrls-defs.c +5 -0
index 8696eb1cdd61..1ea52011247a 100644
@@ -970,6 +970,7 @@ const char *v4l2_ctrl_get_name(u32 id)
       case V4L2_CID_MPEG_VIDEO_LTR_COUNT:            return "LTR Count";
       case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:        return "Frame LTR Index";
       case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:        return "Use LTR Frames";
+    case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:            return "Average QP Value";
       case V4L2_CID_FWHT_I_FRAME_QP:                return "FWHT I-Frame QP Value";
       case V4L2_CID_FWHT_P_FRAME_QP:                return "FWHT P-Frame QP Value";
@@ -1507,6 +1508,10 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum 
 v4l2_ctrl_type *type,
           *max = 0xffffffffffffLL;
           *step = 1;
           break;
+    case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:
+        *type = V4L2_CTRL_TYPE_INTEGER;
+        *flags |= V4L2_CTRL_FLAG_READ_ONLY;
+        break;

      case V4L2_CID_PIXEL_RATE:
          *type = V4L2_CTRL_TYPE_INTEGER64;
          *flags |= V4L2_CTRL_FLAG_READ_ONLY;

include/uapi/linux/v4l2-controls.h +0 -0
index 99c3f5e99da7..974fd254e573 100644
@@ -898,6 +898,8 @@ enum v4l2_mpeg_video_av1_level {

      V4L2_MPEG_VIDEO_AV1_LEVEL_7_3 = 23
  };

include/uapi/linux/v4l2-controls.h +2 -0
+#define V4L2_CID_MPEG_VIDEO_AVERAGE_QP  (V4L2_CID_CODEC_BASE + 657)
+

  /*  MPEG-class control IDs specific to the CX2341x driver as defined by V4L2 */ #define V4L2_CID_CODEC_CX2341X_BASE (V4L2_CTRL_CLASS_CODEC | 0x1000) #define V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE (V4L2_CID_CODEC_CX2341X_BASE+0)

#10 lore raw

Hi Andrzej,

Quoted 82 lines

Hi Ming,

W dniu 30.04.2024 o 04:20, ming qian pisze:

Quoted 54 lines

Hi Andrzej,

Quoted 38 lines

Hi Ming Qian,

W dniu 25.04.2024 o 08:50, Ming Qian pisze:

Quoted 29 lines

Add a control V4L2_CID_MPEG_VIDEO_AVERAGE_QP to report the average qp value of current encoded frame. the value applies to the last dequeued capture buffer.

---
v2
- improve document description according Hans's comments
- drop volatile flag

  Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst | 5 +++++
  drivers/media/v4l2-core/v4l2-ctrls-defs.c                 | 5 +++++
  include/uapi/linux/v4l2-controls.h                        | 2 ++
  3 files changed, 12 insertions(+)
Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst +0 -0
index 2a165ae063fb..7d82ab14b8ba 100644
@@ -1653,6 +1653,11 @@ enum 

v4l2_mpeg_video_h264_hierarchical_coding_type -
Quantization parameter for a P frame for FWHT. Valid range:
from 1

      to 31.

+``V4L2_CID_MPEG_VIDEO_AVERAGE_QP (integer)``
+ This read-only control returns the average QP value of the
currently
+ encoded frame. The value applies to the last dequeued capture
buffer

Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst enum v4l2_mpeg_video_h264_hierarchical_coding_type - · line 1660

Quoted 2 lines
Documentation/userspace-api/media/v4l/ext-ctrls-codec.rst +2 -0
+    (VIDIOC_DQBUF). Applicable to encoders.
+

What is the intended range of the values? And why? Is your intention to make it a hardware-agnostic control or a hardware-specific control?

Quoted 4 lines

Regrds,

Andrzej

The value range depends on the format,
For H264, it's [V4L2_CID_MPEG_VIDEO_H264_MIN_QP, V4L2_CID_MPEG_VIDEO_H264_MIN_QP], usually this is [0, 51].
For HEVC, it's [V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP], usually it's from 0 to 51 for 8 bit and from 0 to 63 for 10 bit.
For H263 and MPEG4, it may be from 1 to 31.
For VPX, it may be from 1 to 128.

Quoted 5 lines

I think the driver that supports this ctrl can set an appropriate value range based on the format it supports.

Users also need to read the value of this ctrl according to the format they set.

What happens if the user does not set the format or until they set it?

This looks like a contextual control, where the context is a cross product between setting or not setting the format and which particular format is actually set.

I don't want to voice strong opinions about whether this should or should not be accepted, but this kind of behavior should definitely be documented so that driver writers know how to implement this control. Right now reading the documentation in the *.rst file as a driver writer I would be puzzled. So, from a driver writer's perspective I'd expect these additions to the doc:

- the range of returned values
- the expected behavior when format has not been set
- the expected behavior when no frames have been processed yet (so there's no QP
to be read back from the hardware)

Quoted 4 lines

Regards,

Andrzej

The ctrl value only applies to the last dequeued capture buffer. It requires that the format has been set and the streaming has been started. Before that, the value is meaningless, and the driver can set a default value by itself.

I agree that it's better to add the value range description in the document, but as discussed above, it's not a fixed value, maybe I can add some description according to format.

Best regards,
Ming

Quoted 51 lines
Quoted 50 lines

Best regards,
Ming

Quoted 46 lines
Quoted 45 lines

.. raw:: latex
\normalsize

drivers/media/v4l2-core/v4l2-ctrls-defs.c +1 -0
index 8696eb1cdd61..1ea52011247a 100644
@@ -970,6 +970,7 @@ const char *v4l2_ctrl_get_name(u32 id)
      case V4L2_CID_MPEG_VIDEO_LTR_COUNT:            return "LTR 
 Count";
      case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:        return "Frame 
 LTR Index";
      case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:        return "Use 
 LTR Frames";
+    case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:            return "Average 

QP Value";
case V4L2_CID_FWHT_I_FRAME_QP: return "FWHT
I-Frame QP Value";
case V4L2_CID_FWHT_P_FRAME_QP: return "FWHT
P-Frame QP Value";

drivers/media/v4l2-core/v4l2-ctrls-defs.c +4 -0
@@ -1507,6 +1508,10 @@ void v4l2_ctrl_fill(u32 id, const char 
 **name, enum v4l2_ctrl_type *type,
          *max = 0xffffffffffffLL;
          *step = 1;
          break;
+    case V4L2_CID_MPEG_VIDEO_AVERAGE_QP:
+        *type = V4L2_CTRL_TYPE_INTEGER;
+        *flags |= V4L2_CTRL_FLAG_READ_ONLY;
+        break;
      case V4L2_CID_PIXEL_RATE:
          *type = V4L2_CTRL_TYPE_INTEGER64;
          *flags |= V4L2_CTRL_FLAG_READ_ONLY;
include/uapi/linux/v4l2-controls.h +2 -0
index 99c3f5e99da7..974fd254e573 100644
@@ -898,6 +898,8 @@ enum v4l2_mpeg_video_av1_level {
      V4L2_MPEG_VIDEO_AV1_LEVEL_7_3 = 23
  };
+#define V4L2_CID_MPEG_VIDEO_AVERAGE_QP  (V4L2_CID_CODEC_BASE + 657)
+
  /*  MPEG-class control IDs specific to the CX2341x driver as 

defined by V4L2 */
#define V4L2_CID_CODEC_CX2341X_BASE (V4L2_CTRL_CLASS_CODEC | 0x1000) #define V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE
(V4L2_CID_CODEC_CX2341X_BASE+0)