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.

3502 lines
133KB

  1. /*
  2. * HEVC video Decoder
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. * Copyright (C) 2012 - 2013 Mickael Raulet
  6. * Copyright (C) 2012 - 2013 Gildas Cocherel
  7. * Copyright (C) 2012 - 2013 Wassim Hamidouche
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include "libavutil/atomic.h"
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/display.h"
  29. #include "libavutil/internal.h"
  30. #include "libavutil/md5.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/stereo3d.h"
  34. #include "bswapdsp.h"
  35. #include "bytestream.h"
  36. #include "cabac_functions.h"
  37. #include "golomb.h"
  38. #include "hevc.h"
  39. const uint8_t ff_hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
  40. /**
  41. * NOTE: Each function hls_foo correspond to the function foo in the
  42. * specification (HLS stands for High Level Syntax).
  43. */
  44. /**
  45. * Section 5.7
  46. */
  47. /* free everything allocated by pic_arrays_init() */
  48. static void pic_arrays_free(HEVCContext *s)
  49. {
  50. av_freep(&s->sao);
  51. av_freep(&s->deblock);
  52. av_freep(&s->skip_flag);
  53. av_freep(&s->tab_ct_depth);
  54. av_freep(&s->tab_ipm);
  55. av_freep(&s->cbf_luma);
  56. av_freep(&s->is_pcm);
  57. av_freep(&s->qp_y_tab);
  58. av_freep(&s->tab_slice_address);
  59. av_freep(&s->filter_slice_edges);
  60. av_freep(&s->horizontal_bs);
  61. av_freep(&s->vertical_bs);
  62. av_freep(&s->sh.entry_point_offset);
  63. av_freep(&s->sh.size);
  64. av_freep(&s->sh.offset);
  65. av_buffer_pool_uninit(&s->tab_mvf_pool);
  66. av_buffer_pool_uninit(&s->rpl_tab_pool);
  67. }
  68. /* allocate arrays that depend on frame dimensions */
  69. static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
  70. {
  71. int log2_min_cb_size = sps->log2_min_cb_size;
  72. int width = sps->width;
  73. int height = sps->height;
  74. int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
  75. ((height >> log2_min_cb_size) + 1);
  76. int ctb_count = sps->ctb_width * sps->ctb_height;
  77. int min_pu_size = sps->min_pu_width * sps->min_pu_height;
  78. s->bs_width = (width >> 2) + 1;
  79. s->bs_height = (height >> 2) + 1;
  80. s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
  81. s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
  82. if (!s->sao || !s->deblock)
  83. goto fail;
  84. s->skip_flag = av_malloc(sps->min_cb_height * sps->min_cb_width);
  85. s->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
  86. if (!s->skip_flag || !s->tab_ct_depth)
  87. goto fail;
  88. s->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
  89. s->tab_ipm = av_mallocz(min_pu_size);
  90. s->is_pcm = av_malloc((sps->min_pu_width + 1) * (sps->min_pu_height + 1));
  91. if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
  92. goto fail;
  93. s->filter_slice_edges = av_mallocz(ctb_count);
  94. s->tab_slice_address = av_malloc_array(pic_size_in_ctb,
  95. sizeof(*s->tab_slice_address));
  96. s->qp_y_tab = av_malloc_array(pic_size_in_ctb,
  97. sizeof(*s->qp_y_tab));
  98. if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
  99. goto fail;
  100. s->horizontal_bs = av_mallocz_array(s->bs_width, s->bs_height);
  101. s->vertical_bs = av_mallocz_array(s->bs_width, s->bs_height);
  102. if (!s->horizontal_bs || !s->vertical_bs)
  103. goto fail;
  104. s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
  105. av_buffer_allocz);
  106. s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
  107. av_buffer_allocz);
  108. if (!s->tab_mvf_pool || !s->rpl_tab_pool)
  109. goto fail;
  110. return 0;
  111. fail:
  112. pic_arrays_free(s);
  113. return AVERROR(ENOMEM);
  114. }
  115. static void pred_weight_table(HEVCContext *s, GetBitContext *gb)
  116. {
  117. int i = 0;
  118. int j = 0;
  119. uint8_t luma_weight_l0_flag[16];
  120. uint8_t chroma_weight_l0_flag[16];
  121. uint8_t luma_weight_l1_flag[16];
  122. uint8_t chroma_weight_l1_flag[16];
  123. s->sh.luma_log2_weight_denom = av_clip_c(get_ue_golomb_long(gb), 0, 7);
  124. if (s->sps->chroma_format_idc != 0) {
  125. int delta = get_se_golomb(gb);
  126. s->sh.chroma_log2_weight_denom = av_clip(s->sh.luma_log2_weight_denom + delta, 0, 7);
  127. }
  128. for (i = 0; i < s->sh.nb_refs[L0]; i++) {
  129. luma_weight_l0_flag[i] = get_bits1(gb);
  130. if (!luma_weight_l0_flag[i]) {
  131. s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
  132. s->sh.luma_offset_l0[i] = 0;
  133. }
  134. }
  135. if (s->sps->chroma_format_idc != 0) {
  136. for (i = 0; i < s->sh.nb_refs[L0]; i++)
  137. chroma_weight_l0_flag[i] = get_bits1(gb);
  138. } else {
  139. for (i = 0; i < s->sh.nb_refs[L0]; i++)
  140. chroma_weight_l0_flag[i] = 0;
  141. }
  142. for (i = 0; i < s->sh.nb_refs[L0]; i++) {
  143. if (luma_weight_l0_flag[i]) {
  144. int delta_luma_weight_l0 = get_se_golomb(gb);
  145. s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
  146. s->sh.luma_offset_l0[i] = get_se_golomb(gb);
  147. }
  148. if (chroma_weight_l0_flag[i]) {
  149. for (j = 0; j < 2; j++) {
  150. int delta_chroma_weight_l0 = get_se_golomb(gb);
  151. int delta_chroma_offset_l0 = get_se_golomb(gb);
  152. s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
  153. s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
  154. >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
  155. }
  156. } else {
  157. s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
  158. s->sh.chroma_offset_l0[i][0] = 0;
  159. s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
  160. s->sh.chroma_offset_l0[i][1] = 0;
  161. }
  162. }
  163. if (s->sh.slice_type == B_SLICE) {
  164. for (i = 0; i < s->sh.nb_refs[L1]; i++) {
  165. luma_weight_l1_flag[i] = get_bits1(gb);
  166. if (!luma_weight_l1_flag[i]) {
  167. s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
  168. s->sh.luma_offset_l1[i] = 0;
  169. }
  170. }
  171. if (s->sps->chroma_format_idc != 0) {
  172. for (i = 0; i < s->sh.nb_refs[L1]; i++)
  173. chroma_weight_l1_flag[i] = get_bits1(gb);
  174. } else {
  175. for (i = 0; i < s->sh.nb_refs[L1]; i++)
  176. chroma_weight_l1_flag[i] = 0;
  177. }
  178. for (i = 0; i < s->sh.nb_refs[L1]; i++) {
  179. if (luma_weight_l1_flag[i]) {
  180. int delta_luma_weight_l1 = get_se_golomb(gb);
  181. s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
  182. s->sh.luma_offset_l1[i] = get_se_golomb(gb);
  183. }
  184. if (chroma_weight_l1_flag[i]) {
  185. for (j = 0; j < 2; j++) {
  186. int delta_chroma_weight_l1 = get_se_golomb(gb);
  187. int delta_chroma_offset_l1 = get_se_golomb(gb);
  188. s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
  189. s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
  190. >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
  191. }
  192. } else {
  193. s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
  194. s->sh.chroma_offset_l1[i][0] = 0;
  195. s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
  196. s->sh.chroma_offset_l1[i][1] = 0;
  197. }
  198. }
  199. }
  200. }
  201. static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
  202. {
  203. const HEVCSPS *sps = s->sps;
  204. int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
  205. int prev_delta_msb = 0;
  206. unsigned int nb_sps = 0, nb_sh;
  207. int i;
  208. rps->nb_refs = 0;
  209. if (!sps->long_term_ref_pics_present_flag)
  210. return 0;
  211. if (sps->num_long_term_ref_pics_sps > 0)
  212. nb_sps = get_ue_golomb_long(gb);
  213. nb_sh = get_ue_golomb_long(gb);
  214. if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
  215. return AVERROR_INVALIDDATA;
  216. rps->nb_refs = nb_sh + nb_sps;
  217. for (i = 0; i < rps->nb_refs; i++) {
  218. uint8_t delta_poc_msb_present;
  219. if (i < nb_sps) {
  220. uint8_t lt_idx_sps = 0;
  221. if (sps->num_long_term_ref_pics_sps > 1)
  222. lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
  223. rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
  224. rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
  225. } else {
  226. rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
  227. rps->used[i] = get_bits1(gb);
  228. }
  229. delta_poc_msb_present = get_bits1(gb);
  230. if (delta_poc_msb_present) {
  231. int delta = get_ue_golomb_long(gb);
  232. if (i && i != nb_sps)
  233. delta += prev_delta_msb;
  234. rps->poc[i] += s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
  235. prev_delta_msb = delta;
  236. }
  237. }
  238. return 0;
  239. }
  240. static int get_buffer_sao(HEVCContext *s, AVFrame *frame, const HEVCSPS *sps)
  241. {
  242. int ret, i;
  243. frame->width = s->avctx->coded_width + 2;
  244. frame->height = s->avctx->coded_height + 2;
  245. if ((ret = ff_get_buffer(s->avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  246. return ret;
  247. for (i = 0; frame->data[i]; i++) {
  248. int offset = frame->linesize[i] + (1 << sps->pixel_shift);
  249. frame->data[i] += offset;
  250. }
  251. frame->width = s->avctx->coded_width;
  252. frame->height = s->avctx->coded_height;
  253. return 0;
  254. }
  255. static int set_sps(HEVCContext *s, const HEVCSPS *sps)
  256. {
  257. int ret;
  258. unsigned int num = 0, den = 0;
  259. pic_arrays_free(s);
  260. ret = pic_arrays_init(s, sps);
  261. if (ret < 0)
  262. goto fail;
  263. s->avctx->coded_width = sps->width;
  264. s->avctx->coded_height = sps->height;
  265. s->avctx->width = sps->output_width;
  266. s->avctx->height = sps->output_height;
  267. s->avctx->pix_fmt = sps->pix_fmt;
  268. s->avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
  269. ff_set_sar(s->avctx, sps->vui.sar);
  270. if (sps->vui.video_signal_type_present_flag)
  271. s->avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
  272. : AVCOL_RANGE_MPEG;
  273. else
  274. s->avctx->color_range = AVCOL_RANGE_MPEG;
  275. if (sps->vui.colour_description_present_flag) {
  276. s->avctx->color_primaries = sps->vui.colour_primaries;
  277. s->avctx->color_trc = sps->vui.transfer_characteristic;
  278. s->avctx->colorspace = sps->vui.matrix_coeffs;
  279. } else {
  280. s->avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
  281. s->avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
  282. s->avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
  283. }
  284. ff_hevc_pred_init(&s->hpc, sps->bit_depth);
  285. ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
  286. ff_videodsp_init (&s->vdsp, sps->bit_depth);
  287. if (sps->sao_enabled) {
  288. av_frame_unref(s->tmp_frame);
  289. ret = get_buffer_sao(s, s->tmp_frame, sps);
  290. s->sao_frame = s->tmp_frame;
  291. }
  292. s->sps = sps;
  293. s->vps = (HEVCVPS*) s->vps_list[s->sps->vps_id]->data;
  294. if (s->vps->vps_timing_info_present_flag) {
  295. num = s->vps->vps_num_units_in_tick;
  296. den = s->vps->vps_time_scale;
  297. } else if (sps->vui.vui_timing_info_present_flag) {
  298. num = sps->vui.vui_num_units_in_tick;
  299. den = sps->vui.vui_time_scale;
  300. }
  301. if (num != 0 && den != 0)
  302. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  303. num, den, 1 << 30);
  304. return 0;
  305. fail:
  306. pic_arrays_free(s);
  307. s->sps = NULL;
  308. return ret;
  309. }
  310. static int hls_slice_header(HEVCContext *s)
  311. {
  312. GetBitContext *gb = &s->HEVClc->gb;
  313. SliceHeader *sh = &s->sh;
  314. int i, j, ret;
  315. // Coded parameters
  316. sh->first_slice_in_pic_flag = get_bits1(gb);
  317. if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
  318. s->seq_decode = (s->seq_decode + 1) & 0xff;
  319. s->max_ra = INT_MAX;
  320. if (IS_IDR(s))
  321. ff_hevc_clear_refs(s);
  322. }
  323. sh->no_output_of_prior_pics_flag = 0;
  324. if (IS_IRAP(s))
  325. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  326. sh->pps_id = get_ue_golomb_long(gb);
  327. if (sh->pps_id >= MAX_PPS_COUNT || !s->pps_list[sh->pps_id]) {
  328. av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  329. return AVERROR_INVALIDDATA;
  330. }
  331. if (!sh->first_slice_in_pic_flag &&
  332. s->pps != (HEVCPPS*)s->pps_list[sh->pps_id]->data) {
  333. av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
  334. return AVERROR_INVALIDDATA;
  335. }
  336. s->pps = (HEVCPPS*)s->pps_list[sh->pps_id]->data;
  337. if (s->nal_unit_type == NAL_CRA_NUT && s->last_eos == 1)
  338. sh->no_output_of_prior_pics_flag = 1;
  339. if (s->sps != (HEVCSPS*)s->sps_list[s->pps->sps_id]->data) {
  340. const HEVCSPS* last_sps = s->sps;
  341. s->sps = (HEVCSPS*)s->sps_list[s->pps->sps_id]->data;
  342. if (last_sps && IS_IRAP(s) && s->nal_unit_type != NAL_CRA_NUT) {
  343. if (s->sps->width != last_sps->width || s->sps->height != last_sps->height ||
  344. s->sps->temporal_layer[s->sps->max_sub_layers - 1].max_dec_pic_buffering !=
  345. last_sps->temporal_layer[last_sps->max_sub_layers - 1].max_dec_pic_buffering)
  346. sh->no_output_of_prior_pics_flag = 0;
  347. }
  348. ff_hevc_clear_refs(s);
  349. ret = set_sps(s, s->sps);
  350. if (ret < 0)
  351. return ret;
  352. s->seq_decode = (s->seq_decode + 1) & 0xff;
  353. s->max_ra = INT_MAX;
  354. }
  355. s->avctx->profile = s->sps->ptl.general_ptl.profile_idc;
  356. s->avctx->level = s->sps->ptl.general_ptl.level_idc;
  357. sh->dependent_slice_segment_flag = 0;
  358. if (!sh->first_slice_in_pic_flag) {
  359. int slice_address_length;
  360. if (s->pps->dependent_slice_segments_enabled_flag)
  361. sh->dependent_slice_segment_flag = get_bits1(gb);
  362. slice_address_length = av_ceil_log2(s->sps->ctb_width *
  363. s->sps->ctb_height);
  364. sh->slice_segment_addr = get_bits(gb, slice_address_length);
  365. if (sh->slice_segment_addr >= s->sps->ctb_width * s->sps->ctb_height) {
  366. av_log(s->avctx, AV_LOG_ERROR,
  367. "Invalid slice segment address: %u.\n",
  368. sh->slice_segment_addr);
  369. return AVERROR_INVALIDDATA;
  370. }
  371. if (!sh->dependent_slice_segment_flag) {
  372. sh->slice_addr = sh->slice_segment_addr;
  373. s->slice_idx++;
  374. }
  375. } else {
  376. sh->slice_segment_addr = sh->slice_addr = 0;
  377. s->slice_idx = 0;
  378. s->slice_initialized = 0;
  379. }
  380. if (!sh->dependent_slice_segment_flag) {
  381. s->slice_initialized = 0;
  382. for (i = 0; i < s->pps->num_extra_slice_header_bits; i++)
  383. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  384. sh->slice_type = get_ue_golomb_long(gb);
  385. if (!(sh->slice_type == I_SLICE ||
  386. sh->slice_type == P_SLICE ||
  387. sh->slice_type == B_SLICE)) {
  388. av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  389. sh->slice_type);
  390. return AVERROR_INVALIDDATA;
  391. }
  392. if (IS_IRAP(s) && sh->slice_type != I_SLICE) {
  393. av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
  394. return AVERROR_INVALIDDATA;
  395. }
  396. // when flag is not present, picture is inferred to be output
  397. sh->pic_output_flag = 1;
  398. if (s->pps->output_flag_present_flag)
  399. sh->pic_output_flag = get_bits1(gb);
  400. if (s->sps->separate_colour_plane_flag)
  401. sh->colour_plane_id = get_bits(gb, 2);
  402. if (!IS_IDR(s)) {
  403. int short_term_ref_pic_set_sps_flag, poc;
  404. sh->pic_order_cnt_lsb = get_bits(gb, s->sps->log2_max_poc_lsb);
  405. poc = ff_hevc_compute_poc(s, sh->pic_order_cnt_lsb);
  406. if (!sh->first_slice_in_pic_flag && poc != s->poc) {
  407. av_log(s->avctx, AV_LOG_WARNING,
  408. "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
  409. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  410. return AVERROR_INVALIDDATA;
  411. poc = s->poc;
  412. }
  413. s->poc = poc;
  414. short_term_ref_pic_set_sps_flag = get_bits1(gb);
  415. if (!short_term_ref_pic_set_sps_flag) {
  416. ret = ff_hevc_decode_short_term_rps(s, &sh->slice_rps, s->sps, 1);
  417. if (ret < 0)
  418. return ret;
  419. sh->short_term_rps = &sh->slice_rps;
  420. } else {
  421. int numbits, rps_idx;
  422. if (!s->sps->nb_st_rps) {
  423. av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
  424. return AVERROR_INVALIDDATA;
  425. }
  426. numbits = av_ceil_log2(s->sps->nb_st_rps);
  427. rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
  428. sh->short_term_rps = &s->sps->st_rps[rps_idx];
  429. }
  430. ret = decode_lt_rps(s, &sh->long_term_rps, gb);
  431. if (ret < 0) {
  432. av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
  433. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  434. return AVERROR_INVALIDDATA;
  435. }
  436. if (s->sps->sps_temporal_mvp_enabled_flag)
  437. sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
  438. else
  439. sh->slice_temporal_mvp_enabled_flag = 0;
  440. } else {
  441. s->sh.short_term_rps = NULL;
  442. s->poc = 0;
  443. }
  444. /* 8.3.1 */
  445. if (s->temporal_id == 0 &&
  446. s->nal_unit_type != NAL_TRAIL_N &&
  447. s->nal_unit_type != NAL_TSA_N &&
  448. s->nal_unit_type != NAL_STSA_N &&
  449. s->nal_unit_type != NAL_RADL_N &&
  450. s->nal_unit_type != NAL_RADL_R &&
  451. s->nal_unit_type != NAL_RASL_N &&
  452. s->nal_unit_type != NAL_RASL_R)
  453. s->pocTid0 = s->poc;
  454. if (s->sps->sao_enabled) {
  455. sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
  456. sh->slice_sample_adaptive_offset_flag[1] =
  457. sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
  458. } else {
  459. sh->slice_sample_adaptive_offset_flag[0] = 0;
  460. sh->slice_sample_adaptive_offset_flag[1] = 0;
  461. sh->slice_sample_adaptive_offset_flag[2] = 0;
  462. }
  463. sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
  464. if (sh->slice_type == P_SLICE || sh->slice_type == B_SLICE) {
  465. int nb_refs;
  466. sh->nb_refs[L0] = s->pps->num_ref_idx_l0_default_active;
  467. if (sh->slice_type == B_SLICE)
  468. sh->nb_refs[L1] = s->pps->num_ref_idx_l1_default_active;
  469. if (get_bits1(gb)) { // num_ref_idx_active_override_flag
  470. sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
  471. if (sh->slice_type == B_SLICE)
  472. sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
  473. }
  474. if (sh->nb_refs[L0] > MAX_REFS || sh->nb_refs[L1] > MAX_REFS) {
  475. av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
  476. sh->nb_refs[L0], sh->nb_refs[L1]);
  477. return AVERROR_INVALIDDATA;
  478. }
  479. sh->rpl_modification_flag[0] = 0;
  480. sh->rpl_modification_flag[1] = 0;
  481. nb_refs = ff_hevc_frame_nb_refs(s);
  482. if (!nb_refs) {
  483. av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
  484. return AVERROR_INVALIDDATA;
  485. }
  486. if (s->pps->lists_modification_present_flag && nb_refs > 1) {
  487. sh->rpl_modification_flag[0] = get_bits1(gb);
  488. if (sh->rpl_modification_flag[0]) {
  489. for (i = 0; i < sh->nb_refs[L0]; i++)
  490. sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
  491. }
  492. if (sh->slice_type == B_SLICE) {
  493. sh->rpl_modification_flag[1] = get_bits1(gb);
  494. if (sh->rpl_modification_flag[1] == 1)
  495. for (i = 0; i < sh->nb_refs[L1]; i++)
  496. sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
  497. }
  498. }
  499. if (sh->slice_type == B_SLICE)
  500. sh->mvd_l1_zero_flag = get_bits1(gb);
  501. if (s->pps->cabac_init_present_flag)
  502. sh->cabac_init_flag = get_bits1(gb);
  503. else
  504. sh->cabac_init_flag = 0;
  505. sh->collocated_ref_idx = 0;
  506. if (sh->slice_temporal_mvp_enabled_flag) {
  507. sh->collocated_list = L0;
  508. if (sh->slice_type == B_SLICE)
  509. sh->collocated_list = !get_bits1(gb);
  510. if (sh->nb_refs[sh->collocated_list] > 1) {
  511. sh->collocated_ref_idx = get_ue_golomb_long(gb);
  512. if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
  513. av_log(s->avctx, AV_LOG_ERROR,
  514. "Invalid collocated_ref_idx: %d.\n",
  515. sh->collocated_ref_idx);
  516. return AVERROR_INVALIDDATA;
  517. }
  518. }
  519. }
  520. if ((s->pps->weighted_pred_flag && sh->slice_type == P_SLICE) ||
  521. (s->pps->weighted_bipred_flag && sh->slice_type == B_SLICE)) {
  522. pred_weight_table(s, gb);
  523. }
  524. sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
  525. if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
  526. av_log(s->avctx, AV_LOG_ERROR,
  527. "Invalid number of merging MVP candidates: %d.\n",
  528. sh->max_num_merge_cand);
  529. return AVERROR_INVALIDDATA;
  530. }
  531. }
  532. sh->slice_qp_delta = get_se_golomb(gb);
  533. if (s->pps->pic_slice_level_chroma_qp_offsets_present_flag) {
  534. sh->slice_cb_qp_offset = get_se_golomb(gb);
  535. sh->slice_cr_qp_offset = get_se_golomb(gb);
  536. } else {
  537. sh->slice_cb_qp_offset = 0;
  538. sh->slice_cr_qp_offset = 0;
  539. }
  540. if (s->pps->chroma_qp_offset_list_enabled_flag)
  541. sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
  542. else
  543. sh->cu_chroma_qp_offset_enabled_flag = 0;
  544. if (s->pps->deblocking_filter_control_present_flag) {
  545. int deblocking_filter_override_flag = 0;
  546. if (s->pps->deblocking_filter_override_enabled_flag)
  547. deblocking_filter_override_flag = get_bits1(gb);
  548. if (deblocking_filter_override_flag) {
  549. sh->disable_deblocking_filter_flag = get_bits1(gb);
  550. if (!sh->disable_deblocking_filter_flag) {
  551. sh->beta_offset = get_se_golomb(gb) * 2;
  552. sh->tc_offset = get_se_golomb(gb) * 2;
  553. }
  554. } else {
  555. sh->disable_deblocking_filter_flag = s->pps->disable_dbf;
  556. sh->beta_offset = s->pps->beta_offset;
  557. sh->tc_offset = s->pps->tc_offset;
  558. }
  559. } else {
  560. sh->disable_deblocking_filter_flag = 0;
  561. sh->beta_offset = 0;
  562. sh->tc_offset = 0;
  563. }
  564. if (s->pps->seq_loop_filter_across_slices_enabled_flag &&
  565. (sh->slice_sample_adaptive_offset_flag[0] ||
  566. sh->slice_sample_adaptive_offset_flag[1] ||
  567. !sh->disable_deblocking_filter_flag)) {
  568. sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
  569. } else {
  570. sh->slice_loop_filter_across_slices_enabled_flag = s->pps->seq_loop_filter_across_slices_enabled_flag;
  571. }
  572. } else if (!s->slice_initialized) {
  573. av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
  574. return AVERROR_INVALIDDATA;
  575. }
  576. sh->num_entry_point_offsets = 0;
  577. if (s->pps->tiles_enabled_flag || s->pps->entropy_coding_sync_enabled_flag) {
  578. unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
  579. // It would be possible to bound this tighter but this here is simpler
  580. if (num_entry_point_offsets > get_bits_left(gb)) {
  581. av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
  582. return AVERROR_INVALIDDATA;
  583. }
  584. sh->num_entry_point_offsets = num_entry_point_offsets;
  585. if (sh->num_entry_point_offsets > 0) {
  586. int offset_len = get_ue_golomb_long(gb) + 1;
  587. int segments = offset_len >> 4;
  588. int rest = (offset_len & 15);
  589. if (offset_len < 1 || offset_len > 32) {
  590. sh->num_entry_point_offsets = 0;
  591. av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
  592. return AVERROR_INVALIDDATA;
  593. }
  594. av_freep(&sh->entry_point_offset);
  595. av_freep(&sh->offset);
  596. av_freep(&sh->size);
  597. sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
  598. sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
  599. sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
  600. if (!sh->entry_point_offset || !sh->offset || !sh->size) {
  601. sh->num_entry_point_offsets = 0;
  602. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
  603. return AVERROR(ENOMEM);
  604. }
  605. for (i = 0; i < sh->num_entry_point_offsets; i++) {
  606. int val = 0;
  607. for (j = 0; j < segments; j++) {
  608. val <<= 16;
  609. val += get_bits(gb, 16);
  610. }
  611. if (rest) {
  612. val <<= rest;
  613. val += get_bits(gb, rest);
  614. }
  615. sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
  616. }
  617. if (s->threads_number > 1 && (s->pps->num_tile_rows > 1 || s->pps->num_tile_columns > 1)) {
  618. s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
  619. s->threads_number = 1;
  620. } else
  621. s->enable_parallel_tiles = 0;
  622. } else
  623. s->enable_parallel_tiles = 0;
  624. }
  625. if (s->pps->slice_header_extension_present_flag) {
  626. unsigned int length = get_ue_golomb_long(gb);
  627. if (length*8LL > get_bits_left(gb)) {
  628. av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
  629. return AVERROR_INVALIDDATA;
  630. }
  631. for (i = 0; i < length; i++)
  632. skip_bits(gb, 8); // slice_header_extension_data_byte
  633. }
  634. // Inferred parameters
  635. sh->slice_qp = 26U + s->pps->pic_init_qp_minus26 + sh->slice_qp_delta;
  636. if (sh->slice_qp > 51 ||
  637. sh->slice_qp < -s->sps->qp_bd_offset) {
  638. av_log(s->avctx, AV_LOG_ERROR,
  639. "The slice_qp %d is outside the valid range "
  640. "[%d, 51].\n",
  641. sh->slice_qp,
  642. -s->sps->qp_bd_offset);
  643. return AVERROR_INVALIDDATA;
  644. }
  645. sh->slice_ctb_addr_rs = sh->slice_segment_addr;
  646. if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
  647. av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
  648. return AVERROR_INVALIDDATA;
  649. }
  650. if (get_bits_left(gb) < 0) {
  651. av_log(s->avctx, AV_LOG_ERROR,
  652. "Overread slice header by %d bits\n", -get_bits_left(gb));
  653. return AVERROR_INVALIDDATA;
  654. }
  655. s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
  656. if (!s->pps->cu_qp_delta_enabled_flag)
  657. s->HEVClc->qp_y = s->sh.slice_qp;
  658. s->slice_initialized = 1;
  659. s->HEVClc->tu.cu_qp_offset_cb = 0;
  660. s->HEVClc->tu.cu_qp_offset_cr = 0;
  661. s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == NAL_CRA_NUT && s->last_eos);
  662. return 0;
  663. }
  664. #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
  665. #define SET_SAO(elem, value) \
  666. do { \
  667. if (!sao_merge_up_flag && !sao_merge_left_flag) \
  668. sao->elem = value; \
  669. else if (sao_merge_left_flag) \
  670. sao->elem = CTB(s->sao, rx-1, ry).elem; \
  671. else if (sao_merge_up_flag) \
  672. sao->elem = CTB(s->sao, rx, ry-1).elem; \
  673. else \
  674. sao->elem = 0; \
  675. } while (0)
  676. static void hls_sao_param(HEVCContext *s, int rx, int ry)
  677. {
  678. HEVCLocalContext *lc = s->HEVClc;
  679. int sao_merge_left_flag = 0;
  680. int sao_merge_up_flag = 0;
  681. SAOParams *sao = &CTB(s->sao, rx, ry);
  682. int c_idx, i;
  683. if (s->sh.slice_sample_adaptive_offset_flag[0] ||
  684. s->sh.slice_sample_adaptive_offset_flag[1]) {
  685. if (rx > 0) {
  686. if (lc->ctb_left_flag)
  687. sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
  688. }
  689. if (ry > 0 && !sao_merge_left_flag) {
  690. if (lc->ctb_up_flag)
  691. sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
  692. }
  693. }
  694. for (c_idx = 0; c_idx < 3; c_idx++) {
  695. int log2_sao_offset_scale = c_idx == 0 ? s->pps->log2_sao_offset_scale_luma :
  696. s->pps->log2_sao_offset_scale_chroma;
  697. if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
  698. sao->type_idx[c_idx] = SAO_NOT_APPLIED;
  699. continue;
  700. }
  701. if (c_idx == 2) {
  702. sao->type_idx[2] = sao->type_idx[1];
  703. sao->eo_class[2] = sao->eo_class[1];
  704. } else {
  705. SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
  706. }
  707. if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
  708. continue;
  709. for (i = 0; i < 4; i++)
  710. SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
  711. if (sao->type_idx[c_idx] == SAO_BAND) {
  712. for (i = 0; i < 4; i++) {
  713. if (sao->offset_abs[c_idx][i]) {
  714. SET_SAO(offset_sign[c_idx][i],
  715. ff_hevc_sao_offset_sign_decode(s));
  716. } else {
  717. sao->offset_sign[c_idx][i] = 0;
  718. }
  719. }
  720. SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
  721. } else if (c_idx != 2) {
  722. SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
  723. }
  724. // Inferred parameters
  725. sao->offset_val[c_idx][0] = 0;
  726. for (i = 0; i < 4; i++) {
  727. sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
  728. if (sao->type_idx[c_idx] == SAO_EDGE) {
  729. if (i > 1)
  730. sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
  731. } else if (sao->offset_sign[c_idx][i]) {
  732. sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
  733. }
  734. sao->offset_val[c_idx][i + 1] <<= log2_sao_offset_scale;
  735. }
  736. }
  737. }
  738. #undef SET_SAO
  739. #undef CTB
  740. static int hls_cross_component_pred(HEVCContext *s, int idx) {
  741. HEVCLocalContext *lc = s->HEVClc;
  742. int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
  743. if (log2_res_scale_abs_plus1 != 0) {
  744. int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
  745. lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
  746. (1 - 2 * res_scale_sign_flag);
  747. } else {
  748. lc->tu.res_scale_val = 0;
  749. }
  750. return 0;
  751. }
  752. static int hls_transform_unit(HEVCContext *s, int x0, int y0,
  753. int xBase, int yBase, int cb_xBase, int cb_yBase,
  754. int log2_cb_size, int log2_trafo_size,
  755. int trafo_depth, int blk_idx,
  756. int cbf_luma, int *cbf_cb, int *cbf_cr)
  757. {
  758. HEVCLocalContext *lc = s->HEVClc;
  759. const int log2_trafo_size_c = log2_trafo_size - s->sps->hshift[1];
  760. int i;
  761. if (lc->cu.pred_mode == MODE_INTRA) {
  762. int trafo_size = 1 << log2_trafo_size;
  763. ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
  764. s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
  765. }
  766. if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
  767. (s->sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
  768. int scan_idx = SCAN_DIAG;
  769. int scan_idx_c = SCAN_DIAG;
  770. int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
  771. (s->sps->chroma_format_idc == 2 &&
  772. (cbf_cb[1] || cbf_cr[1]));
  773. if (s->pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
  774. lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
  775. if (lc->tu.cu_qp_delta != 0)
  776. if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
  777. lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
  778. lc->tu.is_cu_qp_delta_coded = 1;
  779. if (lc->tu.cu_qp_delta < -(26 + s->sps->qp_bd_offset / 2) ||
  780. lc->tu.cu_qp_delta > (25 + s->sps->qp_bd_offset / 2)) {
  781. av_log(s->avctx, AV_LOG_ERROR,
  782. "The cu_qp_delta %d is outside the valid range "
  783. "[%d, %d].\n",
  784. lc->tu.cu_qp_delta,
  785. -(26 + s->sps->qp_bd_offset / 2),
  786. (25 + s->sps->qp_bd_offset / 2));
  787. return AVERROR_INVALIDDATA;
  788. }
  789. ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
  790. }
  791. if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
  792. !lc->cu.cu_transquant_bypass_flag && !lc->tu.is_cu_chroma_qp_offset_coded) {
  793. int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
  794. if (cu_chroma_qp_offset_flag) {
  795. int cu_chroma_qp_offset_idx = 0;
  796. if (s->pps->chroma_qp_offset_list_len_minus1 > 0) {
  797. cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
  798. av_log(s->avctx, AV_LOG_ERROR,
  799. "cu_chroma_qp_offset_idx not yet tested.\n");
  800. }
  801. lc->tu.cu_qp_offset_cb = s->pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
  802. lc->tu.cu_qp_offset_cr = s->pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
  803. } else {
  804. lc->tu.cu_qp_offset_cb = 0;
  805. lc->tu.cu_qp_offset_cr = 0;
  806. }
  807. lc->tu.is_cu_chroma_qp_offset_coded = 1;
  808. }
  809. if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
  810. if (lc->tu.intra_pred_mode >= 6 &&
  811. lc->tu.intra_pred_mode <= 14) {
  812. scan_idx = SCAN_VERT;
  813. } else if (lc->tu.intra_pred_mode >= 22 &&
  814. lc->tu.intra_pred_mode <= 30) {
  815. scan_idx = SCAN_HORIZ;
  816. }
  817. if (lc->tu.intra_pred_mode_c >= 6 &&
  818. lc->tu.intra_pred_mode_c <= 14) {
  819. scan_idx_c = SCAN_VERT;
  820. } else if (lc->tu.intra_pred_mode_c >= 22 &&
  821. lc->tu.intra_pred_mode_c <= 30) {
  822. scan_idx_c = SCAN_HORIZ;
  823. }
  824. }
  825. lc->tu.cross_pf = 0;
  826. if (cbf_luma)
  827. ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
  828. if (log2_trafo_size > 2 || s->sps->chroma_format_idc == 3) {
  829. int trafo_size_h = 1 << (log2_trafo_size_c + s->sps->hshift[1]);
  830. int trafo_size_v = 1 << (log2_trafo_size_c + s->sps->vshift[1]);
  831. lc->tu.cross_pf = (s->pps->cross_component_prediction_enabled_flag && cbf_luma &&
  832. (lc->cu.pred_mode == MODE_INTER ||
  833. (lc->tu.chroma_mode_c == 4)));
  834. if (lc->tu.cross_pf) {
  835. hls_cross_component_pred(s, 0);
  836. }
  837. for (i = 0; i < (s->sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  838. if (lc->cu.pred_mode == MODE_INTRA) {
  839. ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
  840. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
  841. }
  842. if (cbf_cb[i])
  843. ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
  844. log2_trafo_size_c, scan_idx_c, 1);
  845. else
  846. if (lc->tu.cross_pf) {
  847. ptrdiff_t stride = s->frame->linesize[1];
  848. int hshift = s->sps->hshift[1];
  849. int vshift = s->sps->vshift[1];
  850. int16_t *coeffs_y = lc->tu.coeffs[0];
  851. int16_t *coeffs = lc->tu.coeffs[1];
  852. int size = 1 << log2_trafo_size_c;
  853. uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
  854. ((x0 >> hshift) << s->sps->pixel_shift)];
  855. for (i = 0; i < (size * size); i++) {
  856. coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
  857. }
  858. s->hevcdsp.transform_add[log2_trafo_size_c-2](dst, coeffs, stride);
  859. }
  860. }
  861. if (lc->tu.cross_pf) {
  862. hls_cross_component_pred(s, 1);
  863. }
  864. for (i = 0; i < (s->sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  865. if (lc->cu.pred_mode == MODE_INTRA) {
  866. ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
  867. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
  868. }
  869. if (cbf_cr[i])
  870. ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
  871. log2_trafo_size_c, scan_idx_c, 2);
  872. else
  873. if (lc->tu.cross_pf) {
  874. ptrdiff_t stride = s->frame->linesize[2];
  875. int hshift = s->sps->hshift[2];
  876. int vshift = s->sps->vshift[2];
  877. int16_t *coeffs_y = lc->tu.coeffs[0];
  878. int16_t *coeffs = lc->tu.coeffs[1];
  879. int size = 1 << log2_trafo_size_c;
  880. uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
  881. ((x0 >> hshift) << s->sps->pixel_shift)];
  882. for (i = 0; i < (size * size); i++) {
  883. coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
  884. }
  885. s->hevcdsp.transform_add[log2_trafo_size_c-2](dst, coeffs, stride);
  886. }
  887. }
  888. } else if (blk_idx == 3) {
  889. int trafo_size_h = 1 << (log2_trafo_size + 1);
  890. int trafo_size_v = 1 << (log2_trafo_size + s->sps->vshift[1]);
  891. for (i = 0; i < (s->sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  892. if (lc->cu.pred_mode == MODE_INTRA) {
  893. ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
  894. trafo_size_h, trafo_size_v);
  895. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
  896. }
  897. if (cbf_cb[i])
  898. ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
  899. log2_trafo_size, scan_idx_c, 1);
  900. }
  901. for (i = 0; i < (s->sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  902. if (lc->cu.pred_mode == MODE_INTRA) {
  903. ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
  904. trafo_size_h, trafo_size_v);
  905. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
  906. }
  907. if (cbf_cr[i])
  908. ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
  909. log2_trafo_size, scan_idx_c, 2);
  910. }
  911. }
  912. } else if (lc->cu.pred_mode == MODE_INTRA) {
  913. if (log2_trafo_size > 2 || s->sps->chroma_format_idc == 3) {
  914. int trafo_size_h = 1 << (log2_trafo_size_c + s->sps->hshift[1]);
  915. int trafo_size_v = 1 << (log2_trafo_size_c + s->sps->vshift[1]);
  916. ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
  917. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
  918. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
  919. if (s->sps->chroma_format_idc == 2) {
  920. ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
  921. trafo_size_h, trafo_size_v);
  922. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
  923. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
  924. }
  925. } else if (blk_idx == 3) {
  926. int trafo_size_h = 1 << (log2_trafo_size + 1);
  927. int trafo_size_v = 1 << (log2_trafo_size + s->sps->vshift[1]);
  928. ff_hevc_set_neighbour_available(s, xBase, yBase,
  929. trafo_size_h, trafo_size_v);
  930. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
  931. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
  932. if (s->sps->chroma_format_idc == 2) {
  933. ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
  934. trafo_size_h, trafo_size_v);
  935. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
  936. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
  937. }
  938. }
  939. }
  940. return 0;
  941. }
  942. static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
  943. {
  944. int cb_size = 1 << log2_cb_size;
  945. int log2_min_pu_size = s->sps->log2_min_pu_size;
  946. int min_pu_width = s->sps->min_pu_width;
  947. int x_end = FFMIN(x0 + cb_size, s->sps->width);
  948. int y_end = FFMIN(y0 + cb_size, s->sps->height);
  949. int i, j;
  950. for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
  951. for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
  952. s->is_pcm[i + j * min_pu_width] = 2;
  953. }
  954. static int hls_transform_tree(HEVCContext *s, int x0, int y0,
  955. int xBase, int yBase, int cb_xBase, int cb_yBase,
  956. int log2_cb_size, int log2_trafo_size,
  957. int trafo_depth, int blk_idx,
  958. const int *base_cbf_cb, const int *base_cbf_cr)
  959. {
  960. HEVCLocalContext *lc = s->HEVClc;
  961. uint8_t split_transform_flag;
  962. int cbf_cb[2];
  963. int cbf_cr[2];
  964. int ret;
  965. cbf_cb[0] = base_cbf_cb[0];
  966. cbf_cb[1] = base_cbf_cb[1];
  967. cbf_cr[0] = base_cbf_cr[0];
  968. cbf_cr[1] = base_cbf_cr[1];
  969. if (lc->cu.intra_split_flag) {
  970. if (trafo_depth == 1) {
  971. lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
  972. if (s->sps->chroma_format_idc == 3) {
  973. lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
  974. lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
  975. } else {
  976. lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
  977. lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
  978. }
  979. }
  980. } else {
  981. lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
  982. lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
  983. lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
  984. }
  985. if (log2_trafo_size <= s->sps->log2_max_trafo_size &&
  986. log2_trafo_size > s->sps->log2_min_tb_size &&
  987. trafo_depth < lc->cu.max_trafo_depth &&
  988. !(lc->cu.intra_split_flag && trafo_depth == 0)) {
  989. split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
  990. } else {
  991. int inter_split = s->sps->max_transform_hierarchy_depth_inter == 0 &&
  992. lc->cu.pred_mode == MODE_INTER &&
  993. lc->cu.part_mode != PART_2Nx2N &&
  994. trafo_depth == 0;
  995. split_transform_flag = log2_trafo_size > s->sps->log2_max_trafo_size ||
  996. (lc->cu.intra_split_flag && trafo_depth == 0) ||
  997. inter_split;
  998. }
  999. if (log2_trafo_size > 2 || s->sps->chroma_format_idc == 3) {
  1000. if (trafo_depth == 0 || cbf_cb[0]) {
  1001. cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  1002. if (s->sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
  1003. cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  1004. }
  1005. }
  1006. if (trafo_depth == 0 || cbf_cr[0]) {
  1007. cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  1008. if (s->sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
  1009. cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  1010. }
  1011. }
  1012. }
  1013. if (split_transform_flag) {
  1014. const int trafo_size_split = 1 << (log2_trafo_size - 1);
  1015. const int x1 = x0 + trafo_size_split;
  1016. const int y1 = y0 + trafo_size_split;
  1017. #define SUBDIVIDE(x, y, idx) \
  1018. do { \
  1019. ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
  1020. log2_trafo_size - 1, trafo_depth + 1, idx, \
  1021. cbf_cb, cbf_cr); \
  1022. if (ret < 0) \
  1023. return ret; \
  1024. } while (0)
  1025. SUBDIVIDE(x0, y0, 0);
  1026. SUBDIVIDE(x1, y0, 1);
  1027. SUBDIVIDE(x0, y1, 2);
  1028. SUBDIVIDE(x1, y1, 3);
  1029. #undef SUBDIVIDE
  1030. } else {
  1031. int min_tu_size = 1 << s->sps->log2_min_tb_size;
  1032. int log2_min_tu_size = s->sps->log2_min_tb_size;
  1033. int min_tu_width = s->sps->min_tb_width;
  1034. int cbf_luma = 1;
  1035. if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
  1036. cbf_cb[0] || cbf_cr[0] ||
  1037. (s->sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
  1038. cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
  1039. }
  1040. ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
  1041. log2_cb_size, log2_trafo_size, trafo_depth,
  1042. blk_idx, cbf_luma, cbf_cb, cbf_cr);
  1043. if (ret < 0)
  1044. return ret;
  1045. // TODO: store cbf_luma somewhere else
  1046. if (cbf_luma) {
  1047. int i, j;
  1048. for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
  1049. for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
  1050. int x_tu = (x0 + j) >> log2_min_tu_size;
  1051. int y_tu = (y0 + i) >> log2_min_tu_size;
  1052. s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
  1053. }
  1054. }
  1055. if (!s->sh.disable_deblocking_filter_flag) {
  1056. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
  1057. if (s->pps->transquant_bypass_enable_flag &&
  1058. lc->cu.cu_transquant_bypass_flag)
  1059. set_deblocking_bypass(s, x0, y0, log2_trafo_size);
  1060. }
  1061. }
  1062. return 0;
  1063. }
  1064. static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1065. {
  1066. HEVCLocalContext *lc = s->HEVClc;
  1067. GetBitContext gb;
  1068. int cb_size = 1 << log2_cb_size;
  1069. int stride0 = s->frame->linesize[0];
  1070. uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->sps->pixel_shift)];
  1071. int stride1 = s->frame->linesize[1];
  1072. uint8_t *dst1 = &s->frame->data[1][(y0 >> s->sps->vshift[1]) * stride1 + ((x0 >> s->sps->hshift[1]) << s->sps->pixel_shift)];
  1073. int stride2 = s->frame->linesize[2];
  1074. uint8_t *dst2 = &s->frame->data[2][(y0 >> s->sps->vshift[2]) * stride2 + ((x0 >> s->sps->hshift[2]) << s->sps->pixel_shift)];
  1075. int length = cb_size * cb_size * s->sps->pcm.bit_depth +
  1076. (((cb_size >> s->sps->hshift[1]) * (cb_size >> s->sps->vshift[1])) +
  1077. ((cb_size >> s->sps->hshift[2]) * (cb_size >> s->sps->vshift[2]))) *
  1078. s->sps->pcm.bit_depth_chroma;
  1079. const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
  1080. int ret;
  1081. if (!s->sh.disable_deblocking_filter_flag)
  1082. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  1083. ret = init_get_bits(&gb, pcm, length);
  1084. if (ret < 0)
  1085. return ret;
  1086. s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, s->sps->pcm.bit_depth);
  1087. s->hevcdsp.put_pcm(dst1, stride1,
  1088. cb_size >> s->sps->hshift[1],
  1089. cb_size >> s->sps->vshift[1],
  1090. &gb, s->sps->pcm.bit_depth_chroma);
  1091. s->hevcdsp.put_pcm(dst2, stride2,
  1092. cb_size >> s->sps->hshift[2],
  1093. cb_size >> s->sps->vshift[2],
  1094. &gb, s->sps->pcm.bit_depth_chroma);
  1095. return 0;
  1096. }
  1097. /**
  1098. * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
  1099. *
  1100. * @param s HEVC decoding context
  1101. * @param dst target buffer for block data at block position
  1102. * @param dststride stride of the dst buffer
  1103. * @param ref reference picture buffer at origin (0, 0)
  1104. * @param mv motion vector (relative to block position) to get pixel data from
  1105. * @param x_off horizontal position of block from origin (0, 0)
  1106. * @param y_off vertical position of block from origin (0, 0)
  1107. * @param block_w width of block
  1108. * @param block_h height of block
  1109. * @param luma_weight weighting factor applied to the luma prediction
  1110. * @param luma_offset additive offset applied to the luma prediction value
  1111. */
  1112. static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
  1113. AVFrame *ref, const Mv *mv, int x_off, int y_off,
  1114. int block_w, int block_h, int luma_weight, int luma_offset)
  1115. {
  1116. HEVCLocalContext *lc = s->HEVClc;
  1117. uint8_t *src = ref->data[0];
  1118. ptrdiff_t srcstride = ref->linesize[0];
  1119. int pic_width = s->sps->width;
  1120. int pic_height = s->sps->height;
  1121. int mx = mv->x & 3;
  1122. int my = mv->y & 3;
  1123. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1124. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  1125. int idx = ff_hevc_pel_weight[block_w];
  1126. x_off += mv->x >> 2;
  1127. y_off += mv->y >> 2;
  1128. src += y_off * srcstride + (x_off << s->sps->pixel_shift);
  1129. if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
  1130. x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  1131. y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  1132. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1133. int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1134. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1135. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
  1136. edge_emu_stride, srcstride,
  1137. block_w + QPEL_EXTRA,
  1138. block_h + QPEL_EXTRA,
  1139. x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
  1140. pic_width, pic_height);
  1141. src = lc->edge_emu_buffer + buf_offset;
  1142. srcstride = edge_emu_stride;
  1143. }
  1144. if (!weight_flag)
  1145. s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
  1146. block_h, mx, my, block_w);
  1147. else
  1148. s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
  1149. block_h, s->sh.luma_log2_weight_denom,
  1150. luma_weight, luma_offset, mx, my, block_w);
  1151. }
  1152. /**
  1153. * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
  1154. *
  1155. * @param s HEVC decoding context
  1156. * @param dst target buffer for block data at block position
  1157. * @param dststride stride of the dst buffer
  1158. * @param ref0 reference picture0 buffer at origin (0, 0)
  1159. * @param mv0 motion vector0 (relative to block position) to get pixel data from
  1160. * @param x_off horizontal position of block from origin (0, 0)
  1161. * @param y_off vertical position of block from origin (0, 0)
  1162. * @param block_w width of block
  1163. * @param block_h height of block
  1164. * @param ref1 reference picture1 buffer at origin (0, 0)
  1165. * @param mv1 motion vector1 (relative to block position) to get pixel data from
  1166. * @param current_mv current motion vector structure
  1167. */
  1168. static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
  1169. AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
  1170. int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
  1171. {
  1172. HEVCLocalContext *lc = s->HEVClc;
  1173. DECLARE_ALIGNED(16, int16_t, tmp[MAX_PB_SIZE * MAX_PB_SIZE]);
  1174. ptrdiff_t src0stride = ref0->linesize[0];
  1175. ptrdiff_t src1stride = ref1->linesize[0];
  1176. int pic_width = s->sps->width;
  1177. int pic_height = s->sps->height;
  1178. int mx0 = mv0->x & 3;
  1179. int my0 = mv0->y & 3;
  1180. int mx1 = mv1->x & 3;
  1181. int my1 = mv1->y & 3;
  1182. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1183. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  1184. int x_off0 = x_off + (mv0->x >> 2);
  1185. int y_off0 = y_off + (mv0->y >> 2);
  1186. int x_off1 = x_off + (mv1->x >> 2);
  1187. int y_off1 = y_off + (mv1->y >> 2);
  1188. int idx = ff_hevc_pel_weight[block_w];
  1189. uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->sps->pixel_shift);
  1190. uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->sps->pixel_shift);
  1191. if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
  1192. x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  1193. y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  1194. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1195. int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1196. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1197. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
  1198. edge_emu_stride, src0stride,
  1199. block_w + QPEL_EXTRA,
  1200. block_h + QPEL_EXTRA,
  1201. x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
  1202. pic_width, pic_height);
  1203. src0 = lc->edge_emu_buffer + buf_offset;
  1204. src0stride = edge_emu_stride;
  1205. }
  1206. if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
  1207. x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  1208. y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  1209. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1210. int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1211. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1212. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
  1213. edge_emu_stride, src1stride,
  1214. block_w + QPEL_EXTRA,
  1215. block_h + QPEL_EXTRA,
  1216. x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
  1217. pic_width, pic_height);
  1218. src1 = lc->edge_emu_buffer2 + buf_offset;
  1219. src1stride = edge_emu_stride;
  1220. }
  1221. s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](tmp, src0, src0stride,
  1222. block_h, mx0, my0, block_w);
  1223. if (!weight_flag)
  1224. s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, tmp,
  1225. block_h, mx1, my1, block_w);
  1226. else
  1227. s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, tmp,
  1228. block_h, s->sh.luma_log2_weight_denom,
  1229. s->sh.luma_weight_l0[current_mv->ref_idx[0]],
  1230. s->sh.luma_weight_l1[current_mv->ref_idx[1]],
  1231. s->sh.luma_offset_l0[current_mv->ref_idx[0]],
  1232. s->sh.luma_offset_l1[current_mv->ref_idx[1]],
  1233. mx1, my1, block_w);
  1234. }
  1235. /**
  1236. * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
  1237. *
  1238. * @param s HEVC decoding context
  1239. * @param dst1 target buffer for block data at block position (U plane)
  1240. * @param dst2 target buffer for block data at block position (V plane)
  1241. * @param dststride stride of the dst1 and dst2 buffers
  1242. * @param ref reference picture buffer at origin (0, 0)
  1243. * @param mv motion vector (relative to block position) to get pixel data from
  1244. * @param x_off horizontal position of block from origin (0, 0)
  1245. * @param y_off vertical position of block from origin (0, 0)
  1246. * @param block_w width of block
  1247. * @param block_h height of block
  1248. * @param chroma_weight weighting factor applied to the chroma prediction
  1249. * @param chroma_offset additive offset applied to the chroma prediction value
  1250. */
  1251. static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
  1252. ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
  1253. int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
  1254. {
  1255. HEVCLocalContext *lc = s->HEVClc;
  1256. int pic_width = s->sps->width >> s->sps->hshift[1];
  1257. int pic_height = s->sps->height >> s->sps->vshift[1];
  1258. const Mv *mv = &current_mv->mv[reflist];
  1259. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1260. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  1261. int idx = ff_hevc_pel_weight[block_w];
  1262. int hshift = s->sps->hshift[1];
  1263. int vshift = s->sps->vshift[1];
  1264. intptr_t mx = mv->x & ((1 << (2 + hshift)) - 1);
  1265. intptr_t my = mv->y & ((1 << (2 + vshift)) - 1);
  1266. intptr_t _mx = mx << (1 - hshift);
  1267. intptr_t _my = my << (1 - vshift);
  1268. x_off += mv->x >> (2 + hshift);
  1269. y_off += mv->y >> (2 + vshift);
  1270. src0 += y_off * srcstride + (x_off << s->sps->pixel_shift);
  1271. if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
  1272. x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1273. y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1274. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1275. int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->sps->pixel_shift));
  1276. int buf_offset0 = EPEL_EXTRA_BEFORE *
  1277. (edge_emu_stride + (1 << s->sps->pixel_shift));
  1278. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
  1279. edge_emu_stride, srcstride,
  1280. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1281. x_off - EPEL_EXTRA_BEFORE,
  1282. y_off - EPEL_EXTRA_BEFORE,
  1283. pic_width, pic_height);
  1284. src0 = lc->edge_emu_buffer + buf_offset0;
  1285. srcstride = edge_emu_stride;
  1286. }
  1287. if (!weight_flag)
  1288. s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
  1289. block_h, _mx, _my, block_w);
  1290. else
  1291. s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
  1292. block_h, s->sh.chroma_log2_weight_denom,
  1293. chroma_weight, chroma_offset, _mx, _my, block_w);
  1294. }
  1295. /**
  1296. * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
  1297. *
  1298. * @param s HEVC decoding context
  1299. * @param dst target buffer for block data at block position
  1300. * @param dststride stride of the dst buffer
  1301. * @param ref0 reference picture0 buffer at origin (0, 0)
  1302. * @param mv0 motion vector0 (relative to block position) to get pixel data from
  1303. * @param x_off horizontal position of block from origin (0, 0)
  1304. * @param y_off vertical position of block from origin (0, 0)
  1305. * @param block_w width of block
  1306. * @param block_h height of block
  1307. * @param ref1 reference picture1 buffer at origin (0, 0)
  1308. * @param mv1 motion vector1 (relative to block position) to get pixel data from
  1309. * @param current_mv current motion vector structure
  1310. * @param cidx chroma component(cb, cr)
  1311. */
  1312. static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
  1313. int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
  1314. {
  1315. DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
  1316. HEVCLocalContext *lc = s->HEVClc;
  1317. uint8_t *src1 = ref0->data[cidx+1];
  1318. uint8_t *src2 = ref1->data[cidx+1];
  1319. ptrdiff_t src1stride = ref0->linesize[cidx+1];
  1320. ptrdiff_t src2stride = ref1->linesize[cidx+1];
  1321. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1322. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  1323. int pic_width = s->sps->width >> s->sps->hshift[1];
  1324. int pic_height = s->sps->height >> s->sps->vshift[1];
  1325. Mv *mv0 = &current_mv->mv[0];
  1326. Mv *mv1 = &current_mv->mv[1];
  1327. int hshift = s->sps->hshift[1];
  1328. int vshift = s->sps->vshift[1];
  1329. intptr_t mx0 = mv0->x & ((1 << (2 + hshift)) - 1);
  1330. intptr_t my0 = mv0->y & ((1 << (2 + vshift)) - 1);
  1331. intptr_t mx1 = mv1->x & ((1 << (2 + hshift)) - 1);
  1332. intptr_t my1 = mv1->y & ((1 << (2 + vshift)) - 1);
  1333. intptr_t _mx0 = mx0 << (1 - hshift);
  1334. intptr_t _my0 = my0 << (1 - vshift);
  1335. intptr_t _mx1 = mx1 << (1 - hshift);
  1336. intptr_t _my1 = my1 << (1 - vshift);
  1337. int x_off0 = x_off + (mv0->x >> (2 + hshift));
  1338. int y_off0 = y_off + (mv0->y >> (2 + vshift));
  1339. int x_off1 = x_off + (mv1->x >> (2 + hshift));
  1340. int y_off1 = y_off + (mv1->y >> (2 + vshift));
  1341. int idx = ff_hevc_pel_weight[block_w];
  1342. src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << s->sps->pixel_shift);
  1343. src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << s->sps->pixel_shift);
  1344. if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
  1345. x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1346. y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1347. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1348. int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->sps->pixel_shift));
  1349. int buf_offset1 = EPEL_EXTRA_BEFORE *
  1350. (edge_emu_stride + (1 << s->sps->pixel_shift));
  1351. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
  1352. edge_emu_stride, src1stride,
  1353. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1354. x_off0 - EPEL_EXTRA_BEFORE,
  1355. y_off0 - EPEL_EXTRA_BEFORE,
  1356. pic_width, pic_height);
  1357. src1 = lc->edge_emu_buffer + buf_offset1;
  1358. src1stride = edge_emu_stride;
  1359. }
  1360. if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
  1361. x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1362. y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1363. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1364. int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->sps->pixel_shift));
  1365. int buf_offset1 = EPEL_EXTRA_BEFORE *
  1366. (edge_emu_stride + (1 << s->sps->pixel_shift));
  1367. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
  1368. edge_emu_stride, src2stride,
  1369. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1370. x_off1 - EPEL_EXTRA_BEFORE,
  1371. y_off1 - EPEL_EXTRA_BEFORE,
  1372. pic_width, pic_height);
  1373. src2 = lc->edge_emu_buffer2 + buf_offset1;
  1374. src2stride = edge_emu_stride;
  1375. }
  1376. s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](tmp, src1, src1stride,
  1377. block_h, _mx0, _my0, block_w);
  1378. if (!weight_flag)
  1379. s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
  1380. src2, src2stride, tmp,
  1381. block_h, _mx1, _my1, block_w);
  1382. else
  1383. s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
  1384. src2, src2stride, tmp,
  1385. block_h,
  1386. s->sh.chroma_log2_weight_denom,
  1387. s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
  1388. s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
  1389. s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
  1390. s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
  1391. _mx1, _my1, block_w);
  1392. }
  1393. static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
  1394. const Mv *mv, int y0, int height)
  1395. {
  1396. int y = (mv->y >> 2) + y0 + height + 9;
  1397. if (s->threads_type == FF_THREAD_FRAME )
  1398. ff_thread_await_progress(&ref->tf, y, 0);
  1399. }
  1400. static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
  1401. int nPbW, int nPbH,
  1402. int log2_cb_size, int partIdx, int idx)
  1403. {
  1404. #define POS(c_idx, x, y) \
  1405. &s->frame->data[c_idx][((y) >> s->sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
  1406. (((x) >> s->sps->hshift[c_idx]) << s->sps->pixel_shift)]
  1407. HEVCLocalContext *lc = s->HEVClc;
  1408. int merge_idx = 0;
  1409. struct MvField current_mv = {{{ 0 }}};
  1410. int min_pu_width = s->sps->min_pu_width;
  1411. MvField *tab_mvf = s->ref->tab_mvf;
  1412. RefPicList *refPicList = s->ref->refPicList;
  1413. HEVCFrame *ref0, *ref1;
  1414. uint8_t *dst0 = POS(0, x0, y0);
  1415. uint8_t *dst1 = POS(1, x0, y0);
  1416. uint8_t *dst2 = POS(2, x0, y0);
  1417. int log2_min_cb_size = s->sps->log2_min_cb_size;
  1418. int min_cb_width = s->sps->min_cb_width;
  1419. int x_cb = x0 >> log2_min_cb_size;
  1420. int y_cb = y0 >> log2_min_cb_size;
  1421. int ref_idx[2];
  1422. int mvp_flag[2];
  1423. int x_pu, y_pu;
  1424. int i, j;
  1425. if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
  1426. if (s->sh.max_num_merge_cand > 1)
  1427. merge_idx = ff_hevc_merge_idx_decode(s);
  1428. else
  1429. merge_idx = 0;
  1430. ff_hevc_luma_mv_merge_mode(s, x0, y0,
  1431. 1 << log2_cb_size,
  1432. 1 << log2_cb_size,
  1433. log2_cb_size, partIdx,
  1434. merge_idx, &current_mv);
  1435. x_pu = x0 >> s->sps->log2_min_pu_size;
  1436. y_pu = y0 >> s->sps->log2_min_pu_size;
  1437. for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  1438. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  1439. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1440. } else { /* MODE_INTER */
  1441. lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
  1442. if (lc->pu.merge_flag) {
  1443. if (s->sh.max_num_merge_cand > 1)
  1444. merge_idx = ff_hevc_merge_idx_decode(s);
  1445. else
  1446. merge_idx = 0;
  1447. ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1448. partIdx, merge_idx, &current_mv);
  1449. x_pu = x0 >> s->sps->log2_min_pu_size;
  1450. y_pu = y0 >> s->sps->log2_min_pu_size;
  1451. for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  1452. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  1453. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1454. } else {
  1455. enum InterPredIdc inter_pred_idc = PRED_L0;
  1456. ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
  1457. current_mv.pred_flag = 0;
  1458. if (s->sh.slice_type == B_SLICE)
  1459. inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
  1460. if (inter_pred_idc != PRED_L1) {
  1461. if (s->sh.nb_refs[L0]) {
  1462. ref_idx[0] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
  1463. current_mv.ref_idx[0] = ref_idx[0];
  1464. }
  1465. current_mv.pred_flag = PF_L0;
  1466. ff_hevc_hls_mvd_coding(s, x0, y0, 0);
  1467. mvp_flag[0] = ff_hevc_mvp_lx_flag_decode(s);
  1468. ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1469. partIdx, merge_idx, &current_mv,
  1470. mvp_flag[0], 0);
  1471. current_mv.mv[0].x += lc->pu.mvd.x;
  1472. current_mv.mv[0].y += lc->pu.mvd.y;
  1473. }
  1474. if (inter_pred_idc != PRED_L0) {
  1475. if (s->sh.nb_refs[L1]) {
  1476. ref_idx[1] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
  1477. current_mv.ref_idx[1] = ref_idx[1];
  1478. }
  1479. if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
  1480. AV_ZERO32(&lc->pu.mvd);
  1481. } else {
  1482. ff_hevc_hls_mvd_coding(s, x0, y0, 1);
  1483. }
  1484. current_mv.pred_flag += PF_L1;
  1485. mvp_flag[1] = ff_hevc_mvp_lx_flag_decode(s);
  1486. ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1487. partIdx, merge_idx, &current_mv,
  1488. mvp_flag[1], 1);
  1489. current_mv.mv[1].x += lc->pu.mvd.x;
  1490. current_mv.mv[1].y += lc->pu.mvd.y;
  1491. }
  1492. x_pu = x0 >> s->sps->log2_min_pu_size;
  1493. y_pu = y0 >> s->sps->log2_min_pu_size;
  1494. for(j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  1495. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  1496. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1497. }
  1498. }
  1499. if (current_mv.pred_flag & PF_L0) {
  1500. ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
  1501. if (!ref0)
  1502. return;
  1503. hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
  1504. }
  1505. if (current_mv.pred_flag & PF_L1) {
  1506. ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
  1507. if (!ref1)
  1508. return;
  1509. hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
  1510. }
  1511. if (current_mv.pred_flag == PF_L0) {
  1512. int x0_c = x0 >> s->sps->hshift[1];
  1513. int y0_c = y0 >> s->sps->vshift[1];
  1514. int nPbW_c = nPbW >> s->sps->hshift[1];
  1515. int nPbH_c = nPbH >> s->sps->vshift[1];
  1516. luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
  1517. &current_mv.mv[0], x0, y0, nPbW, nPbH,
  1518. s->sh.luma_weight_l0[current_mv.ref_idx[0]],
  1519. s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
  1520. chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
  1521. 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1522. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
  1523. chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
  1524. 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1525. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
  1526. } else if (current_mv.pred_flag == PF_L1) {
  1527. int x0_c = x0 >> s->sps->hshift[1];
  1528. int y0_c = y0 >> s->sps->vshift[1];
  1529. int nPbW_c = nPbW >> s->sps->hshift[1];
  1530. int nPbH_c = nPbH >> s->sps->vshift[1];
  1531. luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
  1532. &current_mv.mv[1], x0, y0, nPbW, nPbH,
  1533. s->sh.luma_weight_l1[current_mv.ref_idx[1]],
  1534. s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
  1535. chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
  1536. 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1537. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
  1538. chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
  1539. 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1540. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
  1541. } else if (current_mv.pred_flag == PF_BI) {
  1542. int x0_c = x0 >> s->sps->hshift[1];
  1543. int y0_c = y0 >> s->sps->vshift[1];
  1544. int nPbW_c = nPbW >> s->sps->hshift[1];
  1545. int nPbH_c = nPbH >> s->sps->vshift[1];
  1546. luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
  1547. &current_mv.mv[0], x0, y0, nPbW, nPbH,
  1548. ref1->frame, &current_mv.mv[1], &current_mv);
  1549. chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
  1550. x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
  1551. chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
  1552. x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
  1553. }
  1554. }
  1555. /**
  1556. * 8.4.1
  1557. */
  1558. static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
  1559. int prev_intra_luma_pred_flag)
  1560. {
  1561. HEVCLocalContext *lc = s->HEVClc;
  1562. int x_pu = x0 >> s->sps->log2_min_pu_size;
  1563. int y_pu = y0 >> s->sps->log2_min_pu_size;
  1564. int min_pu_width = s->sps->min_pu_width;
  1565. int size_in_pus = pu_size >> s->sps->log2_min_pu_size;
  1566. int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
  1567. int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
  1568. int cand_up = (lc->ctb_up_flag || y0b) ?
  1569. s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
  1570. int cand_left = (lc->ctb_left_flag || x0b) ?
  1571. s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
  1572. int y_ctb = (y0 >> (s->sps->log2_ctb_size)) << (s->sps->log2_ctb_size);
  1573. MvField *tab_mvf = s->ref->tab_mvf;
  1574. int intra_pred_mode;
  1575. int candidate[3];
  1576. int i, j;
  1577. // intra_pred_mode prediction does not cross vertical CTB boundaries
  1578. if ((y0 - 1) < y_ctb)
  1579. cand_up = INTRA_DC;
  1580. if (cand_left == cand_up) {
  1581. if (cand_left < 2) {
  1582. candidate[0] = INTRA_PLANAR;
  1583. candidate[1] = INTRA_DC;
  1584. candidate[2] = INTRA_ANGULAR_26;
  1585. } else {
  1586. candidate[0] = cand_left;
  1587. candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
  1588. candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
  1589. }
  1590. } else {
  1591. candidate[0] = cand_left;
  1592. candidate[1] = cand_up;
  1593. if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
  1594. candidate[2] = INTRA_PLANAR;
  1595. } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
  1596. candidate[2] = INTRA_DC;
  1597. } else {
  1598. candidate[2] = INTRA_ANGULAR_26;
  1599. }
  1600. }
  1601. if (prev_intra_luma_pred_flag) {
  1602. intra_pred_mode = candidate[lc->pu.mpm_idx];
  1603. } else {
  1604. if (candidate[0] > candidate[1])
  1605. FFSWAP(uint8_t, candidate[0], candidate[1]);
  1606. if (candidate[0] > candidate[2])
  1607. FFSWAP(uint8_t, candidate[0], candidate[2]);
  1608. if (candidate[1] > candidate[2])
  1609. FFSWAP(uint8_t, candidate[1], candidate[2]);
  1610. intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
  1611. for (i = 0; i < 3; i++)
  1612. if (intra_pred_mode >= candidate[i])
  1613. intra_pred_mode++;
  1614. }
  1615. /* write the intra prediction units into the mv array */
  1616. if (!size_in_pus)
  1617. size_in_pus = 1;
  1618. for (i = 0; i < size_in_pus; i++) {
  1619. memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
  1620. intra_pred_mode, size_in_pus);
  1621. for (j = 0; j < size_in_pus; j++) {
  1622. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
  1623. }
  1624. }
  1625. return intra_pred_mode;
  1626. }
  1627. static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
  1628. int log2_cb_size, int ct_depth)
  1629. {
  1630. int length = (1 << log2_cb_size) >> s->sps->log2_min_cb_size;
  1631. int x_cb = x0 >> s->sps->log2_min_cb_size;
  1632. int y_cb = y0 >> s->sps->log2_min_cb_size;
  1633. int y;
  1634. for (y = 0; y < length; y++)
  1635. memset(&s->tab_ct_depth[(y_cb + y) * s->sps->min_cb_width + x_cb],
  1636. ct_depth, length);
  1637. }
  1638. static const uint8_t tab_mode_idx[] = {
  1639. 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
  1640. 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
  1641. static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
  1642. int log2_cb_size)
  1643. {
  1644. HEVCLocalContext *lc = s->HEVClc;
  1645. static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
  1646. uint8_t prev_intra_luma_pred_flag[4];
  1647. int split = lc->cu.part_mode == PART_NxN;
  1648. int pb_size = (1 << log2_cb_size) >> split;
  1649. int side = split + 1;
  1650. int chroma_mode;
  1651. int i, j;
  1652. for (i = 0; i < side; i++)
  1653. for (j = 0; j < side; j++)
  1654. prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
  1655. for (i = 0; i < side; i++) {
  1656. for (j = 0; j < side; j++) {
  1657. if (prev_intra_luma_pred_flag[2 * i + j])
  1658. lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
  1659. else
  1660. lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
  1661. lc->pu.intra_pred_mode[2 * i + j] =
  1662. luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
  1663. prev_intra_luma_pred_flag[2 * i + j]);
  1664. }
  1665. }
  1666. if (s->sps->chroma_format_idc == 3) {
  1667. for (i = 0; i < side; i++) {
  1668. for (j = 0; j < side; j++) {
  1669. lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
  1670. if (chroma_mode != 4) {
  1671. if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
  1672. lc->pu.intra_pred_mode_c[2 * i + j] = 34;
  1673. else
  1674. lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
  1675. } else {
  1676. lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
  1677. }
  1678. }
  1679. }
  1680. } else if (s->sps->chroma_format_idc == 2) {
  1681. int mode_idx;
  1682. lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
  1683. if (chroma_mode != 4) {
  1684. if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
  1685. mode_idx = 34;
  1686. else
  1687. mode_idx = intra_chroma_table[chroma_mode];
  1688. } else {
  1689. mode_idx = lc->pu.intra_pred_mode[0];
  1690. }
  1691. lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
  1692. } else if (s->sps->chroma_format_idc != 0) {
  1693. chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
  1694. if (chroma_mode != 4) {
  1695. if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
  1696. lc->pu.intra_pred_mode_c[0] = 34;
  1697. else
  1698. lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
  1699. } else {
  1700. lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
  1701. }
  1702. }
  1703. }
  1704. static void intra_prediction_unit_default_value(HEVCContext *s,
  1705. int x0, int y0,
  1706. int log2_cb_size)
  1707. {
  1708. HEVCLocalContext *lc = s->HEVClc;
  1709. int pb_size = 1 << log2_cb_size;
  1710. int size_in_pus = pb_size >> s->sps->log2_min_pu_size;
  1711. int min_pu_width = s->sps->min_pu_width;
  1712. MvField *tab_mvf = s->ref->tab_mvf;
  1713. int x_pu = x0 >> s->sps->log2_min_pu_size;
  1714. int y_pu = y0 >> s->sps->log2_min_pu_size;
  1715. int j, k;
  1716. if (size_in_pus == 0)
  1717. size_in_pus = 1;
  1718. for (j = 0; j < size_in_pus; j++)
  1719. memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
  1720. if (lc->cu.pred_mode == MODE_INTRA)
  1721. for (j = 0; j < size_in_pus; j++)
  1722. for (k = 0; k < size_in_pus; k++)
  1723. tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
  1724. }
  1725. static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1726. {
  1727. int cb_size = 1 << log2_cb_size;
  1728. HEVCLocalContext *lc = s->HEVClc;
  1729. int log2_min_cb_size = s->sps->log2_min_cb_size;
  1730. int length = cb_size >> log2_min_cb_size;
  1731. int min_cb_width = s->sps->min_cb_width;
  1732. int x_cb = x0 >> log2_min_cb_size;
  1733. int y_cb = y0 >> log2_min_cb_size;
  1734. int idx = log2_cb_size - 2;
  1735. int qp_block_mask = (1<<(s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth)) - 1;
  1736. int x, y, ret;
  1737. lc->cu.x = x0;
  1738. lc->cu.y = y0;
  1739. lc->cu.rqt_root_cbf = 1;
  1740. lc->cu.pred_mode = MODE_INTRA;
  1741. lc->cu.part_mode = PART_2Nx2N;
  1742. lc->cu.intra_split_flag = 0;
  1743. lc->cu.pcm_flag = 0;
  1744. SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
  1745. for (x = 0; x < 4; x++)
  1746. lc->pu.intra_pred_mode[x] = 1;
  1747. if (s->pps->transquant_bypass_enable_flag) {
  1748. lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
  1749. if (lc->cu.cu_transquant_bypass_flag)
  1750. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  1751. } else
  1752. lc->cu.cu_transquant_bypass_flag = 0;
  1753. if (s->sh.slice_type != I_SLICE) {
  1754. uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
  1755. x = y_cb * min_cb_width + x_cb;
  1756. for (y = 0; y < length; y++) {
  1757. memset(&s->skip_flag[x], skip_flag, length);
  1758. x += min_cb_width;
  1759. }
  1760. lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
  1761. } else {
  1762. x = y_cb * min_cb_width + x_cb;
  1763. for (y = 0; y < length; y++) {
  1764. memset(&s->skip_flag[x], 0, length);
  1765. x += min_cb_width;
  1766. }
  1767. }
  1768. if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
  1769. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
  1770. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1771. if (!s->sh.disable_deblocking_filter_flag)
  1772. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  1773. } else {
  1774. if (s->sh.slice_type != I_SLICE)
  1775. lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
  1776. if (lc->cu.pred_mode != MODE_INTRA ||
  1777. log2_cb_size == s->sps->log2_min_cb_size) {
  1778. lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
  1779. lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
  1780. lc->cu.pred_mode == MODE_INTRA;
  1781. }
  1782. if (lc->cu.pred_mode == MODE_INTRA) {
  1783. if (lc->cu.part_mode == PART_2Nx2N && s->sps->pcm_enabled_flag &&
  1784. log2_cb_size >= s->sps->pcm.log2_min_pcm_cb_size &&
  1785. log2_cb_size <= s->sps->pcm.log2_max_pcm_cb_size) {
  1786. lc->cu.pcm_flag = ff_hevc_pcm_flag_decode(s);
  1787. }
  1788. if (lc->cu.pcm_flag) {
  1789. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1790. ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
  1791. if (s->sps->pcm.loop_filter_disable_flag)
  1792. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  1793. if (ret < 0)
  1794. return ret;
  1795. } else {
  1796. intra_prediction_unit(s, x0, y0, log2_cb_size);
  1797. }
  1798. } else {
  1799. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1800. switch (lc->cu.part_mode) {
  1801. case PART_2Nx2N:
  1802. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
  1803. break;
  1804. case PART_2NxN:
  1805. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
  1806. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
  1807. break;
  1808. case PART_Nx2N:
  1809. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
  1810. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
  1811. break;
  1812. case PART_2NxnU:
  1813. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
  1814. hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
  1815. break;
  1816. case PART_2NxnD:
  1817. hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
  1818. hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
  1819. break;
  1820. case PART_nLx2N:
  1821. hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
  1822. hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
  1823. break;
  1824. case PART_nRx2N:
  1825. hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
  1826. hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
  1827. break;
  1828. case PART_NxN:
  1829. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
  1830. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
  1831. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
  1832. hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
  1833. break;
  1834. }
  1835. }
  1836. if (!lc->cu.pcm_flag) {
  1837. if (lc->cu.pred_mode != MODE_INTRA &&
  1838. !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
  1839. lc->cu.rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
  1840. }
  1841. if (lc->cu.rqt_root_cbf) {
  1842. const static int cbf[2] = { 0 };
  1843. lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
  1844. s->sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
  1845. s->sps->max_transform_hierarchy_depth_inter;
  1846. ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
  1847. log2_cb_size,
  1848. log2_cb_size, 0, 0, cbf, cbf);
  1849. if (ret < 0)
  1850. return ret;
  1851. } else {
  1852. if (!s->sh.disable_deblocking_filter_flag)
  1853. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  1854. }
  1855. }
  1856. }
  1857. if (s->pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
  1858. ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
  1859. x = y_cb * min_cb_width + x_cb;
  1860. for (y = 0; y < length; y++) {
  1861. memset(&s->qp_y_tab[x], lc->qp_y, length);
  1862. x += min_cb_width;
  1863. }
  1864. if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
  1865. ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
  1866. lc->qPy_pred = lc->qp_y;
  1867. }
  1868. set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
  1869. return 0;
  1870. }
  1871. static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
  1872. int log2_cb_size, int cb_depth)
  1873. {
  1874. HEVCLocalContext *lc = s->HEVClc;
  1875. const int cb_size = 1 << log2_cb_size;
  1876. int ret;
  1877. int qp_block_mask = (1<<(s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth)) - 1;
  1878. int split_cu;
  1879. lc->ct.depth = cb_depth;
  1880. if (x0 + cb_size <= s->sps->width &&
  1881. y0 + cb_size <= s->sps->height &&
  1882. log2_cb_size > s->sps->log2_min_cb_size) {
  1883. split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
  1884. } else {
  1885. split_cu = (log2_cb_size > s->sps->log2_min_cb_size);
  1886. }
  1887. if (s->pps->cu_qp_delta_enabled_flag &&
  1888. log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth) {
  1889. lc->tu.is_cu_qp_delta_coded = 0;
  1890. lc->tu.cu_qp_delta = 0;
  1891. }
  1892. if (s->sh.cu_chroma_qp_offset_enabled_flag &&
  1893. log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_chroma_qp_offset_depth) {
  1894. lc->tu.is_cu_chroma_qp_offset_coded = 0;
  1895. }
  1896. if (split_cu) {
  1897. const int cb_size_split = cb_size >> 1;
  1898. const int x1 = x0 + cb_size_split;
  1899. const int y1 = y0 + cb_size_split;
  1900. int more_data = 0;
  1901. more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
  1902. if (more_data < 0)
  1903. return more_data;
  1904. if (more_data && x1 < s->sps->width) {
  1905. more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
  1906. if (more_data < 0)
  1907. return more_data;
  1908. }
  1909. if (more_data && y1 < s->sps->height) {
  1910. more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
  1911. if (more_data < 0)
  1912. return more_data;
  1913. }
  1914. if (more_data && x1 < s->sps->width &&
  1915. y1 < s->sps->height) {
  1916. more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
  1917. if (more_data < 0)
  1918. return more_data;
  1919. }
  1920. if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
  1921. ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
  1922. lc->qPy_pred = lc->qp_y;
  1923. if (more_data)
  1924. return ((x1 + cb_size_split) < s->sps->width ||
  1925. (y1 + cb_size_split) < s->sps->height);
  1926. else
  1927. return 0;
  1928. } else {
  1929. ret = hls_coding_unit(s, x0, y0, log2_cb_size);
  1930. if (ret < 0)
  1931. return ret;
  1932. if ((!((x0 + cb_size) %
  1933. (1 << (s->sps->log2_ctb_size))) ||
  1934. (x0 + cb_size >= s->sps->width)) &&
  1935. (!((y0 + cb_size) %
  1936. (1 << (s->sps->log2_ctb_size))) ||
  1937. (y0 + cb_size >= s->sps->height))) {
  1938. int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
  1939. return !end_of_slice_flag;
  1940. } else {
  1941. return 1;
  1942. }
  1943. }
  1944. return 0;
  1945. }
  1946. static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
  1947. int ctb_addr_ts)
  1948. {
  1949. HEVCLocalContext *lc = s->HEVClc;
  1950. int ctb_size = 1 << s->sps->log2_ctb_size;
  1951. int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  1952. int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
  1953. s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
  1954. if (s->pps->entropy_coding_sync_enabled_flag) {
  1955. if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
  1956. lc->first_qp_group = 1;
  1957. lc->end_of_tiles_x = s->sps->width;
  1958. } else if (s->pps->tiles_enabled_flag) {
  1959. if (ctb_addr_ts && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
  1960. int idxX = s->pps->col_idxX[x_ctb >> s->sps->log2_ctb_size];
  1961. lc->end_of_tiles_x = x_ctb + (s->pps->column_width[idxX] << s->sps->log2_ctb_size);
  1962. lc->first_qp_group = 1;
  1963. }
  1964. } else {
  1965. lc->end_of_tiles_x = s->sps->width;
  1966. }
  1967. lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->sps->height);
  1968. lc->boundary_flags = 0;
  1969. if (s->pps->tiles_enabled_flag) {
  1970. if (x_ctb > 0 && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
  1971. lc->boundary_flags |= BOUNDARY_LEFT_TILE;
  1972. if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
  1973. lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
  1974. if (y_ctb > 0 && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->sps->ctb_width]])
  1975. lc->boundary_flags |= BOUNDARY_UPPER_TILE;
  1976. if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->sps->ctb_width])
  1977. lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
  1978. } else {
  1979. if (!ctb_addr_in_slice > 0)
  1980. lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
  1981. if (ctb_addr_in_slice < s->sps->ctb_width)
  1982. lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
  1983. }
  1984. lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
  1985. lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
  1986. lc->ctb_up_right_flag = ((y_ctb > 0) && (ctb_addr_in_slice+1 >= s->sps->ctb_width) && (s->pps->tile_id[ctb_addr_ts] == s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - s->sps->ctb_width]]));
  1987. lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= s->sps->ctb_width) && (s->pps->tile_id[ctb_addr_ts] == s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->sps->ctb_width]]));
  1988. }
  1989. static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
  1990. {
  1991. HEVCContext *s = avctxt->priv_data;
  1992. int ctb_size = 1 << s->sps->log2_ctb_size;
  1993. int more_data = 1;
  1994. int x_ctb = 0;
  1995. int y_ctb = 0;
  1996. int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
  1997. if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
  1998. av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
  1999. return AVERROR_INVALIDDATA;
  2000. }
  2001. if (s->sh.dependent_slice_segment_flag) {
  2002. int prev_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
  2003. if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
  2004. av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
  2005. return AVERROR_INVALIDDATA;
  2006. }
  2007. }
  2008. while (more_data && ctb_addr_ts < s->sps->ctb_size) {
  2009. int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  2010. x_ctb = (ctb_addr_rs % ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  2011. y_ctb = (ctb_addr_rs / ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  2012. hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
  2013. ff_hevc_cabac_init(s, ctb_addr_ts);
  2014. hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
  2015. s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
  2016. s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
  2017. s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
  2018. more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
  2019. if (more_data < 0) {
  2020. s->tab_slice_address[ctb_addr_rs] = -1;
  2021. return more_data;
  2022. }
  2023. ctb_addr_ts++;
  2024. ff_hevc_save_states(s, ctb_addr_ts);
  2025. ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
  2026. }
  2027. if (x_ctb + ctb_size >= s->sps->width &&
  2028. y_ctb + ctb_size >= s->sps->height)
  2029. ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
  2030. return ctb_addr_ts;
  2031. }
  2032. static int hls_slice_data(HEVCContext *s)
  2033. {
  2034. int arg[2];
  2035. int ret[2];
  2036. arg[0] = 0;
  2037. arg[1] = 1;
  2038. s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
  2039. return ret[0];
  2040. }
  2041. static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
  2042. {
  2043. HEVCContext *s1 = avctxt->priv_data, *s;
  2044. HEVCLocalContext *lc;
  2045. int ctb_size = 1<< s1->sps->log2_ctb_size;
  2046. int more_data = 1;
  2047. int *ctb_row_p = input_ctb_row;
  2048. int ctb_row = ctb_row_p[job];
  2049. int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->sps->width + ctb_size - 1) >> s1->sps->log2_ctb_size);
  2050. int ctb_addr_ts = s1->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
  2051. int thread = ctb_row % s1->threads_number;
  2052. int ret;
  2053. s = s1->sList[self_id];
  2054. lc = s->HEVClc;
  2055. if(ctb_row) {
  2056. ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
  2057. if (ret < 0)
  2058. return ret;
  2059. ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
  2060. }
  2061. while(more_data && ctb_addr_ts < s->sps->ctb_size) {
  2062. int x_ctb = (ctb_addr_rs % s->sps->ctb_width) << s->sps->log2_ctb_size;
  2063. int y_ctb = (ctb_addr_rs / s->sps->ctb_width) << s->sps->log2_ctb_size;
  2064. hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
  2065. ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
  2066. if (avpriv_atomic_int_get(&s1->wpp_err)){
  2067. ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
  2068. return 0;
  2069. }
  2070. ff_hevc_cabac_init(s, ctb_addr_ts);
  2071. hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
  2072. more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
  2073. if (more_data < 0) {
  2074. s->tab_slice_address[ctb_addr_rs] = -1;
  2075. avpriv_atomic_int_set(&s1->wpp_err, 1);
  2076. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  2077. return more_data;
  2078. }
  2079. ctb_addr_ts++;
  2080. ff_hevc_save_states(s, ctb_addr_ts);
  2081. ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
  2082. ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
  2083. if (!more_data && (x_ctb+ctb_size) < s->sps->width && ctb_row != s->sh.num_entry_point_offsets) {
  2084. avpriv_atomic_int_set(&s1->wpp_err, 1);
  2085. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  2086. return 0;
  2087. }
  2088. if ((x_ctb+ctb_size) >= s->sps->width && (y_ctb+ctb_size) >= s->sps->height ) {
  2089. ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
  2090. ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
  2091. return ctb_addr_ts;
  2092. }
  2093. ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  2094. x_ctb+=ctb_size;
  2095. if(x_ctb >= s->sps->width) {
  2096. break;
  2097. }
  2098. }
  2099. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  2100. return 0;
  2101. }
  2102. static int hls_slice_data_wpp(HEVCContext *s, const uint8_t *nal, int length)
  2103. {
  2104. HEVCLocalContext *lc = s->HEVClc;
  2105. int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
  2106. int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
  2107. int64_t offset;
  2108. int startheader, cmpt = 0;
  2109. int i, j, res = 0;
  2110. ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
  2111. if (!s->sList[1]) {
  2112. for (i = 1; i < s->threads_number; i++) {
  2113. s->sList[i] = av_malloc(sizeof(HEVCContext));
  2114. memcpy(s->sList[i], s, sizeof(HEVCContext));
  2115. s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
  2116. s->sList[i]->HEVClc = s->HEVClcList[i];
  2117. }
  2118. }
  2119. offset = (lc->gb.index >> 3);
  2120. for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < s->skipped_bytes; j++) {
  2121. if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
  2122. startheader--;
  2123. cmpt++;
  2124. }
  2125. }
  2126. for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
  2127. offset += (s->sh.entry_point_offset[i - 1] - cmpt);
  2128. for (j = 0, cmpt = 0, startheader = offset
  2129. + s->sh.entry_point_offset[i]; j < s->skipped_bytes; j++) {
  2130. if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
  2131. startheader--;
  2132. cmpt++;
  2133. }
  2134. }
  2135. s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
  2136. s->sh.offset[i - 1] = offset;
  2137. }
  2138. if (s->sh.num_entry_point_offsets != 0) {
  2139. offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
  2140. if (length < offset) {
  2141. av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
  2142. res = AVERROR_INVALIDDATA;
  2143. goto error;
  2144. }
  2145. s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
  2146. s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
  2147. }
  2148. s->data = nal;
  2149. for (i = 1; i < s->threads_number; i++) {
  2150. s->sList[i]->HEVClc->first_qp_group = 1;
  2151. s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
  2152. memcpy(s->sList[i], s, sizeof(HEVCContext));
  2153. s->sList[i]->HEVClc = s->HEVClcList[i];
  2154. }
  2155. avpriv_atomic_int_set(&s->wpp_err, 0);
  2156. ff_reset_entries(s->avctx);
  2157. for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
  2158. arg[i] = i;
  2159. ret[i] = 0;
  2160. }
  2161. if (s->pps->entropy_coding_sync_enabled_flag)
  2162. s->avctx->execute2(s->avctx, (void *) hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
  2163. for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
  2164. res += ret[i];
  2165. error:
  2166. av_free(ret);
  2167. av_free(arg);
  2168. return res;
  2169. }
  2170. /**
  2171. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  2172. * 0 if the unit should be skipped, 1 otherwise
  2173. */
  2174. static int hls_nal_unit(HEVCContext *s)
  2175. {
  2176. GetBitContext *gb = &s->HEVClc->gb;
  2177. int nuh_layer_id;
  2178. if (get_bits1(gb) != 0)
  2179. return AVERROR_INVALIDDATA;
  2180. s->nal_unit_type = get_bits(gb, 6);
  2181. nuh_layer_id = get_bits(gb, 6);
  2182. s->temporal_id = get_bits(gb, 3) - 1;
  2183. if (s->temporal_id < 0)
  2184. return AVERROR_INVALIDDATA;
  2185. av_log(s->avctx, AV_LOG_DEBUG,
  2186. "nal_unit_type: %d, nuh_layer_id: %d, temporal_id: %d\n",
  2187. s->nal_unit_type, nuh_layer_id, s->temporal_id);
  2188. return nuh_layer_id == 0;
  2189. }
  2190. static int set_side_data(HEVCContext *s)
  2191. {
  2192. AVFrame *out = s->ref->frame;
  2193. if (s->sei_frame_packing_present &&
  2194. s->frame_packing_arrangement_type >= 3 &&
  2195. s->frame_packing_arrangement_type <= 5 &&
  2196. s->content_interpretation_type > 0 &&
  2197. s->content_interpretation_type < 3) {
  2198. AVStereo3D *stereo = av_stereo3d_create_side_data(out);
  2199. if (!stereo)
  2200. return AVERROR(ENOMEM);
  2201. switch (s->frame_packing_arrangement_type) {
  2202. case 3:
  2203. if (s->quincunx_subsampling)
  2204. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  2205. else
  2206. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  2207. break;
  2208. case 4:
  2209. stereo->type = AV_STEREO3D_TOPBOTTOM;
  2210. break;
  2211. case 5:
  2212. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  2213. break;
  2214. }
  2215. if (s->content_interpretation_type == 2)
  2216. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  2217. }
  2218. if (s->sei_display_orientation_present &&
  2219. (s->sei_anticlockwise_rotation || s->sei_hflip || s->sei_vflip)) {
  2220. double angle = s->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
  2221. AVFrameSideData *rotation = av_frame_new_side_data(out,
  2222. AV_FRAME_DATA_DISPLAYMATRIX,
  2223. sizeof(int32_t) * 9);
  2224. if (!rotation)
  2225. return AVERROR(ENOMEM);
  2226. av_display_rotation_set((int32_t *)rotation->data, angle);
  2227. av_display_matrix_flip((int32_t *)rotation->data,
  2228. s->sei_hflip, s->sei_vflip);
  2229. }
  2230. return 0;
  2231. }
  2232. static int hevc_frame_start(HEVCContext *s)
  2233. {
  2234. HEVCLocalContext *lc = s->HEVClc;
  2235. int pic_size_in_ctb = ((s->sps->width >> s->sps->log2_min_cb_size) + 1) *
  2236. ((s->sps->height >> s->sps->log2_min_cb_size) + 1);
  2237. int ret;
  2238. memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
  2239. memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
  2240. memset(s->cbf_luma, 0, s->sps->min_tb_width * s->sps->min_tb_height);
  2241. memset(s->is_pcm, 0, (s->sps->min_pu_width + 1) * (s->sps->min_pu_height + 1));
  2242. memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
  2243. s->is_decoded = 0;
  2244. s->first_nal_type = s->nal_unit_type;
  2245. if (s->pps->tiles_enabled_flag)
  2246. lc->end_of_tiles_x = s->pps->column_width[0] << s->sps->log2_ctb_size;
  2247. ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
  2248. if (ret < 0)
  2249. goto fail;
  2250. ret = ff_hevc_frame_rps(s);
  2251. if (ret < 0) {
  2252. av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
  2253. goto fail;
  2254. }
  2255. s->ref->frame->key_frame = IS_IRAP(s);
  2256. ret = set_side_data(s);
  2257. if (ret < 0)
  2258. goto fail;
  2259. s->frame->pict_type = 3 - s->sh.slice_type;
  2260. if (!IS_IRAP(s))
  2261. ff_hevc_bump_frame(s);
  2262. av_frame_unref(s->output_frame);
  2263. ret = ff_hevc_output_frame(s, s->output_frame, 0);
  2264. if (ret < 0)
  2265. goto fail;
  2266. ff_thread_finish_setup(s->avctx);
  2267. return 0;
  2268. fail:
  2269. if (s->ref && s->threads_type == FF_THREAD_FRAME)
  2270. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  2271. s->ref = NULL;
  2272. return ret;
  2273. }
  2274. static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
  2275. {
  2276. HEVCLocalContext *lc = s->HEVClc;
  2277. GetBitContext *gb = &lc->gb;
  2278. int ctb_addr_ts, ret;
  2279. ret = init_get_bits8(gb, nal, length);
  2280. if (ret < 0)
  2281. return ret;
  2282. ret = hls_nal_unit(s);
  2283. if (ret < 0) {
  2284. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  2285. s->nal_unit_type);
  2286. goto fail;
  2287. } else if (!ret)
  2288. return 0;
  2289. switch (s->nal_unit_type) {
  2290. case NAL_VPS:
  2291. ret = ff_hevc_decode_nal_vps(s);
  2292. if (ret < 0)
  2293. goto fail;
  2294. break;
  2295. case NAL_SPS:
  2296. ret = ff_hevc_decode_nal_sps(s);
  2297. if (ret < 0)
  2298. goto fail;
  2299. break;
  2300. case NAL_PPS:
  2301. ret = ff_hevc_decode_nal_pps(s);
  2302. if (ret < 0)
  2303. goto fail;
  2304. break;
  2305. case NAL_SEI_PREFIX:
  2306. case NAL_SEI_SUFFIX:
  2307. ret = ff_hevc_decode_nal_sei(s);
  2308. if (ret < 0)
  2309. goto fail;
  2310. break;
  2311. case NAL_TRAIL_R:
  2312. case NAL_TRAIL_N:
  2313. case NAL_TSA_N:
  2314. case NAL_TSA_R:
  2315. case NAL_STSA_N:
  2316. case NAL_STSA_R:
  2317. case NAL_BLA_W_LP:
  2318. case NAL_BLA_W_RADL:
  2319. case NAL_BLA_N_LP:
  2320. case NAL_IDR_W_RADL:
  2321. case NAL_IDR_N_LP:
  2322. case NAL_CRA_NUT:
  2323. case NAL_RADL_N:
  2324. case NAL_RADL_R:
  2325. case NAL_RASL_N:
  2326. case NAL_RASL_R:
  2327. ret = hls_slice_header(s);
  2328. if (ret < 0)
  2329. return ret;
  2330. if (s->max_ra == INT_MAX) {
  2331. if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
  2332. s->max_ra = s->poc;
  2333. } else {
  2334. if (IS_IDR(s))
  2335. s->max_ra = INT_MIN;
  2336. }
  2337. }
  2338. if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
  2339. s->poc <= s->max_ra) {
  2340. s->is_decoded = 0;
  2341. break;
  2342. } else {
  2343. if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
  2344. s->max_ra = INT_MIN;
  2345. }
  2346. if (s->sh.first_slice_in_pic_flag) {
  2347. ret = hevc_frame_start(s);
  2348. if (ret < 0)
  2349. return ret;
  2350. } else if (!s->ref) {
  2351. av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
  2352. goto fail;
  2353. }
  2354. if (s->nal_unit_type != s->first_nal_type) {
  2355. av_log(s->avctx, AV_LOG_ERROR,
  2356. "Non-matching NAL types of the VCL NALUs: %d %d\n",
  2357. s->first_nal_type, s->nal_unit_type);
  2358. return AVERROR_INVALIDDATA;
  2359. }
  2360. if (!s->sh.dependent_slice_segment_flag &&
  2361. s->sh.slice_type != I_SLICE) {
  2362. ret = ff_hevc_slice_rpl(s);
  2363. if (ret < 0) {
  2364. av_log(s->avctx, AV_LOG_WARNING,
  2365. "Error constructing the reference lists for the current slice.\n");
  2366. goto fail;
  2367. }
  2368. }
  2369. if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
  2370. ctb_addr_ts = hls_slice_data_wpp(s, nal, length);
  2371. else
  2372. ctb_addr_ts = hls_slice_data(s);
  2373. if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {
  2374. s->is_decoded = 1;
  2375. }
  2376. if (ctb_addr_ts < 0) {
  2377. ret = ctb_addr_ts;
  2378. goto fail;
  2379. }
  2380. break;
  2381. case NAL_EOS_NUT:
  2382. case NAL_EOB_NUT:
  2383. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2384. s->max_ra = INT_MAX;
  2385. break;
  2386. case NAL_AUD:
  2387. case NAL_FD_NUT:
  2388. break;
  2389. default:
  2390. av_log(s->avctx, AV_LOG_INFO,
  2391. "Skipping NAL unit %d\n", s->nal_unit_type);
  2392. }
  2393. return 0;
  2394. fail:
  2395. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  2396. return ret;
  2397. return 0;
  2398. }
  2399. /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
  2400. * between these functions would be nice. */
  2401. int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
  2402. HEVCNAL *nal)
  2403. {
  2404. int i, si, di;
  2405. uint8_t *dst;
  2406. s->skipped_bytes = 0;
  2407. #define STARTCODE_TEST \
  2408. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  2409. if (src[i + 2] != 3) { \
  2410. /* startcode, so we must be past the end */ \
  2411. length = i; \
  2412. } \
  2413. break; \
  2414. }
  2415. #if HAVE_FAST_UNALIGNED
  2416. #define FIND_FIRST_ZERO \
  2417. if (i > 0 && !src[i]) \
  2418. i--; \
  2419. while (src[i]) \
  2420. i++
  2421. #if HAVE_FAST_64BIT
  2422. for (i = 0; i + 1 < length; i += 9) {
  2423. if (!((~AV_RN64A(src + i) &
  2424. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  2425. 0x8000800080008080ULL))
  2426. continue;
  2427. FIND_FIRST_ZERO;
  2428. STARTCODE_TEST;
  2429. i -= 7;
  2430. }
  2431. #else
  2432. for (i = 0; i + 1 < length; i += 5) {
  2433. if (!((~AV_RN32A(src + i) &
  2434. (AV_RN32A(src + i) - 0x01000101U)) &
  2435. 0x80008080U))
  2436. continue;
  2437. FIND_FIRST_ZERO;
  2438. STARTCODE_TEST;
  2439. i -= 3;
  2440. }
  2441. #endif /* HAVE_FAST_64BIT */
  2442. #else
  2443. for (i = 0; i + 1 < length; i += 2) {
  2444. if (src[i])
  2445. continue;
  2446. if (i > 0 && src[i - 1] == 0)
  2447. i--;
  2448. STARTCODE_TEST;
  2449. }
  2450. #endif /* HAVE_FAST_UNALIGNED */
  2451. if (i >= length - 1) { // no escaped 0
  2452. nal->data = src;
  2453. nal->size = length;
  2454. return length;
  2455. }
  2456. av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
  2457. length + FF_INPUT_BUFFER_PADDING_SIZE);
  2458. if (!nal->rbsp_buffer)
  2459. return AVERROR(ENOMEM);
  2460. dst = nal->rbsp_buffer;
  2461. memcpy(dst, src, i);
  2462. si = di = i;
  2463. while (si + 2 < length) {
  2464. // remove escapes (very rare 1:2^22)
  2465. if (src[si + 2] > 3) {
  2466. dst[di++] = src[si++];
  2467. dst[di++] = src[si++];
  2468. } else if (src[si] == 0 && src[si + 1] == 0) {
  2469. if (src[si + 2] == 3) { // escape
  2470. dst[di++] = 0;
  2471. dst[di++] = 0;
  2472. si += 3;
  2473. s->skipped_bytes++;
  2474. if (s->skipped_bytes_pos_size < s->skipped_bytes) {
  2475. s->skipped_bytes_pos_size *= 2;
  2476. av_reallocp_array(&s->skipped_bytes_pos,
  2477. s->skipped_bytes_pos_size,
  2478. sizeof(*s->skipped_bytes_pos));
  2479. if (!s->skipped_bytes_pos)
  2480. return AVERROR(ENOMEM);
  2481. }
  2482. if (s->skipped_bytes_pos)
  2483. s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
  2484. continue;
  2485. } else // next start code
  2486. goto nsc;
  2487. }
  2488. dst[di++] = src[si++];
  2489. }
  2490. while (si < length)
  2491. dst[di++] = src[si++];
  2492. nsc:
  2493. memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  2494. nal->data = dst;
  2495. nal->size = di;
  2496. return si;
  2497. }
  2498. static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
  2499. {
  2500. int i, consumed, ret = 0;
  2501. s->ref = NULL;
  2502. s->last_eos = s->eos;
  2503. s->eos = 0;
  2504. /* split the input packet into NAL units, so we know the upper bound on the
  2505. * number of slices in the frame */
  2506. s->nb_nals = 0;
  2507. while (length >= 4) {
  2508. HEVCNAL *nal;
  2509. int extract_length = 0;
  2510. if (s->is_nalff) {
  2511. int i;
  2512. for (i = 0; i < s->nal_length_size; i++)
  2513. extract_length = (extract_length << 8) | buf[i];
  2514. buf += s->nal_length_size;
  2515. length -= s->nal_length_size;
  2516. if (extract_length > length) {
  2517. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
  2518. ret = AVERROR_INVALIDDATA;
  2519. goto fail;
  2520. }
  2521. } else {
  2522. /* search start code */
  2523. while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
  2524. ++buf;
  2525. --length;
  2526. if (length < 4) {
  2527. av_log(s->avctx, AV_LOG_ERROR, "No start code is found.\n");
  2528. ret = AVERROR_INVALIDDATA;
  2529. goto fail;
  2530. }
  2531. }
  2532. buf += 3;
  2533. length -= 3;
  2534. }
  2535. if (!s->is_nalff)
  2536. extract_length = length;
  2537. if (s->nals_allocated < s->nb_nals + 1) {
  2538. int new_size = s->nals_allocated + 1;
  2539. void *tmp = av_realloc_array(s->nals, new_size, sizeof(*s->nals));
  2540. ret = AVERROR(ENOMEM);
  2541. if (!tmp) {
  2542. goto fail;
  2543. }
  2544. s->nals = tmp;
  2545. memset(s->nals + s->nals_allocated, 0,
  2546. (new_size - s->nals_allocated) * sizeof(*s->nals));
  2547. tmp = av_realloc_array(s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));
  2548. if (!tmp)
  2549. goto fail;
  2550. s->skipped_bytes_nal = tmp;
  2551. tmp = av_realloc_array(s->skipped_bytes_pos_size_nal, new_size, sizeof(*s->skipped_bytes_pos_size_nal));
  2552. if (!tmp)
  2553. goto fail;
  2554. s->skipped_bytes_pos_size_nal = tmp;
  2555. tmp = av_realloc_array(s->skipped_bytes_pos_nal, new_size, sizeof(*s->skipped_bytes_pos_nal));
  2556. if (!tmp)
  2557. goto fail;
  2558. s->skipped_bytes_pos_nal = tmp;
  2559. s->skipped_bytes_pos_size_nal[s->nals_allocated] = 1024; // initial buffer size
  2560. s->skipped_bytes_pos_nal[s->nals_allocated] = av_malloc_array(s->skipped_bytes_pos_size_nal[s->nals_allocated], sizeof(*s->skipped_bytes_pos));
  2561. s->nals_allocated = new_size;
  2562. }
  2563. s->skipped_bytes_pos_size = s->skipped_bytes_pos_size_nal[s->nb_nals];
  2564. s->skipped_bytes_pos = s->skipped_bytes_pos_nal[s->nb_nals];
  2565. nal = &s->nals[s->nb_nals];
  2566. consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
  2567. s->skipped_bytes_nal[s->nb_nals] = s->skipped_bytes;
  2568. s->skipped_bytes_pos_size_nal[s->nb_nals] = s->skipped_bytes_pos_size;
  2569. s->skipped_bytes_pos_nal[s->nb_nals++] = s->skipped_bytes_pos;
  2570. if (consumed < 0) {
  2571. ret = consumed;
  2572. goto fail;
  2573. }
  2574. ret = init_get_bits8(&s->HEVClc->gb, nal->data, nal->size);
  2575. if (ret < 0)
  2576. goto fail;
  2577. hls_nal_unit(s);
  2578. if (s->nal_unit_type == NAL_EOB_NUT ||
  2579. s->nal_unit_type == NAL_EOS_NUT)
  2580. s->eos = 1;
  2581. buf += consumed;
  2582. length -= consumed;
  2583. }
  2584. /* parse the NAL units */
  2585. for (i = 0; i < s->nb_nals; i++) {
  2586. s->skipped_bytes = s->skipped_bytes_nal[i];
  2587. s->skipped_bytes_pos = s->skipped_bytes_pos_nal[i];
  2588. ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);
  2589. if (ret < 0) {
  2590. av_log(s->avctx, AV_LOG_WARNING,
  2591. "Error parsing NAL unit #%d.\n", i);
  2592. goto fail;
  2593. }
  2594. }
  2595. fail:
  2596. if (s->ref && s->threads_type == FF_THREAD_FRAME)
  2597. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  2598. return ret;
  2599. }
  2600. static void print_md5(void *log_ctx, int level, uint8_t md5[16])
  2601. {
  2602. int i;
  2603. for (i = 0; i < 16; i++)
  2604. av_log(log_ctx, level, "%02"PRIx8, md5[i]);
  2605. }
  2606. static int verify_md5(HEVCContext *s, AVFrame *frame)
  2607. {
  2608. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  2609. int pixel_shift;
  2610. int i, j;
  2611. if (!desc)
  2612. return AVERROR(EINVAL);
  2613. pixel_shift = desc->comp[0].depth_minus1 > 7;
  2614. av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
  2615. s->poc);
  2616. /* the checksums are LE, so we have to byteswap for >8bpp formats
  2617. * on BE arches */
  2618. #if HAVE_BIGENDIAN
  2619. if (pixel_shift && !s->checksum_buf) {
  2620. av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
  2621. FFMAX3(frame->linesize[0], frame->linesize[1],
  2622. frame->linesize[2]));
  2623. if (!s->checksum_buf)
  2624. return AVERROR(ENOMEM);
  2625. }
  2626. #endif
  2627. for (i = 0; frame->data[i]; i++) {
  2628. int width = s->avctx->coded_width;
  2629. int height = s->avctx->coded_height;
  2630. int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
  2631. int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
  2632. uint8_t md5[16];
  2633. av_md5_init(s->md5_ctx);
  2634. for (j = 0; j < h; j++) {
  2635. const uint8_t *src = frame->data[i] + j * frame->linesize[i];
  2636. #if HAVE_BIGENDIAN
  2637. if (pixel_shift) {
  2638. s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
  2639. (const uint16_t *) src, w);
  2640. src = s->checksum_buf;
  2641. }
  2642. #endif
  2643. av_md5_update(s->md5_ctx, src, w << pixel_shift);
  2644. }
  2645. av_md5_final(s->md5_ctx, md5);
  2646. if (!memcmp(md5, s->md5[i], 16)) {
  2647. av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
  2648. print_md5(s->avctx, AV_LOG_DEBUG, md5);
  2649. av_log (s->avctx, AV_LOG_DEBUG, "; ");
  2650. } else {
  2651. av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
  2652. print_md5(s->avctx, AV_LOG_ERROR, md5);
  2653. av_log (s->avctx, AV_LOG_ERROR, " != ");
  2654. print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
  2655. av_log (s->avctx, AV_LOG_ERROR, "\n");
  2656. return AVERROR_INVALIDDATA;
  2657. }
  2658. }
  2659. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2660. return 0;
  2661. }
  2662. static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
  2663. AVPacket *avpkt)
  2664. {
  2665. int ret;
  2666. HEVCContext *s = avctx->priv_data;
  2667. if (!avpkt->size) {
  2668. ret = ff_hevc_output_frame(s, data, 1);
  2669. if (ret < 0)
  2670. return ret;
  2671. *got_output = ret;
  2672. return 0;
  2673. }
  2674. s->ref = NULL;
  2675. ret = decode_nal_units(s, avpkt->data, avpkt->size);
  2676. if (ret < 0)
  2677. return ret;
  2678. /* verify the SEI checksum */
  2679. if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
  2680. s->is_md5) {
  2681. ret = verify_md5(s, s->ref->frame);
  2682. if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
  2683. ff_hevc_unref_frame(s, s->ref, ~0);
  2684. return ret;
  2685. }
  2686. }
  2687. s->is_md5 = 0;
  2688. if (s->is_decoded) {
  2689. av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
  2690. s->is_decoded = 0;
  2691. }
  2692. if (s->output_frame->buf[0]) {
  2693. av_frame_move_ref(data, s->output_frame);
  2694. *got_output = 1;
  2695. }
  2696. return avpkt->size;
  2697. }
  2698. static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
  2699. {
  2700. int ret;
  2701. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  2702. if (ret < 0)
  2703. return ret;
  2704. dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
  2705. if (!dst->tab_mvf_buf)
  2706. goto fail;
  2707. dst->tab_mvf = src->tab_mvf;
  2708. dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
  2709. if (!dst->rpl_tab_buf)
  2710. goto fail;
  2711. dst->rpl_tab = src->rpl_tab;
  2712. dst->rpl_buf = av_buffer_ref(src->rpl_buf);
  2713. if (!dst->rpl_buf)
  2714. goto fail;
  2715. dst->poc = src->poc;
  2716. dst->ctb_count = src->ctb_count;
  2717. dst->window = src->window;
  2718. dst->flags = src->flags;
  2719. dst->sequence = src->sequence;
  2720. return 0;
  2721. fail:
  2722. ff_hevc_unref_frame(s, dst, ~0);
  2723. return AVERROR(ENOMEM);
  2724. }
  2725. static av_cold int hevc_decode_free(AVCodecContext *avctx)
  2726. {
  2727. HEVCContext *s = avctx->priv_data;
  2728. int i;
  2729. pic_arrays_free(s);
  2730. av_freep(&s->md5_ctx);
  2731. for(i=0; i < s->nals_allocated; i++) {
  2732. av_freep(&s->skipped_bytes_pos_nal[i]);
  2733. }
  2734. av_freep(&s->skipped_bytes_pos_size_nal);
  2735. av_freep(&s->skipped_bytes_nal);
  2736. av_freep(&s->skipped_bytes_pos_nal);
  2737. av_freep(&s->cabac_state);
  2738. av_frame_free(&s->tmp_frame);
  2739. av_frame_free(&s->output_frame);
  2740. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2741. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2742. av_frame_free(&s->DPB[i].frame);
  2743. }
  2744. for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++)
  2745. av_buffer_unref(&s->vps_list[i]);
  2746. for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
  2747. av_buffer_unref(&s->sps_list[i]);
  2748. for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
  2749. av_buffer_unref(&s->pps_list[i]);
  2750. s->sps = NULL;
  2751. s->pps = NULL;
  2752. s->vps = NULL;
  2753. av_buffer_unref(&s->current_sps);
  2754. av_freep(&s->sh.entry_point_offset);
  2755. av_freep(&s->sh.offset);
  2756. av_freep(&s->sh.size);
  2757. for (i = 1; i < s->threads_number; i++) {
  2758. HEVCLocalContext *lc = s->HEVClcList[i];
  2759. if (lc) {
  2760. av_freep(&s->HEVClcList[i]);
  2761. av_freep(&s->sList[i]);
  2762. }
  2763. }
  2764. if (s->HEVClc == s->HEVClcList[0])
  2765. s->HEVClc = NULL;
  2766. av_freep(&s->HEVClcList[0]);
  2767. for (i = 0; i < s->nals_allocated; i++)
  2768. av_freep(&s->nals[i].rbsp_buffer);
  2769. av_freep(&s->nals);
  2770. s->nals_allocated = 0;
  2771. return 0;
  2772. }
  2773. static av_cold int hevc_init_context(AVCodecContext *avctx)
  2774. {
  2775. HEVCContext *s = avctx->priv_data;
  2776. int i;
  2777. s->avctx = avctx;
  2778. s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
  2779. if (!s->HEVClc)
  2780. goto fail;
  2781. s->HEVClcList[0] = s->HEVClc;
  2782. s->sList[0] = s;
  2783. s->cabac_state = av_malloc(HEVC_CONTEXTS);
  2784. if (!s->cabac_state)
  2785. goto fail;
  2786. s->tmp_frame = av_frame_alloc();
  2787. if (!s->tmp_frame)
  2788. goto fail;
  2789. s->output_frame = av_frame_alloc();
  2790. if (!s->output_frame)
  2791. goto fail;
  2792. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2793. s->DPB[i].frame = av_frame_alloc();
  2794. if (!s->DPB[i].frame)
  2795. goto fail;
  2796. s->DPB[i].tf.f = s->DPB[i].frame;
  2797. }
  2798. s->max_ra = INT_MAX;
  2799. s->md5_ctx = av_md5_alloc();
  2800. if (!s->md5_ctx)
  2801. goto fail;
  2802. ff_bswapdsp_init(&s->bdsp);
  2803. s->context_initialized = 1;
  2804. s->eos = 0;
  2805. return 0;
  2806. fail:
  2807. hevc_decode_free(avctx);
  2808. return AVERROR(ENOMEM);
  2809. }
  2810. static int hevc_update_thread_context(AVCodecContext *dst,
  2811. const AVCodecContext *src)
  2812. {
  2813. HEVCContext *s = dst->priv_data;
  2814. HEVCContext *s0 = src->priv_data;
  2815. int i, ret;
  2816. if (!s->context_initialized) {
  2817. ret = hevc_init_context(dst);
  2818. if (ret < 0)
  2819. return ret;
  2820. }
  2821. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2822. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2823. if (s0->DPB[i].frame->buf[0]) {
  2824. ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
  2825. if (ret < 0)
  2826. return ret;
  2827. }
  2828. }
  2829. if (s->sps != s0->sps)
  2830. s->sps = NULL;
  2831. for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++) {
  2832. av_buffer_unref(&s->vps_list[i]);
  2833. if (s0->vps_list[i]) {
  2834. s->vps_list[i] = av_buffer_ref(s0->vps_list[i]);
  2835. if (!s->vps_list[i])
  2836. return AVERROR(ENOMEM);
  2837. }
  2838. }
  2839. for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++) {
  2840. av_buffer_unref(&s->sps_list[i]);
  2841. if (s0->sps_list[i]) {
  2842. s->sps_list[i] = av_buffer_ref(s0->sps_list[i]);
  2843. if (!s->sps_list[i])
  2844. return AVERROR(ENOMEM);
  2845. }
  2846. }
  2847. for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
  2848. av_buffer_unref(&s->pps_list[i]);
  2849. if (s0->pps_list[i]) {
  2850. s->pps_list[i] = av_buffer_ref(s0->pps_list[i]);
  2851. if (!s->pps_list[i])
  2852. return AVERROR(ENOMEM);
  2853. }
  2854. }
  2855. av_buffer_unref(&s->current_sps);
  2856. if (s0->current_sps) {
  2857. s->current_sps = av_buffer_ref(s0->current_sps);
  2858. if (!s->current_sps)
  2859. return AVERROR(ENOMEM);
  2860. }
  2861. if (s->sps != s0->sps)
  2862. if ((ret = set_sps(s, s0->sps)) < 0)
  2863. return ret;
  2864. s->seq_decode = s0->seq_decode;
  2865. s->seq_output = s0->seq_output;
  2866. s->pocTid0 = s0->pocTid0;
  2867. s->max_ra = s0->max_ra;
  2868. s->eos = s0->eos;
  2869. s->no_rasl_output_flag = s0->no_rasl_output_flag;
  2870. s->is_nalff = s0->is_nalff;
  2871. s->nal_length_size = s0->nal_length_size;
  2872. s->threads_number = s0->threads_number;
  2873. s->threads_type = s0->threads_type;
  2874. if (s0->eos) {
  2875. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2876. s->max_ra = INT_MAX;
  2877. }
  2878. return 0;
  2879. }
  2880. static int hevc_decode_extradata(HEVCContext *s)
  2881. {
  2882. AVCodecContext *avctx = s->avctx;
  2883. GetByteContext gb;
  2884. int ret;
  2885. bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
  2886. if (avctx->extradata_size > 3 &&
  2887. (avctx->extradata[0] || avctx->extradata[1] ||
  2888. avctx->extradata[2] > 1)) {
  2889. /* It seems the extradata is encoded as hvcC format.
  2890. * Temporarily, we support configurationVersion==0 until 14496-15 3rd
  2891. * is finalized. When finalized, configurationVersion will be 1 and we
  2892. * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
  2893. int i, j, num_arrays, nal_len_size;
  2894. s->is_nalff = 1;
  2895. bytestream2_skip(&gb, 21);
  2896. nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
  2897. num_arrays = bytestream2_get_byte(&gb);
  2898. /* nal units in the hvcC always have length coded with 2 bytes,
  2899. * so put a fake nal_length_size = 2 while parsing them */
  2900. s->nal_length_size = 2;
  2901. /* Decode nal units from hvcC. */
  2902. for (i = 0; i < num_arrays; i++) {
  2903. int type = bytestream2_get_byte(&gb) & 0x3f;
  2904. int cnt = bytestream2_get_be16(&gb);
  2905. for (j = 0; j < cnt; j++) {
  2906. // +2 for the nal size field
  2907. int nalsize = bytestream2_peek_be16(&gb) + 2;
  2908. if (bytestream2_get_bytes_left(&gb) < nalsize) {
  2909. av_log(s->avctx, AV_LOG_ERROR,
  2910. "Invalid NAL unit size in extradata.\n");
  2911. return AVERROR_INVALIDDATA;
  2912. }
  2913. ret = decode_nal_units(s, gb.buffer, nalsize);
  2914. if (ret < 0) {
  2915. av_log(avctx, AV_LOG_ERROR,
  2916. "Decoding nal unit %d %d from hvcC failed\n",
  2917. type, i);
  2918. return ret;
  2919. }
  2920. bytestream2_skip(&gb, nalsize);
  2921. }
  2922. }
  2923. /* Now store right nal length size, that will be used to parse
  2924. * all other nals */
  2925. s->nal_length_size = nal_len_size;
  2926. } else {
  2927. s->is_nalff = 0;
  2928. ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
  2929. if (ret < 0)
  2930. return ret;
  2931. }
  2932. return 0;
  2933. }
  2934. static av_cold int hevc_decode_init(AVCodecContext *avctx)
  2935. {
  2936. HEVCContext *s = avctx->priv_data;
  2937. int ret;
  2938. ff_init_cabac_states();
  2939. avctx->internal->allocate_progress = 1;
  2940. ret = hevc_init_context(avctx);
  2941. if (ret < 0)
  2942. return ret;
  2943. s->enable_parallel_tiles = 0;
  2944. s->picture_struct = 0;
  2945. s->eos = 1;
  2946. if(avctx->active_thread_type & FF_THREAD_SLICE)
  2947. s->threads_number = avctx->thread_count;
  2948. else
  2949. s->threads_number = 1;
  2950. if (avctx->extradata_size > 0 && avctx->extradata) {
  2951. ret = hevc_decode_extradata(s);
  2952. if (ret < 0) {
  2953. hevc_decode_free(avctx);
  2954. return ret;
  2955. }
  2956. }
  2957. if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
  2958. s->threads_type = FF_THREAD_FRAME;
  2959. else
  2960. s->threads_type = FF_THREAD_SLICE;
  2961. return 0;
  2962. }
  2963. static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
  2964. {
  2965. HEVCContext *s = avctx->priv_data;
  2966. int ret;
  2967. memset(s, 0, sizeof(*s));
  2968. ret = hevc_init_context(avctx);
  2969. if (ret < 0)
  2970. return ret;
  2971. return 0;
  2972. }
  2973. static void hevc_decode_flush(AVCodecContext *avctx)
  2974. {
  2975. HEVCContext *s = avctx->priv_data;
  2976. ff_hevc_flush_dpb(s);
  2977. s->max_ra = INT_MAX;
  2978. s->eos = 1;
  2979. }
  2980. #define OFFSET(x) offsetof(HEVCContext, x)
  2981. #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  2982. static const AVProfile profiles[] = {
  2983. { FF_PROFILE_HEVC_MAIN, "Main" },
  2984. { FF_PROFILE_HEVC_MAIN_10, "Main 10" },
  2985. { FF_PROFILE_HEVC_MAIN_STILL_PICTURE, "Main Still Picture" },
  2986. { FF_PROFILE_HEVC_REXT, "Rext" },
  2987. { FF_PROFILE_UNKNOWN },
  2988. };
  2989. static const AVOption options[] = {
  2990. { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
  2991. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
  2992. { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
  2993. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
  2994. { NULL },
  2995. };
  2996. static const AVClass hevc_decoder_class = {
  2997. .class_name = "HEVC decoder",
  2998. .item_name = av_default_item_name,
  2999. .option = options,
  3000. .version = LIBAVUTIL_VERSION_INT,
  3001. };
  3002. AVCodec ff_hevc_decoder = {
  3003. .name = "hevc",
  3004. .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
  3005. .type = AVMEDIA_TYPE_VIDEO,
  3006. .id = AV_CODEC_ID_HEVC,
  3007. .priv_data_size = sizeof(HEVCContext),
  3008. .priv_class = &hevc_decoder_class,
  3009. .init = hevc_decode_init,
  3010. .close = hevc_decode_free,
  3011. .decode = hevc_decode_frame,
  3012. .flush = hevc_decode_flush,
  3013. .update_thread_context = hevc_update_thread_context,
  3014. .init_thread_copy = hevc_init_thread_copy,
  3015. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY |
  3016. CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
  3017. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3018. };