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.

3274 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/internal.h"
  28. #include "libavutil/md5.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/stereo3d.h"
  32. #include "bytestream.h"
  33. #include "cabac_functions.h"
  34. #include "dsputil.h"
  35. #include "golomb.h"
  36. #include "hevc.h"
  37. const uint8_t ff_hevc_qpel_extra_before[4] = { 0, 3, 3, 2 };
  38. const uint8_t ff_hevc_qpel_extra_after[4] = { 0, 3, 4, 4 };
  39. const uint8_t ff_hevc_qpel_extra[4] = { 0, 6, 7, 6 };
  40. static const uint8_t scan_1x1[1] = { 0 };
  41. static const uint8_t horiz_scan2x2_x[4] = { 0, 1, 0, 1 };
  42. static const uint8_t horiz_scan2x2_y[4] = { 0, 0, 1, 1 };
  43. static const uint8_t horiz_scan4x4_x[16] = {
  44. 0, 1, 2, 3,
  45. 0, 1, 2, 3,
  46. 0, 1, 2, 3,
  47. 0, 1, 2, 3,
  48. };
  49. static const uint8_t horiz_scan4x4_y[16] = {
  50. 0, 0, 0, 0,
  51. 1, 1, 1, 1,
  52. 2, 2, 2, 2,
  53. 3, 3, 3, 3,
  54. };
  55. static const uint8_t horiz_scan8x8_inv[8][8] = {
  56. { 0, 1, 2, 3, 16, 17, 18, 19, },
  57. { 4, 5, 6, 7, 20, 21, 22, 23, },
  58. { 8, 9, 10, 11, 24, 25, 26, 27, },
  59. { 12, 13, 14, 15, 28, 29, 30, 31, },
  60. { 32, 33, 34, 35, 48, 49, 50, 51, },
  61. { 36, 37, 38, 39, 52, 53, 54, 55, },
  62. { 40, 41, 42, 43, 56, 57, 58, 59, },
  63. { 44, 45, 46, 47, 60, 61, 62, 63, },
  64. };
  65. static const uint8_t diag_scan2x2_x[4] = { 0, 0, 1, 1 };
  66. static const uint8_t diag_scan2x2_y[4] = { 0, 1, 0, 1 };
  67. static const uint8_t diag_scan2x2_inv[2][2] = {
  68. { 0, 2, },
  69. { 1, 3, },
  70. };
  71. const uint8_t ff_hevc_diag_scan4x4_x[16] = {
  72. 0, 0, 1, 0,
  73. 1, 2, 0, 1,
  74. 2, 3, 1, 2,
  75. 3, 2, 3, 3,
  76. };
  77. const uint8_t ff_hevc_diag_scan4x4_y[16] = {
  78. 0, 1, 0, 2,
  79. 1, 0, 3, 2,
  80. 1, 0, 3, 2,
  81. 1, 3, 2, 3,
  82. };
  83. static const uint8_t diag_scan4x4_inv[4][4] = {
  84. { 0, 2, 5, 9, },
  85. { 1, 4, 8, 12, },
  86. { 3, 7, 11, 14, },
  87. { 6, 10, 13, 15, },
  88. };
  89. const uint8_t ff_hevc_diag_scan8x8_x[64] = {
  90. 0, 0, 1, 0,
  91. 1, 2, 0, 1,
  92. 2, 3, 0, 1,
  93. 2, 3, 4, 0,
  94. 1, 2, 3, 4,
  95. 5, 0, 1, 2,
  96. 3, 4, 5, 6,
  97. 0, 1, 2, 3,
  98. 4, 5, 6, 7,
  99. 1, 2, 3, 4,
  100. 5, 6, 7, 2,
  101. 3, 4, 5, 6,
  102. 7, 3, 4, 5,
  103. 6, 7, 4, 5,
  104. 6, 7, 5, 6,
  105. 7, 6, 7, 7,
  106. };
  107. const uint8_t ff_hevc_diag_scan8x8_y[64] = {
  108. 0, 1, 0, 2,
  109. 1, 0, 3, 2,
  110. 1, 0, 4, 3,
  111. 2, 1, 0, 5,
  112. 4, 3, 2, 1,
  113. 0, 6, 5, 4,
  114. 3, 2, 1, 0,
  115. 7, 6, 5, 4,
  116. 3, 2, 1, 0,
  117. 7, 6, 5, 4,
  118. 3, 2, 1, 7,
  119. 6, 5, 4, 3,
  120. 2, 7, 6, 5,
  121. 4, 3, 7, 6,
  122. 5, 4, 7, 6,
  123. 5, 7, 6, 7,
  124. };
  125. static const uint8_t diag_scan8x8_inv[8][8] = {
  126. { 0, 2, 5, 9, 14, 20, 27, 35, },
  127. { 1, 4, 8, 13, 19, 26, 34, 42, },
  128. { 3, 7, 12, 18, 25, 33, 41, 48, },
  129. { 6, 11, 17, 24, 32, 40, 47, 53, },
  130. { 10, 16, 23, 31, 39, 46, 52, 57, },
  131. { 15, 22, 30, 38, 45, 51, 56, 60, },
  132. { 21, 29, 37, 44, 50, 55, 59, 62, },
  133. { 28, 36, 43, 49, 54, 58, 61, 63, },
  134. };
  135. /**
  136. * NOTE: Each function hls_foo correspond to the function foo in the
  137. * specification (HLS stands for High Level Syntax).
  138. */
  139. /**
  140. * Section 5.7
  141. */
  142. /* free everything allocated by pic_arrays_init() */
  143. static void pic_arrays_free(HEVCContext *s)
  144. {
  145. av_freep(&s->sao);
  146. av_freep(&s->deblock);
  147. av_freep(&s->split_cu_flag);
  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 = width * height;
  168. int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
  169. ((height >> log2_min_cb_size) + 1);
  170. int ctb_count = sps->ctb_width * sps->ctb_height;
  171. int min_pu_size = sps->min_pu_width * sps->min_pu_height;
  172. s->bs_width = width >> 3;
  173. s->bs_height = height >> 3;
  174. s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
  175. s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
  176. s->split_cu_flag = av_malloc(pic_size);
  177. if (!s->sao || !s->deblock || !s->split_cu_flag)
  178. goto fail;
  179. s->skip_flag = av_malloc(pic_size_in_ctb);
  180. s->tab_ct_depth = av_malloc(sps->min_cb_height * sps->min_cb_width);
  181. if (!s->skip_flag || !s->tab_ct_depth)
  182. goto fail;
  183. s->cbf_luma = av_malloc(sps->min_tb_width * sps->min_tb_height);
  184. s->tab_ipm = av_mallocz(min_pu_size);
  185. s->is_pcm = av_malloc(min_pu_size);
  186. if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
  187. goto fail;
  188. s->filter_slice_edges = av_malloc(ctb_count);
  189. s->tab_slice_address = av_malloc(pic_size_in_ctb *
  190. sizeof(*s->tab_slice_address));
  191. s->qp_y_tab = av_malloc(pic_size_in_ctb *
  192. sizeof(*s->qp_y_tab));
  193. if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
  194. goto fail;
  195. s->horizontal_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
  196. s->vertical_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
  197. if (!s->horizontal_bs || !s->vertical_bs)
  198. goto fail;
  199. s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
  200. av_buffer_alloc);
  201. s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
  202. av_buffer_allocz);
  203. if (!s->tab_mvf_pool || !s->rpl_tab_pool)
  204. goto fail;
  205. return 0;
  206. fail:
  207. pic_arrays_free(s);
  208. return AVERROR(ENOMEM);
  209. }
  210. static void pred_weight_table(HEVCContext *s, GetBitContext *gb)
  211. {
  212. int i = 0;
  213. int j = 0;
  214. uint8_t luma_weight_l0_flag[16];
  215. uint8_t chroma_weight_l0_flag[16];
  216. uint8_t luma_weight_l1_flag[16];
  217. uint8_t chroma_weight_l1_flag[16];
  218. s->sh.luma_log2_weight_denom = get_ue_golomb_long(gb);
  219. if (s->sps->chroma_format_idc != 0) {
  220. int delta = get_se_golomb(gb);
  221. s->sh.chroma_log2_weight_denom = av_clip_c(s->sh.luma_log2_weight_denom + delta, 0, 7);
  222. }
  223. for (i = 0; i < s->sh.nb_refs[L0]; i++) {
  224. luma_weight_l0_flag[i] = get_bits1(gb);
  225. if (!luma_weight_l0_flag[i]) {
  226. s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
  227. s->sh.luma_offset_l0[i] = 0;
  228. }
  229. }
  230. if (s->sps->chroma_format_idc != 0) { // FIXME: invert "if" and "for"
  231. for (i = 0; i < s->sh.nb_refs[L0]; i++)
  232. chroma_weight_l0_flag[i] = get_bits1(gb);
  233. } else {
  234. for (i = 0; i < s->sh.nb_refs[L0]; i++)
  235. chroma_weight_l0_flag[i] = 0;
  236. }
  237. for (i = 0; i < s->sh.nb_refs[L0]; i++) {
  238. if (luma_weight_l0_flag[i]) {
  239. int delta_luma_weight_l0 = get_se_golomb(gb);
  240. s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
  241. s->sh.luma_offset_l0[i] = get_se_golomb(gb);
  242. }
  243. if (chroma_weight_l0_flag[i]) {
  244. for (j = 0; j < 2; j++) {
  245. int delta_chroma_weight_l0 = get_se_golomb(gb);
  246. int delta_chroma_offset_l0 = get_se_golomb(gb);
  247. s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
  248. s->sh.chroma_offset_l0[i][j] = av_clip_c((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
  249. >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
  250. }
  251. } else {
  252. s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
  253. s->sh.chroma_offset_l0[i][0] = 0;
  254. s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
  255. s->sh.chroma_offset_l0[i][1] = 0;
  256. }
  257. }
  258. if (s->sh.slice_type == B_SLICE) {
  259. for (i = 0; i < s->sh.nb_refs[L1]; i++) {
  260. luma_weight_l1_flag[i] = get_bits1(gb);
  261. if (!luma_weight_l1_flag[i]) {
  262. s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
  263. s->sh.luma_offset_l1[i] = 0;
  264. }
  265. }
  266. if (s->sps->chroma_format_idc != 0) {
  267. for (i = 0; i < s->sh.nb_refs[L1]; i++)
  268. chroma_weight_l1_flag[i] = get_bits1(gb);
  269. } else {
  270. for (i = 0; i < s->sh.nb_refs[L1]; i++)
  271. chroma_weight_l1_flag[i] = 0;
  272. }
  273. for (i = 0; i < s->sh.nb_refs[L1]; i++) {
  274. if (luma_weight_l1_flag[i]) {
  275. int delta_luma_weight_l1 = get_se_golomb(gb);
  276. s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
  277. s->sh.luma_offset_l1[i] = get_se_golomb(gb);
  278. }
  279. if (chroma_weight_l1_flag[i]) {
  280. for (j = 0; j < 2; j++) {
  281. int delta_chroma_weight_l1 = get_se_golomb(gb);
  282. int delta_chroma_offset_l1 = get_se_golomb(gb);
  283. s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
  284. s->sh.chroma_offset_l1[i][j] = av_clip_c((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
  285. >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
  286. }
  287. } else {
  288. s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
  289. s->sh.chroma_offset_l1[i][0] = 0;
  290. s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
  291. s->sh.chroma_offset_l1[i][1] = 0;
  292. }
  293. }
  294. }
  295. }
  296. static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
  297. {
  298. const HEVCSPS *sps = s->sps;
  299. int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
  300. int prev_delta_msb = 0;
  301. unsigned int nb_sps = 0, nb_sh;
  302. int i;
  303. rps->nb_refs = 0;
  304. if (!sps->long_term_ref_pics_present_flag)
  305. return 0;
  306. if (sps->num_long_term_ref_pics_sps > 0)
  307. nb_sps = get_ue_golomb_long(gb);
  308. nb_sh = get_ue_golomb_long(gb);
  309. if (nb_sh + nb_sps > FF_ARRAY_ELEMS(rps->poc))
  310. return AVERROR_INVALIDDATA;
  311. rps->nb_refs = nb_sh + nb_sps;
  312. for (i = 0; i < rps->nb_refs; i++) {
  313. uint8_t delta_poc_msb_present;
  314. if (i < nb_sps) {
  315. uint8_t lt_idx_sps = 0;
  316. if (sps->num_long_term_ref_pics_sps > 1)
  317. lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
  318. rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
  319. rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
  320. } else {
  321. rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
  322. rps->used[i] = get_bits1(gb);
  323. }
  324. delta_poc_msb_present = get_bits1(gb);
  325. if (delta_poc_msb_present) {
  326. int delta = get_ue_golomb_long(gb);
  327. if (i && i != nb_sps)
  328. delta += prev_delta_msb;
  329. rps->poc[i] += s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
  330. prev_delta_msb = delta;
  331. }
  332. }
  333. return 0;
  334. }
  335. static int set_sps(HEVCContext *s, const HEVCSPS *sps)
  336. {
  337. int ret;
  338. unsigned int num = 0, den = 0;
  339. pic_arrays_free(s);
  340. ret = pic_arrays_init(s, sps);
  341. if (ret < 0)
  342. goto fail;
  343. s->avctx->coded_width = sps->width;
  344. s->avctx->coded_height = sps->height;
  345. s->avctx->width = sps->output_width;
  346. s->avctx->height = sps->output_height;
  347. s->avctx->pix_fmt = sps->pix_fmt;
  348. s->avctx->sample_aspect_ratio = sps->vui.sar;
  349. s->avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
  350. if (sps->vui.video_signal_type_present_flag)
  351. s->avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
  352. : AVCOL_RANGE_MPEG;
  353. else
  354. s->avctx->color_range = AVCOL_RANGE_MPEG;
  355. if (sps->vui.colour_description_present_flag) {
  356. s->avctx->color_primaries = sps->vui.colour_primaries;
  357. s->avctx->color_trc = sps->vui.transfer_characteristic;
  358. s->avctx->colorspace = sps->vui.matrix_coeffs;
  359. } else {
  360. s->avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
  361. s->avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
  362. s->avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
  363. }
  364. ff_hevc_pred_init(&s->hpc, sps->bit_depth);
  365. ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
  366. ff_videodsp_init (&s->vdsp, sps->bit_depth);
  367. if (sps->sao_enabled) {
  368. av_frame_unref(s->tmp_frame);
  369. ret = ff_get_buffer(s->avctx, s->tmp_frame, AV_GET_BUFFER_FLAG_REF);
  370. if (ret < 0)
  371. goto fail;
  372. s->frame = s->tmp_frame;
  373. }
  374. s->sps = sps;
  375. s->vps = (HEVCVPS*) s->vps_list[s->sps->vps_id]->data;
  376. if (s->vps->vps_timing_info_present_flag) {
  377. num = s->vps->vps_num_units_in_tick;
  378. den = s->vps->vps_time_scale;
  379. } else if (sps->vui.vui_timing_info_present_flag) {
  380. num = sps->vui.vui_num_units_in_tick;
  381. den = sps->vui.vui_time_scale;
  382. }
  383. if (num != 0 && den != 0)
  384. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  385. num, den, 1 << 30);
  386. return 0;
  387. fail:
  388. pic_arrays_free(s);
  389. s->sps = NULL;
  390. return ret;
  391. }
  392. static int hls_slice_header(HEVCContext *s)
  393. {
  394. GetBitContext *gb = &s->HEVClc.gb;
  395. SliceHeader *sh = &s->sh;
  396. int i, ret;
  397. // Coded parameters
  398. sh->first_slice_in_pic_flag = get_bits1(gb);
  399. if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
  400. s->seq_decode = (s->seq_decode + 1) & 0xff;
  401. s->max_ra = INT_MAX;
  402. if (IS_IDR(s))
  403. ff_hevc_clear_refs(s);
  404. }
  405. if (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
  406. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  407. sh->pps_id = get_ue_golomb_long(gb);
  408. if (sh->pps_id >= MAX_PPS_COUNT || !s->pps_list[sh->pps_id]) {
  409. av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  410. return AVERROR_INVALIDDATA;
  411. }
  412. if (!sh->first_slice_in_pic_flag &&
  413. s->pps != (HEVCPPS*)s->pps_list[sh->pps_id]->data) {
  414. av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
  415. return AVERROR_INVALIDDATA;
  416. }
  417. s->pps = (HEVCPPS*)s->pps_list[sh->pps_id]->data;
  418. if (s->sps != (HEVCSPS*)s->sps_list[s->pps->sps_id]->data) {
  419. s->sps = (HEVCSPS*)s->sps_list[s->pps->sps_id]->data;
  420. ff_hevc_clear_refs(s);
  421. ret = set_sps(s, s->sps);
  422. if (ret < 0)
  423. return ret;
  424. s->seq_decode = (s->seq_decode + 1) & 0xff;
  425. s->max_ra = INT_MAX;
  426. }
  427. s->avctx->profile = s->sps->ptl.general_ptl.profile_idc;
  428. s->avctx->level = s->sps->ptl.general_ptl.level_idc;
  429. sh->dependent_slice_segment_flag = 0;
  430. if (!sh->first_slice_in_pic_flag) {
  431. int slice_address_length;
  432. if (s->pps->dependent_slice_segments_enabled_flag)
  433. sh->dependent_slice_segment_flag = get_bits1(gb);
  434. slice_address_length = av_ceil_log2(s->sps->ctb_width *
  435. s->sps->ctb_height);
  436. sh->slice_segment_addr = get_bits(gb, slice_address_length);
  437. if (sh->slice_segment_addr >= s->sps->ctb_width * s->sps->ctb_height) {
  438. av_log(s->avctx, AV_LOG_ERROR,
  439. "Invalid slice segment address: %u.\n",
  440. sh->slice_segment_addr);
  441. return AVERROR_INVALIDDATA;
  442. }
  443. if (!sh->dependent_slice_segment_flag) {
  444. sh->slice_addr = sh->slice_segment_addr;
  445. s->slice_idx++;
  446. }
  447. } else {
  448. sh->slice_segment_addr = sh->slice_addr = 0;
  449. s->slice_idx = 0;
  450. s->slice_initialized = 0;
  451. }
  452. if (!sh->dependent_slice_segment_flag) {
  453. s->slice_initialized = 0;
  454. for (i = 0; i < s->pps->num_extra_slice_header_bits; i++)
  455. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  456. sh->slice_type = get_ue_golomb_long(gb);
  457. if (!(sh->slice_type == I_SLICE ||
  458. sh->slice_type == P_SLICE ||
  459. sh->slice_type == B_SLICE)) {
  460. av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  461. sh->slice_type);
  462. return AVERROR_INVALIDDATA;
  463. }
  464. if (IS_IRAP(s) && sh->slice_type != I_SLICE) {
  465. av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
  466. return AVERROR_INVALIDDATA;
  467. }
  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. {
  1062. HEVCLocalContext *lc = &s->HEVClc;
  1063. if (lc->cu.pred_mode == MODE_INTRA) {
  1064. int trafo_size = 1 << log2_trafo_size;
  1065. ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
  1066. s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
  1067. if (log2_trafo_size > 2) {
  1068. trafo_size = trafo_size << (s->sps->hshift[1] - 1);
  1069. ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
  1070. s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 1);
  1071. s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 2);
  1072. } else if (blk_idx == 3) {
  1073. trafo_size = trafo_size << s->sps->hshift[1];
  1074. ff_hevc_set_neighbour_available(s, xBase, yBase,
  1075. trafo_size, trafo_size);
  1076. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
  1077. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
  1078. }
  1079. }
  1080. if (lc->tt.cbf_luma ||
  1081. SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
  1082. SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
  1083. int scan_idx = SCAN_DIAG;
  1084. int scan_idx_c = SCAN_DIAG;
  1085. if (s->pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
  1086. lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
  1087. if (lc->tu.cu_qp_delta != 0)
  1088. if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
  1089. lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
  1090. lc->tu.is_cu_qp_delta_coded = 1;
  1091. if (lc->tu.cu_qp_delta < -(26 + s->sps->qp_bd_offset / 2) ||
  1092. lc->tu.cu_qp_delta > (25 + s->sps->qp_bd_offset / 2)) {
  1093. av_log(s->avctx, AV_LOG_ERROR,
  1094. "The cu_qp_delta %d is outside the valid range "
  1095. "[%d, %d].\n",
  1096. lc->tu.cu_qp_delta,
  1097. -(26 + s->sps->qp_bd_offset / 2),
  1098. (25 + s->sps->qp_bd_offset / 2));
  1099. return AVERROR_INVALIDDATA;
  1100. }
  1101. ff_hevc_set_qPy(s, x0, y0, cb_xBase, cb_yBase, log2_cb_size);
  1102. }
  1103. if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
  1104. if (lc->tu.cur_intra_pred_mode >= 6 &&
  1105. lc->tu.cur_intra_pred_mode <= 14) {
  1106. scan_idx = SCAN_VERT;
  1107. } else if (lc->tu.cur_intra_pred_mode >= 22 &&
  1108. lc->tu.cur_intra_pred_mode <= 30) {
  1109. scan_idx = SCAN_HORIZ;
  1110. }
  1111. if (lc->pu.intra_pred_mode_c >= 6 &&
  1112. lc->pu.intra_pred_mode_c <= 14) {
  1113. scan_idx_c = SCAN_VERT;
  1114. } else if (lc->pu.intra_pred_mode_c >= 22 &&
  1115. lc->pu.intra_pred_mode_c <= 30) {
  1116. scan_idx_c = SCAN_HORIZ;
  1117. }
  1118. }
  1119. if (lc->tt.cbf_luma)
  1120. hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
  1121. if (log2_trafo_size > 2) {
  1122. if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0))
  1123. hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 1);
  1124. if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0))
  1125. hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 2);
  1126. } else if (blk_idx == 3) {
  1127. if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], xBase, yBase))
  1128. hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 1);
  1129. if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], xBase, yBase))
  1130. hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 2);
  1131. }
  1132. }
  1133. return 0;
  1134. }
  1135. static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1136. {
  1137. int cb_size = 1 << log2_cb_size;
  1138. int log2_min_pu_size = s->sps->log2_min_pu_size;
  1139. int min_pu_width = s->sps->min_pu_width;
  1140. int x_end = FFMIN(x0 + cb_size, s->sps->width);
  1141. int y_end = FFMIN(y0 + cb_size, s->sps->height);
  1142. int i, j;
  1143. for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
  1144. for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
  1145. s->is_pcm[i + j * min_pu_width] = 2;
  1146. }
  1147. static int hls_transform_tree(HEVCContext *s, int x0, int y0,
  1148. int xBase, int yBase, int cb_xBase, int cb_yBase,
  1149. int log2_cb_size, int log2_trafo_size,
  1150. int trafo_depth, int blk_idx)
  1151. {
  1152. HEVCLocalContext *lc = &s->HEVClc;
  1153. uint8_t split_transform_flag;
  1154. int ret;
  1155. if (trafo_depth > 0 && log2_trafo_size == 2) {
  1156. SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
  1157. SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase);
  1158. SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
  1159. SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase);
  1160. } else {
  1161. SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
  1162. SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) = 0;
  1163. }
  1164. if (lc->cu.intra_split_flag) {
  1165. if (trafo_depth == 1)
  1166. lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
  1167. } else {
  1168. lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[0];
  1169. }
  1170. lc->tt.cbf_luma = 1;
  1171. lc->tt.inter_split_flag = s->sps->max_transform_hierarchy_depth_inter == 0 &&
  1172. lc->cu.pred_mode == MODE_INTER &&
  1173. lc->cu.part_mode != PART_2Nx2N &&
  1174. trafo_depth == 0;
  1175. if (log2_trafo_size <= s->sps->log2_max_trafo_size &&
  1176. log2_trafo_size > s->sps->log2_min_tb_size &&
  1177. trafo_depth < lc->cu.max_trafo_depth &&
  1178. !(lc->cu.intra_split_flag && trafo_depth == 0)) {
  1179. split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
  1180. } else {
  1181. split_transform_flag = log2_trafo_size > s->sps->log2_max_trafo_size ||
  1182. (lc->cu.intra_split_flag && trafo_depth == 0) ||
  1183. lc->tt.inter_split_flag;
  1184. }
  1185. if (log2_trafo_size > 2) {
  1186. if (trafo_depth == 0 ||
  1187. SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase)) {
  1188. SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
  1189. ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  1190. }
  1191. if (trafo_depth == 0 ||
  1192. SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase)) {
  1193. SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
  1194. ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  1195. }
  1196. }
  1197. if (split_transform_flag) {
  1198. int x1 = x0 + ((1 << log2_trafo_size) >> 1);
  1199. int y1 = y0 + ((1 << log2_trafo_size) >> 1);
  1200. ret = hls_transform_tree(s, x0, y0, x0, y0, cb_xBase, cb_yBase,
  1201. log2_cb_size, log2_trafo_size - 1,
  1202. trafo_depth + 1, 0);
  1203. if (ret < 0)
  1204. return ret;
  1205. ret = hls_transform_tree(s, x1, y0, x0, y0, cb_xBase, cb_yBase,
  1206. log2_cb_size, log2_trafo_size - 1,
  1207. trafo_depth + 1, 1);
  1208. if (ret < 0)
  1209. return ret;
  1210. ret = hls_transform_tree(s, x0, y1, x0, y0, cb_xBase, cb_yBase,
  1211. log2_cb_size, log2_trafo_size - 1,
  1212. trafo_depth + 1, 2);
  1213. if (ret < 0)
  1214. return ret;
  1215. ret = hls_transform_tree(s, x1, y1, x0, y0, cb_xBase, cb_yBase,
  1216. log2_cb_size, log2_trafo_size - 1,
  1217. trafo_depth + 1, 3);
  1218. if (ret < 0)
  1219. return ret;
  1220. } else {
  1221. int min_tu_size = 1 << s->sps->log2_min_tb_size;
  1222. int log2_min_tu_size = s->sps->log2_min_tb_size;
  1223. int min_tu_width = s->sps->min_tb_width;
  1224. if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
  1225. SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
  1226. SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
  1227. lc->tt.cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
  1228. }
  1229. ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
  1230. log2_cb_size, log2_trafo_size, trafo_depth,
  1231. blk_idx);
  1232. if (ret < 0)
  1233. return ret;
  1234. // TODO: store cbf_luma somewhere else
  1235. if (lc->tt.cbf_luma) {
  1236. int i, j;
  1237. for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
  1238. for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
  1239. int x_tu = (x0 + j) >> log2_min_tu_size;
  1240. int y_tu = (y0 + i) >> log2_min_tu_size;
  1241. s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
  1242. }
  1243. }
  1244. if (!s->sh.disable_deblocking_filter_flag) {
  1245. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size,
  1246. lc->slice_or_tiles_up_boundary,
  1247. lc->slice_or_tiles_left_boundary);
  1248. if (s->pps->transquant_bypass_enable_flag &&
  1249. lc->cu.cu_transquant_bypass_flag)
  1250. set_deblocking_bypass(s, x0, y0, log2_trafo_size);
  1251. }
  1252. }
  1253. return 0;
  1254. }
  1255. static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1256. {
  1257. //TODO: non-4:2:0 support
  1258. HEVCLocalContext *lc = &s->HEVClc;
  1259. GetBitContext gb;
  1260. int cb_size = 1 << log2_cb_size;
  1261. int stride0 = s->frame->linesize[0];
  1262. uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->sps->pixel_shift)];
  1263. int stride1 = s->frame->linesize[1];
  1264. uint8_t *dst1 = &s->frame->data[1][(y0 >> s->sps->vshift[1]) * stride1 + ((x0 >> s->sps->hshift[1]) << s->sps->pixel_shift)];
  1265. int stride2 = s->frame->linesize[2];
  1266. uint8_t *dst2 = &s->frame->data[2][(y0 >> s->sps->vshift[2]) * stride2 + ((x0 >> s->sps->hshift[2]) << s->sps->pixel_shift)];
  1267. int length = cb_size * cb_size * s->sps->pcm.bit_depth + ((cb_size * cb_size) >> 1) * s->sps->pcm.bit_depth_chroma;
  1268. const uint8_t *pcm = skip_bytes(&s->HEVClc.cc, (length + 7) >> 3);
  1269. int ret;
  1270. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
  1271. lc->slice_or_tiles_up_boundary,
  1272. lc->slice_or_tiles_left_boundary);
  1273. ret = init_get_bits(&gb, pcm, length);
  1274. if (ret < 0)
  1275. return ret;
  1276. s->hevcdsp.put_pcm(dst0, stride0, cb_size, &gb, s->sps->pcm.bit_depth);
  1277. s->hevcdsp.put_pcm(dst1, stride1, cb_size / 2, &gb, s->sps->pcm.bit_depth_chroma);
  1278. s->hevcdsp.put_pcm(dst2, stride2, cb_size / 2, &gb, s->sps->pcm.bit_depth_chroma);
  1279. return 0;
  1280. }
  1281. static void hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1282. {
  1283. HEVCLocalContext *lc = &s->HEVClc;
  1284. int x = ff_hevc_abs_mvd_greater0_flag_decode(s);
  1285. int y = ff_hevc_abs_mvd_greater0_flag_decode(s);
  1286. if (x)
  1287. x += ff_hevc_abs_mvd_greater1_flag_decode(s);
  1288. if (y)
  1289. y += ff_hevc_abs_mvd_greater1_flag_decode(s);
  1290. switch (x) {
  1291. case 2: lc->pu.mvd.x = ff_hevc_mvd_decode(s); break;
  1292. case 1: lc->pu.mvd.x = ff_hevc_mvd_sign_flag_decode(s); break;
  1293. case 0: lc->pu.mvd.x = 0; break;
  1294. }
  1295. switch (y) {
  1296. case 2: lc->pu.mvd.y = ff_hevc_mvd_decode(s); break;
  1297. case 1: lc->pu.mvd.y = ff_hevc_mvd_sign_flag_decode(s); break;
  1298. case 0: lc->pu.mvd.y = 0; break;
  1299. }
  1300. }
  1301. /**
  1302. * 8.5.3.2.2.1 Luma sample interpolation process
  1303. *
  1304. * @param s HEVC decoding context
  1305. * @param dst target buffer for block data at block position
  1306. * @param dststride stride of the dst buffer
  1307. * @param ref reference picture buffer at origin (0, 0)
  1308. * @param mv motion vector (relative to block position) to get pixel data from
  1309. * @param x_off horizontal position of block from origin (0, 0)
  1310. * @param y_off vertical position of block from origin (0, 0)
  1311. * @param block_w width of block
  1312. * @param block_h height of block
  1313. */
  1314. static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride,
  1315. AVFrame *ref, const Mv *mv, int x_off, int y_off,
  1316. int block_w, int block_h)
  1317. {
  1318. HEVCLocalContext *lc = &s->HEVClc;
  1319. uint8_t *src = ref->data[0];
  1320. ptrdiff_t srcstride = ref->linesize[0];
  1321. int pic_width = s->sps->width;
  1322. int pic_height = s->sps->height;
  1323. int mx = mv->x & 3;
  1324. int my = mv->y & 3;
  1325. int extra_left = ff_hevc_qpel_extra_before[mx];
  1326. int extra_top = ff_hevc_qpel_extra_before[my];
  1327. x_off += mv->x >> 2;
  1328. y_off += mv->y >> 2;
  1329. src += y_off * srcstride + (x_off << s->sps->pixel_shift);
  1330. if (x_off < extra_left || y_off < extra_top ||
  1331. x_off >= pic_width - block_w - ff_hevc_qpel_extra_after[mx] ||
  1332. y_off >= pic_height - block_h - ff_hevc_qpel_extra_after[my]) {
  1333. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1334. int offset = extra_top * srcstride + (extra_left << s->sps->pixel_shift);
  1335. int buf_offset = extra_top *
  1336. edge_emu_stride + (extra_left << s->sps->pixel_shift);
  1337. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
  1338. edge_emu_stride, srcstride,
  1339. block_w + ff_hevc_qpel_extra[mx],
  1340. block_h + ff_hevc_qpel_extra[my],
  1341. x_off - extra_left, y_off - extra_top,
  1342. pic_width, pic_height);
  1343. src = lc->edge_emu_buffer + buf_offset;
  1344. srcstride = edge_emu_stride;
  1345. }
  1346. s->hevcdsp.put_hevc_qpel[my][mx](dst, dststride, src, srcstride, block_w,
  1347. block_h, lc->mc_buffer);
  1348. }
  1349. /**
  1350. * 8.5.3.2.2.2 Chroma sample interpolation process
  1351. *
  1352. * @param s HEVC decoding context
  1353. * @param dst1 target buffer for block data at block position (U plane)
  1354. * @param dst2 target buffer for block data at block position (V plane)
  1355. * @param dststride stride of the dst1 and dst2 buffers
  1356. * @param ref reference picture buffer at origin (0, 0)
  1357. * @param mv motion vector (relative to block position) to get pixel data from
  1358. * @param x_off horizontal position of block from origin (0, 0)
  1359. * @param y_off vertical position of block from origin (0, 0)
  1360. * @param block_w width of block
  1361. * @param block_h height of block
  1362. */
  1363. static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2,
  1364. ptrdiff_t dststride, AVFrame *ref, const Mv *mv,
  1365. int x_off, int y_off, int block_w, int block_h)
  1366. {
  1367. HEVCLocalContext *lc = &s->HEVClc;
  1368. uint8_t *src1 = ref->data[1];
  1369. uint8_t *src2 = ref->data[2];
  1370. ptrdiff_t src1stride = ref->linesize[1];
  1371. ptrdiff_t src2stride = ref->linesize[2];
  1372. int pic_width = s->sps->width >> 1;
  1373. int pic_height = s->sps->height >> 1;
  1374. int mx = mv->x & 7;
  1375. int my = mv->y & 7;
  1376. x_off += mv->x >> 3;
  1377. y_off += mv->y >> 3;
  1378. src1 += y_off * src1stride + (x_off << s->sps->pixel_shift);
  1379. src2 += y_off * src2stride + (x_off << s->sps->pixel_shift);
  1380. if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
  1381. x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1382. y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1383. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1384. int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->sps->pixel_shift));
  1385. int buf_offset1 = EPEL_EXTRA_BEFORE *
  1386. (edge_emu_stride + (1 << s->sps->pixel_shift));
  1387. int offset2 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->sps->pixel_shift));
  1388. int buf_offset2 = EPEL_EXTRA_BEFORE *
  1389. (edge_emu_stride + (1 << s->sps->pixel_shift));
  1390. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
  1391. edge_emu_stride, src1stride,
  1392. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1393. x_off - EPEL_EXTRA_BEFORE,
  1394. y_off - EPEL_EXTRA_BEFORE,
  1395. pic_width, pic_height);
  1396. src1 = lc->edge_emu_buffer + buf_offset1;
  1397. src1stride = edge_emu_stride;
  1398. s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
  1399. block_w, block_h, mx, my, lc->mc_buffer);
  1400. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src2 - offset2,
  1401. edge_emu_stride, src2stride,
  1402. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1403. x_off - EPEL_EXTRA_BEFORE,
  1404. y_off - EPEL_EXTRA_BEFORE,
  1405. pic_width, pic_height);
  1406. src2 = lc->edge_emu_buffer + buf_offset2;
  1407. src2stride = edge_emu_stride;
  1408. s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
  1409. block_w, block_h, mx, my,
  1410. lc->mc_buffer);
  1411. } else {
  1412. s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
  1413. block_w, block_h, mx, my,
  1414. lc->mc_buffer);
  1415. s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
  1416. block_w, block_h, mx, my,
  1417. lc->mc_buffer);
  1418. }
  1419. }
  1420. static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
  1421. const Mv *mv, int y0, int height)
  1422. {
  1423. int y = (mv->y >> 2) + y0 + height + 9;
  1424. ff_thread_await_progress(&ref->tf, y, 0);
  1425. }
  1426. static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
  1427. int nPbW, int nPbH,
  1428. int log2_cb_size, int partIdx)
  1429. {
  1430. #define POS(c_idx, x, y) \
  1431. &s->frame->data[c_idx][((y) >> s->sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
  1432. (((x) >> s->sps->hshift[c_idx]) << s->sps->pixel_shift)]
  1433. HEVCLocalContext *lc = &s->HEVClc;
  1434. int merge_idx = 0;
  1435. struct MvField current_mv = {{{ 0 }}};
  1436. int min_pu_width = s->sps->min_pu_width;
  1437. MvField *tab_mvf = s->ref->tab_mvf;
  1438. RefPicList *refPicList = s->ref->refPicList;
  1439. HEVCFrame *ref0, *ref1;
  1440. int tmpstride = MAX_PB_SIZE;
  1441. uint8_t *dst0 = POS(0, x0, y0);
  1442. uint8_t *dst1 = POS(1, x0, y0);
  1443. uint8_t *dst2 = POS(2, x0, y0);
  1444. int log2_min_cb_size = s->sps->log2_min_cb_size;
  1445. int min_cb_width = s->sps->min_cb_width;
  1446. int x_cb = x0 >> log2_min_cb_size;
  1447. int y_cb = y0 >> log2_min_cb_size;
  1448. int ref_idx[2];
  1449. int mvp_flag[2];
  1450. int x_pu, y_pu;
  1451. int i, j;
  1452. if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
  1453. if (s->sh.max_num_merge_cand > 1)
  1454. merge_idx = ff_hevc_merge_idx_decode(s);
  1455. else
  1456. merge_idx = 0;
  1457. ff_hevc_luma_mv_merge_mode(s, x0, y0,
  1458. 1 << log2_cb_size,
  1459. 1 << log2_cb_size,
  1460. log2_cb_size, partIdx,
  1461. merge_idx, &current_mv);
  1462. x_pu = x0 >> s->sps->log2_min_pu_size;
  1463. y_pu = y0 >> s->sps->log2_min_pu_size;
  1464. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  1465. for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  1466. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1467. } else { /* MODE_INTER */
  1468. lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
  1469. if (lc->pu.merge_flag) {
  1470. if (s->sh.max_num_merge_cand > 1)
  1471. merge_idx = ff_hevc_merge_idx_decode(s);
  1472. else
  1473. merge_idx = 0;
  1474. ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1475. partIdx, merge_idx, &current_mv);
  1476. x_pu = x0 >> s->sps->log2_min_pu_size;
  1477. y_pu = y0 >> s->sps->log2_min_pu_size;
  1478. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  1479. for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  1480. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1481. } else {
  1482. enum InterPredIdc inter_pred_idc = PRED_L0;
  1483. ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
  1484. if (s->sh.slice_type == B_SLICE)
  1485. inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
  1486. if (inter_pred_idc != PRED_L1) {
  1487. if (s->sh.nb_refs[L0]) {
  1488. ref_idx[0] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
  1489. current_mv.ref_idx[0] = ref_idx[0];
  1490. }
  1491. current_mv.pred_flag[0] = 1;
  1492. hls_mvd_coding(s, x0, y0, 0);
  1493. mvp_flag[0] = ff_hevc_mvp_lx_flag_decode(s);
  1494. ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1495. partIdx, merge_idx, &current_mv,
  1496. mvp_flag[0], 0);
  1497. current_mv.mv[0].x += lc->pu.mvd.x;
  1498. current_mv.mv[0].y += lc->pu.mvd.y;
  1499. }
  1500. if (inter_pred_idc != PRED_L0) {
  1501. if (s->sh.nb_refs[L1]) {
  1502. ref_idx[1] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
  1503. current_mv.ref_idx[1] = ref_idx[1];
  1504. }
  1505. if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
  1506. lc->pu.mvd.x = 0;
  1507. lc->pu.mvd.y = 0;
  1508. } else {
  1509. hls_mvd_coding(s, x0, y0, 1);
  1510. }
  1511. current_mv.pred_flag[1] = 1;
  1512. mvp_flag[1] = ff_hevc_mvp_lx_flag_decode(s);
  1513. ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1514. partIdx, merge_idx, &current_mv,
  1515. mvp_flag[1], 1);
  1516. current_mv.mv[1].x += lc->pu.mvd.x;
  1517. current_mv.mv[1].y += lc->pu.mvd.y;
  1518. }
  1519. x_pu = x0 >> s->sps->log2_min_pu_size;
  1520. y_pu = y0 >> s->sps->log2_min_pu_size;
  1521. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  1522. for(j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  1523. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1524. }
  1525. }
  1526. if (current_mv.pred_flag[0]) {
  1527. ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
  1528. if (!ref0)
  1529. return;
  1530. hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
  1531. }
  1532. if (current_mv.pred_flag[1]) {
  1533. ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
  1534. if (!ref1)
  1535. return;
  1536. hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
  1537. }
  1538. if (current_mv.pred_flag[0] && !current_mv.pred_flag[1]) {
  1539. DECLARE_ALIGNED(16, int16_t, tmp[MAX_PB_SIZE * MAX_PB_SIZE]);
  1540. DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
  1541. luma_mc(s, tmp, tmpstride, ref0->frame,
  1542. &current_mv.mv[0], x0, y0, nPbW, nPbH);
  1543. if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1544. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
  1545. s->hevcdsp.weighted_pred(s->sh.luma_log2_weight_denom,
  1546. s->sh.luma_weight_l0[current_mv.ref_idx[0]],
  1547. s->sh.luma_offset_l0[current_mv.ref_idx[0]],
  1548. dst0, s->frame->linesize[0], tmp,
  1549. tmpstride, nPbW, nPbH);
  1550. } else {
  1551. s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
  1552. }
  1553. chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
  1554. &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
  1555. if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1556. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
  1557. s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
  1558. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
  1559. s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
  1560. dst1, s->frame->linesize[1], tmp, tmpstride,
  1561. nPbW / 2, nPbH / 2);
  1562. s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
  1563. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
  1564. s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
  1565. dst2, s->frame->linesize[2], tmp2, tmpstride,
  1566. nPbW / 2, nPbH / 2);
  1567. } else {
  1568. s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
  1569. s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
  1570. }
  1571. } else if (!current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
  1572. DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
  1573. DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
  1574. if (!ref1)
  1575. return;
  1576. luma_mc(s, tmp, tmpstride, ref1->frame,
  1577. &current_mv.mv[1], x0, y0, nPbW, nPbH);
  1578. if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1579. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
  1580. s->hevcdsp.weighted_pred(s->sh.luma_log2_weight_denom,
  1581. s->sh.luma_weight_l1[current_mv.ref_idx[1]],
  1582. s->sh.luma_offset_l1[current_mv.ref_idx[1]],
  1583. dst0, s->frame->linesize[0], tmp, tmpstride,
  1584. nPbW, nPbH);
  1585. } else {
  1586. s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
  1587. }
  1588. chroma_mc(s, tmp, tmp2, tmpstride, ref1->frame,
  1589. &current_mv.mv[1], x0/2, y0/2, nPbW/2, nPbH/2);
  1590. if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1591. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
  1592. s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
  1593. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
  1594. s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
  1595. dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
  1596. s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
  1597. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
  1598. s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
  1599. dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
  1600. } else {
  1601. s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
  1602. s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
  1603. }
  1604. } else if (current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
  1605. DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
  1606. DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
  1607. DECLARE_ALIGNED(16, int16_t, tmp3[MAX_PB_SIZE * MAX_PB_SIZE]);
  1608. DECLARE_ALIGNED(16, int16_t, tmp4[MAX_PB_SIZE * MAX_PB_SIZE]);
  1609. HEVCFrame *ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
  1610. HEVCFrame *ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
  1611. if (!ref0 || !ref1)
  1612. return;
  1613. luma_mc(s, tmp, tmpstride, ref0->frame,
  1614. &current_mv.mv[0], x0, y0, nPbW, nPbH);
  1615. luma_mc(s, tmp2, tmpstride, ref1->frame,
  1616. &current_mv.mv[1], x0, y0, nPbW, nPbH);
  1617. if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1618. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
  1619. s->hevcdsp.weighted_pred_avg(s->sh.luma_log2_weight_denom,
  1620. s->sh.luma_weight_l0[current_mv.ref_idx[0]],
  1621. s->sh.luma_weight_l1[current_mv.ref_idx[1]],
  1622. s->sh.luma_offset_l0[current_mv.ref_idx[0]],
  1623. s->sh.luma_offset_l1[current_mv.ref_idx[1]],
  1624. dst0, s->frame->linesize[0],
  1625. tmp, tmp2, tmpstride, nPbW, nPbH);
  1626. } else {
  1627. s->hevcdsp.put_weighted_pred_avg(dst0, s->frame->linesize[0],
  1628. tmp, tmp2, tmpstride, nPbW, nPbH);
  1629. }
  1630. chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
  1631. &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
  1632. chroma_mc(s, tmp3, tmp4, tmpstride, ref1->frame,
  1633. &current_mv.mv[1], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
  1634. if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1635. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
  1636. s->hevcdsp.weighted_pred_avg(s->sh.chroma_log2_weight_denom,
  1637. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
  1638. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
  1639. s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
  1640. s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
  1641. dst1, s->frame->linesize[1], tmp, tmp3,
  1642. tmpstride, nPbW / 2, nPbH / 2);
  1643. s->hevcdsp.weighted_pred_avg(s->sh.chroma_log2_weight_denom,
  1644. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
  1645. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
  1646. s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
  1647. s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
  1648. dst2, s->frame->linesize[2], tmp2, tmp4,
  1649. tmpstride, nPbW / 2, nPbH / 2);
  1650. } else {
  1651. s->hevcdsp.put_weighted_pred_avg(dst1, s->frame->linesize[1], tmp, tmp3, tmpstride, nPbW/2, nPbH/2);
  1652. s->hevcdsp.put_weighted_pred_avg(dst2, s->frame->linesize[2], tmp2, tmp4, tmpstride, nPbW/2, nPbH/2);
  1653. }
  1654. }
  1655. }
  1656. /**
  1657. * 8.4.1
  1658. */
  1659. static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
  1660. int prev_intra_luma_pred_flag)
  1661. {
  1662. HEVCLocalContext *lc = &s->HEVClc;
  1663. int x_pu = x0 >> s->sps->log2_min_pu_size;
  1664. int y_pu = y0 >> s->sps->log2_min_pu_size;
  1665. int min_pu_width = s->sps->min_pu_width;
  1666. int size_in_pus = pu_size >> s->sps->log2_min_pu_size;
  1667. int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
  1668. int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
  1669. int cand_up = (lc->ctb_up_flag || y0b) ?
  1670. s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
  1671. int cand_left = (lc->ctb_left_flag || x0b) ?
  1672. s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
  1673. int y_ctb = (y0 >> (s->sps->log2_ctb_size)) << (s->sps->log2_ctb_size);
  1674. MvField *tab_mvf = s->ref->tab_mvf;
  1675. int intra_pred_mode;
  1676. int candidate[3];
  1677. int i, j;
  1678. // intra_pred_mode prediction does not cross vertical CTB boundaries
  1679. if ((y0 - 1) < y_ctb)
  1680. cand_up = INTRA_DC;
  1681. if (cand_left == cand_up) {
  1682. if (cand_left < 2) {
  1683. candidate[0] = INTRA_PLANAR;
  1684. candidate[1] = INTRA_DC;
  1685. candidate[2] = INTRA_ANGULAR_26;
  1686. } else {
  1687. candidate[0] = cand_left;
  1688. candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
  1689. candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
  1690. }
  1691. } else {
  1692. candidate[0] = cand_left;
  1693. candidate[1] = cand_up;
  1694. if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
  1695. candidate[2] = INTRA_PLANAR;
  1696. } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
  1697. candidate[2] = INTRA_DC;
  1698. } else {
  1699. candidate[2] = INTRA_ANGULAR_26;
  1700. }
  1701. }
  1702. if (prev_intra_luma_pred_flag) {
  1703. intra_pred_mode = candidate[lc->pu.mpm_idx];
  1704. } else {
  1705. if (candidate[0] > candidate[1])
  1706. FFSWAP(uint8_t, candidate[0], candidate[1]);
  1707. if (candidate[0] > candidate[2])
  1708. FFSWAP(uint8_t, candidate[0], candidate[2]);
  1709. if (candidate[1] > candidate[2])
  1710. FFSWAP(uint8_t, candidate[1], candidate[2]);
  1711. intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
  1712. for (i = 0; i < 3; i++)
  1713. if (intra_pred_mode >= candidate[i])
  1714. intra_pred_mode++;
  1715. }
  1716. /* write the intra prediction units into the mv array */
  1717. if (!size_in_pus)
  1718. size_in_pus = 1;
  1719. for (i = 0; i < size_in_pus; i++) {
  1720. memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
  1721. intra_pred_mode, size_in_pus);
  1722. for (j = 0; j < size_in_pus; j++) {
  1723. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].is_intra = 1;
  1724. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[0] = 0;
  1725. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[1] = 0;
  1726. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[0] = 0;
  1727. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[1] = 0;
  1728. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].x = 0;
  1729. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].y = 0;
  1730. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].x = 0;
  1731. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].y = 0;
  1732. }
  1733. }
  1734. return intra_pred_mode;
  1735. }
  1736. static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
  1737. int log2_cb_size, int ct_depth)
  1738. {
  1739. int length = (1 << log2_cb_size) >> s->sps->log2_min_cb_size;
  1740. int x_cb = x0 >> s->sps->log2_min_cb_size;
  1741. int y_cb = y0 >> s->sps->log2_min_cb_size;
  1742. int y;
  1743. for (y = 0; y < length; y++)
  1744. memset(&s->tab_ct_depth[(y_cb + y) * s->sps->min_cb_width + x_cb],
  1745. ct_depth, length);
  1746. }
  1747. static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
  1748. int log2_cb_size)
  1749. {
  1750. HEVCLocalContext *lc = &s->HEVClc;
  1751. static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
  1752. uint8_t prev_intra_luma_pred_flag[4];
  1753. int split = lc->cu.part_mode == PART_NxN;
  1754. int pb_size = (1 << log2_cb_size) >> split;
  1755. int side = split + 1;
  1756. int chroma_mode;
  1757. int i, j;
  1758. for (i = 0; i < side; i++)
  1759. for (j = 0; j < side; j++)
  1760. prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
  1761. for (i = 0; i < side; i++) {
  1762. for (j = 0; j < side; j++) {
  1763. if (prev_intra_luma_pred_flag[2 * i + j])
  1764. lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
  1765. else
  1766. lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
  1767. lc->pu.intra_pred_mode[2 * i + j] =
  1768. luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
  1769. prev_intra_luma_pred_flag[2 * i + j]);
  1770. }
  1771. }
  1772. chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
  1773. if (chroma_mode != 4) {
  1774. if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
  1775. lc->pu.intra_pred_mode_c = 34;
  1776. else
  1777. lc->pu.intra_pred_mode_c = intra_chroma_table[chroma_mode];
  1778. } else {
  1779. lc->pu.intra_pred_mode_c = lc->pu.intra_pred_mode[0];
  1780. }
  1781. }
  1782. static void intra_prediction_unit_default_value(HEVCContext *s,
  1783. int x0, int y0,
  1784. int log2_cb_size)
  1785. {
  1786. HEVCLocalContext *lc = &s->HEVClc;
  1787. int pb_size = 1 << log2_cb_size;
  1788. int size_in_pus = pb_size >> s->sps->log2_min_pu_size;
  1789. int min_pu_width = s->sps->min_pu_width;
  1790. MvField *tab_mvf = s->ref->tab_mvf;
  1791. int x_pu = x0 >> s->sps->log2_min_pu_size;
  1792. int y_pu = y0 >> s->sps->log2_min_pu_size;
  1793. int j, k;
  1794. if (size_in_pus == 0)
  1795. size_in_pus = 1;
  1796. for (j = 0; j < size_in_pus; j++) {
  1797. memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
  1798. for (k = 0; k < size_in_pus; k++)
  1799. tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].is_intra = lc->cu.pred_mode == MODE_INTRA;
  1800. }
  1801. }
  1802. static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1803. {
  1804. int cb_size = 1 << log2_cb_size;
  1805. HEVCLocalContext *lc = &s->HEVClc;
  1806. int log2_min_cb_size = s->sps->log2_min_cb_size;
  1807. int length = cb_size >> log2_min_cb_size;
  1808. int min_cb_width = s->sps->min_cb_width;
  1809. int x_cb = x0 >> log2_min_cb_size;
  1810. int y_cb = y0 >> log2_min_cb_size;
  1811. int x, y, ret;
  1812. lc->cu.x = x0;
  1813. lc->cu.y = y0;
  1814. lc->cu.rqt_root_cbf = 1;
  1815. lc->cu.pred_mode = MODE_INTRA;
  1816. lc->cu.part_mode = PART_2Nx2N;
  1817. lc->cu.intra_split_flag = 0;
  1818. lc->cu.pcm_flag = 0;
  1819. SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
  1820. for (x = 0; x < 4; x++)
  1821. lc->pu.intra_pred_mode[x] = 1;
  1822. if (s->pps->transquant_bypass_enable_flag) {
  1823. lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
  1824. if (lc->cu.cu_transquant_bypass_flag)
  1825. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  1826. } else
  1827. lc->cu.cu_transquant_bypass_flag = 0;
  1828. if (s->sh.slice_type != I_SLICE) {
  1829. uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
  1830. lc->cu.pred_mode = MODE_SKIP;
  1831. x = y_cb * min_cb_width + x_cb;
  1832. for (y = 0; y < length; y++) {
  1833. memset(&s->skip_flag[x], skip_flag, length);
  1834. x += min_cb_width;
  1835. }
  1836. lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
  1837. }
  1838. if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
  1839. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
  1840. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1841. if (!s->sh.disable_deblocking_filter_flag)
  1842. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
  1843. lc->slice_or_tiles_up_boundary,
  1844. lc->slice_or_tiles_left_boundary);
  1845. } else {
  1846. if (s->sh.slice_type != I_SLICE)
  1847. lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
  1848. if (lc->cu.pred_mode != MODE_INTRA ||
  1849. log2_cb_size == s->sps->log2_min_cb_size) {
  1850. lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
  1851. lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
  1852. lc->cu.pred_mode == MODE_INTRA;
  1853. }
  1854. if (lc->cu.pred_mode == MODE_INTRA) {
  1855. if (lc->cu.part_mode == PART_2Nx2N && s->sps->pcm_enabled_flag &&
  1856. log2_cb_size >= s->sps->pcm.log2_min_pcm_cb_size &&
  1857. log2_cb_size <= s->sps->pcm.log2_max_pcm_cb_size) {
  1858. lc->cu.pcm_flag = ff_hevc_pcm_flag_decode(s);
  1859. }
  1860. if (lc->cu.pcm_flag) {
  1861. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1862. ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
  1863. if (s->sps->pcm.loop_filter_disable_flag)
  1864. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  1865. if (ret < 0)
  1866. return ret;
  1867. } else {
  1868. intra_prediction_unit(s, x0, y0, log2_cb_size);
  1869. }
  1870. } else {
  1871. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1872. switch (lc->cu.part_mode) {
  1873. case PART_2Nx2N:
  1874. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
  1875. break;
  1876. case PART_2NxN:
  1877. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0);
  1878. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1);
  1879. break;
  1880. case PART_Nx2N:
  1881. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0);
  1882. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1);
  1883. break;
  1884. case PART_2NxnU:
  1885. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0);
  1886. hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1);
  1887. break;
  1888. case PART_2NxnD:
  1889. hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0);
  1890. hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1);
  1891. break;
  1892. case PART_nLx2N:
  1893. hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0);
  1894. hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1);
  1895. break;
  1896. case PART_nRx2N:
  1897. hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0);
  1898. hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1);
  1899. break;
  1900. case PART_NxN:
  1901. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0);
  1902. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1);
  1903. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2);
  1904. hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3);
  1905. break;
  1906. }
  1907. }
  1908. if (!lc->cu.pcm_flag) {
  1909. if (lc->cu.pred_mode != MODE_INTRA &&
  1910. !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
  1911. lc->cu.rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
  1912. }
  1913. if (lc->cu.rqt_root_cbf) {
  1914. lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
  1915. s->sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
  1916. s->sps->max_transform_hierarchy_depth_inter;
  1917. ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
  1918. log2_cb_size,
  1919. log2_cb_size, 0, 0);
  1920. if (ret < 0)
  1921. return ret;
  1922. } else {
  1923. if (!s->sh.disable_deblocking_filter_flag)
  1924. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
  1925. lc->slice_or_tiles_up_boundary,
  1926. lc->slice_or_tiles_left_boundary);
  1927. }
  1928. }
  1929. }
  1930. if (s->pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
  1931. ff_hevc_set_qPy(s, x0, y0, x0, y0, log2_cb_size);
  1932. x = y_cb * min_cb_width + x_cb;
  1933. for (y = 0; y < length; y++) {
  1934. memset(&s->qp_y_tab[x], lc->qp_y, length);
  1935. x += min_cb_width;
  1936. }
  1937. set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
  1938. return 0;
  1939. }
  1940. static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
  1941. int log2_cb_size, int cb_depth)
  1942. {
  1943. HEVCLocalContext *lc = &s->HEVClc;
  1944. const int cb_size = 1 << log2_cb_size;
  1945. lc->ct.depth = cb_depth;
  1946. if (x0 + cb_size <= s->sps->width &&
  1947. y0 + cb_size <= s->sps->height &&
  1948. log2_cb_size > s->sps->log2_min_cb_size) {
  1949. SAMPLE(s->split_cu_flag, x0, y0) =
  1950. ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
  1951. } else {
  1952. SAMPLE(s->split_cu_flag, x0, y0) =
  1953. (log2_cb_size > s->sps->log2_min_cb_size);
  1954. }
  1955. if (s->pps->cu_qp_delta_enabled_flag &&
  1956. log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth) {
  1957. lc->tu.is_cu_qp_delta_coded = 0;
  1958. lc->tu.cu_qp_delta = 0;
  1959. }
  1960. if (SAMPLE(s->split_cu_flag, x0, y0)) {
  1961. const int cb_size_split = cb_size >> 1;
  1962. const int x1 = x0 + cb_size_split;
  1963. const int y1 = y0 + cb_size_split;
  1964. log2_cb_size--;
  1965. cb_depth++;
  1966. #define SUBDIVIDE(x, y) \
  1967. do { \
  1968. if (x < s->sps->width && y < s->sps->height) { \
  1969. int ret = hls_coding_quadtree(s, x, y, log2_cb_size, cb_depth);\
  1970. if (ret < 0) \
  1971. return ret; \
  1972. } \
  1973. } while (0)
  1974. SUBDIVIDE(x0, y0);
  1975. SUBDIVIDE(x1, y0);
  1976. SUBDIVIDE(x0, y1);
  1977. SUBDIVIDE(x1, y1);
  1978. } else {
  1979. int ret = hls_coding_unit(s, x0, y0, log2_cb_size);
  1980. if (ret < 0)
  1981. return ret;
  1982. }
  1983. return 0;
  1984. }
  1985. static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
  1986. int ctb_addr_ts)
  1987. {
  1988. HEVCLocalContext *lc = &s->HEVClc;
  1989. int ctb_size = 1 << s->sps->log2_ctb_size;
  1990. int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  1991. int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
  1992. int tile_left_boundary, tile_up_boundary;
  1993. int slice_left_boundary, slice_up_boundary;
  1994. s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
  1995. if (s->pps->entropy_coding_sync_enabled_flag) {
  1996. if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
  1997. lc->first_qp_group = 1;
  1998. lc->end_of_tiles_x = s->sps->width;
  1999. } else if (s->pps->tiles_enabled_flag) {
  2000. if (ctb_addr_ts && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
  2001. int idxX = s->pps->col_idxX[x_ctb >> s->sps->log2_ctb_size];
  2002. lc->start_of_tiles_x = x_ctb;
  2003. lc->end_of_tiles_x = x_ctb + (s->pps->column_width[idxX] << s->sps->log2_ctb_size);
  2004. lc->first_qp_group = 1;
  2005. }
  2006. } else {
  2007. lc->end_of_tiles_x = s->sps->width;
  2008. }
  2009. lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->sps->height);
  2010. if (s->pps->tiles_enabled_flag) {
  2011. tile_left_boundary = x_ctb > 0 &&
  2012. s->pps->tile_id[ctb_addr_ts] == s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]];
  2013. slice_left_boundary = x_ctb > 0 &&
  2014. s->tab_slice_address[ctb_addr_rs] == s->tab_slice_address[ctb_addr_rs - 1];
  2015. tile_up_boundary = y_ctb > 0 &&
  2016. 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]];
  2017. slice_up_boundary = y_ctb > 0 &&
  2018. s->tab_slice_address[ctb_addr_rs] == s->tab_slice_address[ctb_addr_rs - s->sps->ctb_width];
  2019. } else {
  2020. tile_left_boundary =
  2021. tile_up_boundary = 1;
  2022. slice_left_boundary = ctb_addr_in_slice > 0;
  2023. slice_up_boundary = ctb_addr_in_slice >= s->sps->ctb_width;
  2024. }
  2025. lc->slice_or_tiles_left_boundary = (!slice_left_boundary) + (!tile_left_boundary << 1);
  2026. lc->slice_or_tiles_up_boundary = (!slice_up_boundary + (!tile_up_boundary << 1));
  2027. lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && tile_left_boundary);
  2028. lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->sps->ctb_width) && tile_up_boundary);
  2029. 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]]));
  2030. 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]]));
  2031. }
  2032. static int hls_slice_data(HEVCContext *s)
  2033. {
  2034. int ctb_size = 1 << s->sps->log2_ctb_size;
  2035. int more_data = 1;
  2036. int x_ctb = 0;
  2037. int y_ctb = 0;
  2038. int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
  2039. int ret;
  2040. while (more_data && ctb_addr_ts < s->sps->ctb_size) {
  2041. int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  2042. x_ctb = (ctb_addr_rs % ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  2043. y_ctb = (ctb_addr_rs / ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  2044. hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
  2045. ff_hevc_cabac_init(s, ctb_addr_ts);
  2046. hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
  2047. s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
  2048. s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
  2049. s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
  2050. ret = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
  2051. if (ret < 0)
  2052. return ret;
  2053. more_data = !ff_hevc_end_of_slice_flag_decode(s);
  2054. ctb_addr_ts++;
  2055. ff_hevc_save_states(s, ctb_addr_ts);
  2056. ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
  2057. }
  2058. if (x_ctb + ctb_size >= s->sps->width &&
  2059. y_ctb + ctb_size >= s->sps->height)
  2060. ff_hevc_hls_filter(s, x_ctb, y_ctb);
  2061. return ctb_addr_ts;
  2062. }
  2063. /**
  2064. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  2065. * 0 if the unit should be skipped, 1 otherwise
  2066. */
  2067. static int hls_nal_unit(HEVCContext *s)
  2068. {
  2069. GetBitContext *gb = &s->HEVClc.gb;
  2070. int nuh_layer_id;
  2071. if (get_bits1(gb) != 0)
  2072. return AVERROR_INVALIDDATA;
  2073. s->nal_unit_type = get_bits(gb, 6);
  2074. nuh_layer_id = get_bits(gb, 6);
  2075. s->temporal_id = get_bits(gb, 3) - 1;
  2076. if (s->temporal_id < 0)
  2077. return AVERROR_INVALIDDATA;
  2078. av_log(s->avctx, AV_LOG_DEBUG,
  2079. "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
  2080. s->nal_unit_type, nuh_layer_id, s->temporal_id);
  2081. return nuh_layer_id == 0;
  2082. }
  2083. static void restore_tqb_pixels(HEVCContext *s)
  2084. {
  2085. int min_pu_size = 1 << s->sps->log2_min_pu_size;
  2086. int x, y, c_idx;
  2087. for (c_idx = 0; c_idx < 3; c_idx++) {
  2088. ptrdiff_t stride = s->frame->linesize[c_idx];
  2089. int hshift = s->sps->hshift[c_idx];
  2090. int vshift = s->sps->vshift[c_idx];
  2091. for (y = 0; y < s->sps->min_pu_height; y++) {
  2092. for (x = 0; x < s->sps->min_pu_width; x++) {
  2093. if (s->is_pcm[y * s->sps->min_pu_width + x]) {
  2094. int n;
  2095. int len = min_pu_size >> hshift;
  2096. 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)];
  2097. 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)];
  2098. for (n = 0; n < (min_pu_size >> vshift); n++) {
  2099. memcpy(dst, src, len);
  2100. src += stride;
  2101. dst += stride;
  2102. }
  2103. }
  2104. }
  2105. }
  2106. }
  2107. }
  2108. static int set_side_data(HEVCContext *s)
  2109. {
  2110. AVFrame *out = s->ref->frame;
  2111. if (s->sei_frame_packing_present &&
  2112. s->frame_packing_arrangement_type >= 3 &&
  2113. s->frame_packing_arrangement_type <= 5 &&
  2114. s->content_interpretation_type > 0 &&
  2115. s->content_interpretation_type < 3) {
  2116. AVStereo3D *stereo = av_stereo3d_create_side_data(out);
  2117. if (!stereo)
  2118. return AVERROR(ENOMEM);
  2119. switch (s->frame_packing_arrangement_type) {
  2120. case 3:
  2121. if (s->quincunx_subsampling)
  2122. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  2123. else
  2124. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  2125. break;
  2126. case 4:
  2127. stereo->type = AV_STEREO3D_TOPBOTTOM;
  2128. break;
  2129. case 5:
  2130. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  2131. break;
  2132. }
  2133. if (s->content_interpretation_type == 2)
  2134. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  2135. }
  2136. return 0;
  2137. }
  2138. static int hevc_frame_start(HEVCContext *s)
  2139. {
  2140. HEVCLocalContext *lc = &s->HEVClc;
  2141. int ret;
  2142. memset(s->horizontal_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
  2143. memset(s->vertical_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
  2144. memset(s->cbf_luma, 0, s->sps->min_tb_width * s->sps->min_tb_height);
  2145. memset(s->is_pcm, 0, s->sps->min_pu_width * s->sps->min_pu_height);
  2146. lc->start_of_tiles_x = 0;
  2147. s->is_decoded = 0;
  2148. s->first_nal_type = s->nal_unit_type;
  2149. if (s->pps->tiles_enabled_flag)
  2150. lc->end_of_tiles_x = s->pps->column_width[0] << s->sps->log2_ctb_size;
  2151. ret = ff_hevc_set_new_ref(s, s->sps->sao_enabled ? &s->sao_frame : &s->frame,
  2152. s->poc);
  2153. if (ret < 0)
  2154. goto fail;
  2155. ret = ff_hevc_frame_rps(s);
  2156. if (ret < 0) {
  2157. av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
  2158. goto fail;
  2159. }
  2160. ret = set_side_data(s);
  2161. if (ret < 0)
  2162. goto fail;
  2163. av_frame_unref(s->output_frame);
  2164. ret = ff_hevc_output_frame(s, s->output_frame, 0);
  2165. if (ret < 0)
  2166. goto fail;
  2167. ff_thread_finish_setup(s->avctx);
  2168. return 0;
  2169. fail:
  2170. if (s->ref)
  2171. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  2172. s->ref = NULL;
  2173. return ret;
  2174. }
  2175. static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
  2176. {
  2177. HEVCLocalContext *lc = &s->HEVClc;
  2178. GetBitContext *gb = &lc->gb;
  2179. int ctb_addr_ts, ret;
  2180. ret = init_get_bits8(gb, nal, length);
  2181. if (ret < 0)
  2182. return ret;
  2183. ret = hls_nal_unit(s);
  2184. if (ret < 0) {
  2185. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  2186. s->nal_unit_type);
  2187. goto fail;
  2188. } else if (!ret)
  2189. return 0;
  2190. switch (s->nal_unit_type) {
  2191. case NAL_VPS:
  2192. ret = ff_hevc_decode_nal_vps(s);
  2193. if (ret < 0)
  2194. goto fail;
  2195. break;
  2196. case NAL_SPS:
  2197. ret = ff_hevc_decode_nal_sps(s);
  2198. if (ret < 0)
  2199. goto fail;
  2200. break;
  2201. case NAL_PPS:
  2202. ret = ff_hevc_decode_nal_pps(s);
  2203. if (ret < 0)
  2204. goto fail;
  2205. break;
  2206. case NAL_SEI_PREFIX:
  2207. case NAL_SEI_SUFFIX:
  2208. ret = ff_hevc_decode_nal_sei(s);
  2209. if (ret < 0)
  2210. goto fail;
  2211. break;
  2212. case NAL_TRAIL_R:
  2213. case NAL_TRAIL_N:
  2214. case NAL_TSA_N:
  2215. case NAL_TSA_R:
  2216. case NAL_STSA_N:
  2217. case NAL_STSA_R:
  2218. case NAL_BLA_W_LP:
  2219. case NAL_BLA_W_RADL:
  2220. case NAL_BLA_N_LP:
  2221. case NAL_IDR_W_RADL:
  2222. case NAL_IDR_N_LP:
  2223. case NAL_CRA_NUT:
  2224. case NAL_RADL_N:
  2225. case NAL_RADL_R:
  2226. case NAL_RASL_N:
  2227. case NAL_RASL_R:
  2228. ret = hls_slice_header(s);
  2229. if (ret < 0)
  2230. return ret;
  2231. if (s->max_ra == INT_MAX) {
  2232. if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
  2233. s->max_ra = s->poc;
  2234. } else {
  2235. if (IS_IDR(s))
  2236. s->max_ra = INT_MIN;
  2237. }
  2238. }
  2239. if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
  2240. s->poc <= s->max_ra) {
  2241. s->is_decoded = 0;
  2242. break;
  2243. } else {
  2244. if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
  2245. s->max_ra = INT_MIN;
  2246. }
  2247. if (s->sh.first_slice_in_pic_flag) {
  2248. ret = hevc_frame_start(s);
  2249. if (ret < 0)
  2250. return ret;
  2251. } else if (!s->ref) {
  2252. av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
  2253. goto fail;
  2254. }
  2255. if (s->nal_unit_type != s->first_nal_type) {
  2256. av_log(s->avctx, AV_LOG_ERROR,
  2257. "Non-matching NAL types of the VCL NALUs: %d %d\n",
  2258. s->first_nal_type, s->nal_unit_type);
  2259. return AVERROR_INVALIDDATA;
  2260. }
  2261. if (!s->sh.dependent_slice_segment_flag &&
  2262. s->sh.slice_type != I_SLICE) {
  2263. ret = ff_hevc_slice_rpl(s);
  2264. if (ret < 0) {
  2265. av_log(s->avctx, AV_LOG_WARNING,
  2266. "Error constructing the reference lists for the current slice.\n");
  2267. goto fail;
  2268. }
  2269. }
  2270. ctb_addr_ts = hls_slice_data(s);
  2271. if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {
  2272. s->is_decoded = 1;
  2273. if ((s->pps->transquant_bypass_enable_flag ||
  2274. (s->sps->pcm.loop_filter_disable_flag && s->sps->pcm_enabled_flag)) &&
  2275. s->sps->sao_enabled)
  2276. restore_tqb_pixels(s);
  2277. }
  2278. if (ctb_addr_ts < 0) {
  2279. ret = ctb_addr_ts;
  2280. goto fail;
  2281. }
  2282. break;
  2283. case NAL_EOS_NUT:
  2284. case NAL_EOB_NUT:
  2285. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2286. s->max_ra = INT_MAX;
  2287. break;
  2288. case NAL_AUD:
  2289. case NAL_FD_NUT:
  2290. break;
  2291. default:
  2292. av_log(s->avctx, AV_LOG_INFO,
  2293. "Skipping NAL unit %d\n", s->nal_unit_type);
  2294. }
  2295. return 0;
  2296. fail:
  2297. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  2298. return ret;
  2299. return 0;
  2300. }
  2301. /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
  2302. * between these functions would be nice. */
  2303. static int extract_rbsp(const uint8_t *src, int length,
  2304. HEVCNAL *nal)
  2305. {
  2306. int i, si, di;
  2307. uint8_t *dst;
  2308. #define STARTCODE_TEST \
  2309. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  2310. if (src[i + 2] != 3) { \
  2311. /* startcode, so we must be past the end */ \
  2312. length = i; \
  2313. } \
  2314. break; \
  2315. }
  2316. #if HAVE_FAST_UNALIGNED
  2317. #define FIND_FIRST_ZERO \
  2318. if (i > 0 && !src[i]) \
  2319. i--; \
  2320. while (src[i]) \
  2321. i++
  2322. #if HAVE_FAST_64BIT
  2323. for (i = 0; i + 1 < length; i += 9) {
  2324. if (!((~AV_RN64A(src + i) &
  2325. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  2326. 0x8000800080008080ULL))
  2327. continue;
  2328. FIND_FIRST_ZERO;
  2329. STARTCODE_TEST;
  2330. i -= 7;
  2331. }
  2332. #else
  2333. for (i = 0; i + 1 < length; i += 5) {
  2334. if (!((~AV_RN32A(src + i) &
  2335. (AV_RN32A(src + i) - 0x01000101U)) &
  2336. 0x80008080U))
  2337. continue;
  2338. FIND_FIRST_ZERO;
  2339. STARTCODE_TEST;
  2340. i -= 3;
  2341. }
  2342. #endif /* HAVE_FAST_64BIT */
  2343. #else
  2344. for (i = 0; i + 1 < length; i += 2) {
  2345. if (src[i])
  2346. continue;
  2347. if (i > 0 && src[i - 1] == 0)
  2348. i--;
  2349. STARTCODE_TEST;
  2350. }
  2351. #endif /* HAVE_FAST_UNALIGNED */
  2352. if (i >= length - 1) { // no escaped 0
  2353. nal->data = src;
  2354. nal->size = length;
  2355. return length;
  2356. }
  2357. av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
  2358. length + FF_INPUT_BUFFER_PADDING_SIZE);
  2359. if (!nal->rbsp_buffer)
  2360. return AVERROR(ENOMEM);
  2361. dst = nal->rbsp_buffer;
  2362. memcpy(dst, src, i);
  2363. si = di = i;
  2364. while (si + 2 < length) {
  2365. // remove escapes (very rare 1:2^22)
  2366. if (src[si + 2] > 3) {
  2367. dst[di++] = src[si++];
  2368. dst[di++] = src[si++];
  2369. } else if (src[si] == 0 && src[si + 1] == 0) {
  2370. if (src[si + 2] == 3) { // escape
  2371. dst[di++] = 0;
  2372. dst[di++] = 0;
  2373. si += 3;
  2374. continue;
  2375. } else // next start code
  2376. goto nsc;
  2377. }
  2378. dst[di++] = src[si++];
  2379. }
  2380. while (si < length)
  2381. dst[di++] = src[si++];
  2382. nsc:
  2383. memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  2384. nal->data = dst;
  2385. nal->size = di;
  2386. return si;
  2387. }
  2388. static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
  2389. {
  2390. int i, consumed, ret = 0;
  2391. s->ref = NULL;
  2392. s->eos = 0;
  2393. /* split the input packet into NAL units, so we know the upper bound on the
  2394. * number of slices in the frame */
  2395. s->nb_nals = 0;
  2396. while (length >= 4) {
  2397. HEVCNAL *nal;
  2398. int extract_length = 0;
  2399. if (s->is_nalff) {
  2400. int i;
  2401. for (i = 0; i < s->nal_length_size; i++)
  2402. extract_length = (extract_length << 8) | buf[i];
  2403. buf += s->nal_length_size;
  2404. length -= s->nal_length_size;
  2405. if (extract_length > length) {
  2406. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
  2407. ret = AVERROR_INVALIDDATA;
  2408. goto fail;
  2409. }
  2410. } else {
  2411. if (buf[2] == 0) {
  2412. length--;
  2413. buf++;
  2414. continue;
  2415. }
  2416. if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
  2417. ret = AVERROR_INVALIDDATA;
  2418. goto fail;
  2419. }
  2420. buf += 3;
  2421. length -= 3;
  2422. extract_length = length;
  2423. }
  2424. if (s->nals_allocated < s->nb_nals + 1) {
  2425. int new_size = s->nals_allocated + 1;
  2426. HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));
  2427. if (!tmp) {
  2428. ret = AVERROR(ENOMEM);
  2429. goto fail;
  2430. }
  2431. s->nals = tmp;
  2432. memset(s->nals + s->nals_allocated, 0,
  2433. (new_size - s->nals_allocated) * sizeof(*tmp));
  2434. s->nals_allocated = new_size;
  2435. }
  2436. nal = &s->nals[s->nb_nals++];
  2437. consumed = extract_rbsp(buf, extract_length, nal);
  2438. if (consumed < 0) {
  2439. ret = consumed;
  2440. goto fail;
  2441. }
  2442. ret = init_get_bits8(&s->HEVClc.gb, nal->data, nal->size);
  2443. if (ret < 0)
  2444. goto fail;
  2445. hls_nal_unit(s);
  2446. if (s->nal_unit_type == NAL_EOB_NUT ||
  2447. s->nal_unit_type == NAL_EOS_NUT)
  2448. s->eos = 1;
  2449. buf += consumed;
  2450. length -= consumed;
  2451. }
  2452. /* parse the NAL units */
  2453. for (i = 0; i < s->nb_nals; i++) {
  2454. int ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);
  2455. if (ret < 0) {
  2456. av_log(s->avctx, AV_LOG_WARNING,
  2457. "Error parsing NAL unit #%d.\n", i);
  2458. goto fail;
  2459. }
  2460. }
  2461. fail:
  2462. if (s->ref)
  2463. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  2464. return ret;
  2465. }
  2466. static void print_md5(void *log_ctx, int level, uint8_t md5[16])
  2467. {
  2468. int i;
  2469. for (i = 0; i < 16; i++)
  2470. av_log(log_ctx, level, "%02"PRIx8, md5[i]);
  2471. }
  2472. static int verify_md5(HEVCContext *s, AVFrame *frame)
  2473. {
  2474. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  2475. int pixel_shift;
  2476. int i, j;
  2477. if (!desc)
  2478. return AVERROR(EINVAL);
  2479. pixel_shift = desc->comp[0].depth_minus1 > 7;
  2480. av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
  2481. s->poc);
  2482. /* the checksums are LE, so we have to byteswap for >8bpp formats
  2483. * on BE arches */
  2484. #if HAVE_BIGENDIAN
  2485. if (pixel_shift && !s->checksum_buf) {
  2486. av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
  2487. FFMAX3(frame->linesize[0], frame->linesize[1],
  2488. frame->linesize[2]));
  2489. if (!s->checksum_buf)
  2490. return AVERROR(ENOMEM);
  2491. }
  2492. #endif
  2493. for (i = 0; frame->data[i]; i++) {
  2494. int width = s->avctx->coded_width;
  2495. int height = s->avctx->coded_height;
  2496. int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
  2497. int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
  2498. uint8_t md5[16];
  2499. av_md5_init(s->md5_ctx);
  2500. for (j = 0; j < h; j++) {
  2501. const uint8_t *src = frame->data[i] + j * frame->linesize[i];
  2502. #if HAVE_BIGENDIAN
  2503. if (pixel_shift) {
  2504. s->dsp.bswap16_buf((uint16_t*)s->checksum_buf,
  2505. (const uint16_t*)src, w);
  2506. src = s->checksum_buf;
  2507. }
  2508. #endif
  2509. av_md5_update(s->md5_ctx, src, w << pixel_shift);
  2510. }
  2511. av_md5_final(s->md5_ctx, md5);
  2512. if (!memcmp(md5, s->md5[i], 16)) {
  2513. av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
  2514. print_md5(s->avctx, AV_LOG_DEBUG, md5);
  2515. av_log (s->avctx, AV_LOG_DEBUG, "; ");
  2516. } else {
  2517. av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
  2518. print_md5(s->avctx, AV_LOG_ERROR, md5);
  2519. av_log (s->avctx, AV_LOG_ERROR, " != ");
  2520. print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
  2521. av_log (s->avctx, AV_LOG_ERROR, "\n");
  2522. return AVERROR_INVALIDDATA;
  2523. }
  2524. }
  2525. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2526. return 0;
  2527. }
  2528. static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
  2529. AVPacket *avpkt)
  2530. {
  2531. int ret;
  2532. HEVCContext *s = avctx->priv_data;
  2533. if (!avpkt->size) {
  2534. ret = ff_hevc_output_frame(s, data, 1);
  2535. if (ret < 0)
  2536. return ret;
  2537. *got_output = ret;
  2538. return 0;
  2539. }
  2540. s->ref = NULL;
  2541. ret = decode_nal_units(s, avpkt->data, avpkt->size);
  2542. if (ret < 0)
  2543. return ret;
  2544. /* verify the SEI checksum */
  2545. if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
  2546. s->is_md5) {
  2547. ret = verify_md5(s, s->ref->frame);
  2548. if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
  2549. ff_hevc_unref_frame(s, s->ref, ~0);
  2550. return ret;
  2551. }
  2552. }
  2553. s->is_md5 = 0;
  2554. if (s->is_decoded) {
  2555. av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
  2556. s->is_decoded = 0;
  2557. }
  2558. if (s->output_frame->buf[0]) {
  2559. av_frame_move_ref(data, s->output_frame);
  2560. *got_output = 1;
  2561. }
  2562. return avpkt->size;
  2563. }
  2564. static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
  2565. {
  2566. int ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  2567. if (ret < 0)
  2568. return ret;
  2569. dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
  2570. if (!dst->tab_mvf_buf)
  2571. goto fail;
  2572. dst->tab_mvf = src->tab_mvf;
  2573. dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
  2574. if (!dst->rpl_tab_buf)
  2575. goto fail;
  2576. dst->rpl_tab = src->rpl_tab;
  2577. dst->rpl_buf = av_buffer_ref(src->rpl_buf);
  2578. if (!dst->rpl_buf)
  2579. goto fail;
  2580. dst->poc = src->poc;
  2581. dst->ctb_count = src->ctb_count;
  2582. dst->window = src->window;
  2583. dst->flags = src->flags;
  2584. dst->sequence = src->sequence;
  2585. return 0;
  2586. fail:
  2587. ff_hevc_unref_frame(s, dst, ~0);
  2588. return AVERROR(ENOMEM);
  2589. }
  2590. static av_cold int hevc_decode_free(AVCodecContext *avctx)
  2591. {
  2592. HEVCContext *s = avctx->priv_data;
  2593. int i;
  2594. pic_arrays_free(s);
  2595. av_freep(&s->md5_ctx);
  2596. av_frame_free(&s->tmp_frame);
  2597. av_frame_free(&s->output_frame);
  2598. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2599. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2600. av_frame_free(&s->DPB[i].frame);
  2601. }
  2602. for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++)
  2603. av_buffer_unref(&s->vps_list[i]);
  2604. for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
  2605. av_buffer_unref(&s->sps_list[i]);
  2606. for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
  2607. av_buffer_unref(&s->pps_list[i]);
  2608. for (i = 0; i < s->nals_allocated; i++)
  2609. av_freep(&s->nals[i].rbsp_buffer);
  2610. av_freep(&s->nals);
  2611. s->nals_allocated = 0;
  2612. return 0;
  2613. }
  2614. static av_cold int hevc_init_context(AVCodecContext *avctx)
  2615. {
  2616. HEVCContext *s = avctx->priv_data;
  2617. int i;
  2618. s->avctx = avctx;
  2619. s->tmp_frame = av_frame_alloc();
  2620. if (!s->tmp_frame)
  2621. goto fail;
  2622. s->output_frame = av_frame_alloc();
  2623. if (!s->output_frame)
  2624. goto fail;
  2625. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2626. s->DPB[i].frame = av_frame_alloc();
  2627. if (!s->DPB[i].frame)
  2628. goto fail;
  2629. s->DPB[i].tf.f = s->DPB[i].frame;
  2630. }
  2631. s->max_ra = INT_MAX;
  2632. s->md5_ctx = av_md5_alloc();
  2633. if (!s->md5_ctx)
  2634. goto fail;
  2635. ff_dsputil_init(&s->dsp, avctx);
  2636. s->context_initialized = 1;
  2637. return 0;
  2638. fail:
  2639. hevc_decode_free(avctx);
  2640. return AVERROR(ENOMEM);
  2641. }
  2642. static int hevc_update_thread_context(AVCodecContext *dst,
  2643. const AVCodecContext *src)
  2644. {
  2645. HEVCContext *s = dst->priv_data;
  2646. HEVCContext *s0 = src->priv_data;
  2647. int i, ret;
  2648. if (!s->context_initialized) {
  2649. ret = hevc_init_context(dst);
  2650. if (ret < 0)
  2651. return ret;
  2652. }
  2653. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2654. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2655. if (s0->DPB[i].frame->buf[0]) {
  2656. ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
  2657. if (ret < 0)
  2658. return ret;
  2659. }
  2660. }
  2661. for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++) {
  2662. av_buffer_unref(&s->vps_list[i]);
  2663. if (s0->vps_list[i]) {
  2664. s->vps_list[i] = av_buffer_ref(s0->vps_list[i]);
  2665. if (!s->vps_list[i])
  2666. return AVERROR(ENOMEM);
  2667. }
  2668. }
  2669. for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++) {
  2670. av_buffer_unref(&s->sps_list[i]);
  2671. if (s0->sps_list[i]) {
  2672. s->sps_list[i] = av_buffer_ref(s0->sps_list[i]);
  2673. if (!s->sps_list[i])
  2674. return AVERROR(ENOMEM);
  2675. }
  2676. }
  2677. for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
  2678. av_buffer_unref(&s->pps_list[i]);
  2679. if (s0->pps_list[i]) {
  2680. s->pps_list[i] = av_buffer_ref(s0->pps_list[i]);
  2681. if (!s->pps_list[i])
  2682. return AVERROR(ENOMEM);
  2683. }
  2684. }
  2685. if (s->sps != s0->sps)
  2686. ret = set_sps(s, s0->sps);
  2687. s->seq_decode = s0->seq_decode;
  2688. s->seq_output = s0->seq_output;
  2689. s->pocTid0 = s0->pocTid0;
  2690. s->max_ra = s0->max_ra;
  2691. s->is_nalff = s0->is_nalff;
  2692. s->nal_length_size = s0->nal_length_size;
  2693. if (s0->eos) {
  2694. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2695. s->max_ra = INT_MAX;
  2696. }
  2697. return 0;
  2698. }
  2699. static int hevc_decode_extradata(HEVCContext *s)
  2700. {
  2701. AVCodecContext *avctx = s->avctx;
  2702. GetByteContext gb;
  2703. int ret;
  2704. bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
  2705. if (avctx->extradata_size > 3 &&
  2706. (avctx->extradata[0] || avctx->extradata[1] ||
  2707. avctx->extradata[2] > 1)) {
  2708. /* It seems the extradata is encoded as hvcC format.
  2709. * Temporarily, we support configurationVersion==0 until 14496-15 3rd
  2710. * is finalized. When finalized, configurationVersion will be 1 and we
  2711. * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
  2712. int i, j, num_arrays, nal_len_size;
  2713. s->is_nalff = 1;
  2714. bytestream2_skip(&gb, 21);
  2715. nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
  2716. num_arrays = bytestream2_get_byte(&gb);
  2717. /* nal units in the hvcC always have length coded with 2 bytes,
  2718. * so put a fake nal_length_size = 2 while parsing them */
  2719. s->nal_length_size = 2;
  2720. /* Decode nal units from hvcC. */
  2721. for (i = 0; i < num_arrays; i++) {
  2722. int type = bytestream2_get_byte(&gb) & 0x3f;
  2723. int cnt = bytestream2_get_be16(&gb);
  2724. for (j = 0; j < cnt; j++) {
  2725. // +2 for the nal size field
  2726. int nalsize = bytestream2_peek_be16(&gb) + 2;
  2727. if (bytestream2_get_bytes_left(&gb) < nalsize) {
  2728. av_log(s->avctx, AV_LOG_ERROR,
  2729. "Invalid NAL unit size in extradata.\n");
  2730. return AVERROR_INVALIDDATA;
  2731. }
  2732. ret = decode_nal_units(s, gb.buffer, nalsize);
  2733. if (ret < 0) {
  2734. av_log(avctx, AV_LOG_ERROR,
  2735. "Decoding nal unit %d %d from hvcC failed\n",
  2736. type, i);
  2737. return ret;
  2738. }
  2739. bytestream2_skip(&gb, nalsize);
  2740. }
  2741. }
  2742. /* Now store right nal length size, that will be used to parse
  2743. * all other nals */
  2744. s->nal_length_size = nal_len_size;
  2745. } else {
  2746. s->is_nalff = 0;
  2747. ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
  2748. if (ret < 0)
  2749. return ret;
  2750. }
  2751. return 0;
  2752. }
  2753. static av_cold int hevc_decode_init(AVCodecContext *avctx)
  2754. {
  2755. HEVCContext *s = avctx->priv_data;
  2756. int ret;
  2757. ff_init_cabac_states();
  2758. avctx->internal->allocate_progress = 1;
  2759. ret = hevc_init_context(avctx);
  2760. if (ret < 0)
  2761. return ret;
  2762. if (avctx->extradata_size > 0 && avctx->extradata) {
  2763. ret = hevc_decode_extradata(s);
  2764. if (ret < 0) {
  2765. hevc_decode_free(avctx);
  2766. return ret;
  2767. }
  2768. }
  2769. return 0;
  2770. }
  2771. static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
  2772. {
  2773. HEVCContext *s = avctx->priv_data;
  2774. int ret;
  2775. memset(s, 0, sizeof(*s));
  2776. ret = hevc_init_context(avctx);
  2777. if (ret < 0)
  2778. return ret;
  2779. return 0;
  2780. }
  2781. static void hevc_decode_flush(AVCodecContext *avctx)
  2782. {
  2783. HEVCContext *s = avctx->priv_data;
  2784. ff_hevc_flush_dpb(s);
  2785. s->max_ra = INT_MAX;
  2786. }
  2787. #define OFFSET(x) offsetof(HEVCContext, x)
  2788. #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  2789. static const AVProfile profiles[] = {
  2790. { FF_PROFILE_HEVC_MAIN, "Main" },
  2791. { FF_PROFILE_HEVC_MAIN_10, "Main 10" },
  2792. { FF_PROFILE_HEVC_MAIN_STILL_PICTURE, "Main Still Picture" },
  2793. { FF_PROFILE_UNKNOWN },
  2794. };
  2795. static const AVOption options[] = {
  2796. { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
  2797. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
  2798. { NULL },
  2799. };
  2800. static const AVClass hevc_decoder_class = {
  2801. .class_name = "HEVC decoder",
  2802. .item_name = av_default_item_name,
  2803. .option = options,
  2804. .version = LIBAVUTIL_VERSION_INT,
  2805. };
  2806. AVCodec ff_hevc_decoder = {
  2807. .name = "hevc",
  2808. .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
  2809. .type = AVMEDIA_TYPE_VIDEO,
  2810. .id = AV_CODEC_ID_HEVC,
  2811. .priv_data_size = sizeof(HEVCContext),
  2812. .priv_class = &hevc_decoder_class,
  2813. .init = hevc_decode_init,
  2814. .close = hevc_decode_free,
  2815. .decode = hevc_decode_frame,
  2816. .flush = hevc_decode_flush,
  2817. .update_thread_context = hevc_update_thread_context,
  2818. .init_thread_copy = hevc_init_thread_copy,
  2819. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY |
  2820. CODEC_CAP_FRAME_THREADS,
  2821. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  2822. };