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.

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