1
2
3
4
5
6
7
8
9
10
11
12
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
28
29
30enum nal_unit_type {
31 SEQUENCE_PARAMETER_SET = 7,
32 PICTURE_PARAMETER_SET = 8,
33 FILLER_DATA = 12,
34};
35
36struct rbsp;
37
38struct 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62struct 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
71static 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
86
87
88
89
90
91
92
93int 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
111
112
113
114
115
116
117
118int 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
158static int rbsp_read_bits(struct rbsp *rbsp, int n, unsigned int *value);
159static int rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int value);
160
161
162
163
164
165
166
167#define EMULATION_PREVENTION_THREE_BYTE (0x3 << 6)
168
169static 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
177static 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
189static 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
220static 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
248static 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
270static 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
286static 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
309static 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
326static 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
345static 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
360static int __rbsp_write_bit(struct rbsp *rbsp, int *value)
361{
362 return rbsp_write_bit(rbsp, *value);
363}
364
365static int __rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int *value)
366{
367 return rbsp_write_bits(rbsp, n, *value);
368}
369
370static 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
377static 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
388static 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
395static 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
402static 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
409static 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
416static 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
423static 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
433static 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
451static 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
469static 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
475 i = rbsp->size - DIV_ROUND_UP(rbsp->pos, 8) - 1;
476 memset(p, 0xff, i);
477 rbsp->pos += i * 8;
478}
479
480static 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
495static 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
521static 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
594static 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
679static 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 ( 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
737
738
739
740
741
742
743
744
745
746
747
748
749ssize_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}
777EXPORT_SYMBOL_GPL(nal_h264_write_sps);
778
779
780
781
782
783
784
785
786
787
788
789
790ssize_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}
824EXPORT_SYMBOL_GPL(nal_h264_read_sps);
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840ssize_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
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}
869EXPORT_SYMBOL_GPL(nal_h264_write_pps);
870
871
872
873
874
875
876
877
878
879
880
881
882ssize_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
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}
906EXPORT_SYMBOL_GPL(nal_h264_read_pps);
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925ssize_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}
949EXPORT_SYMBOL_GPL(nal_h264_write_filler);
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968ssize_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}
1001EXPORT_SYMBOL_GPL(nal_h264_read_filler);
1002