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.

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