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.

3599 lines
139KB

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