summaryrefslogtreecommitdiff
path: root/drivers/amvdec_ports/decoder/aml_hevc_parser.c (plain)
blob: 24977a8eedf2da8e74bc73c22918834aee1e826e
1#include <linux/kernel.h>
2#include <linux/types.h>
3#include <linux/vmalloc.h>
4#include <linux/mm.h>
5#include <linux/string.h>
6
7#include "aml_hevc_parser.h"
8#include "../utils/get_bits.h"
9#include "../utils/put_bits.h"
10#include "../utils/golomb.h"
11#include "../utils/common.h"
12#include "utils.h"
13
14const u8 ff_hevc_diag_scan4x4_x[16] = {
15 0, 0, 1, 0,
16 1, 2, 0, 1,
17 2, 3, 1, 2,
18 3, 2, 3, 3,
19};
20
21const u8 ff_hevc_diag_scan4x4_y[16] = {
22 0, 1, 0, 2,
23 1, 0, 3, 2,
24 1, 0, 3, 2,
25 1, 3, 2, 3,
26};
27
28const u8 ff_hevc_diag_scan8x8_x[64] = {
29 0, 0, 1, 0,
30 1, 2, 0, 1,
31 2, 3, 0, 1,
32 2, 3, 4, 0,
33 1, 2, 3, 4,
34 5, 0, 1, 2,
35 3, 4, 5, 6,
36 0, 1, 2, 3,
37 4, 5, 6, 7,
38 1, 2, 3, 4,
39 5, 6, 7, 2,
40 3, 4, 5, 6,
41 7, 3, 4, 5,
42 6, 7, 4, 5,
43 6, 7, 5, 6,
44 7, 6, 7, 7,
45};
46
47const u8 ff_hevc_diag_scan8x8_y[64] = {
48 0, 1, 0, 2,
49 1, 0, 3, 2,
50 1, 0, 4, 3,
51 2, 1, 0, 5,
52 4, 3, 2, 1,
53 0, 6, 5, 4,
54 3, 2, 1, 0,
55 7, 6, 5, 4,
56 3, 2, 1, 0,
57 7, 6, 5, 4,
58 3, 2, 1, 7,
59 6, 5, 4, 3,
60 2, 7, 6, 5,
61 4, 3, 7, 6,
62 5, 4, 7, 6,
63 5, 7, 6, 7,
64};
65
66static const u8 default_scaling_list_intra[] = {
67 16, 16, 16, 16, 17, 18, 21, 24,
68 16, 16, 16, 16, 17, 19, 22, 25,
69 16, 16, 17, 18, 20, 22, 25, 29,
70 16, 16, 18, 21, 24, 27, 31, 36,
71 17, 17, 20, 24, 30, 35, 41, 47,
72 18, 19, 22, 27, 35, 44, 54, 65,
73 21, 22, 25, 31, 41, 54, 70, 88,
74 24, 25, 29, 36, 47, 65, 88, 115
75};
76
77static const u8 default_scaling_list_inter[] = {
78 16, 16, 16, 16, 17, 18, 20, 24,
79 16, 16, 16, 17, 18, 20, 24, 25,
80 16, 16, 17, 18, 20, 24, 25, 28,
81 16, 17, 18, 20, 24, 25, 28, 33,
82 17, 18, 20, 24, 25, 28, 33, 41,
83 18, 20, 24, 25, 28, 33, 41, 54,
84 20, 24, 25, 28, 33, 41, 54, 71,
85 24, 25, 28, 33, 41, 54, 71, 91
86};
87
88static const struct AVRational vui_sar[] = {
89 { 0, 1 },
90 { 1, 1 },
91 { 12, 11 },
92 { 10, 11 },
93 { 16, 11 },
94 { 40, 33 },
95 { 24, 11 },
96 { 20, 11 },
97 { 32, 11 },
98 { 80, 33 },
99 { 18, 11 },
100 { 15, 11 },
101 { 64, 33 },
102 { 160, 99 },
103 { 4, 3 },
104 { 3, 2 },
105 { 2, 1 },
106};
107
108static const u8 hevc_sub_width_c[] = {
109 1, 2, 2, 1
110};
111
112static const u8 hevc_sub_height_c[] = {
113 1, 2, 1, 1
114};
115
116static int decode_profile_tier_level(struct get_bits_context *gb, struct PTLCommon *ptl)
117{
118 int i;
119
120 if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 16 + 16 + 12)
121 return -1;
122
123 ptl->profile_space = get_bits(gb, 2);
124 ptl->tier_flag = get_bits1(gb);
125 ptl->profile_idc = get_bits(gb, 5);
126 if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
127 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Main profile bitstream\n");
128 else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
129 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Main 10 profile bitstream\n");
130 else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_STILL_PICTURE)
131 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Main Still Picture profile bitstream\n");
132 else if (ptl->profile_idc == FF_PROFILE_HEVC_REXT)
133 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Range Extension profile bitstream\n");
134 else
135 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Unknown HEVC profile: %d\n", ptl->profile_idc);
136
137 for (i = 0; i < 32; i++) {
138 ptl->profile_compatibility_flag[i] = get_bits1(gb);
139
140 if (ptl->profile_idc == 0 && i > 0 && ptl->profile_compatibility_flag[i])
141 ptl->profile_idc = i;
142 }
143 ptl->progressive_source_flag = get_bits1(gb);
144 ptl->interlaced_source_flag = get_bits1(gb);
145 ptl->non_packed_constraint_flag = get_bits1(gb);
146 ptl->frame_only_constraint_flag = get_bits1(gb);
147
148 skip_bits(gb, 16); // XXX_reserved_zero_44bits[0..15]
149 skip_bits(gb, 16); // XXX_reserved_zero_44bits[16..31]
150 skip_bits(gb, 12); // XXX_reserved_zero_44bits[32..43]
151
152 return 0;
153}
154
155static int parse_ptl(struct get_bits_context *gb, struct PTL *ptl, int max_num_sub_layers)
156{
157 int i;
158 if (decode_profile_tier_level(gb, &ptl->general_ptl) < 0 ||
159 get_bits_left(gb) < 8 + (8*2 * (max_num_sub_layers - 1 > 0))) {
160 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "PTL information too short\n");
161 return -1;
162 }
163
164 ptl->general_ptl.level_idc = get_bits(gb, 8);
165
166 for (i = 0; i < max_num_sub_layers - 1; i++) {
167 ptl->sub_layer_profile_present_flag[i] = get_bits1(gb);
168 ptl->sub_layer_level_present_flag[i] = get_bits1(gb);
169 }
170
171 if (max_num_sub_layers - 1> 0)
172 for (i = max_num_sub_layers - 1; i < 8; i++)
173 skip_bits(gb, 2); // reserved_zero_2bits[i]
174 for (i = 0; i < max_num_sub_layers - 1; i++) {
175 if (ptl->sub_layer_profile_present_flag[i] &&
176 decode_profile_tier_level(gb, &ptl->sub_layer_ptl[i]) < 0) {
177 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "PTL information for sublayer %i too short\n", i);
178 return -1;
179 }
180 if (ptl->sub_layer_level_present_flag[i]) {
181 if (get_bits_left(gb) < 8) {
182 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Not enough data for sublayer %i level_idc\n", i);
183 return -1;
184 } else
185 ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
186 }
187 }
188
189 return 0;
190}
191
192static void decode_sublayer_hrd(struct get_bits_context *gb,
193 u32 nb_cpb, int subpic_params_present)
194{
195 int i;
196
197 for (i = 0; i < nb_cpb; i++) {
198 get_ue_golomb_long(gb); // bit_rate_value_minus1
199 get_ue_golomb_long(gb); // cpb_size_value_minus1
200
201 if (subpic_params_present) {
202 get_ue_golomb_long(gb); // cpb_size_du_value_minus1
203 get_ue_golomb_long(gb); // bit_rate_du_value_minus1
204 }
205 skip_bits1(gb); // cbr_flag
206 }
207}
208
209static int decode_hrd(struct get_bits_context *gb,
210 int common_inf_present, int max_sublayers)
211{
212 int nal_params_present = 0, vcl_params_present = 0;
213 int subpic_params_present = 0;
214 int i;
215
216 if (common_inf_present) {
217 nal_params_present = get_bits1(gb);
218 vcl_params_present = get_bits1(gb);
219
220 if (nal_params_present || vcl_params_present) {
221 subpic_params_present = get_bits1(gb);
222
223 if (subpic_params_present) {
224 skip_bits(gb, 8); // tick_divisor_minus2
225 skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
226 skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
227 skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
228 }
229
230 skip_bits(gb, 4); // bit_rate_scale
231 skip_bits(gb, 4); // cpb_size_scale
232
233 if (subpic_params_present)
234 skip_bits(gb, 4); // cpb_size_du_scale
235
236 skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
237 skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
238 skip_bits(gb, 5); // dpb_output_delay_length_minus1
239 }
240 }
241
242 for (i = 0; i < max_sublayers; i++) {
243 int low_delay = 0;
244 u32 nb_cpb = 1;
245 int fixed_rate = get_bits1(gb);
246
247 if (!fixed_rate)
248 fixed_rate = get_bits1(gb);
249
250 if (fixed_rate)
251 get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
252 else
253 low_delay = get_bits1(gb);
254
255 if (!low_delay) {
256 nb_cpb = get_ue_golomb_long(gb) + 1;
257 if (nb_cpb < 1 || nb_cpb > 32) {
258 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "nb_cpb %d invalid\n", nb_cpb);
259 return -1;
260 }
261 }
262
263 if (nal_params_present)
264 decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
265 if (vcl_params_present)
266 decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
267 }
268 return 0;
269}
270
271int ff_hevc_parse_vps(struct get_bits_context *gb, struct h265_VPS_t *vps)
272{
273 int i,j;
274 int vps_id = 0;
275
276 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Decoding VPS\n");
277
278 vps_id = get_bits(gb, 4);
279 if (vps_id >= HEVC_MAX_VPS_COUNT) {
280 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "VPS id out of range: %d\n", vps_id);
281 goto err;
282 }
283
284 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
285 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "vps_reserved_three_2bits is not three\n");
286 goto err;
287 }
288
289 vps->vps_max_layers = get_bits(gb, 6) + 1;
290 vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
291 vps->vps_temporal_id_nesting_flag = get_bits1(gb);
292
293 if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
294 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
295 goto err;
296 }
297
298 if (vps->vps_max_sub_layers > HEVC_MAX_SUB_LAYERS) {
299 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "vps_max_sub_layers out of range: %d\n",
300 vps->vps_max_sub_layers);
301 goto err;
302 }
303
304 if (parse_ptl(gb, &vps->ptl, vps->vps_max_sub_layers) < 0)
305 goto err;
306
307 vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);
308
309 i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
310 for (; i < vps->vps_max_sub_layers; i++) {
311 vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
312 vps->vps_num_reorder_pics[i] = get_ue_golomb_long(gb);
313 vps->vps_max_latency_increase[i] = get_ue_golomb_long(gb) - 1;
314
315 if (vps->vps_max_dec_pic_buffering[i] > HEVC_MAX_DPB_SIZE || !vps->vps_max_dec_pic_buffering[i]) {
316 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
317 vps->vps_max_dec_pic_buffering[i] - 1);
318 goto err;
319 }
320 if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
321 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "vps_max_num_reorder_pics out of range: %d\n",
322 vps->vps_num_reorder_pics[i]);
323 goto err;
324 }
325 }
326
327 vps->vps_max_layer_id = get_bits(gb, 6);
328 vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
329 if (vps->vps_num_layer_sets < 1 || vps->vps_num_layer_sets > 1024 ||
330 (vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
331 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "too many layer_id_included_flags\n");
332 goto err;
333 }
334
335 for (i = 1; i < vps->vps_num_layer_sets; i++)
336 for (j = 0; j <= vps->vps_max_layer_id; j++)
337 skip_bits(gb, 1); // layer_id_included_flag[i][j]
338
339 vps->vps_timing_info_present_flag = get_bits1(gb);
340 if (vps->vps_timing_info_present_flag) {
341 vps->vps_num_units_in_tick = get_bits_long(gb, 32);
342 vps->vps_time_scale = get_bits_long(gb, 32);
343 vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
344 if (vps->vps_poc_proportional_to_timing_flag)
345 vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
346 vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
347 if (vps->vps_num_hrd_parameters > (u32)vps->vps_num_layer_sets) {
348 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
349 goto err;
350 }
351 for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
352 int common_inf_present = 1;
353
354 get_ue_golomb_long(gb); // hrd_layer_set_idx
355 if (i)
356 common_inf_present = get_bits1(gb);
357 decode_hrd(gb, common_inf_present, vps->vps_max_sub_layers);
358 }
359 }
360 get_bits1(gb); /* vps_extension_flag */
361
362 if (get_bits_left(gb) < 0) {
363 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Overread VPS by %d bits\n", -get_bits_left(gb));
364 goto err;
365 }
366
367 return 0;
368err:
369 return -1;
370}
371
372static int map_pixel_format(struct h265_SPS_t *sps)
373{
374 /*const AVPixFmtDescriptor *desc;*/
375 switch (sps->bit_depth) {
376 case 8:
377 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
378 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
379 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
380 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
381 break;
382 case 9:
383 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY9;
384 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
385 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
386 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
387 break;
388 case 10:
389 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
390 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
391 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
392 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
393 break;
394 case 12:
395 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY12;
396 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
397 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
398 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
399 break;
400 default:
401 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
402 "chroma_format_idc is %d, depth is %d\n",
403 sps->chroma_format_idc, sps->bit_depth);
404 return -1;
405 }
406
407 /*desc = av_pix_fmt_desc_get(sps->pix_fmt);
408 if (!desc)
409 return AVERROR(EINVAL);
410
411 sps->hshift[0] = sps->vshift[0] = 0;
412 sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
413 sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;*/
414
415 sps->pixel_shift = sps->bit_depth > 8;
416
417 return 0;
418}
419
420static void set_default_scaling_list_data(struct ScalingList *sl)
421{
422 int matrixId;
423
424 for (matrixId = 0; matrixId < 6; matrixId++) {
425 // 4x4 default is 16
426 memset(sl->sl[0][matrixId], 16, 16);
427 sl->sl_dc[0][matrixId] = 16; // default for 16x16
428 sl->sl_dc[1][matrixId] = 16; // default for 32x32
429 }
430 memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
431 memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
432 memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
433 memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
434 memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
435 memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
436 memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
437 memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
438 memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
439 memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
440 memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
441 memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
442 memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
443 memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
444 memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
445 memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
446 memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
447 memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
448}
449
450static int scaling_list_data(struct get_bits_context *gb,
451 struct ScalingList *sl, struct h265_SPS_t *sps)
452{
453 u8 scaling_list_pred_mode_flag;
454 int scaling_list_dc_coef[2][6];
455 int size_id, matrix_id, pos;
456 int i;
457
458 for (size_id = 0; size_id < 4; size_id++)
459 for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
460 scaling_list_pred_mode_flag = get_bits1(gb);
461 if (!scaling_list_pred_mode_flag) {
462 u32 delta = get_ue_golomb_long(gb);
463 /* Only need to handle non-zero delta. Zero means default,
464 * which should already be in the arrays. */
465 if (delta) {
466 // Copy from previous array.
467 delta *= (size_id == 3) ? 3 : 1;
468 if (matrix_id < delta) {
469 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid delta in scaling list data: %d.\n", delta);
470 return -1;
471 }
472
473 memcpy(sl->sl[size_id][matrix_id],
474 sl->sl[size_id][matrix_id - delta],
475 size_id > 0 ? 64 : 16);
476 if (size_id > 1)
477 sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
478 }
479 } else {
480 int next_coef, coef_num;
481 int scaling_list_delta_coef;
482
483 next_coef = 8;
484 coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
485 if (size_id > 1) {
486 scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
487 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
488 sl->sl_dc[size_id - 2][matrix_id] = next_coef;
489 }
490 for (i = 0; i < coef_num; i++) {
491 if (size_id == 0)
492 pos = 4 * ff_hevc_diag_scan4x4_y[i] +
493 ff_hevc_diag_scan4x4_x[i];
494 else
495 pos = 8 * ff_hevc_diag_scan8x8_y[i] +
496 ff_hevc_diag_scan8x8_x[i];
497
498 scaling_list_delta_coef = get_se_golomb(gb);
499 next_coef = (next_coef + 256U + scaling_list_delta_coef) % 256;
500 sl->sl[size_id][matrix_id][pos] = next_coef;
501 }
502 }
503 }
504
505 if (sps->chroma_format_idc == 3) {
506 for (i = 0; i < 64; i++) {
507 sl->sl[3][1][i] = sl->sl[2][1][i];
508 sl->sl[3][2][i] = sl->sl[2][2][i];
509 sl->sl[3][4][i] = sl->sl[2][4][i];
510 sl->sl[3][5][i] = sl->sl[2][5][i];
511 }
512 sl->sl_dc[1][1] = sl->sl_dc[0][1];
513 sl->sl_dc[1][2] = sl->sl_dc[0][2];
514 sl->sl_dc[1][4] = sl->sl_dc[0][4];
515 sl->sl_dc[1][5] = sl->sl_dc[0][5];
516 }
517
518 return 0;
519}
520
521int ff_hevc_decode_short_term_rps(struct get_bits_context *gb,
522 struct ShortTermRPS *rps, const struct h265_SPS_t *sps, int is_slice_header)
523{
524 u8 rps_predict = 0;
525 int delta_poc;
526 int k0 = 0;
527 int k1 = 0;
528 int k = 0;
529 int i;
530
531 if (rps != sps->st_rps && sps->nb_st_rps)
532 rps_predict = get_bits1(gb);
533
534 if (rps_predict) {
535 const struct ShortTermRPS *rps_ridx;
536 int delta_rps;
537 u32 abs_delta_rps;
538 u8 use_delta_flag = 0;
539 u8 delta_rps_sign;
540
541 if (is_slice_header) {
542 u32 delta_idx = get_ue_golomb_long(gb) + 1;
543 if (delta_idx > sps->nb_st_rps) {
544 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
545 delta_idx, sps->nb_st_rps);
546 return -1;
547 }
548 rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
549 rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
550 } else
551 rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
552
553 delta_rps_sign = get_bits1(gb);
554 abs_delta_rps = get_ue_golomb_long(gb) + 1;
555 if (abs_delta_rps < 1 || abs_delta_rps > 32768) {
556 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid value of abs_delta_rps: %d\n",
557 abs_delta_rps);
558 return -1;
559 }
560 delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
561 for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
562 int used = rps->used[k] = get_bits1(gb);
563
564 if (!used)
565 use_delta_flag = get_bits1(gb);
566
567 if (used || use_delta_flag) {
568 if (i < rps_ridx->num_delta_pocs)
569 delta_poc = delta_rps + rps_ridx->delta_poc[i];
570 else
571 delta_poc = delta_rps;
572 rps->delta_poc[k] = delta_poc;
573 if (delta_poc < 0)
574 k0++;
575 else
576 k1++;
577 k++;
578 }
579 }
580
581 if (k >= ARRAY_SIZE(rps->used)) {
582 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid num_delta_pocs: %d\n", k);
583 return -1;
584 }
585
586 rps->num_delta_pocs = k;
587 rps->num_negative_pics = k0;
588 // sort in increasing order (smallest first)
589 if (rps->num_delta_pocs != 0) {
590 int used, tmp;
591 for (i = 1; i < rps->num_delta_pocs; i++) {
592 delta_poc = rps->delta_poc[i];
593 used = rps->used[i];
594 for (k = i - 1; k >= 0; k--) {
595 tmp = rps->delta_poc[k];
596 if (delta_poc < tmp) {
597 rps->delta_poc[k + 1] = tmp;
598 rps->used[k + 1] = rps->used[k];
599 rps->delta_poc[k] = delta_poc;
600 rps->used[k] = used;
601 }
602 }
603 }
604 }
605 if ((rps->num_negative_pics >> 1) != 0) {
606 int used;
607 k = rps->num_negative_pics - 1;
608 // flip the negative values to largest first
609 for (i = 0; i < rps->num_negative_pics >> 1; i++) {
610 delta_poc = rps->delta_poc[i];
611 used = rps->used[i];
612 rps->delta_poc[i] = rps->delta_poc[k];
613 rps->used[i] = rps->used[k];
614 rps->delta_poc[k] = delta_poc;
615 rps->used[k] = used;
616 k--;
617 }
618 }
619 } else {
620 u32 prev, nb_positive_pics;
621 rps->num_negative_pics = get_ue_golomb_long(gb);
622 nb_positive_pics = get_ue_golomb_long(gb);
623
624 if (rps->num_negative_pics >= HEVC_MAX_REFS ||
625 nb_positive_pics >= HEVC_MAX_REFS) {
626 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Too many refs in a short term RPS.\n");
627 return -1;
628 }
629
630 rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
631 if (rps->num_delta_pocs) {
632 prev = 0;
633 for (i = 0; i < rps->num_negative_pics; i++) {
634 delta_poc = get_ue_golomb_long(gb) + 1;
635 if (delta_poc < 1 || delta_poc > 32768) {
636 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid value of delta_poc: %d\n",
637 delta_poc);
638 return -1;
639 }
640 prev -= delta_poc;
641 rps->delta_poc[i] = prev;
642 rps->used[i] = get_bits1(gb);
643 }
644 prev = 0;
645 for (i = 0; i < nb_positive_pics; i++) {
646 delta_poc = get_ue_golomb_long(gb) + 1;
647 if (delta_poc < 1 || delta_poc > 32768) {
648 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid value of delta_poc: %d\n",
649 delta_poc);
650 return -1;
651 }
652 prev += delta_poc;
653 rps->delta_poc[rps->num_negative_pics + i] = prev;
654 rps->used[rps->num_negative_pics + i] = get_bits1(gb);
655 }
656 }
657 }
658 return 0;
659}
660
661static void decode_vui(struct get_bits_context *gb, struct h265_SPS_t *sps)
662{
663 struct VUI backup_vui, *vui = &sps->vui;
664 struct get_bits_context backup;
665 int sar_present, alt = 0;
666
667 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Decoding VUI\n");
668
669 sar_present = get_bits1(gb);
670 if (sar_present) {
671 u8 sar_idx = get_bits(gb, 8);
672 if (sar_idx < ARRAY_SIZE(vui_sar))
673 vui->sar = vui_sar[sar_idx];
674 else if (sar_idx == 255) {
675 vui->sar.num = get_bits(gb, 16);
676 vui->sar.den = get_bits(gb, 16);
677 } else
678 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER,
679 "Unknown SAR index: %u.\n", sar_idx);
680 }
681
682 vui->overscan_info_present_flag = get_bits1(gb);
683 if (vui->overscan_info_present_flag)
684 vui->overscan_appropriate_flag = get_bits1(gb);
685
686 vui->video_signal_type_present_flag = get_bits1(gb);
687 if (vui->video_signal_type_present_flag) {
688 vui->video_format = get_bits(gb, 3);
689 vui->video_full_range_flag = get_bits1(gb);
690 vui->colour_description_present_flag = get_bits1(gb);
691 if (vui->video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
692 sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
693 if (vui->colour_description_present_flag) {
694 vui->colour_primaries = get_bits(gb, 8);
695 vui->transfer_characteristic = get_bits(gb, 8);
696 vui->matrix_coeffs = get_bits(gb, 8);
697
698 // Set invalid values to "unspecified"
699 if (!av_color_primaries_name(vui->colour_primaries))
700 vui->colour_primaries = AVCOL_PRI_UNSPECIFIED;
701 if (!av_color_transfer_name(vui->transfer_characteristic))
702 vui->transfer_characteristic = AVCOL_TRC_UNSPECIFIED;
703 if (!av_color_space_name(vui->matrix_coeffs))
704 vui->matrix_coeffs = AVCOL_SPC_UNSPECIFIED;
705 if (vui->matrix_coeffs == AVCOL_SPC_RGB) {
706 switch (sps->pix_fmt) {
707 case AV_PIX_FMT_YUV444P:
708 sps->pix_fmt = AV_PIX_FMT_GBRP;
709 break;
710 case AV_PIX_FMT_YUV444P10:
711 sps->pix_fmt = AV_PIX_FMT_GBRP10;
712 break;
713 case AV_PIX_FMT_YUV444P12:
714 sps->pix_fmt = AV_PIX_FMT_GBRP12;
715 break;
716 }
717 }
718 }
719 }
720
721 vui->chroma_loc_info_present_flag = get_bits1(gb);
722 if (vui->chroma_loc_info_present_flag) {
723 vui->chroma_sample_loc_type_top_field = get_ue_golomb_long(gb);
724 vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_long(gb);
725 }
726
727 vui->neutra_chroma_indication_flag = get_bits1(gb);
728 vui->field_seq_flag = get_bits1(gb);
729 vui->frame_field_info_present_flag = get_bits1(gb);
730
731 // Backup context in case an alternate header is detected
732 memcpy(&backup, gb, sizeof(backup));
733 memcpy(&backup_vui, vui, sizeof(backup_vui));
734 if (get_bits_left(gb) >= 68 && show_bits_long(gb, 21) == 0x100000) {
735 vui->default_display_window_flag = 0;
736 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Invalid default display window\n");
737 } else
738 vui->default_display_window_flag = get_bits1(gb);
739
740 if (vui->default_display_window_flag) {
741 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
742 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
743 vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
744 vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
745 vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
746 vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
747 }
748
749timing_info:
750 vui->vui_timing_info_present_flag = get_bits1(gb);
751
752 if (vui->vui_timing_info_present_flag) {
753 if (get_bits_left(gb) < 66 && !alt) {
754 // The alternate syntax seem to have timing info located
755 // at where def_disp_win is normally located
756 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Strange VUI timing information, retrying...\n");
757 memcpy(vui, &backup_vui, sizeof(backup_vui));
758 memcpy(gb, &backup, sizeof(backup));
759 alt = 1;
760 goto timing_info;
761 }
762 vui->vui_num_units_in_tick = get_bits_long(gb, 32);
763 vui->vui_time_scale = get_bits_long(gb, 32);
764 if (alt) {
765 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Retry got %u/%u fps\n",
766 vui->vui_time_scale, vui->vui_num_units_in_tick);
767 }
768 vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
769 if (vui->vui_poc_proportional_to_timing_flag)
770 vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
771 vui->vui_hrd_parameters_present_flag = get_bits1(gb);
772 if (vui->vui_hrd_parameters_present_flag)
773 decode_hrd(gb, 1, sps->max_sub_layers);
774 }
775
776 vui->bitstream_restriction_flag = get_bits1(gb);
777 if (vui->bitstream_restriction_flag) {
778 if (get_bits_left(gb) < 8 && !alt) {
779 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Strange VUI bitstream restriction information, retrying"
780 " from timing information...\n");
781 memcpy(vui, &backup_vui, sizeof(backup_vui));
782 memcpy(gb, &backup, sizeof(backup));
783 alt = 1;
784 goto timing_info;
785 }
786 vui->tiles_fixed_structure_flag = get_bits1(gb);
787 vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
788 vui->restricted_ref_pic_lists_flag = get_bits1(gb);
789 vui->min_spatial_segmentation_idc = get_ue_golomb_long(gb);
790 vui->max_bytes_per_pic_denom = get_ue_golomb_long(gb);
791 vui->max_bits_per_min_cu_denom = get_ue_golomb_long(gb);
792 vui->log2_max_mv_length_horizontal = get_ue_golomb_long(gb);
793 vui->log2_max_mv_length_vertical = get_ue_golomb_long(gb);
794 }
795
796 if (get_bits_left(gb) < 1 && !alt) {
797 // XXX: Alternate syntax when sps_range_extension_flag != 0?
798 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Overread in VUI, retrying from timing information...\n");
799 memcpy(vui, &backup_vui, sizeof(backup_vui));
800 memcpy(gb, &backup, sizeof(backup));
801 alt = 1;
802 goto timing_info;
803 }
804}
805
806int ff_hevc_parse_sps(struct get_bits_context *gb, struct h265_SPS_t *sps)
807{
808 int i, ret = 0;
809 int log2_diff_max_min_transform_block_size;
810 int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
811 struct HEVCWindow *ow;
812
813 sps->vps_id = get_bits(gb, 4);
814 if (sps->vps_id >= HEVC_MAX_VPS_COUNT) {
815 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "VPS id out of range: %d\n", sps->vps_id);
816 return -1;
817 }
818
819 sps->max_sub_layers = get_bits(gb, 3) + 1;
820 if (sps->max_sub_layers > HEVC_MAX_SUB_LAYERS) {
821 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "sps_max_sub_layers out of range: %d\n",
822 sps->max_sub_layers);
823 return -1;
824 }
825
826 sps->temporal_id_nesting_flag = get_bits(gb, 1);
827
828 if ((ret = parse_ptl(gb, &sps->ptl, sps->max_sub_layers)) < 0)
829 return ret;
830
831 sps->sps_id = get_ue_golomb_long(gb);
832 if (sps->sps_id >= HEVC_MAX_SPS_COUNT) {
833 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "SPS id out of range: %d\n", sps->sps_id);
834 return -1;
835 }
836
837 sps->chroma_format_idc = get_ue_golomb_long(gb);
838 if (sps->chroma_format_idc > 3U) {
839 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
840 return -1;
841 }
842
843 if (sps->chroma_format_idc == 3)
844 sps->separate_colour_plane_flag = get_bits1(gb);
845
846 if (sps->separate_colour_plane_flag)
847 sps->chroma_format_idc = 0;
848
849 sps->width = get_ue_golomb_long(gb);
850 sps->height = get_ue_golomb_long(gb);
851 if (sps->width > 8192 || sps->height > 8192) {
852 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "width or height oversize.\n");
853 return -1;
854 }
855
856 if (get_bits1(gb)) { // pic_conformance_flag
857 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
858 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
859 sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
860 sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
861 sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
862 sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
863 sps->output_window = sps->pic_conf_win;
864 }
865
866 sps->bit_depth = get_ue_golomb_long(gb) + 8;
867 bit_depth_chroma = get_ue_golomb_long(gb) + 8;
868 if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth) {
869 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Luma bit depth (%d) is different from chroma bit depth (%d), this is unsupported.\n",
870 sps->bit_depth, bit_depth_chroma);
871 return -1;
872 }
873 sps->bit_depth_chroma = bit_depth_chroma;
874
875 ret = map_pixel_format(sps);
876 if (ret < 0)
877 return ret;
878
879 sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
880 if (sps->log2_max_poc_lsb > 16) {
881 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
882 sps->log2_max_poc_lsb - 4);
883 return -1;
884 }
885
886 sublayer_ordering_info = get_bits1(gb);
887 start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
888 for (i = start; i < sps->max_sub_layers; i++) {
889 sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
890 sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb);
891 sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1;
892 if (sps->temporal_layer[i].max_dec_pic_buffering > (u32)HEVC_MAX_DPB_SIZE) {
893 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
894 sps->temporal_layer[i].max_dec_pic_buffering - 1U);
895 return -1;
896 }
897 if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
898 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "sps_max_num_reorder_pics out of range: %d\n",
899 sps->temporal_layer[i].num_reorder_pics);
900 if (sps->temporal_layer[i].num_reorder_pics > HEVC_MAX_DPB_SIZE - 1) {
901 return -1;
902 }
903 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;
904 }
905 }
906
907 if (!sublayer_ordering_info) {
908 for (i = 0; i < start; i++) {
909 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
910 sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics;
911 sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase;
912 }
913 }
914
915 sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
916 sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
917 sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
918 log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
919 sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size + sps->log2_min_tb_size;
920
921 if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30) {
922 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
923 return -1;
924 }
925
926 if (sps->log2_diff_max_min_coding_block_size > 30) {
927 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid value %d for log2_diff_max_min_coding_block_size", sps->log2_diff_max_min_coding_block_size);
928 return -1;
929 }
930
931 if (sps->log2_min_tb_size >= sps->log2_min_cb_size || sps->log2_min_tb_size < 2) {
932 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid value for log2_min_tb_size");
933 return -1;
934 }
935
936 if (log2_diff_max_min_transform_block_size < 0 || log2_diff_max_min_transform_block_size > 30) {
937 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size", log2_diff_max_min_transform_block_size);
938 return -1;
939 }
940
941 sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
942 sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
943
944 sps->scaling_list_enable_flag = get_bits1(gb);
945 if (sps->scaling_list_enable_flag) {
946 set_default_scaling_list_data(&sps->scaling_list);
947
948 if (get_bits1(gb)) {
949 ret = scaling_list_data(gb, &sps->scaling_list, sps);
950 if (ret < 0)
951 return ret;
952 }
953 }
954
955 sps->amp_enabled_flag = get_bits1(gb);
956 sps->sao_enabled = get_bits1(gb);
957
958 sps->pcm_enabled_flag = get_bits1(gb);
959 if (sps->pcm_enabled_flag) {
960 sps->pcm.bit_depth = get_bits(gb, 4) + 1;
961 sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
962 sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
963 sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
964 get_ue_golomb_long(gb);
965 if (FFMAX(sps->pcm.bit_depth, sps->pcm.bit_depth_chroma) > sps->bit_depth) {
966 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "PCM bit depth (%d, %d) is greater than normal bit depth (%d)\n",
967 sps->pcm.bit_depth, sps->pcm.bit_depth_chroma, sps->bit_depth);
968 return -1;
969 }
970
971 sps->pcm.loop_filter_disable_flag = get_bits1(gb);
972 }
973
974 sps->nb_st_rps = get_ue_golomb_long(gb);
975 if (sps->nb_st_rps > HEVC_MAX_SHORT_TERM_REF_PIC_SETS) {
976 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Too many short term RPS: %d.\n", sps->nb_st_rps);
977 return -1;
978 }
979 for (i = 0; i < sps->nb_st_rps; i++) {
980 if ((ret = ff_hevc_decode_short_term_rps(gb, &sps->st_rps[i], sps, 0)) < 0)
981 return ret;
982 }
983
984 sps->long_term_ref_pics_present_flag = get_bits1(gb);
985 if (sps->long_term_ref_pics_present_flag) {
986 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
987 if (sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PICS) {
988 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Too many long term ref pics: %d.\n",
989 sps->num_long_term_ref_pics_sps);
990 return -1;
991 }
992 for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
993 sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
994 sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
995 }
996 }
997
998 sps->sps_temporal_mvp_enabled_flag = get_bits1(gb);
999 sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);
1000 sps->vui.sar = (struct AVRational){0, 1};
1001 vui_present = get_bits1(gb);
1002 if (vui_present)
1003 decode_vui(gb, sps);
1004
1005 if (get_bits1(gb)) { // sps_extension_flag
1006 sps->sps_range_extension_flag = get_bits1(gb);
1007 skip_bits(gb, 7); //sps_extension_7bits = get_bits(gb, 7);
1008 if (sps->sps_range_extension_flag) {
1009 sps->transform_skip_rotation_enabled_flag = get_bits1(gb);
1010 sps->transform_skip_context_enabled_flag = get_bits1(gb);
1011 sps->implicit_rdpcm_enabled_flag = get_bits1(gb);
1012 sps->explicit_rdpcm_enabled_flag = get_bits1(gb);
1013 sps->extended_precision_processing_flag = get_bits1(gb);
1014 if (sps->extended_precision_processing_flag)
1015 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "extended_precision_processing_flag not yet implemented\n");
1016
1017 sps->intra_smoothing_disabled_flag = get_bits1(gb);
1018 sps->high_precision_offsets_enabled_flag = get_bits1(gb);
1019 if (sps->high_precision_offsets_enabled_flag)
1020 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "high_precision_offsets_enabled_flag not yet implemented\n");
1021
1022 sps->persistent_rice_adaptation_enabled_flag = get_bits1(gb);
1023 sps->cabac_bypass_alignment_enabled_flag = get_bits1(gb);
1024 if (sps->cabac_bypass_alignment_enabled_flag)
1025 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "cabac_bypass_alignment_enabled_flag not yet implemented\n");
1026 }
1027 }
1028
1029 ow = &sps->output_window;
1030 if (ow->left_offset >= INT_MAX - ow->right_offset ||
1031 ow->top_offset >= INT_MAX - ow->bottom_offset ||
1032 ow->left_offset + ow->right_offset >= sps->width ||
1033 ow->top_offset + ow->bottom_offset >= sps->height) {
1034 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid cropping offsets: %u/%u/%u/%u\n",
1035 ow->left_offset, ow->right_offset, ow->top_offset, ow->bottom_offset);
1036 return -1;
1037 }
1038
1039 // Inferred parameters
1040 sps->log2_ctb_size = sps->log2_min_cb_size +
1041 sps->log2_diff_max_min_coding_block_size;
1042 sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
1043
1044 if (sps->log2_ctb_size > HEVC_MAX_LOG2_CTB_SIZE) {
1045 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
1046 return -1;
1047 }
1048 if (sps->log2_ctb_size < 4) {
1049 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "log2_ctb_size %d differs from the bounds of any known profile\n", sps->log2_ctb_size);
1050 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "log2_ctb_size %d", sps->log2_ctb_size);
1051 return -1;
1052 }
1053
1054 sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1055 sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1056 sps->ctb_size = sps->ctb_width * sps->ctb_height;
1057
1058 sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
1059 sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
1060 sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
1061 sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
1062 sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
1063 sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
1064 sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
1065 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1066
1067 if (av_mod_uintp2(sps->width, sps->log2_min_cb_size) ||
1068 av_mod_uintp2(sps->height, sps->log2_min_cb_size)) {
1069 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid coded frame dimensions.\n");
1070 return -1;
1071 }
1072
1073 if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
1074 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
1075 sps->max_transform_hierarchy_depth_inter);
1076 return -1;
1077 }
1078 if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
1079 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
1080 sps->max_transform_hierarchy_depth_intra);
1081 return -1;
1082 }
1083 if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
1084 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "max transform block size out of range: %d\n",
1085 sps->log2_max_trafo_size);
1086 return -1;
1087 }
1088
1089 if (get_bits_left(gb) < 0) {
1090 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Overread SPS by %d bits\n", -get_bits_left(gb));
1091 return -1;
1092 }
1093
1094 v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Parsed SPS: id %d; ref: %d, coded wxh: %dx%d, cropped wxh: %dx%d; pix_fmt: %d.\n",
1095 sps->sps_id, sps->temporal_layer[0].num_reorder_pics, sps->width, sps->height,
1096 sps->width - (sps->output_window.left_offset + sps->output_window.right_offset),
1097 sps->height - (sps->output_window.top_offset + sps->output_window.bottom_offset),
1098 sps->pix_fmt);
1099
1100 return 0;
1101}
1102
1103const char *hevc_nal_type_name[64] = {
1104 "TRAIL_N", // HEVC_NAL_TRAIL_N
1105 "TRAIL_R", // HEVC_NAL_TRAIL_R
1106 "TSA_N", // HEVC_NAL_TSA_N
1107 "TSA_R", // HEVC_NAL_TSA_R
1108 "STSA_N", // HEVC_NAL_STSA_N
1109 "STSA_R", // HEVC_NAL_STSA_R
1110 "RADL_N", // HEVC_NAL_RADL_N
1111 "RADL_R", // HEVC_NAL_RADL_R
1112 "RASL_N", // HEVC_NAL_RASL_N
1113 "RASL_R", // HEVC_NAL_RASL_R
1114 "RSV_VCL_N10", // HEVC_NAL_VCL_N10
1115 "RSV_VCL_R11", // HEVC_NAL_VCL_R11
1116 "RSV_VCL_N12", // HEVC_NAL_VCL_N12
1117 "RSV_VLC_R13", // HEVC_NAL_VCL_R13
1118 "RSV_VCL_N14", // HEVC_NAL_VCL_N14
1119 "RSV_VCL_R15", // HEVC_NAL_VCL_R15
1120 "BLA_W_LP", // HEVC_NAL_BLA_W_LP
1121 "BLA_W_RADL", // HEVC_NAL_BLA_W_RADL
1122 "BLA_N_LP", // HEVC_NAL_BLA_N_LP
1123 "IDR_W_RADL", // HEVC_NAL_IDR_W_RADL
1124 "IDR_N_LP", // HEVC_NAL_IDR_N_LP
1125 "CRA_NUT", // HEVC_NAL_CRA_NUT
1126 "IRAP_IRAP_VCL22", // HEVC_NAL_IRAP_VCL22
1127 "IRAP_IRAP_VCL23", // HEVC_NAL_IRAP_VCL23
1128 "RSV_VCL24", // HEVC_NAL_RSV_VCL24
1129 "RSV_VCL25", // HEVC_NAL_RSV_VCL25
1130 "RSV_VCL26", // HEVC_NAL_RSV_VCL26
1131 "RSV_VCL27", // HEVC_NAL_RSV_VCL27
1132 "RSV_VCL28", // HEVC_NAL_RSV_VCL28
1133 "RSV_VCL29", // HEVC_NAL_RSV_VCL29
1134 "RSV_VCL30", // HEVC_NAL_RSV_VCL30
1135 "RSV_VCL31", // HEVC_NAL_RSV_VCL31
1136 "VPS", // HEVC_NAL_VPS
1137 "SPS", // HEVC_NAL_SPS
1138 "PPS", // HEVC_NAL_PPS
1139 "AUD", // HEVC_NAL_AUD
1140 "EOS_NUT", // HEVC_NAL_EOS_NUT
1141 "EOB_NUT", // HEVC_NAL_EOB_NUT
1142 "FD_NUT", // HEVC_NAL_FD_NUT
1143 "SEI_PREFIX", // HEVC_NAL_SEI_PREFIX
1144 "SEI_SUFFIX", // HEVC_NAL_SEI_SUFFIX
1145 "RSV_NVCL41", // HEVC_NAL_RSV_NVCL41
1146 "RSV_NVCL42", // HEVC_NAL_RSV_NVCL42
1147 "RSV_NVCL43", // HEVC_NAL_RSV_NVCL43
1148 "RSV_NVCL44", // HEVC_NAL_RSV_NVCL44
1149 "RSV_NVCL45", // HEVC_NAL_RSV_NVCL45
1150 "RSV_NVCL46", // HEVC_NAL_RSV_NVCL46
1151 "RSV_NVCL47", // HEVC_NAL_RSV_NVCL47
1152 "UNSPEC48", // HEVC_NAL_UNSPEC48
1153 "UNSPEC49", // HEVC_NAL_UNSPEC49
1154 "UNSPEC50", // HEVC_NAL_UNSPEC50
1155 "UNSPEC51", // HEVC_NAL_UNSPEC51
1156 "UNSPEC52", // HEVC_NAL_UNSPEC52
1157 "UNSPEC53", // HEVC_NAL_UNSPEC53
1158 "UNSPEC54", // HEVC_NAL_UNSPEC54
1159 "UNSPEC55", // HEVC_NAL_UNSPEC55
1160 "UNSPEC56", // HEVC_NAL_UNSPEC56
1161 "UNSPEC57", // HEVC_NAL_UNSPEC57
1162 "UNSPEC58", // HEVC_NAL_UNSPEC58
1163 "UNSPEC59", // HEVC_NAL_UNSPEC59
1164 "UNSPEC60", // HEVC_NAL_UNSPEC60
1165 "UNSPEC61", // HEVC_NAL_UNSPEC61
1166 "UNSPEC62", // HEVC_NAL_UNSPEC62
1167 "UNSPEC63", // HEVC_NAL_UNSPEC63
1168};
1169
1170static const char *hevc_nal_unit_name(int nal_type)
1171{
1172 return hevc_nal_type_name[nal_type];
1173}
1174
1175/**
1176* Parse NAL units of found picture and decode some basic information.
1177*
1178* @param s parser context.
1179* @param avctx codec context.
1180* @param buf buffer with field/frame data.
1181* @param buf_size size of the buffer.
1182*/
1183static int decode_extradata_ps(u8 *data, int size, struct h265_param_sets *ps)
1184{
1185 int ret = 0;
1186 struct get_bits_context gb;
1187 u32 src_len, rbsp_size = 0;
1188 u8 *rbsp_buf = NULL;
1189 int nalu_pos, nuh_layer_id, temporal_id;
1190 u32 nal_type;
1191 u8 *p = data;
1192 u32 len = size;
1193
1194 nalu_pos = find_start_code(p, len);
1195 if (nalu_pos < 0)
1196 return -1;
1197
1198 src_len = calc_nal_len(p + nalu_pos, size - nalu_pos);
1199 rbsp_buf = nal_unit_extract_rbsp(p + nalu_pos, src_len, &rbsp_size);
1200 if (rbsp_buf == NULL)
1201 return -ENOMEM;
1202
1203 ret = init_get_bits8(&gb, rbsp_buf, rbsp_size);
1204 if (ret < 0)
1205 goto out;
1206
1207 if (get_bits1(&gb) != 0) {
1208 ret = -1;
1209 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "invalid data, return!\n");
1210 goto out;
1211 }
1212
1213 nal_type = get_bits(&gb, 6);
1214 nuh_layer_id = get_bits(&gb, 6);
1215 temporal_id = get_bits(&gb, 3) - 1;
1216 if (temporal_id < 0) {
1217 ret = -1;
1218 goto out;
1219 }
1220
1221 /*pr_info("nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
1222 nal_type, hevc_nal_unit_name(nal_type),
1223 nuh_layer_id, temporal_id);*/
1224
1225 switch (nal_type) {
1226 case HEVC_NAL_VPS:
1227 ret = ff_hevc_parse_vps(&gb, &ps->vps);
1228 if (ret < 0)
1229 goto out;
1230 ps->vps_parsed = true;
1231 break;
1232 case HEVC_NAL_SPS:
1233 ret = ff_hevc_parse_sps(&gb, &ps->sps);
1234 if (ret < 0)
1235 goto out;
1236 ps->sps_parsed = true;
1237 break;
1238 /*case HEVC_NAL_PPS:
1239 ret = ff_hevc_decode_nal_pps(&gb, NULL, ps);
1240 if (ret < 0)
1241 goto out;
1242 ps->pps_parsed = true;
1243 break;*/
1244 default:
1245 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Unsupport parser nal type (%s).\n",
1246 hevc_nal_unit_name(nal_type));
1247 break;
1248 }
1249
1250out:
1251 vfree(rbsp_buf);
1252
1253 return ret;
1254}
1255
1256int h265_decode_extradata_ps(u8 *buf, int size, struct h265_param_sets *ps)
1257{
1258 int ret = 0, i = 0, j = 0;
1259 u8 *p = buf;
1260 int len = size;
1261
1262 for (i = 4; i < size; i++) {
1263 j = find_start_code(p, len);
1264 if (j > 0) {
1265 len = size - (p - buf);
1266 ret = decode_extradata_ps(p, len, ps);
1267 if (ret) {
1268 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "parse extra data failed. err: %d\n", ret);
1269 return ret;
1270 }
1271
1272 if (ps->sps_parsed)
1273 break;
1274
1275 p += j;
1276 }
1277 p++;
1278 }
1279
1280 return ret;
1281}
1282
1283