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