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/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/timecode.h"
37 
38 #include "aom_film_grain.h"
39 #include "bswapdsp.h"
40 #include "cabac_functions.h"
41 #include "codec_internal.h"
42 #include "decode.h"
43 #include "golomb.h"
44 #include "hevc.h"
45 #include "hevc_parse.h"
46 #include "hevcdec.h"
47 #include "hwaccel_internal.h"
48 #include "hwconfig.h"
49 #include "internal.h"
50 #include "profiles.h"
51 #include "refstruct.h"
52 #include "thread.h"
53 #include "threadframe.h"
54 
55 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 };
56 
57 /**
58  * NOTE: Each function hls_foo correspond to the function foo in the
59  * specification (HLS stands for High Level Syntax).
60  */
61 
62 /**
63  * Section 5.7
64  */
65 
66 /* free everything allocated by pic_arrays_init() */
68 {
69  av_freep(&s->sao);
70  av_freep(&s->deblock);
71 
72  av_freep(&s->skip_flag);
73  av_freep(&s->tab_ct_depth);
74 
75  av_freep(&s->tab_ipm);
76  av_freep(&s->cbf_luma);
77  av_freep(&s->is_pcm);
78 
79  av_freep(&s->qp_y_tab);
80  av_freep(&s->tab_slice_address);
81  av_freep(&s->filter_slice_edges);
82 
83  av_freep(&s->horizontal_bs);
84  av_freep(&s->vertical_bs);
85 
86  av_freep(&s->sh.entry_point_offset);
87  av_freep(&s->sh.size);
88  av_freep(&s->sh.offset);
89 
90  ff_refstruct_pool_uninit(&s->tab_mvf_pool);
91  ff_refstruct_pool_uninit(&s->rpl_tab_pool);
92 }
93 
94 /* allocate arrays that depend on frame dimensions */
95 static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
96 {
97  int log2_min_cb_size = sps->log2_min_cb_size;
98  int width = sps->width;
99  int height = sps->height;
100  int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
101  ((height >> log2_min_cb_size) + 1);
102  int ctb_count = sps->ctb_width * sps->ctb_height;
103  int min_pu_size = sps->min_pu_width * sps->min_pu_height;
104 
105  s->bs_width = (width >> 2) + 1;
106  s->bs_height = (height >> 2) + 1;
107 
108  s->sao = av_calloc(ctb_count, sizeof(*s->sao));
109  s->deblock = av_calloc(ctb_count, sizeof(*s->deblock));
110  if (!s->sao || !s->deblock)
111  goto fail;
112 
113  s->skip_flag = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
114  s->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
115  if (!s->skip_flag || !s->tab_ct_depth)
116  goto fail;
117 
118  s->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
119  s->tab_ipm = av_mallocz(min_pu_size);
120  s->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
121  if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
122  goto fail;
123 
124  s->filter_slice_edges = av_mallocz(ctb_count);
125  s->tab_slice_address = av_malloc_array(pic_size_in_ctb,
126  sizeof(*s->tab_slice_address));
127  s->qp_y_tab = av_malloc_array(pic_size_in_ctb,
128  sizeof(*s->qp_y_tab));
129  if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
130  goto fail;
131 
132  s->horizontal_bs = av_calloc(s->bs_width, s->bs_height);
133  s->vertical_bs = av_calloc(s->bs_width, s->bs_height);
134  if (!s->horizontal_bs || !s->vertical_bs)
135  goto fail;
136 
137  s->tab_mvf_pool = ff_refstruct_pool_alloc(min_pu_size * sizeof(MvField), 0);
138  s->rpl_tab_pool = ff_refstruct_pool_alloc(ctb_count * sizeof(RefPicListTab), 0);
139  if (!s->tab_mvf_pool || !s->rpl_tab_pool)
140  goto fail;
141 
142  return 0;
143 
144 fail:
146  return AVERROR(ENOMEM);
147 }
148 
150 {
151  int i = 0;
152  int j = 0;
153  uint8_t luma_weight_l0_flag[16];
154  uint8_t chroma_weight_l0_flag[16];
155  uint8_t luma_weight_l1_flag[16];
156  uint8_t chroma_weight_l1_flag[16];
157  int luma_log2_weight_denom;
158 
159  luma_log2_weight_denom = get_ue_golomb_long(gb);
160  if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7) {
161  av_log(s->avctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
162  return AVERROR_INVALIDDATA;
163  }
164  s->sh.luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
165  if (s->ps.sps->chroma_format_idc != 0) {
166  int64_t chroma_log2_weight_denom = luma_log2_weight_denom + (int64_t)get_se_golomb(gb);
167  if (chroma_log2_weight_denom < 0 || chroma_log2_weight_denom > 7) {
168  av_log(s->avctx, AV_LOG_ERROR, "chroma_log2_weight_denom %"PRId64" is invalid\n", chroma_log2_weight_denom);
169  return AVERROR_INVALIDDATA;
170  }
171  s->sh.chroma_log2_weight_denom = chroma_log2_weight_denom;
172  }
173 
174  for (i = 0; i < s->sh.nb_refs[L0]; i++) {
175  luma_weight_l0_flag[i] = get_bits1(gb);
176  if (!luma_weight_l0_flag[i]) {
177  s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
178  s->sh.luma_offset_l0[i] = 0;
179  }
180  }
181  if (s->ps.sps->chroma_format_idc != 0) {
182  for (i = 0; i < s->sh.nb_refs[L0]; i++)
183  chroma_weight_l0_flag[i] = get_bits1(gb);
184  } else {
185  for (i = 0; i < s->sh.nb_refs[L0]; i++)
186  chroma_weight_l0_flag[i] = 0;
187  }
188  for (i = 0; i < s->sh.nb_refs[L0]; i++) {
189  if (luma_weight_l0_flag[i]) {
190  int delta_luma_weight_l0 = get_se_golomb(gb);
191  if ((int8_t)delta_luma_weight_l0 != delta_luma_weight_l0)
192  return AVERROR_INVALIDDATA;
193  s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
194  s->sh.luma_offset_l0[i] = get_se_golomb(gb);
195  }
196  if (chroma_weight_l0_flag[i]) {
197  for (j = 0; j < 2; j++) {
198  int delta_chroma_weight_l0 = get_se_golomb(gb);
199  int delta_chroma_offset_l0 = get_se_golomb(gb);
200 
201  if ( (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
202  || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
203  return AVERROR_INVALIDDATA;
204  }
205 
206  s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
207  s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
208  >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
209  }
210  } else {
211  s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
212  s->sh.chroma_offset_l0[i][0] = 0;
213  s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
214  s->sh.chroma_offset_l0[i][1] = 0;
215  }
216  }
217  if (s->sh.slice_type == HEVC_SLICE_B) {
218  for (i = 0; i < s->sh.nb_refs[L1]; i++) {
219  luma_weight_l1_flag[i] = get_bits1(gb);
220  if (!luma_weight_l1_flag[i]) {
221  s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
222  s->sh.luma_offset_l1[i] = 0;
223  }
224  }
225  if (s->ps.sps->chroma_format_idc != 0) {
226  for (i = 0; i < s->sh.nb_refs[L1]; i++)
227  chroma_weight_l1_flag[i] = get_bits1(gb);
228  } else {
229  for (i = 0; i < s->sh.nb_refs[L1]; i++)
230  chroma_weight_l1_flag[i] = 0;
231  }
232  for (i = 0; i < s->sh.nb_refs[L1]; i++) {
233  if (luma_weight_l1_flag[i]) {
234  int delta_luma_weight_l1 = get_se_golomb(gb);
235  if ((int8_t)delta_luma_weight_l1 != delta_luma_weight_l1)
236  return AVERROR_INVALIDDATA;
237  s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
238  s->sh.luma_offset_l1[i] = get_se_golomb(gb);
239  }
240  if (chroma_weight_l1_flag[i]) {
241  for (j = 0; j < 2; j++) {
242  int delta_chroma_weight_l1 = get_se_golomb(gb);
243  int delta_chroma_offset_l1 = get_se_golomb(gb);
244 
245  if ( (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
246  || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
247  return AVERROR_INVALIDDATA;
248  }
249 
250  s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
251  s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
252  >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
253  }
254  } else {
255  s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
256  s->sh.chroma_offset_l1[i][0] = 0;
257  s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
258  s->sh.chroma_offset_l1[i][1] = 0;
259  }
260  }
261  }
262  return 0;
263 }
264 
266 {
267  const HEVCSPS *sps = s->ps.sps;
268  int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
269  int prev_delta_msb = 0;
270  unsigned int nb_sps = 0, nb_sh;
271  int i;
272 
273  rps->nb_refs = 0;
274  if (!sps->long_term_ref_pics_present_flag)
275  return 0;
276 
277  if (sps->num_long_term_ref_pics_sps > 0)
278  nb_sps = get_ue_golomb_long(gb);
279  nb_sh = get_ue_golomb_long(gb);
280 
281  if (nb_sps > sps->num_long_term_ref_pics_sps)
282  return AVERROR_INVALIDDATA;
283  if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
284  return AVERROR_INVALIDDATA;
285 
286  rps->nb_refs = nb_sh + nb_sps;
287 
288  for (i = 0; i < rps->nb_refs; i++) {
289 
290  if (i < nb_sps) {
291  uint8_t lt_idx_sps = 0;
292 
293  if (sps->num_long_term_ref_pics_sps > 1)
294  lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
295 
296  rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
297  rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
298  } else {
299  rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
300  rps->used[i] = get_bits1(gb);
301  }
302 
303  rps->poc_msb_present[i] = get_bits1(gb);
304  if (rps->poc_msb_present[i]) {
306  int64_t poc;
307 
308  if (i && i != nb_sps)
309  delta += prev_delta_msb;
310 
311  poc = rps->poc[i] + s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
312  if (poc != (int32_t)poc)
313  return AVERROR_INVALIDDATA;
314  rps->poc[i] = poc;
315  prev_delta_msb = delta;
316  }
317  }
318 
319  return 0;
320 }
321 
323 {
324  AVCodecContext *avctx = s->avctx;
325  const HEVCParamSets *ps = &s->ps;
326  const HEVCVPS *vps = ps->vps_list[sps->vps_id];
327  const HEVCWindow *ow = &sps->output_window;
328  unsigned int num = 0, den = 0;
329 
330  avctx->pix_fmt = sps->pix_fmt;
331  avctx->coded_width = sps->width;
332  avctx->coded_height = sps->height;
333  avctx->width = sps->width - ow->left_offset - ow->right_offset;
334  avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
335  avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
336  avctx->profile = sps->ptl.general_ptl.profile_idc;
337  avctx->level = sps->ptl.general_ptl.level_idc;
338 
339  ff_set_sar(avctx, sps->vui.common.sar);
340 
341  if (sps->vui.common.video_signal_type_present_flag)
342  avctx->color_range = sps->vui.common.video_full_range_flag ? AVCOL_RANGE_JPEG
344  else
345  avctx->color_range = AVCOL_RANGE_MPEG;
346 
347  if (sps->vui.common.colour_description_present_flag) {
348  avctx->color_primaries = sps->vui.common.colour_primaries;
349  avctx->color_trc = sps->vui.common.transfer_characteristics;
350  avctx->colorspace = sps->vui.common.matrix_coeffs;
351  } else {
355  }
356 
358  if (sps->chroma_format_idc == 1) {
359  if (sps->vui.common.chroma_loc_info_present_flag) {
360  if (sps->vui.common.chroma_sample_loc_type_top_field <= 5)
361  avctx->chroma_sample_location = sps->vui.common.chroma_sample_loc_type_top_field + 1;
362  } else
364  }
365 
366  if (vps->vps_timing_info_present_flag) {
367  num = vps->vps_num_units_in_tick;
368  den = vps->vps_time_scale;
369  } else if (sps->vui.vui_timing_info_present_flag) {
370  num = sps->vui.vui_num_units_in_tick;
371  den = sps->vui.vui_time_scale;
372  }
373 
374  if (num > 0 && den > 0)
375  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
376  num, den, 1 << 30);
377 }
378 
380 {
381  AVCodecContext *avctx = s->avctx;
382 
383  if (s->sei.common.a53_caption.buf_ref)
384  s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
385 
386  if (s->sei.common.alternative_transfer.present &&
387  av_color_transfer_name(s->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
388  s->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
389  avctx->color_trc = s->sei.common.alternative_transfer.preferred_transfer_characteristics;
390  }
391 
392  if (s->sei.common.film_grain_characteristics.present ||
393  s->sei.common.aom_film_grain.enable)
395 
396  return 0;
397 }
398 
400 {
401 #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
402  CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
403  CONFIG_HEVC_D3D12VA_HWACCEL + \
404  CONFIG_HEVC_NVDEC_HWACCEL + \
405  CONFIG_HEVC_VAAPI_HWACCEL + \
406  CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
407  CONFIG_HEVC_VDPAU_HWACCEL + \
408  CONFIG_HEVC_VULKAN_HWACCEL)
409  enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
410 
411  switch (sps->pix_fmt) {
412  case AV_PIX_FMT_YUV420P:
413  case AV_PIX_FMT_YUVJ420P:
414 #if CONFIG_HEVC_DXVA2_HWACCEL
415  *fmt++ = AV_PIX_FMT_DXVA2_VLD;
416 #endif
417 #if CONFIG_HEVC_D3D11VA_HWACCEL
418  *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
419  *fmt++ = AV_PIX_FMT_D3D11;
420 #endif
421 #if CONFIG_HEVC_D3D12VA_HWACCEL
422  *fmt++ = AV_PIX_FMT_D3D12;
423 #endif
424 #if CONFIG_HEVC_VAAPI_HWACCEL
425  *fmt++ = AV_PIX_FMT_VAAPI;
426 #endif
427 #if CONFIG_HEVC_VDPAU_HWACCEL
428  *fmt++ = AV_PIX_FMT_VDPAU;
429 #endif
430 #if CONFIG_HEVC_NVDEC_HWACCEL
431  *fmt++ = AV_PIX_FMT_CUDA;
432 #endif
433 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
434  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
435 #endif
436 #if CONFIG_HEVC_VULKAN_HWACCEL
437  *fmt++ = AV_PIX_FMT_VULKAN;
438 #endif
439  break;
441 #if CONFIG_HEVC_DXVA2_HWACCEL
442  *fmt++ = AV_PIX_FMT_DXVA2_VLD;
443 #endif
444 #if CONFIG_HEVC_D3D11VA_HWACCEL
445  *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
446  *fmt++ = AV_PIX_FMT_D3D11;
447 #endif
448 #if CONFIG_HEVC_D3D12VA_HWACCEL
449  *fmt++ = AV_PIX_FMT_D3D12;
450 #endif
451 #if CONFIG_HEVC_VAAPI_HWACCEL
452  *fmt++ = AV_PIX_FMT_VAAPI;
453 #endif
454 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
455  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
456 #endif
457 #if CONFIG_HEVC_VULKAN_HWACCEL
458  *fmt++ = AV_PIX_FMT_VULKAN;
459 #endif
460 #if CONFIG_HEVC_VDPAU_HWACCEL
461  *fmt++ = AV_PIX_FMT_VDPAU;
462 #endif
463 #if CONFIG_HEVC_NVDEC_HWACCEL
464  *fmt++ = AV_PIX_FMT_CUDA;
465 #endif
466  break;
467  case AV_PIX_FMT_YUV444P:
468 #if CONFIG_HEVC_VAAPI_HWACCEL
469  *fmt++ = AV_PIX_FMT_VAAPI;
470 #endif
471 #if CONFIG_HEVC_VDPAU_HWACCEL
472  *fmt++ = AV_PIX_FMT_VDPAU;
473 #endif
474 #if CONFIG_HEVC_NVDEC_HWACCEL
475  *fmt++ = AV_PIX_FMT_CUDA;
476 #endif
477 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
478  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
479 #endif
480 #if CONFIG_HEVC_VULKAN_HWACCEL
481  *fmt++ = AV_PIX_FMT_VULKAN;
482 #endif
483  break;
484  case AV_PIX_FMT_YUV422P:
486 #if CONFIG_HEVC_VAAPI_HWACCEL
487  *fmt++ = AV_PIX_FMT_VAAPI;
488 #endif
489 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
490  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
491 #endif
492 #if CONFIG_HEVC_VULKAN_HWACCEL
493  *fmt++ = AV_PIX_FMT_VULKAN;
494 #endif
495  break;
497 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
498  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
499 #endif
500  /* NOTE: fallthrough */
503 #if CONFIG_HEVC_VAAPI_HWACCEL
504  *fmt++ = AV_PIX_FMT_VAAPI;
505 #endif
506 #if CONFIG_HEVC_VDPAU_HWACCEL
507  *fmt++ = AV_PIX_FMT_VDPAU;
508 #endif
509 #if CONFIG_HEVC_VULKAN_HWACCEL
510  *fmt++ = AV_PIX_FMT_VULKAN;
511 #endif
512 #if CONFIG_HEVC_NVDEC_HWACCEL
513  *fmt++ = AV_PIX_FMT_CUDA;
514 #endif
515  break;
517 #if CONFIG_HEVC_VAAPI_HWACCEL
518  *fmt++ = AV_PIX_FMT_VAAPI;
519 #endif
520 #if CONFIG_HEVC_VULKAN_HWACCEL
521  *fmt++ = AV_PIX_FMT_VULKAN;
522 #endif
523  break;
524  }
525 
526  *fmt++ = sps->pix_fmt;
527  *fmt = AV_PIX_FMT_NONE;
528 
529  return ff_get_format(s->avctx, pix_fmts);
530 }
531 
532 static int set_sps(HEVCContext *s, const HEVCSPS *sps,
533  enum AVPixelFormat pix_fmt)
534 {
535  int ret, i;
536 
538  s->ps.sps = NULL;
539  s->ps.vps = NULL;
540 
541  if (!sps)
542  return 0;
543 
544  ret = pic_arrays_init(s, sps);
545  if (ret < 0)
546  goto fail;
547 
549 
550  s->avctx->pix_fmt = pix_fmt;
551 
552  ff_hevc_pred_init(&s->hpc, sps->bit_depth);
553  ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
554  ff_videodsp_init (&s->vdsp, sps->bit_depth);
555 
556  for (i = 0; i < 3; i++) {
557  av_freep(&s->sao_pixel_buffer_h[i]);
558  av_freep(&s->sao_pixel_buffer_v[i]);
559  }
560 
561  if (sps->sao_enabled && !s->avctx->hwaccel) {
562  int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
563  int c_idx;
564 
565  for(c_idx = 0; c_idx < c_count; c_idx++) {
566  int w = sps->width >> sps->hshift[c_idx];
567  int h = sps->height >> sps->vshift[c_idx];
568  s->sao_pixel_buffer_h[c_idx] =
569  av_malloc((w * 2 * sps->ctb_height) <<
570  sps->pixel_shift);
571  s->sao_pixel_buffer_v[c_idx] =
572  av_malloc((h * 2 * sps->ctb_width) <<
573  sps->pixel_shift);
574  if (!s->sao_pixel_buffer_h[c_idx] ||
575  !s->sao_pixel_buffer_v[c_idx])
576  goto fail;
577  }
578  }
579 
580  s->ps.sps = sps;
581  s->ps.vps = s->ps.vps_list[s->ps.sps->vps_id];
582 
583  return 0;
584 
585 fail:
587  for (i = 0; i < 3; i++) {
588  av_freep(&s->sao_pixel_buffer_h[i]);
589  av_freep(&s->sao_pixel_buffer_v[i]);
590  }
591  s->ps.sps = NULL;
592  return ret;
593 }
594 
596 {
597  GetBitContext *gb = &s->HEVClc->gb;
598  SliceHeader *sh = &s->sh;
599  int i, ret;
600 
601  // Coded parameters
603  if (s->ref && sh->first_slice_in_pic_flag) {
604  av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
605  return 1; // This slice will be skipped later, do not corrupt state
606  }
607 
608  if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
609  s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
610  s->max_ra = INT_MAX;
611  if (IS_IDR(s))
613  }
615  if (IS_IRAP(s))
617 
618  sh->pps_id = get_ue_golomb_long(gb);
619  if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
620  av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
621  return AVERROR_INVALIDDATA;
622  }
623  if (!sh->first_slice_in_pic_flag &&
624  s->ps.pps != s->ps.pps_list[sh->pps_id]) {
625  av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
626  return AVERROR_INVALIDDATA;
627  }
628  s->ps.pps = s->ps.pps_list[sh->pps_id];
629  if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
631 
632  if (s->ps.sps != s->ps.sps_list[s->ps.pps->sps_id]) {
633  const HEVCSPS *sps = s->ps.sps_list[s->ps.pps->sps_id];
634  enum AVPixelFormat pix_fmt;
635 
637 
638  ret = set_sps(s, sps, sps->pix_fmt);
639  if (ret < 0)
640  return ret;
641 
642  pix_fmt = get_format(s, sps);
643  if (pix_fmt < 0)
644  return pix_fmt;
645  s->avctx->pix_fmt = pix_fmt;
646 
647  s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
648  s->max_ra = INT_MAX;
649  }
650 
652  if (ret < 0)
653  return ret;
654 
656  if (!sh->first_slice_in_pic_flag) {
657  int slice_address_length;
658 
659  if (s->ps.pps->dependent_slice_segments_enabled_flag)
661  if (sh->dependent_slice_segment_flag && !s->slice_initialized) {
662  av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
663  return AVERROR_INVALIDDATA;
664  }
665 
666  slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
667  s->ps.sps->ctb_height);
668  sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
669  if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
670  av_log(s->avctx, AV_LOG_ERROR,
671  "Invalid slice segment address: %u.\n",
672  sh->slice_segment_addr);
673  return AVERROR_INVALIDDATA;
674  }
675 
676  if (!sh->dependent_slice_segment_flag) {
677  sh->slice_addr = sh->slice_segment_addr;
678  s->slice_idx++;
679  }
680  } else {
681  sh->slice_segment_addr = sh->slice_addr = 0;
682  s->slice_idx = 0;
683  s->slice_initialized = 0;
684  }
685 
686  if (!sh->dependent_slice_segment_flag) {
687  s->slice_initialized = 0;
688 
689  for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
690  skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
691 
692  sh->slice_type = get_ue_golomb_long(gb);
693  if (!(sh->slice_type == HEVC_SLICE_I ||
694  sh->slice_type == HEVC_SLICE_P ||
695  sh->slice_type == HEVC_SLICE_B)) {
696  av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
697  sh->slice_type);
698  return AVERROR_INVALIDDATA;
699  }
700  if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I &&
701  !s->ps.pps->pps_curr_pic_ref_enabled_flag) {
702  av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
703  return AVERROR_INVALIDDATA;
704  }
705 
706  // when flag is not present, picture is inferred to be output
707  sh->pic_output_flag = 1;
708  if (s->ps.pps->output_flag_present_flag)
709  sh->pic_output_flag = get_bits1(gb);
710 
711  if (s->ps.sps->separate_colour_plane_flag)
712  sh->colour_plane_id = get_bits(gb, 2);
713 
714  if (!IS_IDR(s)) {
715  int poc, pos;
716 
717  sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
718  poc = ff_hevc_compute_poc(s->ps.sps, s->pocTid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
719  if (!sh->first_slice_in_pic_flag && poc != s->poc) {
720  av_log(s->avctx, AV_LOG_WARNING,
721  "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
722  if (s->avctx->err_recognition & AV_EF_EXPLODE)
723  return AVERROR_INVALIDDATA;
724  poc = s->poc;
725  }
726  s->poc = poc;
727 
729  pos = get_bits_left(gb);
731  ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
732  if (ret < 0)
733  return ret;
734 
735  sh->short_term_rps = &sh->slice_rps;
736  } else {
737  int numbits, rps_idx;
738 
739  if (!s->ps.sps->nb_st_rps) {
740  av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
741  return AVERROR_INVALIDDATA;
742  }
743 
744  numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
745  rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
746  sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
747  }
749 
750  pos = get_bits_left(gb);
751  ret = decode_lt_rps(s, &sh->long_term_rps, gb);
752  if (ret < 0) {
753  av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
754  if (s->avctx->err_recognition & AV_EF_EXPLODE)
755  return AVERROR_INVALIDDATA;
756  }
758 
759  if (s->ps.sps->sps_temporal_mvp_enabled_flag)
761  else
763  } else {
764  s->poc = 0;
765  sh->pic_order_cnt_lsb = 0;
768  sh->short_term_rps = NULL;
771  }
772 
773  /* 8.3.1 */
774  if (sh->first_slice_in_pic_flag && s->temporal_id == 0 &&
775  s->nal_unit_type != HEVC_NAL_TRAIL_N &&
776  s->nal_unit_type != HEVC_NAL_TSA_N &&
777  s->nal_unit_type != HEVC_NAL_STSA_N &&
778  s->nal_unit_type != HEVC_NAL_RADL_N &&
779  s->nal_unit_type != HEVC_NAL_RADL_R &&
780  s->nal_unit_type != HEVC_NAL_RASL_N &&
781  s->nal_unit_type != HEVC_NAL_RASL_R)
782  s->pocTid0 = s->poc;
783 
784  if (s->ps.sps->sao_enabled) {
786  if (s->ps.sps->chroma_format_idc) {
789  }
790  } else {
794  }
795 
796  sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
797  if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
798  int nb_refs;
799 
800  sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
801  if (sh->slice_type == HEVC_SLICE_B)
802  sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
803 
804  if (get_bits1(gb)) { // num_ref_idx_active_override_flag
805  sh->nb_refs[L0] = get_ue_golomb_31(gb) + 1;
806  if (sh->slice_type == HEVC_SLICE_B)
807  sh->nb_refs[L1] = get_ue_golomb_31(gb) + 1;
808  }
809  if (sh->nb_refs[L0] >= HEVC_MAX_REFS || sh->nb_refs[L1] >= HEVC_MAX_REFS) {
810  av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
811  sh->nb_refs[L0], sh->nb_refs[L1]);
812  return AVERROR_INVALIDDATA;
813  }
814 
815  sh->rpl_modification_flag[0] = 0;
816  sh->rpl_modification_flag[1] = 0;
817  nb_refs = ff_hevc_frame_nb_refs(s);
818  if (!nb_refs) {
819  av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
820  return AVERROR_INVALIDDATA;
821  }
822 
823  if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
824  sh->rpl_modification_flag[0] = get_bits1(gb);
825  if (sh->rpl_modification_flag[0]) {
826  for (i = 0; i < sh->nb_refs[L0]; i++)
827  sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
828  }
829 
830  if (sh->slice_type == HEVC_SLICE_B) {
831  sh->rpl_modification_flag[1] = get_bits1(gb);
832  if (sh->rpl_modification_flag[1] == 1)
833  for (i = 0; i < sh->nb_refs[L1]; i++)
834  sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
835  }
836  }
837 
838  if (sh->slice_type == HEVC_SLICE_B)
839  sh->mvd_l1_zero_flag = get_bits1(gb);
840 
841  if (s->ps.pps->cabac_init_present_flag)
842  sh->cabac_init_flag = get_bits1(gb);
843  else
844  sh->cabac_init_flag = 0;
845 
846  sh->collocated_ref_idx = 0;
848  sh->collocated_list = L0;
849  if (sh->slice_type == HEVC_SLICE_B)
850  sh->collocated_list = !get_bits1(gb);
851 
852  if (sh->nb_refs[sh->collocated_list] > 1) {
854  if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
855  av_log(s->avctx, AV_LOG_ERROR,
856  "Invalid collocated_ref_idx: %d.\n",
857  sh->collocated_ref_idx);
858  return AVERROR_INVALIDDATA;
859  }
860  }
861  }
862 
863  if ((s->ps.pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
864  (s->ps.pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
865  int ret = pred_weight_table(s, gb);
866  if (ret < 0)
867  return ret;
868  }
869 
871  if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
872  av_log(s->avctx, AV_LOG_ERROR,
873  "Invalid number of merging MVP candidates: %d.\n",
874  sh->max_num_merge_cand);
875  return AVERROR_INVALIDDATA;
876  }
877 
878  // Syntax in 7.3.6.1
879  if (s->ps.sps->motion_vector_resolution_control_idc == 2)
880  sh->use_integer_mv_flag = get_bits1(gb);
881  else
882  // Inferred to be equal to motion_vector_resolution_control_idc if not present
883  sh->use_integer_mv_flag = s->ps.sps->motion_vector_resolution_control_idc;
884 
885  }
886 
887  sh->slice_qp_delta = get_se_golomb(gb);
888 
889  if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
892  if (sh->slice_cb_qp_offset < -12 || sh->slice_cb_qp_offset > 12 ||
893  sh->slice_cr_qp_offset < -12 || sh->slice_cr_qp_offset > 12) {
894  av_log(s->avctx, AV_LOG_ERROR, "Invalid slice cx qp offset.\n");
895  return AVERROR_INVALIDDATA;
896  }
897  } else {
898  sh->slice_cb_qp_offset = 0;
899  sh->slice_cr_qp_offset = 0;
900  }
901 
902  if (s->ps.pps->pps_slice_act_qp_offsets_present_flag) {
906  }
907 
908  if (s->ps.pps->chroma_qp_offset_list_enabled_flag)
910  else
912 
913  if (s->ps.pps->deblocking_filter_control_present_flag) {
914  int deblocking_filter_override_flag = 0;
915 
916  if (s->ps.pps->deblocking_filter_override_enabled_flag)
917  deblocking_filter_override_flag = get_bits1(gb);
918 
919  if (deblocking_filter_override_flag) {
922  int beta_offset_div2 = get_se_golomb(gb);
923  int tc_offset_div2 = get_se_golomb(gb) ;
924  if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
925  tc_offset_div2 < -6 || tc_offset_div2 > 6) {
926  av_log(s->avctx, AV_LOG_ERROR,
927  "Invalid deblock filter offsets: %d, %d\n",
928  beta_offset_div2, tc_offset_div2);
929  return AVERROR_INVALIDDATA;
930  }
931  sh->beta_offset = beta_offset_div2 * 2;
932  sh->tc_offset = tc_offset_div2 * 2;
933  }
934  } else {
935  sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
936  sh->beta_offset = s->ps.pps->beta_offset;
937  sh->tc_offset = s->ps.pps->tc_offset;
938  }
939  } else {
941  sh->beta_offset = 0;
942  sh->tc_offset = 0;
943  }
944 
945  if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
950  } else {
951  sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
952  }
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_thread_await_progress(&ref->tf, y, 0);
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_flag &&
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_disable_flag)
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 
2529 static int hls_decode_entry(AVCodecContext *avctxt, void *arg)
2530 {
2531  HEVCContext *s = avctxt->priv_data;
2532  HEVCLocalContext *const lc = s->HEVClc;
2533  int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2534  int more_data = 1;
2535  int x_ctb = 0;
2536  int y_ctb = 0;
2537  int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2538  int ret;
2539 
2540  if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
2541  av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
2542  return AVERROR_INVALIDDATA;
2543  }
2544 
2545  if (s->sh.dependent_slice_segment_flag) {
2546  int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2547  if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2548  av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2549  return AVERROR_INVALIDDATA;
2550  }
2551  }
2552 
2553  while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2554  int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2555 
2556  x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2557  y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2558  hls_decode_neighbour(lc, x_ctb, y_ctb, ctb_addr_ts);
2559 
2560  ret = ff_hevc_cabac_init(lc, ctb_addr_ts);
2561  if (ret < 0) {
2562  s->tab_slice_address[ctb_addr_rs] = -1;
2563  return ret;
2564  }
2565 
2566  hls_sao_param(lc, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2567 
2568  s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2569  s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2570  s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2571 
2572  more_data = hls_coding_quadtree(lc, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2573  if (more_data < 0) {
2574  s->tab_slice_address[ctb_addr_rs] = -1;
2575  return more_data;
2576  }
2577 
2578 
2579  ctb_addr_ts++;
2580  ff_hevc_save_states(lc, ctb_addr_ts);
2581  ff_hevc_hls_filters(lc, x_ctb, y_ctb, ctb_size);
2582  }
2583 
2584  if (x_ctb + ctb_size >= s->ps.sps->width &&
2585  y_ctb + ctb_size >= s->ps.sps->height)
2586  ff_hevc_hls_filter(lc, x_ctb, y_ctb, ctb_size);
2587 
2588  return ctb_addr_ts;
2589 }
2590 
2592 {
2593  int ret = 0;
2594 
2595  s->avctx->execute(s->avctx, hls_decode_entry, NULL, &ret , 1, 0);
2596  return ret;
2597 }
2598 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *hevc_lclist,
2599  int job, int self_id)
2600 {
2601  HEVCLocalContext *lc = ((HEVCLocalContext**)hevc_lclist)[self_id];
2602  const HEVCContext *const s = lc->parent;
2603  int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2604  int more_data = 1;
2605  int ctb_row = job;
2606  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);
2607  int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2608  int thread = ctb_row % s->threads_number;
2609  int ret;
2610 
2611  if(ctb_row) {
2612  ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2613  if (ret < 0)
2614  goto error;
2615  ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2616  }
2617 
2618  while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2619  int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2620  int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2621 
2622  hls_decode_neighbour(lc, x_ctb, y_ctb, ctb_addr_ts);
2623 
2624  ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2625 
2626  /* atomic_load's prototype requires a pointer to non-const atomic variable
2627  * (due to implementations via mutexes, where reads involve writes).
2628  * Of course, casting const away here is nevertheless safe. */
2629  if (atomic_load((atomic_int*)&s->wpp_err)) {
2630  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2631  return 0;
2632  }
2633 
2634  ret = ff_hevc_cabac_init(lc, ctb_addr_ts);
2635  if (ret < 0)
2636  goto error;
2637  hls_sao_param(lc, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2638 
2639  s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2640  s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2641  s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2642 
2643  more_data = hls_coding_quadtree(lc, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2644 
2645  if (more_data < 0) {
2646  ret = more_data;
2647  goto error;
2648  }
2649 
2650  ctb_addr_ts++;
2651 
2652  ff_hevc_save_states(lc, ctb_addr_ts);
2653  ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2654  ff_hevc_hls_filters(lc, x_ctb, y_ctb, ctb_size);
2655 
2656  if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2657  /* Casting const away here is safe, because it is an atomic operation. */
2658  atomic_store((atomic_int*)&s->wpp_err, 1);
2659  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2660  return 0;
2661  }
2662 
2663  if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
2664  ff_hevc_hls_filter(lc, x_ctb, y_ctb, ctb_size);
2665  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2666  return ctb_addr_ts;
2667  }
2668  ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2669  x_ctb+=ctb_size;
2670 
2671  if(x_ctb >= s->ps.sps->width) {
2672  break;
2673  }
2674  }
2675  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2676 
2677  return 0;
2678 error:
2679  s->tab_slice_address[ctb_addr_rs] = -1;
2680  /* Casting const away here is safe, because it is an atomic operation. */
2681  atomic_store((atomic_int*)&s->wpp_err, 1);
2682  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2683  return ret;
2684 }
2685 
2686 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2687 {
2688  const uint8_t *data = nal->data;
2689  int length = nal->size;
2690  HEVCLocalContext *lc = s->HEVClc;
2691  int *ret;
2692  int64_t offset;
2693  int64_t startheader, cmpt = 0;
2694  int i, j, res = 0;
2695 
2696  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) {
2697  av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2698  s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2699  s->ps.sps->ctb_width, s->ps.sps->ctb_height
2700  );
2701  return AVERROR_INVALIDDATA;
2702  }
2703 
2704  for (i = 1; i < s->threads_number; i++) {
2705  if (s->HEVClcList[i])
2706  continue;
2707  s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
2708  if (!s->HEVClcList[i])
2709  return AVERROR(ENOMEM);
2710  s->HEVClcList[i]->logctx = s->avctx;
2711  s->HEVClcList[i]->parent = s;
2712  s->HEVClcList[i]->common_cabac_state = &s->cabac;
2713  }
2714 
2715  offset = (lc->gb.index >> 3);
2716 
2717  for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2718  if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2719  startheader--;
2720  cmpt++;
2721  }
2722  }
2723 
2724  for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2725  offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2726  for (j = 0, cmpt = 0, startheader = offset
2727  + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2728  if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2729  startheader--;
2730  cmpt++;
2731  }
2732  }
2733  s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2734  s->sh.offset[i - 1] = offset;
2735 
2736  }
2737  if (s->sh.num_entry_point_offsets != 0) {
2738  offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2739  if (length < offset) {
2740  av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2741  return AVERROR_INVALIDDATA;
2742  }
2743  s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2744  s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2745 
2746  }
2747  s->data = data;
2748 
2749  for (i = 1; i < s->threads_number; i++) {
2750  s->HEVClcList[i]->first_qp_group = 1;
2751  s->HEVClcList[i]->qp_y = s->HEVClc->qp_y;
2752  }
2753 
2754  atomic_store(&s->wpp_err, 0);
2755  res = ff_slice_thread_allocz_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
2756  if (res < 0)
2757  return res;
2758 
2759  ret = av_calloc(s->sh.num_entry_point_offsets + 1, sizeof(*ret));
2760  if (!ret)
2761  return AVERROR(ENOMEM);
2762 
2763  if (s->ps.pps->entropy_coding_sync_enabled_flag)
2764  s->avctx->execute2(s->avctx, hls_decode_entry_wpp, s->HEVClcList, ret, s->sh.num_entry_point_offsets + 1);
2765 
2766  for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2767  res += ret[i];
2768 
2769  av_free(ret);
2770  return res;
2771 }
2772 
2774 {
2775  AVFrame *out = s->ref->frame;
2776  int ret;
2777 
2778  // Decrement the mastering display and content light level flag when IRAP
2779  // frame has no_rasl_output_flag=1 so the side data persists for the entire
2780  // coded video sequence.
2781  if (IS_IRAP(s) && s->no_rasl_output_flag) {
2782  if (s->sei.common.mastering_display.present > 0)
2783  s->sei.common.mastering_display.present--;
2784 
2785  if (s->sei.common.content_light.present > 0)
2786  s->sei.common.content_light.present--;
2787  }
2788 
2789  ret = ff_h2645_sei_to_frame(out, &s->sei.common, AV_CODEC_ID_HEVC, s->avctx,
2790  &s->ps.sps->vui.common,
2791  s->ps.sps->bit_depth, s->ps.sps->bit_depth_chroma,
2792  s->ref->poc /* no poc_offset in HEVC */);
2793  if (ret < 0)
2794  return ret;
2795 
2796  if (s->sei.timecode.present) {
2797  uint32_t *tc_sd;
2798  char tcbuf[AV_TIMECODE_STR_SIZE];
2799  AVFrameSideData *tcside;
2801  sizeof(uint32_t) * 4, &tcside);
2802  if (ret < 0)
2803  return ret;
2804 
2805  if (tcside) {
2806  tc_sd = (uint32_t*)tcside->data;
2807  tc_sd[0] = s->sei.timecode.num_clock_ts;
2808 
2809  for (int i = 0; i < tc_sd[0]; i++) {
2810  int drop = s->sei.timecode.cnt_dropped_flag[i];
2811  int hh = s->sei.timecode.hours_value[i];
2812  int mm = s->sei.timecode.minutes_value[i];
2813  int ss = s->sei.timecode.seconds_value[i];
2814  int ff = s->sei.timecode.n_frames[i];
2815 
2816  tc_sd[i + 1] = av_timecode_get_smpte(s->avctx->framerate, drop, hh, mm, ss, ff);
2817  av_timecode_make_smpte_tc_string2(tcbuf, s->avctx->framerate, tc_sd[i + 1], 0, 0);
2818  av_dict_set(&out->metadata, "timecode", tcbuf, 0);
2819  }
2820  }
2821 
2822  s->sei.timecode.num_clock_ts = 0;
2823  }
2824 
2825  if (s->sei.common.dynamic_hdr_plus.info) {
2826  AVBufferRef *info_ref = av_buffer_ref(s->sei.common.dynamic_hdr_plus.info);
2827  if (!info_ref)
2828  return AVERROR(ENOMEM);
2829 
2831  if (ret < 0)
2832  return ret;
2833  }
2834 
2835  if (s->rpu_buf) {
2837  if (!rpu)
2838  return AVERROR(ENOMEM);
2839 
2840  s->rpu_buf = NULL;
2841  }
2842 
2843  if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
2844  return ret;
2845 
2846  if (s->sei.common.dynamic_hdr_vivid.info) {
2847  AVBufferRef *info_ref = av_buffer_ref(s->sei.common.dynamic_hdr_vivid.info);
2848  if (!info_ref)
2849  return AVERROR(ENOMEM);
2850 
2852  av_buffer_unref(&info_ref);
2853  return AVERROR(ENOMEM);
2854  }
2855  }
2856 
2857  return 0;
2858 }
2859 
2861 {
2862  HEVCLocalContext *lc = s->HEVClc;
2863  int pic_size_in_ctb = ((s->ps.sps->width >> s->ps.sps->log2_min_cb_size) + 1) *
2864  ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
2865  int ret;
2866 
2867  memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
2868  memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
2869  memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2870  memset(s->is_pcm, 0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
2871  memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2872 
2873  s->is_decoded = 0;
2874  s->first_nal_type = s->nal_unit_type;
2875 
2876  s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
2877 
2878  if (s->ps.pps->tiles_enabled_flag)
2879  lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2880 
2881  ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
2882  if (ret < 0)
2883  goto fail;
2884 
2885  ret = ff_hevc_frame_rps(s);
2886  if (ret < 0) {
2887  av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2888  goto fail;
2889  }
2890 
2891  if (IS_IRAP(s))
2892  s->ref->frame->flags |= AV_FRAME_FLAG_KEY;
2893  else
2894  s->ref->frame->flags &= ~AV_FRAME_FLAG_KEY;
2895 
2896  s->ref->needs_fg = (s->sei.common.film_grain_characteristics.present ||
2897  s->sei.common.aom_film_grain.enable) &&
2898  !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
2899  !s->avctx->hwaccel;
2900 
2901  ret = set_side_data(s);
2902  if (ret < 0)
2903  goto fail;
2904 
2905  if (s->ref->needs_fg &&
2906  (s->sei.common.film_grain_characteristics.present &&
2907  !ff_h274_film_grain_params_supported(s->sei.common.film_grain_characteristics.model_id,
2908  s->ref->frame->format)
2909  || !av_film_grain_params_select(s->ref->frame))) {
2910  av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown,
2911  "Unsupported film grain parameters. Ignoring film grain.\n");
2912  s->ref->needs_fg = 0;
2913  }
2914 
2915  if (s->ref->needs_fg) {
2916  s->ref->frame_grain->format = s->ref->frame->format;
2917  s->ref->frame_grain->width = s->ref->frame->width;
2918  s->ref->frame_grain->height = s->ref->frame->height;
2919  if ((ret = ff_thread_get_buffer(s->avctx, s->ref->frame_grain, 0)) < 0)
2920  goto fail;
2921  }
2922 
2923  s->frame->pict_type = 3 - s->sh.slice_type;
2924 
2925  if (!IS_IRAP(s))
2927 
2928  av_frame_unref(s->output_frame);
2929  ret = ff_hevc_output_frame(s, s->output_frame, 0);
2930  if (ret < 0)
2931  goto fail;
2932 
2933  if (!s->avctx->hwaccel)
2934  ff_thread_finish_setup(s->avctx);
2935 
2936  return 0;
2937 
2938 fail:
2939  if (s->ref)
2940  ff_hevc_unref_frame(s->ref, ~0);
2941  s->ref = s->collocated_ref = NULL;
2942  return ret;
2943 }
2944 
2946 {
2947  HEVCFrame *out = s->ref;
2948  const AVFilmGrainParams *fgp;
2949  av_unused int ret;
2950 
2951  if (out->needs_fg) {
2952  av_assert0(out->frame_grain->buf[0]);
2953  fgp = av_film_grain_params_select(out->frame);
2954  switch (fgp->type) {
2956  av_assert0(0);
2957  return AVERROR_BUG;
2959  ret = ff_h274_apply_film_grain(out->frame_grain, out->frame,
2960  &s->h274db, fgp);
2961  break;
2963  ret = ff_aom_apply_film_grain(out->frame_grain, out->frame, fgp);
2964  break;
2965  }
2966  av_assert1(ret >= 0);
2967  }
2968 
2969  return 0;
2970 }
2971 
2972 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
2973 {
2974  HEVCLocalContext *lc = s->HEVClc;
2975  GetBitContext *gb = &lc->gb;
2976  int ctb_addr_ts, ret;
2977 
2978  *gb = nal->gb;
2979  s->nal_unit_type = nal->type;
2980  s->temporal_id = nal->temporal_id;
2981 
2982  switch (s->nal_unit_type) {
2983  case HEVC_NAL_VPS:
2984  if (FF_HW_HAS_CB(s->avctx, decode_params)) {
2985  ret = FF_HW_CALL(s->avctx, decode_params,
2986  nal->type, nal->raw_data, nal->raw_size);
2987  if (ret < 0)
2988  goto fail;
2989  }
2990  ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2991  if (ret < 0)
2992  goto fail;
2993  break;
2994  case HEVC_NAL_SPS:
2995  if (FF_HW_HAS_CB(s->avctx, decode_params)) {
2996  ret = FF_HW_CALL(s->avctx, decode_params,
2997  nal->type, nal->raw_data, nal->raw_size);
2998  if (ret < 0)
2999  goto fail;
3000  }
3001  ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
3002  s->apply_defdispwin);
3003  if (ret < 0)
3004  goto fail;
3005  break;
3006  case HEVC_NAL_PPS:
3007  if (FF_HW_HAS_CB(s->avctx, decode_params)) {
3008  ret = FF_HW_CALL(s->avctx, decode_params,
3009  nal->type, nal->raw_data, nal->raw_size);
3010  if (ret < 0)
3011  goto fail;
3012  }
3013  ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
3014  if (ret < 0)
3015  goto fail;
3016  break;
3017  case HEVC_NAL_SEI_PREFIX:
3018  case HEVC_NAL_SEI_SUFFIX:
3019  if (FF_HW_HAS_CB(s->avctx, decode_params)) {
3020  ret = FF_HW_CALL(s->avctx, decode_params,
3021  nal->type, nal->raw_data, nal->raw_size);
3022  if (ret < 0)
3023  goto fail;
3024  }
3025  ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
3026  if (ret < 0)
3027  goto fail;
3028  break;
3029  case HEVC_NAL_TRAIL_R:
3030  case HEVC_NAL_TRAIL_N:
3031  case HEVC_NAL_TSA_N:
3032  case HEVC_NAL_TSA_R:
3033  case HEVC_NAL_STSA_N:
3034  case HEVC_NAL_STSA_R:
3035  case HEVC_NAL_BLA_W_LP:
3036  case HEVC_NAL_BLA_W_RADL:
3037  case HEVC_NAL_BLA_N_LP:
3038  case HEVC_NAL_IDR_W_RADL:
3039  case HEVC_NAL_IDR_N_LP:
3040  case HEVC_NAL_CRA_NUT:
3041  case HEVC_NAL_RADL_N:
3042  case HEVC_NAL_RADL_R:
3043  case HEVC_NAL_RASL_N:
3044  case HEVC_NAL_RASL_R:
3045  ret = hls_slice_header(s);
3046  if (ret < 0) {
3047  // hls_slice_header() does not cleanup on failure thus the state now is inconsistant so we cannot use it on depandant slices
3048  s->slice_initialized = 0;
3049  return ret;
3050  }
3051  if (ret == 1) {
3053  goto fail;
3054  }
3055 
3056 
3057  if (
3058  (s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
3059  (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
3060  (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s))) {
3061  break;
3062  }
3063 
3064  if (s->sh.first_slice_in_pic_flag) {
3065  if (s->max_ra == INT_MAX) {
3066  if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
3067  s->max_ra = s->poc;
3068  } else {
3069  if (IS_IDR(s))
3070  s->max_ra = INT_MIN;
3071  }
3072  }
3073 
3074  if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
3075  s->poc <= s->max_ra) {
3076  s->is_decoded = 0;
3077  break;
3078  } else {
3079  if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
3080  s->max_ra = INT_MIN;
3081  }
3082 
3083  s->overlap ++;
3084  ret = hevc_frame_start(s);
3085  if (ret < 0)
3086  return ret;
3087  } else if (!s->ref) {
3088  av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
3089  goto fail;
3090  }
3091 
3092  if (s->nal_unit_type != s->first_nal_type) {
3093  av_log(s->avctx, AV_LOG_ERROR,
3094  "Non-matching NAL types of the VCL NALUs: %d %d\n",
3095  s->first_nal_type, s->nal_unit_type);
3096  return AVERROR_INVALIDDATA;
3097  }
3098 
3099  if (!s->sh.dependent_slice_segment_flag &&
3100  s->sh.slice_type != HEVC_SLICE_I) {
3101  ret = ff_hevc_slice_rpl(s);
3102  if (ret < 0) {
3103  av_log(s->avctx, AV_LOG_WARNING,
3104  "Error constructing the reference lists for the current slice.\n");
3105  goto fail;
3106  }
3107  }
3108 
3109  if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
3110  ret = FF_HW_CALL(s->avctx, start_frame, NULL, 0);
3111  if (ret < 0)
3112  goto fail;
3113  }
3114 
3115  if (s->avctx->hwaccel) {
3116  ret = FF_HW_CALL(s->avctx, decode_slice, nal->raw_data, nal->raw_size);
3117  if (ret < 0)
3118  goto fail;
3119  } else {
3120  if (s->avctx->profile == AV_PROFILE_HEVC_SCC) {
3121  av_log(s->avctx, AV_LOG_ERROR,
3122  "SCC profile is not yet implemented in hevc native decoder.\n");
3124  goto fail;
3125  }
3126 
3127  if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
3128  ctb_addr_ts = hls_slice_data_wpp(s, nal);
3129  else
3130  ctb_addr_ts = hls_slice_data(s);
3131  if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
3132  ret = hevc_frame_end(s);
3133  if (ret < 0)
3134  goto fail;
3135  s->is_decoded = 1;
3136  }
3137 
3138  if (ctb_addr_ts < 0) {
3139  ret = ctb_addr_ts;
3140  goto fail;
3141  }
3142  }
3143  break;
3144  case HEVC_NAL_EOS_NUT:
3145  case HEVC_NAL_EOB_NUT:
3146  s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
3147  s->max_ra = INT_MAX;
3148  break;
3149  case HEVC_NAL_AUD:
3150  case HEVC_NAL_FD_NUT:
3151  case HEVC_NAL_UNSPEC62:
3152  break;
3153  default:
3154  av_log(s->avctx, AV_LOG_INFO,
3155  "Skipping NAL unit %d\n", s->nal_unit_type);
3156  }
3157 
3158  return 0;
3159 fail:
3160  if (s->avctx->err_recognition & AV_EF_EXPLODE)
3161  return ret;
3162  return 0;
3163 }
3164 
3165 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
3166 {
3167  int i, ret = 0;
3168  int eos_at_start = 1;
3169 
3170  s->ref = s->collocated_ref = NULL;
3171  s->last_eos = s->eos;
3172  s->eos = 0;
3173  s->overlap = 0;
3174 
3175  /* split the input packet into NAL units, so we know the upper bound on the
3176  * number of slices in the frame */
3177  ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
3178  s->nal_length_size, s->avctx->codec_id, 1, 0);
3179  if (ret < 0) {
3180  av_log(s->avctx, AV_LOG_ERROR,
3181  "Error splitting the input into NAL units.\n");
3182  return ret;
3183  }
3184 
3185  for (i = 0; i < s->pkt.nb_nals; i++) {
3186  if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
3187  s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
3188  if (eos_at_start) {
3189  s->last_eos = 1;
3190  } else {
3191  s->eos = 1;
3192  }
3193  } else {
3194  eos_at_start = 0;
3195  }
3196  }
3197 
3198  /*
3199  * Check for RPU delimiter.
3200  *
3201  * Dolby Vision RPUs masquerade as unregistered NALs of type 62.
3202  *
3203  * We have to do this check here an create the rpu buffer, since RPUs are appended
3204  * to the end of an AU; they are the last non-EOB/EOS NAL in the AU.
3205  */
3206  if (s->pkt.nb_nals > 1 && s->pkt.nals[s->pkt.nb_nals - 1].type == HEVC_NAL_UNSPEC62 &&
3207  s->pkt.nals[s->pkt.nb_nals - 1].size > 2 && !s->pkt.nals[s->pkt.nb_nals - 1].nuh_layer_id
3208  && !s->pkt.nals[s->pkt.nb_nals - 1].temporal_id) {
3209  H2645NAL *nal = &s->pkt.nals[s->pkt.nb_nals - 1];
3210  if (s->rpu_buf) {
3211  av_buffer_unref(&s->rpu_buf);
3212  av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
3213  }
3214 
3215  s->rpu_buf = av_buffer_alloc(nal->raw_size - 2);
3216  if (!s->rpu_buf)
3217  return AVERROR(ENOMEM);
3218  memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2);
3219 
3220  ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2);
3221  if (ret < 0) {
3222  av_buffer_unref(&s->rpu_buf);
3223  av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
3224  /* ignore */
3225  }
3226  }
3227 
3228  /* decode the NAL units */
3229  for (i = 0; i < s->pkt.nb_nals; i++) {
3230  H2645NAL *nal = &s->pkt.nals[i];
3231 
3232  if (s->avctx->skip_frame >= AVDISCARD_ALL ||
3233  (s->avctx->skip_frame >= AVDISCARD_NONREF
3234  && ff_hevc_nal_is_nonref(nal->type)) || nal->nuh_layer_id > 0)
3235  continue;
3236 
3237  ret = decode_nal_unit(s, nal);
3238  if (ret >= 0 && s->overlap > 2)
3240  if (ret < 0) {
3241  av_log(s->avctx, AV_LOG_WARNING,
3242  "Error parsing NAL unit #%d.\n", i);
3243  goto fail;
3244  }
3245  }
3246 
3247 fail:
3248  if (s->ref && s->threads_type == FF_THREAD_FRAME)
3249  ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
3250 
3251  return ret;
3252 }
3253 
3255 {
3257  char msg_buf[4 * (50 + 2 * 2 * 16 /* MD5-size */)];
3258  int pixel_shift;
3259  int err = 0;
3260  int i, j;
3261 
3262  if (!desc)
3263  return AVERROR(EINVAL);
3264 
3265  pixel_shift = desc->comp[0].depth > 8;
3266 
3267  /* the checksums are LE, so we have to byteswap for >8bpp formats
3268  * on BE arches */
3269 #if HAVE_BIGENDIAN
3270  if (pixel_shift && !s->checksum_buf) {
3271  av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3272  FFMAX3(frame->linesize[0], frame->linesize[1],
3273  frame->linesize[2]));
3274  if (!s->checksum_buf)
3275  return AVERROR(ENOMEM);
3276  }
3277 #endif
3278 
3279  msg_buf[0] = '\0';
3280  for (i = 0; frame->data[i]; i++) {
3281  int width = s->avctx->coded_width;
3282  int height = s->avctx->coded_height;
3283  int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
3284  int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3285  uint8_t md5[16];
3286 
3287  av_md5_init(s->md5_ctx);
3288  for (j = 0; j < h; j++) {
3289  const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3290 #if HAVE_BIGENDIAN
3291  if (pixel_shift) {
3292  s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3293  (const uint16_t *) src, w);
3294  src = s->checksum_buf;
3295  }
3296 #endif
3297  av_md5_update(s->md5_ctx, src, w << pixel_shift);
3298  }
3299  av_md5_final(s->md5_ctx, md5);
3300 
3301 #define MD5_PRI "%016" PRIx64 "%016" PRIx64
3302 #define MD5_PRI_ARG(buf) AV_RB64(buf), AV_RB64((const uint8_t*)(buf) + 8)
3303 
3304  if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3305  av_strlcatf(msg_buf, sizeof(msg_buf),
3306  "plane %d - correct " MD5_PRI "; ",
3307  i, MD5_PRI_ARG(md5));
3308  } else {
3309  av_strlcatf(msg_buf, sizeof(msg_buf),
3310  "mismatching checksum of plane %d - " MD5_PRI " != " MD5_PRI "; ",
3311  i, MD5_PRI_ARG(md5), MD5_PRI_ARG(s->sei.picture_hash.md5[i]));
3312  err = AVERROR_INVALIDDATA;
3313  }
3314  }
3315 
3316  av_log(s->avctx, err < 0 ? AV_LOG_ERROR : AV_LOG_DEBUG,
3317  "Verifying checksum for frame with POC %d: %s\n",
3318  s->poc, msg_buf);
3319 
3320  return err;
3321 }
3322 
3323 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3324 {
3325  int ret, i;
3326 
3327  ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3328  &s->nal_length_size, s->avctx->err_recognition,
3329  s->apply_defdispwin, s->avctx);
3330  if (ret < 0)
3331  return ret;
3332 
3333  /* export stream parameters from the first SPS */
3334  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3335  if (first && s->ps.sps_list[i]) {
3336  const HEVCSPS *sps = s->ps.sps_list[i];
3338  break;
3339  }
3340  }
3341 
3342  /* export stream parameters from SEI */
3344  if (ret < 0)
3345  return ret;
3346 
3347  return 0;
3348 }
3349 
3350 static int hevc_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
3351  int *got_output, AVPacket *avpkt)
3352 {
3353  int ret;
3354  uint8_t *sd;
3355  size_t sd_size;
3356  HEVCContext *s = avctx->priv_data;
3357 
3358  if (!avpkt->size) {
3359  ret = ff_hevc_output_frame(s, rframe, 1);
3360  if (ret < 0)
3361  return ret;
3362 
3363  *got_output = ret;
3364  return 0;
3365  }
3366 
3367  sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
3368  if (sd && sd_size > 0) {
3369  ret = hevc_decode_extradata(s, sd, sd_size, 0);
3370  if (ret < 0)
3371  return ret;
3372  }
3373 
3374  sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
3375  if (sd && sd_size > 0) {
3376  int old = s->dovi_ctx.dv_profile;
3377 
3379  if (old)
3380  av_log(avctx, AV_LOG_DEBUG,
3381  "New DOVI configuration record from input packet (profile %d -> %u).\n",
3382  old, s->dovi_ctx.dv_profile);
3383  }
3384 
3385  s->ref = s->collocated_ref = NULL;
3386  ret = decode_nal_units(s, avpkt->data, avpkt->size);
3387  if (ret < 0)
3388  return ret;
3389 
3390  if (avctx->hwaccel) {
3391  if (s->ref && (ret = FF_HW_SIMPLE_CALL(avctx, end_frame)) < 0) {
3392  av_log(avctx, AV_LOG_ERROR,
3393  "hardware accelerator failed to decode picture\n");
3394  ff_hevc_unref_frame(s->ref, ~0);
3395  return ret;
3396  }
3397  } else {
3398  /* verify the SEI checksum */
3399  if (avctx->err_recognition & AV_EF_CRCCHECK && s->ref && s->is_decoded &&
3400  s->sei.picture_hash.is_md5) {
3401  ret = verify_md5(s, s->ref->frame);
3402  if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
3403  ff_hevc_unref_frame(s->ref, ~0);
3404  return ret;
3405  }
3406  }
3407  }
3408  s->sei.picture_hash.is_md5 = 0;
3409 
3410  if (s->is_decoded) {
3411  av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
3412  s->is_decoded = 0;
3413  }
3414 
3415  if (s->output_frame->buf[0]) {
3416  av_frame_move_ref(rframe, s->output_frame);
3417  *got_output = 1;
3418  }
3419 
3420  return avpkt->size;
3421 }
3422 
3424 {
3425  int ret;
3426 
3427  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
3428  if (ret < 0)
3429  return ret;
3430 
3431  if (src->needs_fg) {
3432  ret = av_frame_ref(dst->frame_grain, src->frame_grain);
3433  if (ret < 0)
3434  return ret;
3435  dst->needs_fg = 1;
3436  }
3437 
3438  dst->tab_mvf = ff_refstruct_ref(src->tab_mvf);
3439  dst->rpl_tab = ff_refstruct_ref(src->rpl_tab);
3440  dst->rpl = ff_refstruct_ref(src->rpl);
3441  dst->nb_rpl_elems = src->nb_rpl_elems;
3442 
3443  dst->poc = src->poc;
3444  dst->ctb_count = src->ctb_count;
3445  dst->flags = src->flags;
3446  dst->sequence = src->sequence;
3447 
3449  src->hwaccel_picture_private);
3450 
3451  return 0;
3452 }
3453 
3455 {
3456  HEVCContext *s = avctx->priv_data;
3457  int i;
3458 
3459  pic_arrays_free(s);
3460 
3461  ff_dovi_ctx_unref(&s->dovi_ctx);
3462  av_buffer_unref(&s->rpu_buf);
3463 
3464  av_freep(&s->md5_ctx);
3465 
3466  for (i = 0; i < 3; i++) {
3467  av_freep(&s->sao_pixel_buffer_h[i]);
3468  av_freep(&s->sao_pixel_buffer_v[i]);
3469  }
3470  av_frame_free(&s->output_frame);
3471 
3472  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3473  ff_hevc_unref_frame(&s->DPB[i], ~0);
3474  av_frame_free(&s->DPB[i].frame);
3475  av_frame_free(&s->DPB[i].frame_grain);
3476  }
3477 
3478  ff_hevc_ps_uninit(&s->ps);
3479 
3480  av_freep(&s->sh.entry_point_offset);
3481  av_freep(&s->sh.offset);
3482  av_freep(&s->sh.size);
3483 
3484  if (s->HEVClcList) {
3485  for (i = 1; i < s->threads_number; i++) {
3486  av_freep(&s->HEVClcList[i]);
3487  }
3488  }
3489  av_freep(&s->HEVClc);
3490  av_freep(&s->HEVClcList);
3491 
3492  ff_h2645_packet_uninit(&s->pkt);
3493 
3494  ff_hevc_reset_sei(&s->sei);
3495 
3496  return 0;
3497 }
3498 
3500 {
3501  HEVCContext *s = avctx->priv_data;
3502  int i;
3503 
3504  s->avctx = avctx;
3505 
3506  s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
3507  s->HEVClcList = av_mallocz(sizeof(HEVCLocalContext*) * s->threads_number);
3508  if (!s->HEVClc || !s->HEVClcList)
3509  return AVERROR(ENOMEM);
3510  s->HEVClc->parent = s;
3511  s->HEVClc->logctx = avctx;
3512  s->HEVClc->common_cabac_state = &s->cabac;
3513  s->HEVClcList[0] = s->HEVClc;
3514 
3515  s->output_frame = av_frame_alloc();
3516  if (!s->output_frame)
3517  return AVERROR(ENOMEM);
3518 
3519  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3520  s->DPB[i].frame = av_frame_alloc();
3521  if (!s->DPB[i].frame)
3522  return AVERROR(ENOMEM);
3523  s->DPB[i].tf.f = s->DPB[i].frame;
3524 
3525  s->DPB[i].frame_grain = av_frame_alloc();
3526  if (!s->DPB[i].frame_grain)
3527  return AVERROR(ENOMEM);
3528  }
3529 
3530  s->max_ra = INT_MAX;
3531 
3532  s->md5_ctx = av_md5_alloc();
3533  if (!s->md5_ctx)
3534  return AVERROR(ENOMEM);
3535 
3536  ff_bswapdsp_init(&s->bdsp);
3537 
3538  s->dovi_ctx.logctx = avctx;
3539  s->eos = 0;
3540 
3541  ff_hevc_reset_sei(&s->sei);
3542 
3543  return 0;
3544 }
3545 
3546 #if HAVE_THREADS
3547 static int hevc_update_thread_context(AVCodecContext *dst,
3548  const AVCodecContext *src)
3549 {
3550  HEVCContext *s = dst->priv_data;
3551  HEVCContext *s0 = src->priv_data;
3552  int i, ret;
3553 
3554  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3555  ff_hevc_unref_frame(&s->DPB[i], ~0);
3556  if (s0->DPB[i].frame->buf[0]) {
3557  ret = hevc_ref_frame(&s->DPB[i], &s0->DPB[i]);
3558  if (ret < 0)
3559  return ret;
3560  }
3561  }
3562 
3563  if (s->ps.sps != s0->ps.sps)
3564  s->ps.sps = NULL;
3565  for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
3566  ff_refstruct_replace(&s->ps.vps_list[i], s0->ps.vps_list[i]);
3567 
3568  for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
3569  ff_refstruct_replace(&s->ps.sps_list[i], s0->ps.sps_list[i]);
3570 
3571  for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
3572  ff_refstruct_replace(&s->ps.pps_list[i], s0->ps.pps_list[i]);
3573 
3574  if (s->ps.sps != s0->ps.sps)
3575  if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
3576  return ret;
3577 
3578  s->seq_decode = s0->seq_decode;
3579  s->seq_output = s0->seq_output;
3580  s->pocTid0 = s0->pocTid0;
3581  s->max_ra = s0->max_ra;
3582  s->eos = s0->eos;
3583  s->no_rasl_output_flag = s0->no_rasl_output_flag;
3584 
3585  s->is_nalff = s0->is_nalff;
3586  s->nal_length_size = s0->nal_length_size;
3587 
3588  s->threads_number = s0->threads_number;
3589  s->threads_type = s0->threads_type;
3590 
3591  s->film_grain_warning_shown = s0->film_grain_warning_shown;
3592 
3593  if (s0->eos) {
3594  s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
3595  s->max_ra = INT_MAX;
3596  }
3597 
3598  ret = ff_h2645_sei_ctx_replace(&s->sei.common, &s0->sei.common);
3599  if (ret < 0)
3600  return ret;
3601 
3602  ret = av_buffer_replace(&s->sei.common.dynamic_hdr_plus.info,
3603  s0->sei.common.dynamic_hdr_plus.info);
3604  if (ret < 0)
3605  return ret;
3606 
3607  ret = av_buffer_replace(&s->rpu_buf, s0->rpu_buf);
3608  if (ret < 0)
3609  return ret;
3610 
3611  ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
3612 
3613  ret = av_buffer_replace(&s->sei.common.dynamic_hdr_vivid.info,
3614  s0->sei.common.dynamic_hdr_vivid.info);
3615  if (ret < 0)
3616  return ret;
3617 
3618  s->sei.common.frame_packing = s0->sei.common.frame_packing;
3619  s->sei.common.display_orientation = s0->sei.common.display_orientation;
3620  s->sei.common.alternative_transfer = s0->sei.common.alternative_transfer;
3621  s->sei.common.mastering_display = s0->sei.common.mastering_display;
3622  s->sei.common.content_light = s0->sei.common.content_light;
3623  s->sei.common.aom_film_grain = s0->sei.common.aom_film_grain;
3624 
3626  if (ret < 0)
3627  return ret;
3628 
3629  return 0;
3630 }
3631 #endif
3632 
3634 {
3635  HEVCContext *s = avctx->priv_data;
3636  int ret;
3637 
3638  if (avctx->active_thread_type & FF_THREAD_SLICE) {
3639  s->threads_number = avctx->thread_count;
3641  if (ret < 0)
3642  return ret;
3643  } else
3644  s->threads_number = 1;
3645 
3646  if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
3647  s->threads_type = FF_THREAD_FRAME;
3648  else
3649  s->threads_type = FF_THREAD_SLICE;
3650 
3651  ret = hevc_init_context(avctx);
3652  if (ret < 0)
3653  return ret;
3654 
3655  s->enable_parallel_tiles = 0;
3656  s->sei.picture_timing.picture_struct = 0;
3657  s->eos = 1;
3658 
3659  atomic_init(&s->wpp_err, 0);
3660 
3661  if (!avctx->internal->is_copy) {
3662  const AVPacketSideData *sd;
3663 
3664  if (avctx->extradata_size > 0 && avctx->extradata) {
3665  ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
3666  if (ret < 0) {
3667  return ret;
3668  }
3669  }
3670 
3672  if (sd && sd->size > 0)
3674  }
3675 
3676  return 0;
3677 }
3678 
3680 {
3681  HEVCContext *s = avctx->priv_data;
3683  ff_hevc_reset_sei(&s->sei);
3684  ff_dovi_ctx_flush(&s->dovi_ctx);
3685  av_buffer_unref(&s->rpu_buf);
3686  s->max_ra = INT_MAX;
3687  s->eos = 1;
3688 
3689  if (FF_HW_HAS_CB(avctx, flush))
3690  FF_HW_SIMPLE_CALL(avctx, flush);
3691 }
3692 
3693 #define OFFSET(x) offsetof(HEVCContext, x)
3694 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3695 
3696 static const AVOption options[] = {
3697  { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3698  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3699  { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
3700  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3701  { NULL },
3702 };
3703 
3704 static const AVClass hevc_decoder_class = {
3705  .class_name = "HEVC decoder",
3706  .item_name = av_default_item_name,
3707  .option = options,
3708  .version = LIBAVUTIL_VERSION_INT,
3709 };
3710 
3712  .p.name = "hevc",
3713  CODEC_LONG_NAME("HEVC (High Efficiency Video Coding)"),
3714  .p.type = AVMEDIA_TYPE_VIDEO,
3715  .p.id = AV_CODEC_ID_HEVC,
3716  .priv_data_size = sizeof(HEVCContext),
3717  .p.priv_class = &hevc_decoder_class,
3718  .init = hevc_decode_init,
3719  .close = hevc_decode_free,
3721  .flush = hevc_decode_flush,
3722  UPDATE_THREAD_CONTEXT(hevc_update_thread_context),
3723  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3725  .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
3727  .p.profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
3728  .hw_configs = (const AVCodecHWConfigInternal *const []) {
3729 #if CONFIG_HEVC_DXVA2_HWACCEL
3730  HWACCEL_DXVA2(hevc),
3731 #endif
3732 #if CONFIG_HEVC_D3D11VA_HWACCEL
3733  HWACCEL_D3D11VA(hevc),
3734 #endif
3735 #if CONFIG_HEVC_D3D11VA2_HWACCEL
3736  HWACCEL_D3D11VA2(hevc),
3737 #endif
3738 #if CONFIG_HEVC_D3D12VA_HWACCEL
3739  HWACCEL_D3D12VA(hevc),
3740 #endif
3741 #if CONFIG_HEVC_NVDEC_HWACCEL
3742  HWACCEL_NVDEC(hevc),
3743 #endif
3744 #if CONFIG_HEVC_VAAPI_HWACCEL
3745  HWACCEL_VAAPI(hevc),
3746 #endif
3747 #if CONFIG_HEVC_VDPAU_HWACCEL
3748  HWACCEL_VDPAU(hevc),
3749 #endif
3750 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
3751  HWACCEL_VIDEOTOOLBOX(hevc),
3752 #endif
3753 #if CONFIG_HEVC_VULKAN_HWACCEL
3754  HWACCEL_VULKAN(hevc),
3755 #endif
3756  NULL
3757  },
3758 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
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:1358
verify_md5
static int verify_md5(HEVCContext *s, AVFrame *frame)
Definition: hevcdec.c:3254
hwconfig.h
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:382
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:379
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
HEVCFrame::tf
ThreadFrame tf
Definition: hevcdec.h:357
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:98
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:44
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:1838
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:3679
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:2972
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:1220
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:54
HEVCWindow::bottom_offset
unsigned int bottom_offset
Definition: hevc_ps.h:91
SAO_BAND
@ SAO_BAND
Definition: hevcdec.h:159
ff_hevc_profiles
const AVProfile ff_hevc_profiles[]
Definition: profiles.c:95
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:2962
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:122
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:532
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:415
HEVCFrame::needs_fg
int needs_fg
Definition: hevcdec.h:358
int64_t
long long int64_t
Definition: coverity.c:34
mv
static const int8_t mv[256][2]
Definition: 4xm.c:80
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:356
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:125
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:130
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:344
pixdesc.h
HEVCFrame::tab_mvf
MvField * tab_mvf
RefStruct reference.
Definition: hevcdec.h:359
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:373
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:683
HEVCFrame::hwaccel_picture_private
void * hwaccel_picture_private
RefStruct reference.
Definition: hevcdec.h:368
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:522
SAOParams::offset_sign
int offset_sign[3][4]
sao_offset_sign
Definition: hevcdsp.h:36
PAR
#define PAR
Definition: hevcdec.c:3694
INTRA_DC
@ INTRA_DC
Definition: hevcdec.h:121
AVOption
AVOption.
Definition: opt.h:346
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
hls_decode_entry
static int hls_decode_entry(AVCodecContext *avctxt, void *arg)
Definition: hevcdec.c:2529
hevc_decode_free
static av_cold int hevc_decode_free(AVCodecContext *avctx)
Definition: hevcdec.c:3454
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:127
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:322
HEVCLocalContext::ctb_up_left_flag
uint8_t ctb_up_left_flag
Definition: hevcdec.h:417
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
ff_thread_await_progress
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_thread_await_progress() 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_thread_report_progress() has been called on them. This includes draw_edges(). Porting codecs to frame threading
OFFSET
#define OFFSET(x)
Definition: hevcdec.c:3693
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:365
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
ff_thread_get_buffer
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 as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in FFCodec caps_internal and use ff_thread_get_buffer() to allocate frames. Otherwise decode directly into the user-supplied frames. Call ff_thread_report_progress() after some part of the current picture has decoded. A good place to put this is where draw_horiz_band() is called - add this if it isn 't called anywhere
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:418
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:284
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:432
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:375
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:416
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ff_hevc_clear_refs
void ff_hevc_clear_refs(HEVCContext *s)
Mark all frames in DPB as unused for reference.
Definition: hevc_refs.c:66
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:95
fail
#define fail()
Definition: checkasm.h:179
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:1582
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:56
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:88
GetBitContext
Definition: get_bits.h:108
HEVCLocalContext::pu
PredictionUnit pu
Definition: hevcdec.h:428
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:52
decode_lt_rps
static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
Definition: hevcdec.c:265
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:3704
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:180
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
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:118
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
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:595
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:3423
ff_thread_report_progress
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread_frame.c:573
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
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:287
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:390
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:532
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:614
ff_hevc_set_new_ref
int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
Definition: hevc_refs.c:130
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:374
HEVCFrame::rpl
RefPicListTab * rpl
RefStruct reference.
Definition: hevcdec.h:365
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:306
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:90
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:272
SliceHeader::slice_ctb_addr_rs
int slice_ctb_addr_rs
Definition: hevcdec.h:279
frame
static AVFrame * frame
Definition: demux_decode.c:54
ff_thread_ref_frame
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:853
FF_CODEC_PROPERTY_FILM_GRAIN
#define FF_CODEC_PROPERTY_FILM_GRAIN
Definition: avcodec.h:1798
arg
const char * arg
Definition: jacosubdec.c:67
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:2686
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
threadframe.h
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:424
ff_hevc_ps_uninit
void ff_hevc_ps_uninit(HEVCParamSets *ps)
Definition: hevc_ps.c:2026
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:704
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:387
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
ff_dovi_update_cfg
void ff_dovi_update_cfg(DOVIContext *s, const AVDOVIDecoderConfigurationRecord *cfg)
Read the contents of an AVDOVIDecoderConfigurationRecord (usually provided by stream side data) and u...
Definition: dovi_rpu.c:75
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:361
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:367
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:756
hevc_pel_weight
static const uint8_t hevc_pel_weight[65]
Definition: hevcdec.c:55
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:281
BOUNDARY_UPPER_TILE
#define BOUNDARY_UPPER_TILE
Definition: hevcdec.h:434
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:1783
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:87
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:473
HEVCLocalContext::edge_emu_buffer
uint8_t edge_emu_buffer[(MAX_PB_SIZE+7) *EDGE_EMU_BUFFER_STRIDE *2]
Definition: hevcdec.h:421
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:414
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:54
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:523
BOUNDARY_UPPER_SLICE
#define BOUNDARY_UPPER_SLICE
Definition: hevcdec.h:433
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:106
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:354
decode_nal_units
static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
Definition: hevcdec.c:3165
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:1297
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_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: codec_internal.h:69
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:2042
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:1819
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:67
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:1594
AVFilmGrainParams
This structure describes how to handle film grain synthesis in video for specific codecs.
Definition: film_grain_params.h:238
HEVC_MAX_REFS
@ HEVC_MAX_REFS
Definition: hevc.h:119
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:703
SliceHeader::no_output_of_prior_pics_flag
uint8_t no_output_of_prior_pics_flag
Definition: hevcdec.h:227
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:431
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:80
get_format
static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
Definition: hevcdec.c:399
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:2945
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
hls_slice_data
static int hls_slice_data(HEVCContext *s)
Definition: hevcdec.c:2591
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:95
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:426
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:1593
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:355
HEVC_NAL_TRAIL_R
@ HEVC_NAL_TRAIL_R
Definition: hevc.h:30
MODE_INTRA
#define MODE_INTRA
Definition: vp3.c:83
hevc_frame_start
static int hevc_frame_start(HEVCContext *s)
Definition: hevcdec.c:2860
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:255
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:139
AVCodecContext::properties
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:1795
HEVCFrame
Definition: hevcdec.h:354
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
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: avpacket.c:252
HEVCLocalContext::gb
GetBitContext gb
Definition: hevcdec.h:392
internal.h
EPEL_EXTRA_AFTER
#define EPEL_EXTRA_AFTER
Definition: hevcdec.h:61
HEVCFrame::nb_rpl_elems
int nb_rpl_elems
Definition: hevcdec.h:366
HEVCFrame::ctb_count
int ctb_count
Definition: hevcdec.h:362
src2
const pixel * src2
Definition: h264pred_template.c:422
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
SliceHeader::offset
int * offset
Definition: hevcdec.h:259
common.h
HEVCFrame::sequence
uint16_t sequence
A sequence counter, so that old frames are output first after a POC reset.
Definition: hevcdec.h:374
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
SliceHeader::mvd_l1_zero_flag
uint8_t mvd_l1_zero_flag
Definition: hevcdec.h:233
delta
float delta
Definition: vorbis_enc_data.h:430
md5.h
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
ff_hevc_bump_frame
void ff_hevc_bump_frame(HEVCContext *s)
Definition: hevc_refs.c:248
av_always_inline
#define av_always_inline
Definition: attributes.h:49
HEVC_SLICE_P
@ HEVC_SLICE_P
Definition: hevc.h:97
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:603
PF_L0
@ PF_L0
Definition: hevcdec.h:114
EDGE_EMU_BUFFER_STRIDE
#define EDGE_EMU_BUFFER_STRIDE
Definition: hevcdec.h:67
tab_mode_idx
static const uint8_t tab_mode_idx[]
Definition: hevcdec.c:2138
cabac_functions.h
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:576
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
ff_hevc_sao_eo_class_decode
int ff_hevc_sao_eo_class_decode(HEVCLocalContext *lc)
Definition: hevc_cabac.c:563
HEVCLocalContext::qp_y
int8_t qp_y
Definition: hevcdec.h:407
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
HEVC_MAX_PPS_COUNT
@ HEVC_MAX_PPS_COUNT
Definition: hevc.h:114
av_buffer_replace
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:233
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:702
HEVC_NAL_TSA_R
@ HEVC_NAL_TSA_R
Definition: hevc.h:32
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:612
ff_hevc_cu_qp_delta_abs
int ff_hevc_cu_qp_delta_abs(HEVCLocalContext *lc)
Definition: hevc_cabac.c:596
SliceHeader::list_entry_lx
unsigned int list_entry_lx[2][32]
Definition: hevcdec.h:224
AVCodecContext::height
int height
Definition: avcodec.h:618
hevc_decode_extradata
static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
Definition: hevcdec.c:3323
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
av_md5_final
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition: md5.c:188
hevc_decode_init
static av_cold int hevc_decode_init(AVCodecContext *avctx)
Definition: hevcdec.c:3633
HEVCFrame::poc
int poc
Definition: hevcdec.h:363
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:666
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
HWACCEL_VIDEOTOOLBOX
#define HWACCEL_VIDEOTOOLBOX(codec)
Definition: hwconfig.h:74
hevc.h
SAOParams
Definition: hevcdsp.h:34
SliceHeader::short_term_rps
const ShortTermRPS * short_term_rps
Definition: hevcdec.h:221
stride
#define stride
Definition: h264pred_template.c:537
ff_dovi_rpu_parse
int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size)
Parse the contents of a Dovi RPU NAL and update the parsed values in the DOVIContext struct.
Definition: dovi_rpu.c:198
HEVC_NAL_VPS
@ HEVC_NAL_VPS
Definition: hevc.h:61
SliceHeader::cu_chroma_qp_offset_enabled_flag
uint8_t cu_chroma_qp_offset_enabled_flag
Definition: hevcdec.h:250
HEVC_NAL_IDR_W_RADL
@ HEVC_NAL_IDR_W_RADL
Definition: hevc.h:48
ff_hevc_unref_frame
void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
Definition: hevc_refs.c:33
ret
ret
Definition: filter_design.txt:187
H2645NAL::raw_data
const uint8_t * raw_data
Definition: h2645_parse.h:45
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
PRED_L1
@ PRED_L1
Definition: hevcdec.h:108
PredictionUnit::mvd
Mv mvd
Definition: hevcdec.h:319
SliceHeader::disable_deblocking_filter_flag
uint8_t disable_deblocking_filter_flag
slice_header_disable_deblocking_filter_flag
Definition: hevcdec.h:236
ff_hevc_dsp_init
void ff_hevc_dsp_init(HEVCDSPContext *hevcdsp, int bit_depth)
Definition: hevcdsp.c:128
HEVCLocalContext::edge_emu_buffer2
uint8_t edge_emu_buffer2[(MAX_PB_SIZE+7) *EDGE_EMU_BUFFER_STRIDE *2]
Definition: hevcdec.h:423
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
hevc_init_context
static av_cold int hevc_init_context(AVCodecContext *avctx)
Definition: hevcdec.c:3499
ff_hevc_save_states
void ff_hevc_save_states(HEVCLocalContext *lc, int ctb_addr_ts)
Definition: hevc_cabac.c:402
ff_refstruct_replace
void ff_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:160
pos
unsigned int pos
Definition: spdifenc.c:413
SliceHeader::max_num_merge_cand
uint8_t max_num_merge_cand
5 - 5_minus_max_num_merge_cand
Definition: hevcdec.h:255
ff_thread_finish_setup
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 as well as code calling up to before the decode process starts Call ff_thread_finish_setup() afterwards. If some code can 't be moved
chroma_mc_bi
static void chroma_mc_bi(HEVCLocalContext *lc, uint8_t *dst0, ptrdiff_t dststride, const AVFrame *ref0, const AVFrame *ref1, int x_off, int y_off, int block_w, int block_h, const MvField *current_mv, int cidx)
8.5.3.2.2.2 Chroma sample bidirectional interpolation process
Definition: hevcdec.c:1773
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:482
HEVC_NAL_EOS_NUT
@ HEVC_NAL_EOS_NUT
Definition: hevc.h:65
ff_hevc_frame_nb_refs
int ff_hevc_frame_nb_refs(const HEVCContext *s)
Get the number of candidate references for the current frame.
Definition: hevc_refs.c:535
HEVCLocalContext::boundary_flags
int boundary_flags
Definition: hevcdec.h:437
ff_slice_thread_allocz_entries
int ff_slice_thread_allocz_entries(AVCodecContext *avctx, int count)
Definition: pthread_slice.c:240
U
#define U(x)
Definition: vpx_arith.h:37
HEVC_NAL_TRAIL_N
@ HEVC_NAL_TRAIL_N
Definition: hevc.h:29
HWACCEL_VULKAN
#define HWACCEL_VULKAN(codec)
Definition: hwconfig.h:76
hls_decode_entry_wpp
static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *hevc_lclist, int job, int self_id)
Definition: hevcdec.c:2598
LongTermRPS
Definition: hevcdec.h:182
SliceHeader::slice_type
enum HEVCSliceType slice_type
Definition: hevcdec.h:208
ff_hevc_flush_dpb
void ff_hevc_flush_dpb(HEVCContext *s)
Drop all frames currently in DPB.
Definition: hevc_refs.c:75
hls_coding_quadtree
static int hls_coding_quadtree(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size, int cb_depth)
Definition: hevcdec.c:2395
HEVC_NAL_AUD
@ HEVC_NAL_AUD
Definition: hevc.h:64
AV_FRAME_DATA_DYNAMIC_HDR_PLUS
@ AV_FRAME_DATA_DYNAMIC_HDR_PLUS
HDR dynamic metadata associated with a video frame.
Definition: frame.h:159
AVCodecContext
main external API structure.
Definition: avcodec.h:445
ff_hevc_hls_filters
void ff_hevc_hls_filters(HEVCLocalContext *lc, int x_ctb, int y_ctb, int ctb_size)
Definition: hevc_filter.c:888
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1601
get_ue_golomb_31
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:120
ff_hevc_mvp_lx_flag_decode
int ff_hevc_mvp_lx_flag_decode(HEVCLocalContext *lc)
Definition: hevc_cabac.c:788
SliceHeader::slice_qp
int8_t slice_qp
Definition: hevcdec.h:263
AV_FILM_GRAIN_PARAMS_H274
@ AV_FILM_GRAIN_PARAMS_H274
The union is valid when interpreted as AVFilmGrainH274Params (codec.h274)
Definition: film_grain_params.h:35
SUBDIVIDE
#define SUBDIVIDE(x, y, idx)
ff_h274_film_grain_params_supported
static int ff_h274_film_grain_params_supported(int model_id, enum AVPixelFormat pix_fmt)
Check whether ff_h274_apply_film_grain() supports the given parameter combination.
Definition: h274.h:49
PredictionUnit::merge_flag
uint8_t merge_flag
Definition: hevcdec.h:320
av_md5_alloc
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:50
AVRational::den
int den
Denominator.
Definition: rational.h:60
pred_weight_table
static int pred_weight_table(HEVCContext *s, GetBitContext *gb)
Definition: hevcdec.c:149
SliceHeader::slice_cr_qp_offset
int slice_cr_qp_offset
Definition: hevcdec.h:244
export_stream_params_from_sei
static int export_stream_params_from_sei(HEVCContext *s)
Definition: hevcdec.c:379
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
FF_HW_CALL
#define FF_HW_CALL(avctx, function,...)
Definition: hwaccel_internal.h:171
HEVCContext
Definition: hevcdec.h:440
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1639
CodingUnit::pred_mode
enum PredMode pred_mode
PredMode.
Definition: hevcdec.h:286
SliceHeader::pic_order_cnt_lsb
int pic_order_cnt_lsb
Definition: hevcdec.h:210
HEVCLocalContext::qPy_pred
int qPy_pred
Definition: hevcdec.h:410
SCAN_DIAG
@ SCAN_DIAG
Definition: hevcdec.h:172
SliceHeader::rpl_modification_flag
uint8_t rpl_modification_flag[2]
Definition: hevcdec.h:226
ff_hevc_mpm_idx_decode
int ff_hevc_mpm_idx_decode(HEVCLocalContext *lc)
Definition: hevc_cabac.c:717
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
SAOParams::type_idx
uint8_t type_idx[3]
sao_type_idx
Definition: hevcdsp.h:44
ff_hevc_set_qPy
void ff_hevc_set_qPy(HEVCLocalContext *lc, int xBase, int yBase, int log2_cb_size)
Definition: hevc_filter.c:119
HEVCWindow::right_offset
unsigned int right_offset
Definition: hevc_ps.h:89
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
FF_CODEC_PROPERTY_CLOSED_CAPTIONS
#define FF_CODEC_PROPERTY_CLOSED_CAPTIONS
Definition: avcodec.h:1797
hevc_decode_frame
static int hevc_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_output, AVPacket *avpkt)
Definition: hevcdec.c:3350
av_md5_update
void av_md5_update(AVMD5 *ctx, const uint8_t *src, size_t len)
Update hash value.
Definition: md5.c:153
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
ff_hevc_inter_pred_idc_decode
int ff_hevc_inter_pred_idc_decode(HEVCLocalContext *lc, int nPbW, int nPbH)
Definition: hevc_cabac.c:762
HEVCLocalContext::tu
TransformUnit tu
Definition: hevcdec.h:412
hls_slice_header
static int hls_slice_header(HEVCContext *s)
Definition: hevcdec.c:595
ff_hevc_part_mode_decode
int ff_hevc_part_mode_decode(HEVCLocalContext *lc, int log2_cb_size)
Definition: hevc_cabac.c:670
CodingUnit::y
int y
Definition: hevcdec.h:284
src0
const pixel *const src0
Definition: h264pred_template.c:420
set_side_data
static int set_side_data(HEVCContext *s)
Definition: hevcdec.c:2773
av_log_once
void av_log_once(void *avcl, int initial_level, int subsequent_level, int *state, const char *fmt,...)
Definition: log.c:422
MvField::mv
Mv mv[2]
mvL0, vvL1
Definition: hevcdec.h:301
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:633
desc
const char * desc
Definition: libsvtav1.c:75
Mv
Definition: hevcdec.h:295
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
HEVC_NAL_SPS
@ HEVC_NAL_SPS
Definition: hevc.h:62
MvField::ref_idx
int8_t ref_idx[2]
refIdxL0, refIdxL1
Definition: hevcdec.h:302
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
PRED_L0
@ PRED_L0
Definition: hevcdec.h:107
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
get_bitsz
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:351
ff_hevc_split_transform_flag_decode
int ff_hevc_split_transform_flag_decode(HEVCLocalContext *lc, int log2_trafo_size)
Definition: hevc_cabac.c:831
HEVCVPS
Definition: hevc_ps.h:154
get_ue_golomb_long
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:104
EPEL_EXTRA
#define EPEL_EXTRA
Definition: hevcdec.h:62
ff_h2645_sei_ctx_replace
int ff_h2645_sei_ctx_replace(H2645SEI *dst, const H2645SEI *src)
Definition: h2645_sei.c:493
s0
#define s0
Definition: regdef.h:37
HEVCSPS
Definition: hevc_ps.h:189
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:250
ff_hevc_sao_offset_abs_decode
int ff_hevc_sao_offset_abs_decode(HEVCLocalContext *lc)
Definition: hevc_cabac.c:548
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
CodingUnit::part_mode
enum PartMode part_mode
PartMode.
Definition: hevcdec.h:287
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
SliceHeader::tc_offset
int tc_offset
tc_offset_div2 * 2
Definition: hevcdec.h:253
ff_hevc_reset_sei
static void ff_hevc_reset_sei(HEVCSEI *sei)
Reset SEI values that are stored on the Context.
Definition: hevc_sei.h:106
LongTermRPS::nb_refs
uint8_t nb_refs
Definition: hevcdec.h:186
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
TransformUnit::cross_pf
uint8_t cross_pf
Definition: hevcdec.h:338
HEVCLocalContext::cu
CodingUnit cu
Definition: hevcdec.h:427
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
av_fast_malloc
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:555
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
SliceHeader::pps_id
unsigned int pps_id
address (in raster order) of the first block in the current slice segment
Definition: hevcdec.h:201
ff_hevc_decoder
const FFCodec ff_hevc_decoder
Definition: hevcdec.c:3711
HWACCEL_VAAPI
#define HWACCEL_VAAPI(codec)
Definition: hwconfig.h:70
HEVCParamSets::vps_list
const HEVCVPS * vps_list[HEVC_MAX_VPS_COUNT]
RefStruct references.
Definition: hevc_ps.h:442
FFMAX3
#define FFMAX3(a, b, c)
Definition: macros.h:48
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
int32_t
int32_t
Definition: audioconvert.c:56
hls_pcm_sample
static int hls_pcm_sample(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:1494
ff_hevc_decode_short_term_rps
int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx, ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
Definition: hevc_ps.c:101
PredictionUnit::mpm_idx
int mpm_idx
Definition: hevcdec.h:316
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
HEVC_NAL_FD_NUT
@ HEVC_NAL_FD_NUT
Definition: hevc.h:67
PredictionUnit::chroma_mode_c
uint8_t chroma_mode_c[4]
Definition: hevcdec.h:322
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
skip_bytes
static const av_unused uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
Definition: cabac_functions.h:203
PredictionUnit::intra_pred_mode
uint8_t intra_pred_mode[4]
Definition: hevcdec.h:318
ff_hevc_decode_nal_pps
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:1759
TransformUnit::is_cu_chroma_qp_offset_coded
uint8_t is_cu_chroma_qp_offset_coded
Definition: hevcdec.h:335
h
h
Definition: vp9dsp_template.c:2038
BOUNDARY_LEFT_SLICE
#define BOUNDARY_LEFT_SLICE
Definition: hevcdec.h:431
SliceHeader::slice_qp_delta
int slice_qp_delta
Definition: hevcdec.h:242
SliceHeader::slice_addr
unsigned int slice_addr
Definition: hevcdec.h:206
SliceHeader::use_integer_mv_flag
uint8_t use_integer_mv_flag
Definition: hevcdec.h:256
avstring.h
HEVC_NAL_EOB_NUT
@ HEVC_NAL_EOB_NUT
Definition: hevc.h:66
atomic_init
#define atomic_init(obj, value)
Definition: stdatomic.h:33
ff_hevc_merge_flag_decode
int ff_hevc_merge_flag_decode(HEVCLocalContext *lc)
Definition: hevc_cabac.c:757
TransformUnit::intra_pred_mode_c
int intra_pred_mode_c
Definition: hevcdec.h:332
AVDISCARD_NONREF
@ AVDISCARD_NONREF
discard all non reference
Definition: defs.h:215
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
AV_FILM_GRAIN_PARAMS_AV1
@ AV_FILM_GRAIN_PARAMS_AV1
The union is valid when interpreted as AVFilmGrainAOMParams (codec.aom)
Definition: film_grain_params.h:30
MD5_PRI_ARG
#define MD5_PRI_ARG(buf)
int
int
Definition: ffmpeg_filter.c:409
HEVCLocalContext::end_of_tiles_y
int end_of_tiles_y
Definition: hevcdec.h:419
AVFilmGrainParams::type
enum AVFilmGrainParamsType type
Specifies the codec for which this structure is valid.
Definition: film_grain_params.h:242
luma_mc_bi
static void luma_mc_bi(HEVCLocalContext *lc, uint8_t *dst, ptrdiff_t dststride, const AVFrame *ref0, const Mv *mv0, int x_off, int y_off, int block_w, int block_h, const AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
8.5.3.2.2.1 Luma sample bidirectional interpolation process
Definition: hevcdec.c:1613
CodingUnit::intra_split_flag
uint8_t intra_split_flag
IntraSplitFlag.
Definition: hevcdec.h:290
SHIFT_CTB_WPP
#define SHIFT_CTB_WPP
Definition: hevcdec.h:46
ff_hevc_ref_idx_lx_decode
int ff_hevc_ref_idx_lx_decode(HEVCLocalContext *lc, int num_ref_idx_lx)
Definition: hevc_cabac.c:772
av_color_transfer_name
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition: pixdesc.c:3317
PART_2NxN
@ PART_2NxN
Definition: hevcdec.h:91
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_rpu.c:83
AV_CODEC_EXPORT_DATA_FILM_GRAIN
#define AV_CODEC_EXPORT_DATA_FILM_GRAIN
Decoding only.
Definition: avcodec.h:420
SliceHeader::long_term_rps
LongTermRPS long_term_rps
Definition: hevcdec.h:223
hls_transform_tree
static int hls_transform_tree(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 trafo_depth, int blk_idx, const int *base_cbf_cb, const int *base_cbf_cr)
Definition: hevcdec.c:1371
HEVCLocalContext::cc
CABACContext cc
Definition: hevcdec.h:393
TransformUnit::cu_qp_offset_cr
int8_t cu_qp_offset_cr
Definition: hevcdec.h:337
ff_hevc_sao_type_idx_decode
int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc)
Definition: hevc_cabac.c:528
ff_dovi_ctx_replace
void ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0)
Definition: dovi_rpu.c:65
hls_cross_component_pred
static int hls_cross_component_pred(HEVCLocalContext *lc, int idx)
Definition: hevcdec.c:1133
options
static const AVOption options[]
Definition: hevcdec.c:3696
AVDOVIDecoderConfigurationRecord
Definition: dovi_meta.h:52
HEVCParamSets
Definition: hevc_ps.h:441