8564220d5304ccf16fe4b3288d15ddf409483064
[libav.git] / libavcodec / cbs_h265_syntax_template.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
20 {
21 int err;
22 av_unused int one = 1, zero = 0;
23 xu(1, rbsp_stop_one_bit, one, 1, 1);
24 while (byte_alignment(rw) != 0)
25 xu(1, rbsp_alignment_zero_bit, zero, 0, 0);
26
27 return 0;
28 }
29
30 static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
31 H265RawNALUnitHeader *current,
32 int expected_nal_unit_type)
33 {
34 int err;
35
36 u(1, forbidden_zero_bit, 0, 0);
37
38 if (expected_nal_unit_type >= 0)
39 u(6, nal_unit_type, expected_nal_unit_type,
40 expected_nal_unit_type);
41 else
42 u(6, nal_unit_type, 0, 63);
43
44 u(6, nuh_layer_id, 0, 62);
45 u(3, nuh_temporal_id_plus1, 1, 7);
46
47 return 0;
48 }
49
50 static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
51 {
52 int err;
53 av_unused int one = 1, zero = 0;
54 xu(1, alignment_bit_equal_to_one, one, 1, 1);
55 while (byte_alignment(rw) != 0)
56 xu(1, alignment_bit_equal_to_zero, zero, 0, 0);
57
58 return 0;
59 }
60
61 static int FUNC(extension_data)(CodedBitstreamContext *ctx, RWContext *rw,
62 H265RawPSExtensionData *current)
63 {
64 int err;
65 size_t k;
66 #ifdef READ
67 BitstreamContext start;
68 uint8_t bit;
69 start = *rw;
70 for (k = 0; cbs_h2645_read_more_rbsp_data(rw); k++)
71 bitstream_skip(rw, 1);
72 current->bit_length = k;
73 if (k > 0) {
74 *rw = start;
75 allocate(current->data, (current->bit_length + 7) / 8);
76 for (k = 0; k < current->bit_length; k++) {
77 xu(1, extension_data, bit, 0, 1);
78 current->data[k / 8] |= bit << (7 - k % 8);
79 }
80 }
81 #else
82 for (k = 0; k < current->bit_length; k++)
83 xu(1, extension_data, current->data[k / 8] >> (7 - k % 8), 0, 1);
84 #endif
85 return 0;
86 }
87
88 static int FUNC(profile_tier_level)(CodedBitstreamContext *ctx, RWContext *rw,
89 H265RawProfileTierLevel *current,
90 int profile_present_flag,
91 int max_num_sub_layers_minus1)
92 {
93 av_unused unsigned int zero = 0;
94 int err, i, j;
95
96 if (profile_present_flag) {
97 u(2, general_profile_space, 0, 0);
98 flag(general_tier_flag);
99 u(5, general_profile_idc, 0, 31);
100
101 for (j = 0; j < 32; j++)
102 flag(general_profile_compatibility_flag[j]);
103
104 flag(general_progressive_source_flag);
105 flag(general_interlaced_source_flag);
106 flag(general_non_packed_constraint_flag);
107 flag(general_frame_only_constraint_flag);
108
109 #define profile_compatible(x) (current->general_profile_idc == (x) || \
110 current->general_profile_compatibility_flag[x])
111 if (profile_compatible(4) || profile_compatible(5) ||
112 profile_compatible(6) || profile_compatible(7) ||
113 profile_compatible(8) || profile_compatible(9) ||
114 profile_compatible(10)) {
115 flag(general_max_12bit_constraint_flag);
116 flag(general_max_10bit_constraint_flag);
117 flag(general_max_8bit_constraint_flag);
118 flag(general_max_422chroma_constraint_flag);
119 flag(general_max_420chroma_constraint_flag);
120 flag(general_max_monochrome_constraint_flag);
121 flag(general_intra_constraint_flag);
122 flag(general_one_picture_only_constraint_flag);
123 flag(general_lower_bit_rate_constraint_flag);
124
125 if (profile_compatible(5) || profile_compatible(9) ||
126 profile_compatible(10)) {
127 flag(general_max_14bit_constraint_flag);
128 xu(24, general_reserved_zero_33bits, zero, 0, 0);
129 xu(9, general_reserved_zero_33bits, zero, 0, 0);
130 } else {
131 xu(24, general_reserved_zero_34bits, zero, 0, 0);
132 xu(10, general_reserved_zero_34bits, zero, 0, 0);
133 }
134 } else {
135 xu(24, general_reserved_zero_43bits, zero, 0, 0);
136 xu(19, general_reserved_zero_43bits, zero, 0, 0);
137 }
138
139 if (profile_compatible(1) || profile_compatible(2) ||
140 profile_compatible(3) || profile_compatible(4) ||
141 profile_compatible(5) || profile_compatible(9)) {
142 flag(general_inbld_flag);
143 } else {
144 xu(1, general_reserved_zero_bit, zero, 0, 0);
145 }
146 #undef profile_compatible
147 }
148
149 u(8, general_level_idc, 0, 255);
150
151 for (i = 0; i < max_num_sub_layers_minus1; i++) {
152 flag(sub_layer_profile_present_flag[i]);
153 flag(sub_layer_level_present_flag[i]);
154 }
155
156 if (max_num_sub_layers_minus1 > 0) {
157 for (i = max_num_sub_layers_minus1; i < 8; i++) {
158 av_unused int zero = 0;
159 xu(2, reserved_zero_2bits, zero, 0, 0);
160 }
161 }
162
163 for (i = 0; i < max_num_sub_layers_minus1; i++) {
164 if (current->sub_layer_profile_present_flag[i])
165 return AVERROR_PATCHWELCOME;
166 if (current->sub_layer_level_present_flag[i])
167 return AVERROR_PATCHWELCOME;
168 }
169
170 return 0;
171 }
172
173 static int FUNC(sub_layer_hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
174 H265RawHRDParameters *hrd,
175 int nal, int sub_layer_id)
176 {
177 H265RawSubLayerHRDParameters *current;
178 int err, i;
179
180 if (nal)
181 current = &hrd->nal_sub_layer_hrd_parameters[sub_layer_id];
182 else
183 current = &hrd->vcl_sub_layer_hrd_parameters[sub_layer_id];
184
185 for (i = 0; i <= hrd->cpb_cnt_minus1[sub_layer_id]; i++) {
186 ue(bit_rate_value_minus1[i], 0, UINT32_MAX - 1);
187 ue(cpb_size_value_minus1[i], 0, UINT32_MAX - 1);
188 if (hrd->sub_pic_hrd_params_present_flag) {
189 ue(cpb_size_du_value_minus1[i], 0, UINT32_MAX - 1);
190 ue(bit_rate_du_value_minus1[i], 0, UINT32_MAX - 1);
191 }
192 flag(cbr_flag[i]);
193 }
194
195 return 0;
196 }
197
198 static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
199 H265RawHRDParameters *current, int common_inf_present_flag,
200 int max_num_sub_layers_minus1)
201 {
202 int err, i;
203
204 if (common_inf_present_flag) {
205 flag(nal_hrd_parameters_present_flag);
206 flag(vcl_hrd_parameters_present_flag);
207
208 if (current->nal_hrd_parameters_present_flag ||
209 current->vcl_hrd_parameters_present_flag) {
210 flag(sub_pic_hrd_params_present_flag);
211 if (current->sub_pic_hrd_params_present_flag) {
212 u(8, tick_divisor_minus2, 0, 255);
213 u(5, du_cpb_removal_delay_increment_length_minus1, 0, 31);
214 flag(sub_pic_cpb_params_in_pic_timing_sei_flag);
215 u(5, dpb_output_delay_du_length_minus1, 0, 31);
216 }
217
218 u(4, bit_rate_scale, 0, 15);
219 u(4, cpb_size_scale, 0, 15);
220 if (current->sub_pic_hrd_params_present_flag)
221 u(4, cpb_size_du_scale, 0, 15);
222
223 u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
224 u(5, au_cpb_removal_delay_length_minus1, 0, 31);
225 u(5, dpb_output_delay_length_minus1, 0, 31);
226 } else {
227 infer(sub_pic_hrd_params_present_flag, 0);
228
229 infer(initial_cpb_removal_delay_length_minus1, 23);
230 infer(au_cpb_removal_delay_length_minus1, 23);
231 infer(dpb_output_delay_length_minus1, 23);
232 }
233 }
234
235 for (i = 0; i <= max_num_sub_layers_minus1; i++) {
236 flag(fixed_pic_rate_general_flag[i]);
237
238 if (!current->fixed_pic_rate_general_flag[i])
239 flag(fixed_pic_rate_within_cvs_flag[i]);
240 else
241 infer(fixed_pic_rate_within_cvs_flag[i], 1);
242
243 if (current->fixed_pic_rate_within_cvs_flag[i]) {
244 ue(elemental_duration_in_tc_minus1[i], 0, 2047);
245 infer(low_delay_hrd_flag[i], 0);
246 } else
247 flag(low_delay_hrd_flag[i]);
248
249 if (!current->low_delay_hrd_flag[i])
250 ue(cpb_cnt_minus1[i], 0, 31);
251 else
252 infer(cpb_cnt_minus1[i], 0);
253
254 if (current->nal_hrd_parameters_present_flag)
255 CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 0, i));
256 if (current->vcl_hrd_parameters_present_flag)
257 CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 1, i));
258 }
259
260 return 0;
261 }
262
263 static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
264 H265RawVUI *current, const H265RawSPS *sps)
265 {
266 int err;
267
268 flag(aspect_ratio_info_present_flag);
269 if (current->aspect_ratio_info_present_flag) {
270 u(8, aspect_ratio_idc, 0, 255);
271 if (current->aspect_ratio_idc == 255) {
272 u(16, sar_width, 0, 65535);
273 u(16, sar_height, 0, 65535);
274 }
275 } else {
276 infer(aspect_ratio_idc, 0);
277 }
278
279 flag(overscan_info_present_flag);
280 if (current->overscan_info_present_flag)
281 flag(overscan_appropriate_flag);
282
283 flag(video_signal_type_present_flag);
284 if (current->video_signal_type_present_flag) {
285 u(3, video_format, 0, 7);
286 flag(video_full_range_flag);
287 flag(colour_description_present_flag);
288 if (current->colour_description_present_flag) {
289 u(8, colour_primaries, 0, 255);
290 u(8, transfer_characteristics, 0, 255);
291 u(8, matrix_coefficients, 0, 255);
292 } else {
293 infer(colour_primaries, 2);
294 infer(transfer_characteristics, 2);
295 infer(matrix_coefficients, 2);
296 }
297 } else {
298 infer(video_format, 5);
299 infer(video_full_range_flag, 0);
300 infer(colour_primaries, 2);
301 infer(transfer_characteristics, 2);
302 infer(matrix_coefficients, 2);
303 }
304
305 flag(chroma_loc_info_present_flag);
306 if (current->chroma_loc_info_present_flag) {
307 ue(chroma_sample_loc_type_top_field, 0, 5);
308 ue(chroma_sample_loc_type_bottom_field, 0, 5);
309 } else {
310 infer(chroma_sample_loc_type_top_field, 0);
311 infer(chroma_sample_loc_type_bottom_field, 0);
312 }
313
314 flag(neutral_chroma_indication_flag);
315 flag(field_seq_flag);
316 flag(frame_field_info_present_flag);
317
318 flag(default_display_window_flag);
319 if (current->default_display_window_flag) {
320 ue(def_disp_win_left_offset, 0, 16384);
321 ue(def_disp_win_right_offset, 0, 16384);
322 ue(def_disp_win_top_offset, 0, 16384);
323 ue(def_disp_win_bottom_offset, 0, 16384);
324 }
325
326 flag(vui_timing_info_present_flag);
327 if (current->vui_timing_info_present_flag) {
328 u(32, vui_num_units_in_tick, 1, UINT32_MAX);
329 u(32, vui_time_scale, 1, UINT32_MAX);
330 flag(vui_poc_proportional_to_timing_flag);
331 if (current->vui_poc_proportional_to_timing_flag)
332 ue(vui_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
333
334 flag(vui_hrd_parameters_present_flag);
335 if (current->vui_hrd_parameters_present_flag) {
336 CHECK(FUNC(hrd_parameters)(ctx, rw, &current->hrd_parameters,
337 1, sps->sps_max_sub_layers_minus1));
338 }
339 }
340
341 flag(bitstream_restriction_flag);
342 if (current->bitstream_restriction_flag) {
343 flag(tiles_fixed_structure_flag);
344 flag(motion_vectors_over_pic_boundaries_flag);
345 flag(restricted_ref_pic_lists_flag);
346 ue(min_spatial_segmentation_idc, 0, 4095);
347 ue(max_bytes_per_pic_denom, 0, 16);
348 ue(max_bits_per_min_cu_denom, 0, 16);
349 ue(log2_max_mv_length_horizontal, 0, 16);
350 ue(log2_max_mv_length_vertical, 0, 16);
351 } else {
352 infer(tiles_fixed_structure_flag, 0);
353 infer(motion_vectors_over_pic_boundaries_flag, 1);
354 infer(min_spatial_segmentation_idc, 0);
355 infer(max_bytes_per_pic_denom, 2);
356 infer(max_bits_per_min_cu_denom, 1);
357 infer(log2_max_mv_length_horizontal, 15);
358 infer(log2_max_mv_length_vertical, 15);
359 }
360
361 return 0;
362 }
363
364 static int FUNC(vps)(CodedBitstreamContext *ctx, RWContext *rw,
365 H265RawVPS *current)
366 {
367 int err, i, j;
368
369 HEADER("Video Parameter Set");
370
371 CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_VPS));
372
373 u(4, vps_video_parameter_set_id, 0, 15);
374
375 flag(vps_base_layer_internal_flag);
376 flag(vps_base_layer_available_flag);
377 u(6, vps_max_layers_minus1, 0, HEVC_MAX_LAYERS - 1);
378 u(3, vps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
379 flag(vps_temporal_id_nesting_flag);
380
381 if (current->vps_max_sub_layers_minus1 == 0 &&
382 current->vps_temporal_id_nesting_flag != 1) {
383 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
384 "vps_temporal_id_nesting_flag must be 1 if "
385 "vps_max_sub_layers_minus1 is 0.\n");
386 return AVERROR_INVALIDDATA;
387 }
388
389 {
390 av_unused uint16_t ffff = 0xffff;
391 xu(16, vps_reserved_0xffff_16bits, ffff, 0xffff, 0xffff);
392 }
393
394 CHECK(FUNC(profile_tier_level)(ctx, rw, &current->profile_tier_level,
395 1, current->vps_max_sub_layers_minus1));
396
397 flag(vps_sub_layer_ordering_info_present_flag);
398 for (i = (current->vps_sub_layer_ordering_info_present_flag ?
399 0 : current->vps_max_sub_layers_minus1);
400 i <= current->vps_max_sub_layers_minus1; i++) {
401 ue(vps_max_dec_pic_buffering_minus1[i], 0, HEVC_MAX_DPB_SIZE - 1);
402 ue(vps_max_num_reorder_pics[i], 0, current->vps_max_dec_pic_buffering_minus1[i]);
403 ue(vps_max_latency_increase_plus1[i], 0, UINT32_MAX - 1);
404 }
405 if (!current->vps_sub_layer_ordering_info_present_flag) {
406 for (i = 0; i < current->vps_max_sub_layers_minus1; i++) {
407 infer(vps_max_dec_pic_buffering_minus1[i],
408 current->vps_max_dec_pic_buffering_minus1[current->vps_max_sub_layers_minus1]);
409 infer(vps_max_num_reorder_pics[i],
410 current->vps_max_num_reorder_pics[current->vps_max_sub_layers_minus1]);
411 infer(vps_max_latency_increase_plus1[i],
412 current->vps_max_latency_increase_plus1[current->vps_max_sub_layers_minus1]);
413 }
414 }
415
416 u(6, vps_max_layer_id, 0, HEVC_MAX_LAYERS - 1);
417 ue(vps_num_layer_sets_minus1, 0, HEVC_MAX_LAYER_SETS - 1);
418 for (i = 1; i <= current->vps_num_layer_sets_minus1; i++) {
419 for (j = 0; j <= current->vps_max_layer_id; j++)
420 flag(layer_id_included_flag[i][j]);
421 }
422 for (j = 0; j <= current->vps_max_layer_id; j++)
423 infer(layer_id_included_flag[0][j], j == 0);
424
425 flag(vps_timing_info_present_flag);
426 if (current->vps_timing_info_present_flag) {
427 u(32, vps_num_units_in_tick, 1, UINT32_MAX);
428 u(32, vps_time_scale, 1, UINT32_MAX);
429 flag(vps_poc_proportional_to_timing_flag);
430 if (current->vps_poc_proportional_to_timing_flag)
431 ue(vps_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
432 ue(vps_num_hrd_parameters, 0, current->vps_num_layer_sets_minus1 + 1);
433 for (i = 0; i < current->vps_num_hrd_parameters; i++) {
434 ue(hrd_layer_set_idx[i],
435 current->vps_base_layer_internal_flag ? 0 : 1,
436 current->vps_num_layer_sets_minus1);
437 if (i > 0)
438 flag(cprms_present_flag[i]);
439 else
440 infer(cprms_present_flag[0], 1);
441
442 CHECK(FUNC(hrd_parameters)(ctx, rw, &current->hrd_parameters[i],
443 current->cprms_present_flag[i],
444 current->vps_max_sub_layers_minus1));
445 }
446 }
447
448 flag(vps_extension_flag);
449 if (current->vps_extension_flag)
450 CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
451
452 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
453
454 return 0;
455 }
456
457 static int FUNC(st_ref_pic_set)(CodedBitstreamContext *ctx, RWContext *rw,
458 H265RawSTRefPicSet *current, int st_rps_idx,
459 const H265RawSPS *sps)
460 {
461 int err, i, j;
462
463 if (st_rps_idx != 0)
464 flag(inter_ref_pic_set_prediction_flag);
465 else
466 infer(inter_ref_pic_set_prediction_flag, 0);
467
468 if (current->inter_ref_pic_set_prediction_flag) {
469 unsigned int ref_rps_idx, num_delta_pocs;
470 const H265RawSTRefPicSet *ref;
471 int delta_rps, d_poc;
472 int ref_delta_poc_s0[HEVC_MAX_REFS], ref_delta_poc_s1[HEVC_MAX_REFS];
473 int delta_poc_s0[HEVC_MAX_REFS], delta_poc_s1[HEVC_MAX_REFS];
474 uint8_t used_by_curr_pic_s0[HEVC_MAX_REFS],
475 used_by_curr_pic_s1[HEVC_MAX_REFS];
476
477 if (st_rps_idx == sps->num_short_term_ref_pic_sets)
478 ue(delta_idx_minus1, 0, st_rps_idx - 1);
479 else
480 infer(delta_idx_minus1, 0);
481
482 ref_rps_idx = st_rps_idx - (current->delta_idx_minus1 + 1);
483 ref = &sps->st_ref_pic_set[ref_rps_idx];
484 num_delta_pocs = ref->num_negative_pics + ref->num_positive_pics;
485
486 flag(delta_rps_sign);
487 ue(abs_delta_rps_minus1, 0, INT16_MAX);
488 delta_rps = (1 - 2 * current->delta_rps_sign) *
489 (current->abs_delta_rps_minus1 + 1);
490
491 for (j = 0; j <= num_delta_pocs; j++) {
492 flag(used_by_curr_pic_flag[j]);
493 if (!current->used_by_curr_pic_flag[j])
494 flag(use_delta_flag[j]);
495 else
496 infer(use_delta_flag[j], 1);
497 }
498
499 // Since the stored form of an RPS here is actually the delta-step
500 // form used when inter_ref_pic_set_prediction_flag is not set, we
501 // need to reconstruct that here in order to be able to refer to
502 // the RPS later (which is required for parsing, because we don't
503 // even know what syntax elements appear without it). Therefore,
504 // this code takes the delta-step form of the reference set, turns
505 // it into the delta-array form, applies the prediction process of
506 // 7.4.8, converts the result back to the delta-step form, and
507 // stores that as the current set for future use. Note that the
508 // inferences here mean that writers using prediction will need
509 // to fill in the delta-step values correctly as well - since the
510 // whole RPS prediction process is somewhat overly sophisticated,
511 // this hopefully forms a useful check for them to ensure their
512 // predicted form actually matches what was intended rather than
513 // an onerous additional requirement.
514
515 d_poc = 0;
516 for (i = 0; i < ref->num_negative_pics; i++) {
517 d_poc -= ref->delta_poc_s0_minus1[i] + 1;
518 ref_delta_poc_s0[i] = d_poc;
519 }
520 d_poc = 0;
521 for (i = 0; i < ref->num_positive_pics; i++) {
522 d_poc += ref->delta_poc_s1_minus1[i] + 1;
523 ref_delta_poc_s1[i] = d_poc;
524 }
525
526 i = 0;
527 for (j = ref->num_positive_pics - 1; j >= 0; j--) {
528 d_poc = ref_delta_poc_s1[j] + delta_rps;
529 if (d_poc < 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
530 delta_poc_s0[i] = d_poc;
531 used_by_curr_pic_s0[i++] =
532 current->used_by_curr_pic_flag[ref->num_negative_pics + j];
533 }
534 }
535 if (delta_rps < 0 && current->use_delta_flag[num_delta_pocs]) {
536 delta_poc_s0[i] = delta_rps;
537 used_by_curr_pic_s0[i++] =
538 current->used_by_curr_pic_flag[num_delta_pocs];
539 }
540 for (j = 0; j < ref->num_negative_pics; j++) {
541 d_poc = ref_delta_poc_s0[j] + delta_rps;
542 if (d_poc < 0 && current->use_delta_flag[j]) {
543 delta_poc_s0[i] = d_poc;
544 used_by_curr_pic_s0[i++] = current->used_by_curr_pic_flag[j];
545 }
546 }
547
548 infer(num_negative_pics, i);
549 for (i = 0; i < current->num_negative_pics; i++) {
550 infer(delta_poc_s0_minus1[i],
551 -(delta_poc_s0[i] - (i == 0 ? 0 : delta_poc_s0[i - 1])) - 1);
552 infer(used_by_curr_pic_s0_flag[i], used_by_curr_pic_s0[i]);
553 }
554
555 i = 0;
556 for (j = ref->num_negative_pics - 1; j >= 0; j--) {
557 d_poc = ref_delta_poc_s0[j] + delta_rps;
558 if (d_poc > 0 && current->use_delta_flag[j]) {
559 delta_poc_s1[i] = d_poc;
560 used_by_curr_pic_s1[i++] = current->used_by_curr_pic_flag[j];
561 }
562 }
563 if (delta_rps > 0 && current->use_delta_flag[num_delta_pocs]) {
564 delta_poc_s1[i] = delta_rps;
565 used_by_curr_pic_s1[i++] =
566 current->used_by_curr_pic_flag[num_delta_pocs];
567 }
568 for (j = 0; j < ref->num_positive_pics; j++) {
569 d_poc = ref_delta_poc_s1[j] + delta_rps;
570 if (d_poc > 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
571 delta_poc_s1[i] = d_poc;
572 used_by_curr_pic_s1[i++] =
573 current->used_by_curr_pic_flag[ref->num_negative_pics + j];
574 }
575 }
576
577 infer(num_positive_pics, i);
578 for (i = 0; i < current->num_positive_pics; i++) {
579 infer(delta_poc_s1_minus1[i],
580 delta_poc_s1[i] - (i == 0 ? 0 : delta_poc_s1[i - 1]) - 1);
581 infer(used_by_curr_pic_s1_flag[i], used_by_curr_pic_s1[i]);
582 }
583
584 } else {
585 ue(num_negative_pics, 0, 15);
586 ue(num_positive_pics, 0, 15 - current->num_negative_pics);
587
588 for (i = 0; i < current->num_negative_pics; i++) {
589 ue(delta_poc_s0_minus1[i], 0, INT16_MAX);
590 flag(used_by_curr_pic_s0_flag[i]);
591 }
592
593 for (i = 0; i < current->num_positive_pics; i++) {
594 ue(delta_poc_s1_minus1[i], 0, INT16_MAX);
595 flag(used_by_curr_pic_s1_flag[i]);
596 }
597 }
598
599 return 0;
600 }
601
602 static int FUNC(scaling_list_data)(CodedBitstreamContext *ctx, RWContext *rw,
603 H265RawScalingList *current)
604 {
605 int sizeId, matrixId;
606 int err, n, i;
607
608 for (sizeId = 0; sizeId < 4; sizeId++) {
609 for (matrixId = 0; matrixId < 6; matrixId += (sizeId == 3 ? 3 : 1)) {
610 flag(scaling_list_pred_mode_flag[sizeId][matrixId]);
611 if (!current->scaling_list_pred_mode_flag[sizeId][matrixId]) {
612 ue(scaling_list_pred_matrix_id_delta[sizeId][matrixId],
613 0, sizeId == 3 ? matrixId / 3 : matrixId);
614 } else {
615 n = FFMIN(64, 1 << (4 + (sizeId << 1)));
616 if (sizeId > 1)
617 se(scaling_list_dc_coef_minus8[sizeId - 2][matrixId], -7, +247);
618 for (i = 0; i < n; i++) {
619 xse(scaling_list_delta_coeff,
620 current->scaling_list_delta_coeff[sizeId][matrixId][i],
621 -128, +127);
622 }
623 }
624 }
625 }
626
627 return 0;
628 }
629
630 static int FUNC(sps_range_extension)(CodedBitstreamContext *ctx, RWContext *rw,
631 H265RawSPS *current)
632 {
633 int err;
634
635 flag(transform_skip_rotation_enabled_flag);
636 flag(transform_skip_context_enabled_flag);
637 flag(implicit_rdpcm_enabled_flag);
638 flag(explicit_rdpcm_enabled_flag);
639 flag(extended_precision_processing_flag);
640 flag(intra_smoothing_disabled_flag);
641 flag(high_precision_offsets_enabled_flag);
642 flag(persistent_rice_adaptation_enabled_flag);
643 flag(cabac_bypass_alignment_enabled_flag);
644
645 return 0;
646 }
647
648 static int FUNC(sps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
649 H265RawSPS *current)
650 {
651 int err, comp, i;
652
653 flag(sps_curr_pic_ref_enabled_flag);
654
655 flag(palette_mode_enabled_flag);
656 if (current->palette_mode_enabled_flag) {
657 ue(palette_max_size, 0, 64);
658 ue(delta_palette_max_predictor_size, 0, 128);
659
660 flag(sps_palette_predictor_initializer_present_flag);
661 if (current->sps_palette_predictor_initializer_present_flag) {
662 ue(sps_num_palette_predictor_initializer_minus1, 0, 128);
663 for (comp = 0; comp < (current->chroma_format_idc ? 3 : 1); comp++) {
664 int bit_depth = comp == 0 ? current->bit_depth_luma_minus8 + 8
665 : current->bit_depth_chroma_minus8 + 8;
666 for (i = 0; i <= current->sps_num_palette_predictor_initializer_minus1; i++)
667 u(bit_depth, sps_palette_predictor_initializers[comp][i],
668 0, (1 << bit_depth) - 1);
669 }
670 }
671 }
672
673 u(2, motion_vector_resolution_control_idc, 0, 2);
674 flag(intra_boundary_filtering_disable_flag);
675
676 return 0;
677 }
678
679 static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
680 H265RawSPS *current)
681 {
682 CodedBitstreamH265Context *h265 = ctx->priv_data;
683 const H265RawVPS *vps;
684 int err, i;
685 unsigned int min_cb_log2_size_y, ctb_log2_size_y,
686 min_cb_size_y, min_tb_log2_size_y;
687
688 HEADER("Sequence Parameter Set");
689
690 CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_SPS));
691
692 u(4, sps_video_parameter_set_id, 0, 15);
693 h265->active_vps = vps = h265->vps[current->sps_video_parameter_set_id];
694
695 u(3, sps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
696 flag(sps_temporal_id_nesting_flag);
697 if (vps) {
698 if (vps->vps_max_sub_layers_minus1 > current->sps_max_sub_layers_minus1) {
699 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
700 "sps_max_sub_layers_minus1 (%d) must be less than or equal to "
701 "vps_max_sub_layers_minus1 (%d).\n",
702 vps->vps_max_sub_layers_minus1,
703 current->sps_max_sub_layers_minus1);
704 return AVERROR_INVALIDDATA;
705 }
706 if (vps->vps_temporal_id_nesting_flag &&
707 !current->sps_temporal_id_nesting_flag) {
708 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
709 "sps_temporal_id_nesting_flag must be 1 if "
710 "vps_temporal_id_nesting_flag is 1.\n");
711 return AVERROR_INVALIDDATA;
712 }
713 }
714
715 CHECK(FUNC(profile_tier_level)(ctx, rw, &current->profile_tier_level,
716 1, current->sps_max_sub_layers_minus1));
717
718 ue(sps_seq_parameter_set_id, 0, 15);
719
720 ue(chroma_format_idc, 0, 3);
721 if (current->chroma_format_idc == 3)
722 flag(separate_colour_plane_flag);
723 else
724 infer(separate_colour_plane_flag, 0);
725
726 ue(pic_width_in_luma_samples, 1, HEVC_MAX_WIDTH);
727 ue(pic_height_in_luma_samples, 1, HEVC_MAX_HEIGHT);
728
729 flag(conformance_window_flag);
730 if (current->conformance_window_flag) {
731 ue(conf_win_left_offset, 0, current->pic_width_in_luma_samples);
732 ue(conf_win_right_offset, 0, current->pic_width_in_luma_samples);
733 ue(conf_win_top_offset, 0, current->pic_height_in_luma_samples);
734 ue(conf_win_bottom_offset, 0, current->pic_height_in_luma_samples);
735 } else {
736 infer(conf_win_left_offset, 0);
737 infer(conf_win_right_offset, 0);
738 infer(conf_win_top_offset, 0);
739 infer(conf_win_bottom_offset, 0);
740 }
741
742 ue(bit_depth_luma_minus8, 0, 8);
743 ue(bit_depth_chroma_minus8, 0, 8);
744
745 ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12);
746
747 flag(sps_sub_layer_ordering_info_present_flag);
748 for (i = (current->sps_sub_layer_ordering_info_present_flag ?
749 0 : current->sps_max_sub_layers_minus1);
750 i <= current->sps_max_sub_layers_minus1; i++) {
751 ue(sps_max_dec_pic_buffering_minus1[i], 0, HEVC_MAX_DPB_SIZE - 1);
752 ue(sps_max_num_reorder_pics[i], 0, current->sps_max_dec_pic_buffering_minus1[i]);
753 ue(sps_max_latency_increase_plus1[i], 0, UINT32_MAX - 1);
754 }
755 if (!current->sps_sub_layer_ordering_info_present_flag) {
756 for (i = 0; i < current->sps_max_sub_layers_minus1; i++) {
757 infer(sps_max_dec_pic_buffering_minus1[i],
758 current->sps_max_dec_pic_buffering_minus1[current->sps_max_sub_layers_minus1]);
759 infer(sps_max_num_reorder_pics[i],
760 current->sps_max_num_reorder_pics[current->sps_max_sub_layers_minus1]);
761 infer(sps_max_latency_increase_plus1[i],
762 current->sps_max_latency_increase_plus1[current->sps_max_sub_layers_minus1]);
763 }
764 }
765
766 ue(log2_min_luma_coding_block_size_minus3, 0, 3);
767 min_cb_log2_size_y = current->log2_min_luma_coding_block_size_minus3 + 3;
768
769 ue(log2_diff_max_min_luma_coding_block_size, 0, 3);
770 ctb_log2_size_y = min_cb_log2_size_y +
771 current->log2_diff_max_min_luma_coding_block_size;
772
773 min_cb_size_y = 1 << min_cb_log2_size_y;
774 if (current->pic_width_in_luma_samples % min_cb_size_y ||
775 current->pic_height_in_luma_samples % min_cb_size_y) {
776 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid dimensions: %ux%u not divisible "
777 "by MinCbSizeY = %u.\n", current->pic_width_in_luma_samples,
778 current->pic_height_in_luma_samples, min_cb_size_y);
779 return AVERROR_INVALIDDATA;
780 }
781
782 ue(log2_min_luma_transform_block_size_minus2, 0, min_cb_log2_size_y - 3);
783 min_tb_log2_size_y = current->log2_min_luma_transform_block_size_minus2 + 2;
784
785 ue(log2_diff_max_min_luma_transform_block_size,
786 0, FFMIN(ctb_log2_size_y, 5) - min_tb_log2_size_y);
787
788 ue(max_transform_hierarchy_depth_inter,
789 0, ctb_log2_size_y - min_tb_log2_size_y);
790 ue(max_transform_hierarchy_depth_intra,
791 0, ctb_log2_size_y - min_tb_log2_size_y);
792
793 flag(scaling_list_enabled_flag);
794 if (current->scaling_list_enabled_flag) {
795 flag(sps_scaling_list_data_present_flag);
796 if (current->sps_scaling_list_data_present_flag)
797 CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
798 } else {
799 infer(sps_scaling_list_data_present_flag, 0);
800 }
801
802 flag(amp_enabled_flag);
803 flag(sample_adaptive_offset_enabled_flag);
804
805 flag(pcm_enabled_flag);
806 if (current->pcm_enabled_flag) {
807 u(4, pcm_sample_bit_depth_luma_minus1,
808 0, current->bit_depth_luma_minus8 + 8 - 1);
809 u(4, pcm_sample_bit_depth_chroma_minus1,
810 0, current->bit_depth_chroma_minus8 + 8 - 1);
811
812 ue(log2_min_pcm_luma_coding_block_size_minus3,
813 FFMIN(min_cb_log2_size_y, 5) - 3, FFMIN(ctb_log2_size_y, 5) - 3);
814 ue(log2_diff_max_min_pcm_luma_coding_block_size,
815 0, FFMIN(ctb_log2_size_y, 5) - (current->log2_min_pcm_luma_coding_block_size_minus3 + 3));
816
817 flag(pcm_loop_filter_disabled_flag);
818 }
819
820 ue(num_short_term_ref_pic_sets, 0, HEVC_MAX_SHORT_TERM_REF_PIC_SETS);
821 for (i = 0; i < current->num_short_term_ref_pic_sets; i++)
822 CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->st_ref_pic_set[i], i, current));
823
824 flag(long_term_ref_pics_present_flag);
825 if (current->long_term_ref_pics_present_flag) {
826 ue(num_long_term_ref_pics_sps, 0, HEVC_MAX_LONG_TERM_REF_PICS);
827 for (i = 0; i < current->num_long_term_ref_pics_sps; i++) {
828 u(current->log2_max_pic_order_cnt_lsb_minus4 + 4,
829 lt_ref_pic_poc_lsb_sps[i],
830 0, (1 << (current->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1);
831 flag(used_by_curr_pic_lt_sps_flag[i]);
832 }
833 }
834
835 flag(sps_temporal_mvp_enabled_flag);
836 flag(strong_intra_smoothing_enabled_flag);
837
838 flag(vui_parameters_present_flag);
839 if (current->vui_parameters_present_flag)
840 CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
841
842 flag(sps_extension_present_flag);
843 if (current->sps_extension_present_flag) {
844 flag(sps_range_extension_flag);
845 flag(sps_multilayer_extension_flag);
846 flag(sps_3d_extension_flag);
847 flag(sps_scc_extension_flag);
848 u(4, sps_extension_4bits, 0, (1 << 4) - 1);
849 }
850
851 if (current->sps_range_extension_flag)
852 CHECK(FUNC(sps_range_extension)(ctx, rw, current));
853 if (current->sps_multilayer_extension_flag)
854 return AVERROR_PATCHWELCOME;
855 if (current->sps_3d_extension_flag)
856 return AVERROR_PATCHWELCOME;
857 if (current->sps_scc_extension_flag)
858 CHECK(FUNC(sps_scc_extension)(ctx, rw, current));
859 if (current->sps_extension_4bits)
860 CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
861
862 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
863
864 return 0;
865 }
866
867 static int FUNC(pps_range_extension)(CodedBitstreamContext *ctx, RWContext *rw,
868 H265RawPPS *current)
869 {
870 CodedBitstreamH265Context *h265 = ctx->priv_data;
871 const H265RawSPS *sps = h265->active_sps;
872 int err, i;
873
874 if (current->transform_skip_enabled_flag)
875 ue(log2_max_transform_skip_block_size_minus2, 0, 4);
876 flag(cross_component_prediction_enabled_flag);
877
878 flag(chroma_qp_offset_list_enabled_flag);
879 if (current->chroma_qp_offset_list_enabled_flag) {
880 ue(diff_cu_chroma_qp_offset_depth,
881 0, sps->log2_diff_max_min_luma_coding_block_size);
882 ue(chroma_qp_offset_list_len_minus1, 0, 5);
883 for (i = 0; i <= current->chroma_qp_offset_list_len_minus1; i++) {
884 se(cb_qp_offset_list[i], -12, +12);
885 se(cr_qp_offset_list[i], -12, +12);
886 }
887 }
888
889 ue(log2_sao_offset_scale_luma, 0, FFMAX(0, sps->bit_depth_luma_minus8 - 2));
890 ue(log2_sao_offset_scale_chroma, 0, FFMAX(0, sps->bit_depth_chroma_minus8 - 2));
891
892 return 0;
893 }
894
895 static int FUNC(pps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
896 H265RawPPS *current)
897 {
898 int err, comp, i;
899
900 flag(pps_curr_pic_ref_enabled_flag);
901
902 flag(residual_adaptive_colour_transform_enabled_flag);
903 if (current->residual_adaptive_colour_transform_enabled_flag) {
904 flag(pps_slice_act_qp_offsets_present_flag);
905 se(pps_act_y_qp_offset_plus5, -7, +17);
906 se(pps_act_cb_qp_offset_plus5, -7, +17);
907 se(pps_act_cr_qp_offset_plus3, -9, +15);
908 } else {
909 infer(pps_slice_act_qp_offsets_present_flag, 0);
910 infer(pps_act_y_qp_offset_plus5, 0);
911 infer(pps_act_cb_qp_offset_plus5, 0);
912 infer(pps_act_cr_qp_offset_plus3, 0);
913 }
914
915 flag(pps_palette_predictor_initializer_present_flag);
916 if (current->pps_palette_predictor_initializer_present_flag) {
917 ue(pps_num_palette_predictor_initializer, 0, 128);
918 if (current->pps_num_palette_predictor_initializer > 0) {
919 flag(monochrome_palette_flag);
920 ue(luma_bit_depth_entry_minus8, 0, 8);
921 if (!current->monochrome_palette_flag)
922 ue(chroma_bit_depth_entry_minus8, 0, 8);
923 for (comp = 0; comp < (current->monochrome_palette_flag ? 1 : 3); comp++) {
924 int bit_depth = comp == 0 ? current->luma_bit_depth_entry_minus8 + 8
925 : current->chroma_bit_depth_entry_minus8 + 8;
926 for (i = 0; i < current->pps_num_palette_predictor_initializer; i++)
927 u(bit_depth, pps_palette_predictor_initializers[comp][i],
928 0, (1 << bit_depth) - 1);
929 }
930 }
931 }
932
933 return 0;
934 }
935
936 static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
937 H265RawPPS *current)
938 {
939 CodedBitstreamH265Context *h265 = ctx->priv_data;
940 const H265RawSPS *sps;
941 int err, i;
942
943 HEADER("Picture Parameter Set");
944
945 CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_PPS));
946
947 ue(pps_pic_parameter_set_id, 0, 63);
948 ue(pps_seq_parameter_set_id, 0, 15);
949 sps = h265->sps[current->pps_seq_parameter_set_id];
950 if (!sps) {
951 av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
952 current->pps_seq_parameter_set_id);
953 return AVERROR_INVALIDDATA;
954 }
955 h265->active_sps = sps;
956
957 flag(dependent_slice_segments_enabled_flag);
958 flag(output_flag_present_flag);
959 u(3, num_extra_slice_header_bits, 0, 7);
960 flag(sign_data_hiding_enabled_flag);
961 flag(cabac_init_present_flag);
962
963 ue(num_ref_idx_l0_default_active_minus1, 0, 14);
964 ue(num_ref_idx_l1_default_active_minus1, 0, 14);
965
966 se(init_qp_minus26, -(26 + 6 * sps->bit_depth_luma_minus8), +25);
967
968 flag(constrained_intra_pred_flag);
969 flag(transform_skip_enabled_flag);
970 flag(cu_qp_delta_enabled_flag);
971 if (current->cu_qp_delta_enabled_flag)
972 ue(diff_cu_qp_delta_depth,
973 0, sps->log2_diff_max_min_luma_coding_block_size);
974 else
975 infer(diff_cu_qp_delta_depth, 0);
976
977 se(pps_cb_qp_offset, -12, +12);
978 se(pps_cr_qp_offset, -12, +12);
979 flag(pps_slice_chroma_qp_offsets_present_flag);
980
981 flag(weighted_pred_flag);
982 flag(weighted_bipred_flag);
983
984 flag(transquant_bypass_enabled_flag);
985 flag(tiles_enabled_flag);
986 flag(entropy_coding_sync_enabled_flag);
987
988 if (current->tiles_enabled_flag) {
989 ue(num_tile_columns_minus1, 0, HEVC_MAX_TILE_COLUMNS);
990 ue(num_tile_rows_minus1, 0, HEVC_MAX_TILE_ROWS);
991 flag(uniform_spacing_flag);
992 if (!current->uniform_spacing_flag) {
993 for (i = 0; i < current->num_tile_columns_minus1; i++)
994 ue(column_width_minus1[i], 0, sps->pic_width_in_luma_samples);
995 for (i = 0; i < current->num_tile_rows_minus1; i++)
996 ue(row_height_minus1[i], 0, sps->pic_height_in_luma_samples);
997 }
998 flag(loop_filter_across_tiles_enabled_flag);
999 } else {
1000 infer(num_tile_columns_minus1, 0);
1001 infer(num_tile_rows_minus1, 0);
1002 }
1003
1004 flag(pps_loop_filter_across_slices_enabled_flag);
1005 flag(deblocking_filter_control_present_flag);
1006 if (current->deblocking_filter_control_present_flag) {
1007 flag(deblocking_filter_override_enabled_flag);
1008 flag(pps_deblocking_filter_disabled_flag);
1009 if (!current->pps_deblocking_filter_disabled_flag) {
1010 se(pps_beta_offset_div2, -6, +6);
1011 se(pps_tc_offset_div2, -6, +6);
1012 } else {
1013 infer(pps_beta_offset_div2, 0);
1014 infer(pps_tc_offset_div2, 0);
1015 }
1016 } else {
1017 infer(deblocking_filter_override_enabled_flag, 0);
1018 infer(pps_deblocking_filter_disabled_flag, 0);
1019 infer(pps_beta_offset_div2, 0);
1020 infer(pps_tc_offset_div2, 0);
1021 }
1022
1023 flag(pps_scaling_list_data_present_flag);
1024 if (current->pps_scaling_list_data_present_flag)
1025 CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
1026
1027 flag(lists_modification_present_flag);
1028
1029 ue(log2_parallel_merge_level_minus2,
1030 0, (sps->log2_min_luma_coding_block_size_minus3 + 3 +
1031 sps->log2_diff_max_min_luma_coding_block_size - 2));
1032
1033 flag(slice_segment_header_extension_present_flag);
1034
1035 flag(pps_extension_present_flag);
1036 if (current->pps_extension_present_flag) {
1037 flag(pps_range_extension_flag);
1038 flag(pps_multilayer_extension_flag);
1039 flag(pps_3d_extension_flag);
1040 flag(pps_scc_extension_flag);
1041 u(4, pps_extension_4bits, 0, (1 << 4) - 1);
1042 }
1043 if (current->pps_range_extension_flag)
1044 CHECK(FUNC(pps_range_extension)(ctx, rw, current));
1045 if (current->pps_multilayer_extension_flag)
1046 return AVERROR_PATCHWELCOME;
1047 if (current->pps_3d_extension_flag)
1048 return AVERROR_PATCHWELCOME;
1049 if (current->pps_scc_extension_flag)
1050 CHECK(FUNC(pps_scc_extension)(ctx, rw, current));
1051 if (current->pps_extension_4bits)
1052 CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
1053
1054 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
1055
1056 return 0;
1057 }
1058
1059 static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
1060 H265RawAUD *current)
1061 {
1062 int err;
1063
1064 HEADER("Access Unit Delimiter");
1065
1066 CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_AUD));
1067
1068 u(3, pic_type, 0, 2);
1069
1070 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
1071
1072 return 0;
1073 }
1074
1075 static int FUNC(ref_pic_lists_modification)(CodedBitstreamContext *ctx, RWContext *rw,
1076 H265RawSliceHeader *current,
1077 unsigned int num_pic_total_curr)
1078 {
1079 unsigned int entry_size;
1080 int err, i;
1081
1082 entry_size = av_log2(num_pic_total_curr - 1) + 1;
1083
1084 flag(ref_pic_list_modification_flag_l0);
1085 if (current->ref_pic_list_modification_flag_l0) {
1086 for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++)
1087 u(entry_size, list_entry_l0[i], 0, num_pic_total_curr - 1);
1088 }
1089
1090 if (current->slice_type == HEVC_SLICE_B) {
1091 flag(ref_pic_list_modification_flag_l1);
1092 if (current->ref_pic_list_modification_flag_l1) {
1093 for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++)
1094 u(entry_size, list_entry_l1[i], 0, num_pic_total_curr - 1);
1095 }
1096 }
1097
1098 return 0;
1099 }
1100
1101 static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
1102 H265RawSliceHeader *current)
1103 {
1104 CodedBitstreamH265Context *h265 = ctx->priv_data;
1105 const H265RawSPS *sps = h265->active_sps;
1106 int err, i, j;
1107 int chroma = !sps->separate_colour_plane_flag &&
1108 sps->chroma_format_idc != 0;
1109
1110 ue(luma_log2_weight_denom, 0, 7);
1111 if (chroma)
1112 se(delta_chroma_log2_weight_denom, -7, 7);
1113 else
1114 infer(delta_chroma_log2_weight_denom, 0);
1115
1116 for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
1117 if (1 /* is not same POC and same layer_id */)
1118 flag(luma_weight_l0_flag[i]);
1119 else
1120 infer(luma_weight_l0_flag[i], 0);
1121 }
1122 if (chroma) {
1123 for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
1124 if (1 /* is not same POC and same layer_id */)
1125 flag(chroma_weight_l0_flag[i]);
1126 else
1127 infer(chroma_weight_l0_flag[i], 0);
1128 }
1129 }
1130
1131 for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
1132 if (current->luma_weight_l0_flag[i]) {
1133 se(delta_luma_weight_l0[i], -128, +127);
1134 se(luma_offset_l0[i],
1135 -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
1136 +(1 << (sps->bit_depth_luma_minus8 + 8 - 1) - 1));
1137 } else {
1138 infer(delta_luma_weight_l0[i], 0);
1139 infer(luma_offset_l0[i], 0);
1140 }
1141 if (current->chroma_weight_l0_flag[i]) {
1142 for (j = 0; j < 2; j++) {
1143 se(delta_chroma_weight_l0[i][j], -128, +127);
1144 se(chroma_offset_l0[i][j],
1145 -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
1146 +(4 << (sps->bit_depth_chroma_minus8 + 8 - 1) - 1));
1147 }
1148 } else {
1149 for (j = 0; j < 2; j++) {
1150 infer(delta_chroma_weight_l0[i][j], 0);
1151 infer(chroma_offset_l0[i][j], 0);
1152 }
1153 }
1154 }
1155
1156 if (current->slice_type == HEVC_SLICE_B) {
1157 for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
1158 if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
1159 flag(luma_weight_l1_flag[i]);
1160 else
1161 infer(luma_weight_l1_flag[i], 0);
1162 }
1163 if (chroma) {
1164 for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
1165 if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
1166 flag(chroma_weight_l1_flag[i]);
1167 else
1168 infer(chroma_weight_l1_flag[i], 0);
1169 }
1170 }
1171
1172 for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
1173 if (current->luma_weight_l1_flag[i]) {
1174 se(delta_luma_weight_l1[i], -128, +127);
1175 se(luma_offset_l1[i],
1176 - 1 << (sps->bit_depth_luma_minus8 + 8 - 1),
1177 + 1 << (sps->bit_depth_luma_minus8 + 8 - 1) - 1);
1178 } else {
1179 infer(delta_luma_weight_l1[i], 0);
1180 infer(luma_offset_l1[i], 0);
1181 }
1182 if (current->chroma_weight_l1_flag[i]) {
1183 for (j = 0; j < 2; j++) {
1184 se(delta_chroma_weight_l1[i][j], -128, +127);
1185 se(chroma_offset_l1[i][j],
1186 - 4 << (sps->bit_depth_chroma_minus8 + 8 - 1),
1187 + 4 << (sps->bit_depth_chroma_minus8 + 8 - 1) - 1);
1188 }
1189 } else {
1190 for (j = 0; j < 2; j++) {
1191 infer(delta_chroma_weight_l1[i][j], 0);
1192 infer(chroma_offset_l1[i][j], 0);
1193 }
1194 }
1195 }
1196 }
1197
1198 return 0;
1199 }
1200
1201 static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
1202 H265RawSliceHeader *current)
1203 {
1204 CodedBitstreamH265Context *h265 = ctx->priv_data;
1205 const H265RawSPS *sps;
1206 const H265RawPPS *pps;
1207 unsigned int min_cb_log2_size_y, ctb_log2_size_y, ctb_size_y;
1208 unsigned int pic_width_in_ctbs_y, pic_height_in_ctbs_y, pic_size_in_ctbs_y;
1209 unsigned int num_pic_total_curr = 0;
1210 int err, i;
1211
1212 HEADER("Slice Segment Header");
1213
1214 CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, -1));
1215
1216 flag(first_slice_segment_in_pic_flag);
1217
1218 if (current->nal_unit_header.nal_unit_type >= HEVC_NAL_BLA_W_LP &&
1219 current->nal_unit_header.nal_unit_type <= HEVC_NAL_IRAP_VCL23)
1220 flag(no_output_of_prior_pics_flag);
1221
1222 ue(slice_pic_parameter_set_id, 0, 63);
1223
1224 pps = h265->pps[current->slice_pic_parameter_set_id];
1225 if (!pps) {
1226 av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
1227 current->slice_pic_parameter_set_id);
1228 return AVERROR_INVALIDDATA;
1229 }
1230 h265->active_pps = pps;
1231
1232 sps = h265->sps[pps->pps_seq_parameter_set_id];
1233 if (!sps) {
1234 av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1235 pps->pps_seq_parameter_set_id);
1236 return AVERROR_INVALIDDATA;
1237 }
1238 h265->active_sps = sps;
1239
1240 min_cb_log2_size_y = sps->log2_min_luma_coding_block_size_minus3 + 3;
1241 ctb_log2_size_y = min_cb_log2_size_y + sps->log2_diff_max_min_luma_coding_block_size;
1242 ctb_size_y = 1 << ctb_log2_size_y;
1243 pic_width_in_ctbs_y =
1244 (sps->pic_width_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
1245 pic_height_in_ctbs_y =
1246 (sps->pic_height_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
1247 pic_size_in_ctbs_y = pic_width_in_ctbs_y * pic_height_in_ctbs_y;
1248
1249 if (!current->first_slice_segment_in_pic_flag) {
1250 unsigned int address_size = av_log2(pic_size_in_ctbs_y - 1) + 1;
1251 if (pps->dependent_slice_segments_enabled_flag)
1252 flag(dependent_slice_segment_flag);
1253 else
1254 infer(dependent_slice_segment_flag, 0);
1255 u(address_size, slice_segment_address, 0, pic_size_in_ctbs_y - 1);
1256 } else {
1257 infer(dependent_slice_segment_flag, 0);
1258 }
1259
1260 if (!current->dependent_slice_segment_flag) {
1261 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
1262 flag(slice_reserved_flag[i]);
1263
1264 ue(slice_type, 0, 2);
1265
1266 if (pps->output_flag_present_flag)
1267 flag(pic_output_flag);
1268
1269 if (sps->separate_colour_plane_flag)
1270 u(2, colour_plane_id, 0, 2);
1271
1272 if (current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_W_RADL &&
1273 current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_N_LP) {
1274 const H265RawSTRefPicSet *rps;
1275
1276 u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, slice_pic_order_cnt_lsb,
1277 0, (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1);
1278
1279 flag(short_term_ref_pic_set_sps_flag);
1280 if (!current->short_term_ref_pic_set_sps_flag) {
1281 CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->short_term_ref_pic_set,
1282 sps->num_short_term_ref_pic_sets, sps));
1283 rps = &current->short_term_ref_pic_set;
1284 } else if (sps->num_short_term_ref_pic_sets > 1) {
1285 unsigned int idx_size = av_log2(sps->num_short_term_ref_pic_sets - 1) + 1;
1286 u(idx_size, short_term_ref_pic_set_idx,
1287 0, sps->num_short_term_ref_pic_sets - 1);
1288 rps = &sps->st_ref_pic_set[current->short_term_ref_pic_set_idx];
1289 } else {
1290 infer(short_term_ref_pic_set_idx, 0);
1291 rps = &sps->st_ref_pic_set[0];
1292 }
1293
1294 num_pic_total_curr = 0;
1295 for (i = 0; i < rps->num_negative_pics; i++)
1296 if (rps->used_by_curr_pic_s0_flag[i])
1297 ++num_pic_total_curr;
1298 for (i = 0; i < rps->num_positive_pics; i++)
1299 if (rps->used_by_curr_pic_s1_flag[i])
1300 ++num_pic_total_curr;
1301
1302 if (sps->long_term_ref_pics_present_flag) {
1303 unsigned int idx_size;
1304
1305 if (sps->num_long_term_ref_pics_sps > 0) {
1306 ue(num_long_term_sps, 0, sps->num_long_term_ref_pics_sps);
1307 idx_size = av_log2(sps->num_long_term_ref_pics_sps - 1) + 1;
1308 } else {
1309 infer(num_long_term_sps, 0);
1310 idx_size = 0;
1311 }
1312 ue(num_long_term_pics, 0, HEVC_MAX_LONG_TERM_REF_PICS);
1313
1314 for (i = 0; i < current->num_long_term_sps +
1315 current->num_long_term_pics; i++) {
1316 if (i < current->num_long_term_sps) {
1317 if (sps->num_long_term_ref_pics_sps > 1)
1318 u(idx_size, lt_idx_sps[i],
1319 0, sps->num_long_term_ref_pics_sps - 1);
1320 if (sps->used_by_curr_pic_lt_sps_flag[current->lt_idx_sps[i]])
1321 ++num_pic_total_curr;
1322 } else {
1323 u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, poc_lsb_lt[i],
1324 0, (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1);
1325 flag(used_by_curr_pic_lt_flag[i]);
1326 if (current->used_by_curr_pic_lt_flag[i])
1327 ++num_pic_total_curr;
1328 }
1329 flag(delta_poc_msb_present_flag[i]);
1330 if (current->delta_poc_msb_present_flag[i])
1331 ue(delta_poc_msb_cycle_lt[i], 0, UINT32_MAX - 1);
1332 else
1333 infer(delta_poc_msb_cycle_lt[i], 0);
1334 }
1335 }
1336
1337 if (sps->sps_temporal_mvp_enabled_flag)
1338 flag(slice_temporal_mvp_enabled_flag);
1339 else
1340 infer(slice_temporal_mvp_enabled_flag, 0);
1341
1342 if (pps->pps_curr_pic_ref_enabled_flag)
1343 ++num_pic_total_curr;
1344 }
1345
1346 if (sps->sample_adaptive_offset_enabled_flag) {
1347 flag(slice_sao_luma_flag);
1348 if (!sps->separate_colour_plane_flag && sps->chroma_format_idc != 0)
1349 flag(slice_sao_chroma_flag);
1350 else
1351 infer(slice_sao_chroma_flag, 0);
1352 } else {
1353 infer(slice_sao_luma_flag, 0);
1354 infer(slice_sao_chroma_flag, 0);
1355 }
1356
1357 if (current->slice_type == HEVC_SLICE_P ||
1358 current->slice_type == HEVC_SLICE_B) {
1359 flag(num_ref_idx_active_override_flag);
1360 if (current->num_ref_idx_active_override_flag) {
1361 ue(num_ref_idx_l0_active_minus1, 0, 14);
1362 if (current->slice_type == HEVC_SLICE_B)
1363 ue(num_ref_idx_l1_active_minus1, 0, 14);
1364 else
1365 infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
1366 } else {
1367 infer(num_ref_idx_l0_active_minus1, pps->num_ref_idx_l0_default_active_minus1);
1368 infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
1369 }
1370
1371 if (pps->lists_modification_present_flag && num_pic_total_curr > 1)
1372 CHECK(FUNC(ref_pic_lists_modification)(ctx, rw, current,
1373 num_pic_total_curr));
1374
1375 if (current->slice_type == HEVC_SLICE_B)
1376 flag(mvd_l1_zero_flag);
1377 if (pps->cabac_init_present_flag)
1378 flag(cabac_init_flag);
1379 else
1380 infer(cabac_init_flag, 0);
1381 if (current->slice_temporal_mvp_enabled_flag) {
1382 if (current->slice_type == HEVC_SLICE_B)
1383 flag(collocated_from_l0_flag);
1384 else
1385 infer(collocated_from_l0_flag, 1);
1386 if (current->collocated_from_l0_flag) {
1387 if (current->num_ref_idx_l0_active_minus1 > 0)
1388 ue(collocated_ref_idx, 0, current->num_ref_idx_l0_active_minus1);
1389 else
1390 infer(collocated_ref_idx, 0);
1391 } else {
1392 if (current->num_ref_idx_l1_active_minus1 > 0)
1393 ue(collocated_ref_idx, 0, current->num_ref_idx_l1_active_minus1);
1394 else
1395 infer(collocated_ref_idx, 0);
1396 }
1397 }
1398
1399 if ((pps->weighted_pred_flag && current->slice_type == HEVC_SLICE_P) ||
1400 (pps->weighted_bipred_flag && current->slice_type == HEVC_SLICE_B))
1401 CHECK(FUNC(pred_weight_table)(ctx, rw, current));
1402
1403 ue(five_minus_max_num_merge_cand, 0, 4);
1404 if (sps->motion_vector_resolution_control_idc == 2)
1405 flag(use_integer_mv_flag);
1406 else
1407 infer(use_integer_mv_flag, sps->motion_vector_resolution_control_idc);
1408 }
1409
1410 se(slice_qp_delta,
1411 - 6 * sps->bit_depth_luma_minus8 - (pps->init_qp_minus26 + 26),
1412 + 51 - (pps->init_qp_minus26 + 26));
1413 if (pps->pps_slice_chroma_qp_offsets_present_flag) {
1414 se(slice_cb_qp_offset, -12, +12);
1415 se(slice_cr_qp_offset, -12, +12);
1416 } else {
1417 infer(slice_cb_qp_offset, 0);
1418 infer(slice_cr_qp_offset, 0);
1419 }
1420 if (pps->pps_slice_act_qp_offsets_present_flag) {
1421 se(slice_act_y_qp_offset,
1422 -12 - (pps->pps_act_y_qp_offset_plus5 - 5),
1423 +12 - (pps->pps_act_y_qp_offset_plus5 - 5));
1424 se(slice_act_cb_qp_offset,
1425 -12 - (pps->pps_act_cb_qp_offset_plus5 - 5),
1426 +12 - (pps->pps_act_cb_qp_offset_plus5 - 5));
1427 se(slice_act_cr_qp_offset,
1428 -12 - (pps->pps_act_cr_qp_offset_plus3 - 3),
1429 +12 - (pps->pps_act_cr_qp_offset_plus3 - 3));
1430 } else {
1431 infer(slice_act_y_qp_offset, 0);
1432 infer(slice_act_cb_qp_offset, 0);
1433 infer(slice_act_cr_qp_offset, 0);
1434 }
1435 if (pps->chroma_qp_offset_list_enabled_flag)
1436 flag(cu_chroma_qp_offset_enabled_flag);
1437 else
1438 infer(cu_chroma_qp_offset_enabled_flag, 0);
1439
1440 if (pps->deblocking_filter_override_enabled_flag)
1441 flag(deblocking_filter_override_flag);
1442 else
1443 infer(deblocking_filter_override_flag, 0);
1444 if (current->deblocking_filter_override_flag) {
1445 flag(slice_deblocking_filter_disabled_flag);
1446 if (!current->slice_deblocking_filter_disabled_flag) {
1447 se(slice_beta_offset_div2, -6, +6);
1448 se(slice_tc_offset_div2, -6, +6);
1449 } else {
1450 infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
1451 infer(slice_tc_offset_div2, pps->pps_tc_offset_div2);
1452 }
1453 } else {
1454 infer(slice_deblocking_filter_disabled_flag,
1455 pps->pps_deblocking_filter_disabled_flag);
1456 infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
1457 infer(slice_tc_offset_div2, pps->pps_tc_offset_div2);
1458 }
1459 if (pps->pps_loop_filter_across_slices_enabled_flag &&
1460 (current->slice_sao_luma_flag || current->slice_sao_chroma_flag ||
1461 !current->slice_deblocking_filter_disabled_flag))
1462 flag(slice_loop_filter_across_slices_enabled_flag);
1463 else
1464 infer(slice_loop_filter_across_slices_enabled_flag,
1465 pps->pps_loop_filter_across_slices_enabled_flag);
1466 }
1467
1468 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
1469 unsigned int num_entry_point_offsets_limit;
1470 if (!pps->tiles_enabled_flag && pps->entropy_coding_sync_enabled_flag)
1471 num_entry_point_offsets_limit = pic_height_in_ctbs_y - 1;
1472 else if (pps->tiles_enabled_flag && !pps->entropy_coding_sync_enabled_flag)
1473 num_entry_point_offsets_limit =
1474 (pps->num_tile_columns_minus1 + 1) * (pps->num_tile_rows_minus1 + 1);
1475 else
1476 num_entry_point_offsets_limit =
1477 (pps->num_tile_columns_minus1 + 1) * pic_height_in_ctbs_y - 1;
1478 ue(num_entry_point_offsets, 0, num_entry_point_offsets_limit);
1479
1480 if (current->num_entry_point_offsets > HEVC_MAX_ENTRY_POINT_OFFSETS) {
1481 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many entry points: "
1482 "%"PRIu16".\n", current->num_entry_point_offsets);
1483 return AVERROR_PATCHWELCOME;
1484 }
1485
1486 if (current->num_entry_point_offsets > 0) {
1487 ue(offset_len_minus1, 0, 31);
1488 for (i = 0; i < current->num_entry_point_offsets; i++)
1489 u(current->offset_len_minus1 + 1, entry_point_offset_minus1[i],
1490 0, (1 << (current->offset_len_minus1 + 1)) - 1);
1491 }
1492 }
1493
1494 if (pps->slice_segment_header_extension_present_flag) {
1495 ue(slice_segment_header_extension_length, 0, 256);
1496 for (i = 0; i < current->slice_segment_header_extension_length; i++)
1497 u(8, slice_segment_header_extension_data_byte[i], 0x00, 0xff);
1498 }
1499
1500 CHECK(FUNC(byte_alignment)(ctx, rw));
1501
1502 return 0;
1503 }