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.

3272 lines
122KB

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