FFmpeg  2.8.17
 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 (s->ref && sh->first_slice_in_pic_flag) {
427  av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
428  return 1; // This slice will be skipped later, do not corrupt state
429  }
430 
431  if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
432  s->seq_decode = (s->seq_decode + 1) & 0xff;
433  s->max_ra = INT_MAX;
434  if (IS_IDR(s))
436  }
438  if (IS_IRAP(s))
440 
441  sh->pps_id = get_ue_golomb_long(gb);
442  if (sh->pps_id >= MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
443  av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
444  return AVERROR_INVALIDDATA;
445  }
446  if (!sh->first_slice_in_pic_flag &&
447  s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
448  av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
449  return AVERROR_INVALIDDATA;
450  }
451  s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
452  if (s->nal_unit_type == NAL_CRA_NUT && s->last_eos == 1)
454 
455  if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
456  const HEVCSPS* last_sps = s->ps.sps;
457  s->ps.sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
458  if (last_sps && IS_IRAP(s) && s->nal_unit_type != NAL_CRA_NUT) {
459  if (s->ps.sps->width != last_sps->width || s->ps.sps->height != last_sps->height ||
461  last_sps->temporal_layer[last_sps->max_sub_layers - 1].max_dec_pic_buffering)
463  }
465  ret = set_sps(s, s->ps.sps, AV_PIX_FMT_NONE);
466  if (ret < 0)
467  return ret;
468 
469  s->seq_decode = (s->seq_decode + 1) & 0xff;
470  s->max_ra = INT_MAX;
471  }
472 
474  if (!sh->first_slice_in_pic_flag) {
475  int slice_address_length;
476 
479 
480  slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
481  s->ps.sps->ctb_height);
482  sh->slice_segment_addr = slice_address_length ? get_bits(gb, slice_address_length) : 0;
483  if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
485  "Invalid slice segment address: %u.\n",
486  sh->slice_segment_addr);
487  return AVERROR_INVALIDDATA;
488  }
489 
490  if (!sh->dependent_slice_segment_flag) {
491  sh->slice_addr = sh->slice_segment_addr;
492  s->slice_idx++;
493  }
494  } else {
495  sh->slice_segment_addr = sh->slice_addr = 0;
496  s->slice_idx = 0;
497  s->slice_initialized = 0;
498  }
499 
500  if (!sh->dependent_slice_segment_flag) {
501  s->slice_initialized = 0;
502 
503  for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
504  skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
505 
506  sh->slice_type = get_ue_golomb_long(gb);
507  if (!(sh->slice_type == I_SLICE ||
508  sh->slice_type == P_SLICE ||
509  sh->slice_type == B_SLICE)) {
510  av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
511  sh->slice_type);
512  return AVERROR_INVALIDDATA;
513  }
514  if (IS_IRAP(s) && sh->slice_type != I_SLICE) {
515  av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
516  return AVERROR_INVALIDDATA;
517  }
518 
519  // when flag is not present, picture is inferred to be output
520  sh->pic_output_flag = 1;
522  sh->pic_output_flag = get_bits1(gb);
523 
525  sh->colour_plane_id = get_bits(gb, 2);
526 
527  if (!IS_IDR(s)) {
528  int poc, pos;
529 
532  if (!sh->first_slice_in_pic_flag && poc != s->poc) {
534  "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
536  return AVERROR_INVALIDDATA;
537  poc = s->poc;
538  }
539  s->poc = poc;
540 
542  pos = get_bits_left(gb);
544  ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
545  if (ret < 0)
546  return ret;
547 
548  sh->short_term_rps = &sh->slice_rps;
549  } else {
550  int numbits, rps_idx;
551 
552  if (!s->ps.sps->nb_st_rps) {
553  av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
554  return AVERROR_INVALIDDATA;
555  }
556 
557  numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
558  rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
559  sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
560  }
562 
563  pos = get_bits_left(gb);
564  ret = decode_lt_rps(s, &sh->long_term_rps, gb);
565  if (ret < 0) {
566  av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
568  return AVERROR_INVALIDDATA;
569  }
571 
574  else
576  } else {
577  s->sh.short_term_rps = NULL;
578  s->poc = 0;
579  }
580 
581  /* 8.3.1 */
582  if (s->temporal_id == 0 &&
583  s->nal_unit_type != NAL_TRAIL_N &&
584  s->nal_unit_type != NAL_TSA_N &&
585  s->nal_unit_type != NAL_STSA_N &&
586  s->nal_unit_type != NAL_RADL_N &&
587  s->nal_unit_type != NAL_RADL_R &&
588  s->nal_unit_type != NAL_RASL_N &&
590  s->pocTid0 = s->poc;
591 
592  if (s->ps.sps->sao_enabled) {
594  if (s->ps.sps->chroma_format_idc) {
597  }
598  } else {
602  }
603 
604  sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
605  if (sh->slice_type == P_SLICE || sh->slice_type == B_SLICE) {
606  int nb_refs;
607 
609  if (sh->slice_type == B_SLICE)
611 
612  if (get_bits1(gb)) { // num_ref_idx_active_override_flag
613  sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
614  if (sh->slice_type == B_SLICE)
615  sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
616  }
617  if (sh->nb_refs[L0] > MAX_REFS || sh->nb_refs[L1] > MAX_REFS) {
618  av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
619  sh->nb_refs[L0], sh->nb_refs[L1]);
620  return AVERROR_INVALIDDATA;
621  }
622 
623  sh->rpl_modification_flag[0] = 0;
624  sh->rpl_modification_flag[1] = 0;
625  nb_refs = ff_hevc_frame_nb_refs(s);
626  if (!nb_refs) {
627  av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
628  return AVERROR_INVALIDDATA;
629  }
630 
631  if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
632  sh->rpl_modification_flag[0] = get_bits1(gb);
633  if (sh->rpl_modification_flag[0]) {
634  for (i = 0; i < sh->nb_refs[L0]; i++)
635  sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
636  }
637 
638  if (sh->slice_type == B_SLICE) {
639  sh->rpl_modification_flag[1] = get_bits1(gb);
640  if (sh->rpl_modification_flag[1] == 1)
641  for (i = 0; i < sh->nb_refs[L1]; i++)
642  sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
643  }
644  }
645 
646  if (sh->slice_type == B_SLICE)
647  sh->mvd_l1_zero_flag = get_bits1(gb);
648 
650  sh->cabac_init_flag = get_bits1(gb);
651  else
652  sh->cabac_init_flag = 0;
653 
654  sh->collocated_ref_idx = 0;
656  sh->collocated_list = L0;
657  if (sh->slice_type == B_SLICE)
658  sh->collocated_list = !get_bits1(gb);
659 
660  if (sh->nb_refs[sh->collocated_list] > 1) {
662  if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
664  "Invalid collocated_ref_idx: %d.\n",
665  sh->collocated_ref_idx);
666  return AVERROR_INVALIDDATA;
667  }
668  }
669  }
670 
671  if ((s->ps.pps->weighted_pred_flag && sh->slice_type == P_SLICE) ||
672  (s->ps.pps->weighted_bipred_flag && sh->slice_type == B_SLICE)) {
673  pred_weight_table(s, gb);
674  }
675 
677  if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
679  "Invalid number of merging MVP candidates: %d.\n",
680  sh->max_num_merge_cand);
681  return AVERROR_INVALIDDATA;
682  }
683  }
684 
685  sh->slice_qp_delta = get_se_golomb(gb);
686 
690  } else {
691  sh->slice_cb_qp_offset = 0;
692  sh->slice_cr_qp_offset = 0;
693  }
694 
697  else
699 
701  int deblocking_filter_override_flag = 0;
702 
704  deblocking_filter_override_flag = get_bits1(gb);
705 
706  if (deblocking_filter_override_flag) {
709  sh->beta_offset = get_se_golomb(gb) * 2;
710  sh->tc_offset = get_se_golomb(gb) * 2;
711  }
712  } else {
714  sh->beta_offset = s->ps.pps->beta_offset;
715  sh->tc_offset = s->ps.pps->tc_offset;
716  }
717  } else {
719  sh->beta_offset = 0;
720  sh->tc_offset = 0;
721  }
722 
728  } else {
730  }
731  } else if (!s->slice_initialized) {
732  av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
733  return AVERROR_INVALIDDATA;
734  }
735 
736  sh->num_entry_point_offsets = 0;
738  unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
739  // It would be possible to bound this tighter but this here is simpler
740  if (num_entry_point_offsets > get_bits_left(gb)) {
741  av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
742  return AVERROR_INVALIDDATA;
743  }
744 
745  sh->num_entry_point_offsets = num_entry_point_offsets;
746  if (sh->num_entry_point_offsets > 0) {
747  int offset_len = get_ue_golomb_long(gb) + 1;
748 
749  if (offset_len < 1 || offset_len > 32) {
750  sh->num_entry_point_offsets = 0;
751  av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
752  return AVERROR_INVALIDDATA;
753  }
754 
756  av_freep(&sh->offset);
757  av_freep(&sh->size);
758  sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
759  sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
760  sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
761  if (!sh->entry_point_offset || !sh->offset || !sh->size) {
762  sh->num_entry_point_offsets = 0;
763  av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
764  return AVERROR(ENOMEM);
765  }
766  for (i = 0; i < sh->num_entry_point_offsets; i++) {
767  unsigned val = get_bits_long(gb, offset_len);
768  sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
769  }
770  if (s->threads_number > 1 && (s->ps.pps->num_tile_rows > 1 || s->ps.pps->num_tile_columns > 1)) {
771  s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
772  s->threads_number = 1;
773  } else
774  s->enable_parallel_tiles = 0;
775  } else
776  s->enable_parallel_tiles = 0;
777  }
778 
780  unsigned int length = get_ue_golomb_long(gb);
781  if (length*8LL > get_bits_left(gb)) {
782  av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
783  return AVERROR_INVALIDDATA;
784  }
785  for (i = 0; i < length; i++)
786  skip_bits(gb, 8); // slice_header_extension_data_byte
787  }
788 
789  // Inferred parameters
790  sh->slice_qp = 26U + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
791  if (sh->slice_qp > 51 ||
792  sh->slice_qp < -s->ps.sps->qp_bd_offset) {
794  "The slice_qp %d is outside the valid range "
795  "[%d, 51].\n",
796  sh->slice_qp,
797  -s->ps.sps->qp_bd_offset);
798  return AVERROR_INVALIDDATA;
799  }
800 
802 
804  av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
805  return AVERROR_INVALIDDATA;
806  }
807 
808  if (get_bits_left(gb) < 0) {
810  "Overread slice header by %d bits\n", -get_bits_left(gb));
811  return AVERROR_INVALIDDATA;
812  }
813 
815 
817  s->HEVClc->qp_y = s->sh.slice_qp;
818 
819  s->slice_initialized = 1;
820  s->HEVClc->tu.cu_qp_offset_cb = 0;
821  s->HEVClc->tu.cu_qp_offset_cr = 0;
822 
823  s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == NAL_CRA_NUT && s->last_eos);
824 
825  return 0;
826 }
827 
828 #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
829 
830 #define SET_SAO(elem, value) \
831 do { \
832  if (!sao_merge_up_flag && !sao_merge_left_flag) \
833  sao->elem = value; \
834  else if (sao_merge_left_flag) \
835  sao->elem = CTB(s->sao, rx-1, ry).elem; \
836  else if (sao_merge_up_flag) \
837  sao->elem = CTB(s->sao, rx, ry-1).elem; \
838  else \
839  sao->elem = 0; \
840 } while (0)
841 
842 static void hls_sao_param(HEVCContext *s, int rx, int ry)
843 {
844  HEVCLocalContext *lc = s->HEVClc;
845  int sao_merge_left_flag = 0;
846  int sao_merge_up_flag = 0;
847  SAOParams *sao = &CTB(s->sao, rx, ry);
848  int c_idx, i;
849 
852  if (rx > 0) {
853  if (lc->ctb_left_flag)
854  sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
855  }
856  if (ry > 0 && !sao_merge_left_flag) {
857  if (lc->ctb_up_flag)
858  sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
859  }
860  }
861 
862  for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
863  int log2_sao_offset_scale = c_idx == 0 ? s->ps.pps->log2_sao_offset_scale_luma :
865 
866  if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
867  sao->type_idx[c_idx] = SAO_NOT_APPLIED;
868  continue;
869  }
870 
871  if (c_idx == 2) {
872  sao->type_idx[2] = sao->type_idx[1];
873  sao->eo_class[2] = sao->eo_class[1];
874  } else {
875  SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
876  }
877 
878  if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
879  continue;
880 
881  for (i = 0; i < 4; i++)
882  SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
883 
884  if (sao->type_idx[c_idx] == SAO_BAND) {
885  for (i = 0; i < 4; i++) {
886  if (sao->offset_abs[c_idx][i]) {
887  SET_SAO(offset_sign[c_idx][i],
889  } else {
890  sao->offset_sign[c_idx][i] = 0;
891  }
892  }
893  SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
894  } else if (c_idx != 2) {
895  SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
896  }
897 
898  // Inferred parameters
899  sao->offset_val[c_idx][0] = 0;
900  for (i = 0; i < 4; i++) {
901  sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
902  if (sao->type_idx[c_idx] == SAO_EDGE) {
903  if (i > 1)
904  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
905  } else if (sao->offset_sign[c_idx][i]) {
906  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
907  }
908  sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
909  }
910  }
911 }
912 
913 #undef SET_SAO
914 #undef CTB
915 
916 static int hls_cross_component_pred(HEVCContext *s, int idx) {
917  HEVCLocalContext *lc = s->HEVClc;
918  int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
919 
920  if (log2_res_scale_abs_plus1 != 0) {
921  int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
922  lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
923  (1 - 2 * res_scale_sign_flag);
924  } else {
925  lc->tu.res_scale_val = 0;
926  }
927 
928 
929  return 0;
930 }
931 
932 static int hls_transform_unit(HEVCContext *s, int x0, int y0,
933  int xBase, int yBase, int cb_xBase, int cb_yBase,
934  int log2_cb_size, int log2_trafo_size,
935  int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
936 {
937  HEVCLocalContext *lc = s->HEVClc;
938  const int log2_trafo_size_c = log2_trafo_size - s->ps.sps->hshift[1];
939  int i;
940 
941  if (lc->cu.pred_mode == MODE_INTRA) {
942  int trafo_size = 1 << log2_trafo_size;
943  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
944 
945  s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
946  }
947 
948  if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
949  (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
950  int scan_idx = SCAN_DIAG;
951  int scan_idx_c = SCAN_DIAG;
952  int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
953  (s->ps.sps->chroma_format_idc == 2 &&
954  (cbf_cb[1] || cbf_cr[1]));
955 
958  if (lc->tu.cu_qp_delta != 0)
959  if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
960  lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
961  lc->tu.is_cu_qp_delta_coded = 1;
962 
963  if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
964  lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
966  "The cu_qp_delta %d is outside the valid range "
967  "[%d, %d].\n",
968  lc->tu.cu_qp_delta,
969  -(26 + s->ps.sps->qp_bd_offset / 2),
970  (25 + s->ps.sps->qp_bd_offset / 2));
971  return AVERROR_INVALIDDATA;
972  }
973 
974  ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
975  }
976 
977  if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
979  int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
980  if (cu_chroma_qp_offset_flag) {
981  int cu_chroma_qp_offset_idx = 0;
983  cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
985  "cu_chroma_qp_offset_idx not yet tested.\n");
986  }
987  lc->tu.cu_qp_offset_cb = s->ps.pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
988  lc->tu.cu_qp_offset_cr = s->ps.pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
989  } else {
990  lc->tu.cu_qp_offset_cb = 0;
991  lc->tu.cu_qp_offset_cr = 0;
992  }
994  }
995 
996  if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
997  if (lc->tu.intra_pred_mode >= 6 &&
998  lc->tu.intra_pred_mode <= 14) {
999  scan_idx = SCAN_VERT;
1000  } else if (lc->tu.intra_pred_mode >= 22 &&
1001  lc->tu.intra_pred_mode <= 30) {
1002  scan_idx = SCAN_HORIZ;
1003  }
1004 
1005  if (lc->tu.intra_pred_mode_c >= 6 &&
1006  lc->tu.intra_pred_mode_c <= 14) {
1007  scan_idx_c = SCAN_VERT;
1008  } else if (lc->tu.intra_pred_mode_c >= 22 &&
1009  lc->tu.intra_pred_mode_c <= 30) {
1010  scan_idx_c = SCAN_HORIZ;
1011  }
1012  }
1013 
1014  lc->tu.cross_pf = 0;
1015 
1016  if (cbf_luma)
1017  ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1018  if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1019  int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1020  int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1021  lc->tu.cross_pf = (s->ps.pps->cross_component_prediction_enabled_flag && cbf_luma &&
1022  (lc->cu.pred_mode == MODE_INTER ||
1023  (lc->tu.chroma_mode_c == 4)));
1024 
1025  if (lc->tu.cross_pf) {
1027  }
1028  for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1029  if (lc->cu.pred_mode == MODE_INTRA) {
1030  ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1031  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
1032  }
1033  if (cbf_cb[i])
1034  ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1035  log2_trafo_size_c, scan_idx_c, 1);
1036  else
1037  if (lc->tu.cross_pf) {
1038  ptrdiff_t stride = s->frame->linesize[1];
1039  int hshift = s->ps.sps->hshift[1];
1040  int vshift = s->ps.sps->vshift[1];
1041  int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1042  int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1043  int size = 1 << log2_trafo_size_c;
1044 
1045  uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
1046  ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1047  for (i = 0; i < (size * size); i++) {
1048  coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1049  }
1050  s->hevcdsp.transform_add[log2_trafo_size_c-2](dst, coeffs, stride);
1051  }
1052  }
1053 
1054  if (lc->tu.cross_pf) {
1056  }
1057  for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1058  if (lc->cu.pred_mode == MODE_INTRA) {
1059  ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1060  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
1061  }
1062  if (cbf_cr[i])
1063  ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1064  log2_trafo_size_c, scan_idx_c, 2);
1065  else
1066  if (lc->tu.cross_pf) {
1067  ptrdiff_t stride = s->frame->linesize[2];
1068  int hshift = s->ps.sps->hshift[2];
1069  int vshift = s->ps.sps->vshift[2];
1070  int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1071  int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1072  int size = 1 << log2_trafo_size_c;
1073 
1074  uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
1075  ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1076  for (i = 0; i < (size * size); i++) {
1077  coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1078  }
1079  s->hevcdsp.transform_add[log2_trafo_size_c-2](dst, coeffs, stride);
1080  }
1081  }
1082  } else if (s->ps.sps->chroma_format_idc && blk_idx == 3) {
1083  int trafo_size_h = 1 << (log2_trafo_size + 1);
1084  int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1085  for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1086  if (lc->cu.pred_mode == MODE_INTRA) {
1087  ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1088  trafo_size_h, trafo_size_v);
1089  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
1090  }
1091  if (cbf_cb[i])
1092  ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1093  log2_trafo_size, scan_idx_c, 1);
1094  }
1095  for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1096  if (lc->cu.pred_mode == MODE_INTRA) {
1097  ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1098  trafo_size_h, trafo_size_v);
1099  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
1100  }
1101  if (cbf_cr[i])
1102  ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1103  log2_trafo_size, scan_idx_c, 2);
1104  }
1105  }
1106  } else if (s->ps.sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1107  if (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3) {
1108  int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1109  int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1110  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
1111  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
1112  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
1113  if (s->ps.sps->chroma_format_idc == 2) {
1114  ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
1115  trafo_size_h, trafo_size_v);
1116  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
1117  s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
1118  }
1119  } else if (blk_idx == 3) {
1120  int trafo_size_h = 1 << (log2_trafo_size + 1);
1121  int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1122  ff_hevc_set_neighbour_available(s, xBase, yBase,
1123  trafo_size_h, trafo_size_v);
1124  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
1125  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
1126  if (s->ps.sps->chroma_format_idc == 2) {
1127  ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
1128  trafo_size_h, trafo_size_v);
1129  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
1130  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
1131  }
1132  }
1133  }
1134 
1135  return 0;
1136 }
1137 
1138 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1139 {
1140  int cb_size = 1 << log2_cb_size;
1141  int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
1142 
1143  int min_pu_width = s->ps.sps->min_pu_width;
1144  int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
1145  int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
1146  int i, j;
1147 
1148  for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1149  for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1150  s->is_pcm[i + j * min_pu_width] = 2;
1151 }
1152 
1153 static int hls_transform_tree(HEVCContext *s, int x0, int y0,
1154  int xBase, int yBase, int cb_xBase, int cb_yBase,
1155  int log2_cb_size, int log2_trafo_size,
1156  int trafo_depth, int blk_idx,
1157  const int *base_cbf_cb, const int *base_cbf_cr)
1158 {
1159  HEVCLocalContext *lc = s->HEVClc;
1160  uint8_t split_transform_flag;
1161  int cbf_cb[2];
1162  int cbf_cr[2];
1163  int ret;
1164 
1165  cbf_cb[0] = base_cbf_cb[0];
1166  cbf_cb[1] = base_cbf_cb[1];
1167  cbf_cr[0] = base_cbf_cr[0];
1168  cbf_cr[1] = base_cbf_cr[1];
1169 
1170  if (lc->cu.intra_split_flag) {
1171  if (trafo_depth == 1) {
1172  lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1173  if (s->ps.sps->chroma_format_idc == 3) {
1174  lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1175  lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1176  } else {
1178  lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1179  }
1180  }
1181  } else {
1182  lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1184  lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1185  }
1186 
1187  if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
1188  log2_trafo_size > s->ps.sps->log2_min_tb_size &&
1189  trafo_depth < lc->cu.max_trafo_depth &&
1190  !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1191  split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1192  } else {
1193  int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
1194  lc->cu.pred_mode == MODE_INTER &&
1195  lc->cu.part_mode != PART_2Nx2N &&
1196  trafo_depth == 0;
1197 
1198  split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
1199  (lc->cu.intra_split_flag && trafo_depth == 0) ||
1200  inter_split;
1201  }
1202 
1203  if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1204  if (trafo_depth == 0 || cbf_cb[0]) {
1205  cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1206  if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1207  cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1208  }
1209  }
1210 
1211  if (trafo_depth == 0 || cbf_cr[0]) {
1212  cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1213  if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1214  cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1215  }
1216  }
1217  }
1218 
1219  if (split_transform_flag) {
1220  const int trafo_size_split = 1 << (log2_trafo_size - 1);
1221  const int x1 = x0 + trafo_size_split;
1222  const int y1 = y0 + trafo_size_split;
1223 
1224 #define SUBDIVIDE(x, y, idx) \
1225 do { \
1226  ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1227  log2_trafo_size - 1, trafo_depth + 1, idx, \
1228  cbf_cb, cbf_cr); \
1229  if (ret < 0) \
1230  return ret; \
1231 } while (0)
1232 
1233  SUBDIVIDE(x0, y0, 0);
1234  SUBDIVIDE(x1, y0, 1);
1235  SUBDIVIDE(x0, y1, 2);
1236  SUBDIVIDE(x1, y1, 3);
1237 
1238 #undef SUBDIVIDE
1239  } else {
1240  int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
1241  int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
1242  int min_tu_width = s->ps.sps->min_tb_width;
1243  int cbf_luma = 1;
1244 
1245  if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1246  cbf_cb[0] || cbf_cr[0] ||
1247  (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1248  cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1249  }
1250 
1251  ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1252  log2_cb_size, log2_trafo_size,
1253  blk_idx, cbf_luma, cbf_cb, cbf_cr);
1254  if (ret < 0)
1255  return ret;
1256  // TODO: store cbf_luma somewhere else
1257  if (cbf_luma) {
1258  int i, j;
1259  for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1260  for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1261  int x_tu = (x0 + j) >> log2_min_tu_size;
1262  int y_tu = (y0 + i) >> log2_min_tu_size;
1263  s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1264  }
1265  }
1267  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
1270  set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1271  }
1272  }
1273  return 0;
1274 }
1275 
1276 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1277 {
1278  HEVCLocalContext *lc = s->HEVClc;
1279  GetBitContext gb;
1280  int cb_size = 1 << log2_cb_size;
1281  int stride0 = s->frame->linesize[0];
1282  uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
1283  int stride1 = s->frame->linesize[1];
1284  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)];
1285  int stride2 = s->frame->linesize[2];
1286  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)];
1287 
1288  int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth +
1289  (((cb_size >> s->ps.sps->hshift[1]) * (cb_size >> s->ps.sps->vshift[1])) +
1290  ((cb_size >> s->ps.sps->hshift[2]) * (cb_size >> s->ps.sps->vshift[2]))) *
1291  s->ps.sps->pcm.bit_depth_chroma;
1292  const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1293  int ret;
1294 
1296  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
1297 
1298  ret = init_get_bits(&gb, pcm, length);
1299  if (ret < 0)
1300  return ret;
1301 
1302  s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, s->ps.sps->pcm.bit_depth);
1303  if (s->ps.sps->chroma_format_idc) {
1304  s->hevcdsp.put_pcm(dst1, stride1,
1305  cb_size >> s->ps.sps->hshift[1],
1306  cb_size >> s->ps.sps->vshift[1],
1307  &gb, s->ps.sps->pcm.bit_depth_chroma);
1308  s->hevcdsp.put_pcm(dst2, stride2,
1309  cb_size >> s->ps.sps->hshift[2],
1310  cb_size >> s->ps.sps->vshift[2],
1311  &gb, s->ps.sps->pcm.bit_depth_chroma);
1312  }
1313 
1314  return 0;
1315 }
1316 
1317 /**
1318  * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1319  *
1320  * @param s HEVC decoding context
1321  * @param dst target buffer for block data at block position
1322  * @param dststride stride of the dst buffer
1323  * @param ref reference picture buffer at origin (0, 0)
1324  * @param mv motion vector (relative to block position) to get pixel data from
1325  * @param x_off horizontal position of block from origin (0, 0)
1326  * @param y_off vertical position of block from origin (0, 0)
1327  * @param block_w width of block
1328  * @param block_h height of block
1329  * @param luma_weight weighting factor applied to the luma prediction
1330  * @param luma_offset additive offset applied to the luma prediction value
1331  */
1332 
1333 static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1334  AVFrame *ref, const Mv *mv, int x_off, int y_off,
1335  int block_w, int block_h, int luma_weight, int luma_offset)
1336 {
1337  HEVCLocalContext *lc = s->HEVClc;
1338  uint8_t *src = ref->data[0];
1339  ptrdiff_t srcstride = ref->linesize[0];
1340  int pic_width = s->ps.sps->width;
1341  int pic_height = s->ps.sps->height;
1342  int mx = mv->x & 3;
1343  int my = mv->y & 3;
1344  int weight_flag = (s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1346  int idx = ff_hevc_pel_weight[block_w];
1347 
1348  x_off += mv->x >> 2;
1349  y_off += mv->y >> 2;
1350  src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1351 
1352  if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1353  x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1354  y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1355  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1356  int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1357  int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1358 
1359  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1360  edge_emu_stride, srcstride,
1361  block_w + QPEL_EXTRA,
1362  block_h + QPEL_EXTRA,
1363  x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1364  pic_width, pic_height);
1365  src = lc->edge_emu_buffer + buf_offset;
1366  srcstride = edge_emu_stride;
1367  }
1368 
1369  if (!weight_flag)
1370  s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1371  block_h, mx, my, block_w);
1372  else
1373  s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1374  block_h, s->sh.luma_log2_weight_denom,
1375  luma_weight, luma_offset, mx, my, block_w);
1376 }
1377 
1378 /**
1379  * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1380  *
1381  * @param s HEVC decoding context
1382  * @param dst target buffer for block data at block position
1383  * @param dststride stride of the dst buffer
1384  * @param ref0 reference picture0 buffer at origin (0, 0)
1385  * @param mv0 motion vector0 (relative to block position) to get pixel data from
1386  * @param x_off horizontal position of block from origin (0, 0)
1387  * @param y_off vertical position of block from origin (0, 0)
1388  * @param block_w width of block
1389  * @param block_h height of block
1390  * @param ref1 reference picture1 buffer at origin (0, 0)
1391  * @param mv1 motion vector1 (relative to block position) to get pixel data from
1392  * @param current_mv current motion vector structure
1393  */
1394  static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1395  AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1396  int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
1397 {
1398  HEVCLocalContext *lc = s->HEVClc;
1399  ptrdiff_t src0stride = ref0->linesize[0];
1400  ptrdiff_t src1stride = ref1->linesize[0];
1401  int pic_width = s->ps.sps->width;
1402  int pic_height = s->ps.sps->height;
1403  int mx0 = mv0->x & 3;
1404  int my0 = mv0->y & 3;
1405  int mx1 = mv1->x & 3;
1406  int my1 = mv1->y & 3;
1407  int weight_flag = (s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1409  int x_off0 = x_off + (mv0->x >> 2);
1410  int y_off0 = y_off + (mv0->y >> 2);
1411  int x_off1 = x_off + (mv1->x >> 2);
1412  int y_off1 = y_off + (mv1->y >> 2);
1413  int idx = ff_hevc_pel_weight[block_w];
1414 
1415  uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1416  uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1417 
1418  if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1419  x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1420  y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1421  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1422  int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1423  int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1424 
1425  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1426  edge_emu_stride, src0stride,
1427  block_w + QPEL_EXTRA,
1428  block_h + QPEL_EXTRA,
1429  x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1430  pic_width, pic_height);
1431  src0 = lc->edge_emu_buffer + buf_offset;
1432  src0stride = edge_emu_stride;
1433  }
1434 
1435  if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1436  x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1437  y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1438  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1439  int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1440  int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1441 
1442  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1443  edge_emu_stride, src1stride,
1444  block_w + QPEL_EXTRA,
1445  block_h + QPEL_EXTRA,
1446  x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1447  pic_width, pic_height);
1448  src1 = lc->edge_emu_buffer2 + buf_offset;
1449  src1stride = edge_emu_stride;
1450  }
1451 
1452  s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1453  block_h, mx0, my0, block_w);
1454  if (!weight_flag)
1455  s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1456  block_h, mx1, my1, block_w);
1457  else
1458  s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1459  block_h, s->sh.luma_log2_weight_denom,
1460  s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1461  s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1462  s->sh.luma_offset_l0[current_mv->ref_idx[0]],
1463  s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1464  mx1, my1, block_w);
1465 
1466 }
1467 
1468 /**
1469  * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1470  *
1471  * @param s HEVC decoding context
1472  * @param dst1 target buffer for block data at block position (U plane)
1473  * @param dst2 target buffer for block data at block position (V plane)
1474  * @param dststride stride of the dst1 and dst2 buffers
1475  * @param ref reference picture buffer at origin (0, 0)
1476  * @param mv motion vector (relative to block position) to get pixel data from
1477  * @param x_off horizontal position of block from origin (0, 0)
1478  * @param y_off vertical position of block from origin (0, 0)
1479  * @param block_w width of block
1480  * @param block_h height of block
1481  * @param chroma_weight weighting factor applied to the chroma prediction
1482  * @param chroma_offset additive offset applied to the chroma prediction value
1483  */
1484 
1485 static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
1486  ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
1487  int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
1488 {
1489  HEVCLocalContext *lc = s->HEVClc;
1490  int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1491  int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1492  const Mv *mv = &current_mv->mv[reflist];
1493  int weight_flag = (s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1495  int idx = ff_hevc_pel_weight[block_w];
1496  int hshift = s->ps.sps->hshift[1];
1497  int vshift = s->ps.sps->vshift[1];
1498  intptr_t mx = av_mod_uintp2(mv->x, 2 + hshift);
1499  intptr_t my = av_mod_uintp2(mv->y, 2 + vshift);
1500  intptr_t _mx = mx << (1 - hshift);
1501  intptr_t _my = my << (1 - vshift);
1502 
1503  x_off += mv->x >> (2 + hshift);
1504  y_off += mv->y >> (2 + vshift);
1505  src0 += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1506 
1507  if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1508  x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1509  y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1510  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1511  int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
1512  int buf_offset0 = EPEL_EXTRA_BEFORE *
1513  (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1514  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1515  edge_emu_stride, srcstride,
1516  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1517  x_off - EPEL_EXTRA_BEFORE,
1518  y_off - EPEL_EXTRA_BEFORE,
1519  pic_width, pic_height);
1520 
1521  src0 = lc->edge_emu_buffer + buf_offset0;
1522  srcstride = edge_emu_stride;
1523  }
1524  if (!weight_flag)
1525  s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1526  block_h, _mx, _my, block_w);
1527  else
1528  s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1529  block_h, s->sh.chroma_log2_weight_denom,
1530  chroma_weight, chroma_offset, _mx, _my, block_w);
1531 }
1532 
1533 /**
1534  * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1535  *
1536  * @param s HEVC decoding context
1537  * @param dst target buffer for block data at block position
1538  * @param dststride stride of the dst buffer
1539  * @param ref0 reference picture0 buffer at origin (0, 0)
1540  * @param mv0 motion vector0 (relative to block position) to get pixel data from
1541  * @param x_off horizontal position of block from origin (0, 0)
1542  * @param y_off vertical position of block from origin (0, 0)
1543  * @param block_w width of block
1544  * @param block_h height of block
1545  * @param ref1 reference picture1 buffer at origin (0, 0)
1546  * @param mv1 motion vector1 (relative to block position) to get pixel data from
1547  * @param current_mv current motion vector structure
1548  * @param cidx chroma component(cb, cr)
1549  */
1550 static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
1551  int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
1552 {
1553  HEVCLocalContext *lc = s->HEVClc;
1554  uint8_t *src1 = ref0->data[cidx+1];
1555  uint8_t *src2 = ref1->data[cidx+1];
1556  ptrdiff_t src1stride = ref0->linesize[cidx+1];
1557  ptrdiff_t src2stride = ref1->linesize[cidx+1];
1558  int weight_flag = (s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1560  int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1561  int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1562  Mv *mv0 = &current_mv->mv[0];
1563  Mv *mv1 = &current_mv->mv[1];
1564  int hshift = s->ps.sps->hshift[1];
1565  int vshift = s->ps.sps->vshift[1];
1566 
1567  intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
1568  intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
1569  intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
1570  intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
1571  intptr_t _mx0 = mx0 << (1 - hshift);
1572  intptr_t _my0 = my0 << (1 - vshift);
1573  intptr_t _mx1 = mx1 << (1 - hshift);
1574  intptr_t _my1 = my1 << (1 - vshift);
1575 
1576  int x_off0 = x_off + (mv0->x >> (2 + hshift));
1577  int y_off0 = y_off + (mv0->y >> (2 + vshift));
1578  int x_off1 = x_off + (mv1->x >> (2 + hshift));
1579  int y_off1 = y_off + (mv1->y >> (2 + vshift));
1580  int idx = ff_hevc_pel_weight[block_w];
1581  src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1582  src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1583 
1584  if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
1585  x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1586  y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1587  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1588  int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
1589  int buf_offset1 = EPEL_EXTRA_BEFORE *
1590  (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1591 
1592  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1593  edge_emu_stride, src1stride,
1594  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1595  x_off0 - EPEL_EXTRA_BEFORE,
1596  y_off0 - EPEL_EXTRA_BEFORE,
1597  pic_width, pic_height);
1598 
1599  src1 = lc->edge_emu_buffer + buf_offset1;
1600  src1stride = edge_emu_stride;
1601  }
1602 
1603  if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
1604  x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1605  y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1606  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1607  int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
1608  int buf_offset1 = EPEL_EXTRA_BEFORE *
1609  (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1610 
1611  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
1612  edge_emu_stride, src2stride,
1613  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1614  x_off1 - EPEL_EXTRA_BEFORE,
1615  y_off1 - EPEL_EXTRA_BEFORE,
1616  pic_width, pic_height);
1617 
1618  src2 = lc->edge_emu_buffer2 + buf_offset1;
1619  src2stride = edge_emu_stride;
1620  }
1621 
1622  s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
1623  block_h, _mx0, _my0, block_w);
1624  if (!weight_flag)
1625  s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1626  src2, src2stride, lc->tmp,
1627  block_h, _mx1, _my1, block_w);
1628  else
1629  s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1630  src2, src2stride, lc->tmp,
1631  block_h,
1633  s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
1634  s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
1635  s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
1636  s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
1637  _mx1, _my1, block_w);
1638 }
1639 
1641  const Mv *mv, int y0, int height)
1642 {
1643  int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1644 
1645  if (s->threads_type == FF_THREAD_FRAME )
1646  ff_thread_await_progress(&ref->tf, y, 0);
1647 }
1648 
1649 static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
1650  int nPbH, int log2_cb_size, int part_idx,
1651  int merge_idx, MvField *mv)
1652 {
1653  HEVCLocalContext *lc = s->HEVClc;
1654  enum InterPredIdc inter_pred_idc = PRED_L0;
1655  int mvp_flag;
1656 
1657  ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1658  mv->pred_flag = 0;
1659  if (s->sh.slice_type == B_SLICE)
1660  inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1661 
1662  if (inter_pred_idc != PRED_L1) {
1663  if (s->sh.nb_refs[L0])
1664  mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1665 
1666  mv->pred_flag = PF_L0;
1667  ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1668  mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1669  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1670  part_idx, merge_idx, mv, mvp_flag, 0);
1671  mv->mv[0].x += lc->pu.mvd.x;
1672  mv->mv[0].y += lc->pu.mvd.y;
1673  }
1674 
1675  if (inter_pred_idc != PRED_L0) {
1676  if (s->sh.nb_refs[L1])
1677  mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1678 
1679  if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1680  AV_ZERO32(&lc->pu.mvd);
1681  } else {
1682  ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1683  }
1684 
1685  mv->pred_flag += PF_L1;
1686  mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1687  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1688  part_idx, merge_idx, mv, mvp_flag, 1);
1689  mv->mv[1].x += lc->pu.mvd.x;
1690  mv->mv[1].y += lc->pu.mvd.y;
1691  }
1692 }
1693 
1694 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1695  int nPbW, int nPbH,
1696  int log2_cb_size, int partIdx, int idx)
1697 {
1698 #define POS(c_idx, x, y) \
1699  &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1700  (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1701  HEVCLocalContext *lc = s->HEVClc;
1702  int merge_idx = 0;
1703  struct MvField current_mv = {{{ 0 }}};
1704 
1705  int min_pu_width = s->ps.sps->min_pu_width;
1706 
1707  MvField *tab_mvf = s->ref->tab_mvf;
1708  RefPicList *refPicList = s->ref->refPicList;
1709  HEVCFrame *ref0 = NULL, *ref1 = NULL;
1710  uint8_t *dst0 = POS(0, x0, y0);
1711  uint8_t *dst1 = POS(1, x0, y0);
1712  uint8_t *dst2 = POS(2, x0, y0);
1713  int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1714  int min_cb_width = s->ps.sps->min_cb_width;
1715  int x_cb = x0 >> log2_min_cb_size;
1716  int y_cb = y0 >> log2_min_cb_size;
1717  int x_pu, y_pu;
1718  int i, j;
1719 
1720  int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1721 
1722  if (!skip_flag)
1724 
1725  if (skip_flag || lc->pu.merge_flag) {
1726  if (s->sh.max_num_merge_cand > 1)
1727  merge_idx = ff_hevc_merge_idx_decode(s);
1728  else
1729  merge_idx = 0;
1730 
1731  ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1732  partIdx, merge_idx, &current_mv);
1733  } else {
1734  hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1735  partIdx, merge_idx, &current_mv);
1736  }
1737 
1738  x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1739  y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1740 
1741  for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1742  for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1743  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1744 
1745  if (current_mv.pred_flag & PF_L0) {
1746  ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1747  if (!ref0)
1748  return;
1749  hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
1750  }
1751  if (current_mv.pred_flag & PF_L1) {
1752  ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1753  if (!ref1)
1754  return;
1755  hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
1756  }
1757 
1758  if (current_mv.pred_flag == PF_L0) {
1759  int x0_c = x0 >> s->ps.sps->hshift[1];
1760  int y0_c = y0 >> s->ps.sps->vshift[1];
1761  int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1762  int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1763 
1764  luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
1765  &current_mv.mv[0], x0, y0, nPbW, nPbH,
1766  s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1767  s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
1768 
1769  if (s->ps.sps->chroma_format_idc) {
1770  chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
1771  0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1772  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
1773  chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
1774  0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1775  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
1776  }
1777  } else if (current_mv.pred_flag == PF_L1) {
1778  int x0_c = x0 >> s->ps.sps->hshift[1];
1779  int y0_c = y0 >> s->ps.sps->vshift[1];
1780  int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1781  int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1782 
1783  luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
1784  &current_mv.mv[1], x0, y0, nPbW, nPbH,
1785  s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1786  s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
1787 
1788  if (s->ps.sps->chroma_format_idc) {
1789  chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
1790  1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1791  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
1792 
1793  chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
1794  1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1795  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
1796  }
1797  } else if (current_mv.pred_flag == PF_BI) {
1798  int x0_c = x0 >> s->ps.sps->hshift[1];
1799  int y0_c = y0 >> s->ps.sps->vshift[1];
1800  int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1801  int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1802 
1803  luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
1804  &current_mv.mv[0], x0, y0, nPbW, nPbH,
1805  ref1->frame, &current_mv.mv[1], &current_mv);
1806 
1807  if (s->ps.sps->chroma_format_idc) {
1808  chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
1809  x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
1810 
1811  chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
1812  x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
1813  }
1814  }
1815 }
1816 
1817 /**
1818  * 8.4.1
1819  */
1820 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1821  int prev_intra_luma_pred_flag)
1822 {
1823  HEVCLocalContext *lc = s->HEVClc;
1824  int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1825  int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1826  int min_pu_width = s->ps.sps->min_pu_width;
1827  int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
1828  int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
1829  int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
1830 
1831  int cand_up = (lc->ctb_up_flag || y0b) ?
1832  s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1833  int cand_left = (lc->ctb_left_flag || x0b) ?
1834  s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1835 
1836  int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
1837 
1838  MvField *tab_mvf = s->ref->tab_mvf;
1839  int intra_pred_mode;
1840  int candidate[3];
1841  int i, j;
1842 
1843  // intra_pred_mode prediction does not cross vertical CTB boundaries
1844  if ((y0 - 1) < y_ctb)
1845  cand_up = INTRA_DC;
1846 
1847  if (cand_left == cand_up) {
1848  if (cand_left < 2) {
1849  candidate[0] = INTRA_PLANAR;
1850  candidate[1] = INTRA_DC;
1851  candidate[2] = INTRA_ANGULAR_26;
1852  } else {
1853  candidate[0] = cand_left;
1854  candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1855  candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1856  }
1857  } else {
1858  candidate[0] = cand_left;
1859  candidate[1] = cand_up;
1860  if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1861  candidate[2] = INTRA_PLANAR;
1862  } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1863  candidate[2] = INTRA_DC;
1864  } else {
1865  candidate[2] = INTRA_ANGULAR_26;
1866  }
1867  }
1868 
1869  if (prev_intra_luma_pred_flag) {
1870  intra_pred_mode = candidate[lc->pu.mpm_idx];
1871  } else {
1872  if (candidate[0] > candidate[1])
1873  FFSWAP(uint8_t, candidate[0], candidate[1]);
1874  if (candidate[0] > candidate[2])
1875  FFSWAP(uint8_t, candidate[0], candidate[2]);
1876  if (candidate[1] > candidate[2])
1877  FFSWAP(uint8_t, candidate[1], candidate[2]);
1878 
1879  intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1880  for (i = 0; i < 3; i++)
1881  if (intra_pred_mode >= candidate[i])
1882  intra_pred_mode++;
1883  }
1884 
1885  /* write the intra prediction units into the mv array */
1886  if (!size_in_pus)
1887  size_in_pus = 1;
1888  for (i = 0; i < size_in_pus; i++) {
1889  memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1890  intra_pred_mode, size_in_pus);
1891 
1892  for (j = 0; j < size_in_pus; j++) {
1893  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
1894  }
1895  }
1896 
1897  return intra_pred_mode;
1898 }
1899 
1900 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1901  int log2_cb_size, int ct_depth)
1902 {
1903  int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
1904  int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
1905  int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
1906  int y;
1907 
1908  for (y = 0; y < length; y++)
1909  memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
1910  ct_depth, length);
1911 }
1912 
1913 static const uint8_t tab_mode_idx[] = {
1914  0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
1915  21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
1916 
1917 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1918  int log2_cb_size)
1919 {
1920  HEVCLocalContext *lc = s->HEVClc;
1921  static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1922  uint8_t prev_intra_luma_pred_flag[4];
1923  int split = lc->cu.part_mode == PART_NxN;
1924  int pb_size = (1 << log2_cb_size) >> split;
1925  int side = split + 1;
1926  int chroma_mode;
1927  int i, j;
1928 
1929  for (i = 0; i < side; i++)
1930  for (j = 0; j < side; j++)
1931  prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1932 
1933  for (i = 0; i < side; i++) {
1934  for (j = 0; j < side; j++) {
1935  if (prev_intra_luma_pred_flag[2 * i + j])
1937  else
1939 
1940  lc->pu.intra_pred_mode[2 * i + j] =
1941  luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
1942  prev_intra_luma_pred_flag[2 * i + j]);
1943  }
1944  }
1945 
1946  if (s->ps.sps->chroma_format_idc == 3) {
1947  for (i = 0; i < side; i++) {
1948  for (j = 0; j < side; j++) {
1949  lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1950  if (chroma_mode != 4) {
1951  if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
1952  lc->pu.intra_pred_mode_c[2 * i + j] = 34;
1953  else
1954  lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
1955  } else {
1956  lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
1957  }
1958  }
1959  }
1960  } else if (s->ps.sps->chroma_format_idc == 2) {
1961  int mode_idx;
1962  lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1963  if (chroma_mode != 4) {
1964  if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
1965  mode_idx = 34;
1966  else
1967  mode_idx = intra_chroma_table[chroma_mode];
1968  } else {
1969  mode_idx = lc->pu.intra_pred_mode[0];
1970  }
1971  lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
1972  } else if (s->ps.sps->chroma_format_idc != 0) {
1973  chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1974  if (chroma_mode != 4) {
1975  if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
1976  lc->pu.intra_pred_mode_c[0] = 34;
1977  else
1978  lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
1979  } else {
1980  lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
1981  }
1982  }
1983 }
1984 
1986  int x0, int y0,
1987  int log2_cb_size)
1988 {
1989  HEVCLocalContext *lc = s->HEVClc;
1990  int pb_size = 1 << log2_cb_size;
1991  int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
1992  int min_pu_width = s->ps.sps->min_pu_width;
1993  MvField *tab_mvf = s->ref->tab_mvf;
1994  int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1995  int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1996  int j, k;
1997 
1998  if (size_in_pus == 0)
1999  size_in_pus = 1;
2000  for (j = 0; j < size_in_pus; j++)
2001  memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2002  if (lc->cu.pred_mode == MODE_INTRA)
2003  for (j = 0; j < size_in_pus; j++)
2004  for (k = 0; k < size_in_pus; k++)
2005  tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2006 }
2007 
2008 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2009 {
2010  int cb_size = 1 << log2_cb_size;
2011  HEVCLocalContext *lc = s->HEVClc;
2012  int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2013  int length = cb_size >> log2_min_cb_size;
2014  int min_cb_width = s->ps.sps->min_cb_width;
2015  int x_cb = x0 >> log2_min_cb_size;
2016  int y_cb = y0 >> log2_min_cb_size;
2017  int idx = log2_cb_size - 2;
2018  int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2019  int x, y, ret;
2020 
2021  lc->cu.x = x0;
2022  lc->cu.y = y0;
2023  lc->cu.pred_mode = MODE_INTRA;
2024  lc->cu.part_mode = PART_2Nx2N;
2025  lc->cu.intra_split_flag = 0;
2026 
2027  SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2028  for (x = 0; x < 4; x++)
2029  lc->pu.intra_pred_mode[x] = 1;
2032  if (lc->cu.cu_transquant_bypass_flag)
2033  set_deblocking_bypass(s, x0, y0, log2_cb_size);
2034  } else
2035  lc->cu.cu_transquant_bypass_flag = 0;
2036 
2037  if (s->sh.slice_type != I_SLICE) {
2038  uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2039 
2040  x = y_cb * min_cb_width + x_cb;
2041  for (y = 0; y < length; y++) {
2042  memset(&s->skip_flag[x], skip_flag, length);
2043  x += min_cb_width;
2044  }
2045  lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2046  } else {
2047  x = y_cb * min_cb_width + x_cb;
2048  for (y = 0; y < length; y++) {
2049  memset(&s->skip_flag[x], 0, length);
2050  x += min_cb_width;
2051  }
2052  }
2053 
2054  if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2055  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2056  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2057 
2059  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2060  } else {
2061  int pcm_flag = 0;
2062 
2063  if (s->sh.slice_type != I_SLICE)
2065  if (lc->cu.pred_mode != MODE_INTRA ||
2066  log2_cb_size == s->ps.sps->log2_min_cb_size) {
2067  lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
2068  lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2069  lc->cu.pred_mode == MODE_INTRA;
2070  }
2071 
2072  if (lc->cu.pred_mode == MODE_INTRA) {
2073  if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2074  log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2075  log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2076  pcm_flag = ff_hevc_pcm_flag_decode(s);
2077  }
2078  if (pcm_flag) {
2079  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2080  ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2082  set_deblocking_bypass(s, x0, y0, log2_cb_size);
2083 
2084  if (ret < 0)
2085  return ret;
2086  } else {
2087  intra_prediction_unit(s, x0, y0, log2_cb_size);
2088  }
2089  } else {
2090  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2091  switch (lc->cu.part_mode) {
2092  case PART_2Nx2N:
2093  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2094  break;
2095  case PART_2NxN:
2096  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2097  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2098  break;
2099  case PART_Nx2N:
2100  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2101  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2102  break;
2103  case PART_2NxnU:
2104  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2105  hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2106  break;
2107  case PART_2NxnD:
2108  hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2109  hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2110  break;
2111  case PART_nLx2N:
2112  hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2113  hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2114  break;
2115  case PART_nRx2N:
2116  hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2117  hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2118  break;
2119  case PART_NxN:
2120  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2121  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2122  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2123  hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2124  break;
2125  }
2126  }
2127 
2128  if (!pcm_flag) {
2129  int rqt_root_cbf = 1;
2130 
2131  if (lc->cu.pred_mode != MODE_INTRA &&
2132  !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2133  rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2134  }
2135  if (rqt_root_cbf) {
2136  const static int cbf[2] = { 0 };
2137  lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2140  ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2141  log2_cb_size,
2142  log2_cb_size, 0, 0, cbf, cbf);
2143  if (ret < 0)
2144  return ret;
2145  } else {
2147  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2148  }
2149  }
2150  }
2151 
2153  ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
2154 
2155  x = y_cb * min_cb_width + x_cb;
2156  for (y = 0; y < length; y++) {
2157  memset(&s->qp_y_tab[x], lc->qp_y, length);
2158  x += min_cb_width;
2159  }
2160 
2161  if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2162  ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2163  lc->qPy_pred = lc->qp_y;
2164  }
2165 
2166  set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
2167 
2168  return 0;
2169 }
2170 
2171 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2172  int log2_cb_size, int cb_depth)
2173 {
2174  HEVCLocalContext *lc = s->HEVClc;
2175  const int cb_size = 1 << log2_cb_size;
2176  int ret;
2177  int split_cu;
2178 
2179  lc->ct_depth = cb_depth;
2180  if (x0 + cb_size <= s->ps.sps->width &&
2181  y0 + cb_size <= s->ps.sps->height &&
2182  log2_cb_size > s->ps.sps->log2_min_cb_size) {
2183  split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2184  } else {
2185  split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2186  }
2187  if (s->ps.pps->cu_qp_delta_enabled_flag &&
2188  log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2189  lc->tu.is_cu_qp_delta_coded = 0;
2190  lc->tu.cu_qp_delta = 0;
2191  }
2192 
2194  log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
2196  }
2197 
2198  if (split_cu) {
2199  int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2200  const int cb_size_split = cb_size >> 1;
2201  const int x1 = x0 + cb_size_split;
2202  const int y1 = y0 + cb_size_split;
2203 
2204  int more_data = 0;
2205 
2206  more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
2207  if (more_data < 0)
2208  return more_data;
2209 
2210  if (more_data && x1 < s->ps.sps->width) {
2211  more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
2212  if (more_data < 0)
2213  return more_data;
2214  }
2215  if (more_data && y1 < s->ps.sps->height) {
2216  more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
2217  if (more_data < 0)
2218  return more_data;
2219  }
2220  if (more_data && x1 < s->ps.sps->width &&
2221  y1 < s->ps.sps->height) {
2222  more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
2223  if (more_data < 0)
2224  return more_data;
2225  }
2226 
2227  if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2228  ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2229  lc->qPy_pred = lc->qp_y;
2230 
2231  if (more_data)
2232  return ((x1 + cb_size_split) < s->ps.sps->width ||
2233  (y1 + cb_size_split) < s->ps.sps->height);
2234  else
2235  return 0;
2236  } else {
2237  ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2238  if (ret < 0)
2239  return ret;
2240  if ((!((x0 + cb_size) %
2241  (1 << (s->ps.sps->log2_ctb_size))) ||
2242  (x0 + cb_size >= s->ps.sps->width)) &&
2243  (!((y0 + cb_size) %
2244  (1 << (s->ps.sps->log2_ctb_size))) ||
2245  (y0 + cb_size >= s->ps.sps->height))) {
2246  int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
2247  return !end_of_slice_flag;
2248  } else {
2249  return 1;
2250  }
2251  }
2252 
2253  return 0;
2254 }
2255 
2256 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2257  int ctb_addr_ts)
2258 {
2259  HEVCLocalContext *lc = s->HEVClc;
2260  int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2261  int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2262  int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2263 
2264  s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2265 
2267  if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2268  lc->first_qp_group = 1;
2269  lc->end_of_tiles_x = s->ps.sps->width;
2270  } else if (s->ps.pps->tiles_enabled_flag) {
2271  if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2272  int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2273  lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2274  lc->first_qp_group = 1;
2275  }
2276  } else {
2277  lc->end_of_tiles_x = s->ps.sps->width;
2278  }
2279 
2280  lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2281 
2282  lc->boundary_flags = 0;
2283  if (s->ps.pps->tiles_enabled_flag) {
2284  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]])
2286  if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2288  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]])
2290  if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2292  } else {
2293  if (ctb_addr_in_slice <= 0)
2295  if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2297  }
2298 
2299  lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2300  lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2301  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]]));
2302  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]]));
2303 }
2304 
2305 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
2306 {
2307  HEVCContext *s = avctxt->priv_data;
2308  int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2309  int more_data = 1;
2310  int x_ctb = 0;
2311  int y_ctb = 0;
2312  int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2313 
2314  if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
2315  av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
2316  return AVERROR_INVALIDDATA;
2317  }
2318 
2320  int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2321  if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2322  av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2323  return AVERROR_INVALIDDATA;
2324  }
2325  }
2326 
2327  while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2328  int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2329 
2330  x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2331  y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2332  hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2333 
2334  ff_hevc_cabac_init(s, ctb_addr_ts);
2335 
2336  hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2337 
2338  s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2339  s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2341 
2342  more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2343  if (more_data < 0) {
2344  s->tab_slice_address[ctb_addr_rs] = -1;
2345  return more_data;
2346  }
2347 
2348 
2349  ctb_addr_ts++;
2350  ff_hevc_save_states(s, ctb_addr_ts);
2351  ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2352  }
2353 
2354  if (x_ctb + ctb_size >= s->ps.sps->width &&
2355  y_ctb + ctb_size >= s->ps.sps->height)
2356  ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2357 
2358  return ctb_addr_ts;
2359 }
2360 
2362 {
2363  int arg[2];
2364  int ret[2];
2365 
2366  arg[0] = 0;
2367  arg[1] = 1;
2368 
2369  s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
2370  return ret[0];
2371 }
2372 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
2373 {
2374  HEVCContext *s1 = avctxt->priv_data, *s;
2375  HEVCLocalContext *lc;
2376  int ctb_size = 1<< s1->ps.sps->log2_ctb_size;
2377  int more_data = 1;
2378  int *ctb_row_p = input_ctb_row;
2379  int ctb_row = ctb_row_p[job];
2380  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);
2381  int ctb_addr_ts = s1->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2382  int thread = ctb_row % s1->threads_number;
2383  int ret;
2384 
2385  s = s1->sList[self_id];
2386  lc = s->HEVClc;
2387 
2388  if(ctb_row) {
2389  ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2390 
2391  if (ret < 0)
2392  return ret;
2393  ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2394  }
2395 
2396  while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2397  int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2398  int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2399 
2400  hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2401 
2402  ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2403 
2404  if (avpriv_atomic_int_get(&s1->wpp_err)){
2405  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2406  return 0;
2407  }
2408 
2409  ff_hevc_cabac_init(s, ctb_addr_ts);
2410  hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2411  more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2412 
2413  if (more_data < 0) {
2414  s->tab_slice_address[ctb_addr_rs] = -1;
2415  return more_data;
2416  }
2417 
2418  ctb_addr_ts++;
2419 
2420  ff_hevc_save_states(s, ctb_addr_ts);
2421  ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2422  ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2423 
2424  if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2425  avpriv_atomic_int_set(&s1->wpp_err, 1);
2426  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2427  return 0;
2428  }
2429 
2430  if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
2431  ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2432  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2433  return ctb_addr_ts;
2434  }
2435  ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2436  x_ctb+=ctb_size;
2437 
2438  if(x_ctb >= s->ps.sps->width) {
2439  break;
2440  }
2441  }
2442  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2443 
2444  return 0;
2445 }
2446 
2447 static int hls_slice_data_wpp(HEVCContext *s, const HEVCNAL *nal)
2448 {
2449  const uint8_t *data = nal->data;
2450  int length = nal->size;
2451  HEVCLocalContext *lc = s->HEVClc;
2452  int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2453  int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2454  int64_t offset;
2455  int64_t startheader, cmpt = 0;
2456  int i, j, res = 0;
2457 
2458  if (!ret || !arg) {
2459  av_free(ret);
2460  av_free(arg);
2461  return AVERROR(ENOMEM);
2462  }
2463 
2465  av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2467  s->ps.sps->ctb_width, s->ps.sps->ctb_height
2468  );
2469  res = AVERROR_INVALIDDATA;
2470  goto error;
2471  }
2472 
2474 
2475  if (!s->sList[1]) {
2476  for (i = 1; i < s->threads_number; i++) {
2477  s->sList[i] = av_malloc(sizeof(HEVCContext));
2478  memcpy(s->sList[i], s, sizeof(HEVCContext));
2479  s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
2480  s->sList[i]->HEVClc = s->HEVClcList[i];
2481  }
2482  }
2483 
2484  offset = (lc->gb.index >> 3);
2485 
2486  for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2487  if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2488  startheader--;
2489  cmpt++;
2490  }
2491  }
2492 
2493  for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2494  offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2495  for (j = 0, cmpt = 0, startheader = offset
2496  + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2497  if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2498  startheader--;
2499  cmpt++;
2500  }
2501  }
2502  s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2503  s->sh.offset[i - 1] = offset;
2504 
2505  }
2506  if (s->sh.num_entry_point_offsets != 0) {
2507  offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2508  if (length < offset) {
2509  av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2510  res = AVERROR_INVALIDDATA;
2511  goto error;
2512  }
2513  s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2514  s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2515 
2516  }
2517  s->data = data;
2518 
2519  for (i = 1; i < s->threads_number; i++) {
2520  s->sList[i]->HEVClc->first_qp_group = 1;
2521  s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
2522  memcpy(s->sList[i], s, sizeof(HEVCContext));
2523  s->sList[i]->HEVClc = s->HEVClcList[i];
2524  }
2525 
2527  ff_reset_entries(s->avctx);
2528 
2529  for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
2530  arg[i] = i;
2531  ret[i] = 0;
2532  }
2533 
2535  s->avctx->execute2(s->avctx, (void *) hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
2536 
2537  for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2538  res += ret[i];
2539 error:
2540  av_free(ret);
2541  av_free(arg);
2542  return res;
2543 }
2544 
2546 {
2547  AVFrame *out = s->ref->frame;
2548 
2549  if (s->sei_frame_packing_present &&
2552  s->content_interpretation_type > 0 &&
2553  s->content_interpretation_type < 3) {
2555  if (!stereo)
2556  return AVERROR(ENOMEM);
2557 
2558  switch (s->frame_packing_arrangement_type) {
2559  case 3:
2560  if (s->quincunx_subsampling)
2562  else
2563  stereo->type = AV_STEREO3D_SIDEBYSIDE;
2564  break;
2565  case 4:
2566  stereo->type = AV_STEREO3D_TOPBOTTOM;
2567  break;
2568  case 5:
2569  stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2570  break;
2571  }
2572 
2573  if (s->content_interpretation_type == 2)
2574  stereo->flags = AV_STEREO3D_FLAG_INVERT;
2575  }
2576 
2578  (s->sei_anticlockwise_rotation || s->sei_hflip || s->sei_vflip)) {
2579  double angle = s->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
2580  AVFrameSideData *rotation = av_frame_new_side_data(out,
2582  sizeof(int32_t) * 9);
2583  if (!rotation)
2584  return AVERROR(ENOMEM);
2585 
2586  av_display_rotation_set((int32_t *)rotation->data, angle);
2587  av_display_matrix_flip((int32_t *)rotation->data,
2588  s->sei_hflip, s->sei_vflip);
2589  }
2590 
2591  return 0;
2592 }
2593 
2595 {
2596  HEVCLocalContext *lc = s->HEVClc;
2597  int pic_size_in_ctb = ((s->ps.sps->width >> s->ps.sps->log2_min_cb_size) + 1) *
2598  ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
2599  int ret;
2600 
2601  memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
2602  memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
2603  memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2604  memset(s->is_pcm, 0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
2605  memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2606 
2607  s->is_decoded = 0;
2608  s->first_nal_type = s->nal_unit_type;
2609 
2610  if (s->ps.pps->tiles_enabled_flag)
2611  lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2612 
2613  ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
2614  if (ret < 0)
2615  goto fail;
2616 
2617  ret = ff_hevc_frame_rps(s);
2618  if (ret < 0) {
2619  av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2620  goto fail;
2621  }
2622 
2623  s->ref->frame->key_frame = IS_IRAP(s);
2624 
2625  ret = set_side_data(s);
2626  if (ret < 0)
2627  goto fail;
2628 
2629  s->frame->pict_type = 3 - s->sh.slice_type;
2630 
2631  if (!IS_IRAP(s))
2632  ff_hevc_bump_frame(s);
2633 
2635  ret = ff_hevc_output_frame(s, s->output_frame, 0);
2636  if (ret < 0)
2637  goto fail;
2638 
2639  if (!s->avctx->hwaccel)
2641 
2642  return 0;
2643 
2644 fail:
2645  if (s->ref)
2646  ff_hevc_unref_frame(s, s->ref, ~0);
2647  s->ref = NULL;
2648  return ret;
2649 }
2650 
2651 static int decode_nal_unit(HEVCContext *s, const HEVCNAL *nal)
2652 {
2653  HEVCLocalContext *lc = s->HEVClc;
2654  GetBitContext *gb = &lc->gb;
2655  int ctb_addr_ts, ret;
2656 
2657  *gb = nal->gb;
2658  s->nal_unit_type = nal->type;
2659  s->temporal_id = nal->temporal_id;
2660 
2661  switch (s->nal_unit_type) {
2662  case NAL_VPS:
2663  ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2664  if (ret < 0)
2665  goto fail;
2666  break;
2667  case NAL_SPS:
2668  ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
2669  s->apply_defdispwin);
2670  if (ret < 0)
2671  goto fail;
2672  break;
2673  case NAL_PPS:
2674  ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
2675  if (ret < 0)
2676  goto fail;
2677  break;
2678  case NAL_SEI_PREFIX:
2679  case NAL_SEI_SUFFIX:
2680  ret = ff_hevc_decode_nal_sei(s);
2681  if (ret < 0)
2682  goto fail;
2683  break;
2684  case NAL_TRAIL_R:
2685  case NAL_TRAIL_N:
2686  case NAL_TSA_N:
2687  case NAL_TSA_R:
2688  case NAL_STSA_N:
2689  case NAL_STSA_R:
2690  case NAL_BLA_W_LP:
2691  case NAL_BLA_W_RADL:
2692  case NAL_BLA_N_LP:
2693  case NAL_IDR_W_RADL:
2694  case NAL_IDR_N_LP:
2695  case NAL_CRA_NUT:
2696  case NAL_RADL_N:
2697  case NAL_RADL_R:
2698  case NAL_RASL_N:
2699  case NAL_RASL_R:
2700  ret = hls_slice_header(s);
2701  if (ret < 0)
2702  return ret;
2703  if (ret == 1) {
2704  ret = AVERROR_INVALIDDATA;
2705  goto fail;
2706  }
2707 
2708 
2709  if (s->max_ra == INT_MAX) {
2710  if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
2711  s->max_ra = s->poc;
2712  } else {
2713  if (IS_IDR(s))
2714  s->max_ra = INT_MIN;
2715  }
2716  }
2717 
2718  if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
2719  s->poc <= s->max_ra) {
2720  s->is_decoded = 0;
2721  break;
2722  } else {
2723  if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
2724  s->max_ra = INT_MIN;
2725  }
2726 
2727  if (s->sh.first_slice_in_pic_flag) {
2728  ret = hevc_frame_start(s);
2729  if (ret < 0)
2730  return ret;
2731  } else if (!s->ref) {
2732  av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2733  goto fail;
2734  }
2735 
2736  if (s->nal_unit_type != s->first_nal_type) {
2738  "Non-matching NAL types of the VCL NALUs: %d %d\n",
2739  s->first_nal_type, s->nal_unit_type);
2740  return AVERROR_INVALIDDATA;
2741  }
2742 
2743  if (!s->sh.dependent_slice_segment_flag &&
2744  s->sh.slice_type != I_SLICE) {
2745  ret = ff_hevc_slice_rpl(s);
2746  if (ret < 0) {
2748  "Error constructing the reference lists for the current slice.\n");
2749  goto fail;
2750  }
2751  }
2752 
2753  if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
2754  ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
2755  if (ret < 0)
2756  goto fail;
2757  }
2758 
2759  if (s->avctx->hwaccel) {
2760  ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
2761  if (ret < 0)
2762  goto fail;
2763  } else {
2764  if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
2765  ctb_addr_ts = hls_slice_data_wpp(s, nal);
2766  else
2767  ctb_addr_ts = hls_slice_data(s);
2768  if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
2769  s->is_decoded = 1;
2770  }
2771 
2772  if (ctb_addr_ts < 0) {
2773  ret = ctb_addr_ts;
2774  goto fail;
2775  }
2776  }
2777  break;
2778  case NAL_EOS_NUT:
2779  case NAL_EOB_NUT:
2780  s->seq_decode = (s->seq_decode + 1) & 0xff;
2781  s->max_ra = INT_MAX;
2782  break;
2783  case NAL_AUD:
2784  case NAL_FD_NUT:
2785  break;
2786  default:
2787  av_log(s->avctx, AV_LOG_INFO,
2788  "Skipping NAL unit %d\n", s->nal_unit_type);
2789  }
2790 
2791  return 0;
2792 fail:
2794  return ret;
2795  return 0;
2796 }
2797 
2798 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2799 {
2800  int i, ret = 0;
2801 
2802  s->ref = NULL;
2803  s->last_eos = s->eos;
2804  s->eos = 0;
2805 
2806  /* split the input packet into NAL units, so we know the upper bound on the
2807  * number of slices in the frame */
2808  ret = ff_hevc_split_packet(s, &s->pkt, buf, length, s->avctx, s->is_nalff,
2809  s->nal_length_size);
2810  if (ret < 0) {
2812  "Error splitting the input into NAL units.\n");
2813  return ret;
2814  }
2815 
2816  for (i = 0; i < s->pkt.nb_nals; i++) {
2817  if (s->pkt.nals[i].type == NAL_EOB_NUT ||
2818  s->pkt.nals[i].type == NAL_EOS_NUT)
2819  s->eos = 1;
2820  }
2821 
2822  /* decode the NAL units */
2823  for (i = 0; i < s->pkt.nb_nals; i++) {
2824  ret = decode_nal_unit(s, &s->pkt.nals[i]);
2825  if (ret < 0) {
2827  "Error parsing NAL unit #%d.\n", i);
2828  goto fail;
2829  }
2830  }
2831 
2832 fail:
2833  if (s->ref && s->threads_type == FF_THREAD_FRAME)
2834  ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2835 
2836  return ret;
2837 }
2838 
2839 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
2840 {
2841  int i;
2842  for (i = 0; i < 16; i++)
2843  av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2844 }
2845 
2847 {
2848  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
2849  int pixel_shift;
2850  int i, j;
2851 
2852  if (!desc)
2853  return AVERROR(EINVAL);
2854 
2855  pixel_shift = desc->comp[0].depth_minus1 > 7;
2856 
2857  av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
2858  s->poc);
2859 
2860  /* the checksums are LE, so we have to byteswap for >8bpp formats
2861  * on BE arches */
2862 #if HAVE_BIGENDIAN
2863  if (pixel_shift && !s->checksum_buf) {
2865  FFMAX3(frame->linesize[0], frame->linesize[1],
2866  frame->linesize[2]));
2867  if (!s->checksum_buf)
2868  return AVERROR(ENOMEM);
2869  }
2870 #endif
2871 
2872  for (i = 0; frame->data[i]; i++) {
2873  int width = s->avctx->coded_width;
2874  int height = s->avctx->coded_height;
2875  int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
2876  int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
2877  uint8_t md5[16];
2878 
2879  av_md5_init(s->md5_ctx);
2880  for (j = 0; j < h; j++) {
2881  const uint8_t *src = frame->data[i] + j * frame->linesize[i];
2882 #if HAVE_BIGENDIAN
2883  if (pixel_shift) {
2884  s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
2885  (const uint16_t *) src, w);
2886  src = s->checksum_buf;
2887  }
2888 #endif
2889  av_md5_update(s->md5_ctx, src, w << pixel_shift);
2890  }
2891  av_md5_final(s->md5_ctx, md5);
2892 
2893  if (!memcmp(md5, s->md5[i], 16)) {
2894  av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
2895  print_md5(s->avctx, AV_LOG_DEBUG, md5);
2896  av_log (s->avctx, AV_LOG_DEBUG, "; ");
2897  } else {
2898  av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
2899  print_md5(s->avctx, AV_LOG_ERROR, md5);
2900  av_log (s->avctx, AV_LOG_ERROR, " != ");
2901  print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
2902  av_log (s->avctx, AV_LOG_ERROR, "\n");
2903  return AVERROR_INVALIDDATA;
2904  }
2905  }
2906 
2907  av_log(s->avctx, AV_LOG_DEBUG, "\n");
2908 
2909  return 0;
2910 }
2911 
2912 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
2913  AVPacket *avpkt)
2914 {
2915  int ret;
2916  HEVCContext *s = avctx->priv_data;
2917 
2918  if (!avpkt->size) {
2919  ret = ff_hevc_output_frame(s, data, 1);
2920  if (ret < 0)
2921  return ret;
2922 
2923  *got_output = ret;
2924  return 0;
2925  }
2926 
2927  s->ref = NULL;
2928  ret = decode_nal_units(s, avpkt->data, avpkt->size);
2929  if (ret < 0)
2930  return ret;
2931 
2932  if (avctx->hwaccel) {
2933  if (s->ref && (ret = avctx->hwaccel->end_frame(avctx)) < 0) {
2934  av_log(avctx, AV_LOG_ERROR,
2935  "hardware accelerator failed to decode picture\n");
2936  ff_hevc_unref_frame(s, s->ref, ~0);
2937  return ret;
2938  }
2939  } else {
2940  /* verify the SEI checksum */
2941  if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
2942  s->is_md5) {
2943  ret = verify_md5(s, s->ref->frame);
2944  if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
2945  ff_hevc_unref_frame(s, s->ref, ~0);
2946  return ret;
2947  }
2948  }
2949  }
2950  s->is_md5 = 0;
2951 
2952  if (s->is_decoded) {
2953  av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
2954  s->is_decoded = 0;
2955  }
2956 
2957  if (s->output_frame->buf[0]) {
2958  av_frame_move_ref(data, s->output_frame);
2959  *got_output = 1;
2960  }
2961 
2962  return avpkt->size;
2963 }
2964 
2966 {
2967  int ret;
2968 
2969  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
2970  if (ret < 0)
2971  return ret;
2972 
2973  dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
2974  if (!dst->tab_mvf_buf)
2975  goto fail;
2976  dst->tab_mvf = src->tab_mvf;
2977 
2978  dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
2979  if (!dst->rpl_tab_buf)
2980  goto fail;
2981  dst->rpl_tab = src->rpl_tab;
2982 
2983  dst->rpl_buf = av_buffer_ref(src->rpl_buf);
2984  if (!dst->rpl_buf)
2985  goto fail;
2986 
2987  dst->poc = src->poc;
2988  dst->ctb_count = src->ctb_count;
2989  dst->window = src->window;
2990  dst->flags = src->flags;
2991  dst->sequence = src->sequence;
2992 
2993  if (src->hwaccel_picture_private) {
2995  if (!dst->hwaccel_priv_buf)
2996  goto fail;
2998  }
2999 
3000  return 0;
3001 fail:
3002  ff_hevc_unref_frame(s, dst, ~0);
3003  return AVERROR(ENOMEM);
3004 }
3005 
3007 {
3008  HEVCContext *s = avctx->priv_data;
3009  int i;
3010 
3011  pic_arrays_free(s);
3012 
3013  av_freep(&s->md5_ctx);
3014 
3015  av_freep(&s->cabac_state);
3016 
3017  for (i = 0; i < 3; i++) {
3018  av_freep(&s->sao_pixel_buffer_h[i]);
3019  av_freep(&s->sao_pixel_buffer_v[i]);
3020  }
3022 
3023  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3024  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3025  av_frame_free(&s->DPB[i].frame);
3026  }
3027 
3028  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
3029  av_buffer_unref(&s->ps.vps_list[i]);
3030  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
3031  av_buffer_unref(&s->ps.sps_list[i]);
3032  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
3033  av_buffer_unref(&s->ps.pps_list[i]);
3034  s->ps.sps = NULL;
3035  s->ps.pps = NULL;
3036  s->ps.vps = NULL;
3037 
3039  av_freep(&s->sh.offset);
3040  av_freep(&s->sh.size);
3041 
3042  for (i = 1; i < s->threads_number; i++) {
3043  HEVCLocalContext *lc = s->HEVClcList[i];
3044  if (lc) {
3045  av_freep(&s->HEVClcList[i]);
3046  av_freep(&s->sList[i]);
3047  }
3048  }
3049  if (s->HEVClc == s->HEVClcList[0])
3050  s->HEVClc = NULL;
3051  av_freep(&s->HEVClcList[0]);
3052 
3053  for (i = 0; i < s->pkt.nals_allocated; i++) {
3054  av_freep(&s->pkt.nals[i].rbsp_buffer);
3056  }
3057  av_freep(&s->pkt.nals);
3058  s->pkt.nals_allocated = 0;
3059 
3060  return 0;
3061 }
3062 
3064 {
3065  HEVCContext *s = avctx->priv_data;
3066  int i;
3067 
3068  s->avctx = avctx;
3069 
3070  s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
3071  if (!s->HEVClc)
3072  goto fail;
3073  s->HEVClcList[0] = s->HEVClc;
3074  s->sList[0] = s;
3075 
3077  if (!s->cabac_state)
3078  goto fail;
3079 
3081  if (!s->output_frame)
3082  goto fail;
3083 
3084  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3085  s->DPB[i].frame = av_frame_alloc();
3086  if (!s->DPB[i].frame)
3087  goto fail;
3088  s->DPB[i].tf.f = s->DPB[i].frame;
3089  }
3090 
3091  s->max_ra = INT_MAX;
3092 
3093  s->md5_ctx = av_md5_alloc();
3094  if (!s->md5_ctx)
3095  goto fail;
3096 
3097  ff_bswapdsp_init(&s->bdsp);
3098 
3099  s->context_initialized = 1;
3100  s->eos = 0;
3101 
3102  return 0;
3103 
3104 fail:
3105  hevc_decode_free(avctx);
3106  return AVERROR(ENOMEM);
3107 }
3108 
3110  const AVCodecContext *src)
3111 {
3112  HEVCContext *s = dst->priv_data;
3113  HEVCContext *s0 = src->priv_data;
3114  int i, ret;
3115 
3116  if (!s->context_initialized) {
3117  ret = hevc_init_context(dst);
3118  if (ret < 0)
3119  return ret;
3120  }
3121 
3122  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3123  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3124  if (s0->DPB[i].frame->buf[0]) {
3125  ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
3126  if (ret < 0)
3127  return ret;
3128  }
3129  }
3130 
3131  if (s->ps.sps != s0->ps.sps)
3132  s->ps.sps = NULL;
3133  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
3134  av_buffer_unref(&s->ps.vps_list[i]);
3135  if (s0->ps.vps_list[i]) {
3136  s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
3137  if (!s->ps.vps_list[i])
3138  return AVERROR(ENOMEM);
3139  }
3140  }
3141 
3142  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3143  av_buffer_unref(&s->ps.sps_list[i]);
3144  if (s0->ps.sps_list[i]) {
3145  s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
3146  if (!s->ps.sps_list[i])
3147  return AVERROR(ENOMEM);
3148  }
3149  }
3150 
3151  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
3152  av_buffer_unref(&s->ps.pps_list[i]);
3153  if (s0->ps.pps_list[i]) {
3154  s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
3155  if (!s->ps.pps_list[i])
3156  return AVERROR(ENOMEM);
3157  }
3158  }
3159 
3160  if (s->ps.sps != s0->ps.sps)
3161  if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
3162  return ret;
3163 
3164  s->seq_decode = s0->seq_decode;
3165  s->seq_output = s0->seq_output;
3166  s->pocTid0 = s0->pocTid0;
3167  s->max_ra = s0->max_ra;
3168  s->eos = s0->eos;
3170 
3171  s->is_nalff = s0->is_nalff;
3173 
3174  s->threads_number = s0->threads_number;
3175  s->threads_type = s0->threads_type;
3176 
3177  if (s0->eos) {
3178  s->seq_decode = (s->seq_decode + 1) & 0xff;
3179  s->max_ra = INT_MAX;
3180  }
3181 
3182  return 0;
3183 }
3184 
3186 {
3187  AVCodecContext *avctx = s->avctx;
3188  GetByteContext gb;
3189  int ret, i;
3190 
3191  bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
3192 
3193  if (avctx->extradata_size > 3 &&
3194  (avctx->extradata[0] || avctx->extradata[1] ||
3195  avctx->extradata[2] > 1)) {
3196  /* It seems the extradata is encoded as hvcC format.
3197  * Temporarily, we support configurationVersion==0 until 14496-15 3rd
3198  * is finalized. When finalized, configurationVersion will be 1 and we
3199  * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
3200  int i, j, num_arrays, nal_len_size;
3201 
3202  s->is_nalff = 1;
3203 
3204  bytestream2_skip(&gb, 21);
3205  nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
3206  num_arrays = bytestream2_get_byte(&gb);
3207 
3208  /* nal units in the hvcC always have length coded with 2 bytes,
3209  * so put a fake nal_length_size = 2 while parsing them */
3210  s->nal_length_size = 2;
3211 
3212  /* Decode nal units from hvcC. */
3213  for (i = 0; i < num_arrays; i++) {
3214  int type = bytestream2_get_byte(&gb) & 0x3f;
3215  int cnt = bytestream2_get_be16(&gb);
3216 
3217  for (j = 0; j < cnt; j++) {
3218  // +2 for the nal size field
3219  int nalsize = bytestream2_peek_be16(&gb) + 2;
3220  if (bytestream2_get_bytes_left(&gb) < nalsize) {
3222  "Invalid NAL unit size in extradata.\n");
3223  return AVERROR_INVALIDDATA;
3224  }
3225 
3226  ret = decode_nal_units(s, gb.buffer, nalsize);
3227  if (ret < 0) {
3228  av_log(avctx, AV_LOG_ERROR,
3229  "Decoding nal unit %d %d from hvcC failed\n",
3230  type, i);
3231  return ret;
3232  }
3233  bytestream2_skip(&gb, nalsize);
3234  }
3235  }
3236 
3237  /* Now store right nal length size, that will be used to parse
3238  * all other nals */
3239  s->nal_length_size = nal_len_size;
3240  } else {
3241  s->is_nalff = 0;
3242  ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
3243  if (ret < 0)
3244  return ret;
3245  }
3246 
3247  /* export stream parameters from the first SPS */
3248  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3249  if (s->ps.sps_list[i]) {
3250  const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
3251  export_stream_params(s->avctx, &s->ps, sps);
3252  break;
3253  }
3254  }
3255 
3256  return 0;
3257 }
3258 
3260 {
3261  HEVCContext *s = avctx->priv_data;
3262  int ret;
3263 
3265 
3266  avctx->internal->allocate_progress = 1;
3267 
3268  ret = hevc_init_context(avctx);
3269  if (ret < 0)
3270  return ret;
3271 
3272  s->enable_parallel_tiles = 0;
3273  s->picture_struct = 0;
3274  s->eos = 1;
3275 
3276  if(avctx->active_thread_type & FF_THREAD_SLICE)
3277  s->threads_number = avctx->thread_count;
3278  else
3279  s->threads_number = 1;
3280 
3281  if (avctx->extradata_size > 0 && avctx->extradata) {
3282  ret = hevc_decode_extradata(s);
3283  if (ret < 0) {
3284  hevc_decode_free(avctx);
3285  return ret;
3286  }
3287  }
3288 
3289  if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
3291  else
3293 
3294  return 0;
3295 }
3296 
3298 {
3299  HEVCContext *s = avctx->priv_data;
3300  int ret;
3301 
3302  memset(s, 0, sizeof(*s));
3303 
3304  ret = hevc_init_context(avctx);
3305  if (ret < 0)
3306  return ret;
3307 
3308  return 0;
3309 }
3310 
3312 {
3313  HEVCContext *s = avctx->priv_data;
3314  ff_hevc_flush_dpb(s);
3315  s->max_ra = INT_MAX;
3316  s->eos = 1;
3317 }
3318 
3319 #define OFFSET(x) offsetof(HEVCContext, x)
3320 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3321 
3322 static const AVProfile profiles[] = {
3323  { FF_PROFILE_HEVC_MAIN, "Main" },
3324  { FF_PROFILE_HEVC_MAIN_10, "Main 10" },
3325  { FF_PROFILE_HEVC_MAIN_STILL_PICTURE, "Main Still Picture" },
3326  { FF_PROFILE_HEVC_REXT, "Rext" },
3327  { FF_PROFILE_UNKNOWN },
3328 };
3329 
3330 static const AVOption options[] = {
3331  { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3332  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
3333  { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
3334  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
3335  { NULL },
3336 };
3337 
3338 static const AVClass hevc_decoder_class = {
3339  .class_name = "HEVC decoder",
3340  .item_name = av_default_item_name,
3341  .option = options,
3342  .version = LIBAVUTIL_VERSION_INT,
3343 };
3344 
3346  .name = "hevc",
3347  .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3348  .type = AVMEDIA_TYPE_VIDEO,
3349  .id = AV_CODEC_ID_HEVC,
3350  .priv_data_size = sizeof(HEVCContext),
3351  .priv_class = &hevc_decoder_class,
3353  .close = hevc_decode_free,
3358  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3360  .profiles = NULL_IF_CONFIG_SMALL(profiles),
3361 };
#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:1640
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:3109
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:1917
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:2305
static int hls_slice_data(HEVCContext *s)
Definition: hevc.c:2361
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:842
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:3320
#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:2545
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:2008
#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:3784
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:2839
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:932
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:830
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:3311
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:1394
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:2447
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:3297
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:2846
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:3338
#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:3063
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:3330
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:79
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:1550
int size
Definition: hevc.h:754
static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
Definition: hevc.c:2798
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:1820
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:3319
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:1276
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:2594
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:544
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:1485
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:828
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:3259
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:1333
#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:3185
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:2256
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:3345
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:2651
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:1900
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:227
#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:1138
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:153
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:1649
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:2171
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:543
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:511
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:67
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:1153
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:2912
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:1694
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:1913
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:916
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:272
#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:2372
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:65
static av_cold int hevc_decode_free(AVCodecContext *avctx)
Definition: hevc.c:3006
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:2965
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:3322
#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:1985
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