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.

3513 lines
133KB

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