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.

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