FFmpeg
hevcdec.c
Go to the documentation of this file.
1 /*
2  * HEVC video Decoder
3  *
4  * Copyright (C) 2012 - 2013 Guillaume Martres
5  * Copyright (C) 2012 - 2013 Mickael Raulet
6  * Copyright (C) 2012 - 2013 Gildas Cocherel
7  * Copyright (C) 2012 - 2013 Wassim Hamidouche
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "config_components.h"
27 
28 #include "libavutil/attributes.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/common.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/md5.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/timecode.h"
38 
39 #include "aom_film_grain.h"
40 #include "bswapdsp.h"
41 #include "cabac_functions.h"
42 #include "codec_internal.h"
43 #include "decode.h"
44 #include "golomb.h"
45 #include "hevc.h"
46 #include "parse.h"
47 #include "hevcdec.h"
48 #include "hwaccel_internal.h"
49 #include "hwconfig.h"
50 #include "internal.h"
51 #include "profiles.h"
52 #include "progressframe.h"
53 #include "refstruct.h"
54 #include "thread.h"
55 
56 static const uint8_t hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
57 
58 /**
59  * NOTE: Each function hls_foo correspond to the function foo in the
60  * specification (HLS stands for High Level Syntax).
61  */
62 
63 /**
64  * Section 5.7
65  */
66 
67 /* free everything allocated by pic_arrays_init() */
69 {
70  av_freep(&s->sao);
71  av_freep(&s->deblock);
72 
73  av_freep(&s->skip_flag);
74  av_freep(&s->tab_ct_depth);
75 
76  av_freep(&s->tab_ipm);
77  av_freep(&s->cbf_luma);
78  av_freep(&s->is_pcm);
79 
80  av_freep(&s->qp_y_tab);
81  av_freep(&s->tab_slice_address);
82  av_freep(&s->filter_slice_edges);
83 
84  av_freep(&s->horizontal_bs);
85  av_freep(&s->vertical_bs);
86 
87  ff_refstruct_pool_uninit(&s->tab_mvf_pool);
88  ff_refstruct_pool_uninit(&s->rpl_tab_pool);
89 }
90 
91 /* allocate arrays that depend on frame dimensions */
92 static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
93 {
94  int log2_min_cb_size = sps->log2_min_cb_size;
95  int width = sps->width;
96  int height = sps->height;
97  int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
98  ((height >> log2_min_cb_size) + 1);
99  int ctb_count = sps->ctb_width * sps->ctb_height;
100  int min_pu_size = sps->min_pu_width * sps->min_pu_height;
101 
102  s->bs_width = (width >> 2) + 1;
103  s->bs_height = (height >> 2) + 1;
104 
105  s->sao = av_calloc(ctb_count, sizeof(*s->sao));
106  s->deblock = av_calloc(ctb_count, sizeof(*s->deblock));
107  if (!s->sao || !s->deblock)
108  goto fail;
109 
110  s->skip_flag = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
111  s->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
112  if (!s->skip_flag || !s->tab_ct_depth)
113  goto fail;
114 
115  s->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
116  s->tab_ipm = av_mallocz(min_pu_size);
117  s->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
118  if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
119  goto fail;
120 
121  s->filter_slice_edges = av_mallocz(ctb_count);
122  s->tab_slice_address = av_malloc_array(pic_size_in_ctb,
123  sizeof(*s->tab_slice_address));
124  s->qp_y_tab = av_malloc_array(pic_size_in_ctb,
125  sizeof(*s->qp_y_tab));
126  if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
127  goto fail;
128 
129  s->horizontal_bs = av_calloc(s->bs_width, s->bs_height);
130  s->vertical_bs = av_calloc(s->bs_width, s->bs_height);
131  if (!s->horizontal_bs || !s->vertical_bs)
132  goto fail;
133 
134  s->tab_mvf_pool = ff_refstruct_pool_alloc(min_pu_size * sizeof(MvField), 0);
135  s->rpl_tab_pool = ff_refstruct_pool_alloc(ctb_count * sizeof(RefPicListTab), 0);
136  if (!s->tab_mvf_pool || !s->rpl_tab_pool)
137  goto fail;
138 
139  return 0;
140 
141 fail:
143  return AVERROR(ENOMEM);
144 }
145 
146 static int pred_weight_table(SliceHeader *sh, void *logctx,
147  const HEVCSPS *sps, GetBitContext *gb)
148 {
149  int i = 0;
150  int j = 0;
151  uint8_t luma_weight_l0_flag[16];
152  uint8_t chroma_weight_l0_flag[16];
153  uint8_t luma_weight_l1_flag[16];
154  uint8_t chroma_weight_l1_flag[16];
155  int luma_log2_weight_denom;
156 
157  luma_log2_weight_denom = get_ue_golomb_long(gb);
158  if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7) {
159  av_log(logctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
160  return AVERROR_INVALIDDATA;
161  }
162  sh->luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
163  if (sps->chroma_format_idc != 0) {
164  int64_t chroma_log2_weight_denom = luma_log2_weight_denom + (int64_t)get_se_golomb(gb);
165  if (chroma_log2_weight_denom < 0 || chroma_log2_weight_denom > 7) {
166  av_log(logctx, AV_LOG_ERROR, "chroma_log2_weight_denom %"PRId64" is invalid\n", chroma_log2_weight_denom);
167  return AVERROR_INVALIDDATA;
168  }
169  sh->chroma_log2_weight_denom = chroma_log2_weight_denom;
170  }
171 
172  for (i = 0; i < sh->nb_refs[L0]; i++) {
173  luma_weight_l0_flag[i] = get_bits1(gb);
174  if (!luma_weight_l0_flag[i]) {
175  sh->luma_weight_l0[i] = 1 << sh->luma_log2_weight_denom;
176  sh->luma_offset_l0[i] = 0;
177  }
178  }
179  if (sps->chroma_format_idc != 0) {
180  for (i = 0; i < sh->nb_refs[L0]; i++)
181  chroma_weight_l0_flag[i] = get_bits1(gb);
182  } else {
183  for (i = 0; i < sh->nb_refs[L0]; i++)
184  chroma_weight_l0_flag[i] = 0;
185  }
186  for (i = 0; i < sh->nb_refs[L0]; i++) {
187  if (luma_weight_l0_flag[i]) {
188  int delta_luma_weight_l0 = get_se_golomb(gb);
189  if ((int8_t)delta_luma_weight_l0 != delta_luma_weight_l0)
190  return AVERROR_INVALIDDATA;
191  sh->luma_weight_l0[i] = (1 << sh->luma_log2_weight_denom) + delta_luma_weight_l0;
192  sh->luma_offset_l0[i] = get_se_golomb(gb);
193  }
194  if (chroma_weight_l0_flag[i]) {
195  for (j = 0; j < 2; j++) {
196  int delta_chroma_weight_l0 = get_se_golomb(gb);
197  int delta_chroma_offset_l0 = get_se_golomb(gb);
198 
199  if ( (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
200  || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
201  return AVERROR_INVALIDDATA;
202  }
203 
204  sh->chroma_weight_l0[i][j] = (1 << sh->chroma_log2_weight_denom) + delta_chroma_weight_l0;
205  sh->chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * sh->chroma_weight_l0[i][j])
206  >> sh->chroma_log2_weight_denom) + 128), -128, 127);
207  }
208  } else {
209  sh->chroma_weight_l0[i][0] = 1 << sh->chroma_log2_weight_denom;
210  sh->chroma_offset_l0[i][0] = 0;
211  sh->chroma_weight_l0[i][1] = 1 << sh->chroma_log2_weight_denom;
212  sh->chroma_offset_l0[i][1] = 0;
213  }
214  }
215  if (sh->slice_type == HEVC_SLICE_B) {
216  for (i = 0; i < sh->nb_refs[L1]; i++) {
217  luma_weight_l1_flag[i] = get_bits1(gb);
218  if (!luma_weight_l1_flag[i]) {
219  sh->luma_weight_l1[i] = 1 << sh->luma_log2_weight_denom;
220  sh->luma_offset_l1[i] = 0;
221  }
222  }
223  if (sps->chroma_format_idc != 0) {
224  for (i = 0; i < sh->nb_refs[L1]; i++)
225  chroma_weight_l1_flag[i] = get_bits1(gb);
226  } else {
227  for (i = 0; i < sh->nb_refs[L1]; i++)
228  chroma_weight_l1_flag[i] = 0;
229  }
230  for (i = 0; i < sh->nb_refs[L1]; i++) {
231  if (luma_weight_l1_flag[i]) {
232  int delta_luma_weight_l1 = get_se_golomb(gb);
233  if ((int8_t)delta_luma_weight_l1 != delta_luma_weight_l1)
234  return AVERROR_INVALIDDATA;
235  sh->luma_weight_l1[i] = (1 << sh->luma_log2_weight_denom) + delta_luma_weight_l1;
236  sh->luma_offset_l1[i] = get_se_golomb(gb);
237  }
238  if (chroma_weight_l1_flag[i]) {
239  for (j = 0; j < 2; j++) {
240  int delta_chroma_weight_l1 = get_se_golomb(gb);
241  int delta_chroma_offset_l1 = get_se_golomb(gb);
242 
243  if ( (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
244  || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
245  return AVERROR_INVALIDDATA;
246  }
247 
248  sh->chroma_weight_l1[i][j] = (1 << sh->chroma_log2_weight_denom) + delta_chroma_weight_l1;
249  sh->chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * sh->chroma_weight_l1[i][j])
250  >> sh->chroma_log2_weight_denom) + 128), -128, 127);
251  }
252  } else {
253  sh->chroma_weight_l1[i][0] = 1 << sh->chroma_log2_weight_denom;
254  sh->chroma_offset_l1[i][0] = 0;
255  sh->chroma_weight_l1[i][1] = 1 << sh->chroma_log2_weight_denom;
256  sh->chroma_offset_l1[i][1] = 0;
257  }
258  }
259  }
260  return 0;
261 }
262 
263 static int decode_lt_rps(const HEVCSPS *sps, LongTermRPS *rps,
264  GetBitContext *gb, int cur_poc, int poc_lsb)
265 {
266  int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
267  int prev_delta_msb = 0;
268  unsigned int nb_sps = 0, nb_sh;
269  int i;
270 
271  rps->nb_refs = 0;
272  if (!sps->long_term_ref_pics_present)
273  return 0;
274 
275  if (sps->num_long_term_ref_pics_sps > 0)
276  nb_sps = get_ue_golomb_long(gb);
277  nb_sh = get_ue_golomb_long(gb);
278 
279  if (nb_sps > sps->num_long_term_ref_pics_sps)
280  return AVERROR_INVALIDDATA;
281  if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
282  return AVERROR_INVALIDDATA;
283 
284  rps->nb_refs = nb_sh + nb_sps;
285 
286  for (i = 0; i < rps->nb_refs; i++) {
287 
288  if (i < nb_sps) {
289  uint8_t lt_idx_sps = 0;
290 
291  if (sps->num_long_term_ref_pics_sps > 1)
292  lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
293 
294  rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
295  rps->used[i] = !!(sps->used_by_curr_pic_lt & (1 << lt_idx_sps));
296  } else {
297  rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
298  rps->used[i] = get_bits1(gb);
299  }
300 
301  rps->poc_msb_present[i] = get_bits1(gb);
302  if (rps->poc_msb_present[i]) {
304  int64_t poc;
305 
306  if (i && i != nb_sps)
307  delta += prev_delta_msb;
308 
309  poc = rps->poc[i] + cur_poc - delta * max_poc_lsb - poc_lsb;
310  if (poc != (int32_t)poc)
311  return AVERROR_INVALIDDATA;
312  rps->poc[i] = poc;
313  prev_delta_msb = delta;
314  }
315  }
316 
317  return 0;
318 }
319 
321 {
322  AVCodecContext *avctx = s->avctx;
323  const HEVCVPS *vps = sps->vps;
324  const HEVCWindow *ow = &sps->output_window;
325  unsigned int num = 0, den = 0;
326 
327  avctx->pix_fmt = sps->pix_fmt;
328  avctx->coded_width = sps->width;
329  avctx->coded_height = sps->height;
330  avctx->width = sps->width - ow->left_offset - ow->right_offset;
331  avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
332  avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
333  avctx->profile = sps->ptl.general_ptl.profile_idc;
334  avctx->level = sps->ptl.general_ptl.level_idc;
335 
336  ff_set_sar(avctx, sps->vui.common.sar);
337 
338  if (sps->vui.common.video_signal_type_present_flag)
339  avctx->color_range = sps->vui.common.video_full_range_flag ? AVCOL_RANGE_JPEG
341  else
342  avctx->color_range = AVCOL_RANGE_MPEG;
343 
344  if (sps->vui.common.colour_description_present_flag) {
345  avctx->color_primaries = sps->vui.common.colour_primaries;
346  avctx->color_trc = sps->vui.common.transfer_characteristics;
347  avctx->colorspace = sps->vui.common.matrix_coeffs;
348  } else {
352  }
353 
355  if (sps->chroma_format_idc == 1) {
356  if (sps->vui.common.chroma_loc_info_present_flag) {
357  if (sps->vui.common.chroma_sample_loc_type_top_field <= 5)
358  avctx->chroma_sample_location = sps->vui.common.chroma_sample_loc_type_top_field + 1;
359  } else
361  }
362 
363  if (vps->vps_timing_info_present_flag) {
364  num = vps->vps_num_units_in_tick;
365  den = vps->vps_time_scale;
366  } else if (sps->vui.vui_timing_info_present_flag) {
367  num = sps->vui.vui_num_units_in_tick;
368  den = sps->vui.vui_time_scale;
369  }
370 
371  if (num != 0 && den != 0)
372  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
373  num, den, 1 << 30);
374 }
375 
377 {
378  AVCodecContext *avctx = s->avctx;
379 
380  if (s->sei.common.a53_caption.buf_ref)
381  s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
382 
383  if (s->sei.common.alternative_transfer.present &&
384  av_color_transfer_name(s->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
385  s->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
386  avctx->color_trc = s->sei.common.alternative_transfer.preferred_transfer_characteristics;
387  }
388 
389  if (s->sei.common.film_grain_characteristics.present ||
390  s->sei.common.aom_film_grain.enable)
392 
393  return 0;
394 }
395 
397 {
398 #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
399  CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
400  CONFIG_HEVC_D3D12VA_HWACCEL + \
401  CONFIG_HEVC_NVDEC_HWACCEL + \
402  CONFIG_HEVC_VAAPI_HWACCEL + \
403  CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
404  CONFIG_HEVC_VDPAU_HWACCEL + \
405  CONFIG_HEVC_VULKAN_HWACCEL)
406  enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
407 
408  switch (sps->pix_fmt) {
409  case AV_PIX_FMT_YUV420P:
410  case AV_PIX_FMT_YUVJ420P:
411 #if CONFIG_HEVC_DXVA2_HWACCEL
412  *fmt++ = AV_PIX_FMT_DXVA2_VLD;
413 #endif
414 #if CONFIG_HEVC_D3D11VA_HWACCEL
415  *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
416  *fmt++ = AV_PIX_FMT_D3D11;
417 #endif
418 #if CONFIG_HEVC_D3D12VA_HWACCEL
419  *fmt++ = AV_PIX_FMT_D3D12;
420 #endif
421 #if CONFIG_HEVC_VAAPI_HWACCEL
422  *fmt++ = AV_PIX_FMT_VAAPI;
423 #endif
424 #if CONFIG_HEVC_VDPAU_HWACCEL
425  *fmt++ = AV_PIX_FMT_VDPAU;
426 #endif
427 #if CONFIG_HEVC_NVDEC_HWACCEL
428  *fmt++ = AV_PIX_FMT_CUDA;
429 #endif
430 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
431  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
432 #endif
433 #if CONFIG_HEVC_VULKAN_HWACCEL
434  *fmt++ = AV_PIX_FMT_VULKAN;
435 #endif
436  break;
438 #if CONFIG_HEVC_DXVA2_HWACCEL
439  *fmt++ = AV_PIX_FMT_DXVA2_VLD;
440 #endif
441 #if CONFIG_HEVC_D3D11VA_HWACCEL
442  *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
443  *fmt++ = AV_PIX_FMT_D3D11;
444 #endif
445 #if CONFIG_HEVC_D3D12VA_HWACCEL
446  *fmt++ = AV_PIX_FMT_D3D12;
447 #endif
448 #if CONFIG_HEVC_VAAPI_HWACCEL
449  *fmt++ = AV_PIX_FMT_VAAPI;
450 #endif
451 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
452  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
453 #endif
454 #if CONFIG_HEVC_VULKAN_HWACCEL
455  *fmt++ = AV_PIX_FMT_VULKAN;
456 #endif
457 #if CONFIG_HEVC_VDPAU_HWACCEL
458  *fmt++ = AV_PIX_FMT_VDPAU;
459 #endif
460 #if CONFIG_HEVC_NVDEC_HWACCEL
461  *fmt++ = AV_PIX_FMT_CUDA;
462 #endif
463  break;
464  case AV_PIX_FMT_YUV444P:
465 #if CONFIG_HEVC_VAAPI_HWACCEL
466  *fmt++ = AV_PIX_FMT_VAAPI;
467 #endif
468 #if CONFIG_HEVC_VDPAU_HWACCEL
469  *fmt++ = AV_PIX_FMT_VDPAU;
470 #endif
471 #if CONFIG_HEVC_NVDEC_HWACCEL
472  *fmt++ = AV_PIX_FMT_CUDA;
473 #endif
474 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
475  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
476 #endif
477 #if CONFIG_HEVC_VULKAN_HWACCEL
478  *fmt++ = AV_PIX_FMT_VULKAN;
479 #endif
480  break;
481  case AV_PIX_FMT_YUV422P:
483 #if CONFIG_HEVC_VAAPI_HWACCEL
484  *fmt++ = AV_PIX_FMT_VAAPI;
485 #endif
486 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
487  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
488 #endif
489 #if CONFIG_HEVC_VULKAN_HWACCEL
490  *fmt++ = AV_PIX_FMT_VULKAN;
491 #endif
492  break;
494 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
495  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
496 #endif
497  /* NOTE: fallthrough */
500 #if CONFIG_HEVC_VAAPI_HWACCEL
501  *fmt++ = AV_PIX_FMT_VAAPI;
502 #endif
503 #if CONFIG_HEVC_VDPAU_HWACCEL
504  *fmt++ = AV_PIX_FMT_VDPAU;
505 #endif
506 #if CONFIG_HEVC_VULKAN_HWACCEL
507  *fmt++ = AV_PIX_FMT_VULKAN;
508 #endif
509 #if CONFIG_HEVC_NVDEC_HWACCEL
510  *fmt++ = AV_PIX_FMT_CUDA;
511 #endif
512  break;
514 #if CONFIG_HEVC_VAAPI_HWACCEL
515  *fmt++ = AV_PIX_FMT_VAAPI;
516 #endif
517 #if CONFIG_HEVC_VULKAN_HWACCEL
518  *fmt++ = AV_PIX_FMT_VULKAN;
519 #endif
520  break;
521  }
522 
523  *fmt++ = sps->pix_fmt;
524  *fmt = AV_PIX_FMT_NONE;
525 
526  return ff_get_format(s->avctx, pix_fmts);
527 }
528 
529 static int set_sps(HEVCContext *s, const HEVCSPS *sps)
530 {
531  int ret, i;
532 
534  s->ps.sps = NULL;
535  s->ps.vps = NULL;
536 
537  if (!sps)
538  return 0;
539 
540  ret = pic_arrays_init(s, sps);
541  if (ret < 0)
542  goto fail;
543 
544  ff_hevc_pred_init(&s->hpc, sps->bit_depth);
545  ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
546  ff_videodsp_init (&s->vdsp, sps->bit_depth);
547 
548  for (i = 0; i < 3; i++) {
549  av_freep(&s->sao_pixel_buffer_h[i]);
550  av_freep(&s->sao_pixel_buffer_v[i]);
551  }
552 
553  if (sps->sao_enabled && !s->avctx->hwaccel) {
554  int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
555  int c_idx;
556 
557  for(c_idx = 0; c_idx < c_count; c_idx++) {
558  int w = sps->width >> sps->hshift[c_idx];
559  int h = sps->height >> sps->vshift[c_idx];
560  s->sao_pixel_buffer_h[c_idx] =
561  av_malloc((w * 2 * sps->ctb_height) <<
562  sps->pixel_shift);
563  s->sao_pixel_buffer_v[c_idx] =
564  av_malloc((h * 2 * sps->ctb_width) <<
565  sps->pixel_shift);
566  if (!s->sao_pixel_buffer_h[c_idx] ||
567  !s->sao_pixel_buffer_v[c_idx])
568  goto fail;
569  }
570  }
571 
572  s->ps.sps = sps;
573  s->ps.vps = sps->vps;
574 
575  return 0;
576 
577 fail:
579  for (i = 0; i < 3; i++) {
580  av_freep(&s->sao_pixel_buffer_h[i]);
581  av_freep(&s->sao_pixel_buffer_v[i]);
582  }
583  s->ps.sps = NULL;
584  return ret;
585 }
586 
588 {
589  const HEVCPPS *pps;
590  const HEVCSPS *sps;
591  unsigned pps_id;
592  int i, ret;
593 
594  // Coded parameters
596 
598  if (IS_IRAP(s))
600 
601  pps_id = get_ue_golomb_long(gb);
602  if (pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[pps_id]) {
603  av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
604  return AVERROR_INVALIDDATA;
605  }
606  if (!sh->first_slice_in_pic_flag && s->ps.pps_list[pps_id] != s->pps) {
607  av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
608  return AVERROR_INVALIDDATA;
609  }
610  sh->pps_id = pps_id;
611 
612  pps = s->ps.pps_list[pps_id];
613  sps = pps->sps;
614 
615  if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
617 
619  if (!sh->first_slice_in_pic_flag) {
620  int slice_address_length;
621 
622  if (pps->dependent_slice_segments_enabled_flag)
624 
625  slice_address_length = av_ceil_log2(sps->ctb_width *
626  sps->ctb_height);
627  sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
628  if (sh->slice_segment_addr >= sps->ctb_width * sps->ctb_height) {
629  av_log(s->avctx, AV_LOG_ERROR,
630  "Invalid slice segment address: %u.\n",
631  sh->slice_segment_addr);
632  return AVERROR_INVALIDDATA;
633  }
634 
635  if (!sh->dependent_slice_segment_flag) {
636  sh->slice_addr = sh->slice_segment_addr;
637  }
638  } else {
639  sh->slice_segment_addr = sh->slice_addr = 0;
640  }
641 
642  if (!sh->dependent_slice_segment_flag) {
643  for (i = 0; i < pps->num_extra_slice_header_bits; i++)
644  skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
645 
646  sh->slice_type = get_ue_golomb_long(gb);
647  if (!(sh->slice_type == HEVC_SLICE_I ||
648  sh->slice_type == HEVC_SLICE_P ||
649  sh->slice_type == HEVC_SLICE_B)) {
650  av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
651  sh->slice_type);
652  return AVERROR_INVALIDDATA;
653  }
654  if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I &&
655  !pps->pps_curr_pic_ref_enabled_flag) {
656  av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
657  return AVERROR_INVALIDDATA;
658  }
659 
660  // when flag is not present, picture is inferred to be output
661  sh->pic_output_flag = 1;
662  if (pps->output_flag_present_flag)
663  sh->pic_output_flag = get_bits1(gb);
664 
665  if (sps->separate_colour_plane)
666  sh->colour_plane_id = get_bits(gb, 2);
667 
668  if (!IS_IDR(s)) {
669  int poc, pos;
670 
671  sh->pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
672  poc = ff_hevc_compute_poc(sps, s->poc_tid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
673  if (!sh->first_slice_in_pic_flag && poc != sh->poc) {
674  av_log(s->avctx, AV_LOG_WARNING,
675  "Ignoring POC change between slices: %d -> %d\n", poc, sh->poc);
676  if (s->avctx->err_recognition & AV_EF_EXPLODE)
677  return AVERROR_INVALIDDATA;
678  poc = sh->poc;
679  }
680  sh->poc = poc;
681 
683  pos = get_bits_left(gb);
685  ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, sps, 1);
686  if (ret < 0)
687  return ret;
688 
689  sh->short_term_rps = &sh->slice_rps;
690  } else {
691  int numbits, rps_idx;
692 
693  if (!sps->nb_st_rps) {
694  av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
695  return AVERROR_INVALIDDATA;
696  }
697 
698  numbits = av_ceil_log2(sps->nb_st_rps);
699  rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
700  sh->short_term_rps = &sps->st_rps[rps_idx];
701  }
703 
704  pos = get_bits_left(gb);
705  ret = decode_lt_rps(sps, &sh->long_term_rps, gb, sh->poc, sh->pic_order_cnt_lsb);
706  if (ret < 0) {
707  av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
708  if (s->avctx->err_recognition & AV_EF_EXPLODE)
709  return AVERROR_INVALIDDATA;
710  }
712 
713  if (sps->temporal_mvp_enabled)
715  else
717  } else {
718  sh->poc = 0;
719  sh->pic_order_cnt_lsb = 0;
722  sh->short_term_rps = NULL;
725  }
726 
727  if (sps->sao_enabled) {
729  if (sps->chroma_format_idc) {
732  }
733  } else {
737  }
738 
739  sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
740  if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
741  int nb_refs;
742 
743  sh->nb_refs[L0] = pps->num_ref_idx_l0_default_active;
744  if (sh->slice_type == HEVC_SLICE_B)
745  sh->nb_refs[L1] = pps->num_ref_idx_l1_default_active;
746 
747  if (get_bits1(gb)) { // num_ref_idx_active_override_flag
748  sh->nb_refs[L0] = get_ue_golomb_31(gb) + 1;
749  if (sh->slice_type == HEVC_SLICE_B)
750  sh->nb_refs[L1] = get_ue_golomb_31(gb) + 1;
751  }
752  if (sh->nb_refs[L0] >= HEVC_MAX_REFS || sh->nb_refs[L1] >= HEVC_MAX_REFS) {
753  av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
754  sh->nb_refs[L0], sh->nb_refs[L1]);
755  return AVERROR_INVALIDDATA;
756  }
757 
758  sh->rpl_modification_flag[0] = 0;
759  sh->rpl_modification_flag[1] = 0;
760  nb_refs = ff_hevc_frame_nb_refs(sh, pps);
761  if (!nb_refs) {
762  av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
763  return AVERROR_INVALIDDATA;
764  }
765 
766  if (pps->lists_modification_present_flag && nb_refs > 1) {
767  sh->rpl_modification_flag[0] = get_bits1(gb);
768  if (sh->rpl_modification_flag[0]) {
769  for (i = 0; i < sh->nb_refs[L0]; i++)
770  sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
771  }
772 
773  if (sh->slice_type == HEVC_SLICE_B) {
774  sh->rpl_modification_flag[1] = get_bits1(gb);
775  if (sh->rpl_modification_flag[1] == 1)
776  for (i = 0; i < sh->nb_refs[L1]; i++)
777  sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
778  }
779  }
780 
781  if (sh->slice_type == HEVC_SLICE_B)
782  sh->mvd_l1_zero_flag = get_bits1(gb);
783 
784  if (pps->cabac_init_present_flag)
785  sh->cabac_init_flag = get_bits1(gb);
786  else
787  sh->cabac_init_flag = 0;
788 
789  sh->collocated_ref_idx = 0;
791  sh->collocated_list = L0;
792  if (sh->slice_type == HEVC_SLICE_B)
793  sh->collocated_list = !get_bits1(gb);
794 
795  if (sh->nb_refs[sh->collocated_list] > 1) {
797  if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
798  av_log(s->avctx, AV_LOG_ERROR,
799  "Invalid collocated_ref_idx: %d.\n",
800  sh->collocated_ref_idx);
801  return AVERROR_INVALIDDATA;
802  }
803  }
804  }
805 
806  if ((pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
807  (pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
808  int ret = pred_weight_table(sh, s->avctx, sps, gb);
809  if (ret < 0)
810  return ret;
811  }
812 
814  if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
815  av_log(s->avctx, AV_LOG_ERROR,
816  "Invalid number of merging MVP candidates: %d.\n",
817  sh->max_num_merge_cand);
818  return AVERROR_INVALIDDATA;
819  }
820 
821  // Syntax in 7.3.6.1
822  if (sps->motion_vector_resolution_control_idc == 2)
823  sh->use_integer_mv_flag = get_bits1(gb);
824  else
825  // Inferred to be equal to motion_vector_resolution_control_idc if not present
826  sh->use_integer_mv_flag = sps->motion_vector_resolution_control_idc;
827 
828  }
829 
830  sh->slice_qp_delta = get_se_golomb(gb);
831 
832  if (pps->pic_slice_level_chroma_qp_offsets_present_flag) {
835  if (sh->slice_cb_qp_offset < -12 || sh->slice_cb_qp_offset > 12 ||
836  sh->slice_cr_qp_offset < -12 || sh->slice_cr_qp_offset > 12) {
837  av_log(s->avctx, AV_LOG_ERROR, "Invalid slice cx qp offset.\n");
838  return AVERROR_INVALIDDATA;
839  }
840  } else {
841  sh->slice_cb_qp_offset = 0;
842  sh->slice_cr_qp_offset = 0;
843  }
844 
845  if (pps->pps_slice_act_qp_offsets_present_flag) {
849  }
850 
851  if (pps->chroma_qp_offset_list_enabled_flag)
853  else
855 
856  if (pps->deblocking_filter_control_present_flag) {
857  int deblocking_filter_override_flag = 0;
858 
859  if (pps->deblocking_filter_override_enabled_flag)
860  deblocking_filter_override_flag = get_bits1(gb);
861 
862  if (deblocking_filter_override_flag) {
865  int beta_offset_div2 = get_se_golomb(gb);
866  int tc_offset_div2 = get_se_golomb(gb) ;
867  if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
868  tc_offset_div2 < -6 || tc_offset_div2 > 6) {
869  av_log(s->avctx, AV_LOG_ERROR,
870  "Invalid deblock filter offsets: %d, %d\n",
871  beta_offset_div2, tc_offset_div2);
872  return AVERROR_INVALIDDATA;
873  }
874  sh->beta_offset = beta_offset_div2 * 2;
875  sh->tc_offset = tc_offset_div2 * 2;
876  }
877  } else {
878  sh->disable_deblocking_filter_flag = pps->disable_dbf;
879  sh->beta_offset = pps->beta_offset;
880  sh->tc_offset = pps->tc_offset;
881  }
882  } else {
884  sh->beta_offset = 0;
885  sh->tc_offset = 0;
886  }
887 
888  if (pps->seq_loop_filter_across_slices_enabled_flag &&
893  } else {
894  sh->slice_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag;
895  }
896  } else if (!s->slice_initialized) {
897  av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
898  return AVERROR_INVALIDDATA;
899  }
900 
901  sh->num_entry_point_offsets = 0;
902  if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
903  unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
904  // It would be possible to bound this tighter but this here is simpler
905  if (num_entry_point_offsets > get_bits_left(gb)) {
906  av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
907  return AVERROR_INVALIDDATA;
908  }
909 
910  sh->num_entry_point_offsets = num_entry_point_offsets;
911  if (sh->num_entry_point_offsets > 0) {
912  int offset_len = get_ue_golomb_long(gb) + 1;
913 
914  if (offset_len < 1 || offset_len > 32) {
915  sh->num_entry_point_offsets = 0;
916  av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
917  return AVERROR_INVALIDDATA;
918  }
919 
921  av_freep(&sh->offset);
922  av_freep(&sh->size);
923  sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
924  sh->offset = av_malloc_array(sh->num_entry_point_offsets + 1, sizeof(int));
925  sh->size = av_malloc_array(sh->num_entry_point_offsets + 1, sizeof(int));
926  if (!sh->entry_point_offset || !sh->offset || !sh->size) {
927  sh->num_entry_point_offsets = 0;
928  av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
929  return AVERROR(ENOMEM);
930  }
931  for (i = 0; i < sh->num_entry_point_offsets; i++) {
932  unsigned val = get_bits_long(gb, offset_len);
933  sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
934  }
935  }
936  }
937 
938  if (pps->slice_header_extension_present_flag) {
939  unsigned int length = get_ue_golomb_long(gb);
940  if (length*8LL > get_bits_left(gb)) {
941  av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
942  return AVERROR_INVALIDDATA;
943  }
944  for (i = 0; i < length; i++)
945  skip_bits(gb, 8); // slice_header_extension_data_byte
946  }
947 
948  ret = get_bits1(gb);
949  if (!ret) {
950  av_log(s->avctx, AV_LOG_ERROR, "alignment_bit_equal_to_one=0\n");
951  return AVERROR_INVALIDDATA;
952  }
953  sh->data_offset = align_get_bits(gb) - gb->buffer;
954 
955  // Inferred parameters
956  sh->slice_qp = 26U + pps->pic_init_qp_minus26 + sh->slice_qp_delta;
957  if (sh->slice_qp > 51 ||
958  sh->slice_qp < -sps->qp_bd_offset) {
959  av_log(s->avctx, AV_LOG_ERROR,
960  "The slice_qp %d is outside the valid range "
961  "[%d, 51].\n",
962  sh->slice_qp,
963  -sps->qp_bd_offset);
964  return AVERROR_INVALIDDATA;
965  }
966 
968 
970  (!sh->slice_ctb_addr_rs || !pps->ctb_addr_rs_to_ts[sh->slice_ctb_addr_rs])) {
971  av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
972  return AVERROR_INVALIDDATA;
973  }
974 
975  if (get_bits_left(gb) < 0) {
976  av_log(s->avctx, AV_LOG_ERROR,
977  "Overread slice header by %d bits\n", -get_bits_left(gb));
978  return AVERROR_INVALIDDATA;
979  }
980 
981  return 0;
982 }
983 
984 #define CTB(tab, x, y) ((tab)[(y) * sps->ctb_width + (x)])
985 
986 #define SET_SAO(elem, value) \
987 do { \
988  if (!sao_merge_up_flag && !sao_merge_left_flag) \
989  sao->elem = value; \
990  else if (sao_merge_left_flag) \
991  sao->elem = CTB(s->sao, rx-1, ry).elem; \
992  else if (sao_merge_up_flag) \
993  sao->elem = CTB(s->sao, rx, ry-1).elem; \
994  else \
995  sao->elem = 0; \
996 } while (0)
997 
999  const HEVCPPS *pps, const HEVCSPS *sps,
1000  int rx, int ry)
1001 {
1002  const HEVCContext *const s = lc->parent;
1003  int sao_merge_left_flag = 0;
1004  int sao_merge_up_flag = 0;
1005  SAOParams *sao = &CTB(s->sao, rx, ry);
1006  int c_idx, i;
1007 
1008  if (s->sh.slice_sample_adaptive_offset_flag[0] ||
1009  s->sh.slice_sample_adaptive_offset_flag[1]) {
1010  if (rx > 0) {
1011  if (lc->ctb_left_flag)
1012  sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(lc);
1013  }
1014  if (ry > 0 && !sao_merge_left_flag) {
1015  if (lc->ctb_up_flag)
1016  sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(lc);
1017  }
1018  }
1019 
1020  for (c_idx = 0; c_idx < (sps->chroma_format_idc ? 3 : 1); c_idx++) {
1021  int log2_sao_offset_scale = c_idx == 0 ? pps->log2_sao_offset_scale_luma :
1022  pps->log2_sao_offset_scale_chroma;
1023 
1024  if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
1025  sao->type_idx[c_idx] = SAO_NOT_APPLIED;
1026  continue;
1027  }
1028 
1029  if (c_idx == 2) {
1030  sao->type_idx[2] = sao->type_idx[1];
1031  sao->eo_class[2] = sao->eo_class[1];
1032  } else {
1033  SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(lc));
1034  }
1035 
1036  if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
1037  continue;
1038 
1039  for (i = 0; i < 4; i++)
1040  SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(lc, sps->bit_depth));
1041 
1042  if (sao->type_idx[c_idx] == SAO_BAND) {
1043  for (i = 0; i < 4; i++) {
1044  if (sao->offset_abs[c_idx][i]) {
1045  SET_SAO(offset_sign[c_idx][i],
1047  } else {
1048  sao->offset_sign[c_idx][i] = 0;
1049  }
1050  }
1051  SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(lc));
1052  } else if (c_idx != 2) {
1053  SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(lc));
1054  }
1055 
1056  // Inferred parameters
1057  sao->offset_val[c_idx][0] = 0;
1058  for (i = 0; i < 4; i++) {
1059  sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
1060  if (sao->type_idx[c_idx] == SAO_EDGE) {
1061  if (i > 1)
1062  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1063  } else if (sao->offset_sign[c_idx][i]) {
1064  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1065  }
1066  sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
1067  }
1068  }
1069 }
1070 
1071 #undef SET_SAO
1072 #undef CTB
1073 
1075 {
1076  int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(lc, idx);
1077 
1078  if (log2_res_scale_abs_plus1 != 0) {
1079  int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(lc, idx);
1080  lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
1081  (1 - 2 * res_scale_sign_flag);
1082  } else {
1083  lc->tu.res_scale_val = 0;
1084  }
1085 
1086 
1087  return 0;
1088 }
1089 
1091  const HEVCPPS *pps, const HEVCSPS *sps,
1092  int x0, int y0,
1093  int xBase, int yBase, int cb_xBase, int cb_yBase,
1094  int log2_cb_size, int log2_trafo_size,
1095  int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
1096 {
1097  const HEVCContext *const s = lc->parent;
1098  const int log2_trafo_size_c = log2_trafo_size - sps->hshift[1];
1099  int i;
1100 
1101  if (lc->cu.pred_mode == MODE_INTRA) {
1102  int trafo_size = 1 << log2_trafo_size;
1103  ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size, trafo_size, sps->log2_ctb_size);
1104 
1105  s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, x0, y0, 0);
1106  }
1107 
1108  if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
1109  (sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1110  int scan_idx = SCAN_DIAG;
1111  int scan_idx_c = SCAN_DIAG;
1112  int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
1113  (sps->chroma_format_idc == 2 &&
1114  (cbf_cb[1] || cbf_cr[1]));
1115 
1116  if (pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1118  if (lc->tu.cu_qp_delta != 0)
1119  if (ff_hevc_cu_qp_delta_sign_flag(lc) == 1)
1120  lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1121  lc->tu.is_cu_qp_delta_coded = 1;
1122 
1123  if (lc->tu.cu_qp_delta < -(26 + sps->qp_bd_offset / 2) ||
1124  lc->tu.cu_qp_delta > (25 + sps->qp_bd_offset / 2)) {
1125  av_log(s->avctx, AV_LOG_ERROR,
1126  "The cu_qp_delta %d is outside the valid range "
1127  "[%d, %d].\n",
1128  lc->tu.cu_qp_delta,
1129  -(26 + sps->qp_bd_offset / 2),
1130  (25 + sps->qp_bd_offset / 2));
1131  return AVERROR_INVALIDDATA;
1132  }
1133 
1134  ff_hevc_set_qPy(lc, pps, cb_xBase, cb_yBase, log2_cb_size);
1135  }
1136 
1137  if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1139  int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(lc);
1140  if (cu_chroma_qp_offset_flag) {
1141  int cu_chroma_qp_offset_idx = 0;
1142  if (pps->chroma_qp_offset_list_len_minus1 > 0) {
1143  cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(lc, pps->chroma_qp_offset_list_len_minus1);
1144  av_log(s->avctx, AV_LOG_ERROR,
1145  "cu_chroma_qp_offset_idx not yet tested.\n");
1146  }
1147  lc->tu.cu_qp_offset_cb = pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1148  lc->tu.cu_qp_offset_cr = pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1149  } else {
1150  lc->tu.cu_qp_offset_cb = 0;
1151  lc->tu.cu_qp_offset_cr = 0;
1152  }
1154  }
1155 
1156  if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1157  if (lc->tu.intra_pred_mode >= 6 &&
1158  lc->tu.intra_pred_mode <= 14) {
1159  scan_idx = SCAN_VERT;
1160  } else if (lc->tu.intra_pred_mode >= 22 &&
1161  lc->tu.intra_pred_mode <= 30) {
1162  scan_idx = SCAN_HORIZ;
1163  }
1164 
1165  if (lc->tu.intra_pred_mode_c >= 6 &&
1166  lc->tu.intra_pred_mode_c <= 14) {
1167  scan_idx_c = SCAN_VERT;
1168  } else if (lc->tu.intra_pred_mode_c >= 22 &&
1169  lc->tu.intra_pred_mode_c <= 30) {
1170  scan_idx_c = SCAN_HORIZ;
1171  }
1172  }
1173 
1174  lc->tu.cross_pf = 0;
1175 
1176  if (cbf_luma)
1177  ff_hevc_hls_residual_coding(lc, pps, x0, y0, log2_trafo_size, scan_idx, 0);
1178  if (sps->chroma_format_idc && (log2_trafo_size > 2 || sps->chroma_format_idc == 3)) {
1179  int trafo_size_h = 1 << (log2_trafo_size_c + sps->hshift[1]);
1180  int trafo_size_v = 1 << (log2_trafo_size_c + sps->vshift[1]);
1181  lc->tu.cross_pf = (pps->cross_component_prediction_enabled_flag && cbf_luma &&
1182  (lc->cu.pred_mode == MODE_INTER ||
1183  (lc->tu.chroma_mode_c == 4)));
1184 
1185  if (lc->tu.cross_pf) {
1186  hls_cross_component_pred(lc, 0);
1187  }
1188  for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1189  if (lc->cu.pred_mode == MODE_INTRA) {
1190  ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c),
1191  trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1192  s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (i << log2_trafo_size_c), 1);
1193  }
1194  if (cbf_cb[i])
1195  ff_hevc_hls_residual_coding(lc, pps, x0, y0 + (i << log2_trafo_size_c),
1196  log2_trafo_size_c, scan_idx_c, 1);
1197  else
1198  if (lc->tu.cross_pf) {
1199  ptrdiff_t stride = s->cur_frame->f->linesize[1];
1200  int hshift = sps->hshift[1];
1201  int vshift = sps->vshift[1];
1202  const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1203  int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1204  int size = 1 << log2_trafo_size_c;
1205 
1206  uint8_t *dst = &s->cur_frame->f->data[1][(y0 >> vshift) * stride +
1207  ((x0 >> hshift) << sps->pixel_shift)];
1208  for (i = 0; i < (size * size); i++) {
1209  coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1210  }
1211  s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1212  }
1213  }
1214 
1215  if (lc->tu.cross_pf) {
1216  hls_cross_component_pred(lc, 1);
1217  }
1218  for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1219  if (lc->cu.pred_mode == MODE_INTRA) {
1220  ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c),
1221  trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1222  s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (i << log2_trafo_size_c), 2);
1223  }
1224  if (cbf_cr[i])
1225  ff_hevc_hls_residual_coding(lc, pps, x0, y0 + (i << log2_trafo_size_c),
1226  log2_trafo_size_c, scan_idx_c, 2);
1227  else
1228  if (lc->tu.cross_pf) {
1229  ptrdiff_t stride = s->cur_frame->f->linesize[2];
1230  int hshift = sps->hshift[2];
1231  int vshift = sps->vshift[2];
1232  const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1233  int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1234  int size = 1 << log2_trafo_size_c;
1235 
1236  uint8_t *dst = &s->cur_frame->f->data[2][(y0 >> vshift) * stride +
1237  ((x0 >> hshift) << sps->pixel_shift)];
1238  for (i = 0; i < (size * size); i++) {
1239  coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1240  }
1241  s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1242  }
1243  }
1244  } else if (sps->chroma_format_idc && blk_idx == 3) {
1245  int trafo_size_h = 1 << (log2_trafo_size + 1);
1246  int trafo_size_v = 1 << (log2_trafo_size + sps->vshift[1]);
1247  for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1248  if (lc->cu.pred_mode == MODE_INTRA) {
1249  ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
1250  trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1251  s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (i << log2_trafo_size), 1);
1252  }
1253  if (cbf_cb[i])
1254  ff_hevc_hls_residual_coding(lc, pps, xBase, yBase + (i << log2_trafo_size),
1255  log2_trafo_size, scan_idx_c, 1);
1256  }
1257  for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1258  if (lc->cu.pred_mode == MODE_INTRA) {
1259  ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
1260  trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1261  s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (i << log2_trafo_size), 2);
1262  }
1263  if (cbf_cr[i])
1264  ff_hevc_hls_residual_coding(lc, pps, xBase, yBase + (i << log2_trafo_size),
1265  log2_trafo_size, scan_idx_c, 2);
1266  }
1267  }
1268  } else if (sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1269  if (log2_trafo_size > 2 || sps->chroma_format_idc == 3) {
1270  int trafo_size_h = 1 << (log2_trafo_size_c + sps->hshift[1]);
1271  int trafo_size_v = 1 << (log2_trafo_size_c + sps->vshift[1]);
1272  ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size_h, trafo_size_v,
1273  sps->log2_ctb_size);
1274  s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0, 1);
1275  s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0, 2);
1276  if (sps->chroma_format_idc == 2) {
1277  ff_hevc_set_neighbour_available(lc, x0, y0 + (1 << log2_trafo_size_c),
1278  trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1279  s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (1 << log2_trafo_size_c), 1);
1280  s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (1 << log2_trafo_size_c), 2);
1281  }
1282  } else if (blk_idx == 3) {
1283  int trafo_size_h = 1 << (log2_trafo_size + 1);
1284  int trafo_size_v = 1 << (log2_trafo_size + sps->vshift[1]);
1285  ff_hevc_set_neighbour_available(lc, xBase, yBase,
1286  trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1287  s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase, 1);
1288  s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase, 2);
1289  if (sps->chroma_format_idc == 2) {
1290  ff_hevc_set_neighbour_available(lc, xBase, yBase + (1 << log2_trafo_size),
1291  trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1292  s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (1 << log2_trafo_size), 1);
1293  s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (1 << log2_trafo_size), 2);
1294  }
1295  }
1296  }
1297 
1298  return 0;
1299 }
1300 
1301 static void set_deblocking_bypass(const HEVCContext *s, const HEVCSPS *sps,
1302  int x0, int y0, int log2_cb_size)
1303 {
1304  int cb_size = 1 << log2_cb_size;
1305  int log2_min_pu_size = sps->log2_min_pu_size;
1306 
1307  int min_pu_width = sps->min_pu_width;
1308  int x_end = FFMIN(x0 + cb_size, sps->width);
1309  int y_end = FFMIN(y0 + cb_size, sps->height);
1310  int i, j;
1311 
1312  for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1313  for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1314  s->is_pcm[i + j * min_pu_width] = 2;
1315 }
1316 
1318  const HEVCPPS *pps, const HEVCSPS *sps,
1319  int x0, int y0,
1320  int xBase, int yBase, int cb_xBase, int cb_yBase,
1321  int log2_cb_size, int log2_trafo_size,
1322  int trafo_depth, int blk_idx,
1323  const int *base_cbf_cb, const int *base_cbf_cr)
1324 {
1325  const HEVCContext *const s = lc->parent;
1326  uint8_t split_transform_flag;
1327  int cbf_cb[2];
1328  int cbf_cr[2];
1329  int ret;
1330 
1331  cbf_cb[0] = base_cbf_cb[0];
1332  cbf_cb[1] = base_cbf_cb[1];
1333  cbf_cr[0] = base_cbf_cr[0];
1334  cbf_cr[1] = base_cbf_cr[1];
1335 
1336  if (lc->cu.intra_split_flag) {
1337  if (trafo_depth == 1) {
1338  lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1339  if (sps->chroma_format_idc == 3) {
1340  lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1341  lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1342  } else {
1344  lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1345  }
1346  }
1347  } else {
1348  lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1350  lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1351  }
1352 
1353  if (log2_trafo_size <= sps->log2_max_trafo_size &&
1354  log2_trafo_size > sps->log2_min_tb_size &&
1355  trafo_depth < lc->cu.max_trafo_depth &&
1356  !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1357  split_transform_flag = ff_hevc_split_transform_flag_decode(lc, log2_trafo_size);
1358  } else {
1359  int inter_split = sps->max_transform_hierarchy_depth_inter == 0 &&
1360  lc->cu.pred_mode == MODE_INTER &&
1361  lc->cu.part_mode != PART_2Nx2N &&
1362  trafo_depth == 0;
1363 
1364  split_transform_flag = log2_trafo_size > sps->log2_max_trafo_size ||
1365  (lc->cu.intra_split_flag && trafo_depth == 0) ||
1366  inter_split;
1367  }
1368 
1369  if (sps->chroma_format_idc && (log2_trafo_size > 2 || sps->chroma_format_idc == 3)) {
1370  if (trafo_depth == 0 || cbf_cb[0]) {
1371  cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1372  if (sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1373  cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1374  }
1375  }
1376 
1377  if (trafo_depth == 0 || cbf_cr[0]) {
1378  cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1379  if (sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1380  cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1381  }
1382  }
1383  }
1384 
1385  if (split_transform_flag) {
1386  const int trafo_size_split = 1 << (log2_trafo_size - 1);
1387  const int x1 = x0 + trafo_size_split;
1388  const int y1 = y0 + trafo_size_split;
1389 
1390 #define SUBDIVIDE(x, y, idx) \
1391 do { \
1392  ret = hls_transform_tree(lc, pps, sps, \
1393  x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1394  log2_trafo_size - 1, trafo_depth + 1, idx, \
1395  cbf_cb, cbf_cr); \
1396  if (ret < 0) \
1397  return ret; \
1398 } while (0)
1399 
1400  SUBDIVIDE(x0, y0, 0);
1401  SUBDIVIDE(x1, y0, 1);
1402  SUBDIVIDE(x0, y1, 2);
1403  SUBDIVIDE(x1, y1, 3);
1404 
1405 #undef SUBDIVIDE
1406  } else {
1407  int min_tu_size = 1 << sps->log2_min_tb_size;
1408  int log2_min_tu_size = sps->log2_min_tb_size;
1409  int min_tu_width = sps->min_tb_width;
1410  int cbf_luma = 1;
1411 
1412  if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1413  cbf_cb[0] || cbf_cr[0] ||
1414  (sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1415  cbf_luma = ff_hevc_cbf_luma_decode(lc, trafo_depth);
1416  }
1417 
1418  ret = hls_transform_unit(lc, pps, sps,
1419  x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1420  log2_cb_size, log2_trafo_size,
1421  blk_idx, cbf_luma, cbf_cb, cbf_cr);
1422  if (ret < 0)
1423  return ret;
1424  // TODO: store cbf_luma somewhere else
1425  if (cbf_luma) {
1426  int i, j;
1427  for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1428  for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1429  int x_tu = (x0 + j) >> log2_min_tu_size;
1430  int y_tu = (y0 + i) >> log2_min_tu_size;
1431  s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1432  }
1433  }
1434  if (!s->sh.disable_deblocking_filter_flag) {
1435  ff_hevc_deblocking_boundary_strengths(lc, pps, x0, y0, log2_trafo_size);
1436  if (pps->transquant_bypass_enable_flag &&
1438  set_deblocking_bypass(s, sps, x0, y0, log2_trafo_size);
1439  }
1440  }
1441  return 0;
1442 }
1443 
1445  int x0, int y0, int log2_cb_size)
1446 {
1447  const HEVCContext *const s = lc->parent;
1448  const HEVCSPS *const sps = pps->sps;
1449  GetBitContext gb;
1450  int cb_size = 1 << log2_cb_size;
1451  ptrdiff_t stride0 = s->cur_frame->f->linesize[0];
1452  ptrdiff_t stride1 = s->cur_frame->f->linesize[1];
1453  ptrdiff_t stride2 = s->cur_frame->f->linesize[2];
1454  uint8_t *dst0 = &s->cur_frame->f->data[0][y0 * stride0 + (x0 << sps->pixel_shift)];
1455  uint8_t *dst1 = &s->cur_frame->f->data[1][(y0 >> sps->vshift[1]) * stride1 + ((x0 >> sps->hshift[1]) << sps->pixel_shift)];
1456  uint8_t *dst2 = &s->cur_frame->f->data[2][(y0 >> sps->vshift[2]) * stride2 + ((x0 >> sps->hshift[2]) << sps->pixel_shift)];
1457 
1458  int length = cb_size * cb_size * sps->pcm.bit_depth +
1459  (((cb_size >> sps->hshift[1]) * (cb_size >> sps->vshift[1])) +
1460  ((cb_size >> sps->hshift[2]) * (cb_size >> sps->vshift[2]))) *
1461  sps->pcm.bit_depth_chroma;
1462  const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1463  int ret;
1464 
1465  if (!s->sh.disable_deblocking_filter_flag)
1466  ff_hevc_deblocking_boundary_strengths(lc, pps, x0, y0, log2_cb_size);
1467 
1468  ret = init_get_bits(&gb, pcm, length);
1469  if (ret < 0)
1470  return ret;
1471 
1472  s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, sps->pcm.bit_depth);
1473  if (sps->chroma_format_idc) {
1474  s->hevcdsp.put_pcm(dst1, stride1,
1475  cb_size >> sps->hshift[1],
1476  cb_size >> sps->vshift[1],
1477  &gb, sps->pcm.bit_depth_chroma);
1478  s->hevcdsp.put_pcm(dst2, stride2,
1479  cb_size >> sps->hshift[2],
1480  cb_size >> sps->vshift[2],
1481  &gb, sps->pcm.bit_depth_chroma);
1482  }
1483 
1484  return 0;
1485 }
1486 
1487 /**
1488  * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1489  *
1490  * @param s HEVC decoding context
1491  * @param dst target buffer for block data at block position
1492  * @param dststride stride of the dst buffer
1493  * @param ref reference picture buffer at origin (0, 0)
1494  * @param mv motion vector (relative to block position) to get pixel data from
1495  * @param x_off horizontal position of block from origin (0, 0)
1496  * @param y_off vertical position of block from origin (0, 0)
1497  * @param block_w width of block
1498  * @param block_h height of block
1499  * @param luma_weight weighting factor applied to the luma prediction
1500  * @param luma_offset additive offset applied to the luma prediction value
1501  */
1502 
1504  const HEVCPPS *pps, const HEVCSPS *sps,
1505  uint8_t *dst, ptrdiff_t dststride,
1506  const AVFrame *ref, const Mv *mv, int x_off, int y_off,
1507  int block_w, int block_h, int luma_weight, int luma_offset)
1508 {
1509  const HEVCContext *const s = lc->parent;
1510  const uint8_t *src = ref->data[0];
1511  ptrdiff_t srcstride = ref->linesize[0];
1512  int pic_width = sps->width;
1513  int pic_height = sps->height;
1514  int mx = mv->x & 3;
1515  int my = mv->y & 3;
1516  int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1517  (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1518  int idx = hevc_pel_weight[block_w];
1519 
1520  x_off += mv->x >> 2;
1521  y_off += mv->y >> 2;
1522  src += y_off * srcstride + (x_off * (1 << sps->pixel_shift));
1523 
1524  if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1525  x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1526  y_off >= pic_height - block_h - QPEL_EXTRA_AFTER ||
1527  ref == s->cur_frame->f) {
1528  const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1529  int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1530  int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1531 
1532  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1533  edge_emu_stride, srcstride,
1534  block_w + QPEL_EXTRA,
1535  block_h + QPEL_EXTRA,
1536  x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1537  pic_width, pic_height);
1538  src = lc->edge_emu_buffer + buf_offset;
1539  srcstride = edge_emu_stride;
1540  }
1541 
1542  if (!weight_flag)
1543  s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1544  block_h, mx, my, block_w);
1545  else
1546  s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1547  block_h, s->sh.luma_log2_weight_denom,
1548  luma_weight, luma_offset, mx, my, block_w);
1549 }
1550 
1551 /**
1552  * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1553  *
1554  * @param s HEVC decoding context
1555  * @param dst target buffer for block data at block position
1556  * @param dststride stride of the dst buffer
1557  * @param ref0 reference picture0 buffer at origin (0, 0)
1558  * @param mv0 motion vector0 (relative to block position) to get pixel data from
1559  * @param x_off horizontal position of block from origin (0, 0)
1560  * @param y_off vertical position of block from origin (0, 0)
1561  * @param block_w width of block
1562  * @param block_h height of block
1563  * @param ref1 reference picture1 buffer at origin (0, 0)
1564  * @param mv1 motion vector1 (relative to block position) to get pixel data from
1565  * @param current_mv current motion vector structure
1566  */
1568  const HEVCPPS *pps, const HEVCSPS *sps,
1569  uint8_t *dst, ptrdiff_t dststride,
1570  const AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1571  int block_w, int block_h, const AVFrame *ref1,
1572  const Mv *mv1, struct MvField *current_mv)
1573 {
1574  const HEVCContext *const s = lc->parent;
1575  ptrdiff_t src0stride = ref0->linesize[0];
1576  ptrdiff_t src1stride = ref1->linesize[0];
1577  int pic_width = sps->width;
1578  int pic_height = sps->height;
1579  int mx0 = mv0->x & 3;
1580  int my0 = mv0->y & 3;
1581  int mx1 = mv1->x & 3;
1582  int my1 = mv1->y & 3;
1583  int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1584  (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1585  int x_off0 = x_off + (mv0->x >> 2);
1586  int y_off0 = y_off + (mv0->y >> 2);
1587  int x_off1 = x_off + (mv1->x >> 2);
1588  int y_off1 = y_off + (mv1->y >> 2);
1589  int idx = hevc_pel_weight[block_w];
1590 
1591  const uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << sps->pixel_shift);
1592  const uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << sps->pixel_shift);
1593 
1594  if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1595  x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1596  y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1597  const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1598  int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1599  int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1600 
1601  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1602  edge_emu_stride, src0stride,
1603  block_w + QPEL_EXTRA,
1604  block_h + QPEL_EXTRA,
1605  x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1606  pic_width, pic_height);
1607  src0 = lc->edge_emu_buffer + buf_offset;
1608  src0stride = edge_emu_stride;
1609  }
1610 
1611  if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1612  x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1613  y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1614  const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1615  int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1616  int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1617 
1618  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1619  edge_emu_stride, src1stride,
1620  block_w + QPEL_EXTRA,
1621  block_h + QPEL_EXTRA,
1622  x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1623  pic_width, pic_height);
1624  src1 = lc->edge_emu_buffer2 + buf_offset;
1625  src1stride = edge_emu_stride;
1626  }
1627 
1628  s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1629  block_h, mx0, my0, block_w);
1630  if (!weight_flag)
1631  s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1632  block_h, mx1, my1, block_w);
1633  else
1634  s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1635  block_h, s->sh.luma_log2_weight_denom,
1636  s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1637  s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1638  s->sh.luma_offset_l0[current_mv->ref_idx[0]],
1639  s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1640  mx1, my1, block_w);
1641 
1642 }
1643 
1644 /**
1645  * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1646  *
1647  * @param s HEVC decoding context
1648  * @param dst1 target buffer for block data at block position (U plane)
1649  * @param dst2 target buffer for block data at block position (V plane)
1650  * @param dststride stride of the dst1 and dst2 buffers
1651  * @param ref reference picture buffer at origin (0, 0)
1652  * @param mv motion vector (relative to block position) to get pixel data from
1653  * @param x_off horizontal position of block from origin (0, 0)
1654  * @param y_off vertical position of block from origin (0, 0)
1655  * @param block_w width of block
1656  * @param block_h height of block
1657  * @param chroma_weight weighting factor applied to the chroma prediction
1658  * @param chroma_offset additive offset applied to the chroma prediction value
1659  */
1660 
1662  const HEVCPPS *pps, const HEVCSPS *sps,
1663  uint8_t *dst0,
1664  ptrdiff_t dststride, const uint8_t *src0, ptrdiff_t srcstride, int reflist,
1665  int x_off, int y_off, int block_w, int block_h,
1666  const struct MvField *current_mv, int chroma_weight, int chroma_offset)
1667 {
1668  const HEVCContext *const s = lc->parent;
1669  int pic_width = sps->width >> sps->hshift[1];
1670  int pic_height = sps->height >> sps->vshift[1];
1671  const Mv *mv = &current_mv->mv[reflist];
1672  int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1673  (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1674  int idx = hevc_pel_weight[block_w];
1675  int hshift = sps->hshift[1];
1676  int vshift = sps->vshift[1];
1677  intptr_t mx = av_zero_extend(mv->x, 2 + hshift);
1678  intptr_t my = av_zero_extend(mv->y, 2 + vshift);
1679  intptr_t _mx = mx << (1 - hshift);
1680  intptr_t _my = my << (1 - vshift);
1681  int emu = src0 == s->cur_frame->f->data[1] || src0 == s->cur_frame->f->data[2];
1682 
1683  x_off += mv->x >> (2 + hshift);
1684  y_off += mv->y >> (2 + vshift);
1685  src0 += y_off * srcstride + (x_off * (1 << sps->pixel_shift));
1686 
1687  if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1688  x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1689  y_off >= pic_height - block_h - EPEL_EXTRA_AFTER ||
1690  emu) {
1691  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1692  int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << sps->pixel_shift));
1693  int buf_offset0 = EPEL_EXTRA_BEFORE *
1694  (edge_emu_stride + (1 << sps->pixel_shift));
1695  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1696  edge_emu_stride, srcstride,
1697  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1698  x_off - EPEL_EXTRA_BEFORE,
1699  y_off - EPEL_EXTRA_BEFORE,
1700  pic_width, pic_height);
1701 
1702  src0 = lc->edge_emu_buffer + buf_offset0;
1703  srcstride = edge_emu_stride;
1704  }
1705  if (!weight_flag)
1706  s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1707  block_h, _mx, _my, block_w);
1708  else
1709  s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1710  block_h, s->sh.chroma_log2_weight_denom,
1711  chroma_weight, chroma_offset, _mx, _my, block_w);
1712 }
1713 
1714 /**
1715  * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1716  *
1717  * @param s HEVC decoding context
1718  * @param dst target buffer for block data at block position
1719  * @param dststride stride of the dst buffer
1720  * @param ref0 reference picture0 buffer at origin (0, 0)
1721  * @param mv0 motion vector0 (relative to block position) to get pixel data from
1722  * @param x_off horizontal position of block from origin (0, 0)
1723  * @param y_off vertical position of block from origin (0, 0)
1724  * @param block_w width of block
1725  * @param block_h height of block
1726  * @param ref1 reference picture1 buffer at origin (0, 0)
1727  * @param mv1 motion vector1 (relative to block position) to get pixel data from
1728  * @param current_mv current motion vector structure
1729  * @param cidx chroma component(cb, cr)
1730  */
1732  const HEVCPPS *pps, const HEVCSPS *sps,
1733  uint8_t *dst0, ptrdiff_t dststride,
1734  const AVFrame *ref0, const AVFrame *ref1,
1735  int x_off, int y_off, int block_w, int block_h, const MvField *current_mv, int cidx)
1736 {
1737  const HEVCContext *const s = lc->parent;
1738  const uint8_t *src1 = ref0->data[cidx+1];
1739  const uint8_t *src2 = ref1->data[cidx+1];
1740  ptrdiff_t src1stride = ref0->linesize[cidx+1];
1741  ptrdiff_t src2stride = ref1->linesize[cidx+1];
1742  int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1743  (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1744  int pic_width = sps->width >> sps->hshift[1];
1745  int pic_height = sps->height >> sps->vshift[1];
1746  const Mv *const mv0 = &current_mv->mv[0];
1747  const Mv *const mv1 = &current_mv->mv[1];
1748  int hshift = sps->hshift[1];
1749  int vshift = sps->vshift[1];
1750 
1751  intptr_t mx0 = av_zero_extend(mv0->x, 2 + hshift);
1752  intptr_t my0 = av_zero_extend(mv0->y, 2 + vshift);
1753  intptr_t mx1 = av_zero_extend(mv1->x, 2 + hshift);
1754  intptr_t my1 = av_zero_extend(mv1->y, 2 + vshift);
1755  intptr_t _mx0 = mx0 << (1 - hshift);
1756  intptr_t _my0 = my0 << (1 - vshift);
1757  intptr_t _mx1 = mx1 << (1 - hshift);
1758  intptr_t _my1 = my1 << (1 - vshift);
1759 
1760  int x_off0 = x_off + (mv0->x >> (2 + hshift));
1761  int y_off0 = y_off + (mv0->y >> (2 + vshift));
1762  int x_off1 = x_off + (mv1->x >> (2 + hshift));
1763  int y_off1 = y_off + (mv1->y >> (2 + vshift));
1764  int idx = hevc_pel_weight[block_w];
1765  src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << sps->pixel_shift);
1766  src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << sps->pixel_shift);
1767 
1768  if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
1769  x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1770  y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1771  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1772  int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << sps->pixel_shift));
1773  int buf_offset1 = EPEL_EXTRA_BEFORE *
1774  (edge_emu_stride + (1 << sps->pixel_shift));
1775 
1776  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1777  edge_emu_stride, src1stride,
1778  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1779  x_off0 - EPEL_EXTRA_BEFORE,
1780  y_off0 - EPEL_EXTRA_BEFORE,
1781  pic_width, pic_height);
1782 
1783  src1 = lc->edge_emu_buffer + buf_offset1;
1784  src1stride = edge_emu_stride;
1785  }
1786 
1787  if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
1788  x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1789  y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1790  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1791  int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << sps->pixel_shift));
1792  int buf_offset1 = EPEL_EXTRA_BEFORE *
1793  (edge_emu_stride + (1 << sps->pixel_shift));
1794 
1795  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
1796  edge_emu_stride, src2stride,
1797  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1798  x_off1 - EPEL_EXTRA_BEFORE,
1799  y_off1 - EPEL_EXTRA_BEFORE,
1800  pic_width, pic_height);
1801 
1802  src2 = lc->edge_emu_buffer2 + buf_offset1;
1803  src2stride = edge_emu_stride;
1804  }
1805 
1806  s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
1807  block_h, _mx0, _my0, block_w);
1808  if (!weight_flag)
1809  s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->cur_frame->f->linesize[cidx+1],
1810  src2, src2stride, lc->tmp,
1811  block_h, _mx1, _my1, block_w);
1812  else
1813  s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->cur_frame->f->linesize[cidx+1],
1814  src2, src2stride, lc->tmp,
1815  block_h,
1816  s->sh.chroma_log2_weight_denom,
1817  s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
1818  s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
1819  s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
1820  s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
1821  _mx1, _my1, block_w);
1822 }
1823 
1824 static void hevc_await_progress(const HEVCContext *s, const HEVCFrame *ref,
1825  const Mv *mv, int y0, int height)
1826 {
1827  if (s->avctx->active_thread_type == FF_THREAD_FRAME ) {
1828  int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1829 
1830  ff_progress_frame_await(&ref->tf, y);
1831  }
1832 }
1833 
1835  const HEVCPPS *pps, const HEVCSPS *sps,
1836  int x0, int y0, int nPbW,
1837  int nPbH, int log2_cb_size, int part_idx,
1838  int merge_idx, MvField *mv)
1839 {
1840  const HEVCContext *const s = lc->parent;
1841  enum InterPredIdc inter_pred_idc = PRED_L0;
1842  int mvp_flag;
1843 
1844  ff_hevc_set_neighbour_available(lc, x0, y0, nPbW, nPbH, sps->log2_ctb_size);
1845  mv->pred_flag = 0;
1846  if (s->sh.slice_type == HEVC_SLICE_B)
1847  inter_pred_idc = ff_hevc_inter_pred_idc_decode(lc, nPbW, nPbH);
1848 
1849  if (inter_pred_idc != PRED_L1) {
1850  if (s->sh.nb_refs[L0])
1851  mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L0]);
1852 
1853  mv->pred_flag = PF_L0;
1854  ff_hevc_hls_mvd_coding(lc, x0, y0, 0);
1855  mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
1856  ff_hevc_luma_mv_mvp_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
1857  part_idx, merge_idx, mv, mvp_flag, 0);
1858  mv->mv[0].x += lc->pu.mvd.x;
1859  mv->mv[0].y += lc->pu.mvd.y;
1860  }
1861 
1862  if (inter_pred_idc != PRED_L0) {
1863  if (s->sh.nb_refs[L1])
1864  mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L1]);
1865 
1866  if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1867  AV_ZERO32(&lc->pu.mvd);
1868  } else {
1869  ff_hevc_hls_mvd_coding(lc, x0, y0, 1);
1870  }
1871 
1872  mv->pred_flag += PF_L1;
1873  mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
1874  ff_hevc_luma_mv_mvp_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
1875  part_idx, merge_idx, mv, mvp_flag, 1);
1876  mv->mv[1].x += lc->pu.mvd.x;
1877  mv->mv[1].y += lc->pu.mvd.y;
1878  }
1879 }
1880 
1882  const HEVCPPS *pps, const HEVCSPS *sps,
1883  int x0, int y0, int nPbW, int nPbH,
1884  int log2_cb_size, int partIdx, int idx)
1885 {
1886 #define POS(c_idx, x, y) \
1887  &s->cur_frame->f->data[c_idx][((y) >> sps->vshift[c_idx]) * linesize[c_idx] + \
1888  (((x) >> sps->hshift[c_idx]) << sps->pixel_shift)]
1889  const HEVCContext *const s = lc->parent;
1890  int merge_idx = 0;
1891  struct MvField current_mv = {{{ 0 }}};
1892 
1893  int min_pu_width = sps->min_pu_width;
1894 
1895  MvField *tab_mvf = s->cur_frame->tab_mvf;
1896  const RefPicList *refPicList = s->cur_frame->refPicList;
1897  const HEVCFrame *ref0 = NULL, *ref1 = NULL;
1898  const int *linesize = s->cur_frame->f->linesize;
1899  uint8_t *dst0 = POS(0, x0, y0);
1900  uint8_t *dst1 = POS(1, x0, y0);
1901  uint8_t *dst2 = POS(2, x0, y0);
1902  int log2_min_cb_size = sps->log2_min_cb_size;
1903  int min_cb_width = sps->min_cb_width;
1904  int x_cb = x0 >> log2_min_cb_size;
1905  int y_cb = y0 >> log2_min_cb_size;
1906  int x_pu, y_pu;
1907  int i, j;
1908 
1909  int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1910 
1911  if (!skip_flag)
1913 
1914  if (skip_flag || lc->pu.merge_flag) {
1915  if (s->sh.max_num_merge_cand > 1)
1916  merge_idx = ff_hevc_merge_idx_decode(lc);
1917  else
1918  merge_idx = 0;
1919 
1920  ff_hevc_luma_mv_merge_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
1921  partIdx, merge_idx, &current_mv);
1922  } else {
1923  hevc_luma_mv_mvp_mode(lc, pps, sps, x0, y0, nPbW, nPbH, log2_cb_size,
1924  partIdx, merge_idx, &current_mv);
1925  }
1926 
1927  x_pu = x0 >> sps->log2_min_pu_size;
1928  y_pu = y0 >> sps->log2_min_pu_size;
1929 
1930  for (j = 0; j < nPbH >> sps->log2_min_pu_size; j++)
1931  for (i = 0; i < nPbW >> sps->log2_min_pu_size; i++)
1932  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1933 
1934  if (current_mv.pred_flag & PF_L0) {
1935  ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1936  if (!ref0 || !ref0->f)
1937  return;
1938  hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
1939  }
1940  if (current_mv.pred_flag & PF_L1) {
1941  ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1942  if (!ref1 || !ref1->f)
1943  return;
1944  hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
1945  }
1946 
1947  if (current_mv.pred_flag == PF_L0) {
1948  int x0_c = x0 >> sps->hshift[1];
1949  int y0_c = y0 >> sps->vshift[1];
1950  int nPbW_c = nPbW >> sps->hshift[1];
1951  int nPbH_c = nPbH >> sps->vshift[1];
1952 
1953  luma_mc_uni(lc, pps, sps, dst0, linesize[0], ref0->f,
1954  &current_mv.mv[0], x0, y0, nPbW, nPbH,
1955  s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1956  s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
1957 
1958  if (sps->chroma_format_idc) {
1959  chroma_mc_uni(lc, pps, sps, dst1, linesize[1], ref0->f->data[1], ref0->f->linesize[1],
1960  0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1961  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
1962  chroma_mc_uni(lc, pps, sps, dst2, linesize[2], ref0->f->data[2], ref0->f->linesize[2],
1963  0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1964  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
1965  }
1966  } else if (current_mv.pred_flag == PF_L1) {
1967  int x0_c = x0 >> sps->hshift[1];
1968  int y0_c = y0 >> sps->vshift[1];
1969  int nPbW_c = nPbW >> sps->hshift[1];
1970  int nPbH_c = nPbH >> sps->vshift[1];
1971 
1972  luma_mc_uni(lc, pps, sps, dst0, linesize[0], ref1->f,
1973  &current_mv.mv[1], x0, y0, nPbW, nPbH,
1974  s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1975  s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
1976 
1977  if (sps->chroma_format_idc) {
1978  chroma_mc_uni(lc, pps, sps, dst1, linesize[1], ref1->f->data[1], ref1->f->linesize[1],
1979  1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1980  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
1981 
1982  chroma_mc_uni(lc, pps, sps, dst2, linesize[2], ref1->f->data[2], ref1->f->linesize[2],
1983  1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1984  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
1985  }
1986  } else if (current_mv.pred_flag == PF_BI) {
1987  int x0_c = x0 >> sps->hshift[1];
1988  int y0_c = y0 >> sps->vshift[1];
1989  int nPbW_c = nPbW >> sps->hshift[1];
1990  int nPbH_c = nPbH >> sps->vshift[1];
1991 
1992  luma_mc_bi(lc, pps, sps, dst0, linesize[0], ref0->f,
1993  &current_mv.mv[0], x0, y0, nPbW, nPbH,
1994  ref1->f, &current_mv.mv[1], &current_mv);
1995 
1996  if (sps->chroma_format_idc) {
1997  chroma_mc_bi(lc, pps, sps, dst1, linesize[1], ref0->f, ref1->f,
1998  x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
1999 
2000  chroma_mc_bi(lc, pps, sps, dst2, linesize[2], ref0->f, ref1->f,
2001  x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
2002  }
2003  }
2004 }
2005 
2006 /**
2007  * 8.4.1
2008  */
2010  int x0, int y0, int pu_size,
2011  int prev_intra_luma_pred_flag)
2012 {
2013  const HEVCContext *const s = lc->parent;
2014  int x_pu = x0 >> sps->log2_min_pu_size;
2015  int y_pu = y0 >> sps->log2_min_pu_size;
2016  int min_pu_width = sps->min_pu_width;
2017  int size_in_pus = pu_size >> sps->log2_min_pu_size;
2018  int x0b = av_zero_extend(x0, sps->log2_ctb_size);
2019  int y0b = av_zero_extend(y0, sps->log2_ctb_size);
2020 
2021  int cand_up = (lc->ctb_up_flag || y0b) ?
2022  s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
2023  int cand_left = (lc->ctb_left_flag || x0b) ?
2024  s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
2025 
2026  int y_ctb = (y0 >> (sps->log2_ctb_size)) << (sps->log2_ctb_size);
2027 
2028  MvField *tab_mvf = s->cur_frame->tab_mvf;
2029  int intra_pred_mode;
2030  int candidate[3];
2031  int i, j;
2032 
2033  // intra_pred_mode prediction does not cross vertical CTB boundaries
2034  if ((y0 - 1) < y_ctb)
2035  cand_up = INTRA_DC;
2036 
2037  if (cand_left == cand_up) {
2038  if (cand_left < 2) {
2039  candidate[0] = INTRA_PLANAR;
2040  candidate[1] = INTRA_DC;
2041  candidate[2] = INTRA_ANGULAR_26;
2042  } else {
2043  candidate[0] = cand_left;
2044  candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
2045  candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
2046  }
2047  } else {
2048  candidate[0] = cand_left;
2049  candidate[1] = cand_up;
2050  if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
2051  candidate[2] = INTRA_PLANAR;
2052  } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
2053  candidate[2] = INTRA_DC;
2054  } else {
2055  candidate[2] = INTRA_ANGULAR_26;
2056  }
2057  }
2058 
2059  if (prev_intra_luma_pred_flag) {
2060  intra_pred_mode = candidate[lc->pu.mpm_idx];
2061  } else {
2062  if (candidate[0] > candidate[1])
2063  FFSWAP(uint8_t, candidate[0], candidate[1]);
2064  if (candidate[0] > candidate[2])
2065  FFSWAP(uint8_t, candidate[0], candidate[2]);
2066  if (candidate[1] > candidate[2])
2067  FFSWAP(uint8_t, candidate[1], candidate[2]);
2068 
2069  intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
2070  for (i = 0; i < 3; i++)
2071  if (intra_pred_mode >= candidate[i])
2072  intra_pred_mode++;
2073  }
2074 
2075  /* write the intra prediction units into the mv array */
2076  if (!size_in_pus)
2077  size_in_pus = 1;
2078  for (i = 0; i < size_in_pus; i++) {
2079  memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
2080  intra_pred_mode, size_in_pus);
2081 
2082  for (j = 0; j < size_in_pus; j++) {
2083  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
2084  }
2085  }
2086 
2087  return intra_pred_mode;
2088 }
2089 
2090 static av_always_inline void set_ct_depth(const HEVCSPS *sps, uint8_t *tab_ct_depth,
2091  int x0, int y0,
2092  int log2_cb_size, int ct_depth)
2093 {
2094  int length = (1 << log2_cb_size) >> sps->log2_min_cb_size;
2095  int x_cb = x0 >> sps->log2_min_cb_size;
2096  int y_cb = y0 >> sps->log2_min_cb_size;
2097  int y;
2098 
2099  for (y = 0; y < length; y++)
2100  memset(&tab_ct_depth[(y_cb + y) * sps->min_cb_width + x_cb],
2101  ct_depth, length);
2102 }
2103 
2104 static const uint8_t tab_mode_idx[] = {
2105  0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
2106  21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
2107 
2109  int x0, int y0,
2110  int log2_cb_size)
2111 {
2112  static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
2113  uint8_t prev_intra_luma_pred_flag[4];
2114  int split = lc->cu.part_mode == PART_NxN;
2115  int pb_size = (1 << log2_cb_size) >> split;
2116  int side = split + 1;
2117  int chroma_mode;
2118  int i, j;
2119 
2120  for (i = 0; i < side; i++)
2121  for (j = 0; j < side; j++)
2122  prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(lc);
2123 
2124  for (i = 0; i < side; i++) {
2125  for (j = 0; j < side; j++) {
2126  if (prev_intra_luma_pred_flag[2 * i + j])
2127  lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(lc);
2128  else
2130 
2131  lc->pu.intra_pred_mode[2 * i + j] =
2132  luma_intra_pred_mode(lc, sps, x0 + pb_size * j, y0 + pb_size * i, pb_size,
2133  prev_intra_luma_pred_flag[2 * i + j]);
2134  }
2135  }
2136 
2137  if (sps->chroma_format_idc == 3) {
2138  for (i = 0; i < side; i++) {
2139  for (j = 0; j < side; j++) {
2140  lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2141  if (chroma_mode != 4) {
2142  if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
2143  lc->pu.intra_pred_mode_c[2 * i + j] = 34;
2144  else
2145  lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2146  } else {
2147  lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2148  }
2149  }
2150  }
2151  } else if (sps->chroma_format_idc == 2) {
2152  int mode_idx;
2153  lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2154  if (chroma_mode != 4) {
2155  if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2156  mode_idx = 34;
2157  else
2158  mode_idx = intra_chroma_table[chroma_mode];
2159  } else {
2160  mode_idx = lc->pu.intra_pred_mode[0];
2161  }
2162  lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2163  } else if (sps->chroma_format_idc != 0) {
2164  chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2165  if (chroma_mode != 4) {
2166  if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2167  lc->pu.intra_pred_mode_c[0] = 34;
2168  else
2169  lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2170  } else {
2171  lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2172  }
2173  }
2174 }
2175 
2177  const HEVCSPS *sps,
2178  int x0, int y0,
2179  int log2_cb_size)
2180 {
2181  const HEVCContext *const s = lc->parent;
2182  int pb_size = 1 << log2_cb_size;
2183  int size_in_pus = pb_size >> sps->log2_min_pu_size;
2184  int min_pu_width = sps->min_pu_width;
2185  MvField *tab_mvf = s->cur_frame->tab_mvf;
2186  int x_pu = x0 >> sps->log2_min_pu_size;
2187  int y_pu = y0 >> sps->log2_min_pu_size;
2188  int j, k;
2189 
2190  if (size_in_pus == 0)
2191  size_in_pus = 1;
2192  for (j = 0; j < size_in_pus; j++)
2193  memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2194  if (lc->cu.pred_mode == MODE_INTRA)
2195  for (j = 0; j < size_in_pus; j++)
2196  for (k = 0; k < size_in_pus; k++)
2197  tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2198 }
2199 
2201  const HEVCPPS *pps, const HEVCSPS *sps,
2202  int x0, int y0, int log2_cb_size)
2203 {
2204  int cb_size = 1 << log2_cb_size;
2205  int log2_min_cb_size = sps->log2_min_cb_size;
2206  int length = cb_size >> log2_min_cb_size;
2207  int min_cb_width = sps->min_cb_width;
2208  int x_cb = x0 >> log2_min_cb_size;
2209  int y_cb = y0 >> log2_min_cb_size;
2210  int idx = log2_cb_size - 2;
2211  int qp_block_mask = (1 << (sps->log2_ctb_size - pps->diff_cu_qp_delta_depth)) - 1;
2212  int x, y, ret;
2213 
2214  lc->cu.x = x0;
2215  lc->cu.y = y0;
2216  lc->cu.pred_mode = MODE_INTRA;
2217  lc->cu.part_mode = PART_2Nx2N;
2218  lc->cu.intra_split_flag = 0;
2219 
2220  SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2221  for (x = 0; x < 4; x++)
2222  lc->pu.intra_pred_mode[x] = 1;
2223  if (pps->transquant_bypass_enable_flag) {
2225  if (lc->cu.cu_transquant_bypass_flag)
2226  set_deblocking_bypass(s, sps, x0, y0, log2_cb_size);
2227  } else
2228  lc->cu.cu_transquant_bypass_flag = 0;
2229 
2230  if (s->sh.slice_type != HEVC_SLICE_I) {
2231  const int x0b = av_zero_extend(x0, sps->log2_ctb_size);
2232  const int y0b = av_zero_extend(y0, sps->log2_ctb_size);
2233  uint8_t skip_flag = ff_hevc_skip_flag_decode(lc, x0b, y0b, x_cb, y_cb,
2234  min_cb_width);
2235 
2236  x = y_cb * min_cb_width + x_cb;
2237  for (y = 0; y < length; y++) {
2238  memset(&s->skip_flag[x], skip_flag, length);
2239  x += min_cb_width;
2240  }
2241  lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2242  } else {
2243  x = y_cb * min_cb_width + x_cb;
2244  for (y = 0; y < length; y++) {
2245  memset(&s->skip_flag[x], 0, length);
2246  x += min_cb_width;
2247  }
2248  }
2249 
2250  if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2252  x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2253  intra_prediction_unit_default_value(lc, sps, x0, y0, log2_cb_size);
2254 
2255  if (!s->sh.disable_deblocking_filter_flag)
2256  ff_hevc_deblocking_boundary_strengths(lc, pps, x0, y0, log2_cb_size);
2257  } else {
2258  int pcm_flag = 0;
2259 
2260  if (s->sh.slice_type != HEVC_SLICE_I)
2262  if (lc->cu.pred_mode != MODE_INTRA ||
2263  log2_cb_size == sps->log2_min_cb_size) {
2264  lc->cu.part_mode = ff_hevc_part_mode_decode(lc, sps, log2_cb_size);
2265  lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2266  lc->cu.pred_mode == MODE_INTRA;
2267  }
2268 
2269  if (lc->cu.pred_mode == MODE_INTRA) {
2270  if (lc->cu.part_mode == PART_2Nx2N && sps->pcm_enabled &&
2271  log2_cb_size >= sps->pcm.log2_min_pcm_cb_size &&
2272  log2_cb_size <= sps->pcm.log2_max_pcm_cb_size) {
2273  pcm_flag = ff_hevc_pcm_flag_decode(lc);
2274  }
2275  if (pcm_flag) {
2276  intra_prediction_unit_default_value(lc, sps, x0, y0, log2_cb_size);
2277  ret = hls_pcm_sample(lc, pps, x0, y0, log2_cb_size);
2278  if (sps->pcm_loop_filter_disabled)
2279  set_deblocking_bypass(s, sps, x0, y0, log2_cb_size);
2280 
2281  if (ret < 0)
2282  return ret;
2283  } else {
2284  intra_prediction_unit(lc, sps, x0, y0, log2_cb_size);
2285  }
2286  } else {
2287  intra_prediction_unit_default_value(lc, sps, x0, y0, log2_cb_size);
2288  switch (lc->cu.part_mode) {
2289  case PART_2Nx2N:
2291  x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2292  break;
2293  case PART_2NxN:
2295  x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2297  x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2298  break;
2299  case PART_Nx2N:
2301  x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2303  x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2304  break;
2305  case PART_2NxnU:
2307  x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2309  x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2310  break;
2311  case PART_2NxnD:
2313  x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2315  x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2316  break;
2317  case PART_nLx2N:
2319  x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2321  x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2322  break;
2323  case PART_nRx2N:
2325  x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2327  x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2328  break;
2329  case PART_NxN:
2331  x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2333  x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2335  x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2337  x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2338  break;
2339  }
2340  }
2341 
2342  if (!pcm_flag) {
2343  int rqt_root_cbf = 1;
2344 
2345  if (lc->cu.pred_mode != MODE_INTRA &&
2346  !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2347  rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(lc);
2348  }
2349  if (rqt_root_cbf) {
2350  const static int cbf[2] = { 0 };
2351  lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2352  sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2353  sps->max_transform_hierarchy_depth_inter;
2354  ret = hls_transform_tree(lc, pps, sps, x0, y0, x0, y0, x0, y0,
2355  log2_cb_size,
2356  log2_cb_size, 0, 0, cbf, cbf);
2357  if (ret < 0)
2358  return ret;
2359  } else {
2360  if (!s->sh.disable_deblocking_filter_flag)
2361  ff_hevc_deblocking_boundary_strengths(lc, pps, x0, y0, log2_cb_size);
2362  }
2363  }
2364  }
2365 
2366  if (pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2367  ff_hevc_set_qPy(lc, pps, x0, y0, log2_cb_size);
2368 
2369  x = y_cb * min_cb_width + x_cb;
2370  for (y = 0; y < length; y++) {
2371  memset(&s->qp_y_tab[x], lc->qp_y, length);
2372  x += min_cb_width;
2373  }
2374 
2375  if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2376  ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2377  lc->qPy_pred = lc->qp_y;
2378  }
2379 
2380  set_ct_depth(sps, s->tab_ct_depth, x0, y0, log2_cb_size, lc->ct_depth);
2381 
2382  return 0;
2383 }
2384 
2386  const HEVCPPS *pps, const HEVCSPS *sps,
2387  int x0, int y0,
2388  int log2_cb_size, int cb_depth)
2389 {
2390  const HEVCContext *const s = lc->parent;
2391  const int cb_size = 1 << log2_cb_size;
2392  int ret;
2393  int split_cu;
2394 
2395  lc->ct_depth = cb_depth;
2396  if (x0 + cb_size <= sps->width &&
2397  y0 + cb_size <= sps->height &&
2398  log2_cb_size > sps->log2_min_cb_size) {
2399  split_cu = ff_hevc_split_coding_unit_flag_decode(lc, sps, cb_depth, x0, y0);
2400  } else {
2401  split_cu = (log2_cb_size > sps->log2_min_cb_size);
2402  }
2403  if (pps->cu_qp_delta_enabled_flag &&
2404  log2_cb_size >= sps->log2_ctb_size - pps->diff_cu_qp_delta_depth) {
2405  lc->tu.is_cu_qp_delta_coded = 0;
2406  lc->tu.cu_qp_delta = 0;
2407  }
2408 
2409  if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2410  log2_cb_size >= sps->log2_ctb_size - pps->diff_cu_chroma_qp_offset_depth) {
2412  }
2413 
2414  if (split_cu) {
2415  int qp_block_mask = (1 << (sps->log2_ctb_size - pps->diff_cu_qp_delta_depth)) - 1;
2416  const int cb_size_split = cb_size >> 1;
2417  const int x1 = x0 + cb_size_split;
2418  const int y1 = y0 + cb_size_split;
2419 
2420  int more_data = 0;
2421 
2422  more_data = hls_coding_quadtree(lc, pps, sps, x0, y0, log2_cb_size - 1, cb_depth + 1);
2423  if (more_data < 0)
2424  return more_data;
2425 
2426  if (more_data && x1 < sps->width) {
2427  more_data = hls_coding_quadtree(lc, pps, sps, x1, y0, log2_cb_size - 1, cb_depth + 1);
2428  if (more_data < 0)
2429  return more_data;
2430  }
2431  if (more_data && y1 < sps->height) {
2432  more_data = hls_coding_quadtree(lc, pps, sps, x0, y1, log2_cb_size - 1, cb_depth + 1);
2433  if (more_data < 0)
2434  return more_data;
2435  }
2436  if (more_data && x1 < sps->width &&
2437  y1 < sps->height) {
2438  more_data = hls_coding_quadtree(lc, pps, sps, x1, y1, log2_cb_size - 1, cb_depth + 1);
2439  if (more_data < 0)
2440  return more_data;
2441  }
2442 
2443  if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2444  ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2445  lc->qPy_pred = lc->qp_y;
2446 
2447  if (more_data)
2448  return ((x1 + cb_size_split) < sps->width ||
2449  (y1 + cb_size_split) < sps->height);
2450  else
2451  return 0;
2452  } else {
2453  ret = hls_coding_unit(lc, s, pps, sps, x0, y0, log2_cb_size);
2454  if (ret < 0)
2455  return ret;
2456  if ((!((x0 + cb_size) %
2457  (1 << (sps->log2_ctb_size))) ||
2458  (x0 + cb_size >= sps->width)) &&
2459  (!((y0 + cb_size) %
2460  (1 << (sps->log2_ctb_size))) ||
2461  (y0 + cb_size >= sps->height))) {
2462  int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(lc);
2463  return !end_of_slice_flag;
2464  } else {
2465  return 1;
2466  }
2467  }
2468 
2469  return 0;
2470 }
2471 
2473  const HEVCPPS *pps, const HEVCSPS *sps,
2474  int x_ctb, int y_ctb, int ctb_addr_ts)
2475 {
2476  const HEVCContext *const s = lc->parent;
2477  int ctb_size = 1 << sps->log2_ctb_size;
2478  int ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2479  int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2480 
2481  s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2482 
2483  if (pps->entropy_coding_sync_enabled_flag) {
2484  if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2485  lc->first_qp_group = 1;
2486  lc->end_of_tiles_x = sps->width;
2487  } else if (pps->tiles_enabled_flag) {
2488  if (ctb_addr_ts && pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1]) {
2489  int idxX = pps->col_idxX[x_ctb >> sps->log2_ctb_size];
2490  lc->end_of_tiles_x = x_ctb + (pps->column_width[idxX] << sps->log2_ctb_size);
2491  lc->first_qp_group = 1;
2492  }
2493  } else {
2494  lc->end_of_tiles_x = sps->width;
2495  }
2496 
2497  lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, sps->height);
2498 
2499  lc->boundary_flags = 0;
2500  if (pps->tiles_enabled_flag) {
2501  if (x_ctb > 0 && pps->tile_id[ctb_addr_ts] != pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
2503  if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2505  if (y_ctb > 0 && pps->tile_id[ctb_addr_ts] != pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs - sps->ctb_width]])
2507  if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - sps->ctb_width])
2509  } else {
2510  if (ctb_addr_in_slice <= 0)
2512  if (ctb_addr_in_slice < sps->ctb_width)
2514  }
2515 
2516  lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2517  lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2518  lc->ctb_up_right_flag = ((y_ctb > 0) && (ctb_addr_in_slice+1 >= sps->ctb_width) && (pps->tile_id[ctb_addr_ts] == pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - sps->ctb_width]]));
2519  lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= sps->ctb_width) && (pps->tile_id[ctb_addr_ts] == pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - sps->ctb_width]]));
2520 }
2521 
2523 {
2524  HEVCLocalContext *const lc = &s->local_ctx[0];
2525  const HEVCPPS *const pps = s->pps;
2526  const HEVCSPS *const sps = pps->sps;
2527  const uint8_t *slice_data = gb->buffer + s->sh.data_offset;
2528  const size_t slice_size = gb->buffer_end - gb->buffer - s->sh.data_offset;
2529  int ctb_size = 1 << sps->log2_ctb_size;
2530  int more_data = 1;
2531  int x_ctb = 0;
2532  int y_ctb = 0;
2533  int ctb_addr_ts = pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2534  int ret;
2535 
2536  while (more_data && ctb_addr_ts < sps->ctb_size) {
2537  int ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2538 
2539  x_ctb = (ctb_addr_rs % ((sps->width + ctb_size - 1) >> sps->log2_ctb_size)) << sps->log2_ctb_size;
2540  y_ctb = (ctb_addr_rs / ((sps->width + ctb_size - 1) >> sps->log2_ctb_size)) << sps->log2_ctb_size;
2541  hls_decode_neighbour(lc, pps, sps, x_ctb, y_ctb, ctb_addr_ts);
2542 
2543  ret = ff_hevc_cabac_init(lc, pps, ctb_addr_ts, slice_data, slice_size, 0);
2544  if (ret < 0) {
2545  s->tab_slice_address[ctb_addr_rs] = -1;
2546  return ret;
2547  }
2548 
2549  hls_sao_param(lc, pps, sps,
2550  x_ctb >> sps->log2_ctb_size, y_ctb >> sps->log2_ctb_size);
2551 
2552  s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2553  s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2554  s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2555 
2556  more_data = hls_coding_quadtree(lc, pps, sps, x_ctb, y_ctb, sps->log2_ctb_size, 0);
2557  if (more_data < 0) {
2558  s->tab_slice_address[ctb_addr_rs] = -1;
2559  return more_data;
2560  }
2561 
2562 
2563  ctb_addr_ts++;
2564  ff_hevc_save_states(lc, pps, ctb_addr_ts);
2565  ff_hevc_hls_filters(lc, pps, x_ctb, y_ctb, ctb_size);
2566  }
2567 
2568  if (x_ctb + ctb_size >= sps->width &&
2569  y_ctb + ctb_size >= sps->height)
2570  ff_hevc_hls_filter(lc, pps, x_ctb, y_ctb, ctb_size);
2571 
2572  return ctb_addr_ts;
2573 }
2574 
2575 static int hls_decode_entry_wpp(AVCodecContext *avctx, void *hevc_lclist,
2576  int job, int self_id)
2577 {
2578  HEVCLocalContext *lc = &((HEVCLocalContext*)hevc_lclist)[self_id];
2579  const HEVCContext *const s = lc->parent;
2580  const HEVCPPS *const pps = s->pps;
2581  const HEVCSPS *const sps = pps->sps;
2582  int ctb_size = 1 << sps->log2_ctb_size;
2583  int more_data = 1;
2584  int ctb_row = job;
2585  int ctb_addr_rs = s->sh.slice_ctb_addr_rs + ctb_row * ((sps->width + ctb_size - 1) >> sps->log2_ctb_size);
2586  int ctb_addr_ts = pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2587  int thread = ctb_row % avctx->thread_count;
2588 
2589  const uint8_t *data = s->data + s->sh.offset[ctb_row];
2590  const size_t data_size = s->sh.size[ctb_row];
2591 
2592  int ret;
2593 
2594  if (ctb_row)
2595  ff_init_cabac_decoder(&lc->cc, data, data_size);
2596 
2597  while(more_data && ctb_addr_ts < sps->ctb_size) {
2598  int x_ctb = (ctb_addr_rs % sps->ctb_width) << sps->log2_ctb_size;
2599  int y_ctb = (ctb_addr_rs / sps->ctb_width) << sps->log2_ctb_size;
2600 
2601  hls_decode_neighbour(lc, pps, sps, x_ctb, y_ctb, ctb_addr_ts);
2602 
2603  ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2604 
2605  /* atomic_load's prototype requires a pointer to non-const atomic variable
2606  * (due to implementations via mutexes, where reads involve writes).
2607  * Of course, casting const away here is nevertheless safe. */
2608  if (atomic_load((atomic_int*)&s->wpp_err)) {
2609  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2610  return 0;
2611  }
2612 
2613  ret = ff_hevc_cabac_init(lc, pps, ctb_addr_ts, data, data_size, 1);
2614  if (ret < 0)
2615  goto error;
2616  hls_sao_param(lc, pps, sps,
2617  x_ctb >> sps->log2_ctb_size, y_ctb >> sps->log2_ctb_size);
2618  more_data = hls_coding_quadtree(lc, pps, sps, x_ctb, y_ctb, sps->log2_ctb_size, 0);
2619 
2620  if (more_data < 0) {
2621  ret = more_data;
2622  goto error;
2623  }
2624 
2625  ctb_addr_ts++;
2626 
2627  ff_hevc_save_states(lc, pps, ctb_addr_ts);
2628  ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2629  ff_hevc_hls_filters(lc, pps, x_ctb, y_ctb, ctb_size);
2630 
2631  if (!more_data && (x_ctb+ctb_size) < sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2632  /* Casting const away here is safe, because it is an atomic operation. */
2633  atomic_store((atomic_int*)&s->wpp_err, 1);
2634  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2635  return 0;
2636  }
2637 
2638  if ((x_ctb+ctb_size) >= sps->width && (y_ctb+ctb_size) >= sps->height ) {
2639  ff_hevc_hls_filter(lc, pps, x_ctb, y_ctb, ctb_size);
2640  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2641  return ctb_addr_ts;
2642  }
2643  ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2644  x_ctb+=ctb_size;
2645 
2646  if(x_ctb >= sps->width) {
2647  break;
2648  }
2649  }
2650  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2651 
2652  return 0;
2653 error:
2654  s->tab_slice_address[ctb_addr_rs] = -1;
2655  /* Casting const away here is safe, because it is an atomic operation. */
2656  atomic_store((atomic_int*)&s->wpp_err, 1);
2657  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2658  return ret;
2659 }
2660 
2661 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2662 {
2663  const HEVCPPS *const pps = s->pps;
2664  const HEVCSPS *const sps = pps->sps;
2665  const uint8_t *data = nal->data;
2666  int length = nal->size;
2667  int *ret;
2668  int64_t offset;
2669  int64_t startheader, cmpt = 0;
2670  int i, j, res = 0;
2671 
2672  if (s->sh.slice_ctb_addr_rs + s->sh.num_entry_point_offsets * sps->ctb_width >= sps->ctb_width * sps->ctb_height) {
2673  av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2674  s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2675  sps->ctb_width, sps->ctb_height
2676  );
2677  return AVERROR_INVALIDDATA;
2678  }
2679 
2680  if (s->avctx->thread_count > s->nb_local_ctx) {
2681  HEVCLocalContext *tmp = av_malloc_array(s->avctx->thread_count, sizeof(*s->local_ctx));
2682 
2683  if (!tmp)
2684  return AVERROR(ENOMEM);
2685 
2686  memcpy(tmp, s->local_ctx, sizeof(*s->local_ctx) * s->nb_local_ctx);
2687  av_free(s->local_ctx);
2688  s->local_ctx = tmp;
2689 
2690  for (unsigned i = s->nb_local_ctx; i < s->avctx->thread_count; i++) {
2691  tmp = &s->local_ctx[i];
2692 
2693  memset(tmp, 0, sizeof(*tmp));
2694 
2695  tmp->logctx = s->avctx;
2696  tmp->parent = s;
2697  tmp->common_cabac_state = &s->cabac;
2698  }
2699 
2700  s->nb_local_ctx = s->avctx->thread_count;
2701  }
2702 
2703  offset = s->sh.data_offset;
2704 
2705  for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2706  if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2707  startheader--;
2708  cmpt++;
2709  }
2710  }
2711 
2712  for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2713  offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2714  for (j = 0, cmpt = 0, startheader = offset
2715  + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2716  if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2717  startheader--;
2718  cmpt++;
2719  }
2720  }
2721  s->sh.size[i] = s->sh.entry_point_offset[i] - cmpt;
2722  s->sh.offset[i] = offset;
2723 
2724  }
2725 
2726  offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2727  if (length < offset) {
2728  av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2729  return AVERROR_INVALIDDATA;
2730  }
2731  s->sh.size [s->sh.num_entry_point_offsets] = length - offset;
2732  s->sh.offset[s->sh.num_entry_point_offsets] = offset;
2733 
2734  s->sh.offset[0] = s->sh.data_offset;
2735  s->sh.size[0] = s->sh.offset[1] - s->sh.offset[0];
2736 
2737  s->data = data;
2738 
2739  for (i = 1; i < s->nb_local_ctx; i++) {
2740  s->local_ctx[i].first_qp_group = 1;
2741  s->local_ctx[i].qp_y = s->local_ctx[0].qp_y;
2742  }
2743 
2744  atomic_store(&s->wpp_err, 0);
2745  res = ff_slice_thread_allocz_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
2746  if (res < 0)
2747  return res;
2748 
2749  ret = av_calloc(s->sh.num_entry_point_offsets + 1, sizeof(*ret));
2750  if (!ret)
2751  return AVERROR(ENOMEM);
2752 
2753  if (pps->entropy_coding_sync_enabled_flag)
2754  s->avctx->execute2(s->avctx, hls_decode_entry_wpp, s->local_ctx, ret, s->sh.num_entry_point_offsets + 1);
2755 
2756  for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2757  res += ret[i];
2758 
2759  av_free(ret);
2760  return res;
2761 }
2762 
2764 {
2765  const HEVCPPS *pps = s->pps;
2766  int ret;
2767 
2768  if (!s->sh.first_slice_in_pic_flag)
2769  s->slice_idx += !s->sh.dependent_slice_segment_flag;
2770 
2771  if (!s->sh.dependent_slice_segment_flag && s->sh.slice_type != HEVC_SLICE_I) {
2772  ret = ff_hevc_slice_rpl(s);
2773  if (ret < 0) {
2774  av_log(s->avctx, AV_LOG_WARNING,
2775  "Error constructing the reference lists for the current slice.\n");
2776  return ret;
2777  }
2778  }
2779 
2780  s->slice_initialized = 1;
2781 
2782  if (s->avctx->hwaccel)
2783  return FF_HW_CALL(s->avctx, decode_slice, nal->raw_data, nal->raw_size);
2784 
2785  if (s->avctx->profile == AV_PROFILE_HEVC_SCC) {
2786  av_log(s->avctx, AV_LOG_ERROR,
2787  "SCC profile is not yet implemented in hevc native decoder.\n");
2788  return AVERROR_PATCHWELCOME;
2789  }
2790 
2791  if (s->sh.dependent_slice_segment_flag) {
2792  int ctb_addr_ts = pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2793  int prev_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2794  if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2795  av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2796  return AVERROR_INVALIDDATA;
2797  }
2798  }
2799 
2800  s->local_ctx[0].first_qp_group = !s->sh.dependent_slice_segment_flag;
2801 
2802  if (!pps->cu_qp_delta_enabled_flag)
2803  s->local_ctx[0].qp_y = s->sh.slice_qp;
2804 
2805  s->local_ctx[0].tu.cu_qp_offset_cb = 0;
2806  s->local_ctx[0].tu.cu_qp_offset_cr = 0;
2807 
2808  if (s->avctx->active_thread_type == FF_THREAD_SLICE &&
2809  s->sh.num_entry_point_offsets > 0 &&
2810  pps->num_tile_rows == 1 && pps->num_tile_columns == 1)
2811  return hls_slice_data_wpp(s, nal);
2812 
2813  return hls_decode_entry(s, gb);
2814 }
2815 
2817 {
2818  AVFrame *out = s->cur_frame->f;
2819  int ret;
2820 
2821  // Decrement the mastering display and content light level flag when IRAP
2822  // frame has no_rasl_output_flag=1 so the side data persists for the entire
2823  // coded video sequence.
2824  if (IS_IRAP(s) && s->no_rasl_output_flag) {
2825  if (s->sei.common.mastering_display.present > 0)
2826  s->sei.common.mastering_display.present--;
2827 
2828  if (s->sei.common.content_light.present > 0)
2829  s->sei.common.content_light.present--;
2830  }
2831 
2832  ret = ff_h2645_sei_to_frame(out, &s->sei.common, AV_CODEC_ID_HEVC, s->avctx,
2833  &s->ps.sps->vui.common,
2834  s->ps.sps->bit_depth, s->ps.sps->bit_depth_chroma,
2835  s->cur_frame->poc /* no poc_offset in HEVC */);
2836  if (ret < 0)
2837  return ret;
2838 
2839  if (s->sei.timecode.present) {
2840  uint32_t *tc_sd;
2841  char tcbuf[AV_TIMECODE_STR_SIZE];
2842  AVFrameSideData *tcside;
2844  sizeof(uint32_t) * 4, &tcside);
2845  if (ret < 0)
2846  return ret;
2847 
2848  if (tcside) {
2849  tc_sd = (uint32_t*)tcside->data;
2850  tc_sd[0] = s->sei.timecode.num_clock_ts;
2851 
2852  for (int i = 0; i < tc_sd[0]; i++) {
2853  int drop = s->sei.timecode.cnt_dropped_flag[i];
2854  int hh = s->sei.timecode.hours_value[i];
2855  int mm = s->sei.timecode.minutes_value[i];
2856  int ss = s->sei.timecode.seconds_value[i];
2857  int ff = s->sei.timecode.n_frames[i];
2858 
2859  tc_sd[i + 1] = av_timecode_get_smpte(s->avctx->framerate, drop, hh, mm, ss, ff);
2860  av_timecode_make_smpte_tc_string2(tcbuf, s->avctx->framerate, tc_sd[i + 1], 0, 0);
2861  av_dict_set(&out->metadata, "timecode", tcbuf, 0);
2862  }
2863  }
2864 
2865  s->sei.timecode.num_clock_ts = 0;
2866  }
2867 
2868  if (s->sei.common.dynamic_hdr_plus.info) {
2869  AVBufferRef *info_ref = av_buffer_ref(s->sei.common.dynamic_hdr_plus.info);
2870  if (!info_ref)
2871  return AVERROR(ENOMEM);
2872 
2874  if (ret < 0)
2875  return ret;
2876  }
2877 
2878  if (s->rpu_buf) {
2880  if (!rpu)
2881  return AVERROR(ENOMEM);
2882 
2883  s->rpu_buf = NULL;
2884  }
2885 
2886  if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
2887  return ret;
2888 
2889  if (s->sei.common.dynamic_hdr_vivid.info) {
2890  AVBufferRef *info_ref = av_buffer_ref(s->sei.common.dynamic_hdr_vivid.info);
2891  if (!info_ref)
2892  return AVERROR(ENOMEM);
2893 
2895  av_buffer_unref(&info_ref);
2896  return AVERROR(ENOMEM);
2897  }
2898  }
2899 
2900  return 0;
2901 }
2902 
2904 {
2905  const HEVCPPS *const pps = s->ps.pps_list[s->sh.pps_id];
2906  const HEVCSPS *const sps = pps->sps;
2907  int pic_size_in_ctb = ((sps->width >> sps->log2_min_cb_size) + 1) *
2908  ((sps->height >> sps->log2_min_cb_size) + 1);
2909  int ret;
2910 
2911  ff_refstruct_replace(&s->pps, pps);
2912  if (s->ps.sps != sps) {
2913  enum AVPixelFormat pix_fmt;
2914 
2916 
2917  ret = set_sps(s, sps);
2918  if (ret < 0)
2919  return ret;
2920 
2922 
2923  pix_fmt = get_format(s, sps);
2924  if (pix_fmt < 0)
2925  return pix_fmt;
2926  s->avctx->pix_fmt = pix_fmt;
2927 
2928  s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
2929  }
2930 
2931  memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
2932  memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
2933  memset(s->cbf_luma, 0, sps->min_tb_width * sps->min_tb_height);
2934  memset(s->is_pcm, 0, (sps->min_pu_width + 1) * (sps->min_pu_height + 1));
2935  memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2936 
2937  if ((IS_IDR(s) || IS_BLA(s))) {
2938  s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
2939  if (IS_IDR(s))
2941  }
2942 
2943  s->slice_idx = 0;
2944  s->first_nal_type = s->nal_unit_type;
2945  s->poc = s->sh.poc;
2946 
2947  if (IS_IRAP(s))
2948  s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) ||
2949  (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
2950 
2951  /* 8.3.1 */
2952  if (s->temporal_id == 0 &&
2953  s->nal_unit_type != HEVC_NAL_TRAIL_N &&
2954  s->nal_unit_type != HEVC_NAL_TSA_N &&
2955  s->nal_unit_type != HEVC_NAL_STSA_N &&
2956  s->nal_unit_type != HEVC_NAL_RADL_N &&
2957  s->nal_unit_type != HEVC_NAL_RADL_R &&
2958  s->nal_unit_type != HEVC_NAL_RASL_N &&
2959  s->nal_unit_type != HEVC_NAL_RASL_R)
2960  s->poc_tid0 = s->poc;
2961 
2962  if (pps->tiles_enabled_flag)
2963  s->local_ctx[0].end_of_tiles_x = pps->column_width[0] << sps->log2_ctb_size;
2964 
2966  if (ret < 0)
2967  return ret;
2968 
2969  ret = ff_hevc_set_new_ref(s, s->poc);
2970  if (ret < 0)
2971  goto fail;
2972 
2973  ret = ff_hevc_frame_rps(s);
2974  if (ret < 0) {
2975  av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2976  goto fail;
2977  }
2978 
2979  if (IS_IRAP(s))
2980  s->cur_frame->f->flags |= AV_FRAME_FLAG_KEY;
2981  else
2982  s->cur_frame->f->flags &= ~AV_FRAME_FLAG_KEY;
2983 
2984  s->cur_frame->needs_fg = (s->sei.common.film_grain_characteristics.present ||
2985  s->sei.common.aom_film_grain.enable) &&
2986  !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
2987  !s->avctx->hwaccel;
2988 
2989  ret = set_side_data(s);
2990  if (ret < 0)
2991  goto fail;
2992 
2993  if (s->cur_frame->needs_fg &&
2994  (s->sei.common.film_grain_characteristics.present &&
2995  !ff_h274_film_grain_params_supported(s->sei.common.film_grain_characteristics.model_id,
2996  s->cur_frame->f->format) ||
2997  !av_film_grain_params_select(s->cur_frame->f))) {
2998  av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown,
2999  "Unsupported film grain parameters. Ignoring film grain.\n");
3000  s->cur_frame->needs_fg = 0;
3001  }
3002 
3003  if (s->cur_frame->needs_fg) {
3004  s->cur_frame->frame_grain->format = s->cur_frame->f->format;
3005  s->cur_frame->frame_grain->width = s->cur_frame->f->width;
3006  s->cur_frame->frame_grain->height = s->cur_frame->f->height;
3007  if ((ret = ff_thread_get_buffer(s->avctx, s->cur_frame->frame_grain, 0)) < 0)
3008  goto fail;
3009  }
3010 
3011  s->cur_frame->f->pict_type = 3 - s->sh.slice_type;
3012 
3013  if (!IS_IRAP(s))
3015 
3016  av_frame_unref(s->output_frame);
3017  ret = ff_hevc_output_frame(s, s->output_frame, 0);
3018  if (ret < 0)
3019  goto fail;
3020 
3021  if (s->avctx->hwaccel) {
3022  ret = FF_HW_CALL(s->avctx, start_frame, NULL, 0);
3023  if (ret < 0)
3024  goto fail;
3025  } else
3026  ff_thread_finish_setup(s->avctx);
3027 
3028  return 0;
3029 
3030 fail:
3031  if (s->cur_frame)
3032  ff_hevc_unref_frame(s->cur_frame, ~0);
3033  s->cur_frame = s->collocated_ref = NULL;
3034  s->slice_initialized = 0;
3035  return ret;
3036 }
3037 
3039 {
3041  char msg_buf[4 * (50 + 2 * 2 * 16 /* MD5-size */)];
3042  int pixel_shift;
3043  int err = 0;
3044  int i, j;
3045 
3046  if (!desc)
3047  return AVERROR(EINVAL);
3048 
3049  pixel_shift = desc->comp[0].depth > 8;
3050 
3051  /* the checksums are LE, so we have to byteswap for >8bpp formats
3052  * on BE arches */
3053 #if HAVE_BIGENDIAN
3054  if (pixel_shift && !s->checksum_buf) {
3055  av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3056  FFMAX3(frame->linesize[0], frame->linesize[1],
3057  frame->linesize[2]));
3058  if (!s->checksum_buf)
3059  return AVERROR(ENOMEM);
3060  }
3061 #endif
3062 
3063  msg_buf[0] = '\0';
3064  for (i = 0; frame->data[i]; i++) {
3065  int width = s->avctx->coded_width;
3066  int height = s->avctx->coded_height;
3067  int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
3068  int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3069  uint8_t md5[16];
3070 
3071  av_md5_init(s->md5_ctx);
3072  for (j = 0; j < h; j++) {
3073  const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3074 #if HAVE_BIGENDIAN
3075  if (pixel_shift) {
3076  s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3077  (const uint16_t *) src, w);
3078  src = s->checksum_buf;
3079  }
3080 #endif
3081  av_md5_update(s->md5_ctx, src, w << pixel_shift);
3082  }
3083  av_md5_final(s->md5_ctx, md5);
3084 
3085 #define MD5_PRI "%016" PRIx64 "%016" PRIx64
3086 #define MD5_PRI_ARG(buf) AV_RB64(buf), AV_RB64((const uint8_t*)(buf) + 8)
3087 
3088  if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3089  av_strlcatf(msg_buf, sizeof(msg_buf),
3090  "plane %d - correct " MD5_PRI "; ",
3091  i, MD5_PRI_ARG(md5));
3092  } else {
3093  av_strlcatf(msg_buf, sizeof(msg_buf),
3094  "mismatching checksum of plane %d - " MD5_PRI " != " MD5_PRI "; ",
3095  i, MD5_PRI_ARG(md5), MD5_PRI_ARG(s->sei.picture_hash.md5[i]));
3096  err = AVERROR_INVALIDDATA;
3097  }
3098  }
3099 
3100  av_log(s->avctx, err < 0 ? AV_LOG_ERROR : AV_LOG_DEBUG,
3101  "Verifying checksum for frame with POC %d: %s\n",
3102  s->poc, msg_buf);
3103 
3104  return err;
3105  }
3106 
3108 {
3109  HEVCFrame *out = s->cur_frame;
3110  const AVFilmGrainParams *fgp;
3111  av_unused int ret;
3112 
3113  if (out->needs_fg) {
3114  av_assert0(out->frame_grain->buf[0]);
3115  fgp = av_film_grain_params_select(out->f);
3116  switch (fgp->type) {
3118  av_assert0(0);
3119  return AVERROR_BUG;
3121  ret = ff_h274_apply_film_grain(out->frame_grain, out->f,
3122  &s->h274db, fgp);
3123  break;
3125  ret = ff_aom_apply_film_grain(out->frame_grain, out->f, fgp);
3126  break;
3127  }
3128  av_assert1(ret >= 0);
3129  }
3130 
3131  if (s->avctx->hwaccel) {
3132  ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame);
3133  if (ret < 0) {
3134  av_log(s->avctx, AV_LOG_ERROR,
3135  "hardware accelerator failed to decode picture\n");
3136  return ret;
3137  }
3138  } else {
3139  if (s->avctx->err_recognition & AV_EF_CRCCHECK &&
3140  s->sei.picture_hash.is_md5) {
3141  ret = verify_md5(s, s->cur_frame->f);
3142  if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
3143  return ret;
3144  }
3145  }
3146  s->sei.picture_hash.is_md5 = 0;
3147 
3148  av_log(s->avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
3149 
3150  return 0;
3151 }
3152 
3153 static int decode_slice(HEVCContext *s, const H2645NAL *nal, GetBitContext *gb)
3154 {
3155  int ret;
3156 
3157  ret = hls_slice_header(&s->sh, s, gb);
3158  if (ret < 0)
3159  return ret;
3160 
3161  if ((s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
3162  (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
3163  (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s)) ||
3164  ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
3165  s->no_rasl_output_flag)) {
3166  return 0;
3167  }
3168 
3169  if (s->sh.first_slice_in_pic_flag) {
3170  if (s->cur_frame) {
3171  av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
3172  return AVERROR_INVALIDDATA;
3173  }
3174 
3175  ret = hevc_frame_start(s);
3176  if (ret < 0)
3177  return ret;
3178  } else if (!s->cur_frame) {
3179  av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
3180  return AVERROR_INVALIDDATA;
3181  }
3182 
3183  if (s->nal_unit_type != s->first_nal_type) {
3184  av_log(s->avctx, AV_LOG_ERROR,
3185  "Non-matching NAL types of the VCL NALUs: %d %d\n",
3186  s->first_nal_type, s->nal_unit_type);
3187  return AVERROR_INVALIDDATA;
3188  }
3189 
3190  ret = decode_slice_data(s, nal, gb);
3191  if (ret < 0)
3192  return ret;
3193 
3194  return 0;
3195 }
3196 
3197 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
3198 {
3199  GetBitContext gb = nal->gb;
3200  int ret;
3201 
3202  s->nal_unit_type = nal->type;
3203  s->temporal_id = nal->temporal_id;
3204 
3205  if (FF_HW_HAS_CB(s->avctx, decode_params) &&
3206  (s->nal_unit_type == HEVC_NAL_VPS ||
3207  s->nal_unit_type == HEVC_NAL_SPS ||
3208  s->nal_unit_type == HEVC_NAL_PPS ||
3209  s->nal_unit_type == HEVC_NAL_SEI_PREFIX ||
3210  s->nal_unit_type == HEVC_NAL_SEI_SUFFIX)) {
3211  ret = FF_HW_CALL(s->avctx, decode_params,
3212  nal->type, nal->raw_data, nal->raw_size);
3213  if (ret < 0)
3214  goto fail;
3215  }
3216 
3217  switch (s->nal_unit_type) {
3218  case HEVC_NAL_VPS:
3219  ret = ff_hevc_decode_nal_vps(&gb, s->avctx, &s->ps);
3220  if (ret < 0)
3221  goto fail;
3222  break;
3223  case HEVC_NAL_SPS:
3224  ret = ff_hevc_decode_nal_sps(&gb, s->avctx, &s->ps,
3225  s->apply_defdispwin);
3226  if (ret < 0)
3227  goto fail;
3228  break;
3229  case HEVC_NAL_PPS:
3230  ret = ff_hevc_decode_nal_pps(&gb, s->avctx, &s->ps);
3231  if (ret < 0)
3232  goto fail;
3233  break;
3234  case HEVC_NAL_SEI_PREFIX:
3235  case HEVC_NAL_SEI_SUFFIX:
3236  ret = ff_hevc_decode_nal_sei(&gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
3237  if (ret < 0)
3238  goto fail;
3239  break;
3240  case HEVC_NAL_TRAIL_R:
3241  case HEVC_NAL_TRAIL_N:
3242  case HEVC_NAL_TSA_N:
3243  case HEVC_NAL_TSA_R:
3244  case HEVC_NAL_STSA_N:
3245  case HEVC_NAL_STSA_R:
3246  case HEVC_NAL_BLA_W_LP:
3247  case HEVC_NAL_BLA_W_RADL:
3248  case HEVC_NAL_BLA_N_LP:
3249  case HEVC_NAL_IDR_W_RADL:
3250  case HEVC_NAL_IDR_N_LP:
3251  case HEVC_NAL_CRA_NUT:
3252  case HEVC_NAL_RADL_N:
3253  case HEVC_NAL_RADL_R:
3254  case HEVC_NAL_RASL_N:
3255  case HEVC_NAL_RASL_R:
3256  ret = decode_slice(s, nal, &gb);
3257  if (ret < 0)
3258  goto fail;
3259  break;
3260  case HEVC_NAL_EOS_NUT:
3261  case HEVC_NAL_EOB_NUT:
3262  s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
3263  break;
3264  case HEVC_NAL_AUD:
3265  case HEVC_NAL_FD_NUT:
3266  case HEVC_NAL_UNSPEC62:
3267  break;
3268  default:
3269  av_log(s->avctx, AV_LOG_INFO,
3270  "Skipping NAL unit %d\n", s->nal_unit_type);
3271  }
3272 
3273  return 0;
3274 fail:
3275  if (ret == AVERROR_INVALIDDATA &&
3276  !(s->avctx->err_recognition & AV_EF_EXPLODE)) {
3277  av_log(s->avctx, AV_LOG_WARNING,
3278  "Skipping invalid undecodable NALU: %d\n", s->nal_unit_type);
3279  return 0;
3280  }
3281  return ret;
3282 }
3283 
3284 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
3285 {
3286  int i, ret = 0;
3287  int eos_at_start = 1;
3288 
3289  s->cur_frame = s->collocated_ref = NULL;
3290  s->last_eos = s->eos;
3291  s->eos = 0;
3292  s->slice_initialized = 0;
3293 
3294  /* split the input packet into NAL units, so we know the upper bound on the
3295  * number of slices in the frame */
3296  ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
3297  s->nal_length_size, s->avctx->codec_id, 1, 0);
3298  if (ret < 0) {
3299  av_log(s->avctx, AV_LOG_ERROR,
3300  "Error splitting the input into NAL units.\n");
3301  return ret;
3302  }
3303 
3304  for (i = 0; i < s->pkt.nb_nals; i++) {
3305  if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
3306  s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
3307  if (eos_at_start) {
3308  s->last_eos = 1;
3309  } else {
3310  s->eos = 1;
3311  }
3312  } else {
3313  eos_at_start = 0;
3314  }
3315  }
3316 
3317  /*
3318  * Check for RPU delimiter.
3319  *
3320  * Dolby Vision RPUs masquerade as unregistered NALs of type 62.
3321  *
3322  * We have to do this check here an create the rpu buffer, since RPUs are appended
3323  * to the end of an AU; they are the last non-EOB/EOS NAL in the AU.
3324  */
3325  if (s->pkt.nb_nals > 1 && s->pkt.nals[s->pkt.nb_nals - 1].type == HEVC_NAL_UNSPEC62 &&
3326  s->pkt.nals[s->pkt.nb_nals - 1].size > 2 && !s->pkt.nals[s->pkt.nb_nals - 1].nuh_layer_id
3327  && !s->pkt.nals[s->pkt.nb_nals - 1].temporal_id) {
3328  H2645NAL *nal = &s->pkt.nals[s->pkt.nb_nals - 1];
3329  if (s->rpu_buf) {
3330  av_buffer_unref(&s->rpu_buf);
3331  av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
3332  }
3333 
3334  s->rpu_buf = av_buffer_alloc(nal->raw_size - 2);
3335  if (!s->rpu_buf)
3336  return AVERROR(ENOMEM);
3337  memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2);
3338 
3339  ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2,
3340  s->avctx->err_recognition);
3341  if (ret < 0) {
3342  av_buffer_unref(&s->rpu_buf);
3343  av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
3344  /* ignore */
3345  }
3346  }
3347 
3348  /* decode the NAL units */
3349  for (i = 0; i < s->pkt.nb_nals; i++) {
3350  H2645NAL *nal = &s->pkt.nals[i];
3351 
3352  if (s->avctx->skip_frame >= AVDISCARD_ALL ||
3353  (s->avctx->skip_frame >= AVDISCARD_NONREF
3354  && ff_hevc_nal_is_nonref(nal->type)) || nal->nuh_layer_id > 0)
3355  continue;
3356 
3357  ret = decode_nal_unit(s, nal);
3358  if (ret < 0) {
3359  av_log(s->avctx, AV_LOG_WARNING,
3360  "Error parsing NAL unit #%d.\n", i);
3361  goto fail;
3362  }
3363  }
3364 
3365 fail:
3366  if (s->cur_frame) {
3367  if (ret >= 0)
3368  ret = hevc_frame_end(s);
3369 
3370  if (s->avctx->active_thread_type == FF_THREAD_FRAME)
3371  ff_progress_frame_report(&s->cur_frame->tf, INT_MAX);
3372  }
3373 
3374  return ret;
3375 }
3376 
3377 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3378 {
3379  int ret, i;
3380 
3381  ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3382  &s->nal_length_size, s->avctx->err_recognition,
3383  s->apply_defdispwin, s->avctx);
3384  if (ret < 0)
3385  return ret;
3386 
3387  /* export stream parameters from the first SPS */
3388  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3389  if (first && s->ps.sps_list[i]) {
3390  const HEVCSPS *sps = s->ps.sps_list[i];
3392  break;
3393  }
3394  }
3395 
3396  /* export stream parameters from SEI */
3398  if (ret < 0)
3399  return ret;
3400 
3401  return 0;
3402 }
3403 
3404 static int hevc_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
3405  int *got_output, AVPacket *avpkt)
3406 {
3407  int ret;
3408  uint8_t *sd;
3409  size_t sd_size;
3410  HEVCContext *s = avctx->priv_data;
3411 
3412  if (!avpkt->size) {
3413  ret = ff_hevc_output_frame(s, rframe, 1);
3414  if (ret < 0)
3415  return ret;
3416 
3417  *got_output = ret;
3418  return 0;
3419  }
3420 
3421  sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
3422  if (sd && sd_size > 0) {
3423  ret = hevc_decode_extradata(s, sd, sd_size, 0);
3424  if (ret < 0)
3425  return ret;
3426  }
3427 
3428  sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
3429  if (sd && sd_size >= sizeof(s->dovi_ctx.cfg)) {
3430  int old = s->dovi_ctx.cfg.dv_profile;
3431  s->dovi_ctx.cfg = *(AVDOVIDecoderConfigurationRecord *) sd;
3432  if (old)
3433  av_log(avctx, AV_LOG_DEBUG,
3434  "New DOVI configuration record from input packet (profile %d -> %u).\n",
3435  old, s->dovi_ctx.cfg.dv_profile);
3436  }
3437 
3438  ret = decode_nal_units(s, avpkt->data, avpkt->size);
3439  if (ret < 0)
3440  return ret;
3441 
3442  if (s->output_frame->buf[0]) {
3443  av_frame_move_ref(rframe, s->output_frame);
3444  *got_output = 1;
3445  }
3446 
3447  return avpkt->size;
3448 }
3449 
3450 static int hevc_ref_frame(HEVCFrame *dst, const HEVCFrame *src)
3451 {
3452  int ret;
3453 
3454  ff_progress_frame_ref(&dst->tf, &src->tf);
3455 
3456  if (src->needs_fg) {
3457  ret = av_frame_ref(dst->frame_grain, src->frame_grain);
3458  if (ret < 0) {
3459  ff_hevc_unref_frame(dst, ~0);
3460  return ret;
3461  }
3462  dst->needs_fg = 1;
3463  }
3464 
3465  dst->tab_mvf = ff_refstruct_ref(src->tab_mvf);
3466  dst->rpl_tab = ff_refstruct_ref(src->rpl_tab);
3467  dst->rpl = ff_refstruct_ref(src->rpl);
3468  dst->nb_rpl_elems = src->nb_rpl_elems;
3469 
3470  dst->poc = src->poc;
3471  dst->ctb_count = src->ctb_count;
3472  dst->flags = src->flags;
3473  dst->sequence = src->sequence;
3474 
3476  src->hwaccel_picture_private);
3477 
3478  return 0;
3479 }
3480 
3482 {
3483  HEVCContext *s = avctx->priv_data;
3484  int i;
3485 
3486  pic_arrays_free(s);
3487 
3488  ff_refstruct_unref(&s->pps);
3489 
3490  ff_dovi_ctx_unref(&s->dovi_ctx);
3491  av_buffer_unref(&s->rpu_buf);
3492 
3493  av_freep(&s->md5_ctx);
3494 
3495  for (i = 0; i < 3; i++) {
3496  av_freep(&s->sao_pixel_buffer_h[i]);
3497  av_freep(&s->sao_pixel_buffer_v[i]);
3498  }
3499  av_frame_free(&s->output_frame);
3500 
3501  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3502  ff_hevc_unref_frame(&s->DPB[i], ~0);
3503  av_frame_free(&s->DPB[i].frame_grain);
3504  }
3505 
3506  ff_hevc_ps_uninit(&s->ps);
3507 
3508  av_freep(&s->sh.entry_point_offset);
3509  av_freep(&s->sh.offset);
3510  av_freep(&s->sh.size);
3511 
3512  av_freep(&s->local_ctx);
3513 
3514  ff_h2645_packet_uninit(&s->pkt);
3515 
3516  ff_hevc_reset_sei(&s->sei);
3517 
3518  return 0;
3519 }
3520 
3522 {
3523  HEVCContext *s = avctx->priv_data;
3524  int i;
3525 
3526  s->avctx = avctx;
3527 
3528  s->local_ctx = av_mallocz(sizeof(*s->local_ctx));
3529  if (!s->local_ctx)
3530  return AVERROR(ENOMEM);
3531  s->nb_local_ctx = 1;
3532 
3533  s->local_ctx[0].parent = s;
3534  s->local_ctx[0].logctx = avctx;
3535  s->local_ctx[0].common_cabac_state = &s->cabac;
3536 
3537  s->output_frame = av_frame_alloc();
3538  if (!s->output_frame)
3539  return AVERROR(ENOMEM);
3540 
3541  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3542  s->DPB[i].frame_grain = av_frame_alloc();
3543  if (!s->DPB[i].frame_grain)
3544  return AVERROR(ENOMEM);
3545  }
3546 
3547  s->md5_ctx = av_md5_alloc();
3548  if (!s->md5_ctx)
3549  return AVERROR(ENOMEM);
3550 
3551  ff_bswapdsp_init(&s->bdsp);
3552 
3553  s->dovi_ctx.logctx = avctx;
3554  s->eos = 0;
3555 
3556  ff_hevc_reset_sei(&s->sei);
3557 
3558  return 0;
3559 }
3560 
3561 #if HAVE_THREADS
3562 static int hevc_update_thread_context(AVCodecContext *dst,
3563  const AVCodecContext *src)
3564 {
3565  HEVCContext *s = dst->priv_data;
3566  HEVCContext *s0 = src->priv_data;
3567  int i, ret;
3568 
3569  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3570  ff_hevc_unref_frame(&s->DPB[i], ~0);
3571  if (s0->DPB[i].f) {
3572  ret = hevc_ref_frame(&s->DPB[i], &s0->DPB[i]);
3573  if (ret < 0)
3574  return ret;
3575  }
3576  }
3577 
3578  if (s->ps.sps != s0->ps.sps)
3579  s->ps.sps = NULL;
3580  for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
3581  ff_refstruct_replace(&s->ps.vps_list[i], s0->ps.vps_list[i]);
3582 
3583  for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
3584  ff_refstruct_replace(&s->ps.sps_list[i], s0->ps.sps_list[i]);
3585 
3586  for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
3587  ff_refstruct_replace(&s->ps.pps_list[i], s0->ps.pps_list[i]);
3588 
3589  // PPS do not persist between frames
3590  ff_refstruct_unref(&s->pps);
3591 
3592  if (s->ps.sps != s0->ps.sps)
3593  if ((ret = set_sps(s, s0->ps.sps)) < 0)
3594  return ret;
3595 
3596  s->seq_decode = s0->seq_decode;
3597  s->seq_output = s0->seq_output;
3598  s->poc_tid0 = s0->poc_tid0;
3599  s->eos = s0->eos;
3600  s->no_rasl_output_flag = s0->no_rasl_output_flag;
3601 
3602  s->is_nalff = s0->is_nalff;
3603  s->nal_length_size = s0->nal_length_size;
3604 
3605  s->film_grain_warning_shown = s0->film_grain_warning_shown;
3606 
3607  if (s0->eos) {
3608  s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
3609  }
3610 
3611  ret = ff_h2645_sei_ctx_replace(&s->sei.common, &s0->sei.common);
3612  if (ret < 0)
3613  return ret;
3614 
3615  ret = av_buffer_replace(&s->sei.common.dynamic_hdr_plus.info,
3617  if (ret < 0)
3618  return ret;
3619 
3620  ret = av_buffer_replace(&s->rpu_buf, s0->rpu_buf);
3621  if (ret < 0)
3622  return ret;
3623 
3624  ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
3625 
3626  ret = av_buffer_replace(&s->sei.common.dynamic_hdr_vivid.info,
3628  if (ret < 0)
3629  return ret;
3630 
3631  s->sei.common.frame_packing = s0->sei.common.frame_packing;
3632  s->sei.common.display_orientation = s0->sei.common.display_orientation;
3633  s->sei.common.alternative_transfer = s0->sei.common.alternative_transfer;
3634  s->sei.common.mastering_display = s0->sei.common.mastering_display;
3635  s->sei.common.content_light = s0->sei.common.content_light;
3636  s->sei.common.aom_film_grain = s0->sei.common.aom_film_grain;
3637 
3638  return 0;
3639 }
3640 #endif
3641 
3643 {
3644  HEVCContext *s = avctx->priv_data;
3645  int ret;
3646 
3647  if (avctx->active_thread_type & FF_THREAD_SLICE) {
3649  if (ret < 0)
3650  return ret;
3651  }
3652 
3653  ret = hevc_init_context(avctx);
3654  if (ret < 0)
3655  return ret;
3656 
3657  s->sei.picture_timing.picture_struct = 0;
3658  s->eos = 1;
3659 
3660  atomic_init(&s->wpp_err, 0);
3661 
3662  if (!avctx->internal->is_copy) {
3663  const AVPacketSideData *sd;
3664 
3665  if (avctx->extradata_size > 0 && avctx->extradata) {
3666  ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
3667  if (ret < 0) {
3668  return ret;
3669  }
3670 
3671  ret = ff_h2645_sei_to_context(avctx, &s->sei.common);
3672  if (ret < 0)
3673  return ret;
3674  }
3675 
3677  if (sd && sd->size >= sizeof(s->dovi_ctx.cfg))
3678  s->dovi_ctx.cfg = *(AVDOVIDecoderConfigurationRecord *) sd->data;
3679  }
3680 
3681  return 0;
3682 }
3683 
3685 {
3686  HEVCContext *s = avctx->priv_data;
3688  ff_hevc_reset_sei(&s->sei);
3689  ff_dovi_ctx_flush(&s->dovi_ctx);
3690  av_buffer_unref(&s->rpu_buf);
3691  s->eos = 1;
3692 
3693  if (FF_HW_HAS_CB(avctx, flush))
3694  FF_HW_SIMPLE_CALL(avctx, flush);
3695 }
3696 
3697 #define OFFSET(x) offsetof(HEVCContext, x)
3698 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3699 
3700 static const AVOption options[] = {
3701  { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3702  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3703  { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
3704  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3705  { NULL },
3706 };
3707 
3708 static const AVClass hevc_decoder_class = {
3709  .class_name = "HEVC decoder",
3710  .item_name = av_default_item_name,
3711  .option = options,
3712  .version = LIBAVUTIL_VERSION_INT,
3713 };
3714 
3716  .p.name = "hevc",
3717  CODEC_LONG_NAME("HEVC (High Efficiency Video Coding)"),
3718  .p.type = AVMEDIA_TYPE_VIDEO,
3719  .p.id = AV_CODEC_ID_HEVC,
3720  .priv_data_size = sizeof(HEVCContext),
3721  .p.priv_class = &hevc_decoder_class,
3722  .init = hevc_decode_init,
3723  .close = hevc_decode_free,
3725  .flush = hevc_decode_flush,
3726  UPDATE_THREAD_CONTEXT(hevc_update_thread_context),
3727  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3729  .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
3732  .p.profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
3733  .hw_configs = (const AVCodecHWConfigInternal *const []) {
3734 #if CONFIG_HEVC_DXVA2_HWACCEL
3735  HWACCEL_DXVA2(hevc),
3736 #endif
3737 #if CONFIG_HEVC_D3D11VA_HWACCEL
3738  HWACCEL_D3D11VA(hevc),
3739 #endif
3740 #if CONFIG_HEVC_D3D11VA2_HWACCEL
3741  HWACCEL_D3D11VA2(hevc),
3742 #endif
3743 #if CONFIG_HEVC_D3D12VA_HWACCEL
3744  HWACCEL_D3D12VA(hevc),
3745 #endif
3746 #if CONFIG_HEVC_NVDEC_HWACCEL
3747  HWACCEL_NVDEC(hevc),
3748 #endif
3749 #if CONFIG_HEVC_VAAPI_HWACCEL
3750  HWACCEL_VAAPI(hevc),
3751 #endif
3752 #if CONFIG_HEVC_VDPAU_HWACCEL
3753  HWACCEL_VDPAU(hevc),
3754 #endif
3755 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
3756  HWACCEL_VIDEOTOOLBOX(hevc),
3757 #endif
3758 #if CONFIG_HEVC_VULKAN_HWACCEL
3759  HWACCEL_VULKAN(hevc),
3760 #endif
3761  NULL
3762  },
3763 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
HEVCContext::seq_decode
uint16_t seq_decode
Sequence counters for decoded and output frames, so that old frames are output first after a POC rese...
Definition: hevcdec.h:525
HEVC_NAL_AUD
@ HEVC_NAL_AUD
Definition: hevc.h:64
HWACCEL_D3D12VA
#define HWACCEL_D3D12VA(codec)
Definition: hwconfig.h:80
ff_get_coded_side_data
const AVPacketSideData * ff_get_coded_side_data(const AVCodecContext *avctx, enum AVPacketSideDataType type)
Get side data of the given type from a decoding context.
Definition: decode.c:1379
hls_decode_neighbour
static void hls_decode_neighbour(HEVCLocalContext *lc, const HEVCPPS *pps, const HEVCSPS *sps, int x_ctb, int y_ctb, int ctb_addr_ts)
Definition: hevcdec.c:2472
hwconfig.h
ff_progress_frame_report
void ff_progress_frame_report(ProgressFrame *f, int n)
Notify later decoding threads when part of their reference frame is ready.
Definition: decode.c:1759
hevc_decoder_class
static const AVClass hevc_decoder_class
Definition: hevcdec.c:3708
SliceHeader::beta_offset
int beta_offset
beta_offset_div2 * 2
Definition: hevcdec.h:254
bswapdsp.h
L1
F H1 F F H1 F F F F H1<-F-------F-------F v v v H2 H3 H2 ^ ^ ^ F-------F-------F-> H1<-F-------F-------F|||||||||F H1 F|||||||||F H1 Funavailable fullpel samples(outside the picture for example) shall be equalto the closest available fullpel sampleSmaller pel interpolation:--------------------------if diag_mc is set then points which lie on a line between 2 vertically, horizontally or diagonally adjacent halfpel points shall be interpolatedlinearly with rounding to nearest and halfway values rounded up.points which lie on 2 diagonals at the same time should only use the onediagonal not containing the fullpel point F--> O q O<--h1-> O q O<--F v \/v \/v O O O O O O O|/|\|q q q q q|/|\|O O O O O O O ^/\ ^/\ ^ h2--> O q O<--h3-> O q O<--h2 v \/v \/v O O O O O O O|\|/|q q q q q|\|/|O O O O O O O ^/\ ^/\ ^ F--> O q O<--h1-> O q O<--Fthe remaining points shall be bilinearly interpolated from theup to 4 surrounding halfpel and fullpel points, again rounding should be tonearest and halfway values rounded upcompliant Snow decoders MUST support 1-1/8 pel luma and 1/2-1/16 pel chromainterpolation at leastOverlapped block motion compensation:-------------------------------------FIXMELL band prediction:===================Each sample in the LL0 subband is predicted by the median of the left, top andleft+top-topleft samples, samples outside the subband shall be considered tobe 0. To reverse this prediction in the decoder apply the following.for(y=0;y< height;y++){ for(x=0;x< width;x++){ sample[y][x]+=median(sample[y-1][x], sample[y][x-1], sample[y-1][x]+sample[y][x-1]-sample[y-1][x-1]);}}sample[-1][ *]=sample[ *][-1]=0;width, height here are the width and height of the LL0 subband not of the finalvideoDequantization:===============FIXMEWavelet Transform:==================Snow supports 2 wavelet transforms, the symmetric biorthogonal 5/3 integertransform and an integer approximation of the symmetric biorthogonal 9/7daubechies wavelet.2D IDWT(inverse discrete wavelet transform) --------------------------------------------The 2D IDWT applies a 2D filter recursively, each time combining the4 lowest frequency subbands into a single subband until only 1 subbandremains.The 2D filter is done by first applying a 1D filter in the vertical directionand then applying it in the horizontal one. --------------- --------------- --------------- ---------------|LL0|HL0|||||||||||||---+---|HL1||L0|H0|HL1||LL1|HL1|||||LH0|HH0|||||||||||||-------+-------|-> L1 H1 LH1 HH1 LH1 HH1 LH1 HH1 L1
Definition: snow.txt:554
ff_hevc_dsp_init
void ff_hevc_dsp_init(HEVCDSPContext *hevcdsp, int bit_depth)
Definition: dsp.c:128
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
H2645SEI::aom_film_grain
AVFilmGrainAFGS1Params aom_film_grain
Definition: h2645_sei.h:137
AV_TIMECODE_STR_SIZE
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
ff_hevc_luma_mv_mvp_mode
void ff_hevc_luma_mv_mvp_mode(HEVCLocalContext *lc, const HEVCPPS *pps, int x0, int y0, int nPbW, int nPbH, int log2_cb_size, int part_idx, int merge_idx, MvField *mv, int mvp_lx_flag, int LX)
Definition: mvs.c:589
HEVCLocalContext
Definition: hevcdec.h:389
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:260
HEVCFrame::flags
uint8_t flags
A combination of HEVC_FRAME_FLAG_*.
Definition: hevcdec.h:386
SliceHeader::slice_act_cr_qp_offset
int slice_act_cr_qp_offset
Definition: hevcdec.h:250
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
ff_hevc_merge_flag_decode
int ff_hevc_merge_flag_decode(HEVCLocalContext *lc)
Definition: cabac.c:748
av_clip
#define av_clip
Definition: common.h:100
atomic_store
#define atomic_store(object, desired)
Definition: stdatomic.h:85
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
ff_refstruct_ref
void * ff_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition: refstruct.c:140
SliceHeader::chroma_offset_l1
int16_t chroma_offset_l1[16][2]
Definition: hevcdec.h:279
HEVCContext::seq_output
uint16_t seq_output
Definition: hevcdec.h:526
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
hevc_decode_frame
static int hevc_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_output, AVPacket *avpkt)
Definition: hevcdec.c:3404
opt.h
ff_dovi_ctx_unref
void ff_dovi_ctx_unref(DOVIContext *s)
Completely reset a DOVIContext, preserving only logctx.
Definition: dovi_rpu.c:29
ff_frame_new_side_data_from_buf
int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx, AVFrame *frame, enum AVFrameSideDataType type, AVBufferRef **buf, AVFrameSideData **psd)
Similar to ff_frame_new_side_data, but using an existing buffer ref.
Definition: decode.c:1997
MD5_PRI_ARG
#define MD5_PRI_ARG(buf)
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:685
ff_hevc_end_of_slice_flag_decode
int ff_hevc_end_of_slice_flag_decode(HEVCLocalContext *lc)
Definition: cabac.c:563
ff_hevc_bump_frame
void ff_hevc_bump_frame(HEVCContext *s)
Definition: refs.c:242
PART_NxN
@ PART_NxN
Definition: hevcdec.h:94
ff_hevc_cu_qp_delta_sign_flag
int ff_hevc_cu_qp_delta_sign_flag(HEVCLocalContext *lc)
Definition: cabac.c:614
SliceHeader::slice_act_y_qp_offset
int slice_act_y_qp_offset
Definition: hevcdec.h:248
ff_get_format
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1241
ff_refstruct_pool_alloc
FFRefStructPool * ff_refstruct_pool_alloc(size_t size, unsigned flags)
Equivalent to ff_refstruct_pool_alloc(size, flags, NULL, NULL, NULL, NULL, NULL)
Definition: refstruct.c:335
out
FILE * out
Definition: movenc.c:55
HEVCWindow::bottom_offset
unsigned int bottom_offset
Definition: ps.h:93
SAO_BAND
@ SAO_BAND
Definition: hevcdec.h:160
HEVCParamSets::pps_list
const HEVCPPS * pps_list[HEVC_MAX_PPS_COUNT]
RefStruct references.
Definition: ps.h:449
ff_hevc_profiles
const AVProfile ff_hevc_profiles[]
Definition: profiles.c:97
ff_hevc_set_neighbour_available
void ff_hevc_set_neighbour_available(HEVCLocalContext *lc, int x0, int y0, int nPbW, int nPbH, int log2_ctb_size)
Definition: mvs.c:43
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:56
chroma_mc_uni
static void chroma_mc_uni(HEVCLocalContext *lc, const HEVCPPS *pps, const HEVCSPS *sps, uint8_t *dst0, ptrdiff_t dststride, const uint8_t *src0, ptrdiff_t srcstride, int reflist, int x_off, int y_off, int block_w, int block_h, const struct MvField *current_mv, int chroma_weight, int chroma_offset)
8.5.3.2.2.2 Chroma sample uniprediction interpolation process
Definition: hevcdec.c:1661
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:124
ff_h2645_sei_to_frame
int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei, enum AVCodecID codec_id, AVCodecContext *avctx, const H2645VUI *vui, unsigned bit_depth_luma, unsigned bit_depth_chroma, int seed)
Definition: h2645_sei.c:672
src1
const pixel * src1
Definition: h264pred_template.c:421
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
HEVCLocalContext::ctb_up_flag
uint8_t ctb_up_flag
Definition: hevcdec.h:421
ff_dovi_rpu_parse
int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size, int err_recognition)
Parse the contents of a Dovi RPU NAL and update the parsed values in the DOVIContext struct.
Definition: dovi_rpudec.c:304
HEVCFrame::needs_fg
int needs_fg
Definition: hevcdec.h:365
hevc_await_progress
static void hevc_await_progress(const HEVCContext *s, const HEVCFrame *ref, const Mv *mv, int y0, int height)
Definition: hevcdec.c:1824
int64_t
long long int64_t
Definition: coverity.c:34
mv
static const int8_t mv[256][2]
Definition: 4xm.c:81
SliceHeader::num_entry_point_offsets
int num_entry_point_offsets
Definition: hevcdec.h:263
HEVCFrame::frame_grain
AVFrame * frame_grain
Definition: hevcdec.h:364
ff_hevc_decode_short_term_rps
int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx, ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
Definition: ps.c:95
hls_slice_header
static int hls_slice_header(SliceHeader *sh, const HEVCContext *s, GetBitContext *gb)
Definition: hevcdec.c:587
PART_2NxnU
@ PART_2NxnU
Definition: hevcdec.h:95
av_unused
#define av_unused
Definition: attributes.h:131
AV_FRAME_DATA_S12M_TIMECODE
@ AV_FRAME_DATA_S12M_TIMECODE
Timecode which conforms to SMPTE ST 12-1.
Definition: frame.h:152
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
ff_hevc_flush_dpb
void ff_hevc_flush_dpb(HEVCContext *s)
Drop all frames currently in DPB.
Definition: refs.c:70
H2645NAL::nuh_layer_id
int nuh_layer_id
Definition: h2645_parse.h:67
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
pixdesc.h
set_deblocking_bypass
static void set_deblocking_bypass(const HEVCContext *s, const HEVCSPS *sps, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:1301
HEVCFrame::tab_mvf
MvField * tab_mvf
RefStruct reference.
Definition: hevcdec.h:366
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:678
TransformUnit::cu_qp_delta
int cu_qp_delta
Definition: hevcdec.h:329
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:384
w
uint8_t w
Definition: llviddspenc.c:38
HWACCEL_DXVA2
#define HWACCEL_DXVA2(codec)
Definition: hwconfig.h:64
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:686
HEVCFrame::hwaccel_picture_private
void * hwaccel_picture_private
RefStruct reference.
Definition: hevcdec.h:375
AVPacket::data
uint8_t * data
Definition: packet.h:533
HEVC_NAL_BLA_N_LP
@ HEVC_NAL_BLA_N_LP
Definition: hevc.h:47
SAOParams::offset_sign
int offset_sign[3][4]
sao_offset_sign
Definition: dsp.h:36
INTRA_DC
@ INTRA_DC
Definition: hevcdec.h:122
AVOption
AVOption.
Definition: opt.h:357
HWACCEL_D3D11VA2
#define HWACCEL_D3D11VA2(codec)
Definition: hwconfig.h:66
ff_h2645_packet_uninit
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:599
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:583
ff_hevc_pred_mode_decode
int ff_hevc_pred_mode_decode(HEVCLocalContext *lc)
Definition: cabac.c:635
HEVCContext::no_rasl_output_flag
int no_rasl_output_flag
Definition: hevcdec.h:492
data
const char data[16]
Definition: mxf.c:148
Mv::y
int16_t y
vertical component of motion vector
Definition: hevcdec.h:300
HEVC_NAL_TSA_N
@ HEVC_NAL_TSA_N
Definition: hevc.h:31
AV_FRAME_DATA_DOVI_RPU_BUFFER
@ AV_FRAME_DATA_DOVI_RPU_BUFFER
Dolby Vision RPU raw data, suitable for passing to x265 or other libraries.
Definition: frame.h:201
HEVC_NAL_IDR_N_LP
@ HEVC_NAL_IDR_N_LP
Definition: hevc.h:49
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:478
SAO_EDGE
@ SAO_EDGE
Definition: hevcdec.h:161
atomic_int
intptr_t atomic_int
Definition: stdatomic.h:55
ff_aom_apply_film_grain
int ff_aom_apply_film_grain(AVFrame *out, const AVFrame *in, const AVFilmGrainParams *params)
Definition: aom_film_grain.c:66
SliceHeader::slice_temporal_mvp_enabled_flag
uint8_t slice_temporal_mvp_enabled_flag
Definition: hevcdec.h:230
AV_PIX_FMT_D3D11VA_VLD
@ AV_PIX_FMT_D3D11VA_VLD
HW decoding through Direct3D11 via old API, Picture.data[3] contains a ID3D11VideoDecoderOutputView p...
Definition: pixfmt.h:254
TransformUnit::is_cu_qp_delta_coded
uint8_t is_cu_qp_delta_coded
Definition: hevcdec.h:337
FFCodec
Definition: codec_internal.h:126
ff_hevc_set_new_ref
int ff_hevc_set_new_ref(HEVCContext *s, int poc)
Definition: refs.c:125
FF_HW_SIMPLE_CALL
#define FF_HW_SIMPLE_CALL(avctx, function)
Definition: hwaccel_internal.h:174
SliceHeader::slice_loop_filter_across_slices_enabled_flag
uint8_t slice_loop_filter_across_slices_enabled_flag
Definition: hevcdec.h:239
ff_hevc_sao_type_idx_decode
int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc)
Definition: cabac.c:521
hls_transform_unit
static int hls_transform_unit(HEVCLocalContext *lc, const HEVCPPS *pps, const HEVCSPS *sps, int x0, int y0, int xBase, int yBase, int cb_xBase, int cb_yBase, int log2_cb_size, int log2_trafo_size, int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
Definition: hevcdec.c:1090
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
HEVCLocalContext::ctb_up_left_flag
uint8_t ctb_up_left_flag
Definition: hevcdec.h:423
HEVC_NAL_IDR_W_RADL
@ HEVC_NAL_IDR_W_RADL
Definition: hevc.h:48
decode_slice_data
static int decode_slice_data(HEVCContext *s, const H2645NAL *nal, GetBitContext *gb)
Definition: hevcdec.c:2763
H2645NAL::temporal_id
int temporal_id
HEVC only, nuh_temporal_id_plus_1 - 1.
Definition: h2645_parse.h:62
av_timecode_get_smpte
uint32_t av_timecode_get_smpte(AVRational rate, int drop, int hh, int mm, int ss, int ff)
Convert sei info to SMPTE 12M binary representation.
Definition: timecode.c:69
RefPicList
Definition: hevcdec.h:190
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:103
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
ff_hevc_prev_intra_luma_pred_flag_decode
int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCLocalContext *lc)
Definition: cabac.c:703
ff_hevc_output_frame
int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush)
Find next frame in output order and put a reference to it in frame.
Definition: refs.c:174
PF_INTRA
@ PF_INTRA
Definition: hevcdec.h:114
AV_PIX_FMT_VULKAN
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition: pixfmt.h:379
ff_hevc_decode_nal_sps
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps, int apply_defdispwin)
Definition: ps.c:1308
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:395
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
verify_md5
static int verify_md5(HEVCContext *s, AVFrame *frame)
Definition: hevcdec.c:3038
intra_prediction_unit
static void intra_prediction_unit(HEVCLocalContext *lc, const HEVCSPS *sps, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:2108
MODE_SKIP
@ MODE_SKIP
Definition: hevcdec.h:104
HEVCLocalContext::end_of_tiles_x
int end_of_tiles_x
Definition: hevcdec.h:424
ff_hevc_deblocking_boundary_strengths
void ff_hevc_deblocking_boundary_strengths(HEVCLocalContext *lc, const HEVCPPS *pps, int x0, int y0, int log2_trafo_size)
Definition: filter.c:739
AV_PKT_DATA_DOVI_CONF
@ AV_PKT_DATA_DOVI_CONF
DOVI configuration ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2....
Definition: packet.h:280
CodingUnit::x
int x
Definition: hevcdec.h:286
tab_mode_idx
static const uint8_t tab_mode_idx[]
Definition: hevcdec.c:2104
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
BOUNDARY_LEFT_TILE
#define BOUNDARY_LEFT_TILE
Definition: hevcdec.h:438
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:560
golomb.h
exp golomb vlc stuff
hls_coding_unit
static int hls_coding_unit(HEVCLocalContext *lc, const HEVCContext *s, const HEVCPPS *pps, const HEVCSPS *sps, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:2200
AVCodecInternal::is_copy
int is_copy
When using frame-threaded decoding, this field is set for the first worker thread (e....
Definition: internal.h:54
AVPacketSideData::size
size_t size
Definition: packet.h:386
PART_2Nx2N
@ PART_2Nx2N
Definition: hevcdec.h:91
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
hevc_decode_free
static av_cold int hevc_decode_free(AVCodecContext *avctx)
Definition: hevcdec.c:3481
HEVCLocalContext::ctb_up_right_flag
uint8_t ctb_up_right_flag
Definition: hevcdec.h:422
hls_prediction_unit
static void hls_prediction_unit(HEVCLocalContext *lc, const HEVCPPS *pps, const HEVCSPS *sps, int x0, int y0, int nPbW, int nPbH, int log2_cb_size, int partIdx, int idx)
Definition: hevcdec.c:1881
ff_hevc_decoder
const FFCodec ff_hevc_decoder
Definition: hevcdec.c:3715
ff_hevc_compute_poc
int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
Compute POC of the current frame and return it.
Definition: ps.c:2056
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
PRED_BI
@ PRED_BI
Definition: hevcdec.h:110
ff_hevc_split_transform_flag_decode
int ff_hevc_split_transform_flag_decode(HEVCLocalContext *lc, int log2_trafo_size)
Definition: cabac.c:822
av_ceil_log2
#define av_ceil_log2
Definition: common.h:97
fail
#define fail()
Definition: checkasm.h:186
PredictionUnit::intra_pred_mode_c
uint8_t intra_pred_mode_c[4]
Definition: hevcdec.h:324
H2645SEI::dynamic_hdr_vivid
HEVCSEIDynamicHDRVivid dynamic_hdr_vivid
Definition: h2645_sei.h:128
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1583
ff_refstruct_pool_uninit
static void ff_refstruct_pool_uninit(FFRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition: refstruct.h:292
md5
struct AVMD5 * md5
Definition: movenc.c:57
InterPredIdc
InterPredIdc
Definition: hevcdec.h:107
MODE_INTER
@ MODE_INTER
Definition: hevcdec.h:102
timecode.h
HEVCWindow::left_offset
unsigned int left_offset
Definition: ps.h:90
GetBitContext
Definition: get_bits.h:108
SliceHeader::luma_log2_weight_denom
uint8_t luma_log2_weight_denom
Definition: hevcdec.h:267
ff_hevc_hls_mvd_coding
void ff_hevc_hls_mvd_coding(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size)
Definition: cabac.c:1494
HEVCLocalContext::pu
PredictionUnit pu
Definition: hevcdec.h:434
HEVC_NAL_SEI_SUFFIX
@ HEVC_NAL_SEI_SUFFIX
Definition: hevc.h:69
av_film_grain_params_select
const AVFilmGrainParams * av_film_grain_params_select(const AVFrame *frame)
Select the most appropriate film grain parameters set for the frame, taking into account the frame's ...
Definition: film_grain_params.c:53
TransformUnit::res_scale_val
int res_scale_val
Definition: hevcdec.h:331
SliceHeader::short_term_ref_pic_set_size
int short_term_ref_pic_set_size
Definition: hevcdec.h:221
ff_hevc_cu_chroma_qp_offset_idx
int ff_hevc_cu_chroma_qp_offset_idx(HEVCLocalContext *lc, int chroma_qp_offset_list_len_minus1)
Definition: cabac.c:624
val
static double val(void *priv, double ch)
Definition: aeval.c:78
ff_hevc_sao_offset_abs_decode
int ff_hevc_sao_offset_abs_decode(HEVCLocalContext *lc, int bit_depth)
Definition: cabac.c:541
HWACCEL_VDPAU
#define HWACCEL_VDPAU(codec)
Definition: hwconfig.h:72
ff_videodsp_init
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:39
HEVC_MAX_PPS_COUNT
@ HEVC_MAX_PPS_COUNT
Definition: hevc.h:114
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:633
SliceHeader::long_term_ref_pic_set_size
int long_term_ref_pic_set_size
Definition: hevcdec.h:224
hls_cross_component_pred
static int hls_cross_component_pred(HEVCLocalContext *lc, int idx)
Definition: hevcdec.c:1074
HEVCParamSets::sps_list
const HEVCSPS * sps_list[HEVC_MAX_SPS_COUNT]
RefStruct references.
Definition: ps.h:448
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:202
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
ff_hevc_luma_mv_merge_mode
void ff_hevc_luma_mv_merge_mode(HEVCLocalContext *lc, const HEVCPPS *pps, int x0, int y0, int nPbW, int nPbH, int log2_cb_size, int part_idx, int merge_idx, MvField *mv)
Definition: mvs.c:482
AVRational::num
int num
Numerator.
Definition: rational.h:59
progressframe.h
refstruct.h
ff_hevc_pred_init
void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth)
Definition: pred.c:43
SliceHeader::slice_segment_addr
unsigned int slice_segment_addr
address (in raster order) of the first block in the current slice
Definition: hevcdec.h:205
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:148
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:481
ff_thread_report_progress2
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
Definition: pthread_slice.c:210
first
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
Definition: rate_distortion.txt:12
FF_CODEC_CAP_USES_PROGRESSFRAMES
#define FF_CODEC_CAP_USES_PROGRESSFRAMES
The decoder might make use of the ProgressFrame API.
Definition: codec_internal.h:68
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:671
ff_hevc_mvp_lx_flag_decode
int ff_hevc_mvp_lx_flag_decode(HEVCLocalContext *lc)
Definition: cabac.c:779
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
HEVC_SLICE_B
@ HEVC_SLICE_B
Definition: hevc.h:96
set_side_data
static int set_side_data(HEVCContext *s)
Definition: hevcdec.c:2816
QPEL_EXTRA_AFTER
#define QPEL_EXTRA_AFTER
Definition: hevcdec.h:65
film_grain_params.h
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
luma_mc_uni
static void luma_mc_uni(HEVCLocalContext *lc, const HEVCPPS *pps, const HEVCSPS *sps, uint8_t *dst, ptrdiff_t dststride, const AVFrame *ref, const Mv *mv, int x_off, int y_off, int block_w, int block_h, int luma_weight, int luma_offset)
8.5.3.2.2.1 Luma sample unidirectional interpolation process
Definition: hevcdec.c:1503
TransformUnit::intra_pred_mode
int intra_pred_mode
Definition: hevcdec.h:334
HEVCSEIDynamicHDRPlus::info
AVBufferRef * info
Definition: h2645_sei.h:46
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
hls_coding_quadtree
static int hls_coding_quadtree(HEVCLocalContext *lc, const HEVCPPS *pps, const HEVCSPS *sps, int x0, int y0, int log2_cb_size, int cb_depth)
Definition: hevcdec.c:2385
SliceHeader::cabac_init_flag
uint8_t cabac_init_flag
Definition: hevcdec.h:237
H2645NAL::size
int size
Definition: h2645_parse.h:36
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:723
ff_progress_frame_ref
void ff_progress_frame_ref(ProgressFrame *dst, const ProgressFrame *src)
Set dst->f to src->f and make dst a co-owner of src->f.
Definition: decode.c:1734
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
QPEL_EXTRA_BEFORE
#define QPEL_EXTRA_BEFORE
Definition: hevcdec.h:64
AV_PIX_FMT_DXVA2_VLD
@ AV_PIX_FMT_DXVA2_VLD
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer.
Definition: pixfmt.h:134
HEVCLocalContext::parent
const struct HEVCContext * parent
Definition: hevcdec.h:397
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_thread_await_progress2
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
Definition: pthread_slice.c:222
SAO_NOT_APPLIED
@ SAO_NOT_APPLIED
Definition: hevcdec.h:159
AV_PROFILE_HEVC_SCC
#define AV_PROFILE_HEVC_SCC
Definition: defs.h:163
set_ct_depth
static av_always_inline void set_ct_depth(const HEVCSPS *sps, uint8_t *tab_ct_depth, int x0, int y0, int log2_cb_size, int ct_depth)
Definition: hevcdec.c:2090
parse.h
ff_hevc_part_mode_decode
int ff_hevc_part_mode_decode(HEVCLocalContext *lc, const HEVCSPS *sps, int log2_cb_size)
Definition: cabac.c:661
SliceHeader::luma_offset_l1
int16_t luma_offset_l1[16]
Definition: hevcdec.h:278
AV_ZERO32
#define AV_ZERO32(d)
Definition: intreadwrite.h:621
ff_hevc_merge_idx_decode
int ff_hevc_merge_idx_decode(HEVCLocalContext *lc)
Definition: cabac.c:737
HEVCSEI::common
H2645SEI common
Definition: sei.h:83
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
ff_hevc_nal_is_nonref
static av_always_inline int ff_hevc_nal_is_nonref(enum HEVCNALUnitType type)
Definition: hevcdec.h:618
ff_thread_get_buffer
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:986
HEVC_NAL_VPS
@ HEVC_NAL_VPS
Definition: hevc.h:61
HEVC_NAL_UNSPEC62
@ HEVC_NAL_UNSPEC62
Definition: hevc.h:91
export_stream_params_from_sei
static int export_stream_params_from_sei(HEVCContext *s)
Definition: hevcdec.c:376
hls_decode_entry_wpp
static int hls_decode_entry_wpp(AVCodecContext *avctx, void *hevc_lclist, int job, int self_id)
Definition: hevcdec.c:2575
ff_hevc_save_states
void ff_hevc_save_states(HEVCLocalContext *lc, const HEVCPPS *pps, int ctb_addr_ts)
Definition: cabac.c:402
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
ff_hevc_frame_rps
int ff_hevc_frame_rps(HEVCContext *s)
Construct the reference picture sets for the current frame.
Definition: refs.c:467
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:304
SliceHeader::slice_rps
ShortTermRPS slice_rps
Definition: hevcdec.h:222
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
AVPacketSideData::data
uint8_t * data
Definition: packet.h:385
HEVCFrame::rpl
RefPicListTab * rpl
RefStruct reference.
Definition: hevcdec.h:372
ff_progress_frame_await
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before ff_progress_frame_await() has been called on them. reget_buffer() and buffer age optimizations no longer work. *The contents of buffers must not be written to after ff_progress_frame_report() has been called on them. This includes draw_edges(). Porting codecs to frame threading
decode.h
ff_hevc_cbf_cb_cr_decode
int ff_hevc_cbf_cb_cr_decode(HEVCLocalContext *lc, int trafo_depth)
Definition: cabac.c:827
hevc.h
IS_IDR
#define IS_IDR(s)
Definition: hevcdec.h:76
H2645NAL::data
const uint8_t * data
Definition: h2645_parse.h:35
RefPicList::ref
struct HEVCFrame * ref[HEVC_MAX_REFS]
Definition: hevcdec.h:191
H2645NAL::skipped_bytes_pos
int * skipped_bytes_pos
Definition: h2645_parse.h:71
HEVCWindow::top_offset
unsigned int top_offset
Definition: ps.h:92
export_stream_params
static void export_stream_params(HEVCContext *s, const HEVCSPS *sps)
Definition: hevcdec.c:320
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
SliceHeader::size
int * size
Definition: hevcdec.h:262
SliceHeader::collocated_list
uint8_t collocated_list
Definition: hevcdec.h:240
atomic_load
#define atomic_load(object)
Definition: stdatomic.h:93
SliceHeader::luma_offset_l0
int16_t luma_offset_l0[16]
Definition: hevcdec.h:275
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:558
AV_FILM_GRAIN_PARAMS_NONE
@ AV_FILM_GRAIN_PARAMS_NONE
Definition: film_grain_params.h:25
AVDISCARD_BIDIR
@ AVDISCARD_BIDIR
discard all bidirectional frames
Definition: defs.h:217
get_se_golomb
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:239
INTRA_ANGULAR_26
@ INTRA_ANGULAR_26
Definition: hevcdec.h:147
H2645NAL::type
int type
NAL unit type.
Definition: h2645_parse.h:52
SliceHeader::chroma_weight_l1
int16_t chroma_weight_l1[16][2]
Definition: hevcdec.h:272
CodingUnit::max_trafo_depth
uint8_t max_trafo_depth
MaxTrafoDepth.
Definition: hevcdec.h:294
AV_FRAME_DATA_DYNAMIC_HDR_VIVID
@ AV_FRAME_DATA_DYNAMIC_HDR_VIVID
HDR Vivid dynamic metadata associated with a video frame.
Definition: frame.h:215
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
SliceHeader::slice_ctb_addr_rs
int slice_ctb_addr_rs
Definition: hevcdec.h:281
SliceHeader::poc
int poc
Definition: hevcdec.h:212
ff_hevc_hls_residual_coding
void ff_hevc_hls_residual_coding(HEVCLocalContext *lc, const HEVCPPS *pps, int x0, int y0, int log2_trafo_size, enum ScanType scan_idx, int c_idx)
Definition: cabac.c:982
ff_hevc_decode_nal_vps
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: ps.c:454
FF_CODEC_PROPERTY_FILM_GRAIN
#define FF_CODEC_PROPERTY_FILM_GRAIN
Definition: avcodec.h:1799
hevc_ref_frame
static int hevc_ref_frame(HEVCFrame *dst, const HEVCFrame *src)
Definition: hevcdec.c:3450
if
if(ret)
Definition: filter_design.txt:179
hevc_decode_init
static av_cold int hevc_decode_init(AVCodecContext *avctx)
Definition: hevcdec.c:3642
SliceHeader::pic_output_flag
uint8_t pic_output_flag
Definition: hevcdec.h:216
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:220
GetBitContext::buffer
const uint8_t * buffer
Definition: get_bits.h:109
pred_weight_table
static int pred_weight_table(SliceHeader *sh, void *logctx, const HEVCSPS *sps, GetBitContext *gb)
Definition: hevcdec.c:146
PredictionUnit::rem_intra_luma_pred_mode
int rem_intra_luma_pred_mode
Definition: hevcdec.h:320
HEVCContext::sei
HEVCSEI sei
Definition: hevcdec.h:465
H2645NAL::raw_size
int raw_size
Definition: h2645_parse.h:44
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
IS_BLA
#define IS_BLA(s)
Definition: hevcdec.h:77
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_bswapdsp_init
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
pic_arrays_free
static void pic_arrays_free(HEVCContext *s)
NOTE: Each function hls_foo correspond to the function foo in the specification (HLS stands for High ...
Definition: hevcdec.c:68
NULL
#define NULL
Definition: coverity.c:32
HEVC_SEQUENCE_COUNTER_MASK
#define HEVC_SEQUENCE_COUNTER_MASK
Definition: hevcdec.h:354
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
SAOParams::offset_abs
int offset_abs[3][4]
sao_offset_abs
Definition: dsp.h:35
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:695
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
HEVCLocalContext::tmp
int16_t tmp[MAX_PB_SIZE *MAX_PB_SIZE]
Definition: hevcdec.h:430
CTB
#define CTB(tab, x, y)
Definition: hevcdec.c:984
hwaccel_internal.h
LongTermRPS::poc
int poc[32]
Definition: hevcdec.h:184
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:707
CodingUnit::cu_transquant_bypass_flag
uint8_t cu_transquant_bypass_flag
Definition: hevcdec.h:295
HEVC_SLICE_I
@ HEVC_SLICE_I
Definition: hevc.h:98
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:480
ff_hevc_decode_nal_pps
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: ps.c:1769
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:85
HEVCLocalContext::first_qp_group
uint8_t first_qp_group
Definition: hevcdec.h:394
ff_h2645_sei_to_context
int ff_h2645_sei_to_context(AVCodecContext *avctx, H2645SEI *sei)
Definition: h2645_sei.c:862
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
HEVC_NAL_STSA_N
@ HEVC_NAL_STSA_N
Definition: hevc.h:33
profiles.h
ff_hevc_reset_sei
static void ff_hevc_reset_sei(HEVCSEI *sei)
Reset SEI values that are stored on the Context.
Definition: sei.h:107
ff_hevc_res_scale_sign_flag
int ff_hevc_res_scale_sign_flag(HEVCLocalContext *lc, int idx)
Definition: cabac.c:862
ff_set_sar
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:109
L0
#define L0
Definition: hevcdec.h:58
HEVCFrame::rpl_tab
RefPicListTab ** rpl_tab
RefStruct reference.
Definition: hevcdec.h:368
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:368
LongTermRPS::poc_msb_present
uint8_t poc_msb_present[32]
Definition: hevcdec.h:185
HEVC_NAL_EOS_NUT
@ HEVC_NAL_EOS_NUT
Definition: hevc.h:65
HEVCContext::film_grain_warning_shown
int film_grain_warning_shown
Definition: hevcdec.h:546
av_frame_new_side_data_from_buf
AVFrameSideData * av_frame_new_side_data_from_buf(AVFrame *frame, enum AVFrameSideDataType type, AVBufferRef *buf)
Add a new side data to a frame from an existing AVBufferRef.
Definition: frame.c:786
HEVCContext::DPB
HEVCFrame DPB[32]
Definition: hevcdec.h:482
PART_Nx2N
@ PART_Nx2N
Definition: hevcdec.h:93
ff_hevc_ref_idx_lx_decode
int ff_hevc_ref_idx_lx_decode(HEVCLocalContext *lc, int num_ref_idx_lx)
Definition: cabac.c:763
RefPicListTab
Definition: hevcdec.h:197
UPDATE_THREAD_CONTEXT
#define UPDATE_THREAD_CONTEXT(func)
Definition: codec_internal.h:280
BOUNDARY_UPPER_TILE
#define BOUNDARY_UPPER_TILE
Definition: hevcdec.h:440
AV_PIX_FMT_D3D12
@ AV_PIX_FMT_D3D12
Hardware surfaces for Direct3D 12.
Definition: pixfmt.h:440
aom_film_grain.h
hls_pcm_sample
static int hls_pcm_sample(HEVCLocalContext *lc, const HEVCPPS *pps, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:1444
vps
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
Definition: cbs_h265_syntax_template.c:423
HEVCContext::is_nalff
int is_nalff
this flag is != 0 if bitstream is encapsulated as a format defined in 14496-15
Definition: hevcdec.h:539
ff_hevc_hls_filters
void ff_hevc_hls_filters(HEVCLocalContext *lc, const HEVCPPS *pps, int x_ctb, int y_ctb, int ctb_size)
Definition: filter.c:907
AV_EF_CRCCHECK
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition: defs.h:48
FF_HW_HAS_CB
#define FF_HW_HAS_CB(avctx, function)
Definition: hwaccel_internal.h:177
SliceHeader::nb_refs
unsigned int nb_refs[2]
Definition: hevcdec.h:232
Mv::x
int16_t x
horizontal component of motion vector
Definition: hevcdec.h:299
ff_slice_thread_init_progress
int av_cold ff_slice_thread_init_progress(AVCodecContext *avctx)
Definition: pthread_slice.c:179
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1784
PF_BI
@ PF_BI
Definition: hevcdec.h:117
ff_hevc_sao_eo_class_decode
int ff_hevc_sao_eo_class_decode(HEVCLocalContext *lc)
Definition: cabac.c:556
H2645SEI::display_orientation
H2645SEIDisplayOrientation display_orientation
Definition: h2645_sei.h:131
SAMPLE_CTB
#define SAMPLE_CTB(tab, x, y)
Definition: hevcdec.h:74
HEVCWindow
Definition: ps.h:89
SCAN_HORIZ
@ SCAN_HORIZ
Definition: hevcdec.h:174
PAR
#define PAR
Definition: hevcdec.c:3698
HEVCLocalContext::edge_emu_buffer
uint8_t edge_emu_buffer[(MAX_PB_SIZE+7) *EDGE_EMU_BUFFER_STRIDE *2]
Definition: hevcdec.h:427
IS_IRAP
#define IS_IRAP(s)
Definition: hevcdec.h:79
SAOParams::offset_val
int16_t offset_val[3][5]
SaoOffsetVal.
Definition: dsp.h:42
LongTermRPS::used
uint8_t used[32]
Definition: hevcdec.h:186
HEVC_NAL_RASL_R
@ HEVC_NAL_RASL_R
Definition: hevc.h:38
SliceHeader::colour_plane_id
uint8_t colour_plane_id
RPS coded in the slice header itself is stored here.
Definition: hevcdec.h:217
PART_nLx2N
@ PART_nLx2N
Definition: hevcdec.h:97
ff_hevc_cabac_init
int ff_hevc_cabac_init(HEVCLocalContext *lc, const HEVCPPS *pps, int ctb_addr_ts, const uint8_t *data, size_t size, int is_wpp)
Definition: cabac.c:454
HEVCContext::nal_length_size
int nal_length_size
Number of bytes used for nal length (1, 2 or 4)
Definition: hevcdec.h:543
luma_mc_bi
static void luma_mc_bi(HEVCLocalContext *lc, const HEVCPPS *pps, const HEVCSPS *sps, uint8_t *dst, ptrdiff_t dststride, const AVFrame *ref0, const Mv *mv0, int x_off, int y_off, int block_w, int block_h, const AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
8.5.3.2.2.1 Luma sample bidirectional interpolation process
Definition: hevcdec.c:1567
SliceHeader::dependent_slice_segment_flag
uint8_t dependent_slice_segment_flag
Definition: hevcdec.h:215
SliceHeader::slice_act_cb_qp_offset
int slice_act_cb_qp_offset
Definition: hevcdec.h:249
AVDISCARD_NONKEY
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: defs.h:219
SliceHeader::first_slice_in_pic_flag
uint8_t first_slice_in_pic_flag
Definition: hevcdec.h:214
HEVCLocalContext::ctb_left_flag
uint8_t ctb_left_flag
Definition: hevcdec.h:420
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
ff_dovi_ctx_flush
void ff_dovi_ctx_flush(DOVIContext *s)
Partially reset the internal state.
Definition: dovi_rpu.c:42
H2645SEI::frame_packing
H2645SEIFramePacking frame_packing
Definition: h2645_sei.h:130
AVPacket::size
int size
Definition: packet.h:534
BOUNDARY_UPPER_SLICE
#define BOUNDARY_UPPER_SLICE
Definition: hevcdec.h:439
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94