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