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.

3502 lines
134KB

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