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.

3070 lines
116KB

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