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.

3518 lines
132KB

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