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.

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