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.

3328 lines
128KB

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