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.

3067 lines
115KB

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