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.

2607 lines
97KB

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