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