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.

3296 lines
120KB

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