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.

3027 lines
112KB

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