Skip to contentSkip to conversation

utils: fwht: support resolution changes

PATCH In review Rivka S 2 participants 2 messages Mar 26 – Jul 16 Media (V4L/DVB)

lore
Share & tools
lore

Rivka S

author
#0 lore raw

PATCH 1/1 utils: fwht: support resolution changes

The FWHT decoder currently fails when the resolution changes between frames.

Instead of returning -EINVAL, update the internal state to match the new resolution.

When a resolution change is detected:
- Update visible width and height from the frame header
- Recalculate coded dimensions
- Update stride and reference stride
- Reset GOP-related state

Also recompute dependent values such as dst_chroma_stride, ref_chroma_stride and dst_size after updating the state.

On the userspace side, ensure the destination buffer is large enough for the updated frame size and reallocate it if needed.

This allows decoding streams with dynamic resolution changes.

Tested using a custom FWHT decoding script.

---
 utils/common/codec-v4l2-fwht.c | 29 ++++++++++++++++++++++-------
 utils/qvidcap/capture.cpp      | 15 ++++++++++++---
 2 files changed, 34 insertions(+), 10 deletions(-)
utils/common/codec-v4l2-fwht.c +22 -7
index 0c83678f..f45b06c3 100644
@@ -288,9 +288,9 @@ int v4l2_fwht_decode(struct v4l2_fwht_state *state, u8 *p_in, u8 *p_out)
 	const struct v4l2_fwht_pixfmt_info *info;
 	unsigned int hdr_width_div, hdr_height_div;
 	struct fwht_raw_frame dst_rf;
-	unsigned int dst_chroma_stride = state->stride;
-	unsigned int ref_chroma_stride = state->ref_stride;
-	unsigned int dst_size = state->stride * state->coded_height;
+	unsigned int dst_chroma_stride;
+	unsigned int ref_chroma_stride;
+	unsigned int dst_size;
 	unsigned int ref_size;
 
 	if (!state->info)
@@ -309,10 +309,25 @@ int v4l2_fwht_decode(struct v4l2_fwht_state *state, u8 *p_in, u8 *p_out)
 	    state->header.magic2 != FWHT_MAGIC2)
 		return -EINVAL;
 
-	/* TODO: support resolution changes */
-	if (ntohl(state->header.width)  != state->visible_width ||
-	    ntohl(state->header.height) != state->visible_height)
-		return -EINVAL;
+	if (ntohl(state->header.width) != state->visible_width ||
+	    ntohl(state->header.height) != state->visible_height) {
+		state->visible_width = ntohl(state->header.width);
+		state->visible_height = ntohl(state->header.height);
+		state->coded_width = vic_round_dim(
+			state->visible_width + (info->width_div - 1),
+			info->width_div);
+		state->coded_height = vic_round_dim(
+			state->visible_height + (info->height_div - 1),
+			info->height_div);
+		state->stride = state->coded_width * info->bytesperline_mult;
+		state->ref_stride = state->stride;
+		state->gop_cnt = 0;
+		state->ref_frame_ts = 0;
+	}
+
+	dst_chroma_stride = state->stride;
+	ref_chroma_stride = state->ref_stride;
+	dst_size = state->stride * state->coded_height;
 
 	flags = ntohl(state->header.flags);
 
utils/qvidcap/capture.cpp +12 -3
index 6b41933e..8fff20c8 100644
@@ -1337,9 +1337,18 @@ void CaptureWin::sockReadEvent()
 			offset += n;
 			sz -= n;
 		}
-		if (is_fwht)
-			fwht_decompress(m_ctx, dst, data_size, m_curData[p], m_curSize[p]);
-		else
+		if (is_fwht) {
+			__u32 needed_out = m_v4l_fmt.g_sizeimage(p);
+
+			if (!m_curData[p] || m_curSize[p] < needed_out) {
+				delete[] m_curData[p];
+				m_curData[p] = new __u8[needed_out];
+				m_curSize[p] = needed_out;
+			}
+
+			fwht_decompress(m_ctx, dst, data_size, m_curData[p],
+					m_curSize[p]);
+		} else
 			rle_decompress(dst, size, data_size,
 				       rle_calc_bpl(m_v4l_fmt.g_bytesperline(p), m_v4l_fmt.g_pixelformat()));
 	}
Signature · 1 line
2.43.0
#1 lore raw

Hi Rivka,

Apologies for the late reply!

Quoted 18 lines · Rivka S

