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.

3119 lines
116KB

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