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.

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