Libav
hevc_filter.c
Go to the documentation of this file.
1 /*
2  * HEVC video decoder
3  *
4  * Copyright (C) 2012 - 2013 Guillaume Martres
5  * Copyright (C) 2013 Seppo Tomperi
6  * Copyright (C) 2013 Wassim Hamidouche
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27 
28 #include "cabac_functions.h"
29 #include "golomb.h"
30 #include "hevc.h"
31 
32 #define LUMA 0
33 #define CB 1
34 #define CR 2
35 
36 static const uint8_t tctable[54] = {
37  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // QP 0...18
38  1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, // QP 19...37
39  5, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16, 18, 20, 22, 24 // QP 38...53
40 };
41 
42 static const uint8_t betatable[52] = {
43  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 8, // QP 0...18
44  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, // QP 19...37
45  38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64 // QP 38...51
46 };
47 
48 static int chroma_tc(HEVCContext *s, int qp_y, int c_idx, int tc_offset)
49 {
50  static const int qp_c[] = {
51  29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37
52  };
53  int qp, qp_i, offset, idxt;
54 
55  // slice qp offset is not used for deblocking
56  if (c_idx == 1)
57  offset = s->pps->cb_qp_offset;
58  else
59  offset = s->pps->cr_qp_offset;
60 
61  qp_i = av_clip_c(qp_y + offset, 0, 57);
62  if (qp_i < 30)
63  qp = qp_i;
64  else if (qp_i > 43)
65  qp = qp_i - 6;
66  else
67  qp = qp_c[qp_i - 30];
68 
69  idxt = av_clip_c(qp + DEFAULT_INTRA_TC_OFFSET + tc_offset, 0, 53);
70  return tctable[idxt];
71 }
72 
73 static int get_qPy_pred(HEVCContext *s, int xC, int yC,
74  int xBase, int yBase, int log2_cb_size)
75 {
76  HEVCLocalContext *lc = &s->HEVClc;
77  int ctb_size_mask = (1 << s->sps->log2_ctb_size) - 1;
78  int MinCuQpDeltaSizeMask = (1 << (s->sps->log2_ctb_size -
79  s->pps->diff_cu_qp_delta_depth)) - 1;
80  int xQgBase = xBase - (xBase & MinCuQpDeltaSizeMask);
81  int yQgBase = yBase - (yBase & MinCuQpDeltaSizeMask);
82  int min_cb_width = s->sps->min_cb_width;
83  int min_cb_height = s->sps->min_cb_height;
84  int x_cb = xQgBase >> s->sps->log2_min_cb_size;
85  int y_cb = yQgBase >> s->sps->log2_min_cb_size;
86  int availableA = (xBase & ctb_size_mask) &&
87  (xQgBase & ctb_size_mask);
88  int availableB = (yBase & ctb_size_mask) &&
89  (yQgBase & ctb_size_mask);
90  int qPy_pred, qPy_a, qPy_b;
91 
92  // qPy_pred
93  if (lc->first_qp_group || (!xQgBase && !yQgBase)) {
95  qPy_pred = s->sh.slice_qp;
96  } else {
97  qPy_pred = lc->qp_y;
98  if (log2_cb_size < s->sps->log2_ctb_size -
100  static const int offsetX[8][8] = {
101  { -1, 1, 3, 1, 7, 1, 3, 1 },
102  { 0, 0, 0, 0, 0, 0, 0, 0 },
103  { 1, 3, 1, 3, 1, 3, 1, 3 },
104  { 2, 2, 2, 2, 2, 2, 2, 2 },
105  { 3, 5, 7, 5, 3, 5, 7, 5 },
106  { 4, 4, 4, 4, 4, 4, 4, 4 },
107  { 5, 7, 5, 7, 5, 7, 5, 7 },
108  { 6, 6, 6, 6, 6, 6, 6, 6 }
109  };
110  static const int offsetY[8][8] = {
111  { 7, 0, 1, 2, 3, 4, 5, 6 },
112  { 0, 1, 2, 3, 4, 5, 6, 7 },
113  { 1, 0, 3, 2, 5, 4, 7, 6 },
114  { 0, 1, 2, 3, 4, 5, 6, 7 },
115  { 3, 0, 1, 2, 7, 4, 5, 6 },
116  { 0, 1, 2, 3, 4, 5, 6, 7 },
117  { 1, 0, 3, 2, 5, 4, 7, 6 },
118  { 0, 1, 2, 3, 4, 5, 6, 7 }
119  };
120  int xC0b = (xC - (xC & ctb_size_mask)) >> s->sps->log2_min_cb_size;
121  int yC0b = (yC - (yC & ctb_size_mask)) >> s->sps->log2_min_cb_size;
122  int idxX = (xQgBase & ctb_size_mask) >> s->sps->log2_min_cb_size;
123  int idxY = (yQgBase & ctb_size_mask) >> s->sps->log2_min_cb_size;
124  int idx_mask = ctb_size_mask >> s->sps->log2_min_cb_size;
125  int x, y;
126 
127  x = FFMIN(xC0b + offsetX[idxX][idxY], min_cb_width - 1);
128  y = FFMIN(yC0b + (offsetY[idxX][idxY] & idx_mask), min_cb_height - 1);
129 
130  if (xC0b == (lc->start_of_tiles_x >> s->sps->log2_min_cb_size) &&
131  offsetX[idxX][idxY] == -1) {
132  x = (lc->end_of_tiles_x >> s->sps->log2_min_cb_size) - 1;
133  y = yC0b - 1;
134  }
135  qPy_pred = s->qp_y_tab[y * min_cb_width + x];
136  }
137  }
138 
139  // qPy_a
140  if (availableA == 0)
141  qPy_a = qPy_pred;
142  else
143  qPy_a = s->qp_y_tab[(x_cb - 1) + y_cb * min_cb_width];
144 
145  // qPy_b
146  if (availableB == 0)
147  qPy_b = qPy_pred;
148  else
149  qPy_b = s->qp_y_tab[x_cb + (y_cb - 1) * min_cb_width];
150 
151  return (qPy_a + qPy_b + 1) >> 1;
152 }
153 
154 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC,
155  int xBase, int yBase, int log2_cb_size)
156 {
157  int qp_y = get_qPy_pred(s, xC, yC, xBase, yBase, log2_cb_size);
158 
159  if (s->HEVClc.tu.cu_qp_delta != 0) {
160  int off = s->sps->qp_bd_offset;
161  s->HEVClc.qp_y = FFUMOD(qp_y + s->HEVClc.tu.cu_qp_delta + 52 + 2 * off,
162  52 + off) - off;
163  } else
164  s->HEVClc.qp_y = qp_y;
165 }
166 
167 static int get_qPy(HEVCContext *s, int xC, int yC)
168 {
169  int log2_min_cb_size = s->sps->log2_min_cb_size;
170  int x = xC >> log2_min_cb_size;
171  int y = yC >> log2_min_cb_size;
172  return s->qp_y_tab[x + y * s->sps->min_cb_width];
173 }
174 
175 static void copy_CTB(uint8_t *dst, uint8_t *src,
176  int width, int height, int stride)
177 {
178  int i;
179 
180  for (i = 0; i < height; i++) {
181  memcpy(dst, src, width);
182  dst += stride;
183  src += stride;
184  }
185 }
186 
187 #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
188 
189 static void sao_filter_CTB(HEVCContext *s, int x, int y)
190 {
191  // TODO: This should be easily parallelizable
192  // TODO: skip CBs when (cu_transquant_bypass_flag || (pcm_loop_filter_disable_flag && pcm_flag))
193  int c_idx = 0;
194  int class = 1, class_index;
195  int edges[4]; // 0 left 1 top 2 right 3 bottom
196  SAOParams *sao[4];
197  int classes[4];
198  int x_shift = 0, y_shift = 0;
199  int x_ctb = x >> s->sps->log2_ctb_size;
200  int y_ctb = y >> s->sps->log2_ctb_size;
201  int ctb_addr_rs = y_ctb * s->sps->ctb_width + x_ctb;
202  int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
203 
204  // flags indicating unfilterable edges
205  uint8_t vert_edge[] = { 0, 0, 0, 0 };
206  uint8_t horiz_edge[] = { 0, 0, 0, 0 };
207  uint8_t diag_edge[] = { 0, 0, 0, 0 };
208  uint8_t lfase[3]; // current, above, left
209  uint8_t no_tile_filter = s->pps->tiles_enabled_flag &&
211  uint8_t left_tile_edge = 0, up_tile_edge = 0;
212 
213  sao[0] = &CTB(s->sao, x_ctb, y_ctb);
214  edges[0] = x_ctb == 0;
215  edges[1] = y_ctb == 0;
216  edges[2] = x_ctb == s->sps->ctb_width - 1;
217  edges[3] = y_ctb == s->sps->ctb_height - 1;
218  lfase[0] = CTB(s->filter_slice_edges, x_ctb, y_ctb);
219  classes[0] = 0;
220 
221  if (!edges[0]) {
222  left_tile_edge = no_tile_filter && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs-1]];
223  sao[class] = &CTB(s->sao, x_ctb - 1, y_ctb);
224  vert_edge[0] = (!lfase[0] && CTB(s->tab_slice_address, x_ctb, y_ctb) != CTB(s->tab_slice_address, x_ctb - 1, y_ctb)) || left_tile_edge;
225  vert_edge[2] = vert_edge[0];
226  lfase[2] = CTB(s->filter_slice_edges, x_ctb - 1, y_ctb);
227  classes[class] = 2;
228  class++;
229  x_shift = 8;
230  }
231 
232  if (!edges[1]) {
233  up_tile_edge = no_tile_filter && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->sps->ctb_width]];
234  sao[class] = &CTB(s->sao, x_ctb, y_ctb - 1);
235  horiz_edge[0] = (!lfase[0] && CTB(s->tab_slice_address, x_ctb, y_ctb) != CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) || up_tile_edge;
236  horiz_edge[1] = horiz_edge[0];
237  lfase[1] = CTB(s->filter_slice_edges, x_ctb, y_ctb - 1);
238  classes[class] = 1;
239  class++;
240  y_shift = 4;
241 
242  if (!edges[0]) {
243  classes[class] = 3;
244  sao[class] = &CTB(s->sao, x_ctb - 1, y_ctb - 1);
245  class++;
246 
247  // Tile check here is done current CTB row/col, not above/left like you'd expect,
248  //but that is because the tile boundary always extends through the whole pic
249  vert_edge[1] = (!lfase[1] && CTB(s->tab_slice_address, x_ctb, y_ctb - 1) != CTB(s->tab_slice_address, x_ctb - 1, y_ctb - 1)) || left_tile_edge;
250  vert_edge[3] = vert_edge[1];
251  horiz_edge[2] = (!lfase[2] && CTB(s->tab_slice_address, x_ctb - 1, y_ctb) != CTB(s->tab_slice_address, x_ctb - 1, y_ctb - 1)) || up_tile_edge;
252  horiz_edge[3] = horiz_edge[2];
253  diag_edge[0] = (!lfase[0] && CTB(s->tab_slice_address, x_ctb, y_ctb) != CTB(s->tab_slice_address, x_ctb - 1, y_ctb - 1)) || left_tile_edge || up_tile_edge;
254  diag_edge[3] = diag_edge[0];
255 
256  // Does left CTB comes after above CTB?
257  if (CTB(s->tab_slice_address, x_ctb - 1, y_ctb) >
258  CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) {
259  diag_edge[2] = !lfase[2] || left_tile_edge || up_tile_edge;
260  diag_edge[1] = diag_edge[2];
261  } else if (CTB(s->tab_slice_address, x_ctb - 1, y_ctb) <
262  CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) {
263  diag_edge[1] = !lfase[1] || left_tile_edge || up_tile_edge;
264  diag_edge[2] = diag_edge[1];
265  } else {
266  // Same slice, only consider tiles
267  diag_edge[2] = left_tile_edge || up_tile_edge;
268  diag_edge[1] = diag_edge[2];
269  }
270  }
271  }
272 
273  for (c_idx = 0; c_idx < 3; c_idx++) {
274  int chroma = c_idx ? 1 : 0;
275  int x0 = x >> chroma;
276  int y0 = y >> chroma;
277  int stride = s->frame->linesize[c_idx];
278  int ctb_size = (1 << (s->sps->log2_ctb_size)) >> s->sps->hshift[c_idx];
279  int width = FFMIN(ctb_size,
280  (s->sps->width >> s->sps->hshift[c_idx]) - x0);
281  int height = FFMIN(ctb_size,
282  (s->sps->height >> s->sps->vshift[c_idx]) - y0);
283 
284  uint8_t *src = &s->frame->data[c_idx][y0 * stride + (x0 << s->sps->pixel_shift)];
285  uint8_t *dst = &s->sao_frame->data[c_idx][y0 * stride + (x0 << s->sps->pixel_shift)];
286  int offset = (y_shift >> chroma) * stride + ((x_shift >> chroma) << s->sps->pixel_shift);
287 
288  copy_CTB(dst - offset, src - offset,
289  (edges[2] ? width + (x_shift >> chroma) : width) << s->sps->pixel_shift,
290  (edges[3] ? height + (y_shift >> chroma) : height), stride);
291 
292  for (class_index = 0; class_index < class; class_index++) {
293 
294  switch (sao[class_index]->type_idx[c_idx]) {
295  case SAO_BAND:
296  s->hevcdsp.sao_band_filter[classes[class_index]](dst, src,
297  stride,
298  sao[class_index],
299  edges, width,
300  height, c_idx);
301  break;
302  case SAO_EDGE:
303  s->hevcdsp.sao_edge_filter[classes[class_index]](dst, src,
304  stride,
305  sao[class_index],
306  edges, width,
307  height, c_idx,
308  vert_edge[classes[class_index]],
309  horiz_edge[classes[class_index]],
310  diag_edge[classes[class_index]]);
311  break;
312  }
313  }
314  }
315 }
316 
317 static int get_pcm(HEVCContext *s, int x, int y)
318 {
319  int log2_min_pu_size = s->sps->log2_min_pu_size;
320  int x_pu, y_pu;
321 
322  if (x < 0 || y < 0)
323  return 2;
324 
325  x_pu = x >> log2_min_pu_size;
326  y_pu = y >> log2_min_pu_size;
327 
328  if (x_pu >= s->sps->min_pu_width || y_pu >= s->sps->min_pu_height)
329  return 2;
330  return s->is_pcm[y_pu * s->sps->min_pu_width + x_pu];
331 }
332 
333 #define TC_CALC(qp, bs) \
334  tctable[av_clip((qp) + DEFAULT_INTRA_TC_OFFSET * ((bs) - 1) + \
335  (tc_offset >> 1 << 1), \
336  0, MAX_QP + DEFAULT_INTRA_TC_OFFSET)]
337 
338 static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
339 {
340  uint8_t *src;
341  int x, y, x_end, y_end, chroma;
342  int c_tc[2], tc[2], beta;
343  uint8_t no_p[2] = { 0 };
344  uint8_t no_q[2] = { 0 };
345 
346  int log2_ctb_size = s->sps->log2_ctb_size;
347  int ctb_size = 1 << log2_ctb_size;
348  int ctb = (x0 >> log2_ctb_size) +
349  (y0 >> log2_ctb_size) * s->sps->ctb_width;
350  int cur_tc_offset = s->deblock[ctb].tc_offset;
351  int cur_beta_offset = s->deblock[ctb].beta_offset;
352  int tc_offset, left_tc_offset, beta_offset, left_beta_offset;
353  int pcmf = (s->sps->pcm_enabled_flag &&
356 
357  if (x0) {
358  left_tc_offset = s->deblock[ctb - 1].tc_offset;
359  left_beta_offset = s->deblock[ctb - 1].beta_offset;
360  }
361 
362  x_end = x0 + ctb_size;
363  if (x_end > s->sps->width)
364  x_end = s->sps->width;
365  y_end = y0 + ctb_size;
366  if (y_end > s->sps->height)
367  y_end = s->sps->height;
368 
369  tc_offset = cur_tc_offset;
370  beta_offset = cur_beta_offset;
371 
372  // vertical filtering luma
373  for (y = y0; y < y_end; y += 8) {
374  for (x = x0 ? x0 : 8; x < x_end; x += 8) {
375  const int bs0 = s->vertical_bs[(x >> 3) + (y >> 2) * s->bs_width];
376  const int bs1 = s->vertical_bs[(x >> 3) + ((y + 4) >> 2) * s->bs_width];
377  if (bs0 || bs1) {
378  const int qp = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
379 
380  beta = betatable[av_clip(qp + beta_offset, 0, MAX_QP)];
381 
382  tc[0] = bs0 ? TC_CALC(qp, bs0) : 0;
383  tc[1] = bs1 ? TC_CALC(qp, bs1) : 0;
384  src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
385  if (pcmf) {
386  no_p[0] = get_pcm(s, x - 1, y);
387  no_p[1] = get_pcm(s, x - 1, y + 4);
388  no_q[0] = get_pcm(s, x, y);
389  no_q[1] = get_pcm(s, x, y + 4);
391  s->frame->linesize[LUMA],
392  beta, tc, no_p, no_q);
393  } else
395  s->frame->linesize[LUMA],
396  beta, tc, no_p, no_q);
397  }
398  }
399  }
400 
401  // vertical filtering chroma
402  for (chroma = 1; chroma <= 2; chroma++) {
403  for (y = y0; y < y_end; y += 16) {
404  for (x = x0 ? x0 : 16; x < x_end; x += 16) {
405  const int bs0 = s->vertical_bs[(x >> 3) + (y >> 2) * s->bs_width];
406  const int bs1 = s->vertical_bs[(x >> 3) + ((y + 8) >> 2) * s->bs_width];
407  if ((bs0 == 2) || (bs1 == 2)) {
408  const int qp0 = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
409  const int qp1 = (get_qPy(s, x - 1, y + 8) + get_qPy(s, x, y + 8) + 1) >> 1;
410 
411  c_tc[0] = (bs0 == 2) ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
412  c_tc[1] = (bs1 == 2) ? chroma_tc(s, qp1, chroma, tc_offset) : 0;
413  src = &s->frame->data[chroma][y / 2 * s->frame->linesize[chroma] + ((x / 2) << s->sps->pixel_shift)];
414  if (pcmf) {
415  no_p[0] = get_pcm(s, x - 1, y);
416  no_p[1] = get_pcm(s, x - 1, y + 8);
417  no_q[0] = get_pcm(s, x, y);
418  no_q[1] = get_pcm(s, x, y + 8);
420  s->frame->linesize[chroma],
421  c_tc, no_p, no_q);
422  } else
424  s->frame->linesize[chroma],
425  c_tc, no_p, no_q);
426  }
427  }
428  }
429  }
430 
431  // horizontal filtering luma
432  if (x_end != s->sps->width)
433  x_end -= 8;
434  for (y = y0 ? y0 : 8; y < y_end; y += 8) {
435  for (x = x0 ? x0 - 8 : 0; x < x_end; x += 8) {
436  const int bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
437  const int bs1 = s->horizontal_bs[(x + 4 + y * s->bs_width) >> 2];
438  if (bs0 || bs1) {
439  const int qp = (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1;
440 
441  tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
442  beta_offset = x >= x0 ? cur_beta_offset : left_beta_offset;
443 
444  beta = betatable[av_clip(qp + beta_offset, 0, MAX_QP)];
445  tc[0] = bs0 ? TC_CALC(qp, bs0) : 0;
446  tc[1] = bs1 ? TC_CALC(qp, bs1) : 0;
447  src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
448  if (pcmf) {
449  no_p[0] = get_pcm(s, x, y - 1);
450  no_p[1] = get_pcm(s, x + 4, y - 1);
451  no_q[0] = get_pcm(s, x, y);
452  no_q[1] = get_pcm(s, x + 4, y);
454  s->frame->linesize[LUMA],
455  beta, tc, no_p, no_q);
456  } else
458  s->frame->linesize[LUMA],
459  beta, tc, no_p, no_q);
460  }
461  }
462  }
463 
464  // horizontal filtering chroma
465  for (chroma = 1; chroma <= 2; chroma++) {
466  for (y = y0 ? y0 : 16; y < y_end; y += 16) {
467  for (x = x0 - 8; x < x_end; x += 16) {
468  int bs0, bs1;
469  // to make sure no memory access over boundary when x = -8
470  // TODO: simplify with row based deblocking
471  if (x < 0) {
472  bs0 = 0;
473  bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
474  } else if (x >= x_end - 8) {
475  bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
476  bs1 = 0;
477  } else {
478  bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
479  bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
480  }
481 
482  if ((bs0 == 2) || (bs1 == 2)) {
483  const int qp0 = bs0 == 2 ? (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1 : 0;
484  const int qp1 = bs1 == 2 ? (get_qPy(s, x + 8, y - 1) + get_qPy(s, x + 8, y) + 1) >> 1 : 0;
485 
486  tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
487  c_tc[0] = bs0 == 2 ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
488  c_tc[1] = bs1 == 2 ? chroma_tc(s, qp1, chroma, cur_tc_offset) : 0;
489  src = &s->frame->data[chroma][y / 2 * s->frame->linesize[chroma] + ((x / 2) << s->sps->pixel_shift)];
490  if (pcmf) {
491  no_p[0] = get_pcm(s, x, y - 1);
492  no_p[1] = get_pcm(s, x + 8, y - 1);
493  no_q[0] = get_pcm(s, x, y);
494  no_q[1] = get_pcm(s, x + 8, y);
496  s->frame->linesize[chroma],
497  c_tc, no_p, no_q);
498  } else
500  s->frame->linesize[chroma],
501  c_tc, no_p, no_q);
502  }
503  }
504  }
505  }
506 }
507 
508 static int boundary_strength(HEVCContext *s, MvField *curr,
509  uint8_t curr_cbf_luma, MvField *neigh,
510  uint8_t neigh_cbf_luma,
511  RefPicList *neigh_refPicList,
512  int tu_border)
513 {
514  int mvs = curr->pred_flag[0] + curr->pred_flag[1];
515 
516  if (tu_border) {
517  if (curr->is_intra || neigh->is_intra)
518  return 2;
519  if (curr_cbf_luma || neigh_cbf_luma)
520  return 1;
521  }
522 
523  if (mvs == neigh->pred_flag[0] + neigh->pred_flag[1]) {
524  if (mvs == 2) {
525  // same L0 and L1
526  if (s->ref->refPicList[0].list[curr->ref_idx[0]] == neigh_refPicList[0].list[neigh->ref_idx[0]] &&
527  s->ref->refPicList[0].list[curr->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]] &&
528  neigh_refPicList[0].list[neigh->ref_idx[0]] == neigh_refPicList[1].list[neigh->ref_idx[1]]) {
529  if ((abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
530  abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4) &&
531  (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
532  abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4))
533  return 1;
534  else
535  return 0;
536  } else if (neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
537  neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
538  if (abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
539  abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4)
540  return 1;
541  else
542  return 0;
543  } else if (neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
544  neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
545  if (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
546  abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4)
547  return 1;
548  else
549  return 0;
550  } else {
551  return 1;
552  }
553  } else { // 1 MV
554  Mv A, B;
555  int ref_A, ref_B;
556 
557  if (curr->pred_flag[0]) {
558  A = curr->mv[0];
559  ref_A = s->ref->refPicList[0].list[curr->ref_idx[0]];
560  } else {
561  A = curr->mv[1];
562  ref_A = s->ref->refPicList[1].list[curr->ref_idx[1]];
563  }
564 
565  if (neigh->pred_flag[0]) {
566  B = neigh->mv[0];
567  ref_B = neigh_refPicList[0].list[neigh->ref_idx[0]];
568  } else {
569  B = neigh->mv[1];
570  ref_B = neigh_refPicList[1].list[neigh->ref_idx[1]];
571  }
572 
573  if (ref_A == ref_B) {
574  if (abs(A.x - B.x) >= 4 || abs(A.y - B.y) >= 4)
575  return 1;
576  else
577  return 0;
578  } else
579  return 1;
580  }
581  }
582 
583  return 1;
584 }
585 
587  int log2_trafo_size)
588 {
589  HEVCLocalContext *lc = &s->HEVClc;
590  MvField *tab_mvf = s->ref->tab_mvf;
591  int log2_min_pu_size = s->sps->log2_min_pu_size;
592  int log2_min_tu_size = s->sps->log2_min_tb_size;
593  int min_pu_width = s->sps->min_pu_width;
594  int min_tu_width = s->sps->min_tb_width;
595  int is_intra = tab_mvf[(y0 >> log2_min_pu_size) * min_pu_width +
596  (x0 >> log2_min_pu_size)].is_intra;
597  int boundary_upper, boundary_left;
598  int i, j, bs;
599 
600  boundary_upper = y0 > 0 && !(y0 & 7);
601  if (boundary_upper &&
604  (y0 % (1 << s->sps->log2_ctb_size)) == 0) ||
607  (y0 % (1 << s->sps->log2_ctb_size)) == 0)))
608  boundary_upper = 0;
609 
610  if (boundary_upper) {
611  RefPicList *rpl_top = (lc->boundary_flags & BOUNDARY_UPPER_SLICE) ?
612  ff_hevc_get_ref_list(s, s->ref, x0, y0 - 1) :
613  s->ref->refPicList;
614 
615  int yp_pu = (y0 - 1) >> log2_min_pu_size;
616  int yq_pu = y0 >> log2_min_pu_size;
617  int yp_tu = (y0 - 1) >> log2_min_tu_size;
618  int yq_tu = y0 >> log2_min_tu_size;
619 
620  for (i = 0; i < (1 << log2_trafo_size); i += 4) {
621  int x_pu = (x0 + i) >> log2_min_pu_size;
622  int x_tu = (x0 + i) >> log2_min_tu_size;
623  MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
624  MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
625  uint8_t top_cbf_luma = s->cbf_luma[yp_tu * min_tu_width + x_tu];
626  uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
627 
628  bs = boundary_strength(s, curr, curr_cbf_luma,
629  top, top_cbf_luma, rpl_top, 1);
630  if (bs)
631  s->horizontal_bs[((x0 + i) + y0 * s->bs_width) >> 2] = bs;
632  }
633  }
634 
635  // bs for TU internal horizontal PU boundaries
636  if (log2_trafo_size > s->sps->log2_min_pu_size && !is_intra) {
637  RefPicList *rpl = s->ref->refPicList;
638 
639  for (j = 8; j < (1 << log2_trafo_size); j += 8) {
640  int yp_pu = (y0 + j - 1) >> log2_min_pu_size;
641  int yq_pu = (y0 + j) >> log2_min_pu_size;
642  int yp_tu = (y0 + j - 1) >> log2_min_tu_size;
643  int yq_tu = (y0 + j) >> log2_min_tu_size;
644 
645  for (i = 0; i < (1 << log2_trafo_size); i += 4) {
646  int x_pu = (x0 + i) >> log2_min_pu_size;
647  int x_tu = (x0 + i) >> log2_min_tu_size;
648  MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
649  MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
650  uint8_t top_cbf_luma = s->cbf_luma[yp_tu * min_tu_width + x_tu];
651  uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
652 
653  bs = boundary_strength(s, curr, curr_cbf_luma,
654  top, top_cbf_luma, rpl, 0);
655  if (bs)
656  s->horizontal_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
657  }
658  }
659  }
660 
661  // bs for vertical TU boundaries
662  boundary_left = x0 > 0 && !(x0 & 7);
663  if (boundary_left &&
666  (x0 % (1 << s->sps->log2_ctb_size)) == 0) ||
669  (x0 % (1 << s->sps->log2_ctb_size)) == 0)))
670  boundary_left = 0;
671 
672  if (boundary_left) {
673  RefPicList *rpl_left = (lc->boundary_flags & BOUNDARY_LEFT_SLICE) ?
674  ff_hevc_get_ref_list(s, s->ref, x0 - 1, y0) :
675  s->ref->refPicList;
676 
677  int xp_pu = (x0 - 1) >> log2_min_pu_size;
678  int xq_pu = x0 >> log2_min_pu_size;
679  int xp_tu = (x0 - 1) >> log2_min_tu_size;
680  int xq_tu = x0 >> log2_min_tu_size;
681 
682  for (i = 0; i < (1 << log2_trafo_size); i += 4) {
683  int y_pu = (y0 + i) >> log2_min_pu_size;
684  int y_tu = (y0 + i) >> log2_min_tu_size;
685  MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
686  MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
687 
688  uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
689  uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
690 
691  bs = boundary_strength(s, curr, curr_cbf_luma,
692  left, left_cbf_luma, rpl_left, 1);
693  if (bs)
694  s->vertical_bs[(x0 >> 3) + ((y0 + i) >> 2) * s->bs_width] = bs;
695  }
696  }
697 
698  // bs for TU internal vertical PU boundaries
699  if (log2_trafo_size > log2_min_pu_size && !is_intra) {
700  RefPicList *rpl = s->ref->refPicList;
701 
702  for (j = 0; j < (1 << log2_trafo_size); j += 4) {
703  int y_pu = (y0 + j) >> log2_min_pu_size;
704  int y_tu = (y0 + j) >> log2_min_tu_size;
705 
706  for (i = 8; i < (1 << log2_trafo_size); i += 8) {
707  int xp_pu = (x0 + i - 1) >> log2_min_pu_size;
708  int xq_pu = (x0 + i) >> log2_min_pu_size;
709  int xp_tu = (x0 + i - 1) >> log2_min_tu_size;
710  int xq_tu = (x0 + i) >> log2_min_tu_size;
711  MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
712  MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
713  uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
714  uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
715 
716  bs = boundary_strength(s, curr, curr_cbf_luma,
717  left, left_cbf_luma, rpl, 0);
718  if (bs)
719  s->vertical_bs[((x0 + i) >> 3) + ((y0 + j) >> 2) * s->bs_width] = bs;
720  }
721  }
722  }
723 }
724 
725 #undef LUMA
726 #undef CB
727 #undef CR
728 
729 void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
730 {
731  deblocking_filter_CTB(s, x, y);
732  if (s->sps->sao_enabled)
733  sao_filter_CTB(s, x, y);
734 }
735 
736 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
737 {
738  if (y_ctb && x_ctb)
739  ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb - ctb_size);
740  if (y_ctb && x_ctb >= s->sps->width - ctb_size) {
741  ff_hevc_hls_filter(s, x_ctb, y_ctb - ctb_size);
742  ff_thread_report_progress(&s->ref->tf, y_ctb - ctb_size, 0);
743  }
744  if (x_ctb && y_ctb >= s->sps->height - ctb_size)
745  ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb);
746 }
unsigned int log2_min_cb_size
Definition: hevc.h:435
void(* hevc_h_loop_filter_luma_c)(uint8_t *pix, ptrdiff_t stride, int beta, int *tc, uint8_t *no_p, uint8_t *no_q)
Definition: hevcdsp.h:91
HEVCFrame * ref
Definition: hevc.h:786
Definition: hevc.h:623
int ctb_height
Definition: hevc.h:449
uint8_t is_cu_qp_delta_coded
Definition: hevc.h:653
void(* hevc_h_loop_filter_chroma_c)(uint8_t *pix, ptrdiff_t stride, int *tc, uint8_t *no_p, uint8_t *no_q)
Definition: hevcdsp.h:97
int16_t x
horizontal component of motion vector
Definition: hevc.h:619
void(* hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride, int *tc, uint8_t *no_p, uint8_t *no_q)
Definition: hevcdsp.h:89
MvField * tab_mvf
Definition: hevc.h:668
int bs_width
Definition: hevc.h:793
AVFrame * sao_frame
Definition: hevc.h:764
static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
Clip a signed integer value into the amin-amax range.
Definition: common.h:93
int vshift[3]
Definition: hevc.h:459
int tc_offset
Definition: hevc.h:658
Definition: hevc.h:246
int min_cb_height
Definition: hevc.h:452
int list[MAX_REFS]
Definition: hevc.h:277
int stride
Definition: mace.c:144
struct HEVCSPS::@26 pcm
int width
Definition: hevc.h:446
int qp_bd_offset
Definition: hevc.h:461
static int get_qPy_pred(HEVCContext *s, int xC, int yC, int xBase, int yBase, int log2_cb_size)
Definition: hevc_filter.c:73
int pixel_shift
Definition: hevc.h:395
static void copy_CTB(uint8_t *dst, uint8_t *src, int width, int height, int stride)
Definition: hevc_filter.c:175
static void sao_filter_CTB(HEVCContext *s, int x, int y)
Definition: hevc_filter.c:189
#define LUMA
Definition: hevc_filter.c:32
int end_of_tiles_x
Definition: hevc.h:734
void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
Definition: hevc_filter.c:729
uint8_t
void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0, int log2_trafo_size)
Definition: hevc_filter.c:586
int min_tb_width
Definition: hevc.h:453
SAOParams * sao
Definition: hevc.h:782
void(* sao_band_filter[4])(uint8_t *dst, uint8_t *src, ptrdiff_t stride, struct SAOParams *sao, int *borders, int width, int height, int c_idx)
Definition: hevcdsp.h:53
int min_cb_width
Definition: hevc.h:451
void(* hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride, int beta, int *tc, uint8_t *no_p, uint8_t *no_q)
Definition: hevcdsp.h:81
ThreadFrame tf
Definition: hevc.h:667
static const uint8_t tctable[54]
Definition: hevc_filter.c:36
int8_t * qp_y_tab
Definition: hevc.h:802
uint8_t loop_filter_disable_flag
Definition: hevc.h:430
#define B
Definition: huffyuv.h:49
static const uint8_t betatable[52]
Definition: hevc_filter.c:42
uint8_t transquant_bypass_enable_flag
Definition: hevc.h:487
#define FFUMOD(a, b)
Definition: hevc.h:88
uint8_t first_qp_group
Definition: hevc.h:719
HEVCDSPContext hevcdsp
Definition: hevc.h:799
#define BOUNDARY_UPPER_SLICE
Definition: hevc.h:745
void(* sao_edge_filter[4])(uint8_t *dst, uint8_t *src, ptrdiff_t stride, struct SAOParams *sao, int *borders, int width, int height, int c_idx, uint8_t vert_edge, uint8_t horiz_edge, uint8_t diag_edge)
Definition: hevcdsp.h:56
const HEVCSPS * sps
Definition: hevc.h:769
int min_pu_height
Definition: hevc.h:456
void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
Definition: hevc_filter.c:736
RefPicList * refPicList
Definition: hevc.h:669
HEVCLocalContext HEVClc
Definition: hevc.h:756
unsigned int log2_ctb_size
Definition: hevc.h:439
#define TC_CALC(qp, bs)
Definition: hevc_filter.c:333
int8_t slice_qp
Definition: hevc.h:578
Definition: vf_drawbox.c:37
void(* hevc_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride, int *tc, uint8_t *no_p, uint8_t *no_q)
Definition: hevcdsp.h:87
uint8_t * vertical_bs
Definition: hevc.h:804
uint8_t tiles_enabled_flag
Definition: hevc.h:490
const HEVCPPS * pps
Definition: hevc.h:770
common internal API header
uint8_t is_intra
Definition: hevc.h:627
int hshift[3]
Definition: hevc.h:458
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
int8_t qp_y
Definition: hevc.h:724
Context Adaptive Binary Arithmetic Coder inline functions.
int ctb_width
Definition: hevc.h:448
int height
Definition: hevc.h:447
if(ac->has_optimized_func)
static int width
Definition: utils.c:156
int * ctb_addr_rs_to_ts
CtbAddrRSToTS.
Definition: hevc.h:521
unsigned int log2_min_pu_size
Definition: hevc.h:440
int8_t pred_flag[2]
Definition: hevc.h:626
uint8_t sao_enabled
Definition: hevc.h:418
int16_t y
vertical component of motion vector
Definition: hevc.h:620
uint8_t loop_filter_across_tiles_enabled_flag
Definition: hevc.h:496
TransformUnit tu
Definition: hevc.h:727
int cu_qp_delta
Definition: hevc.h:649
uint8_t * is_pcm
Definition: hevc.h:815
#define CTB(tab, x, y)
Definition: hevc_filter.c:187
AVFrame * frame
Definition: hevc.h:763
void(* hevc_v_loop_filter_chroma_c)(uint8_t *pix, ptrdiff_t stride, int *tc, uint8_t *no_p, uint8_t *no_q)
Definition: hevcdsp.h:100
DBParams * deblock
Definition: hevc.h:783
unsigned int log2_min_tb_size
Definition: hevc.h:437
static int get_qPy(HEVCContext *s, int xC, int yC)
Definition: hevc_filter.c:167
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
static int boundary_strength(HEVCContext *s, MvField *curr, uint8_t curr_cbf_luma, MvField *neigh, uint8_t neigh_cbf_luma, RefPicList *neigh_refPicList, int tu_border)
Definition: hevc_filter.c:508
int start_of_tiles_x
Definition: hevc.h:733
Definition: hevc.h:618
int * tile_id
TileId.
Definition: hevc.h:523
int cr_qp_offset
Definition: hevc.h:482
static int get_pcm(HEVCContext *s, int x, int y)
Definition: hevc_filter.c:317
#define DEFAULT_INTRA_TC_OFFSET
Definition: hevc.h:61
int height
Definition: gxfenc.c:72
RefPicList * ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame, int x0, int y0)
Definition: hevc_refs.c:52
void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase, int log2_cb_size)
Definition: hevc_filter.c:154
Mv mv[2]
Definition: hevc.h:624
static int chroma_tc(HEVCContext *s, int qp_y, int c_idx, int tc_offset)
Definition: hevc_filter.c:48
int8_t ref_idx[2]
Definition: hevc.h:625
uint8_t * horizontal_bs
Definition: hevc.h:803
Definition: hevc.h:245
#define BOUNDARY_LEFT_SLICE
Definition: hevc.h:743
int32_t * tab_slice_address
Definition: hevc.h:806
void(* hevc_v_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride, int beta, int *tc, uint8_t *no_p, uint8_t *no_q)
Definition: hevcdsp.h:84
#define MAX_QP
Definition: hevc.h:60
uint8_t * filter_slice_edges
Definition: hevc.h:818
uint8_t slice_loop_filter_across_slices_enabled_flag
Definition: hevc.h:562
int min_pu_width
Definition: hevc.h:455
int beta_offset
Definition: hevc.h:657
int boundary_flags
Definition: hevc.h:749
int diff_cu_qp_delta_depth
Definition: hevc.h:479
int cb_qp_offset
Definition: hevc.h:481
#define BOUNDARY_LEFT_TILE
Definition: hevc.h:744
uint8_t * cbf_luma
Definition: hevc.h:814
SliceHeader sh
Definition: hevc.h:781
exp golomb vlc stuff
int pcm_enabled_flag
Definition: hevc.h:399
void(* hevc_v_loop_filter_luma_c)(uint8_t *pix, ptrdiff_t stride, int beta, int *tc, uint8_t *no_p, uint8_t *no_q)
Definition: hevcdsp.h:94
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
#define FFMIN(a, b)
Definition: common.h:57
static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
Definition: hevc_filter.c:338
#define BOUNDARY_UPPER_TILE
Definition: hevc.h:746