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.

3600 lines
140KB

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