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.

3543 lines
134KB

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