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.

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