FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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  const int mb_x = sl->mb_x;
44  const int mb_y = sl->mb_y;
45  const int mb_xy = sl->mb_xy;
46  const int mb_type = h->cur_pic.mb_type[mb_xy];
47  uint8_t *dest_y, *dest_cb, *dest_cr;
48  int linesize, uvlinesize /*dct_offset*/;
49  int i, j;
50  const int *block_offset = &h->block_offset[0];
51  const int transform_bypass = !SIMPLE && (sl->qscale == 0 && h->sps.transform_bypass);
52  void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
53  const int block_h = 16 >> h->chroma_y_shift;
54  const int chroma422 = CHROMA422(h);
55 
56  dest_y = h->cur_pic.f->data[0] + ((mb_x << PIXEL_SHIFT) + mb_y * sl->linesize) * 16;
57  dest_cb = h->cur_pic.f->data[1] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * sl->uvlinesize * block_h;
58  dest_cr = h->cur_pic.f->data[2] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * sl->uvlinesize * block_h;
59 
60  h->vdsp.prefetch(dest_y + (sl->mb_x & 3) * 4 * sl->linesize + (64 << PIXEL_SHIFT), sl->linesize, 4);
61  h->vdsp.prefetch(dest_cb + (sl->mb_x & 7) * sl->uvlinesize + (64 << PIXEL_SHIFT), dest_cr - dest_cb, 2);
62 
63  h->list_counts[mb_xy] = sl->list_count;
64 
65  if (!SIMPLE && MB_FIELD(sl)) {
66  linesize = sl->mb_linesize = sl->linesize * 2;
67  uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
68  block_offset = &h->block_offset[48];
69  if (mb_y & 1) { // FIXME move out of this function?
70  dest_y -= sl->linesize * 15;
71  dest_cb -= sl->uvlinesize * (block_h - 1);
72  dest_cr -= sl->uvlinesize * (block_h - 1);
73  }
74  if (FRAME_MBAFF(h)) {
75  int list;
76  for (list = 0; list < sl->list_count; list++) {
77  if (!USES_LIST(mb_type, list))
78  continue;
79  if (IS_16X16(mb_type)) {
80  int8_t *ref = &sl->ref_cache[list][scan8[0]];
81  fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (sl->mb_y & 1), 1);
82  } else {
83  for (i = 0; i < 16; i += 4) {
84  int ref = sl->ref_cache[list][scan8[i]];
85  if (ref >= 0)
86  fill_rectangle(&sl->ref_cache[list][scan8[i]], 2, 2,
87  8, (16 + ref) ^ (sl->mb_y & 1), 1);
88  }
89  }
90  }
91  }
92  } else {
93  linesize = sl->mb_linesize = sl->linesize;
94  uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
95  // dct_offset = s->linesize * 16;
96  }
97 
98  if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
99  const int bit_depth = h->sps.bit_depth_luma;
100  if (PIXEL_SHIFT) {
101  int j;
102  GetBitContext gb;
103  init_get_bits(&gb, sl->intra_pcm_ptr,
104  ff_h264_mb_sizes[h->sps.chroma_format_idc] * bit_depth);
105 
106  for (i = 0; i < 16; i++) {
107  uint16_t *tmp_y = (uint16_t *)(dest_y + i * linesize);
108  for (j = 0; j < 16; j++)
109  tmp_y[j] = get_bits(&gb, bit_depth);
110  }
111  if (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
112  if (!h->sps.chroma_format_idc) {
113  for (i = 0; i < block_h; i++) {
114  uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
115  uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
116  for (j = 0; j < 8; j++) {
117  tmp_cb[j] = tmp_cr[j] = 1 << (bit_depth - 1);
118  }
119  }
120  } else {
121  for (i = 0; i < block_h; i++) {
122  uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
123  for (j = 0; j < 8; j++)
124  tmp_cb[j] = get_bits(&gb, bit_depth);
125  }
126  for (i = 0; i < block_h; i++) {
127  uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
128  for (j = 0; j < 8; j++)
129  tmp_cr[j] = get_bits(&gb, bit_depth);
130  }
131  }
132  }
133  } else {
134  for (i = 0; i < 16; i++)
135  memcpy(dest_y + i * linesize, sl->intra_pcm_ptr + i * 16, 16);
136  if (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
137  if (!h->sps.chroma_format_idc) {
138  for (i = 0; i < 8; i++) {
139  memset(dest_cb + i * uvlinesize, 1 << (bit_depth - 1), 8);
140  memset(dest_cr + i * uvlinesize, 1 << (bit_depth - 1), 8);
141  }
142  } else {
143  const uint8_t *src_cb = sl->intra_pcm_ptr + 256;
144  const uint8_t *src_cr = sl->intra_pcm_ptr + 256 + block_h * 8;
145  for (i = 0; i < block_h; i++) {
146  memcpy(dest_cb + i * uvlinesize, src_cb + i * 8, 8);
147  memcpy(dest_cr + i * uvlinesize, src_cr + i * 8, 8);
148  }
149  }
150  }
151  }
152  } else {
153  if (IS_INTRA(mb_type)) {
154  if (sl->deblocking_filter)
155  xchg_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
156  uvlinesize, 1, 0, SIMPLE, PIXEL_SHIFT);
157 
158  if (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
159  h->hpc.pred8x8[sl->chroma_pred_mode](dest_cb, uvlinesize);
160  h->hpc.pred8x8[sl->chroma_pred_mode](dest_cr, uvlinesize);
161  }
162 
163  hl_decode_mb_predict_luma(h, sl, mb_type, SIMPLE,
164  transform_bypass, PIXEL_SHIFT,
165  block_offset, linesize, dest_y, 0);
166 
167  if (sl->deblocking_filter)
168  xchg_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
169  uvlinesize, 0, 0, SIMPLE, PIXEL_SHIFT);
170  } else {
171  if (chroma422) {
172  FUNC(hl_motion_422)(h, sl, dest_y, dest_cb, dest_cr,
173  h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
174  h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
175  h->h264dsp.weight_h264_pixels_tab,
176  h->h264dsp.biweight_h264_pixels_tab);
177  } else {
178  FUNC(hl_motion_420)(h, sl, dest_y, dest_cb, dest_cr,
179  h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
180  h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
181  h->h264dsp.weight_h264_pixels_tab,
182  h->h264dsp.biweight_h264_pixels_tab);
183  }
184  }
185 
186  hl_decode_mb_idct_luma(h, sl, mb_type, SIMPLE, transform_bypass,
187  PIXEL_SHIFT, block_offset, linesize, dest_y, 0);
188 
189  if ((SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) &&
190  (sl->cbp & 0x30)) {
191  uint8_t *dest[2] = { dest_cb, dest_cr };
192  if (transform_bypass) {
193  if (IS_INTRA(mb_type) && h->sps.profile_idc == 244 &&
194  (sl->chroma_pred_mode == VERT_PRED8x8 ||
195  sl->chroma_pred_mode == HOR_PRED8x8)) {
196  h->hpc.pred8x8_add[sl->chroma_pred_mode](dest[0],
197  block_offset + 16,
198  sl->mb + (16 * 16 * 1 << PIXEL_SHIFT),
199  uvlinesize);
200  h->hpc.pred8x8_add[sl->chroma_pred_mode](dest[1],
201  block_offset + 32,
202  sl->mb + (16 * 16 * 2 << PIXEL_SHIFT),
203  uvlinesize);
204  } else {
205  idct_add = h->h264dsp.h264_add_pixels4_clear;
206  for (j = 1; j < 3; j++) {
207  for (i = j * 16; i < j * 16 + 4; i++)
208  if (sl->non_zero_count_cache[scan8[i]] ||
209  dctcoef_get(sl->mb, PIXEL_SHIFT, i * 16))
210  idct_add(dest[j - 1] + block_offset[i],
211  sl->mb + (i * 16 << PIXEL_SHIFT),
212  uvlinesize);
213  if (chroma422) {
214  for (i = j * 16 + 4; i < j * 16 + 8; i++)
215  if (sl->non_zero_count_cache[scan8[i + 4]] ||
216  dctcoef_get(sl->mb, PIXEL_SHIFT, i * 16))
217  idct_add(dest[j - 1] + block_offset[i + 4],
218  sl->mb + (i * 16 << PIXEL_SHIFT),
219  uvlinesize);
220  }
221  }
222  }
223  } else {
224  int qp[2];
225  if (chroma422) {
226  qp[0] = sl->chroma_qp[0] + 3;
227  qp[1] = sl->chroma_qp[1] + 3;
228  } else {
229  qp[0] = sl->chroma_qp[0];
230  qp[1] = sl->chroma_qp[1];
231  }
232  if (sl->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
233  h->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + (16 * 16 * 1 << PIXEL_SHIFT),
234  h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
235  if (sl->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
236  h->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + (16 * 16 * 2 << PIXEL_SHIFT),
237  h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][qp[1]][0]);
238  h->h264dsp.h264_idct_add8(dest, block_offset,
239  sl->mb, uvlinesize,
240  sl->non_zero_count_cache);
241  }
242  }
243  }
244 }
245 
246 #if !SIMPLE || BITS == 8
247 
248 #undef CHROMA_IDC
249 #define CHROMA_IDC 3
250 #include "h264_mc_template.c"
251 
253 {
254  const int mb_x = sl->mb_x;
255  const int mb_y = sl->mb_y;
256  const int mb_xy = sl->mb_xy;
257  const int mb_type = h->cur_pic.mb_type[mb_xy];
258  uint8_t *dest[3];
259  int linesize;
260  int i, j, p;
261  const int *block_offset = &h->block_offset[0];
262  const int transform_bypass = !SIMPLE && (sl->qscale == 0 && h->sps.transform_bypass);
263  const int plane_count = (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) ? 3 : 1;
264 
265  for (p = 0; p < plane_count; p++) {
266  dest[p] = h->cur_pic.f->data[p] +
267  ((mb_x << PIXEL_SHIFT) + mb_y * sl->linesize) * 16;
268  h->vdsp.prefetch(dest[p] + (sl->mb_x & 3) * 4 * sl->linesize + (64 << PIXEL_SHIFT),
269  sl->linesize, 4);
270  }
271 
272  h->list_counts[mb_xy] = sl->list_count;
273 
274  if (!SIMPLE && MB_FIELD(sl)) {
275  linesize = sl->mb_linesize = sl->mb_uvlinesize = sl->linesize * 2;
276  block_offset = &h->block_offset[48];
277  if (mb_y & 1) // FIXME move out of this function?
278  for (p = 0; p < 3; p++)
279  dest[p] -= sl->linesize * 15;
280  if (FRAME_MBAFF(h)) {
281  int list;
282  for (list = 0; list < sl->list_count; list++) {
283  if (!USES_LIST(mb_type, list))
284  continue;
285  if (IS_16X16(mb_type)) {
286  int8_t *ref = &sl->ref_cache[list][scan8[0]];
287  fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (sl->mb_y & 1), 1);
288  } else {
289  for (i = 0; i < 16; i += 4) {
290  int ref = sl->ref_cache[list][scan8[i]];
291  if (ref >= 0)
292  fill_rectangle(&sl->ref_cache[list][scan8[i]], 2, 2,
293  8, (16 + ref) ^ (sl->mb_y & 1), 1);
294  }
295  }
296  }
297  }
298  } else {
299  linesize = sl->mb_linesize = sl->mb_uvlinesize = sl->linesize;
300  }
301 
302  if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
303  if (PIXEL_SHIFT) {
304  const int bit_depth = h->sps.bit_depth_luma;
305  GetBitContext gb;
306  init_get_bits(&gb, sl->intra_pcm_ptr, 768 * bit_depth);
307 
308  for (p = 0; p < plane_count; p++)
309  for (i = 0; i < 16; i++) {
310  uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
311  for (j = 0; j < 16; j++)
312  tmp[j] = get_bits(&gb, bit_depth);
313  }
314  } else {
315  for (p = 0; p < plane_count; p++)
316  for (i = 0; i < 16; i++)
317  memcpy(dest[p] + i * linesize,
318  sl->intra_pcm_ptr + p * 256 + i * 16, 16);
319  }
320  } else {
321  if (IS_INTRA(mb_type)) {
322  if (sl->deblocking_filter)
323  xchg_mb_border(h, sl, dest[0], dest[1], dest[2], linesize,
324  linesize, 1, 1, SIMPLE, PIXEL_SHIFT);
325 
326  for (p = 0; p < plane_count; p++)
327  hl_decode_mb_predict_luma(h, sl, mb_type, SIMPLE,
328  transform_bypass, PIXEL_SHIFT,
329  block_offset, linesize, dest[p], p);
330 
331  if (sl->deblocking_filter)
332  xchg_mb_border(h, sl, dest[0], dest[1], dest[2], linesize,
333  linesize, 0, 1, SIMPLE, PIXEL_SHIFT);
334  } else {
335  FUNC(hl_motion_444)(h, sl, dest[0], dest[1], dest[2],
336  h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
337  h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
338  h->h264dsp.weight_h264_pixels_tab,
339  h->h264dsp.biweight_h264_pixels_tab);
340  }
341 
342  for (p = 0; p < plane_count; p++)
343  hl_decode_mb_idct_luma(h, sl, mb_type, SIMPLE, transform_bypass,
344  PIXEL_SHIFT, block_offset, linesize,
345  dest[p], p);
346  }
347 }
348 
349 #endif
#define VERT_PRED8x8
Definition: h264pred.h:70
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
H264Context.
Definition: h264.h:514
#define HOR_PRED8x8
Definition: h264pred.h:69
#define USES_LIST(a, list)
Definition: mpegutils.h:101
#define MB_FIELD(sl)
Definition: h264.h:75
uint8_t
static void fill_rectangle(SDL_Surface *screen, int x, int y, int w, int h, int color, int update)
Definition: ffplay.c:805
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:855
static int ref[MAX_W *MAX_W]
#define CHROMA_DC_BLOCK_INDEX
Definition: h264.h:971
static const uint8_t scan8[16 *3+3]
Definition: h264.h:974
#define IS_INTRA_PCM(a)
Definition: mpegutils.h:84
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static uint8_t tmp[8]
Definition: des-test.c:38
static av_always_inline void hl_decode_mb_idct_luma(const H264Context *h, H264SliceContext *sl, int mb_type, int simple, int transform_bypass, int pixel_shift, const int *block_offset, int linesize, uint8_t *dest_y, int p)
Definition: h264_mb.c:726
static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth, int index)
Definition: h264_mb.c:594
#define IS_16X16(a)
Definition: mpegutils.h:88
#define CHROMA422(h)
Definition: h264.h:101
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:418
#define FUNC(n)
static av_always_inline void xchg_mb_border(const H264Context *h, H264SliceContext *sl, 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:507
static av_always_inline void hl_decode_mb_predict_luma(const H264Context *h, H264SliceContext *sl, int mb_type, int simple, int transform_bypass, int pixel_shift, const int *block_offset, int linesize, uint8_t *dest_y, int p)
Definition: h264_mb.c:612
#define SIMPLE
Definition: h264_mb.c:795
if(ret< 0)
Definition: vf_mcdeint.c:282
static unsigned bit_depth(uint64_t mask)
Definition: af_astats.c:142
static av_noinline void FUNC() hl_decode_mb(const H264Context *h, H264SliceContext *sl)
#define IS_INTRA(x, y)
#define FRAME_MBAFF(h)
Definition: h264.h:76
const uint16_t ff_h264_mb_sizes[4]
Definition: h264.c:58
#define av_noinline
Definition: attributes.h:62
#define stride
static av_noinline void FUNC() hl_decode_mb_444(const H264Context *h, H264SliceContext *sl)
#define PIXEL_SHIFT
static int16_t block[64]
Definition: dct-test.c:112