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.

3464 lines
131KB

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