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.

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