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.

3278 lines
119KB

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