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.

3684 lines
142KB

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