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.

3482 lines
135KB

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