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.

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