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.

3356 lines
129KB

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