The FWHT decoder currently fails when the resolution changes between frames.

Instead of returning -EINVAL, update the internal state to match the new resolution.

When a resolution change is detected:
- Update visible width and height from the frame header
- Recalculate coded dimensions
- Update stride and reference stride
- Reset GOP-related state

Also recompute dependent values such as dst_chroma_stride, ref_chroma_stride and dst_size after updating the state.

On the userspace side, ensure the destination buffer is large enough for the updated frame size and reallocate it if needed.

Quoted 7 lines

This allows decoding streams with dynamic resolution changes.

Tested using a custom FWHT decoding script.

---
 utils/common/codec-v4l2-fwht.c | 29 ++++++++++++++++++++++-------

Note that codec-v4l2-fwht.c is a copy of the linux kernel source drivers/media/test-drivers/vicodec/codec-v4l2-fwht.c. So any changes should go there.

But I don't think the problem is in that code.

How exactly do you test this? Are you streaming over a socket? (qvidcap -p)

Streaming over a socket does not support dynamic resolution changes, that's correct. But that's something that (I think) needs to be fixed in v4l2-ctl and qvidcap.

Regards,

Hans

Quoted 76 lines
 utils/qvidcap/capture.cpp      | 15 ++++++++++++---
 2 files changed, 34 insertions(+), 10 deletions(-)
utils/common/codec-v4l2-fwht.c +22 -7
index 0c83678f..f45b06c3 100644
@@ -288,9 +288,9 @@ int v4l2_fwht_decode(struct v4l2_fwht_state *state, u8 *p_in, u8 *p_out)
 	const struct v4l2_fwht_pixfmt_info *info;
 	unsigned int hdr_width_div, hdr_height_div;
 	struct fwht_raw_frame dst_rf;
-	unsigned int dst_chroma_stride = state->stride;
-	unsigned int ref_chroma_stride = state->ref_stride;
-	unsigned int dst_size = state->stride * state->coded_height;
+	unsigned int dst_chroma_stride;
+	unsigned int ref_chroma_stride;
+	unsigned int dst_size;
 	unsigned int ref_size;
 
 	if (!state->info)
@@ -309,10 +309,25 @@ int v4l2_fwht_decode(struct v4l2_fwht_state *state, u8 *p_in, u8 *p_out)
 	    state->header.magic2 != FWHT_MAGIC2)
 		return -EINVAL;
 
-	/* TODO: support resolution changes */
-	if (ntohl(state->header.width)  != state->visible_width ||
-	    ntohl(state->header.height) != state->visible_height)
-		return -EINVAL;
+	if (ntohl(state->header.width) != state->visible_width ||
+	    ntohl(state->header.height) != state->visible_height) {
+		state->visible_width = ntohl(state->header.width);
+		state->visible_height = ntohl(state->header.height);
+		state->coded_width = vic_round_dim(
+			state->visible_width + (info->width_div - 1),
+			info->width_div);
+		state->coded_height = vic_round_dim(
+			state->visible_height + (info->height_div - 1),
+			info->height_div);
+		state->stride = state->coded_width * info->bytesperline_mult;
+		state->ref_stride = state->stride;
+		state->gop_cnt = 0;
+		state->ref_frame_ts = 0;
+	}
+
+	dst_chroma_stride = state->stride;
+	ref_chroma_stride = state->ref_stride;
+	dst_size = state->stride * state->coded_height;
 
 	flags = ntohl(state->header.flags);
 
utils/qvidcap/capture.cpp +12 -3
index 6b41933e..8fff20c8 100644
@@ -1337,9 +1337,18 @@ void CaptureWin::sockReadEvent()
 			offset += n;
 			sz -= n;
 		}
-		if (is_fwht)
-			fwht_decompress(m_ctx, dst, data_size, m_curData[p], m_curSize[p]);
-		else
+		if (is_fwht) {
+			__u32 needed_out = m_v4l_fmt.g_sizeimage(p);
+
+			if (!m_curData[p] || m_curSize[p] < needed_out) {
+				delete[] m_curData[p];
+				m_curData[p] = new __u8[needed_out];
+				m_curSize[p] = needed_out;
+			}
+
+			fwht_decompress(m_ctx, dst, data_size, m_curData[p],
+					m_curSize[p]);
+		} else
 			rle_decompress(dst, size, data_size,
 				       rle_calc_bpl(m_v4l_fmt.g_bytesperline(p), m_v4l_fmt.g_pixelformat()));
 	}