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.

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