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.

3118 lines
115KB

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