Libav
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 Libav.
10  *
11  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "libavutil/attributes.h"
27 #include "libavutil/common.h"
28 #include "libavutil/display.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/md5.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/stereo3d.h"
34 
35 #include "bswapdsp.h"
36 #include "bytestream.h"
37 #include "cabac_functions.h"
38 #include "golomb.h"
39 #include "hevc.h"
40 #include "profiles.h"
41 
42 const uint8_t ff_hevc_qpel_extra_before[4] = { 0, 3, 3, 3 };
43 const uint8_t ff_hevc_qpel_extra_after[4] = { 0, 4, 4, 4 };
44 const uint8_t ff_hevc_qpel_extra[4] = { 0, 7, 7, 7 };
45 
46 static const uint8_t scan_1x1[1] = { 0 };
47 
48 static const uint8_t horiz_scan2x2_x[4] = { 0, 1, 0, 1 };
49 
50 static const uint8_t horiz_scan2x2_y[4] = { 0, 0, 1, 1 };
51 
52 static const uint8_t horiz_scan4x4_x[16] = {
53  0, 1, 2, 3,
54  0, 1, 2, 3,
55  0, 1, 2, 3,
56  0, 1, 2, 3,
57 };
58 
59 static const uint8_t horiz_scan4x4_y[16] = {
60  0, 0, 0, 0,
61  1, 1, 1, 1,
62  2, 2, 2, 2,
63  3, 3, 3, 3,
64 };
65 
66 static const uint8_t horiz_scan8x8_inv[8][8] = {
67  { 0, 1, 2, 3, 16, 17, 18, 19, },
68  { 4, 5, 6, 7, 20, 21, 22, 23, },
69  { 8, 9, 10, 11, 24, 25, 26, 27, },
70  { 12, 13, 14, 15, 28, 29, 30, 31, },
71  { 32, 33, 34, 35, 48, 49, 50, 51, },
72  { 36, 37, 38, 39, 52, 53, 54, 55, },
73  { 40, 41, 42, 43, 56, 57, 58, 59, },
74  { 44, 45, 46, 47, 60, 61, 62, 63, },
75 };
76 
77 static const uint8_t diag_scan2x2_x[4] = { 0, 0, 1, 1 };
78 
79 static const uint8_t diag_scan2x2_y[4] = { 0, 1, 0, 1 };
80 
81 static const uint8_t diag_scan2x2_inv[2][2] = {
82  { 0, 2, },
83  { 1, 3, },
84 };
85 
86 static const uint8_t diag_scan4x4_inv[4][4] = {
87  { 0, 2, 5, 9, },
88  { 1, 4, 8, 12, },
89  { 3, 7, 11, 14, },
90  { 6, 10, 13, 15, },
91 };
92 
93 static const uint8_t diag_scan8x8_inv[8][8] = {
94  { 0, 2, 5, 9, 14, 20, 27, 35, },
95  { 1, 4, 8, 13, 19, 26, 34, 42, },
96  { 3, 7, 12, 18, 25, 33, 41, 48, },
97  { 6, 11, 17, 24, 32, 40, 47, 53, },
98  { 10, 16, 23, 31, 39, 46, 52, 57, },
99  { 15, 22, 30, 38, 45, 51, 56, 60, },
100  { 21, 29, 37, 44, 50, 55, 59, 62, },
101  { 28, 36, 43, 49, 54, 58, 61, 63, },
102 };
103 
113 /* free everything allocated by pic_arrays_init() */
115 {
116  av_freep(&s->sao);
117  av_freep(&s->deblock);
118 
119  av_freep(&s->skip_flag);
120  av_freep(&s->tab_ct_depth);
121 
122  av_freep(&s->tab_ipm);
123  av_freep(&s->cbf_luma);
124  av_freep(&s->is_pcm);
125 
126  av_freep(&s->qp_y_tab);
129 
130  av_freep(&s->horizontal_bs);
131  av_freep(&s->vertical_bs);
132 
135 }
136 
137 /* allocate arrays that depend on frame dimensions */
138 static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
139 {
140  int log2_min_cb_size = sps->log2_min_cb_size;
141  int width = sps->width;
142  int height = sps->height;
143  int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
144  ((height >> log2_min_cb_size) + 1);
145  int ctb_count = sps->ctb_width * sps->ctb_height;
146  int min_pu_size = sps->min_pu_width * sps->min_pu_height;
147 
148  s->bs_width = width >> 3;
149  s->bs_height = height >> 3;
150 
151  s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
152  s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
153  if (!s->sao || !s->deblock)
154  goto fail;
155 
156  s->skip_flag = av_malloc(pic_size_in_ctb);
158  if (!s->skip_flag || !s->tab_ct_depth)
159  goto fail;
160 
161  s->cbf_luma = av_malloc(sps->min_tb_width * sps->min_tb_height);
162  s->tab_ipm = av_mallocz(min_pu_size);
163  s->is_pcm = av_malloc(min_pu_size);
164  if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
165  goto fail;
166 
167  s->filter_slice_edges = av_malloc(ctb_count);
168  s->tab_slice_address = av_malloc(pic_size_in_ctb *
169  sizeof(*s->tab_slice_address));
170  s->qp_y_tab = av_malloc(pic_size_in_ctb *
171  sizeof(*s->qp_y_tab));
172  if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
173  goto fail;
174 
175  s->horizontal_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
176  s->vertical_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
177  if (!s->horizontal_bs || !s->vertical_bs)
178  goto fail;
179 
180  s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
182  s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
184  if (!s->tab_mvf_pool || !s->rpl_tab_pool)
185  goto fail;
186 
187  return 0;
188 
189 fail:
190  pic_arrays_free(s);
191  return AVERROR(ENOMEM);
192 }
193 
195 {
196  int i = 0;
197  int j = 0;
198  uint8_t luma_weight_l0_flag[16];
199  uint8_t chroma_weight_l0_flag[16];
200  uint8_t luma_weight_l1_flag[16];
201  uint8_t chroma_weight_l1_flag[16];
202 
203  s->sh.luma_log2_weight_denom = av_clip(get_ue_golomb_long(gb), 0, 7);
204  if (s->ps.sps->chroma_format_idc != 0) {
205  int delta = get_se_golomb(gb);
206  s->sh.chroma_log2_weight_denom = av_clip(s->sh.luma_log2_weight_denom + delta, 0, 7);
207  }
208 
209  for (i = 0; i < s->sh.nb_refs[L0]; i++) {
210  luma_weight_l0_flag[i] = get_bits1(gb);
211  if (!luma_weight_l0_flag[i]) {
212  s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
213  s->sh.luma_offset_l0[i] = 0;
214  }
215  }
216  if (s->ps.sps->chroma_format_idc != 0) { // FIXME: invert "if" and "for"
217  for (i = 0; i < s->sh.nb_refs[L0]; i++)
218  chroma_weight_l0_flag[i] = get_bits1(gb);
219  } else {
220  for (i = 0; i < s->sh.nb_refs[L0]; i++)
221  chroma_weight_l0_flag[i] = 0;
222  }
223  for (i = 0; i < s->sh.nb_refs[L0]; i++) {
224  if (luma_weight_l0_flag[i]) {
225  int delta_luma_weight_l0 = get_se_golomb(gb);
226  s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
227  s->sh.luma_offset_l0[i] = get_se_golomb(gb);
228  }
229  if (chroma_weight_l0_flag[i]) {
230  for (j = 0; j < 2; j++) {
231  int delta_chroma_weight_l0 = get_se_golomb(gb);
232  int delta_chroma_offset_l0 = get_se_golomb(gb);
233  s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
234  s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
235  >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
236  }
237  } else {
238  s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
239  s->sh.chroma_offset_l0[i][0] = 0;
240  s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
241  s->sh.chroma_offset_l0[i][1] = 0;
242  }
243  }
244  if (s->sh.slice_type == B_SLICE) {
245  for (i = 0; i < s->sh.nb_refs[L1]; i++) {
246  luma_weight_l1_flag[i] = get_bits1(gb);
247  if (!luma_weight_l1_flag[i]) {
248  s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
249  s->sh.luma_offset_l1[i] = 0;
250  }
251  }
252  if (s->ps.sps->chroma_format_idc != 0) {
253  for (i = 0; i < s->sh.nb_refs[L1]; i++)
254  chroma_weight_l1_flag[i] = get_bits1(gb);
255  } else {
256  for (i = 0; i < s->sh.nb_refs[L1]; i++)
257  chroma_weight_l1_flag[i] = 0;
258  }
259  for (i = 0; i < s->sh.nb_refs[L1]; i++) {
260  if (luma_weight_l1_flag[i]) {
261  int delta_luma_weight_l1 = get_se_golomb(gb);
262  s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
263  s->sh.luma_offset_l1[i] = get_se_golomb(gb);
264  }
265  if (chroma_weight_l1_flag[i]) {
266  for (j = 0; j < 2; j++) {
267  int delta_chroma_weight_l1 = get_se_golomb(gb);
268  int delta_chroma_offset_l1 = get_se_golomb(gb);
269  s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
270  s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
271  >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
272  }
273  } else {
274  s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
275  s->sh.chroma_offset_l1[i][0] = 0;
276  s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
277  s->sh.chroma_offset_l1[i][1] = 0;
278  }
279  }
280  }
281 }
282 
284 {
285  const HEVCSPS *sps = s->ps.sps;
286  int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
287  int prev_delta_msb = 0;
288  unsigned int nb_sps = 0, nb_sh;
289  int i;
290 
291  rps->nb_refs = 0;
293  return 0;
294 
295  if (sps->num_long_term_ref_pics_sps > 0)
296  nb_sps = get_ue_golomb_long(gb);
297  nb_sh = get_ue_golomb_long(gb);
298 
299  if (nb_sh + nb_sps > FF_ARRAY_ELEMS(rps->poc))
300  return AVERROR_INVALIDDATA;
301 
302  rps->nb_refs = nb_sh + nb_sps;
303 
304  for (i = 0; i < rps->nb_refs; i++) {
305  uint8_t delta_poc_msb_present;
306 
307  if (i < nb_sps) {
308  uint8_t lt_idx_sps = 0;
309 
310  if (sps->num_long_term_ref_pics_sps > 1)
311  lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
312 
313  rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
314  rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
315  } else {
316  rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
317  rps->used[i] = get_bits1(gb);
318  }
319 
320  delta_poc_msb_present = get_bits1(gb);
321  if (delta_poc_msb_present) {
322  int delta = get_ue_golomb_long(gb);
323 
324  if (i && i != nb_sps)
325  delta += prev_delta_msb;
326 
327  rps->poc[i] += s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
328  prev_delta_msb = delta;
329  }
330  }
331 
332  return 0;
333 }
334 
335 static void export_stream_params(AVCodecContext *avctx, const HEVCParamSets *ps,
336  const HEVCSPS *sps)
337 {
338  const HEVCVPS *vps = (const HEVCVPS*)ps->vps_list[sps->vps_id]->data;
339  unsigned int num = 0, den = 0;
340 
341  avctx->pix_fmt = sps->pix_fmt;
342  avctx->coded_width = sps->width;
343  avctx->coded_height = sps->height;
344  avctx->width = sps->output_width;
345  avctx->height = sps->output_height;
347  avctx->profile = sps->ptl.general_ptl.profile_idc;
348  avctx->level = sps->ptl.general_ptl.level_idc;
349 
350  ff_set_sar(avctx, sps->vui.sar);
351 
355  else
356  avctx->color_range = AVCOL_RANGE_MPEG;
357 
359  avctx->color_primaries = sps->vui.colour_primaries;
360  avctx->color_trc = sps->vui.transfer_characteristic;
361  avctx->colorspace = sps->vui.matrix_coeffs;
362  } else {
366  }
367 
368  if (vps->vps_timing_info_present_flag) {
369  num = vps->vps_num_units_in_tick;
370  den = vps->vps_time_scale;
371  } else if (sps->vui.vui_timing_info_present_flag) {
372  num = sps->vui.vui_num_units_in_tick;
373  den = sps->vui.vui_time_scale;
374  }
375 
376  if (num != 0 && den != 0)
377  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
378  num, den, 1 << 30);
379 }
380 
381 static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
382 {
383  #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + CONFIG_HEVC_D3D11VA_HWACCEL + CONFIG_HEVC_VDPAU_HWACCEL)
384  enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
385 
386  if (sps->pix_fmt == AV_PIX_FMT_YUV420P || sps->pix_fmt == AV_PIX_FMT_YUVJ420P ||
387  sps->pix_fmt == AV_PIX_FMT_YUV420P10) {
388 #if CONFIG_HEVC_DXVA2_HWACCEL
389  *fmt++ = AV_PIX_FMT_DXVA2_VLD;
390 #endif
391  }
392  if (sps->pix_fmt == AV_PIX_FMT_YUV420P || sps->pix_fmt == AV_PIX_FMT_YUVJ420P) {
393 #if CONFIG_HEVC_D3D11VA_HWACCEL
394  *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
395 #endif
396 #if CONFIG_HEVC_VDPAU_HWACCEL
397  *fmt++ = AV_PIX_FMT_VDPAU;
398 #endif
399  }
400 
401  *fmt++ = sps->pix_fmt;
402  *fmt = AV_PIX_FMT_NONE;
403 
404  return ff_get_format(s->avctx, pix_fmts);
405 }
406 
407 static int set_sps(HEVCContext *s, const HEVCSPS *sps,
408  enum AVPixelFormat pix_fmt)
409 {
410  int ret;
411 
412  pic_arrays_free(s);
413  s->ps.sps = NULL;
414  s->ps.vps = NULL;
415 
416  if (!sps)
417  return 0;
418 
419  ret = pic_arrays_init(s, sps);
420  if (ret < 0)
421  goto fail;
422 
423  export_stream_params(s->avctx, &s->ps, sps);
424 
425  s->avctx->pix_fmt = pix_fmt;
426 
427  ff_hevc_pred_init(&s->hpc, sps->bit_depth);
428  ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
429  ff_videodsp_init (&s->vdsp, sps->bit_depth);
430 
431  if (sps->sao_enabled && !s->avctx->hwaccel) {
434  if (ret < 0)
435  goto fail;
436  s->frame = s->tmp_frame;
437  }
438 
439  s->ps.sps = sps;
440  s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
441 
442  return 0;
443 
444 fail:
445  pic_arrays_free(s);
446  s->ps.sps = NULL;
447  return ret;
448 }
449 
451 {
452  GetBitContext *gb = &s->HEVClc.gb;
453  SliceHeader *sh = &s->sh;
454  int i, ret;
455 
456  // Coded parameters
458  if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
459  s->seq_decode = (s->seq_decode + 1) & 0xff;
460  s->max_ra = INT_MAX;
461  if (IS_IDR(s))
463  }
464  if (IS_IRAP(s))
466 
467  sh->pps_id = get_ue_golomb_long(gb);
468  if (sh->pps_id >= MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
469  av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
470  return AVERROR_INVALIDDATA;
471  }
472  if (!sh->first_slice_in_pic_flag &&
473  s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
474  av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
475  return AVERROR_INVALIDDATA;
476  }
477  s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
478 
479  if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
480  const HEVCSPS *sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
481  enum AVPixelFormat pix_fmt;
482 
484 
485  pix_fmt = get_format(s, sps);
486  if (pix_fmt < 0)
487  return pix_fmt;
488 
489  ret = set_sps(s, sps, pix_fmt);
490  if (ret < 0)
491  return ret;
492 
493  s->seq_decode = (s->seq_decode + 1) & 0xff;
494  s->max_ra = INT_MAX;
495  }
496 
498  if (!sh->first_slice_in_pic_flag) {
499  int slice_address_length;
500 
503 
504  slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
505  s->ps.sps->ctb_height);
506  sh->slice_segment_addr = slice_address_length ? get_bits(gb, slice_address_length) : 0;
507  if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
509  "Invalid slice segment address: %u.\n",
510  sh->slice_segment_addr);
511  return AVERROR_INVALIDDATA;
512  }
513 
514  if (!sh->dependent_slice_segment_flag) {
515  sh->slice_addr = sh->slice_segment_addr;
516  s->slice_idx++;
517  }
518  } else {
519  sh->slice_segment_addr = sh->slice_addr = 0;
520  s->slice_idx = 0;
521  s->slice_initialized = 0;
522  }
523 
524  if (!sh->dependent_slice_segment_flag) {
525  s->slice_initialized = 0;
526 
527  for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
528  skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
529 
530  sh->slice_type = get_ue_golomb_long(gb);
531  if (!(sh->slice_type == I_SLICE ||
532  sh->slice_type == P_SLICE ||
533  sh->slice_type == B_SLICE)) {
534  av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
535  sh->slice_type);
536  return AVERROR_INVALIDDATA;
537  }
538  if (IS_IRAP(s) && sh->slice_type != I_SLICE) {
539  av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
540  return AVERROR_INVALIDDATA;
541  }
542 
543  // when flag is not present, picture is inferred to be output
544  sh->pic_output_flag = 1;
546  sh->pic_output_flag = get_bits1(gb);
547 
549  sh->colour_plane_id = get_bits(gb, 2);
550 
551  if (!IS_IDR(s)) {
552  int poc, pos;
553 
556  if (!sh->first_slice_in_pic_flag && poc != s->poc) {
558  "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
560  return AVERROR_INVALIDDATA;
561  poc = s->poc;
562  }
563  s->poc = poc;
564 
566  pos = get_bits_left(gb);
568  ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
569  if (ret < 0)
570  return ret;
571 
572  sh->short_term_rps = &sh->slice_rps;
573  } else {
574  int numbits, rps_idx;
575 
576  if (!s->ps.sps->nb_st_rps) {
577  av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
578  return AVERROR_INVALIDDATA;
579  }
580 
581  numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
582  rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
583  sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
584  }
586 
587  pos = get_bits_left(gb);
588  ret = decode_lt_rps(s, &sh->long_term_rps, gb);
589  if (ret < 0) {
590  av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
592  return AVERROR_INVALIDDATA;
593  }
595 
598  else
600  } else {
601  s->sh.short_term_rps = NULL;
602  s->poc = 0;
603  }
604 
605  /* 8.3.1 */
606  if (s->temporal_id == 0 &&
607  s->nal_unit_type != NAL_TRAIL_N &&
608  s->nal_unit_type != NAL_TSA_N &&
609  s->nal_unit_type != NAL_STSA_N &&
610  s->nal_unit_type != NAL_RADL_N &&
611  s->nal_unit_type != NAL_RADL_R &&
612  s->nal_unit_type != NAL_RASL_N &&
614  s->pocTid0 = s->poc;
615 
616  if (s->ps.sps->sao_enabled) {
620  } else {
624  }
625 
626  sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
627  if (sh->slice_type == P_SLICE || sh->slice_type == B_SLICE) {
628  int nb_refs;
629 
631  if (sh->slice_type == B_SLICE)
633 
634  if (get_bits1(gb)) { // num_ref_idx_active_override_flag
635  sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
636  if (sh->slice_type == B_SLICE)
637  sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
638  }
639  if (sh->nb_refs[L0] > MAX_REFS || sh->nb_refs[L1] > MAX_REFS) {
640  av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
641  sh->nb_refs[L0], sh->nb_refs[L1]);
642  return AVERROR_INVALIDDATA;
643  }
644 
645  sh->rpl_modification_flag[0] = 0;
646  sh->rpl_modification_flag[1] = 0;
647  nb_refs = ff_hevc_frame_nb_refs(s);
648  if (!nb_refs) {
649  av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
650  return AVERROR_INVALIDDATA;
651  }
652 
653  if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
654  sh->rpl_modification_flag[0] = get_bits1(gb);
655  if (sh->rpl_modification_flag[0]) {
656  for (i = 0; i < sh->nb_refs[L0]; i++)
657  sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
658  }
659 
660  if (sh->slice_type == B_SLICE) {
661  sh->rpl_modification_flag[1] = get_bits1(gb);
662  if (sh->rpl_modification_flag[1] == 1)
663  for (i = 0; i < sh->nb_refs[L1]; i++)
664  sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
665  }
666  }
667 
668  if (sh->slice_type == B_SLICE)
669  sh->mvd_l1_zero_flag = get_bits1(gb);
670 
672  sh->cabac_init_flag = get_bits1(gb);
673  else
674  sh->cabac_init_flag = 0;
675 
676  sh->collocated_ref_idx = 0;
678  sh->collocated_list = L0;
679  if (sh->slice_type == B_SLICE)
680  sh->collocated_list = !get_bits1(gb);
681 
682  if (sh->nb_refs[sh->collocated_list] > 1) {
684  if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
686  "Invalid collocated_ref_idx: %d.\n",
687  sh->collocated_ref_idx);
688  return AVERROR_INVALIDDATA;
689  }
690  }
691  }
692 
693  if ((s->ps.pps->weighted_pred_flag && sh->slice_type == P_SLICE) ||
694  (s->ps.pps->weighted_bipred_flag && sh->slice_type == B_SLICE)) {
695  pred_weight_table(s, gb);
696  }
697 
699  if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
701  "Invalid number of merging MVP candidates: %d.\n",
702  sh->max_num_merge_cand);
703  return AVERROR_INVALIDDATA;
704  }
705  }
706 
707  sh->slice_qp_delta = get_se_golomb(gb);
708 
712  } else {
713  sh->slice_cb_qp_offset = 0;
714  sh->slice_cr_qp_offset = 0;
715  }
716 
718  int deblocking_filter_override_flag = 0;
719 
721  deblocking_filter_override_flag = get_bits1(gb);
722 
723  if (deblocking_filter_override_flag) {
726  sh->beta_offset = get_se_golomb(gb) * 2;
727  sh->tc_offset = get_se_golomb(gb) * 2;
728  }
729  } else {
731  sh->beta_offset = s->ps.pps->beta_offset;
732  sh->tc_offset = s->ps.pps->tc_offset;
733  }
734  } else {
736  sh->beta_offset = 0;
737  sh->tc_offset = 0;
738  }
739 
745  } else {
747  }
748  } else if (!s->slice_initialized) {
749  av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
750  return AVERROR_INVALIDDATA;
751  }
752 
753  sh->num_entry_point_offsets = 0;
756  if (sh->num_entry_point_offsets > 0) {
757  int offset_len = get_ue_golomb_long(gb) + 1;
758 
759  for (i = 0; i < sh->num_entry_point_offsets; i++)
760  skip_bits(gb, offset_len);
761  }
762  }
763 
765  unsigned int length = get_ue_golomb_long(gb);
766  for (i = 0; i < length; i++)
767  skip_bits(gb, 8); // slice_header_extension_data_byte
768  }
769 
770  // Inferred parameters
771  sh->slice_qp = 26 + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
772  if (sh->slice_qp > 51 ||
773  sh->slice_qp < -s->ps.sps->qp_bd_offset) {
775  "The slice_qp %d is outside the valid range "
776  "[%d, 51].\n",
777  sh->slice_qp,
778  -s->ps.sps->qp_bd_offset);
779  return AVERROR_INVALIDDATA;
780  }
781 
783 
785  av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
786  return AVERROR_INVALIDDATA;
787  }
788 
790 
792  s->HEVClc.qp_y = FFUMOD(s->sh.slice_qp + 52 + 2 * s->ps.sps->qp_bd_offset,
793  52 + s->ps.sps->qp_bd_offset) - s->ps.sps->qp_bd_offset;
794 
795  s->slice_initialized = 1;
796 
797  return 0;
798 }
799 
800 #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
801 
802 #define SET_SAO(elem, value) \
803 do { \
804  if (!sao_merge_up_flag && !sao_merge_left_flag) \
805  sao->elem = value; \
806  else if (sao_merge_left_flag) \
807  sao->elem = CTB(s->sao, rx-1, ry).elem; \
808  else if (sao_merge_up_flag) \
809  sao->elem = CTB(s->sao, rx, ry-1).elem; \
810  else \
811  sao->elem = 0; \
812 } while (0)
813 
814 static void hls_sao_param(HEVCContext *s, int rx, int ry)
815 {
816  HEVCLocalContext *lc = &s->HEVClc;
817  int sao_merge_left_flag = 0;
818  int sao_merge_up_flag = 0;
819  int shift = s->ps.sps->bit_depth - FFMIN(s->ps.sps->bit_depth, 10);
820  SAOParams *sao = &CTB(s->sao, rx, ry);
821  int c_idx, i;
822 
825  if (rx > 0) {
826  if (lc->ctb_left_flag)
827  sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
828  }
829  if (ry > 0 && !sao_merge_left_flag) {
830  if (lc->ctb_up_flag)
831  sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
832  }
833  }
834 
835  for (c_idx = 0; c_idx < 3; c_idx++) {
836  if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
837  sao->type_idx[c_idx] = SAO_NOT_APPLIED;
838  continue;
839  }
840 
841  if (c_idx == 2) {
842  sao->type_idx[2] = sao->type_idx[1];
843  sao->eo_class[2] = sao->eo_class[1];
844  } else {
845  SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
846  }
847 
848  if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
849  continue;
850 
851  for (i = 0; i < 4; i++)
852  SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
853 
854  if (sao->type_idx[c_idx] == SAO_BAND) {
855  for (i = 0; i < 4; i++) {
856  if (sao->offset_abs[c_idx][i]) {
857  SET_SAO(offset_sign[c_idx][i],
859  } else {
860  sao->offset_sign[c_idx][i] = 0;
861  }
862  }
863  SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
864  } else if (c_idx != 2) {
865  SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
866  }
867 
868  // Inferred parameters
869  sao->offset_val[c_idx][0] = 0;
870  for (i = 0; i < 4; i++) {
871  sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i] << shift;
872  if (sao->type_idx[c_idx] == SAO_EDGE) {
873  if (i > 1)
874  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
875  } else if (sao->offset_sign[c_idx][i]) {
876  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
877  }
878  }
879  }
880 }
881 
882 #undef SET_SAO
883 #undef CTB
884 
885 static void hls_residual_coding(HEVCContext *s, int x0, int y0,
886  int log2_trafo_size, enum ScanType scan_idx,
887  int c_idx)
888 {
889 #define GET_COORD(offset, n) \
890  do { \
891  x_c = (scan_x_cg[offset >> 4] << 2) + scan_x_off[n]; \
892  y_c = (scan_y_cg[offset >> 4] << 2) + scan_y_off[n]; \
893  } while (0)
894  HEVCLocalContext *lc = &s->HEVClc;
895  int transform_skip_flag = 0;
896 
897  int last_significant_coeff_x, last_significant_coeff_y;
898  int last_scan_pos;
899  int n_end;
900  int num_coeff = 0;
901  int greater1_ctx = 1;
902 
903  int num_last_subset;
904  int x_cg_last_sig, y_cg_last_sig;
905 
906  const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
907 
908  ptrdiff_t stride = s->frame->linesize[c_idx];
909  int hshift = s->ps.sps->hshift[c_idx];
910  int vshift = s->ps.sps->vshift[c_idx];
911  uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
912  ((x0 >> hshift) << s->ps.sps->pixel_shift)];
913  DECLARE_ALIGNED(32, int16_t, coeffs[MAX_TB_SIZE * MAX_TB_SIZE]) = { 0 };
914  DECLARE_ALIGNED(8, uint8_t, significant_coeff_group_flag[8][8]) = { { 0 } };
915 
916  int trafo_size = 1 << log2_trafo_size;
917  int i, qp, shift, add, scale, scale_m;
918  static const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
919  const uint8_t *scale_matrix;
920  uint8_t dc_scale;
921 
922  // Derive QP for dequant
923  if (!lc->cu.cu_transquant_bypass_flag) {
924  static const int qp_c[] = {
925  29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37
926  };
927 
928  static const uint8_t rem6[51 + 2 * 6 + 1] = {
929  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
930  3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
931  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
932  };
933 
934  static const uint8_t div6[51 + 2 * 6 + 1] = {
935  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
936  3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
937  7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
938  };
939  int qp_y = lc->qp_y;
940 
941  if (c_idx == 0) {
942  qp = qp_y + s->ps.sps->qp_bd_offset;
943  } else {
944  int qp_i, offset;
945 
946  if (c_idx == 1)
947  offset = s->ps.pps->cb_qp_offset + s->sh.slice_cb_qp_offset;
948  else
949  offset = s->ps.pps->cr_qp_offset + s->sh.slice_cr_qp_offset;
950 
951  qp_i = av_clip(qp_y + offset, -s->ps.sps->qp_bd_offset, 57);
952  if (qp_i < 30)
953  qp = qp_i;
954  else if (qp_i > 43)
955  qp = qp_i - 6;
956  else
957  qp = qp_c[qp_i - 30];
958 
959  qp += s->ps.sps->qp_bd_offset;
960  }
961 
962  shift = s->ps.sps->bit_depth + log2_trafo_size - 5;
963  add = 1 << (shift - 1);
964  scale = level_scale[rem6[qp]] << (div6[qp]);
965  scale_m = 16; // default when no custom scaling lists.
966  dc_scale = 16;
967 
968  if (s->ps.sps->scaling_list_enable_flag) {
970  &s->ps.pps->scaling_list : &s->ps.sps->scaling_list;
971  int matrix_id = lc->cu.pred_mode != MODE_INTRA;
972 
973  if (log2_trafo_size != 5)
974  matrix_id = 3 * matrix_id + c_idx;
975 
976  scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
977  if (log2_trafo_size >= 4)
978  dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
979  }
980  }
981 
984  log2_trafo_size == 2) {
985  transform_skip_flag = ff_hevc_transform_skip_flag_decode(s, c_idx);
986  }
987 
988  last_significant_coeff_x =
989  ff_hevc_last_significant_coeff_x_prefix_decode(s, c_idx, log2_trafo_size);
990  last_significant_coeff_y =
991  ff_hevc_last_significant_coeff_y_prefix_decode(s, c_idx, log2_trafo_size);
992 
993  if (last_significant_coeff_x > 3) {
994  int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_x);
995  last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
996  (2 + (last_significant_coeff_x & 1)) +
997  suffix;
998  }
999 
1000  if (last_significant_coeff_y > 3) {
1001  int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_y);
1002  last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1003  (2 + (last_significant_coeff_y & 1)) +
1004  suffix;
1005  }
1006 
1007  if (scan_idx == SCAN_VERT)
1008  FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
1009 
1010  x_cg_last_sig = last_significant_coeff_x >> 2;
1011  y_cg_last_sig = last_significant_coeff_y >> 2;
1012 
1013  switch (scan_idx) {
1014  case SCAN_DIAG: {
1015  int last_x_c = last_significant_coeff_x & 3;
1016  int last_y_c = last_significant_coeff_y & 3;
1017 
1018  scan_x_off = ff_hevc_diag_scan4x4_x;
1019  scan_y_off = ff_hevc_diag_scan4x4_y;
1020  num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
1021  if (trafo_size == 4) {
1022  scan_x_cg = scan_1x1;
1023  scan_y_cg = scan_1x1;
1024  } else if (trafo_size == 8) {
1025  num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1026  scan_x_cg = diag_scan2x2_x;
1027  scan_y_cg = diag_scan2x2_y;
1028  } else if (trafo_size == 16) {
1029  num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1030  scan_x_cg = ff_hevc_diag_scan4x4_x;
1031  scan_y_cg = ff_hevc_diag_scan4x4_y;
1032  } else { // trafo_size == 32
1033  num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1034  scan_x_cg = ff_hevc_diag_scan8x8_x;
1035  scan_y_cg = ff_hevc_diag_scan8x8_y;
1036  }
1037  break;
1038  }
1039  case SCAN_HORIZ:
1040  scan_x_cg = horiz_scan2x2_x;
1041  scan_y_cg = horiz_scan2x2_y;
1042  scan_x_off = horiz_scan4x4_x;
1043  scan_y_off = horiz_scan4x4_y;
1044  num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1045  break;
1046  default: //SCAN_VERT
1047  scan_x_cg = horiz_scan2x2_y;
1048  scan_y_cg = horiz_scan2x2_x;
1049  scan_x_off = horiz_scan4x4_y;
1050  scan_y_off = horiz_scan4x4_x;
1051  num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1052  break;
1053  }
1054  num_coeff++;
1055  num_last_subset = (num_coeff - 1) >> 4;
1056 
1057  for (i = num_last_subset; i >= 0; i--) {
1058  int n, m;
1059  int x_cg, y_cg, x_c, y_c;
1060  int implicit_non_zero_coeff = 0;
1061  int64_t trans_coeff_level;
1062  int prev_sig = 0;
1063  int offset = i << 4;
1064 
1065  uint8_t significant_coeff_flag_idx[16];
1066  uint8_t nb_significant_coeff_flag = 0;
1067 
1068  x_cg = scan_x_cg[i];
1069  y_cg = scan_y_cg[i];
1070 
1071  if (i < num_last_subset && i > 0) {
1072  int ctx_cg = 0;
1073  if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1074  ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1075  if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1076  ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1077 
1078  significant_coeff_group_flag[x_cg][y_cg] =
1080  implicit_non_zero_coeff = 1;
1081  } else {
1082  significant_coeff_group_flag[x_cg][y_cg] =
1083  ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1084  (x_cg == 0 && y_cg == 0));
1085  }
1086 
1087  last_scan_pos = num_coeff - offset - 1;
1088 
1089  if (i == num_last_subset) {
1090  n_end = last_scan_pos - 1;
1091  significant_coeff_flag_idx[0] = last_scan_pos;
1092  nb_significant_coeff_flag = 1;
1093  } else {
1094  n_end = 15;
1095  }
1096 
1097  if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1098  prev_sig = significant_coeff_group_flag[x_cg + 1][y_cg];
1099  if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1100  prev_sig += significant_coeff_group_flag[x_cg][y_cg + 1] << 1;
1101 
1102  for (n = n_end; n >= 0; n--) {
1103  GET_COORD(offset, n);
1104 
1105  if (significant_coeff_group_flag[x_cg][y_cg] &&
1106  (n > 0 || implicit_non_zero_coeff == 0)) {
1107  if (ff_hevc_significant_coeff_flag_decode(s, c_idx, x_c, y_c,
1108  log2_trafo_size,
1109  scan_idx,
1110  prev_sig) == 1) {
1111  significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1112  nb_significant_coeff_flag++;
1113  implicit_non_zero_coeff = 0;
1114  }
1115  } else {
1116  int last_cg = (x_c == (x_cg << 2) && y_c == (y_cg << 2));
1117  if (last_cg && implicit_non_zero_coeff && significant_coeff_group_flag[x_cg][y_cg]) {
1118  significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1119  nb_significant_coeff_flag++;
1120  }
1121  }
1122  }
1123 
1124  n_end = nb_significant_coeff_flag;
1125 
1126  if (n_end) {
1127  int first_nz_pos_in_cg = 16;
1128  int last_nz_pos_in_cg = -1;
1129  int c_rice_param = 0;
1130  int first_greater1_coeff_idx = -1;
1131  uint8_t coeff_abs_level_greater1_flag[16] = { 0 };
1132  uint16_t coeff_sign_flag;
1133  int sum_abs = 0;
1134  int sign_hidden = 0;
1135 
1136  // initialize first elem of coeff_bas_level_greater1_flag
1137  int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1138 
1139  if (!(i == num_last_subset) && greater1_ctx == 0)
1140  ctx_set++;
1141  greater1_ctx = 1;
1142  last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1143 
1144  for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1145  int n_idx = significant_coeff_flag_idx[m];
1146  int inc = (ctx_set << 2) + greater1_ctx;
1147  coeff_abs_level_greater1_flag[n_idx] =
1149  if (coeff_abs_level_greater1_flag[n_idx]) {
1150  greater1_ctx = 0;
1151  } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1152  greater1_ctx++;
1153  }
1154 
1155  if (coeff_abs_level_greater1_flag[n_idx] &&
1156  first_greater1_coeff_idx == -1)
1157  first_greater1_coeff_idx = n_idx;
1158  }
1159  first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1160  sign_hidden = last_nz_pos_in_cg - first_nz_pos_in_cg >= 4 &&
1162 
1163  if (first_greater1_coeff_idx != -1) {
1164  coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += ff_hevc_coeff_abs_level_greater2_flag_decode(s, c_idx, ctx_set);
1165  }
1166  if (!s->ps.pps->sign_data_hiding_flag || !sign_hidden) {
1167  coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1168  } else {
1169  coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
1170  }
1171 
1172  for (m = 0; m < n_end; m++) {
1173  n = significant_coeff_flag_idx[m];
1174  GET_COORD(offset, n);
1175  trans_coeff_level = 1 + coeff_abs_level_greater1_flag[n];
1176  if (trans_coeff_level == ((m < 8) ?
1177  ((n == first_greater1_coeff_idx) ? 3 : 2) : 1)) {
1178  int last_coeff_abs_level_remaining = ff_hevc_coeff_abs_level_remaining(s, trans_coeff_level, c_rice_param);
1179 
1180  trans_coeff_level += last_coeff_abs_level_remaining;
1181  if ((trans_coeff_level) > (3 * (1 << c_rice_param)))
1182  c_rice_param = FFMIN(c_rice_param + 1, 4);
1183  }
1184  if (s->ps.pps->sign_data_hiding_flag && sign_hidden) {
1185  sum_abs += trans_coeff_level;
1186  if (n == first_nz_pos_in_cg && ((sum_abs & 1) == 1))
1187  trans_coeff_level = -trans_coeff_level;
1188  }
1189  if (coeff_sign_flag >> 15)
1190  trans_coeff_level = -trans_coeff_level;
1191  coeff_sign_flag <<= 1;
1192  if (!lc->cu.cu_transquant_bypass_flag) {
1193  if (s->ps.sps->scaling_list_enable_flag) {
1194  if (y_c || x_c || log2_trafo_size < 4) {
1195  int pos;
1196  switch (log2_trafo_size) {
1197  case 3: pos = (y_c << 3) + x_c; break;
1198  case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
1199  case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
1200  default: pos = (y_c << 2) + x_c;
1201  }
1202  scale_m = scale_matrix[pos];
1203  } else {
1204  scale_m = dc_scale;
1205  }
1206  }
1207  trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1208  if(trans_coeff_level < 0) {
1209  if((~trans_coeff_level) & 0xFffffffffff8000)
1210  trans_coeff_level = -32768;
1211  } else {
1212  if (trans_coeff_level & 0xffffffffffff8000)
1213  trans_coeff_level = 32767;
1214  }
1215  }
1216  coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1217  }
1218  }
1219  }
1220 
1221  if (!lc->cu.cu_transquant_bypass_flag) {
1222  if (transform_skip_flag)
1223  s->hevcdsp.dequant(coeffs);
1224  else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 &&
1225  log2_trafo_size == 2)
1226  s->hevcdsp.transform_4x4_luma(coeffs);
1227  else {
1228  int max_xy = FFMAX(last_significant_coeff_x, last_significant_coeff_y);
1229  if (max_xy == 0)
1230  s->hevcdsp.idct_dc[log2_trafo_size - 2](coeffs);
1231  else {
1232  int col_limit = last_significant_coeff_x + last_significant_coeff_y + 4;
1233  if (max_xy < 4)
1234  col_limit = FFMIN(4, col_limit);
1235  else if (max_xy < 8)
1236  col_limit = FFMIN(8, col_limit);
1237  else if (max_xy < 12)
1238  col_limit = FFMIN(24, col_limit);
1239  s->hevcdsp.idct[log2_trafo_size - 2](coeffs, col_limit);
1240  }
1241  }
1242  }
1243  s->hevcdsp.add_residual[log2_trafo_size - 2](dst, coeffs, stride);
1244 }
1245 
1246 static int hls_transform_unit(HEVCContext *s, int x0, int y0,
1247  int xBase, int yBase, int cb_xBase, int cb_yBase,
1248  int log2_cb_size, int log2_trafo_size,
1249  int blk_idx, int cbf_luma, int cbf_cb, int cbf_cr)
1250 {
1251  HEVCLocalContext *lc = &s->HEVClc;
1252 
1253  if (lc->cu.pred_mode == MODE_INTRA) {
1254  int trafo_size = 1 << log2_trafo_size;
1255  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1256 
1257  s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
1258  if (log2_trafo_size > 2) {
1259  trafo_size = trafo_size << (s->ps.sps->hshift[1] - 1);
1260  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1261  s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 1);
1262  s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 2);
1263  } else if (blk_idx == 3) {
1264  trafo_size = trafo_size << s->ps.sps->hshift[1];
1265  ff_hevc_set_neighbour_available(s, xBase, yBase,
1266  trafo_size, trafo_size);
1267  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
1268  s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
1269  }
1270  }
1271 
1272  if (cbf_luma || cbf_cb || cbf_cr) {
1273  int scan_idx = SCAN_DIAG;
1274  int scan_idx_c = SCAN_DIAG;
1275 
1278  if (lc->tu.cu_qp_delta != 0)
1279  if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
1280  lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1281  lc->tu.is_cu_qp_delta_coded = 1;
1282 
1283  if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
1284  lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
1286  "The cu_qp_delta %d is outside the valid range "
1287  "[%d, %d].\n",
1288  lc->tu.cu_qp_delta,
1289  -(26 + s->ps.sps->qp_bd_offset / 2),
1290  (25 + s->ps.sps->qp_bd_offset / 2));
1291  return AVERROR_INVALIDDATA;
1292  }
1293 
1294  ff_hevc_set_qPy(s, x0, y0, cb_xBase, cb_yBase, log2_cb_size);
1295  }
1296 
1297  if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1298  if (lc->tu.cur_intra_pred_mode >= 6 &&
1299  lc->tu.cur_intra_pred_mode <= 14) {
1300  scan_idx = SCAN_VERT;
1301  } else if (lc->tu.cur_intra_pred_mode >= 22 &&
1302  lc->tu.cur_intra_pred_mode <= 30) {
1303  scan_idx = SCAN_HORIZ;
1304  }
1305 
1306  if (lc->pu.intra_pred_mode_c >= 6 &&
1307  lc->pu.intra_pred_mode_c <= 14) {
1308  scan_idx_c = SCAN_VERT;
1309  } else if (lc->pu.intra_pred_mode_c >= 22 &&
1310  lc->pu.intra_pred_mode_c <= 30) {
1311  scan_idx_c = SCAN_HORIZ;
1312  }
1313  }
1314 
1315  if (cbf_luma)
1316  hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1317  if (log2_trafo_size > 2) {
1318  if (cbf_cb)
1319  hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 1);
1320  if (cbf_cr)
1321  hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 2);
1322  } else if (blk_idx == 3) {
1323  if (cbf_cb)
1324  hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 1);
1325  if (cbf_cr)
1326  hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 2);
1327  }
1328  }
1329  return 0;
1330 }
1331 
1332 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1333 {
1334  int cb_size = 1 << log2_cb_size;
1335  int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
1336 
1337  int min_pu_width = s->ps.sps->min_pu_width;
1338  int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
1339  int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
1340  int i, j;
1341 
1342  for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1343  for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1344  s->is_pcm[i + j * min_pu_width] = 2;
1345 }
1346 
1347 static int hls_transform_tree(HEVCContext *s, int x0, int y0,
1348  int xBase, int yBase, int cb_xBase, int cb_yBase,
1349  int log2_cb_size, int log2_trafo_size,
1350  int trafo_depth, int blk_idx,
1351  int cbf_cb, int cbf_cr)
1352 {
1353  HEVCLocalContext *lc = &s->HEVClc;
1354  uint8_t split_transform_flag;
1355  int ret;
1356 
1357  if (lc->cu.intra_split_flag) {
1358  if (trafo_depth == 1)
1359  lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1360  } else {
1362  }
1363 
1364  if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
1365  log2_trafo_size > s->ps.sps->log2_min_tb_size &&
1366  trafo_depth < lc->cu.max_trafo_depth &&
1367  !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1368  split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1369  } else {
1370  int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
1371  lc->cu.pred_mode == MODE_INTER &&
1372  lc->cu.part_mode != PART_2Nx2N &&
1373  trafo_depth == 0;
1374 
1375  split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
1376  (lc->cu.intra_split_flag && trafo_depth == 0) ||
1377  inter_split;
1378  }
1379 
1380  if (log2_trafo_size > 2 && (trafo_depth == 0 || cbf_cb))
1381  cbf_cb = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1382  else if (log2_trafo_size > 2 || trafo_depth == 0)
1383  cbf_cb = 0;
1384  if (log2_trafo_size > 2 && (trafo_depth == 0 || cbf_cr))
1385  cbf_cr = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1386  else if (log2_trafo_size > 2 || trafo_depth == 0)
1387  cbf_cr = 0;
1388 
1389  if (split_transform_flag) {
1390  const int trafo_size_split = 1 << (log2_trafo_size - 1);
1391  const int x1 = x0 + trafo_size_split;
1392  const int y1 = y0 + trafo_size_split;
1393 
1394 #define SUBDIVIDE(x, y, idx) \
1395 do { \
1396  ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1397  log2_trafo_size - 1, trafo_depth + 1, idx, \
1398  cbf_cb, cbf_cr); \
1399  if (ret < 0) \
1400  return ret; \
1401 } while (0)
1402 
1403  SUBDIVIDE(x0, y0, 0);
1404  SUBDIVIDE(x1, y0, 1);
1405  SUBDIVIDE(x0, y1, 2);
1406  SUBDIVIDE(x1, y1, 3);
1407 
1408 #undef SUBDIVIDE
1409  } else {
1410  int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
1411  int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
1412  int min_tu_width = s->ps.sps->min_tb_width;
1413  int cbf_luma = 1;
1414 
1415  if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1416  cbf_cb || cbf_cr)
1417  cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1418 
1419  ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1420  log2_cb_size, log2_trafo_size,
1421  blk_idx, cbf_luma, cbf_cb, cbf_cr);
1422  if (ret < 0)
1423  return ret;
1424  // TODO: store cbf_luma somewhere else
1425  if (cbf_luma) {
1426  int i, j;
1427  for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1428  for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1429  int x_tu = (x0 + j) >> log2_min_tu_size;
1430  int y_tu = (y0 + i) >> log2_min_tu_size;
1431  s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1432  }
1433  }
1435  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
1438  set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1439  }
1440  }
1441  return 0;
1442 }
1443 
1444 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1445 {
1446  //TODO: non-4:2:0 support
1447  HEVCLocalContext *lc = &s->HEVClc;
1448  GetBitContext gb;
1449  int cb_size = 1 << log2_cb_size;
1450  int stride0 = s->frame->linesize[0];
1451  uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
1452  int stride1 = s->frame->linesize[1];
1453  uint8_t *dst1 = &s->frame->data[1][(y0 >> s->ps.sps->vshift[1]) * stride1 + ((x0 >> s->ps.sps->hshift[1]) << s->ps.sps->pixel_shift)];
1454  int stride2 = s->frame->linesize[2];
1455  uint8_t *dst2 = &s->frame->data[2][(y0 >> s->ps.sps->vshift[2]) * stride2 + ((x0 >> s->ps.sps->hshift[2]) << s->ps.sps->pixel_shift)];
1456 
1457  int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth + ((cb_size * cb_size) >> 1) * s->ps.sps->pcm.bit_depth_chroma;
1458  const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1459  int ret;
1460 
1462  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
1463 
1464  ret = init_get_bits(&gb, pcm, length);
1465  if (ret < 0)
1466  return ret;
1467 
1468  s->hevcdsp.put_pcm(dst0, stride0, cb_size, &gb, s->ps.sps->pcm.bit_depth);
1469  s->hevcdsp.put_pcm(dst1, stride1, cb_size / 2, &gb, s->ps.sps->pcm.bit_depth_chroma);
1470  s->hevcdsp.put_pcm(dst2, stride2, cb_size / 2, &gb, s->ps.sps->pcm.bit_depth_chroma);
1471  return 0;
1472 }
1473 
1474 static void hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
1475 {
1476  HEVCLocalContext *lc = &s->HEVClc;
1479 
1480  if (x)
1482  if (y)
1484 
1485  switch (x) {
1486  case 2: lc->pu.mvd.x = ff_hevc_mvd_decode(s); break;
1487  case 1: lc->pu.mvd.x = ff_hevc_mvd_sign_flag_decode(s); break;
1488  case 0: lc->pu.mvd.x = 0; break;
1489  }
1490 
1491  switch (y) {
1492  case 2: lc->pu.mvd.y = ff_hevc_mvd_decode(s); break;
1493  case 1: lc->pu.mvd.y = ff_hevc_mvd_sign_flag_decode(s); break;
1494  case 0: lc->pu.mvd.y = 0; break;
1495  }
1496 }
1497 
1511 static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride,
1512  AVFrame *ref, const Mv *mv, int x_off, int y_off,
1513  int block_w, int block_h, int pred_idx)
1514 {
1515  HEVCLocalContext *lc = &s->HEVClc;
1516  uint8_t *src = ref->data[0];
1517  ptrdiff_t srcstride = ref->linesize[0];
1518  int pic_width = s->ps.sps->width;
1519  int pic_height = s->ps.sps->height;
1520 
1521  int mx = mv->x & 3;
1522  int my = mv->y & 3;
1523  int extra_left = ff_hevc_qpel_extra_before[mx];
1524  int extra_top = ff_hevc_qpel_extra_before[my];
1525 
1526  x_off += mv->x >> 2;
1527  y_off += mv->y >> 2;
1528  src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1529 
1530  if (x_off < extra_left || y_off < extra_top ||
1531  x_off >= pic_width - block_w - ff_hevc_qpel_extra_after[mx] ||
1532  y_off >= pic_height - block_h - ff_hevc_qpel_extra_after[my]) {
1533  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1534  int offset = extra_top * srcstride + (extra_left << s->ps.sps->pixel_shift);
1535  int buf_offset = extra_top *
1536  edge_emu_stride + (extra_left << s->ps.sps->pixel_shift);
1537 
1538  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1539  edge_emu_stride, srcstride,
1540  block_w + ff_hevc_qpel_extra[mx],
1541  block_h + ff_hevc_qpel_extra[my],
1542  x_off - extra_left, y_off - extra_top,
1543  pic_width, pic_height);
1544  src = lc->edge_emu_buffer + buf_offset;
1545  srcstride = edge_emu_stride;
1546  }
1547  s->hevcdsp.put_hevc_qpel[!!my][!!mx][pred_idx](dst, dststride, src, srcstride,
1548  block_h, mx, my, lc->mc_buffer);
1549 }
1550 
1565 static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2,
1566  ptrdiff_t dststride, AVFrame *ref, const Mv *mv,
1567  int x_off, int y_off, int block_w, int block_h, int pred_idx)
1568 {
1569  HEVCLocalContext *lc = &s->HEVClc;
1570  uint8_t *src1 = ref->data[1];
1571  uint8_t *src2 = ref->data[2];
1572  ptrdiff_t src1stride = ref->linesize[1];
1573  ptrdiff_t src2stride = ref->linesize[2];
1574  int pic_width = s->ps.sps->width >> 1;
1575  int pic_height = s->ps.sps->height >> 1;
1576 
1577  int mx = mv->x & 7;
1578  int my = mv->y & 7;
1579 
1580  x_off += mv->x >> 3;
1581  y_off += mv->y >> 3;
1582  src1 += y_off * src1stride + (x_off * (1 << s->ps.sps->pixel_shift));
1583  src2 += y_off * src2stride + (x_off * (1 << s->ps.sps->pixel_shift));
1584 
1585  if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1586  x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1587  y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1588  const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1589  int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
1590  int buf_offset1 = EPEL_EXTRA_BEFORE *
1591  (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1592  int offset2 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
1593  int buf_offset2 = EPEL_EXTRA_BEFORE *
1594  (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1595 
1596  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1597  edge_emu_stride, src1stride,
1598  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1599  x_off - EPEL_EXTRA_BEFORE,
1600  y_off - EPEL_EXTRA_BEFORE,
1601  pic_width, pic_height);
1602 
1603  src1 = lc->edge_emu_buffer + buf_offset1;
1604  src1stride = edge_emu_stride;
1605  s->hevcdsp.put_hevc_epel[!!my][!!mx][pred_idx](dst1, dststride, src1, src1stride,
1606  block_h, mx, my, lc->mc_buffer);
1607 
1608  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src2 - offset2,
1609  edge_emu_stride, src2stride,
1610  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1611  x_off - EPEL_EXTRA_BEFORE,
1612  y_off - EPEL_EXTRA_BEFORE,
1613  pic_width, pic_height);
1614  src2 = lc->edge_emu_buffer + buf_offset2;
1615  src2stride = edge_emu_stride;
1616 
1617  s->hevcdsp.put_hevc_epel[!!my][!!mx][pred_idx](dst2, dststride, src2, src2stride,
1618  block_h, mx, my, lc->mc_buffer);
1619  } else {
1620  s->hevcdsp.put_hevc_epel[!!my][!!mx][pred_idx](dst1, dststride, src1, src1stride,
1621  block_h, mx, my, lc->mc_buffer);
1622  s->hevcdsp.put_hevc_epel[!!my][!!mx][pred_idx](dst2, dststride, src2, src2stride,
1623  block_h, mx, my, lc->mc_buffer);
1624  }
1625 }
1626 
1628  const Mv *mv, int y0, int height)
1629 {
1630  int y = (mv->y >> 2) + y0 + height + 9;
1631  ff_thread_await_progress(&ref->tf, y, 0);
1632 }
1633 
1634 static void hevc_luma_mv_mpv_mode(HEVCContext *s, int x0, int y0, int nPbW,
1635  int nPbH, int log2_cb_size, int part_idx,
1636  int merge_idx, MvField *mv)
1637 {
1638  HEVCLocalContext *lc = &s->HEVClc;
1639  enum InterPredIdc inter_pred_idc = PRED_L0;
1640  int mvp_flag;
1641 
1642  ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1643  if (s->sh.slice_type == B_SLICE)
1644  inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1645 
1646  if (inter_pred_idc != PRED_L1) {
1647  if (s->sh.nb_refs[L0])
1648  mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1649 
1650  mv->pred_flag[0] = 1;
1651  hls_mvd_coding(s, x0, y0, 0);
1652  mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1653  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1654  part_idx, merge_idx, mv, mvp_flag, 0);
1655  mv->mv[0].x += lc->pu.mvd.x;
1656  mv->mv[0].y += lc->pu.mvd.y;
1657  }
1658 
1659  if (inter_pred_idc != PRED_L0) {
1660  if (s->sh.nb_refs[L1])
1661  mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1662 
1663  if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1664  AV_ZERO32(&lc->pu.mvd);
1665  } else {
1666  hls_mvd_coding(s, x0, y0, 1);
1667  }
1668 
1669  mv->pred_flag[1] = 1;
1670  mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1671  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1672  part_idx, merge_idx, mv, mvp_flag, 1);
1673  mv->mv[1].x += lc->pu.mvd.x;
1674  mv->mv[1].y += lc->pu.mvd.y;
1675  }
1676 }
1677 
1678 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1679  int nPbW, int nPbH,
1680  int log2_cb_size, int partIdx)
1681 {
1682  static const int pred_indices[] = {
1683  [4] = 0, [8] = 1, [12] = 2, [16] = 3, [24] = 4, [32] = 5, [48] = 6, [64] = 7,
1684  };
1685  const int pred_idx = pred_indices[nPbW];
1686 
1687 #define POS(c_idx, x, y) \
1688  &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1689  (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1690  HEVCLocalContext *lc = &s->HEVClc;
1691  int merge_idx = 0;
1692  struct MvField current_mv = {{{ 0 }}};
1693 
1694  int min_pu_width = s->ps.sps->min_pu_width;
1695 
1696  MvField *tab_mvf = s->ref->tab_mvf;
1697  RefPicList *refPicList = s->ref->refPicList;
1698  HEVCFrame *ref0, *ref1;
1699 
1700  int tmpstride = MAX_PB_SIZE * sizeof(int16_t);
1701 
1702  uint8_t *dst0 = POS(0, x0, y0);
1703  uint8_t *dst1 = POS(1, x0, y0);
1704  uint8_t *dst2 = POS(2, x0, y0);
1705  int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1706  int min_cb_width = s->ps.sps->min_cb_width;
1707  int x_cb = x0 >> log2_min_cb_size;
1708  int y_cb = y0 >> log2_min_cb_size;
1709  int x_pu, y_pu;
1710  int i, j;
1711 
1712  int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1713 
1714  if (!skip_flag)
1716 
1717  if (skip_flag || lc->pu.merge_flag) {
1718  if (s->sh.max_num_merge_cand > 1)
1719  merge_idx = ff_hevc_merge_idx_decode(s);
1720  else
1721  merge_idx = 0;
1722 
1723  ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1724  partIdx, merge_idx, &current_mv);
1725  } else {
1726  hevc_luma_mv_mpv_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1727  partIdx, merge_idx, &current_mv);
1728  }
1729 
1730  x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1731  y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1732 
1733  for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1734  for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1735  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1736 
1737  if (current_mv.pred_flag[0]) {
1738  ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1739  if (!ref0)
1740  return;
1741  hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
1742  }
1743  if (current_mv.pred_flag[1]) {
1744  ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1745  if (!ref1)
1746  return;
1747  hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
1748  }
1749 
1750  if (current_mv.pred_flag[0] && !current_mv.pred_flag[1]) {
1751  DECLARE_ALIGNED(16, int16_t, tmp[MAX_PB_SIZE * MAX_PB_SIZE]);
1752  DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1753 
1754  luma_mc(s, tmp, tmpstride, ref0->frame,
1755  &current_mv.mv[0], x0, y0, nPbW, nPbH, pred_idx);
1756 
1757  if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1758  (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1760  s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1761  s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1762  dst0, s->frame->linesize[0], tmp,
1763  tmpstride, nPbH);
1764  } else {
1765  s->hevcdsp.put_unweighted_pred[pred_idx](dst0, s->frame->linesize[0], tmp, tmpstride, nPbH);
1766  }
1767  chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1768  &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2, pred_idx);
1769 
1770  if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1771  (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1773  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1774  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1775  dst1, s->frame->linesize[1], tmp, tmpstride,
1776  nPbH / 2);
1778  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1779  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1780  dst2, s->frame->linesize[2], tmp2, tmpstride,
1781  nPbH / 2);
1782  } else {
1783  s->hevcdsp.put_unweighted_pred_chroma[pred_idx](dst1, s->frame->linesize[1], tmp, tmpstride, nPbH / 2);
1784  s->hevcdsp.put_unweighted_pred_chroma[pred_idx](dst2, s->frame->linesize[2], tmp2, tmpstride, nPbH / 2);
1785  }
1786  } else if (!current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1787  DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1788  DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1789 
1790  luma_mc(s, tmp, tmpstride, ref1->frame,
1791  &current_mv.mv[1], x0, y0, nPbW, nPbH, pred_idx);
1792 
1793  if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1794  (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1796  s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1797  s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1798  dst0, s->frame->linesize[0], tmp, tmpstride,
1799  nPbH);
1800  } else {
1801  s->hevcdsp.put_unweighted_pred[pred_idx](dst0, s->frame->linesize[0], tmp, tmpstride, nPbH);
1802  }
1803 
1804  chroma_mc(s, tmp, tmp2, tmpstride, ref1->frame,
1805  &current_mv.mv[1], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2, pred_idx);
1806 
1807  if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1808  (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1810  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1811  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1812  dst1, s->frame->linesize[1], tmp, tmpstride, nPbH/2);
1814  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1815  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1816  dst2, s->frame->linesize[2], tmp2, tmpstride, nPbH/2);
1817  } else {
1818  s->hevcdsp.put_unweighted_pred_chroma[pred_idx](dst1, s->frame->linesize[1], tmp, tmpstride, nPbH / 2);
1819  s->hevcdsp.put_unweighted_pred_chroma[pred_idx](dst2, s->frame->linesize[2], tmp2, tmpstride, nPbH / 2);
1820  }
1821  } else if (current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1822  DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1823  DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1824  DECLARE_ALIGNED(16, int16_t, tmp3[MAX_PB_SIZE * MAX_PB_SIZE]);
1825  DECLARE_ALIGNED(16, int16_t, tmp4[MAX_PB_SIZE * MAX_PB_SIZE]);
1826 
1827  luma_mc(s, tmp, tmpstride, ref0->frame,
1828  &current_mv.mv[0], x0, y0, nPbW, nPbH, pred_idx);
1829  luma_mc(s, tmp2, tmpstride, ref1->frame,
1830  &current_mv.mv[1], x0, y0, nPbW, nPbH, pred_idx);
1831 
1832  if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1833  (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1835  s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1836  s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1837  s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1838  s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1839  dst0, s->frame->linesize[0],
1840  tmp, tmp2, tmpstride, nPbH);
1841  } else {
1842  s->hevcdsp.put_unweighted_pred_avg[pred_idx](dst0, s->frame->linesize[0],
1843  tmp, tmp2, tmpstride, nPbH);
1844  }
1845 
1846  chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1847  &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2, pred_idx);
1848  chroma_mc(s, tmp3, tmp4, tmpstride, ref1->frame,
1849  &current_mv.mv[1], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2, pred_idx);
1850 
1851  if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1852  (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1854  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1855  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1856  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1857  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1858  dst1, s->frame->linesize[1], tmp, tmp3,
1859  tmpstride, nPbH / 2);
1861  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1862  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1863  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1864  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1865  dst2, s->frame->linesize[2], tmp2, tmp4,
1866  tmpstride, nPbH / 2);
1867  } else {
1868  s->hevcdsp.put_unweighted_pred_avg_chroma[pred_idx](dst1, s->frame->linesize[1], tmp, tmp3, tmpstride, nPbH/2);
1869  s->hevcdsp.put_unweighted_pred_avg_chroma[pred_idx](dst2, s->frame->linesize[2], tmp2, tmp4, tmpstride, nPbH/2);
1870  }
1871  }
1872 }
1873 
1877 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1878  int prev_intra_luma_pred_flag)
1879 {
1880  HEVCLocalContext *lc = &s->HEVClc;
1881  int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1882  int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1883  int min_pu_width = s->ps.sps->min_pu_width;
1884  int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
1885  int x0b = x0 & ((1 << s->ps.sps->log2_ctb_size) - 1);
1886  int y0b = y0 & ((1 << s->ps.sps->log2_ctb_size) - 1);
1887 
1888  int cand_up = (lc->ctb_up_flag || y0b) ?
1889  s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1890  int cand_left = (lc->ctb_left_flag || x0b) ?
1891  s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1892 
1893  int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
1894 
1895  MvField *tab_mvf = s->ref->tab_mvf;
1896  int intra_pred_mode;
1897  int candidate[3];
1898  int i, j;
1899 
1900  // intra_pred_mode prediction does not cross vertical CTB boundaries
1901  if ((y0 - 1) < y_ctb)
1902  cand_up = INTRA_DC;
1903 
1904  if (cand_left == cand_up) {
1905  if (cand_left < 2) {
1906  candidate[0] = INTRA_PLANAR;
1907  candidate[1] = INTRA_DC;
1908  candidate[2] = INTRA_ANGULAR_26;
1909  } else {
1910  candidate[0] = cand_left;
1911  candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1912  candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1913  }
1914  } else {
1915  candidate[0] = cand_left;
1916  candidate[1] = cand_up;
1917  if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1918  candidate[2] = INTRA_PLANAR;
1919  } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1920  candidate[2] = INTRA_DC;
1921  } else {
1922  candidate[2] = INTRA_ANGULAR_26;
1923  }
1924  }
1925 
1926  if (prev_intra_luma_pred_flag) {
1927  intra_pred_mode = candidate[lc->pu.mpm_idx];
1928  } else {
1929  if (candidate[0] > candidate[1])
1930  FFSWAP(uint8_t, candidate[0], candidate[1]);
1931  if (candidate[0] > candidate[2])
1932  FFSWAP(uint8_t, candidate[0], candidate[2]);
1933  if (candidate[1] > candidate[2])
1934  FFSWAP(uint8_t, candidate[1], candidate[2]);
1935 
1936  intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1937  for (i = 0; i < 3; i++)
1938  if (intra_pred_mode >= candidate[i])
1939  intra_pred_mode++;
1940  }
1941 
1942  /* write the intra prediction units into the mv array */
1943  if (!size_in_pus)
1944  size_in_pus = 1;
1945  for (i = 0; i < size_in_pus; i++) {
1946  memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1947  intra_pred_mode, size_in_pus);
1948 
1949  for (j = 0; j < size_in_pus; j++) {
1950  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].is_intra = 1;
1951  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[0] = 0;
1952  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[1] = 0;
1953  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[0] = 0;
1954  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[1] = 0;
1955  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].x = 0;
1956  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].y = 0;
1957  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].x = 0;
1958  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].y = 0;
1959  }
1960  }
1961 
1962  return intra_pred_mode;
1963 }
1964 
1965 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1966  int log2_cb_size, int ct_depth)
1967 {
1968  int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
1969  int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
1970  int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
1971  int y;
1972 
1973  for (y = 0; y < length; y++)
1974  memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
1975  ct_depth, length);
1976 }
1977 
1978 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1979  int log2_cb_size)
1980 {
1981  HEVCLocalContext *lc = &s->HEVClc;
1982  static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1983  uint8_t prev_intra_luma_pred_flag[4];
1984  int split = lc->cu.part_mode == PART_NxN;
1985  int pb_size = (1 << log2_cb_size) >> split;
1986  int side = split + 1;
1987  int chroma_mode;
1988  int i, j;
1989 
1990  for (i = 0; i < side; i++)
1991  for (j = 0; j < side; j++)
1992  prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1993 
1994  for (i = 0; i < side; i++) {
1995  for (j = 0; j < side; j++) {
1996  if (prev_intra_luma_pred_flag[2 * i + j])
1998  else
2000 
2001  lc->pu.intra_pred_mode[2 * i + j] =
2002  luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
2003  prev_intra_luma_pred_flag[2 * i + j]);
2004  }
2005  }
2006 
2007  chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2008  if (chroma_mode != 4) {
2009  if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2010  lc->pu.intra_pred_mode_c = 34;
2011  else
2012  lc->pu.intra_pred_mode_c = intra_chroma_table[chroma_mode];
2013  } else {
2014  lc->pu.intra_pred_mode_c = lc->pu.intra_pred_mode[0];
2015  }
2016 }
2017 
2019  int x0, int y0,
2020  int log2_cb_size)
2021 {
2022  HEVCLocalContext *lc = &s->HEVClc;
2023  int pb_size = 1 << log2_cb_size;
2024  int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
2025  int min_pu_width = s->ps.sps->min_pu_width;
2026  MvField *tab_mvf = s->ref->tab_mvf;
2027  int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
2028  int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
2029  int j, k;
2030 
2031  if (size_in_pus == 0)
2032  size_in_pus = 1;
2033  for (j = 0; j < size_in_pus; j++) {
2034  memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2035  for (k = 0; k < size_in_pus; k++)
2036  tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].is_intra = lc->cu.pred_mode == MODE_INTRA;
2037  }
2038 }
2039 
2040 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2041 {
2042  int cb_size = 1 << log2_cb_size;
2043  HEVCLocalContext *lc = &s->HEVClc;
2044  int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2045  int length = cb_size >> log2_min_cb_size;
2046  int min_cb_width = s->ps.sps->min_cb_width;
2047  int x_cb = x0 >> log2_min_cb_size;
2048  int y_cb = y0 >> log2_min_cb_size;
2049  int x, y, ret;
2050 
2051  lc->cu.x = x0;
2052  lc->cu.y = y0;
2053  lc->cu.pred_mode = MODE_INTRA;
2054  lc->cu.part_mode = PART_2Nx2N;
2055  lc->cu.intra_split_flag = 0;
2056 
2057  SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2058  for (x = 0; x < 4; x++)
2059  lc->pu.intra_pred_mode[x] = 1;
2062  if (lc->cu.cu_transquant_bypass_flag)
2063  set_deblocking_bypass(s, x0, y0, log2_cb_size);
2064  } else
2065  lc->cu.cu_transquant_bypass_flag = 0;
2066 
2067  if (s->sh.slice_type != I_SLICE) {
2068  uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2069 
2070  x = y_cb * min_cb_width + x_cb;
2071  for (y = 0; y < length; y++) {
2072  memset(&s->skip_flag[x], skip_flag, length);
2073  x += min_cb_width;
2074  }
2075  lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2076  }
2077 
2078  if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2079  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
2080  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2081 
2083  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2084  } else {
2085  int pcm_flag = 0;
2086 
2087  if (s->sh.slice_type != I_SLICE)
2089  if (lc->cu.pred_mode != MODE_INTRA ||
2090  log2_cb_size == s->ps.sps->log2_min_cb_size) {
2091  lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
2092  lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2093  lc->cu.pred_mode == MODE_INTRA;
2094  }
2095 
2096  if (lc->cu.pred_mode == MODE_INTRA) {
2097  if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2098  log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2099  log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2100  pcm_flag = ff_hevc_pcm_flag_decode(s);
2101  }
2102  if (pcm_flag) {
2103  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2104  ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2106  set_deblocking_bypass(s, x0, y0, log2_cb_size);
2107 
2108  if (ret < 0)
2109  return ret;
2110  } else {
2111  intra_prediction_unit(s, x0, y0, log2_cb_size);
2112  }
2113  } else {
2114  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2115  switch (lc->cu.part_mode) {
2116  case PART_2Nx2N:
2117  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
2118  break;
2119  case PART_2NxN:
2120  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0);
2121  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1);
2122  break;
2123  case PART_Nx2N:
2124  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0);
2125  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1);
2126  break;
2127  case PART_2NxnU:
2128  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0);
2129  hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1);
2130  break;
2131  case PART_2NxnD:
2132  hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0);
2133  hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1);
2134  break;
2135  case PART_nLx2N:
2136  hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0);
2137  hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1);
2138  break;
2139  case PART_nRx2N:
2140  hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0);
2141  hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1);
2142  break;
2143  case PART_NxN:
2144  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0);
2145  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1);
2146  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2);
2147  hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3);
2148  break;
2149  }
2150  }
2151 
2152  if (!pcm_flag) {
2153  int rqt_root_cbf = 1;
2154 
2155  if (lc->cu.pred_mode != MODE_INTRA &&
2156  !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2157  rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2158  }
2159  if (rqt_root_cbf) {
2160  lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2163  ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2164  log2_cb_size,
2165  log2_cb_size, 0, 0, 0, 0);
2166  if (ret < 0)
2167  return ret;
2168  } else {
2170  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2171  }
2172  }
2173  }
2174 
2176  ff_hevc_set_qPy(s, x0, y0, x0, y0, log2_cb_size);
2177 
2178  x = y_cb * min_cb_width + x_cb;
2179  for (y = 0; y < length; y++) {
2180  memset(&s->qp_y_tab[x], lc->qp_y, length);
2181  x += min_cb_width;
2182  }
2183 
2184  set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
2185 
2186  return 0;
2187 }
2188 
2189 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2190  int log2_cb_size, int cb_depth)
2191 {
2192  HEVCLocalContext *lc = &s->HEVClc;
2193  const int cb_size = 1 << log2_cb_size;
2194  int split_cu;
2195 
2196  lc->ct.depth = cb_depth;
2197  if (x0 + cb_size <= s->ps.sps->width &&
2198  y0 + cb_size <= s->ps.sps->height &&
2199  log2_cb_size > s->ps.sps->log2_min_cb_size) {
2200  split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2201  } else {
2202  split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2203  }
2204  if (s->ps.pps->cu_qp_delta_enabled_flag &&
2205  log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2206  lc->tu.is_cu_qp_delta_coded = 0;
2207  lc->tu.cu_qp_delta = 0;
2208  }
2209 
2210  if (split_cu) {
2211  const int cb_size_split = cb_size >> 1;
2212  const int x1 = x0 + cb_size_split;
2213  const int y1 = y0 + cb_size_split;
2214 
2215  log2_cb_size--;
2216  cb_depth++;
2217 
2218 #define SUBDIVIDE(x, y) \
2219 do { \
2220  if (x < s->ps.sps->width && y < s->ps.sps->height) { \
2221  int ret = hls_coding_quadtree(s, x, y, log2_cb_size, cb_depth);\
2222  if (ret < 0) \
2223  return ret; \
2224  } \
2225 } while (0)
2226 
2227  SUBDIVIDE(x0, y0);
2228  SUBDIVIDE(x1, y0);
2229  SUBDIVIDE(x0, y1);
2230  SUBDIVIDE(x1, y1);
2231  } else {
2232  int ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2233  if (ret < 0)
2234  return ret;
2235  }
2236 
2237  return 0;
2238 }
2239 
2240 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2241  int ctb_addr_ts)
2242 {
2243  HEVCLocalContext *lc = &s->HEVClc;
2244  int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2245  int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2246  int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2247 
2248  s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2249 
2251  if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2252  lc->first_qp_group = 1;
2253  lc->end_of_tiles_x = s->ps.sps->width;
2254  } else if (s->ps.pps->tiles_enabled_flag) {
2255  if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2256  int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2257  lc->start_of_tiles_x = x_ctb;
2258  lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2259  lc->first_qp_group = 1;
2260  }
2261  } else {
2262  lc->end_of_tiles_x = s->ps.sps->width;
2263  }
2264 
2265  lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2266 
2267  lc->boundary_flags = 0;
2268  if (s->ps.pps->tiles_enabled_flag) {
2269  if (x_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
2271  if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2273  if (y_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->ps.sps->ctb_width]])
2275  if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2277  } else {
2278  if (!ctb_addr_in_slice)
2280  if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2282  }
2283 
2284  lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2285  lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2286  lc->ctb_up_right_flag = ((y_ctb > 0) && (ctb_addr_in_slice+1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - s->ps.sps->ctb_width]]));
2287  lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->ps.sps->ctb_width]]));
2288 }
2289 
2291 {
2292  int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2293  int more_data = 1;
2294  int x_ctb = 0;
2295  int y_ctb = 0;
2296  int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2297  int ret;
2298 
2299  while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2300  int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2301 
2302  x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2303  y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2304  hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2305 
2306  ff_hevc_cabac_init(s, ctb_addr_ts);
2307 
2308  hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2309 
2310  s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2311  s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2313 
2314  ret = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2315  if (ret < 0)
2316  return ret;
2317  more_data = !ff_hevc_end_of_slice_flag_decode(s);
2318 
2319  ctb_addr_ts++;
2320  ff_hevc_save_states(s, ctb_addr_ts);
2321  ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2322  }
2323 
2324  if (x_ctb + ctb_size >= s->ps.sps->width &&
2325  y_ctb + ctb_size >= s->ps.sps->height)
2326  ff_hevc_hls_filter(s, x_ctb, y_ctb);
2327 
2328  return ctb_addr_ts;
2329 }
2330 
2332 {
2333  int min_pu_size = 1 << s->ps.sps->log2_min_pu_size;
2334  int x, y, c_idx;
2335 
2336  for (c_idx = 0; c_idx < 3; c_idx++) {
2337  ptrdiff_t stride = s->frame->linesize[c_idx];
2338  int hshift = s->ps.sps->hshift[c_idx];
2339  int vshift = s->ps.sps->vshift[c_idx];
2340  for (y = 0; y < s->ps.sps->min_pu_height; y++) {
2341  for (x = 0; x < s->ps.sps->min_pu_width; x++) {
2342  if (s->is_pcm[y * s->ps.sps->min_pu_width + x]) {
2343  int n;
2344  int len = min_pu_size >> hshift;
2345  uint8_t *src = &s->frame->data[c_idx][((y << s->ps.sps->log2_min_pu_size) >> vshift) * stride + (((x << s->ps.sps->log2_min_pu_size) >> hshift) << s->ps.sps->pixel_shift)];
2346  uint8_t *dst = &s->sao_frame->data[c_idx][((y << s->ps.sps->log2_min_pu_size) >> vshift) * stride + (((x << s->ps.sps->log2_min_pu_size) >> hshift) << s->ps.sps->pixel_shift)];
2347  for (n = 0; n < (min_pu_size >> vshift); n++) {
2348  memcpy(dst, src, len);
2349  src += stride;
2350  dst += stride;
2351  }
2352  }
2353  }
2354  }
2355  }
2356 }
2357 
2359 {
2360  AVFrame *out = s->ref->frame;
2361 
2362  if (s->sei_frame_packing_present &&
2365  s->content_interpretation_type > 0 &&
2366  s->content_interpretation_type < 3) {
2368  if (!stereo)
2369  return AVERROR(ENOMEM);
2370 
2371  switch (s->frame_packing_arrangement_type) {
2372  case 3:
2373  if (s->quincunx_subsampling)
2375  else
2376  stereo->type = AV_STEREO3D_SIDEBYSIDE;
2377  break;
2378  case 4:
2379  stereo->type = AV_STEREO3D_TOPBOTTOM;
2380  break;
2381  case 5:
2382  stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2383  break;
2384  }
2385 
2386  if (s->content_interpretation_type == 2)
2387  stereo->flags = AV_STEREO3D_FLAG_INVERT;
2388  }
2389 
2391  (s->sei_anticlockwise_rotation || s->sei_hflip || s->sei_vflip)) {
2392  double angle = s->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
2393  AVFrameSideData *rotation = av_frame_new_side_data(out,
2395  sizeof(int32_t) * 9);
2396  if (!rotation)
2397  return AVERROR(ENOMEM);
2398 
2399  av_display_rotation_set((int32_t *)rotation->data, angle);
2400  av_display_matrix_flip((int32_t *)rotation->data,
2401  s->sei_hflip, s->sei_vflip);
2402  }
2403 
2404  return 0;
2405 }
2406 
2408 {
2409  HEVCLocalContext *lc = &s->HEVClc;
2410  int ret;
2411 
2412  memset(s->horizontal_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2413  memset(s->vertical_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2414  memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2415  memset(s->is_pcm, 0, s->ps.sps->min_pu_width * s->ps.sps->min_pu_height);
2416 
2417  lc->start_of_tiles_x = 0;
2418  s->is_decoded = 0;
2419  s->first_nal_type = s->nal_unit_type;
2420 
2421  if (s->ps.pps->tiles_enabled_flag)
2422  lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2423 
2424  ret = ff_hevc_set_new_ref(s, s->ps.sps->sao_enabled ? &s->sao_frame : &s->frame,
2425  s->poc);
2426  if (ret < 0)
2427  goto fail;
2428 
2429  ret = ff_hevc_frame_rps(s);
2430  if (ret < 0) {
2431  av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2432  goto fail;
2433  }
2434 
2435  s->ref->frame->key_frame = IS_IRAP(s);
2436 
2437  ret = set_side_data(s);
2438  if (ret < 0)
2439  goto fail;
2440 
2442  ret = ff_hevc_output_frame(s, s->output_frame, 0);
2443  if (ret < 0)
2444  goto fail;
2445 
2447 
2448  return 0;
2449 
2450 fail:
2451  if (s->ref)
2452  ff_hevc_unref_frame(s, s->ref, ~0);
2453  s->ref = NULL;
2454  return ret;
2455 }
2456 
2457 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
2458 {
2459  HEVCLocalContext *lc = &s->HEVClc;
2460  GetBitContext *gb = &lc->gb;
2461  int ctb_addr_ts, ret;
2462 
2463  *gb = nal->gb;
2464  s->nal_unit_type = nal->type;
2465  s->temporal_id = nal->temporal_id;
2466 
2467  switch (s->nal_unit_type) {
2468  case NAL_VPS:
2469  ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2470  if (ret < 0)
2471  goto fail;
2472  break;
2473  case NAL_SPS:
2474  ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
2475  s->apply_defdispwin);
2476  if (ret < 0)
2477  goto fail;
2478  break;
2479  case NAL_PPS:
2480  ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
2481  if (ret < 0)
2482  goto fail;
2483  break;
2484  case NAL_SEI_PREFIX:
2485  case NAL_SEI_SUFFIX:
2486  ret = ff_hevc_decode_nal_sei(s);
2487  if (ret < 0)
2488  goto fail;
2489  break;
2490  case NAL_TRAIL_R:
2491  case NAL_TRAIL_N:
2492  case NAL_TSA_N:
2493  case NAL_TSA_R:
2494  case NAL_STSA_N:
2495  case NAL_STSA_R:
2496  case NAL_BLA_W_LP:
2497  case NAL_BLA_W_RADL:
2498  case NAL_BLA_N_LP:
2499  case NAL_IDR_W_RADL:
2500  case NAL_IDR_N_LP:
2501  case NAL_CRA_NUT:
2502  case NAL_RADL_N:
2503  case NAL_RADL_R:
2504  case NAL_RASL_N:
2505  case NAL_RASL_R:
2506  ret = hls_slice_header(s);
2507  if (ret < 0)
2508  return ret;
2509 
2510  if (s->max_ra == INT_MAX) {
2511  if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
2512  s->max_ra = s->poc;
2513  } else {
2514  if (IS_IDR(s))
2515  s->max_ra = INT_MIN;
2516  }
2517  }
2518 
2519  if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
2520  s->poc <= s->max_ra) {
2521  s->is_decoded = 0;
2522  break;
2523  } else {
2524  if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
2525  s->max_ra = INT_MIN;
2526  }
2527 
2528  if (s->sh.first_slice_in_pic_flag) {
2529  ret = hevc_frame_start(s);
2530  if (ret < 0)
2531  return ret;
2532  } else if (!s->ref) {
2533  av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2534  goto fail;
2535  }
2536 
2537  if (s->nal_unit_type != s->first_nal_type) {
2539  "Non-matching NAL types of the VCL NALUs: %d %d\n",
2540  s->first_nal_type, s->nal_unit_type);
2541  return AVERROR_INVALIDDATA;
2542  }
2543 
2544  if (!s->sh.dependent_slice_segment_flag &&
2545  s->sh.slice_type != I_SLICE) {
2546  ret = ff_hevc_slice_rpl(s);
2547  if (ret < 0) {
2549  "Error constructing the reference lists for the current slice.\n");
2550  goto fail;
2551  }
2552  }
2553 
2554  if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
2555  ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
2556  if (ret < 0)
2557  goto fail;
2558  }
2559 
2560  if (s->avctx->hwaccel) {
2561  ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
2562  if (ret < 0)
2563  goto fail;
2564  } else {
2565  ctb_addr_ts = hls_slice_data(s);
2566  if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
2567  s->is_decoded = 1;
2570  s->ps.sps->sao_enabled)
2571  restore_tqb_pixels(s);
2572  }
2573 
2574  if (ctb_addr_ts < 0) {
2575  ret = ctb_addr_ts;
2576  goto fail;
2577  }
2578  }
2579  break;
2580  case NAL_EOS_NUT:
2581  case NAL_EOB_NUT:
2582  s->seq_decode = (s->seq_decode + 1) & 0xff;
2583  s->max_ra = INT_MAX;
2584  break;
2585  case NAL_AUD:
2586  case NAL_FD_NUT:
2587  break;
2588  default:
2589  av_log(s->avctx, AV_LOG_INFO,
2590  "Skipping NAL unit %d\n", s->nal_unit_type);
2591  }
2592 
2593  return 0;
2594 fail:
2596  return ret;
2597  return 0;
2598 }
2599 
2600 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2601 {
2602  int i, ret = 0;
2603 
2604  s->ref = NULL;
2605  s->eos = 0;
2606 
2607  /* split the input packet into NAL units, so we know the upper bound on the
2608  * number of slices in the frame */
2609  ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
2610  s->nal_length_size, s->avctx->codec_id);
2611  if (ret < 0) {
2613  "Error splitting the input into NAL units.\n");
2614  return ret;
2615  }
2616 
2617  for (i = 0; i < s->pkt.nb_nals; i++) {
2618  if (s->pkt.nals[i].type == NAL_EOB_NUT ||
2619  s->pkt.nals[i].type == NAL_EOS_NUT)
2620  s->eos = 1;
2621  }
2622 
2623  /* decode the NAL units */
2624  for (i = 0; i < s->pkt.nb_nals; i++) {
2625  ret = decode_nal_unit(s, &s->pkt.nals[i]);
2626  if (ret < 0) {
2628  "Error parsing NAL unit #%d.\n", i);
2629  goto fail;
2630  }
2631  }
2632 
2633 fail:
2634  if (s->ref)
2635  ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2636 
2637  return ret;
2638 }
2639 
2640 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
2641 {
2642  int i;
2643  for (i = 0; i < 16; i++)
2644  av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2645 }
2646 
2647 static int verify_md5(HEVCContext *s, AVFrame *frame)
2648 {
2650  int pixel_shift;
2651  int i, j;
2652 
2653  if (!desc)
2654  return AVERROR(EINVAL);
2655 
2656  pixel_shift = desc->comp[0].depth > 8;
2657 
2658  av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
2659  s->poc);
2660 
2661  /* the checksums are LE, so we have to byteswap for >8bpp formats
2662  * on BE arches */
2663 #if HAVE_BIGENDIAN
2664  if (pixel_shift && !s->checksum_buf) {
2666  FFMAX3(frame->linesize[0], frame->linesize[1],
2667  frame->linesize[2]));
2668  if (!s->checksum_buf)
2669  return AVERROR(ENOMEM);
2670  }
2671 #endif
2672 
2673  for (i = 0; frame->data[i]; i++) {
2674  int width = s->avctx->coded_width;
2675  int height = s->avctx->coded_height;
2676  int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
2677  int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
2678  uint8_t md5[16];
2679 
2680  av_md5_init(s->md5_ctx);
2681  for (j = 0; j < h; j++) {
2682  const uint8_t *src = frame->data[i] + j * frame->linesize[i];
2683 #if HAVE_BIGENDIAN
2684  if (pixel_shift) {
2685  s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
2686  (const uint16_t *) src, w);
2687  src = s->checksum_buf;
2688  }
2689 #endif
2690  av_md5_update(s->md5_ctx, src, w << pixel_shift);
2691  }
2692  av_md5_final(s->md5_ctx, md5);
2693 
2694  if (!memcmp(md5, s->md5[i], 16)) {
2695  av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
2696  print_md5(s->avctx, AV_LOG_DEBUG, md5);
2697  av_log (s->avctx, AV_LOG_DEBUG, "; ");
2698  } else {
2699  av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
2700  print_md5(s->avctx, AV_LOG_ERROR, md5);
2701  av_log (s->avctx, AV_LOG_ERROR, " != ");
2702  print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
2703  av_log (s->avctx, AV_LOG_ERROR, "\n");
2704  return AVERROR_INVALIDDATA;
2705  }
2706  }
2707 
2708  av_log(s->avctx, AV_LOG_DEBUG, "\n");
2709 
2710  return 0;
2711 }
2712 
2713 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
2714  AVPacket *avpkt)
2715 {
2716  int ret;
2717  HEVCContext *s = avctx->priv_data;
2718 
2719  if (!avpkt->size) {
2720  ret = ff_hevc_output_frame(s, data, 1);
2721  if (ret < 0)
2722  return ret;
2723 
2724  *got_output = ret;
2725  return 0;
2726  }
2727 
2728  s->ref = NULL;
2729  ret = decode_nal_units(s, avpkt->data, avpkt->size);
2730  if (ret < 0)
2731  return ret;
2732 
2733  if (avctx->hwaccel) {
2734  if (s->ref && avctx->hwaccel->end_frame(avctx) < 0)
2735  av_log(avctx, AV_LOG_ERROR,
2736  "hardware accelerator failed to decode picture\n");
2737  } else {
2738  /* verify the SEI checksum */
2739  if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
2740  s->is_md5) {
2741  ret = verify_md5(s, s->ref->frame);
2742  if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
2743  ff_hevc_unref_frame(s, s->ref, ~0);
2744  return ret;
2745  }
2746  }
2747  }
2748  s->is_md5 = 0;
2749 
2750  if (s->is_decoded) {
2751  av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
2752  s->is_decoded = 0;
2753  }
2754 
2755  if (s->output_frame->buf[0]) {
2756  av_frame_move_ref(data, s->output_frame);
2757  *got_output = 1;
2758  }
2759 
2760  return avpkt->size;
2761 }
2762 
2764 {
2765  int ret = ff_thread_ref_frame(&dst->tf, &src->tf);
2766  if (ret < 0)
2767  return ret;
2768 
2769  dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
2770  if (!dst->tab_mvf_buf)
2771  goto fail;
2772  dst->tab_mvf = src->tab_mvf;
2773 
2774  dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
2775  if (!dst->rpl_tab_buf)
2776  goto fail;
2777  dst->rpl_tab = src->rpl_tab;
2778 
2779  dst->rpl_buf = av_buffer_ref(src->rpl_buf);
2780  if (!dst->rpl_buf)
2781  goto fail;
2782 
2783  dst->poc = src->poc;
2784  dst->ctb_count = src->ctb_count;
2785  dst->window = src->window;
2786  dst->flags = src->flags;
2787  dst->sequence = src->sequence;
2788 
2789  if (src->hwaccel_picture_private) {
2791  if (!dst->hwaccel_priv_buf)
2792  goto fail;
2794  }
2795 
2796  return 0;
2797 fail:
2798  ff_hevc_unref_frame(s, dst, ~0);
2799  return AVERROR(ENOMEM);
2800 }
2801 
2803 {
2804  HEVCContext *s = avctx->priv_data;
2805  int i;
2806 
2807  pic_arrays_free(s);
2808 
2809  av_freep(&s->md5_ctx);
2810 
2811  av_frame_free(&s->tmp_frame);
2813 
2814  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2815  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2816  av_frame_free(&s->DPB[i].frame);
2817  }
2818 
2819  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
2820  av_buffer_unref(&s->ps.vps_list[i]);
2821  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
2822  av_buffer_unref(&s->ps.sps_list[i]);
2823  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
2824  av_buffer_unref(&s->ps.pps_list[i]);
2825 
2827 
2828  return 0;
2829 }
2830 
2832 {
2833  HEVCContext *s = avctx->priv_data;
2834  int i;
2835 
2836  s->avctx = avctx;
2837 
2838  s->tmp_frame = av_frame_alloc();
2839  if (!s->tmp_frame)
2840  goto fail;
2841 
2843  if (!s->output_frame)
2844  goto fail;
2845 
2846  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2847  s->DPB[i].frame = av_frame_alloc();
2848  if (!s->DPB[i].frame)
2849  goto fail;
2850  s->DPB[i].tf.f = s->DPB[i].frame;
2851  }
2852 
2853  s->max_ra = INT_MAX;
2854 
2855  s->md5_ctx = av_md5_alloc();
2856  if (!s->md5_ctx)
2857  goto fail;
2858 
2859  ff_bswapdsp_init(&s->bdsp);
2860 
2861  s->context_initialized = 1;
2862 
2863  return 0;
2864 
2865 fail:
2866  hevc_decode_free(avctx);
2867  return AVERROR(ENOMEM);
2868 }
2869 
2871  const AVCodecContext *src)
2872 {
2873  HEVCContext *s = dst->priv_data;
2874  HEVCContext *s0 = src->priv_data;
2875  int i, ret;
2876 
2877  if (!s->context_initialized) {
2878  ret = hevc_init_context(dst);
2879  if (ret < 0)
2880  return ret;
2881  }
2882 
2883  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2884  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2885  if (s0->DPB[i].frame->buf[0]) {
2886  ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
2887  if (ret < 0)
2888  return ret;
2889  }
2890  }
2891 
2892  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
2893  av_buffer_unref(&s->ps.vps_list[i]);
2894  if (s0->ps.vps_list[i]) {
2895  s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
2896  if (!s->ps.vps_list[i])
2897  return AVERROR(ENOMEM);
2898  }
2899  }
2900 
2901  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
2902  av_buffer_unref(&s->ps.sps_list[i]);
2903  if (s0->ps.sps_list[i]) {
2904  s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
2905  if (!s->ps.sps_list[i])
2906  return AVERROR(ENOMEM);
2907  }
2908  }
2909 
2910  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
2911  av_buffer_unref(&s->ps.pps_list[i]);
2912  if (s0->ps.pps_list[i]) {
2913  s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
2914  if (!s->ps.pps_list[i])
2915  return AVERROR(ENOMEM);
2916  }
2917  }
2918 
2919  if (s->ps.sps != s0->ps.sps)
2920  ret = set_sps(s, s0->ps.sps, src->pix_fmt);
2921 
2922  s->seq_decode = s0->seq_decode;
2923  s->seq_output = s0->seq_output;
2924  s->pocTid0 = s0->pocTid0;
2925  s->max_ra = s0->max_ra;
2926 
2927  s->is_nalff = s0->is_nalff;
2929 
2930  if (s0->eos) {
2931  s->seq_decode = (s->seq_decode + 1) & 0xff;
2932  s->max_ra = INT_MAX;
2933  }
2934 
2935  return 0;
2936 }
2937 
2939 {
2940  AVCodecContext *avctx = s->avctx;
2941  GetByteContext gb;
2942  int ret, i;
2943 
2944  bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
2945 
2946  if (avctx->extradata_size > 3 &&
2947  (avctx->extradata[0] || avctx->extradata[1] ||
2948  avctx->extradata[2] > 1)) {
2949  /* It seems the extradata is encoded as hvcC format.
2950  * Temporarily, we support configurationVersion==0 until 14496-15 3rd
2951  * is finalized. When finalized, configurationVersion will be 1 and we
2952  * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
2953  int i, j, num_arrays, nal_len_size;
2954 
2955  s->is_nalff = 1;
2956 
2957  bytestream2_skip(&gb, 21);
2958  nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
2959  num_arrays = bytestream2_get_byte(&gb);
2960 
2961  /* nal units in the hvcC always have length coded with 2 bytes,
2962  * so put a fake nal_length_size = 2 while parsing them */
2963  s->nal_length_size = 2;
2964 
2965  /* Decode nal units from hvcC. */
2966  for (i = 0; i < num_arrays; i++) {
2967  int type = bytestream2_get_byte(&gb) & 0x3f;
2968  int cnt = bytestream2_get_be16(&gb);
2969 
2970  for (j = 0; j < cnt; j++) {
2971  // +2 for the nal size field
2972  int nalsize = bytestream2_peek_be16(&gb) + 2;
2973  if (bytestream2_get_bytes_left(&gb) < nalsize) {
2975  "Invalid NAL unit size in extradata.\n");
2976  return AVERROR_INVALIDDATA;
2977  }
2978 
2979  ret = decode_nal_units(s, gb.buffer, nalsize);
2980  if (ret < 0) {
2981  av_log(avctx, AV_LOG_ERROR,
2982  "Decoding nal unit %d %d from hvcC failed\n",
2983  type, i);
2984  return ret;
2985  }
2986  bytestream2_skip(&gb, nalsize);
2987  }
2988  }
2989 
2990  /* Now store right nal length size, that will be used to parse
2991  * all other nals */
2992  s->nal_length_size = nal_len_size;
2993  } else {
2994  s->is_nalff = 0;
2995  ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
2996  if (ret < 0)
2997  return ret;
2998  }
2999 
3000  /* export stream parameters from the first SPS */
3001  for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3002  if (s->ps.sps_list[i]) {
3003  const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
3004  export_stream_params(s->avctx, &s->ps, sps);
3005  break;
3006  }
3007  }
3008 
3009  return 0;
3010 }
3011 
3013 {
3014  HEVCContext *s = avctx->priv_data;
3015  int ret;
3016 
3017  avctx->internal->allocate_progress = 1;
3018 
3019  ret = hevc_init_context(avctx);
3020  if (ret < 0)
3021  return ret;
3022 
3023  if (avctx->extradata_size > 0 && avctx->extradata) {
3024  ret = hevc_decode_extradata(s);
3025  if (ret < 0) {
3026  hevc_decode_free(avctx);
3027  return ret;
3028  }
3029  }
3030 
3031  return 0;
3032 }
3033 
3035 {
3036  HEVCContext *s = avctx->priv_data;
3037  int ret;
3038 
3039  memset(s, 0, sizeof(*s));
3040 
3041  ret = hevc_init_context(avctx);
3042  if (ret < 0)
3043  return ret;
3044 
3045  return 0;
3046 }
3047 
3049 {
3050  HEVCContext *s = avctx->priv_data;
3051  ff_hevc_flush_dpb(s);
3052  s->max_ra = INT_MAX;
3053 }
3054 
3055 #define OFFSET(x) offsetof(HEVCContext, x)
3056 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3057 
3058 static const AVOption options[] = {
3059  { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3060  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
3061  { NULL },
3062 };
3063 
3064 static const AVClass hevc_decoder_class = {
3065  .class_name = "HEVC decoder",
3066  .item_name = av_default_item_name,
3067  .option = options,
3068  .version = LIBAVUTIL_VERSION_INT,
3069 };
3070 
3072  .name = "hevc",
3073  .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3074  .type = AVMEDIA_TYPE_VIDEO,
3075  .id = AV_CODEC_ID_HEVC,
3076  .priv_data_size = sizeof(HEVCContext),
3077  .priv_class = &hevc_decoder_class,
3079  .close = hevc_decode_free,
3082  .update_thread_context = hevc_update_thread_context,
3083  .init_thread_copy = hevc_init_thread_copy,
3084  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3087 };
#define EDGE_EMU_BUFFER_STRIDE
Definition: hevc.h:75
static const uint8_t horiz_scan2x2_x[4]
Definition: hevc.c:48
int frame_packing_arrangement_type
Definition: hevc.h:852
uint8_t ctb_up_flag
Definition: hevc.h:737
const HEVCPPS * pps
Definition: hevc.h:538
AVFrame * frame
Definition: hevc.h:678
unsigned int log2_min_cb_size
Definition: hevc.h:437
AVRational framerate
Definition: avcodec.h:3063
int sei_frame_packing_present
frame packing arrangement variables
Definition: hevc.h:851
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int ff_hevc_merge_idx_decode(HEVCContext *s)
Definition: hevc_cabac.c:619
HEVCPredContext hpc
Definition: hevc.h:800
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int pic_order_cnt_lsb
Definition: hevc.h:551
int short_term_ref_pic_set_sps_flag
Definition: hevc.h:559
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:106
static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2, ptrdiff_t dststride, AVFrame *ref, const Mv *mv, int x_off, int y_off, int block_w, int block_h, int pred_idx)
8.5.3.2.2.2 Chroma sample interpolation process
Definition: hevc.c:1565
int ff_hevc_frame_nb_refs(HEVCContext *s)
Get the number of candidate references for the current frame.
Definition: hevc_refs.c:488
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1768
int quincunx_subsampling
Definition: hevc.h:854
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
HEVCFrame * ref
Definition: hevc.h:788
static void restore_tqb_pixels(HEVCContext *s)
Definition: hevc.c:2331
#define EPEL_EXTRA_AFTER
Definition: hevc.h:72
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
Definition: bswapdsp.h:26
Definition: hevc.h:635
int ctb_height
Definition: hevc.h:451
uint8_t is_cu_qp_delta_coded
Definition: hevc.h:665
static void hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:1474
AVOption.
Definition: opt.h:234
Views are alternated temporally.
Definition: stereo3d.h:66
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:179
int ff_hevc_merge_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:630
Definition: hevc.h:97
int ff_hevc_sao_band_position_decode(HEVCContext *s)
Definition: hevc_cabac.c:432
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1595
VideoDSPContext vdsp
Definition: hevc.h:802
static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref, const Mv *mv, int y0, int height)
Definition: hevc.c:1627
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
Views are next to each other, but when upscaling apply a checkerboard pattern.
Definition: stereo3d.h:87
Definition: hevc.h:201
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
int content_interpretation_type
Definition: hevc.h:853
AVFrame * f
Definition: thread.h:36
int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth)
Definition: hevc_cabac.c:712
void(* put_unweighted_pred_avg[8])(uint8_t *dst, ptrdiff_t dststride, int16_t *src1, int16_t *src2, ptrdiff_t srcstride, int height)
Definition: hevcdsp.h:71
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:308
int vps_id
Definition: hevc.h:386
int16_t x
horizontal component of motion vector
Definition: hevc.h:631
#define HWACCEL_MAX
const char * desc
Definition: nvenc.c:101
void * hwaccel_picture_private
Definition: hevc.h:694
static int hevc_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: hevc.c:2870
#define MAX_REFS
Definition: hevc.h:43
int sei_hflip
Definition: hevc.h:859
uint8_t nb_refs
Definition: hevc.h:274
MvField * tab_mvf
Definition: hevc.h:680
int pic_init_qp_minus26
Definition: hevc.h:475
int bs_width
Definition: hevc.h:795
int ff_hevc_end_of_slice_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:464
uint8_t intra_split_flag
IntraSplitFlag.
Definition: hevc.h:625
void(* put_hevc_epel[2][2][8])(int16_t *dst, ptrdiff_t dststride, uint8_t *src, ptrdiff_t srcstride, int height, int mx, int my, int16_t *mcbuffer)
Definition: hevcdsp.h:63
VUI vui
Definition: hevc.h:410
AVFrame * sao_frame
Definition: hevc.h:771
int rem_intra_luma_pred_mode
Definition: hevc.h:653
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2127
int vshift[3]
Definition: hevc.h:461
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:546
int size
Definition: avcodec.h:1347
static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
Definition: hevc.c:2457
int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx, int ctx_cg)
Definition: hevc_cabac.c:763
const uint8_t ff_hevc_diag_scan4x4_x[16]
Definition: hevc_data.c:25
void(* put_unweighted_pred[8])(uint8_t *dst, ptrdiff_t dststride, int16_t *src, ptrdiff_t srcstride, int height)
Definition: hevcdsp.h:67
unsigned int slice_addr
Definition: hevc.h:547
uint32_t vui_time_scale
Definition: hevc.h:322
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
uint8_t weighted_bipred_flag
Definition: hevc.h:487
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int is_nalff, int nal_length_size, enum AVCodecID codec_id)
Split an input packet into NAL units.
Definition: h2645_parse.c:214
void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
Definition: hevc_refs.c:31
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:58
void(* add_residual[4])(uint8_t *dst, int16_t *coeffs, ptrdiff_t stride)
Definition: hevcdsp.h:45
#define MAX_PB_SIZE
Definition: hevc.h:59
int tc_offset
Definition: hevc.h:670
int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s)
Definition: hevc_cabac.c:598
PredictionUnit pu
Definition: hevc.h:747
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
static void intra_prediction_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:1978
uint8_t seq_loop_filter_across_slices_enabled_flag
Definition: hevc.h:500
uint8_t cabac_init_present_flag
Definition: hevc.h:471
int16_t chroma_offset_l1[16][2]
Definition: hevc.h:608
void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
Definition: hevc_filter.c:728
Definition: hevc.h:247
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:319
int ff_hevc_frame_rps(HEVCContext *s)
Construct the reference picture sets for the current frame.
Definition: hevc_refs.c:405
#define FF_ARRAY_ELEMS(a)
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps, int apply_defdispwin)
Definition: hevc_ps.c:984
HEVCParamSets ps
Definition: hevc.h:775
int x
Definition: hevc.h:618
int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx, int inc)
Definition: hevc_cabac.c:831
int min_cb_height
Definition: hevc.h:454
int * ctb_addr_ts_to_rs
CtbAddrTSToRS.
Definition: hevc.h:524
int num_ref_idx_l0_default_active
num_ref_idx_l0_default_active_minus1 + 1
Definition: hevc.h:473
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:278
uint8_t dependent_slice_segment_flag
Definition: hevc.h:554
CABACContext cc
Definition: hevc.h:729
ShortTermRPS slice_rps
Definition: hevc.h:561
int profile
profile
Definition: avcodec.h:2880
AVBufferRef * vps_list[MAX_VPS_COUNT]
Definition: hevc.h:531
ShortTermRPS st_rps[MAX_SHORT_TERM_RPS_COUNT]
Definition: hevc.h:417
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
int width
Definition: hevc.h:448
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:91
int ff_hevc_sao_type_idx_decode(HEVCContext *s)
Definition: hevc_cabac.c:422
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:830
static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
Definition: hevc.c:381
enum NALUnitType first_nal_type
Definition: hevc.h:835
Macro definitions for various function/variable attributes.
int qp_bd_offset
Definition: hevc.h:463
void(* idct[4])(int16_t *coeffs, int col_limit)
Definition: hevcdsp.h:49
int pixel_shift
Definition: hevc.h:397
uint8_t entropy_coding_sync_enabled_flag
Definition: hevc.h:493
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int max_ra
Definition: hevc.h:794
void(* weighted_pred_chroma[8])(uint8_t denom, int16_t wlxFlag, int16_t olxFlag, uint8_t *dst, ptrdiff_t dststride, int16_t *src, ptrdiff_t srcstride, int height)
Definition: hevcdsp.h:80
int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:469
static int hls_slice_data(HEVCContext *s)
Definition: hevc.c:2290
int output_width
Definition: hevc.h:391
const uint8_t ff_hevc_qpel_extra_before[4]
Definition: hevc.c:42
static void hls_sao_param(HEVCContext *s, int rx, int ry)
Definition: hevc.c:814
AVBufferPool * rpl_tab_pool
candidate references for the current frame
Definition: hevc.h:778
CodingTree ct
Definition: hevc.h:745
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2696
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:39
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:1634
#define PAR
Definition: hevc.c:3056
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:863
int ff_hevc_last_significant_coeff_y_prefix_decode(HEVCContext *s, int c_idx, int log2_size)
Definition: hevc_cabac.c:745
int chroma_format_idc
Definition: hevc.h:387
uint8_t disable_dbf
Definition: hevc.h:504
unsigned int log2_max_trafo_size
Definition: hevc.h:440
unsigned int slice_segment_addr
address (in raster order) of the first block in the current slice
Definition: hevc.h:545
struct AVMD5 * av_md5_alloc(void)
Definition: md5.c:46
int ff_hevc_mpm_idx_decode(HEVCContext *s)
Definition: hevc_cabac.c:590
int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx, int ctx_set)
Definition: hevc_cabac.c:822
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:119
int end_of_tiles_x
Definition: hevc.h:741
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0, int x_cb, int y_cb)
Definition: hevc_cabac.c:474
InterPredIdc
Definition: hevc.h:200
float delta
AVOptions.
void(* weighted_pred_avg_chroma[8])(uint8_t denom, int16_t wl0Flag, int16_t wl1Flag, int16_t ol0Flag, int16_t ol1Flag, uint8_t *dst, ptrdiff_t dststride, int16_t *src1, int16_t *src2, ptrdiff_t srcstride, int height)
Definition: hevcdsp.h:87
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:2358
uint8_t ctb_up_right_flag
Definition: hevc.h:738
LongTermRPS long_term_rps
Definition: hevc.h:564
int poc[32]
Definition: hevc.h:272
uint8_t vps_timing_info_present_flag
Definition: hevc.h:370
int ff_hevc_abs_mvd_greater0_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:671
uint8_t matrix_coeffs
Definition: hevc.h:307
static int hls_slice_header(HEVCContext *s)
Definition: hevc.c:450
int min_tb_width
Definition: hevc.h:455
int depth
ctDepth
Definition: hevc.h:614
int num_entry_point_offsets
Definition: hevc.h:592
AVFrame * output_frame
Definition: hevc.h:773
int apply_defdispwin
Definition: hevc.h:845
SAOParams * sao
Definition: hevc.h:784
const HEVCVPS * vps
Definition: hevc.h:536
int num_ref_idx_l1_default_active
num_ref_idx_l1_default_active_minus1 + 1
Definition: hevc.h:474
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
unsigned int log2_min_pcm_cb_size
Definition: hevc.h:430
AVCodecContext * avctx
Definition: hevc.h:761
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:1246
int min_cb_width
Definition: hevc.h:453
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:323
static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:2040
static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride, AVFrame *ref, const Mv *mv, int x_off, int y_off, int block_w, int block_h, int pred_idx)
8.5.3.2.2.1 Luma sample interpolation process
Definition: hevc.c:1511
struct AVMD5 * md5
Definition: movenc.c:56
const char data[16]
Definition: mxf.c:70
uint8_t scaling_list_data_present_flag
Definition: hevc.h:508
BswapDSPContext bdsp
Definition: hevc.h:803
ThreadFrame tf
Definition: hevc.h:679
#define MAX_PPS_COUNT
Definition: h264_ps.h:38
uint8_t first_slice_in_pic_flag
Definition: hevc.h:553
uint8_t * data
Definition: avcodec.h:1346
uint8_t bit_depth_chroma
Definition: hevc.h:429
const uint8_t * buffer
Definition: bytestream.h:33
uint8_t ctb_up_left_flag
Definition: hevc.h:739
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:2655
#define MAX_TB_SIZE
Definition: hevc.h:58
int ff_hevc_decode_nal_sei(HEVCContext *s)
Definition: hevc_sei.c:184
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:146
Definition: hevc.h:202
#define EPEL_EXTRA_BEFORE
Definition: hevc.h:71
int8_t * qp_y_tab
Definition: hevc.h:804
uint8_t loop_filter_disable_flag
Definition: hevc.h:432
int ff_hevc_mvd_decode(HEVCContext *s)
Definition: hevc_cabac.c:681
static void print_md5(void *log_ctx, int level, uint8_t md5[16])
Definition: hevc.c:2640
int sei_anticlockwise_rotation
Definition: hevc.h:858
uint8_t pic_output_flag
Definition: hevc.h:555
uint8_t * tab_ct_depth
Definition: hevc.h:812
void ff_hevc_flush_dpb(HEVCContext *s)
Drop all frames currently in DPB.
Definition: hevc_refs.c:75
void(* weighted_pred_avg[8])(uint8_t denom, int16_t wl0Flag, int16_t wl1Flag, int16_t ol0Flag, int16_t ol1Flag, uint8_t *dst, ptrdiff_t dststride, int16_t *src1, int16_t *src2, ptrdiff_t srcstride, int height)
Definition: hevcdsp.h:83
static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
Definition: hevc.c:283
uint8_t cu_transquant_bypass_flag
Definition: hevc.h:627
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
static 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:489
#define FFUMOD(a, b)
Definition: hevc.h:89
uint8_t used[32]
Definition: hevc.h:273
int ff_hevc_sao_offset_sign_decode(HEVCContext *s)
Definition: hevc_cabac.c:452
int temporal_id
temporal_id_plus1 - 1
Definition: hevc.h:787
#define SET_SAO(elem, value)
Definition: hevc.c:802
uint8_t first_qp_group
Definition: hevc.h:726
void(* put_unweighted_pred_chroma[8])(uint8_t *dst, ptrdiff_t dststride, int16_t *src, ptrdiff_t srcstride, int height)
Definition: hevcdsp.h:69
HEVCDSPContext hevcdsp
Definition: hevc.h:801
int ctb_count
Definition: hevc.h:683
uint8_t no_output_of_prior_pics_flag
Definition: hevc.h:568
static void hevc_decode_flush(AVCodecContext *avctx)
Definition: hevc.c:3048
Definition: hevc.h:113
const AVProfile ff_hevc_profiles[]
Definition: profiles.c:67
int slice_idx
number of the slice being currently decoded
Definition: hevc.h:792
#define BOUNDARY_UPPER_SLICE
Definition: hevc.h:752
#define src
Definition: vp8dsp.c:254
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
uint8_t intra_pred_mode[4]
Definition: hevc.h:654
uint8_t colour_plane_id
RPS coded in the slice header itself is stored here.
Definition: hevc.h:556
const char * suffix
Definition: checkasm.c:105
void av_md5_update(AVMD5 *ctx, const uint8_t *src, const int len)
Definition: md5.c:149
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1715
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:3034
void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0, int log2_trafo_size)
Definition: hevc_filter.c:585
uint8_t slice_initialized
1 if the independent slice segment header was successfully parsed
Definition: hevc.h:768
unsigned int log2_max_poc_lsb
Definition: hevc.h:400
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:100
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:1175
int min_pu_height
Definition: hevc.h:458
AVBufferRef * rpl_tab_buf
Definition: hevc.h:690
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:161
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
uint8_t rpl_modification_flag[2]
Definition: hevc.h:567
int vui_timing_info_present_flag
Definition: hevc.h:320
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
Definition: hevc.h:187
RefPicList * refPicList
Definition: hevc.h:681
int16_t luma_offset_l0[16]
Definition: hevc.h:604
HEVCLocalContext HEVClc
Definition: hevc.h:763
int bs_height
Definition: hevc.h:796
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
void(* intra_pred[4])(struct HEVCContext *s, int x0, int y0, int c_idx)
Definition: hevc.h:711
int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb)
Compute POC of the current frame and return it.
Definition: hevc_refs.c:465
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:151
unsigned int log2_ctb_size
Definition: hevc.h:441
int tc_offset
tc_offset_div2 * 2
Definition: hevc.h:588
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
const ShortTermRPS * short_term_rps
Definition: hevc.h:562
uint8_t merge_flag
Definition: hevc.h:656
struct AVMD5 * md5_ctx
Definition: hevc.h:838
void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts)
Definition: hevc_cabac.c:326
int8_t slice_qp
Definition: hevc.h:594
static int verify_md5(HEVCContext *s, AVFrame *frame)
Definition: hevc.c:2647
#define FFMAX(a, b)
Definition: common.h:64
static const AVClass hevc_decoder_class
Definition: hevc.c:3064
#define fail()
Definition: checkasm.h:80
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:893
uint8_t max_trafo_depth
MaxTrafoDepth.
Definition: hevc.h:626
int raw_size
Definition: h2645_parse.h:42
uint16_t sequence
A sequence counter, so that old frames are output first after a POC reset.
Definition: hevc.h:700
uint8_t colour_primaries
Definition: hevc.h:305
uint8_t slice_temporal_mvp_enabled_flag
Definition: hevc.h:569
uint8_t * vertical_bs
Definition: hevc.h:806
static char * split(char *message, char delim)
Definition: af_channelmap.c:85
uint8_t tiles_enabled_flag
Definition: hevc.h:492
int eo_class[3]
sao_eo_class
Definition: hevcdsp.h:34
uint32_t vps_num_units_in_tick
Definition: hevc.h:371
static av_cold int hevc_init_context(AVCodecContext *avctx)
Definition: hevc.c:2831
int16_t luma_weight_l0[16]
Definition: hevc.h:599
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int * col_idxX
Definition: hevc.h:521
int ff_hevc_mvd_sign_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:697
void(* put_hevc_qpel[2][2][8])(int16_t *dst, ptrdiff_t dststride, uint8_t *src, ptrdiff_t srcstride, int height, int mx, int my, int16_t *mcbuffer)
Definition: hevcdsp.h:60
int slice_qp_delta
Definition: hevc.h:583
common internal API header
int16_t mc_buffer[(64+24) *64]
Definition: hevc.h:723
int ff_hevc_mvp_lx_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:661
const HEVCSPS * sps
Definition: hevc.h:537
uint8_t is_intra
Definition: hevc.h:639
uint8_t lists_modification_present_flag
Definition: hevc.h:511
uint8_t profile_idc
Definition: hevc.h:341
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:37
AVBufferRef * tab_mvf_buf
Definition: hevc.h:689
int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int base_level, int rc_rice_param)
Definition: hevc_cabac.c:839
uint8_t type_idx[3]
sao_type_idx
Definition: hevcdsp.h:38
int beta_offset
beta_offset_div2 * 2
Definition: hevc.h:587
const uint8_t ff_hevc_qpel_extra[4]
Definition: hevc.c:44
int ff_hevc_transform_skip_flag_decode(HEVCContext *s, int c_idx)
Definition: hevc_cabac.c:717
const uint8_t ff_hevc_qpel_extra_after[4]
Definition: hevc.c:43
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2670
int max_transform_hierarchy_depth_inter
Definition: hevc.h:444
#define IS_IDR(s)
Definition: hevc.h:83
#define FFMIN(a, b)
Definition: common.h:66
static const AVOption options[]
Definition: hevc.c:3058
int slice_cr_qp_offset
Definition: hevc.h:585
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:71
int offset_abs[3][4]
sao_offset_abs
Definition: hevcdsp.h:29
int output_height
Definition: hevc.h:391
int width
picture width / height.
Definition: avcodec.h:1580
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:169
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
uint8_t * tab_ipm
Definition: hevc.h:814
static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
Definition: hevc.c:2600
void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
Definition: hevc_filter.c:735
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:1877
AVBufferRef * pps_list[MAX_PPS_COUNT]
Definition: hevc.h:533
int hshift[3]
Definition: hevc.h:460
static int set_sps(HEVCContext *s, const HEVCSPS *sps, enum AVPixelFormat pix_fmt)
Definition: hevc.c:407
int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size)
Definition: hevc_cabac.c:543
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
void(* emulated_edge_mc)(uint8_t *buf, const uint8_t *src, ptrdiff_t buf_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:52
#define OFFSET(x)
Definition: hevc.c:3055
int32_t
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:114
void(* idct_dc[4])(int16_t *coeffs)
Definition: hevcdsp.h:50
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2106
uint8_t cu_qp_delta_enabled_flag
Definition: hevc.h:480
uint8_t used_by_curr_pic_lt_sps_flag[32]
Definition: hevc.h:424
int8_t qp_y
Definition: hevc.h:731
static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:1444
Definition: hevc.h:134
Context Adaptive Binary Arithmetic Coder inline functions.
int level
level
Definition: avcodec.h:2970
Definition: hevc.h:203
int ctb_width
Definition: hevc.h:450
void(* put_pcm)(uint8_t *dst, ptrdiff_t stride, int size, GetBitContext *gb, int pcm_bit_depth)
Definition: hevcdsp.h:42
int16_t chroma_weight_l0[16][2]
Definition: hevc.h:600
void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0, int nPbW, int nPbH)
Definition: hevc_mvs.c:41
uint8_t sl_dc[2][6]
Definition: hevc.h:382
uint8_t sign_data_hiding_flag
Definition: hevc.h:469
int height
Definition: hevc.h:449
#define AV_EF_EXPLODE
Definition: avcodec.h:2681
uint8_t output_flag_present_flag
Definition: hevc.h:488
uint16_t seq_output
Definition: hevc.h:831
int mpm_idx
Definition: hevc.h:652
enum AVPixelFormat pix_fmt
Definition: movenc.c:853
PTLCommon general_ptl
Definition: hevc.h:351
int16_t luma_offset_l1[16]
Definition: hevc.h:607
int offset_val[3][5]
SaoOffsetVal.
Definition: hevcdsp.h:36
int16_t chroma_offset_l0[16][2]
Definition: hevc.h:605
int type
NAL unit type.
Definition: h2645_parse.h:50
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
void av_display_rotation_set(int32_t matrix[9], double angle)
Initialize a transformation matrix describing a pure counterclockwise rotation by the specified angle...
Definition: display.c:52
Definition: hevc.h:112
static int hevc_frame_start(HEVCContext *s)
Definition: hevc.c:2407
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:365
unsigned int pps_id
address (in raster order) of the first block in the current slice segment
Definition: hevc.h:542
#define IS_BLA(s)
Definition: hevc.h:84
if(ac->has_optimized_func)
uint8_t sl[4][6][64]
Definition: hevc.h:381
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: utils.c:785
uint8_t pic_slice_level_chroma_qp_offsets_present_flag
Definition: hevc.h:485
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:81
int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0)
Definition: hevc_cabac.c:524
uint32_t vps_time_scale
Definition: hevc.h:372
Definition: hevc.h:133
int colour_description_present_flag
Definition: hevc.h:304
static const int8_t mv[256][2]
Definition: 4xm.c:75
HEVCFrame DPB[32]
Definition: hevc.h:789
Definition: hevc.h:385
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:191
static void hls_prediction_unit(HEVCContext *s, int x0, int y0, int nPbW, int nPbH, int log2_cb_size, int partIdx)
Definition: hevc.c:1678
enum AVPixelFormat pix_fmt
Definition: hevc.h:398
Definition: hevc.h:358
RefPicListTab ** rpl_tab
Definition: hevc.h:682
int sei_display_orientation_present
display orientation
Definition: hevc.h:857
NULL
Definition: eval.c:55
int slice_cb_qp_offset
Definition: hevc.h:584
void ff_hevc_dsp_init(HEVCDSPContext *hevcdsp, int bit_depth)
Definition: hevcdsp.c:136
static int width
Definition: utils.c:156
#define src1
Definition: h264pred.c:139
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:127
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
#define CTB(tab, x, y)
Definition: hevc.c:800
static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
Definition: hevc.c:138
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:114
Definition: hevc.h:466
uint8_t transform_skip_enabled_flag
Definition: hevc.h:478
static av_cold int hevc_decode_init(AVCodecContext *avctx)
Definition: hevc.c:3012
int short_term_ref_pic_set_size
Definition: hevc.h:560
#define IS_IRAP(s)
Definition: hevc.h:86
int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c, int y_c, int log2_trafo_size, int scan_idx, int prev_sig)
Definition: hevc_cabac.c:772
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: frame.h:83
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:3325
uint8_t is_nalff
this flag is != 0 if bitstream is encapsulated as a format defined in 14496-15
Definition: hevc.h:843
enum AVCodecID codec_id
Definition: avcodec.h:1426
int * ctb_addr_rs_to_ts
CtbAddrRSToTS.
Definition: hevc.h:523
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
PTL ptl
Definition: hevc.h:411
int max_sub_layers
Definition: hevc.h:403
unsigned int log2_min_pu_size
Definition: hevc.h:442
int ff_hevc_abs_mvd_greater1_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:676
av_default_item_name
Definition: dnxhdenc.c:55
int8_t pred_flag[2]
Definition: hevc.h:638
uint8_t md5[3][16]
Definition: hevc.h:839
unsigned int sps_id
seq_parameter_set_id
Definition: hevc.h:467
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:80
ScalingList scaling_list
Definition: hevc.h:509
int long_term_ref_pic_set_size
Definition: hevc.h:563
main external API structure.
Definition: avcodec.h:1409
uint8_t is_md5
Definition: hevc.h:840
uint8_t sao_enabled
Definition: hevc.h:420
void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip)
Flip the input matrix horizontally and/or vertically.
Definition: display.c:67
static int hevc_decode_extradata(HEVCContext *s)
Definition: hevc.c:2938
enum PredMode pred_mode
PredMode.
Definition: hevc.h:621
AVBufferRef * hwaccel_priv_buf
Definition: hevc.h:693
int num_extra_slice_header_bits
Definition: hevc.h:513
uint8_t * data
The data buffer.
Definition: buffer.h:89
int16_t y
vertical component of motion vector
Definition: hevc.h:632
Definition: hevc.h:111
void ff_hevc_clear_refs(HEVCContext *s)
Mark all frames in DPB as unused for reference.
Definition: hevc_refs.c:66
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
void(* transform_4x4_luma)(int16_t *coeffs)
Definition: hevcdsp.h:48
uint8_t num_long_term_ref_pics_sps
Definition: hevc.h:425
uint8_t * data
Definition: frame.h:109
void av_md5_init(AVMD5 *ctx)
Definition: md5.c:139
TransformUnit tu
Definition: hevc.h:734
static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb, int ctb_addr_ts)
Definition: hevc.c:2240
uint32_t vui_num_units_in_tick
Definition: hevc.h:321
int extradata_size
Definition: avcodec.h:1524
static const AVProfile profiles[]
Definition: libdcadec.c:181
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:736
int y
Definition: hevc.h:619
AVCodec ff_hevc_decoder
Definition: hevc.c:3071
uint8_t deblocking_filter_control_present_flag
Definition: hevc.h:502
static void export_stream_params(AVCodecContext *avctx, const HEVCParamSets *ps, const HEVCSPS *sps)
Definition: hevc.c:335
int cu_qp_delta
Definition: hevc.h:661
Definition: hevc.h:114
uint8_t * is_pcm
Definition: hevc.h:817
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth)
Definition: hevcpred.c:37
uint8_t * checksum_buf
used on BE to byteswap the lines for checksumming
Definition: hevc.h:823
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:263
uint8_t sps_temporal_mvp_enabled_flag
Definition: hevc.h:434
unsigned int nb_st_rps
Definition: hevc.h:416
int coded_height
Definition: avcodec.h:1595
AVFrame * tmp_frame
Definition: hevc.h:772
uint8_t cabac_init_flag
Definition: hevc.h:576
Describe the class of an AVClass context structure.
Definition: log.h:34
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size)
Add a new side data to a frame.
Definition: frame.c:468
void(* weighted_pred[8])(uint8_t denom, int16_t wlxFlag, int16_t olxFlag, uint8_t *dst, ptrdiff_t dststride, int16_t *src, ptrdiff_t srcstride, int height)
Definition: hevcdsp.h:77
const uint8_t ff_hevc_diag_scan8x8_y[64]
Definition: hevc_data.c:58
#define POS(c_idx, x, y)
int poc
Definition: hevc.h:684
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:254
static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0, int log2_cb_size, int ct_depth)
Definition: hevc.c:1965
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2120
static void pred_weight_table(HEVCContext *s, GetBitContext *gb)
Definition: hevc.c:194
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2113
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:189
#define EPEL_EXTRA
Definition: hevc.h:73
AVFrame * frame
Definition: hevc.h:770
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
uint8_t edge_emu_buffer[(MAX_PB_SIZE+7) *EDGE_EMU_BUFFER_STRIDE *2]
Definition: hevc.h:744
int ff_hevc_sao_eo_class_decode(HEVCContext *s)
Definition: hevc_cabac.c:457
unsigned int max_num_merge_cand
5 - 5_minus_max_num_merge_cand
Definition: hevc.h:590
static const uint8_t diag_scan2x2_inv[2][2]
Definition: hevc.c:81
int checksum_buf_size
Definition: hevc.h:824
DBParams * deblock
Definition: hevc.h:785
GetBitContext gb
Definition: hevc.h:728
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data...
Definition: avcodec.h:2678
unsigned int log2_min_tb_size
Definition: hevc.h:439
int poc
Definition: hevc.h:790
#define L0
Definition: hevc.h:68
enum PartMode part_mode
PartMode.
Definition: hevc.h:622
uint16_t lt_ref_pic_poc_lsb_sps[32]
Definition: hevc.h:423
int ff_hevc_slice_rpl(HEVCContext *s)
Construct the reference picture list(s) for the current slice.
Definition: hevc_refs.c:247
static const uint8_t horiz_scan4x4_x[16]
Definition: hevc.c:52
enum NALUnitType nal_unit_type
Definition: hevc.h:786
int start_of_tiles_x
Definition: hevc.h:740
Definition: hevc.h:630
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Definition: md5.c:165
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:119
HEVCWindow window
Definition: hevc.h:687
uint8_t scaling_list_enable_flag
Definition: hevc.h:413
int * tile_id
TileId.
Definition: hevc.h:525
static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:1332
static const uint8_t horiz_scan4x4_y[16]
Definition: hevc.c:59
int16_t luma_weight_l1[16]
Definition: hevc.h:602
int16_t chroma_log2_weight_denom
Definition: hevc.h:597
int tc_offset
tc_offset_div2 * 2
Definition: hevc.h:506
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:137
uint8_t transfer_characteristic
Definition: hevc.h:306
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
int pocTid0
Definition: hevc.h:791
uint8_t flags
A combination of HEVC_FRAME_FLAG_*.
Definition: hevc.h:705
Views are on top of each other.
Definition: stereo3d.h:55
Definition: hevc.h:208
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:302
int cr_qp_offset
Definition: hevc.h:484
ScalingList scaling_list
Definition: hevc.h:414
int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH)
Definition: hevc_cabac.c:635
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:358
int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth)
Definition: hevc_cabac.c:707
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:3314
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx)
Definition: hevc_cabac.c:645
uint8_t level
Definition: svq3.c:204
uint8_t intra_pred_mode_c
Definition: hevc.h:657
static int hls_coding_quadtree(HEVCContext *s, int x0, int y0, int log2_cb_size, int cb_depth)
Definition: hevc.c:2189
uint8_t level_idc
Definition: hevc.h:343
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:364
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:3301
int eos
current packet contains an EOS/EOB NAL
Definition: hevc.h:793
int height
Definition: gxfenc.c:72
Views are next to each other.
Definition: stereo3d.h:45
int sei_vflip
Definition: hevc.h:859
int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s)
Definition: hevc_cabac.c:608
int max_transform_hierarchy_depth_intra
coded frame dimension in various units
Definition: hevc.h:445
Mv mv[2]
Definition: hevc.h:636
int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:666
uint8_t * skip_flag
Definition: hevc.h:811
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:394
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
int8_t ref_idx[2]
Definition: hevc.h:637
static void hls_residual_coding(HEVCContext *s, int x0, int y0, int log2_trafo_size, enum ScanType scan_idx, int c_idx)
Definition: hevc.c:885
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:217
uint8_t weighted_pred_flag
Definition: hevc.h:486
uint8_t * horizontal_bs
Definition: hevc.h:805
Definition: hevc.h:246
#define BOUNDARY_LEFT_SLICE
Definition: hevc.h:750
void(* put_unweighted_pred_avg_chroma[8])(uint8_t *dst, ptrdiff_t dststride, int16_t *src1, int16_t *src2, ptrdiff_t srcstride, int height)
Definition: hevcdsp.h:74
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1797
unsigned int nb_refs[2]
Definition: hevc.h:571
int32_t * tab_slice_address
Definition: hevc.h:808
uint8_t disable_deblocking_filter_flag
slice_header_disable_deblocking_filter_flag
Definition: hevc.h:577
static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output, AVPacket *avpkt)
Definition: hevc.c:2713
unsigned int * column_width
ColumnWidth.
Definition: hevc.h:517
static const uint8_t diag_scan2x2_x[4]
Definition: hevc.c:77
Definition: hevc.h:98
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
uint8_t * filter_slice_edges
Definition: hevc.h:820
uint8_t slice_header_extension_present_flag
Definition: hevc.h:514
uint8_t collocated_list
Definition: hevc.h:579
int ff_hevc_coeff_sign_flag(HEVCContext *s, uint8_t nb)
Definition: hevc_cabac.c:864
int nal_length_size
Number of bytes used for nal length (1, 2 or 4)
Definition: hevc.h:847
int den
denominator
Definition: rational.h:45
int slice_ctb_addr_rs
Definition: hevc.h:610
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:645
AVBufferPool * tab_mvf_pool
Definition: hevc.h:777
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
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, int cbf_cb, int cbf_cr)
Definition: hevc.c:1347
int video_full_range_flag
Definition: hevc.h:303
GetBitContext gb
Definition: h2645_parse.h:45
int ff_hevc_cu_qp_delta_abs(HEVCContext *s)
Definition: hevc_cabac.c:489
AVRational sar
Definition: hevc.h:296
uint8_t slice_loop_filter_across_slices_enabled_flag
Definition: hevc.h:578
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts)
Definition: hevc_cabac.c:377
void * priv_data
Definition: avcodec.h:1451
const uint8_t * raw_data
Definition: h2645_parse.h:43
#define SUBDIVIDE(x, y, idx)
int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size)
Definition: hevc_cabac.c:702
unsigned int collocated_ref_idx
Definition: hevc.h:581
#define GET_COORD(offset, n)
CodingUnit cu
Definition: hevc.h:746
int len
int min_pu_width
Definition: hevc.h:457
static const uint8_t horiz_scan8x8_inv[8][8]
Definition: hevc.c:66
int ff_hevc_pred_mode_decode(HEVCContext *s)
Definition: hevc_cabac.c:519
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1459
static uint8_t tmp[8]
Definition: des.c:38
int beta_offset
Definition: hevc.h:669
int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s)
Definition: hevc_cabac.c:514
H2645NAL * nals
Definition: h2645_parse.h:65
int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:585
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
#define AV_ZERO32(d)
Definition: intreadwrite.h:545
void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase, int log2_cb_size)
Definition: hevc_filter.c:153
unsigned int list_entry_lx[2][32]
Definition: hevc.h:565
uint8_t luma_log2_weight_denom
Definition: hevc.h:596
static const uint8_t diag_scan4x4_inv[4][4]
Definition: hevc.c:86
struct HEVCSPS::@22 pcm
int16_t chroma_weight_l1[16][2]
Definition: hevc.h:601
int ff_hevc_last_significant_coeff_suffix_decode(HEVCContext *s, int last_significant_coeff_prefix)
Definition: hevc_cabac.c:751
uint8_t long_term_ref_pics_present_flag
Definition: hevc.h:422
Definition: hevc.h:132
H2645Packet pkt
Definition: hevc.h:833
int temporal_id
HEVC only, nuh_temporal_id_plus_1 - 1.
Definition: h2645_parse.h:55
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:205
int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
Definition: hevc_refs.c:133
static const uint8_t scan_1x1[1]
Definition: hevc.c:46
int ff_hevc_last_significant_coeff_x_prefix_decode(HEVCContext *s, int c_idx, int log2_size)
Definition: hevc_cabac.c:739
int boundary_flags
Definition: hevc.h:756
FILE * out
Definition: movenc.c:54
int diff_cu_qp_delta_depth
Definition: hevc.h:481
void(* dequant)(int16_t *coeffs)
Definition: hevcdsp.h:47
int ff_hevc_sao_offset_abs_decode(HEVCContext *s)
Definition: hevc_cabac.c:442
int cur_intra_pred_mode
Definition: hevc.h:664
int ff_hevc_pcm_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:580
const uint8_t ff_hevc_diag_scan8x8_x[64]
Definition: hevc_data.c:39
int num_reorder_pics
Definition: hevc.h:406
#define av_always_inline
Definition: attributes.h:40
struct HEVCSPS::@21 temporal_layer[MAX_SUB_LAYERS]
HW decoding through Direct3D11, Picture.data[3] contains a ID3D11VideoDecoderOutputView pointer...
Definition: pixfmt.h:222
int cb_qp_offset
Definition: hevc.h:483
uint8_t context_initialized
Definition: hevc.h:842
AVBufferRef * rpl_buf
Definition: hevc.h:691
int is_decoded
Definition: hevc.h:798
int video_signal_type_present_flag
Definition: hevc.h:301
#define FFSWAP(type, a, b)
Definition: common.h:69
uint8_t deblocking_filter_override_enabled_flag
Definition: hevc.h:503
int bit_depth
Definition: hevc.h:396
enum SliceType slice_type
Definition: hevc.h:549
int beta_offset
beta_offset_div2 * 2
Definition: hevc.h:505
int min_tb_height
Definition: hevc.h:456
static const uint8_t horiz_scan2x2_y[4]
Definition: hevc.c:50
#define BOUNDARY_LEFT_TILE
Definition: hevc.h:751
static const uint8_t diag_scan2x2_y[4]
Definition: hevc.c:79
#define L1
Definition: hevc.h:69
uint8_t * cbf_luma
Definition: hevc.h:816
int depth
Number of bits in the component.
Definition: pixdesc.h:57
int ff_hevc_sao_merge_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:417
SliceHeader sh
Definition: hevc.h:783
int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx, ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
Definition: hevc_ps.c:109
ScanType
Definition: hevc.h:257
exp golomb vlc stuff
int pcm_enabled_flag
Definition: hevc.h:401
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
static av_cold int hevc_decode_free(AVCodecContext *avctx)
Definition: hevc.c:2802
This structure stores compressed data.
Definition: avcodec.h:1323
AVBufferRef * sps_list[MAX_SPS_COUNT]
Definition: hevc.h:532
uint8_t mvd_l1_zero_flag
Definition: hevc.h:574
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1183
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:211
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
Definition: hevc.c:2763
static const uint8_t diag_scan8x8_inv[8][8]
Definition: hevc.c:93
for(j=16;j >0;--j)
uint8_t separate_colour_plane_flag
output (i.e. cropped) values
Definition: hevc.h:388
#define FFMAX3(a, b, c)
Definition: common.h:65
int end_of_tiles_y
Definition: hevc.h:742
const uint8_t ff_hevc_diag_scan4x4_y[16]
Definition: hevc_data.c:32
static void intra_prediction_unit_default_value(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:2018
uint8_t slice_sample_adaptive_offset_flag[3]
Definition: hevc.h:573
#define SAMPLE_CTB(tab, x, y)
Definition: hevc.h:81
uint8_t dependent_slice_segments_enabled_flag
Definition: hevc.h:491
int offset_sign[3][4]
sao_offset_sign
Definition: hevcdsp.h:30
#define BOUNDARY_UPPER_TILE
Definition: hevc.h:753