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.

3492 lines
134KB

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