FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/attributes.h"
27 #include "libavutil/common.h"
28 #include "libavutil/display.h"
29 #include "libavutil/internal.h"
31 #include "libavutil/md5.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/stereo3d.h"
35 
36 #include "bswapdsp.h"
37 #include "bytestream.h"
38 #include "cabac_functions.h"
39 #include "golomb.h"
40 #include "hevc.h"
41 #include "hevc_data.h"
42 #include "hevcdec.h"
43 #include "profiles.h"
44 
45 const uint8_t ff_hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
46 
47 /**
48  * NOTE: Each function hls_foo correspond to the function foo in the
49  * specification (HLS stands for High Level Syntax).
50  */
51 
52 /**
53  * Section 5.7
54  */
55 
56 /* free everything allocated by pic_arrays_init() */
58 {
59  av_freep(&s->sao);
60  av_freep(&s->deblock);
61 
62  av_freep(&s->skip_flag);
64 
65  av_freep(&s->tab_ipm);
66  av_freep(&s->cbf_luma);
67  av_freep(&s->is_pcm);
68 
69  av_freep(&s->qp_y_tab);
72 
74  av_freep(&s->vertical_bs);
75 
77  av_freep(&s->sh.size);
78  av_freep(&s->sh.offset);
79 
82 }
83 
84 /* allocate arrays that depend on frame dimensions */
85 static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
86 {
87  int log2_min_cb_size = sps->log2_min_cb_size;
88  int width = sps->width;
89  int height = sps->height;
90  int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
91  ((height >> log2_min_cb_size) + 1);
92  int ctb_count = sps->ctb_width * sps->ctb_height;
93  int min_pu_size = sps->min_pu_width * sps->min_pu_height;
94 
95  s->bs_width = (width >> 2) + 1;
96  s->bs_height = (height >> 2) + 1;
97 
98  s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
99  s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
100  if (!s->sao || !s->deblock)
101  goto fail;
102 
105  if (!s->skip_flag || !s->tab_ct_depth)
106  goto fail;
107 
109  s->tab_ipm = av_mallocz(min_pu_size);
110  s->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
111  if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
112  goto fail;
113 
114  s->filter_slice_edges = av_mallocz(ctb_count);
115  s->tab_slice_address = av_malloc_array(pic_size_in_ctb,
116  sizeof(*s->tab_slice_address));
117  s->qp_y_tab = av_malloc_array(pic_size_in_ctb,
118  sizeof(*s->qp_y_tab));
119  if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
120  goto fail;
121 
124  if (!s->horizontal_bs || !s->vertical_bs)
125  goto fail;
126 
127  s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
129  s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
131  if (!s->tab_mvf_pool || !s->rpl_tab_pool)
132  goto fail;
133 
134  return 0;
135 
136 fail:
137  pic_arrays_free(s);
138  return AVERROR(ENOMEM);
139 }
140 
142 {
143  int i = 0;
144  int j = 0;
145  uint8_t luma_weight_l0_flag[16];
146  uint8_t chroma_weight_l0_flag[16];
147  uint8_t luma_weight_l1_flag[16];
148  uint8_t chroma_weight_l1_flag[16];
149  int luma_log2_weight_denom;
150 
151  luma_log2_weight_denom = get_ue_golomb_long(gb);
152  if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7)
153  av_log(s->avctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
154  s->sh.luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
155  if (s->ps.sps->chroma_format_idc != 0) {
156  int delta = get_se_golomb(gb);
157  s->sh.chroma_log2_weight_denom = av_clip_uintp2(s->sh.luma_log2_weight_denom + delta, 3);
158  }
159 
160  for (i = 0; i < s->sh.nb_refs[L0]; i++) {
161  luma_weight_l0_flag[i] = get_bits1(gb);
162  if (!luma_weight_l0_flag[i]) {
163  s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
164  s->sh.luma_offset_l0[i] = 0;
165  }
166  }
167  if (s->ps.sps->chroma_format_idc != 0) {
168  for (i = 0; i < s->sh.nb_refs[L0]; i++)
169  chroma_weight_l0_flag[i] = get_bits1(gb);
170  } else {
171  for (i = 0; i < s->sh.nb_refs[L0]; i++)
172  chroma_weight_l0_flag[i] = 0;
173  }
174  for (i = 0; i < s->sh.nb_refs[L0]; i++) {
175  if (luma_weight_l0_flag[i]) {
176  int delta_luma_weight_l0 = get_se_golomb(gb);
177  s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
178  s->sh.luma_offset_l0[i] = get_se_golomb(gb);
179  }
180  if (chroma_weight_l0_flag[i]) {
181  for (j = 0; j < 2; j++) {
182  int delta_chroma_weight_l0 = get_se_golomb(gb);
183  int delta_chroma_offset_l0 = get_se_golomb(gb);
184 
185  if ( (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
186  || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
187  return AVERROR_INVALIDDATA;
188  }
189 
190  s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
191  s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
192  >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
193  }
194  } else {
195  s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
196  s->sh.chroma_offset_l0[i][0] = 0;
197  s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
198  s->sh.chroma_offset_l0[i][1] = 0;
199  }
200  }
201  if (s->sh.slice_type == HEVC_SLICE_B) {
202  for (i = 0; i < s->sh.nb_refs[L1]; i++) {
203  luma_weight_l1_flag[i] = get_bits1(gb);
204  if (!luma_weight_l1_flag[i]) {
205  s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
206  s->sh.luma_offset_l1[i] = 0;
207  }
208  }
209  if (s->ps.sps->chroma_format_idc != 0) {
210  for (i = 0; i < s->sh.nb_refs[L1]; i++)
211  chroma_weight_l1_flag[i] = get_bits1(gb);
212  } else {
213  for (i = 0; i < s->sh.nb_refs[L1]; i++)
214  chroma_weight_l1_flag[i] = 0;
215  }
216  for (i = 0; i < s->sh.nb_refs[L1]; i++) {
217  if (luma_weight_l1_flag[i]) {
218  int delta_luma_weight_l1 = get_se_golomb(gb);
219  s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
220  s->sh.luma_offset_l1[i] = get_se_golomb(gb);
221  }
222  if (chroma_weight_l1_flag[i]) {
223  for (j = 0; j < 2; j++) {
224  int delta_chroma_weight_l1 = get_se_golomb(gb);
225  int delta_chroma_offset_l1 = get_se_golomb(gb);
226 
227  if ( (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
228  || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
229  return AVERROR_INVALIDDATA;
230  }
231 
232  s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
233  s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
234  >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
235  }
236  } else {
237  s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
238  s->sh.chroma_offset_l1[i][0] = 0;
239  s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
240  s->sh.chroma_offset_l1[i][1] = 0;
241  }
242  }
243  }
244  return 0;
245 }
246 
248 {
249  const HEVCSPS *sps = s->ps.sps;
250  int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
251  int prev_delta_msb = 0;
252  unsigned int nb_sps = 0, nb_sh;
253  int i;
254 
255  rps->nb_refs = 0;
257  return 0;
258 
259  if (sps->num_long_term_ref_pics_sps > 0)
260  nb_sps = get_ue_golomb_long(gb);
261  nb_sh = get_ue_golomb_long(gb);
262 
263  if (nb_sps > sps->num_long_term_ref_pics_sps)
264  return AVERROR_INVALIDDATA;
265  if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
266  return AVERROR_INVALIDDATA;
267 
268  rps->nb_refs = nb_sh + nb_sps;
269 
270  for (i = 0; i < rps->nb_refs; i++) {
271  uint8_t delta_poc_msb_present;
272 
273  if (i < nb_sps) {
274  uint8_t lt_idx_sps = 0;
275 
276  if (sps->num_long_term_ref_pics_sps > 1)
277  lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
278 
279  rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
280  rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
281  } else {
282  rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
283  rps->used[i] = get_bits1(gb);
284  }
285 
286  delta_poc_msb_present = get_bits1(gb);
287  if (delta_poc_msb_present) {
288  int64_t delta = get_ue_golomb_long(gb);
289  int64_t poc;
290 
291  if (i && i != nb_sps)
292  delta += prev_delta_msb;
293 
294  poc = rps->poc[i] + s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
295  if (poc != (int32_t)poc)
296  return AVERROR_INVALIDDATA;
297  rps->poc[i] = poc;
298  prev_delta_msb = delta;
299  }
300  }
301 
302  return 0;
303 }
304 
305 static void export_stream_params(AVCodecContext *avctx, const HEVCParamSets *ps,
306  const HEVCSPS *sps)
307 {
308  const HEVCVPS *vps = (const HEVCVPS*)ps->vps_list[sps->vps_id]->data;
309  unsigned int num = 0, den = 0;
310 
311  avctx->pix_fmt = sps->pix_fmt;
312  avctx->coded_width = sps->width;
313  avctx->coded_height = sps->height;
314  avctx->width = sps->output_width;
315  avctx->height = sps->output_height;
317  avctx->profile = sps->ptl.general_ptl.profile_idc;
318  avctx->level = sps->ptl.general_ptl.level_idc;
319 
320  ff_set_sar(avctx, sps->vui.sar);
321 
325  else
326  avctx->color_range = AVCOL_RANGE_MPEG;
327 
329  avctx->color_primaries = sps->vui.colour_primaries;
330  avctx->color_trc = sps->vui.transfer_characteristic;
331  avctx->colorspace = sps->vui.matrix_coeffs;
332  } else {
336  }
337 
338  if (vps->vps_timing_info_present_flag) {
339  num = vps->vps_num_units_in_tick;
340  den = vps->vps_time_scale;
341  } else if (sps->vui.vui_timing_info_present_flag) {
342  num = sps->vui.vui_num_units_in_tick;
343  den = sps->vui.vui_time_scale;
344  }
345 
346  if (num != 0 && den != 0)
347  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
348  num, den, 1 << 30);
349 }
350 
351 static int set_sps(HEVCContext *s, const HEVCSPS *sps, enum AVPixelFormat pix_fmt)
352 {
353  #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + CONFIG_HEVC_D3D11VA_HWACCEL + CONFIG_HEVC_VAAPI_HWACCEL + CONFIG_HEVC_VDPAU_HWACCEL)
355  int ret, i;
356 
357  pic_arrays_free(s);
358  s->ps.sps = NULL;
359  s->ps.vps = NULL;
360 
361  if (!sps)
362  return 0;
363 
364  ret = pic_arrays_init(s, sps);
365  if (ret < 0)
366  goto fail;
367 
368  export_stream_params(s->avctx, &s->ps, sps);
369 
370  switch (sps->pix_fmt) {
371  case AV_PIX_FMT_YUV420P:
372  case AV_PIX_FMT_YUVJ420P:
373 #if CONFIG_HEVC_DXVA2_HWACCEL
374  *fmt++ = AV_PIX_FMT_DXVA2_VLD;
375 #endif
376 #if CONFIG_HEVC_D3D11VA_HWACCEL
377  *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
378 #endif
379 #if CONFIG_HEVC_VAAPI_HWACCEL
380  *fmt++ = AV_PIX_FMT_VAAPI;
381 #endif
382 #if CONFIG_HEVC_VDPAU_HWACCEL
383  *fmt++ = AV_PIX_FMT_VDPAU;
384 #endif
385  break;
387 #if CONFIG_HEVC_DXVA2_HWACCEL
388  *fmt++ = AV_PIX_FMT_DXVA2_VLD;
389 #endif
390 #if CONFIG_HEVC_D3D11VA_HWACCEL
391  *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
392 #endif
393 #if CONFIG_HEVC_VAAPI_HWACCEL
394  *fmt++ = AV_PIX_FMT_VAAPI;
395 #endif
396  break;
397  }
398 
399  if (pix_fmt == AV_PIX_FMT_NONE) {
400  *fmt++ = sps->pix_fmt;
401  *fmt = AV_PIX_FMT_NONE;
402 
403  ret = ff_thread_get_format(s->avctx, pix_fmts);
404  if (ret < 0)
405  goto fail;
406  s->avctx->pix_fmt = ret;
407  }
408  else {
409  s->avctx->pix_fmt = pix_fmt;
410  }
411 
412  ff_hevc_pred_init(&s->hpc, sps->bit_depth);
413  ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
414  ff_videodsp_init (&s->vdsp, sps->bit_depth);
415 
416  for (i = 0; i < 3; i++) {
419  }
420 
421  if (sps->sao_enabled && !s->avctx->hwaccel) {
422  int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
423  int c_idx;
424 
425  for(c_idx = 0; c_idx < c_count; c_idx++) {
426  int w = sps->width >> sps->hshift[c_idx];
427  int h = sps->height >> sps->vshift[c_idx];
428  s->sao_pixel_buffer_h[c_idx] =
429  av_malloc((w * 2 * sps->ctb_height) <<
430  sps->pixel_shift);
431  s->sao_pixel_buffer_v[c_idx] =
432  av_malloc((h * 2 * sps->ctb_width) <<
433  sps->pixel_shift);
434  }
435  }
436 
437  s->ps.sps = sps;
438  s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
439 
440  return 0;
441 
442 fail:
443  pic_arrays_free(s);
444  s->ps.sps = NULL;
445  return ret;
446 }
447 
449 {
450  GetBitContext *gb = &s->HEVClc->gb;
451  SliceHeader *sh = &s->sh;
452  int i, ret;
453 
454  // Coded parameters
456  if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
457  s->seq_decode = (s->seq_decode + 1) & 0xff;
458  s->max_ra = INT_MAX;
459  if (IS_IDR(s))
461  }
463  if (IS_IRAP(s))
465 
466  sh->pps_id = get_ue_golomb_long(gb);
467  if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
468  av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
469  return AVERROR_INVALIDDATA;
470  }
471  if (!sh->first_slice_in_pic_flag &&
472  s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
473  av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
474  return AVERROR_INVALIDDATA;
475  }
476  s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
477  if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
479 
480  if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
481  const HEVCSPS* last_sps = s->ps.sps;
482  s->ps.sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
483  if (last_sps && IS_IRAP(s) && s->nal_unit_type != HEVC_NAL_CRA_NUT) {
484  if (s->ps.sps->width != last_sps->width || s->ps.sps->height != last_sps->height ||
486  last_sps->temporal_layer[last_sps->max_sub_layers - 1].max_dec_pic_buffering)
488  }
490  ret = set_sps(s, s->ps.sps, AV_PIX_FMT_NONE);
491  if (ret < 0)
492  return ret;
493 
494  s->seq_decode = (s->seq_decode + 1) & 0xff;
495  s->max_ra = INT_MAX;
496  }
497 
499  if (!sh->first_slice_in_pic_flag) {
500  int slice_address_length;
501 
504 
505  slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
506  s->ps.sps->ctb_height);
507  sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
508  if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
510  "Invalid slice segment address: %u.\n",
511  sh->slice_segment_addr);
512  return AVERROR_INVALIDDATA;
513  }
514 
515  if (!sh->dependent_slice_segment_flag) {
516  sh->slice_addr = sh->slice_segment_addr;
517  s->slice_idx++;
518  }
519  } else {
520  sh->slice_segment_addr = sh->slice_addr = 0;
521  s->slice_idx = 0;
522  s->slice_initialized = 0;
523  }
524 
525  if (!sh->dependent_slice_segment_flag) {
526  s->slice_initialized = 0;
527 
528  for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
529  skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
530 
531  sh->slice_type = get_ue_golomb_long(gb);
532  if (!(sh->slice_type == HEVC_SLICE_I ||
533  sh->slice_type == HEVC_SLICE_P ||
534  sh->slice_type == HEVC_SLICE_B)) {
535  av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
536  sh->slice_type);
537  return AVERROR_INVALIDDATA;
538  }
539  if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I) {
540  av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
541  return AVERROR_INVALIDDATA;
542  }
543 
544  // when flag is not present, picture is inferred to be output
545  sh->pic_output_flag = 1;
547  sh->pic_output_flag = get_bits1(gb);
548 
550  sh->colour_plane_id = get_bits(gb, 2);
551 
552  if (!IS_IDR(s)) {
553  int poc, pos;
554 
557  if (!sh->first_slice_in_pic_flag && poc != s->poc) {
559  "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
561  return AVERROR_INVALIDDATA;
562  poc = s->poc;
563  }
564  s->poc = poc;
565 
567  pos = get_bits_left(gb);
569  ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
570  if (ret < 0)
571  return ret;
572 
573  sh->short_term_rps = &sh->slice_rps;
574  } else {
575  int numbits, rps_idx;
576 
577  if (!s->ps.sps->nb_st_rps) {
578  av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
579  return AVERROR_INVALIDDATA;
580  }
581 
582  numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
583  rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
584  sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
585  }
587 
588  pos = get_bits_left(gb);
589  ret = decode_lt_rps(s, &sh->long_term_rps, gb);
590  if (ret < 0) {
591  av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
593  return AVERROR_INVALIDDATA;
594  }
596 
599  else
601  } else {
602  s->sh.short_term_rps = NULL;
603  s->poc = 0;
604  }
605 
606  /* 8.3.1 */
607  if (sh->first_slice_in_pic_flag && s->temporal_id == 0 &&
615  s->pocTid0 = s->poc;
616 
617  if (s->ps.sps->sao_enabled) {
619  if (s->ps.sps->chroma_format_idc) {
622  }
623  } else {
627  }
628 
629  sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
630  if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
631  int nb_refs;
632 
634  if (sh->slice_type == HEVC_SLICE_B)
636 
637  if (get_bits1(gb)) { // num_ref_idx_active_override_flag
638  sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
639  if (sh->slice_type == HEVC_SLICE_B)
640  sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
641  }
642  if (sh->nb_refs[L0] > HEVC_MAX_REFS || sh->nb_refs[L1] > HEVC_MAX_REFS) {
643  av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
644  sh->nb_refs[L0], sh->nb_refs[L1]);
645  return AVERROR_INVALIDDATA;
646  }
647 
648  sh->rpl_modification_flag[0] = 0;
649  sh->rpl_modification_flag[1] = 0;
650  nb_refs = ff_hevc_frame_nb_refs(s);
651  if (!nb_refs) {
652  av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
653  return AVERROR_INVALIDDATA;
654  }
655 
656  if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
657  sh->rpl_modification_flag[0] = get_bits1(gb);
658  if (sh->rpl_modification_flag[0]) {
659  for (i = 0; i < sh->nb_refs[L0]; i++)
660  sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
661  }
662 
663  if (sh->slice_type == HEVC_SLICE_B) {
664  sh->rpl_modification_flag[1] = get_bits1(gb);
665  if (sh->rpl_modification_flag[1] == 1)
666  for (i = 0; i < sh->nb_refs[L1]; i++)
667  sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
668  }
669  }
670 
671  if (sh->slice_type == HEVC_SLICE_B)
672  sh->mvd_l1_zero_flag = get_bits1(gb);
673 
675  sh->cabac_init_flag = get_bits1(gb);
676  else
677  sh->cabac_init_flag = 0;
678 
679  sh->collocated_ref_idx = 0;
681  sh->collocated_list = L0;
682  if (sh->slice_type == HEVC_SLICE_B)
683  sh->collocated_list = !get_bits1(gb);
684 
685  if (sh->nb_refs[sh->collocated_list] > 1) {
687  if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
689  "Invalid collocated_ref_idx: %d.\n",
690  sh->collocated_ref_idx);
691  return AVERROR_INVALIDDATA;
692  }
693  }
694  }
695 
696  if ((s->ps.pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
698  int ret = pred_weight_table(s, gb);
699  if (ret < 0)
700  return ret;
701  }
702 
704  if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
706  "Invalid number of merging MVP candidates: %d.\n",
707  sh->max_num_merge_cand);
708  return AVERROR_INVALIDDATA;
709  }
710  }
711 
712  sh->slice_qp_delta = get_se_golomb(gb);
713 
717  } else {
718  sh->slice_cb_qp_offset = 0;
719  sh->slice_cr_qp_offset = 0;
720  }
721 
724  else
726 
728  int deblocking_filter_override_flag = 0;
729 
731  deblocking_filter_override_flag = get_bits1(gb);
732 
733  if (deblocking_filter_override_flag) {
736  sh->beta_offset = get_se_golomb(gb) * 2;
737  sh->tc_offset = get_se_golomb(gb) * 2;
738  }
739  } else {
741  sh->beta_offset = s->ps.pps->beta_offset;
742  sh->tc_offset = s->ps.pps->tc_offset;
743  }
744  } else {
746  sh->beta_offset = 0;
747  sh->tc_offset = 0;
748  }
749 
755  } else {
757  }
758  } else if (!s->slice_initialized) {
759  av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
760  return AVERROR_INVALIDDATA;
761  }
762 
763  sh->num_entry_point_offsets = 0;
765  unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
766  // It would be possible to bound this tighter but this here is simpler
767  if (num_entry_point_offsets > get_bits_left(gb)) {
768  av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
769  return AVERROR_INVALIDDATA;
770  }
771 
772  sh->num_entry_point_offsets = num_entry_point_offsets;
773  if (sh->num_entry_point_offsets > 0) {
774  int offset_len = get_ue_golomb_long(gb) + 1;
775 
776  if (offset_len < 1 || offset_len > 32) {
777  sh->num_entry_point_offsets = 0;
778  av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
779  return AVERROR_INVALIDDATA;
780  }
781 
783  av_freep(&sh->offset);
784  av_freep(&sh->size);
785  sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
786  sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
787  sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
788  if (!sh->entry_point_offset || !sh->offset || !sh->size) {
789  sh->num_entry_point_offsets = 0;
790  av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
791  return AVERROR(ENOMEM);
792  }
793  for (i = 0; i < sh->num_entry_point_offsets; i++) {
794  unsigned val = get_bits_long(gb, offset_len);
795  sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
796  }
797  if (s->threads_number > 1 && (s->ps.pps->num_tile_rows > 1 || s->ps.pps->num_tile_columns > 1)) {
798  s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
799  s->threads_number = 1;
800  } else
801  s->enable_parallel_tiles = 0;
802  } else
803  s->enable_parallel_tiles = 0;
804  }
805 
807  unsigned int length = get_ue_golomb_long(gb);
808  if (length*8LL > get_bits_left(gb)) {
809  av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
810  return AVERROR_INVALIDDATA;
811  }
812  for (i = 0; i < length; i++)
813  skip_bits(gb, 8); // slice_header_extension_data_byte
814  }
815 
816  // Inferred parameters
817  sh->slice_qp = 26U + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
818  if (sh->slice_qp > 51 ||
819  sh->slice_qp < -s->ps.sps->qp_bd_offset) {
821  "The slice_qp %d is outside the valid range "
822  "[%d, 51].\n",
823  sh->slice_qp,
824  -s->ps.sps->qp_bd_offset);
825  return AVERROR_INVALIDDATA;
826  }
827 
829 
831  av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
832  return AVERROR_INVALIDDATA;
833  }
834 
835  if (get_bits_left(gb) < 0) {
837  "Overread slice header by %d bits\n", -get_bits_left(gb));
838  return AVERROR_INVALIDDATA;
839  }
840 
842 
844  s->HEVClc->qp_y = s->sh.slice_qp;
845 
846  s->slice_initialized = 1;
847  s->HEVClc->tu.cu_qp_offset_cb = 0;
848  s->HEVClc->tu.cu_qp_offset_cr = 0;
849 
850  return 0;
851 }
852 
853 #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
854 
855 #define SET_SAO(elem, value) \
856 do { \
857  if (!sao_merge_up_flag && !sao_merge_left_flag) \
858  sao->elem = value; \
859  else if (sao_merge_left_flag) \
860  sao->elem = CTB(s->sao, rx-1, ry).elem; \
861  else if (sao_merge_up_flag) \
862  sao->elem = CTB(s->sao, rx, ry-1).elem; \
863  else \
864  sao->elem = 0; \
865 } while (0)
866 
867 static void hls_sao_param(HEVCContext *s, int rx, int ry)
868 {
869  HEVCLocalContext *lc = s->HEVClc;
870  int sao_merge_left_flag = 0;
871  int sao_merge_up_flag = 0;
872  SAOParams *sao = &CTB(s->sao, rx, ry);
873  int c_idx, i;
874 
877  if (rx > 0) {
878  if (lc->ctb_left_flag)
879  sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
880  }
881  if (ry > 0 && !sao_merge_left_flag) {
882  if (lc->ctb_up_flag)
883  sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
884  }
885  }
886 
887  for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
888  int log2_sao_offset_scale = c_idx == 0 ? s->ps.pps->log2_sao_offset_scale_luma :
890 
891  if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
892  sao->type_idx[c_idx] = SAO_NOT_APPLIED;
893  continue;
894  }
895 
896  if (c_idx == 2) {
897  sao->type_idx[2] = sao->type_idx[1];
898  sao->eo_class[2] = sao->eo_class[1];
899  } else {
900  SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
901  }
902 
903  if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
904  continue;
905 
906  for (i = 0; i < 4; i++)
907  SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
908 
909  if (sao->type_idx[c_idx] == SAO_BAND) {
910  for (i = 0; i < 4; i++) {
911  if (sao->offset_abs[c_idx][i]) {
912  SET_SAO(offset_sign[c_idx][i],
914  } else {
915  sao->offset_sign[c_idx][i] = 0;
916  }
917  }
918  SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
919  } else if (c_idx != 2) {
920  SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
921  }
922 
923  // Inferred parameters
924  sao->offset_val[c_idx][0] = 0;
925  for (i = 0; i < 4; i++) {
926  sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
927  if (sao->type_idx[c_idx] == SAO_EDGE) {
928  if (i > 1)
929  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
930  } else if (sao->offset_sign[c_idx][i]) {
931  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
932  }
933  sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
934  }
935  }
936 }
937 
938 #undef SET_SAO
939 #undef CTB
940 
941 static int hls_cross_component_pred(HEVCContext *s, int idx) {
942  HEVCLocalContext *lc = s->HEVClc;
943  int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
944 
945  if (log2_res_scale_abs_plus1 != 0) {
946  int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
947  lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
948  (1 - 2 * res_scale_sign_flag);
949  } else {
950  lc->tu.res_scale_val = 0;
951  }
952 
953 
954  return 0;
955 }
956 
957 static int hls_transform_unit(HEVCContext *s, int x0, int y0,
958  int xBase, int yBase, int cb_xBase, int cb_yBase,
959  int log2_cb_size, int log2_trafo_size,
960  int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
961 {
962  HEVCLocalContext *lc = s->HEVClc;
963  const int log2_trafo_size_c = log2_trafo_size - s->ps.sps->hshift[1];
964  int i;
965 
966  if (lc->cu.pred_mode == MODE_INTRA) {
967  int trafo_size = 1 << log2_trafo_size;
968  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
969 
970  s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
971  }
972 
973  if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
974  (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
975  int scan_idx = SCAN_DIAG;
976  int scan_idx_c = SCAN_DIAG;
977  int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
978  (s->ps.sps->chroma_format_idc == 2 &&
979  (cbf_cb[1] || cbf_cr[1]));
980 
983  if (lc->tu.cu_qp_delta != 0)
984  if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
985  lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
986  lc->tu.is_cu_qp_delta_coded = 1;
987 
988  if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
989  lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
991  "The cu_qp_delta %d is outside the valid range "
992  "[%d, %d].\n",
993  lc->tu.cu_qp_delta,
994  -(26 + s->ps.sps->qp_bd_offset / 2),
995  (25 + s->ps.sps->qp_bd_offset / 2));
996  return AVERROR_INVALIDDATA;
997  }
998 
999  ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
1000  }
1001 
1002  if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1004  int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
1005  if (cu_chroma_qp_offset_flag) {
1006  int cu_chroma_qp_offset_idx = 0;
1007  if (s->ps.pps->chroma_qp_offset_list_len_minus1 > 0) {
1008  cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
1010  "cu_chroma_qp_offset_idx not yet tested.\n");
1011  }
1012  lc->tu.cu_qp_offset_cb = s->ps.pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1013  lc->tu.cu_qp_offset_cr = s->ps.pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1014  } else {
1015  lc->tu.cu_qp_offset_cb = 0;
1016  lc->tu.cu_qp_offset_cr = 0;
1017  }
1019  }
1020 
1021  if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1022  if (lc->tu.intra_pred_mode >= 6 &&
1023  lc->tu.intra_pred_mode <= 14) {
1024  scan_idx = SCAN_VERT;
1025  } else if (lc->tu.intra_pred_mode >= 22 &&
1026  lc->tu.intra_pred_mode <= 30) {
1027  scan_idx = SCAN_HORIZ;
1028  }
1029 
1030  if (lc->tu.intra_pred_mode_c >= 6 &&
1031  lc->tu.intra_pred_mode_c <= 14) {
1032  scan_idx_c = SCAN_VERT;
1033  } else if (lc->tu.intra_pred_mode_c >= 22 &&
1034  lc->tu.intra_pred_mode_c <= 30) {
1035  scan_idx_c = SCAN_HORIZ;
1036  }
1037  }
1038 
1039  lc->tu.cross_pf = 0;
1040 
1041  if (cbf_luma)
1042  ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1043  if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1044  int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1045  int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1046  lc->tu.cross_pf = (s->ps.pps->cross_component_prediction_enabled_flag && cbf_luma &&
1047  (lc->cu.pred_mode == MODE_INTER ||
1048  (lc->tu.chroma_mode_c == 4)));
1049 
1050  if (lc->tu.cross_pf) {
1052  }
1053  for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1054  if (lc->cu.pred_mode == MODE_INTRA) {
1055  ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1056  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
1057  }
1058  if (cbf_cb[i])
1059  ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1060  log2_trafo_size_c, scan_idx_c, 1);
1061  else
1062  if (lc->tu.cross_pf) {
1063  ptrdiff_t stride = s->frame->linesize[1];
1064  int hshift = s->ps.sps->hshift[1];
1065  int vshift = s->ps.sps->vshift[1];
1066  int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1067  int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1068  int size = 1 << log2_trafo_size_c;
1069 
1070  uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
1071  ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1072  for (i = 0; i < (size * size); i++) {
1073  coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1074  }
1075  s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1076  }
1077  }
1078 
1079  if (lc->tu.cross_pf) {
1081  }
1082  for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1083  if (lc->cu.pred_mode == MODE_INTRA) {
1084  ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1085  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
1086  }
1087  if (cbf_cr[i])
1088  ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1089  log2_trafo_size_c, scan_idx_c, 2);
1090  else
1091  if (lc->tu.cross_pf) {
1092  ptrdiff_t stride = s->frame->linesize[2];
1093  int hshift = s->ps.sps->hshift[2];
1094  int vshift = s->ps.sps->vshift[2];
1095  int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1096  int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1097  int size = 1 << log2_trafo_size_c;
1098 
1099  uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
1100  ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1101  for (i = 0; i < (size * size); i++) {
1102  coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1103  }
1104  s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1105  }
1106  }
1107  } else if (s->ps.sps->chroma_format_idc && blk_idx == 3) {
1108  int trafo_size_h = 1 << (log2_trafo_size + 1);
1109  int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1110  for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1111  if (lc->cu.pred_mode == MODE_INTRA) {
1112  ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1113  trafo_size_h, trafo_size_v);
1114  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
1115  }
1116  if (cbf_cb[i])
1117  ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1118  log2_trafo_size, scan_idx_c, 1);
1119  }
1120  for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1121  if (lc->cu.pred_mode == MODE_INTRA) {
1122  ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1123  trafo_size_h, trafo_size_v);
1124  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
1125  }
1126  if (cbf_cr[i])
1127  ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1128  log2_trafo_size, scan_idx_c, 2);
1129  }
1130  }
1131  } else if (s->ps.sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1132  if (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3) {
1133  int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1134  int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1135  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
1136  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
1137  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
1138  if (s->ps.sps->chroma_format_idc == 2) {
1139  ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
1140  trafo_size_h, trafo_size_v);
1141  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
1142  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
1143  }
1144  } else if (blk_idx == 3) {
1145  int trafo_size_h = 1 << (log2_trafo_size + 1);
1146  int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1147  ff_hevc_set_neighbour_available(s, xBase, yBase,
1148  trafo_size_h, trafo_size_v);
1149  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
1150  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
1151  if (s->ps.sps->chroma_format_idc == 2) {
1152  ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
1153  trafo_size_h, trafo_size_v);
1154  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
1155  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
1156  }
1157  }
1158  }
1159 
1160  return 0;
1161 }
1162 
1163 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1164 {
1165  int cb_size = 1 << log2_cb_size;
1166  int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
1167 
1168  int min_pu_width = s->ps.sps->min_pu_width;
1169  int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
1170  int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
1171  int i, j;
1172 
1173  for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1174  for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1175  s->is_pcm[i + j * min_pu_width] = 2;
1176 }
1177 
1178 static int hls_transform_tree(HEVCContext *s, int x0, int y0,
1179  int xBase, int yBase, int cb_xBase, int cb_yBase,
1180  int log2_cb_size, int log2_trafo_size,
1181  int trafo_depth, int blk_idx,
1182  const int *base_cbf_cb, const int *base_cbf_cr)
1183 {
1184  HEVCLocalContext *lc = s->HEVClc;
1185  uint8_t split_transform_flag;
1186  int cbf_cb[2];
1187  int cbf_cr[2];
1188  int ret;
1189 
1190  cbf_cb[0] = base_cbf_cb[0];
1191  cbf_cb[1] = base_cbf_cb[1];
1192  cbf_cr[0] = base_cbf_cr[0];
1193  cbf_cr[1] = base_cbf_cr[1];
1194 
1195  if (lc->cu.intra_split_flag) {
1196  if (trafo_depth == 1) {
1197  lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1198  if (s->ps.sps->chroma_format_idc == 3) {
1199  lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1200  lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1201  } else {
1203  lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1204  }
1205  }
1206  } else {
1207  lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1209  lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1210  }
1211 
1212  if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
1213  log2_trafo_size > s->ps.sps->log2_min_tb_size &&
1214  trafo_depth < lc->cu.max_trafo_depth &&
1215  !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1216  split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1217  } else {
1218  int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
1219  lc->cu.pred_mode == MODE_INTER &&
1220  lc->cu.part_mode != PART_2Nx2N &&
1221  trafo_depth == 0;
1222 
1223  split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
1224  (lc->cu.intra_split_flag && trafo_depth == 0) ||
1225  inter_split;
1226  }
1227 
1228  if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1229  if (trafo_depth == 0 || cbf_cb[0]) {
1230  cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1231  if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1232  cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1233  }
1234  }
1235 
1236  if (trafo_depth == 0 || cbf_cr[0]) {
1237  cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1238  if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1239  cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1240  }
1241  }
1242  }
1243 
1244  if (split_transform_flag) {
1245  const int trafo_size_split = 1 << (log2_trafo_size - 1);
1246  const int x1 = x0 + trafo_size_split;
1247  const int y1 = y0 + trafo_size_split;
1248 
1249 #define SUBDIVIDE(x, y, idx) \
1250 do { \
1251  ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1252  log2_trafo_size - 1, trafo_depth + 1, idx, \
1253  cbf_cb, cbf_cr); \
1254  if (ret < 0) \
1255  return ret; \
1256 } while (0)
1257 
1258  SUBDIVIDE(x0, y0, 0);
1259  SUBDIVIDE(x1, y0, 1);
1260  SUBDIVIDE(x0, y1, 2);
1261  SUBDIVIDE(x1, y1, 3);
1262 
1263 #undef SUBDIVIDE
1264  } else {
1265  int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
1266  int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
1267  int min_tu_width = s->ps.sps->min_tb_width;
1268  int cbf_luma = 1;
1269 
1270  if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1271  cbf_cb[0] || cbf_cr[0] ||
1272  (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1273  cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1274  }
1275 
1276  ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1277  log2_cb_size, log2_trafo_size,
1278  blk_idx, cbf_luma, cbf_cb, cbf_cr);
1279  if (ret < 0)
1280  return ret;
1281  // TODO: store cbf_luma somewhere else
1282  if (cbf_luma) {
1283  int i, j;
1284  for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1285  for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1286  int x_tu = (x0 + j) >> log2_min_tu_size;
1287  int y_tu = (y0 + i) >> log2_min_tu_size;
1288  s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1289  }
1290  }
1292  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
1295  set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1296  }
1297  }
1298  return 0;
1299 }
1300 
1301 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1302 {
1303  HEVCLocalContext *lc = s->HEVClc;
1304  GetBitContext gb;
1305  int cb_size = 1 << log2_cb_size;
1306  ptrdiff_t stride0 = s->frame->linesize[0];
1307  ptrdiff_t stride1 = s->frame->linesize[1];
1308  ptrdiff_t stride2 = s->frame->linesize[2];
1309  uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
1310  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)];
1311  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)];
1312 
1313  int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth +
1314  (((cb_size >> s->ps.sps->hshift[1]) * (cb_size >> s->ps.sps->vshift[1])) +
1315  ((cb_size >> s->ps.sps->hshift[2]) * (cb_size >> s->ps.sps->vshift[2]))) *
1316  s->ps.sps->pcm.bit_depth_chroma;
1317  const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1318  int ret;
1319 
1321  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
1322 
1323  ret = init_get_bits(&gb, pcm, length);
1324  if (ret < 0)
1325  return ret;
1326 
1327  s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, s->ps.sps->pcm.bit_depth);
1328  if (s->ps.sps->chroma_format_idc) {
1329  s->hevcdsp.put_pcm(dst1, stride1,
1330  cb_size >> s->ps.sps->hshift[1],
1331  cb_size >> s->ps.sps->vshift[1],
1332  &gb, s->ps.sps->pcm.bit_depth_chroma);
1333  s->hevcdsp.put_pcm(dst2, stride2,
1334  cb_size >> s->ps.sps->hshift[2],
1335  cb_size >> s->ps.sps->vshift[2],
1336  &gb, s->ps.sps->pcm.bit_depth_chroma);
1337  }
1338 
1339  return 0;
1340 }
1341 
1342 /**
1343  * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1344  *
1345  * @param s HEVC decoding context
1346  * @param dst target buffer for block data at block position
1347  * @param dststride stride of the dst buffer
1348  * @param ref reference picture buffer at origin (0, 0)
1349  * @param mv motion vector (relative to block position) to get pixel data from
1350  * @param x_off horizontal position of block from origin (0, 0)
1351  * @param y_off vertical position of block from origin (0, 0)
1352  * @param block_w width of block
1353  * @param block_h height of block
1354  * @param luma_weight weighting factor applied to the luma prediction
1355  * @param luma_offset additive offset applied to the luma prediction value
1356  */
1357 
1358 static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1359  AVFrame *ref, const Mv *mv, int x_off, int y_off,
1360  int block_w, int block_h, int luma_weight, int luma_offset)
1361 {
1362  HEVCLocalContext *lc = s->HEVClc;
1363  uint8_t *src = ref->data[0];
1364  ptrdiff_t srcstride = ref->linesize[0];
1365  int pic_width = s->ps.sps->width;
1366  int pic_height = s->ps.sps->height;
1367  int mx = mv->x & 3;
1368  int my = mv->y & 3;
1369  int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1371  int idx = ff_hevc_pel_weight[block_w];
1372 
1373  x_off += mv->x >> 2;
1374  y_off += mv->y >> 2;
1375  src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1376 
1377  if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1378  x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1379  y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1380  const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1381  int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1382  int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1383 
1384  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1385  edge_emu_stride, srcstride,
1386  block_w + QPEL_EXTRA,
1387  block_h + QPEL_EXTRA,
1388  x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1389  pic_width, pic_height);
1390  src = lc->edge_emu_buffer + buf_offset;
1391  srcstride = edge_emu_stride;
1392  }
1393 
1394  if (!weight_flag)
1395  s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1396  block_h, mx, my, block_w);
1397  else
1398  s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1399  block_h, s->sh.luma_log2_weight_denom,
1400  luma_weight, luma_offset, mx, my, block_w);
1401 }
1402 
1403 /**
1404  * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1405  *
1406  * @param s HEVC decoding context
1407  * @param dst target buffer for block data at block position
1408  * @param dststride stride of the dst buffer
1409  * @param ref0 reference picture0 buffer at origin (0, 0)
1410  * @param mv0 motion vector0 (relative to block position) to get pixel data from
1411  * @param x_off horizontal position of block from origin (0, 0)
1412  * @param y_off vertical position of block from origin (0, 0)
1413  * @param block_w width of block
1414  * @param block_h height of block
1415  * @param ref1 reference picture1 buffer at origin (0, 0)
1416  * @param mv1 motion vector1 (relative to block position) to get pixel data from
1417  * @param current_mv current motion vector structure
1418  */
1419  static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1420  AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1421  int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
1422 {
1423  HEVCLocalContext *lc = s->HEVClc;
1424  ptrdiff_t src0stride = ref0->linesize[0];
1425  ptrdiff_t src1stride = ref1->linesize[0];
1426  int pic_width = s->ps.sps->width;
1427  int pic_height = s->ps.sps->height;
1428  int mx0 = mv0->x & 3;
1429  int my0 = mv0->y & 3;
1430  int mx1 = mv1->x & 3;
1431  int my1 = mv1->y & 3;
1432  int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1434  int x_off0 = x_off + (mv0->x >> 2);
1435  int y_off0 = y_off + (mv0->y >> 2);
1436  int x_off1 = x_off + (mv1->x >> 2);
1437  int y_off1 = y_off + (mv1->y >> 2);
1438  int idx = ff_hevc_pel_weight[block_w];
1439 
1440  uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1441  uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1442 
1443  if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1444  x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1445  y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1446  const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1447  int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1448  int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1449 
1450  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1451  edge_emu_stride, src0stride,
1452  block_w + QPEL_EXTRA,
1453  block_h + QPEL_EXTRA,
1454  x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1455  pic_width, pic_height);
1456  src0 = lc->edge_emu_buffer + buf_offset;
1457  src0stride = edge_emu_stride;
1458  }
1459 
1460  if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1461  x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1462  y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1463  const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1464  int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1465  int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1466 
1467  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1468  edge_emu_stride, src1stride,
1469  block_w + QPEL_EXTRA,
1470  block_h + QPEL_EXTRA,
1471  x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1472  pic_width, pic_height);
1473  src1 = lc->edge_emu_buffer2 + buf_offset;
1474  src1stride = edge_emu_stride;
1475  }
1476 
1477  s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1478  block_h, mx0, my0, block_w);
1479  if (!weight_flag)
1480  s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1481  block_h, mx1, my1, block_w);
1482  else
1483  s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1484  block_h, s->sh.luma_log2_weight_denom,
1485  s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1486  s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1487  s->sh.luma_offset_l0[current_mv->ref_idx[0]],
1488  s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1489  mx1, my1, block_w);
1490 
1491 }
1492 
1493 /**
1494  * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1495  *
1496  * @param s HEVC decoding context
1497  * @param dst1 target buffer for block data at block position (U plane)
1498  * @param dst2 target buffer for block data at block position (V plane)
1499  * @param dststride stride of the dst1 and dst2 buffers
1500  * @param ref reference picture buffer at origin (0, 0)
1501  * @param mv motion vector (relative to block position) to get pixel data from
1502  * @param x_off horizontal position of block from origin (0, 0)
1503  * @param y_off vertical position of block from origin (0, 0)
1504  * @param block_w width of block
1505  * @param block_h height of block
1506  * @param chroma_weight weighting factor applied to the chroma prediction
1507  * @param chroma_offset additive offset applied to the chroma prediction value
1508  */
1509 
1510 static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
1511  ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
1512  int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
1513 {
1514  HEVCLocalContext *lc = s->HEVClc;
1515  int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1516  int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1517  const Mv *mv = &current_mv->mv[reflist];
1518  int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1520  int idx = ff_hevc_pel_weight[block_w];
1521  int hshift = s->ps.sps->hshift[1];
1522  int vshift = s->ps.sps->vshift[1];
1523  intptr_t mx = av_mod_uintp2(mv->x, 2 + hshift);
1524  intptr_t my = av_mod_uintp2(mv->y, 2 + vshift);
1525  intptr_t _mx = mx << (1 - hshift);
1526  intptr_t _my = my << (1 - vshift);
1527 
1528  x_off += mv->x >> (2 + hshift);
1529  y_off += mv->y >> (2 + vshift);
1530  src0 += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1531 
1532  if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1533  x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1534  y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1535  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1536  int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
1537  int buf_offset0 = EPEL_EXTRA_BEFORE *
1538  (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1539  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1540  edge_emu_stride, srcstride,
1541  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1542  x_off - EPEL_EXTRA_BEFORE,
1543  y_off - EPEL_EXTRA_BEFORE,
1544  pic_width, pic_height);
1545 
1546  src0 = lc->edge_emu_buffer + buf_offset0;
1547  srcstride = edge_emu_stride;
1548  }
1549  if (!weight_flag)
1550  s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1551  block_h, _mx, _my, block_w);
1552  else
1553  s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1554  block_h, s->sh.chroma_log2_weight_denom,
1555  chroma_weight, chroma_offset, _mx, _my, block_w);
1556 }
1557 
1558 /**
1559  * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1560  *
1561  * @param s HEVC decoding context
1562  * @param dst target buffer for block data at block position
1563  * @param dststride stride of the dst buffer
1564  * @param ref0 reference picture0 buffer at origin (0, 0)
1565  * @param mv0 motion vector0 (relative to block position) to get pixel data from
1566  * @param x_off horizontal position of block from origin (0, 0)
1567  * @param y_off vertical position of block from origin (0, 0)
1568  * @param block_w width of block
1569  * @param block_h height of block
1570  * @param ref1 reference picture1 buffer at origin (0, 0)
1571  * @param mv1 motion vector1 (relative to block position) to get pixel data from
1572  * @param current_mv current motion vector structure
1573  * @param cidx chroma component(cb, cr)
1574  */
1575 static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
1576  int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
1577 {
1578  HEVCLocalContext *lc = s->HEVClc;
1579  uint8_t *src1 = ref0->data[cidx+1];
1580  uint8_t *src2 = ref1->data[cidx+1];
1581  ptrdiff_t src1stride = ref0->linesize[cidx+1];
1582  ptrdiff_t src2stride = ref1->linesize[cidx+1];
1583  int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1585  int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1586  int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1587  Mv *mv0 = &current_mv->mv[0];
1588  Mv *mv1 = &current_mv->mv[1];
1589  int hshift = s->ps.sps->hshift[1];
1590  int vshift = s->ps.sps->vshift[1];
1591 
1592  intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
1593  intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
1594  intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
1595  intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
1596  intptr_t _mx0 = mx0 << (1 - hshift);
1597  intptr_t _my0 = my0 << (1 - vshift);
1598  intptr_t _mx1 = mx1 << (1 - hshift);
1599  intptr_t _my1 = my1 << (1 - vshift);
1600 
1601  int x_off0 = x_off + (mv0->x >> (2 + hshift));
1602  int y_off0 = y_off + (mv0->y >> (2 + vshift));
1603  int x_off1 = x_off + (mv1->x >> (2 + hshift));
1604  int y_off1 = y_off + (mv1->y >> (2 + vshift));
1605  int idx = ff_hevc_pel_weight[block_w];
1606  src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1607  src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1608 
1609  if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
1610  x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1611  y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1612  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1613  int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
1614  int buf_offset1 = EPEL_EXTRA_BEFORE *
1615  (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1616 
1617  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1618  edge_emu_stride, src1stride,
1619  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1620  x_off0 - EPEL_EXTRA_BEFORE,
1621  y_off0 - EPEL_EXTRA_BEFORE,
1622  pic_width, pic_height);
1623 
1624  src1 = lc->edge_emu_buffer + buf_offset1;
1625  src1stride = edge_emu_stride;
1626  }
1627 
1628  if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
1629  x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1630  y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1631  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1632  int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
1633  int buf_offset1 = EPEL_EXTRA_BEFORE *
1634  (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1635 
1636  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
1637  edge_emu_stride, src2stride,
1638  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1639  x_off1 - EPEL_EXTRA_BEFORE,
1640  y_off1 - EPEL_EXTRA_BEFORE,
1641  pic_width, pic_height);
1642 
1643  src2 = lc->edge_emu_buffer2 + buf_offset1;
1644  src2stride = edge_emu_stride;
1645  }
1646 
1647  s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
1648  block_h, _mx0, _my0, block_w);
1649  if (!weight_flag)
1650  s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1651  src2, src2stride, lc->tmp,
1652  block_h, _mx1, _my1, block_w);
1653  else
1654  s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1655  src2, src2stride, lc->tmp,
1656  block_h,
1658  s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
1659  s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
1660  s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
1661  s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
1662  _mx1, _my1, block_w);
1663 }
1664 
1666  const Mv *mv, int y0, int height)
1667 {
1668  int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1669 
1670  if (s->threads_type == FF_THREAD_FRAME )
1671  ff_thread_await_progress(&ref->tf, y, 0);
1672 }
1673 
1674 static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
1675  int nPbH, int log2_cb_size, int part_idx,
1676  int merge_idx, MvField *mv)
1677 {
1678  HEVCLocalContext *lc = s->HEVClc;
1679  enum InterPredIdc inter_pred_idc = PRED_L0;
1680  int mvp_flag;
1681 
1682  ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1683  mv->pred_flag = 0;
1684  if (s->sh.slice_type == HEVC_SLICE_B)
1685  inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1686 
1687  if (inter_pred_idc != PRED_L1) {
1688  if (s->sh.nb_refs[L0])
1689  mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1690 
1691  mv->pred_flag = PF_L0;
1692  ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1693  mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1694  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1695  part_idx, merge_idx, mv, mvp_flag, 0);
1696  mv->mv[0].x += lc->pu.mvd.x;
1697  mv->mv[0].y += lc->pu.mvd.y;
1698  }
1699 
1700  if (inter_pred_idc != PRED_L0) {
1701  if (s->sh.nb_refs[L1])
1702  mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1703 
1704  if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1705  AV_ZERO32(&lc->pu.mvd);
1706  } else {
1707  ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1708  }
1709 
1710  mv->pred_flag += PF_L1;
1711  mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1712  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1713  part_idx, merge_idx, mv, mvp_flag, 1);
1714  mv->mv[1].x += lc->pu.mvd.x;
1715  mv->mv[1].y += lc->pu.mvd.y;
1716  }
1717 }
1718 
1719 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1720  int nPbW, int nPbH,
1721  int log2_cb_size, int partIdx, int idx)
1722 {
1723 #define POS(c_idx, x, y) \
1724  &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1725  (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1726  HEVCLocalContext *lc = s->HEVClc;
1727  int merge_idx = 0;
1728  struct MvField current_mv = {{{ 0 }}};
1729 
1730  int min_pu_width = s->ps.sps->min_pu_width;
1731 
1732  MvField *tab_mvf = s->ref->tab_mvf;
1733  RefPicList *refPicList = s->ref->refPicList;
1734  HEVCFrame *ref0 = NULL, *ref1 = NULL;
1735  uint8_t *dst0 = POS(0, x0, y0);
1736  uint8_t *dst1 = POS(1, x0, y0);
1737  uint8_t *dst2 = POS(2, x0, y0);
1738  int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1739  int min_cb_width = s->ps.sps->min_cb_width;
1740  int x_cb = x0 >> log2_min_cb_size;
1741  int y_cb = y0 >> log2_min_cb_size;
1742  int x_pu, y_pu;
1743  int i, j;
1744 
1745  int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1746 
1747  if (!skip_flag)
1749 
1750  if (skip_flag || lc->pu.merge_flag) {
1751  if (s->sh.max_num_merge_cand > 1)
1752  merge_idx = ff_hevc_merge_idx_decode(s);
1753  else
1754  merge_idx = 0;
1755 
1756  ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1757  partIdx, merge_idx, &current_mv);
1758  } else {
1759  hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1760  partIdx, merge_idx, &current_mv);
1761  }
1762 
1763  x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1764  y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1765 
1766  for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1767  for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1768  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1769 
1770  if (current_mv.pred_flag & PF_L0) {
1771  ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1772  if (!ref0)
1773  return;
1774  hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
1775  }
1776  if (current_mv.pred_flag & PF_L1) {
1777  ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1778  if (!ref1)
1779  return;
1780  hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
1781  }
1782 
1783  if (current_mv.pred_flag == PF_L0) {
1784  int x0_c = x0 >> s->ps.sps->hshift[1];
1785  int y0_c = y0 >> s->ps.sps->vshift[1];
1786  int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1787  int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1788 
1789  luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
1790  &current_mv.mv[0], x0, y0, nPbW, nPbH,
1791  s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1792  s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
1793 
1794  if (s->ps.sps->chroma_format_idc) {
1795  chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
1796  0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1797  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
1798  chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
1799  0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1800  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
1801  }
1802  } else if (current_mv.pred_flag == PF_L1) {
1803  int x0_c = x0 >> s->ps.sps->hshift[1];
1804  int y0_c = y0 >> s->ps.sps->vshift[1];
1805  int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1806  int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1807 
1808  luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
1809  &current_mv.mv[1], x0, y0, nPbW, nPbH,
1810  s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1811  s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
1812 
1813  if (s->ps.sps->chroma_format_idc) {
1814  chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
1815  1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1816  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
1817 
1818  chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
1819  1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1820  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
1821  }
1822  } else if (current_mv.pred_flag == PF_BI) {
1823  int x0_c = x0 >> s->ps.sps->hshift[1];
1824  int y0_c = y0 >> s->ps.sps->vshift[1];
1825  int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1826  int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1827 
1828  luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
1829  &current_mv.mv[0], x0, y0, nPbW, nPbH,
1830  ref1->frame, &current_mv.mv[1], &current_mv);
1831 
1832  if (s->ps.sps->chroma_format_idc) {
1833  chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
1834  x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
1835 
1836  chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
1837  x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
1838  }
1839  }
1840 }
1841 
1842 /**
1843  * 8.4.1
1844  */
1845 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1846  int prev_intra_luma_pred_flag)
1847 {
1848  HEVCLocalContext *lc = s->HEVClc;
1849  int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1850  int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1851  int min_pu_width = s->ps.sps->min_pu_width;
1852  int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
1853  int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
1854  int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
1855 
1856  int cand_up = (lc->ctb_up_flag || y0b) ?
1857  s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1858  int cand_left = (lc->ctb_left_flag || x0b) ?
1859  s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1860 
1861  int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
1862 
1863  MvField *tab_mvf = s->ref->tab_mvf;
1864  int intra_pred_mode;
1865  int candidate[3];
1866  int i, j;
1867 
1868  // intra_pred_mode prediction does not cross vertical CTB boundaries
1869  if ((y0 - 1) < y_ctb)
1870  cand_up = INTRA_DC;
1871 
1872  if (cand_left == cand_up) {
1873  if (cand_left < 2) {
1874  candidate[0] = INTRA_PLANAR;
1875  candidate[1] = INTRA_DC;
1876  candidate[2] = INTRA_ANGULAR_26;
1877  } else {
1878  candidate[0] = cand_left;
1879  candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1880  candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1881  }
1882  } else {
1883  candidate[0] = cand_left;
1884  candidate[1] = cand_up;
1885  if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1886  candidate[2] = INTRA_PLANAR;
1887  } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1888  candidate[2] = INTRA_DC;
1889  } else {
1890  candidate[2] = INTRA_ANGULAR_26;
1891  }
1892  }
1893 
1894  if (prev_intra_luma_pred_flag) {
1895  intra_pred_mode = candidate[lc->pu.mpm_idx];
1896  } else {
1897  if (candidate[0] > candidate[1])
1898  FFSWAP(uint8_t, candidate[0], candidate[1]);
1899  if (candidate[0] > candidate[2])
1900  FFSWAP(uint8_t, candidate[0], candidate[2]);
1901  if (candidate[1] > candidate[2])
1902  FFSWAP(uint8_t, candidate[1], candidate[2]);
1903 
1904  intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1905  for (i = 0; i < 3; i++)
1906  if (intra_pred_mode >= candidate[i])
1907  intra_pred_mode++;
1908  }
1909 
1910  /* write the intra prediction units into the mv array */
1911  if (!size_in_pus)
1912  size_in_pus = 1;
1913  for (i = 0; i < size_in_pus; i++) {
1914  memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1915  intra_pred_mode, size_in_pus);
1916 
1917  for (j = 0; j < size_in_pus; j++) {
1918  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
1919  }
1920  }
1921 
1922  return intra_pred_mode;
1923 }
1924 
1925 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1926  int log2_cb_size, int ct_depth)
1927 {
1928  int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
1929  int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
1930  int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
1931  int y;
1932 
1933  for (y = 0; y < length; y++)
1934  memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
1935  ct_depth, length);
1936 }
1937 
1938 static const uint8_t tab_mode_idx[] = {
1939  0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
1940  21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
1941 
1942 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1943  int log2_cb_size)
1944 {
1945  HEVCLocalContext *lc = s->HEVClc;
1946  static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1947  uint8_t prev_intra_luma_pred_flag[4];
1948  int split = lc->cu.part_mode == PART_NxN;
1949  int pb_size = (1 << log2_cb_size) >> split;
1950  int side = split + 1;
1951  int chroma_mode;
1952  int i, j;
1953 
1954  for (i = 0; i < side; i++)
1955  for (j = 0; j < side; j++)
1956  prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1957 
1958  for (i = 0; i < side; i++) {
1959  for (j = 0; j < side; j++) {
1960  if (prev_intra_luma_pred_flag[2 * i + j])
1962  else
1964 
1965  lc->pu.intra_pred_mode[2 * i + j] =
1966  luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
1967  prev_intra_luma_pred_flag[2 * i + j]);
1968  }
1969  }
1970 
1971  if (s->ps.sps->chroma_format_idc == 3) {
1972  for (i = 0; i < side; i++) {
1973  for (j = 0; j < side; j++) {
1974  lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1975  if (chroma_mode != 4) {
1976  if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
1977  lc->pu.intra_pred_mode_c[2 * i + j] = 34;
1978  else
1979  lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
1980  } else {
1981  lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
1982  }
1983  }
1984  }
1985  } else if (s->ps.sps->chroma_format_idc == 2) {
1986  int mode_idx;
1987  lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1988  if (chroma_mode != 4) {
1989  if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
1990  mode_idx = 34;
1991  else
1992  mode_idx = intra_chroma_table[chroma_mode];
1993  } else {
1994  mode_idx = lc->pu.intra_pred_mode[0];
1995  }
1996  lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
1997  } else if (s->ps.sps->chroma_format_idc != 0) {
1998  chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1999  if (chroma_mode != 4) {
2000  if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2001  lc->pu.intra_pred_mode_c[0] = 34;
2002  else
2003  lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2004  } else {
2005  lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2006  }
2007  }
2008 }
2009 
2011  int x0, int y0,
2012  int log2_cb_size)
2013 {
2014  HEVCLocalContext *lc = s->HEVClc;
2015  int pb_size = 1 << log2_cb_size;
2016  int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
2017  int min_pu_width = s->ps.sps->min_pu_width;
2018  MvField *tab_mvf = s->ref->tab_mvf;
2019  int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
2020  int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
2021  int j, k;
2022 
2023  if (size_in_pus == 0)
2024  size_in_pus = 1;
2025  for (j = 0; j < size_in_pus; j++)
2026  memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2027  if (lc->cu.pred_mode == MODE_INTRA)
2028  for (j = 0; j < size_in_pus; j++)
2029  for (k = 0; k < size_in_pus; k++)
2030  tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2031 }
2032 
2033 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2034 {
2035  int cb_size = 1 << log2_cb_size;
2036  HEVCLocalContext *lc = s->HEVClc;
2037  int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2038  int length = cb_size >> log2_min_cb_size;
2039  int min_cb_width = s->ps.sps->min_cb_width;
2040  int x_cb = x0 >> log2_min_cb_size;
2041  int y_cb = y0 >> log2_min_cb_size;
2042  int idx = log2_cb_size - 2;
2043  int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2044  int x, y, ret;
2045 
2046  lc->cu.x = x0;
2047  lc->cu.y = y0;
2048  lc->cu.pred_mode = MODE_INTRA;
2049  lc->cu.part_mode = PART_2Nx2N;
2050  lc->cu.intra_split_flag = 0;
2051 
2052  SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2053  for (x = 0; x < 4; x++)
2054  lc->pu.intra_pred_mode[x] = 1;
2057  if (lc->cu.cu_transquant_bypass_flag)
2058  set_deblocking_bypass(s, x0, y0, log2_cb_size);
2059  } else
2060  lc->cu.cu_transquant_bypass_flag = 0;
2061 
2062  if (s->sh.slice_type != HEVC_SLICE_I) {
2063  uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2064 
2065  x = y_cb * min_cb_width + x_cb;
2066  for (y = 0; y < length; y++) {
2067  memset(&s->skip_flag[x], skip_flag, length);
2068  x += min_cb_width;
2069  }
2070  lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2071  } else {
2072  x = y_cb * min_cb_width + x_cb;
2073  for (y = 0; y < length; y++) {
2074  memset(&s->skip_flag[x], 0, length);
2075  x += min_cb_width;
2076  }
2077  }
2078 
2079  if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2080  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2081  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2082 
2084  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2085  } else {
2086  int pcm_flag = 0;
2087 
2088  if (s->sh.slice_type != HEVC_SLICE_I)
2090  if (lc->cu.pred_mode != MODE_INTRA ||
2091  log2_cb_size == s->ps.sps->log2_min_cb_size) {
2092  lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
2093  lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2094  lc->cu.pred_mode == MODE_INTRA;
2095  }
2096 
2097  if (lc->cu.pred_mode == MODE_INTRA) {
2098  if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2099  log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2100  log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2101  pcm_flag = ff_hevc_pcm_flag_decode(s);
2102  }
2103  if (pcm_flag) {
2104  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2105  ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2107  set_deblocking_bypass(s, x0, y0, log2_cb_size);
2108 
2109  if (ret < 0)
2110  return ret;
2111  } else {
2112  intra_prediction_unit(s, x0, y0, log2_cb_size);
2113  }
2114  } else {
2115  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2116  switch (lc->cu.part_mode) {
2117  case PART_2Nx2N:
2118  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2119  break;
2120  case PART_2NxN:
2121  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2122  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2123  break;
2124  case PART_Nx2N:
2125  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2126  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2127  break;
2128  case PART_2NxnU:
2129  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2130  hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2131  break;
2132  case PART_2NxnD:
2133  hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2134  hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2135  break;
2136  case PART_nLx2N:
2137  hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2138  hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2139  break;
2140  case PART_nRx2N:
2141  hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2142  hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2143  break;
2144  case PART_NxN:
2145  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2146  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2147  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2148  hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2149  break;
2150  }
2151  }
2152 
2153  if (!pcm_flag) {
2154  int rqt_root_cbf = 1;
2155 
2156  if (lc->cu.pred_mode != MODE_INTRA &&
2157  !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2158  rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2159  }
2160  if (rqt_root_cbf) {
2161  const static int cbf[2] = { 0 };
2162  lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2165  ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2166  log2_cb_size,
2167  log2_cb_size, 0, 0, cbf, cbf);
2168  if (ret < 0)
2169  return ret;
2170  } else {
2172  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2173  }
2174  }
2175  }
2176 
2178  ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
2179 
2180  x = y_cb * min_cb_width + x_cb;
2181  for (y = 0; y < length; y++) {
2182  memset(&s->qp_y_tab[x], lc->qp_y, length);
2183  x += min_cb_width;
2184  }
2185 
2186  if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2187  ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2188  lc->qPy_pred = lc->qp_y;
2189  }
2190 
2191  set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
2192 
2193  return 0;
2194 }
2195 
2196 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2197  int log2_cb_size, int cb_depth)
2198 {
2199  HEVCLocalContext *lc = s->HEVClc;
2200  const int cb_size = 1 << log2_cb_size;
2201  int ret;
2202  int split_cu;
2203 
2204  lc->ct_depth = cb_depth;
2205  if (x0 + cb_size <= s->ps.sps->width &&
2206  y0 + cb_size <= s->ps.sps->height &&
2207  log2_cb_size > s->ps.sps->log2_min_cb_size) {
2208  split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2209  } else {
2210  split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2211  }
2212  if (s->ps.pps->cu_qp_delta_enabled_flag &&
2213  log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2214  lc->tu.is_cu_qp_delta_coded = 0;
2215  lc->tu.cu_qp_delta = 0;
2216  }
2217 
2219  log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
2221  }
2222 
2223  if (split_cu) {
2224  int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2225  const int cb_size_split = cb_size >> 1;
2226  const int x1 = x0 + cb_size_split;
2227  const int y1 = y0 + cb_size_split;
2228 
2229  int more_data = 0;
2230 
2231  more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
2232  if (more_data < 0)
2233  return more_data;
2234 
2235  if (more_data && x1 < s->ps.sps->width) {
2236  more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
2237  if (more_data < 0)
2238  return more_data;
2239  }
2240  if (more_data && y1 < s->ps.sps->height) {
2241  more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
2242  if (more_data < 0)
2243  return more_data;
2244  }
2245  if (more_data && x1 < s->ps.sps->width &&
2246  y1 < s->ps.sps->height) {
2247  more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
2248  if (more_data < 0)
2249  return more_data;
2250  }
2251 
2252  if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2253  ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2254  lc->qPy_pred = lc->qp_y;
2255 
2256  if (more_data)
2257  return ((x1 + cb_size_split) < s->ps.sps->width ||
2258  (y1 + cb_size_split) < s->ps.sps->height);
2259  else
2260  return 0;
2261  } else {
2262  ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2263  if (ret < 0)
2264  return ret;
2265  if ((!((x0 + cb_size) %
2266  (1 << (s->ps.sps->log2_ctb_size))) ||
2267  (x0 + cb_size >= s->ps.sps->width)) &&
2268  (!((y0 + cb_size) %
2269  (1 << (s->ps.sps->log2_ctb_size))) ||
2270  (y0 + cb_size >= s->ps.sps->height))) {
2271  int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
2272  return !end_of_slice_flag;
2273  } else {
2274  return 1;
2275  }
2276  }
2277 
2278  return 0;
2279 }
2280 
2281 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2282  int ctb_addr_ts)
2283 {
2284  HEVCLocalContext *lc = s->HEVClc;
2285  int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2286  int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2287  int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2288 
2289  s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2290 
2292  if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2293  lc->first_qp_group = 1;
2294  lc->end_of_tiles_x = s->ps.sps->width;
2295  } else if (s->ps.pps->tiles_enabled_flag) {
2296  if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2297  int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2298  lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2299  lc->first_qp_group = 1;
2300  }
2301  } else {
2302  lc->end_of_tiles_x = s->ps.sps->width;
2303  }
2304 
2305  lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2306 
2307  lc->boundary_flags = 0;
2308  if (s->ps.pps->tiles_enabled_flag) {
2309  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]])
2311  if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2313  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]])
2315  if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2317  } else {
2318  if (ctb_addr_in_slice <= 0)
2320  if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2322  }
2323 
2324  lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2325  lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2326  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]]));
2327  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]]));
2328 }
2329 
2330 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
2331 {
2332  HEVCContext *s = avctxt->priv_data;
2333  int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2334  int more_data = 1;
2335  int x_ctb = 0;
2336  int y_ctb = 0;
2337  int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2338 
2339  if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
2340  av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
2341  return AVERROR_INVALIDDATA;
2342  }
2343 
2345  int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2346  if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2347  av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2348  return AVERROR_INVALIDDATA;
2349  }
2350  }
2351 
2352  while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2353  int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2354 
2355  x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2356  y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2357  hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2358 
2359  ff_hevc_cabac_init(s, ctb_addr_ts);
2360 
2361  hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2362 
2363  s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2364  s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2366 
2367  more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2368  if (more_data < 0) {
2369  s->tab_slice_address[ctb_addr_rs] = -1;
2370  return more_data;
2371  }
2372 
2373 
2374  ctb_addr_ts++;
2375  ff_hevc_save_states(s, ctb_addr_ts);
2376  ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2377  }
2378 
2379  if (x_ctb + ctb_size >= s->ps.sps->width &&
2380  y_ctb + ctb_size >= s->ps.sps->height)
2381  ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2382 
2383  return ctb_addr_ts;
2384 }
2385 
2387 {
2388  int arg[2];
2389  int ret[2];
2390 
2391  arg[0] = 0;
2392  arg[1] = 1;
2393 
2394  s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
2395  return ret[0];
2396 }
2397 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
2398 {
2399  HEVCContext *s1 = avctxt->priv_data, *s;
2400  HEVCLocalContext *lc;
2401  int ctb_size = 1<< s1->ps.sps->log2_ctb_size;
2402  int more_data = 1;
2403  int *ctb_row_p = input_ctb_row;
2404  int ctb_row = ctb_row_p[job];
2405  int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->ps.sps->width + ctb_size - 1) >> s1->ps.sps->log2_ctb_size);
2406  int ctb_addr_ts = s1->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2407  int thread = ctb_row % s1->threads_number;
2408  int ret;
2409 
2410  s = s1->sList[self_id];
2411  lc = s->HEVClc;
2412 
2413  if(ctb_row) {
2414  ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2415 
2416  if (ret < 0)
2417  return ret;
2418  ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2419  }
2420 
2421  while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2422  int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2423  int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2424 
2425  hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2426 
2427  ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2428 
2429  if (atomic_load(&s1->wpp_err)) {
2430  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2431  return 0;
2432  }
2433 
2434  ff_hevc_cabac_init(s, ctb_addr_ts);
2435  hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2436  more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2437 
2438  if (more_data < 0) {
2439  s->tab_slice_address[ctb_addr_rs] = -1;
2440  atomic_store(&s1->wpp_err, 1);
2441  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2442  return more_data;
2443  }
2444 
2445  ctb_addr_ts++;
2446 
2447  ff_hevc_save_states(s, ctb_addr_ts);
2448  ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2449  ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2450 
2451  if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2452  atomic_store(&s1->wpp_err, 1);
2453  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2454  return 0;
2455  }
2456 
2457  if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
2458  ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2459  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2460  return ctb_addr_ts;
2461  }
2462  ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2463  x_ctb+=ctb_size;
2464 
2465  if(x_ctb >= s->ps.sps->width) {
2466  break;
2467  }
2468  }
2469  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2470 
2471  return 0;
2472 }
2473 
2474 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2475 {
2476  const uint8_t *data = nal->data;
2477  int length = nal->size;
2478  HEVCLocalContext *lc = s->HEVClc;
2479  int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2480  int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2481  int64_t offset;
2482  int64_t startheader, cmpt = 0;
2483  int i, j, res = 0;
2484 
2485  if (!ret || !arg) {
2486  av_free(ret);
2487  av_free(arg);
2488  return AVERROR(ENOMEM);
2489  }
2490 
2492  av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2494  s->ps.sps->ctb_width, s->ps.sps->ctb_height
2495  );
2496  res = AVERROR_INVALIDDATA;
2497  goto error;
2498  }
2499 
2501 
2502  if (!s->sList[1]) {
2503  for (i = 1; i < s->threads_number; i++) {
2504  s->sList[i] = av_malloc(sizeof(HEVCContext));
2505  memcpy(s->sList[i], s, sizeof(HEVCContext));
2506  s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
2507  s->sList[i]->HEVClc = s->HEVClcList[i];
2508  }
2509  }
2510 
2511  offset = (lc->gb.index >> 3);
2512 
2513  for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2514  if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2515  startheader--;
2516  cmpt++;
2517  }
2518  }
2519 
2520  for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2521  offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2522  for (j = 0, cmpt = 0, startheader = offset
2523  + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2524  if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2525  startheader--;
2526  cmpt++;
2527  }
2528  }
2529  s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2530  s->sh.offset[i - 1] = offset;
2531 
2532  }
2533  if (s->sh.num_entry_point_offsets != 0) {
2534  offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2535  if (length < offset) {
2536  av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2537  res = AVERROR_INVALIDDATA;
2538  goto error;
2539  }
2540  s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2541  s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2542 
2543  }
2544  s->data = data;
2545 
2546  for (i = 1; i < s->threads_number; i++) {
2547  s->sList[i]->HEVClc->first_qp_group = 1;
2548  s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
2549  memcpy(s->sList[i], s, sizeof(HEVCContext));
2550  s->sList[i]->HEVClc = s->HEVClcList[i];
2551  }
2552 
2553  atomic_store(&s->wpp_err, 0);
2554  ff_reset_entries(s->avctx);
2555 
2556  for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
2557  arg[i] = i;
2558  ret[i] = 0;
2559  }
2560 
2563 
2564  for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2565  res += ret[i];
2566 error:
2567  av_free(ret);
2568  av_free(arg);
2569  return res;
2570 }
2571 
2573 {
2574  AVFrame *out = s->ref->frame;
2575 
2576  if (s->sei_frame_packing_present &&
2579  s->content_interpretation_type > 0 &&
2580  s->content_interpretation_type < 3) {
2582  if (!stereo)
2583  return AVERROR(ENOMEM);
2584 
2585  switch (s->frame_packing_arrangement_type) {
2586  case 3:
2587  if (s->quincunx_subsampling)
2589  else
2590  stereo->type = AV_STEREO3D_SIDEBYSIDE;
2591  break;
2592  case 4:
2593  stereo->type = AV_STEREO3D_TOPBOTTOM;
2594  break;
2595  case 5:
2596  stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2597  break;
2598  }
2599 
2600  if (s->content_interpretation_type == 2)
2601  stereo->flags = AV_STEREO3D_FLAG_INVERT;
2602  }
2603 
2605  (s->sei_anticlockwise_rotation || s->sei_hflip || s->sei_vflip)) {
2606  double angle = s->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
2607  AVFrameSideData *rotation = av_frame_new_side_data(out,
2609  sizeof(int32_t) * 9);
2610  if (!rotation)
2611  return AVERROR(ENOMEM);
2612 
2613  av_display_rotation_set((int32_t *)rotation->data, angle);
2614  av_display_matrix_flip((int32_t *)rotation->data,
2615  s->sei_hflip, s->sei_vflip);
2616  }
2617 
2618  // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2619  // so the side data persists for the entire coded video sequence.
2621  IS_IRAP(s) && s->no_rasl_output_flag) {
2623  }
2625  // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
2626  const int mapping[3] = {2, 0, 1};
2627  const int chroma_den = 50000;
2628  const int luma_den = 10000;
2629  int i;
2630  AVMasteringDisplayMetadata *metadata =
2632  if (!metadata)
2633  return AVERROR(ENOMEM);
2634 
2635  for (i = 0; i < 3; i++) {
2636  const int j = mapping[i];
2637  metadata->display_primaries[i][0].num = s->display_primaries[j][0];
2638  metadata->display_primaries[i][0].den = chroma_den;
2639  metadata->display_primaries[i][1].num = s->display_primaries[j][1];
2640  metadata->display_primaries[i][1].den = chroma_den;
2641  }
2642  metadata->white_point[0].num = s->white_point[0];
2643  metadata->white_point[0].den = chroma_den;
2644  metadata->white_point[1].num = s->white_point[1];
2645  metadata->white_point[1].den = chroma_den;
2646 
2647  metadata->max_luminance.num = s->max_mastering_luminance;
2648  metadata->max_luminance.den = luma_den;
2649  metadata->min_luminance.num = s->min_mastering_luminance;
2650  metadata->min_luminance.den = luma_den;
2651  metadata->has_luminance = 1;
2652  metadata->has_primaries = 1;
2653 
2654  av_log(s->avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
2656  "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
2657  av_q2d(metadata->display_primaries[0][0]),
2658  av_q2d(metadata->display_primaries[0][1]),
2659  av_q2d(metadata->display_primaries[1][0]),
2660  av_q2d(metadata->display_primaries[1][1]),
2661  av_q2d(metadata->display_primaries[2][0]),
2662  av_q2d(metadata->display_primaries[2][1]),
2663  av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
2665  "min_luminance=%f, max_luminance=%f\n",
2666  av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
2667  }
2668 
2669  if (s->a53_caption) {
2672  s->a53_caption_size);
2673  if (sd)
2674  memcpy(sd->data, s->a53_caption, s->a53_caption_size);
2675  av_freep(&s->a53_caption);
2676  s->a53_caption_size = 0;
2678  }
2679 
2680  return 0;
2681 }
2682 
2684 {
2685  HEVCLocalContext *lc = s->HEVClc;
2686  int pic_size_in_ctb = ((s->ps.sps->width >> s->ps.sps->log2_min_cb_size) + 1) *
2687  ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
2688  int ret;
2689 
2690  memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
2691  memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
2692  memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2693  memset(s->is_pcm, 0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
2694  memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2695 
2696  s->is_decoded = 0;
2697  s->first_nal_type = s->nal_unit_type;
2698 
2700 
2701  if (s->ps.pps->tiles_enabled_flag)
2702  lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2703 
2704  ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
2705  if (ret < 0)
2706  goto fail;
2707 
2708  ret = ff_hevc_frame_rps(s);
2709  if (ret < 0) {
2710  av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2711  goto fail;
2712  }
2713 
2714  s->ref->frame->key_frame = IS_IRAP(s);
2715 
2716  ret = set_side_data(s);
2717  if (ret < 0)
2718  goto fail;
2719 
2720  s->frame->pict_type = 3 - s->sh.slice_type;
2721 
2722  if (!IS_IRAP(s))
2723  ff_hevc_bump_frame(s);
2724 
2726  ret = ff_hevc_output_frame(s, s->output_frame, 0);
2727  if (ret < 0)
2728  goto fail;
2729 
2730  if (!s->avctx->hwaccel)
2732 
2733  return 0;
2734 
2735 fail:
2736  if (s->ref)
2737  ff_hevc_unref_frame(s, s->ref, ~0);
2738  s->ref = NULL;
2739  return ret;
2740 }
2741 
2742 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
2743 {
2744  HEVCLocalContext *lc = s->HEVClc;
2745  GetBitContext *gb = &lc->gb;
2746  int ctb_addr_ts, ret;
2747 
2748  *gb = nal->gb;
2749  s->nal_unit_type = nal->type;
2750  s->temporal_id = nal->temporal_id;
2751 
2752  switch (s->nal_unit_type) {
2753  case HEVC_NAL_VPS:
2754  ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2755  if (ret < 0)
2756  goto fail;
2757  break;
2758  case HEVC_NAL_SPS:
2759  ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
2760  s->apply_defdispwin);
2761  if (ret < 0)
2762  goto fail;
2763  break;
2764  case HEVC_NAL_PPS:
2765  ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
2766  if (ret < 0)
2767  goto fail;
2768  break;
2769  case HEVC_NAL_SEI_PREFIX:
2770  case HEVC_NAL_SEI_SUFFIX:
2771  ret = ff_hevc_decode_nal_sei(s);
2772  if (ret < 0)
2773  goto fail;
2774  break;
2775  case HEVC_NAL_TRAIL_R:
2776  case HEVC_NAL_TRAIL_N:
2777  case HEVC_NAL_TSA_N:
2778  case HEVC_NAL_TSA_R:
2779  case HEVC_NAL_STSA_N:
2780  case HEVC_NAL_STSA_R:
2781  case HEVC_NAL_BLA_W_LP:
2782  case HEVC_NAL_BLA_W_RADL:
2783  case HEVC_NAL_BLA_N_LP:
2784  case HEVC_NAL_IDR_W_RADL:
2785  case HEVC_NAL_IDR_N_LP:
2786  case HEVC_NAL_CRA_NUT:
2787  case HEVC_NAL_RADL_N:
2788  case HEVC_NAL_RADL_R:
2789  case HEVC_NAL_RASL_N:
2790  case HEVC_NAL_RASL_R:
2791  ret = hls_slice_header(s);
2792  if (ret < 0)
2793  return ret;
2794 
2795  if (s->sh.first_slice_in_pic_flag) {
2796  if (s->max_ra == INT_MAX) {
2797  if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
2798  s->max_ra = s->poc;
2799  } else {
2800  if (IS_IDR(s))
2801  s->max_ra = INT_MIN;
2802  }
2803  }
2804 
2806  s->poc <= s->max_ra) {
2807  s->is_decoded = 0;
2808  break;
2809  } else {
2810  if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
2811  s->max_ra = INT_MIN;
2812  }
2813 
2814  ret = hevc_frame_start(s);
2815  if (ret < 0)
2816  return ret;
2817  } else if (!s->ref) {
2818  av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2819  goto fail;
2820  }
2821 
2822  if (s->nal_unit_type != s->first_nal_type) {
2824  "Non-matching NAL types of the VCL NALUs: %d %d\n",
2825  s->first_nal_type, s->nal_unit_type);
2826  return AVERROR_INVALIDDATA;
2827  }
2828 
2829  if (!s->sh.dependent_slice_segment_flag &&
2830  s->sh.slice_type != HEVC_SLICE_I) {
2831  ret = ff_hevc_slice_rpl(s);
2832  if (ret < 0) {
2834  "Error constructing the reference lists for the current slice.\n");
2835  goto fail;
2836  }
2837  }
2838 
2839  if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
2840  ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
2841  if (ret < 0)
2842  goto fail;
2843  }
2844 
2845  if (s->avctx->hwaccel) {
2846  ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
2847  if (ret < 0)
2848  goto fail;
2849  } else {
2850  if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
2851  ctb_addr_ts = hls_slice_data_wpp(s, nal);
2852  else
2853  ctb_addr_ts = hls_slice_data(s);
2854  if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
2855  s->is_decoded = 1;
2856  }
2857 
2858  if (ctb_addr_ts < 0) {
2859  ret = ctb_addr_ts;
2860  goto fail;
2861  }
2862  }
2863  break;
2864  case HEVC_NAL_EOS_NUT:
2865  case HEVC_NAL_EOB_NUT:
2866  s->seq_decode = (s->seq_decode + 1) & 0xff;
2867  s->max_ra = INT_MAX;
2868  break;
2869  case HEVC_NAL_AUD:
2870  case HEVC_NAL_FD_NUT:
2871  break;
2872  default:
2873  av_log(s->avctx, AV_LOG_INFO,
2874  "Skipping NAL unit %d\n", s->nal_unit_type);
2875  }
2876 
2877  return 0;
2878 fail:
2880  return ret;
2881  return 0;
2882 }
2883 
2884 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2885 {
2886  int i, ret = 0;
2887 
2888  s->ref = NULL;
2889  s->last_eos = s->eos;
2890  s->eos = 0;
2891 
2892  /* split the input packet into NAL units, so we know the upper bound on the
2893  * number of slices in the frame */
2894  ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
2895  s->nal_length_size, s->avctx->codec_id, 1);
2896  if (ret < 0) {
2898  "Error splitting the input into NAL units.\n");
2899  return ret;
2900  }
2901 
2902  for (i = 0; i < s->pkt.nb_nals; i++) {
2903  if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
2904  s->pkt.nals[i].type == HEVC_NAL_EOS_NUT)
2905  s->eos = 1;
2906  }
2907 
2908  /* decode the NAL units */
2909  for (i = 0; i < s->pkt.nb_nals; i++) {
2910  ret = decode_nal_unit(s, &s->pkt.nals[i]);
2911  if (ret < 0) {
2913  "Error parsing NAL unit #%d.\n", i);
2914  goto fail;
2915  }
2916  }
2917 
2918 fail:
2919  if (s->ref && s->threads_type == FF_THREAD_FRAME)
2920  ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2921 
2922  return ret;
2923 }
2924 
2925 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
2926 {
2927  int i;
2928  for (i = 0; i < 16; i++)
2929  av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2930 }
2931 
2933 {
2935  int pixel_shift;
2936  int i, j;
2937 
2938  if (!desc)
2939  return AVERROR(EINVAL);
2940 
2941  pixel_shift = desc->comp[0].depth > 8;
2942 
2943  av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
2944  s->poc);
2945 
2946  /* the checksums are LE, so we have to byteswap for >8bpp formats
2947  * on BE arches */
2948 #if HAVE_BIGENDIAN
2949  if (pixel_shift && !s->checksum_buf) {
2951  FFMAX3(frame->linesize[0], frame->linesize[1],
2952  frame->linesize[2]));
2953  if (!s->checksum_buf)
2954  return AVERROR(ENOMEM);
2955  }
2956 #endif
2957 
2958  for (i = 0; frame->data[i]; i++) {
2959  int width = s->avctx->coded_width;
2960  int height = s->avctx->coded_height;
2961  int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
2962  int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
2963  uint8_t md5[16];
2964 
2965  av_md5_init(s->md5_ctx);
2966  for (j = 0; j < h; j++) {
2967  const uint8_t *src = frame->data[i] + j * frame->linesize[i];
2968 #if HAVE_BIGENDIAN
2969  if (pixel_shift) {
2970  s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
2971  (const uint16_t *) src, w);
2972  src = s->checksum_buf;
2973  }
2974 #endif
2975  av_md5_update(s->md5_ctx, src, w << pixel_shift);
2976  }
2977  av_md5_final(s->md5_ctx, md5);
2978 
2979  if (!memcmp(md5, s->md5[i], 16)) {
2980  av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
2981  print_md5(s->avctx, AV_LOG_DEBUG, md5);
2982  av_log (s->avctx, AV_LOG_DEBUG, "; ");
2983  } else {
2984  av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
2985  print_md5(s->avctx, AV_LOG_ERROR, md5);
2986  av_log (s->avctx, AV_LOG_ERROR, " != ");
2987  print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
2988  av_log (s->avctx, AV_LOG_ERROR, "\n");
2989  return AVERROR_INVALIDDATA;
2990  }
2991  }
2992 
2993  av_log(s->avctx, AV_LOG_DEBUG, "\n");
2994 
2995  return 0;
2996 }
2997 
2998 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
2999 {
3000  AVCodecContext *avctx = s->avctx;
3001  GetByteContext gb;
3002  int ret, i;
3003 
3004  bytestream2_init(&gb, buf, length);
3005 
3006  if (length > 3 && (buf[0] || buf[1] || buf[2] > 1)) {
3007  /* It seems the extradata is encoded as hvcC format.
3008  * Temporarily, we support configurationVersion==0 until 14496-15 3rd
3009  * is finalized. When finalized, configurationVersion will be 1 and we
3010  * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
3011  int i, j, num_arrays, nal_len_size;
3012 
3013  s->is_nalff = 1;
3014 
3015  bytestream2_skip(&gb, 21);
3016  nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
3017  num_arrays = bytestream2_get_byte(&gb);
3018 
3019  /* nal units in the hvcC always have length coded with 2 bytes,
3020  * so put a fake nal_length_size = 2 while parsing them */
3021  s->nal_length_size = 2;
3022 
3023  /* Decode nal units from hvcC. */
3024  for (i = 0; i < num_arrays; i++) {
3025  int type = bytestream2_get_byte(&gb) & 0x3f;
3026  int cnt = bytestream2_get_be16(&gb);
3027 
3028  for (j = 0; j < cnt; j++) {
3029  // +2 for the nal size field
3030  int nalsize = bytestream2_peek_be16(&gb) + 2;
3031  if (bytestream2_get_bytes_left(&gb) < nalsize) {
3033  "Invalid NAL unit size in extradata.\n");
3034  return AVERROR_INVALIDDATA;
3035  }
3036 
3037  ret = decode_nal_units(s, gb.buffer, nalsize);
3038  if (ret < 0) {
3039  av_log(avctx, AV_LOG_ERROR,
3040  "Decoding nal unit %d %d from hvcC failed\n",
3041  type, i);
3042  return ret;
3043  }
3044  bytestream2_skip(&gb, nalsize);
3045  }
3046  }
3047 
3048  /* Now store right nal length size, that will be used to parse
3049  * all other nals */
3050  s->nal_length_size = nal_len_size;
3051  } else {
3052  s->is_nalff = 0;
3053  ret = decode_nal_units(s, buf, length);
3054  if (ret < 0)
3055  return ret;
3056  }
3057 
3058  /* export stream parameters from the first SPS */
3059  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3060  if (first && s->ps.sps_list[i]) {
3061  const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
3062  export_stream_params(s->avctx, &s->ps, sps);
3063  break;
3064  }
3065  }
3066 
3067  return 0;
3068 }
3069 
3070 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
3071  AVPacket *avpkt)
3072 {
3073  int ret;
3074  int new_extradata_size;
3075  uint8_t *new_extradata;
3076  HEVCContext *s = avctx->priv_data;
3077 
3078  if (!avpkt->size) {
3079  ret = ff_hevc_output_frame(s, data, 1);
3080  if (ret < 0)
3081  return ret;
3082 
3083  *got_output = ret;
3084  return 0;
3085  }
3086 
3087  new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
3088  &new_extradata_size);
3089  if (new_extradata && new_extradata_size > 0) {
3090  ret = hevc_decode_extradata(s, new_extradata, new_extradata_size, 0);
3091  if (ret < 0)
3092  return ret;
3093  }
3094 
3095  s->ref = NULL;
3096  ret = decode_nal_units(s, avpkt->data, avpkt->size);
3097  if (ret < 0)
3098  return ret;
3099 
3100  if (avctx->hwaccel) {
3101  if (s->ref && (ret = avctx->hwaccel->end_frame(avctx)) < 0) {
3102  av_log(avctx, AV_LOG_ERROR,
3103  "hardware accelerator failed to decode picture\n");
3104  ff_hevc_unref_frame(s, s->ref, ~0);
3105  return ret;
3106  }
3107  } else {
3108  /* verify the SEI checksum */
3109  if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
3110  s->is_md5) {
3111  ret = verify_md5(s, s->ref->frame);
3112  if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
3113  ff_hevc_unref_frame(s, s->ref, ~0);
3114  return ret;
3115  }
3116  }
3117  }
3118  s->is_md5 = 0;
3119 
3120  if (s->is_decoded) {
3121  av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
3122  s->is_decoded = 0;
3123  }
3124 
3125  if (s->output_frame->buf[0]) {
3126  av_frame_move_ref(data, s->output_frame);
3127  *got_output = 1;
3128  }
3129 
3130  return avpkt->size;
3131 }
3132 
3134 {
3135  int ret;
3136 
3137  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
3138  if (ret < 0)
3139  return ret;
3140 
3141  dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
3142  if (!dst->tab_mvf_buf)
3143  goto fail;
3144  dst->tab_mvf = src->tab_mvf;
3145 
3146  dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
3147  if (!dst->rpl_tab_buf)
3148  goto fail;
3149  dst->rpl_tab = src->rpl_tab;
3150 
3151  dst->rpl_buf = av_buffer_ref(src->rpl_buf);
3152  if (!dst->rpl_buf)
3153  goto fail;
3154 
3155  dst->poc = src->poc;
3156  dst->ctb_count = src->ctb_count;
3157  dst->window = src->window;
3158  dst->flags = src->flags;
3159  dst->sequence = src->sequence;
3160 
3161  if (src->hwaccel_picture_private) {
3163  if (!dst->hwaccel_priv_buf)
3164  goto fail;
3166  }
3167 
3168  return 0;
3169 fail:
3170  ff_hevc_unref_frame(s, dst, ~0);
3171  return AVERROR(ENOMEM);
3172 }
3173 
3175 {
3176  HEVCContext *s = avctx->priv_data;
3177  int i;
3178 
3179  pic_arrays_free(s);
3180 
3181  av_freep(&s->md5_ctx);
3182 
3183  av_freep(&s->cabac_state);
3184 
3185  for (i = 0; i < 3; i++) {
3186  av_freep(&s->sao_pixel_buffer_h[i]);
3187  av_freep(&s->sao_pixel_buffer_v[i]);
3188  }
3190 
3191  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3192  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3193  av_frame_free(&s->DPB[i].frame);
3194  }
3195 
3196  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
3197  av_buffer_unref(&s->ps.vps_list[i]);
3198  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
3199  av_buffer_unref(&s->ps.sps_list[i]);
3200  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
3201  av_buffer_unref(&s->ps.pps_list[i]);
3202  s->ps.sps = NULL;
3203  s->ps.pps = NULL;
3204  s->ps.vps = NULL;
3205 
3207  av_freep(&s->sh.offset);
3208  av_freep(&s->sh.size);
3209 
3210  for (i = 1; i < s->threads_number; i++) {
3211  HEVCLocalContext *lc = s->HEVClcList[i];
3212  if (lc) {
3213  av_freep(&s->HEVClcList[i]);
3214  av_freep(&s->sList[i]);
3215  }
3216  }
3217  if (s->HEVClc == s->HEVClcList[0])
3218  s->HEVClc = NULL;
3219  av_freep(&s->HEVClcList[0]);
3220 
3222 
3223  return 0;
3224 }
3225 
3227 {
3228  HEVCContext *s = avctx->priv_data;
3229  int i;
3230 
3231  s->avctx = avctx;
3232 
3233  s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
3234  if (!s->HEVClc)
3235  goto fail;
3236  s->HEVClcList[0] = s->HEVClc;
3237  s->sList[0] = s;
3238 
3240  if (!s->cabac_state)
3241  goto fail;
3242 
3244  if (!s->output_frame)
3245  goto fail;
3246 
3247  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3248  s->DPB[i].frame = av_frame_alloc();
3249  if (!s->DPB[i].frame)
3250  goto fail;
3251  s->DPB[i].tf.f = s->DPB[i].frame;
3252  }
3253 
3254  s->max_ra = INT_MAX;
3255 
3256  s->md5_ctx = av_md5_alloc();
3257  if (!s->md5_ctx)
3258  goto fail;
3259 
3260  ff_bswapdsp_init(&s->bdsp);
3261 
3262  s->context_initialized = 1;
3263  s->eos = 0;
3264 
3265  ff_hevc_reset_sei(s);
3266 
3267  return 0;
3268 
3269 fail:
3270  hevc_decode_free(avctx);
3271  return AVERROR(ENOMEM);
3272 }
3273 
3275  const AVCodecContext *src)
3276 {
3277  HEVCContext *s = dst->priv_data;
3278  HEVCContext *s0 = src->priv_data;
3279  int i, ret;
3280 
3281  if (!s->context_initialized) {
3282  ret = hevc_init_context(dst);
3283  if (ret < 0)
3284  return ret;
3285  }
3286 
3287  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3288  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3289  if (s0->DPB[i].frame->buf[0]) {
3290  ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
3291  if (ret < 0)
3292  return ret;
3293  }
3294  }
3295 
3296  if (s->ps.sps != s0->ps.sps)
3297  s->ps.sps = NULL;
3298  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
3299  av_buffer_unref(&s->ps.vps_list[i]);
3300  if (s0->ps.vps_list[i]) {
3301  s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
3302  if (!s->ps.vps_list[i])
3303  return AVERROR(ENOMEM);
3304  }
3305  }
3306 
3307  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3308  av_buffer_unref(&s->ps.sps_list[i]);
3309  if (s0->ps.sps_list[i]) {
3310  s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
3311  if (!s->ps.sps_list[i])
3312  return AVERROR(ENOMEM);
3313  }
3314  }
3315 
3316  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
3317  av_buffer_unref(&s->ps.pps_list[i]);
3318  if (s0->ps.pps_list[i]) {
3319  s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
3320  if (!s->ps.pps_list[i])
3321  return AVERROR(ENOMEM);
3322  }
3323  }
3324 
3325  if (s->ps.sps != s0->ps.sps)
3326  if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
3327  return ret;
3328 
3329  s->seq_decode = s0->seq_decode;
3330  s->seq_output = s0->seq_output;
3331  s->pocTid0 = s0->pocTid0;
3332  s->max_ra = s0->max_ra;
3333  s->eos = s0->eos;
3335 
3336  s->is_nalff = s0->is_nalff;
3338 
3339  s->threads_number = s0->threads_number;
3340  s->threads_type = s0->threads_type;
3341 
3342  if (s0->eos) {
3343  s->seq_decode = (s->seq_decode + 1) & 0xff;
3344  s->max_ra = INT_MAX;
3345  }
3346 
3347  return 0;
3348 }
3349 
3351 {
3352  HEVCContext *s = avctx->priv_data;
3353  int ret;
3354 
3355  avctx->internal->allocate_progress = 1;
3356 
3357  ret = hevc_init_context(avctx);
3358  if (ret < 0)
3359  return ret;
3360 
3361  s->enable_parallel_tiles = 0;
3362  s->picture_struct = 0;
3363  s->eos = 1;
3364 
3365  atomic_init(&s->wpp_err, 0);
3366 
3367  if(avctx->active_thread_type & FF_THREAD_SLICE)
3368  s->threads_number = avctx->thread_count;
3369  else
3370  s->threads_number = 1;
3371 
3372  if (avctx->extradata_size > 0 && avctx->extradata) {
3373  ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
3374  if (ret < 0) {
3375  hevc_decode_free(avctx);
3376  return ret;
3377  }
3378  }
3379 
3380  if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
3382  else
3384 
3385  return 0;
3386 }
3387 
3389 {
3390  HEVCContext *s = avctx->priv_data;
3391  int ret;
3392 
3393  memset(s, 0, sizeof(*s));
3394 
3395  ret = hevc_init_context(avctx);
3396  if (ret < 0)
3397  return ret;
3398 
3399  return 0;
3400 }
3401 
3403 {
3404  HEVCContext *s = avctx->priv_data;
3405  ff_hevc_flush_dpb(s);
3406  s->max_ra = INT_MAX;
3407  s->eos = 1;
3408 }
3409 
3410 #define OFFSET(x) offsetof(HEVCContext, x)
3411 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3412 
3413 static const AVOption options[] = {
3414  { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3415  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3416  { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
3417  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3418  { NULL },
3419 };
3420 
3421 static const AVClass hevc_decoder_class = {
3422  .class_name = "HEVC decoder",
3423  .item_name = av_default_item_name,
3424  .option = options,
3425  .version = LIBAVUTIL_VERSION_INT,
3426 };
3427 
3429  .name = "hevc",
3430  .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3431  .type = AVMEDIA_TYPE_VIDEO,
3432  .id = AV_CODEC_ID_HEVC,
3433  .priv_data_size = sizeof(HEVCContext),
3434  .priv_class = &hevc_decoder_class,
3436  .close = hevc_decode_free,
3439  .update_thread_context = hevc_update_thread_context,
3441  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3443  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
3445 };
static int set_side_data(HEVCContext *s)
Definition: hevcdec.c:2572
static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride, 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:1358
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)
Split an input packet into NAL units.
Definition: h2645_parse.c:250
int8_t cu_qp_offset_cr
Definition: hevcdec.h:379
int frame_packing_arrangement_type
Definition: hevcdec.h:578
static void intra_prediction_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:1942
uint8_t ctb_up_flag
Definition: hevcdec.h:442
const HEVCPPS * pps
Definition: hevc_ps.h:319
AVFrame * frame
Definition: hevcdec.h:394
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
#define NULL
Definition: coverity.c:32
unsigned int log2_min_cb_size
Definition: hevc_ps.h:194
#define SHIFT_CTB_WPP
Definition: hevcdec.h:45
AVRational framerate
Definition: avcodec.h:3429
int sei_frame_packing_present
frame packing arrangement variables
Definition: hevcdec.h:577
const char const char void * val
Definition: avisynth_c.h:771
int(* start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Called at the beginning of each frame or field picture.
Definition: avcodec.h:3864
uint8_t log2_sao_offset_scale_luma
Definition: hevc_ps.h:290
HEVCPredContext hpc
Definition: hevcdec.h:519
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static enum AVPixelFormat pix_fmt
int pic_order_cnt_lsb
Definition: hevcdec.h:257
int short_term_ref_pic_set_sps_flag
Definition: hevcdec.h:265
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:125
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2333
static int verify_md5(HEVCContext *s, AVFrame *frame)
Definition: hevcdec.c:2932
int quincunx_subsampling
Definition: hevcdec.h:580
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
HEVCFrame * ref
Definition: hevcdec.h:505
#define HEVC_CONTEXTS
Definition: hevcdec.h:54
int ctb_height
Definition: hevc_ps.h:215
uint8_t is_cu_qp_delta_coded
Definition: hevcdec.h:376
#define atomic_store(object, desired)
Definition: stdatomic.h:85
AVOption.
Definition: opt.h:246
AVBufferRef * vps_list[HEVC_MAX_VPS_COUNT]
Definition: hevc_ps.h:312
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
int size
Definition: h2645_parse.h:35
uint8_t diff_cu_chroma_qp_offset_depth
Definition: hevc_ps.h:286
struct HEVCSPS::@68 pcm
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:183
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1934
int max_dec_pic_buffering
Definition: hevc_ps.h:162
const char * fmt
Definition: avisynth_c.h:769
void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth)
Definition: hevcpred.c:43
VideoDSPContext vdsp
Definition: hevcdec.h:521
int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0)
Definition: hevc_cabac.c:675
uint8_t edge_emu_buffer[(MAX_PB_SIZE+7)*EDGE_EMU_BUFFER_STRIDE *2]
Definition: hevcdec.h:448
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:392
void(* put_hevc_qpel_bi_w[10][2][2])(uint8_t *dst, ptrdiff_t dststride, uint8_t *_src, ptrdiff_t _srcstride, int16_t *src2, int height, int denom, int wx0, int wx1, int ox0, int ox1, intptr_t mx, intptr_t my, int width)
Definition: hevcdsp.h:82
int content_interpretation_type
Definition: hevcdec.h:579
AVFrame * f
Definition: thread.h:36
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
Definition: hevc_refs.c:135
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:370
int16_t x
horizontal component of motion vector
Definition: hevcdec.h:338
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
Definition: bswapdsp.h:26
const char * desc
Definition: nvenc.c:60
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
Definition: hevcdec.c:2742
ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_RPS_COUNT]
Definition: hevc_ps.h:174
void * hwaccel_picture_private
Definition: hevcdec.h:410
uint8_t * cabac_state
Definition: hevcdec.h:482
int sei_hflip
Definition: hevcdec.h:585
InterPredIdc
Definition: hevcdec.h:159
uint8_t nb_refs
Definition: hevcdec.h:233
MvField * tab_mvf
Definition: hevcdec.h:396
int pic_init_qp_minus26
Definition: hevc_ps.h:243
int bs_width
Definition: hevcdec.h:513
uint8_t intra_split_flag
IntraSplitFlag.
Definition: hevcdec.h:332
VUI vui
Definition: hevc_ps.h:167
int rem_intra_luma_pred_mode
Definition: hevcdec.h:359
static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1, int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
8.5.3.2.2.2 Chroma sample bidirectional interpolation process
Definition: hevcdec.c:1575
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2469
static int hls_transform_tree(HEVCContext *s, 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:1178
int vshift[3]
Definition: hevc_ps.h:226
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
void ff_hevc_flush_dpb(HEVCContext *s)
Drop all frames currently in DPB.
Definition: hevc_refs.c:75
int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth)
Definition: hevc_cabac.c:865
static const AVClass hevc_decoder_class
Definition: hevcdec.c:3421
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1658
unsigned int slice_addr
Definition: hevcdec.h:253
uint32_t vui_time_scale
Definition: hevc_ps.h:76
static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
Definition: hevcdec.c:247
uint8_t weighted_bipred_flag
Definition: hevc_ps.h:255
static void hevc_decode_flush(AVCodecContext *avctx)
Definition: hevcdec.c:3402
int ff_hevc_sao_offset_sign_decode(HEVCContext *s)
Definition: hevc_cabac.c:585
int ff_hevc_frame_rps(HEVCContext *s)
Construct the reference picture sets for the current frame.
Definition: hevc_refs.c:459
int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx, ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
Definition: hevc_ps.c:111
int tc_offset
Definition: hevcdec.h:385
PredictionUnit pu
Definition: hevcdec.h:455
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1960
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
uint8_t seq_loop_filter_across_slices_enabled_flag
Definition: hevc_ps.h:268
uint8_t cabac_init_present_flag
Definition: hevc_ps.h:239
int16_t chroma_offset_l1[16][2]
Definition: hevcdec.h:319
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:524
void(* put_hevc_epel_uni[10][2][2])(uint8_t *dst, ptrdiff_t dststride, uint8_t *_src, ptrdiff_t _srcstride, int height, intptr_t mx, intptr_t my, int width)
Definition: hevcdsp.h:89
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
HEVCParamSets ps
Definition: hevcdec.h:492
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
int min_cb_height
Definition: hevc_ps.h:218
int * ctb_addr_ts_to_rs
CtbAddrTSToRS.
Definition: hevc_ps.h:301
int num_ref_idx_l0_default_active
num_ref_idx_l0_default_active_minus1 + 1
Definition: hevc_ps.h:241
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
int8_t cr_qp_offset_list[6]
Definition: hevc_ps.h:289
int ff_hevc_merge_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:781
uint8_t dependent_slice_segment_flag
Definition: hevcdec.h:260
#define src
Definition: vp8dsp.c:254
CABACContext cc
Definition: hevcdec.h:432
ShortTermRPS slice_rps
Definition: hevcdec.h:267
#define SET_SAO(elem, value)
Definition: hevcdec.c:855
int profile
profile
Definition: avcodec.h:3235
Views are next to each other.
Definition: stereo3d.h:45
AVCodec.
Definition: avcodec.h:3681
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
int width
Definition: hevc_ps.h:212
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
static void hls_sao_param(HEVCContext *s, int rx, int ry)
Definition: hevcdec.c:867
uint16_t seq_decode
Sequence counters for decoded and output frames, so that old frames are output first after a POC rese...
Definition: hevcdec.h:549
static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb, int ctb_addr_ts)
Definition: hevcdec.c:2281
void av_md5_update(AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
Definition: md5.c:153
uint8_t threads_type
Definition: hevcdec.h:476
Macro definitions for various function/variable attributes.
int qp_bd_offset
Definition: hevc_ps.h:228
int pixel_shift
Definition: hevc_ps.h:154
uint8_t entropy_coding_sync_enabled_flag
Definition: hevc_ps.h:261
#define PAR
Definition: hevcdec.c:3411
uint32_t min_mastering_luminance
Definition: hevcdec.h:597
int max_ra
Definition: hevcdec.h:512
int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:817
int output_width
Definition: hevc_ps.h:148
const uint8_t * data
Definition: hevcdec.h:555
AVBufferPool * rpl_tab_pool
candidate references for the current frame
Definition: hevcdec.h:495
uint8_t log2_sao_offset_scale_chroma
Definition: hevc_ps.h:291
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:3052
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:72
#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: avcodec.h:1019
void(* emulated_edge_mc)(uint8_t *dst, const uint8_t *src, ptrdiff_t dst_linesize, ptrdiff_t src_linesize, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:63
int chroma_format_idc
Definition: hevc_ps.h:144
uint8_t disable_dbf
Definition: hevc_ps.h:272
unsigned int log2_max_trafo_size
Definition: hevc_ps.h:197
unsigned int slice_segment_addr
address (in raster order) of the first block in the current slice
Definition: hevcdec.h:251
void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
Definition: hevc_refs.c:32
void(* put_hevc_epel[10][2][2])(int16_t *dst, uint8_t *src, ptrdiff_t srcstride, int height, intptr_t mx, intptr_t my, int width)
Definition: hevcdsp.h:86
enum HEVCSliceType slice_type
Definition: hevcdec.h:255
AVBufferRef * sps_list[HEVC_MAX_SPS_COUNT]
Definition: hevc_ps.h:313
int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size)
Definition: hevc_cabac.c:855
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:48
int ff_hevc_sao_merge_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:550
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s)
Definition: hevc_cabac.c:749
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
int end_of_tiles_x
Definition: hevcdec.h:445
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
enum HEVCNALUnitType nal_unit_type
Definition: hevcdec.h:503
static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0, int log2_cb_size, int ct_depth)
Definition: hevcdec.c:1925
static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:1163
void ff_hevc_reset_sei(HEVCContext *s)
Reset SEI values that are stored on the Context.
Definition: hevc_sei.c:383
float delta
AVOptions.
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:123
uint8_t ctb_up_right_flag
Definition: hevcdec.h:443
LongTermRPS long_term_rps
Definition: hevcdec.h:270
int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH)
Definition: hevc_cabac.c:786
int poc[32]
Definition: hevcdec.h:231
static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
Definition: hevcdec.c:2474
struct HEVCFrame * ref[HEVC_MAX_REFS]
Definition: hevcdec.h:237
uint8_t vps_timing_info_present_flag
Definition: hevc_ps.h:124
uint8_t matrix_coeffs
Definition: hevc_ps.h:61
#define OFFSET(x)
Definition: hevcdec.c:3410
void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0, int log2_trafo_size)
Definition: hevc_filter.c:714
int min_tb_width
Definition: hevc_ps.h:219
int num_entry_point_offsets
Definition: hevcdec.h:303
#define BOUNDARY_LEFT_TILE
Definition: hevcdec.h:459
int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size)
Definition: hevc_cabac.c:694
AVFrame * output_frame
Definition: hevcdec.h:488
int apply_defdispwin
Definition: hevcdec.h:569
SAOParams * sao
Definition: hevcdec.h:501
const HEVCVPS * vps
Definition: hevc_ps.h:317
int num_ref_idx_l1_default_active
num_ref_idx_l1_default_active_minus1 + 1
Definition: hevc_ps.h:242
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1847
unsigned int log2_min_pcm_cb_size
Definition: hevc_ps.h:187
AVCodecContext * avctx
Definition: hevcdec.h:469
int min_cb_width
Definition: hevc_ps.h:217
int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:736
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:371
static AVFrame * frame
#define QPEL_EXTRA_BEFORE
Definition: hevcdec.h:64
struct AVMD5 * md5
Definition: movenc.c:56
Structure to hold side data for an AVFrame.
Definition: frame.h:149
int ff_hevc_slice_rpl(HEVCContext *s)
Construct the reference picture list(s) for the current slice.
Definition: hevc_refs.c:299
BswapDSPContext bdsp
Definition: hevcdec.h:522
#define height
ThreadFrame tf
Definition: hevcdec.h:395
uint8_t first_slice_in_pic_flag
Definition: hevcdec.h:259
uint8_t * data
Definition: avcodec.h:1657
uint8_t bit_depth_chroma
Definition: hevc_ps.h:186
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
const uint8_t * buffer
Definition: bytestream.h:34
uint8_t ctb_up_left_flag
Definition: hevcdec.h:444
void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc_cabac.c:1520
int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0, int x_cb, int y_cb)
Definition: hevc_cabac.c:607
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:3970
uint8_t threads_number
Definition: hevcdec.h:477
uint8_t is_cu_chroma_qp_offset_coded
Definition: hevcdec.h:377
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:225
void(* put_hevc_qpel_uni_w[10][2][2])(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride, int height, int denom, int wx, int ox, intptr_t mx, intptr_t my, int width)
Definition: hevcdsp.h:76
int8_t pred_flag
Definition: hevcdec.h:345
AVBufferRef * pps_list[HEVC_MAX_PPS_COUNT]
Definition: hevc_ps.h:314
int8_t * qp_y_tab
Definition: hevcdec.h:523
uint8_t loop_filter_disable_flag
Definition: hevc_ps.h:189
int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth)
Definition: hevc_cabac.c:860
int sei_anticlockwise_rotation
Definition: hevcdec.h:584
ptrdiff_t size
Definition: opengl_enc.c:101
uint8_t pic_output_flag
Definition: hevcdec.h:261
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
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
uint8_t * tab_ct_depth
Definition: hevcdec.h:531
uint8_t cu_transquant_bypass_flag
Definition: hevcdec.h:334
int16_t tmp[MAX_PB_SIZE *MAX_PB_SIZE]
Definition: hevcdec.h:451
void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size)
Definition: hevc_filter.c:842
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
Views are alternated temporally.
Definition: stereo3d.h:66
void(* put_hevc_qpel[10][2][2])(int16_t *dst, uint8_t *src, ptrdiff_t srcstride, int height, intptr_t mx, intptr_t my, int width)
Definition: hevcdsp.h:72
int8_t cb_qp_offset_list[6]
Definition: hevc_ps.h:288
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
int ff_hevc_log2_res_scale_abs(HEVCContext *s, int idx)
Definition: hevc_cabac.c:885
uint8_t transquant_bypass_enable_flag
Definition: hevc_ps.h:257
#define av_log(a,...)
uint8_t used[32]
Definition: hevcdec.h:232
int temporal_id
temporal_id_plus1 - 1
Definition: hevcdec.h:504
uint8_t first_qp_group
Definition: hevcdec.h:429
HEVCDSPContext hevcdsp
Definition: hevcdec.h:520
int ctb_count
Definition: hevcdec.h:399
uint8_t no_output_of_prior_pics_flag
Definition: hevcdec.h:274
HEVCLocalContext * HEVClcList[MAX_NB_THREADS]
Definition: hevcdec.h:473
void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts)
Definition: hevc_cabac.c:504
int ff_hevc_cu_chroma_qp_offset_idx(HEVCContext *s)
Definition: hevc_cabac.c:659
const AVProfile ff_hevc_profiles[]
Definition: profiles.c:78
int slice_idx
number of the slice being currently decoded
Definition: hevcdec.h:509
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
Definition: hevcdec.c:2397
uint8_t intra_pred_mode[4]
Definition: hevcdec.h:360
uint8_t colour_plane_id
RPS coded in the slice header itself is stored here.
Definition: hevcdec.h:262
#define QPEL_EXTRA_AFTER
Definition: hevcdec.h:65
int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:602
#define SAMPLE_CTB(tab, x, y)
Definition: hevcdec.h:74
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:2054
uint8_t cu_chroma_qp_offset_enabled_flag
Definition: hevcdec.h:293
int flags
Additional information about the frame packing.
Definition: stereo3d.h:132
uint8_t slice_initialized
1 if the independent slice segment header was successfully parsed
Definition: hevcdec.h:485
unsigned int log2_max_poc_lsb
Definition: hevc_ps.h:157
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
Definition: hevcdec.c:3133
int * skipped_bytes_pos
Definition: h2645_parse.h:61
int min_pu_height
Definition: hevc_ps.h:222
#define atomic_load(object)
Definition: stdatomic.h:93
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:416
av_default_item_name
AVBufferRef * rpl_tab_buf
Definition: hevcdec.h:406
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
Definition: hevc_filter.c:867
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
uint8_t rpl_modification_flag[2]
Definition: hevcdec.h:273
int * size
Definition: hevcdec.h:302
int vui_timing_info_present_flag
Definition: hevc_ps.h:74
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
Definition: hevcdec.c:2884
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:3180
static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:1301
RefPicList * refPicList
Definition: hevcdec.h:397
int16_t luma_offset_l0[16]
Definition: hevcdec.h:315
int bs_height
Definition: hevcdec.h:514
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:57
void(* intra_pred[4])(struct HEVCContext *s, int x0, int y0, int c_idx)
Definition: hevcpred.h:32
#define CTB(tab, x, y)
Definition: hevcdec.c:853
#define s0
Definition: regdef.h:37
uint16_t display_primaries[3][2]
Definition: hevcdec.h:594
static void intra_prediction_unit_default_value(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:2010
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
const char * arg
Definition: jacosubdec.c:66
unsigned int log2_ctb_size
Definition: hevc_ps.h:198
static int set_sps(HEVCContext *s, const HEVCSPS *sps, enum AVPixelFormat pix_fmt)
Definition: hevcdec.c:351
int picture_struct
Definition: hevcdec.h:587
uint8_t * sao_pixel_buffer_h[3]
Definition: hevcdec.h:489
int8_t cu_qp_offset_cb
Definition: hevcdec.h:378
GLsizei GLsizei * length
Definition: opengl_enc.c:115
int tc_offset
tc_offset_div2 * 2
Definition: hevcdec.h:296
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
const ShortTermRPS * short_term_rps
Definition: hevcdec.h:268
uint8_t merge_flag
Definition: hevcdec.h:362
#define IS_IDR(s)
Definition: hevcdec.h:76
struct AVMD5 * md5_ctx
Definition: hevcdec.h:562
unsigned * entry_point_offset
Definition: hevcdec.h:300
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int8_t slice_qp
Definition: hevcdec.h:305
#define FFMAX(a, b)
Definition: common.h:94
#define BOUNDARY_UPPER_TILE
Definition: hevcdec.h:461
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
#define fail()
Definition: checkasm.h:89
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1057
uint8_t max_trafo_depth
MaxTrafoDepth.
Definition: hevcdec.h:333
uint8_t edge_emu_buffer2[(MAX_PB_SIZE+7)*EDGE_EMU_BUFFER_STRIDE *2]
Definition: hevcdec.h:450
int raw_size
Definition: h2645_parse.h:44
uint16_t sequence
A sequence counter, so that old frames are output first after a POC reset.
Definition: hevcdec.h:416
uint8_t colour_primaries
Definition: hevc_ps.h:59
static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output, AVPacket *avpkt)
Definition: hevcdec.c:3070
static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevcdec.c:2033
uint8_t slice_temporal_mvp_enabled_flag
Definition: hevcdec.h:275
Definition: hevcdec.h:168
uint8_t * vertical_bs
Definition: hevcdec.h:525
int ff_hevc_sao_band_position_decode(HEVCContext *s)
Definition: hevc_cabac.c:565
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
int chroma_mode_c
Definition: hevcdec.h:375
uint8_t tiles_enabled_flag
Definition: hevc_ps.h:260
int ff_alloc_entries(AVCodecContext *avctx, int count)
int eo_class[3]
sao_eo_class
Definition: hevcdsp.h:38
uint32_t vps_num_units_in_tick
Definition: hevc_ps.h:125
int16_t luma_weight_l0[16]
Definition: hevcdec.h:310
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:469
int * col_idxX
Definition: hevc_ps.h:298
struct HEVCContext * sList[MAX_NB_THREADS]
Definition: hevcdec.h:471
int slice_qp_delta
Definition: hevcdec.h:289
common internal API header
int intra_pred_mode
Definition: hevcdec.h:373
enum HEVCNALUnitType first_nal_type
Definition: hevcdec.h:559
int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb)
Compute POC of the current frame and return it.
Definition: hevc_refs.c:519
const HEVCSPS * sps
Definition: hevc_ps.h:318
int ff_hevc_sao_eo_class_decode(HEVCContext *s)
Definition: hevc_cabac.c:590
uint8_t lists_modification_present_flag
Definition: hevc_ps.h:279
#define IS_IRAP(s)
Definition: hevcdec.h:79
uint8_t profile_idc
Definition: hevc_ps.h:95
#define L1
Definition: hevcdec.h:59
AVBufferRef * tab_mvf_buf
Definition: hevcdec.h:405
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:171
uint8_t type_idx[3]
sao_type_idx
Definition: hevcdsp.h:42
uint8_t chroma_mode_c[4]
Definition: hevcdec.h:364
int beta_offset
beta_offset_div2 * 2
Definition: hevcdec.h:295
AVMasteringDisplayMetadata * av_mastering_display_metadata_create_side_data(AVFrame *frame)
Allocate a complete AVMasteringDisplayMetadata and add it to the frame.
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:261
int res_scale_val
Definition: hevcdec.h:370
int ff_hevc_res_scale_sign_flag(HEVCContext *s, int idx)
Definition: hevc_cabac.c:894
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:3020
Definition: hevcdec.h:169
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:3172
int max_transform_hierarchy_depth_inter
Definition: hevc_ps.h:201
#define FFMIN(a, b)
Definition: common.h:96
uint8_t * sao_pixel_buffer_v[3]
Definition: hevcdec.h:490
#define EPEL_EXTRA_BEFORE
Definition: hevcdec.h:61
int slice_cr_qp_offset
Definition: hevcdec.h:291
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:74
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
int offset_abs[3][4]
sao_offset_abs
Definition: hevcdsp.h:33
int num_tile_columns
num_tile_columns_minus1 + 1
Definition: hevc_ps.h:263
#define width
int output_height
Definition: hevc_ps.h:148
int width
picture width / height.
Definition: avcodec.h:1919
uint8_t * tab_ipm
Definition: hevcdec.h:533
static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size, int prev_intra_luma_pred_flag)
8.4.1
Definition: hevcdec.c:1845
int hshift[3]
Definition: hevc_ps.h:225
struct HEVCSPS::@67 temporal_layer[HEVC_MAX_SUB_LAYERS]
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
int32_t
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:177
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2448
uint8_t cu_qp_delta_enabled_flag
Definition: hevc_ps.h:248
uint8_t used_by_curr_pic_lt_sps_flag[32]
Definition: hevc_ps.h:181
static void print_md5(void *log_ctx, int level, uint8_t md5[16])
Definition: hevcdec.c:2925
int sei_mastering_display_info_present
mastering display
Definition: hevcdec.h:593
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:3173
Context Adaptive Binary Arithmetic Coder inline functions.
int level
level
Definition: avcodec.h:3333
int intra_pred_mode_c
Definition: hevcdec.h:374
int ctb_width
Definition: hevc_ps.h:214
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:57
int16_t chroma_weight_l0[16][2]
Definition: hevcdec.h:311
int height
Definition: hevc_ps.h:213
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:3031
void(* put_hevc_qpel_bi[10][2][2])(uint8_t *dst, ptrdiff_t dststride, uint8_t *_src, ptrdiff_t _srcstride, int16_t *src2, int height, intptr_t mx, intptr_t my, int width)
Definition: hevcdsp.h:79
static av_cold int hevc_decode_init(AVCodecContext *avctx)
Definition: hevcdec.c:3350
uint8_t output_flag_present_flag
Definition: hevc_ps.h:256
uint16_t seq_output
Definition: hevcdec.h:550
PTLCommon general_ptl
Definition: hevc_ps.h:105
static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
Definition: hevcdec.c:2998
int16_t luma_offset_l1[16]
Definition: hevcdec.h:318
int16_t chroma_offset_l0[16][2]
Definition: hevcdec.h:316
uint32_t max_mastering_luminance
Definition: hevcdec.h:596
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:114
int type
NAL unit type.
Definition: h2645_parse.h:52
static void error(const char *err)
void av_display_rotation_set(int32_t matrix[9], double angle)
Initialize a transformation matrix describing a pure counterclockwise rotation by the specified angle...
Definition: display.c:50
#define IS_BLA(s)
Definition: hevcdec.h:77
#define POS(c_idx, x, y)
unsigned vps_id
Definition: hevc_ps.h:143
#define FF_ARRAY_ELEMS(a)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3161
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:476
unsigned int pps_id
address (in raster order) of the first block in the current slice segment
Definition: hevcdec.h:248
uint8_t pic_slice_level_chroma_qp_offsets_present_flag
Definition: hevc_ps.h:253
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:85
uint32_t vps_time_scale
Definition: hevc_ps.h:126
void ff_reset_entries(AVCodecContext *avctx)
int colour_description_present_flag
Definition: hevc_ps.h:58
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1061
static const int8_t mv[256][2]
Definition: 4xm.c:77
HEVCFrame DPB[32]
Definition: hevcdec.h:506
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:251
enum AVPixelFormat pix_fmt
Definition: hevc_ps.h:155
RefPicListTab ** rpl_tab
Definition: hevcdec.h:398
int ff_hevc_merge_idx_decode(HEVCContext *s)
Definition: hevc_cabac.c:770
int sei_display_orientation_present
display orientation
Definition: hevcdec.h:583
int slice_cb_qp_offset
Definition: hevcdec.h:290
void ff_hevc_dsp_init(HEVCDSPContext *hevcdsp, int bit_depth)
Definition: hevcdsp.c:126
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1412
const uint8_t ff_hevc_pel_weight[65]
Definition: hevcdec.c:45
#define src1
Definition: h264pred.c:139
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:127
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
void ff_hevc_clear_refs(HEVCContext *s)
Mark all frames in DPB as unused for reference.
Definition: hevc_refs.c:66
static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist, int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
8.5.3.2.2.2 Chroma sample uniprediction interpolation process
Definition: hevcdec.c:1510
int short_term_ref_pic_set_size
Definition: hevcdec.h:266
static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW, int nPbH, int log2_cb_size, int part_idx, int merge_idx, MvField *mv)
Definition: hevcdec.c:1674
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: frame.h:83
#define L0
Definition: hevcdec.h:58
uint8_t is_nalff
this flag is != 0 if bitstream is encapsulated as a format defined in 14496-15
Definition: hevcdec.h:567
static int hls_transform_unit(HEVCContext *s, 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:957
enum AVCodecID codec_id
Definition: avcodec.h:1749
int * ctb_addr_rs_to_ts
CtbAddrRSToTS.
Definition: hevc_ps.h:300
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
PTL ptl
Definition: hevc_ps.h:168
int max_sub_layers
Definition: