Skip to content

Commit 1b72999

Browse files
ndufresnemchehab
authored andcommitted
media: v4l2: Reorder field reflist
As per spec, the field reflist requires interleaving top and bottom field in a specific way that does not fit inside the sort operation. The process consist of alternating references parity, starting with a reference of the same parity as the current picture. This processs is done twice, once for short term references and a second time for the long term references. Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com> Reviewed-by: Sebastian Fricke <sebastian.fricke@collabora.com> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl> Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
1 parent 6cafdc8 commit 1b72999

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

drivers/media/v4l2-core/v4l2-h264.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,40 @@ static int v4l2_h264_b1_ref_list_cmp(const void *ptra, const void *ptrb,
250250
return poca < pocb ? -1 : 1;
251251
}
252252

253+
/*
254+
* The references need to be reordered so that references are alternating
255+
* between top and bottom field references starting with the current picture
256+
* parity. This has to be done for short term and long term references
257+
* separately.
258+
*/
259+
static void reorder_field_reflist(const struct v4l2_h264_reflist_builder *b,
260+
struct v4l2_h264_reference *reflist)
261+
{
262+
struct v4l2_h264_reference tmplist[V4L2_H264_REF_LIST_LEN];
263+
u8 lt, i = 0, j = 0, k = 0;
264+
265+
memcpy(tmplist, reflist, sizeof(tmplist[0]) * b->num_valid);
266+
267+
for (lt = 0; lt <= 1; lt++) {
268+
do {
269+
for (; i < b->num_valid && b->refs[tmplist[i].index].longterm == lt; i++) {
270+
if (tmplist[i].fields == b->cur_pic_fields) {
271+
reflist[k++] = tmplist[i++];
272+
break;
273+
}
274+
}
275+
276+
for (; j < b->num_valid && b->refs[tmplist[j].index].longterm == lt; j++) {
277+
if (tmplist[j].fields != b->cur_pic_fields) {
278+
reflist[k++] = tmplist[j++];
279+
break;
280+
}
281+
}
282+
} while ((i < b->num_valid && b->refs[tmplist[i].index].longterm == lt) ||
283+
(j < b->num_valid && b->refs[tmplist[j].index].longterm == lt));
284+
}
285+
}
286+
253287
static char ref_type_to_char(u8 ref_type)
254288
{
255289
switch (ref_type) {
@@ -360,6 +394,9 @@ v4l2_h264_build_p_ref_list(const struct v4l2_h264_reflist_builder *builder,
360394
sort_r(reflist, builder->num_valid, sizeof(*reflist),
361395
v4l2_h264_p_ref_list_cmp, NULL, builder);
362396

397+
if (builder->cur_pic_fields != V4L2_H264_FRAME_REF)
398+
reorder_field_reflist(builder, reflist);
399+
363400
print_ref_list_p(builder, reflist);
364401
}
365402
EXPORT_SYMBOL_GPL(v4l2_h264_build_p_ref_list);
@@ -393,6 +430,11 @@ v4l2_h264_build_b_ref_lists(const struct v4l2_h264_reflist_builder *builder,
393430
sort_r(b1_reflist, builder->num_valid, sizeof(*b1_reflist),
394431
v4l2_h264_b1_ref_list_cmp, NULL, builder);
395432

433+
if (builder->cur_pic_fields != V4L2_H264_FRAME_REF) {
434+
reorder_field_reflist(builder, b0_reflist);
435+
reorder_field_reflist(builder, b1_reflist);
436+
}
437+
396438
if (builder->num_valid > 1 &&
397439
!memcmp(b1_reflist, b0_reflist, builder->num_valid))
398440
swap(b1_reflist[0], b1_reflist[1]);

0 commit comments

Comments
 (0)