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.

3086 lines
114KB

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