FFmpeg
 All Data Structures 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  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;
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  const int bit_depth = h->sps.bit_depth_luma;
103  if (PIXEL_SHIFT) {
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  uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
119  for (j = 0; j < 8; j++) {
120  tmp_cb[j] = tmp_cr[j] = 1 << (bit_depth - 1);
121  }
122  }
123  } else {
124  for (i = 0; i < block_h; i++) {
125  uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
126  for (j = 0; j < 8; j++)
127  tmp_cb[j] = get_bits(&gb, bit_depth);
128  }
129  for (i = 0; i < block_h; i++) {
130  uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
131  for (j = 0; j < 8; j++)
132  tmp_cr[j] = get_bits(&gb, bit_depth);
133  }
134  }
135  }
136  } else {
137  for (i = 0; i < 16; i++)
138  memcpy(dest_y + i * linesize, (uint8_t *)h->mb + i * 16, 16);
139  if (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
140  if (!h->sps.chroma_format_idc) {
141  for (i = 0; i < 8; i++) {
142  memset(dest_cb + i*uvlinesize, 1 << (bit_depth - 1), 8);
143  memset(dest_cr + i*uvlinesize, 1 << (bit_depth - 1), 8);
144  }
145  } else {
146  uint8_t *src_cb = (uint8_t *)h->mb + 256;
147  uint8_t *src_cr = (uint8_t *)h->mb + 256 + block_h * 8;
148  for (i = 0; i < block_h; i++) {
149  memcpy(dest_cb + i * uvlinesize, src_cb + i * 8, 8);
150  memcpy(dest_cr + i * uvlinesize, src_cr + i * 8, 8);
151  }
152  }
153  }
154  }
155  } else {
156  if (IS_INTRA(mb_type)) {
157  if (h->deblocking_filter)
158  xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
159  uvlinesize, 1, 0, SIMPLE, PIXEL_SHIFT);
160 
161  if (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
162  h->hpc.pred8x8[h->chroma_pred_mode](dest_cb, uvlinesize);
163  h->hpc.pred8x8[h->chroma_pred_mode](dest_cr, uvlinesize);
164  }
165 
166  hl_decode_mb_predict_luma(h, mb_type, is_h264, SIMPLE,
167  transform_bypass, PIXEL_SHIFT,
168  block_offset, linesize, dest_y, 0);
169 
170  if (h->deblocking_filter)
171  xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
172  uvlinesize, 0, 0, SIMPLE, PIXEL_SHIFT);
173  } else if (is_h264) {
174  if (chroma422) {
175  FUNC(hl_motion_422)(h, dest_y, dest_cb, dest_cr,
178  h->h264dsp.weight_h264_pixels_tab,
179  h->h264dsp.biweight_h264_pixels_tab);
180  } else {
181  FUNC(hl_motion_420)(h, dest_y, dest_cb, dest_cr,
184  h->h264dsp.weight_h264_pixels_tab,
185  h->h264dsp.biweight_h264_pixels_tab);
186  }
187  }
188 
189  hl_decode_mb_idct_luma(h, mb_type, is_h264, SIMPLE, transform_bypass,
190  PIXEL_SHIFT, block_offset, linesize, dest_y, 0);
191 
192  if ((SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) &&
193  (h->cbp & 0x30)) {
194  uint8_t *dest[2] = { dest_cb, dest_cr };
195  if (transform_bypass) {
196  if (IS_INTRA(mb_type) && h->sps.profile_idc == 244 &&
197  (h->chroma_pred_mode == VERT_PRED8x8 ||
198  h->chroma_pred_mode == HOR_PRED8x8)) {
199  h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0],
200  block_offset + 16,
201  h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
202  uvlinesize);
203  h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1],
204  block_offset + 32,
205  h->mb + (16 * 16 * 2 << PIXEL_SHIFT),
206  uvlinesize);
207  } else {
208  idct_add = s->dsp.add_pixels4;
209  for (j = 1; j < 3; j++) {
210  for (i = j * 16; i < j * 16 + 4; i++)
211  if (h->non_zero_count_cache[scan8[i]] ||
212  dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
213  idct_add(dest[j - 1] + block_offset[i],
214  h->mb + (i * 16 << PIXEL_SHIFT),
215  uvlinesize);
216  if (chroma422) {
217  for (i = j * 16 + 4; i < j * 16 + 8; i++)
218  if (h->non_zero_count_cache[scan8[i + 4]] ||
219  dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
220  idct_add(dest[j - 1] + block_offset[i + 4],
221  h->mb + (i * 16 << PIXEL_SHIFT),
222  uvlinesize);
223  }
224  }
225  }
226  } else {
227  if (is_h264) {
228  int qp[2];
229  if (chroma422) {
230  qp[0] = h->chroma_qp[0] + 3;
231  qp[1] = h->chroma_qp[1] + 3;
232  } else {
233  qp[0] = h->chroma_qp[0];
234  qp[1] = h->chroma_qp[1];
235  }
236  if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
237  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
238  h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
239  if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
240  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 2 << PIXEL_SHIFT),
241  h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][qp[1]][0]);
242  h->h264dsp.h264_idct_add8(dest, block_offset,
243  h->mb, uvlinesize,
244  h->non_zero_count_cache);
245  } else if (CONFIG_SVQ3_DECODER) {
246  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 1,
247  h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][h->chroma_qp[0]][0]);
248  h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 2,
249  h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][h->chroma_qp[1]][0]);
250  for (j = 1; j < 3; j++) {
251  for (i = j * 16; i < j * 16 + 4; i++)
252  if (h->non_zero_count_cache[scan8[i]] || h->mb[i * 16]) {
253  uint8_t *const ptr = dest[j - 1] + block_offset[i];
254  ff_svq3_add_idct_c(ptr, h->mb + i * 16,
255  uvlinesize,
256  ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
257  }
258  }
259  }
260  }
261  }
262  }
263  if (h->cbp || IS_INTRA(mb_type)) {
264  s->dsp.clear_blocks(h->mb);
265  s->dsp.clear_blocks(h->mb + (24 * 16 << PIXEL_SHIFT));
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  MpegEncContext *const s = &h->s;
278  const int mb_x = s->mb_x;
279  const int mb_y = s->mb_y;
280  const int mb_xy = h->mb_xy;
281  const int mb_type = s->current_picture.f.mb_type[mb_xy];
282  uint8_t *dest[3];
283  int linesize;
284  int i, j, p;
285  int *block_offset = &h->block_offset[0];
286  const int transform_bypass = !SIMPLE && (s->qscale == 0 && h->sps.transform_bypass);
287  const int plane_count = (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) ? 3 : 1;
288 
289  for (p = 0; p < plane_count; p++) {
290  dest[p] = s->current_picture.f.data[p] +
291  ((mb_x << PIXEL_SHIFT) + mb_y * s->linesize) * 16;
292  s->vdsp.prefetch(dest[p] + (s->mb_x & 3) * 4 * s->linesize + (64 << PIXEL_SHIFT),
293  s->linesize, 4);
294  }
295 
296  h->list_counts[mb_xy] = h->list_count;
297 
298  if (!SIMPLE && MB_FIELD) {
299  linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;
300  block_offset = &h->block_offset[48];
301  if (mb_y & 1) // FIXME move out of this function?
302  for (p = 0; p < 3; p++)
303  dest[p] -= s->linesize * 15;
304  if (FRAME_MBAFF) {
305  int list;
306  for (list = 0; list < h->list_count; list++) {
307  if (!USES_LIST(mb_type, list))
308  continue;
309  if (IS_16X16(mb_type)) {
310  int8_t *ref = &h->ref_cache[list][scan8[0]];
311  fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);
312  } else {
313  for (i = 0; i < 16; i += 4) {
314  int ref = h->ref_cache[list][scan8[i]];
315  if (ref >= 0)
316  fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
317  8, (16 + ref) ^ (s->mb_y & 1), 1);
318  }
319  }
320  }
321  }
322  } else {
323  linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;
324  }
325 
326  if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
327  if (PIXEL_SHIFT) {
328  const int bit_depth = h->sps.bit_depth_luma;
329  GetBitContext gb;
330  init_get_bits(&gb, (uint8_t *)h->mb, 768 * bit_depth);
331 
332  for (p = 0; p < plane_count; p++)
333  for (i = 0; i < 16; i++) {
334  uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
335  for (j = 0; j < 16; j++)
336  tmp[j] = get_bits(&gb, bit_depth);
337  }
338  } else {
339  for (p = 0; p < plane_count; p++)
340  for (i = 0; i < 16; i++)
341  memcpy(dest[p] + i * linesize,
342  (uint8_t *)h->mb + p * 256 + i * 16, 16);
343  }
344  } else {
345  if (IS_INTRA(mb_type)) {
346  if (h->deblocking_filter)
347  xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
348  linesize, 1, 1, SIMPLE, PIXEL_SHIFT);
349 
350  for (p = 0; p < plane_count; p++)
351  hl_decode_mb_predict_luma(h, mb_type, 1, SIMPLE,
352  transform_bypass, PIXEL_SHIFT,
353  block_offset, linesize, dest[p], p);
354 
355  if (h->deblocking_filter)
356  xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
357  linesize, 0, 1, SIMPLE, PIXEL_SHIFT);
358  } else {
359  FUNC(hl_motion_444)(h, dest[0], dest[1], dest[2],
362  h->h264dsp.weight_h264_pixels_tab,
363  h->h264dsp.biweight_h264_pixels_tab);
364  }
365 
366  for (p = 0; p < plane_count; p++)
367  hl_decode_mb_idct_luma(h, mb_type, 1, SIMPLE, transform_bypass,
368  PIXEL_SHIFT, block_offset, linesize,
369  dest[p], p);
370  }
371  if (h->cbp || IS_INTRA(mb_type)) {
372  s->dsp.clear_blocks(h->mb);
373  s->dsp.clear_blocks(h->mb + (24 * 16 << PIXEL_SHIFT));
374  }
375 }
376 
377 #endif