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.

3463 lines
131KB

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