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.

3257 lines
119KB

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