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.

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