1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2019 Collabora, Ltd.
10 #include <linux/module.h>
11 #include <linux/sort.h>
13 #include <media/v4l2-h264.h>
16 * Size of the tempory buffer allocated when printing reference lists. The
17 * output will be truncated if the size is too small.
19 static const int tmp_str_size = 1024;
22 * v4l2_h264_init_reflist_builder() - Initialize a P/B0/B1 reference list
25 * @b: the builder context to initialize
26 * @dec_params: decode parameters control
28 * @dpb: DPB to use when creating the reference list
31 v4l2_h264_init_reflist_builder(struct v4l2_h264_reflist_builder *b,
32 const struct v4l2_ctrl_h264_decode_params *dec_params,
33 const struct v4l2_ctrl_h264_sps *sps,
34 const struct v4l2_h264_dpb_entry dpb[V4L2_H264_NUM_DPB_ENTRIES])
36 int cur_frame_num, max_frame_num;
39 max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
40 cur_frame_num = dec_params->frame_num;
42 memset(b, 0, sizeof(*b));
43 if (!(dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC)) {
44 b->cur_pic_order_count = min(dec_params->bottom_field_order_cnt,
45 dec_params->top_field_order_cnt);
46 b->cur_pic_fields = V4L2_H264_FRAME_REF;
47 } else if (dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_BOTTOM_FIELD) {
48 b->cur_pic_order_count = dec_params->bottom_field_order_cnt;
49 b->cur_pic_fields = V4L2_H264_BOTTOM_FIELD_REF;
51 b->cur_pic_order_count = dec_params->top_field_order_cnt;
52 b->cur_pic_fields = V4L2_H264_TOP_FIELD_REF;
55 for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) {
56 if (!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
59 if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
60 b->refs[i].longterm = true;
63 * Handle frame_num wraparound as described in section
64 * '8.2.4.1 Decoding process for picture numbers' of the spec.
65 * For long term references, frame_num is set to
66 * long_term_frame_idx which requires no wrapping.
68 if (!b->refs[i].longterm && dpb[i].frame_num > cur_frame_num)
69 b->refs[i].frame_num = (int)dpb[i].frame_num -
72 b->refs[i].frame_num = dpb[i].frame_num;
74 b->refs[i].top_field_order_cnt = dpb[i].top_field_order_cnt;
75 b->refs[i].bottom_field_order_cnt = dpb[i].bottom_field_order_cnt;
77 if (b->cur_pic_fields == V4L2_H264_FRAME_REF) {
78 u8 fields = V4L2_H264_FRAME_REF;
80 b->unordered_reflist[b->num_valid].index = i;
81 b->unordered_reflist[b->num_valid].fields = fields;
86 if (dpb[i].fields & V4L2_H264_TOP_FIELD_REF) {
87 u8 fields = V4L2_H264_TOP_FIELD_REF;
89 b->unordered_reflist[b->num_valid].index = i;
90 b->unordered_reflist[b->num_valid].fields = fields;
94 if (dpb[i].fields & V4L2_H264_BOTTOM_FIELD_REF) {
95 u8 fields = V4L2_H264_BOTTOM_FIELD_REF;
97 b->unordered_reflist[b->num_valid].index = i;
98 b->unordered_reflist[b->num_valid].fields = fields;
103 for (i = b->num_valid; i < ARRAY_SIZE(b->unordered_reflist); i++)
104 b->unordered_reflist[i].index = i;
106 EXPORT_SYMBOL_GPL(v4l2_h264_init_reflist_builder);
108 static s32 v4l2_h264_get_poc(const struct v4l2_h264_reflist_builder *b,
109 const struct v4l2_h264_reference *ref)
111 switch (ref->fields) {
112 case V4L2_H264_FRAME_REF:
113 return min(b->refs[ref->index].top_field_order_cnt,
114 b->refs[ref->index].bottom_field_order_cnt);
115 case V4L2_H264_TOP_FIELD_REF:
116 return b->refs[ref->index].top_field_order_cnt;
117 case V4L2_H264_BOTTOM_FIELD_REF:
118 return b->refs[ref->index].bottom_field_order_cnt;
125 static int v4l2_h264_p_ref_list_cmp(const void *ptra, const void *ptrb,
128 const struct v4l2_h264_reflist_builder *builder = data;
131 idxa = ((struct v4l2_h264_reference *)ptra)->index;
132 idxb = ((struct v4l2_h264_reference *)ptrb)->index;
134 if (WARN_ON(idxa >= V4L2_H264_NUM_DPB_ENTRIES ||
135 idxb >= V4L2_H264_NUM_DPB_ENTRIES))
138 if (builder->refs[idxa].longterm != builder->refs[idxb].longterm) {
139 /* Short term pics first. */
140 if (!builder->refs[idxa].longterm)
147 * For frames, short term pics are in descending pic num order and long
148 * term ones in ascending order. For fields, the same direction is used
149 * but with frame_num (wrapped). For frames, the value of pic_num and
150 * frame_num are the same (see formula (8-28) and (8-29)). For this
151 * reason we can use frame_num only and share this function between
152 * frames and fields reflist.
154 if (!builder->refs[idxa].longterm)
155 return builder->refs[idxb].frame_num <
156 builder->refs[idxa].frame_num ?
159 return builder->refs[idxa].frame_num < builder->refs[idxb].frame_num ?
163 static int v4l2_h264_b0_ref_list_cmp(const void *ptra, const void *ptrb,
166 const struct v4l2_h264_reflist_builder *builder = data;
170 idxa = ((struct v4l2_h264_reference *)ptra)->index;
171 idxb = ((struct v4l2_h264_reference *)ptrb)->index;
173 if (WARN_ON(idxa >= V4L2_H264_NUM_DPB_ENTRIES ||
174 idxb >= V4L2_H264_NUM_DPB_ENTRIES))
177 if (builder->refs[idxa].longterm != builder->refs[idxb].longterm) {
178 /* Short term pics first. */
179 if (!builder->refs[idxa].longterm)
185 /* Long term pics in ascending frame num order. */
186 if (builder->refs[idxa].longterm)
187 return builder->refs[idxa].frame_num <
188 builder->refs[idxb].frame_num ?
191 poca = v4l2_h264_get_poc(builder, ptra);
192 pocb = v4l2_h264_get_poc(builder, ptrb);
195 * Short term pics with POC < cur POC first in POC descending order
196 * followed by short term pics with POC > cur POC in POC ascending
199 if ((poca < builder->cur_pic_order_count) !=
200 (pocb < builder->cur_pic_order_count))
201 return poca < pocb ? -1 : 1;
202 else if (poca < builder->cur_pic_order_count)
203 return pocb < poca ? -1 : 1;
205 return poca < pocb ? -1 : 1;
208 static int v4l2_h264_b1_ref_list_cmp(const void *ptra, const void *ptrb,
211 const struct v4l2_h264_reflist_builder *builder = data;
215 idxa = ((struct v4l2_h264_reference *)ptra)->index;
216 idxb = ((struct v4l2_h264_reference *)ptrb)->index;
218 if (WARN_ON(idxa >= V4L2_H264_NUM_DPB_ENTRIES ||
219 idxb >= V4L2_H264_NUM_DPB_ENTRIES))
222 if (builder->refs[idxa].longterm != builder->refs[idxb].longterm) {
223 /* Short term pics first. */
224 if (!builder->refs[idxa].longterm)
230 /* Long term pics in ascending frame num order. */
231 if (builder->refs[idxa].longterm)
232 return builder->refs[idxa].frame_num <
233 builder->refs[idxb].frame_num ?
236 poca = v4l2_h264_get_poc(builder, ptra);
237 pocb = v4l2_h264_get_poc(builder, ptrb);
240 * Short term pics with POC > cur POC first in POC ascending order
241 * followed by short term pics with POC < cur POC in POC descending
244 if ((poca < builder->cur_pic_order_count) !=
245 (pocb < builder->cur_pic_order_count))
246 return pocb < poca ? -1 : 1;
247 else if (poca < builder->cur_pic_order_count)
248 return pocb < poca ? -1 : 1;
250 return poca < pocb ? -1 : 1;
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
259 static void reorder_field_reflist(const struct v4l2_h264_reflist_builder *b,
260 struct v4l2_h264_reference *reflist)
262 struct v4l2_h264_reference tmplist[V4L2_H264_REF_LIST_LEN];
263 u8 lt, i = 0, j = 0, k = 0;
265 memcpy(tmplist, reflist, sizeof(tmplist[0]) * b->num_valid);
267 for (lt = 0; lt <= 1; lt++) {
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++];
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++];
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));
287 static char ref_type_to_char(u8 ref_type)
290 case V4L2_H264_FRAME_REF:
292 case V4L2_H264_TOP_FIELD_REF:
294 case V4L2_H264_BOTTOM_FIELD_REF:
301 static const char *format_ref_list_p(const struct v4l2_h264_reflist_builder *builder,
302 struct v4l2_h264_reference *reflist,
307 *out_str = kmalloc(tmp_str_size, GFP_KERNEL);
309 n += snprintf(*out_str + n, tmp_str_size - n, "|");
311 for (i = 0; i < builder->num_valid; i++) {
312 /* this is pic_num for frame and frame_num (wrapped) for field,
313 * but for frame pic_num is equal to frame_num (wrapped).
315 int frame_num = builder->refs[reflist[i].index].frame_num;
316 bool longterm = builder->refs[reflist[i].index].longterm;
318 n += scnprintf(*out_str + n, tmp_str_size - n, "%i%c%c|",
319 frame_num, longterm ? 'l' : 's',
320 ref_type_to_char(reflist[i].fields));
326 static void print_ref_list_p(const struct v4l2_h264_reflist_builder *builder,
327 struct v4l2_h264_reference *reflist)
331 pr_debug("ref_pic_list_p (cur_poc %u%c) %s\n",
332 builder->cur_pic_order_count,
333 ref_type_to_char(builder->cur_pic_fields),
334 format_ref_list_p(builder, reflist, &buf));
339 static const char *format_ref_list_b(const struct v4l2_h264_reflist_builder *builder,
340 struct v4l2_h264_reference *reflist,
345 *out_str = kmalloc(tmp_str_size, GFP_KERNEL);
347 n += snprintf(*out_str + n, tmp_str_size - n, "|");
349 for (i = 0; i < builder->num_valid; i++) {
350 int frame_num = builder->refs[reflist[i].index].frame_num;
351 u32 poc = v4l2_h264_get_poc(builder, reflist + i);
352 bool longterm = builder->refs[reflist[i].index].longterm;
354 n += scnprintf(*out_str + n, tmp_str_size - n, "%i%c%c|",
355 longterm ? frame_num : poc,
356 longterm ? 'l' : 's',
357 ref_type_to_char(reflist[i].fields));
363 static void print_ref_list_b(const struct v4l2_h264_reflist_builder *builder,
364 struct v4l2_h264_reference *reflist, u8 list_num)
368 pr_debug("ref_pic_list_b%u (cur_poc %u%c) %s",
369 list_num, builder->cur_pic_order_count,
370 ref_type_to_char(builder->cur_pic_fields),
371 format_ref_list_b(builder, reflist, &buf));
377 * v4l2_h264_build_p_ref_list() - Build the P reference list
379 * @builder: reference list builder context
380 * @reflist: 32 sized array used to store the P reference list. Each entry
381 * is a v4l2_h264_reference structure
383 * This functions builds the P reference lists. This procedure is describe in
384 * section '8.2.4 Decoding process for reference picture lists construction'
385 * of the H264 spec. This function can be used by H264 decoder drivers that
386 * need to pass a P reference list to the hardware.
389 v4l2_h264_build_p_ref_list(const struct v4l2_h264_reflist_builder *builder,
390 struct v4l2_h264_reference *reflist)
392 memcpy(reflist, builder->unordered_reflist,
393 sizeof(builder->unordered_reflist[0]) * builder->num_valid);
394 sort_r(reflist, builder->num_valid, sizeof(*reflist),
395 v4l2_h264_p_ref_list_cmp, NULL, builder);
397 if (builder->cur_pic_fields != V4L2_H264_FRAME_REF)
398 reorder_field_reflist(builder, reflist);
400 print_ref_list_p(builder, reflist);
402 EXPORT_SYMBOL_GPL(v4l2_h264_build_p_ref_list);
405 * v4l2_h264_build_b_ref_lists() - Build the B0/B1 reference lists
407 * @builder: reference list builder context
408 * @b0_reflist: 32 sized array used to store the B0 reference list. Each entry
409 * is a v4l2_h264_reference structure
410 * @b1_reflist: 32 sized array used to store the B1 reference list. Each entry
411 * is a v4l2_h264_reference structure
413 * This functions builds the B0/B1 reference lists. This procedure is described
414 * in section '8.2.4 Decoding process for reference picture lists construction'
415 * of the H264 spec. This function can be used by H264 decoder drivers that
416 * need to pass B0/B1 reference lists to the hardware.
419 v4l2_h264_build_b_ref_lists(const struct v4l2_h264_reflist_builder *builder,
420 struct v4l2_h264_reference *b0_reflist,
421 struct v4l2_h264_reference *b1_reflist)
423 memcpy(b0_reflist, builder->unordered_reflist,
424 sizeof(builder->unordered_reflist[0]) * builder->num_valid);
425 sort_r(b0_reflist, builder->num_valid, sizeof(*b0_reflist),
426 v4l2_h264_b0_ref_list_cmp, NULL, builder);
428 memcpy(b1_reflist, builder->unordered_reflist,
429 sizeof(builder->unordered_reflist[0]) * builder->num_valid);
430 sort_r(b1_reflist, builder->num_valid, sizeof(*b1_reflist),
431 v4l2_h264_b1_ref_list_cmp, NULL, builder);
433 if (builder->cur_pic_fields != V4L2_H264_FRAME_REF) {
434 reorder_field_reflist(builder, b0_reflist);
435 reorder_field_reflist(builder, b1_reflist);
438 if (builder->num_valid > 1 &&
439 !memcmp(b1_reflist, b0_reflist, builder->num_valid))
440 swap(b1_reflist[0], b1_reflist[1]);
442 print_ref_list_b(builder, b0_reflist, 0);
443 print_ref_list_b(builder, b1_reflist, 1);
445 EXPORT_SYMBOL_GPL(v4l2_h264_build_b_ref_lists);
447 MODULE_LICENSE("GPL");
448 MODULE_DESCRIPTION("V4L2 H264 Helpers");