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.

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