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