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.

3274 lines
122KB

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