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.

2877 lines
107KB

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