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.

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