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.

3134 lines
116KB

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