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.

3137 lines
116KB

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