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.

3504 lines
132KB

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