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 #undef FUNC
23 #undef PIXEL_SHIFT
24 
25 #if SIMPLE
26 # define FUNC(n) AV_JOIN(n ## _simple_, BITS)
27 # define PIXEL_SHIFT (BITS >> 4)
28 #else
29 # define FUNC(n) n ## _complex
30 # define PIXEL_SHIFT h->pixel_shift
31 #endif
32 
33 #undef CHROMA_IDC
34 #define CHROMA_IDC 1
35 #include "h264_mc_template.c"
36 
37 #undef CHROMA_IDC
38 #define CHROMA_IDC 2
39 #include "h264_mc_template.c"
40 
42 {
43  MpegEncContext *const s = &h->s;
44  const int mb_x = s->mb_x;
45  const int mb_y = s->mb_y;
46  const int mb_xy = h->mb_xy;
47  const int mb_type = s->current_picture.f.mb_type[mb_xy];
48  uint8_t *dest_y, *dest_cb, *dest_cr;
49  int linesize, uvlinesize /*dct_offset*/;
50  int i, j;
51  int *block_offset = &h->block_offset[0];
52  const int transform_bypass = !SIMPLE && (s->qscale == 0 && h->sps.transform_bypass);
53  /* is_h264 should always be true if SVQ3 is disabled. */
54  const int is_h264 = !CONFIG_SVQ3_DECODER || SIMPLE || s->codec_id == AV_CODEC_ID_H264;
55  void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
56  const int block_h = 16 >> s->chroma_y_shift;
57  const int chroma422 = CHROMA422;
58 
59  dest_y = s->current_picture.f.data[0] + ((mb_x << PIXEL_SHIFT) + mb_y * s->linesize) * 16;
60  dest_cb = s->current_picture.f.data[1] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * s->uvlinesize * block_h;
61  dest_cr = s->current_picture.f.data[2] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * s->uvlinesize * block_h;
62 
63  s->vdsp.prefetch(dest_y + (s->mb_x & 3) * 4 * s->linesize + (64 << PIXEL_SHIFT), s->linesize, 4);
64  s->vdsp.prefetch(dest_cb + (s->mb_x & 7) * s->uvlinesize + (64 << PIXEL_SHIFT), dest_cr - dest_cb, 2);
65 
66  h->list_counts[mb_xy] = h->list_count;
67 
68  if (!SIMPLE && MB_FIELD) {
69  linesize = h->mb_linesize = s->linesize * 2;
70  uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
71  block_offset = &h->block_offset[48];
72  if (mb_y & 1) { // FIXME move out of this function?
73  dest_y -= s->linesize * 15;
74  dest_cb -= s->uvlinesize * (block_h - 1);
75  dest_cr -= s->uvlinesize * (block_h - 1);
76  }
77  if (FRAME_MBAFF) {
78  int list;
79  for (list = 0; list < h->list_count; list++) {
80  if (!USES_LIST(mb_type, list))
81  continue;
82  if (IS_16X16(mb_type)) {
83  int8_t *ref = &h->ref_cache[list][scan8[0]];
84  fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);
85  } else {
86  for (i = 0; i < 16; i += 4) {
87  int ref = h->ref_cache[list][scan8[i]];
88  if (ref >= 0)
89  fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
90  8, (16 + ref) ^ (s->mb_y & 1), 1);
91  }
92  }
93  }
94  }
95  } else {
96  linesize = h->mb_linesize = s->linesize;
97  uvlinesize = h->mb_uvlinesize = s->uvlinesize;
98  // dct_offset = s->linesize * 16;
99  }
100 
101  if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
102  if (PIXEL_SHIFT) {
103  const int bit_depth = h->sps.bit_depth_luma;
104  int j;
105  GetBitContext gb;
106  init_get_bits(&gb, (uint8_t *)h->mb,
107  ff_h264_mb_sizes[h->sps.chroma_format_idc] * bit_depth);
108 
109  for (i = 0; i < 16; i++) {
110  uint16_t *tmp_y = (uint16_t *)(dest_y + i * linesize);
111  for (j = 0; j < 16; j++)
112  tmp_y[j] = get_bits(&gb, bit_depth);
113  }
114  if (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
115  if (!h->sps.chroma_format_idc) {
116  for (i = 0; i < block_h; i++) {
117  uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
118  for (j = 0; j < 8; j++)
119  tmp_cb[j] = 1 << (bit_depth - 1);
120  }
121  for (i = 0; i < block_h; i++) {
122  uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
123  for (j = 0; j < 8; j++)
124  tmp_cr[j] = 1 << (bit_depth - 1);
125  }
126  } else {
127  for (i = 0; i < block_h; i++) {
128  uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
129  for (j = 0; j < 8; j++)
130  tmp_cb[j] = get_bits(&gb, bit_depth);
131  }
132  for (i = 0; i < block_h; i++) {
133  uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
134  for (j = 0; j < 8; j++)
135  tmp_cr[j] = get_bits(&gb, bit_depth);
136  }
137  }
138  }
139  } else {
140  for (i = 0; i < 16; i++)
141  memcpy(dest_y + i * linesize, (uint8_t *)h->mb + i * 16, 16);
142  if (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
143  if (!h->sps.chroma_format_idc) {
144  for (i = 0; i < block_h; i++) {
145  memset(dest_cb + i * uvlinesize, 128, 8);
146  memset(dest_cr + i * uvlinesize, 128, 8);
147  }
148  } else {
149  uint8_t *src_cb = (uint8_t *)h->mb + 256;
150  uint8_t *src_cr = (uint8_t *)h->mb + 256 + block_h * 8;
151  for (i = 0; i < block_h; i++) {
152  memcpy(dest_cb + i * uvlinesize, src_cb + i * 8, 8);
153  memcpy(dest_cr + i * uvlinesize, src_cr + i * 8, 8);
154  }
155  }
156  }
157  }
158  } else {
159  if (IS_INTRA(mb_type)) {
160  if (h->deblocking_filter)
161  xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
162  uvlinesize, 1, 0, SIMPLE, PIXEL_SHIFT);
163 
164  if (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
165  h->hpc.pred8x8[h->chroma_pred_mode](dest_cb, uvlinesize);
166  h->hpc.pred8x8[h->chroma_pred_mode](dest_cr, uvlinesize);
167  }
168 
169  hl_decode_mb_predict_luma(h, mb_type, is_h264, SIMPLE,
170  transform_bypass, PIXEL_SHIFT,
171  block_offset, linesize, dest_y, 0);
172 
173  if (h->deblocking_filter)
174  xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
175  uvlinesize, 0, 0, SIMPLE, PIXEL_SHIFT);
176  } else if (is_h264) {
177  if (chroma422) {
178  FUNC(hl_motion_422)(h, dest_y, dest_cb, dest_cr,
181  h->h264dsp.weight_h264_pixels_tab,
182  h->h264dsp.biweight_h264_pixels_tab);
183  } else {
184  FUNC(hl_motion_420)(h, dest_y, dest_cb, dest_cr,
187  h->h264dsp.weight_h264_pixels_tab,
188  h->h264dsp.biweight_h264_pixels_tab);
189  }
190  }
191 
192  hl_decode_mb_idct_luma(h, mb_type, is_h264, SIMPLE, transform_bypass,
193  PIXEL_SHIFT, block_offset, linesize, dest_y, 0);
194 
195  if ((SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) &&
196  (h->cbp & 0x30)) {
197  uint8_t *dest[2] = { dest_cb, dest_cr };
198  if (transform_bypass) {
199  if (IS_INTRA(mb_type) && h->sps.profile_idc == 244 &&
200  (h->chroma_pred_mode == VERT_PRED8x8 ||
201  h->chroma_pred_mode == HOR_PRED8x8)) {
202  h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0],
203  block_offset + 16,
204  h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
205  uvlinesize);
206  h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1],
207  block_offset + 32,
208  h->mb + (16 * 16 * 2 << PIXEL_SHIFT),
209  uvlinesize);
210  } else {
211  idct_add = s->dsp.add_pixels4;
212  for (j = 1; j < 3; j++) {
213  for (i = j * 16; i < j * 16 + 4; i++)
214  if (h->non_zero_count_cache[scan8[i]] ||
215  dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
216  idct_add(dest[j - 1] + block_offset[i],
217  h->mb + (i * 16 << PIXEL_SHIFT),
218  uvlinesize);
219  if (chroma422) {
220  for (i = j * 16 + 4; i < j * 16 + 8; i++)
221  if (h->non_zero_count_cache[scan8[i + 4]] ||
222  dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
223  idct_add(dest[j - 1] + block_offset[i + 4],
224  h->mb + (i * 16 << PIXEL_SHIFT),
225  uvlinesize);
226  }
227  }
228  }
229  } else {
230  if (is_h264) {
231  int qp[2];
232  if (chroma422) {
233  qp[0] = h->chroma_qp[0] + 3;
234  qp[1] = h->chroma_qp[1] + 3;
235  } else {
236  qp[0] = h->chroma_qp[0];
237  qp[1] = h->chroma_qp[1];
238  }
239  if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
240  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
241  h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
242  if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
243  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 2 << PIXEL_SHIFT),
244  h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][qp[1]][0]);
245  h->h264dsp.h264_idct_add8(dest, block_offset,
246  h->mb, uvlinesize,
247  h->non_zero_count_cache);
248  } else if (CONFIG_SVQ3_DECODER) {
249  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 1,
250  h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][h->chroma_qp[0]][0]);
251  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 2,
252  h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][h->chroma_qp[1]][0]);
253  for (j = 1; j < 3; j++) {
254  for (i = j * 16; i < j * 16 + 4; i++)
255  if (h->non_zero_count_cache[scan8[i]] || h->mb[i * 16]) {
256  uint8_t *const ptr = dest[j - 1] + block_offset[i];
257  ff_svq3_add_idct_c(ptr, h->mb + i * 16,
258  uvlinesize,
259  ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
260  }
261  }
262  }
263  }
264  }
265  }
266  if (h->cbp || IS_INTRA(mb_type)) {
267  s->dsp.clear_blocks(h->mb);
268  s->dsp.clear_blocks(h->mb + (24 * 16 << PIXEL_SHIFT));
269  }
270 }
271 
272 #if !SIMPLE || BITS == 8
273 
274 #undef CHROMA_IDC
275 #define CHROMA_IDC 3
276 #include "h264_mc_template.c"
277 
279 {
280  MpegEncContext *const s = &h->s;
281  const int mb_x = s->mb_x;
282  const int mb_y = s->mb_y;
283  const int mb_xy = h->mb_xy;
284  const int mb_type = s->current_picture.f.mb_type[mb_xy];
285  uint8_t *dest[3];
286  int linesize;
287  int i, j, p;
288  int *block_offset = &h->block_offset[0];
289  const int transform_bypass = !SIMPLE && (s->qscale == 0 && h->sps.transform_bypass);
290  const int plane_count = (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) ? 3 : 1;
291 
292  for (p = 0; p < plane_count; p++) {
293  dest[p] = s->current_picture.f.data[p] +
294  ((mb_x << PIXEL_SHIFT) + mb_y * s->linesize) * 16;
295  s->vdsp.prefetch(dest[p] + (s->mb_x & 3) * 4 * s->linesize + (64 << PIXEL_SHIFT),
296  s->linesize, 4);
297  }
298 
299  h->list_counts[mb_xy] = h->list_count;
300 
301  if (!SIMPLE && MB_FIELD) {
302  linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;
303  block_offset = &h->block_offset[48];
304  if (mb_y & 1) // FIXME move out of this function?
305  for (p = 0; p < 3; p++)
306  dest[p] -= s->linesize * 15;
307  if (FRAME_MBAFF) {
308  int list;
309  for (list = 0; list < h->list_count; list++) {
310  if (!USES_LIST(mb_type, list))
311  continue;
312  if (IS_16X16(mb_type)) {
313  int8_t *ref = &h->ref_cache[list][scan8[0]];
314  fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);
315  } else {
316  for (i = 0; i < 16; i += 4) {
317  int ref = h->ref_cache[list][scan8[i]];
318  if (ref >= 0)
319  fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
320  8, (16 + ref) ^ (s->mb_y & 1), 1);
321  }
322  }
323  }
324  }
325  } else {
326  linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;
327  }
328 
329  if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
330  if (PIXEL_SHIFT) {
331  const int bit_depth = h->sps.bit_depth_luma;
332  GetBitContext gb;
333  init_get_bits(&gb, (uint8_t *)h->mb, 768 * bit_depth);
334 
335  for (p = 0; p < plane_count; p++)
336  for (i = 0; i < 16; i++) {
337  uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
338  for (j = 0; j < 16; j++)
339  tmp[j] = get_bits(&gb, bit_depth);
340  }
341  } else {
342  for (p = 0; p < plane_count; p++)
343  for (i = 0; i < 16; i++)
344  memcpy(dest[p] + i * linesize,
345  (uint8_t *)h->mb + p * 256 + i * 16, 16);
346  }
347  } else {
348  if (IS_INTRA(mb_type)) {
349  if (h->deblocking_filter)
350  xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
351  linesize, 1, 1, SIMPLE, PIXEL_SHIFT);
352 
353  for (p = 0; p < plane_count; p++)
354  hl_decode_mb_predict_luma(h, mb_type, 1, SIMPLE,
355  transform_bypass, PIXEL_SHIFT,
356  block_offset, linesize, dest[p], p);
357 
358  if (h->deblocking_filter)
359  xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
360  linesize, 0, 1, SIMPLE, PIXEL_SHIFT);
361  } else {
362  FUNC(hl_motion_444)(h, dest[0], dest[1], dest[2],
365  h->h264dsp.weight_h264_pixels_tab,
366  h->h264dsp.biweight_h264_pixels_tab);
367  }
368 
369  for (p = 0; p < plane_count; p++)
370  hl_decode_mb_idct_luma(h, mb_type, 1, SIMPLE, transform_bypass,
371  PIXEL_SHIFT, block_offset, linesize,
372  dest[p], p);
373  }
374  if (h->cbp || IS_INTRA(mb_type)) {
375  s->dsp.clear_blocks(h->mb);
376  s->dsp.clear_blocks(h->mb + (24 * 16 << PIXEL_SHIFT));
377  }
378 }
379 
380 #endif
#define VERT_PRED8x8
Definition: h264pred.h:70
void(* prefetch)(uint8_t *buf, ptrdiff_t stride, int h)
Prefetch memory into cache (if supported by hardware).
Definition: videodsp.h:61
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.c:1777
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
#define CHROMA422
Definition: h264.h:88
enum AVCodecID codec_id
Definition: mpegvideo.h:227
#define CONFIG_SVQ3_DECODER
Definition: config.h:492
static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth, int index)
Definition: h264.c:1759
H264Context.
Definition: h264.h:254
#define HOR_PRED8x8
Definition: h264pred.h:69
int stride
Definition: mace.c:144
void ff_svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc)
Definition: svq3.c:175
int qscale
QP.
Definition: mpegvideo.h:342
#define IS_INTRA_PCM(a)
Definition: mpegvideo.h:111
#define USES_LIST(a, list)
does this mb use listX, note does not work if subMBs
Definition: mpegvideo.h:126
#define CONFIG_GRAY
Definition: config.h:277
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.c:1895
uint8_t
h264_chroma_mc_func avg_h264_chroma_pixels_tab[3]
Definition: dsputil.h:322
int mb_xy
Definition: h264.h:427
qpel_mc_func(* qpel_put)[16]
Definition: mpegvideo.h:198
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:313
static av_noinline void FUNC() hl_decode_mb_444(H264Context *h)
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:76
int chroma_y_shift
Definition: mpegvideo.h:657
#define CHROMA_DC_BLOCK_INDEX
Definition: h264.h:734
#define IS_INTRA(a)
Definition: mpegvideo.h:108
h264_chroma_mc_func put_h264_chroma_pixels_tab[3]
h264 Chroma MC
Definition: dsputil.h:321
static const uint8_t scan8[16 *3+3]
Definition: h264.h:737
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.c:1670
#define FRAME_MBAFF
Definition: h264.h:64
static DCTELEM block[64]
Definition: dct-test.c:169
static void fill_rectangle(SDL_Surface *screen, int x, int y, int w, int h, int color)
Definition: avplay.c:401
#define MB_FIELD
Definition: h264.h:63
MotionEstContext me
Definition: mpegvideo.h:405
static void idct_add(uint8_t *dest, int line_size, DCTELEM *block)
Definition: dsputil_sh4.c:73
uint32_t * mb_type
macroblock type table mb_type_base + mb_width + 2
Definition: avcodec.h:1180
MpegEncContext s
Definition: h264.h:255
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
DSPContext dsp
pointers for accelerated dsp functions
Definition: mpegvideo.h:361
short DCTELEM
Definition: dsputil.h:39
#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:316
static av_noinline void FUNC() hl_decode_mb(H264Context *h)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
qpel_mc_func(* qpel_avg)[16]
Definition: mpegvideo.h:199
#define SIMPLE
Definition: h264.c:1975
void(* clear_blocks)(DCTELEM *blocks)
Definition: dsputil.h:219
MpegEncContext.
Definition: mpegvideo.h:211
VideoDSPContext vdsp
Definition: mpegvideo.h:362
const uint16_t ff_h264_mb_sizes[4]
Definition: h264.c:48
void(* add_pixels4)(uint8_t *pixels, DCTELEM *block, int line_size)
Definition: dsputil.h:207
int linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:253
#define IS_16X16(a)
Definition: mpegvideo.h:115
struct AVFrame f
Definition: mpegvideo.h:95
int flags
AVCodecContext.flags (HQ, MV4, ...)
Definition: mpegvideo.h:230
int uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:254
#define PIXEL_SHIFT
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)