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.

1741 lines
66KB

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