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.

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