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.

3099 lines
117KB

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