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.

3080 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 && y1 < s->sps->height)
  1583. more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
  1584. if (more_data && x1 < s->sps->width &&
  1585. y1 < s->sps->height) {
  1586. return hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
  1587. }
  1588. if (more_data)
  1589. return ((x1 + cb_size_split) < s->sps->width ||
  1590. (y1 + cb_size_split) < s->sps->height);
  1591. else
  1592. return 0;
  1593. } else {
  1594. ret = hls_coding_unit(s, x0, y0, log2_cb_size);
  1595. if (ret < 0)
  1596. return ret;
  1597. if ((!((x0 + cb_size) %
  1598. (1 << (s->sps->log2_ctb_size))) ||
  1599. (x0 + cb_size >= s->sps->width)) &&
  1600. (!((y0 + cb_size) %
  1601. (1 << (s->sps->log2_ctb_size))) ||
  1602. (y0 + cb_size >= s->sps->height))) {
  1603. int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
  1604. return !end_of_slice_flag;
  1605. } else {
  1606. return 1;
  1607. }
  1608. }
  1609. return 0;
  1610. }
  1611. static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
  1612. int ctb_addr_ts)
  1613. {
  1614. HEVCLocalContext *lc = s->HEVClc;
  1615. int ctb_size = 1 << s->sps->log2_ctb_size;
  1616. int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  1617. int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
  1618. int tile_left_boundary, tile_up_boundary;
  1619. int slice_left_boundary, slice_up_boundary;
  1620. s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
  1621. if (s->pps->entropy_coding_sync_enabled_flag) {
  1622. if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
  1623. lc->first_qp_group = 1;
  1624. lc->end_of_tiles_x = s->sps->width;
  1625. } else if (s->pps->tiles_enabled_flag) {
  1626. if (ctb_addr_ts && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
  1627. int idxX = s->pps->col_idxX[x_ctb >> s->sps->log2_ctb_size];
  1628. lc->start_of_tiles_x = x_ctb;
  1629. lc->end_of_tiles_x = x_ctb + (s->pps->column_width[idxX] << s->sps->log2_ctb_size);
  1630. lc->first_qp_group = 1;
  1631. }
  1632. } else {
  1633. lc->end_of_tiles_x = s->sps->width;
  1634. }
  1635. lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->sps->height);
  1636. if (s->pps->tiles_enabled_flag) {
  1637. tile_left_boundary = x_ctb > 0 &&
  1638. s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs-1]];
  1639. slice_left_boundary = x_ctb > 0 &&
  1640. s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1];
  1641. tile_up_boundary = y_ctb > 0 &&
  1642. 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]];
  1643. slice_up_boundary = y_ctb > 0 &&
  1644. s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->sps->ctb_width];
  1645. } else {
  1646. tile_left_boundary =
  1647. tile_up_boundary = 0;
  1648. slice_left_boundary = ctb_addr_in_slice <= 0;
  1649. slice_up_boundary = ctb_addr_in_slice < s->sps->ctb_width;
  1650. }
  1651. lc->slice_or_tiles_left_boundary = slice_left_boundary + (tile_left_boundary << 1);
  1652. lc->slice_or_tiles_up_boundary = slice_up_boundary + (tile_up_boundary << 1);
  1653. lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !tile_left_boundary);
  1654. lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->sps->ctb_width) && !tile_up_boundary);
  1655. 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]]));
  1656. 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]]));
  1657. }
  1658. static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
  1659. {
  1660. HEVCContext *s = avctxt->priv_data;
  1661. int ctb_size = 1 << s->sps->log2_ctb_size;
  1662. int more_data = 1;
  1663. int x_ctb = 0;
  1664. int y_ctb = 0;
  1665. int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
  1666. if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
  1667. av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
  1668. return AVERROR_INVALIDDATA;
  1669. }
  1670. while (more_data && ctb_addr_ts < s->sps->ctb_size) {
  1671. int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  1672. x_ctb = (ctb_addr_rs % ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  1673. y_ctb = (ctb_addr_rs / ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
  1674. hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
  1675. ff_hevc_cabac_init(s, ctb_addr_ts);
  1676. hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
  1677. s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
  1678. s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
  1679. s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
  1680. more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
  1681. if (more_data < 0)
  1682. return more_data;
  1683. ctb_addr_ts++;
  1684. ff_hevc_save_states(s, ctb_addr_ts);
  1685. ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
  1686. }
  1687. if (x_ctb + ctb_size >= s->sps->width &&
  1688. y_ctb + ctb_size >= s->sps->height)
  1689. ff_hevc_hls_filter(s, x_ctb, y_ctb);
  1690. return ctb_addr_ts;
  1691. }
  1692. static int hls_slice_data(HEVCContext *s)
  1693. {
  1694. int arg[2];
  1695. int ret[2];
  1696. arg[0] = 0;
  1697. arg[1] = 1;
  1698. s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
  1699. return ret[0];
  1700. }
  1701. static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
  1702. {
  1703. HEVCContext *s1 = avctxt->priv_data, *s;
  1704. HEVCLocalContext *lc;
  1705. int ctb_size = 1<< s1->sps->log2_ctb_size;
  1706. int more_data = 1;
  1707. int *ctb_row_p = input_ctb_row;
  1708. int ctb_row = ctb_row_p[job];
  1709. int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->sps->width + ctb_size - 1) >> s1->sps->log2_ctb_size);
  1710. int ctb_addr_ts = s1->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
  1711. int thread = ctb_row % s1->threads_number;
  1712. int ret;
  1713. s = s1->sList[self_id];
  1714. lc = s->HEVClc;
  1715. if(ctb_row) {
  1716. ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
  1717. if (ret < 0)
  1718. return ret;
  1719. ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
  1720. }
  1721. while(more_data && ctb_addr_ts < s->sps->ctb_size) {
  1722. int x_ctb = (ctb_addr_rs % s->sps->ctb_width) << s->sps->log2_ctb_size;
  1723. int y_ctb = (ctb_addr_rs / s->sps->ctb_width) << s->sps->log2_ctb_size;
  1724. hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
  1725. ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
  1726. if (avpriv_atomic_int_get(&s1->wpp_err)){
  1727. ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
  1728. return 0;
  1729. }
  1730. ff_hevc_cabac_init(s, ctb_addr_ts);
  1731. hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
  1732. more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
  1733. if (more_data < 0)
  1734. return more_data;
  1735. ctb_addr_ts++;
  1736. ff_hevc_save_states(s, ctb_addr_ts);
  1737. ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
  1738. ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
  1739. if (!more_data && (x_ctb+ctb_size) < s->sps->width && ctb_row != s->sh.num_entry_point_offsets) {
  1740. avpriv_atomic_int_set(&s1->wpp_err, 1);
  1741. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  1742. return 0;
  1743. }
  1744. if ((x_ctb+ctb_size) >= s->sps->width && (y_ctb+ctb_size) >= s->sps->height ) {
  1745. ff_hevc_hls_filter(s, x_ctb, y_ctb);
  1746. ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
  1747. return ctb_addr_ts;
  1748. }
  1749. ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  1750. x_ctb+=ctb_size;
  1751. if(x_ctb >= s->sps->width) {
  1752. break;
  1753. }
  1754. }
  1755. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  1756. return 0;
  1757. }
  1758. static int hls_slice_data_wpp(HEVCContext *s, const uint8_t *nal, int length)
  1759. {
  1760. HEVCLocalContext *lc = s->HEVClc;
  1761. int *ret = av_malloc((s->sh.num_entry_point_offsets + 1) * sizeof(int));
  1762. int *arg = av_malloc((s->sh.num_entry_point_offsets + 1) * sizeof(int));
  1763. int offset;
  1764. int startheader, cmpt = 0;
  1765. int i, j, res = 0;
  1766. if (!s->sList[1]) {
  1767. ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
  1768. for (i = 1; i < s->threads_number; i++) {
  1769. s->sList[i] = av_malloc(sizeof(HEVCContext));
  1770. memcpy(s->sList[i], s, sizeof(HEVCContext));
  1771. s->HEVClcList[i] = av_malloc(sizeof(HEVCLocalContext));
  1772. s->sList[i]->HEVClc = s->HEVClcList[i];
  1773. }
  1774. }
  1775. offset = (lc->gb.index >> 3);
  1776. for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < s->skipped_bytes; j++) {
  1777. if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
  1778. startheader--;
  1779. cmpt++;
  1780. }
  1781. }
  1782. for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
  1783. offset += (s->sh.entry_point_offset[i - 1] - cmpt);
  1784. for (j = 0, cmpt = 0, startheader = offset
  1785. + s->sh.entry_point_offset[i]; j < s->skipped_bytes; j++) {
  1786. if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
  1787. startheader--;
  1788. cmpt++;
  1789. }
  1790. }
  1791. s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
  1792. s->sh.offset[i - 1] = offset;
  1793. }
  1794. if (s->sh.num_entry_point_offsets != 0) {
  1795. offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
  1796. s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
  1797. s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
  1798. }
  1799. s->data = nal;
  1800. for (i = 1; i < s->threads_number; i++) {
  1801. s->sList[i]->HEVClc->first_qp_group = 1;
  1802. s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
  1803. memcpy(s->sList[i], s, sizeof(HEVCContext));
  1804. s->sList[i]->HEVClc = s->HEVClcList[i];
  1805. }
  1806. avpriv_atomic_int_set(&s->wpp_err, 0);
  1807. ff_reset_entries(s->avctx);
  1808. for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
  1809. arg[i] = i;
  1810. ret[i] = 0;
  1811. }
  1812. if (s->pps->entropy_coding_sync_enabled_flag)
  1813. s->avctx->execute2(s->avctx, (void *) hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
  1814. for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
  1815. res += ret[i];
  1816. av_free(ret);
  1817. av_free(arg);
  1818. return res;
  1819. }
  1820. /**
  1821. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  1822. * 0 if the unit should be skipped, 1 otherwise
  1823. */
  1824. static int hls_nal_unit(HEVCContext *s)
  1825. {
  1826. GetBitContext *gb = &s->HEVClc->gb;
  1827. int nuh_layer_id;
  1828. if (get_bits1(gb) != 0)
  1829. return AVERROR_INVALIDDATA;
  1830. s->nal_unit_type = get_bits(gb, 6);
  1831. nuh_layer_id = get_bits(gb, 6);
  1832. s->temporal_id = get_bits(gb, 3) - 1;
  1833. if (s->temporal_id < 0)
  1834. return AVERROR_INVALIDDATA;
  1835. av_log(s->avctx, AV_LOG_DEBUG,
  1836. "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
  1837. s->nal_unit_type, nuh_layer_id, s->temporal_id);
  1838. return nuh_layer_id == 0;
  1839. }
  1840. static void restore_tqb_pixels(HEVCContext *s)
  1841. {
  1842. int min_pu_size = 1 << s->sps->log2_min_pu_size;
  1843. int x, y, c_idx;
  1844. for (c_idx = 0; c_idx < 3; c_idx++) {
  1845. ptrdiff_t stride = s->frame->linesize[c_idx];
  1846. int hshift = s->sps->hshift[c_idx];
  1847. int vshift = s->sps->vshift[c_idx];
  1848. for (y = 0; y < s->sps->min_pu_height; y++) {
  1849. for (x = 0; x < s->sps->min_pu_width; x++) {
  1850. if (s->is_pcm[y * s->sps->min_pu_width + x]) {
  1851. int n;
  1852. int len = min_pu_size >> hshift;
  1853. 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)];
  1854. 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)];
  1855. for (n = 0; n < (min_pu_size >> vshift); n++) {
  1856. memcpy(dst, src, len);
  1857. src += stride;
  1858. dst += stride;
  1859. }
  1860. }
  1861. }
  1862. }
  1863. }
  1864. }
  1865. static int set_side_data(HEVCContext *s)
  1866. {
  1867. AVFrame *out = s->ref->frame;
  1868. if (s->sei_frame_packing_present &&
  1869. s->frame_packing_arrangement_type >= 3 &&
  1870. s->frame_packing_arrangement_type <= 5 &&
  1871. s->content_interpretation_type > 0 &&
  1872. s->content_interpretation_type < 3) {
  1873. AVStereo3D *stereo = av_stereo3d_create_side_data(out);
  1874. if (!stereo)
  1875. return AVERROR(ENOMEM);
  1876. switch (s->frame_packing_arrangement_type) {
  1877. case 3:
  1878. if (s->quincunx_subsampling)
  1879. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  1880. else
  1881. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  1882. break;
  1883. case 4:
  1884. stereo->type = AV_STEREO3D_TOPBOTTOM;
  1885. break;
  1886. case 5:
  1887. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  1888. break;
  1889. }
  1890. if (s->content_interpretation_type == 2)
  1891. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  1892. }
  1893. return 0;
  1894. }
  1895. static int hevc_frame_start(HEVCContext *s)
  1896. {
  1897. HEVCLocalContext *lc = s->HEVClc;
  1898. int ret;
  1899. memset(s->horizontal_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
  1900. memset(s->vertical_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
  1901. memset(s->cbf_luma, 0, s->sps->min_tb_width * s->sps->min_tb_height);
  1902. memset(s->is_pcm, 0, s->sps->min_pu_width * s->sps->min_pu_height);
  1903. lc->start_of_tiles_x = 0;
  1904. s->is_decoded = 0;
  1905. if (s->pps->tiles_enabled_flag)
  1906. lc->end_of_tiles_x = s->pps->column_width[0] << s->sps->log2_ctb_size;
  1907. ret = ff_hevc_set_new_ref(s, s->sps->sao_enabled ? &s->sao_frame : &s->frame,
  1908. s->poc);
  1909. if (ret < 0)
  1910. goto fail;
  1911. ret = ff_hevc_frame_rps(s);
  1912. if (ret < 0) {
  1913. av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
  1914. goto fail;
  1915. }
  1916. ret = set_side_data(s);
  1917. if (ret < 0)
  1918. goto fail;
  1919. av_frame_unref(s->output_frame);
  1920. ret = ff_hevc_output_frame(s, s->output_frame, 0);
  1921. if (ret < 0)
  1922. goto fail;
  1923. ff_thread_finish_setup(s->avctx);
  1924. return 0;
  1925. fail:
  1926. if (s->ref && s->threads_type == FF_THREAD_FRAME)
  1927. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  1928. s->ref = NULL;
  1929. return ret;
  1930. }
  1931. static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
  1932. {
  1933. HEVCLocalContext *lc = s->HEVClc;
  1934. GetBitContext *gb = &lc->gb;
  1935. int ctb_addr_ts, ret;
  1936. ret = init_get_bits8(gb, nal, length);
  1937. if (ret < 0)
  1938. return ret;
  1939. ret = hls_nal_unit(s);
  1940. if (ret < 0) {
  1941. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  1942. s->nal_unit_type);
  1943. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  1944. return ret;
  1945. return 0;
  1946. } else if (!ret)
  1947. return 0;
  1948. switch (s->nal_unit_type) {
  1949. case NAL_VPS:
  1950. ret = ff_hevc_decode_nal_vps(s);
  1951. if (ret < 0)
  1952. return ret;
  1953. break;
  1954. case NAL_SPS:
  1955. ret = ff_hevc_decode_nal_sps(s);
  1956. if (ret < 0)
  1957. return ret;
  1958. break;
  1959. case NAL_PPS:
  1960. ret = ff_hevc_decode_nal_pps(s);
  1961. if (ret < 0)
  1962. return ret;
  1963. break;
  1964. case NAL_SEI_PREFIX:
  1965. case NAL_SEI_SUFFIX:
  1966. ret = ff_hevc_decode_nal_sei(s);
  1967. if (ret < 0)
  1968. return ret;
  1969. break;
  1970. case NAL_TRAIL_R:
  1971. case NAL_TRAIL_N:
  1972. case NAL_TSA_N:
  1973. case NAL_TSA_R:
  1974. case NAL_STSA_N:
  1975. case NAL_STSA_R:
  1976. case NAL_BLA_W_LP:
  1977. case NAL_BLA_W_RADL:
  1978. case NAL_BLA_N_LP:
  1979. case NAL_IDR_W_RADL:
  1980. case NAL_IDR_N_LP:
  1981. case NAL_CRA_NUT:
  1982. case NAL_RADL_N:
  1983. case NAL_RADL_R:
  1984. case NAL_RASL_N:
  1985. case NAL_RASL_R:
  1986. ret = hls_slice_header(s);
  1987. if (ret < 0)
  1988. return ret;
  1989. if (s->max_ra == INT_MAX) {
  1990. if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
  1991. s->max_ra = s->poc;
  1992. } else {
  1993. if (IS_IDR(s))
  1994. s->max_ra = INT_MIN;
  1995. }
  1996. }
  1997. if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
  1998. s->poc <= s->max_ra) {
  1999. s->is_decoded = 0;
  2000. break;
  2001. } else {
  2002. if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
  2003. s->max_ra = INT_MIN;
  2004. }
  2005. if (s->sh.first_slice_in_pic_flag) {
  2006. ret = hevc_frame_start(s);
  2007. if (ret < 0)
  2008. return ret;
  2009. } else if (!s->ref) {
  2010. av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
  2011. return AVERROR_INVALIDDATA;
  2012. }
  2013. if (!s->sh.dependent_slice_segment_flag &&
  2014. s->sh.slice_type != I_SLICE) {
  2015. ret = ff_hevc_slice_rpl(s);
  2016. if (ret < 0) {
  2017. av_log(s->avctx, AV_LOG_WARNING,
  2018. "Error constructing the reference lists for the current slice.\n");
  2019. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  2020. return ret;
  2021. }
  2022. }
  2023. if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
  2024. ctb_addr_ts = hls_slice_data_wpp(s, nal, length);
  2025. else
  2026. ctb_addr_ts = hls_slice_data(s);
  2027. if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {
  2028. s->is_decoded = 1;
  2029. if ((s->pps->transquant_bypass_enable_flag ||
  2030. (s->sps->pcm.loop_filter_disable_flag && s->sps->pcm_enabled_flag)) &&
  2031. s->sps->sao_enabled)
  2032. restore_tqb_pixels(s);
  2033. }
  2034. if (ctb_addr_ts < 0)
  2035. return ctb_addr_ts;
  2036. break;
  2037. case NAL_EOS_NUT:
  2038. case NAL_EOB_NUT:
  2039. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2040. s->max_ra = INT_MAX;
  2041. break;
  2042. case NAL_AUD:
  2043. case NAL_FD_NUT:
  2044. break;
  2045. default:
  2046. av_log(s->avctx, AV_LOG_INFO,
  2047. "Skipping NAL unit %d\n", s->nal_unit_type);
  2048. }
  2049. return 0;
  2050. }
  2051. /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
  2052. * between these functions would be nice. */
  2053. int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
  2054. HEVCNAL *nal)
  2055. {
  2056. int i, si, di;
  2057. uint8_t *dst;
  2058. s->skipped_bytes = 0;
  2059. #define STARTCODE_TEST \
  2060. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  2061. if (src[i + 2] != 3) { \
  2062. /* startcode, so we must be past the end */ \
  2063. length = i; \
  2064. } \
  2065. break; \
  2066. }
  2067. #if HAVE_FAST_UNALIGNED
  2068. #define FIND_FIRST_ZERO \
  2069. if (i > 0 && !src[i]) \
  2070. i--; \
  2071. while (src[i]) \
  2072. i++
  2073. #if HAVE_FAST_64BIT
  2074. for (i = 0; i + 1 < length; i += 9) {
  2075. if (!((~AV_RN64A(src + i) &
  2076. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  2077. 0x8000800080008080ULL))
  2078. continue;
  2079. FIND_FIRST_ZERO;
  2080. STARTCODE_TEST;
  2081. i -= 7;
  2082. }
  2083. #else
  2084. for (i = 0; i + 1 < length; i += 5) {
  2085. if (!((~AV_RN32A(src + i) &
  2086. (AV_RN32A(src + i) - 0x01000101U)) &
  2087. 0x80008080U))
  2088. continue;
  2089. FIND_FIRST_ZERO;
  2090. STARTCODE_TEST;
  2091. i -= 3;
  2092. }
  2093. #endif /* HAVE_FAST_64BIT */
  2094. #else
  2095. for (i = 0; i + 1 < length; i += 2) {
  2096. if (src[i])
  2097. continue;
  2098. if (i > 0 && src[i - 1] == 0)
  2099. i--;
  2100. STARTCODE_TEST;
  2101. }
  2102. #endif /* HAVE_FAST_UNALIGNED */
  2103. if (i >= length - 1) { // no escaped 0
  2104. nal->data = src;
  2105. nal->size = length;
  2106. return length;
  2107. }
  2108. av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
  2109. length + FF_INPUT_BUFFER_PADDING_SIZE);
  2110. if (!nal->rbsp_buffer)
  2111. return AVERROR(ENOMEM);
  2112. dst = nal->rbsp_buffer;
  2113. memcpy(dst, src, i);
  2114. si = di = i;
  2115. while (si + 2 < length) {
  2116. // remove escapes (very rare 1:2^22)
  2117. if (src[si + 2] > 3) {
  2118. dst[di++] = src[si++];
  2119. dst[di++] = src[si++];
  2120. } else if (src[si] == 0 && src[si + 1] == 0) {
  2121. if (src[si + 2] == 3) { // escape
  2122. dst[di++] = 0;
  2123. dst[di++] = 0;
  2124. si += 3;
  2125. s->skipped_bytes++;
  2126. if (s->skipped_bytes_pos_size < s->skipped_bytes) {
  2127. s->skipped_bytes_pos_size *= 2;
  2128. av_reallocp_array(&s->skipped_bytes_pos,
  2129. s->skipped_bytes_pos_size,
  2130. sizeof(*s->skipped_bytes_pos));
  2131. if (!s->skipped_bytes_pos)
  2132. return AVERROR(ENOMEM);
  2133. }
  2134. if (s->skipped_bytes_pos)
  2135. s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
  2136. continue;
  2137. } else // next start code
  2138. goto nsc;
  2139. }
  2140. dst[di++] = src[si++];
  2141. }
  2142. while (si < length)
  2143. dst[di++] = src[si++];
  2144. nsc:
  2145. memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  2146. nal->data = dst;
  2147. nal->size = di;
  2148. return si;
  2149. }
  2150. static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
  2151. {
  2152. int i, consumed, ret = 0;
  2153. s->ref = NULL;
  2154. s->eos = 0;
  2155. /* split the input packet into NAL units, so we know the upper bound on the
  2156. * number of slices in the frame */
  2157. s->nb_nals = 0;
  2158. while (length >= 4) {
  2159. HEVCNAL *nal;
  2160. int extract_length = 0;
  2161. if (s->is_nalff) {
  2162. int i;
  2163. for (i = 0; i < s->nal_length_size; i++)
  2164. extract_length = (extract_length << 8) | buf[i];
  2165. buf += s->nal_length_size;
  2166. length -= s->nal_length_size;
  2167. if (extract_length > length) {
  2168. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
  2169. ret = AVERROR_INVALIDDATA;
  2170. goto fail;
  2171. }
  2172. } else {
  2173. /* search start code */
  2174. while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
  2175. ++buf;
  2176. --length;
  2177. if (length < 4) {
  2178. av_log(s->avctx, AV_LOG_ERROR, "No start code is found.\n");
  2179. ret = AVERROR_INVALIDDATA;
  2180. goto fail;
  2181. }
  2182. }
  2183. buf += 3;
  2184. length -= 3;
  2185. }
  2186. if (!s->is_nalff)
  2187. extract_length = length;
  2188. if (s->nals_allocated < s->nb_nals + 1) {
  2189. int new_size = s->nals_allocated + 1;
  2190. HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));
  2191. if (!tmp) {
  2192. ret = AVERROR(ENOMEM);
  2193. goto fail;
  2194. }
  2195. s->nals = tmp;
  2196. memset(s->nals + s->nals_allocated, 0,
  2197. (new_size - s->nals_allocated) * sizeof(*tmp));
  2198. av_reallocp_array(&s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));
  2199. av_reallocp_array(&s->skipped_bytes_pos_size_nal, new_size, sizeof(*s->skipped_bytes_pos_size_nal));
  2200. av_reallocp_array(&s->skipped_bytes_pos_nal, new_size, sizeof(*s->skipped_bytes_pos_nal));
  2201. s->skipped_bytes_pos_size_nal[s->nals_allocated] = 1024; // initial buffer size
  2202. 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));
  2203. s->nals_allocated = new_size;
  2204. }
  2205. s->skipped_bytes_pos_size = s->skipped_bytes_pos_size_nal[s->nb_nals];
  2206. s->skipped_bytes_pos = s->skipped_bytes_pos_nal[s->nb_nals];
  2207. nal = &s->nals[s->nb_nals];
  2208. consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
  2209. s->skipped_bytes_nal[s->nb_nals] = s->skipped_bytes;
  2210. s->skipped_bytes_pos_size_nal[s->nb_nals] = s->skipped_bytes_pos_size;
  2211. s->skipped_bytes_pos_nal[s->nb_nals++] = s->skipped_bytes_pos;
  2212. if (consumed < 0) {
  2213. ret = consumed;
  2214. goto fail;
  2215. }
  2216. ret = init_get_bits8(&s->HEVClc->gb, nal->data, nal->size);
  2217. if (ret < 0)
  2218. goto fail;
  2219. hls_nal_unit(s);
  2220. if (s->nal_unit_type == NAL_EOB_NUT ||
  2221. s->nal_unit_type == NAL_EOS_NUT)
  2222. s->eos = 1;
  2223. buf += consumed;
  2224. length -= consumed;
  2225. }
  2226. /* parse the NAL units */
  2227. for (i = 0; i < s->nb_nals; i++) {
  2228. int ret;
  2229. s->skipped_bytes = s->skipped_bytes_nal[i];
  2230. s->skipped_bytes_pos = s->skipped_bytes_pos_nal[i];
  2231. ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);
  2232. if (ret < 0) {
  2233. av_log(s->avctx, AV_LOG_WARNING,
  2234. "Error parsing NAL unit #%d.\n", i);
  2235. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  2236. goto fail;
  2237. }
  2238. }
  2239. fail:
  2240. if (s->ref && s->threads_type == FF_THREAD_FRAME)
  2241. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  2242. return ret;
  2243. }
  2244. static void print_md5(void *log_ctx, int level, uint8_t md5[16])
  2245. {
  2246. int i;
  2247. for (i = 0; i < 16; i++)
  2248. av_log(log_ctx, level, "%02"PRIx8, md5[i]);
  2249. }
  2250. static int verify_md5(HEVCContext *s, AVFrame *frame)
  2251. {
  2252. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  2253. int pixel_shift;
  2254. int i, j;
  2255. if (!desc)
  2256. return AVERROR(EINVAL);
  2257. pixel_shift = desc->comp[0].depth_minus1 > 7;
  2258. av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
  2259. s->poc);
  2260. /* the checksums are LE, so we have to byteswap for >8bpp formats
  2261. * on BE arches */
  2262. #if HAVE_BIGENDIAN
  2263. if (pixel_shift && !s->checksum_buf) {
  2264. av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
  2265. FFMAX3(frame->linesize[0], frame->linesize[1],
  2266. frame->linesize[2]));
  2267. if (!s->checksum_buf)
  2268. return AVERROR(ENOMEM);
  2269. }
  2270. #endif
  2271. for (i = 0; frame->data[i]; i++) {
  2272. int width = s->avctx->coded_width;
  2273. int height = s->avctx->coded_height;
  2274. int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
  2275. int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
  2276. uint8_t md5[16];
  2277. av_md5_init(s->md5_ctx);
  2278. for (j = 0; j < h; j++) {
  2279. const uint8_t *src = frame->data[i] + j * frame->linesize[i];
  2280. #if HAVE_BIGENDIAN
  2281. if (pixel_shift) {
  2282. s->dsp.bswap16_buf((uint16_t*)s->checksum_buf,
  2283. (const uint16_t*)src, w);
  2284. src = s->checksum_buf;
  2285. }
  2286. #endif
  2287. av_md5_update(s->md5_ctx, src, w << pixel_shift);
  2288. }
  2289. av_md5_final(s->md5_ctx, md5);
  2290. if (!memcmp(md5, s->md5[i], 16)) {
  2291. av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
  2292. print_md5(s->avctx, AV_LOG_DEBUG, md5);
  2293. av_log (s->avctx, AV_LOG_DEBUG, "; ");
  2294. } else {
  2295. av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
  2296. print_md5(s->avctx, AV_LOG_ERROR, md5);
  2297. av_log (s->avctx, AV_LOG_ERROR, " != ");
  2298. print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
  2299. av_log (s->avctx, AV_LOG_ERROR, "\n");
  2300. return AVERROR_INVALIDDATA;
  2301. }
  2302. }
  2303. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2304. return 0;
  2305. }
  2306. static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
  2307. AVPacket *avpkt)
  2308. {
  2309. int ret;
  2310. HEVCContext *s = avctx->priv_data;
  2311. if (!avpkt->size) {
  2312. ret = ff_hevc_output_frame(s, data, 1);
  2313. if (ret < 0)
  2314. return ret;
  2315. *got_output = ret;
  2316. return 0;
  2317. }
  2318. s->ref = NULL;
  2319. ret = decode_nal_units(s, avpkt->data, avpkt->size);
  2320. if (ret < 0)
  2321. return ret;
  2322. /* verify the SEI checksum */
  2323. if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
  2324. s->is_md5) {
  2325. ret = verify_md5(s, s->ref->frame);
  2326. if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
  2327. ff_hevc_unref_frame(s, s->ref, ~0);
  2328. return ret;
  2329. }
  2330. }
  2331. s->is_md5 = 0;
  2332. if (s->is_decoded) {
  2333. av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
  2334. s->is_decoded = 0;
  2335. }
  2336. if (s->output_frame->buf[0]) {
  2337. av_frame_move_ref(data, s->output_frame);
  2338. *got_output = 1;
  2339. }
  2340. return avpkt->size;
  2341. }
  2342. static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
  2343. {
  2344. int ret;
  2345. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  2346. if (ret < 0)
  2347. return ret;
  2348. dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
  2349. if (!dst->tab_mvf_buf)
  2350. goto fail;
  2351. dst->tab_mvf = src->tab_mvf;
  2352. dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
  2353. if (!dst->rpl_tab_buf)
  2354. goto fail;
  2355. dst->rpl_tab = src->rpl_tab;
  2356. dst->rpl_buf = av_buffer_ref(src->rpl_buf);
  2357. if (!dst->rpl_buf)
  2358. goto fail;
  2359. dst->poc = src->poc;
  2360. dst->ctb_count = src->ctb_count;
  2361. dst->window = src->window;
  2362. dst->flags = src->flags;
  2363. dst->sequence = src->sequence;
  2364. return 0;
  2365. fail:
  2366. ff_hevc_unref_frame(s, dst, ~0);
  2367. return AVERROR(ENOMEM);
  2368. }
  2369. static av_cold int hevc_decode_free(AVCodecContext *avctx)
  2370. {
  2371. HEVCContext *s = avctx->priv_data;
  2372. HEVCLocalContext *lc = s->HEVClc;
  2373. int i;
  2374. pic_arrays_free(s);
  2375. av_freep(&s->md5_ctx);
  2376. for(i=0; i < s->nals_allocated; i++) {
  2377. av_freep(&s->skipped_bytes_pos_nal[i]);
  2378. }
  2379. av_freep(&s->skipped_bytes_pos_size_nal);
  2380. av_freep(&s->skipped_bytes_nal);
  2381. av_freep(&s->skipped_bytes_pos_nal);
  2382. av_freep(&s->cabac_state);
  2383. av_frame_free(&s->tmp_frame);
  2384. av_frame_free(&s->output_frame);
  2385. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2386. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2387. av_frame_free(&s->DPB[i].frame);
  2388. }
  2389. for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++)
  2390. av_buffer_unref(&s->vps_list[i]);
  2391. for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
  2392. av_buffer_unref(&s->sps_list[i]);
  2393. for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
  2394. av_buffer_unref(&s->pps_list[i]);
  2395. av_freep(&s->sh.entry_point_offset);
  2396. av_freep(&s->sh.offset);
  2397. av_freep(&s->sh.size);
  2398. for (i = 1; i < s->threads_number; i++) {
  2399. lc = s->HEVClcList[i];
  2400. if (lc) {
  2401. av_freep(&s->HEVClcList[i]);
  2402. av_freep(&s->sList[i]);
  2403. }
  2404. }
  2405. if (s->HEVClc == s->HEVClcList[0])
  2406. s->HEVClc = NULL;
  2407. av_freep(&s->HEVClcList[0]);
  2408. for (i = 0; i < s->nals_allocated; i++)
  2409. av_freep(&s->nals[i].rbsp_buffer);
  2410. av_freep(&s->nals);
  2411. s->nals_allocated = 0;
  2412. return 0;
  2413. }
  2414. static av_cold int hevc_init_context(AVCodecContext *avctx)
  2415. {
  2416. HEVCContext *s = avctx->priv_data;
  2417. int i;
  2418. s->avctx = avctx;
  2419. s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
  2420. if (!s->HEVClc)
  2421. goto fail;
  2422. s->HEVClcList[0] = s->HEVClc;
  2423. s->sList[0] = s;
  2424. s->cabac_state = av_malloc(HEVC_CONTEXTS);
  2425. if (!s->cabac_state)
  2426. goto fail;
  2427. s->tmp_frame = av_frame_alloc();
  2428. if (!s->tmp_frame)
  2429. goto fail;
  2430. s->output_frame = av_frame_alloc();
  2431. if (!s->output_frame)
  2432. goto fail;
  2433. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2434. s->DPB[i].frame = av_frame_alloc();
  2435. if (!s->DPB[i].frame)
  2436. goto fail;
  2437. s->DPB[i].tf.f = s->DPB[i].frame;
  2438. }
  2439. s->max_ra = INT_MAX;
  2440. s->md5_ctx = av_md5_alloc();
  2441. if (!s->md5_ctx)
  2442. goto fail;
  2443. ff_dsputil_init(&s->dsp, avctx);
  2444. s->context_initialized = 1;
  2445. return 0;
  2446. fail:
  2447. hevc_decode_free(avctx);
  2448. return AVERROR(ENOMEM);
  2449. }
  2450. static int hevc_update_thread_context(AVCodecContext *dst,
  2451. const AVCodecContext *src)
  2452. {
  2453. HEVCContext *s = dst->priv_data;
  2454. HEVCContext *s0 = src->priv_data;
  2455. int i, ret;
  2456. if (!s->context_initialized) {
  2457. ret = hevc_init_context(dst);
  2458. if (ret < 0)
  2459. return ret;
  2460. }
  2461. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  2462. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  2463. if (s0->DPB[i].frame->buf[0]) {
  2464. ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
  2465. if (ret < 0)
  2466. return ret;
  2467. }
  2468. }
  2469. for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++) {
  2470. av_buffer_unref(&s->vps_list[i]);
  2471. if (s0->vps_list[i]) {
  2472. s->vps_list[i] = av_buffer_ref(s0->vps_list[i]);
  2473. if (!s->vps_list[i])
  2474. return AVERROR(ENOMEM);
  2475. }
  2476. }
  2477. for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++) {
  2478. av_buffer_unref(&s->sps_list[i]);
  2479. if (s0->sps_list[i]) {
  2480. s->sps_list[i] = av_buffer_ref(s0->sps_list[i]);
  2481. if (!s->sps_list[i])
  2482. return AVERROR(ENOMEM);
  2483. }
  2484. }
  2485. for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
  2486. av_buffer_unref(&s->pps_list[i]);
  2487. if (s0->pps_list[i]) {
  2488. s->pps_list[i] = av_buffer_ref(s0->pps_list[i]);
  2489. if (!s->pps_list[i])
  2490. return AVERROR(ENOMEM);
  2491. }
  2492. }
  2493. if (s->sps != s0->sps)
  2494. ret = set_sps(s, s0->sps);
  2495. s->seq_decode = s0->seq_decode;
  2496. s->seq_output = s0->seq_output;
  2497. s->pocTid0 = s0->pocTid0;
  2498. s->max_ra = s0->max_ra;
  2499. s->is_nalff = s0->is_nalff;
  2500. s->nal_length_size = s0->nal_length_size;
  2501. s->threads_number = s0->threads_number;
  2502. s->threads_type = s0->threads_type;
  2503. if (s0->eos) {
  2504. s->seq_decode = (s->seq_decode + 1) & 0xff;
  2505. s->max_ra = INT_MAX;
  2506. }
  2507. return 0;
  2508. }
  2509. static int hevc_decode_extradata(HEVCContext *s)
  2510. {
  2511. AVCodecContext *avctx = s->avctx;
  2512. GetByteContext gb;
  2513. int ret;
  2514. bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
  2515. if (avctx->extradata_size > 3 &&
  2516. (avctx->extradata[0] || avctx->extradata[1] ||
  2517. avctx->extradata[2] > 1)) {
  2518. /* It seems the extradata is encoded as hvcC format.
  2519. * Temporarily, we support configurationVersion==0 until 14496-15 3rd
  2520. * is finalized. When finalized, configurationVersion will be 1 and we
  2521. * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
  2522. int i, j, num_arrays, nal_len_size;
  2523. s->is_nalff = 1;
  2524. bytestream2_skip(&gb, 21);
  2525. nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
  2526. num_arrays = bytestream2_get_byte(&gb);
  2527. /* nal units in the hvcC always have length coded with 2 bytes,
  2528. * so put a fake nal_length_size = 2 while parsing them */
  2529. s->nal_length_size = 2;
  2530. /* Decode nal units from hvcC. */
  2531. for (i = 0; i < num_arrays; i++) {
  2532. int type = bytestream2_get_byte(&gb) & 0x3f;
  2533. int cnt = bytestream2_get_be16(&gb);
  2534. for (j = 0; j < cnt; j++) {
  2535. // +2 for the nal size field
  2536. int nalsize = bytestream2_peek_be16(&gb) + 2;
  2537. if (bytestream2_get_bytes_left(&gb) < nalsize) {
  2538. av_log(s->avctx, AV_LOG_ERROR,
  2539. "Invalid NAL unit size in extradata.\n");
  2540. return AVERROR_INVALIDDATA;
  2541. }
  2542. ret = decode_nal_units(s, gb.buffer, nalsize);
  2543. if (ret < 0) {
  2544. av_log(avctx, AV_LOG_ERROR,
  2545. "Decoding nal unit %d %d from hvcC failed\n",
  2546. type, i);
  2547. return ret;
  2548. }
  2549. bytestream2_skip(&gb, nalsize);
  2550. }
  2551. }
  2552. /* Now store right nal length size, that will be used to parse
  2553. * all other nals */
  2554. s->nal_length_size = nal_len_size;
  2555. } else {
  2556. s->is_nalff = 0;
  2557. ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
  2558. if (ret < 0)
  2559. return ret;
  2560. }
  2561. return 0;
  2562. }
  2563. static av_cold int hevc_decode_init(AVCodecContext *avctx)
  2564. {
  2565. HEVCContext *s = avctx->priv_data;
  2566. int ret;
  2567. ff_init_cabac_states();
  2568. avctx->internal->allocate_progress = 1;
  2569. ret = hevc_init_context(avctx);
  2570. if (ret < 0)
  2571. return ret;
  2572. s->enable_parallel_tiles = 0;
  2573. s->picture_struct = 0;
  2574. if(avctx->active_thread_type & FF_THREAD_SLICE)
  2575. s->threads_number = avctx->thread_count;
  2576. else
  2577. s->threads_number = 1;
  2578. if (avctx->extradata_size > 0 && avctx->extradata) {
  2579. ret = hevc_decode_extradata(s);
  2580. if (ret < 0) {
  2581. hevc_decode_free(avctx);
  2582. return ret;
  2583. }
  2584. }
  2585. if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
  2586. s->threads_type = FF_THREAD_FRAME;
  2587. else
  2588. s->threads_type = FF_THREAD_SLICE;
  2589. return 0;
  2590. }
  2591. static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
  2592. {
  2593. HEVCContext *s = avctx->priv_data;
  2594. int ret;
  2595. memset(s, 0, sizeof(*s));
  2596. ret = hevc_init_context(avctx);
  2597. if (ret < 0)
  2598. return ret;
  2599. return 0;
  2600. }
  2601. static void hevc_decode_flush(AVCodecContext *avctx)
  2602. {
  2603. HEVCContext *s = avctx->priv_data;
  2604. ff_hevc_flush_dpb(s);
  2605. s->max_ra = INT_MAX;
  2606. }
  2607. #define OFFSET(x) offsetof(HEVCContext, x)
  2608. #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  2609. static const AVProfile profiles[] = {
  2610. { FF_PROFILE_HEVC_MAIN, "Main" },
  2611. { FF_PROFILE_HEVC_MAIN_10, "Main 10" },
  2612. { FF_PROFILE_HEVC_MAIN_STILL_PICTURE, "Main Still Picture" },
  2613. { FF_PROFILE_UNKNOWN },
  2614. };
  2615. static const AVOption options[] = {
  2616. { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
  2617. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
  2618. { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
  2619. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
  2620. { NULL },
  2621. };
  2622. static const AVClass hevc_decoder_class = {
  2623. .class_name = "HEVC decoder",
  2624. .item_name = av_default_item_name,
  2625. .option = options,
  2626. .version = LIBAVUTIL_VERSION_INT,
  2627. };
  2628. AVCodec ff_hevc_decoder = {
  2629. .name = "hevc",
  2630. .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
  2631. .type = AVMEDIA_TYPE_VIDEO,
  2632. .id = AV_CODEC_ID_HEVC,
  2633. .priv_data_size = sizeof(HEVCContext),
  2634. .priv_class = &hevc_decoder_class,
  2635. .init = hevc_decode_init,
  2636. .close = hevc_decode_free,
  2637. .decode = hevc_decode_frame,
  2638. .flush = hevc_decode_flush,
  2639. .update_thread_context = hevc_update_thread_context,
  2640. .init_thread_copy = hevc_init_thread_copy,
  2641. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY |
  2642. CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
  2643. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  2644. };