Skip to contentSkip to conversation

Trigger dynamic source change for colorspace

PATCH In review [email protected] 2 participants 7 messages Dec 17, 2025 – Dec 24, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover Trigger dynamic source change for colorspace

From: Ming Qian <[email protected]>

If colorspace changes, the client needs to renegotiate the pipeline, otherwise the decoded frame may not be displayed correctly.

A change in colorspace triggers a source change, allowing the client to detect the specific reason and decide whether request buffer reallocation.

Change logs:
v4
- Drop V4L2_EVENT_SRC_CH_COLORSPACE, to avoid backward compatibility issues.
- Add cover-leter

v3
- Improve the description according to comments

v2
- Add V4L2_EVENT_SRC_CH_COLORSPACE for colorspace source change event

Ming Qian (3):
media: docs: dev-decoder: Trigger dynamic source change for colorspace
media: amphion: Clear last_buffer_dequeued flag for DEC_CMD_START
media: amphion: Trigger source change if colorspace changed

 .../userspace-api/media/v4l/dev-decoder.rst   |  5 +-
 drivers/media/platform/amphion/vdec.c         | 62 ++++++++++++-------
 drivers/media/platform/amphion/vpu_v4l2.c     |  1 -
 3 files changed, 45 insertions(+), 23 deletions(-)

base-commit: f7231cff1f3ff8259bef02dc4999bc132abf29cf
prerequisite-patch-id: 0000000000000000000000000000000000000000
prerequisite-patch-id: f33b49b7c9c245b32701d1a6e8250da297ccc39b

Signature · 1 line
2.52.0
#1 lore raw

PATCH v4 1/3 media: docs: dev-decoder: Trigger dynamic source change for colorspace

From: Ming Qian <[email protected]>

If colorspace changes, the client needs to renegotiate the pipeline, otherwise the decoded frame may not be displayed correctly.

So add colorspace as a trigger parameter for dynamic resolution change.

---
 Documentation/userspace-api/media/v4l/dev-decoder.rst | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/userspace-api/media/v4l/dev-decoder.rst +4 -1
index eb662ced0ab4..2beb6ba1b3c2 100644
@@ -933,7 +933,10 @@ reflected by corresponding queries):
 
 * the minimum number of buffers needed for decoding,
 
-* bit-depth of the bitstream has been changed.
+* bit-depth of the bitstream has been changed,
+
+* colorspace of the bitstream has been changed, but it doesn't require
+  buffer reallocation.
 
 Whenever that happens, the decoder must proceed as follows:
 
Signature · 1 line
2.52.0
Reviewed-by on v4 1/3
#2 lore raw

Re: [PATCH v4 1/3] media: docs: dev-decoder: Trigger dynamic source change for colorspace

Le mercredi 17 décembre 2025 à 11:02 +0800, [email protected] a écrit :

Quoted 8 lines

From: Ming Qian <[email protected]>

If colorspace changes, the client needs to renegotiate the pipeline, otherwise the decoded frame may not be displayed correctly.

So add colorspace as a trigger parameter for dynamic resolution change.

Quoted 21 lines

---
 Documentation/userspace-api/media/v4l/dev-decoder.rst | 5 ++++-

 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/userspace-api/media/v4l/dev-decoder.rst +0 -0
index eb662ced0ab4..2beb6ba1b3c2 100644
@@ -933,7 +933,10 @@ reflected by corresponding queries):


 * the minimum number of buffers needed for decoding,

Documentation/userspace-api/media/v4l/dev-decoder.rst +4 -1
-* bit-depth of the bitstream has been changed.
+* bit-depth of the bitstream has been changed,
+
+* colorspace of the bitstream has been changed, but it doesn't require
+  buffer reallocation.


 Whenever that happens, the decoder must proceed as follows:

#3 lore raw

PATCH v4 2/3 media: amphion: Clear last_buffer_dequeued flag for DEC_CMD_START

In reply to [email protected]

From: Ming Qian <[email protected]>

The V4L2_DEC_CMD_START command may be used to handle the dynamic source change, which will triggers an implicit decoder drain.
The last_buffer_dequeued flag is set in the implicit decoder drain, so driver need to clear it to continue the following decoding flow.

---
 drivers/media/platform/amphion/vdec.c | 1 +
 1 file changed, 1 insertion(+)
drivers/media/platform/amphion/vdec.c +1 -0
index c0d2aabb9e0e..f25dbcebdccf 100644
@@ -724,6 +724,7 @@ static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd
 	switch (cmd->cmd) {
 	case V4L2_DEC_CMD_START:
 		vdec_cmd_start(inst);
+		vb2_clear_last_buffer_dequeued(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx));
 		break;
 	case V4L2_DEC_CMD_STOP:
 		vdec_cmd_stop(inst);
