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.

3431 lines
133KB

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