1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019 Pengutronix, Michael Tretter <kernel@pengutronix.de>
4  *
5  * Convert NAL units between raw byte sequence payloads (RBSP) and C structs
6  *
7  * The conversion is defined in "ITU-T Rec. H.264 (04/2017) Advanced video
8  * coding for generic audiovisual services". Decoder drivers may use the
9  * parser to parse RBSP from encoded streams and configure the hardware, if
10  * the hardware is not able to parse RBSP itself.  Encoder drivers may use the
11  * generator to generate the RBSP for SPS/PPS nal units and add them to the
12  * encoded stream if the hardware does not generate the units.
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/string.h>
18 #include <linux/v4l2-controls.h>
19 
20 #include <linux/device.h>
21 #include <linux/export.h>
22 #include <linux/log2.h>
23 
24 #include "nal-h264.h"
25 
26 /*
27  * See Rec. ITU-T H.264 (04/2017) Table 7-1 – NAL unit type codes, syntax
28  * element categories, and NAL unit type classes
29  */
30 enum nal_unit_type {
31 	SEQUENCE_PARAMETER_SET = 7,
32 	PICTURE_PARAMETER_SET = 8,
33 	FILLER_DATA = 12,
34 };
35 
36 struct rbsp;
37 
38 struct nal_h264_ops {
39 	int (*rbsp_bit)(struct rbsp *rbsp, int *val);
40 	int (*rbsp_bits)(struct rbsp *rbsp, int n, unsigned int *val);
41 	int (*rbsp_uev)(struct rbsp *rbsp, unsigned int *val);
42 	int (*rbsp_sev)(struct rbsp *rbsp, int *val);
43 };
44 
45 /**
46  * struct rbsp - State object for handling a raw byte sequence payload
47  * @data: pointer to the data of the rbsp
48  * @size: maximum size of the data of the rbsp
49  * @pos: current bit position inside the rbsp
50  * @num_consecutive_zeros: number of zeros before @pos
51  * @ops: per datatype functions for interacting with the rbsp
52  * @error: an error occurred while handling the rbsp
53  *
54  * This struct is passed around the various parsing functions and tracks the
55  * current position within the raw byte sequence payload.
56  *
57  * The @ops field allows to separate the operation, i.e., reading/writing a
58  * value from/to that rbsp, from the structure of the NAL unit. This allows to
59  * have a single function for iterating the NAL unit, while @ops has function
60  * pointers for handling each type in the rbsp.
61  */
62 struct rbsp {
63 	u8 *data;
64 	size_t size;
65 	unsigned int pos;
66 	unsigned int num_consecutive_zeros;
67 	struct nal_h264_ops *ops;
68 	int error;
69 };
70 
rbsp_init(struct rbsp * rbsp,void * addr,size_t size,struct nal_h264_ops * ops)71 static void rbsp_init(struct rbsp *rbsp, void *addr, size_t size,
72 		      struct nal_h264_ops *ops)
73 {
74 	if (!rbsp)
75 		return;
76 
77 	rbsp->data = addr;
78 	rbsp->size = size;
79 	rbsp->pos = 0;
80 	rbsp->ops = ops;
81 	rbsp->error = 0;
82 }
83 
84 /**
85  * nal_h264_profile_from_v4l2() - Get profile_idc for v4l2 h264 profile
86  * @profile: the profile as &enum v4l2_mpeg_video_h264_profile
87  *
88  * Convert the &enum v4l2_mpeg_video_h264_profile to profile_idc as specified
89  * in Rec. ITU-T H.264 (04/2017) A.2.
90  *
91  * Return: the profile_idc for the passed level
92  */
nal_h264_profile_from_v4l2(enum v4l2_mpeg_video_h264_profile profile)93 int nal_h264_profile_from_v4l2(enum v4l2_mpeg_video_h264_profile profile)
94 {
95 	switch (profile) {
96 	case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
97 		return 66;
98 	case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
99 		return 77;
100 	case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED:
101 		return 88;
102 	case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
103 		return 100;
104 	default:
105 		return -EINVAL;
106 	}
107 }
108 
109 /**
110  * nal_h264_level_from_v4l2() - Get level_idc for v4l2 h264 level
111  * @level: the level as &enum v4l2_mpeg_video_h264_level
112  *
113  * Convert the &enum v4l2_mpeg_video_h264_level to level_idc as specified in
114  * Rec. ITU-T H.264 (04/2017) A.3.2.
115  *
116  * Return: the level_idc for the passed level
117  */
nal_h264_level_from_v4l2(enum v4l2_mpeg_video_h264_level level)118 int nal_h264_level_from_v4l2(enum v4l2_mpeg_video_h264_level level)
119 {
120 	switch (level) {
121 	case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
122 		return 10;
123 	case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
124 		return 9;
125 	case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
126 		return 11;
127 	case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
128 		return 12;
129 	case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
130 		return 13;
131 	case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
132 		return 20;
133 	case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
134 		return 21;
135 	case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
136 		return 22;
137 	case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
138 		return 30;
139 	case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
140 		return 31;
141 	case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
142 		return 32;
143 	case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
144 		return 40;
145 	case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
146 		return 41;
147 	case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
148 		return 42;
149 	case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
150 		return 50;
151 	case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
152 		return 51;
153 	default:
154 		return -EINVAL;
155 	}
156 }
157 
158 static int rbsp_read_bits(struct rbsp *rbsp, int n, unsigned int *value);
159 static int rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int value);
160 
161 /*
162  * When reading or writing, the emulation_prevention_three_byte is detected
163  * only when the 2 one bits need to be inserted. Therefore, we are not
164  * actually adding the 0x3 byte, but the 2 one bits and the six 0 bits of the
165  * next byte.
166  */
167 #define EMULATION_PREVENTION_THREE_BYTE (0x3 << 6)
168 
add_emulation_prevention_three_byte(struct rbsp * rbsp)169 static int add_emulation_prevention_three_byte(struct rbsp *rbsp)
170 {
171 	rbsp->num_consecutive_zeros = 0;
172 	rbsp_write_bits(rbsp, 8, EMULATION_PREVENTION_THREE_BYTE);
173 
174 	return 0;
175 }
176 
discard_emulation_prevention_three_byte(struct rbsp * rbsp)177 static int discard_emulation_prevention_three_byte(struct rbsp *rbsp)
178 {
179 	unsigned int tmp = 0;
180 
181 	rbsp->num_consecutive_zeros = 0;
182 	rbsp_read_bits(rbsp, 8, &tmp);
183 	if (tmp != EMULATION_PREVENTION_THREE_BYTE)
184 		return -EINVAL;
185 
186 	return 0;
187 }
188 
rbsp_read_bit(struct rbsp * rbsp)189 static inline int rbsp_read_bit(struct rbsp *rbsp)
190 {
191 	int shift;
192 	int ofs;
193 	int bit;
194 	int err;
195 
196 	if (rbsp->num_consecutive_zeros == 22) {
197 		err = discard_emulation_prevention_three_byte(rbsp);
198 		if (err)
199 			return err;
200 	}
201 
202 	shift = 7 - (rbsp->pos % 8);
203 	ofs = rbsp->pos / 8;
204 	if (ofs >= rbsp->size)
205 		return -EINVAL;
206 
207 	bit = (rbsp->data[ofs] >> shift) & 1;
208 
209 	rbsp->pos++;
210 
211 	if (bit == 1 ||
212 	    (rbsp->num_consecutive_zeros < 7 && (rbsp->pos % 8 == 0)))
213 		rbsp->num_consecutive_zeros = 0;
214 	else
215 		rbsp->num_consecutive_zeros++;
216 
217 	return bit;
218 }
219 
rbsp_write_bit(struct rbsp * rbsp,bool value)220 static inline int rbsp_write_bit(struct rbsp *rbsp, bool value)
221 {
222 	int shift;
223 	int ofs;
224 
225 	if (rbsp->num_consecutive_zeros == 22)
226 		add_emulation_prevention_three_byte(rbsp);
227 
228 	shift = 7 - (rbsp->pos % 8);
229 	ofs = rbsp->pos / 8;
230 	if (ofs >= rbsp->size)
231 		return -EINVAL;
232 
233 	rbsp->data[ofs] &= ~(1 << shift);
234 	rbsp->data[ofs] |= value << shift;
235 
236 	rbsp->pos++;
237 
238 	if (value ||
239 	    (rbsp->num_consecutive_zeros < 7 && (rbsp->pos % 8 == 0))) {
240 		rbsp->num_consecutive_zeros = 0;
241 	} else {
242 		rbsp->num_consecutive_zeros++;
243 	}
244 
245 	return 0;
246 }
247 
rbsp_read_bits(struct rbsp * rbsp,int n,unsigned int * value)248 static inline int rbsp_read_bits(struct rbsp *rbsp, int n, unsigned int *value)
249 {
250 	int i;
251 	int bit;
252 	unsigned int tmp = 0;
253 
254 	if (n > 8 * sizeof(*value))
255 		return -EINVAL;
256 
257 	for (i = n; i > 0; i--) {
258 		bit = rbsp_read_bit(rbsp);
259 		if (bit < 0)
260 			return bit;
261 		tmp |= bit << (i - 1);
262 	}
263 
264 	if (value)
265 		*value = tmp;
266 
267 	return 0;
268 }
269 
rbsp_write_bits(struct rbsp * rbsp,int n,unsigned int value)270 static int rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int value)
271 {
272 	int ret;
273 
274 	if (n > 8 * sizeof(value))
275 		return -EINVAL;
276 
277 	while (n--) {
278 		ret = rbsp_write_bit(rbsp, (value >> n) & 1);
279 		if (ret)
280 			return ret;
281 	}
282 
283 	return 0;
284 }
285 
rbsp_read_uev(struct rbsp * rbsp,unsigned int * value)286 static int rbsp_read_uev(struct rbsp *rbsp, unsigned int *value)
287 {
288 	int leading_zero_bits = 0;
289 	unsigned int tmp = 0;
290 	int ret;
291 
292 	while ((ret = rbsp_read_bit(rbsp)) == 0)
293 		leading_zero_bits++;
294 	if (ret < 0)
295 		return ret;
296 
297 	if (leading_zero_bits > 0) {
298 		ret = rbsp_read_bits(rbsp, leading_zero_bits, &tmp);
299 		if (ret)
300 			return ret;
301 	}
302 
303 	if (value)
304 		*value = (1 << leading_zero_bits) - 1 + tmp;
305 
306 	return 0;
307 }
308 
rbsp_write_uev(struct rbsp * rbsp,unsigned int * value)309 static int rbsp_write_uev(struct rbsp *rbsp, unsigned int *value)
310 {
311 	int ret;
312 	int leading_zero_bits;
313 
314 	if (!value)
315 		return -EINVAL;
316 
317 	leading_zero_bits = ilog2(*value + 1);
318 
319 	ret = rbsp_write_bits(rbsp, leading_zero_bits, 0);
320 	if (ret)
321 		return ret;
322 
323 	return rbsp_write_bits(rbsp, leading_zero_bits + 1, *value + 1);
324 }
325 
rbsp_read_sev(struct rbsp * rbsp,int * value)326 static int rbsp_read_sev(struct rbsp *rbsp, int *value)
327 {
328 	int ret;
329 	unsigned int tmp;
330 
331 	ret = rbsp_read_uev(rbsp, &tmp);
332 	if (ret)
333 		return ret;
334 
335 	if (value) {
336 		if (tmp & 1)
337 			*value = (tmp + 1) / 2;
338 		else
339 			*value = -(tmp / 2);
340 	}
341 
342 	return 0;
343 }
344 
rbsp_write_sev(struct rbsp * rbsp,int * value)345 static int rbsp_write_sev(struct rbsp *rbsp, int *value)
346 {
347 	unsigned int tmp;
348 
349 	if (!value)
350 		return -EINVAL;
351 
352 	if (*value > 0)
353 		tmp = (2 * (*value)) | 1;
354 	else
355 		tmp = -2 * (*value);
356 
357 	return rbsp_write_uev(rbsp, &tmp);
358 }
359 
__rbsp_write_bit(struct rbsp * rbsp,int * value)360 static int __rbsp_write_bit(struct rbsp *rbsp, int *value)
361 {
362 	return rbsp_write_bit(rbsp, *value);
363 }
364 
__rbsp_write_bits(struct rbsp * rbsp,int n,unsigned int * value)365 static int __rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int *value)
366 {
367 	return rbsp_write_bits(rbsp, n, *value);
368 }
369 
370 static struct nal_h264_ops write = {
371 	.rbsp_bit = __rbsp_write_bit,
372 	.rbsp_bits = __rbsp_write_bits,
373 	.rbsp_uev = rbsp_write_uev,
374 	.rbsp_sev = rbsp_write_sev,
375 };
376 
__rbsp_read_bit(struct rbsp * rbsp,int * value)377 static int __rbsp_read_bit(struct rbsp *rbsp, int *value)
378 {
379 	int tmp = rbsp_read_bit(rbsp);
380 
381 	if (tmp < 0)
382 		return tmp;
383 	*value = tmp;
384 
385 	return 0;
386 }
387 
388 static struct nal_h264_ops read = {
389 	.rbsp_bit = __rbsp_read_bit,
390 	.rbsp_bits = rbsp_read_bits,
391 	.rbsp_uev = rbsp_read_uev,
392 	.rbsp_sev = rbsp_read_sev,
393 };
394 
rbsp_bit(struct rbsp * rbsp,int * value)395 static inline void rbsp_bit(struct rbsp *rbsp, int *value)
396 {
397 	if (rbsp->error)
398 		return;
399 	rbsp->error = rbsp->ops->rbsp_bit(rbsp, value);
400 }
401 
rbsp_bits(struct rbsp * rbsp,int n,int * value)402 static inline void rbsp_bits(struct rbsp *rbsp, int n, int *value)
403 {
404 	if (rbsp->error)
405 		return;
406 	rbsp->error = rbsp->ops->rbsp_bits(rbsp, n, value);
407 }
408 
rbsp_uev(struct rbsp * rbsp,unsigned int * value)409 static inline void rbsp_uev(struct rbsp *rbsp, unsigned int *value)
410 {
411 	if (rbsp->error)
412 		return;
413 	rbsp->error = rbsp->ops->rbsp_uev(rbsp, value);
414 }
415 
rbsp_sev(struct rbsp * rbsp,int * value)416 static inline void rbsp_sev(struct rbsp *rbsp, int *value)
417 {
418 	if (rbsp->error)
419 		return;
420 	rbsp->error = rbsp->ops->rbsp_sev(rbsp, value);
421 }
422 
nal_h264_rbsp_trailing_bits(struct rbsp * rbsp)423 static void nal_h264_rbsp_trailing_bits(struct rbsp *rbsp)
424 {
425 	unsigned int rbsp_stop_one_bit = 1;
426 	unsigned int rbsp_alignment_zero_bit = 0;
427 
428 	rbsp_bit(rbsp, &rbsp_stop_one_bit);
429 	rbsp_bits(rbsp, round_up(rbsp->pos, 8) - rbsp->pos,
430 		  &rbsp_alignment_zero_bit);
431 }
432 
nal_h264_write_start_code_prefix(struct rbsp * rbsp)433 static void nal_h264_write_start_code_prefix(struct rbsp *rbsp)
434 {
435 	u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
436 	int i = 4;
437 
438 	if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
439 		rbsp->error = -EINVAL;
440 		return;
441 	}
442 
443 	p[0] = 0x00;
444 	p[1] = 0x00;
445 	p[2] = 0x00;
446 	p[3] = 0x01;
447 
448 	rbsp->pos += i * 8;
449 }
450 
nal_h264_read_start_code_prefix(struct rbsp * rbsp)451 static void nal_h264_read_start_code_prefix(struct rbsp *rbsp)
452 {
453 	u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
454 	int i = 4;
455 
456 	if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
457 		rbsp->error = -EINVAL;
458 		return;
459 	}
460 
461 	if (p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x00 || p[3] != 0x01) {
462 		rbsp->error = -EINVAL;
463 		return;
464 	}
465 
466 	rbsp->pos += i * 8;
467 }
468 
nal_h264_write_filler_data(struct rbsp * rbsp)469 static void nal_h264_write_filler_data(struct rbsp *rbsp)
470 {
471 	u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
472 	int i;
473 
474 	/* Keep 1 byte extra for terminating the NAL unit */
475 	i = rbsp->size - DIV_ROUND_UP(rbsp->pos, 8) - 1;
476 	memset(p, 0xff, i);
477 	rbsp->pos += i * 8;
478 }
479 
nal_h264_read_filler_data(struct rbsp * rbsp)480 static void nal_h264_read_filler_data(struct rbsp *rbsp)
481 {
482 	u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
483 
484 	while (*p == 0xff) {
485 		if (DIV_ROUND_UP(rbsp->pos, 8) > rbsp->size) {
486 			rbsp->error = -EINVAL;
487 			return;
488 		}
489 
490 		p++;
491 		rbsp->pos += 8;
492 	}
493 }
494 
nal_h264_rbsp_hrd_parameters(struct rbsp * rbsp,struct nal_h264_hrd_parameters * hrd)495 static void nal_h264_rbsp_hrd_parameters(struct rbsp *rbsp,
496 					 struct nal_h264_hrd_parameters *hrd)
497 {
498 	unsigned int i;
499 
500 	if (!hrd) {
501 		rbsp->error = -EINVAL;
502 		return;
503 	}
504 
505 	rbsp_uev(rbsp, &hrd->cpb_cnt_minus1);
506 	rbsp_bits(rbsp, 4, &hrd->bit_rate_scale);
507 	rbsp_bits(rbsp, 4, &hrd->cpb_size_scale);
508 
509 	for (i = 0; i <= hrd->cpb_cnt_minus1; i++) {
510 		rbsp_uev(rbsp, &hrd->bit_rate_value_minus1[i]);
511 		rbsp_uev(rbsp, &hrd->cpb_size_value_minus1[i]);
512 		rbsp_bit(rbsp, &hrd->cbr_flag[i]);
513 	}
514 
515 	rbsp_bits(rbsp, 5, &hrd->initial_cpb_removal_delay_length_minus1);
516 	rbsp_bits(rbsp, 5, &hrd->cpb_removal_delay_length_minus1);
517 	rbsp_bits(rbsp, 5, &hrd->dpb_output_delay_length_minus1);
518 	rbsp_bits(rbsp, 5, &hrd->time_offset_length);
519 }
520 
nal_h264_rbsp_vui_parameters(struct rbsp * rbsp,struct nal_h264_vui_parameters * vui)521 static void nal_h264_rbsp_vui_parameters(struct rbsp *rbsp,
522 					 struct nal_h264_vui_parameters *vui)
523 {
524 	if (!vui) {
525 		rbsp->error = -EINVAL;
526 		return;
527 	}
528 
529 	rbsp_bit(rbsp, &vui->aspect_ratio_info_present_flag);
530 	if (vui->aspect_ratio_info_present_flag) {
531 		rbsp_bits(rbsp, 8, &vui->aspect_ratio_idc);
532 		if (vui->aspect_ratio_idc == 255) {
533 			rbsp_bits(rbsp, 16, &vui->sar_width);
534 			rbsp_bits(rbsp, 16, &vui->sar_height);
535 		}
536 	}
537 
538 	rbsp_bit(rbsp, &vui->overscan_info_present_flag);
539 	if (vui->overscan_info_present_flag)
540 		rbsp_bit(rbsp, &vui->overscan_appropriate_flag);
541 
542 	rbsp_bit(rbsp, &vui->video_signal_type_present_flag);
543 	if (vui->video_signal_type_present_flag) {
544 		rbsp_bits(rbsp, 3, &vui->video_format);
545 		rbsp_bit(rbsp, &vui->video_full_range_flag);
546 
547 		rbsp_bit(rbsp, &vui->colour_description_present_flag);
548 		if (vui->colour_description_present_flag) {
549 			rbsp_bits(rbsp, 8, &vui->colour_primaries);
550 			rbsp_bits(rbsp, 8, &vui->transfer_characteristics);
551 			rbsp_bits(rbsp, 8, &vui->matrix_coefficients);
552 		}
553 	}
554 
555 	rbsp_bit(rbsp, &vui->chroma_loc_info_present_flag);
556 	if (vui->chroma_loc_info_present_flag) {
557 		rbsp_uev(rbsp, &vui->chroma_sample_loc_type_top_field);
558 		rbsp_uev(rbsp, &vui->chroma_sample_loc_type_bottom_field);
559 	}
560 
561 	rbsp_bit(rbsp, &vui->timing_info_present_flag);
562 	if (vui->timing_info_present_flag) {
563 		rbsp_bits(rbsp, 32, &vui->num_units_in_tick);
564 		rbsp_bits(rbsp, 32, &vui->time_scale);
565 		rbsp_bit(rbsp, &vui->fixed_frame_rate_flag);
566 	}
567 
568 	rbsp_bit(rbsp, &vui->nal_hrd_parameters_present_flag);
569 	if (vui->nal_hrd_parameters_present_flag)
570 		nal_h264_rbsp_hrd_parameters(rbsp, &vui->nal_hrd_parameters);
571 
572 	rbsp_bit(rbsp, &vui->vcl_hrd_parameters_present_flag);
573 	if (vui->vcl_hrd_parameters_present_flag)
574 		nal_h264_rbsp_hrd_parameters(rbsp, &vui->vcl_hrd_parameters);
575 
576 	if (vui->nal_hrd_parameters_present_flag ||
577 	    vui->vcl_hrd_parameters_present_flag)
578 		rbsp_bit(rbsp, &vui->low_delay_hrd_flag);
579 
580 	rbsp_bit(rbsp, &vui->pic_struct_present_flag);
581 
582 	rbsp_bit(rbsp, &vui->bitstream_restriction_flag);
583 	if (vui->bitstream_restriction_flag) {
584 		rbsp_bit(rbsp, &vui->motion_vectors_over_pic_boundaries_flag);
585 		rbsp_uev(rbsp, &vui->max_bytes_per_pic_denom);
586 		rbsp_uev(rbsp, &vui->max_bits_per_mb_denom);
587 		rbsp_uev(rbsp, &vui->log2_max_mv_length_horizontal);
588 		rbsp_uev(rbsp, &vui->log21_max_mv_length_vertical);
589 		rbsp_uev(rbsp, &vui->max_num_reorder_frames);
590 		rbsp_uev(rbsp, &vui->max_dec_frame_buffering);
591 	}
592 }
593 
nal_h264_rbsp_sps(struct rbsp * rbsp,struct nal_h264_sps * sps)594 static void nal_h264_rbsp_sps(struct rbsp *rbsp, struct nal_h264_sps *sps)
595 {
596 	unsigned int i;
597 
598 	if (!sps) {
599 		rbsp->error = -EINVAL;
600 		return;
601 	}
602 
603 	rbsp_bits(rbsp, 8, &sps->profile_idc);
604 	rbsp_bit(rbsp, &sps->constraint_set0_flag);
605 	rbsp_bit(rbsp, &sps->constraint_set1_flag);
606 	rbsp_bit(rbsp, &sps->constraint_set2_flag);
607 	rbsp_bit(rbsp, &sps->constraint_set3_flag);
608 	rbsp_bit(rbsp, &sps->constraint_set4_flag);
609 	rbsp_bit(rbsp, &sps->constraint_set5_flag);
610 	rbsp_bits(rbsp, 2, &sps->reserved_zero_2bits);
611 	rbsp_bits(rbsp, 8, &sps->level_idc);
612 
613 	rbsp_uev(rbsp, &sps->seq_parameter_set_id);
614 
615 	if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
616 	    sps->profile_idc == 122 || sps->profile_idc == 244 ||
617 	    sps->profile_idc == 44 || sps->profile_idc == 83 ||
618 	    sps->profile_idc == 86 || sps->profile_idc == 118 ||
619 	    sps->profile_idc == 128 || sps->profile_idc == 138 ||
620 	    sps->profile_idc == 139 || sps->profile_idc == 134 ||
621 	    sps->profile_idc == 135) {
622 		rbsp_uev(rbsp, &sps->chroma_format_idc);
623 
624 		if (sps->chroma_format_idc == 3)
625 			rbsp_bit(rbsp, &sps->separate_colour_plane_flag);
626 		rbsp_uev(rbsp, &sps->bit_depth_luma_minus8);
627 		rbsp_uev(rbsp, &sps->bit_depth_chroma_minus8);
628 		rbsp_bit(rbsp, &sps->qpprime_y_zero_transform_bypass_flag);
629 		rbsp_bit(rbsp, &sps->seq_scaling_matrix_present_flag);
630 		if (sps->seq_scaling_matrix_present_flag)
631 			rbsp->error = -EINVAL;
632 	}
633 
634 	rbsp_uev(rbsp, &sps->log2_max_frame_num_minus4);
635 
636 	rbsp_uev(rbsp, &sps->pic_order_cnt_type);
637 	switch (sps->pic_order_cnt_type) {
638 	case 0:
639 		rbsp_uev(rbsp, &sps->log2_max_pic_order_cnt_lsb_minus4);
640 		break;
641 	case 1:
642 		rbsp_bit(rbsp, &sps->delta_pic_order_always_zero_flag);
643 		rbsp_sev(rbsp, &sps->offset_for_non_ref_pic);
644 		rbsp_sev(rbsp, &sps->offset_for_top_to_bottom_field);
645 
646 		rbsp_uev(rbsp, &sps->num_ref_frames_in_pic_order_cnt_cycle);
647 		for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
648 			rbsp_sev(rbsp, &sps->offset_for_ref_frame[i]);
649 		break;
650 	default:
651 		rbsp->error = -EINVAL;
652 		break;
653 	}
654 
655 	rbsp_uev(rbsp, &sps->max_num_ref_frames);
656 	rbsp_bit(rbsp, &sps->gaps_in_frame_num_value_allowed_flag);
657 	rbsp_uev(rbsp, &sps->pic_width_in_mbs_minus1);
658 	rbsp_uev(rbsp, &sps->pic_height_in_map_units_minus1);
659 
660 	rbsp_bit(rbsp, &sps->frame_mbs_only_flag);
661 	if (!sps->frame_mbs_only_flag)
662 		rbsp_bit(rbsp, &sps->mb_adaptive_frame_field_flag);
663 
664 	rbsp_bit(rbsp, &sps->direct_8x8_inference_flag);
665 
666 	rbsp_bit(rbsp, &sps->frame_cropping_flag);
667 	if (sps->frame_cropping_flag) {
668 		rbsp_uev(rbsp, &sps->crop_left);
669 		rbsp_uev(rbsp, &sps->crop_right);
670 		rbsp_uev(rbsp, &sps->crop_top);
671 		rbsp_uev(rbsp, &sps->crop_bottom);
672 	}
673 
674 	rbsp_bit(rbsp, &sps->vui_parameters_present_flag);
675 	if (sps->vui_parameters_present_flag)
676 		nal_h264_rbsp_vui_parameters(rbsp, &sps->vui);
677 }
678 
nal_h264_rbsp_pps(struct rbsp * rbsp,struct nal_h264_pps * pps)679 static void nal_h264_rbsp_pps(struct rbsp *rbsp, struct nal_h264_pps *pps)
680 {
681 	int i;
682 
683 	rbsp_uev(rbsp, &pps->pic_parameter_set_id);
684 	rbsp_uev(rbsp, &pps->seq_parameter_set_id);
685 	rbsp_bit(rbsp, &pps->entropy_coding_mode_flag);
686 	rbsp_bit(rbsp, &pps->bottom_field_pic_order_in_frame_present_flag);
687 	rbsp_uev(rbsp, &pps->num_slice_groups_minus1);
688 	if (pps->num_slice_groups_minus1 > 0) {
689 		rbsp_uev(rbsp, &pps->slice_group_map_type);
690 		switch (pps->slice_group_map_type) {
691 		case 0:
692 			for (i = 0; i < pps->num_slice_groups_minus1; i++)
693 				rbsp_uev(rbsp, &pps->run_length_minus1[i]);
694 			break;
695 		case 2:
696 			for (i = 0; i < pps->num_slice_groups_minus1; i++) {
697 				rbsp_uev(rbsp, &pps->top_left[i]);
698 				rbsp_uev(rbsp, &pps->bottom_right[i]);
699 			}
700 			break;
701 		case 3: case 4: case 5:
702 			rbsp_bit(rbsp, &pps->slice_group_change_direction_flag);
703 			rbsp_uev(rbsp, &pps->slice_group_change_rate_minus1);
704 			break;
705 		case 6:
706 			rbsp_uev(rbsp, &pps->pic_size_in_map_units_minus1);
707 			for (i = 0; i < pps->pic_size_in_map_units_minus1; i++)
708 				rbsp_bits(rbsp,
709 					  order_base_2(pps->num_slice_groups_minus1 + 1),
710 					  &pps->slice_group_id[i]);
711 			break;
712 		default:
713 			break;
714 		}
715 	}
716 	rbsp_uev(rbsp, &pps->num_ref_idx_l0_default_active_minus1);
717 	rbsp_uev(rbsp, &pps->num_ref_idx_l1_default_active_minus1);
718 	rbsp_bit(rbsp, &pps->weighted_pred_flag);
719 	rbsp_bits(rbsp, 2, &pps->weighted_bipred_idc);
720 	rbsp_sev(rbsp, &pps->pic_init_qp_minus26);
721 	rbsp_sev(rbsp, &pps->pic_init_qs_minus26);
722 	rbsp_sev(rbsp, &pps->chroma_qp_index_offset);
723 	rbsp_bit(rbsp, &pps->deblocking_filter_control_present_flag);
724 	rbsp_bit(rbsp, &pps->constrained_intra_pred_flag);
725 	rbsp_bit(rbsp, &pps->redundant_pic_cnt_present_flag);
726 	if (/* more_rbsp_data() */ false) {
727 		rbsp_bit(rbsp, &pps->transform_8x8_mode_flag);
728 		rbsp_bit(rbsp, &pps->pic_scaling_matrix_present_flag);
729 		if (pps->pic_scaling_matrix_present_flag)
730 			rbsp->error = -EINVAL;
731 		rbsp_sev(rbsp, &pps->second_chroma_qp_index_offset);
732 	}
733 }
734 
735 /**
736  * nal_h264_write_sps() - Write SPS NAL unit into RBSP format
737  * @dev: device pointer
738  * @dest: the buffer that is filled with RBSP data
739  * @n: maximum size of @dest in bytes
740  * @sps: &struct nal_h264_sps to convert to RBSP
741  *
742  * Convert @sps to RBSP data and write it into @dest.
743  *
744  * The size of the SPS NAL unit is not known in advance and this function will
745  * fail, if @dest does not hold sufficient space for the SPS NAL unit.
746  *
747  * Return: number of bytes written to @dest or negative error code
748  */
nal_h264_write_sps(const struct device * dev,void * dest,size_t n,struct nal_h264_sps * sps)749 ssize_t nal_h264_write_sps(const struct device *dev,
750 			   void *dest, size_t n, struct nal_h264_sps *sps)
751 {
752 	struct rbsp rbsp;
753 	unsigned int forbidden_zero_bit = 0;
754 	unsigned int nal_ref_idc = 0;
755 	unsigned int nal_unit_type = SEQUENCE_PARAMETER_SET;
756 
757 	if (!dest)
758 		return -EINVAL;
759 
760 	rbsp_init(&rbsp, dest, n, &write);
761 
762 	nal_h264_write_start_code_prefix(&rbsp);
763 
764 	rbsp_bit(&rbsp, &forbidden_zero_bit);
765 	rbsp_bits(&rbsp, 2, &nal_ref_idc);
766 	rbsp_bits(&rbsp, 5, &nal_unit_type);
767 
768 	nal_h264_rbsp_sps(&rbsp, sps);
769 
770 	nal_h264_rbsp_trailing_bits(&rbsp);
771 
772 	if (rbsp.error)
773 		return rbsp.error;
774 
775 	return DIV_ROUND_UP(rbsp.pos, 8);
776 }
777 EXPORT_SYMBOL_GPL(nal_h264_write_sps);
778 
779 /**
780  * nal_h264_read_sps() - Read SPS NAL unit from RBSP format
781  * @dev: device pointer
782  * @sps: the &struct nal_h264_sps to fill from the RBSP data
783  * @src: the buffer that contains the RBSP data
784  * @n: size of @src in bytes
785  *
786  * Read RBSP data from @src and use it to fill @sps.
787  *
788  * Return: number of bytes read from @src or negative error code
789  */
nal_h264_read_sps(const struct device * dev,struct nal_h264_sps * sps,void * src,size_t n)790 ssize_t nal_h264_read_sps(const struct device *dev,
791 			  struct nal_h264_sps *sps, void *src, size_t n)
792 {
793 	struct rbsp rbsp;
794 	unsigned int forbidden_zero_bit;
795 	unsigned int nal_ref_idc;
796 	unsigned int nal_unit_type;
797 
798 	if (!src)
799 		return -EINVAL;
800 
801 	rbsp_init(&rbsp, src, n, &read);
802 
803 	nal_h264_read_start_code_prefix(&rbsp);
804 
805 	rbsp_bit(&rbsp, &forbidden_zero_bit);
806 	rbsp_bits(&rbsp, 2, &nal_ref_idc);
807 	rbsp_bits(&rbsp, 5, &nal_unit_type);
808 
809 	if (rbsp.error ||
810 	    forbidden_zero_bit != 0 ||
811 	    nal_ref_idc != 0 ||
812 	    nal_unit_type != SEQUENCE_PARAMETER_SET)
813 		return -EINVAL;
814 
815 	nal_h264_rbsp_sps(&rbsp, sps);
816 
817 	nal_h264_rbsp_trailing_bits(&rbsp);
818 
819 	if (rbsp.error)
820 		return rbsp.error;
821 
822 	return DIV_ROUND_UP(rbsp.pos, 8);
823 }
824 EXPORT_SYMBOL_GPL(nal_h264_read_sps);
825 
826 /**
827  * nal_h264_write_pps() - Write PPS NAL unit into RBSP format
828  * @dev: device pointer
829  * @dest: the buffer that is filled with RBSP data
830  * @n: maximum size of @dest in bytes
831  * @pps: &struct nal_h264_pps to convert to RBSP
832  *
833  * Convert @pps to RBSP data and write it into @dest.
834  *
835  * The size of the PPS NAL unit is not known in advance and this function will
836  * fail, if @dest does not hold sufficient space for the PPS NAL unit.
837  *
838  * Return: number of bytes written to @dest or negative error code
839  */
nal_h264_write_pps(const struct device * dev,void * dest,size_t n,struct nal_h264_pps * pps)840 ssize_t nal_h264_write_pps(const struct device *dev,
841 			   void *dest, size_t n, struct nal_h264_pps *pps)
842 {
843 	struct rbsp rbsp;
844 	unsigned int forbidden_zero_bit = 0;
845 	unsigned int nal_ref_idc = 0;
846 	unsigned int nal_unit_type = PICTURE_PARAMETER_SET;
847 
848 	if (!dest)
849 		return -EINVAL;
850 
851 	rbsp_init(&rbsp, dest, n, &write);
852 
853 	nal_h264_write_start_code_prefix(&rbsp);
854 
855 	/* NAL unit header */
856 	rbsp_bit(&rbsp, &forbidden_zero_bit);
857 	rbsp_bits(&rbsp, 2, &nal_ref_idc);
858 	rbsp_bits(&rbsp, 5, &nal_unit_type);
859 
860 	nal_h264_rbsp_pps(&rbsp, pps);
861 
862 	nal_h264_rbsp_trailing_bits(&rbsp);
863 
864 	if (rbsp.error)
865 		return rbsp.error;
866 
867 	return DIV_ROUND_UP(rbsp.pos, 8);
868 }
869 EXPORT_SYMBOL_GPL(nal_h264_write_pps);
870 
871 /**
872  * nal_h264_read_pps() - Read PPS NAL unit from RBSP format
873  * @dev: device pointer
874  * @pps: the &struct nal_h264_pps to fill from the RBSP data
875  * @src: the buffer that contains the RBSP data
876  * @n: size of @src in bytes
877  *
878  * Read RBSP data from @src and use it to fill @pps.
879  *
880  * Return: number of bytes read from @src or negative error code
881  */
nal_h264_read_pps(const struct device * dev,struct nal_h264_pps * pps,void * src,size_t n)882 ssize_t nal_h264_read_pps(const struct device *dev,
883 			  struct nal_h264_pps *pps, void *src, size_t n)
884 {
885 	struct rbsp rbsp;
886 
887 	if (!src)
888 		return -EINVAL;
889 
890 	rbsp_init(&rbsp, src, n, &read);
891 
892 	nal_h264_read_start_code_prefix(&rbsp);
893 
894 	/* NAL unit header */
895 	rbsp.pos += 8;
896 
897 	nal_h264_rbsp_pps(&rbsp, pps);
898 
899 	nal_h264_rbsp_trailing_bits(&rbsp);
900 
901 	if (rbsp.error)
902 		return rbsp.error;
903 
904 	return DIV_ROUND_UP(rbsp.pos, 8);
905 }
906 EXPORT_SYMBOL_GPL(nal_h264_read_pps);
907 
908 /**
909  * nal_h264_write_filler() - Write filler data RBSP
910  * @dev: device pointer
911  * @dest: buffer to fill with filler data
912  * @n: size of the buffer to fill with filler data
913  *
914  * Write a filler data RBSP to @dest with a size of @n bytes and return the
915  * number of written filler data bytes.
916  *
917  * Use this function to generate dummy data in an RBSP data stream that can be
918  * safely ignored by h264 decoders.
919  *
920  * The RBSP format of the filler data is specified in Rec. ITU-T H.264
921  * (04/2017) 7.3.2.7 Filler data RBSP syntax.
922  *
923  * Return: number of filler data bytes (including marker) or negative error
924  */
nal_h264_write_filler(const struct device * dev,void * dest,size_t n)925 ssize_t nal_h264_write_filler(const struct device *dev, void *dest, size_t n)
926 {
927 	struct rbsp rbsp;
928 	unsigned int forbidden_zero_bit = 0;
929 	unsigned int nal_ref_idc = 0;
930 	unsigned int nal_unit_type = FILLER_DATA;
931 
932 	if (!dest)
933 		return -EINVAL;
934 
935 	rbsp_init(&rbsp, dest, n, &write);
936 
937 	nal_h264_write_start_code_prefix(&rbsp);
938 
939 	rbsp_bit(&rbsp, &forbidden_zero_bit);
940 	rbsp_bits(&rbsp, 2, &nal_ref_idc);
941 	rbsp_bits(&rbsp, 5, &nal_unit_type);
942 
943 	nal_h264_write_filler_data(&rbsp);
944 
945 	nal_h264_rbsp_trailing_bits(&rbsp);
946 
947 	return DIV_ROUND_UP(rbsp.pos, 8);
948 }
949 EXPORT_SYMBOL_GPL(nal_h264_write_filler);
950 
951 /**
952  * nal_h264_read_filler() - Read filler data RBSP
953  * @dev: device pointer
954  * @src: buffer with RBSP data that is read
955  * @n: maximum size of src that shall be read
956  *
957  * Read a filler data RBSP from @src up to a maximum size of @n bytes and
958  * return the size of the filler data in bytes including the marker.
959  *
960  * This function is used to parse filler data and skip the respective bytes in
961  * the RBSP data.
962  *
963  * The RBSP format of the filler data is specified in Rec. ITU-T H.264
964  * (04/2017) 7.3.2.7 Filler data RBSP syntax.
965  *
966  * Return: number of filler data bytes (including marker) or negative error
967  */
nal_h264_read_filler(const struct device * dev,void * src,size_t n)968 ssize_t nal_h264_read_filler(const struct device *dev, void *src, size_t n)
969 {
970 	struct rbsp rbsp;
971 	unsigned int forbidden_zero_bit;
972 	unsigned int nal_ref_idc;
973 	unsigned int nal_unit_type;
974 
975 	if (!src)
976 		return -EINVAL;
977 
978 	rbsp_init(&rbsp, src, n, &read);
979 
980 	nal_h264_read_start_code_prefix(&rbsp);
981 
982 	rbsp_bit(&rbsp, &forbidden_zero_bit);
983 	rbsp_bits(&rbsp, 2, &nal_ref_idc);
984 	rbsp_bits(&rbsp, 5, &nal_unit_type);
985 
986 	if (rbsp.error)
987 		return rbsp.error;
988 	if (forbidden_zero_bit != 0 ||
989 	    nal_ref_idc != 0 ||
990 	    nal_unit_type != FILLER_DATA)
991 		return -EINVAL;
992 
993 	nal_h264_read_filler_data(&rbsp);
994 	nal_h264_rbsp_trailing_bits(&rbsp);
995 
996 	if (rbsp.error)
997 		return rbsp.error;
998 
999 	return DIV_ROUND_UP(rbsp.pos, 8);
1000 }
1001 EXPORT_SYMBOL_GPL(nal_h264_read_filler);
1002