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.

3436 lines
130KB

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