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.

3256 lines
121KB

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