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.

2235 lines
83KB

  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. fixed(1, forbidden_zero_bit, 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. ub(6, nal_unit_type);
  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. ub(5, general_profile_idc);
  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. ub(8, general_level_idc);
  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. ubs(5, sub_layer_profile_idc[i], 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. ubs(8, sub_layer_level_idc[i], 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. ub(8, tick_divisor_minus2);
  233. ub(5, du_cpb_removal_delay_increment_length_minus1);
  234. flag(sub_pic_cpb_params_in_pic_timing_sei_flag);
  235. ub(5, dpb_output_delay_du_length_minus1);
  236. }
  237. ub(4, bit_rate_scale);
  238. ub(4, cpb_size_scale);
  239. if (current->sub_pic_hrd_params_present_flag)
  240. ub(4, cpb_size_du_scale);
  241. ub(5, initial_cpb_removal_delay_length_minus1);
  242. ub(5, au_cpb_removal_delay_length_minus1);
  243. ub(5, dpb_output_delay_length_minus1);
  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. ub(8, aspect_ratio_idc);
  280. if (current->aspect_ratio_idc == 255) {
  281. ub(16, sar_width);
  282. ub(16, sar_height);
  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. ub(3, video_format);
  293. flag(video_full_range_flag);
  294. flag(colour_description_present_flag);
  295. if (current->colour_description_present_flag) {
  296. ub(8, colour_primaries);
  297. ub(8, transfer_characteristics);
  298. ub(8, matrix_coefficients);
  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. ub(4, vps_video_parameter_set_id);
  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. ubs(bit_depth, sps_palette_predictor_initializers[comp][i], 2, comp, i);
  629. }
  630. }
  631. }
  632. u(2, motion_vector_resolution_control_idc, 0, 2);
  633. flag(intra_boundary_filtering_disable_flag);
  634. return 0;
  635. }
  636. static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
  637. H265RawSPS *current)
  638. {
  639. CodedBitstreamH265Context *h265 = ctx->priv_data;
  640. const H265RawVPS *vps;
  641. int err, i;
  642. unsigned int min_cb_log2_size_y, ctb_log2_size_y,
  643. min_cb_size_y, min_tb_log2_size_y;
  644. HEADER("Sequence Parameter Set");
  645. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_SPS));
  646. ub(4, sps_video_parameter_set_id);
  647. h265->active_vps = vps = h265->vps[current->sps_video_parameter_set_id];
  648. u(3, sps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
  649. flag(sps_temporal_id_nesting_flag);
  650. if (vps) {
  651. if (vps->vps_max_sub_layers_minus1 > current->sps_max_sub_layers_minus1) {
  652. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
  653. "sps_max_sub_layers_minus1 (%d) must be less than or equal to "
  654. "vps_max_sub_layers_minus1 (%d).\n",
  655. vps->vps_max_sub_layers_minus1,
  656. current->sps_max_sub_layers_minus1);
  657. return AVERROR_INVALIDDATA;
  658. }
  659. if (vps->vps_temporal_id_nesting_flag &&
  660. !current->sps_temporal_id_nesting_flag) {
  661. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
  662. "sps_temporal_id_nesting_flag must be 1 if "
  663. "vps_temporal_id_nesting_flag is 1.\n");
  664. return AVERROR_INVALIDDATA;
  665. }
  666. }
  667. CHECK(FUNC(profile_tier_level)(ctx, rw, &current->profile_tier_level,
  668. 1, current->sps_max_sub_layers_minus1));
  669. ue(sps_seq_parameter_set_id, 0, 15);
  670. ue(chroma_format_idc, 0, 3);
  671. if (current->chroma_format_idc == 3)
  672. flag(separate_colour_plane_flag);
  673. else
  674. infer(separate_colour_plane_flag, 0);
  675. ue(pic_width_in_luma_samples, 1, HEVC_MAX_WIDTH);
  676. ue(pic_height_in_luma_samples, 1, HEVC_MAX_HEIGHT);
  677. flag(conformance_window_flag);
  678. if (current->conformance_window_flag) {
  679. ue(conf_win_left_offset, 0, current->pic_width_in_luma_samples);
  680. ue(conf_win_right_offset, 0, current->pic_width_in_luma_samples);
  681. ue(conf_win_top_offset, 0, current->pic_height_in_luma_samples);
  682. ue(conf_win_bottom_offset, 0, current->pic_height_in_luma_samples);
  683. } else {
  684. infer(conf_win_left_offset, 0);
  685. infer(conf_win_right_offset, 0);
  686. infer(conf_win_top_offset, 0);
  687. infer(conf_win_bottom_offset, 0);
  688. }
  689. ue(bit_depth_luma_minus8, 0, 8);
  690. ue(bit_depth_chroma_minus8, 0, 8);
  691. ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12);
  692. flag(sps_sub_layer_ordering_info_present_flag);
  693. for (i = (current->sps_sub_layer_ordering_info_present_flag ?
  694. 0 : current->sps_max_sub_layers_minus1);
  695. i <= current->sps_max_sub_layers_minus1; i++) {
  696. ues(sps_max_dec_pic_buffering_minus1[i],
  697. 0, HEVC_MAX_DPB_SIZE - 1, 1, i);
  698. ues(sps_max_num_reorder_pics[i],
  699. 0, current->sps_max_dec_pic_buffering_minus1[i], 1, i);
  700. ues(sps_max_latency_increase_plus1[i],
  701. 0, UINT32_MAX - 1, 1, i);
  702. }
  703. if (!current->sps_sub_layer_ordering_info_present_flag) {
  704. for (i = 0; i < current->sps_max_sub_layers_minus1; i++) {
  705. infer(sps_max_dec_pic_buffering_minus1[i],
  706. current->sps_max_dec_pic_buffering_minus1[current->sps_max_sub_layers_minus1]);
  707. infer(sps_max_num_reorder_pics[i],
  708. current->sps_max_num_reorder_pics[current->sps_max_sub_layers_minus1]);
  709. infer(sps_max_latency_increase_plus1[i],
  710. current->sps_max_latency_increase_plus1[current->sps_max_sub_layers_minus1]);
  711. }
  712. }
  713. ue(log2_min_luma_coding_block_size_minus3, 0, 3);
  714. min_cb_log2_size_y = current->log2_min_luma_coding_block_size_minus3 + 3;
  715. ue(log2_diff_max_min_luma_coding_block_size, 0, 3);
  716. ctb_log2_size_y = min_cb_log2_size_y +
  717. current->log2_diff_max_min_luma_coding_block_size;
  718. min_cb_size_y = 1 << min_cb_log2_size_y;
  719. if (current->pic_width_in_luma_samples % min_cb_size_y ||
  720. current->pic_height_in_luma_samples % min_cb_size_y) {
  721. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid dimensions: %ux%u not divisible "
  722. "by MinCbSizeY = %u.\n", current->pic_width_in_luma_samples,
  723. current->pic_height_in_luma_samples, min_cb_size_y);
  724. return AVERROR_INVALIDDATA;
  725. }
  726. ue(log2_min_luma_transform_block_size_minus2, 0, min_cb_log2_size_y - 3);
  727. min_tb_log2_size_y = current->log2_min_luma_transform_block_size_minus2 + 2;
  728. ue(log2_diff_max_min_luma_transform_block_size,
  729. 0, FFMIN(ctb_log2_size_y, 5) - min_tb_log2_size_y);
  730. ue(max_transform_hierarchy_depth_inter,
  731. 0, ctb_log2_size_y - min_tb_log2_size_y);
  732. ue(max_transform_hierarchy_depth_intra,
  733. 0, ctb_log2_size_y - min_tb_log2_size_y);
  734. flag(scaling_list_enabled_flag);
  735. if (current->scaling_list_enabled_flag) {
  736. flag(sps_scaling_list_data_present_flag);
  737. if (current->sps_scaling_list_data_present_flag)
  738. CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
  739. } else {
  740. infer(sps_scaling_list_data_present_flag, 0);
  741. }
  742. flag(amp_enabled_flag);
  743. flag(sample_adaptive_offset_enabled_flag);
  744. flag(pcm_enabled_flag);
  745. if (current->pcm_enabled_flag) {
  746. u(4, pcm_sample_bit_depth_luma_minus1,
  747. 0, current->bit_depth_luma_minus8 + 8 - 1);
  748. u(4, pcm_sample_bit_depth_chroma_minus1,
  749. 0, current->bit_depth_chroma_minus8 + 8 - 1);
  750. ue(log2_min_pcm_luma_coding_block_size_minus3,
  751. FFMIN(min_cb_log2_size_y, 5) - 3, FFMIN(ctb_log2_size_y, 5) - 3);
  752. ue(log2_diff_max_min_pcm_luma_coding_block_size,
  753. 0, FFMIN(ctb_log2_size_y, 5) - (current->log2_min_pcm_luma_coding_block_size_minus3 + 3));
  754. flag(pcm_loop_filter_disabled_flag);
  755. }
  756. ue(num_short_term_ref_pic_sets, 0, HEVC_MAX_SHORT_TERM_REF_PIC_SETS);
  757. for (i = 0; i < current->num_short_term_ref_pic_sets; i++)
  758. CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->st_ref_pic_set[i], i, current));
  759. flag(long_term_ref_pics_present_flag);
  760. if (current->long_term_ref_pics_present_flag) {
  761. ue(num_long_term_ref_pics_sps, 0, HEVC_MAX_LONG_TERM_REF_PICS);
  762. for (i = 0; i < current->num_long_term_ref_pics_sps; i++) {
  763. ubs(current->log2_max_pic_order_cnt_lsb_minus4 + 4,
  764. lt_ref_pic_poc_lsb_sps[i], 1, i);
  765. flags(used_by_curr_pic_lt_sps_flag[i], 1, i);
  766. }
  767. }
  768. flag(sps_temporal_mvp_enabled_flag);
  769. flag(strong_intra_smoothing_enabled_flag);
  770. flag(vui_parameters_present_flag);
  771. if (current->vui_parameters_present_flag)
  772. CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
  773. flag(sps_extension_present_flag);
  774. if (current->sps_extension_present_flag) {
  775. flag(sps_range_extension_flag);
  776. flag(sps_multilayer_extension_flag);
  777. flag(sps_3d_extension_flag);
  778. flag(sps_scc_extension_flag);
  779. ub(4, sps_extension_4bits);
  780. }
  781. if (current->sps_range_extension_flag)
  782. CHECK(FUNC(sps_range_extension)(ctx, rw, current));
  783. if (current->sps_multilayer_extension_flag)
  784. return AVERROR_PATCHWELCOME;
  785. if (current->sps_3d_extension_flag)
  786. return AVERROR_PATCHWELCOME;
  787. if (current->sps_scc_extension_flag)
  788. CHECK(FUNC(sps_scc_extension)(ctx, rw, current));
  789. if (current->sps_extension_4bits)
  790. CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
  791. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  792. return 0;
  793. }
  794. static int FUNC(pps_range_extension)(CodedBitstreamContext *ctx, RWContext *rw,
  795. H265RawPPS *current)
  796. {
  797. CodedBitstreamH265Context *h265 = ctx->priv_data;
  798. const H265RawSPS *sps = h265->active_sps;
  799. int err, i;
  800. if (current->transform_skip_enabled_flag)
  801. ue(log2_max_transform_skip_block_size_minus2, 0, 3);
  802. flag(cross_component_prediction_enabled_flag);
  803. flag(chroma_qp_offset_list_enabled_flag);
  804. if (current->chroma_qp_offset_list_enabled_flag) {
  805. ue(diff_cu_chroma_qp_offset_depth,
  806. 0, sps->log2_diff_max_min_luma_coding_block_size);
  807. ue(chroma_qp_offset_list_len_minus1, 0, 5);
  808. for (i = 0; i <= current->chroma_qp_offset_list_len_minus1; i++) {
  809. ses(cb_qp_offset_list[i], -12, +12, 1, i);
  810. ses(cr_qp_offset_list[i], -12, +12, 1, i);
  811. }
  812. }
  813. ue(log2_sao_offset_scale_luma, 0, FFMAX(0, sps->bit_depth_luma_minus8 - 2));
  814. ue(log2_sao_offset_scale_chroma, 0, FFMAX(0, sps->bit_depth_chroma_minus8 - 2));
  815. return 0;
  816. }
  817. static int FUNC(pps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
  818. H265RawPPS *current)
  819. {
  820. int err, comp, i;
  821. flag(pps_curr_pic_ref_enabled_flag);
  822. flag(residual_adaptive_colour_transform_enabled_flag);
  823. if (current->residual_adaptive_colour_transform_enabled_flag) {
  824. flag(pps_slice_act_qp_offsets_present_flag);
  825. se(pps_act_y_qp_offset_plus5, -7, +17);
  826. se(pps_act_cb_qp_offset_plus5, -7, +17);
  827. se(pps_act_cr_qp_offset_plus3, -9, +15);
  828. } else {
  829. infer(pps_slice_act_qp_offsets_present_flag, 0);
  830. infer(pps_act_y_qp_offset_plus5, 0);
  831. infer(pps_act_cb_qp_offset_plus5, 0);
  832. infer(pps_act_cr_qp_offset_plus3, 0);
  833. }
  834. flag(pps_palette_predictor_initializer_present_flag);
  835. if (current->pps_palette_predictor_initializer_present_flag) {
  836. ue(pps_num_palette_predictor_initializer, 0, 128);
  837. if (current->pps_num_palette_predictor_initializer > 0) {
  838. flag(monochrome_palette_flag);
  839. ue(luma_bit_depth_entry_minus8, 0, 8);
  840. if (!current->monochrome_palette_flag)
  841. ue(chroma_bit_depth_entry_minus8, 0, 8);
  842. for (comp = 0; comp < (current->monochrome_palette_flag ? 1 : 3); comp++) {
  843. int bit_depth = comp == 0 ? current->luma_bit_depth_entry_minus8 + 8
  844. : current->chroma_bit_depth_entry_minus8 + 8;
  845. for (i = 0; i < current->pps_num_palette_predictor_initializer; i++)
  846. ubs(bit_depth, pps_palette_predictor_initializers[comp][i], 2, comp, i);
  847. }
  848. }
  849. }
  850. return 0;
  851. }
  852. static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
  853. H265RawPPS *current)
  854. {
  855. CodedBitstreamH265Context *h265 = ctx->priv_data;
  856. const H265RawSPS *sps;
  857. int err, i;
  858. HEADER("Picture Parameter Set");
  859. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_PPS));
  860. ue(pps_pic_parameter_set_id, 0, 63);
  861. ue(pps_seq_parameter_set_id, 0, 15);
  862. sps = h265->sps[current->pps_seq_parameter_set_id];
  863. if (!sps) {
  864. av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
  865. current->pps_seq_parameter_set_id);
  866. return AVERROR_INVALIDDATA;
  867. }
  868. h265->active_sps = sps;
  869. flag(dependent_slice_segments_enabled_flag);
  870. flag(output_flag_present_flag);
  871. ub(3, num_extra_slice_header_bits);
  872. flag(sign_data_hiding_enabled_flag);
  873. flag(cabac_init_present_flag);
  874. ue(num_ref_idx_l0_default_active_minus1, 0, 14);
  875. ue(num_ref_idx_l1_default_active_minus1, 0, 14);
  876. se(init_qp_minus26, -(26 + 6 * sps->bit_depth_luma_minus8), +25);
  877. flag(constrained_intra_pred_flag);
  878. flag(transform_skip_enabled_flag);
  879. flag(cu_qp_delta_enabled_flag);
  880. if (current->cu_qp_delta_enabled_flag)
  881. ue(diff_cu_qp_delta_depth,
  882. 0, sps->log2_diff_max_min_luma_coding_block_size);
  883. else
  884. infer(diff_cu_qp_delta_depth, 0);
  885. se(pps_cb_qp_offset, -12, +12);
  886. se(pps_cr_qp_offset, -12, +12);
  887. flag(pps_slice_chroma_qp_offsets_present_flag);
  888. flag(weighted_pred_flag);
  889. flag(weighted_bipred_flag);
  890. flag(transquant_bypass_enabled_flag);
  891. flag(tiles_enabled_flag);
  892. flag(entropy_coding_sync_enabled_flag);
  893. if (current->tiles_enabled_flag) {
  894. ue(num_tile_columns_minus1, 0, HEVC_MAX_TILE_COLUMNS);
  895. ue(num_tile_rows_minus1, 0, HEVC_MAX_TILE_ROWS);
  896. flag(uniform_spacing_flag);
  897. if (!current->uniform_spacing_flag) {
  898. for (i = 0; i < current->num_tile_columns_minus1; i++)
  899. ues(column_width_minus1[i], 0, sps->pic_width_in_luma_samples, 1, i);
  900. for (i = 0; i < current->num_tile_rows_minus1; i++)
  901. ues(row_height_minus1[i], 0, sps->pic_height_in_luma_samples, 1, i);
  902. }
  903. flag(loop_filter_across_tiles_enabled_flag);
  904. } else {
  905. infer(num_tile_columns_minus1, 0);
  906. infer(num_tile_rows_minus1, 0);
  907. }
  908. flag(pps_loop_filter_across_slices_enabled_flag);
  909. flag(deblocking_filter_control_present_flag);
  910. if (current->deblocking_filter_control_present_flag) {
  911. flag(deblocking_filter_override_enabled_flag);
  912. flag(pps_deblocking_filter_disabled_flag);
  913. if (!current->pps_deblocking_filter_disabled_flag) {
  914. se(pps_beta_offset_div2, -6, +6);
  915. se(pps_tc_offset_div2, -6, +6);
  916. } else {
  917. infer(pps_beta_offset_div2, 0);
  918. infer(pps_tc_offset_div2, 0);
  919. }
  920. } else {
  921. infer(deblocking_filter_override_enabled_flag, 0);
  922. infer(pps_deblocking_filter_disabled_flag, 0);
  923. infer(pps_beta_offset_div2, 0);
  924. infer(pps_tc_offset_div2, 0);
  925. }
  926. flag(pps_scaling_list_data_present_flag);
  927. if (current->pps_scaling_list_data_present_flag)
  928. CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
  929. flag(lists_modification_present_flag);
  930. ue(log2_parallel_merge_level_minus2,
  931. 0, (sps->log2_min_luma_coding_block_size_minus3 + 3 +
  932. sps->log2_diff_max_min_luma_coding_block_size - 2));
  933. flag(slice_segment_header_extension_present_flag);
  934. flag(pps_extension_present_flag);
  935. if (current->pps_extension_present_flag) {
  936. flag(pps_range_extension_flag);
  937. flag(pps_multilayer_extension_flag);
  938. flag(pps_3d_extension_flag);
  939. flag(pps_scc_extension_flag);
  940. ub(4, pps_extension_4bits);
  941. }
  942. if (current->pps_range_extension_flag)
  943. CHECK(FUNC(pps_range_extension)(ctx, rw, current));
  944. if (current->pps_multilayer_extension_flag)
  945. return AVERROR_PATCHWELCOME;
  946. if (current->pps_3d_extension_flag)
  947. return AVERROR_PATCHWELCOME;
  948. if (current->pps_scc_extension_flag)
  949. CHECK(FUNC(pps_scc_extension)(ctx, rw, current));
  950. if (current->pps_extension_4bits)
  951. CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
  952. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  953. return 0;
  954. }
  955. static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
  956. H265RawAUD *current)
  957. {
  958. int err;
  959. HEADER("Access Unit Delimiter");
  960. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_AUD));
  961. u(3, pic_type, 0, 2);
  962. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  963. return 0;
  964. }
  965. static int FUNC(ref_pic_lists_modification)(CodedBitstreamContext *ctx, RWContext *rw,
  966. H265RawSliceHeader *current,
  967. unsigned int num_pic_total_curr)
  968. {
  969. unsigned int entry_size;
  970. int err, i;
  971. entry_size = av_log2(num_pic_total_curr - 1) + 1;
  972. flag(ref_pic_list_modification_flag_l0);
  973. if (current->ref_pic_list_modification_flag_l0) {
  974. for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++)
  975. us(entry_size, list_entry_l0[i], 0, num_pic_total_curr - 1, 1, i);
  976. }
  977. if (current->slice_type == HEVC_SLICE_B) {
  978. flag(ref_pic_list_modification_flag_l1);
  979. if (current->ref_pic_list_modification_flag_l1) {
  980. for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++)
  981. us(entry_size, list_entry_l1[i], 0, num_pic_total_curr - 1, 1, i);
  982. }
  983. }
  984. return 0;
  985. }
  986. static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
  987. H265RawSliceHeader *current)
  988. {
  989. CodedBitstreamH265Context *h265 = ctx->priv_data;
  990. const H265RawSPS *sps = h265->active_sps;
  991. int err, i, j;
  992. int chroma = !sps->separate_colour_plane_flag &&
  993. sps->chroma_format_idc != 0;
  994. ue(luma_log2_weight_denom, 0, 7);
  995. if (chroma)
  996. se(delta_chroma_log2_weight_denom, -7, 7);
  997. else
  998. infer(delta_chroma_log2_weight_denom, 0);
  999. for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
  1000. if (1 /* is not same POC and same layer_id */)
  1001. flags(luma_weight_l0_flag[i], 1, i);
  1002. else
  1003. infer(luma_weight_l0_flag[i], 0);
  1004. }
  1005. if (chroma) {
  1006. for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
  1007. if (1 /* is not same POC and same layer_id */)
  1008. flags(chroma_weight_l0_flag[i], 1, i);
  1009. else
  1010. infer(chroma_weight_l0_flag[i], 0);
  1011. }
  1012. }
  1013. for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
  1014. if (current->luma_weight_l0_flag[i]) {
  1015. ses(delta_luma_weight_l0[i], -128, +127, 1, i);
  1016. ses(luma_offset_l0[i],
  1017. -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
  1018. ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1), 1, i);
  1019. } else {
  1020. infer(delta_luma_weight_l0[i], 0);
  1021. infer(luma_offset_l0[i], 0);
  1022. }
  1023. if (current->chroma_weight_l0_flag[i]) {
  1024. for (j = 0; j < 2; j++) {
  1025. ses(delta_chroma_weight_l0[i][j], -128, +127, 2, i, j);
  1026. ses(chroma_offset_l0[i][j],
  1027. -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
  1028. ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1), 2, i, j);
  1029. }
  1030. } else {
  1031. for (j = 0; j < 2; j++) {
  1032. infer(delta_chroma_weight_l0[i][j], 0);
  1033. infer(chroma_offset_l0[i][j], 0);
  1034. }
  1035. }
  1036. }
  1037. if (current->slice_type == HEVC_SLICE_B) {
  1038. for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
  1039. if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
  1040. flags(luma_weight_l1_flag[i], 1, i);
  1041. else
  1042. infer(luma_weight_l1_flag[i], 0);
  1043. }
  1044. if (chroma) {
  1045. for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
  1046. if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
  1047. flags(chroma_weight_l1_flag[i], 1, i);
  1048. else
  1049. infer(chroma_weight_l1_flag[i], 0);
  1050. }
  1051. }
  1052. for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
  1053. if (current->luma_weight_l1_flag[i]) {
  1054. ses(delta_luma_weight_l1[i], -128, +127, 1, i);
  1055. ses(luma_offset_l1[i],
  1056. -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
  1057. ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1), 1, i);
  1058. } else {
  1059. infer(delta_luma_weight_l1[i], 0);
  1060. infer(luma_offset_l1[i], 0);
  1061. }
  1062. if (current->chroma_weight_l1_flag[i]) {
  1063. for (j = 0; j < 2; j++) {
  1064. ses(delta_chroma_weight_l1[i][j], -128, +127, 2, i, j);
  1065. ses(chroma_offset_l1[i][j],
  1066. -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
  1067. ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1), 2, i, j);
  1068. }
  1069. } else {
  1070. for (j = 0; j < 2; j++) {
  1071. infer(delta_chroma_weight_l1[i][j], 0);
  1072. infer(chroma_offset_l1[i][j], 0);
  1073. }
  1074. }
  1075. }
  1076. }
  1077. return 0;
  1078. }
  1079. static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
  1080. H265RawSliceHeader *current)
  1081. {
  1082. CodedBitstreamH265Context *h265 = ctx->priv_data;
  1083. const H265RawSPS *sps;
  1084. const H265RawPPS *pps;
  1085. unsigned int min_cb_log2_size_y, ctb_log2_size_y, ctb_size_y;
  1086. unsigned int pic_width_in_ctbs_y, pic_height_in_ctbs_y, pic_size_in_ctbs_y;
  1087. unsigned int num_pic_total_curr = 0;
  1088. int err, i;
  1089. HEADER("Slice Segment Header");
  1090. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, -1));
  1091. flag(first_slice_segment_in_pic_flag);
  1092. if (current->nal_unit_header.nal_unit_type >= HEVC_NAL_BLA_W_LP &&
  1093. current->nal_unit_header.nal_unit_type <= HEVC_NAL_IRAP_VCL23)
  1094. flag(no_output_of_prior_pics_flag);
  1095. ue(slice_pic_parameter_set_id, 0, 63);
  1096. pps = h265->pps[current->slice_pic_parameter_set_id];
  1097. if (!pps) {
  1098. av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
  1099. current->slice_pic_parameter_set_id);
  1100. return AVERROR_INVALIDDATA;
  1101. }
  1102. h265->active_pps = pps;
  1103. sps = h265->sps[pps->pps_seq_parameter_set_id];
  1104. if (!sps) {
  1105. av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
  1106. pps->pps_seq_parameter_set_id);
  1107. return AVERROR_INVALIDDATA;
  1108. }
  1109. h265->active_sps = sps;
  1110. min_cb_log2_size_y = sps->log2_min_luma_coding_block_size_minus3 + 3;
  1111. ctb_log2_size_y = min_cb_log2_size_y + sps->log2_diff_max_min_luma_coding_block_size;
  1112. ctb_size_y = 1 << ctb_log2_size_y;
  1113. pic_width_in_ctbs_y =
  1114. (sps->pic_width_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
  1115. pic_height_in_ctbs_y =
  1116. (sps->pic_height_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
  1117. pic_size_in_ctbs_y = pic_width_in_ctbs_y * pic_height_in_ctbs_y;
  1118. if (!current->first_slice_segment_in_pic_flag) {
  1119. unsigned int address_size = av_log2(pic_size_in_ctbs_y - 1) + 1;
  1120. if (pps->dependent_slice_segments_enabled_flag)
  1121. flag(dependent_slice_segment_flag);
  1122. else
  1123. infer(dependent_slice_segment_flag, 0);
  1124. u(address_size, slice_segment_address, 0, pic_size_in_ctbs_y - 1);
  1125. } else {
  1126. infer(dependent_slice_segment_flag, 0);
  1127. }
  1128. if (!current->dependent_slice_segment_flag) {
  1129. for (i = 0; i < pps->num_extra_slice_header_bits; i++)
  1130. flags(slice_reserved_flag[i], 1, i);
  1131. ue(slice_type, 0, 2);
  1132. if (pps->output_flag_present_flag)
  1133. flag(pic_output_flag);
  1134. if (sps->separate_colour_plane_flag)
  1135. u(2, colour_plane_id, 0, 2);
  1136. if (current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_W_RADL &&
  1137. current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_N_LP) {
  1138. const H265RawSTRefPicSet *rps;
  1139. ub(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, slice_pic_order_cnt_lsb);
  1140. flag(short_term_ref_pic_set_sps_flag);
  1141. if (!current->short_term_ref_pic_set_sps_flag) {
  1142. CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->short_term_ref_pic_set,
  1143. sps->num_short_term_ref_pic_sets, sps));
  1144. rps = &current->short_term_ref_pic_set;
  1145. } else if (sps->num_short_term_ref_pic_sets > 1) {
  1146. unsigned int idx_size = av_log2(sps->num_short_term_ref_pic_sets - 1) + 1;
  1147. u(idx_size, short_term_ref_pic_set_idx,
  1148. 0, sps->num_short_term_ref_pic_sets - 1);
  1149. rps = &sps->st_ref_pic_set[current->short_term_ref_pic_set_idx];
  1150. } else {
  1151. infer(short_term_ref_pic_set_idx, 0);
  1152. rps = &sps->st_ref_pic_set[0];
  1153. }
  1154. num_pic_total_curr = 0;
  1155. for (i = 0; i < rps->num_negative_pics; i++)
  1156. if (rps->used_by_curr_pic_s0_flag[i])
  1157. ++num_pic_total_curr;
  1158. for (i = 0; i < rps->num_positive_pics; i++)
  1159. if (rps->used_by_curr_pic_s1_flag[i])
  1160. ++num_pic_total_curr;
  1161. if (sps->long_term_ref_pics_present_flag) {
  1162. unsigned int idx_size;
  1163. if (sps->num_long_term_ref_pics_sps > 0) {
  1164. ue(num_long_term_sps, 0, sps->num_long_term_ref_pics_sps);
  1165. idx_size = av_log2(sps->num_long_term_ref_pics_sps - 1) + 1;
  1166. } else {
  1167. infer(num_long_term_sps, 0);
  1168. idx_size = 0;
  1169. }
  1170. ue(num_long_term_pics, 0, HEVC_MAX_LONG_TERM_REF_PICS);
  1171. for (i = 0; i < current->num_long_term_sps +
  1172. current->num_long_term_pics; i++) {
  1173. if (i < current->num_long_term_sps) {
  1174. if (sps->num_long_term_ref_pics_sps > 1)
  1175. us(idx_size, lt_idx_sps[i],
  1176. 0, sps->num_long_term_ref_pics_sps - 1, 1, i);
  1177. if (sps->used_by_curr_pic_lt_sps_flag[current->lt_idx_sps[i]])
  1178. ++num_pic_total_curr;
  1179. } else {
  1180. ubs(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, poc_lsb_lt[i], 1, i);
  1181. flags(used_by_curr_pic_lt_flag[i], 1, i);
  1182. if (current->used_by_curr_pic_lt_flag[i])
  1183. ++num_pic_total_curr;
  1184. }
  1185. flags(delta_poc_msb_present_flag[i], 1, i);
  1186. if (current->delta_poc_msb_present_flag[i])
  1187. ues(delta_poc_msb_cycle_lt[i], 0, UINT32_MAX - 1, 1, i);
  1188. else
  1189. infer(delta_poc_msb_cycle_lt[i], 0);
  1190. }
  1191. }
  1192. if (sps->sps_temporal_mvp_enabled_flag)
  1193. flag(slice_temporal_mvp_enabled_flag);
  1194. else
  1195. infer(slice_temporal_mvp_enabled_flag, 0);
  1196. if (pps->pps_curr_pic_ref_enabled_flag)
  1197. ++num_pic_total_curr;
  1198. }
  1199. if (sps->sample_adaptive_offset_enabled_flag) {
  1200. flag(slice_sao_luma_flag);
  1201. if (!sps->separate_colour_plane_flag && sps->chroma_format_idc != 0)
  1202. flag(slice_sao_chroma_flag);
  1203. else
  1204. infer(slice_sao_chroma_flag, 0);
  1205. } else {
  1206. infer(slice_sao_luma_flag, 0);
  1207. infer(slice_sao_chroma_flag, 0);
  1208. }
  1209. if (current->slice_type == HEVC_SLICE_P ||
  1210. current->slice_type == HEVC_SLICE_B) {
  1211. flag(num_ref_idx_active_override_flag);
  1212. if (current->num_ref_idx_active_override_flag) {
  1213. ue(num_ref_idx_l0_active_minus1, 0, 14);
  1214. if (current->slice_type == HEVC_SLICE_B)
  1215. ue(num_ref_idx_l1_active_minus1, 0, 14);
  1216. else
  1217. infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
  1218. } else {
  1219. infer(num_ref_idx_l0_active_minus1, pps->num_ref_idx_l0_default_active_minus1);
  1220. infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
  1221. }
  1222. if (pps->lists_modification_present_flag && num_pic_total_curr > 1)
  1223. CHECK(FUNC(ref_pic_lists_modification)(ctx, rw, current,
  1224. num_pic_total_curr));
  1225. if (current->slice_type == HEVC_SLICE_B)
  1226. flag(mvd_l1_zero_flag);
  1227. if (pps->cabac_init_present_flag)
  1228. flag(cabac_init_flag);
  1229. else
  1230. infer(cabac_init_flag, 0);
  1231. if (current->slice_temporal_mvp_enabled_flag) {
  1232. if (current->slice_type == HEVC_SLICE_B)
  1233. flag(collocated_from_l0_flag);
  1234. else
  1235. infer(collocated_from_l0_flag, 1);
  1236. if (current->collocated_from_l0_flag) {
  1237. if (current->num_ref_idx_l0_active_minus1 > 0)
  1238. ue(collocated_ref_idx, 0, current->num_ref_idx_l0_active_minus1);
  1239. else
  1240. infer(collocated_ref_idx, 0);
  1241. } else {
  1242. if (current->num_ref_idx_l1_active_minus1 > 0)
  1243. ue(collocated_ref_idx, 0, current->num_ref_idx_l1_active_minus1);
  1244. else
  1245. infer(collocated_ref_idx, 0);
  1246. }
  1247. }
  1248. if ((pps->weighted_pred_flag && current->slice_type == HEVC_SLICE_P) ||
  1249. (pps->weighted_bipred_flag && current->slice_type == HEVC_SLICE_B))
  1250. CHECK(FUNC(pred_weight_table)(ctx, rw, current));
  1251. ue(five_minus_max_num_merge_cand, 0, 4);
  1252. if (sps->motion_vector_resolution_control_idc == 2)
  1253. flag(use_integer_mv_flag);
  1254. else
  1255. infer(use_integer_mv_flag, sps->motion_vector_resolution_control_idc);
  1256. }
  1257. se(slice_qp_delta,
  1258. - 6 * sps->bit_depth_luma_minus8 - (pps->init_qp_minus26 + 26),
  1259. + 51 - (pps->init_qp_minus26 + 26));
  1260. if (pps->pps_slice_chroma_qp_offsets_present_flag) {
  1261. se(slice_cb_qp_offset, -12, +12);
  1262. se(slice_cr_qp_offset, -12, +12);
  1263. } else {
  1264. infer(slice_cb_qp_offset, 0);
  1265. infer(slice_cr_qp_offset, 0);
  1266. }
  1267. if (pps->pps_slice_act_qp_offsets_present_flag) {
  1268. se(slice_act_y_qp_offset,
  1269. -12 - (pps->pps_act_y_qp_offset_plus5 - 5),
  1270. +12 - (pps->pps_act_y_qp_offset_plus5 - 5));
  1271. se(slice_act_cb_qp_offset,
  1272. -12 - (pps->pps_act_cb_qp_offset_plus5 - 5),
  1273. +12 - (pps->pps_act_cb_qp_offset_plus5 - 5));
  1274. se(slice_act_cr_qp_offset,
  1275. -12 - (pps->pps_act_cr_qp_offset_plus3 - 3),
  1276. +12 - (pps->pps_act_cr_qp_offset_plus3 - 3));
  1277. } else {
  1278. infer(slice_act_y_qp_offset, 0);
  1279. infer(slice_act_cb_qp_offset, 0);
  1280. infer(slice_act_cr_qp_offset, 0);
  1281. }
  1282. if (pps->chroma_qp_offset_list_enabled_flag)
  1283. flag(cu_chroma_qp_offset_enabled_flag);
  1284. else
  1285. infer(cu_chroma_qp_offset_enabled_flag, 0);
  1286. if (pps->deblocking_filter_override_enabled_flag)
  1287. flag(deblocking_filter_override_flag);
  1288. else
  1289. infer(deblocking_filter_override_flag, 0);
  1290. if (current->deblocking_filter_override_flag) {
  1291. flag(slice_deblocking_filter_disabled_flag);
  1292. if (!current->slice_deblocking_filter_disabled_flag) {
  1293. se(slice_beta_offset_div2, -6, +6);
  1294. se(slice_tc_offset_div2, -6, +6);
  1295. } else {
  1296. infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
  1297. infer(slice_tc_offset_div2, pps->pps_tc_offset_div2);
  1298. }
  1299. } else {
  1300. infer(slice_deblocking_filter_disabled_flag,
  1301. pps->pps_deblocking_filter_disabled_flag);
  1302. infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
  1303. infer(slice_tc_offset_div2, pps->pps_tc_offset_div2);
  1304. }
  1305. if (pps->pps_loop_filter_across_slices_enabled_flag &&
  1306. (current->slice_sao_luma_flag || current->slice_sao_chroma_flag ||
  1307. !current->slice_deblocking_filter_disabled_flag))
  1308. flag(slice_loop_filter_across_slices_enabled_flag);
  1309. else
  1310. infer(slice_loop_filter_across_slices_enabled_flag,
  1311. pps->pps_loop_filter_across_slices_enabled_flag);
  1312. }
  1313. if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
  1314. unsigned int num_entry_point_offsets_limit;
  1315. if (!pps->tiles_enabled_flag && pps->entropy_coding_sync_enabled_flag)
  1316. num_entry_point_offsets_limit = pic_height_in_ctbs_y - 1;
  1317. else if (pps->tiles_enabled_flag && !pps->entropy_coding_sync_enabled_flag)
  1318. num_entry_point_offsets_limit =
  1319. (pps->num_tile_columns_minus1 + 1) * (pps->num_tile_rows_minus1 + 1);
  1320. else
  1321. num_entry_point_offsets_limit =
  1322. (pps->num_tile_columns_minus1 + 1) * pic_height_in_ctbs_y - 1;
  1323. ue(num_entry_point_offsets, 0, num_entry_point_offsets_limit);
  1324. if (current->num_entry_point_offsets > HEVC_MAX_ENTRY_POINT_OFFSETS) {
  1325. av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many entry points: "
  1326. "%"PRIu16".\n", current->num_entry_point_offsets);
  1327. return AVERROR_PATCHWELCOME;
  1328. }
  1329. if (current->num_entry_point_offsets > 0) {
  1330. ue(offset_len_minus1, 0, 31);
  1331. for (i = 0; i < current->num_entry_point_offsets; i++)
  1332. ubs(current->offset_len_minus1 + 1, entry_point_offset_minus1[i], 1, i);
  1333. }
  1334. }
  1335. if (pps->slice_segment_header_extension_present_flag) {
  1336. ue(slice_segment_header_extension_length, 0, 256);
  1337. for (i = 0; i < current->slice_segment_header_extension_length; i++)
  1338. us(8, slice_segment_header_extension_data_byte[i], 0x00, 0xff, 1, i);
  1339. }
  1340. CHECK(FUNC(byte_alignment)(ctx, rw));
  1341. return 0;
  1342. }
  1343. static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
  1344. H265RawSEIBufferingPeriod *current,
  1345. uint32_t *payload_size)
  1346. {
  1347. CodedBitstreamH265Context *h265 = ctx->priv_data;
  1348. const H265RawSPS *sps;
  1349. const H265RawHRDParameters *hrd;
  1350. int err, i, length;
  1351. #ifdef READ
  1352. int start_pos, end_pos, bits_left;
  1353. start_pos = get_bits_count(rw);
  1354. #endif
  1355. HEADER("Buffering Period");
  1356. ue(bp_seq_parameter_set_id, 0, HEVC_MAX_SPS_COUNT - 1);
  1357. sps = h265->sps[current->bp_seq_parameter_set_id];
  1358. if (!sps) {
  1359. av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
  1360. current->bp_seq_parameter_set_id);
  1361. return AVERROR_INVALIDDATA;
  1362. }
  1363. h265->active_sps = sps;
  1364. if (!sps->vui_parameters_present_flag ||
  1365. !sps->vui.vui_hrd_parameters_present_flag) {
  1366. av_log(ctx->log_ctx, AV_LOG_ERROR, "Buffering period SEI requires "
  1367. "HRD parameters to be present in SPS.\n");
  1368. return AVERROR_INVALIDDATA;
  1369. }
  1370. hrd = &sps->vui.hrd_parameters;
  1371. if (!hrd->nal_hrd_parameters_present_flag &&
  1372. !hrd->vcl_hrd_parameters_present_flag) {
  1373. av_log(ctx->log_ctx, AV_LOG_ERROR, "Buffering period SEI requires "
  1374. "NAL or VCL HRD parameters to be present.\n");
  1375. return AVERROR_INVALIDDATA;
  1376. }
  1377. if (!hrd->sub_pic_hrd_params_present_flag)
  1378. flag(irap_cpb_params_present_flag);
  1379. else
  1380. infer(irap_cpb_params_present_flag, 0);
  1381. if (current->irap_cpb_params_present_flag) {
  1382. length = hrd->au_cpb_removal_delay_length_minus1 + 1;
  1383. ub(length, cpb_delay_offset);
  1384. length = hrd->dpb_output_delay_length_minus1 + 1;
  1385. ub(length, dpb_delay_offset);
  1386. } else {
  1387. infer(cpb_delay_offset, 0);
  1388. infer(dpb_delay_offset, 0);
  1389. }
  1390. flag(concatenation_flag);
  1391. length = hrd->au_cpb_removal_delay_length_minus1 + 1;
  1392. ub(length, au_cpb_removal_delay_delta_minus1);
  1393. if (hrd->nal_hrd_parameters_present_flag) {
  1394. for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
  1395. length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
  1396. ubs(length, nal_initial_cpb_removal_delay[i], 1, i);
  1397. ubs(length, nal_initial_cpb_removal_offset[i], 1, i);
  1398. if (hrd->sub_pic_hrd_params_present_flag ||
  1399. current->irap_cpb_params_present_flag) {
  1400. ubs(length, nal_initial_alt_cpb_removal_delay[i], 1, i);
  1401. ubs(length, nal_initial_alt_cpb_removal_offset[i], 1, i);
  1402. }
  1403. }
  1404. }
  1405. if (hrd->vcl_hrd_parameters_present_flag) {
  1406. for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
  1407. length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
  1408. ubs(length, vcl_initial_cpb_removal_delay[i], 1, i);
  1409. ubs(length, vcl_initial_cpb_removal_offset[i], 1, i);
  1410. if (hrd->sub_pic_hrd_params_present_flag ||
  1411. current->irap_cpb_params_present_flag) {
  1412. ubs(length, vcl_initial_alt_cpb_removal_delay[i], 1, i);
  1413. ubs(length, vcl_initial_alt_cpb_removal_offset[i], 1, i);
  1414. }
  1415. }
  1416. }
  1417. #ifdef READ
  1418. // payload_extension_present() - true if we are before the last 1-bit
  1419. // in the payload structure, which must be in the last byte.
  1420. end_pos = get_bits_count(rw);
  1421. bits_left = *payload_size * 8 - (end_pos - start_pos);
  1422. if (bits_left > 0 &&
  1423. (bits_left > 7 || ff_ctz(show_bits(rw, bits_left)) < bits_left - 1))
  1424. flag(use_alt_cpb_params_flag);
  1425. else
  1426. infer(use_alt_cpb_params_flag, 0);
  1427. #else
  1428. if (current->use_alt_cpb_params_flag)
  1429. flag(use_alt_cpb_params_flag);
  1430. #endif
  1431. return 0;
  1432. }
  1433. static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
  1434. H265RawSEIPicTiming *current)
  1435. {
  1436. CodedBitstreamH265Context *h265 = ctx->priv_data;
  1437. const H265RawSPS *sps;
  1438. const H265RawHRDParameters *hrd;
  1439. int err, expected_source_scan_type, i, length;
  1440. HEADER("Picture Timing");
  1441. sps = h265->active_sps;
  1442. if (!sps) {
  1443. av_log(ctx->log_ctx, AV_LOG_ERROR,
  1444. "No active SPS for pic_timing.\n");
  1445. return AVERROR_INVALIDDATA;
  1446. }
  1447. expected_source_scan_type = 2 -
  1448. 2 * sps->profile_tier_level.general_interlaced_source_flag -
  1449. sps->profile_tier_level.general_progressive_source_flag;
  1450. if (sps->vui.frame_field_info_present_flag) {
  1451. u(4, pic_struct, 0, 12);
  1452. u(2, source_scan_type,
  1453. expected_source_scan_type >= 0 ? expected_source_scan_type : 0,
  1454. expected_source_scan_type >= 0 ? expected_source_scan_type : 2);
  1455. flag(duplicate_flag);
  1456. } else {
  1457. infer(pic_struct, 0);
  1458. infer(source_scan_type,
  1459. expected_source_scan_type >= 0 ? expected_source_scan_type : 2);
  1460. infer(duplicate_flag, 0);
  1461. }
  1462. if (sps->vui_parameters_present_flag &&
  1463. sps->vui.vui_hrd_parameters_present_flag)
  1464. hrd = &sps->vui.hrd_parameters;
  1465. else
  1466. hrd = NULL;
  1467. if (hrd && (hrd->nal_hrd_parameters_present_flag ||
  1468. hrd->vcl_hrd_parameters_present_flag)) {
  1469. length = hrd->au_cpb_removal_delay_length_minus1 + 1;
  1470. ub(length, au_cpb_removal_delay_minus1);
  1471. length = hrd->dpb_output_delay_length_minus1 + 1;
  1472. ub(length, pic_dpb_output_delay);
  1473. if (hrd->sub_pic_hrd_params_present_flag) {
  1474. length = hrd->dpb_output_delay_du_length_minus1 + 1;
  1475. ub(length, pic_dpb_output_du_delay);
  1476. }
  1477. if (hrd->sub_pic_hrd_params_present_flag &&
  1478. hrd->sub_pic_cpb_params_in_pic_timing_sei_flag) {
  1479. // Each decoding unit must contain at least one slice segment.
  1480. ue(num_decoding_units_minus1, 0, HEVC_MAX_SLICE_SEGMENTS);
  1481. flag(du_common_cpb_removal_delay_flag);
  1482. length = hrd->du_cpb_removal_delay_increment_length_minus1 + 1;
  1483. if (current->du_common_cpb_removal_delay_flag)
  1484. ub(length, du_common_cpb_removal_delay_increment_minus1);
  1485. for (i = 0; i <= current->num_decoding_units_minus1; i++) {
  1486. ues(num_nalus_in_du_minus1[i],
  1487. 0, HEVC_MAX_SLICE_SEGMENTS, 1, i);
  1488. if (!current->du_common_cpb_removal_delay_flag &&
  1489. i < current->num_decoding_units_minus1)
  1490. ubs(length, du_cpb_removal_delay_increment_minus1[i], 1, i);
  1491. }
  1492. }
  1493. }
  1494. return 0;
  1495. }
  1496. static int FUNC(sei_pan_scan_rect)(CodedBitstreamContext *ctx, RWContext *rw,
  1497. H265RawSEIPanScanRect *current)
  1498. {
  1499. int err, i;
  1500. HEADER("Pan-Scan Rectangle");
  1501. ue(pan_scan_rect_id, 0, UINT32_MAX - 1);
  1502. flag(pan_scan_rect_cancel_flag);
  1503. if (!current->pan_scan_rect_cancel_flag) {
  1504. ue(pan_scan_cnt_minus1, 0, 2);
  1505. for (i = 0; i <= current->pan_scan_cnt_minus1; i++) {
  1506. ses(pan_scan_rect_left_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  1507. ses(pan_scan_rect_right_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  1508. ses(pan_scan_rect_top_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  1509. ses(pan_scan_rect_bottom_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  1510. }
  1511. flag(pan_scan_rect_persistence_flag);
  1512. }
  1513. return 0;
  1514. }
  1515. static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
  1516. H265RawSEIUserDataRegistered *current,
  1517. uint32_t *payload_size)
  1518. {
  1519. int err, i, j;
  1520. HEADER("User Data Registered ITU-T T.35");
  1521. u(8, itu_t_t35_country_code, 0x00, 0xff);
  1522. if (current->itu_t_t35_country_code != 0xff)
  1523. i = 1;
  1524. else {
  1525. u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
  1526. i = 2;
  1527. }
  1528. #ifdef READ
  1529. if (*payload_size < i) {
  1530. av_log(ctx->log_ctx, AV_LOG_ERROR,
  1531. "Invalid SEI user data registered payload.\n");
  1532. return AVERROR_INVALIDDATA;
  1533. }
  1534. current->data_length = *payload_size - i;
  1535. #else
  1536. *payload_size = i + current->data_length;
  1537. #endif
  1538. allocate(current->data, current->data_length);
  1539. for (j = 0; j < current->data_length; j++)
  1540. xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
  1541. return 0;
  1542. }
  1543. static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
  1544. H265RawSEIUserDataUnregistered *current,
  1545. uint32_t *payload_size)
  1546. {
  1547. int err, i;
  1548. HEADER("User Data Unregistered");
  1549. #ifdef READ
  1550. if (*payload_size < 16) {
  1551. av_log(ctx->log_ctx, AV_LOG_ERROR,
  1552. "Invalid SEI user data unregistered payload.\n");
  1553. return AVERROR_INVALIDDATA;
  1554. }
  1555. current->data_length = *payload_size - 16;
  1556. #else
  1557. *payload_size = 16 + current->data_length;
  1558. #endif
  1559. for (i = 0; i < 16; i++)
  1560. us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
  1561. allocate(current->data, current->data_length);
  1562. for (i = 0; i < current->data_length; i++)
  1563. xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
  1564. return 0;
  1565. }
  1566. static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
  1567. H265RawSEIRecoveryPoint *current)
  1568. {
  1569. int err;
  1570. HEADER("Recovery Point");
  1571. se(recovery_poc_cnt, -32768, 32767);
  1572. flag(exact_match_flag);
  1573. flag(broken_link_flag);
  1574. return 0;
  1575. }
  1576. static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
  1577. H265RawSEIDisplayOrientation *current)
  1578. {
  1579. int err;
  1580. HEADER("Display Orientation");
  1581. flag(display_orientation_cancel_flag);
  1582. if (!current->display_orientation_cancel_flag) {
  1583. flag(hor_flip);
  1584. flag(ver_flip);
  1585. ub(16, anticlockwise_rotation);
  1586. flag(display_orientation_persistence_flag);
  1587. }
  1588. return 0;
  1589. }
  1590. static int FUNC(sei_active_parameter_sets)(CodedBitstreamContext *ctx, RWContext *rw,
  1591. H265RawSEIActiveParameterSets *current)
  1592. {
  1593. CodedBitstreamH265Context *h265 = ctx->priv_data;
  1594. const H265RawVPS *vps;
  1595. int err, i;
  1596. HEADER("Active Parameter Sets");
  1597. u(4, active_video_parameter_set_id, 0, HEVC_MAX_VPS_COUNT);
  1598. vps = h265->vps[current->active_video_parameter_set_id];
  1599. if (!vps) {
  1600. av_log(ctx->log_ctx, AV_LOG_ERROR, "VPS id %d not available for active "
  1601. "parameter sets.\n", current->active_video_parameter_set_id);
  1602. return AVERROR_INVALIDDATA;
  1603. }
  1604. h265->active_vps = vps;
  1605. flag(self_contained_cvs_flag);
  1606. flag(no_parameter_set_update_flag);
  1607. ue(num_sps_ids_minus1, 0, HEVC_MAX_SPS_COUNT - 1);
  1608. for (i = 0; i <= current->num_sps_ids_minus1; i++)
  1609. ues(active_seq_parameter_set_id[i], 0, HEVC_MAX_SPS_COUNT - 1, 1, i);
  1610. for (i = vps->vps_base_layer_internal_flag;
  1611. i <= FFMIN(62, vps->vps_max_layers_minus1); i++) {
  1612. ues(layer_sps_idx[i], 0, current->num_sps_ids_minus1, 1, i);
  1613. if (i == 0)
  1614. h265->active_sps = h265->sps[current->active_seq_parameter_set_id[current->layer_sps_idx[0]]];
  1615. }
  1616. return 0;
  1617. }
  1618. static int FUNC(sei_decoded_picture_hash)(CodedBitstreamContext *ctx, RWContext *rw,
  1619. H265RawSEIDecodedPictureHash *current)
  1620. {
  1621. CodedBitstreamH265Context *h265 = ctx->priv_data;
  1622. const H265RawSPS *sps = h265->active_sps;
  1623. int err, c, i;
  1624. HEADER("Decoded Picture Hash");
  1625. if (!sps) {
  1626. av_log(ctx->log_ctx, AV_LOG_ERROR,
  1627. "No active SPS for decoded picture hash.\n");
  1628. return AVERROR_INVALIDDATA;
  1629. }
  1630. u(8, hash_type, 0, 2);
  1631. for (c = 0; c < (sps->chroma_format_idc == 0 ? 1 : 3); c++) {
  1632. if (current->hash_type == 0) {
  1633. for (i = 0; i < 16; i++)
  1634. us(8, picture_md5[c][i], 0x00, 0xff, 2, c, i);
  1635. } else if (current->hash_type == 1) {
  1636. us(16, picture_crc[c], 0x0000, 0xffff, 1, c);
  1637. } else if (current->hash_type == 2) {
  1638. us(32, picture_checksum[c], 0x00000000, 0xffffffff, 1, c);
  1639. }
  1640. }
  1641. return 0;
  1642. }
  1643. static int FUNC(sei_time_code)(CodedBitstreamContext *ctx, RWContext *rw,
  1644. H265RawSEITimeCode *current)
  1645. {
  1646. int err, i;
  1647. HEADER("Time Code");
  1648. u(2, num_clock_ts, 1, 3);
  1649. for (i = 0; i < current->num_clock_ts; i++) {
  1650. flags(clock_timestamp_flag[i], 1, i);
  1651. if (current->clock_timestamp_flag[i]) {
  1652. flags(units_field_based_flag[i], 1, i);
  1653. us(5, counting_type[i], 0, 6, 1, i);
  1654. flags(full_timestamp_flag[i], 1, i);
  1655. flags(discontinuity_flag[i], 1, i);
  1656. flags(cnt_dropped_flag[i], 1, i);
  1657. ubs(9, n_frames[i], 1, i);
  1658. if (current->full_timestamp_flag[i]) {
  1659. us(6, seconds_value[i], 0, 59, 1, i);
  1660. us(6, minutes_value[i], 0, 59, 1, i);
  1661. us(5, hours_value[i], 0, 23, 1, i);
  1662. } else {
  1663. flags(seconds_flag[i], 1, i);
  1664. if (current->seconds_flag[i]) {
  1665. us(6, seconds_value[i], 0, 59, 1, i);
  1666. flags(minutes_flag[i], 1, i);
  1667. if (current->minutes_flag[i]) {
  1668. us(6, minutes_value[i], 0, 59, 1, i);
  1669. flags(hours_flag[i], 1, i);
  1670. if (current->hours_flag[i])
  1671. us(5, hours_value[i], 0, 23, 1, i);
  1672. }
  1673. }
  1674. }
  1675. ubs(5, time_offset_length[i], 1, i);
  1676. if (current->time_offset_length[i] > 0)
  1677. ibs(current->time_offset_length[i], time_offset_value[i], 1, i);
  1678. else
  1679. infer(time_offset_value[i], 0);
  1680. }
  1681. }
  1682. return 0;
  1683. }
  1684. static int FUNC(sei_mastering_display)(CodedBitstreamContext *ctx, RWContext *rw,
  1685. H265RawSEIMasteringDisplayColourVolume *current)
  1686. {
  1687. int err, c;
  1688. HEADER("Mastering Display Colour Volume");
  1689. for (c = 0; c < 3; c++) {
  1690. us(16, display_primaries_x[c], 0, 50000, 1, c);
  1691. us(16, display_primaries_y[c], 0, 50000, 1, c);
  1692. }
  1693. u(16, white_point_x, 0, 50000);
  1694. u(16, white_point_y, 0, 50000);
  1695. u(32, max_display_mastering_luminance,
  1696. 1, MAX_UINT_BITS(32));
  1697. u(32, min_display_mastering_luminance,
  1698. 0, current->max_display_mastering_luminance - 1);
  1699. return 0;
  1700. }
  1701. static int FUNC(sei_content_light_level)(CodedBitstreamContext *ctx, RWContext *rw,
  1702. H265RawSEIContentLightLevelInfo *current)
  1703. {
  1704. int err;
  1705. HEADER("Content Light Level");
  1706. ub(16, max_content_light_level);
  1707. ub(16, max_pic_average_light_level);
  1708. return 0;
  1709. }
  1710. static int FUNC(sei_alternative_transfer_characteristics)(CodedBitstreamContext *ctx,
  1711. RWContext *rw,
  1712. H265RawSEIAlternativeTransferCharacteristics *current)
  1713. {
  1714. int err;
  1715. HEADER("Alternative Transfer Characteristics");
  1716. ub(8, preferred_transfer_characteristics);
  1717. return 0;
  1718. }
  1719. static int FUNC(sei_alpha_channel_info)(CodedBitstreamContext *ctx,
  1720. RWContext *rw,
  1721. H265RawSEIAlphaChannelInfo *current)
  1722. {
  1723. int err, length;
  1724. HEADER("Alpha Channel Information");
  1725. flag(alpha_channel_cancel_flag);
  1726. if (!current->alpha_channel_cancel_flag) {
  1727. ub(3, alpha_channel_use_idc);
  1728. ub(3, alpha_channel_bit_depth_minus8);
  1729. length = current->alpha_channel_bit_depth_minus8 + 9;
  1730. ub(length, alpha_transparent_value);
  1731. ub(length, alpha_opaque_value);
  1732. flag(alpha_channel_incr_flag);
  1733. flag(alpha_channel_clip_flag);
  1734. if (current->alpha_channel_clip_flag)
  1735. flag(alpha_channel_clip_type_flag);
  1736. } else {
  1737. infer(alpha_channel_use_idc, 2);
  1738. infer(alpha_channel_incr_flag, 0);
  1739. infer(alpha_channel_clip_flag, 0);
  1740. }
  1741. return 0;
  1742. }
  1743. static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
  1744. H265RawSEIPayload *current, int prefix)
  1745. {
  1746. int err, i;
  1747. int start_position, end_position;
  1748. #ifdef READ
  1749. start_position = get_bits_count(rw);
  1750. #else
  1751. start_position = put_bits_count(rw);
  1752. #endif
  1753. switch (current->payload_type) {
  1754. #define SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid) do { \
  1755. if (prefix && !prefix_valid) { \
  1756. av_log(ctx->log_ctx, AV_LOG_ERROR, "SEI type %s invalid " \
  1757. "as prefix SEI!\n", #name); \
  1758. return AVERROR_INVALIDDATA; \
  1759. } \
  1760. if (!prefix && !suffix_valid) { \
  1761. av_log(ctx->log_ctx, AV_LOG_ERROR, "SEI type %s invalid " \
  1762. "as suffix SEI!\n", #name); \
  1763. return AVERROR_INVALIDDATA; \
  1764. } \
  1765. } while (0)
  1766. #define SEI_TYPE_N(type, prefix_valid, suffix_valid, name) \
  1767. case HEVC_SEI_TYPE_ ## type: \
  1768. SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid); \
  1769. CHECK(FUNC(sei_ ## name)(ctx, rw, &current->payload.name)); \
  1770. break
  1771. #define SEI_TYPE_S(type, prefix_valid, suffix_valid, name) \
  1772. case HEVC_SEI_TYPE_ ## type: \
  1773. SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid); \
  1774. CHECK(FUNC(sei_ ## name)(ctx, rw, &current->payload.name, \
  1775. &current->payload_size)); \
  1776. break
  1777. SEI_TYPE_S(BUFFERING_PERIOD, 1, 0, buffering_period);
  1778. SEI_TYPE_N(PICTURE_TIMING, 1, 0, pic_timing);
  1779. SEI_TYPE_N(PAN_SCAN_RECT, 1, 0, pan_scan_rect);
  1780. SEI_TYPE_S(USER_DATA_REGISTERED_ITU_T_T35,
  1781. 1, 1, user_data_registered);
  1782. SEI_TYPE_S(USER_DATA_UNREGISTERED, 1, 1, user_data_unregistered);
  1783. SEI_TYPE_N(RECOVERY_POINT, 1, 0, recovery_point);
  1784. SEI_TYPE_N(DISPLAY_ORIENTATION, 1, 0, display_orientation);
  1785. SEI_TYPE_N(ACTIVE_PARAMETER_SETS, 1, 0, active_parameter_sets);
  1786. SEI_TYPE_N(DECODED_PICTURE_HASH, 0, 1, decoded_picture_hash);
  1787. SEI_TYPE_N(TIME_CODE, 1, 0, time_code);
  1788. SEI_TYPE_N(MASTERING_DISPLAY_INFO, 1, 0, mastering_display);
  1789. SEI_TYPE_N(CONTENT_LIGHT_LEVEL_INFO, 1, 0, content_light_level);
  1790. SEI_TYPE_N(ALTERNATIVE_TRANSFER_CHARACTERISTICS,
  1791. 1, 0, alternative_transfer_characteristics);
  1792. SEI_TYPE_N(ALPHA_CHANNEL_INFO, 1, 0, alpha_channel_info);
  1793. #undef SEI_TYPE
  1794. default:
  1795. {
  1796. #ifdef READ
  1797. current->payload.other.data_length = current->payload_size;
  1798. #endif
  1799. allocate(current->payload.other.data, current->payload.other.data_length);
  1800. for (i = 0; i < current->payload_size; i++)
  1801. xu(8, payload_byte[i], current->payload.other.data[i], 0, 255,
  1802. 1, i);
  1803. }
  1804. }
  1805. if (byte_alignment(rw)) {
  1806. fixed(1, bit_equal_to_one, 1);
  1807. while (byte_alignment(rw))
  1808. fixed(1, bit_equal_to_zero, 0);
  1809. }
  1810. #ifdef READ
  1811. end_position = get_bits_count(rw);
  1812. if (end_position < start_position + 8 * current->payload_size) {
  1813. av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
  1814. "header %"PRIu32" bits, actually %d bits.\n",
  1815. 8 * current->payload_size,
  1816. end_position - start_position);
  1817. return AVERROR_INVALIDDATA;
  1818. }
  1819. #else
  1820. end_position = put_bits_count(rw);
  1821. current->payload_size = (end_position - start_position) >> 3;
  1822. #endif
  1823. return 0;
  1824. }
  1825. static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
  1826. H265RawSEI *current, int prefix)
  1827. {
  1828. int err, k;
  1829. if (prefix)
  1830. HEADER("Prefix Supplemental Enhancement Information");
  1831. else
  1832. HEADER("Suffix Supplemental Enhancement Information");
  1833. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  1834. prefix ? HEVC_NAL_SEI_PREFIX
  1835. : HEVC_NAL_SEI_SUFFIX));
  1836. #ifdef READ
  1837. for (k = 0; k < H265_MAX_SEI_PAYLOADS; k++) {
  1838. uint32_t payload_type = 0;
  1839. uint32_t payload_size = 0;
  1840. uint32_t tmp;
  1841. while (show_bits(rw, 8) == 0xff) {
  1842. fixed(8, ff_byte, 0xff);
  1843. payload_type += 255;
  1844. }
  1845. xu(8, last_payload_type_byte, tmp, 0, 254, 0);
  1846. payload_type += tmp;
  1847. while (show_bits(rw, 8) == 0xff) {
  1848. fixed(8, ff_byte, 0xff);
  1849. payload_size += 255;
  1850. }
  1851. xu(8, last_payload_size_byte, tmp, 0, 254, 0);
  1852. payload_size += tmp;
  1853. current->payload[k].payload_type = payload_type;
  1854. current->payload[k].payload_size = payload_size;
  1855. CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k], prefix));
  1856. if (!cbs_h2645_read_more_rbsp_data(rw))
  1857. break;
  1858. }
  1859. if (k >= H265_MAX_SEI_PAYLOADS) {
  1860. av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
  1861. "SEI message: found %d.\n", k);
  1862. return AVERROR_INVALIDDATA;
  1863. }
  1864. current->payload_count = k + 1;
  1865. #else
  1866. for (k = 0; k < current->payload_count; k++) {
  1867. PutBitContext start_state;
  1868. uint32_t tmp;
  1869. int need_size, i;
  1870. // Somewhat clumsy: we write the payload twice when
  1871. // we don't know the size in advance. This will mess
  1872. // with trace output, but is otherwise harmless.
  1873. start_state = *rw;
  1874. need_size = !current->payload[k].payload_size;
  1875. for (i = 0; i < 1 + need_size; i++) {
  1876. *rw = start_state;
  1877. tmp = current->payload[k].payload_type;
  1878. while (tmp >= 255) {
  1879. fixed(8, ff_byte, 0xff);
  1880. tmp -= 255;
  1881. }
  1882. xu(8, last_payload_type_byte, tmp, 0, 254, 0);
  1883. tmp = current->payload[k].payload_size;
  1884. while (tmp >= 255) {
  1885. fixed(8, ff_byte, 0xff);
  1886. tmp -= 255;
  1887. }
  1888. xu(8, last_payload_size_byte, tmp, 0, 254, 0);
  1889. CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k], prefix));
  1890. }
  1891. }
  1892. #endif
  1893. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  1894. return 0;
  1895. }