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.

3043 lines
112KB

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