h264: remove the svq3-specific code
[libav.git] / libavcodec / h264_mb_template.c
CommitLineData
28fff0d9
MR
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
6490a0c0 41static av_noinline void FUNC(hl_decode_mb)(const H264Context *h, H264SliceContext *sl)
28fff0d9 42{
d4d9068c
AK
43 const int mb_x = sl->mb_x;
44 const int mb_y = sl->mb_y;
0edbe6fa 45 const int mb_xy = sl->mb_xy;
759001c5 46 const int mb_type = h->cur_pic.mb_type[mb_xy];
28fff0d9
MR
47 uint8_t *dest_y, *dest_cb, *dest_cr;
48 int linesize, uvlinesize /*dct_offset*/;
49 int i, j;
51822879 50 const int *block_offset = &h->block_offset[0];
d231e84b 51 const int transform_bypass = !SIMPLE && (sl->qscale == 0 && h->sps.transform_bypass);
88bd7fdc 52 void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
2c541554 53 const int block_h = 16 >> h->chroma_y_shift;
e962bd08 54 const int chroma422 = CHROMA422(h);
28fff0d9 55
a0f29460
AK
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;
28fff0d9 59
c28ed1d7
AK
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);
28fff0d9 62
95eb35f3 63 h->list_counts[mb_xy] = sl->list_count;
28fff0d9 64
bc98e8c0 65 if (!SIMPLE && MB_FIELD(sl)) {
c28ed1d7
AK
66 linesize = sl->mb_linesize = sl->linesize * 2;
67 uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
28fff0d9
MR
68 block_offset = &h->block_offset[48];
69 if (mb_y & 1) { // FIXME move out of this function?
c28ed1d7
AK
70 dest_y -= sl->linesize * 15;
71 dest_cb -= sl->uvlinesize * (block_h - 1);
72 dest_cr -= sl->uvlinesize * (block_h - 1);
28fff0d9 73 }
7bece9b2 74 if (FRAME_MBAFF(h)) {
28fff0d9 75 int list;
95eb35f3 76 for (list = 0; list < sl->list_count; list++) {
28fff0d9
MR
77 if (!USES_LIST(mb_type, list))
78 continue;
79 if (IS_16X16(mb_type)) {
e6287f07 80 int8_t *ref = &sl->ref_cache[list][scan8[0]];
d4d9068c 81 fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (sl->mb_y & 1), 1);
28fff0d9
MR
82 } else {
83 for (i = 0; i < 16; i += 4) {
e6287f07 84 int ref = sl->ref_cache[list][scan8[i]];
28fff0d9 85 if (ref >= 0)
e6287f07 86 fill_rectangle(&sl->ref_cache[list][scan8[i]], 2, 2,
d4d9068c 87 8, (16 + ref) ^ (sl->mb_y & 1), 1);
28fff0d9
MR
88 }
89 }
90 }
91 }
92 } else {
c28ed1d7
AK
93 linesize = sl->mb_linesize = sl->linesize;
94 uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
28fff0d9
MR
95 // dct_offset = s->linesize * 16;
96 }
97
98 if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
99 if (PIXEL_SHIFT) {
100 const int bit_depth = h->sps.bit_depth_luma;
101 int j;
102 GetBitContext gb;
b063582e 103 init_get_bits(&gb, sl->intra_pcm_ptr,
28fff0d9
MR
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 }
7c6eb0a1 111 if (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
28fff0d9
MR
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 for (j = 0; j < 8; j++)
116 tmp_cb[j] = 1 << (bit_depth - 1);
117 }
118 for (i = 0; i < block_h; i++) {
119 uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
120 for (j = 0; j < 8; j++)
121 tmp_cr[j] = 1 << (bit_depth - 1);
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++)
b063582e 138 memcpy(dest_y + i * linesize, sl->intra_pcm_ptr + i * 16, 16);
7c6eb0a1 139 if (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
28fff0d9
MR
140 if (!h->sps.chroma_format_idc) {
141 for (i = 0; i < block_h; i++) {
142 memset(dest_cb + i * uvlinesize, 128, 8);
143 memset(dest_cr + i * uvlinesize, 128, 8);
144 }
145 } else {
b063582e
AK
146 const uint8_t *src_cb = sl->intra_pcm_ptr + 256;
147 const uint8_t *src_cr = sl->intra_pcm_ptr + 256 + block_h * 8;
28fff0d9
MR
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)) {
e6c90ce9 157 if (sl->deblocking_filter)
8b00f4df 158 xchg_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
28fff0d9
MR
159 uvlinesize, 1, 0, SIMPLE, PIXEL_SHIFT);
160
7c6eb0a1 161 if (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
4bd5ac20
AK
162 h->hpc.pred8x8[sl->chroma_pred_mode](dest_cb, uvlinesize);
163 h->hpc.pred8x8[sl->chroma_pred_mode](dest_cr, uvlinesize);
28fff0d9
MR
164 }
165
9b30f8dd 166 hl_decode_mb_predict_luma(h, sl, mb_type, SIMPLE,
28fff0d9
MR
167 transform_bypass, PIXEL_SHIFT,
168 block_offset, linesize, dest_y, 0);
169
e6c90ce9 170 if (sl->deblocking_filter)
8b00f4df 171 xchg_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
28fff0d9 172 uvlinesize, 0, 0, SIMPLE, PIXEL_SHIFT);
9b30f8dd 173 } else {
28fff0d9 174 if (chroma422) {
92c6c2a6 175 FUNC(hl_motion_422)(h, sl, dest_y, dest_cb, dest_cr,
136034d8
VG
176 h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
177 h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
28fff0d9
MR
178 h->h264dsp.weight_h264_pixels_tab,
179 h->h264dsp.biweight_h264_pixels_tab);
180 } else {
92c6c2a6 181 FUNC(hl_motion_420)(h, sl, dest_y, dest_cb, dest_cr,
136034d8
VG
182 h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
183 h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
28fff0d9
MR
184 h->h264dsp.weight_h264_pixels_tab,
185 h->h264dsp.biweight_h264_pixels_tab);
186 }
187 }
188
9b30f8dd 189 hl_decode_mb_idct_luma(h, sl, mb_type, SIMPLE, transform_bypass,
28fff0d9
MR
190 PIXEL_SHIFT, block_offset, linesize, dest_y, 0);
191
7c6eb0a1 192 if ((SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) &&
e7226984 193 (sl->cbp & 0x30)) {
28fff0d9
MR
194 uint8_t *dest[2] = { dest_cb, dest_cr };
195 if (transform_bypass) {
196 if (IS_INTRA(mb_type) && h->sps.profile_idc == 244 &&
4bd5ac20
AK
197 (sl->chroma_pred_mode == VERT_PRED8x8 ||
198 sl->chroma_pred_mode == HOR_PRED8x8)) {
199 h->hpc.pred8x8_add[sl->chroma_pred_mode](dest[0],
28fff0d9 200 block_offset + 16,
bf03a878 201 sl->mb + (16 * 16 * 1 << PIXEL_SHIFT),
28fff0d9 202 uvlinesize);
4bd5ac20 203 h->hpc.pred8x8_add[sl->chroma_pred_mode](dest[1],
28fff0d9 204 block_offset + 32,
bf03a878 205 sl->mb + (16 * 16 * 2 << PIXEL_SHIFT),
28fff0d9
MR
206 uvlinesize);
207 } else {
62844c3f 208 idct_add = h->h264dsp.h264_add_pixels4_clear;
28fff0d9
MR
209 for (j = 1; j < 3; j++) {
210 for (i = j * 16; i < j * 16 + 4; i++)
f69574cf 211 if (sl->non_zero_count_cache[scan8[i]] ||
bf03a878 212 dctcoef_get(sl->mb, PIXEL_SHIFT, i * 16))
28fff0d9 213 idct_add(dest[j - 1] + block_offset[i],
bf03a878 214 sl->mb + (i * 16 << PIXEL_SHIFT),
28fff0d9
MR
215 uvlinesize);
216 if (chroma422) {
217 for (i = j * 16 + 4; i < j * 16 + 8; i++)
f69574cf 218 if (sl->non_zero_count_cache[scan8[i + 4]] ||
bf03a878 219 dctcoef_get(sl->mb, PIXEL_SHIFT, i * 16))
28fff0d9 220 idct_add(dest[j - 1] + block_offset[i + 4],
bf03a878 221 sl->mb + (i * 16 << PIXEL_SHIFT),
28fff0d9
MR
222 uvlinesize);
223 }
224 }
225 }
226 } else {
9b30f8dd
AK
227 int qp[2];
228 if (chroma422) {
229 qp[0] = sl->chroma_qp[0] + 3;
230 qp[1] = sl->chroma_qp[1] + 3;
231 } else {
232 qp[0] = sl->chroma_qp[0];
233 qp[1] = sl->chroma_qp[1];
28fff0d9 234 }
9b30f8dd
AK
235 if (sl->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
236 h->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + (16 * 16 * 1 << PIXEL_SHIFT),
237 h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
238 if (sl->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
239 h->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + (16 * 16 * 2 << PIXEL_SHIFT),
240 h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][qp[1]][0]);
241 h->h264dsp.h264_idct_add8(dest, block_offset,
242 sl->mb, uvlinesize,
243 sl->non_zero_count_cache);
28fff0d9
MR
244 }
245 }
28fff0d9
MR
246 }
247}
248
249#if !SIMPLE || BITS == 8
250
251#undef CHROMA_IDC
252#define CHROMA_IDC 3
253#include "h264_mc_template.c"
254
6490a0c0 255static av_noinline void FUNC(hl_decode_mb_444)(const H264Context *h, H264SliceContext *sl)
28fff0d9 256{
d4d9068c
AK
257 const int mb_x = sl->mb_x;
258 const int mb_y = sl->mb_y;
0edbe6fa 259 const int mb_xy = sl->mb_xy;
759001c5 260 const int mb_type = h->cur_pic.mb_type[mb_xy];
28fff0d9
MR
261 uint8_t *dest[3];
262 int linesize;
263 int i, j, p;
51822879 264 const int *block_offset = &h->block_offset[0];
d231e84b 265 const int transform_bypass = !SIMPLE && (sl->qscale == 0 && h->sps.transform_bypass);
7c6eb0a1 266 const int plane_count = (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) ? 3 : 1;
28fff0d9
MR
267
268 for (p = 0; p < plane_count; p++) {
a0f29460 269 dest[p] = h->cur_pic.f->data[p] +
c28ed1d7
AK
270 ((mb_x << PIXEL_SHIFT) + mb_y * sl->linesize) * 16;
271 h->vdsp.prefetch(dest[p] + (sl->mb_x & 3) * 4 * sl->linesize + (64 << PIXEL_SHIFT),
272 sl->linesize, 4);
28fff0d9
MR
273 }
274
95eb35f3 275 h->list_counts[mb_xy] = sl->list_count;
28fff0d9 276
bc98e8c0 277 if (!SIMPLE && MB_FIELD(sl)) {
c28ed1d7 278 linesize = sl->mb_linesize = sl->mb_uvlinesize = sl->linesize * 2;
28fff0d9
MR
279 block_offset = &h->block_offset[48];
280 if (mb_y & 1) // FIXME move out of this function?
281 for (p = 0; p < 3; p++)
c28ed1d7 282 dest[p] -= sl->linesize * 15;
7bece9b2 283 if (FRAME_MBAFF(h)) {
28fff0d9 284 int list;
95eb35f3 285 for (list = 0; list < sl->list_count; list++) {
28fff0d9
MR
286 if (!USES_LIST(mb_type, list))
287 continue;
288 if (IS_16X16(mb_type)) {
e6287f07 289 int8_t *ref = &sl->ref_cache[list][scan8[0]];
d4d9068c 290 fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (sl->mb_y & 1), 1);
28fff0d9
MR
291 } else {
292 for (i = 0; i < 16; i += 4) {
e6287f07 293 int ref = sl->ref_cache[list][scan8[i]];
28fff0d9 294 if (ref >= 0)
e6287f07 295 fill_rectangle(&sl->ref_cache[list][scan8[i]], 2, 2,
d4d9068c 296 8, (16 + ref) ^ (sl->mb_y & 1), 1);
28fff0d9
MR
297 }
298 }
299 }
300 }
301 } else {
c28ed1d7 302 linesize = sl->mb_linesize = sl->mb_uvlinesize = sl->linesize;
28fff0d9
MR
303 }
304
305 if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
306 if (PIXEL_SHIFT) {
307 const int bit_depth = h->sps.bit_depth_luma;
308 GetBitContext gb;
b063582e 309 init_get_bits(&gb, sl->intra_pcm_ptr, 768 * bit_depth);
28fff0d9
MR
310
311 for (p = 0; p < plane_count; p++)
312 for (i = 0; i < 16; i++) {
313 uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
314 for (j = 0; j < 16; j++)
315 tmp[j] = get_bits(&gb, bit_depth);
316 }
317 } else {
318 for (p = 0; p < plane_count; p++)
319 for (i = 0; i < 16; i++)
320 memcpy(dest[p] + i * linesize,
b063582e 321 sl->intra_pcm_ptr + p * 256 + i * 16, 16);
28fff0d9
MR
322 }
323 } else {
324 if (IS_INTRA(mb_type)) {
e6c90ce9 325 if (sl->deblocking_filter)
8b00f4df 326 xchg_mb_border(h, sl, dest[0], dest[1], dest[2], linesize,
28fff0d9
MR
327 linesize, 1, 1, SIMPLE, PIXEL_SHIFT);
328
329 for (p = 0; p < plane_count; p++)
9b30f8dd 330 hl_decode_mb_predict_luma(h, sl, mb_type, SIMPLE,
28fff0d9
MR
331 transform_bypass, PIXEL_SHIFT,
332 block_offset, linesize, dest[p], p);
333
e6c90ce9 334 if (sl->deblocking_filter)
8b00f4df 335 xchg_mb_border(h, sl, dest[0], dest[1], dest[2], linesize,
28fff0d9
MR
336 linesize, 0, 1, SIMPLE, PIXEL_SHIFT);
337 } else {
92c6c2a6 338 FUNC(hl_motion_444)(h, sl, dest[0], dest[1], dest[2],
136034d8
VG
339 h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
340 h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
28fff0d9
MR
341 h->h264dsp.weight_h264_pixels_tab,
342 h->h264dsp.biweight_h264_pixels_tab);
343 }
344
345 for (p = 0; p < plane_count; p++)
9b30f8dd 346 hl_decode_mb_idct_luma(h, sl, mb_type, SIMPLE, transform_bypass,
28fff0d9
MR
347 PIXEL_SHIFT, block_offset, linesize,
348 dest[p], p);
28fff0d9
MR
349 }
350}
351
352#endif