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.

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