Signature · 1 line
2.52.0
Reviewed-by on v4 2/3
#4 lore raw

Re: [PATCH v4 2/3] media: amphion: Clear last_buffer_dequeued flag for DEC_CMD_START

Le mercredi 17 décembre 2025 à 11:02 +0800, [email protected] a écrit :

Quoted 8 lines

From: Ming Qian <[email protected]>

The V4L2_DEC_CMD_START command may be used to handle the dynamic source change, which will triggers an implicit decoder drain.
The last_buffer_dequeued flag is set in the implicit decoder drain, so driver need to clear it to continue the following decoding flow.

Quoted 16 lines

---
 drivers/media/platform/amphion/vdec.c | 1 +

 1 file changed, 1 insertion(+)
drivers/media/platform/amphion/vdec.c +0 -0
index c0d2aabb9e0e..f25dbcebdccf 100644
@@ -724,6 +724,7 @@ static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd
  	switch (cmd->cmd) {

  case V4L2_DEC_CMD_START:
  vdec_cmd_start(inst);

drivers/media/platform/amphion/vdec.c +1 -0
+		vb2_clear_last_buffer_dequeued(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx));

  break;
  case V4L2_DEC_CMD_STOP:
  vdec_cmd_stop(inst);

#5 lore raw

PATCH v4 3/3 media: amphion: Trigger source change if colorspace changed

In reply to [email protected]

From: Ming Qian <[email protected]>

After encountering a colorspace change in the stream, the decoder sends a V4L2_EVENT_SOURCE_CHANGE event with changes set to V4L2_EVENT_SRC_CH_RESOLUTION.

Then the client can detect and handle the colorspace change without any buffer reallocation

---
 drivers/media/platform/amphion/vdec.c     | 61 +++++++++++++++--------
 drivers/media/platform/amphion/vpu_v4l2.c |  1 -
 2 files changed, 40 insertions(+), 22 deletions(-)
drivers/media/platform/amphion/vdec.c +40 -21
index f25dbcebdccf..beeaf75c76b4 100644
@@ -950,7 +950,7 @@ static void vdec_stop_done(struct vpu_inst *inst)
 	vpu_inst_unlock(inst);
 }
 
-static bool vdec_check_source_change(struct vpu_inst *inst)
+static bool vdec_check_source_change(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)
 {
 	struct vdec_t *vdec = inst->priv;
 	const struct vpu_format *sibling;
@@ -962,26 +962,35 @@ static bool vdec_check_source_change(struct vpu_inst *inst)
 		return false;
 
 	sibling = vpu_helper_find_sibling(inst, inst->cap_format.type, inst->cap_format.pixfmt);
-	if (sibling && vdec->codec_info.pixfmt == sibling->pixfmt)
-		vdec->codec_info.pixfmt = inst->cap_format.pixfmt;
+	if (sibling && hdr->pixfmt == sibling->pixfmt)
+		hdr->pixfmt = inst->cap_format.pixfmt;
 
 	if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)))
 		return true;
-	if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
+	if (inst->cap_format.pixfmt != hdr->pixfmt)
 		return true;
-	if (inst->cap_format.width != vdec->codec_info.decoded_width)
+	if (inst->cap_format.width != hdr->decoded_width)
 		return true;
-	if (inst->cap_format.height != vdec->codec_info.decoded_height)
+	if (inst->cap_format.height != hdr->decoded_height)
 		return true;
 	if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap)
 		return true;
-	if (inst->crop.left != vdec->codec_info.offset_x)
+	if (inst->crop.left != hdr->offset_x)
 		return true;
-	if (inst->crop.top != vdec->codec_info.offset_y)
+	if (inst->crop.top != hdr->offset_y)
 		return true;
-	if (inst->crop.width != vdec->codec_info.width)
+	if (inst->crop.width != hdr->width)
 		return true;
-	if (inst->crop.height != vdec->codec_info.height)
+	if (inst->crop.height != hdr->height)
+		return true;
+	if (!hdr->progressive)
+		return true;
+
+	if (vdec->seq_hdr_found &&
+	    (hdr->color_primaries != vdec->codec_info.color_primaries ||
+	     hdr->transfer_chars != vdec->codec_info.transfer_chars ||
+	     hdr->matrix_coeffs != vdec->codec_info.matrix_coeffs ||
+	     hdr->full_range != vdec->codec_info.full_range))
 		return true;
 
 	return false;
