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