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.

3107 lines
115KB

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