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.

3113 lines
115KB

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