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#include "nal-rbsp.h"
26
27
28
29
30
31enum nal_unit_type {
32 SEQUENCE_PARAMETER_SET = 7,
33 PICTURE_PARAMETER_SET = 8,
34 FILLER_DATA = 12,
35};
36
37
38
39
40
41
42
43
44
45
46int nal_h264_profile_from_v4l2(enum v4l2_mpeg_video_h264_profile profile)
47{
48 switch (profile) {
49 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
50 return 66;
51 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
52 return 77;
53 case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED:
54 return 88;
55 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
56 return 100;
57 default:
58 return -EINVAL;
59 }
60}
61
62
63
64
65
66
67
68
69
70
71int nal_h264_level_from_v4l2(enum v4l2_mpeg_video_h264_level level)
72{
73 switch (level) {
74 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
75 return 10;
76 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
77 return 9;
78 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
79 return 11;
80 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
81 return 12;
82 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
83 return 13;
84 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
85 return 20;
86 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
87 return 21;
88 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
89 return 22;
90 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
91 return 30;
92 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
93 return 31;
94 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
95 return 32;
96 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
97 return 40;
98 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
99 return 41;
100 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
101 return 42;
102 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
103 return 50;
104 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
105 return 51;
106 default:
107 return -EINVAL;
108 }
109}
110
111static void nal_h264_write_start_code_prefix(struct rbsp *rbsp)
112{
113 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
114 int i = 4;
115
116 if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
117 rbsp->error = -EINVAL;
118 return;
119 }
120
121 p[0] = 0x00;
122 p[1] = 0x00;
123 p[2] = 0x00;
124 p[3] = 0x01;
125
126 rbsp->pos += i * 8;
127}
128
129static void nal_h264_read_start_code_prefix(struct rbsp *rbsp)
130{
131 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
132 int i = 4;
133
134 if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
135 rbsp->error = -EINVAL;
136 return;
137 }
138
139 if (p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x00 || p[3] != 0x01) {
140 rbsp->error = -EINVAL;
141 return;
142 }
143
144 rbsp->pos += i * 8;
145}
146
147static void nal_h264_write_filler_data(struct rbsp *rbsp)
148{
149 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
150 int i;
151
152
153 i = rbsp->size - DIV_ROUND_UP(rbsp->pos, 8) - 1;
154 memset(p, 0xff, i);
155 rbsp->pos += i * 8;
156}
157
158static void nal_h264_read_filler_data(struct rbsp *rbsp)
159{
160 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
161
162 while (*p == 0xff) {
163 if (DIV_ROUND_UP(rbsp->pos, 8) > rbsp->size) {
164 rbsp->error = -EINVAL;
165 return;
166 }
167
168 p++;
169 rbsp->pos += 8;
170 }
171}
172
173static void nal_h264_rbsp_hrd_parameters(struct rbsp *rbsp,
174 struct nal_h264_hrd_parameters *hrd)
175{
176 unsigned int i;
177
178 if (!hrd) {
179 rbsp->error = -EINVAL;
180 return;
181 }
182
183 rbsp_uev(rbsp, &hrd->cpb_cnt_minus1);
184 rbsp_bits(rbsp, 4, &hrd->bit_rate_scale);
185 rbsp_bits(rbsp, 4, &hrd->cpb_size_scale);
186
187 for (i = 0; i <= hrd->cpb_cnt_minus1; i++) {
188 rbsp_uev(rbsp, &hrd->bit_rate_value_minus1[i]);
189 rbsp_uev(rbsp, &hrd->cpb_size_value_minus1[i]);
190 rbsp_bit(rbsp, &hrd->cbr_flag[i]);
191 }
192
193 rbsp_bits(rbsp, 5, &hrd->initial_cpb_removal_delay_length_minus1);
194 rbsp_bits(rbsp, 5, &hrd->cpb_removal_delay_length_minus1);
195 rbsp_bits(rbsp, 5, &hrd->dpb_output_delay_length_minus1);
196 rbsp_bits(rbsp, 5, &hrd->time_offset_length);
197}
198
199static void nal_h264_rbsp_vui_parameters(struct rbsp *rbsp,
200 struct nal_h264_vui_parameters *vui)
201{
202 if (!vui) {
203 rbsp->error = -EINVAL;
204 return;
205 }
206
207 rbsp_bit(rbsp, &vui->aspect_ratio_info_present_flag);
208 if (vui->aspect_ratio_info_present_flag) {
209 rbsp_bits(rbsp, 8, &vui->aspect_ratio_idc);
210 if (vui->aspect_ratio_idc == 255) {
211 rbsp_bits(rbsp, 16, &vui->sar_width);
212 rbsp_bits(rbsp, 16, &vui->sar_height);
213 }
214 }
215
216 rbsp_bit(rbsp, &vui->overscan_info_present_flag);
217 if (vui->overscan_info_present_flag)
218 rbsp_bit(rbsp, &vui->overscan_appropriate_flag);
219
220 rbsp_bit(rbsp, &vui->video_signal_type_present_flag);
221 if (vui->video_signal_type_present_flag) {
222 rbsp_bits(rbsp, 3, &vui->video_format);
223 rbsp_bit(rbsp, &vui->video_full_range_flag);
224
225 rbsp_bit(rbsp, &vui->colour_description_present_flag);
226 if (vui->colour_description_present_flag) {
227 rbsp_bits(rbsp, 8, &vui->colour_primaries);
228 rbsp_bits(rbsp, 8, &vui->transfer_characteristics);
229 rbsp_bits(rbsp, 8, &vui->matrix_coefficients);
230 }
231 }
232
233 rbsp_bit(rbsp, &vui->chroma_loc_info_present_flag);
234 if (vui->chroma_loc_info_present_flag) {
235 rbsp_uev(rbsp, &vui->chroma_sample_loc_type_top_field);
236 rbsp_uev(rbsp, &vui->chroma_sample_loc_type_bottom_field);
237 }
238
239 rbsp_bit(rbsp, &vui->timing_info_present_flag);
240 if (vui->timing_info_present_flag) {
241 rbsp_bits(rbsp, 32, &vui->num_units_in_tick);
242 rbsp_bits(rbsp, 32, &vui->time_scale);
243 rbsp_bit(rbsp, &vui->fixed_frame_rate_flag);
244 }
245
246 rbsp_bit(rbsp, &vui->nal_hrd_parameters_present_flag);
247 if (vui->nal_hrd_parameters_present_flag)
248 nal_h264_rbsp_hrd_parameters(rbsp, &vui->nal_hrd_parameters);
249
250 rbsp_bit(rbsp, &vui->vcl_hrd_parameters_present_flag);
251 if (vui->vcl_hrd_parameters_present_flag)
252 nal_h264_rbsp_hrd_parameters(rbsp, &vui->vcl_hrd_parameters);
253
254 if (vui->nal_hrd_parameters_present_flag ||
255 vui->vcl_hrd_parameters_present_flag)
256 rbsp_bit(rbsp, &vui->low_delay_hrd_flag);
257
258 rbsp_bit(rbsp, &vui->pic_struct_present_flag);
259
260 rbsp_bit(rbsp, &vui->bitstream_restriction_flag);
261 if (vui->bitstream_restriction_flag) {
262 rbsp_bit(rbsp, &vui->motion_vectors_over_pic_boundaries_flag);
263 rbsp_uev(rbsp, &vui->max_bytes_per_pic_denom);
264 rbsp_uev(rbsp, &vui->max_bits_per_mb_denom);
265 rbsp_uev(rbsp, &vui->log2_max_mv_length_horizontal);
266 rbsp_uev(rbsp, &vui->log21_max_mv_length_vertical);
267 rbsp_uev(rbsp, &vui->max_num_reorder_frames);
268 rbsp_uev(rbsp, &vui->max_dec_frame_buffering);
269 }
270}
271
272static void nal_h264_rbsp_sps(struct rbsp *rbsp, struct nal_h264_sps *sps)
273{
274 unsigned int i;
275
276 if (!sps) {
277 rbsp->error = -EINVAL;
278 return;
279 }
280
281 rbsp_bits(rbsp, 8, &sps->profile_idc);
282 rbsp_bit(rbsp, &sps->constraint_set0_flag);
283 rbsp_bit(rbsp, &sps->constraint_set1_flag);
284 rbsp_bit(rbsp, &sps->constraint_set2_flag);
285 rbsp_bit(rbsp, &sps->constraint_set3_flag);
286 rbsp_bit(rbsp, &sps->constraint_set4_flag);
287 rbsp_bit(rbsp, &sps->constraint_set5_flag);
288 rbsp_bits(rbsp, 2, &sps->reserved_zero_2bits);
289 rbsp_bits(rbsp, 8, &sps->level_idc);
290
291 rbsp_uev(rbsp, &sps->seq_parameter_set_id);
292
293 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
294 sps->profile_idc == 122 || sps->profile_idc == 244 ||
295 sps->profile_idc == 44 || sps->profile_idc == 83 ||
296 sps->profile_idc == 86 || sps->profile_idc == 118 ||
297 sps->profile_idc == 128 || sps->profile_idc == 138 ||
298 sps->profile_idc == 139 || sps->profile_idc == 134 ||
299 sps->profile_idc == 135) {
300 rbsp_uev(rbsp, &sps->chroma_format_idc);
301
302 if (sps->chroma_format_idc == 3)
303 rbsp_bit(rbsp, &sps->separate_colour_plane_flag);
304 rbsp_uev(rbsp, &sps->bit_depth_luma_minus8);
305 rbsp_uev(rbsp, &sps->bit_depth_chroma_minus8);
306 rbsp_bit(rbsp, &sps->qpprime_y_zero_transform_bypass_flag);
307 rbsp_bit(rbsp, &sps->seq_scaling_matrix_present_flag);
308 if (sps->seq_scaling_matrix_present_flag)
309 rbsp->error = -EINVAL;
310 }
311
312 rbsp_uev(rbsp, &sps->log2_max_frame_num_minus4);
313
314 rbsp_uev(rbsp, &sps->pic_order_cnt_type);
315 switch (sps->pic_order_cnt_type) {
316 case 0:
317 rbsp_uev(rbsp, &sps->log2_max_pic_order_cnt_lsb_minus4);
318 break;
319 case 1:
320 rbsp_bit(rbsp, &sps->delta_pic_order_always_zero_flag);
321 rbsp_sev(rbsp, &sps->offset_for_non_ref_pic);
322 rbsp_sev(rbsp, &sps->offset_for_top_to_bottom_field);
323
324 rbsp_uev(rbsp, &sps->num_ref_frames_in_pic_order_cnt_cycle);
325 for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
326 rbsp_sev(rbsp, &sps->offset_for_ref_frame[i]);
327 break;
328 default:
329 rbsp->error = -EINVAL;
330 break;
331 }
332
333 rbsp_uev(rbsp, &sps->max_num_ref_frames);
334 rbsp_bit(rbsp, &sps->gaps_in_frame_num_value_allowed_flag);
335 rbsp_uev(rbsp, &sps->pic_width_in_mbs_minus1);
336 rbsp_uev(rbsp, &sps->pic_height_in_map_units_minus1);
337
338 rbsp_bit(rbsp, &sps->frame_mbs_only_flag);
339 if (!sps->frame_mbs_only_flag)
340 rbsp_bit(rbsp, &sps->mb_adaptive_frame_field_flag);
341
342 rbsp_bit(rbsp, &sps->direct_8x8_inference_flag);
343
344 rbsp_bit(rbsp, &sps->frame_cropping_flag);
345 if (sps->frame_cropping_flag) {
346 rbsp_uev(rbsp, &sps->crop_left);
347 rbsp_uev(rbsp, &sps->crop_right);
348 rbsp_uev(rbsp, &sps->crop_top);
349 rbsp_uev(rbsp, &sps->crop_bottom);
350 }
351
352 rbsp_bit(rbsp, &sps->vui_parameters_present_flag);
353 if (sps->vui_parameters_present_flag)
354 nal_h264_rbsp_vui_parameters(rbsp, &sps->vui);
355}
356
357static void nal_h264_rbsp_pps(struct rbsp *rbsp, struct nal_h264_pps *pps)
358{
359 int i;
360
361 rbsp_uev(rbsp, &pps->pic_parameter_set_id);
362 rbsp_uev(rbsp, &pps->seq_parameter_set_id);
363 rbsp_bit(rbsp, &pps->entropy_coding_mode_flag);
364 rbsp_bit(rbsp, &pps->bottom_field_pic_order_in_frame_present_flag);
365 rbsp_uev(rbsp, &pps->num_slice_groups_minus1);
366 if (pps->num_slice_groups_minus1 > 0) {
367 rbsp_uev(rbsp, &pps->slice_group_map_type);
368 switch (pps->slice_group_map_type) {
369 case 0:
370 for (i = 0; i < pps->num_slice_groups_minus1; i++)
371 rbsp_uev(rbsp, &pps->run_length_minus1[i]);
372 break;
373 case 2:
374 for (i = 0; i < pps->num_slice_groups_minus1; i++) {
375 rbsp_uev(rbsp, &pps->top_left[i]);
376 rbsp_uev(rbsp, &pps->bottom_right[i]);
377 }
378 break;
379 case 3: case 4: case 5:
380 rbsp_bit(rbsp, &pps->slice_group_change_direction_flag);
381 rbsp_uev(rbsp, &pps->slice_group_change_rate_minus1);
382 break;
383 case 6:
384 rbsp_uev(rbsp, &pps->pic_size_in_map_units_minus1);
385 for (i = 0; i < pps->pic_size_in_map_units_minus1; i++)
386 rbsp_bits(rbsp,
387 order_base_2(pps->num_slice_groups_minus1 + 1),
388 &pps->slice_group_id[i]);
389 break;
390 default:
391 break;
392 }
393 }
394 rbsp_uev(rbsp, &pps->num_ref_idx_l0_default_active_minus1);
395 rbsp_uev(rbsp, &pps->num_ref_idx_l1_default_active_minus1);
396 rbsp_bit(rbsp, &pps->weighted_pred_flag);
397 rbsp_bits(rbsp, 2, &pps->weighted_bipred_idc);
398 rbsp_sev(rbsp, &pps->pic_init_qp_minus26);
399 rbsp_sev(rbsp, &pps->pic_init_qs_minus26);
400 rbsp_sev(rbsp, &pps->chroma_qp_index_offset);
401 rbsp_bit(rbsp, &pps->deblocking_filter_control_present_flag);
402 rbsp_bit(rbsp, &pps->constrained_intra_pred_flag);
403 rbsp_bit(rbsp, &pps->redundant_pic_cnt_present_flag);
404 if ( false) {
405 rbsp_bit(rbsp, &pps->transform_8x8_mode_flag);
406 rbsp_bit(rbsp, &pps->pic_scaling_matrix_present_flag);
407 if (pps->pic_scaling_matrix_present_flag)
408 rbsp->error = -EINVAL;
409 rbsp_sev(rbsp, &pps->second_chroma_qp_index_offset);
410 }
411}
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427ssize_t nal_h264_write_sps(const struct device *dev,
428 void *dest, size_t n, struct nal_h264_sps *sps)
429{
430 struct rbsp rbsp;
431 unsigned int forbidden_zero_bit = 0;
432 unsigned int nal_ref_idc = 0;
433 unsigned int nal_unit_type = SEQUENCE_PARAMETER_SET;
434
435 if (!dest)
436 return -EINVAL;
437
438 rbsp_init(&rbsp, dest, n, &write);
439
440 nal_h264_write_start_code_prefix(&rbsp);
441
442 rbsp_bit(&rbsp, &forbidden_zero_bit);
443 rbsp_bits(&rbsp, 2, &nal_ref_idc);
444 rbsp_bits(&rbsp, 5, &nal_unit_type);
445
446 nal_h264_rbsp_sps(&rbsp, sps);
447
448 rbsp_trailing_bits(&rbsp);
449
450 if (rbsp.error)
451 return rbsp.error;
452
453 return DIV_ROUND_UP(rbsp.pos, 8);
454}
455EXPORT_SYMBOL_GPL(nal_h264_write_sps);
456
457
458
459
460
461
462
463
464
465
466
467
468ssize_t nal_h264_read_sps(const struct device *dev,
469 struct nal_h264_sps *sps, void *src, size_t n)
470{
471 struct rbsp rbsp;
472 unsigned int forbidden_zero_bit;
473 unsigned int nal_ref_idc;
474 unsigned int nal_unit_type;
475
476 if (!src)
477 return -EINVAL;
478
479 rbsp_init(&rbsp, src, n, &read);
480
481 nal_h264_read_start_code_prefix(&rbsp);
482
483 rbsp_bit(&rbsp, &forbidden_zero_bit);
484 rbsp_bits(&rbsp, 2, &nal_ref_idc);
485 rbsp_bits(&rbsp, 5, &nal_unit_type);
486
487 if (rbsp.error ||
488 forbidden_zero_bit != 0 ||
489 nal_ref_idc != 0 ||
490 nal_unit_type != SEQUENCE_PARAMETER_SET)
491 return -EINVAL;
492
493 nal_h264_rbsp_sps(&rbsp, sps);
494
495 rbsp_trailing_bits(&rbsp);
496
497 if (rbsp.error)
498 return rbsp.error;
499
500 return DIV_ROUND_UP(rbsp.pos, 8);
501}
502EXPORT_SYMBOL_GPL(nal_h264_read_sps);
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518ssize_t nal_h264_write_pps(const struct device *dev,
519 void *dest, size_t n, struct nal_h264_pps *pps)
520{
521 struct rbsp rbsp;
522 unsigned int forbidden_zero_bit = 0;
523 unsigned int nal_ref_idc = 0;
524 unsigned int nal_unit_type = PICTURE_PARAMETER_SET;
525
526 if (!dest)
527 return -EINVAL;
528
529 rbsp_init(&rbsp, dest, n, &write);
530
531 nal_h264_write_start_code_prefix(&rbsp);
532
533
534 rbsp_bit(&rbsp, &forbidden_zero_bit);
535 rbsp_bits(&rbsp, 2, &nal_ref_idc);
536 rbsp_bits(&rbsp, 5, &nal_unit_type);
537
538 nal_h264_rbsp_pps(&rbsp, pps);
539
540 rbsp_trailing_bits(&rbsp);
541
542 if (rbsp.error)
543 return rbsp.error;
544
545 return DIV_ROUND_UP(rbsp.pos, 8);
546}
547EXPORT_SYMBOL_GPL(nal_h264_write_pps);
548
549
550
551
552
553
554
555
556
557
558
559
560ssize_t nal_h264_read_pps(const struct device *dev,
561 struct nal_h264_pps *pps, void *src, size_t n)
562{
563 struct rbsp rbsp;
564
565 if (!src)
566 return -EINVAL;
567
568 rbsp_init(&rbsp, src, n, &read);
569
570 nal_h264_read_start_code_prefix(&rbsp);
571
572
573 rbsp.pos += 8;
574
575 nal_h264_rbsp_pps(&rbsp, pps);
576
577 rbsp_trailing_bits(&rbsp);
578
579 if (rbsp.error)
580 return rbsp.error;
581
582 return DIV_ROUND_UP(rbsp.pos, 8);
583}
584EXPORT_SYMBOL_GPL(nal_h264_read_pps);
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603ssize_t nal_h264_write_filler(const struct device *dev, void *dest, size_t n)
604{
605 struct rbsp rbsp;
606 unsigned int forbidden_zero_bit = 0;
607 unsigned int nal_ref_idc = 0;
608 unsigned int nal_unit_type = FILLER_DATA;
609
610 if (!dest)
611 return -EINVAL;
612
613 rbsp_init(&rbsp, dest, n, &write);
614
615 nal_h264_write_start_code_prefix(&rbsp);
616
617 rbsp_bit(&rbsp, &forbidden_zero_bit);
618 rbsp_bits(&rbsp, 2, &nal_ref_idc);
619 rbsp_bits(&rbsp, 5, &nal_unit_type);
620
621 nal_h264_write_filler_data(&rbsp);
622
623 rbsp_trailing_bits(&rbsp);
624
625 return DIV_ROUND_UP(rbsp.pos, 8);
626}
627EXPORT_SYMBOL_GPL(nal_h264_write_filler);
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646ssize_t nal_h264_read_filler(const struct device *dev, void *src, size_t n)
647{
648 struct rbsp rbsp;
649 unsigned int forbidden_zero_bit;
650 unsigned int nal_ref_idc;
651 unsigned int nal_unit_type;
652
653 if (!src)
654 return -EINVAL;
655
656 rbsp_init(&rbsp, src, n, &read);
657
658 nal_h264_read_start_code_prefix(&rbsp);
659
660 rbsp_bit(&rbsp, &forbidden_zero_bit);
661 rbsp_bits(&rbsp, 2, &nal_ref_idc);
662 rbsp_bits(&rbsp, 5, &nal_unit_type);
663
664 if (rbsp.error)
665 return rbsp.error;
666 if (forbidden_zero_bit != 0 ||
667 nal_ref_idc != 0 ||
668 nal_unit_type != FILLER_DATA)
669 return -EINVAL;
670
671 nal_h264_read_filler_data(&rbsp);
672 rbsp_trailing_bits(&rbsp);
673
674 if (rbsp.error)
675 return rbsp.error;
676
677 return DIV_ROUND_UP(rbsp.pos, 8);
678}
679EXPORT_SYMBOL_GPL(nal_h264_read_filler);
680