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.

3177 lines
117KB

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