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