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.

3074 lines
116KB

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