diff mbox series

[v1,08/24] media: v4l2: Reorder field reflist

Message ID 20220328195936.82552-9-nicolas.dufresne@collabora.com (mailing list archive)
State New, archived
Headers show
Series [v1,01/24] media: h264: Increase reference lists size to 32 | expand

Commit Message

Nicolas Dufresne March 28, 2022, 7:59 p.m. UTC
As per spec, the field refslist requires interleaving top and bottom
field in a specific way that does not fit inside the sort operation.
Reorder in-place the references so that their parity sart with the same
parity as the current picture and do that for both short and longterm
references separately.

Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
---
 drivers/media/v4l2-core/v4l2-h264.c | 45 +++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Comments

Sebastian Fricke March 29, 2022, 1:54 p.m. UTC | #1
Hey Nicolas,

On 28.03.2022 15:59, Nicolas Dufresne wrote:
>As per spec, the field refslist requires interleaving top and bottom

In other patches you call it always field reflist, so I'd say let's
stick to that:
s/field refslist/field reflist/

>field in a specific way that does not fit inside the sort operation.
>Reorder in-place the references so that their parity sart with the same
>parity as the current picture and do that for both short and longterm
>references separately.

I find that sentence hard to understand, is this maybe better:

"""
Rearrange the references in place so that their parity matches that of
the current image, and do this separately for both short- and long-term
references.
"""

>
>Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
Reviewed-by: Sebastian Fricke <sebastian.fricke@collabora.com>

