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.

3413 lines
132KB

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