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.

3117 lines
118KB

  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 Libav.
  10. *
  11. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/display.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/md5.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/stereo3d.h"
  33. #include "bswapdsp.h"
  34. #include "bytestream.h"
  35. #include "cabac_functions.h"
  36. #include "golomb_legacy.h"
  37. #include "hevc.h"
  38. #include "hevc_data.h"
  39. #include "hevcdec.h"
  40. #include "profiles.h"
  41. const uint8_t ff_hevc_qpel_extra_before[4] = { 0, 3, 3, 3 };
  42. const uint8_t ff_hevc_qpel_extra_after[4] = { 0, 4, 4, 4 };
  43. const uint8_t ff_hevc_qpel_extra[4] = { 0, 7, 7, 7 };
  44. static const uint8_t scan_1x1[1] = { 0 };
  45. static const uint8_t horiz_scan2x2_x[4] = { 0, 1, 0, 1 };
  46. static const uint8_t horiz_scan2x2_y[4] = { 0, 0, 1, 1 };
  47. static const uint8_t horiz_scan4x4_x[16] = {
  48. 0, 1, 2, 3,
  49. 0, 1, 2, 3,
  50. 0, 1, 2, 3,
  51. 0, 1, 2, 3,
  52. };
  53. static const uint8_t horiz_scan4x4_y[16] = {
  54. 0, 0, 0, 0,
  55. 1, 1, 1, 1,
  56. 2, 2, 2, 2,
  57. 3, 3, 3, 3,
  58. };
  59. static const uint8_t horiz_scan8x8_inv[8][8] = {
  60. { 0, 1, 2, 3, 16, 17, 18, 19, },
  61. { 4, 5, 6, 7, 20, 21, 22, 23, },
  62. { 8, 9, 10, 11, 24, 25, 26, 27, },
  63. { 12, 13, 14, 15, 28, 29, 30, 31, },
  64. { 32, 33, 34, 35, 48, 49, 50, 51, },
  65. { 36, 37, 38, 39, 52, 53, 54, 55, },
  66. { 40, 41, 42, 43, 56, 57, 58, 59, },
  67. { 44, 45, 46, 47, 60, 61, 62, 63, },
  68. };
  69. static const uint8_t diag_scan2x2_x[4] = { 0, 0, 1, 1 };
  70. static const uint8_t diag_scan2x2_y[4] = { 0, 1, 0, 1 };
  71. static const uint8_t diag_scan2x2_inv[2][2] = {
  72. { 0, 2, },
  73. { 1, 3, },
  74. };
  75. static const uint8_t diag_scan4x4_inv[4][4] = {
  76. { 0, 2, 5, 9, },
  77. { 1, 4, 8, 12, },
  78. { 3, 7, 11, 14, },
  79. { 6, 10, 13, 15, },
  80. };
  81. static const uint8_t diag_scan8x8_inv[8][8] = {
  82. { 0, 2, 5, 9, 14, 20, 27, 35, },
  83. { 1, 4, 8, 13, 19, 26, 34, 42, },
  84. { 3, 7, 12, 18, 25, 33, 41, 48, },
  85. { 6, 11, 17, 24, 32, 40, 47, 53, },
  86. { 10, 16, 23, 31, 39, 46, 52, 57, },
  87. { 15, 22, 30, 38, 45, 51, 56, 60, },
  88. { 21, 29, 37, 44, 50, 55, 59, 62, },
  89. { 28, 36, 43, 49, 54, 58, 61, 63, },
  90. };
  91. /**
  92. * NOTE: Each function hls_foo correspond to the function foo in the
  93. * specification (HLS stands for High Level Syntax).
  94. */
  95. /**
  96. * Section 5.7
  97. */
  98. /* free everything allocated by pic_arrays_init() */
  99. static void pic_arrays_free(HEVCContext *s)
  100. {
  101. av_freep(&s->sao);
  102. av_freep(&s->deblock);
  103. av_freep(&s->skip_flag);
  104. av_freep(&s->tab_ct_depth);
  105. av_freep(&s->tab_ipm);
  106. av_freep(&s->cbf_luma);
  107. av_freep(&s->is_pcm);
  108. av_freep(&s->qp_y_tab);
  109. av_freep(&s->tab_slice_address);
  110. av_freep(&s->filter_slice_edges);
  111. av_freep(&s->horizontal_bs);
  112. av_freep(&s->vertical_bs);
  113. av_buffer_pool_uninit(&s->tab_mvf_pool);
  114. av_buffer_pool_uninit(&s->rpl_tab_pool);
  115. }
  116. /* allocate arrays that depend on frame dimensions */
  117. static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
  118. {
  119. int log2_min_cb_size = sps->log2_min_cb_size;
  120. int width = sps->width;
  121. int height = sps->height;
  122. int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
  123. ((height >> log2_min_cb_size) + 1);
  124. int ctb_count = sps->ctb_width * sps->ctb_height;
  125. int min_pu_size = sps->min_pu_width * sps->min_pu_height;
  126. s->bs_width = width >> 3;
  127. s->bs_height = height >> 3;
  128. s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
  129. s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
  130. if (!s->sao || !s->deblock)
  131. goto fail;
  132. s->skip_flag = av_malloc(pic_size_in_ctb);
  133. s->tab_ct_depth = av_malloc(sps->min_cb_height * sps->min_cb_width);
  134. if (!s->skip_flag || !s->tab_ct_depth)
  135. goto fail;
  136. s->cbf_luma = av_malloc(sps->min_tb_width * sps->min_tb_height);
  137. s->tab_ipm = av_mallocz(min_pu_size);
  138. s->is_pcm = av_malloc(min_pu_size);
  139. if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
  140. goto fail;
  141. s->filter_slice_edges = av_malloc(ctb_count);
  142. s->tab_slice_address = av_malloc(pic_size_in_ctb *
  143. sizeof(*s->tab_slice_address));
  144. s->qp_y_tab = av_malloc(pic_size_in_ctb *
  145. sizeof(*s->qp_y_tab));
  146. if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
  147. goto fail;
  148. s->horizontal_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
  149. s->vertical_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
  150. if (!s->horizontal_bs || !s->vertical_bs)
  151. goto fail;
  152. s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
  153. av_buffer_alloc);
  154. s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
  155. av_buffer_allocz);
  156. if (!s->tab_mvf_pool || !s->rpl_tab_pool)
  157. goto fail;
  158. return 0;
  159. fail:
  160. pic_arrays_free(s);
  161. return AVERROR(ENOMEM);
  162. }
  163. static void pred_weight_table(HEVCContext *s, GetBitContext *gb)
  164. {
  165. int i = 0;
  166. int j = 0;
  167. uint8_t luma_weight_l0_flag[16];
  168. uint8_t chroma_weight_l0_flag[16];
  169. uint8_t luma_weight_l1_flag[16];
  170. uint8_t chroma_weight_l1_flag[16];
  171. s->sh.luma_log2_weight_denom = av_clip(get_ue_golomb_long(gb), 0, 7);
  172. if (s->ps.sps->chroma_format_idc != 0) {
  173. int delta = get_se_golomb(gb);
  174. s->sh.chroma_log2_weight_denom = av_clip(s->sh.luma_log2_weight_denom + delta, 0, 7);
  175. }
  176. for (i = 0; i < s->sh.nb_refs[L0]; i++) {
  177. luma_weight_l0_flag[i] = get_bits1(gb);
  178. if (!luma_weight_l0_flag[i]) {
  179. s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
  180. s->sh.luma_offset_l0[i] = 0;
  181. }
  182. }
  183. if (s->ps.sps->chroma_format_idc != 0) { // FIXME: invert "if" and "for"
  184. for (i = 0; i < s->sh.nb_refs[L0]; i++)
  185. chroma_weight_l0_flag[i] = get_bits1(gb);
  186. } else {
  187. for (i = 0; i < s->sh.nb_refs[L0]; i++)
  188. chroma_weight_l0_flag[i] = 0;
  189. }
  190. for (i = 0; i < s->sh.nb_refs[L0]; i++) {
  191. if (luma_weight_l0_flag[i]) {
  192. int delta_luma_weight_l0 = get_se_golomb(gb);
  193. s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
  194. s->sh.luma_offset_l0[i] = get_se_golomb(gb);
  195. }
  196. if (chroma_weight_l0_flag[i]) {
  197. for (j = 0; j < 2; j++) {
  198. int delta_chroma_weight_l0 = get_se_golomb(gb);
  199. int delta_chroma_offset_l0 = get_se_golomb(gb);
  200. s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
  201. s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
  202. >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
  203. }
  204. } else {
  205. s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
  206. s->sh.chroma_offset_l0[i][0] = 0;
  207. s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
  208. s->sh.chroma_offset_l0[i][1] = 0;
  209. }
  210. }
  211. if (s->sh.slice_type == HEVC_SLICE_B) {
  212. for (i = 0; i < s->sh.nb_refs[L1]; i++) {
  213. luma_weight_l1_flag[i] = get_bits1(gb);
  214. if (!luma_weight_l1_flag[i]) {
  215. s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
  216. s->sh.luma_offset_l1[i] = 0;
  217. }
  218. }
  219. if (s->ps.sps->chroma_format_idc != 0) {
  220. for (i = 0; i < s->sh.nb_refs[L1]; i++)
  221. chroma_weight_l1_flag[i] = get_bits1(gb);
  222. } else {
  223. for (i = 0; i < s->sh.nb_refs[L1]; i++)
  224. chroma_weight_l1_flag[i] = 0;
  225. }
  226. for (i = 0; i < s->sh.nb_refs[L1]; i++) {
  227. if (luma_weight_l1_flag[i]) {
  228. int delta_luma_weight_l1 = get_se_golomb(gb);
  229. s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
  230. s->sh.luma_offset_l1[i] = get_se_golomb(gb);
  231. }
  232. if (chroma_weight_l1_flag[i]) {
  233. for (j = 0; j < 2; j++) {
  234. int delta_chroma_weight_l1 = get_se_golomb(gb);
  235. int delta_chroma_offset_l1 = get_se_golomb(gb);
  236. s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
  237. s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
  238. >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
  239. }
  240. } else {
  241. s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
  242. s->sh.chroma_offset_l1[i][0] = 0;
  243. s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
  244. s->sh.chroma_offset_l1[i][1] = 0;
  245. }
  246. }
  247. }
  248. }
  249. static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
  250. {
  251. const HEVCSPS *sps = s->ps.sps;
  252. int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
  253. int prev_delta_msb = 0;
  254. unsigned int nb_sps = 0, nb_sh;
  255. int i;
  256. rps->nb_refs = 0;
  257. if (!sps->long_term_ref_pics_present_flag)
  258. return 0;
  259. if (sps->num_long_term_ref_pics_sps > 0)
  260. nb_sps = get_ue_golomb_long(gb);
  261. nb_sh = get_ue_golomb_long(gb);
  262. if (nb_sh + nb_sps > FF_ARRAY_ELEMS(rps->poc))
  263. return AVERROR_INVALIDDATA;
  264. rps->nb_refs = nb_sh + nb_sps;
  265. for (i = 0; i < rps->nb_refs; i++) {
  266. uint8_t delta_poc_msb_present;
  267. if (i < nb_sps) {
  268. uint8_t lt_idx_sps = 0;
  269. if (sps->num_long_term_ref_pics_sps > 1)
  270. lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
  271. rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
  272. rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
  273. } else {
  274. rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
  275. rps->used[i] = get_bits1(gb);
  276. }
  277. delta_poc_msb_present = get_bits1(gb);
  278. if (delta_poc_msb_present) {
  279. int delta = get_ue_golomb_long(gb);
  280. if (i && i != nb_sps)
  281. delta += prev_delta_msb;
  282. rps->poc[i] += s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
  283. prev_delta_msb = delta;
  284. }
  285. }
  286. return 0;
  287. }
  288. static void export_stream_params(AVCodecContext *avctx, const HEVCParamSets *ps,
  289. const HEVCSPS *sps)
  290. {
  291. const HEVCVPS *vps = (const HEVCVPS*)ps->vps_list[sps->vps_id]->data;
  292. const HEVCWindow *ow = &sps->output_window;
  293. unsigned int num = 0, den = 0;
  294. avctx->pix_fmt = sps->pix_fmt;
  295. avctx->coded_width = sps->width;
  296. avctx->coded_height = sps->height;
  297. avctx->width = sps->width - ow->left_offset - ow->right_offset;
  298. avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
  299. avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
  300. avctx->profile = sps->ptl.general_ptl.profile_idc;
  301. avctx->level = sps->ptl.general_ptl.level_idc;
  302. ff_set_sar(avctx, sps->vui.sar);
  303. if (sps->vui.video_signal_type_present_flag)
  304. avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
  305. : AVCOL_RANGE_MPEG;
  306. else
  307. avctx->color_range = AVCOL_RANGE_MPEG;
  308. if (sps->vui.colour_description_present_flag) {
  309. avctx->color_primaries = sps->vui.colour_primaries;
  310. avctx->color_trc = sps->vui.transfer_characteristic;
  311. avctx->colorspace = sps->vui.matrix_coeffs;
  312. } else {
  313. avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
  314. avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
  315. avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
  316. }
  317. if (vps->vps_timing_info_present_flag) {
  318. num = vps->vps_num_units_in_tick;
  319. den = vps->vps_time_scale;
  320. } else if (sps->vui.vui_timing_info_present_flag) {
  321. num = sps->vui.vui_num_units_in_tick;
  322. den = sps->vui.vui_time_scale;
  323. }
  324. if (num != 0 && den != 0)
  325. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  326. num, den, 1 << 30);
  327. }
  328. static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
  329. {
  330. #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
  331. CONFIG_HEVC_VAAPI_HWACCEL + CONFIG_HEVC_VDPAU_HWACCEL + \
  332. CONFIG_HEVC_CUVID_HWACCEL)
  333. enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
  334. if (sps->pix_fmt == AV_PIX_FMT_YUV420P || sps->pix_fmt == AV_PIX_FMT_YUVJ420P ||
  335. sps->pix_fmt == AV_PIX_FMT_YUV420P10) {
  336. #if CONFIG_HEVC_D3D11VA_HWACCEL
  337. *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
  338. *fmt++ = AV_PIX_FMT_D3D11;
  339. #endif
  340. #if CONFIG_HEVC_DXVA2_HWACCEL
  341. *fmt++ = AV_PIX_FMT_DXVA2_VLD;
  342. #endif
  343. #if CONFIG_HEVC_VAAPI_HWACCEL
  344. *fmt++ = AV_PIX_FMT_VAAPI;
  345. #endif
  346. #if CONFIG_HEVC_CUVID_HWACCEL && HAVE_CUVIDDECODECREATEINFO_BITDEPTHMINUS8
  347. *fmt++ = AV_PIX_FMT_CUDA;
  348. #endif
  349. }
  350. if (sps->pix_fmt == AV_PIX_FMT_YUV420P || sps->pix_fmt == AV_PIX_FMT_YUVJ420P) {
  351. #if CONFIG_HEVC_CUVID_HWACCEL && !HAVE_CUVIDDECODECREATEINFO_BITDEPTHMINUS8
  352. *fmt++ = AV_PIX_FMT_CUDA;
  353. #endif
  354. #if CONFIG_HEVC_VDPAU_HWACCEL
  355. *fmt++ = AV_PIX_FMT_VDPAU;
  356. #endif
  357. }
  358. *fmt++ = sps->pix_fmt;
  359. *fmt = AV_PIX_FMT_NONE;
  360. return ff_get_format(s->avctx, pix_fmts);
  361. }
  362. static int set_sps(HEVCContext *s, const HEVCSPS *sps,
  363. enum AVPixelFormat pix_fmt)
  364. {
  365. int ret;
  366. pic_arrays_free(s);
  367. s->ps.sps = NULL;
  368. s->ps.vps = NULL;
  369. if (!sps)
  370. return 0;
  371. ret = pic_arrays_init(s, sps);
  372. if (ret < 0)
  373. goto fail;
  374. export_stream_params(s->avctx, &s->ps, sps);
  375. s->avctx->pix_fmt = pix_fmt;
  376. ff_hevc_pred_init(&s->hpc, sps->bit_depth);
  377. ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
  378. ff_videodsp_init (&s->vdsp, sps->bit_depth);
  379. if (sps->sao_enabled && !s->avctx->hwaccel) {
  380. av_frame_unref(s->tmp_frame);
  381. ret = ff_get_buffer(s->avctx, s->tmp_frame, AV_GET_BUFFER_FLAG_REF);
  382. if (ret < 0)
  383. goto fail;
  384. s->frame = s->tmp_frame;
  385. }
  386. s->ps.sps = sps;
  387. s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
  388. return 0;
  389. fail:
  390. pic_arrays_free(s);
  391. s->ps.sps = NULL;
  392. return ret;
  393. }
  394. static int hls_slice_header(HEVCContext *s)
  395. {
  396. GetBitContext *gb = &s->HEVClc.gb;
  397. SliceHeader *sh = &s->sh;
  398. int i, ret;
  399. // Coded parameters
  400. sh->first_slice_in_pic_flag = get_bits1(gb);
  401. if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
  402. s->seq_decode = (s->seq_decode + 1) & 0xff;
  403. s->max_ra = INT_MAX;
  404. if (IS_IDR(s))
  405. ff_hevc_clear_refs(s);
  406. }
  407. if (IS_IRAP(s))
  408. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  409. sh->pps_id = get_ue_golomb_long(gb);
  410. if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
  411. av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  412. return AVERROR_INVALIDDATA;
  413. }
  414. if (!sh->first_slice_in_pic_flag &&
  415. s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
  416. av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
  417. return AVERROR_INVALIDDATA;
  418. }
  419. s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
  420. if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
  421. const HEVCSPS *sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
  422. enum AVPixelFormat pix_fmt;
  423. ff_hevc_clear_refs(s);
  424. ret = set_sps(s, sps, sps->pix_fmt);
  425. if (ret < 0)
  426. return ret;
  427. pix_fmt = get_format(s, sps);
  428. if (pix_fmt < 0)
  429. return pix_fmt;
  430. s->avctx->pix_fmt = pix_fmt;
  431. s->seq_decode = (s->seq_decode + 1) & 0xff;
  432. s->max_ra = INT_MAX;
  433. }
  434. sh->dependent_slice_segment_flag = 0;
  435. if (!sh->first_slice_in_pic_flag) {
  436. int slice_address_length;
  437. if (s->ps.pps->dependent_slice_segments_enabled_flag)
  438. sh->dependent_slice_segment_flag = get_bits1(gb);
  439. slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
  440. s->ps.sps->ctb_height);
  441. sh->slice_segment_addr = slice_address_length ? get_bits(gb, slice_address_length) : 0;
  442. if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
  443. av_log(s->avctx, AV_LOG_ERROR,
  444. "Invalid slice segment address: %u.\n",
  445. sh->slice_segment_addr);
  446. return AVERROR_INVALIDDATA;
  447. }
  448. if (!sh->dependent_slice_segment_flag) {
  449. sh->slice_addr = sh->slice_segment_addr;
  450. s->slice_idx++;
  451. }
  452. } else {
  453. sh->slice_segment_addr = sh->slice_addr = 0;
  454. s->slice_idx = 0;
  455. s->slice_initialized = 0;
  456. }
  457. if (!sh->dependent_slice_segment_flag) {
  458. s->slice_initialized = 0;
  459. for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
  460. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  461. sh->slice_type = get_ue_golomb_long(gb);
  462. if (!(sh->slice_type == HEVC_SLICE_I ||
  463. sh->slice_type == HEVC_SLICE_P ||
  464. sh->slice_type == HEVC_SLICE_B)) {
  465. av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  466. sh->slice_type);
  467. return AVERROR_INVALIDDATA;
  468. }
  469. if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I) {
  470. av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
  471. return AVERROR_INVALIDDATA;
  472. }
  473. // when flag is not present, picture is inferred to be output
  474. sh->pic_output_flag = 1;
  475. if (s->ps.pps->output_flag_present_flag)
  476. sh->pic_output_flag = get_bits1(gb);
  477. if (s->ps.sps->separate_colour_plane_flag)
  478. sh->colour_plane_id = get_bits(gb, 2);
  479. if (!IS_IDR(s)) {
  480. int poc, pos;
  481. sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
  482. poc = ff_hevc_compute_poc(s, sh->pic_order_cnt_lsb);
  483. if (!sh->first_slice_in_pic_flag && poc != s->poc) {
  484. av_log(s->avctx, AV_LOG_WARNING,
  485. "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
  486. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  487. return AVERROR_INVALIDDATA;
  488. poc = s->poc;
  489. }
  490. s->poc = poc;
  491. sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
  492. pos = get_bits_left(gb);
  493. if (!sh->short_term_ref_pic_set_sps_flag) {
  494. ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
  495. if (ret < 0)
  496. return ret;
  497. sh->short_term_rps = &sh->slice_rps;
  498. } else {
  499. int numbits, rps_idx;
  500. if (!s->ps.sps->nb_st_rps) {
  501. av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
  502. return AVERROR_INVALIDDATA;
  503. }
  504. numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
  505. rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
  506. sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
  507. }
  508. sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
  509. pos = get_bits_left(gb);
  510. ret = decode_lt_rps(s, &sh->long_term_rps, gb);
  511. if (ret < 0) {
  512. av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
  513. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  514. return AVERROR_INVALIDDATA;
  515. }
  516. sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
  517. if (s->ps.sps->sps_temporal_mvp_enabled_flag)
  518. sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
  519. else
  520. sh->slice_temporal_mvp_enabled_flag = 0;
  521. } else {
  522. s->sh.short_term_rps = NULL;
  523. s->poc = 0;
  524. }
  525. /* 8.3.1 */
  526. if (s->temporal_id == 0 &&
  527. s->nal_unit_type != HEVC_NAL_TRAIL_N &&
  528. s->nal_unit_type != HEVC_NAL_TSA_N &&
  529. s->nal_unit_type != HEVC_NAL_STSA_N &&
  530. s->nal_unit_type != HEVC_NAL_RADL_N &&
  531. s->nal_unit_type != HEVC_NAL_RADL_R &&
  532. s->nal_unit_type != HEVC_NAL_RASL_N &&
  533. s->nal_unit_type != HEVC_NAL_RASL_R)
  534. s->pocTid0 = s->poc;
  535. if (s->ps.sps->sao_enabled) {
  536. sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
  537. sh->slice_sample_adaptive_offset_flag[1] =
  538. sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
  539. } else {
  540. sh->slice_sample_adaptive_offset_flag[0] = 0;
  541. sh->slice_sample_adaptive_offset_flag[1] = 0;
  542. sh->slice_sample_adaptive_offset_flag[2] = 0;
  543. }
  544. sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
  545. if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
  546. int nb_refs;
  547. sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
  548. if (sh->slice_type == HEVC_SLICE_B)
  549. sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
  550. if (get_bits1(gb)) { // num_ref_idx_active_override_flag
  551. sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
  552. if (sh->slice_type == HEVC_SLICE_B)
  553. sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
  554. }
  555. if (sh->nb_refs[L0] > HEVC_MAX_REFS || sh->nb_refs[L1] > HEVC_MAX_REFS) {
  556. av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
  557. sh->nb_refs[L0], sh->nb_refs[L1]);
  558. return AVERROR_INVALIDDATA;
  559. }
  560. sh->rpl_modification_flag[0] = 0;
  561. sh->rpl_modification_flag[1] = 0;
  562. nb_refs = ff_hevc_frame_nb_refs(s);
  563. if (!nb_refs) {
  564. av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
  565. return AVERROR_INVALIDDATA;
  566. }
  567. if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
  568. sh->rpl_modification_flag[0] = get_bits1(gb);
  569. if (sh->rpl_modification_flag[0]) {
  570. for (i = 0; i < sh->nb_refs[L0]; i++)
  571. sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
  572. }
  573. if (sh->slice_type == HEVC_SLICE_B) {
  574. sh->rpl_modification_flag[1] = get_bits1(gb);
  575. if (sh->rpl_modification_flag[1] == 1)
  576. for (i = 0; i < sh->nb_refs[L1]; i++)
  577. sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
  578. }
  579. }
  580. if (sh->slice_type == HEVC_SLICE_B)
  581. sh->mvd_l1_zero_flag = get_bits1(gb);
  582. if (s->ps.pps->cabac_init_present_flag)
  583. sh->cabac_init_flag = get_bits1(gb);
  584. else
  585. sh->cabac_init_flag = 0;
  586. sh->collocated_ref_idx = 0;
  587. if (sh->slice_temporal_mvp_enabled_flag) {
  588. sh->collocated_list = L0;
  589. if (sh->slice_type == HEVC_SLICE_B)
  590. sh->collocated_list = !get_bits1(gb);
  591. if (sh->nb_refs[sh->collocated_list] > 1) {
  592. sh->collocated_ref_idx = get_ue_golomb_long(gb);
  593. if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
  594. av_log(s->avctx, AV_LOG_ERROR,
  595. "Invalid collocated_ref_idx: %d.\n",
  596. sh->collocated_ref_idx);
  597. return AVERROR_INVALIDDATA;
  598. }
  599. }
  600. }
  601. if ((s->ps.pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
  602. (s->ps.pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
  603. pred_weight_table(s, gb);
  604. }
  605. sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
  606. if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
  607. av_log(s->avctx, AV_LOG_ERROR,
  608. "Invalid number of merging MVP candidates: %d.\n",
  609. sh->max_num_merge_cand);
  610. return AVERROR_INVALIDDATA;
  611. }
  612. }
  613. sh->slice_qp_delta = get_se_golomb(gb);
  614. if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
  615. sh->slice_cb_qp_offset = get_se_golomb(gb);
  616. sh->slice_cr_qp_offset = get_se_golomb(gb);
  617. } else {
  618. sh->slice_cb_qp_offset = 0;
  619. sh->slice_cr_qp_offset = 0;
  620. }
  621. if (s->ps.pps->deblocking_filter_control_present_flag) {
  622. int deblocking_filter_override_flag = 0;
  623. if (s->ps.pps->deblocking_filter_override_enabled_flag)
  624. deblocking_filter_override_flag = get_bits1(gb);
  625. if (deblocking_filter_override_flag) {
  626. sh->disable_deblocking_filter_flag = get_bits1(gb);
  627. if (!sh->disable_deblocking_filter_flag) {
  628. sh->beta_offset = get_se_golomb(gb) * 2;
  629. sh->tc_offset = get_se_golomb(gb) * 2;
  630. }
  631. } else {
  632. sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
  633. sh->beta_offset = s->ps.pps->beta_offset;
  634. sh->tc_offset = s->ps.pps->tc_offset;
  635. }
  636. } else {
  637. sh->disable_deblocking_filter_flag = 0;
  638. sh->beta_offset = 0;
  639. sh->tc_offset = 0;
  640. }
  641. if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
  642. (sh->slice_sample_adaptive_offset_flag[0] ||
  643. sh->slice_sample_adaptive_offset_flag[1] ||
  644. !sh->disable_deblocking_filter_flag)) {
  645. sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
  646. } else {
  647. sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
  648. }
  649. } else if (!s->slice_initialized) {
  650. av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
  651. return AVERROR_INVALIDDATA;
  652. }
  653. sh->num_entry_point_offsets = 0;
  654. if (s->ps.pps->tiles_enabled_flag || s->ps.pps->entropy_coding_sync_enabled_flag) {
  655. sh->num_entry_point_offsets = get_ue_golomb_long(gb);
  656. if (sh->num_entry_point_offsets > 0) {
  657. int offset_len = get_ue_golomb_long(gb) + 1;
  658. for (i = 0; i < sh->num_entry_point_offsets; i++)
  659. skip_bits(gb, offset_len);
  660. }
  661. }
  662. if (s->ps.pps->slice_header_extension_present_flag) {
  663. unsigned int length = get_ue_golomb_long(gb);
  664. for (i = 0; i < length; i++)
  665. skip_bits(gb, 8); // slice_header_extension_data_byte
  666. }
  667. // Inferred parameters
  668. sh->slice_qp = 26 + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
  669. if (sh->slice_qp > 51 ||
  670. sh->slice_qp < -s->ps.sps->qp_bd_offset) {
  671. av_log(s->avctx, AV_LOG_ERROR,
  672. "The slice_qp %d is outside the valid range "
  673. "[%d, 51].\n",
  674. sh->slice_qp,
  675. -s->ps.sps->qp_bd_offset);
  676. return AVERROR_INVALIDDATA;
  677. }
  678. sh->slice_ctb_addr_rs = sh->slice_segment_addr;
  679. if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
  680. av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
  681. return AVERROR_INVALIDDATA;
  682. }
  683. s->HEVClc.first_qp_group = !s->sh.dependent_slice_segment_flag;
  684. if (!s->ps.pps->cu_qp_delta_enabled_flag)
  685. s->HEVClc.qp_y = FFUMOD(s->sh.slice_qp + 52 + 2 * s->ps.sps->qp_bd_offset,
  686. 52 + s->ps.sps->qp_bd_offset) - s->ps.sps->qp_bd_offset;
  687. s->slice_initialized = 1;
  688. return 0;
  689. }
  690. #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
  691. #define SET_SAO(elem, value) \
  692. do { \
  693. if (!sao_merge_up_flag && !sao_merge_left_flag) \
  694. sao->elem = value; \
  695. else if (sao_merge_left_flag) \
  696. sao->elem = CTB(s->sao, rx-1, ry).elem; \
  697. else if (sao_merge_up_flag) \
  698. sao->elem = CTB(s->sao, rx, ry-1).elem; \
  699. else \
  700. sao->elem = 0; \
  701. } while (0)
  702. static void hls_sao_param(HEVCContext *s, int rx, int ry)
  703. {
  704. HEVCLocalContext *lc = &s->HEVClc;
  705. int sao_merge_left_flag = 0;
  706. int sao_merge_up_flag = 0;
  707. int shift = s->ps.sps->bit_depth - FFMIN(s->ps.sps->bit_depth, 10);
  708. SAOParams *sao = &CTB(s->sao, rx, ry);
  709. int c_idx, i;
  710. if (s->sh.slice_sample_adaptive_offset_flag[0] ||
  711. s->sh.slice_sample_adaptive_offset_flag[1]) {
  712. if (rx > 0) {
  713. if (lc->ctb_left_flag)
  714. sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
  715. }
  716. if (ry > 0 && !sao_merge_left_flag) {
  717. if (lc->ctb_up_flag)
  718. sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
  719. }
  720. }
  721. for (c_idx = 0; c_idx < 3; c_idx++) {
  722. if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
  723. sao->type_idx[c_idx] = SAO_NOT_APPLIED;
  724. continue;
  725. }
  726. if (c_idx == 2) {
  727. sao->type_idx[2] = sao->type_idx[1];
  728. sao->eo_class[2] = sao->eo_class[1];
  729. } else {
  730. SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
  731. }
  732. if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
  733. continue;
  734. for (i = 0; i < 4; i++)
  735. SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
  736. if (sao->type_idx[c_idx] == SAO_BAND) {
  737. for (i = 0; i < 4; i++) {
  738. if (sao->offset_abs[c_idx][i]) {
  739. SET_SAO(offset_sign[c_idx][i],
  740. ff_hevc_sao_offset_sign_decode(s));
  741. } else {
  742. sao->offset_sign[c_idx][i] = 0;
  743. }
  744. }
  745. SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
  746. } else if (c_idx != 2) {
  747. SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
  748. }
  749. // Inferred parameters
  750. sao->offset_val[c_idx][0] = 0;
  751. for (i = 0; i < 4; i++) {
  752. sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i] << shift;
  753. if (sao->type_idx[c_idx] == SAO_EDGE) {
  754. if (i > 1)
  755. sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
  756. } else if (sao->offset_sign[c_idx][i]) {
  757. sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
  758. }
  759. }
  760. }
  761. }
  762. #undef SET_SAO
  763. #undef CTB
  764. static void hls_residual_coding(HEVCContext *s, int x0, int y0,
  765. int log2_trafo_size, enum ScanType scan_idx,
  766. int c_idx)
  767. {
  768. #define GET_COORD(offset, n) \
  769. do { \
  770. x_c = (scan_x_cg[offset >> 4] << 2) + scan_x_off[n]; \
  771. y_c = (scan_y_cg[offset >> 4] << 2) + scan_y_off[n]; \
  772. } while (0)
  773. HEVCLocalContext *lc = &s->HEVClc;
  774. int transform_skip_flag = 0;
  775. int last_significant_coeff_x, last_significant_coeff_y;
  776. int last_scan_pos;
  777. int n_end;
  778. int num_coeff = 0;
  779. int greater1_ctx = 1;
  780. int num_last_subset;
  781. int x_cg_last_sig, y_cg_last_sig;
  782. const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
  783. ptrdiff_t stride = s->frame->linesize[c_idx];
  784. int hshift = s->ps.sps->hshift[c_idx];
  785. int vshift = s->ps.sps->vshift[c_idx];
  786. uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
  787. ((x0 >> hshift) << s->ps.sps->pixel_shift)];
  788. LOCAL_ALIGNED_32(int16_t, coeffs, [MAX_TB_SIZE * MAX_TB_SIZE]);
  789. LOCAL_ALIGNED_8(uint8_t, significant_coeff_group_flag, [8], [8]);
  790. int trafo_size = 1 << log2_trafo_size;
  791. int i, qp, shift, add, scale, scale_m;
  792. static const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
  793. const uint8_t *scale_matrix;
  794. uint8_t dc_scale;
  795. memset(coeffs, 0, sizeof(int16_t) * MAX_TB_SIZE * MAX_TB_SIZE);
  796. memset(significant_coeff_group_flag, 0, sizeof(uint8_t) * 8 * 8);
  797. // Derive QP for dequant
  798. if (!lc->cu.cu_transquant_bypass_flag) {
  799. static const int qp_c[] = {
  800. 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37
  801. };
  802. static const uint8_t rem6[51 + 2 * 6 + 1] = {
  803. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
  804. 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
  805. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  806. };
  807. static const uint8_t div6[51 + 2 * 6 + 1] = {
  808. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
  809. 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
  810. 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
  811. };
  812. int qp_y = lc->qp_y;
  813. if (c_idx == 0) {
  814. qp = qp_y + s->ps.sps->qp_bd_offset;
  815. } else {
  816. int qp_i, offset;
  817. if (c_idx == 1)
  818. offset = s->ps.pps->cb_qp_offset + s->sh.slice_cb_qp_offset;
  819. else
  820. offset = s->ps.pps->cr_qp_offset + s->sh.slice_cr_qp_offset;
  821. qp_i = av_clip(qp_y + offset, -s->ps.sps->qp_bd_offset, 57);
  822. if (qp_i < 30)
  823. qp = qp_i;
  824. else if (qp_i > 43)
  825. qp = qp_i - 6;
  826. else
  827. qp = qp_c[qp_i - 30];
  828. qp += s->ps.sps->qp_bd_offset;
  829. }
  830. shift = s->ps.sps->bit_depth + log2_trafo_size - 5;
  831. add = 1 << (shift - 1);
  832. scale = level_scale[rem6[qp]] << (div6[qp]);
  833. scale_m = 16; // default when no custom scaling lists.
  834. dc_scale = 16;
  835. if (s->ps.sps->scaling_list_enable_flag) {
  836. const ScalingList *sl = s->ps.pps->scaling_list_data_present_flag ?
  837. &s->ps.pps->scaling_list : &s->ps.sps->scaling_list;
  838. int matrix_id = lc->cu.pred_mode != MODE_INTRA;
  839. if (log2_trafo_size != 5)
  840. matrix_id = 3 * matrix_id + c_idx;
  841. scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
  842. if (log2_trafo_size >= 4)
  843. dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
  844. }
  845. }
  846. if (s->ps.pps->transform_skip_enabled_flag &&
  847. !lc->cu.cu_transquant_bypass_flag &&
  848. log2_trafo_size == 2) {
  849. transform_skip_flag = ff_hevc_transform_skip_flag_decode(s, c_idx);
  850. }
  851. last_significant_coeff_x =
  852. ff_hevc_last_significant_coeff_x_prefix_decode(s, c_idx, log2_trafo_size);
  853. last_significant_coeff_y =
  854. ff_hevc_last_significant_coeff_y_prefix_decode(s, c_idx, log2_trafo_size);
  855. if (last_significant_coeff_x > 3) {
  856. int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_x);
  857. last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
  858. (2 + (last_significant_coeff_x & 1)) +
  859. suffix;
  860. }
  861. if (last_significant_coeff_y > 3) {
  862. int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_y);
  863. last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
  864. (2 + (last_significant_coeff_y & 1)) +
  865. suffix;
  866. }
  867. if (scan_idx == SCAN_VERT)
  868. FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
  869. x_cg_last_sig = last_significant_coeff_x >> 2;
  870. y_cg_last_sig = last_significant_coeff_y >> 2;
  871. switch (scan_idx) {
  872. case SCAN_DIAG: {
  873. int last_x_c = last_significant_coeff_x & 3;
  874. int last_y_c = last_significant_coeff_y & 3;
  875. scan_x_off = ff_hevc_diag_scan4x4_x;
  876. scan_y_off = ff_hevc_diag_scan4x4_y;
  877. num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
  878. if (trafo_size == 4) {
  879. scan_x_cg = scan_1x1;
  880. scan_y_cg = scan_1x1;
  881. } else if (trafo_size == 8) {
  882. num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
  883. scan_x_cg = diag_scan2x2_x;
  884. scan_y_cg = diag_scan2x2_y;
  885. } else if (trafo_size == 16) {
  886. num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
  887. scan_x_cg = ff_hevc_diag_scan4x4_x;
  888. scan_y_cg = ff_hevc_diag_scan4x4_y;
  889. } else { // trafo_size == 32
  890. num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
  891. scan_x_cg = ff_hevc_diag_scan8x8_x;
  892. scan_y_cg = ff_hevc_diag_scan8x8_y;
  893. }
  894. break;
  895. }
  896. case SCAN_HORIZ:
  897. scan_x_cg = horiz_scan2x2_x;
  898. scan_y_cg = horiz_scan2x2_y;
  899. scan_x_off = horiz_scan4x4_x;
  900. scan_y_off = horiz_scan4x4_y;
  901. num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
  902. break;
  903. default: //SCAN_VERT
  904. scan_x_cg = horiz_scan2x2_y;
  905. scan_y_cg = horiz_scan2x2_x;
  906. scan_x_off = horiz_scan4x4_y;
  907. scan_y_off = horiz_scan4x4_x;
  908. num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
  909. break;
  910. }
  911. num_coeff++;
  912. num_last_subset = (num_coeff - 1) >> 4;
  913. for (i = num_last_subset; i >= 0; i--) {
  914. int n, m;
  915. int x_cg, y_cg, x_c, y_c;
  916. int implicit_non_zero_coeff = 0;
  917. int64_t trans_coeff_level;
  918. int prev_sig = 0;
  919. int offset = i << 4;
  920. uint8_t significant_coeff_flag_idx[16];
  921. uint8_t nb_significant_coeff_flag = 0;
  922. x_cg = scan_x_cg[i];
  923. y_cg = scan_y_cg[i];
  924. if (i < num_last_subset && i > 0) {
  925. int ctx_cg = 0;
  926. if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
  927. ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
  928. if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
  929. ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
  930. significant_coeff_group_flag[x_cg][y_cg] =
  931. ff_hevc_significant_coeff_group_flag_decode(s, c_idx, ctx_cg);
  932. implicit_non_zero_coeff = 1;
  933. } else {
  934. significant_coeff_group_flag[x_cg][y_cg] =
  935. ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
  936. (x_cg == 0 && y_cg == 0));
  937. }
  938. last_scan_pos = num_coeff - offset - 1;
  939. if (i == num_last_subset) {
  940. n_end = last_scan_pos - 1;
  941. significant_coeff_flag_idx[0] = last_scan_pos;
  942. nb_significant_coeff_flag = 1;
  943. } else {
  944. n_end = 15;
  945. }
  946. if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
  947. prev_sig = significant_coeff_group_flag[x_cg + 1][y_cg];
  948. if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
  949. prev_sig += significant_coeff_group_flag[x_cg][y_cg + 1] << 1;
  950. for (n = n_end; n >= 0; n--) {
  951. GET_COORD(offset, n);
  952. if (significant_coeff_group_flag[x_cg][y_cg] &&
  953. (n > 0 || implicit_non_zero_coeff == 0)) {
  954. if (ff_hevc_significant_coeff_flag_decode(s, c_idx, x_c, y_c,
  955. log2_trafo_size,
  956. scan_idx,
  957. prev_sig) == 1) {
  958. significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
  959. nb_significant_coeff_flag++;
  960. implicit_non_zero_coeff = 0;
  961. }
  962. } else {
  963. int last_cg = (x_c == (x_cg << 2) && y_c == (y_cg << 2));
  964. if (last_cg && implicit_non_zero_coeff && significant_coeff_group_flag[x_cg][y_cg]) {
  965. significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
  966. nb_significant_coeff_flag++;
  967. }
  968. }
  969. }
  970. n_end = nb_significant_coeff_flag;
  971. if (n_end) {
  972. int first_nz_pos_in_cg = 16;
  973. int last_nz_pos_in_cg = -1;
  974. int c_rice_param = 0;
  975. int first_greater1_coeff_idx = -1;
  976. uint8_t coeff_abs_level_greater1_flag[16] = { 0 };
  977. uint16_t coeff_sign_flag;
  978. int sum_abs = 0;
  979. int sign_hidden = 0;
  980. // initialize first elem of coeff_bas_level_greater1_flag
  981. int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
  982. if (!(i == num_last_subset) && greater1_ctx == 0)
  983. ctx_set++;
  984. greater1_ctx = 1;
  985. last_nz_pos_in_cg = significant_coeff_flag_idx[0];
  986. for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
  987. int n_idx = significant_coeff_flag_idx[m];
  988. int inc = (ctx_set << 2) + greater1_ctx;
  989. coeff_abs_level_greater1_flag[n_idx] =
  990. ff_hevc_coeff_abs_level_greater1_flag_decode(s, c_idx, inc);
  991. if (coeff_abs_level_greater1_flag[n_idx]) {
  992. greater1_ctx = 0;
  993. } else if (greater1_ctx > 0 && greater1_ctx < 3) {
  994. greater1_ctx++;
  995. }
  996. if (coeff_abs_level_greater1_flag[n_idx] &&
  997. first_greater1_coeff_idx == -1)
  998. first_greater1_coeff_idx = n_idx;
  999. }
  1000. first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
  1001. sign_hidden = last_nz_pos_in_cg - first_nz_pos_in_cg >= 4 &&
  1002. !lc->cu.cu_transquant_bypass_flag;
  1003. if (first_greater1_coeff_idx != -1) {
  1004. coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += ff_hevc_coeff_abs_level_greater2_flag_decode(s, c_idx, ctx_set);
  1005. }
  1006. if (!s->ps.pps->sign_data_hiding_flag || !sign_hidden) {
  1007. coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
  1008. } else {
  1009. coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
  1010. }
  1011. for (m = 0; m < n_end; m++) {
  1012. n = significant_coeff_flag_idx[m];
  1013. GET_COORD(offset, n);
  1014. trans_coeff_level = 1 + coeff_abs_level_greater1_flag[n];
  1015. if (trans_coeff_level == ((m < 8) ?
  1016. ((n == first_greater1_coeff_idx) ? 3 : 2) : 1)) {
  1017. trans_coeff_level += ff_hevc_coeff_abs_level_remaining(s, trans_coeff_level, c_rice_param);
  1018. if ((trans_coeff_level) > (3 * (1 << c_rice_param)))
  1019. c_rice_param = FFMIN(c_rice_param + 1, 4);
  1020. }
  1021. if (s->ps.pps->sign_data_hiding_flag && sign_hidden) {
  1022. sum_abs += trans_coeff_level;
  1023. if (n == first_nz_pos_in_cg && ((sum_abs & 1) == 1))
  1024. trans_coeff_level = -trans_coeff_level;
  1025. }
  1026. if (coeff_sign_flag >> 15)
  1027. trans_coeff_level = -trans_coeff_level;
  1028. coeff_sign_flag <<= 1;
  1029. if (!lc->cu.cu_transquant_bypass_flag) {
  1030. if (s->ps.sps->scaling_list_enable_flag) {
  1031. if (y_c || x_c || log2_trafo_size < 4) {
  1032. int pos;
  1033. switch (log2_trafo_size) {
  1034. case 3: pos = (y_c << 3) + x_c; break;
  1035. case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
  1036. case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
  1037. default: pos = (y_c << 2) + x_c;
  1038. }
  1039. scale_m = scale_matrix[pos];
  1040. } else {
  1041. scale_m = dc_scale;
  1042. }
  1043. }
  1044. trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
  1045. if(trans_coeff_level < 0) {
  1046. if((~trans_coeff_level) & 0xFffffffffff8000)
  1047. trans_coeff_level = -32768;
  1048. } else {
  1049. if (trans_coeff_level & 0xffffffffffff8000)
  1050. trans_coeff_level = 32767;
  1051. }
  1052. }
  1053. coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
  1054. }
  1055. }
  1056. }
  1057. if (!lc->cu.cu_transquant_bypass_flag) {
  1058. if (transform_skip_flag)
  1059. s->hevcdsp.dequant(coeffs);
  1060. else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 &&
  1061. log2_trafo_size == 2)
  1062. s->hevcdsp.transform_4x4_luma(coeffs);
  1063. else {
  1064. int max_xy = FFMAX(last_significant_coeff_x, last_significant_coeff_y);
  1065. if (max_xy == 0)
  1066. s->hevcdsp.idct_dc[log2_trafo_size - 2](coeffs);
  1067. else {
  1068. int col_limit = last_significant_coeff_x + last_significant_coeff_y + 4;
  1069. if (max_xy < 4)
  1070. col_limit = FFMIN(4, col_limit);
  1071. else if (max_xy < 8)
  1072. col_limit = FFMIN(8, col_limit);
  1073. else if (max_xy < 12)
  1074. col_limit = FFMIN(24, col_limit);
  1075. s->hevcdsp.idct[log2_trafo_size - 2](coeffs, col_limit);
  1076. }
  1077. }
  1078. }
  1079. s->hevcdsp.add_residual[log2_trafo_size - 2](dst, coeffs, stride);
  1080. }
  1081. static int hls_transform_unit(HEVCContext *s, int x0, int y0,
  1082. int xBase, int yBase, int cb_xBase, int cb_yBase,
  1083. int log2_cb_size, int log2_trafo_size,
  1084. int blk_idx, int cbf_luma, int cbf_cb, int cbf_cr)
  1085. {
  1086. HEVCLocalContext *lc = &s->HEVClc;
  1087. if (lc->cu.pred_mode == MODE_INTRA) {
  1088. int trafo_size = 1 << log2_trafo_size;
  1089. ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
  1090. s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
  1091. if (log2_trafo_size > 2) {
  1092. trafo_size = trafo_size << (s->ps.sps->hshift[1] - 1);
  1093. ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
  1094. s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 1);
  1095. s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 2);
  1096. } else if (blk_idx == 3) {
  1097. trafo_size = trafo_size << s->ps.sps->hshift[1];
  1098. ff_hevc_set_neighbour_available(s, xBase, yBase,
  1099. trafo_size, trafo_size);
  1100. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
  1101. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
  1102. }
  1103. }
  1104. if (cbf_luma || cbf_cb || cbf_cr) {
  1105. int scan_idx = SCAN_DIAG;
  1106. int scan_idx_c = SCAN_DIAG;
  1107. if (s->ps.pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
  1108. lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
  1109. if (lc->tu.cu_qp_delta != 0)
  1110. if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
  1111. lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
  1112. lc->tu.is_cu_qp_delta_coded = 1;
  1113. if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
  1114. lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
  1115. av_log(s->avctx, AV_LOG_ERROR,
  1116. "The cu_qp_delta %d is outside the valid range "
  1117. "[%d, %d].\n",
  1118. lc->tu.cu_qp_delta,
  1119. -(26 + s->ps.sps->qp_bd_offset / 2),
  1120. (25 + s->ps.sps->qp_bd_offset / 2));
  1121. return AVERROR_INVALIDDATA;
  1122. }
  1123. ff_hevc_set_qPy(s, x0, y0, cb_xBase, cb_yBase, log2_cb_size);
  1124. }
  1125. if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
  1126. if (lc->tu.cur_intra_pred_mode >= 6 &&
  1127. lc->tu.cur_intra_pred_mode <= 14) {
  1128. scan_idx = SCAN_VERT;
  1129. } else if (lc->tu.cur_intra_pred_mode >= 22 &&
  1130. lc->tu.cur_intra_pred_mode <= 30) {
  1131. scan_idx = SCAN_HORIZ;
  1132. }
  1133. if (lc->pu.intra_pred_mode_c >= 6 &&
  1134. lc->pu.intra_pred_mode_c <= 14) {
  1135. scan_idx_c = SCAN_VERT;
  1136. } else if (lc->pu.intra_pred_mode_c >= 22 &&
  1137. lc->pu.intra_pred_mode_c <= 30) {
  1138. scan_idx_c = SCAN_HORIZ;
  1139. }
  1140. }
  1141. if (cbf_luma)
  1142. hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
  1143. if (log2_trafo_size > 2) {
  1144. if (cbf_cb)
  1145. hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 1);
  1146. if (cbf_cr)
  1147. hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 2);
  1148. } else if (blk_idx == 3) {
  1149. if (cbf_cb)
  1150. hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 1);
  1151. if (cbf_cr)
  1152. hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 2);
  1153. }
  1154. }
  1155. return 0;
  1156. }
  1157. static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1158. {
  1159. int cb_size = 1 << log2_cb_size;
  1160. int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
  1161. int min_pu_width = s->ps.sps->min_pu_width;
  1162. int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
  1163. int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
  1164. int i, j;
  1165. for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
  1166. for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
  1167. s->is_pcm[i + j * min_pu_width] = 2;
  1168. }
  1169. static int hls_transform_tree(HEVCContext *s, int x0, int y0,
  1170. int xBase, int yBase, int cb_xBase, int cb_yBase,
  1171. int log2_cb_size, int log2_trafo_size,
  1172. int trafo_depth, int blk_idx,
  1173. int cbf_cb, int cbf_cr)
  1174. {
  1175. HEVCLocalContext *lc = &s->HEVClc;
  1176. uint8_t split_transform_flag;
  1177. int ret;
  1178. if (lc->cu.intra_split_flag) {
  1179. if (trafo_depth == 1)
  1180. lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
  1181. } else {
  1182. lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[0];
  1183. }
  1184. if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
  1185. log2_trafo_size > s->ps.sps->log2_min_tb_size &&
  1186. trafo_depth < lc->cu.max_trafo_depth &&
  1187. !(lc->cu.intra_split_flag && trafo_depth == 0)) {
  1188. split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
  1189. } else {
  1190. int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
  1191. lc->cu.pred_mode == MODE_INTER &&
  1192. lc->cu.part_mode != PART_2Nx2N &&
  1193. trafo_depth == 0;
  1194. split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
  1195. (lc->cu.intra_split_flag && trafo_depth == 0) ||
  1196. inter_split;
  1197. }
  1198. if (log2_trafo_size > 2 && (trafo_depth == 0 || cbf_cb))
  1199. cbf_cb = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  1200. else if (log2_trafo_size > 2 || trafo_depth == 0)
  1201. cbf_cb = 0;
  1202. if (log2_trafo_size > 2 && (trafo_depth == 0 || cbf_cr))
  1203. cbf_cr = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  1204. else if (log2_trafo_size > 2 || trafo_depth == 0)
  1205. cbf_cr = 0;
  1206. if (split_transform_flag) {
  1207. const int trafo_size_split = 1 << (log2_trafo_size - 1);
  1208. const int x1 = x0 + trafo_size_split;
  1209. const int y1 = y0 + trafo_size_split;
  1210. #define SUBDIVIDE(x, y, idx) \
  1211. do { \
  1212. ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
  1213. log2_trafo_size - 1, trafo_depth + 1, idx, \
  1214. cbf_cb, cbf_cr); \
  1215. if (ret < 0) \
  1216. return ret; \
  1217. } while (0)
  1218. SUBDIVIDE(x0, y0, 0);
  1219. SUBDIVIDE(x1, y0, 1);
  1220. SUBDIVIDE(x0, y1, 2);
  1221. SUBDIVIDE(x1, y1, 3);
  1222. #undef SUBDIVIDE
  1223. } else {
  1224. int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
  1225. int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
  1226. int min_tu_width = s->ps.sps->min_tb_width;
  1227. int cbf_luma = 1;
  1228. if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
  1229. cbf_cb || cbf_cr)
  1230. cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
  1231. ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
  1232. log2_cb_size, log2_trafo_size,
  1233. blk_idx, cbf_luma, cbf_cb, cbf_cr);
  1234. if (ret < 0)
  1235. return ret;
  1236. // TODO: store cbf_luma somewhere else
  1237. if (cbf_luma) {
  1238. int i, j;
  1239. for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
  1240. for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
  1241. int x_tu = (x0 + j) >> log2_min_tu_size;
  1242. int y_tu = (y0 + i) >> log2_min_tu_size;
  1243. s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
  1244. }
  1245. }
  1246. if (!s->sh.disable_deblocking_filter_flag) {
  1247. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
  1248. if (s->ps.pps->transquant_bypass_enable_flag &&
  1249. lc->cu.cu_transquant_bypass_flag)
  1250. set_deblocking_bypass(s, x0, y0, log2_trafo_size);
  1251. }
  1252. }
  1253. return 0;
  1254. }
  1255. static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1256. {
  1257. //TODO: non-4:2:0 support
  1258. HEVCLocalContext *lc = &s->HEVClc;
  1259. GetBitContext gb;
  1260. int cb_size = 1 << log2_cb_size;
  1261. ptrdiff_t stride0 = s->frame->linesize[0];
  1262. ptrdiff_t stride1 = s->frame->linesize[1];
  1263. ptrdiff_t stride2 = s->frame->linesize[2];
  1264. uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
  1265. uint8_t *dst1 = &s->frame->data[1][(y0 >> s->ps.sps->vshift[1]) * stride1 + ((x0 >> s->ps.sps->hshift[1]) << s->ps.sps->pixel_shift)];
  1266. uint8_t *dst2 = &s->frame->data[2][(y0 >> s->ps.sps->vshift[2]) * stride2 + ((x0 >> s->ps.sps->hshift[2]) << s->ps.sps->pixel_shift)];
  1267. int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth + ((cb_size * cb_size) >> 1) * s->ps.sps->pcm.bit_depth_chroma;
  1268. const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
  1269. int ret;
  1270. if (!s->sh.disable_deblocking_filter_flag)
  1271. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  1272. ret = init_get_bits(&gb, pcm, length);
  1273. if (ret < 0)
  1274. return ret;
  1275. s->hevcdsp.put_pcm(dst0, stride0, cb_size, &gb, s->ps.sps->pcm.bit_depth);
  1276. s->hevcdsp.put_pcm(dst1, stride1, cb_size / 2, &gb, s->ps.sps->pcm.bit_depth_chroma);
  1277. s->hevcdsp.put_pcm(dst2, stride2, cb_size / 2, &gb, s->ps.sps->pcm.bit_depth_chroma);
  1278. return 0;
  1279. }
  1280. static void hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1281. {
  1282. HEVCLocalContext *lc = &s->HEVClc;
  1283. int x = ff_hevc_abs_mvd_greater0_flag_decode(s);
  1284. int y = ff_hevc_abs_mvd_greater0_flag_decode(s);
  1285. if (x)
  1286. x += ff_hevc_abs_mvd_greater1_flag_decode(s);
  1287. if (y)
  1288. y += ff_hevc_abs_mvd_greater1_flag_decode(s);
  1289. switch (x) {
  1290. case 2: lc->pu.mvd.x = ff_hevc_mvd_decode(s); break;
  1291. case 1: lc->pu.mvd.x = ff_hevc_mvd_sign_flag_decode(s); break;
  1292. case 0: lc->pu.mvd.x = 0; break;
  1293. }
  1294. switch (y) {
  1295. case 2: lc->pu.mvd.y = ff_hevc_mvd_decode(s); break;
  1296. case 1: lc->pu.mvd.y = ff_hevc_mvd_sign_flag_decode(s); break;
  1297. case 0: lc->pu.mvd.y = 0; break;
  1298. }
  1299. }
  1300. /**
  1301. * 8.5.3.2.2.1 Luma sample interpolation process
  1302. *
  1303. * @param s HEVC decoding context
  1304. * @param dst target buffer for block data at block position
  1305. * @param dststride stride of the dst buffer
  1306. * @param ref reference picture buffer at origin (0, 0)
  1307. * @param mv motion vector (relative to block position) to get pixel data from
  1308. * @param x_off horizontal position of block from origin (0, 0)
  1309. * @param y_off vertical position of block from origin (0, 0)
  1310. * @param block_w width of block
  1311. * @param block_h height of block
  1312. */
  1313. static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride,
  1314. AVFrame *ref, const Mv *mv, int x_off, int y_off,
  1315. int block_w, int block_h, int pred_idx)
  1316. {
  1317. HEVCLocalContext *lc = &s->HEVClc;
  1318. uint8_t *src = ref->data[0];
  1319. ptrdiff_t srcstride = ref->linesize[0];
  1320. int pic_width = s->ps.sps->width;
  1321. int pic_height = s->ps.sps->height;
  1322. int mx = mv->x & 3;
  1323. int my = mv->y & 3;
  1324. int extra_left = ff_hevc_qpel_extra_before[mx];
  1325. int extra_top = ff_hevc_qpel_extra_before[my];
  1326. x_off += mv->x >> 2;
  1327. y_off += mv->y >> 2;
  1328. src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
  1329. if (x_off < extra_left || y_off < extra_top ||
  1330. x_off >= pic_width - block_w - ff_hevc_qpel_extra_after[mx] ||
  1331. y_off >= pic_height - block_h - ff_hevc_qpel_extra_after[my]) {
  1332. const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
  1333. int offset = extra_top * srcstride + (extra_left << s->ps.sps->pixel_shift);
  1334. int buf_offset = extra_top *
  1335. edge_emu_stride + (extra_left << s->ps.sps->pixel_shift);
  1336. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
  1337. edge_emu_stride, srcstride,
  1338. block_w + ff_hevc_qpel_extra[mx],
  1339. block_h + ff_hevc_qpel_extra[my],
  1340. x_off - extra_left, y_off - extra_top,
  1341. pic_width, pic_height);
  1342. src = lc->edge_emu_buffer + buf_offset;
  1343. srcstride = edge_emu_stride;
  1344. }
  1345. s->hevcdsp.put_hevc_qpel[!!my][!!mx][pred_idx](dst, dststride, src, srcstride,
  1346. block_h, mx, my, lc->mc_buffer);
  1347. }
  1348. /**
  1349. * 8.5.3.2.2.2 Chroma sample interpolation process
  1350. *
  1351. * @param s HEVC decoding context
  1352. * @param dst1 target buffer for block data at block position (U plane)
  1353. * @param dst2 target buffer for block data at block position (V plane)
  1354. * @param dststride stride of the dst1 and dst2 buffers
  1355. * @param ref reference picture buffer at origin (0, 0)
  1356. * @param mv motion vector (relative to block position) to get pixel data from
  1357. * @param x_off horizontal position of block from origin (0, 0)
  1358. * @param y_off vertical position of block from origin (0, 0)
  1359. * @param block_w width of block
  1360. * @param block_h height of block
  1361. */
  1362. static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2,
  1363. ptrdiff_t dststride, AVFrame *ref, const Mv *mv,
  1364. int x_off, int y_off, int block_w, int block_h, int pred_idx)
  1365. {
  1366. HEVCLocalContext *lc = &s->HEVClc;
  1367. uint8_t *src1 = ref->data[1];
  1368. uint8_t *src2 = ref->data[2];
  1369. ptrdiff_t src1stride = ref->linesize[1];
  1370. ptrdiff_t src2stride = ref->linesize[2];
  1371. int pic_width = s->ps.sps->width >> 1;
  1372. int pic_height = s->ps.sps->height >> 1;
  1373. int mx = mv->x & 7;
  1374. int my = mv->y & 7;
  1375. x_off += mv->x >> 3;
  1376. y_off += mv->y >> 3;
  1377. src1 += y_off * src1stride + (x_off * (1 << s->ps.sps->pixel_shift));
  1378. src2 += y_off * src2stride + (x_off * (1 << s->ps.sps->pixel_shift));
  1379. if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
  1380. x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1381. y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1382. const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
  1383. int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
  1384. int buf_offset1 = EPEL_EXTRA_BEFORE *
  1385. (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
  1386. int offset2 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
  1387. int buf_offset2 = EPEL_EXTRA_BEFORE *
  1388. (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
  1389. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
  1390. edge_emu_stride, src1stride,
  1391. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1392. x_off - EPEL_EXTRA_BEFORE,
  1393. y_off - EPEL_EXTRA_BEFORE,
  1394. pic_width, pic_height);
  1395. src1 = lc->edge_emu_buffer + buf_offset1;
  1396. src1stride = edge_emu_stride;
  1397. s->hevcdsp.put_hevc_epel[!!my][!!mx][pred_idx](dst1, dststride, src1, src1stride,
  1398. block_h, mx, my, lc->mc_buffer);
  1399. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src2 - offset2,
  1400. edge_emu_stride, src2stride,
  1401. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1402. x_off - EPEL_EXTRA_BEFORE,
  1403. y_off - EPEL_EXTRA_BEFORE,
  1404. pic_width, pic_height);
  1405. src2 = lc->edge_emu_buffer + buf_offset2;
  1406. src2stride = edge_emu_stride;
  1407. s->hevcdsp.put_hevc_epel[!!my][!!mx][pred_idx](dst2, dststride, src2, src2stride,
  1408. block_h, mx, my, lc->mc_buffer);
  1409. } else {
  1410. s->hevcdsp.put_hevc_epel[!!my][!!mx][pred_idx](dst1, dststride, src1, src1stride,
  1411. block_h, mx, my, lc->mc_buffer);
  1412. s->hevcdsp.put_hevc_epel[!!my][!!mx][pred_idx](dst2, dststride, src2, src2stride,
  1413. block_h, mx, my, lc->mc_buffer);
  1414. }
  1415. }
  1416. static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
  1417. const Mv *mv, int y0, int height)
  1418. {
  1419. int y = (mv->y >> 2) + y0 + height + 9;
  1420. ff_thread_await_progress(&ref->tf, y, 0);
  1421. }
  1422. static void hevc_luma_mv_mpv_mode(HEVCContext *s, int x0, int y0, int nPbW,
  1423. int nPbH, int log2_cb_size, int part_idx,
  1424. int merge_idx, MvField *mv)
  1425. {
  1426. HEVCLocalContext *lc = &s->HEVClc;
  1427. enum InterPredIdc inter_pred_idc = PRED_L0;
  1428. int mvp_flag;
  1429. ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
  1430. if (s->sh.slice_type == HEVC_SLICE_B)
  1431. inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
  1432. if (inter_pred_idc != PRED_L1) {
  1433. if (s->sh.nb_refs[L0])
  1434. mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
  1435. mv->pred_flag[0] = 1;
  1436. hls_mvd_coding(s, x0, y0, 0);
  1437. mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
  1438. ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1439. part_idx, merge_idx, mv, mvp_flag, 0);
  1440. mv->mv[0].x += lc->pu.mvd.x;
  1441. mv->mv[0].y += lc->pu.mvd.y;
  1442. }
  1443. if (inter_pred_idc != PRED_L0) {
  1444. if (s->sh.nb_refs[L1])
  1445. mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
  1446. if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
  1447. AV_ZERO32(&lc->pu.mvd);
  1448. } else {
  1449. hls_mvd_coding(s, x0, y0, 1);
  1450. }
  1451. mv->pred_flag[1] = 1;
  1452. mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
  1453. ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1454. part_idx, merge_idx, mv, mvp_flag, 1);
  1455. mv->mv[1].x += lc->pu.mvd.x;
  1456. mv->mv[1].y += lc->pu.mvd.y;
  1457. }
  1458. }
  1459. static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
  1460. int nPbW, int nPbH,
  1461. int log2_cb_size, int partIdx)
  1462. {
  1463. static const int pred_indices[] = {
  1464. [4] = 0, [8] = 1, [12] = 2, [16] = 3, [24] = 4, [32] = 5, [48] = 6, [64] = 7,
  1465. };
  1466. const int pred_idx = pred_indices[nPbW];
  1467. #define POS(c_idx, x, y) \
  1468. &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
  1469. (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
  1470. HEVCLocalContext *lc = &s->HEVClc;
  1471. int merge_idx = 0;
  1472. struct MvField current_mv = {{{ 0 }}};
  1473. int min_pu_width = s->ps.sps->min_pu_width;
  1474. int weighted_pred = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
  1475. (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
  1476. MvField *tab_mvf = s->ref->tab_mvf;
  1477. RefPicList *refPicList = s->ref->refPicList;
  1478. HEVCFrame *ref0, *ref1;
  1479. ptrdiff_t tmpstride = MAX_PB_SIZE * sizeof(int16_t);
  1480. uint8_t *dst0 = POS(0, x0, y0);
  1481. uint8_t *dst1 = POS(1, x0, y0);
  1482. uint8_t *dst2 = POS(2, x0, y0);
  1483. int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
  1484. int min_cb_width = s->ps.sps->min_cb_width;
  1485. int x_cb = x0 >> log2_min_cb_size;
  1486. int y_cb = y0 >> log2_min_cb_size;
  1487. int x_pu, y_pu;
  1488. int i, j;
  1489. int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
  1490. if (!skip_flag)
  1491. lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
  1492. if (skip_flag || lc->pu.merge_flag) {
  1493. if (s->sh.max_num_merge_cand > 1)
  1494. merge_idx = ff_hevc_merge_idx_decode(s);
  1495. else
  1496. merge_idx = 0;
  1497. ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1498. partIdx, merge_idx, &current_mv);
  1499. } else {
  1500. hevc_luma_mv_mpv_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1501. partIdx, merge_idx, &current_mv);
  1502. }
  1503. x_pu = x0 >> s->ps.sps->log2_min_pu_size;
  1504. y_pu = y0 >> s->ps.sps->log2_min_pu_size;
  1505. for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
  1506. for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
  1507. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1508. if (current_mv.pred_flag[0]) {
  1509. ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
  1510. if (!ref0)
  1511. return;
  1512. hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
  1513. }
  1514. if (current_mv.pred_flag[1]) {
  1515. ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
  1516. if (!ref1)
  1517. return;
  1518. hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
  1519. }
  1520. if (current_mv.pred_flag[0] && !current_mv.pred_flag[1]) {
  1521. LOCAL_ALIGNED_16(int16_t, tmp, [MAX_PB_SIZE * MAX_PB_SIZE]);
  1522. LOCAL_ALIGNED_16(int16_t, tmp2, [MAX_PB_SIZE * MAX_PB_SIZE]);
  1523. luma_mc(s, tmp, tmpstride, ref0->frame,
  1524. &current_mv.mv[0], x0, y0, nPbW, nPbH, pred_idx);
  1525. if (weighted_pred) {
  1526. s->hevcdsp.weighted_pred[pred_idx](s->sh.luma_log2_weight_denom,
  1527. s->sh.luma_weight_l0[current_mv.ref_idx[0]],
  1528. s->sh.luma_offset_l0[current_mv.ref_idx[0]],
  1529. dst0, s->frame->linesize[0], tmp,
  1530. tmpstride, nPbH);
  1531. } else {
  1532. s->hevcdsp.put_unweighted_pred[pred_idx](dst0, s->frame->linesize[0], tmp, tmpstride, nPbH);
  1533. }
  1534. chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
  1535. &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2, pred_idx);
  1536. if (weighted_pred) {
  1537. s->hevcdsp.weighted_pred_chroma[pred_idx](s->sh.chroma_log2_weight_denom,
  1538. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
  1539. s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
  1540. dst1, s->frame->linesize[1], tmp, tmpstride,
  1541. nPbH / 2);
  1542. s->hevcdsp.weighted_pred_chroma[pred_idx](s->sh.chroma_log2_weight_denom,
  1543. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
  1544. s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
  1545. dst2, s->frame->linesize[2], tmp2, tmpstride,
  1546. nPbH / 2);
  1547. } else {
  1548. s->hevcdsp.put_unweighted_pred_chroma[pred_idx](dst1, s->frame->linesize[1], tmp, tmpstride, nPbH / 2);
  1549. s->hevcdsp.put_unweighted_pred_chroma[pred_idx](dst2, s->frame->linesize[2], tmp2, tmpstride, nPbH / 2);
  1550. }
  1551. } else if (!current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
  1552. LOCAL_ALIGNED_16(int16_t, tmp, [MAX_PB_SIZE * MAX_PB_SIZE]);
  1553. LOCAL_ALIGNED_16(int16_t, tmp2, [MAX_PB_SIZE * MAX_PB_SIZE]);
  1554. luma_mc(s, tmp, tmpstride, ref1->frame,
  1555. &current_mv.mv[1], x0, y0, nPbW, nPbH, pred_idx);
  1556. if (weighted_pred) {
  1557. s->hevcdsp.weighted_pred[pred_idx](s->sh.luma_log2_weight_denom,
  1558. s->sh.luma_weight_l1[current_mv.ref_idx[1]],
  1559. s->sh.luma_offset_l1[current_mv.ref_idx[1]],
  1560. dst0, s->frame->linesize[0], tmp, tmpstride,
  1561. nPbH);
  1562. } else {
  1563. s->hevcdsp.put_unweighted_pred[pred_idx](dst0, s->frame->linesize[0], tmp, tmpstride, nPbH);
  1564. }
  1565. chroma_mc(s, tmp, tmp2, tmpstride, ref1->frame,
  1566. &current_mv.mv[1], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2, pred_idx);
  1567. if (weighted_pred) {
  1568. s->hevcdsp.weighted_pred_chroma[pred_idx](s->sh.chroma_log2_weight_denom,
  1569. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
  1570. s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
  1571. dst1, s->frame->linesize[1], tmp, tmpstride, nPbH/2);
  1572. s->hevcdsp.weighted_pred_chroma[pred_idx](s->sh.chroma_log2_weight_denom,
  1573. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
  1574. s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
  1575. dst2, s->frame->linesize[2], tmp2, tmpstride, nPbH/2);
  1576. } else {
  1577. s->hevcdsp.put_unweighted_pred_chroma[pred_idx](dst1, s->frame->linesize[1], tmp, tmpstride, nPbH / 2);
  1578. s->hevcdsp.put_unweighted_pred_chroma[pred_idx](dst2, s->frame->linesize[2], tmp2, tmpstride, nPbH / 2);
  1579. }
  1580. } else if (current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
  1581. LOCAL_ALIGNED_16(int16_t, tmp, [MAX_PB_SIZE * MAX_PB_SIZE]);
  1582. LOCAL_ALIGNED_16(int16_t, tmp2, [MAX_PB_SIZE * MAX_PB_SIZE]);
  1583. LOCAL_ALIGNED_16(int16_t, tmp3, [MAX_PB_SIZE * MAX_PB_SIZE]);
  1584. LOCAL_ALIGNED_16(int16_t, tmp4, [MAX_PB_SIZE * MAX_PB_SIZE]);
  1585. luma_mc(s, tmp, tmpstride, ref0->frame,
  1586. &current_mv.mv[0], x0, y0, nPbW, nPbH, pred_idx);
  1587. luma_mc(s, tmp2, tmpstride, ref1->frame,
  1588. &current_mv.mv[1], x0, y0, nPbW, nPbH, pred_idx);
  1589. if (weighted_pred) {
  1590. s->hevcdsp.weighted_pred_avg[pred_idx](s->sh.luma_log2_weight_denom,
  1591. s->sh.luma_weight_l0[current_mv.ref_idx[0]],
  1592. s->sh.luma_weight_l1[current_mv.ref_idx[1]],
  1593. s->sh.luma_offset_l0[current_mv.ref_idx[0]],
  1594. s->sh.luma_offset_l1[current_mv.ref_idx[1]],
  1595. dst0, s->frame->linesize[0],
  1596. tmp, tmp2, tmpstride, nPbH);
  1597. } else {
  1598. s->hevcdsp.put_unweighted_pred_avg[pred_idx](dst0, s->frame->linesize[0],
  1599. tmp, tmp2, tmpstride, nPbH);
  1600. }
  1601. chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
  1602. &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2, pred_idx);
  1603. chroma_mc(s, tmp3, tmp4, tmpstride, ref1->frame,
  1604. &current_mv.mv[1], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2, pred_idx);
  1605. if (weighted_pred) {
  1606. s->hevcdsp.weighted_pred_avg_chroma[pred_idx](s->sh.chroma_log2_weight_denom,
  1607. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
  1608. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
  1609. s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
  1610. s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
  1611. dst1, s->frame->linesize[1], tmp, tmp3,
  1612. tmpstride, nPbH / 2);
  1613. s->hevcdsp.weighted_pred_avg_chroma[pred_idx](s->sh.chroma_log2_weight_denom,
  1614. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
  1615. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
  1616. s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
  1617. s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
  1618. dst2, s->frame->linesize[2], tmp2, tmp4,
  1619. tmpstride, nPbH / 2);
  1620. } else {
  1621. s->hevcdsp.put_unweighted_pred_avg_chroma[pred_idx](dst1, s->frame->linesize[1], tmp, tmp3, tmpstride, nPbH/2);
  1622. s->hevcdsp.put_unweighted_pred_avg_chroma[pred_idx](dst2, s->frame->linesize[2], tmp2, tmp4, tmpstride, nPbH/2);
  1623. }
  1624. }
  1625. }
  1626. /**
  1627. * 8.4.1
  1628. */
  1629. static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
  1630. int prev_intra_luma_pred_flag)
  1631. {
  1632. HEVCLocalContext *lc = &s->HEVClc;
  1633. int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
  1634. int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
  1635. int min_pu_width = s->ps.sps->min_pu_width;
  1636. int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
  1637. int x0b = x0 & ((1 << s->ps.sps->log2_ctb_size) - 1);
  1638. int y0b = y0 & ((1 << s->ps.sps->log2_ctb_size) - 1);
  1639. int cand_up = (lc->ctb_up_flag || y0b) ?
  1640. s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
  1641. int cand_left = (lc->ctb_left_flag || x0b) ?
  1642. s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
  1643. int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
  1644. MvField *tab_mvf = s->ref->tab_mvf;
  1645. int intra_pred_mode;
  1646. int candidate[3];
  1647. int i, j;
  1648. // intra_pred_mode prediction does not cross vertical CTB boundaries
  1649. if ((y0 - 1) < y_ctb)
  1650. cand_up = INTRA_DC;
  1651. if (cand_left == cand_up) {
  1652. if (cand_left < 2) {
  1653. candidate[0] = INTRA_PLANAR;
  1654. candidate[1] = INTRA_DC;
  1655. candidate[2] = INTRA_ANGULAR_26;
  1656. } else {
  1657. candidate[0] = cand_left;
  1658. candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
  1659. candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
  1660. }
  1661. } else {
  1662. candidate[0] = cand_left;
  1663. candidate[1] = cand_up;
  1664. if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
  1665. candidate[2] = INTRA_PLANAR;
  1666. } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
  1667. candidate[2] = INTRA_DC;
  1668. } else {
  1669. candidate[2] = INTRA_ANGULAR_26;
  1670. }
  1671. }
  1672. if (prev_intra_luma_pred_flag) {
  1673. intra_pred_mode = candidate[lc->pu.mpm_idx];
  1674. } else {
  1675. if (candidate[0] > candidate[1])
  1676. FFSWAP(uint8_t, candidate[0], candidate[1]);
  1677. if (candidate[0] > candidate[2])
  1678. FFSWAP(uint8_t, candidate[0], candidate[2]);
  1679. if (candidate[1] > candidate[2])
  1680. FFSWAP(uint8_t, candidate[1], candidate[2]);
  1681. intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
  1682. for (i = 0; i < 3; i++)
  1683. if (intra_pred_mode >= candidate[i])
  1684. intra_pred_mode++;
  1685. }
  1686. /* write the intra prediction units into the mv array */
  1687. if (!size_in_pus)
  1688. size_in_pus = 1;
  1689. for (i = 0; i < size_in_pus; i++) {
  1690. memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
  1691. intra_pred_mode, size_in_pus);
  1692. for (j = 0; j < size_in_pus; j++) {
  1693. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].is_intra = 1;
  1694. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[0] = 0;
  1695. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[1] = 0;
  1696. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[0] = 0;
  1697. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[1] = 0;
  1698. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].x = 0;
  1699. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].y = 0;
  1700. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].x = 0;
  1701. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].y = 0;
  1702. }
  1703. }
  1704. return intra_pred_mode;
  1705. }
  1706. static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
  1707. int log2_cb_size, int ct_depth)
  1708. {
  1709. int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
  1710. int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
  1711. int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
  1712. int y;
  1713. for (y = 0; y < length; y++)
  1714. memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
  1715. ct_depth, length);
  1716. }
  1717. static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
  1718. int log2_cb_size)
  1719. {
  1720. HEVCLocalContext *lc = &s->HEVClc;
  1721. static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
  1722. uint8_t prev_intra_luma_pred_flag[4];
  1723. int split = lc->cu.part_mode == PART_NxN;
  1724. int pb_size = (1 << log2_cb_size) >> split;
  1725. int side = split + 1;
  1726. int chroma_mode;
  1727. int i, j;
  1728. for (i = 0; i < side; i++)
  1729. for (j = 0; j < side; j++)
  1730. prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
  1731. for (i = 0; i < side; i++) {
  1732. for (j = 0; j < side; j++) {
  1733. if (prev_intra_luma_pred_flag[2 * i + j])
  1734. lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
  1735. else
  1736. lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
  1737. lc->pu.intra_pred_mode[2 * i + j] =
  1738. luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
  1739. prev_intra_luma_pred_flag[2 * i + j]);
  1740. }
  1741. }
  1742. chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
  1743. if (chroma_mode != 4) {
  1744. if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
  1745. lc->pu.intra_pred_mode_c = 34;
  1746. else
  1747. lc->pu.intra_pred_mode_c = intra_chroma_table[chroma_mode];
  1748. } else {
  1749. lc->pu.intra_pred_mode_c = lc->pu.intra_pred_mode[0];
  1750. }
  1751. }
  1752. static void intra_prediction_unit_default_value(HEVCContext *s,
  1753. int x0, int y0,
  1754. int log2_cb_size)
  1755. {
  1756. HEVCLocalContext *lc = &s->HEVClc;
  1757. int pb_size = 1 << log2_cb_size;
  1758. int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
  1759. int min_pu_width = s->ps.sps->min_pu_width;
  1760. MvField *tab_mvf = s->ref->tab_mvf;
  1761. int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
  1762. int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
  1763. int j, k;
  1764. if (size_in_pus == 0)
  1765. size_in_pus = 1;
  1766. for (j = 0; j < size_in_pus; j++) {
  1767. memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
  1768. for (k = 0; k < size_in_pus; k++)
  1769. tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].is_intra = lc->cu.pred_mode == MODE_INTRA;
  1770. }
  1771. }
  1772. static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1773. {
  1774. int cb_size = 1 << log2_cb_size;
  1775. HEVCLocalContext *lc = &s->HEVClc;
  1776. int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
  1777. int length = cb_size >> log2_min_cb_size;
  1778. int min_cb_width = s->ps.sps->min_cb_width;
  1779. int x_cb = x0 >> log2_min_cb_size;
  1780. int y_cb = y0 >> log2_min_cb_size;
  1781. int x, y, ret;
  1782. lc->cu.x = x0;
  1783. lc->cu.y = y0;
  1784. lc->cu.pred_mode = MODE_INTRA;
  1785. lc->cu.part_mode = PART_2Nx2N;
  1786. lc->cu.intra_split_flag = 0;
  1787. SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
  1788. for (x = 0; x < 4; x++)
  1789. lc->pu.intra_pred_mode[x] = 1;
  1790. if (s->ps.pps->transquant_bypass_enable_flag) {
  1791. lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
  1792. if (lc->cu.cu_transquant_bypass_flag)
  1793. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  1794. } else
  1795. lc->cu.cu_transquant_bypass_flag = 0;
  1796. if (s->sh.slice_type != HEVC_SLICE_I) {
  1797. uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
  1798. x = y_cb * min_cb_width + x_cb;
  1799. for (y = 0; y < length; y++) {
  1800. memset(&s->skip_flag[x], skip_flag, length);
  1801. x += min_cb_width;
  1802. }
  1803. lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
  1804. }
  1805. if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
  1806. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
  1807. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1808. if (!s->sh.disable_deblocking_filter_flag)
  1809. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  1810. } else {
  1811. int pcm_flag = 0;
  1812. if (s->sh.slice_type != HEVC_SLICE_I)
  1813. lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
  1814. if (lc->cu.pred_mode != MODE_INTRA ||
  1815. log2_cb_size == s->ps.sps->log2_min_cb_size) {
  1816. lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
  1817. lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
  1818. lc->cu.pred_mode == MODE_INTRA;
  1819. }
  1820. if (lc->cu.pred_mode == MODE_INTRA) {
  1821. if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
  1822. log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
  1823. log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
  1824. pcm_flag = ff_hevc_pcm_flag_decode(s);
  1825. }
  1826. if (pcm_flag) {
  1827. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1828. ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
  1829. if (s->ps.sps->pcm.loop_filter_disable_flag)
  1830. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  1831. if (ret < 0)
  1832. return ret;
  1833. } else {
  1834. intra_prediction_unit(s, x0, y0, log2_cb_size);
  1835. }
  1836. } else {
  1837. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1838. switch (lc->cu.part_mode) {
  1839. case PART_2Nx2N:
  1840. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
  1841. break;
  1842. case PART_2NxN:
  1843. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0);
  1844. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1);
  1845. break;
  1846. case PART_Nx2N:
  1847. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0);
  1848. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1);
  1849. break;
  1850. case PART_2NxnU:
  1851. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0);
  1852. hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1);
  1853. break;
  1854. case PART_2NxnD:
  1855. hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0);
  1856. hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1);
  1857. break;
  1858. case PART_nLx2N:
  1859. hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0);
  1860. hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1);
  1861. break;
  1862. case PART_nRx2N:
  1863. hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0);
  1864. hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1);
  1865. break;
  1866. case PART_NxN:
  1867. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0);
  1868. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1);
  1869. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2);
  1870. hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3);
  1871. break;
  1872. }
  1873. }
  1874. if (!pcm_flag) {
  1875. int rqt_root_cbf = 1;
  1876. if (lc->cu.pred_mode != MODE_INTRA &&
  1877. !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
  1878. rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
  1879. }
  1880. if (rqt_root_cbf) {
  1881. lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
  1882. s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
  1883. s->ps.sps->max_transform_hierarchy_depth_inter;
  1884. ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
  1885. log2_cb_size,
  1886. log2_cb_size, 0, 0, 0, 0);
  1887. if (ret < 0)
  1888. return ret;
  1889. } else {
  1890. if (!s->sh.disable_deblocking_filter_flag)
  1891. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  1892. }
  1893. }
  1894. }
  1895. if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
  1896. ff_hevc_set_qPy(s, x0, y0, x0, y0, log2_cb_size);
  1897. x = y_cb * min_cb_width + x_cb;
  1898. for (y = 0; y < length; y++) {
  1899. memset(&s->qp_y_tab[x], lc->qp_y, length);
  1900. x += min_cb_width;
  1901. }
  1902. set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
  1903. return 0;
  1904. }
  1905. static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
  1906. int log2_cb_size, int cb_depth)
  1907. {
  1908. HEVCLocalContext *lc = &s->HEVClc;
  1909. const int cb_size = 1 << log2_cb_size;
  1910. int split_cu;
  1911. lc->ct.depth = cb_depth;
  1912. if (x0 + cb_size <= s->ps.sps->width &&
  1913. y0 + cb_size <= s->ps.sps->height &&
  1914. log2_cb_size > s->ps.sps->log2_min_cb_size) {
  1915. split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
  1916. } else {
  1917. split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
  1918. }
  1919. if (s->ps.pps->cu_qp_delta_enabled_flag &&
  1920. log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
  1921. lc->tu.is_cu_qp_delta_coded = 0;
  1922. lc->tu.cu_qp_delta = 0;
  1923. }
  1924. if (split_cu) {
  1925. const int cb_size_split = cb_size >> 1;
  1926. const int x1 = x0 + cb_size_split;
  1927. const int y1 = y0 + cb_size_split;
  1928. log2_cb_size--;
  1929. cb_depth++;
  1930. #define SUBDIVIDE(x, y) \
  1931. do { \
  1932. if (x < s->ps.sps->width && y < s->ps.sps->height) { \
  1933. int ret = hls_coding_quadtree(s, x, y, log2_cb_size, cb_depth);\
  1934. if (ret < 0) \
  1935. return ret; \
  1936. } \
  1937. } while (0)
  1938. SUBDIVIDE(x0, y0);
  1939. SUBDIVIDE(x1, y0);
  1940. SUBDIVIDE(x0, y1);
  1941. SUBDIVIDE(x1, y1);
  1942. } else {
  1943. int ret = hls_coding_unit(s, x0, y0, log2_cb_size);
  1944. if (ret < 0)
  1945. return ret;
  1946. }
  1947. return 0;
  1948. }
  1949. static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
  1950. int ctb_addr_ts)
  1951. {
  1952. HEVCLocalContext *lc = &s->HEVClc;
  1953. int ctb_size = 1 << s->ps.sps->log2_ctb_size;
  1954. int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  1955. int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
  1956. s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
  1957. if (s->ps.pps->entropy_coding_sync_enabled_flag) {
  1958. if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
  1959. lc->first_qp_group = 1;
  1960. lc->end_of_tiles_x = s->ps.sps->width;
  1961. } else if (s->ps.pps->tiles_enabled_flag) {
  1962. if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
  1963. int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
  1964. lc->start_of_tiles_x = x_ctb;
  1965. lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
  1966. lc->first_qp_group = 1;
  1967. }
  1968. } else {
  1969. lc->end_of_tiles_x = s->ps.sps->width;
  1970. }
  1971. lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
  1972. lc->boundary_flags = 0;
  1973. if (s->ps.pps->tiles_enabled_flag) {
  1974. if (x_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
  1975. lc->boundary_flags |= BOUNDARY_LEFT_TILE;
  1976. if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
  1977. lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
  1978. if (y_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->ps.sps->ctb_width]])
  1979. lc->boundary_flags |= BOUNDARY_UPPER_TILE;
  1980. if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
  1981. lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
  1982. } else {
  1983. if (!ctb_addr_in_slice)
  1984. lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
  1985. if (ctb_addr_in_slice < s->ps.sps->ctb_width)
  1986. lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
  1987. }
  1988. lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
  1989. lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
  1990. lc->ctb_up_right_flag = ((y_ctb > 0) && (ctb_addr_in_slice+1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - s->ps.sps->ctb_width]]));
  1991. lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->ps.sps->ctb_width]]));
  1992. }
  1993. static int hls_slice_data(HEVCContext *s)
  1994. {
  1995. int ctb_size = 1 << s->ps.sps->log2_ctb_size;
  1996. int more_data = 1;
  1997. int x_ctb = 0;
  1998. int y_ctb = 0;
  1999. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
  2000. int ret;
  2001. while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
  2002. int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  2003. x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
  2004. y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
  2005. hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
  2006. ff_hevc_cabac_init(s, ctb_addr_ts);
  2007. hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
  2008. s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
  2009. s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
  2010. s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
  2011. ret = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
  2012. if (ret < 0)
  2013. return ret;
  2014. more_data = !ff_hevc_end_of_slice_flag_decode(s);
  2015. ctb_addr_ts++;
  2016. ff_hevc_save_states(s, ctb_addr_ts);
  2017. ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
  2018. }
  2019. if (x_ctb + ctb_size >= s->ps.sps->width &&
  2020. y_ctb + ctb_size >= s->ps.sps->height)
  2021. ff_hevc_hls_filter(s, x_ctb, y_ctb);
  2022. return ctb_addr_ts;
  2023. }
  2024. static void restore_tqb_pixels(HEVCContext *s)
  2025. {
  2026. int min_pu_size = 1 << s->ps.sps->log2_min_pu_size;
  2027. int x, y, c_idx;
  2028. for (c_idx = 0; c_idx < 3; c_idx++) {
  2029. ptrdiff_t stride = s->frame->linesize[c_idx];
  2030. int hshift = s->ps.sps->hshift[c_idx];
  2031. int vshift = s->ps.sps->vshift[c_idx];
  2032. for (y = 0; y < s->ps.sps->min_pu_height; y++) {
  2033. for (x = 0; x < s->ps.sps->min_pu_width; x++) {
  2034. if (s->is_pcm[y * s->ps.sps->min_pu_width + x]) {
  2035. int n;
  2036. int len = min_pu_size >> hshift;
  2037. uint8_t *src = &s->frame->data[c_idx][((y << s->ps.sps->log2_min_pu_size) >> vshift) * stride + (((x << s->ps.sps->log2_min_pu_size) >> hshift) << s->ps.sps->pixel_shift)];
  2038. uint8_t *dst = &s->sao_frame->data[c_idx][((y << s->ps.sps->log2_min_pu_size) >> vshift) * stride + (((x << s->ps.sps->log2_min_pu_size) >> hshift) << s->ps.sps->pixel_shift)];
  2039. for (n = 0; n < (min_pu_size >> vshift); n++) {
  2040. memcpy(dst, src, len);
  2041. src += stride;
  2042. dst += stride;
  2043. }
  2044. }
  2045. }
  2046. }
  2047. }
  2048. }
  2049. static int set_side_data(HEVCContext *s)
  2050. {
  2051. AVFrame *out = s->ref->frame;
  2052. if (s->sei.frame_packing.present &&
  2053. s->sei.frame_packing.arrangement_type >= 3 &&
  2054. s->sei.frame_packing.arrangement_type <= 5 &&
  2055. s->sei.frame_packing.content_interpretation_type > 0 &&
  2056. s->sei.frame_packing.content_interpretation_type < 3) {
  2057. AVStereo3D *stereo = av_stereo3d_create_side_data(out);
  2058. if (!stereo)
  2059. return AVERROR(ENOMEM);
  2060. switch (s->sei.frame_packing.arrangement_type) {
  2061. case 3:
  2062. if (s->sei.frame_packing.quincunx_subsampling)
  2063. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  2064. else
  2065. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  2066. break;
  2067. case 4:
  2068. stereo->type = AV_STEREO3D_TOPBOTTOM;
  2069. break;
  2070. case 5:
  2071. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  2072. break;
  2073. }
  2074. if (s->sei.frame_packing.content_interpretation_type == 2)
  2075. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  2076. }
  2077. if (s->sei.display_orientation.present &&
  2078. (s->sei.display_orientation.anticlockwise_rotation ||
  2079. s->sei.display_orientation.hflip || s->sei.display_orientation.vflip)) {
  2080. double angle = s->sei.display_orientation.anticlockwise_rotation * 360 / (double) (1 << 16);
  2081. AVFrameSideData *rotation = av_frame_new_side_data(out,
  2082. AV_FRAME_DATA_DISPLAYMATRIX,
  2083. sizeof(int32_t) * 9);
  2084. if (!rotation)
  2085. return AVERROR(ENOMEM);
  2086. av_display_rotation_set((int32_t *)rotation->data, angle);
  2087. av_display_matrix_flip((int32_t *)rotation->data,
  2088. s->sei.display_orientation.hflip,
  2089. s->sei.display_orientation.vflip);
  2090. }
  2091. if (s->sei.alternative_transfer.present &&
  2092. av_color_transfer_name(s->sei.alternative_transfer.preferred_transfer_characteristics) &&
  2093. s->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
  2094. s->avctx->color_trc = out->color_trc = s->sei.alternative_transfer.preferred_transfer_characteristics;
  2095. }
  2096. return 0;
  2097. }
  2098. static int hevc_frame_start(HEVCContext *s)
  2099. {
  2100. HEVCLocalContext *lc = &s->HEVClc;
  2101. int ret;
  2102. memset(s->horizontal_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
  2103. memset(s->vertical_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
  2104. memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
  2105. memset(s->is_pcm, 0, s->ps.sps->min_pu_width * s->ps.sps->min_pu_height);
  2106. lc->start_of_tiles_x = 0;
  2107. s->is_decoded = 0;
  2108. s->first_nal_type = s->nal_unit_type;
  2109. if (s->ps.pps->tiles_enabled_flag)
  2110. lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
  2111. ret = ff_hevc_set_new_ref(s, s->ps.sps->sao_enabled ? &s->sao_frame : &s->frame,
  2112. s->poc);
  2113. if (ret < 0)
  2114. goto fail;
  2115. ret = ff_hevc_frame_rps(s);
  2116. if (ret < 0) {
  2117. av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
  2118. goto fail;
  2119. }
  2120. s->ref->frame->key_frame = IS_IRAP(s);
  2121. ret = set_side_data(s);
  2122. if (ret < 0)
  2123. goto fail;
  2124. av_frame_unref(s->output_frame);
  2125. ret = ff_hevc_output_frame(s, s->output_frame, 0);
  2126. if (ret < 0)
  2127. goto fail;
  2128. ff_thread_finish_setup(s->avctx);
  2129. return 0;
  2130. fail:
  2131. if (s->ref)
  2132. ff_hevc_unref_frame(s, s->ref, ~0);
  2133. s->ref = NULL;
  2134. return ret;
  2135. }
  2136. static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
  2137. {
  2138. HEVCLocalContext *lc = &s->HEVClc;
  2139. GetBitContext *gb = &lc->gb;
  2140. int ctb_addr_ts, ret;
  2141. *gb = nal->gb;
  2142. s->nal_unit_type = nal->type;
  2143. s->temporal_id = nal->temporal_id;
  2144. switch (s->nal_unit_type) {
  2145. case HEVC_NAL_VPS:
  2146. ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
  2147. if (ret < 0)
  2148. goto fail;
  2149. break;
  2150. case HEVC_NAL_SPS:
  2151. ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
  2152. s->apply_defdispwin);
  2153. if (ret < 0)
  2154. goto fail;
  2155. break;
  2156. case HEVC_NAL_PPS:
  2157. ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
  2158. if (ret < 0)
  2159. goto fail;
  2160. break;
  2161. case HEVC_NAL_SEI_PREFIX:
  2162. case HEVC_NAL_SEI_SUFFIX:
  2163. ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei,
  2164. s->nal_unit_type);
  2165. if (ret < 0)
  2166. goto fail;
  2167. break;
  2168. case HEVC_NAL_TRAIL_R:
  2169. case HEVC_NAL_TRAIL_N:
  2170. case HEVC_NAL_TSA_N:
  2171. case HEVC_NAL_TSA_R:
  2172. case HEVC_NAL_STSA_N:
  2173. case HEVC_NAL_STSA_R:
  2174. case HEVC_NAL_BLA_W_LP:
  2175. case HEVC_NAL_BLA_W_RADL:
  2176. case HEVC_NAL_BLA_N_LP:
  2177. case HEVC_NAL_IDR_W_RADL:
  2178. case HEVC_NAL_IDR_N_LP:
  2179. case HEVC_NAL_CRA_NUT:
  2180. case HEVC_NAL_RADL_N:
  2181. case HEVC_NAL_RADL_R:
  2182. case HEVC_NAL_RASL_N:
  2183. case HEVC_NAL_RASL_R:
  2184. ret = hls_slice_header(s);
  2185. if (ret < 0)
  2186. return ret;
  2187. if (s->max_ra == INT_MAX) {
  2188. if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
  2189. s->max_ra = s->poc;
  2190. } else {
  2191. if (IS_IDR(s))
  2192. s->max_ra = INT_MIN;
  2193. }
  2194. }
  2195. if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
  2196. s->poc <= s->max_ra) {
  2197. s->is_decoded = 0;
  2198. break;
  2199. } else {
  2200. if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
  2201. s->max_ra = INT_MIN;
  2202. }
  2203. if (s->sh.first_slice_in_pic_flag) {
  2204. ret = hevc_frame_start(s);
  2205. if (ret < 0)
  2206. return ret;
  2207. } else if (!s->ref) {
  2208. av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
  2209. goto fail;
  2210. }
  2211. if (s->nal_unit_type != s->first_nal_type) {
  2212. av_log(s->avctx, AV_LOG_ERROR,
  2213. "Non-matching NAL types of the VCL NALUs: %d %d\n",
  2214. s->first_nal_type, s->nal_unit_type);
  2215. return AVERROR_INVALIDDATA;
  2216. }
  2217. if (!s->sh.dependent_slice_segment_flag &&
  2218. s->sh.slice_type != HEVC_SLICE_I) {
  2219. ret = ff_hevc_slice_rpl(s);
  2220. if (ret < 0) {
  2221. av_log(s->avctx, AV_LOG_WARNING,
  2222. "Error constructing the reference lists for the current slice.\n");
  2223. goto fail;
  2224. }
  2225. }
  2226. if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
  2227. ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
  2228. if (ret < 0)
  2229. goto fail;
  2230. }
  2231. if (s->avctx->hwaccel) {
  2232. ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
  2233. if (ret < 0)
  2234. goto fail;
  2235. } else {
  2236. ctb_addr_ts = hls_slice_data(s);
  2237. if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
  2238. s->is_decoded = 1;
  2239. if ((s->ps.pps->transquant_bypass_enable_flag ||
  2240. (s->ps.sps->pcm.loop_filter_disable_flag && s->ps.sps->pcm_enabled_flag)) &&
  2241. s->ps.sps->sao_enabled)
  2242. restore_tqb_pixels(s);
  2243. }
  2244. if (ctb_addr_ts < 0) {
  2245. ret = ctb_addr_ts;
  2246. goto fail;
  2247. }
  2248. }
  2249. break;
  2250. case HEVC_NAL_EOS_NUT:
  2251. case HEVC_NAL_EOB_NUT:
  2252. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2253. s->max_ra = INT_MAX;
  2254. break;
  2255. case HEVC_NAL_AUD:
  2256. case HEVC_NAL_FD_NUT:
  2257. break;
  2258. default:
  2259. av_log(s->avctx, AV_LOG_INFO,
  2260. "Skipping NAL unit %d\n", s->nal_unit_type);
  2261. }
  2262. return 0;
  2263. fail:
  2264. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  2265. return ret;
  2266. return 0;
  2267. }
  2268. static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
  2269. {
  2270. int i, ret = 0;
  2271. s->ref = NULL;
  2272. s->eos = 0;
  2273. /* split the input packet into NAL units, so we know the upper bound on the
  2274. * number of slices in the frame */
  2275. ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
  2276. s->nal_length_size, s->avctx->codec_id);
  2277. if (ret < 0) {
  2278. av_log(s->avctx, AV_LOG_ERROR,
  2279. "Error splitting the input into NAL units.\n");
  2280. return ret;
  2281. }
  2282. for (i = 0; i < s->pkt.nb_nals; i++) {
  2283. if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
  2284. s->pkt.nals[i].type == HEVC_NAL_EOS_NUT)
  2285. s->eos = 1;
  2286. }
  2287. /* decode the NAL units */
  2288. for (i = 0; i < s->pkt.nb_nals; i++) {
  2289. ret = decode_nal_unit(s, &s->pkt.nals[i]);
  2290. if (ret < 0) {
  2291. av_log(s->avctx, AV_LOG_WARNING,
  2292. "Error parsing NAL unit #%d.\n", i);
  2293. goto fail;
  2294. }
  2295. }
  2296. fail:
  2297. if (s->ref)
  2298. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  2299. return ret;
  2300. }
  2301. static void print_md5(void *log_ctx, int level, uint8_t md5[16])
  2302. {
  2303. int i;
  2304. for (i = 0; i < 16; i++)
  2305. av_log(log_ctx, level, "%02"PRIx8, md5[i]);
  2306. }
  2307. static int verify_md5(HEVCContext *s, AVFrame *frame)
  2308. {
  2309. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  2310. int pixel_shift;
  2311. int i, j;
  2312. if (!desc)
  2313. return AVERROR(EINVAL);
  2314. pixel_shift = desc->comp[0].depth > 8;
  2315. av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
  2316. s->poc);
  2317. /* the checksums are LE, so we have to byteswap for >8bpp formats
  2318. * on BE arches */
  2319. #if HAVE_BIGENDIAN
  2320. if (pixel_shift && !s->checksum_buf) {
  2321. av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
  2322. FFMAX3(frame->linesize[0], frame->linesize[1],
  2323. frame->linesize[2]));
  2324. if (!s->checksum_buf)
  2325. return AVERROR(ENOMEM);
  2326. }
  2327. #endif
  2328. for (i = 0; frame->data[i]; i++) {
  2329. int width = s->avctx->coded_width;
  2330. int height = s->avctx->coded_height;
  2331. int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
  2332. int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
  2333. uint8_t md5[16];
  2334. av_md5_init(s->md5_ctx);
  2335. for (j = 0; j < h; j++) {
  2336. const uint8_t *src = frame->data[i] + j * frame->linesize[i];
  2337. #if HAVE_BIGENDIAN
  2338. if (pixel_shift) {
  2339. s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
  2340. (const uint16_t *) src, w);
  2341. src = s->checksum_buf;
  2342. }
  2343. #endif
  2344. av_md5_update(s->md5_ctx, src, w << pixel_shift);
  2345. }
  2346. av_md5_final(s->md5_ctx, md5);
  2347. if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
  2348. av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
  2349. print_md5(s->avctx, AV_LOG_DEBUG, md5);
  2350. av_log (s->avctx, AV_LOG_DEBUG, "; ");
  2351. } else {
  2352. av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
  2353. print_md5(s->avctx, AV_LOG_ERROR, md5);
  2354. av_log (s->avctx, AV_LOG_ERROR, " != ");
  2355. print_md5(s->avctx, AV_LOG_ERROR, s->sei.picture_hash.md5[i]);
  2356. av_log (s->avctx, AV_LOG_ERROR, "\n");
  2357. return AVERROR_INVALIDDATA;
  2358. }
  2359. }
  2360. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2361. return 0;
  2362. }
  2363. static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length)
  2364. {
  2365. AVCodecContext *avctx = s->avctx;
  2366. GetByteContext gb;
  2367. int ret, i;
  2368. bytestream2_init(&gb, buf, length);
  2369. if (length > 3 && (buf[0] || buf[1] || buf[2] > 1)) {
  2370. /* It seems the extradata is encoded as hvcC format.
  2371. * Temporarily, we support configurationVersion==0 until 14496-15 3rd
  2372. * is finalized. When finalized, configurationVersion will be 1 and we
  2373. * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
  2374. int i, j, num_arrays, nal_len_size;
  2375. s->is_nalff = 1;
  2376. bytestream2_skip(&gb, 21);
  2377. nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
  2378. num_arrays = bytestream2_get_byte(&gb);
  2379. /* nal units in the hvcC always have length coded with 2 bytes,
  2380. * so put a fake nal_length_size = 2 while parsing them */
  2381. s->nal_length_size = 2;
  2382. /* Decode nal units from hvcC. */
  2383. for (i = 0; i < num_arrays; i++) {
  2384. int type = bytestream2_get_byte(&gb) & 0x3f;
  2385. int cnt = bytestream2_get_be16(&gb);
  2386. for (j = 0; j < cnt; j++) {
  2387. // +2 for the nal size field
  2388. int nalsize = bytestream2_peek_be16(&gb) + 2;
  2389. if (bytestream2_get_bytes_left(&gb) < nalsize) {
  2390. av_log(s->avctx, AV_LOG_ERROR,
  2391. "Invalid NAL unit size in extradata.\n");
  2392. return AVERROR_INVALIDDATA;
  2393. }
  2394. ret = decode_nal_units(s, gb.buffer, nalsize);
  2395. if (ret < 0) {
  2396. av_log(avctx, AV_LOG_ERROR,
  2397. "Decoding nal unit %d %d from hvcC failed\n",
  2398. type, i);
  2399. return ret;
  2400. }
  2401. bytestream2_skip(&gb, nalsize);
  2402. }
  2403. }
  2404. /* Now store right nal length size, that will be used to parse
  2405. * all other nals */
  2406. s->nal_length_size = nal_len_size;
  2407. } else {
  2408. s->is_nalff = 0;
  2409. ret = decode_nal_units(s, buf, length);
  2410. if (ret < 0)
  2411. return ret;
  2412. }
  2413. /* export stream parameters from the first SPS */
  2414. for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
  2415. if (s->ps.sps_list[i]) {
  2416. const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
  2417. export_stream_params(s->avctx, &s->ps, sps);
  2418. break;
  2419. }
  2420. }
  2421. return 0;
  2422. }
  2423. static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
  2424. AVPacket *avpkt)
  2425. {
  2426. int ret;
  2427. int new_extradata_size;
  2428. uint8_t *new_extradata;
  2429. HEVCContext *s = avctx->priv_data;
  2430. if (!avpkt->size) {
  2431. ret = ff_hevc_output_frame(s, data, 1);
  2432. if (ret < 0)
  2433. return ret;
  2434. *got_output = ret;
  2435. return 0;
  2436. }
  2437. new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
  2438. &new_extradata_size);
  2439. if (new_extradata && new_extradata_size > 0) {
  2440. ret = hevc_decode_extradata(s, new_extradata, new_extradata_size);
  2441. if (ret < 0)
  2442. return ret;
  2443. }
  2444. s->ref = NULL;
  2445. ret = decode_nal_units(s, avpkt->data, avpkt->size);
  2446. if (ret < 0)
  2447. return ret;
  2448. if (avctx->hwaccel) {
  2449. if (s->ref && avctx->hwaccel->end_frame(avctx) < 0)
  2450. av_log(avctx, AV_LOG_ERROR,
  2451. "hardware accelerator failed to decode picture\n");
  2452. } else {
  2453. /* verify the SEI checksum */
  2454. if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
  2455. s->sei.picture_hash.is_md5) {
  2456. ret = verify_md5(s, s->ref->frame);
  2457. if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
  2458. ff_hevc_unref_frame(s, s->ref, ~0);
  2459. return ret;
  2460. }
  2461. }
  2462. }
  2463. s->sei.picture_hash.is_md5 = 0;
  2464. if (s->is_decoded) {
  2465. av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
  2466. s->is_decoded = 0;
  2467. }
  2468. if (s->output_frame->buf[0]) {
  2469. av_frame_move_ref(data, s->output_frame);
  2470. *got_output = 1;
  2471. }
  2472. return avpkt->size;
  2473. }
  2474. static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
  2475. {
  2476. int ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  2477. if (ret < 0)
  2478. return ret;
  2479. dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
  2480. if (!dst->tab_mvf_buf)
  2481. goto fail;
  2482. dst->tab_mvf = src->tab_mvf;
  2483. dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
  2484. if (!dst->rpl_tab_buf)
  2485. goto fail;
  2486. dst->rpl_tab = src->rpl_tab;
  2487. dst->rpl_buf = av_buffer_ref(src->rpl_buf);
  2488. if (!dst->rpl_buf)
  2489. goto fail;
  2490. dst->poc = src->poc;
  2491. dst->ctb_count = src->ctb_count;
  2492. dst->flags = src->flags;
  2493. dst->sequence = src->sequence;
  2494. if (src->hwaccel_picture_private) {
  2495. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  2496. if (!dst->hwaccel_priv_buf)
  2497. goto fail;
  2498. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  2499. }
  2500. return 0;
  2501. fail:
  2502. ff_hevc_unref_frame(s, dst, ~0);
  2503. return AVERROR(ENOMEM);
  2504. }
  2505. static av_cold int hevc_decode_free(AVCodecContext *avctx)
  2506. {
  2507. HEVCContext *s = avctx->priv_data;
  2508. int i;
  2509. pic_arrays_free(s);
  2510. av_freep(&s->md5_ctx);
  2511. av_frame_free(&s->tmp_frame);
  2512. av_frame_free(&s->output_frame);
  2513. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2514. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2515. av_frame_free(&s->DPB[i].frame);
  2516. }
  2517. for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
  2518. av_buffer_unref(&s->ps.vps_list[i]);
  2519. for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
  2520. av_buffer_unref(&s->ps.sps_list[i]);
  2521. for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
  2522. av_buffer_unref(&s->ps.pps_list[i]);
  2523. ff_h2645_packet_uninit(&s->pkt);
  2524. return 0;
  2525. }
  2526. static av_cold int hevc_init_context(AVCodecContext *avctx)
  2527. {
  2528. HEVCContext *s = avctx->priv_data;
  2529. int i;
  2530. s->avctx = avctx;
  2531. s->tmp_frame = av_frame_alloc();
  2532. if (!s->tmp_frame)
  2533. goto fail;
  2534. s->output_frame = av_frame_alloc();
  2535. if (!s->output_frame)
  2536. goto fail;
  2537. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2538. s->DPB[i].frame = av_frame_alloc();
  2539. if (!s->DPB[i].frame)
  2540. goto fail;
  2541. s->DPB[i].tf.f = s->DPB[i].frame;
  2542. }
  2543. s->max_ra = INT_MAX;
  2544. s->md5_ctx = av_md5_alloc();
  2545. if (!s->md5_ctx)
  2546. goto fail;
  2547. ff_bswapdsp_init(&s->bdsp);
  2548. s->context_initialized = 1;
  2549. return 0;
  2550. fail:
  2551. hevc_decode_free(avctx);
  2552. return AVERROR(ENOMEM);
  2553. }
  2554. static int hevc_update_thread_context(AVCodecContext *dst,
  2555. const AVCodecContext *src)
  2556. {
  2557. HEVCContext *s = dst->priv_data;
  2558. HEVCContext *s0 = src->priv_data;
  2559. int i, ret;
  2560. if (!s->context_initialized) {
  2561. ret = hevc_init_context(dst);
  2562. if (ret < 0)
  2563. return ret;
  2564. }
  2565. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2566. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2567. if (s0->DPB[i].frame->buf[0]) {
  2568. ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
  2569. if (ret < 0)
  2570. return ret;
  2571. }
  2572. }
  2573. for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
  2574. av_buffer_unref(&s->ps.vps_list[i]);
  2575. if (s0->ps.vps_list[i]) {
  2576. s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
  2577. if (!s->ps.vps_list[i])
  2578. return AVERROR(ENOMEM);
  2579. }
  2580. }
  2581. for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
  2582. av_buffer_unref(&s->ps.sps_list[i]);
  2583. if (s0->ps.sps_list[i]) {
  2584. s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
  2585. if (!s->ps.sps_list[i])
  2586. return AVERROR(ENOMEM);
  2587. }
  2588. }
  2589. for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
  2590. av_buffer_unref(&s->ps.pps_list[i]);
  2591. if (s0->ps.pps_list[i]) {
  2592. s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
  2593. if (!s->ps.pps_list[i])
  2594. return AVERROR(ENOMEM);
  2595. }
  2596. }
  2597. if (s->ps.sps != s0->ps.sps)
  2598. ret = set_sps(s, s0->ps.sps, src->pix_fmt);
  2599. s->seq_decode = s0->seq_decode;
  2600. s->seq_output = s0->seq_output;
  2601. s->pocTid0 = s0->pocTid0;
  2602. s->max_ra = s0->max_ra;
  2603. s->is_nalff = s0->is_nalff;
  2604. s->nal_length_size = s0->nal_length_size;
  2605. if (s0->eos) {
  2606. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2607. s->max_ra = INT_MAX;
  2608. }
  2609. return 0;
  2610. }
  2611. static av_cold int hevc_decode_init(AVCodecContext *avctx)
  2612. {
  2613. HEVCContext *s = avctx->priv_data;
  2614. int ret;
  2615. avctx->internal->allocate_progress = 1;
  2616. ret = hevc_init_context(avctx);
  2617. if (ret < 0)
  2618. return ret;
  2619. if (avctx->extradata_size > 0 && avctx->extradata) {
  2620. ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size);
  2621. if (ret < 0) {
  2622. hevc_decode_free(avctx);
  2623. return ret;
  2624. }
  2625. }
  2626. return 0;
  2627. }
  2628. static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
  2629. {
  2630. HEVCContext *s = avctx->priv_data;
  2631. int ret;
  2632. memset(s, 0, sizeof(*s));
  2633. ret = hevc_init_context(avctx);
  2634. if (ret < 0)
  2635. return ret;
  2636. return 0;
  2637. }
  2638. static void hevc_decode_flush(AVCodecContext *avctx)
  2639. {
  2640. HEVCContext *s = avctx->priv_data;
  2641. ff_hevc_flush_dpb(s);
  2642. s->max_ra = INT_MAX;
  2643. }
  2644. #define OFFSET(x) offsetof(HEVCContext, x)
  2645. #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  2646. static const AVOption options[] = {
  2647. { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
  2648. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
  2649. { NULL },
  2650. };
  2651. static const AVClass hevc_decoder_class = {
  2652. .class_name = "HEVC decoder",
  2653. .item_name = av_default_item_name,
  2654. .option = options,
  2655. .version = LIBAVUTIL_VERSION_INT,
  2656. };
  2657. AVCodec ff_hevc_decoder = {
  2658. .name = "hevc",
  2659. .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
  2660. .type = AVMEDIA_TYPE_VIDEO,
  2661. .id = AV_CODEC_ID_HEVC,
  2662. .priv_data_size = sizeof(HEVCContext),
  2663. .priv_class = &hevc_decoder_class,
  2664. .init = hevc_decode_init,
  2665. .close = hevc_decode_free,
  2666. .decode = hevc_decode_frame,
  2667. .flush = hevc_decode_flush,
  2668. .update_thread_context = hevc_update_thread_context,
  2669. .init_thread_copy = hevc_init_thread_copy,
  2670. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
  2671. AV_CODEC_CAP_FRAME_THREADS,
  2672. .profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
  2673. .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING | FF_CODEC_CAP_INIT_THREADSAFE,
  2674. };