>---
> drivers/media/v4l2-core/v4l2-h264.c | 45 +++++++++++++++++++++++++++++
> 1 file changed, 45 insertions(+)
>
>diff --git a/drivers/media/v4l2-core/v4l2-h264.c b/drivers/media/v4l2-core/v4l2-h264.c
>index c3fad382882d..2f7ee8d5479f 100644
>--- a/drivers/media/v4l2-core/v4l2-h264.c
>+++ b/drivers/media/v4l2-core/v4l2-h264.c
>@@ -243,6 +243,43 @@ static int v4l2_h264_b1_ref_list_cmp(const void *ptra, const void *ptrb,
> 	return poca < pocb ? -1 : 1;
> }
>
>+/*
>+ * The references need to be reorder so that reference are alternating between

s/to be reorder/to be reordered/
s/ so that reference/, so that references/

>+ * top and bottom ref starting with the current picture parity. This have to be

s/bottom ref/bottom field references/
s/This have to be/This has to be/

>+ * done for short term and long term references separately.
>+ */
>+static void reorder_field_reflist(const struct v4l2_h264_reflist_builder *b,
>+				  struct v4l2_h264_reference *reflist)
>+{
>+	struct v4l2_h264_reference tmplist[V4L2_H264_REF_LIST_LEN];
>+	u8 lt, i = 0, j = 0, k = 0;
>+
>+	memcpy(tmplist, reflist, sizeof(tmplist[0]) * b->num_valid);
>+
>+	for (lt = 0; lt <= 1; lt++) {
>+		do {
>+			for (; i < b->num_valid && b->refs[tmplist[i].index].longterm == lt; i++) {
>+				if (tmplist[i].fields == b->cur_pic_fields) {
>+					reflist[k] = tmplist[i];
>+					k++;
>+					i++;

You can just say: `reflist[k++] = tmplist[i++];`

>+					break;
>+				}
>+			}
>+
>+			for (; j < b->num_valid && b->refs[tmplist[j].index].longterm == lt; j++) {
>+				if (tmplist[j].fields != b->cur_pic_fields) {
>+					reflist[k] = tmplist[j];
>+					k++;
>+					j++;

Same here: `reflist[k++] = tmplist[j++];`

Greetings,
Sebastian

>+					break;
>+				}
>+			}
>+		} while ((i < b->num_valid && b->refs[tmplist[i].index].longterm == lt) ||
>+			 (j < b->num_valid && b->refs[tmplist[j].index].longterm == lt));
>+	}
>+}
>+
> static char ref_type_to_char (u8 ref_type)
> {
> 	switch (ref_type) {
>@@ -345,6 +382,9 @@ v4l2_h264_build_p_ref_list(const struct v4l2_h264_reflist_builder *builder,
> 	sort_r(reflist, builder->num_valid, sizeof(*reflist),
> 	       v4l2_h264_p_ref_list_cmp, NULL, builder);
>
>+	if (builder->cur_pic_fields != V4L2_H264_FRAME_REF)
>+		reorder_field_reflist(builder, reflist);
>+
> 	print_ref_list_p(builder, reflist);
> }
> EXPORT_SYMBOL_GPL(v4l2_h264_build_p_ref_list);
>@@ -378,6 +418,11 @@ v4l2_h264_build_b_ref_lists(const struct v4l2_h264_reflist_builder *builder,
> 	sort_r(b1_reflist, builder->num_valid, sizeof(*b1_reflist),
> 	       v4l2_h264_b1_ref_list_cmp, NULL, builder);
>
>+	if (builder->cur_pic_fields != V4L2_H264_FRAME_REF) {
>+		reorder_field_reflist(builder, b0_reflist);
>+		reorder_field_reflist(builder, b1_reflist);
>+	}
>+
> 	if (builder->num_valid > 1 &&
> 	    !memcmp(b1_reflist, b0_reflist, builder->num_valid))
> 		swap(b1_reflist[0], b1_reflist[1]);
>-- 
>2.34.1
>
diff mbox series

Patch

diff --git a/drivers/media/v4l2-core/v4l2-h264.c b/drivers/media/v4l2-core/v4l2-h264.c
index c3fad382882d..2f7ee8d5479f 100644
--- a/drivers/media/v4l2-core/v4l2-h264.c
+++ b/drivers/media/v4l2-core/v4l2-h264.c
@@ -243,6 +243,43 @@  static int v4l2_h264_b1_ref_list_cmp(const void *ptra, const void *ptrb,
 	return poca < pocb ? -1 : 1;
 }
 
+/*
+ * The references need to be reorder so that reference are alternating between
+ * top and bottom ref starting with the current picture parity. This have to be
+ * done for short term and long term references separately.
+ */
+static void reorder_field_reflist(const struct v4l2_h264_reflist_builder *b,
+				  struct v4l2_h264_reference *reflist)
+{
+	struct v4l2_h264_reference tmplist[V4L2_H264_REF_LIST_LEN];
+	u8 lt, i = 0, j = 0, k = 0;
+
+	memcpy(tmplist, reflist, sizeof(tmplist[0]) * b->num_valid);
+
+	for (lt = 0; lt <= 1; lt++) {
+		do {
+			for (; i < b->num_valid && b->refs[tmplist[i].index].longterm == lt; i++) {
+				if (tmplist[i].fields == b->cur_pic_fields) {
+					reflist[k] = tmplist[i];
+					k++;
+					i++;
+					break;
+				}
+			}
+
+			for (; j < b->num_valid && b->refs[tmplist[j].index].longterm == lt; j++) {
+				if (tmplist[j].fields != b->cur_pic_fields) {
+					reflist[k] = tmplist[j];
+					k++;
+					j++;
+					break;
+				}
+			}
+		} while ((i < b->num_valid && b->refs[tmplist[i].index].longterm == lt) ||
+			 (j < b->num_valid && b->refs[tmplist[j].index].longterm == lt));
+	}
+}
+
 static char ref_type_to_char (u8 ref_type)
 {
 	switch (ref_type) {
@@ -345,6 +382,9 @@  v4l2_h264_build_p_ref_list(const struct v4l2_h264_reflist_builder *builder,
 	sort_r(reflist, builder->num_valid, sizeof(*reflist),
 	       v4l2_h264_p_ref_list_cmp, NULL, builder);
 
+	if (builder->cur_pic_fields != V4L2_H264_FRAME_REF)
+		reorder_field_reflist(builder, reflist);
+
 	print_ref_list_p(builder, reflist);
 }
 EXPORT_SYMBOL_GPL(v4l2_h264_build_p_ref_list);
@@ -378,6 +418,11 @@  v4l2_h264_build_b_ref_lists(const struct v4l2_h264_reflist_builder *builder,
 	sort_r(b1_reflist, builder->num_valid, sizeof(*b1_reflist),
 	       v4l2_h264_b1_ref_list_cmp, NULL, builder);
 
+	if (builder->cur_pic_fields != V4L2_H264_FRAME_REF) {
+		reorder_field_reflist(builder, b0_reflist);
+		reorder_field_reflist(builder, b1_reflist);
+	}
+
 	if (builder->num_valid > 1 &&
 	    !memcmp(b1_reflist, b0_reflist, builder->num_valid))
 		swap(b1_reflist[0], b1_reflist[1]);