@@ -1333,20 +1342,25 @@ static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info
 	struct vdec_t *vdec = inst->priv;
 
 	vpu_inst_lock(inst);
-	memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
 
-	vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
+	vpu_trace(inst->dev,
+		  "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d, colorspace: %d, %d, %d, %d\n",
 		  inst->id,
-		  vdec->codec_info.decoded_width,
-		  vdec->codec_info.decoded_height,
-		  vdec->codec_info.offset_x,
-		  vdec->codec_info.offset_y,
-		  vdec->codec_info.width,
-		  vdec->codec_info.height,
+		  hdr->decoded_width,
+		  hdr->decoded_height,
+		  hdr->offset_x,
+		  hdr->offset_y,
+		  hdr->width,
+		  hdr->height,
 		  hdr->num_ref_frms,
-		  hdr->num_dpb_frms);
+		  hdr->num_dpb_frms,
+		  hdr->color_primaries,
+		  hdr->transfer_chars,
+		  hdr->matrix_coeffs,
+		  hdr->full_range);
 	inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;
-	vdec->is_source_changed = vdec_check_source_change(inst);
+	vdec->is_source_changed = vdec_check_source_change(inst, hdr);
+	memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
 	vdec_init_fmt(inst);
 	vdec_init_crop(inst);
 	vdec_init_mbi(inst);
@@ -1375,7 +1389,12 @@ static void vdec_event_resolution_change(struct vpu_inst *inst)
 {
 	struct vdec_t *vdec = inst->priv;
 
-	vpu_trace(inst->dev, "[%d]\n", inst->id);
+	vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
+		  inst->id,
+		  vdec->params.frame_count,
+		  vdec->decoded_frame_count,
+		  vdec->display_frame_count,
+		  vdec->sequence);
 	vpu_inst_lock(inst);
 	vdec->seq_tag = vdec->codec_info.tag;
 	vdec_clear_fs(&vdec->mbi);
drivers/media/platform/amphion/vpu_v4l2.c +0 -1
index 47dff9a35bb4..121165a7184f 100644
@@ -102,7 +102,6 @@ static int vpu_notify_eos(struct vpu_inst *inst)
 int vpu_notify_source_change(struct vpu_inst *inst)
 {
 	static const struct v4l2_event ev = {
-		.id = 0,
 		.type = V4L2_EVENT_SOURCE_CHANGE,
 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION
 	};
Signature · 1 line
2.52.0
Reviewed-by on v4 3/3
#6 lore raw

Re: [PATCH v4 3/3] media: amphion: Trigger source change if colorspace changed

Hi Ming,

Le mercredi 17 décembre 2025 à 11:02 +0800, [email protected] a écrit :

Quoted 10 lines

From: Ming Qian <[email protected]>

After encountering a colorspace change in the stream, the decoder sends a V4L2_EVENT_SOURCE_CHANGE event with changes set to V4L2_EVENT_SRC_CH_RESOLUTION.

Then the client can detect and handle the colorspace change without any buffer reallocation

Quoted 125 lines

---
 drivers/media/platform/amphion/vdec.c     | 61 +++++++++++++++-------- drivers/media/platform/amphion/vpu_v4l2.c |  1 -

 2 files changed, 40 insertions(+), 22 deletions(-)
drivers/media/platform/amphion/vdec.c +0 -0
index f25dbcebdccf..beeaf75c76b4 100644
@@ -950,7 +950,7 @@ static void vdec_stop_done(struct vpu_inst *inst)
  	vpu_inst_unlock(inst);
  }

drivers/media/platform/amphion/vdec.c +1 -1
-static bool vdec_check_source_change(struct vpu_inst *inst)
+static bool vdec_check_source_change(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)

 {
  struct vdec_t *vdec = inst->priv;
  const struct vpu_format *sibling;

drivers/media/platform/amphion/vdec.c +0 -0
@@ -962,26 +962,35 @@ static bool vdec_check_source_change(struct vpu_inst *inst)
  		return false;


  sibling = vpu_helper_find_sibling(inst, inst->cap_format.type, inst->cap_format.pixfmt);

drivers/media/platform/amphion/vdec.c +2 -2
-	if (sibling && vdec->codec_info.pixfmt == sibling->pixfmt)
-		vdec->codec_info.pixfmt = inst->cap_format.pixfmt;
+	if (sibling && hdr->pixfmt == sibling->pixfmt)
+		hdr->pixfmt = inst->cap_format.pixfmt;


  if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx))) return true;

