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.

3268 lines
120KB

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