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.

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