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.

3475 lines
132KB

  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, HEVCSPS *sps)
  241. {
  242. int ret, i;
  243. frame->width = s->avctx->width + 2;
  244. frame->height = s->avctx->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->width;
  252. frame->height = s->avctx->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. s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
  640. if (!s->pps->cu_qp_delta_enabled_flag)
  641. s->HEVClc->qp_y = s->sh.slice_qp;
  642. s->slice_initialized = 1;
  643. s->HEVClc->tu.cu_qp_offset_cb = 0;
  644. s->HEVClc->tu.cu_qp_offset_cr = 0;
  645. return 0;
  646. }
  647. #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
  648. #define SET_SAO(elem, value) \
  649. do { \
  650. if (!sao_merge_up_flag && !sao_merge_left_flag) \
  651. sao->elem = value; \
  652. else if (sao_merge_left_flag) \
  653. sao->elem = CTB(s->sao, rx-1, ry).elem; \
  654. else if (sao_merge_up_flag) \
  655. sao->elem = CTB(s->sao, rx, ry-1).elem; \
  656. else \
  657. sao->elem = 0; \
  658. } while (0)
  659. static void hls_sao_param(HEVCContext *s, int rx, int ry)
  660. {
  661. HEVCLocalContext *lc = s->HEVClc;
  662. int sao_merge_left_flag = 0;
  663. int sao_merge_up_flag = 0;
  664. SAOParams *sao = &CTB(s->sao, rx, ry);
  665. int c_idx, i;
  666. if (s->sh.slice_sample_adaptive_offset_flag[0] ||
  667. s->sh.slice_sample_adaptive_offset_flag[1]) {
  668. if (rx > 0) {
  669. if (lc->ctb_left_flag)
  670. sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
  671. }
  672. if (ry > 0 && !sao_merge_left_flag) {
  673. if (lc->ctb_up_flag)
  674. sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
  675. }
  676. }
  677. for (c_idx = 0; c_idx < 3; c_idx++) {
  678. int log2_sao_offset_scale = c_idx == 0 ? s->pps->log2_sao_offset_scale_luma :
  679. s->pps->log2_sao_offset_scale_chroma;
  680. if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
  681. sao->type_idx[c_idx] = SAO_NOT_APPLIED;
  682. continue;
  683. }
  684. if (c_idx == 2) {
  685. sao->type_idx[2] = sao->type_idx[1];
  686. sao->eo_class[2] = sao->eo_class[1];
  687. } else {
  688. SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
  689. }
  690. if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
  691. continue;
  692. for (i = 0; i < 4; i++)
  693. SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
  694. if (sao->type_idx[c_idx] == SAO_BAND) {
  695. for (i = 0; i < 4; i++) {
  696. if (sao->offset_abs[c_idx][i]) {
  697. SET_SAO(offset_sign[c_idx][i],
  698. ff_hevc_sao_offset_sign_decode(s));
  699. } else {
  700. sao->offset_sign[c_idx][i] = 0;
  701. }
  702. }
  703. SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
  704. } else if (c_idx != 2) {
  705. SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
  706. }
  707. // Inferred parameters
  708. sao->offset_val[c_idx][0] = 0;
  709. for (i = 0; i < 4; i++) {
  710. sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
  711. if (sao->type_idx[c_idx] == SAO_EDGE) {
  712. if (i > 1)
  713. sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
  714. } else if (sao->offset_sign[c_idx][i]) {
  715. sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
  716. }
  717. sao->offset_val[c_idx][i + 1] <<= log2_sao_offset_scale;
  718. }
  719. }
  720. }
  721. #undef SET_SAO
  722. #undef CTB
  723. static int hls_cross_component_pred(HEVCContext *s, int idx) {
  724. HEVCLocalContext *lc = s->HEVClc;
  725. int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
  726. if (log2_res_scale_abs_plus1 != 0) {
  727. int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
  728. lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
  729. (1 - 2 * res_scale_sign_flag);
  730. } else {
  731. lc->tu.res_scale_val = 0;
  732. }
  733. return 0;
  734. }
  735. static int hls_transform_unit(HEVCContext *s, int x0, int y0,
  736. int xBase, int yBase, int cb_xBase, int cb_yBase,
  737. int log2_cb_size, int log2_trafo_size,
  738. int trafo_depth, int blk_idx, int *cbf_cb, int *cbf_cr)
  739. {
  740. HEVCLocalContext *lc = s->HEVClc;
  741. const int log2_trafo_size_c = log2_trafo_size - s->sps->hshift[1];
  742. int i;
  743. if (lc->cu.pred_mode == MODE_INTRA) {
  744. int trafo_size = 1 << log2_trafo_size;
  745. ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
  746. s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
  747. }
  748. if (lc->tt.cbf_luma || cbf_cb[0] || cbf_cr[0] ||
  749. (s->sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
  750. int scan_idx = SCAN_DIAG;
  751. int scan_idx_c = SCAN_DIAG;
  752. int cbf_luma = lc->tt.cbf_luma;
  753. int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
  754. (s->sps->chroma_format_idc == 2 &&
  755. (cbf_cb[1] || cbf_cr[1]));
  756. if (s->pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
  757. lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
  758. if (lc->tu.cu_qp_delta != 0)
  759. if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
  760. lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
  761. lc->tu.is_cu_qp_delta_coded = 1;
  762. if (lc->tu.cu_qp_delta < -(26 + s->sps->qp_bd_offset / 2) ||
  763. lc->tu.cu_qp_delta > (25 + s->sps->qp_bd_offset / 2)) {
  764. av_log(s->avctx, AV_LOG_ERROR,
  765. "The cu_qp_delta %d is outside the valid range "
  766. "[%d, %d].\n",
  767. lc->tu.cu_qp_delta,
  768. -(26 + s->sps->qp_bd_offset / 2),
  769. (25 + s->sps->qp_bd_offset / 2));
  770. return AVERROR_INVALIDDATA;
  771. }
  772. ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
  773. }
  774. if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
  775. !lc->cu.cu_transquant_bypass_flag && !lc->tu.is_cu_chroma_qp_offset_coded) {
  776. int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
  777. if (cu_chroma_qp_offset_flag) {
  778. int cu_chroma_qp_offset_idx = 0;
  779. if (s->pps->chroma_qp_offset_list_len_minus1 > 0) {
  780. cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
  781. av_log(s->avctx, AV_LOG_ERROR,
  782. "cu_chroma_qp_offset_idx not yet tested.\n");
  783. }
  784. lc->tu.cu_qp_offset_cb = s->pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
  785. lc->tu.cu_qp_offset_cr = s->pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
  786. } else {
  787. lc->tu.cu_qp_offset_cb = 0;
  788. lc->tu.cu_qp_offset_cr = 0;
  789. }
  790. lc->tu.is_cu_chroma_qp_offset_coded = 1;
  791. }
  792. if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
  793. if (lc->tu.intra_pred_mode >= 6 &&
  794. lc->tu.intra_pred_mode <= 14) {
  795. scan_idx = SCAN_VERT;
  796. } else if (lc->tu.intra_pred_mode >= 22 &&
  797. lc->tu.intra_pred_mode <= 30) {
  798. scan_idx = SCAN_HORIZ;
  799. }
  800. if (lc->tu.intra_pred_mode_c >= 6 &&
  801. lc->tu.intra_pred_mode_c <= 14) {
  802. scan_idx_c = SCAN_VERT;
  803. } else if (lc->tu.intra_pred_mode_c >= 22 &&
  804. lc->tu.intra_pred_mode_c <= 30) {
  805. scan_idx_c = SCAN_HORIZ;
  806. }
  807. }
  808. lc->tu.cross_pf = 0;
  809. if (cbf_luma)
  810. ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
  811. if (log2_trafo_size > 2 || s->sps->chroma_format_idc == 3) {
  812. int trafo_size_h = 1 << (log2_trafo_size_c + s->sps->hshift[1]);
  813. int trafo_size_v = 1 << (log2_trafo_size_c + s->sps->vshift[1]);
  814. lc->tu.cross_pf = (s->pps->cross_component_prediction_enabled_flag && cbf_luma &&
  815. (lc->cu.pred_mode == MODE_INTER ||
  816. (lc->tu.chroma_mode_c == 4)));
  817. if (lc->tu.cross_pf) {
  818. hls_cross_component_pred(s, 0);
  819. }
  820. for (i = 0; i < (s->sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  821. if (lc->cu.pred_mode == MODE_INTRA) {
  822. ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
  823. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
  824. }
  825. if (cbf_cb[i])
  826. ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
  827. log2_trafo_size_c, scan_idx_c, 1);
  828. else
  829. if (lc->tu.cross_pf) {
  830. ptrdiff_t stride = s->frame->linesize[1];
  831. int hshift = s->sps->hshift[1];
  832. int vshift = s->sps->vshift[1];
  833. int16_t *coeffs_y = lc->tu.coeffs[0];
  834. int16_t *coeffs = lc->tu.coeffs[1];
  835. int size = 1 << log2_trafo_size_c;
  836. uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
  837. ((x0 >> hshift) << s->sps->pixel_shift)];
  838. for (i = 0; i < (size * size); i++) {
  839. coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
  840. }
  841. s->hevcdsp.transform_add[log2_trafo_size-2](dst, coeffs, stride);
  842. }
  843. }
  844. if (lc->tu.cross_pf) {
  845. hls_cross_component_pred(s, 1);
  846. }
  847. for (i = 0; i < (s->sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  848. if (lc->cu.pred_mode == MODE_INTRA) {
  849. ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
  850. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
  851. }
  852. if (cbf_cr[i])
  853. ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
  854. log2_trafo_size_c, scan_idx_c, 2);
  855. else
  856. if (lc->tu.cross_pf) {
  857. ptrdiff_t stride = s->frame->linesize[2];
  858. int hshift = s->sps->hshift[2];
  859. int vshift = s->sps->vshift[2];
  860. int16_t *coeffs_y = lc->tu.coeffs[0];
  861. int16_t *coeffs = lc->tu.coeffs[1];
  862. int size = 1 << log2_trafo_size_c;
  863. uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
  864. ((x0 >> hshift) << s->sps->pixel_shift)];
  865. for (i = 0; i < (size * size); i++) {
  866. coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
  867. }
  868. s->hevcdsp.transform_add[log2_trafo_size-2](dst, coeffs, stride);
  869. }
  870. }
  871. } else if (blk_idx == 3) {
  872. int trafo_size_h = 1 << (log2_trafo_size + 1);
  873. int trafo_size_v = 1 << (log2_trafo_size + s->sps->vshift[1]);
  874. for (i = 0; i < (s->sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  875. if (lc->cu.pred_mode == MODE_INTRA) {
  876. ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
  877. trafo_size_h, trafo_size_v);
  878. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
  879. }
  880. if (cbf_cb[i])
  881. ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
  882. log2_trafo_size, scan_idx_c, 1);
  883. }
  884. for (i = 0; i < (s->sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  885. if (lc->cu.pred_mode == MODE_INTRA) {
  886. ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
  887. trafo_size_h, trafo_size_v);
  888. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
  889. }
  890. if (cbf_cr[i])
  891. ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
  892. log2_trafo_size, scan_idx_c, 2);
  893. }
  894. }
  895. } else if (lc->cu.pred_mode == MODE_INTRA) {
  896. if (log2_trafo_size > 2 || s->sps->chroma_format_idc == 3) {
  897. int trafo_size_h = 1 << (log2_trafo_size_c + s->sps->hshift[1]);
  898. int trafo_size_v = 1 << (log2_trafo_size_c + s->sps->vshift[1]);
  899. ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
  900. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
  901. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
  902. if (s->sps->chroma_format_idc == 2) {
  903. ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
  904. trafo_size_h, trafo_size_v);
  905. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
  906. s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
  907. }
  908. } else if (blk_idx == 3) {
  909. int trafo_size_h = 1 << (log2_trafo_size + 1);
  910. int trafo_size_v = 1 << (log2_trafo_size + s->sps->vshift[1]);
  911. ff_hevc_set_neighbour_available(s, xBase, yBase,
  912. trafo_size_h, trafo_size_v);
  913. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
  914. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
  915. if (s->sps->chroma_format_idc == 2) {
  916. ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
  917. trafo_size_h, trafo_size_v);
  918. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
  919. s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
  920. }
  921. }
  922. }
  923. return 0;
  924. }
  925. static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
  926. {
  927. int cb_size = 1 << log2_cb_size;
  928. int log2_min_pu_size = s->sps->log2_min_pu_size;
  929. int min_pu_width = s->sps->min_pu_width;
  930. int x_end = FFMIN(x0 + cb_size, s->sps->width);
  931. int y_end = FFMIN(y0 + cb_size, s->sps->height);
  932. int i, j;
  933. for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
  934. for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
  935. s->is_pcm[i + j * min_pu_width] = 2;
  936. }
  937. static int hls_transform_tree(HEVCContext *s, int x0, int y0,
  938. int xBase, int yBase, int cb_xBase, int cb_yBase,
  939. int log2_cb_size, int log2_trafo_size,
  940. int trafo_depth, int blk_idx, int *base_cbf_cb, int *base_cbf_cr)
  941. {
  942. HEVCLocalContext *lc = s->HEVClc;
  943. uint8_t split_transform_flag;
  944. int cbf_cb[2];
  945. int cbf_cr[2];
  946. int ret;
  947. cbf_cb[0] = base_cbf_cb[0];
  948. cbf_cb[1] = base_cbf_cb[1];
  949. cbf_cr[0] = base_cbf_cr[0];
  950. cbf_cr[1] = base_cbf_cr[1];
  951. if (lc->cu.intra_split_flag) {
  952. if (trafo_depth == 1) {
  953. lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
  954. if (s->sps->chroma_format_idc == 3) {
  955. lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
  956. lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
  957. } else {
  958. lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
  959. lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
  960. }
  961. }
  962. } else {
  963. lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
  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. lc->tt.cbf_luma = 1;
  968. lc->tt.inter_split_flag = s->sps->max_transform_hierarchy_depth_inter == 0 &&
  969. lc->cu.pred_mode == MODE_INTER &&
  970. lc->cu.part_mode != PART_2Nx2N &&
  971. trafo_depth == 0;
  972. if (log2_trafo_size <= s->sps->log2_max_trafo_size &&
  973. log2_trafo_size > s->sps->log2_min_tb_size &&
  974. trafo_depth < lc->cu.max_trafo_depth &&
  975. !(lc->cu.intra_split_flag && trafo_depth == 0)) {
  976. split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
  977. } else {
  978. split_transform_flag = log2_trafo_size > s->sps->log2_max_trafo_size ||
  979. (lc->cu.intra_split_flag && trafo_depth == 0) ||
  980. lc->tt.inter_split_flag;
  981. }
  982. if (log2_trafo_size > 2 || s->sps->chroma_format_idc == 3) {
  983. if (trafo_depth == 0 || cbf_cb[0]) {
  984. cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  985. if (s->sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
  986. cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  987. }
  988. } else if (trafo_depth == 0) {
  989. cbf_cb[0] =
  990. cbf_cb[1] = 0;
  991. }
  992. if (trafo_depth == 0 || cbf_cr[0]) {
  993. cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  994. if (s->sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
  995. cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
  996. }
  997. } else if (trafo_depth == 0) {
  998. cbf_cr[0] =
  999. cbf_cr[1] = 0;
  1000. }
  1001. }
  1002. if (split_transform_flag) {
  1003. int x1 = x0 + ((1 << log2_trafo_size) >> 1);
  1004. int y1 = y0 + ((1 << log2_trafo_size) >> 1);
  1005. ret = hls_transform_tree(s, x0, y0, x0, y0, cb_xBase, cb_yBase,
  1006. log2_cb_size, log2_trafo_size - 1,
  1007. trafo_depth + 1, 0, cbf_cb, cbf_cr);
  1008. if (ret < 0)
  1009. return ret;
  1010. ret = hls_transform_tree(s, x1, y0, x0, y0, cb_xBase, cb_yBase,
  1011. log2_cb_size, log2_trafo_size - 1,
  1012. trafo_depth + 1, 1, cbf_cb, cbf_cr);
  1013. if (ret < 0)
  1014. return ret;
  1015. ret = hls_transform_tree(s, x0, y1, x0, y0, cb_xBase, cb_yBase,
  1016. log2_cb_size, log2_trafo_size - 1,
  1017. trafo_depth + 1, 2, cbf_cb, cbf_cr);
  1018. if (ret < 0)
  1019. return ret;
  1020. ret = hls_transform_tree(s, x1, y1, x0, y0, cb_xBase, cb_yBase,
  1021. log2_cb_size, log2_trafo_size - 1,
  1022. trafo_depth + 1, 3, cbf_cb, cbf_cr);
  1023. if (ret < 0)
  1024. return ret;
  1025. } else {
  1026. int min_tu_size = 1 << s->sps->log2_min_tb_size;
  1027. int log2_min_tu_size = s->sps->log2_min_tb_size;
  1028. int min_tu_width = s->sps->min_tb_width;
  1029. if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
  1030. cbf_cb[0] || cbf_cr[0] ||
  1031. (s->sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
  1032. lc->tt.cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
  1033. }
  1034. ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
  1035. log2_cb_size, log2_trafo_size, trafo_depth,
  1036. blk_idx, cbf_cb, cbf_cr);
  1037. if (ret < 0)
  1038. return ret;
  1039. // TODO: store cbf_luma somewhere else
  1040. if (lc->tt.cbf_luma) {
  1041. int i, j;
  1042. for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
  1043. for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
  1044. int x_tu = (x0 + j) >> log2_min_tu_size;
  1045. int y_tu = (y0 + i) >> log2_min_tu_size;
  1046. s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
  1047. }
  1048. }
  1049. if (!s->sh.disable_deblocking_filter_flag) {
  1050. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
  1051. if (s->pps->transquant_bypass_enable_flag &&
  1052. lc->cu.cu_transquant_bypass_flag)
  1053. set_deblocking_bypass(s, x0, y0, log2_trafo_size);
  1054. }
  1055. }
  1056. return 0;
  1057. }
  1058. static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1059. {
  1060. //TODO: non-4:2:0 support
  1061. HEVCLocalContext *lc = s->HEVClc;
  1062. GetBitContext gb;
  1063. int cb_size = 1 << log2_cb_size;
  1064. int stride0 = s->frame->linesize[0];
  1065. uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->sps->pixel_shift)];
  1066. int stride1 = s->frame->linesize[1];
  1067. uint8_t *dst1 = &s->frame->data[1][(y0 >> s->sps->vshift[1]) * stride1 + ((x0 >> s->sps->hshift[1]) << s->sps->pixel_shift)];
  1068. int stride2 = s->frame->linesize[2];
  1069. uint8_t *dst2 = &s->frame->data[2][(y0 >> s->sps->vshift[2]) * stride2 + ((x0 >> s->sps->hshift[2]) << s->sps->pixel_shift)];
  1070. int length = cb_size * cb_size * s->sps->pcm.bit_depth +
  1071. (((cb_size >> s->sps->hshift[1]) * (cb_size >> s->sps->vshift[1])) +
  1072. ((cb_size >> s->sps->hshift[2]) * (cb_size >> s->sps->vshift[2]))) *
  1073. s->sps->pcm.bit_depth_chroma;
  1074. const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
  1075. int ret;
  1076. if (!s->sh.disable_deblocking_filter_flag)
  1077. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  1078. ret = init_get_bits(&gb, pcm, length);
  1079. if (ret < 0)
  1080. return ret;
  1081. s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, s->sps->pcm.bit_depth);
  1082. s->hevcdsp.put_pcm(dst1, stride1,
  1083. cb_size >> s->sps->hshift[1],
  1084. cb_size >> s->sps->vshift[1],
  1085. &gb, s->sps->pcm.bit_depth_chroma);
  1086. s->hevcdsp.put_pcm(dst2, stride2,
  1087. cb_size >> s->sps->hshift[2],
  1088. cb_size >> s->sps->vshift[2],
  1089. &gb, s->sps->pcm.bit_depth_chroma);
  1090. return 0;
  1091. }
  1092. /**
  1093. * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
  1094. *
  1095. * @param s HEVC decoding context
  1096. * @param dst target buffer for block data at block position
  1097. * @param dststride stride of the dst buffer
  1098. * @param ref reference picture buffer at origin (0, 0)
  1099. * @param mv motion vector (relative to block position) to get pixel data from
  1100. * @param x_off horizontal position of block from origin (0, 0)
  1101. * @param y_off vertical position of block from origin (0, 0)
  1102. * @param block_w width of block
  1103. * @param block_h height of block
  1104. * @param luma_weight weighting factor applied to the luma prediction
  1105. * @param luma_offset additive offset applied to the luma prediction value
  1106. */
  1107. static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
  1108. AVFrame *ref, const Mv *mv, int x_off, int y_off,
  1109. int block_w, int block_h, int luma_weight, int luma_offset)
  1110. {
  1111. HEVCLocalContext *lc = s->HEVClc;
  1112. uint8_t *src = ref->data[0];
  1113. ptrdiff_t srcstride = ref->linesize[0];
  1114. int pic_width = s->sps->width;
  1115. int pic_height = s->sps->height;
  1116. int mx = mv->x & 3;
  1117. int my = mv->y & 3;
  1118. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1119. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  1120. int idx = ff_hevc_pel_weight[block_w];
  1121. x_off += mv->x >> 2;
  1122. y_off += mv->y >> 2;
  1123. src += y_off * srcstride + (x_off << s->sps->pixel_shift);
  1124. if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
  1125. x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  1126. y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  1127. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1128. int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1129. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1130. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
  1131. edge_emu_stride, srcstride,
  1132. block_w + QPEL_EXTRA,
  1133. block_h + QPEL_EXTRA,
  1134. x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
  1135. pic_width, pic_height);
  1136. src = lc->edge_emu_buffer + buf_offset;
  1137. srcstride = edge_emu_stride;
  1138. }
  1139. if (!weight_flag)
  1140. s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
  1141. block_h, mx, my, block_w);
  1142. else
  1143. s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
  1144. block_h, s->sh.luma_log2_weight_denom,
  1145. luma_weight, luma_offset, mx, my, block_w);
  1146. }
  1147. /**
  1148. * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
  1149. *
  1150. * @param s HEVC decoding context
  1151. * @param dst target buffer for block data at block position
  1152. * @param dststride stride of the dst buffer
  1153. * @param ref0 reference picture0 buffer at origin (0, 0)
  1154. * @param mv0 motion vector0 (relative to block position) to get pixel data from
  1155. * @param x_off horizontal position of block from origin (0, 0)
  1156. * @param y_off vertical position of block from origin (0, 0)
  1157. * @param block_w width of block
  1158. * @param block_h height of block
  1159. * @param ref1 reference picture1 buffer at origin (0, 0)
  1160. * @param mv1 motion vector1 (relative to block position) to get pixel data from
  1161. * @param current_mv current motion vector structure
  1162. */
  1163. static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
  1164. AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
  1165. int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
  1166. {
  1167. HEVCLocalContext *lc = s->HEVClc;
  1168. DECLARE_ALIGNED(16, int16_t, tmp[MAX_PB_SIZE * MAX_PB_SIZE]);
  1169. ptrdiff_t src0stride = ref0->linesize[0];
  1170. ptrdiff_t src1stride = ref1->linesize[0];
  1171. int pic_width = s->sps->width;
  1172. int pic_height = s->sps->height;
  1173. int mx0 = mv0->x & 3;
  1174. int my0 = mv0->y & 3;
  1175. int mx1 = mv1->x & 3;
  1176. int my1 = mv1->y & 3;
  1177. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1178. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  1179. int x_off0 = x_off + (mv0->x >> 2);
  1180. int y_off0 = y_off + (mv0->y >> 2);
  1181. int x_off1 = x_off + (mv1->x >> 2);
  1182. int y_off1 = y_off + (mv1->y >> 2);
  1183. int idx = ff_hevc_pel_weight[block_w];
  1184. uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->sps->pixel_shift);
  1185. uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->sps->pixel_shift);
  1186. if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
  1187. x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  1188. y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  1189. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1190. int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1191. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1192. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
  1193. edge_emu_stride, src0stride,
  1194. block_w + QPEL_EXTRA,
  1195. block_h + QPEL_EXTRA,
  1196. x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
  1197. pic_width, pic_height);
  1198. src0 = lc->edge_emu_buffer + buf_offset;
  1199. src0stride = edge_emu_stride;
  1200. }
  1201. if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
  1202. x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  1203. y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  1204. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1205. int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1206. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->sps->pixel_shift);
  1207. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
  1208. edge_emu_stride, src1stride,
  1209. block_w + QPEL_EXTRA,
  1210. block_h + QPEL_EXTRA,
  1211. x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
  1212. pic_width, pic_height);
  1213. src1 = lc->edge_emu_buffer2 + buf_offset;
  1214. src1stride = edge_emu_stride;
  1215. }
  1216. s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](tmp, MAX_PB_SIZE, src0, src0stride,
  1217. block_h, mx0, my0, block_w);
  1218. if (!weight_flag)
  1219. s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, tmp, MAX_PB_SIZE,
  1220. block_h, mx1, my1, block_w);
  1221. else
  1222. s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, tmp, MAX_PB_SIZE,
  1223. block_h, s->sh.luma_log2_weight_denom,
  1224. s->sh.luma_weight_l0[current_mv->ref_idx[0]],
  1225. s->sh.luma_weight_l1[current_mv->ref_idx[1]],
  1226. s->sh.luma_offset_l0[current_mv->ref_idx[0]],
  1227. s->sh.luma_offset_l1[current_mv->ref_idx[1]],
  1228. mx1, my1, block_w);
  1229. }
  1230. /**
  1231. * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
  1232. *
  1233. * @param s HEVC decoding context
  1234. * @param dst1 target buffer for block data at block position (U plane)
  1235. * @param dst2 target buffer for block data at block position (V plane)
  1236. * @param dststride stride of the dst1 and dst2 buffers
  1237. * @param ref reference picture buffer at origin (0, 0)
  1238. * @param mv motion vector (relative to block position) to get pixel data from
  1239. * @param x_off horizontal position of block from origin (0, 0)
  1240. * @param y_off vertical position of block from origin (0, 0)
  1241. * @param block_w width of block
  1242. * @param block_h height of block
  1243. * @param chroma_weight weighting factor applied to the chroma prediction
  1244. * @param chroma_offset additive offset applied to the chroma prediction value
  1245. */
  1246. static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
  1247. ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
  1248. int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
  1249. {
  1250. HEVCLocalContext *lc = s->HEVClc;
  1251. int pic_width = s->sps->width >> s->sps->hshift[1];
  1252. int pic_height = s->sps->height >> s->sps->vshift[1];
  1253. const Mv *mv = &current_mv->mv[reflist];
  1254. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1255. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  1256. int idx = ff_hevc_pel_weight[block_w];
  1257. int hshift = s->sps->hshift[1];
  1258. int vshift = s->sps->vshift[1];
  1259. intptr_t mx = mv->x & ((1 << (2 + hshift)) - 1);
  1260. intptr_t my = mv->y & ((1 << (2 + vshift)) - 1);
  1261. intptr_t _mx = mx << (1 - hshift);
  1262. intptr_t _my = my << (1 - vshift);
  1263. x_off += mv->x >> (2 + hshift);
  1264. y_off += mv->y >> (2 + vshift);
  1265. src0 += y_off * srcstride + (x_off << s->sps->pixel_shift);
  1266. if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
  1267. x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1268. y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1269. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1270. int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->sps->pixel_shift));
  1271. int buf_offset0 = EPEL_EXTRA_BEFORE *
  1272. (edge_emu_stride + (1 << s->sps->pixel_shift));
  1273. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
  1274. edge_emu_stride, srcstride,
  1275. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1276. x_off - EPEL_EXTRA_BEFORE,
  1277. y_off - EPEL_EXTRA_BEFORE,
  1278. pic_width, pic_height);
  1279. src0 = lc->edge_emu_buffer + buf_offset0;
  1280. srcstride = edge_emu_stride;
  1281. }
  1282. if (!weight_flag)
  1283. s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
  1284. block_h, _mx, _my, block_w);
  1285. else
  1286. s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
  1287. block_h, s->sh.chroma_log2_weight_denom,
  1288. chroma_weight, chroma_offset, _mx, _my, block_w);
  1289. }
  1290. /**
  1291. * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
  1292. *
  1293. * @param s HEVC decoding context
  1294. * @param dst target buffer for block data at block position
  1295. * @param dststride stride of the dst buffer
  1296. * @param ref0 reference picture0 buffer at origin (0, 0)
  1297. * @param mv0 motion vector0 (relative to block position) to get pixel data from
  1298. * @param x_off horizontal position of block from origin (0, 0)
  1299. * @param y_off vertical position of block from origin (0, 0)
  1300. * @param block_w width of block
  1301. * @param block_h height of block
  1302. * @param ref1 reference picture1 buffer at origin (0, 0)
  1303. * @param mv1 motion vector1 (relative to block position) to get pixel data from
  1304. * @param current_mv current motion vector structure
  1305. * @param cidx chroma component(cb, cr)
  1306. */
  1307. static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
  1308. int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
  1309. {
  1310. DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
  1311. int tmpstride = MAX_PB_SIZE;
  1312. HEVCLocalContext *lc = s->HEVClc;
  1313. uint8_t *src1 = ref0->data[cidx+1];
  1314. uint8_t *src2 = ref1->data[cidx+1];
  1315. ptrdiff_t src1stride = ref0->linesize[cidx+1];
  1316. ptrdiff_t src2stride = ref1->linesize[cidx+1];
  1317. int weight_flag = (s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
  1318. (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag);
  1319. int pic_width = s->sps->width >> s->sps->hshift[1];
  1320. int pic_height = s->sps->height >> s->sps->vshift[1];
  1321. Mv *mv0 = &current_mv->mv[0];
  1322. Mv *mv1 = &current_mv->mv[1];
  1323. int hshift = s->sps->hshift[1];
  1324. int vshift = s->sps->vshift[1];
  1325. intptr_t mx0 = mv0->x & ((1 << (2 + hshift)) - 1);
  1326. intptr_t my0 = mv0->y & ((1 << (2 + vshift)) - 1);
  1327. intptr_t mx1 = mv1->x & ((1 << (2 + hshift)) - 1);
  1328. intptr_t my1 = mv1->y & ((1 << (2 + vshift)) - 1);
  1329. intptr_t _mx0 = mx0 << (1 - hshift);
  1330. intptr_t _my0 = my0 << (1 - vshift);
  1331. intptr_t _mx1 = mx1 << (1 - hshift);
  1332. intptr_t _my1 = my1 << (1 - vshift);
  1333. int x_off0 = x_off + (mv0->x >> (2 + hshift));
  1334. int y_off0 = y_off + (mv0->y >> (2 + vshift));
  1335. int x_off1 = x_off + (mv1->x >> (2 + hshift));
  1336. int y_off1 = y_off + (mv1->y >> (2 + vshift));
  1337. int idx = ff_hevc_pel_weight[block_w];
  1338. src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << s->sps->pixel_shift);
  1339. src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << s->sps->pixel_shift);
  1340. if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
  1341. x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1342. y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1343. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1344. int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->sps->pixel_shift));
  1345. int buf_offset1 = EPEL_EXTRA_BEFORE *
  1346. (edge_emu_stride + (1 << s->sps->pixel_shift));
  1347. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
  1348. edge_emu_stride, src1stride,
  1349. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1350. x_off0 - EPEL_EXTRA_BEFORE,
  1351. y_off0 - EPEL_EXTRA_BEFORE,
  1352. pic_width, pic_height);
  1353. src1 = lc->edge_emu_buffer + buf_offset1;
  1354. src1stride = edge_emu_stride;
  1355. }
  1356. if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
  1357. x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1358. y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1359. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
  1360. int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->sps->pixel_shift));
  1361. int buf_offset1 = EPEL_EXTRA_BEFORE *
  1362. (edge_emu_stride + (1 << s->sps->pixel_shift));
  1363. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
  1364. edge_emu_stride, src2stride,
  1365. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1366. x_off1 - EPEL_EXTRA_BEFORE,
  1367. y_off1 - EPEL_EXTRA_BEFORE,
  1368. pic_width, pic_height);
  1369. src2 = lc->edge_emu_buffer2 + buf_offset1;
  1370. src2stride = edge_emu_stride;
  1371. }
  1372. s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](tmp, tmpstride, src1, src1stride,
  1373. block_h, _mx0, _my0, block_w);
  1374. if (!weight_flag)
  1375. s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
  1376. src2, src2stride, tmp, tmpstride,
  1377. block_h, _mx1, _my1, block_w);
  1378. else
  1379. s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
  1380. src2, src2stride, tmp, tmpstride,
  1381. block_h,
  1382. s->sh.chroma_log2_weight_denom,
  1383. s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
  1384. s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
  1385. s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
  1386. s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
  1387. _mx1, _my1, block_w);
  1388. }
  1389. static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
  1390. const Mv *mv, int y0, int height)
  1391. {
  1392. int y = (mv->y >> 2) + y0 + height + 9;
  1393. if (s->threads_type == FF_THREAD_FRAME )
  1394. ff_thread_await_progress(&ref->tf, y, 0);
  1395. }
  1396. static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
  1397. int nPbW, int nPbH,
  1398. int log2_cb_size, int partIdx, int idx)
  1399. {
  1400. #define POS(c_idx, x, y) \
  1401. &s->frame->data[c_idx][((y) >> s->sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
  1402. (((x) >> s->sps->hshift[c_idx]) << s->sps->pixel_shift)]
  1403. HEVCLocalContext *lc = s->HEVClc;
  1404. int merge_idx = 0;
  1405. struct MvField current_mv = {{{ 0 }}};
  1406. int min_pu_width = s->sps->min_pu_width;
  1407. MvField *tab_mvf = s->ref->tab_mvf;
  1408. RefPicList *refPicList = s->ref->refPicList;
  1409. HEVCFrame *ref0, *ref1;
  1410. uint8_t *dst0 = POS(0, x0, y0);
  1411. uint8_t *dst1 = POS(1, x0, y0);
  1412. uint8_t *dst2 = POS(2, x0, y0);
  1413. int log2_min_cb_size = s->sps->log2_min_cb_size;
  1414. int min_cb_width = s->sps->min_cb_width;
  1415. int x_cb = x0 >> log2_min_cb_size;
  1416. int y_cb = y0 >> log2_min_cb_size;
  1417. int ref_idx[2];
  1418. int mvp_flag[2];
  1419. int x_pu, y_pu;
  1420. int i, j;
  1421. if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
  1422. if (s->sh.max_num_merge_cand > 1)
  1423. merge_idx = ff_hevc_merge_idx_decode(s);
  1424. else
  1425. merge_idx = 0;
  1426. ff_hevc_luma_mv_merge_mode(s, x0, y0,
  1427. 1 << log2_cb_size,
  1428. 1 << log2_cb_size,
  1429. log2_cb_size, partIdx,
  1430. merge_idx, &current_mv);
  1431. x_pu = x0 >> s->sps->log2_min_pu_size;
  1432. y_pu = y0 >> s->sps->log2_min_pu_size;
  1433. for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  1434. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  1435. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1436. } else { /* MODE_INTER */
  1437. lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
  1438. if (lc->pu.merge_flag) {
  1439. if (s->sh.max_num_merge_cand > 1)
  1440. merge_idx = ff_hevc_merge_idx_decode(s);
  1441. else
  1442. merge_idx = 0;
  1443. ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1444. partIdx, merge_idx, &current_mv);
  1445. x_pu = x0 >> s->sps->log2_min_pu_size;
  1446. y_pu = y0 >> s->sps->log2_min_pu_size;
  1447. for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  1448. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  1449. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1450. } else {
  1451. enum InterPredIdc inter_pred_idc = PRED_L0;
  1452. ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
  1453. current_mv.pred_flag = 0;
  1454. if (s->sh.slice_type == B_SLICE)
  1455. inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
  1456. if (inter_pred_idc != PRED_L1) {
  1457. if (s->sh.nb_refs[L0]) {
  1458. ref_idx[0] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
  1459. current_mv.ref_idx[0] = ref_idx[0];
  1460. }
  1461. current_mv.pred_flag = PF_L0;
  1462. ff_hevc_hls_mvd_coding(s, x0, y0, 0);
  1463. mvp_flag[0] = ff_hevc_mvp_lx_flag_decode(s);
  1464. ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1465. partIdx, merge_idx, &current_mv,
  1466. mvp_flag[0], 0);
  1467. current_mv.mv[0].x += lc->pu.mvd.x;
  1468. current_mv.mv[0].y += lc->pu.mvd.y;
  1469. }
  1470. if (inter_pred_idc != PRED_L0) {
  1471. if (s->sh.nb_refs[L1]) {
  1472. ref_idx[1] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
  1473. current_mv.ref_idx[1] = ref_idx[1];
  1474. }
  1475. if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
  1476. AV_ZERO32(&lc->pu.mvd);
  1477. } else {
  1478. ff_hevc_hls_mvd_coding(s, x0, y0, 1);
  1479. }
  1480. current_mv.pred_flag += PF_L1;
  1481. mvp_flag[1] = ff_hevc_mvp_lx_flag_decode(s);
  1482. ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
  1483. partIdx, merge_idx, &current_mv,
  1484. mvp_flag[1], 1);
  1485. current_mv.mv[1].x += lc->pu.mvd.x;
  1486. current_mv.mv[1].y += lc->pu.mvd.y;
  1487. }
  1488. x_pu = x0 >> s->sps->log2_min_pu_size;
  1489. y_pu = y0 >> s->sps->log2_min_pu_size;
  1490. for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
  1491. for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
  1492. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1493. }
  1494. }
  1495. if (current_mv.pred_flag & PF_L0) {
  1496. ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
  1497. if (!ref0)
  1498. return;
  1499. hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
  1500. }
  1501. if (current_mv.pred_flag & PF_L1) {
  1502. ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
  1503. if (!ref1)
  1504. return;
  1505. hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
  1506. }
  1507. if (current_mv.pred_flag == PF_L0) {
  1508. int x0_c = x0 >> s->sps->hshift[1];
  1509. int y0_c = y0 >> s->sps->vshift[1];
  1510. int nPbW_c = nPbW >> s->sps->hshift[1];
  1511. int nPbH_c = nPbH >> s->sps->vshift[1];
  1512. luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
  1513. &current_mv.mv[0], x0, y0, nPbW, nPbH,
  1514. s->sh.luma_weight_l0[current_mv.ref_idx[0]],
  1515. s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
  1516. chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
  1517. 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1518. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
  1519. chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
  1520. 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1521. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
  1522. } else if (current_mv.pred_flag == PF_L1) {
  1523. int x0_c = x0 >> s->sps->hshift[1];
  1524. int y0_c = y0 >> s->sps->vshift[1];
  1525. int nPbW_c = nPbW >> s->sps->hshift[1];
  1526. int nPbH_c = nPbH >> s->sps->vshift[1];
  1527. luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
  1528. &current_mv.mv[1], x0, y0, nPbW, nPbH,
  1529. s->sh.luma_weight_l1[current_mv.ref_idx[1]],
  1530. s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
  1531. chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
  1532. 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1533. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
  1534. chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
  1535. 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1536. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
  1537. } else if (current_mv.pred_flag == PF_BI) {
  1538. int x0_c = x0 >> s->sps->hshift[1];
  1539. int y0_c = y0 >> s->sps->vshift[1];
  1540. int nPbW_c = nPbW >> s->sps->hshift[1];
  1541. int nPbH_c = nPbH >> s->sps->vshift[1];
  1542. luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
  1543. &current_mv.mv[0], x0, y0, nPbW, nPbH,
  1544. ref1->frame, &current_mv.mv[1], &current_mv);
  1545. chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
  1546. x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
  1547. chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
  1548. x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
  1549. }
  1550. }
  1551. /**
  1552. * 8.4.1
  1553. */
  1554. static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
  1555. int prev_intra_luma_pred_flag)
  1556. {
  1557. HEVCLocalContext *lc = s->HEVClc;
  1558. int x_pu = x0 >> s->sps->log2_min_pu_size;
  1559. int y_pu = y0 >> s->sps->log2_min_pu_size;
  1560. int min_pu_width = s->sps->min_pu_width;
  1561. int size_in_pus = pu_size >> s->sps->log2_min_pu_size;
  1562. int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
  1563. int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
  1564. int cand_up = (lc->ctb_up_flag || y0b) ?
  1565. s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
  1566. int cand_left = (lc->ctb_left_flag || x0b) ?
  1567. s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
  1568. int y_ctb = (y0 >> (s->sps->log2_ctb_size)) << (s->sps->log2_ctb_size);
  1569. MvField *tab_mvf = s->ref->tab_mvf;
  1570. int intra_pred_mode;
  1571. int candidate[3];
  1572. int i, j;
  1573. // intra_pred_mode prediction does not cross vertical CTB boundaries
  1574. if ((y0 - 1) < y_ctb)
  1575. cand_up = INTRA_DC;
  1576. if (cand_left == cand_up) {
  1577. if (cand_left < 2) {
  1578. candidate[0] = INTRA_PLANAR;
  1579. candidate[1] = INTRA_DC;
  1580. candidate[2] = INTRA_ANGULAR_26;
  1581. } else {
  1582. candidate[0] = cand_left;
  1583. candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
  1584. candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
  1585. }
  1586. } else {
  1587. candidate[0] = cand_left;
  1588. candidate[1] = cand_up;
  1589. if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
  1590. candidate[2] = INTRA_PLANAR;
  1591. } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
  1592. candidate[2] = INTRA_DC;
  1593. } else {
  1594. candidate[2] = INTRA_ANGULAR_26;
  1595. }
  1596. }
  1597. if (prev_intra_luma_pred_flag) {
  1598. intra_pred_mode = candidate[lc->pu.mpm_idx];
  1599. } else {
  1600. if (candidate[0] > candidate[1])
  1601. FFSWAP(uint8_t, candidate[0], candidate[1]);
  1602. if (candidate[0] > candidate[2])
  1603. FFSWAP(uint8_t, candidate[0], candidate[2]);
  1604. if (candidate[1] > candidate[2])
  1605. FFSWAP(uint8_t, candidate[1], candidate[2]);
  1606. intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
  1607. for (i = 0; i < 3; i++)
  1608. if (intra_pred_mode >= candidate[i])
  1609. intra_pred_mode++;
  1610. }
  1611. /* write the intra prediction units into the mv array */
  1612. if (!size_in_pus)
  1613. size_in_pus = 1;
  1614. for (i = 0; i < size_in_pus; i++) {
  1615. memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
  1616. intra_pred_mode, size_in_pus);
  1617. for (j = 0; j < size_in_pus; j++) {
  1618. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
  1619. }
  1620. }
  1621. return intra_pred_mode;
  1622. }
  1623. static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
  1624. int log2_cb_size, int ct_depth)
  1625. {
  1626. int length = (1 << log2_cb_size) >> s->sps->log2_min_cb_size;
  1627. int x_cb = x0 >> s->sps->log2_min_cb_size;
  1628. int y_cb = y0 >> s->sps->log2_min_cb_size;
  1629. int y;
  1630. for (y = 0; y < length; y++)
  1631. memset(&s->tab_ct_depth[(y_cb + y) * s->sps->min_cb_width + x_cb],
  1632. ct_depth, length);
  1633. }
  1634. static const uint8_t tab_mode_idx[] = {
  1635. 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
  1636. 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
  1637. static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
  1638. int log2_cb_size)
  1639. {
  1640. HEVCLocalContext *lc = s->HEVClc;
  1641. static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
  1642. uint8_t prev_intra_luma_pred_flag[4];
  1643. int split = lc->cu.part_mode == PART_NxN;
  1644. int pb_size = (1 << log2_cb_size) >> split;
  1645. int side = split + 1;
  1646. int chroma_mode;
  1647. int i, j;
  1648. for (i = 0; i < side; i++)
  1649. for (j = 0; j < side; j++)
  1650. prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
  1651. for (i = 0; i < side; i++) {
  1652. for (j = 0; j < side; j++) {
  1653. if (prev_intra_luma_pred_flag[2 * i + j])
  1654. lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
  1655. else
  1656. lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
  1657. lc->pu.intra_pred_mode[2 * i + j] =
  1658. luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
  1659. prev_intra_luma_pred_flag[2 * i + j]);
  1660. }
  1661. }
  1662. if (s->sps->chroma_format_idc == 3) {
  1663. for (i = 0; i < side; i++) {
  1664. for (j = 0; j < side; j++) {
  1665. lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
  1666. if (chroma_mode != 4) {
  1667. if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
  1668. lc->pu.intra_pred_mode_c[2 * i + j] = 34;
  1669. else
  1670. lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
  1671. } else {
  1672. lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
  1673. }
  1674. }
  1675. }
  1676. } else if (s->sps->chroma_format_idc == 2) {
  1677. int mode_idx;
  1678. lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
  1679. if (chroma_mode != 4) {
  1680. if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
  1681. mode_idx = 34;
  1682. else
  1683. mode_idx = intra_chroma_table[chroma_mode];
  1684. } else {
  1685. mode_idx = lc->pu.intra_pred_mode[0];
  1686. }
  1687. lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
  1688. } else if (s->sps->chroma_format_idc != 0) {
  1689. chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
  1690. if (chroma_mode != 4) {
  1691. if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
  1692. lc->pu.intra_pred_mode_c[0] = 34;
  1693. else
  1694. lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
  1695. } else {
  1696. lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
  1697. }
  1698. }
  1699. }
  1700. static void intra_prediction_unit_default_value(HEVCContext *s,
  1701. int x0, int y0,
  1702. int log2_cb_size)
  1703. {
  1704. HEVCLocalContext *lc = s->HEVClc;
  1705. int pb_size = 1 << log2_cb_size;
  1706. int size_in_pus = pb_size >> s->sps->log2_min_pu_size;
  1707. int min_pu_width = s->sps->min_pu_width;
  1708. MvField *tab_mvf = s->ref->tab_mvf;
  1709. int x_pu = x0 >> s->sps->log2_min_pu_size;
  1710. int y_pu = y0 >> s->sps->log2_min_pu_size;
  1711. int j, k;
  1712. if (size_in_pus == 0)
  1713. size_in_pus = 1;
  1714. for (j = 0; j < size_in_pus; j++)
  1715. memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
  1716. if (lc->cu.pred_mode == MODE_INTRA)
  1717. for (j = 0; j < size_in_pus; j++)
  1718. for (k = 0; k < size_in_pus; k++)
  1719. tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
  1720. }
  1721. static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
  1722. {
  1723. int cb_size = 1 << log2_cb_size;
  1724. HEVCLocalContext *lc = s->HEVClc;
  1725. int log2_min_cb_size = s->sps->log2_min_cb_size;
  1726. int length = cb_size >> log2_min_cb_size;
  1727. int min_cb_width = s->sps->min_cb_width;
  1728. int x_cb = x0 >> log2_min_cb_size;
  1729. int y_cb = y0 >> log2_min_cb_size;
  1730. int idx = log2_cb_size - 2;
  1731. int qp_block_mask = (1<<(s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth)) - 1;
  1732. int x, y, ret;
  1733. lc->cu.x = x0;
  1734. lc->cu.y = y0;
  1735. lc->cu.rqt_root_cbf = 1;
  1736. lc->cu.pred_mode = MODE_INTRA;
  1737. lc->cu.part_mode = PART_2Nx2N;
  1738. lc->cu.intra_split_flag = 0;
  1739. lc->cu.pcm_flag = 0;
  1740. SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
  1741. for (x = 0; x < 4; x++)
  1742. lc->pu.intra_pred_mode[x] = 1;
  1743. if (s->pps->transquant_bypass_enable_flag) {
  1744. lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
  1745. if (lc->cu.cu_transquant_bypass_flag)
  1746. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  1747. } else
  1748. lc->cu.cu_transquant_bypass_flag = 0;
  1749. if (s->sh.slice_type != I_SLICE) {
  1750. uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
  1751. x = y_cb * min_cb_width + x_cb;
  1752. for (y = 0; y < length; y++) {
  1753. memset(&s->skip_flag[x], skip_flag, length);
  1754. x += min_cb_width;
  1755. }
  1756. lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
  1757. } else {
  1758. x = y_cb * min_cb_width + x_cb;
  1759. for (y = 0; y < length; y++) {
  1760. memset(&s->skip_flag[x], 0, length);
  1761. x += min_cb_width;
  1762. }
  1763. }
  1764. if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
  1765. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
  1766. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1767. if (!s->sh.disable_deblocking_filter_flag)
  1768. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  1769. } else {
  1770. if (s->sh.slice_type != I_SLICE)
  1771. lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
  1772. if (lc->cu.pred_mode != MODE_INTRA ||
  1773. log2_cb_size == s->sps->log2_min_cb_size) {
  1774. lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
  1775. lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
  1776. lc->cu.pred_mode == MODE_INTRA;
  1777. }
  1778. if (lc->cu.pred_mode == MODE_INTRA) {
  1779. if (lc->cu.part_mode == PART_2Nx2N && s->sps->pcm_enabled_flag &&
  1780. log2_cb_size >= s->sps->pcm.log2_min_pcm_cb_size &&
  1781. log2_cb_size <= s->sps->pcm.log2_max_pcm_cb_size) {
  1782. lc->cu.pcm_flag = ff_hevc_pcm_flag_decode(s);
  1783. }
  1784. if (lc->cu.pcm_flag) {
  1785. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1786. ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
  1787. if (s->sps->pcm.loop_filter_disable_flag)
  1788. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  1789. if (ret < 0)
  1790. return ret;
  1791. } else {
  1792. intra_prediction_unit(s, x0, y0, log2_cb_size);
  1793. }
  1794. } else {
  1795. intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
  1796. switch (lc->cu.part_mode) {
  1797. case PART_2Nx2N:
  1798. hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
  1799. break;
  1800. case PART_2NxN:
  1801. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
  1802. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
  1803. break;
  1804. case PART_Nx2N:
  1805. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
  1806. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
  1807. break;
  1808. case PART_2NxnU:
  1809. hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
  1810. hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
  1811. break;
  1812. case PART_2NxnD:
  1813. hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
  1814. hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
  1815. break;
  1816. case PART_nLx2N:
  1817. hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
  1818. hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
  1819. break;
  1820. case PART_nRx2N:
  1821. hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
  1822. hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
  1823. break;
  1824. case PART_NxN:
  1825. hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
  1826. hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
  1827. hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
  1828. hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
  1829. break;
  1830. }
  1831. }
  1832. if (!lc->cu.pcm_flag) {
  1833. if (lc->cu.pred_mode != MODE_INTRA &&
  1834. !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
  1835. lc->cu.rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
  1836. }
  1837. if (lc->cu.rqt_root_cbf) {
  1838. int cbf[2] = { 0 };
  1839. lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
  1840. s->sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
  1841. s->sps->max_transform_hierarchy_depth_inter;
  1842. ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
  1843. log2_cb_size,
  1844. log2_cb_size, 0, 0, cbf, cbf);
  1845. if (ret < 0)
  1846. return ret;
  1847. } else {
  1848. if (!s->sh.disable_deblocking_filter_flag)
  1849. ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
  1850. }
  1851. }
  1852. }
  1853. if (s->pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
  1854. ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
  1855. x = y_cb * min_cb_width + x_cb;
  1856. for (y = 0; y < length; y++) {
  1857. memset(&s->qp_y_tab[x], lc->qp_y, length);
  1858. x += min_cb_width;
  1859. }
  1860. if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
  1861. ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
  1862. lc->qPy_pred = lc->qp_y;
  1863. }
  1864. set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
  1865. return 0;
  1866. }
  1867. static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
  1868. int log2_cb_size, int cb_depth)
  1869. {
  1870. HEVCLocalContext *lc = s->HEVClc;
  1871. const int cb_size = 1 << log2_cb_size;
  1872. int ret;
  1873. int qp_block_mask = (1<<(s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth)) - 1;
  1874. int split_cu;
  1875. lc->ct.depth = cb_depth;
  1876. if (x0 + cb_size <= s->sps->width &&
  1877. y0 + cb_size <= s->sps->height &&
  1878. log2_cb_size > s->sps->log2_min_cb_size) {
  1879. split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
  1880. } else {
  1881. split_cu = (log2_cb_size > s->sps->log2_min_cb_size);
  1882. }
  1883. if (s->pps->cu_qp_delta_enabled_flag &&
  1884. log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth) {
  1885. lc->tu.is_cu_qp_delta_coded = 0;
  1886. lc->tu.cu_qp_delta = 0;
  1887. }
  1888. if (s->sh.cu_chroma_qp_offset_enabled_flag &&
  1889. log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_chroma_qp_offset_depth) {
  1890. lc->tu.is_cu_chroma_qp_offset_coded = 0;
  1891. }
  1892. if (split_cu) {
  1893. const int cb_size_split = cb_size >> 1;
  1894. const int x1 = x0 + cb_size_split;
  1895. const int y1 = y0 + cb_size_split;
  1896. int more_data = 0;
  1897. more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
  1898. if (more_data < 0)
  1899. return more_data;
  1900. if (more_data && x1 < s->sps->width) {
  1901. more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
  1902. if (more_data < 0)
  1903. return more_data;
  1904. }
  1905. if (more_data && y1 < s->sps->height) {
  1906. more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
  1907. if (more_data < 0)
  1908. return more_data;
  1909. }
  1910. if (more_data && x1 < s->sps->width &&
  1911. y1 < s->sps->height) {
  1912. more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
  1913. if (more_data < 0)
  1914. return more_data;
  1915. }
  1916. if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
  1917. ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
  1918. lc->qPy_pred = lc->qp_y;
  1919. if (more_data)
  1920. return ((x1 + cb_size_split) < s->sps->width ||
  1921. (y1 + cb_size_split) < s->sps->height);
  1922. else
  1923. return 0;
  1924. } else {
  1925. ret = hls_coding_unit(s, x0, y0, log2_cb_size);
  1926. if (ret < 0)
  1927. return ret;
  1928. if ((!((x0 + cb_size) %
  1929. (1 << (s->sps->log2_ctb_size))) ||
  1930. (x0 + cb_size >= s->sps->width)) &&
  1931. (!((y0 + cb_size) %
  1932. (1 << (s->sps->log2_ctb_size))) ||
  1933. (y0 + cb_size >= s->sps->height))) {
  1934. int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
  1935. return !end_of_slice_flag;
  1936. } else {
  1937. return 1;
  1938. }
  1939. }
  1940. return 0;
  1941. }
  1942. static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
  1943. int ctb_addr_ts)
  1944. {
  1945. HEVCLocalContext *lc = s->HEVClc;
  1946. int ctb_size = 1 << s->sps->log2_ctb_size;
  1947. int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  1948. int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
  1949. int tile_left_boundary, tile_up_boundary;
  1950. int slice_left_boundary, slice_up_boundary;
  1951. s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
  1952. if (s->pps->entropy_coding_sync_enabled_flag) {
  1953. if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
  1954. lc->first_qp_group = 1;
  1955. lc->end_of_tiles_x = s->sps->width;
  1956. } else if (s->pps->tiles_enabled_flag) {
  1957. if (ctb_addr_ts && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
  1958. int idxX = s->pps->col_idxX[x_ctb >> s->sps->log2_ctb_size];
  1959. lc->end_of_tiles_x = x_ctb + (s->pps->column_width[idxX] << s->sps->log2_ctb_size);
  1960. lc->first_qp_group = 1;
  1961. }
  1962. } else {
  1963. lc->end_of_tiles_x = s->sps->width;
  1964. }
  1965. lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->sps->height);
  1966. if (s->pps->tiles_enabled_flag) {
  1967. tile_left_boundary = x_ctb > 0 &&
  1968. s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs-1]];
  1969. slice_left_boundary = x_ctb > 0 &&
  1970. s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1];
  1971. tile_up_boundary = y_ctb > 0 &&
  1972. 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]];
  1973. slice_up_boundary = y_ctb > 0 &&
  1974. s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->sps->ctb_width];
  1975. } else {
  1976. tile_left_boundary =
  1977. tile_up_boundary = 0;
  1978. slice_left_boundary = ctb_addr_in_slice <= 0;
  1979. slice_up_boundary = ctb_addr_in_slice < s->sps->ctb_width;
  1980. }
  1981. lc->slice_or_tiles_left_boundary = slice_left_boundary + (tile_left_boundary << 1);
  1982. lc->slice_or_tiles_up_boundary = slice_up_boundary + (tile_up_boundary << 1);
  1983. lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !tile_left_boundary);
  1984. lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->sps->ctb_width) && !tile_up_boundary);
  1985. 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]]));
  1986. 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]]));
  1987. }
  1988. static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
  1989. {
  1990. HEVCContext *s = avctxt->priv_data;
  1991. int ctb_size = 1 << s->sps->log2_ctb_size;
  1992. int more_data = 1;
  1993. int x_ctb = 0;
  1994. int y_ctb = 0;
  1995. int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
  1996. if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
  1997. av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
  1998. return AVERROR_INVALIDDATA;
  1999. }
  2000. if (s->sh.dependent_slice_segment_flag) {
  2001. int prev_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
  2002. if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
  2003. av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
  2004. return AVERROR_INVALIDDATA;
  2005. }
  2006. }
  2007. while (more_data && ctb_addr_ts < s->sps->ctb_size) {
  2008. int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  2009. x_ctb = (ctb_addr_rs % ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  2010. y_ctb = (ctb_addr_rs / ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  2011. hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
  2012. ff_hevc_cabac_init(s, ctb_addr_ts);
  2013. hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
  2014. s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
  2015. s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
  2016. s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
  2017. more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
  2018. if (more_data < 0) {
  2019. s->tab_slice_address[ctb_addr_rs] = -1;
  2020. return more_data;
  2021. }
  2022. ctb_addr_ts++;
  2023. ff_hevc_save_states(s, ctb_addr_ts);
  2024. ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
  2025. }
  2026. if (x_ctb + ctb_size >= s->sps->width &&
  2027. y_ctb + ctb_size >= s->sps->height)
  2028. ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
  2029. return ctb_addr_ts;
  2030. }
  2031. static int hls_slice_data(HEVCContext *s)
  2032. {
  2033. int arg[2];
  2034. int ret[2];
  2035. arg[0] = 0;
  2036. arg[1] = 1;
  2037. s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
  2038. return ret[0];
  2039. }
  2040. static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
  2041. {
  2042. HEVCContext *s1 = avctxt->priv_data, *s;
  2043. HEVCLocalContext *lc;
  2044. int ctb_size = 1<< s1->sps->log2_ctb_size;
  2045. int more_data = 1;
  2046. int *ctb_row_p = input_ctb_row;
  2047. int ctb_row = ctb_row_p[job];
  2048. int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->sps->width + ctb_size - 1) >> s1->sps->log2_ctb_size);
  2049. int ctb_addr_ts = s1->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
  2050. int thread = ctb_row % s1->threads_number;
  2051. int ret;
  2052. s = s1->sList[self_id];
  2053. lc = s->HEVClc;
  2054. if(ctb_row) {
  2055. ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
  2056. if (ret < 0)
  2057. return ret;
  2058. ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
  2059. }
  2060. while(more_data && ctb_addr_ts < s->sps->ctb_size) {
  2061. int x_ctb = (ctb_addr_rs % s->sps->ctb_width) << s->sps->log2_ctb_size;
  2062. int y_ctb = (ctb_addr_rs / s->sps->ctb_width) << s->sps->log2_ctb_size;
  2063. hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
  2064. ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
  2065. if (avpriv_atomic_int_get(&s1->wpp_err)){
  2066. ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
  2067. return 0;
  2068. }
  2069. ff_hevc_cabac_init(s, ctb_addr_ts);
  2070. hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
  2071. more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
  2072. if (more_data < 0) {
  2073. s->tab_slice_address[ctb_addr_rs] = -1;
  2074. return more_data;
  2075. }
  2076. ctb_addr_ts++;
  2077. ff_hevc_save_states(s, ctb_addr_ts);
  2078. ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
  2079. ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
  2080. if (!more_data && (x_ctb+ctb_size) < s->sps->width && ctb_row != s->sh.num_entry_point_offsets) {
  2081. avpriv_atomic_int_set(&s1->wpp_err, 1);
  2082. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  2083. return 0;
  2084. }
  2085. if ((x_ctb+ctb_size) >= s->sps->width && (y_ctb+ctb_size) >= s->sps->height ) {
  2086. ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
  2087. ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
  2088. return ctb_addr_ts;
  2089. }
  2090. ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  2091. x_ctb+=ctb_size;
  2092. if(x_ctb >= s->sps->width) {
  2093. break;
  2094. }
  2095. }
  2096. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  2097. return 0;
  2098. }
  2099. static int hls_slice_data_wpp(HEVCContext *s, const uint8_t *nal, int length)
  2100. {
  2101. HEVCLocalContext *lc = s->HEVClc;
  2102. int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
  2103. int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
  2104. int offset;
  2105. int startheader, cmpt = 0;
  2106. int i, j, res = 0;
  2107. if (!s->sList[1]) {
  2108. ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
  2109. for (i = 1; i < s->threads_number; i++) {
  2110. s->sList[i] = av_malloc(sizeof(HEVCContext));
  2111. memcpy(s->sList[i], s, sizeof(HEVCContext));
  2112. s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
  2113. s->sList[i]->HEVClc = s->HEVClcList[i];
  2114. }
  2115. }
  2116. offset = (lc->gb.index >> 3);
  2117. for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < s->skipped_bytes; j++) {
  2118. if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
  2119. startheader--;
  2120. cmpt++;
  2121. }
  2122. }
  2123. for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
  2124. offset += (s->sh.entry_point_offset[i - 1] - cmpt);
  2125. for (j = 0, cmpt = 0, startheader = offset
  2126. + s->sh.entry_point_offset[i]; j < s->skipped_bytes; j++) {
  2127. if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
  2128. startheader--;
  2129. cmpt++;
  2130. }
  2131. }
  2132. s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
  2133. s->sh.offset[i - 1] = offset;
  2134. }
  2135. if (s->sh.num_entry_point_offsets != 0) {
  2136. offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
  2137. s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
  2138. s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
  2139. }
  2140. s->data = nal;
  2141. for (i = 1; i < s->threads_number; i++) {
  2142. s->sList[i]->HEVClc->first_qp_group = 1;
  2143. s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
  2144. memcpy(s->sList[i], s, sizeof(HEVCContext));
  2145. s->sList[i]->HEVClc = s->HEVClcList[i];
  2146. }
  2147. avpriv_atomic_int_set(&s->wpp_err, 0);
  2148. ff_reset_entries(s->avctx);
  2149. for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
  2150. arg[i] = i;
  2151. ret[i] = 0;
  2152. }
  2153. if (s->pps->entropy_coding_sync_enabled_flag)
  2154. s->avctx->execute2(s->avctx, (void *) hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
  2155. for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
  2156. res += ret[i];
  2157. av_free(ret);
  2158. av_free(arg);
  2159. return res;
  2160. }
  2161. /**
  2162. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  2163. * 0 if the unit should be skipped, 1 otherwise
  2164. */
  2165. static int hls_nal_unit(HEVCContext *s)
  2166. {
  2167. GetBitContext *gb = &s->HEVClc->gb;
  2168. int nuh_layer_id;
  2169. if (get_bits1(gb) != 0)
  2170. return AVERROR_INVALIDDATA;
  2171. s->nal_unit_type = get_bits(gb, 6);
  2172. nuh_layer_id = get_bits(gb, 6);
  2173. s->temporal_id = get_bits(gb, 3) - 1;
  2174. if (s->temporal_id < 0)
  2175. return AVERROR_INVALIDDATA;
  2176. av_log(s->avctx, AV_LOG_DEBUG,
  2177. "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
  2178. s->nal_unit_type, nuh_layer_id, s->temporal_id);
  2179. return nuh_layer_id == 0;
  2180. }
  2181. static int set_side_data(HEVCContext *s)
  2182. {
  2183. AVFrame *out = s->ref->frame;
  2184. if (s->sei_frame_packing_present &&
  2185. s->frame_packing_arrangement_type >= 3 &&
  2186. s->frame_packing_arrangement_type <= 5 &&
  2187. s->content_interpretation_type > 0 &&
  2188. s->content_interpretation_type < 3) {
  2189. AVStereo3D *stereo = av_stereo3d_create_side_data(out);
  2190. if (!stereo)
  2191. return AVERROR(ENOMEM);
  2192. switch (s->frame_packing_arrangement_type) {
  2193. case 3:
  2194. if (s->quincunx_subsampling)
  2195. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  2196. else
  2197. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  2198. break;
  2199. case 4:
  2200. stereo->type = AV_STEREO3D_TOPBOTTOM;
  2201. break;
  2202. case 5:
  2203. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  2204. break;
  2205. }
  2206. if (s->content_interpretation_type == 2)
  2207. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  2208. }
  2209. if (s->sei_display_orientation_present &&
  2210. (s->sei_anticlockwise_rotation || s->sei_hflip || s->sei_vflip)) {
  2211. double angle = s->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
  2212. AVFrameSideData *rotation = av_frame_new_side_data(out,
  2213. AV_FRAME_DATA_DISPLAYMATRIX,
  2214. sizeof(int32_t) * 9);
  2215. if (!rotation)
  2216. return AVERROR(ENOMEM);
  2217. av_display_rotation_set((int32_t *)rotation->data, angle);
  2218. av_display_matrix_flip((int32_t *)rotation->data,
  2219. s->sei_vflip, s->sei_hflip);
  2220. }
  2221. return 0;
  2222. }
  2223. static int hevc_frame_start(HEVCContext *s)
  2224. {
  2225. HEVCLocalContext *lc = s->HEVClc;
  2226. int pic_size_in_ctb = ((s->sps->width >> s->sps->log2_min_cb_size) + 1) *
  2227. ((s->sps->height >> s->sps->log2_min_cb_size) + 1);
  2228. int ret;
  2229. memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
  2230. memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
  2231. memset(s->cbf_luma, 0, s->sps->min_tb_width * s->sps->min_tb_height);
  2232. memset(s->is_pcm, 0, (s->sps->min_pu_width + 1) * (s->sps->min_pu_height + 1));
  2233. memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
  2234. s->is_decoded = 0;
  2235. s->first_nal_type = s->nal_unit_type;
  2236. if (s->pps->tiles_enabled_flag)
  2237. lc->end_of_tiles_x = s->pps->column_width[0] << s->sps->log2_ctb_size;
  2238. ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
  2239. if (ret < 0)
  2240. goto fail;
  2241. ret = ff_hevc_frame_rps(s);
  2242. if (ret < 0) {
  2243. av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
  2244. goto fail;
  2245. }
  2246. s->ref->frame->key_frame = IS_IRAP(s);
  2247. ret = set_side_data(s);
  2248. if (ret < 0)
  2249. goto fail;
  2250. s->frame->pict_type = 3 - s->sh.slice_type;
  2251. if (!IS_IRAP(s))
  2252. ff_hevc_bump_frame(s);
  2253. av_frame_unref(s->output_frame);
  2254. ret = ff_hevc_output_frame(s, s->output_frame, 0);
  2255. if (ret < 0)
  2256. goto fail;
  2257. ff_thread_finish_setup(s->avctx);
  2258. return 0;
  2259. fail:
  2260. if (s->ref && s->threads_type == FF_THREAD_FRAME)
  2261. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  2262. s->ref = NULL;
  2263. return ret;
  2264. }
  2265. static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
  2266. {
  2267. HEVCLocalContext *lc = s->HEVClc;
  2268. GetBitContext *gb = &lc->gb;
  2269. int ctb_addr_ts, ret;
  2270. ret = init_get_bits8(gb, nal, length);
  2271. if (ret < 0)
  2272. return ret;
  2273. ret = hls_nal_unit(s);
  2274. if (ret < 0) {
  2275. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  2276. s->nal_unit_type);
  2277. goto fail;
  2278. } else if (!ret)
  2279. return 0;
  2280. switch (s->nal_unit_type) {
  2281. case NAL_VPS:
  2282. ret = ff_hevc_decode_nal_vps(s);
  2283. if (ret < 0)
  2284. goto fail;
  2285. break;
  2286. case NAL_SPS:
  2287. ret = ff_hevc_decode_nal_sps(s);
  2288. if (ret < 0)
  2289. goto fail;
  2290. break;
  2291. case NAL_PPS:
  2292. ret = ff_hevc_decode_nal_pps(s);
  2293. if (ret < 0)
  2294. goto fail;
  2295. break;
  2296. case NAL_SEI_PREFIX:
  2297. case NAL_SEI_SUFFIX:
  2298. ret = ff_hevc_decode_nal_sei(s);
  2299. if (ret < 0)
  2300. goto fail;
  2301. break;
  2302. case NAL_TRAIL_R:
  2303. case NAL_TRAIL_N:
  2304. case NAL_TSA_N:
  2305. case NAL_TSA_R:
  2306. case NAL_STSA_N:
  2307. case NAL_STSA_R:
  2308. case NAL_BLA_W_LP:
  2309. case NAL_BLA_W_RADL:
  2310. case NAL_BLA_N_LP:
  2311. case NAL_IDR_W_RADL:
  2312. case NAL_IDR_N_LP:
  2313. case NAL_CRA_NUT:
  2314. case NAL_RADL_N:
  2315. case NAL_RADL_R:
  2316. case NAL_RASL_N:
  2317. case NAL_RASL_R:
  2318. ret = hls_slice_header(s);
  2319. if (ret < 0)
  2320. return ret;
  2321. if (s->max_ra == INT_MAX) {
  2322. if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
  2323. s->max_ra = s->poc;
  2324. } else {
  2325. if (IS_IDR(s))
  2326. s->max_ra = INT_MIN;
  2327. }
  2328. }
  2329. if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
  2330. s->poc <= s->max_ra) {
  2331. s->is_decoded = 0;
  2332. break;
  2333. } else {
  2334. if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
  2335. s->max_ra = INT_MIN;
  2336. }
  2337. if (s->sh.first_slice_in_pic_flag) {
  2338. ret = hevc_frame_start(s);
  2339. if (ret < 0)
  2340. return ret;
  2341. } else if (!s->ref) {
  2342. av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
  2343. goto fail;
  2344. }
  2345. if (s->nal_unit_type != s->first_nal_type) {
  2346. av_log(s->avctx, AV_LOG_ERROR,
  2347. "Non-matching NAL types of the VCL NALUs: %d %d\n",
  2348. s->first_nal_type, s->nal_unit_type);
  2349. return AVERROR_INVALIDDATA;
  2350. }
  2351. if (!s->sh.dependent_slice_segment_flag &&
  2352. s->sh.slice_type != I_SLICE) {
  2353. ret = ff_hevc_slice_rpl(s);
  2354. if (ret < 0) {
  2355. av_log(s->avctx, AV_LOG_WARNING,
  2356. "Error constructing the reference lists for the current slice.\n");
  2357. goto fail;
  2358. }
  2359. }
  2360. if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
  2361. ctb_addr_ts = hls_slice_data_wpp(s, nal, length);
  2362. else
  2363. ctb_addr_ts = hls_slice_data(s);
  2364. if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {
  2365. s->is_decoded = 1;
  2366. }
  2367. if (ctb_addr_ts < 0) {
  2368. ret = ctb_addr_ts;
  2369. goto fail;
  2370. }
  2371. break;
  2372. case NAL_EOS_NUT:
  2373. case NAL_EOB_NUT:
  2374. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2375. s->max_ra = INT_MAX;
  2376. break;
  2377. case NAL_AUD:
  2378. case NAL_FD_NUT:
  2379. break;
  2380. default:
  2381. av_log(s->avctx, AV_LOG_INFO,
  2382. "Skipping NAL unit %d\n", s->nal_unit_type);
  2383. }
  2384. return 0;
  2385. fail:
  2386. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  2387. return ret;
  2388. return 0;
  2389. }
  2390. /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
  2391. * between these functions would be nice. */
  2392. int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
  2393. HEVCNAL *nal)
  2394. {
  2395. int i, si, di;
  2396. uint8_t *dst;
  2397. s->skipped_bytes = 0;
  2398. #define STARTCODE_TEST \
  2399. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  2400. if (src[i + 2] != 3) { \
  2401. /* startcode, so we must be past the end */ \
  2402. length = i; \
  2403. } \
  2404. break; \
  2405. }
  2406. #if HAVE_FAST_UNALIGNED
  2407. #define FIND_FIRST_ZERO \
  2408. if (i > 0 && !src[i]) \
  2409. i--; \
  2410. while (src[i]) \
  2411. i++
  2412. #if HAVE_FAST_64BIT
  2413. for (i = 0; i + 1 < length; i += 9) {
  2414. if (!((~AV_RN64A(src + i) &
  2415. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  2416. 0x8000800080008080ULL))
  2417. continue;
  2418. FIND_FIRST_ZERO;
  2419. STARTCODE_TEST;
  2420. i -= 7;
  2421. }
  2422. #else
  2423. for (i = 0; i + 1 < length; i += 5) {
  2424. if (!((~AV_RN32A(src + i) &
  2425. (AV_RN32A(src + i) - 0x01000101U)) &
  2426. 0x80008080U))
  2427. continue;
  2428. FIND_FIRST_ZERO;
  2429. STARTCODE_TEST;
  2430. i -= 3;
  2431. }
  2432. #endif /* HAVE_FAST_64BIT */
  2433. #else
  2434. for (i = 0; i + 1 < length; i += 2) {
  2435. if (src[i])
  2436. continue;
  2437. if (i > 0 && src[i - 1] == 0)
  2438. i--;
  2439. STARTCODE_TEST;
  2440. }
  2441. #endif /* HAVE_FAST_UNALIGNED */
  2442. if (i >= length - 1) { // no escaped 0
  2443. nal->data = src;
  2444. nal->size = length;
  2445. return length;
  2446. }
  2447. av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
  2448. length + FF_INPUT_BUFFER_PADDING_SIZE);
  2449. if (!nal->rbsp_buffer)
  2450. return AVERROR(ENOMEM);
  2451. dst = nal->rbsp_buffer;
  2452. memcpy(dst, src, i);
  2453. si = di = i;
  2454. while (si + 2 < length) {
  2455. // remove escapes (very rare 1:2^22)
  2456. if (src[si + 2] > 3) {
  2457. dst[di++] = src[si++];
  2458. dst[di++] = src[si++];
  2459. } else if (src[si] == 0 && src[si + 1] == 0) {
  2460. if (src[si + 2] == 3) { // escape
  2461. dst[di++] = 0;
  2462. dst[di++] = 0;
  2463. si += 3;
  2464. s->skipped_bytes++;
  2465. if (s->skipped_bytes_pos_size < s->skipped_bytes) {
  2466. s->skipped_bytes_pos_size *= 2;
  2467. av_reallocp_array(&s->skipped_bytes_pos,
  2468. s->skipped_bytes_pos_size,
  2469. sizeof(*s->skipped_bytes_pos));
  2470. if (!s->skipped_bytes_pos)
  2471. return AVERROR(ENOMEM);
  2472. }
  2473. if (s->skipped_bytes_pos)
  2474. s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
  2475. continue;
  2476. } else // next start code
  2477. goto nsc;
  2478. }
  2479. dst[di++] = src[si++];
  2480. }
  2481. while (si < length)
  2482. dst[di++] = src[si++];
  2483. nsc:
  2484. memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  2485. nal->data = dst;
  2486. nal->size = di;
  2487. return si;
  2488. }
  2489. static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
  2490. {
  2491. int i, consumed, ret = 0;
  2492. s->ref = NULL;
  2493. s->last_eos = s->eos;
  2494. s->eos = 0;
  2495. /* split the input packet into NAL units, so we know the upper bound on the
  2496. * number of slices in the frame */
  2497. s->nb_nals = 0;
  2498. while (length >= 4) {
  2499. HEVCNAL *nal;
  2500. int extract_length = 0;
  2501. if (s->is_nalff) {
  2502. int i;
  2503. for (i = 0; i < s->nal_length_size; i++)
  2504. extract_length = (extract_length << 8) | buf[i];
  2505. buf += s->nal_length_size;
  2506. length -= s->nal_length_size;
  2507. if (extract_length > length) {
  2508. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
  2509. ret = AVERROR_INVALIDDATA;
  2510. goto fail;
  2511. }
  2512. } else {
  2513. /* search start code */
  2514. while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
  2515. ++buf;
  2516. --length;
  2517. if (length < 4) {
  2518. av_log(s->avctx, AV_LOG_ERROR, "No start code is found.\n");
  2519. ret = AVERROR_INVALIDDATA;
  2520. goto fail;
  2521. }
  2522. }
  2523. buf += 3;
  2524. length -= 3;
  2525. }
  2526. if (!s->is_nalff)
  2527. extract_length = length;
  2528. if (s->nals_allocated < s->nb_nals + 1) {
  2529. int new_size = s->nals_allocated + 1;
  2530. HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));
  2531. if (!tmp) {
  2532. ret = AVERROR(ENOMEM);
  2533. goto fail;
  2534. }
  2535. s->nals = tmp;
  2536. memset(s->nals + s->nals_allocated, 0,
  2537. (new_size - s->nals_allocated) * sizeof(*tmp));
  2538. av_reallocp_array(&s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));
  2539. av_reallocp_array(&s->skipped_bytes_pos_size_nal, new_size, sizeof(*s->skipped_bytes_pos_size_nal));
  2540. av_reallocp_array(&s->skipped_bytes_pos_nal, new_size, sizeof(*s->skipped_bytes_pos_nal));
  2541. s->skipped_bytes_pos_size_nal[s->nals_allocated] = 1024; // initial buffer size
  2542. 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));
  2543. s->nals_allocated = new_size;
  2544. }
  2545. s->skipped_bytes_pos_size = s->skipped_bytes_pos_size_nal[s->nb_nals];
  2546. s->skipped_bytes_pos = s->skipped_bytes_pos_nal[s->nb_nals];
  2547. nal = &s->nals[s->nb_nals];
  2548. consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
  2549. s->skipped_bytes_nal[s->nb_nals] = s->skipped_bytes;
  2550. s->skipped_bytes_pos_size_nal[s->nb_nals] = s->skipped_bytes_pos_size;
  2551. s->skipped_bytes_pos_nal[s->nb_nals++] = s->skipped_bytes_pos;
  2552. if (consumed < 0) {
  2553. ret = consumed;
  2554. goto fail;
  2555. }
  2556. ret = init_get_bits8(&s->HEVClc->gb, nal->data, nal->size);
  2557. if (ret < 0)
  2558. goto fail;
  2559. hls_nal_unit(s);
  2560. if (s->nal_unit_type == NAL_EOB_NUT ||
  2561. s->nal_unit_type == NAL_EOS_NUT)
  2562. s->eos = 1;
  2563. buf += consumed;
  2564. length -= consumed;
  2565. }
  2566. /* parse the NAL units */
  2567. for (i = 0; i < s->nb_nals; i++) {
  2568. int ret;
  2569. s->skipped_bytes = s->skipped_bytes_nal[i];
  2570. s->skipped_bytes_pos = s->skipped_bytes_pos_nal[i];
  2571. ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);
  2572. if (ret < 0) {
  2573. av_log(s->avctx, AV_LOG_WARNING,
  2574. "Error parsing NAL unit #%d.\n", i);
  2575. goto fail;
  2576. }
  2577. }
  2578. fail:
  2579. if (s->ref && s->threads_type == FF_THREAD_FRAME)
  2580. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  2581. return ret;
  2582. }
  2583. static void print_md5(void *log_ctx, int level, uint8_t md5[16])
  2584. {
  2585. int i;
  2586. for (i = 0; i < 16; i++)
  2587. av_log(log_ctx, level, "%02"PRIx8, md5[i]);
  2588. }
  2589. static int verify_md5(HEVCContext *s, AVFrame *frame)
  2590. {
  2591. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  2592. int pixel_shift;
  2593. int i, j;
  2594. if (!desc)
  2595. return AVERROR(EINVAL);
  2596. pixel_shift = desc->comp[0].depth_minus1 > 7;
  2597. av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
  2598. s->poc);
  2599. /* the checksums are LE, so we have to byteswap for >8bpp formats
  2600. * on BE arches */
  2601. #if HAVE_BIGENDIAN
  2602. if (pixel_shift && !s->checksum_buf) {
  2603. av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
  2604. FFMAX3(frame->linesize[0], frame->linesize[1],
  2605. frame->linesize[2]));
  2606. if (!s->checksum_buf)
  2607. return AVERROR(ENOMEM);
  2608. }
  2609. #endif
  2610. for (i = 0; frame->data[i]; i++) {
  2611. int width = s->avctx->coded_width;
  2612. int height = s->avctx->coded_height;
  2613. int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
  2614. int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
  2615. uint8_t md5[16];
  2616. av_md5_init(s->md5_ctx);
  2617. for (j = 0; j < h; j++) {
  2618. const uint8_t *src = frame->data[i] + j * frame->linesize[i];
  2619. #if HAVE_BIGENDIAN
  2620. if (pixel_shift) {
  2621. s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
  2622. (const uint16_t *) src, w);
  2623. src = s->checksum_buf;
  2624. }
  2625. #endif
  2626. av_md5_update(s->md5_ctx, src, w << pixel_shift);
  2627. }
  2628. av_md5_final(s->md5_ctx, md5);
  2629. if (!memcmp(md5, s->md5[i], 16)) {
  2630. av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
  2631. print_md5(s->avctx, AV_LOG_DEBUG, md5);
  2632. av_log (s->avctx, AV_LOG_DEBUG, "; ");
  2633. } else {
  2634. av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
  2635. print_md5(s->avctx, AV_LOG_ERROR, md5);
  2636. av_log (s->avctx, AV_LOG_ERROR, " != ");
  2637. print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
  2638. av_log (s->avctx, AV_LOG_ERROR, "\n");
  2639. return AVERROR_INVALIDDATA;
  2640. }
  2641. }
  2642. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2643. return 0;
  2644. }
  2645. static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
  2646. AVPacket *avpkt)
  2647. {
  2648. int ret;
  2649. HEVCContext *s = avctx->priv_data;
  2650. if (!avpkt->size) {
  2651. ret = ff_hevc_output_frame(s, data, 1);
  2652. if (ret < 0)
  2653. return ret;
  2654. *got_output = ret;
  2655. return 0;
  2656. }
  2657. s->ref = NULL;
  2658. ret = decode_nal_units(s, avpkt->data, avpkt->size);
  2659. if (ret < 0)
  2660. return ret;
  2661. /* verify the SEI checksum */
  2662. if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
  2663. s->is_md5) {
  2664. ret = verify_md5(s, s->ref->frame);
  2665. if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
  2666. ff_hevc_unref_frame(s, s->ref, ~0);
  2667. return ret;
  2668. }
  2669. }
  2670. s->is_md5 = 0;
  2671. if (s->is_decoded) {
  2672. av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
  2673. s->is_decoded = 0;
  2674. }
  2675. if (s->output_frame->buf[0]) {
  2676. av_frame_move_ref(data, s->output_frame);
  2677. *got_output = 1;
  2678. }
  2679. return avpkt->size;
  2680. }
  2681. static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
  2682. {
  2683. int ret;
  2684. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  2685. if (ret < 0)
  2686. return ret;
  2687. dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
  2688. if (!dst->tab_mvf_buf)
  2689. goto fail;
  2690. dst->tab_mvf = src->tab_mvf;
  2691. dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
  2692. if (!dst->rpl_tab_buf)
  2693. goto fail;
  2694. dst->rpl_tab = src->rpl_tab;
  2695. dst->rpl_buf = av_buffer_ref(src->rpl_buf);
  2696. if (!dst->rpl_buf)
  2697. goto fail;
  2698. dst->poc = src->poc;
  2699. dst->ctb_count = src->ctb_count;
  2700. dst->window = src->window;
  2701. dst->flags = src->flags;
  2702. dst->sequence = src->sequence;
  2703. return 0;
  2704. fail:
  2705. ff_hevc_unref_frame(s, dst, ~0);
  2706. return AVERROR(ENOMEM);
  2707. }
  2708. static av_cold int hevc_decode_free(AVCodecContext *avctx)
  2709. {
  2710. HEVCContext *s = avctx->priv_data;
  2711. HEVCLocalContext *lc = s->HEVClc;
  2712. int i;
  2713. pic_arrays_free(s);
  2714. av_freep(&s->md5_ctx);
  2715. for(i=0; i < s->nals_allocated; i++) {
  2716. av_freep(&s->skipped_bytes_pos_nal[i]);
  2717. }
  2718. av_freep(&s->skipped_bytes_pos_size_nal);
  2719. av_freep(&s->skipped_bytes_nal);
  2720. av_freep(&s->skipped_bytes_pos_nal);
  2721. av_freep(&s->cabac_state);
  2722. av_frame_free(&s->tmp_frame);
  2723. av_frame_free(&s->output_frame);
  2724. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2725. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2726. av_frame_free(&s->DPB[i].frame);
  2727. }
  2728. for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++)
  2729. av_buffer_unref(&s->vps_list[i]);
  2730. for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
  2731. av_buffer_unref(&s->sps_list[i]);
  2732. for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
  2733. av_buffer_unref(&s->pps_list[i]);
  2734. s->sps = NULL;
  2735. s->pps = NULL;
  2736. s->vps = NULL;
  2737. av_buffer_unref(&s->current_sps);
  2738. av_freep(&s->sh.entry_point_offset);
  2739. av_freep(&s->sh.offset);
  2740. av_freep(&s->sh.size);
  2741. for (i = 1; i < s->threads_number; i++) {
  2742. lc = s->HEVClcList[i];
  2743. if (lc) {
  2744. av_freep(&s->HEVClcList[i]);
  2745. av_freep(&s->sList[i]);
  2746. }
  2747. }
  2748. if (s->HEVClc == s->HEVClcList[0])
  2749. s->HEVClc = NULL;
  2750. av_freep(&s->HEVClcList[0]);
  2751. for (i = 0; i < s->nals_allocated; i++)
  2752. av_freep(&s->nals[i].rbsp_buffer);
  2753. av_freep(&s->nals);
  2754. s->nals_allocated = 0;
  2755. return 0;
  2756. }
  2757. static av_cold int hevc_init_context(AVCodecContext *avctx)
  2758. {
  2759. HEVCContext *s = avctx->priv_data;
  2760. int i;
  2761. s->avctx = avctx;
  2762. s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
  2763. if (!s->HEVClc)
  2764. goto fail;
  2765. s->HEVClcList[0] = s->HEVClc;
  2766. s->sList[0] = s;
  2767. s->cabac_state = av_malloc(HEVC_CONTEXTS);
  2768. if (!s->cabac_state)
  2769. goto fail;
  2770. s->tmp_frame = av_frame_alloc();
  2771. if (!s->tmp_frame)
  2772. goto fail;
  2773. s->output_frame = av_frame_alloc();
  2774. if (!s->output_frame)
  2775. goto fail;
  2776. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2777. s->DPB[i].frame = av_frame_alloc();
  2778. if (!s->DPB[i].frame)
  2779. goto fail;
  2780. s->DPB[i].tf.f = s->DPB[i].frame;
  2781. }
  2782. s->max_ra = INT_MAX;
  2783. s->md5_ctx = av_md5_alloc();
  2784. if (!s->md5_ctx)
  2785. goto fail;
  2786. ff_bswapdsp_init(&s->bdsp);
  2787. s->context_initialized = 1;
  2788. s->eos = 0;
  2789. return 0;
  2790. fail:
  2791. hevc_decode_free(avctx);
  2792. return AVERROR(ENOMEM);
  2793. }
  2794. static int hevc_update_thread_context(AVCodecContext *dst,
  2795. const AVCodecContext *src)
  2796. {
  2797. HEVCContext *s = dst->priv_data;
  2798. HEVCContext *s0 = src->priv_data;
  2799. int i, ret;
  2800. if (!s->context_initialized) {
  2801. ret = hevc_init_context(dst);
  2802. if (ret < 0)
  2803. return ret;
  2804. }
  2805. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2806. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2807. if (s0->DPB[i].frame->buf[0]) {
  2808. ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
  2809. if (ret < 0)
  2810. return ret;
  2811. }
  2812. }
  2813. if (s->sps != s0->sps)
  2814. s->sps = NULL;
  2815. for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++) {
  2816. av_buffer_unref(&s->vps_list[i]);
  2817. if (s0->vps_list[i]) {
  2818. s->vps_list[i] = av_buffer_ref(s0->vps_list[i]);
  2819. if (!s->vps_list[i])
  2820. return AVERROR(ENOMEM);
  2821. }
  2822. }
  2823. for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++) {
  2824. av_buffer_unref(&s->sps_list[i]);
  2825. if (s0->sps_list[i]) {
  2826. s->sps_list[i] = av_buffer_ref(s0->sps_list[i]);
  2827. if (!s->sps_list[i])
  2828. return AVERROR(ENOMEM);
  2829. }
  2830. }
  2831. for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
  2832. av_buffer_unref(&s->pps_list[i]);
  2833. if (s0->pps_list[i]) {
  2834. s->pps_list[i] = av_buffer_ref(s0->pps_list[i]);
  2835. if (!s->pps_list[i])
  2836. return AVERROR(ENOMEM);
  2837. }
  2838. }
  2839. av_buffer_unref(&s->current_sps);
  2840. if (s0->current_sps) {
  2841. s->current_sps = av_buffer_ref(s0->current_sps);
  2842. if (!s->current_sps)
  2843. return AVERROR(ENOMEM);
  2844. }
  2845. if (s->sps != s0->sps)
  2846. if ((ret = set_sps(s, s0->sps)) < 0)
  2847. return ret;
  2848. s->seq_decode = s0->seq_decode;
  2849. s->seq_output = s0->seq_output;
  2850. s->pocTid0 = s0->pocTid0;
  2851. s->max_ra = s0->max_ra;
  2852. s->eos = s0->eos;
  2853. s->is_nalff = s0->is_nalff;
  2854. s->nal_length_size = s0->nal_length_size;
  2855. s->threads_number = s0->threads_number;
  2856. s->threads_type = s0->threads_type;
  2857. if (s0->eos) {
  2858. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2859. s->max_ra = INT_MAX;
  2860. }
  2861. return 0;
  2862. }
  2863. static int hevc_decode_extradata(HEVCContext *s)
  2864. {
  2865. AVCodecContext *avctx = s->avctx;
  2866. GetByteContext gb;
  2867. int ret;
  2868. bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
  2869. if (avctx->extradata_size > 3 &&
  2870. (avctx->extradata[0] || avctx->extradata[1] ||
  2871. avctx->extradata[2] > 1)) {
  2872. /* It seems the extradata is encoded as hvcC format.
  2873. * Temporarily, we support configurationVersion==0 until 14496-15 3rd
  2874. * is finalized. When finalized, configurationVersion will be 1 and we
  2875. * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
  2876. int i, j, num_arrays, nal_len_size;
  2877. s->is_nalff = 1;
  2878. bytestream2_skip(&gb, 21);
  2879. nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
  2880. num_arrays = bytestream2_get_byte(&gb);
  2881. /* nal units in the hvcC always have length coded with 2 bytes,
  2882. * so put a fake nal_length_size = 2 while parsing them */
  2883. s->nal_length_size = 2;
  2884. /* Decode nal units from hvcC. */
  2885. for (i = 0; i < num_arrays; i++) {
  2886. int type = bytestream2_get_byte(&gb) & 0x3f;
  2887. int cnt = bytestream2_get_be16(&gb);
  2888. for (j = 0; j < cnt; j++) {
  2889. // +2 for the nal size field
  2890. int nalsize = bytestream2_peek_be16(&gb) + 2;
  2891. if (bytestream2_get_bytes_left(&gb) < nalsize) {
  2892. av_log(s->avctx, AV_LOG_ERROR,
  2893. "Invalid NAL unit size in extradata.\n");
  2894. return AVERROR_INVALIDDATA;
  2895. }
  2896. ret = decode_nal_units(s, gb.buffer, nalsize);
  2897. if (ret < 0) {
  2898. av_log(avctx, AV_LOG_ERROR,
  2899. "Decoding nal unit %d %d from hvcC failed\n",
  2900. type, i);
  2901. return ret;
  2902. }
  2903. bytestream2_skip(&gb, nalsize);
  2904. }
  2905. }
  2906. /* Now store right nal length size, that will be used to parse
  2907. * all other nals */
  2908. s->nal_length_size = nal_len_size;
  2909. } else {
  2910. s->is_nalff = 0;
  2911. ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
  2912. if (ret < 0)
  2913. return ret;
  2914. }
  2915. return 0;
  2916. }
  2917. static av_cold int hevc_decode_init(AVCodecContext *avctx)
  2918. {
  2919. HEVCContext *s = avctx->priv_data;
  2920. int ret;
  2921. ff_init_cabac_states();
  2922. avctx->internal->allocate_progress = 1;
  2923. ret = hevc_init_context(avctx);
  2924. if (ret < 0)
  2925. return ret;
  2926. s->enable_parallel_tiles = 0;
  2927. s->picture_struct = 0;
  2928. if(avctx->active_thread_type & FF_THREAD_SLICE)
  2929. s->threads_number = avctx->thread_count;
  2930. else
  2931. s->threads_number = 1;
  2932. if (avctx->extradata_size > 0 && avctx->extradata) {
  2933. ret = hevc_decode_extradata(s);
  2934. if (ret < 0) {
  2935. hevc_decode_free(avctx);
  2936. return ret;
  2937. }
  2938. }
  2939. if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
  2940. s->threads_type = FF_THREAD_FRAME;
  2941. else
  2942. s->threads_type = FF_THREAD_SLICE;
  2943. return 0;
  2944. }
  2945. static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
  2946. {
  2947. HEVCContext *s = avctx->priv_data;
  2948. int ret;
  2949. memset(s, 0, sizeof(*s));
  2950. ret = hevc_init_context(avctx);
  2951. if (ret < 0)
  2952. return ret;
  2953. return 0;
  2954. }
  2955. static void hevc_decode_flush(AVCodecContext *avctx)
  2956. {
  2957. HEVCContext *s = avctx->priv_data;
  2958. ff_hevc_flush_dpb(s);
  2959. s->max_ra = INT_MAX;
  2960. }
  2961. #define OFFSET(x) offsetof(HEVCContext, x)
  2962. #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  2963. static const AVProfile profiles[] = {
  2964. { FF_PROFILE_HEVC_MAIN, "Main" },
  2965. { FF_PROFILE_HEVC_MAIN_10, "Main 10" },
  2966. { FF_PROFILE_HEVC_MAIN_STILL_PICTURE, "Main Still Picture" },
  2967. { FF_PROFILE_HEVC_REXT, "Rext" },
  2968. { FF_PROFILE_UNKNOWN },
  2969. };
  2970. static const AVOption options[] = {
  2971. { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
  2972. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
  2973. { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
  2974. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
  2975. { NULL },
  2976. };
  2977. static const AVClass hevc_decoder_class = {
  2978. .class_name = "HEVC decoder",
  2979. .item_name = av_default_item_name,
  2980. .option = options,
  2981. .version = LIBAVUTIL_VERSION_INT,
  2982. };
  2983. AVCodec ff_hevc_decoder = {
  2984. .name = "hevc",
  2985. .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
  2986. .type = AVMEDIA_TYPE_VIDEO,
  2987. .id = AV_CODEC_ID_HEVC,
  2988. .priv_data_size = sizeof(HEVCContext),
  2989. .priv_class = &hevc_decoder_class,
  2990. .init = hevc_decode_init,
  2991. .close = hevc_decode_free,
  2992. .decode = hevc_decode_frame,
  2993. .flush = hevc_decode_flush,
  2994. .update_thread_context = hevc_update_thread_context,
  2995. .init_thread_copy = hevc_init_thread_copy,
  2996. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY |
  2997. CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
  2998. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  2999. };