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.

3447 lines
130KB

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