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.

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