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.

3294 lines
121KB

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