drivers/media/platform/amphion/vdec.c +1 -1
-	if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
+	if (inst->cap_format.pixfmt != hdr->pixfmt)

  return true;

drivers/media/platform/amphion/vdec.c +1 -1
-	if (inst->cap_format.width != vdec->codec_info.decoded_width)
+	if (inst->cap_format.width != hdr->decoded_width)

  return true;

drivers/media/platform/amphion/vdec.c +1 -1
-	if (inst->cap_format.height != vdec->codec_info.decoded_height)
+	if (inst->cap_format.height != hdr->decoded_height)

  return true;
  if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap) return true;

drivers/media/platform/amphion/vdec.c +1 -1
-	if (inst->crop.left != vdec->codec_info.offset_x)
+	if (inst->crop.left != hdr->offset_x)

  return true;

drivers/media/platform/amphion/vdec.c +1 -1
-	if (inst->crop.top != vdec->codec_info.offset_y)
+	if (inst->crop.top != hdr->offset_y)

  return true;

drivers/media/platform/amphion/vdec.c +1 -1
-	if (inst->crop.width != vdec->codec_info.width)
+	if (inst->crop.width != hdr->width)

  return true;

drivers/media/platform/amphion/vdec.c +10 -1
-	if (inst->crop.height != vdec->codec_info.height)
+	if (inst->crop.height != hdr->height)
+		return true;
+	if (!hdr->progressive)
+		return true;
+
+	if (vdec->seq_hdr_found &&
+	    (hdr->color_primaries != vdec->codec_info.color_primaries ||
+	     hdr->transfer_chars != vdec->codec_info.transfer_chars ||
+	     hdr->matrix_coeffs != vdec->codec_info.matrix_coeffs ||
+	     hdr->full_range != vdec->codec_info.full_range))

  return true;

  return false;

drivers/media/platform/amphion/vdec.c +0 -0
@@ -1333,20 +1342,25 @@ static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info
  	struct vdec_t *vdec = inst->priv;


  vpu_inst_lock(inst);

drivers/media/platform/amphion/vdec.c +0 -1
-	memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));

drivers/media/platform/amphion/vdec.c +2 -1
-	vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
+	vpu_trace(inst->dev,
+		  "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d, colorspace: %d, %d, %d, %d\n",

    inst->id,

drivers/media/platform/amphion/vdec.c +6 -6
-		  vdec->codec_info.decoded_width,
-		  vdec->codec_info.decoded_height,
-		  vdec->codec_info.offset_x,
-		  vdec->codec_info.offset_y,
-		  vdec->codec_info.width,
-		  vdec->codec_info.height,
+		  hdr->decoded_width,
+		  hdr->decoded_height,
+		  hdr->offset_x,
+		  hdr->offset_y,
+		  hdr->width,
+		  hdr->height,

    hdr->num_ref_frms,

drivers/media/platform/amphion/vdec.c +5 -1
-		  hdr->num_dpb_frms);
+		  hdr->num_dpb_frms,
+		  hdr->color_primaries,
+		  hdr->transfer_chars,
+		  hdr->matrix_coeffs,
+		  hdr->full_range);

  inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;

drivers/media/platform/amphion/vdec.c +2 -1
-	vdec->is_source_changed = vdec_check_source_change(inst);
+	vdec->is_source_changed = vdec_check_source_change(inst, hdr);
+	memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));

  vdec_init_fmt(inst);
  vdec_init_crop(inst);
  vdec_init_mbi(inst);

drivers/media/platform/amphion/vdec.c +0 -0
@@ -1375,7 +1389,12 @@ static void vdec_event_resolution_change(struct vpu_inst *inst)
  {
  	struct vdec_t *vdec = inst->priv;

drivers/media/platform/amphion/vdec.c +6 -1
-	vpu_trace(inst->dev, "[%d]\n", inst->id);
+	vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
+		  inst->id,
+		  vdec->params.frame_count,
+		  vdec->decoded_frame_count,
+		  vdec->display_frame_count,
+		  vdec->sequence);

  vpu_inst_lock(inst);
  vdec->seq_tag = vdec->codec_info.tag;
  vdec_clear_fs(&vdec->mbi);

drivers/media/platform/amphion/vpu_v4l2.c +0 -1
index 47dff9a35bb4..121165a7184f 100644
@@ -102,7 +102,6 @@ static int vpu_notify_eos(struct vpu_inst *inst)
  int vpu_notify_source_change(struct vpu_inst *inst)
  {
  	static const struct v4l2_event ev = {
-		.id = 0,
  		.type = V4L2_EVENT_SOURCE_CHANGE,

  .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };