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.

3333 lines
128KB

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