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.

2950 lines
109KB

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