You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1504 lines
58KB

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