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.

3554 lines
134KB

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