Libav
h264_mb_template.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "svq3.h"
23 
24 #undef FUNC
25 #undef PIXEL_SHIFT
26 
27 #if SIMPLE
28 # define FUNC(n) AV_JOIN(n ## _simple_, BITS)
29 # define PIXEL_SHIFT (BITS >> 4)
30 #else
31 # define FUNC(n) n ## _complex
32 # define PIXEL_SHIFT h->pixel_shift
33 #endif
34 
35 #undef CHROMA_IDC
36 #define CHROMA_IDC 1
37 #include "h264_mc_template.c"
38 
39 #undef CHROMA_IDC
40 #define CHROMA_IDC 2
41 #include "h264_mc_template.c"
42 
44 {
45  const int mb_x = h->mb_x;
46  const int mb_y = h->mb_y;
47  const int mb_xy = h->mb_xy;
48  const int mb_type = h->cur_pic.mb_type[mb_xy];
49  uint8_t *dest_y, *dest_cb, *dest_cr;
50  int linesize, uvlinesize /*dct_offset*/;
51  int i, j;
52  int *block_offset = &h->block_offset[0];
53  const int transform_bypass = !SIMPLE && (h->qscale == 0 && h->sps.transform_bypass);
54  /* is_h264 should always be true if SVQ3 is disabled. */
55  const int is_h264 = !CONFIG_SVQ3_DECODER || SIMPLE || h->avctx->codec_id == AV_CODEC_ID_H264;
56  void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
57  const int block_h = 16 >> h->chroma_y_shift;
58  const int chroma422 = CHROMA422(h);
59 
60  dest_y = h->cur_pic.f.data[0] + ((mb_x << PIXEL_SHIFT) + mb_y * h->linesize) * 16;
61  dest_cb = h->cur_pic.f.data[1] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * h->uvlinesize * block_h;
62  dest_cr = h->cur_pic.f.data[2] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * h->uvlinesize * block_h;
63 
64  h->vdsp.prefetch(dest_y + (h->mb_x & 3) * 4 * h->linesize + (64 << PIXEL_SHIFT), h->linesize, 4);
65  h->vdsp.prefetch(dest_cb + (h->mb_x & 7) * h->uvlinesize + (64 << PIXEL_SHIFT), dest_cr - dest_cb, 2);
66 
67  h->list_counts[mb_xy] = h->list_count;
68 
69  if (!SIMPLE && MB_FIELD(h)) {
70  linesize = h->mb_linesize = h->linesize * 2;
71  uvlinesize = h->mb_uvlinesize = h->uvlinesize * 2;
72  block_offset = &h->block_offset[48];
73  if (mb_y & 1) { // FIXME move out of this function?
74  dest_y -= h->linesize * 15;
75  dest_cb -= h->uvlinesize * (block_h - 1);
76  dest_cr -= h->uvlinesize * (block_h - 1);
77  }
78  if (FRAME_MBAFF(h)) {
79  int list;
80  for (list = 0; list < h->list_count; list++) {
81  if (!USES_LIST(mb_type, list))
82  continue;
83  if (IS_16X16(mb_type)) {
84  int8_t *ref = &h->ref_cache[list][scan8[0]];
85  fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (h->mb_y & 1), 1);
86  } else {
87  for (i = 0; i < 16; i += 4) {
88  int ref = h->ref_cache[list][scan8[i]];
89  if (ref >= 0)
90  fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
91  8, (16 + ref) ^ (h->mb_y & 1), 1);
92  }
93  }
94  }
95  }
96  } else {
97  linesize = h->mb_linesize = h->linesize;
98  uvlinesize = h->mb_uvlinesize = h->uvlinesize;
99  // dct_offset = s->linesize * 16;
100  }
101 
102  if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
103  if (PIXEL_SHIFT) {
104  const int bit_depth = h->sps.bit_depth_luma;
105  int j;
107  init_get_bits(&gb, h->intra_pcm_ptr,
108  ff_h264_mb_sizes[h->sps.chroma_format_idc] * bit_depth);
109 
110  for (i = 0; i < 16; i++) {
111  uint16_t *tmp_y = (uint16_t *)(dest_y + i * linesize);
112  for (j = 0; j < 16; j++)
113  tmp_y[j] = get_bits(&gb, bit_depth);
114  }
115  if (SIMPLE || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
116  if (!h->sps.chroma_format_idc) {
117  for (i = 0; i < block_h; i++) {
118  uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
119  for (j = 0; j < 8; j++)
120  tmp_cb[j] = 1 << (bit_depth - 1);
121  }
122  for (i = 0; i < block_h; i++) {
123  uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
124  for (j = 0; j < 8; j++)
125  tmp_cr[j] = 1 << (bit_depth - 1);
126  }
127  } else {
128  for (i = 0; i < block_h; i++) {
129  uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
130  for (j = 0; j < 8; j++)
131  tmp_cb[j] = get_bits(&gb, bit_depth);
132  }
133  for (i = 0; i < block_h; i++) {
134  uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
135  for (j = 0; j < 8; j++)
136  tmp_cr[j] = get_bits(&gb, bit_depth);
137  }
138  }
139  }
140  } else {
141  for (i = 0; i < 16; i++)
142  memcpy(dest_y + i * linesize, h->intra_pcm_ptr + i * 16, 16);
143  if (SIMPLE || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
144  if (!h->sps.chroma_format_idc) {
145  for (i = 0; i < block_h; i++) {
146  memset(dest_cb + i * uvlinesize, 128, 8);
147  memset(dest_cr + i * uvlinesize, 128, 8);
148  }
149  } else {
150  const uint8_t *src_cb = h->intra_pcm_ptr + 256;
151  const uint8_t *src_cr = h->intra_pcm_ptr + 256 + block_h * 8;
152  for (i = 0; i < block_h; i++) {
153  memcpy(dest_cb + i * uvlinesize, src_cb + i * 8, 8);
154  memcpy(dest_cr + i * uvlinesize, src_cr + i * 8, 8);
155  }
156  }
157  }
158  }
159  } else {
160  if (IS_INTRA(mb_type)) {
161  if (h->deblocking_filter)
162  xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
163  uvlinesize, 1, 0, SIMPLE, PIXEL_SHIFT);
164 
165  if (SIMPLE || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
166  h->hpc.pred8x8[h->chroma_pred_mode](dest_cb, uvlinesize);
167  h->hpc.pred8x8[h->chroma_pred_mode](dest_cr, uvlinesize);
168  }
169 
170  hl_decode_mb_predict_luma(h, mb_type, is_h264, SIMPLE,
171  transform_bypass, PIXEL_SHIFT,
172  block_offset, linesize, dest_y, 0);
173 
174  if (h->deblocking_filter)
175  xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
176  uvlinesize, 0, 0, SIMPLE, PIXEL_SHIFT);
177  } else if (is_h264) {
178  if (chroma422) {
179  FUNC(hl_motion_422)(h, dest_y, dest_cb, dest_cr,
180  h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
181  h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
182  h->h264dsp.weight_h264_pixels_tab,
183  h->h264dsp.biweight_h264_pixels_tab);
184  } else {
185  FUNC(hl_motion_420)(h, dest_y, dest_cb, dest_cr,
186  h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
187  h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
188  h->h264dsp.weight_h264_pixels_tab,
189  h->h264dsp.biweight_h264_pixels_tab);
190  }
191  }
192 
193  hl_decode_mb_idct_luma(h, mb_type, is_h264, SIMPLE, transform_bypass,
194  PIXEL_SHIFT, block_offset, linesize, dest_y, 0);
195 
196  if ((SIMPLE || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) &&
197  (h->cbp & 0x30)) {
198  uint8_t *dest[2] = { dest_cb, dest_cr };
199  if (transform_bypass) {
200  if (IS_INTRA(mb_type) && h->sps.profile_idc == 244 &&
201  (h->chroma_pred_mode == VERT_PRED8x8 ||
202  h->chroma_pred_mode == HOR_PRED8x8)) {
203  h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0],
204  block_offset + 16,
205  h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
206  uvlinesize);
207  h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1],
208  block_offset + 32,
209  h->mb + (16 * 16 * 2 << PIXEL_SHIFT),
210  uvlinesize);
211  } else {
212  idct_add = h->h264dsp.h264_add_pixels4_clear;
213  for (j = 1; j < 3; j++) {
214  for (i = j * 16; i < j * 16 + 4; i++)
215  if (h->non_zero_count_cache[scan8[i]] ||
216  dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
217  idct_add(dest[j - 1] + block_offset[i],
218  h->mb + (i * 16 << PIXEL_SHIFT),
219  uvlinesize);
220  if (chroma422) {
221  for (i = j * 16 + 4; i < j * 16 + 8; i++)
222  if (h->non_zero_count_cache[scan8[i + 4]] ||
223  dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
224  idct_add(dest[j - 1] + block_offset[i + 4],
225  h->mb + (i * 16 << PIXEL_SHIFT),
226  uvlinesize);
227  }
228  }
229  }
230  } else {
231  if (is_h264) {
232  int qp[2];
233  if (chroma422) {
234  qp[0] = h->chroma_qp[0] + 3;
235  qp[1] = h->chroma_qp[1] + 3;
236  } else {
237  qp[0] = h->chroma_qp[0];
238  qp[1] = h->chroma_qp[1];
239  }
240  if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
241  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
242  h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
243  if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
244  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 2 << PIXEL_SHIFT),
245  h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][qp[1]][0]);
246  h->h264dsp.h264_idct_add8(dest, block_offset,
247  h->mb, uvlinesize,
248  h->non_zero_count_cache);
249  } else if (CONFIG_SVQ3_DECODER) {
250  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 1,
251  h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][h->chroma_qp[0]][0]);
252  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 2,
253  h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][h->chroma_qp[1]][0]);
254  for (j = 1; j < 3; j++) {
255  for (i = j * 16; i < j * 16 + 4; i++)
256  if (h->non_zero_count_cache[scan8[i]] || h->mb[i * 16]) {
257  uint8_t *const ptr = dest[j - 1] + block_offset[i];
258  ff_svq3_add_idct_c(ptr, h->mb + i * 16,
259  uvlinesize,
260  ff_h264_chroma_qp[0][h->qscale + 12] - 12, 2);
261  }
262  }
263  }
264  }
265  }
266  }
267 }
268 
269 #if !SIMPLE || BITS == 8
270 
271 #undef CHROMA_IDC
272 #define CHROMA_IDC 3
273 #include "h264_mc_template.c"
274 
276 {
277  const int mb_x = h->mb_x;
278  const int mb_y = h->mb_y;
279  const int mb_xy = h->mb_xy;
280  const int mb_type = h->cur_pic.mb_type[mb_xy];
281  uint8_t *dest[3];
282  int linesize;
283  int i, j, p;
284  int *block_offset = &h->block_offset[0];
285  const int transform_bypass = !SIMPLE && (h->qscale == 0 && h->sps.transform_bypass);
286  const int plane_count = (SIMPLE || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) ? 3 : 1;
287 
288  for (p = 0; p < plane_count; p++) {
289  dest[p] = h->cur_pic.f.data[p] +
290  ((mb_x << PIXEL_SHIFT) + mb_y * h->linesize) * 16;
291  h->vdsp.prefetch(dest[p] + (h->mb_x & 3) * 4 * h->linesize + (64 << PIXEL_SHIFT),
292  h->linesize, 4);
293  }
294 
295  h->list_counts[mb_xy] = h->list_count;
296 
297  if (!SIMPLE && MB_FIELD(h)) {
298  linesize = h->mb_linesize = h->mb_uvlinesize = h->linesize * 2;
299  block_offset = &h->block_offset[48];
300  if (mb_y & 1) // FIXME move out of this function?
301  for (p = 0; p < 3; p++)
302  dest[p] -= h->linesize * 15;
303  if (FRAME_MBAFF(h)) {
304  int list;
305  for (list = 0; list < h->list_count; list++) {
306  if (!USES_LIST(mb_type, list))
307  continue;
308  if (IS_16X16(mb_type)) {
309  int8_t *ref = &h->ref_cache[list][scan8[0]];
310  fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (h->mb_y & 1), 1);
311  } else {
312  for (i = 0; i < 16; i += 4) {
313  int ref = h->ref_cache[list][scan8[i]];
314  if (ref >= 0)
315  fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
316  8, (16 + ref) ^ (h->mb_y & 1), 1);
317  }
318  }
319  }
320  }
321  } else {
322  linesize = h->mb_linesize = h->mb_uvlinesize = h->linesize;
323  }
324 
325  if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
326  if (PIXEL_SHIFT) {
327  const int bit_depth = h->sps.bit_depth_luma;
329  init_get_bits(&gb, h->intra_pcm_ptr, 768 * bit_depth);
330 
331  for (p = 0; p < plane_count; p++)
332  for (i = 0; i < 16; i++) {
333  uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
334  for (j = 0; j < 16; j++)
335  tmp[j] = get_bits(&gb, bit_depth);
336  }
337  } else {
338  for (p = 0; p < plane_count; p++)
339  for (i = 0; i < 16; i++)
340  memcpy(dest[p] + i * linesize,
341  h->intra_pcm_ptr + p * 256 + i * 16, 16);
342  }
343  } else {
344  if (IS_INTRA(mb_type)) {
345  if (h->deblocking_filter)
346  xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
347  linesize, 1, 1, SIMPLE, PIXEL_SHIFT);
348 
349  for (p = 0; p < plane_count; p++)
350  hl_decode_mb_predict_luma(h, mb_type, 1, SIMPLE,
351  transform_bypass, PIXEL_SHIFT,
352  block_offset, linesize, dest[p], p);
353 
354  if (h->deblocking_filter)
355  xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
356  linesize, 0, 1, SIMPLE, PIXEL_SHIFT);
357  } else {
358  FUNC(hl_motion_444)(h, dest[0], dest[1], dest[2],
359  h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
360  h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
361  h->h264dsp.weight_h264_pixels_tab,
362  h->h264dsp.biweight_h264_pixels_tab);
363  }
364 
365  for (p = 0; p < plane_count; p++)
366  hl_decode_mb_idct_luma(h, mb_type, 1, SIMPLE, transform_bypass,
367  PIXEL_SHIFT, block_offset, linesize,
368  dest[p], p);
369  }
370 }
371 
372 #endif
#define VERT_PRED8x8
Definition: h264pred.h:70
GetBitContext gb
Definition: h264.h:311
static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int chroma444, int simple, int pixel_shift)
Definition: h264_mb.c:498
ptrdiff_t uvlinesize
Definition: h264.h:325
#define CONFIG_SVQ3_DECODER
Definition: config.h:593
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
int mb_y
Definition: h264.h:498
H264Context.
Definition: h264.h:303
#define HOR_PRED8x8
Definition: h264pred.h:69
int stride
Definition: mace.c:144
#define USES_LIST(a, list)
Definition: mpegutils.h:95
uint8_t
int mb_xy
Definition: h264.h:505
static av_noinline void FUNC() hl_decode_mb_444(H264Context *h)
int mb_x
Definition: h264.h:498
const uint8_t ff_h264_chroma_qp[3][QP_MAX_NUM+1]
One chroma qp table for each supported bit depth (8, 9, 10).
Definition: h264_ps.c:73
#define CHROMA_DC_BLOCK_INDEX
Definition: h264.h:865
static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass, int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
Definition: h264_mb.c:722
#define MB_FIELD(h)
Definition: h264.h:70
static const uint8_t scan8[16 *3+3]
Definition: h264.h:868
void ff_svq3_add_idct_c(uint8_t *dst, int16_t *block, int stride, int qp, int dc)
Definition: svq3.c:197
#define IS_INTRA_PCM(a)
Definition: mpegutils.h:78
ptrdiff_t linesize
Definition: h264.h:325
#define CONFIG_GRAY
Definition: config.h:359
static void fill_rectangle(SDL_Surface *screen, int x, int y, int w, int h, int color)
Definition: avplay.c:396
if(ac->has_optimized_func)
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth, int index)
Definition: h264_mb.c:588
#define IS_16X16(a)
Definition: mpegutils.h:82
#define CHROMA422(h)
Definition: h264.h:95
static av_always_inline void hl_decode_mb_predict_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass, int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
Definition: h264_mb.c:606
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
#define FUNC(n)
int block_offset[2 *(16 *3)]
block_offset[ 0..23] for frame macroblocks block_offset[24..47] for field macroblocks ...
Definition: h264.h:392
static av_noinline void FUNC() hl_decode_mb(H264Context *h)
#define SIMPLE
Definition: h264_mb.c:801
#define CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:637
#define IS_INTRA(x, y)
#define FRAME_MBAFF(h)
Definition: h264.h:71
const uint16_t ff_h264_mb_sizes[4]
Definition: h264.c:52
#define av_noinline
Definition: attributes.h:48
#define PIXEL_SHIFT
static int16_t block[64]
Definition: dct-test.c:88