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.

2932 lines
108KB

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