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.

3289 lines
123KB

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