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.

3202 lines
118KB

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