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.

3217 lines
118KB

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