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.

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