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.

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