vc1dec: take ME precision into account while scaling MV predictors.
[libav.git] / libavcodec / vc1dec.c
CommitLineData
b761659b
DB
1/*
2 * VC-1 and WMV3 decoder
cad16562 3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
b761659b
DB
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 *
2912e87a 7 * This file is part of Libav.
b761659b 8 *
2912e87a 9 * Libav is free software; you can redistribute it and/or
b761659b
DB
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
2912e87a 14 * Libav is distributed in the hope that it will be useful,
b761659b
DB
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
2912e87a 20 * License along with Libav; if not, write to the Free Software
b761659b
DB
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24/**
ba87f080 25 * @file
b761659b 26 * VC-1 and WMV3 decoder
b761659b 27 */
50f97219 28
b761659b
DB
29#include "internal.h"
30#include "dsputil.h"
31#include "avcodec.h"
32#include "mpegvideo.h"
d68b27a9 33#include "h263.h"
b761659b
DB
34#include "vc1.h"
35#include "vc1data.h"
36#include "vc1acdata.h"
37#include "msmpeg4data.h"
38#include "unary.h"
39#include "simple_idct.h"
40#include "mathops.h"
41#include "vdpau_internal.h"
42
43#undef NDEBUG
44#include <assert.h>
45
46#define MB_INTRA_VLC_BITS 9
47#define DC_VLC_BITS 9
48#define AC_VLC_BITS 9
b761659b
DB
49
50
51static const uint16_t vlc_offs[] = {
cad16562
MSS
52 0, 520, 552, 616, 1128, 1160, 1224, 1740, 1772, 1836, 1900, 2436,
53 2986, 3050, 3610, 4154, 4218, 4746, 5326, 5390, 5902, 6554, 7658, 8342,
54 9304, 9988, 10630, 11234, 12174, 13006, 13560, 14232, 14786, 15432, 16350, 17522,
55 20372, 21818, 22330, 22394, 23166, 23678, 23742, 24820, 25332, 25396, 26460, 26980,
56 27048, 27592, 27600, 27608, 27616, 27624, 28224, 28258, 28290, 28802, 28834, 28866,
57 29378, 29412, 29444, 29960, 29994, 30026, 30538, 30572, 30604, 31120, 31154, 31186,
58 31714, 31746, 31778, 32306, 32340, 32372
b761659b
DB
59};
60
cad16562 61// offset tables for interlaced picture MVDATA decoding
50f97219
KS
62static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
63static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
cad16562 64
b761659b
DB
65/**
66 * Init VC-1 specific tables and VC1Context members
67 * @param v The VC1Context to initialize
68 * @return Status
69 */
70static int vc1_init_common(VC1Context *v)
71{
72 static int done = 0;
73 int i = 0;
cad16562 74 static VLC_TYPE vlc_table[32372][2];
b761659b
DB
75
76 v->hrd_rate = v->hrd_buffer = NULL;
77
78 /* VLC tables */
50f97219 79 if (!done) {
b761659b 80 INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
50f97219
KS
81 ff_vc1_bfraction_bits, 1, 1,
82 ff_vc1_bfraction_codes, 1, 1, 1 << VC1_BFRACTION_VLC_BITS);
b761659b 83 INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
50f97219
KS
84 ff_vc1_norm2_bits, 1, 1,
85 ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
b761659b 86 INIT_VLC_STATIC(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
50f97219
KS
87 ff_vc1_norm6_bits, 1, 1,
88 ff_vc1_norm6_codes, 2, 2, 556);
b761659b 89 INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
50f97219
KS
90 ff_vc1_imode_bits, 1, 1,
91 ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
92 for (i = 0; i < 3; i++) {
93 ff_vc1_ttmb_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 0]];
94 ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i * 3 + 1] - vlc_offs[i * 3 + 0];
b761659b
DB
95 init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
96 ff_vc1_ttmb_bits[i], 1, 1,
97 ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
50f97219
KS
98 ff_vc1_ttblk_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 1]];
99 ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i * 3 + 2] - vlc_offs[i * 3 + 1];
b761659b
DB
100 init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
101 ff_vc1_ttblk_bits[i], 1, 1,
102 ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
50f97219
KS
103 ff_vc1_subblkpat_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 2]];
104 ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i * 3 + 3] - vlc_offs[i * 3 + 2];
b761659b
DB
105 init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
106 ff_vc1_subblkpat_bits[i], 1, 1,
107 ff_vc1_subblkpat_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
108 }
50f97219
KS
109 for (i = 0; i < 4; i++) {
110 ff_vc1_4mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 9]];
111 ff_vc1_4mv_block_pattern_vlc[i].table_allocated = vlc_offs[i * 3 + 10] - vlc_offs[i * 3 + 9];
b761659b
DB
112 init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
113 ff_vc1_4mv_block_pattern_bits[i], 1, 1,
114 ff_vc1_4mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
50f97219
KS
115 ff_vc1_cbpcy_p_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 10]];
116 ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i * 3 + 11] - vlc_offs[i * 3 + 10];
b761659b
DB
117 init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
118 ff_vc1_cbpcy_p_bits[i], 1, 1,
119 ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
50f97219
KS
120 ff_vc1_mv_diff_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 11]];
121 ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i * 3 + 12] - vlc_offs[i * 3 + 11];
b761659b
DB
122 init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
123 ff_vc1_mv_diff_bits[i], 1, 1,
124 ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
125 }
50f97219
KS
126 for (i = 0; i < 8; i++) {
127 ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i * 2 + 21]];
128 ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i * 2 + 22] - vlc_offs[i * 2 + 21];
b761659b
DB
129 init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
130 &vc1_ac_tables[i][0][1], 8, 4,
131 &vc1_ac_tables[i][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC);
cad16562 132 /* initialize interlaced MVDATA tables (2-Ref) */
50f97219
KS
133 ff_vc1_2ref_mvdata_vlc[i].table = &vlc_table[vlc_offs[i * 2 + 22]];
134 ff_vc1_2ref_mvdata_vlc[i].table_allocated = vlc_offs[i * 2 + 23] - vlc_offs[i * 2 + 22];
cad16562
MSS
135 init_vlc(&ff_vc1_2ref_mvdata_vlc[i], VC1_2REF_MVDATA_VLC_BITS, 126,
136 ff_vc1_2ref_mvdata_bits[i], 1, 1,
137 ff_vc1_2ref_mvdata_codes[i], 4, 4, INIT_VLC_USE_NEW_STATIC);
138 }
50f97219 139 for (i = 0; i < 4; i++) {
cad16562 140 /* initialize 4MV MBMODE VLC tables for interlaced frame P picture */
50f97219
KS
141 ff_vc1_intfr_4mv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 37]];
142 ff_vc1_intfr_4mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 38] - vlc_offs[i * 3 + 37];
cad16562
MSS
143 init_vlc(&ff_vc1_intfr_4mv_mbmode_vlc[i], VC1_INTFR_4MV_MBMODE_VLC_BITS, 15,
144 ff_vc1_intfr_4mv_mbmode_bits[i], 1, 1,
145 ff_vc1_intfr_4mv_mbmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
146 /* initialize NON-4MV MBMODE VLC tables for the same */
50f97219
KS
147 ff_vc1_intfr_non4mv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 38]];
148 ff_vc1_intfr_non4mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 39] - vlc_offs[i * 3 + 38];
cad16562
MSS
149 init_vlc(&ff_vc1_intfr_non4mv_mbmode_vlc[i], VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 9,
150 ff_vc1_intfr_non4mv_mbmode_bits[i], 1, 1,
151 ff_vc1_intfr_non4mv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
152 /* initialize interlaced MVDATA tables (1-Ref) */
50f97219
KS
153 ff_vc1_1ref_mvdata_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 39]];
154 ff_vc1_1ref_mvdata_vlc[i].table_allocated = vlc_offs[i * 3 + 40] - vlc_offs[i * 3 + 39];
cad16562
MSS
155 init_vlc(&ff_vc1_1ref_mvdata_vlc[i], VC1_1REF_MVDATA_VLC_BITS, 72,
156 ff_vc1_1ref_mvdata_bits[i], 1, 1,
157 ff_vc1_1ref_mvdata_codes[i], 4, 4, INIT_VLC_USE_NEW_STATIC);
158 }
50f97219 159 for (i = 0; i < 4; i++) {
cad16562 160 /* Initialize 2MV Block pattern VLC tables */
50f97219
KS
161 ff_vc1_2mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i + 49]];
162 ff_vc1_2mv_block_pattern_vlc[i].table_allocated = vlc_offs[i + 50] - vlc_offs[i + 49];
cad16562 163 init_vlc(&ff_vc1_2mv_block_pattern_vlc[i], VC1_2MV_BLOCK_PATTERN_VLC_BITS, 4,
50f97219
KS
164 ff_vc1_2mv_block_pattern_bits[i], 1, 1,
165 ff_vc1_2mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
cad16562 166 }
50f97219 167 for (i = 0; i < 8; i++) {
cad16562 168 /* Initialize interlaced CBPCY VLC tables (Table 124 - Table 131) */
50f97219
KS
169 ff_vc1_icbpcy_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 53]];
170 ff_vc1_icbpcy_vlc[i].table_allocated = vlc_offs[i * 3 + 54] - vlc_offs[i * 3 + 53];
cad16562 171 init_vlc(&ff_vc1_icbpcy_vlc[i], VC1_ICBPCY_VLC_BITS, 63,
50f97219
KS
172 ff_vc1_icbpcy_p_bits[i], 1, 1,
173 ff_vc1_icbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
cad16562 174 /* Initialize interlaced field picture MBMODE VLC tables */
50f97219
KS
175 ff_vc1_if_mmv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 54]];
176 ff_vc1_if_mmv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 55] - vlc_offs[i * 3 + 54];
cad16562 177 init_vlc(&ff_vc1_if_mmv_mbmode_vlc[i], VC1_IF_MMV_MBMODE_VLC_BITS, 8,
50f97219
KS
178 ff_vc1_if_mmv_mbmode_bits[i], 1, 1,
179 ff_vc1_if_mmv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
180 ff_vc1_if_1mv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 55]];
181 ff_vc1_if_1mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 56] - vlc_offs[i * 3 + 55];
cad16562 182 init_vlc(&ff_vc1_if_1mv_mbmode_vlc[i], VC1_IF_1MV_MBMODE_VLC_BITS, 6,
50f97219
KS
183 ff_vc1_if_1mv_mbmode_bits[i], 1, 1,
184 ff_vc1_if_1mv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
b761659b 185 }
b761659b
DB
186 done = 1;
187 }
188
189 /* Other defaults */
50f97219 190 v->pq = -1;
b761659b
DB
191 v->mvrange = 0; /* 7.1.1.18, p80 */
192
193 return 0;
194}
195
196/***********************************************************************/
197/**
21a19b79 198 * @name VC-1 Bitplane decoding
b761659b
DB
199 * @see 8.7, p56
200 * @{
201 */
202
203/**
204 * Imode types
205 * @{
206 */
207enum Imode {
208 IMODE_RAW,
209 IMODE_NORM2,
210 IMODE_DIFF2,
211 IMODE_NORM6,
212 IMODE_DIFF6,
213 IMODE_ROWSKIP,
214 IMODE_COLSKIP
215};
216/** @} */ //imode defines
217
218
219/** @} */ //Bitplane group
220
7d2e03af
RB
221static void vc1_put_signed_blocks_clamped(VC1Context *v)
222{
223 MpegEncContext *s = &v->s;
cad16562
MSS
224 int topleft_mb_pos, top_mb_pos;
225 int stride_y, fieldtx;
226 int v_dist;
7d2e03af
RB
227
228 /* The put pixels loop is always one MB row behind the decoding loop,
229 * because we can only put pixels when overlap filtering is done, and
230 * for filtering of the bottom edge of a MB, we need the next MB row
231 * present as well.
232 * Within the row, the put pixels loop is also one MB col behind the
233 * decoding loop. The reason for this is again, because for filtering
234 * of the right MB edge, we need the next MB present. */
235 if (!s->first_slice_line) {
236 if (s->mb_x) {
cad16562 237 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
50f97219
KS
238 fieldtx = v->fieldtx_plane[topleft_mb_pos];
239 stride_y = (s->linesize) << fieldtx;
240 v_dist = (16 - fieldtx) >> (fieldtx == 0);
7d2e03af
RB
241 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
242 s->dest[0] - 16 * s->linesize - 16,
cad16562 243 stride_y);
7d2e03af
RB
244 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
245 s->dest[0] - 16 * s->linesize - 8,
cad16562 246 stride_y);
7d2e03af 247 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
cad16562
MSS
248 s->dest[0] - v_dist * s->linesize - 16,
249 stride_y);
7d2e03af 250 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
cad16562
MSS
251 s->dest[0] - v_dist * s->linesize - 8,
252 stride_y);
7d2e03af
RB
253 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
254 s->dest[1] - 8 * s->uvlinesize - 8,
255 s->uvlinesize);
256 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
257 s->dest[2] - 8 * s->uvlinesize - 8,
258 s->uvlinesize);
259 }
260 if (s->mb_x == s->mb_width - 1) {
cad16562 261 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
50f97219
KS
262 fieldtx = v->fieldtx_plane[top_mb_pos];
263 stride_y = s->linesize << fieldtx;
264 v_dist = fieldtx ? 15 : 8;
7d2e03af
RB
265 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
266 s->dest[0] - 16 * s->linesize,
cad16562 267 stride_y);
7d2e03af
RB
268 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
269 s->dest[0] - 16 * s->linesize + 8,
cad16562 270 stride_y);
7d2e03af 271 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
cad16562
MSS
272 s->dest[0] - v_dist * s->linesize,
273 stride_y);
7d2e03af 274 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
cad16562
MSS
275 s->dest[0] - v_dist * s->linesize + 8,
276 stride_y);
7d2e03af
RB
277 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
278 s->dest[1] - 8 * s->uvlinesize,
279 s->uvlinesize);
280 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
281 s->dest[2] - 8 * s->uvlinesize,
282 s->uvlinesize);
283 }
284 }
285
286#define inc_blk_idx(idx) do { \
287 idx++; \
288 if (idx >= v->n_allocated_blks) \
289 idx = 0; \
290 } while (0)
291
292 inc_blk_idx(v->topleft_blk_idx);
293 inc_blk_idx(v->top_blk_idx);
294 inc_blk_idx(v->left_blk_idx);
295 inc_blk_idx(v->cur_blk_idx);
296}
297
12802ec0 298static void vc1_loop_filter_iblk(VC1Context *v, int pq)
b761659b 299{
12802ec0 300 MpegEncContext *s = &v->s;
fca58a81
DC
301 int j;
302 if (!s->first_slice_line) {
12802ec0 303 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
fca58a81 304 if (s->mb_x)
50f97219
KS
305 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
306 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
307 for (j = 0; j < 2; j++) {
308 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
fca58a81 309 if (s->mb_x)
50f97219 310 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
fca58a81
DC
311 }
312 }
50f97219 313 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
fca58a81 314
50f97219 315 if (s->mb_y == s->end_mb_y - 1) {
fca58a81 316 if (s->mb_x) {
12802ec0
RB
317 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
318 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
319 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
fca58a81 320 }
12802ec0 321 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
b761659b
DB
322 }
323}
324
7d2e03af
RB
325static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
326{
327 MpegEncContext *s = &v->s;
328 int j;
329
330 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
331 * means it runs two rows/cols behind the decoding loop. */
332 if (!s->first_slice_line) {
333 if (s->mb_x) {
334 if (s->mb_y >= s->start_mb_y + 2) {
335 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
336
337 if (s->mb_x >= 2)
338 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
339 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
50f97219
KS
340 for (j = 0; j < 2; j++) {
341 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
7d2e03af 342 if (s->mb_x >= 2) {
50f97219 343 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
7d2e03af
RB
344 }
345 }
346 }
347 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
348 }
349
350 if (s->mb_x == s->mb_width - 1) {
351 if (s->mb_y >= s->start_mb_y + 2) {
352 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
353
354 if (s->mb_x)
355 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
356 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
50f97219
KS
357 for (j = 0; j < 2; j++) {
358 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
7d2e03af 359 if (s->mb_x >= 2) {
50f97219 360 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
7d2e03af
RB
361 }
362 }
363 }
364 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
365 }
366
1cf82cab 367 if (s->mb_y == s->end_mb_y) {
7d2e03af
RB
368 if (s->mb_x) {
369 if (s->mb_x >= 2)
370 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
371 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
372 if (s->mb_x >= 2) {
50f97219
KS
373 for (j = 0; j < 2; j++) {
374 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
7d2e03af
RB
375 }
376 }
377 }
378
379 if (s->mb_x == s->mb_width - 1) {
380 if (s->mb_x)
381 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
382 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
383 if (s->mb_x) {
50f97219
KS
384 for (j = 0; j < 2; j++) {
385 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
7d2e03af
RB
386 }
387 }
388 }
389 }
390 }
391}
392
393static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
394{
395 MpegEncContext *s = &v->s;
396 int mb_pos;
397
398 if (v->condover == CONDOVER_NONE)
399 return;
400
401 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
402
403 /* Within a MB, the horizontal overlap always runs before the vertical.
404 * To accomplish that, we run the H on left and internal borders of the
405 * currently decoded MB. Then, we wait for the next overlap iteration
406 * to do H overlap on the right edge of this MB, before moving over and
407 * running the V overlap. Therefore, the V overlap makes us trail by one
408 * MB col and the H overlap filter makes us trail by one MB row. This
409 * is reflected in the time at which we run the put_pixels loop. */
50f97219
KS
410 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
411 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
412 v->over_flags_plane[mb_pos - 1])) {
7d2e03af
RB
413 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
414 v->block[v->cur_blk_idx][0]);
415 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
416 v->block[v->cur_blk_idx][2]);
50f97219 417 if (!(s->flags & CODEC_FLAG_GRAY)) {
7d2e03af
RB
418 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
419 v->block[v->cur_blk_idx][4]);
420 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
421 v->block[v->cur_blk_idx][5]);
422 }
423 }
424 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
425 v->block[v->cur_blk_idx][1]);
426 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
427 v->block[v->cur_blk_idx][3]);
428
429 if (s->mb_x == s->mb_width - 1) {
50f97219
KS
430 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
431 v->over_flags_plane[mb_pos - s->mb_stride])) {
7d2e03af
RB
432 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
433 v->block[v->cur_blk_idx][0]);
434 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
435 v->block[v->cur_blk_idx][1]);
50f97219 436 if (!(s->flags & CODEC_FLAG_GRAY)) {
7d2e03af
RB
437 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
438 v->block[v->cur_blk_idx][4]);
439 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
440 v->block[v->cur_blk_idx][5]);
441 }
442 }
443 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
444 v->block[v->cur_blk_idx][2]);
445 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
446 v->block[v->cur_blk_idx][3]);
447 }
448 }
449 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
50f97219
KS
450 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
451 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
7d2e03af
RB
452 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
453 v->block[v->left_blk_idx][0]);
454 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
455 v->block[v->left_blk_idx][1]);
50f97219 456 if (!(s->flags & CODEC_FLAG_GRAY)) {
7d2e03af
RB
457 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
458 v->block[v->left_blk_idx][4]);
459 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
460 v->block[v->left_blk_idx][5]);
461 }
462 }
463 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
464 v->block[v->left_blk_idx][2]);
465 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
466 v->block[v->left_blk_idx][3]);
467 }
468}
469
b761659b
DB
470/** Do motion compensation over 1 macroblock
471 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
472 */
473static void vc1_mc_1mv(VC1Context *v, int dir)
474{
475 MpegEncContext *s = &v->s;
50f97219 476 DSPContext *dsp = &v->s.dsp;
b761659b
DB
477 uint8_t *srcY, *srcU, *srcV;
478 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
cad16562
MSS
479 int off, off_uv;
480 int v_edge_pos = s->v_edge_pos >> v->field_mode;
481 if (!v->field_mode && !v->s.last_picture.f.data[0])
482 return;
b761659b
DB
483
484 mx = s->mv[dir][0][0];
485 my = s->mv[dir][0][1];
486
487 // store motion vectors for further use in B frames
50f97219 488 if (s->pict_type == AV_PICTURE_TYPE_P) {
cad16562
MSS
489 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
490 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
b761659b 491 }
cad16562 492
b761659b
DB
493 uvmx = (mx + ((mx & 3) == 3)) >> 1;
494 uvmy = (my + ((my & 3) == 3)) >> 1;
c47d3835
RB
495 v->luma_mv[s->mb_x][0] = uvmx;
496 v->luma_mv[s->mb_x][1] = uvmy;
cad16562
MSS
497
498 if (v->field_mode &&
499 v->cur_field_type != v->ref_field_type[dir]) {
50f97219 500 my = my - 2 + 4 * v->cur_field_type;
cad16562
MSS
501 uvmy = uvmy - 2 + 4 * v->cur_field_type;
502 }
503
50f97219
KS
504 if (v->fastuvmc && (v->fcm != 1)) { // fastuvmc shall be ignored for interlaced frame picture
505 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
506 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
b761659b 507 }
cad16562
MSS
508 if (v->field_mode) { // interlaced field picture
509 if (!dir) {
510 if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type) {
511 srcY = s->current_picture.f.data[0];
512 srcU = s->current_picture.f.data[1];
513 srcV = s->current_picture.f.data[2];
514 } else {
515 srcY = s->last_picture.f.data[0];
516 srcU = s->last_picture.f.data[1];
517 srcV = s->last_picture.f.data[2];
518 }
519 } else {
520 srcY = s->next_picture.f.data[0];
521 srcU = s->next_picture.f.data[1];
522 srcV = s->next_picture.f.data[2];
523 }
b761659b 524 } else {
50f97219 525 if (!dir) {
cad16562
MSS
526 srcY = s->last_picture.f.data[0];
527 srcU = s->last_picture.f.data[1];
528 srcV = s->last_picture.f.data[2];
529 } else {
530 srcY = s->next_picture.f.data[0];
531 srcU = s->next_picture.f.data[1];
532 srcV = s->next_picture.f.data[2];
533 }
b761659b
DB
534 }
535
50f97219
KS
536 src_x = s->mb_x * 16 + (mx >> 2);
537 src_y = s->mb_y * 16 + (my >> 2);
538 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
539 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
b761659b 540
50f97219 541 if (v->profile != PROFILE_ADVANCED) {
b761659b
DB
542 src_x = av_clip( src_x, -16, s->mb_width * 16);
543 src_y = av_clip( src_y, -16, s->mb_height * 16);
544 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
545 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
50f97219 546 } else {
b761659b
DB
547 src_x = av_clip( src_x, -17, s->avctx->coded_width);
548 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
549 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
550 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
551 }
552
50f97219 553 srcY += src_y * s->linesize + src_x;
b761659b
DB
554 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
555 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
556
cad16562
MSS
557 if (v->field_mode && v->ref_field_type[dir]) {
558 srcY += s->current_picture_ptr->f.linesize[0];
559 srcU += s->current_picture_ptr->f.linesize[1];
560 srcV += s->current_picture_ptr->f.linesize[2];
561 }
562
b761659b 563 /* for grayscale we should not try to read from unknown area */
50f97219 564 if (s->flags & CODEC_FLAG_GRAY) {
b761659b
DB
565 srcU = s->edge_emu_buffer + 18 * s->linesize;
566 srcV = s->edge_emu_buffer + 18 * s->linesize;
567 }
568
50f97219
KS
569 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
570 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
571 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my&3) - 16 - s->mspel * 3) {
572 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
b761659b
DB
573
574 srcY -= s->mspel * (1 + s->linesize);
50f97219
KS
575 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
576 17 + s->mspel * 2, 17 + s->mspel * 2,
577 src_x - s->mspel, src_y - s->mspel,
578 s->h_edge_pos, v_edge_pos);
b761659b 579 srcY = s->edge_emu_buffer;
50f97219
KS
580 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
581 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
582 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
583 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
b761659b
DB
584 srcU = uvbuf;
585 srcV = uvbuf + 16;
586 /* if we deal with range reduction we need to scale source blocks */
50f97219 587 if (v->rangeredfrm) {
b761659b
DB
588 int i, j;
589 uint8_t *src, *src2;
590
591 src = srcY;
50f97219
KS
592 for (j = 0; j < 17 + s->mspel * 2; j++) {
593 for (i = 0; i < 17 + s->mspel * 2; i++)
594 src[i] = ((src[i] - 128) >> 1) + 128;
b761659b
DB
595 src += s->linesize;
596 }
50f97219
KS
597 src = srcU;
598 src2 = srcV;
599 for (j = 0; j < 9; j++) {
600 for (i = 0; i < 9; i++) {
601 src[i] = ((src[i] - 128) >> 1) + 128;
b761659b
DB
602 src2[i] = ((src2[i] - 128) >> 1) + 128;
603 }
50f97219 604 src += s->uvlinesize;
b761659b
DB
605 src2 += s->uvlinesize;
606 }
607 }
608 /* if we deal with intensity compensation we need to scale source blocks */
50f97219 609 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
b761659b
DB
610 int i, j;
611 uint8_t *src, *src2;
612
613 src = srcY;
50f97219
KS
614 for (j = 0; j < 17 + s->mspel * 2; j++) {
615 for (i = 0; i < 17 + s->mspel * 2; i++)
616 src[i] = v->luty[src[i]];
b761659b
DB
617 src += s->linesize;
618 }
50f97219
KS
619 src = srcU;
620 src2 = srcV;
621 for (j = 0; j < 9; j++) {
622 for (i = 0; i < 9; i++) {
623 src[i] = v->lutuv[src[i]];
b761659b
DB
624 src2[i] = v->lutuv[src2[i]];
625 }
50f97219 626 src += s->uvlinesize;
b761659b
DB
627 src2 += s->uvlinesize;
628 }
629 }
630 srcY += s->mspel * (1 + s->linesize);
631 }
632
cad16562
MSS
633 if (v->field_mode && v->cur_field_type) {
634 off = s->current_picture_ptr->f.linesize[0];
635 off_uv = s->current_picture_ptr->f.linesize[1];
636 } else {
637 off = 0;
638 off_uv = 0;
639 }
50f97219 640 if (s->mspel) {
b761659b 641 dxy = ((my & 3) << 2) | (mx & 3);
cad16562
MSS
642 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
643 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
b761659b 644 srcY += s->linesize * 8;
cad16562
MSS
645 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
646 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
b761659b
DB
647 } else { // hpel mc - always used for luma
648 dxy = (my & 2) | ((mx & 2) >> 1);
50f97219 649 if (!v->rnd)
cad16562 650 dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
b761659b 651 else
cad16562 652 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
b761659b
DB
653 }
654
50f97219 655 if (s->flags & CODEC_FLAG_GRAY) return;
b761659b 656 /* Chroma MC always uses qpel bilinear */
50f97219
KS
657 uvmx = (uvmx & 3) << 1;
658 uvmy = (uvmy & 3) << 1;
659 if (!v->rnd) {
cad16562
MSS
660 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
661 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
50f97219 662 } else {
cad16562
MSS
663 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
664 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
665 }
666}
667
668static inline int median4(int a, int b, int c, int d)
669{
50f97219
KS
670 if (a < b) {
671 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
672 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
cad16562 673 } else {
50f97219
KS
674 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
675 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
b761659b
DB
676 }
677}
678
679/** Do motion compensation for 4-MV macroblock - luminance block
680 */
cad16562 681static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
b761659b
DB
682{
683 MpegEncContext *s = &v->s;
684 DSPContext *dsp = &v->s.dsp;
685 uint8_t *srcY;
686 int dxy, mx, my, src_x, src_y;
687 int off;
cad16562
MSS
688 int fieldmv = (v->fcm == 1) ? v->blk_mv_type[s->block_index[n]] : 0;
689 int v_edge_pos = s->v_edge_pos >> v->field_mode;
50f97219
KS
690
691 if (!v->field_mode && !v->s.last_picture.f.data[0])
692 return;
693
cad16562
MSS
694 mx = s->mv[dir][n][0];
695 my = s->mv[dir][n][1];
696
697 if (!dir) {
698 if (v->field_mode) {
699 if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type)
700 srcY = s->current_picture.f.data[0];
701 else
702 srcY = s->last_picture.f.data[0];
703 } else
704 srcY = s->last_picture.f.data[0];
705 } else
706 srcY = s->next_picture.f.data[0];
b761659b 707
cad16562
MSS
708 if (v->field_mode) {
709 if (v->cur_field_type != v->ref_field_type[dir])
710 my = my - 2 + 4 * v->cur_field_type;
711 }
712
713 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
714 int same_count = 0, opp_count = 0, k;
715 int chosen_mv[2][4][2], f;
716 int tx, ty;
717 for (k = 0; k < 4; k++) {
718 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
719 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
720 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
50f97219 721 opp_count += f;
cad16562
MSS
722 same_count += 1 - f;
723 }
724 f = opp_count > same_count;
725 switch (f ? opp_count : same_count) {
726 case 4:
50f97219
KS
727 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
728 chosen_mv[f][2][0], chosen_mv[f][3][0]);
729 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
730 chosen_mv[f][2][1], chosen_mv[f][3][1]);
cad16562
MSS
731 break;
732 case 3:
733 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
734 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
735 break;
736 case 2:
737 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
738 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
739 break;
740 }
741 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
742 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
50f97219
KS
743 for (k = 0; k < 4; k++)
744 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
cad16562
MSS
745 }
746
747 if (v->fcm == 1) { // not sure if needed for other types of picture
748 int qx, qy;
50f97219 749 int width = s->avctx->coded_width;
cad16562
MSS
750 int height = s->avctx->coded_height >> 1;
751 qx = (s->mb_x * 16) + (mx >> 2);
752 qy = (s->mb_y * 8) + (my >> 3);
753
754 if (qx < -17)
755 mx -= 4 * (qx + 17);
756 else if (qx > width)
757 mx -= 4 * (qx - width);
758 if (qy < -18)
759 my -= 8 * (qy + 18);
760 else if (qy > height + 1)
761 my -= 8 * (qy - height - 1);
762 }
763
764 if ((v->fcm == 1) && fieldmv)
50f97219 765 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
cad16562 766 else
50f97219 767 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
cad16562
MSS
768 if (v->field_mode && v->cur_field_type)
769 off += s->current_picture_ptr->f.linesize[0];
b761659b 770
50f97219 771 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
cad16562 772 if (!fieldmv)
50f97219 773 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
cad16562
MSS
774 else
775 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
b761659b 776
50f97219
KS
777 if (v->profile != PROFILE_ADVANCED) {
778 src_x = av_clip(src_x, -16, s->mb_width * 16);
779 src_y = av_clip(src_y, -16, s->mb_height * 16);
780 } else {
781 src_x = av_clip(src_x, -17, s->avctx->coded_width);
cad16562
MSS
782 if (v->fcm == 1) {
783 if (src_y & 1)
50f97219 784 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
cad16562 785 else
50f97219 786 src_y = av_clip(src_y, -18, s->avctx->coded_height);
cad16562 787 } else {
50f97219 788 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
cad16562 789 }
b761659b
DB
790 }
791
792 srcY += src_y * s->linesize + src_x;
cad16562
MSS
793 if (v->field_mode && v->ref_field_type[dir])
794 srcY += s->current_picture_ptr->f.linesize[0];
b761659b 795
cad16562
MSS
796 if (fieldmv && !(src_y & 1))
797 v_edge_pos--;
798 if (fieldmv && (src_y & 1) && src_y < 4)
799 src_y--;
50f97219
KS
800 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
801 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
802 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
cad16562
MSS
803 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
804 /* check emulate edge stride and offset */
50f97219
KS
805 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
806 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
807 src_x - s->mspel, src_y - (s->mspel << fieldmv),
808 s->h_edge_pos, v_edge_pos);
b761659b
DB
809 srcY = s->edge_emu_buffer;
810 /* if we deal with range reduction we need to scale source blocks */
50f97219 811 if (v->rangeredfrm) {
b761659b
DB
812 int i, j;
813 uint8_t *src;
814
815 src = srcY;
50f97219
KS
816 for (j = 0; j < 9 + s->mspel * 2; j++) {
817 for (i = 0; i < 9 + s->mspel * 2; i++)
818 src[i] = ((src[i] - 128) >> 1) + 128;
cad16562 819 src += s->linesize << fieldmv;
b761659b
DB
820 }
821 }
822 /* if we deal with intensity compensation we need to scale source blocks */
50f97219 823 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
b761659b
DB
824 int i, j;
825 uint8_t *src;
826
827 src = srcY;
50f97219
KS
828 for (j = 0; j < 9 + s->mspel * 2; j++) {
829 for (i = 0; i < 9 + s->mspel * 2; i++)
830 src[i] = v->luty[src[i]];
cad16562 831 src += s->linesize << fieldmv;
b761659b
DB
832 }
833 }
cad16562 834 srcY += s->mspel * (1 + (s->linesize << fieldmv));
b761659b
DB
835 }
836
50f97219 837 if (s->mspel) {
b761659b 838 dxy = ((my & 3) << 2) | (mx & 3);
cad16562 839 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
b761659b
DB
840 } else { // hpel mc - always used for luma
841 dxy = (my & 2) | ((mx & 2) >> 1);
50f97219 842 if (!v->rnd)
b761659b
DB
843 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
844 else
845 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
846 }
847}
848
cad16562 849static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
b761659b 850{
cad16562
MSS
851 int idx, i;
852 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
50f97219
KS
853
854 idx = ((a[3] != flag) << 3)
855 | ((a[2] != flag) << 2)
856 | ((a[1] != flag) << 1)
857 | (a[0] != flag);
858 if (!idx) {
cad16562
MSS
859 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
860 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
861 return 4;
50f97219
KS
862 } else if (count[idx] == 1) {
863 switch (idx) {
cad16562
MSS
864 case 0x1:
865 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
866 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
867 return 3;
868 case 0x2:
869 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
870 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
871 return 3;
872 case 0x4:
873 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
874 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
875 return 3;
876 case 0x8:
877 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
878 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
879 return 3;
880 }
50f97219 881 } else if (count[idx] == 2) {
cad16562
MSS
882 int t1 = 0, t2 = 0;
883 for (i = 0; i < 3; i++)
884 if (!a[i]) {
885 t1 = i;
886 break;
887 }
888 for (i = t1 + 1; i < 4; i++)
889 if (!a[i]) {
890 t2 = i;
891 break;
892 }
893 *tx = (mvx[t1] + mvx[t2]) / 2;
894 *ty = (mvy[t1] + mvy[t2]) / 2;
895 return 2;
b761659b 896 } else {
cad16562 897 return 0;
b761659b 898 }
cad16562 899 return -1;
b761659b
DB
900}
901
b761659b
DB
902/** Do motion compensation for 4-MV macroblock - both chroma blocks
903 */
cad16562 904static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
b761659b
DB
905{
906 MpegEncContext *s = &v->s;
50f97219 907 DSPContext *dsp = &v->s.dsp;
b761659b
DB
908 uint8_t *srcU, *srcV;
909 int uvmx, uvmy, uvsrc_x, uvsrc_y;
cad16562
MSS
910 int k, tx = 0, ty = 0;
911 int mvx[4], mvy[4], intra[4], mv_f[4];
912 int valid_count;
913 int chroma_ref_type = v->cur_field_type, off = 0;
914 int v_edge_pos = s->v_edge_pos >> v->field_mode;
b761659b 915
50f97219
KS
916 if (!v->field_mode && !v->s.last_picture.f.data[0])
917 return;
918 if (s->flags & CODEC_FLAG_GRAY)
919 return;
b761659b 920
50f97219 921 for (k = 0; k < 4; k++) {
cad16562
MSS
922 mvx[k] = s->mv[dir][k][0];
923 mvy[k] = s->mv[dir][k][1];
924 intra[k] = v->mb_type[0][s->block_index[k]];
925 if (v->field_mode)
926 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
b761659b
DB
927 }
928
929 /* calculate chroma MV vector from four luma MVs */
cad16562
MSS
930 if (!v->field_mode || (v->field_mode && !v->numref)) {
931 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
932 if (!valid_count) {
f1874652
MSS
933 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
934 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
cad16562
MSS
935 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
936 return; //no need to do MC for intra blocks
b761659b 937 }
b761659b 938 } else {
cad16562
MSS
939 int dominant = 0;
940 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
941 dominant = 1;
942 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
943 if (dominant)
944 chroma_ref_type = !v->cur_field_type;
b761659b 945 }
f1874652
MSS
946 s->current_picture.f.motion_val[1][s->block_index[0]][0] = tx;
947 s->current_picture.f.motion_val[1][s->block_index[0]][1] = ty;
50f97219
KS
948 uvmx = (tx + ((tx & 3) == 3)) >> 1;
949 uvmy = (ty + ((ty & 3) == 3)) >> 1;
cad16562 950
c47d3835
RB
951 v->luma_mv[s->mb_x][0] = uvmx;
952 v->luma_mv[s->mb_x][1] = uvmy;
cad16562 953
50f97219
KS
954 if (v->fastuvmc) {
955 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
956 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
b761659b 957 }
cad16562
MSS
958 // Field conversion bias
959 if (v->cur_field_type != chroma_ref_type)
960 uvmy += 2 - 4 * chroma_ref_type;
b761659b
DB
961
962 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
963 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
964
50f97219
KS
965 if (v->profile != PROFILE_ADVANCED) {
966 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
967 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
968 } else {
969 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
970 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
b761659b
DB
971 }
972
cad16562
MSS
973 if (!dir) {
974 if (v->field_mode) {
975 if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
976 srcU = s->current_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
977 srcV = s->current_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
978 } else {
979 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
980 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
981 }
982 } else {
983 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
984 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
985 }
986 } else {
987 srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
988 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
989 }
990
991 if (v->field_mode) {
992 if (chroma_ref_type) {
993 srcU += s->current_picture_ptr->f.linesize[1];
994 srcV += s->current_picture_ptr->f.linesize[2];
995 }
996 off = v->cur_field_type ? s->current_picture_ptr->f.linesize[1] : 0;
997 }
998
50f97219
KS
999 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1000 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
1001 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
1002 s->dsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
1003 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
1004 s->h_edge_pos >> 1, v_edge_pos >> 1);
1005 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1006 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
1007 s->h_edge_pos >> 1, v_edge_pos >> 1);
b761659b
DB
1008 srcU = s->edge_emu_buffer;
1009 srcV = s->edge_emu_buffer + 16;
1010
1011 /* if we deal with range reduction we need to scale source blocks */
50f97219 1012 if (v->rangeredfrm) {
b761659b
DB
1013 int i, j;
1014 uint8_t *src, *src2;
1015
50f97219
KS
1016 src = srcU;
1017 src2 = srcV;
1018 for (j = 0; j < 9; j++) {
1019 for (i = 0; i < 9; i++) {
1020 src[i] = ((src[i] - 128) >> 1) + 128;
b761659b
DB
1021 src2[i] = ((src2[i] - 128) >> 1) + 128;
1022 }
50f97219 1023 src += s->uvlinesize;
b761659b
DB
1024 src2 += s->uvlinesize;
1025 }
1026 }
1027 /* if we deal with intensity compensation we need to scale source blocks */
50f97219 1028 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
b761659b
DB
1029 int i, j;
1030 uint8_t *src, *src2;
1031
50f97219
KS
1032 src = srcU;
1033 src2 = srcV;
1034 for (j = 0; j < 9; j++) {
1035 for (i = 0; i < 9; i++) {
1036 src[i] = v->lutuv[src[i]];
b761659b
DB
1037 src2[i] = v->lutuv[src2[i]];
1038 }
50f97219 1039 src += s->uvlinesize;
b761659b
DB
1040 src2 += s->uvlinesize;
1041 }
1042 }
1043 }
1044
1045 /* Chroma MC always uses qpel bilinear */
50f97219
KS
1046 uvmx = (uvmx & 3) << 1;
1047 uvmy = (uvmy & 3) << 1;
1048 if (!v->rnd) {
cad16562
MSS
1049 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
1050 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
50f97219 1051 } else {
cad16562
MSS
1052 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
1053 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
1054 }
1055}
1056
1057/** Do motion compensation for 4-MV field chroma macroblock (both U and V)
1058 */
1059static void vc1_mc_4mv_chroma4(VC1Context *v)
1060{
1061 MpegEncContext *s = &v->s;
1062 DSPContext *dsp = &v->s.dsp;
1063 uint8_t *srcU, *srcV;
1064 int uvsrc_x, uvsrc_y;
1065 int uvmx_field[4], uvmy_field[4];
1066 int i, off, tx, ty;
1067 int fieldmv = v->blk_mv_type[s->block_index[0]];
50f97219 1068 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
cad16562
MSS
1069 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
1070 int v_edge_pos = s->v_edge_pos >> 1;
1071
50f97219
KS
1072 if (!v->s.last_picture.f.data[0])
1073 return;
1074 if (s->flags & CODEC_FLAG_GRAY)
1075 return;
cad16562
MSS
1076
1077 for (i = 0; i < 4; i++) {
1078 tx = s->mv[0][i][0];
1079 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
1080 ty = s->mv[0][i][1];
1081 if (fieldmv)
1082 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
1083 else
1084 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
1085 }
1086
1087 for (i = 0; i < 4; i++) {
1088 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
50f97219 1089 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
cad16562
MSS
1090 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1091 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
50f97219
KS
1092 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1093 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
cad16562
MSS
1094 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1095 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1096 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1097 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1098
1099 if (fieldmv && !(uvsrc_y & 1))
1100 v_edge_pos--;
1101 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1102 uvsrc_y--;
50f97219 1103 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
cad16562 1104 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
50f97219
KS
1105 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1106 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
1107 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1108 s->h_edge_pos >> 1, v_edge_pos);
1109 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1110 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1111 s->h_edge_pos >> 1, v_edge_pos);
cad16562
MSS
1112 srcU = s->edge_emu_buffer;
1113 srcV = s->edge_emu_buffer + 16;
1114
1115 /* if we deal with intensity compensation we need to scale source blocks */
50f97219 1116 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
cad16562
MSS
1117 int i, j;
1118 uint8_t *src, *src2;
1119
50f97219
KS
1120 src = srcU;
1121 src2 = srcV;
1122 for (j = 0; j < 5; j++) {
1123 for (i = 0; i < 5; i++) {
1124 src[i] = v->lutuv[src[i]];
cad16562
MSS
1125 src2[i] = v->lutuv[src2[i]];
1126 }
50f97219 1127 src += s->uvlinesize << 1;
cad16562
MSS
1128 src2 += s->uvlinesize << 1;
1129 }
1130 }
1131 }
1132 if (!v->rnd) {
1133 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1134 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1135 } else {
1136 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1137 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1138 }
b761659b
DB
1139 }
1140}
1141
1142/***********************************************************************/
1143/**
21a19b79 1144 * @name VC-1 Block-level functions
b761659b
DB
1145 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1146 * @{
1147 */
1148
1149/**
1150 * @def GET_MQUANT
1151 * @brief Get macroblock-level quantizer scale
1152 */
1153#define GET_MQUANT() \
50f97219
KS
1154 if (v->dquantfrm) { \
1155 int edges = 0; \
1156 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1157 if (v->dqbilevel) { \
1158 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1159 } else { \
1160 mqdiff = get_bits(gb, 3); \
1161 if (mqdiff != 7) \
1162 mquant = v->pq + mqdiff; \
1163 else \
1164 mquant = get_bits(gb, 5); \
1165 } \
1166 } \
1167 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1168 edges = 1 << v->dqsbedge; \
1169 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1170 edges = (3 << v->dqsbedge) % 15; \
1171 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1172 edges = 15; \
1173 if ((edges&1) && !s->mb_x) \
1174 mquant = v->altpq; \
1175 if ((edges&2) && s->first_slice_line) \
1176 mquant = v->altpq; \
1177 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1178 mquant = v->altpq; \
1179 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1180 mquant = v->altpq; \
1181 }
b761659b
DB
1182
1183/**
1184 * @def GET_MVDATA(_dmv_x, _dmv_y)
1185 * @brief Get MV differentials
1186 * @see MVDATA decoding from 8.3.5.2, p(1)20
1187 * @param _dmv_x Horizontal differential for decoded MV
1188 * @param _dmv_y Vertical differential for decoded MV
1189 */
50f97219
KS
1190#define GET_MVDATA(_dmv_x, _dmv_y) \
1191 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1192 VC1_MV_DIFF_VLC_BITS, 2); \
1193 if (index > 36) { \
1194 mb_has_coeffs = 1; \
1195 index -= 37; \
1196 } else \
1197 mb_has_coeffs = 0; \
1198 s->mb_intra = 0; \
1199 if (!index) { \
1200 _dmv_x = _dmv_y = 0; \
1201 } else if (index == 35) { \
1202 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1203 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1204 } else if (index == 36) { \
1205 _dmv_x = 0; \
1206 _dmv_y = 0; \
1207 s->mb_intra = 1; \
1208 } else { \
1209 index1 = index % 6; \
1210 if (!s->quarter_sample && index1 == 5) val = 1; \
1211 else val = 0; \
1212 if (size_table[index1] - val > 0) \
1213 val = get_bits(gb, size_table[index1] - val); \
1214 else val = 0; \
1215 sign = 0 - (val&1); \
1216 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1217 \
1218 index1 = index / 6; \
1219 if (!s->quarter_sample && index1 == 5) val = 1; \
1220 else val = 0; \
1221 if (size_table[index1] - val > 0) \
1222 val = get_bits(gb, size_table[index1] - val); \
1223 else val = 0; \
1224 sign = 0 - (val & 1); \
1225 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1226 }
1227
1228static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1229 int *dmv_y, int *pred_flag)
cad16562
MSS
1230{
1231 int index, index1;
1232 int extend_x = 0, extend_y = 0;
1233 GetBitContext *gb = &v->s.gb;
1234 int bits, esc;
1235 int val, sign;
1236 const int* offs_tab;
1237
1238 if (v->numref) {
1239 bits = VC1_2REF_MVDATA_VLC_BITS;
50f97219 1240 esc = 125;
cad16562
MSS
1241 } else {
1242 bits = VC1_1REF_MVDATA_VLC_BITS;
50f97219 1243 esc = 71;
cad16562
MSS
1244 }
1245 switch (v->dmvrange) {
1246 case 1:
1247 extend_x = 1;
1248 break;
1249 case 2:
1250 extend_y = 1;
1251 break;
1252 case 3:
1253 extend_x = extend_y = 1;
1254 break;
1255 }
1256 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1257 if (index == esc) {
1258 *dmv_x = get_bits(gb, v->k_x);
1259 *dmv_y = get_bits(gb, v->k_y);
1260 if (v->numref) {
1261 *pred_flag = *dmv_y & 1;
50f97219 1262 *dmv_y = (*dmv_y + *pred_flag) >> 1;
cad16562
MSS
1263 }
1264 }
1265 else {
1266 if (extend_x)
1267 offs_tab = offset_table2;
1268 else
1269 offs_tab = offset_table1;
1270 index1 = (index + 1) % 9;
1271 if (index1 != 0) {
50f97219
KS
1272 val = get_bits(gb, index1 + extend_x);
1273 sign = 0 -(val & 1);
cad16562
MSS
1274 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1275 } else
1276 *dmv_x = 0;
1277 if (extend_y)
1278 offs_tab = offset_table2;
1279 else
1280 offs_tab = offset_table1;
1281 index1 = (index + 1) / 9;
1282 if (index1 > v->numref) {
50f97219
KS
1283 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1284 sign = 0 - (val & 1);
cad16562
MSS
1285 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1286 } else
1287 *dmv_y = 0;
1288 if (v->numref)
1289 *pred_flag = index1 & 1;
1290 }
1291}
1292
1293static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1294{
1295 int scaledvalue, refdist;
1296 int scalesame1, scalesame2;
1297 int scalezone1_x, zone1offset_x;
73447eb4 1298 int table_index = dir ^ v->second_field;
cad16562
MSS
1299
1300 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1301 refdist = v->refdist;
1302 else
1303 refdist = dir ? v->brfd : v->frfd;
1304 if (refdist > 3)
1305 refdist = 3;
73447eb4
MSS
1306 scalesame1 = vc1_field_mvpred_scales[table_index][1][refdist];
1307 scalesame2 = vc1_field_mvpred_scales[table_index][2][refdist];
1308 scalezone1_x = vc1_field_mvpred_scales[table_index][3][refdist];
1309 zone1offset_x = vc1_field_mvpred_scales[table_index][5][refdist];
cad16562
MSS
1310
1311 if (FFABS(n) > 255)
1312 scaledvalue = n;
1313 else {
1314 if (FFABS(n) < scalezone1_x)
1315 scaledvalue = (n * scalesame1) >> 8;
1316 else {
1317 if (n < 0)
1318 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1319 else
1320 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1321 }
1322 }
1323 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1324}
1325
1326static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1327{
1328 int scaledvalue, refdist;
1329 int scalesame1, scalesame2;
1330 int scalezone1_y, zone1offset_y;
73447eb4 1331 int table_index = dir ^ v->second_field;
cad16562
MSS
1332
1333 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1334 refdist = v->refdist;
1335 else
1336 refdist = dir ? v->brfd : v->frfd;
1337 if (refdist > 3)
1338 refdist = 3;
73447eb4
MSS
1339 scalesame1 = vc1_field_mvpred_scales[table_index][1][refdist];
1340 scalesame2 = vc1_field_mvpred_scales[table_index][2][refdist];
1341 scalezone1_y = vc1_field_mvpred_scales[table_index][4][refdist];
1342 zone1offset_y = vc1_field_mvpred_scales[table_index][6][refdist];
cad16562
MSS
1343
1344 if (FFABS(n) > 63)
1345 scaledvalue = n;
1346 else {
1347 if (FFABS(n) < scalezone1_y)
1348 scaledvalue = (n * scalesame1) >> 8;
1349 else {
1350 if (n < 0)
1351 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1352 else
1353 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1354 }
1355 }
1356
1357 if (v->cur_field_type && !v->ref_field_type[dir])
1358 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1359 else
1360 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1361}
1362
1363static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1364{
1365 int scalezone1_x, zone1offset_x;
1366 int scaleopp1, scaleopp2, brfd;
1367 int scaledvalue;
1368
1369 brfd = FFMIN(v->brfd, 3);
1370 scalezone1_x = vc1_b_field_mvpred_scales[3][brfd];
1371 zone1offset_x = vc1_b_field_mvpred_scales[5][brfd];
50f97219
KS
1372 scaleopp1 = vc1_b_field_mvpred_scales[1][brfd];
1373 scaleopp2 = vc1_b_field_mvpred_scales[2][brfd];
cad16562
MSS
1374
1375 if (FFABS(n) > 255)
1376 scaledvalue = n;
1377 else {
1378 if (FFABS(n) < scalezone1_x)
1379 scaledvalue = (n * scaleopp1) >> 8;
1380 else {
1381 if (n < 0)
1382 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1383 else
1384 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1385 }
1386 }
1387 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1388}
1389
1390static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1391{
1392 int scalezone1_y, zone1offset_y;
1393 int scaleopp1, scaleopp2, brfd;
1394 int scaledvalue;
1395
1396 brfd = FFMIN(v->brfd, 3);
1397 scalezone1_y = vc1_b_field_mvpred_scales[4][brfd];
1398 zone1offset_y = vc1_b_field_mvpred_scales[6][brfd];
50f97219
KS
1399 scaleopp1 = vc1_b_field_mvpred_scales[1][brfd];
1400 scaleopp2 = vc1_b_field_mvpred_scales[2][brfd];
cad16562
MSS
1401
1402 if (FFABS(n) > 63)
1403 scaledvalue = n;
1404 else {
1405 if (FFABS(n) < scalezone1_y)
1406 scaledvalue = (n * scaleopp1) >> 8;
1407 else {
1408 if (n < 0)
1409 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1410 else
1411 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1412 }
1413 }
1414 if (v->cur_field_type && !v->ref_field_type[dir]) {
1415 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1416 } else {
1417 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1418 }
1419}
1420
50f97219
KS
1421static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1422 int dim, int dir)
cad16562
MSS
1423{
1424 int brfd, scalesame;
6475a6e1 1425 int hpel = 1 - v->s.quarter_sample;
50f97219 1426
6475a6e1 1427 n >>= hpel;
cad16562
MSS
1428 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1429 if (dim)
6475a6e1 1430 n = scaleforsame_y(v, i, n, dir) << hpel;
cad16562 1431 else
6475a6e1
A
1432 n = scaleforsame_x(v, n, dir) << hpel;
1433 return n;
cad16562 1434 }
50f97219 1435 brfd = FFMIN(v->brfd, 3);
cad16562 1436 scalesame = vc1_b_field_mvpred_scales[0][brfd];
50f97219 1437
6475a6e1
A
1438 n = (n * scalesame >> 8) << hpel;
1439 return n;
cad16562
MSS
1440}
1441
50f97219
KS
1442static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1443 int dim, int dir)
cad16562
MSS
1444{
1445 int refdist, scaleopp;
6475a6e1 1446 int hpel = 1 - v->s.quarter_sample;
50f97219 1447
6475a6e1 1448 n >>= hpel;
cad16562
MSS
1449 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1450 if (dim)
6475a6e1 1451 n = scaleforopp_y(v, n, dir) << hpel;
cad16562 1452 else
6475a6e1
A
1453 n = scaleforopp_x(v, n) << hpel;
1454 return n;
cad16562
MSS
1455 }
1456 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1457 refdist = FFMIN(v->refdist, 3);
1458 else
1459 refdist = dir ? v->brfd : v->frfd;
73447eb4 1460 scaleopp = vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
50f97219 1461
6475a6e1
A
1462 n = (n * scaleopp >> 8) << hpel;
1463 return n;
cad16562
MSS
1464}
1465
b761659b
DB
1466/** Predict and set motion vector
1467 */
50f97219
KS
1468static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1469 int mv1, int r_x, int r_y, uint8_t* is_intra,
1470 int pred_flag, int dir)
b761659b 1471{
c47d3835 1472 MpegEncContext *s = &v->s;
b761659b
DB
1473 int xy, wrap, off = 0;
1474 int16_t *A, *B, *C;
1475 int px, py;
1476 int sum;
cad16562
MSS
1477 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1478 int opposit, f;
50f97219 1479 int16_t samefield_pred[2], oppfield_pred[2];
cad16562
MSS
1480 int16_t samefield_predA[2], oppfield_predA[2];
1481 int16_t samefield_predB[2], oppfield_predB[2];
1482 int16_t samefield_predC[2], oppfield_predC[2];
1483 int16_t *predA, *predC;
1484 int a_valid, b_valid, c_valid;
1485 int hybridmv_thresh, y_bias = 0;
1486
1487 if (v->mv_mode == MV_PMODE_MIXED_MV ||
50f97219
KS
1488 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1489 mixedmv_pic = 1;
1490 else
1491 mixedmv_pic = 0;
b761659b
DB
1492 /* scale MV difference to be quad-pel */
1493 dmv_x <<= 1 - s->quarter_sample;
1494 dmv_y <<= 1 - s->quarter_sample;
1495
1496 wrap = s->b8_stride;
50f97219 1497 xy = s->block_index[n];
b761659b 1498
50f97219 1499 if (s->mb_intra) {
cad16562
MSS
1500 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1501 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1502 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1503 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
50f97219
KS
1504 if (mv1) { /* duplicate motion data for 1-MV block */
1505 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1506 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1507 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1508 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
cad16562
MSS
1509 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1510 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
c47d3835 1511 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
50f97219
KS
1512 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1513 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1514 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1515 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
cad16562
MSS
1516 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1517 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
b761659b
DB
1518 }
1519 return;
1520 }
1521
50f97219 1522 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
cad16562 1523 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
50f97219 1524 if (mv1) {
cad16562
MSS
1525 if (v->field_mode && mixedmv_pic)
1526 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1527 else
1528 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1529 } else {
b761659b 1530 //in 4-MV mode different blocks have different B predictor position
cad16562 1531 switch (n) {
b761659b
DB
1532 case 0:
1533 off = (s->mb_x > 0) ? -1 : 1;
1534 break;
1535 case 1:
1536 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1537 break;
1538 case 2:
1539 off = 1;
1540 break;
1541 case 3:
1542 off = -1;
1543 }
1544 }
cad16562
MSS
1545 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1546
50f97219 1547 a_valid = !s->first_slice_line || (n == 2 || n == 3);
cad16562 1548 b_valid = a_valid && (s->mb_width > 1);
50f97219 1549 c_valid = s->mb_x || (n == 1 || n == 3);
cad16562
MSS
1550 if (v->field_mode) {
1551 a_valid = a_valid && !is_intra[xy - wrap];
1552 b_valid = b_valid && !is_intra[xy - wrap + off];
1553 c_valid = c_valid && !is_intra[xy - 1];
1554 }
1555
1556 if (a_valid) {
1557 f = v->mv_f[dir][xy - wrap + v->blocks_off];
50f97219 1558 num_oppfield += f;
cad16562
MSS
1559 num_samefield += 1 - f;
1560 if (f) {
50f97219
KS
1561 oppfield_predA[0] = A[0];
1562 oppfield_predA[1] = A[1];
cad16562
MSS
1563 samefield_predA[0] = scaleforsame(v, 0, A[0], 0, dir);
1564 samefield_predA[1] = scaleforsame(v, n, A[1], 1, dir);
1565 } else {
1566 samefield_predA[0] = A[0];
1567 samefield_predA[1] = A[1];
1568 if (v->numref)
1569 oppfield_predA[0] = scaleforopp(v, A[0], 0, dir);
1570 if (v->numref)
1571 oppfield_predA[1] = scaleforopp(v, A[1], 1, dir);
1572 }
1573 } else {
1574 samefield_predA[0] = samefield_predA[1] = 0;
50f97219 1575 oppfield_predA[0] = oppfield_predA[1] = 0;
cad16562
MSS
1576 }
1577 if (c_valid) {
1578 f = v->mv_f[dir][xy - 1 + v->blocks_off];
50f97219 1579 num_oppfield += f;
cad16562
MSS
1580 num_samefield += 1 - f;
1581 if (f) {
50f97219
KS
1582 oppfield_predC[0] = C[0];
1583 oppfield_predC[1] = C[1];
cad16562
MSS
1584 samefield_predC[0] = scaleforsame(v, 0, C[0], 0, dir);
1585 samefield_predC[1] = scaleforsame(v, n, C[1], 1, dir);
1586 } else {
1587 samefield_predC[0] = C[0];
1588 samefield_predC[1] = C[1];
1589 if (v->numref)
1590 oppfield_predC[0] = scaleforopp(v, C[0], 0, dir);
1591 if (v->numref)
1592 oppfield_predC[1] = scaleforopp(v, C[1], 1, dir);
1593 }
1594 } else {
1595 samefield_predC[0] = samefield_predC[1] = 0;
50f97219 1596 oppfield_predC[0] = oppfield_predC[1] = 0;
cad16562
MSS
1597 }
1598 if (b_valid) {
1599 f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
50f97219 1600 num_oppfield += f;
cad16562
MSS
1601 num_samefield += 1 - f;
1602 if (f) {
50f97219
KS
1603 oppfield_predB[0] = B[0];
1604 oppfield_predB[1] = B[1];
cad16562
MSS
1605 samefield_predB[0] = scaleforsame(v, 0, B[0], 0, dir);
1606 samefield_predB[1] = scaleforsame(v, n, B[1], 1, dir);
b761659b 1607 } else {
cad16562
MSS
1608 samefield_predB[0] = B[0];
1609 samefield_predB[1] = B[1];
1610 if (v->numref)
1611 oppfield_predB[0] = scaleforopp(v, B[0], 0, dir);
1612 if (v->numref)
1613 oppfield_predB[1] = scaleforopp(v, B[1], 1, dir);
b761659b 1614 }
b761659b 1615 } else {
cad16562 1616 samefield_predB[0] = samefield_predB[1] = 0;
50f97219 1617 oppfield_predB[0] = oppfield_predB[1] = 0;
cad16562
MSS
1618 }
1619
1620 if (a_valid) {
1621 samefield_pred[0] = samefield_predA[0];
1622 samefield_pred[1] = samefield_predA[1];
50f97219
KS
1623 oppfield_pred[0] = oppfield_predA[0];
1624 oppfield_pred[1] = oppfield_predA[1];
cad16562
MSS
1625 } else if (c_valid) {
1626 samefield_pred[0] = samefield_predC[0];
1627 samefield_pred[1] = samefield_predC[1];
50f97219
KS
1628 oppfield_pred[0] = oppfield_predC[0];
1629 oppfield_pred[1] = oppfield_predC[1];
cad16562
MSS
1630 } else if (b_valid) {
1631 samefield_pred[0] = samefield_predB[0];
1632 samefield_pred[1] = samefield_predB[1];
50f97219
KS
1633 oppfield_pred[0] = oppfield_predB[0];
1634 oppfield_pred[1] = oppfield_predB[1];
cad16562
MSS
1635 } else {
1636 samefield_pred[0] = samefield_pred[1] = 0;
50f97219 1637 oppfield_pred[0] = oppfield_pred[1] = 0;
cad16562
MSS
1638 }
1639
1640 if (num_samefield + num_oppfield > 1) {
1641 samefield_pred[0] = mid_pred(samefield_predA[0], samefield_predB[0], samefield_predC[0]);
1642 samefield_pred[1] = mid_pred(samefield_predA[1], samefield_predB[1], samefield_predC[1]);
1643 if (v->numref)
1644 oppfield_pred[0] = mid_pred(oppfield_predA[0], oppfield_predB[0], oppfield_predC[0]);
1645 if (v->numref)
1646 oppfield_pred[1] = mid_pred(oppfield_predA[1], oppfield_predB[1], oppfield_predC[1]);
1647 }
1648
1649 if (v->field_mode) {
1650 if (num_samefield <= num_oppfield)
1651 opposit = 1 - pred_flag;
1652 else
1653 opposit = pred_flag;
1654 } else
1655 opposit = 0;
1656 if (opposit) {
50f97219
KS
1657 px = oppfield_pred[0];
1658 py = oppfield_pred[1];
cad16562
MSS
1659 predA = oppfield_predA;
1660 predC = oppfield_predC;
1661 v->mv_f[dir][xy + v->blocks_off] = f = 1;
1662 v->ref_field_type[dir] = !v->cur_field_type;
1663 } else {
50f97219
KS
1664 px = samefield_pred[0];
1665 py = samefield_pred[1];
cad16562
MSS
1666 predA = samefield_predA;
1667 predC = samefield_predC;
1668 v->mv_f[dir][xy + v->blocks_off] = f = 0;
1669 v->ref_field_type[dir] = v->cur_field_type;
b761659b 1670 }
cad16562 1671
b761659b 1672 /* Pullback MV as specified in 8.3.5.3.4 */
cad16562 1673 if (!v->field_mode) {
b761659b 1674 int qx, qy, X, Y;
50f97219
KS
1675 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1676 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1677 X = (s->mb_width << 6) - 4;
1678 Y = (s->mb_height << 6) - 4;
1679 if (mv1) {
1680 if (qx + px < -60) px = -60 - qx;
1681 if (qy + py < -60) py = -60 - qy;
b761659b 1682 } else {
50f97219
KS
1683 if (qx + px < -28) px = -28 - qx;
1684 if (qy + py < -28) py = -28 - qy;
b761659b 1685 }
50f97219
KS
1686 if (qx + px > X) px = X - qx;
1687 if (qy + py > Y) py = Y - qy;
b761659b 1688 }
cad16562
MSS
1689
1690 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1691 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1692 if (v->field_mode && !s->quarter_sample)
1693 hybridmv_thresh = 16;
b761659b 1694 else
cad16562
MSS
1695 hybridmv_thresh = 32;
1696 if (a_valid && c_valid) {
1697 if (is_intra[xy - wrap])
1698 sum = FFABS(px) + FFABS(py);
1699 else
1700 sum = FFABS(px - predA[0]) + FFABS(py - predA[1]);
1701 if (sum > hybridmv_thresh) {
1702 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1703 px = predA[0];
1704 py = predA[1];
1705 } else {
1706 px = predC[0];
1707 py = predC[1];
1708 }
b761659b 1709 } else {
cad16562
MSS
1710 if (is_intra[xy - 1])
1711 sum = FFABS(px) + FFABS(py);
1712 else
1713 sum = FFABS(px - predC[0]) + FFABS(py - predC[1]);
1714 if (sum > hybridmv_thresh) {
50f97219 1715 if (get_bits1(&s->gb)) {
cad16562
MSS
1716 px = predA[0];
1717 py = predA[1];
1718 } else {
1719 px = predC[0];
1720 py = predC[1];
1721 }
1722 }
b761659b 1723 }
cad16562
MSS
1724 }
1725 }
1726
1727 if (v->field_mode && !s->quarter_sample) {
1728 r_x <<= 1;
1729 r_y <<= 1;
1730 }
1731 if (v->field_mode && v->numref)
1732 r_y >>= 1;
1733 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1734 y_bias = 1;
1735 /* store MV using signed modulus of MV range defined in 4.11 */
1736 s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1737 s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
50f97219
KS
1738 if (mv1) { /* duplicate motion data for 1-MV block */
1739 s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1740 s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1741 s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1742 s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
cad16562
MSS
1743 s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1744 s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
50f97219 1745 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
cad16562
MSS
1746 v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1747 }
1748}
1749
1750/** Predict and set motion vector for interlaced frame picture MBs
1751 */
50f97219
KS
1752static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1753 int mvn, int r_x, int r_y, uint8_t* is_intra)
cad16562
MSS
1754{
1755 MpegEncContext *s = &v->s;
1756 int xy, wrap, off = 0;
1757 int A[2], B[2], C[2];
1758 int px, py;
1759 int a_valid = 0, b_valid = 0, c_valid = 0;
1760 int field_a, field_b, field_c; // 0: same, 1: opposit
1761 int total_valid, num_samefield, num_oppfield;
1762 int pos_c, pos_b, n_adj;
1763
1764 wrap = s->b8_stride;
1765 xy = s->block_index[n];
1766
50f97219 1767 if (s->mb_intra) {
cad16562
MSS
1768 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1769 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1770 s->current_picture.f.motion_val[1][xy][0] = 0;
1771 s->current_picture.f.motion_val[1][xy][1] = 0;
50f97219
KS
1772 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1773 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1774 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1775 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1776 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
cad16562
MSS
1777 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1778 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1779 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
50f97219
KS
1780 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1781 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1782 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1783 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
cad16562
MSS
1784 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1785 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1786 }
1787 return;
1788 }
1789
1790 off = ((n == 0) || (n == 1)) ? 1 : -1;
1791 /* predict A */
1792 if (s->mb_x || (n == 1) || (n == 3)) {
1793 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
50f97219 1794 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
cad16562
MSS
1795 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1796 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1797 a_valid = 1;
1798 } else { // current block has frame mv and cand. has field MV (so average)
1799 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
50f97219 1800 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
cad16562 1801 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
50f97219 1802 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
cad16562
MSS
1803 a_valid = 1;
1804 }
1805 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1806 a_valid = 0;
1807 A[0] = A[1] = 0;
1808 }
50f97219
KS
1809 } else
1810 A[0] = A[1] = 0;
cad16562
MSS
1811 /* Predict B and C */
1812 B[0] = B[1] = C[0] = C[1] = 0;
1813 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1814 if (!s->first_slice_line) {
1815 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1816 b_valid = 1;
50f97219
KS
1817 n_adj = n | 2;
1818 pos_b = s->block_index[n_adj] - 2 * wrap;
cad16562
MSS
1819 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1820 n_adj = (n & 2) | (n & 1);
1821 }
1822 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1823 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1824 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
50f97219
KS
1825 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1826 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
cad16562
MSS
1827 }
1828 }
1829 if (s->mb_width > 1) {
1830 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1831 c_valid = 1;
50f97219
KS
1832 n_adj = 2;
1833 pos_c = s->block_index[2] - 2 * wrap + 2;
cad16562
MSS
1834 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1835 n_adj = n & 2;
1836 }
50f97219
KS
1837 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1838 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
cad16562
MSS
1839 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1840 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1841 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1842 }
1843 if (s->mb_x == s->mb_width - 1) {
1844 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1845 c_valid = 1;
50f97219
KS
1846 n_adj = 3;
1847 pos_c = s->block_index[3] - 2 * wrap - 2;
cad16562
MSS
1848 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1849 n_adj = n | 1;
1850 }
50f97219
KS
1851 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1852 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
cad16562 1853 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
50f97219
KS
1854 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1855 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
cad16562 1856 }
50f97219
KS
1857 } else
1858 c_valid = 0;
cad16562
MSS
1859 }
1860 }
1861 }
1862 }
1863 } else {
50f97219 1864 pos_b = s->block_index[1];
cad16562 1865 b_valid = 1;
50f97219
KS
1866 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1867 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1868 pos_c = s->block_index[0];
cad16562 1869 c_valid = 1;
50f97219
KS
1870 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1871 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
cad16562
MSS
1872 }
1873
1874 total_valid = a_valid + b_valid + c_valid;
1875 // check if predictor A is out of bounds
50f97219 1876 if (!s->mb_x && !(n == 1 || n == 3)) {
cad16562
MSS
1877 A[0] = A[1] = 0;
1878 }
1879 // check if predictor B is out of bounds
1880 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1881 B[0] = B[1] = C[0] = C[1] = 0;
1882 }
1883 if (!v->blk_mv_type[xy]) {
50f97219 1884 if (s->mb_width == 1) {
cad16562
MSS
1885 px = B[0];
1886 py = B[1];
b761659b 1887 } else {
cad16562
MSS
1888 if (total_valid >= 2) {
1889 px = mid_pred(A[0], B[0], C[0]);
1890 py = mid_pred(A[1], B[1], C[1]);
1891 } else if (total_valid) {
1892 if (a_valid) { px = A[0]; py = A[1]; }
1893 if (b_valid) { px = B[0]; py = B[1]; }
1894 if (c_valid) { px = C[0]; py = C[1]; }
50f97219
KS
1895 } else
1896 px = py = 0;
cad16562
MSS
1897 }
1898 } else {
1899 if (a_valid)
1900 field_a = (A[1] & 4) ? 1 : 0;
1901 else
1902 field_a = 0;
1903 if (b_valid)
1904 field_b = (B[1] & 4) ? 1 : 0;
1905 else
1906 field_b = 0;
1907 if (c_valid)
1908 field_c = (C[1] & 4) ? 1 : 0;
1909 else
1910 field_c = 0;
1911
50f97219 1912 num_oppfield = field_a + field_b + field_c;
cad16562
MSS
1913 num_samefield = total_valid - num_oppfield;
1914 if (total_valid == 3) {
1915 if ((num_samefield == 3) || (num_oppfield == 3)) {
1916 px = mid_pred(A[0], B[0], C[0]);
1917 py = mid_pred(A[1], B[1], C[1]);
1918 } else if (num_samefield >= num_oppfield) {
1919 /* take one MV from same field set depending on priority
1920 the check for B may not be necessary */
50f97219
KS
1921 px = !field_a ? A[0] : B[0];
1922 py = !field_a ? A[1] : B[1];
cad16562 1923 } else {
50f97219
KS
1924 px = field_a ? A[0] : B[0];
1925 py = field_a ? A[1] : B[1];
cad16562
MSS
1926 }
1927 } else if (total_valid == 2) {
1928 if (num_samefield >= num_oppfield) {
1929 if (!field_a && a_valid) {
b761659b
DB
1930 px = A[0];
1931 py = A[1];
cad16562
MSS
1932 } else if (!field_b && b_valid) {
1933 px = B[0];
1934 py = B[1];
1935 } else if (c_valid) {
1936 px = C[0];
1937 py = C[1];
1938 } else px = py = 0;
1939 } else {
1940 if (field_a && a_valid) {
1941 px = A[0];
1942 py = A[1];
1943 } else if (field_b && b_valid) {
1944 px = B[0];
1945 py = B[1];
1946 } else if (c_valid) {
b761659b
DB
1947 px = C[0];
1948 py = C[1];
1949 }
1950 }
cad16562
MSS
1951 } else if (total_valid == 1) {
1952 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1953 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
50f97219
KS
1954 } else
1955 px = py = 0;
b761659b 1956 }
cad16562 1957
b761659b 1958 /* store MV using signed modulus of MV range defined in 4.11 */
657ccb5a
DB
1959 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1960 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
50f97219
KS
1961 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1962 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1963 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1964 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1965 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
657ccb5a
DB
1966 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1967 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
cad16562
MSS
1968 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1969 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1970 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1971 s->mv[0][n + 1][0] = s->mv[0][n][0];
1972 s->mv[0][n + 1][1] = s->mv[0][n][1];
b761659b
DB
1973 }
1974}
1975
1976/** Motion compensation for direct or interpolated blocks in B-frames
1977 */
1978static void vc1_interp_mc(VC1Context *v)
1979{
1980 MpegEncContext *s = &v->s;
1981 DSPContext *dsp = &v->s.dsp;
1982 uint8_t *srcY, *srcU, *srcV;
1983 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
cad16562
MSS
1984 int off, off_uv;
1985 int v_edge_pos = s->v_edge_pos >> v->field_mode;
b761659b 1986
cad16562
MSS
1987 if (!v->field_mode && !v->s.next_picture.f.data[0])
1988 return;
b761659b 1989
50f97219
KS
1990 mx = s->mv[1][0][0];
1991 my = s->mv[1][0][1];
b761659b
DB
1992 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1993 uvmy = (my + ((my & 3) == 3)) >> 1;
cad16562
MSS
1994 if (v->field_mode) {
1995 if (v->cur_field_type != v->ref_field_type[1])
50f97219 1996 my = my - 2 + 4 * v->cur_field_type;
cad16562
MSS
1997 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1998 }
50f97219
KS
1999 if (v->fastuvmc) {
2000 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
2001 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
b761659b 2002 }
657ccb5a
DB
2003 srcY = s->next_picture.f.data[0];
2004 srcU = s->next_picture.f.data[1];
2005 srcV = s->next_picture.f.data[2];
b761659b 2006
50f97219
KS
2007 src_x = s->mb_x * 16 + (mx >> 2);
2008 src_y = s->mb_y * 16 + (my >> 2);
2009 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
2010 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
b761659b 2011
50f97219 2012 if (v->profile != PROFILE_ADVANCED) {
b761659b
DB
2013 src_x = av_clip( src_x, -16, s->mb_width * 16);
2014 src_y = av_clip( src_y, -16, s->mb_height * 16);
2015 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
2016 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
50f97219 2017 } else {
b761659b
DB
2018 src_x = av_clip( src_x, -17, s->avctx->coded_width);
2019 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
2020 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
2021 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
2022 }
2023
50f97219 2024 srcY += src_y * s->linesize + src_x;
b761659b
DB
2025 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
2026 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
2027
cad16562
MSS
2028 if (v->field_mode && v->ref_field_type[1]) {
2029 srcY += s->current_picture_ptr->f.linesize[0];
2030 srcU += s->current_picture_ptr->f.linesize[1];
2031 srcV += s->current_picture_ptr->f.linesize[2];
2032 }
2033
b761659b 2034 /* for grayscale we should not try to read from unknown area */
50f97219 2035 if (s->flags & CODEC_FLAG_GRAY) {
b761659b
DB
2036 srcU = s->edge_emu_buffer + 18 * s->linesize;
2037 srcV = s->edge_emu_buffer + 18 * s->linesize;
2038 }
2039
50f97219
KS
2040 if (v->rangeredfrm
2041 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 16 - s->mspel * 3
2042 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my & 3) - 16 - s->mspel * 3) {
2043 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
b761659b
DB
2044
2045 srcY -= s->mspel * (1 + s->linesize);
50f97219
KS
2046 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
2047 17 + s->mspel * 2, 17 + s->mspel * 2,
2048 src_x - s->mspel, src_y - s->mspel,
2049 s->h_edge_pos, v_edge_pos);
b761659b 2050 srcY = s->edge_emu_buffer;
50f97219
KS
2051 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
2052 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
2053 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
2054 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
b761659b
DB
2055 srcU = uvbuf;
2056 srcV = uvbuf + 16;
2057 /* if we deal with range reduction we need to scale source blocks */
50f97219 2058 if (v->rangeredfrm) {
b761659b
DB
2059 int i, j;
2060 uint8_t *src, *src2;
2061
2062 src = srcY;
50f97219
KS
2063 for (j = 0; j < 17 + s->mspel * 2; j++) {
2064 for (i = 0; i < 17 + s->mspel * 2; i++)
2065 src[i] = ((src[i] - 128) >> 1) + 128;
b761659b
DB
2066 src += s->linesize;
2067 }
50f97219
KS
2068 src = srcU;
2069 src2 = srcV;
2070 for (j = 0; j < 9; j++) {
2071 for (i = 0; i < 9; i++) {
2072 src[i] = ((src[i] - 128) >> 1) + 128;
b761659b
DB
2073 src2[i] = ((src2[i] - 128) >> 1) + 128;
2074 }
50f97219 2075 src += s->uvlinesize;
b761659b
DB
2076 src2 += s->uvlinesize;
2077 }
2078 }
2079 srcY += s->mspel * (1 + s->linesize);
2080 }
2081
cad16562 2082 if (v->field_mode && v->cur_field_type) {
50f97219 2083 off = s->current_picture_ptr->f.linesize[0];
cad16562
MSS
2084 off_uv = s->current_picture_ptr->f.linesize[1];
2085 } else {
50f97219 2086 off = 0;
cad16562
MSS
2087 off_uv = 0;
2088 }
2089
50f97219 2090 if (s->mspel) {
b761659b 2091 dxy = ((my & 3) << 2) | (mx & 3);
cad16562
MSS
2092 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2093 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
b761659b 2094 srcY += s->linesize * 8;
cad16562
MSS
2095 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2096 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
b761659b
DB
2097 } else { // hpel mc
2098 dxy = (my & 2) | ((mx & 2) >> 1);
2099
50f97219 2100 if (!v->rnd)
cad16562 2101 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
b761659b 2102 else
cad16562 2103 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
b761659b
DB
2104 }
2105
50f97219 2106 if (s->flags & CODEC_FLAG_GRAY) return;
b761659b 2107 /* Chroma MC always uses qpel blilinear */
50f97219
KS
2108 uvmx = (uvmx & 3) << 1;
2109 uvmy = (uvmy & 3) << 1;
2110 if (!v->rnd) {
cad16562
MSS
2111 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2112 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
50f97219 2113 } else {
cad16562
MSS
2114 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2115 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
b761659b
DB
2116 }
2117}
2118
2119static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2120{
2121 int n = bfrac;
2122
2123#if B_FRACTION_DEN==256
50f97219 2124 if (inv)
b761659b 2125 n -= 256;
50f97219 2126 if (!qs)
b761659b
DB
2127 return 2 * ((value * n + 255) >> 9);
2128 return (value * n + 128) >> 8;
2129#else
50f97219 2130 if (inv)
b761659b 2131 n -= B_FRACTION_DEN;
50f97219 2132 if (!qs)
b761659b
DB
2133 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2134 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2135#endif
2136}
2137
50f97219
KS
2138static av_always_inline int scale_mv_intfi(int value, int bfrac, int inv,
2139 int qs, int qs_last)
cad16562
MSS
2140{
2141 int n = bfrac;
2142
2143 if (inv)
2144 n -= 256;
2145 n <<= !qs_last;
2146 if (!qs)
2147 return (value * n + 255) >> 9;
2148 else
2149 return (value * n + 128) >> 8;
2150}
2151
b761659b
DB
2152/** Reconstruct motion vector for B-frame and do motion compensation
2153 */
50f97219
KS
2154static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2155 int direct, int mode)
b761659b 2156{
50f97219 2157 if (v->use_ic) {
b761659b 2158 v->mv_mode2 = v->mv_mode;
50f97219 2159 v->mv_mode = MV_PMODE_INTENSITY_COMP;
b761659b 2160 }
50f97219 2161 if (direct) {
b761659b
DB
2162 vc1_mc_1mv(v, 0);
2163 vc1_interp_mc(v);
50f97219
KS
2164 if (v->use_ic)
2165 v->mv_mode = v->mv_mode2;
b761659b
DB
2166 return;
2167 }
50f97219 2168 if (mode == BMV_TYPE_INTERPOLATED) {
b761659b
DB
2169 vc1_mc_1mv(v, 0);
2170 vc1_interp_mc(v);
50f97219
KS
2171 if (v->use_ic)
2172 v->mv_mode = v->mv_mode2;
b761659b
DB
2173 return;
2174 }
2175
50f97219
KS
2176 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2177 v->mv_mode = v->mv_mode2;
b761659b 2178 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
50f97219
KS
2179 if (v->use_ic)
2180 v->mv_mode = v->mv_mode2;
b761659b
DB
2181}
2182
50f97219
KS
2183static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2184 int direct, int mvtype)
b761659b
DB
2185{
2186 MpegEncContext *s = &v->s;
2187 int xy, wrap, off = 0;
2188 int16_t *A, *B, *C;
2189 int px, py;
2190 int sum;
2191 int r_x, r_y;
2192 const uint8_t *is_intra = v->mb_type[0];
2193
2194 r_x = v->range_x;
2195 r_y = v->range_y;
2196 /* scale MV difference to be quad-pel */
2197 dmv_x[0] <<= 1 - s->quarter_sample;
2198 dmv_y[0] <<= 1 - s->quarter_sample;
2199 dmv_x[1] <<= 1 - s->quarter_sample;
2200 dmv_y[1] <<= 1 - s->quarter_sample;
2201
2202 wrap = s->b8_stride;
2203 xy = s->block_index[0];
2204
50f97219 2205 if (s->mb_intra) {
cad16562
MSS
2206 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2207 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2208 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2209 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
b761659b
DB
2210 return;
2211 }
cad16562
MSS
2212 if (!v->field_mode) {
2213 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2214 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2215 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2216 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
b761659b 2217
cad16562
MSS
2218 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2219 s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
2220 s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
2221 s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
2222 s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
2223 }
50f97219 2224 if (direct) {
cad16562
MSS
2225 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2226 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2227 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2228 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
b761659b
DB
2229 return;
2230 }
2231
50f97219
KS
2232 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2233 C = s->current_picture.f.motion_val[0][xy - 2];
2234 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
b761659b 2235 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
50f97219 2236 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
b761659b 2237
50f97219
KS
2238 if (!s->mb_x) C[0] = C[1] = 0;
2239 if (!s->first_slice_line) { // predictor A is not out of bounds
2240 if (s->mb_width == 1) {
b761659b
DB
2241 px = A[0];
2242 py = A[1];
2243 } else {
2244 px = mid_pred(A[0], B[0], C[0]);
2245 py = mid_pred(A[1], B[1], C[1]);
2246 }
50f97219 2247 } else if (s->mb_x) { // predictor C is not out of bounds
b761659b
DB
2248 px = C[0];
2249 py = C[1];
2250 } else {
2251 px = py = 0;
2252 }
2253 /* Pullback MV as specified in 8.3.5.3.4 */
2254 {
2255 int qx, qy, X, Y;
50f97219 2256 if (v->profile < PROFILE_ADVANCED) {
b761659b
DB
2257 qx = (s->mb_x << 5);
2258 qy = (s->mb_y << 5);
50f97219
KS
2259 X = (s->mb_width << 5) - 4;
2260 Y = (s->mb_height << 5) - 4;
2261 if (qx + px < -28) px = -28 - qx;
2262 if (qy + py < -28) py = -28 - qy;
2263 if (qx + px > X) px = X - qx;
2264 if (qy + py > Y) py = Y - qy;
b761659b
DB
2265 } else {
2266 qx = (s->mb_x << 6);
2267 qy = (s->mb_y << 6);
50f97219
KS
2268 X = (s->mb_width << 6) - 4;
2269 Y = (s->mb_height << 6) - 4;
2270 if (qx + px < -60) px = -60 - qx;
2271 if (qy + py < -60) py = -60 - qy;
2272 if (qx + px > X) px = X - qx;
2273 if (qy + py > Y) py = Y - qy;
b761659b
DB
2274 }
2275 }
2276 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
50f97219
KS
2277 if (0 && !s->first_slice_line && s->mb_x) {
2278 if (is_intra[xy - wrap])
b761659b
DB
2279 sum = FFABS(px) + FFABS(py);
2280 else
2281 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
50f97219
KS
2282 if (sum > 32) {
2283 if (get_bits1(&s->gb)) {
b761659b
DB
2284 px = A[0];
2285 py = A[1];
2286 } else {
2287 px = C[0];
2288 py = C[1];
2289 }
2290 } else {
50f97219 2291 if (is_intra[xy - 2])
b761659b
DB
2292 sum = FFABS(px) + FFABS(py);
2293 else
2294 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
50f97219
KS
2295 if (sum > 32) {
2296 if (get_bits1(&s->gb)) {
b761659b
DB
2297 px = A[0];
2298 py = A[1];
2299 } else {
2300 px = C[0];
2301 py = C[1];
2302 }
2303 }
2304 }
2305 }
2306 /* store MV using signed modulus of MV range defined in 4.11 */
2307 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2308 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2309 }
50f97219
KS
2310 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2311 C = s->current_picture.f.motion_val[1][xy - 2];
2312 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
b761659b 2313 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
50f97219 2314 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
b761659b 2315
50f97219
KS
2316 if (!s->mb_x)
2317 C[0] = C[1] = 0;
2318 if (!s->first_slice_line) { // predictor A is not out of bounds
2319 if (s->mb_width == 1) {
b761659b
DB
2320 px = A[0];
2321 py = A[1];
2322 } else {
2323 px = mid_pred(A[0], B[0], C[0]);
2324 py = mid_pred(A[1], B[1], C[1]);
2325 }
50f97219 2326 } else if (s->mb_x) { // predictor C is not out of bounds
b761659b
DB
2327 px = C[0];
2328 py = C[1];
2329 } else {
2330 px = py = 0;
2331 }
2332 /* Pullback MV as specified in 8.3.5.3.4 */
2333 {
2334 int qx, qy, X, Y;
50f97219 2335 if (v->profile < PROFILE_ADVANCED) {
b761659b
DB
2336 qx = (s->mb_x << 5);
2337 qy = (s->mb_y << 5);
50f97219
KS
2338 X = (s->mb_width << 5) - 4;
2339 Y = (s->mb_height << 5) - 4;
2340 if (qx + px < -28) px = -28 - qx;
2341 if (qy + py < -28) py = -28 - qy;
2342 if (qx + px > X) px = X - qx;
2343 if (qy + py > Y) py = Y - qy;
b761659b
DB
2344 } else {
2345 qx = (s->mb_x << 6);
2346 qy = (s->mb_y << 6);
50f97219
KS
2347 X = (s->mb_width << 6) - 4;
2348 Y = (s->mb_height << 6) - 4;
2349 if (qx + px < -60) px = -60 - qx;
2350 if (qy + py < -60) py = -60 - qy;
2351 if (qx + px > X) px = X - qx;
2352 if (qy + py > Y) py = Y - qy;
b761659b
DB
2353 }
2354 }
2355 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
50f97219
KS
2356 if (0 && !s->first_slice_line && s->mb_x) {
2357 if (is_intra[xy - wrap])
b761659b
DB
2358 sum = FFABS(px) + FFABS(py);
2359 else
2360 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
50f97219
KS
2361 if (sum > 32) {
2362 if (get_bits1(&s->gb)) {
b761659b
DB
2363 px = A[0];
2364 py = A[1];
2365 } else {
2366 px = C[0];
2367 py = C[1];
2368 }
2369 } else {
50f97219 2370 if (is_intra[xy - 2])
b761659b
DB
2371 sum = FFABS(px) + FFABS(py);
2372 else
2373 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
50f97219
KS
2374 if (sum > 32) {
2375 if (get_bits1(&s->gb)) {
b761659b
DB
2376 px = A[0];
2377 py = A[1];
2378 } else {
2379 px = C[0];
2380 py = C[1];
2381 }
2382 }
2383 }
2384 }
2385 /* store MV using signed modulus of MV range defined in 4.11 */
2386
2387 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2388 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2389 }
657ccb5a
DB
2390 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2391 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2392 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2393 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
b761659b
DB
2394}
2395
cad16562
MSS
2396static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2397{
2398 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2399 MpegEncContext *s = &v->s;
2400 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2401
2402 if (v->bmvtype == BMV_TYPE_DIRECT) {
2403 int total_opp, k, f;
2404 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
50f97219
KS
2405 s->mv[0][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2406 v->bfraction, 0, s->quarter_sample, v->qs_last);
2407 s->mv[0][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2408 v->bfraction, 0, s->quarter_sample, v->qs_last);
2409 s->mv[1][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2410 v->bfraction, 1, s->quarter_sample, v->qs_last);
2411 s->mv[1][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2412 v->bfraction, 1, s->quarter_sample, v->qs_last);
cad16562
MSS
2413
2414 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2415 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2416 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2417 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2418 f = (total_opp > 2) ? 1 : 0;
2419 } else {
2420 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2421 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2422 f = 0;
2423 }
2424 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2425 for (k = 0; k < 4; k++) {
2426 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2427 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2428 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2429 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2430 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2431 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2432 }
2433 return;
2434 }
2435 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2436 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
2437 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
2438 return;
2439 }
2440 if (dir) { // backward
2441 vc1_pred_mv(v, n, dmv_x[1], dmv_y[1], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
2442 if (n == 3 || mv1) {
50f97219 2443 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
cad16562
MSS
2444 }
2445 } else { // forward
2446 vc1_pred_mv(v, n, dmv_x[0], dmv_y[0], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
2447 if (n == 3 || mv1) {
50f97219 2448 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
cad16562
MSS
2449 }
2450 }
2451}
2452
b761659b
DB
2453/** Get predicted DC value for I-frames only
2454 * prediction dir: left=0, top=1
2455 * @param s MpegEncContext
2456 * @param overlap flag indicating that overlap filtering is used
2457 * @param pq integer part of picture quantizer
2458 * @param[in] n block index in the current MB
2459 * @param dc_val_ptr Pointer to DC predictor
2460 * @param dir_ptr Prediction direction for use in AC prediction
2461 */
2462static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
50f97219 2463 int16_t **dc_val_ptr, int *dir_ptr)
b761659b
DB
2464{
2465 int a, b, c, wrap, pred, scale;
2466 int16_t *dc_val;
2467 static const uint16_t dcpred[32] = {
50f97219
KS
2468 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2469 114, 102, 93, 85, 79, 73, 68, 64,
2470 60, 57, 54, 51, 49, 47, 45, 43,
2471 41, 39, 38, 37, 35, 34, 33
b761659b
DB
2472 };
2473
2474 /* find prediction - wmv3_dc_scale always used here in fact */
50f97219
KS
2475 if (n < 4) scale = s->y_dc_scale;
2476 else scale = s->c_dc_scale;
b761659b 2477
50f97219
KS
2478 wrap = s->block_wrap[n];
2479 dc_val = s->dc_val[0] + s->block_index[n];
b761659b
DB
2480
2481 /* B A
2482 * C X
2483 */
2484 c = dc_val[ - 1];
2485 b = dc_val[ - 1 - wrap];
2486 a = dc_val[ - wrap];
2487
50f97219 2488 if (pq < 9 || !overlap) {
b761659b 2489 /* Set outer values */
50f97219
KS
2490 if (s->first_slice_line && (n != 2 && n != 3))
2491 b = a = dcpred[scale];
2492 if (s->mb_x == 0 && (n != 1 && n != 3))
2493 b = c = dcpred[scale];
2494 } else {
b761659b 2495 /* Set outer values */
50f97219
KS
2496 if (s->first_slice_line && (n != 2 && n != 3))
2497 b = a = 0;
2498 if (s->mb_x == 0 && (n != 1 && n != 3))
2499 b = c = 0;
b761659b
DB
2500 }
2501
2502 if (abs(a - b) <= abs(b - c)) {
50f97219
KS
2503 pred = c;
2504 *dir_ptr = 1; // left
b761659b 2505 } else {
50f97219
KS
2506 pred = a;
2507 *dir_ptr = 0; // top
b761659b
DB
2508 }
2509
2510 /* update predictor */
2511 *dc_val_ptr = &dc_val[0];
2512 return pred;
2513}
2514
2515
2516/** Get predicted DC value
2517 * prediction dir: left=0, top=1
2518 * @param s MpegEncContext
2519 * @param overlap flag indicating that overlap filtering is used
2520 * @param pq integer part of picture quantizer
2521 * @param[in] n block index in the current MB
2522 * @param a_avail flag indicating top block availability
2523 * @param c_avail flag indicating left block availability
2524 * @param dc_val_ptr Pointer to DC predictor
2525 * @param dir_ptr Prediction direction for use in AC prediction
2526 */
2527static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2528 int a_avail, int c_avail,
2529 int16_t **dc_val_ptr, int *dir_ptr)
2530{
2531 int a, b, c, wrap, pred;
2532 int16_t *dc_val;
2533 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2534 int q1, q2 = 0;
2535
2536 wrap = s->block_wrap[n];
50f97219 2537 dc_val = s->dc_val[0] + s->block_index[n];
b761659b
DB
2538
2539 /* B A
2540 * C X
2541 */
2542 c = dc_val[ - 1];
2543 b = dc_val[ - 1 - wrap];
2544 a = dc_val[ - wrap];
2545 /* scale predictors if needed */
657ccb5a 2546 q1 = s->current_picture.f.qscale_table[mb_pos];
50f97219 2547 if (c_avail && (n != 1 && n != 3)) {
657ccb5a 2548 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
50f97219 2549 if (q2 && q2 != q1)
b761659b
DB
2550 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2551 }
50f97219 2552 if (a_avail && (n != 2 && n != 3)) {
657ccb5a 2553 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
50f97219 2554 if (q2 && q2 != q1)
b761659b
DB
2555 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2556 }
50f97219 2557 if (a_avail && c_avail && (n != 3)) {
b761659b 2558 int off = mb_pos;
50f97219
KS
2559 if (n != 1)
2560 off--;
2561 if (n != 2)
2562 off -= s->mb_stride;
657ccb5a 2563 q2 = s->current_picture.f.qscale_table[off];
50f97219 2564 if (q2 && q2 != q1)
b761659b
DB
2565 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2566 }
2567
50f97219
KS
2568 if (a_avail && c_avail) {
2569 if (abs(a - b) <= abs(b - c)) {
2570 pred = c;
2571 *dir_ptr = 1; // left
b761659b 2572 } else {
50f97219
KS
2573 pred = a;
2574 *dir_ptr = 0; // top
2575 }
2576 } else if (a_avail) {
2577 pred = a;
2578 *dir_ptr = 0; // top
2579 } else if (c_avail) {
2580 pred = c;
2581 *dir_ptr = 1; // left
b761659b 2582 } else {
50f97219
KS
2583 pred = 0;
2584 *dir_ptr = 1; // left
b761659b
DB
2585 }
2586
2587 /* update predictor */
2588 *dc_val_ptr = &dc_val[0];
2589 return pred;
2590}
2591
2592/** @} */ // Block group
2593
2594/**
21a19b79 2595 * @name VC1 Macroblock-level functions in Simple/Main Profiles
b761659b
DB
2596 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2597 * @{
2598 */
2599
50f97219
KS
2600static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2601 uint8_t **coded_block_ptr)
b761659b
DB
2602{
2603 int xy, wrap, pred, a, b, c;
2604
50f97219 2605 xy = s->block_index[n];
b761659b
DB
2606 wrap = s->b8_stride;
2607
2608 /* B C
2609 * A X
2610 */
2611 a = s->coded_block[xy - 1 ];
2612 b = s->coded_block[xy - 1 - wrap];
2613 c = s->coded_block[xy - wrap];
2614
2615 if (b == c) {
2616 pred = a;
2617 } else {
2618 pred = c;
2619 }
2620
2621 /* store value */
2622 *coded_block_ptr = &s->coded_block[xy];
2623
2624 return pred;
2625}
2626
2627/**
2628 * Decode one AC coefficient
2629 * @param v The VC1 context
2630 * @param last Last coefficient
2631 * @param skip How much zero coefficients to skip
2632 * @param value Decoded AC coefficient value
2633 * @param codingset set of VLC to decode data
2634 * @see 8.1.3.4
2635 */
50f97219
KS
2636static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2637 int *value, int codingset)
b761659b
DB
2638{
2639 GetBitContext *gb = &v->s.gb;
2640 int index, escape, run = 0, level = 0, lst = 0;
2641
2642 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2643 if (index != vc1_ac_sizes[codingset] - 1) {
50f97219 2644 run = vc1_index_decode_table[codingset][index][0];
b761659b 2645 level = vc1_index_decode_table[codingset][index][1];
50f97219
KS
2646 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2647 if (get_bits1(gb))
b761659b
DB
2648 level = -level;
2649 } else {
2650 escape = decode210(gb);
2651 if (escape != 2) {
2652 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
50f97219 2653 run = vc1_index_decode_table[codingset][index][0];
b761659b 2654 level = vc1_index_decode_table[codingset][index][1];
50f97219
KS
2655 lst = index >= vc1_last_decode_table[codingset];
2656 if (escape == 0) {
2657 if (lst)
b761659b
DB
2658 level += vc1_last_delta_level_table[codingset][run];
2659 else
2660 level += vc1_delta_level_table[codingset][run];
2661 } else {
50f97219 2662 if (lst)
b761659b
DB
2663 run += vc1_last_delta_run_table[codingset][level] + 1;
2664 else
2665 run += vc1_delta_run_table[codingset][level] + 1;
2666 }
50f97219 2667 if (get_bits1(gb))
b761659b
DB
2668 level = -level;
2669 } else {
2670 int sign;
2671 lst = get_bits1(gb);
50f97219
KS
2672 if (v->s.esc3_level_length == 0) {
2673 if (v->pq < 8 || v->dquantfrm) { // table 59
b761659b 2674 v->s.esc3_level_length = get_bits(gb, 3);
50f97219 2675 if (!v->s.esc3_level_length)
b761659b 2676 v->s.esc3_level_length = get_bits(gb, 2) + 8;
50f97219 2677 } else { // table 60
b761659b
DB
2678 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2679 }
2680 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2681 }
50f97219
KS
2682 run = get_bits(gb, v->s.esc3_run_length);
2683 sign = get_bits1(gb);
b761659b 2684 level = get_bits(gb, v->s.esc3_level_length);
50f97219 2685 if (sign)
b761659b
DB
2686 level = -level;
2687 }
2688 }
2689
50f97219
KS
2690 *last = lst;
2691 *skip = run;
b761659b
DB
2692 *value = level;
2693}
2694
2695/** Decode intra block in intra frames - should be faster than decode_intra_block
2696 * @param v VC1Context
2697 * @param block block to decode
2698 * @param[in] n subblock index
2699 * @param coded are AC coeffs present or not
2700 * @param codingset set of VLC to decode data
2701 */
50f97219
KS
2702static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2703 int coded, int codingset)
b761659b
DB
2704{
2705 GetBitContext *gb = &v->s.gb;
2706 MpegEncContext *s = &v->s;
2707 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2708 int i;
2709 int16_t *dc_val;
2710 int16_t *ac_val, *ac_val2;
2711 int dcdiff;
2712
2713 /* Get DC differential */
2714 if (n < 4) {
2715 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2716 } else {
2717 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2718 }
50f97219 2719 if (dcdiff < 0) {
b761659b
DB
2720 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2721 return -1;
2722 }
50f97219
KS
2723 if (dcdiff) {
2724 if (dcdiff == 119 /* ESC index value */) {
b761659b 2725 /* TODO: Optimize */
50f97219 2726 if (v->pq == 1) dcdiff = get_bits(gb, 10);
b761659b 2727 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
50f97219
KS
2728 else dcdiff = get_bits(gb, 8);
2729 } else {
b761659b 2730 if (v->pq == 1)
50f97219 2731 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
b761659b 2732 else if (v->pq == 2)
50f97219 2733 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
b761659b
DB
2734 }
2735 if (get_bits1(gb))
2736 dcdiff = -dcdiff;
2737 }
2738
2739 /* Prediction */
2740 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2741 *dc_val = dcdiff;
2742
2743 /* Store the quantized DC coeff, used for prediction */
2744 if (n < 4) {
2745 block[0] = dcdiff * s->y_dc_scale;
2746 } else {
2747 block[0] = dcdiff * s->c_dc_scale;
2748 }
2749 /* Skip ? */
2750 if (!coded) {
2751 goto not_coded;
2752 }
2753
50f97219 2754 // AC Decoding
b761659b
DB
2755 i = 1;
2756
2757 {
2758 int last = 0, skip, value;
0724a674 2759 const uint8_t *zz_table;
b761659b
DB
2760 int scale;
2761 int k;
2762
2763 scale = v->pq * 2 + v->halfpq;
2764
50f97219
KS
2765 if (v->s.ac_pred) {
2766 if (!dc_pred_dir)
1da6ea39 2767 zz_table = v->zz_8x8[2];
b761659b 2768 else
1da6ea39 2769 zz_table = v->zz_8x8[3];
b761659b 2770 } else
1da6ea39 2771 zz_table = v->zz_8x8[1];
b761659b 2772
50f97219 2773 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
b761659b 2774 ac_val2 = ac_val;
50f97219 2775 if (dc_pred_dir) // left
b761659b 2776 ac_val -= 16;
50f97219 2777 else // top
b761659b
DB
2778 ac_val -= 16 * s->block_wrap[n];
2779
2780 while (!last) {
2781 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2782 i += skip;
50f97219 2783 if (i > 63)
b761659b
DB
2784 break;
2785 block[zz_table[i++]] = value;
2786 }
2787
2788 /* apply AC prediction if needed */
50f97219
KS
2789 if (s->ac_pred) {
2790 if (dc_pred_dir) { // left
2791 for (k = 1; k < 8; k++)
58bb6b7d 2792 block[k << v->left_blk_sh] += ac_val[k];
50f97219
KS
2793 } else { // top
2794 for (k = 1; k < 8; k++)
58bb6b7d 2795 block[k << v->top_blk_sh] += ac_val[k + 8];
b761659b
DB
2796 }
2797 }
2798 /* save AC coeffs for further prediction */
50f97219 2799 for (k = 1; k < 8; k++) {
58bb6b7d
RB
2800 ac_val2[k] = block[k << v->left_blk_sh];
2801 ac_val2[k + 8] = block[k << v->top_blk_sh];
b761659b
DB
2802 }
2803
2804 /* scale AC coeffs */
50f97219
KS
2805 for (k = 1; k < 64; k++)
2806 if (block[k]) {
b761659b 2807 block[k] *= scale;
50f97219 2808 if (!v->pquantizer)
b761659b
DB
2809 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2810 }
2811
50f97219 2812 if (s->ac_pred) i = 63;
b761659b
DB
2813 }
2814
2815not_coded:
50f97219 2816 if (!coded) {
b761659b 2817 int k, scale;
50f97219 2818 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
b761659b
DB
2819 ac_val2 = ac_val;
2820
2821 i = 0;
2822 scale = v->pq * 2 + v->halfpq;
2823 memset(ac_val2, 0, 16 * 2);
50f97219 2824 if (dc_pred_dir) { // left
b761659b 2825 ac_val -= 16;
50f97219 2826 if (s->ac_pred)
b761659b 2827 memcpy(ac_val2, ac_val, 8 * 2);
50f97219 2828 } else { // top
b761659b 2829 ac_val -= 16 * s->block_wrap[n];
50f97219 2830 if (s->ac_pred)
b761659b
DB
2831 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2832 }
2833
2834 /* apply AC prediction if needed */
50f97219
KS
2835 if (s->ac_pred) {
2836 if (dc_pred_dir) { //left
2837 for (k = 1; k < 8; k++) {
58bb6b7d 2838 block[k << v->left_blk_sh] = ac_val[k] * scale;
50f97219 2839 if (!v->pquantizer && block[k << v->left_blk_sh])
58bb6b7d 2840 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
b761659b 2841 }
50f97219
KS
2842 } else { // top
2843 for (k = 1; k < 8; k++) {
58bb6b7d 2844 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
50f97219 2845 if (!v->pquantizer && block[k << v->top_blk_sh])
58bb6b7d 2846 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
b761659b
DB
2847 }
2848 }
2849 i = 63;
2850 }
2851 }
2852 s->block_last_index[n] = i;
2853
2854 return 0;
2855}
2856
2857/** Decode intra block in intra frames - should be faster than decode_intra_block
2858 * @param v VC1Context
2859 * @param block block to decode
2860 * @param[in] n subblock number
2861 * @param coded are AC coeffs present or not
2862 * @param codingset set of VLC to decode data
2863 * @param mquant quantizer value for this macroblock
2864 */
50f97219
KS
2865static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2866 int coded, int codingset, int mquant)
b761659b
DB
2867{
2868 GetBitContext *gb = &v->s.gb;
2869 MpegEncContext *s = &v->s;
2870 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2871 int i;
2872 int16_t *dc_val;
2873 int16_t *ac_val, *ac_val2;
2874 int dcdiff;
2875 int a_avail = v->a_avail, c_avail = v->c_avail;
2876 int use_pred = s->ac_pred;
2877 int scale;
2878 int q1, q2 = 0;
2879 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2880
2881 /* Get DC differential */
2882 if (n < 4) {
2883 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2884 } else {
2885 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2886 }
50f97219 2887 if (dcdiff < 0) {
b761659b
DB
2888 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2889 return -1;
2890 }
50f97219
KS
2891 if (dcdiff) {
2892 if (dcdiff == 119 /* ESC index value */) {
b761659b 2893 /* TODO: Optimize */
50f97219 2894 if (mquant == 1) dcdiff = get_bits(gb, 10);
b761659b 2895 else if (mquant == 2) dcdiff = get_bits(gb, 9);
50f97219
KS
2896 else dcdiff = get_bits(gb, 8);
2897 } else {
b761659b 2898 if (mquant == 1)
50f97219 2899 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
b761659b 2900 else if (mquant == 2)
50f97219 2901 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
b761659b
DB
2902 }
2903 if (get_bits1(gb))
2904 dcdiff = -dcdiff;
2905 }
2906
2907 /* Prediction */
2908 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2909 *dc_val = dcdiff;
2910
2911 /* Store the quantized DC coeff, used for prediction */
2912 if (n < 4) {
2913 block[0] = dcdiff * s->y_dc_scale;
2914 } else {
2915 block[0] = dcdiff * s->c_dc_scale;
2916 }
2917
2918 //AC Decoding
2919 i = 1;
2920
2921 /* check if AC is needed at all */
50f97219
KS
2922 if (!a_avail && !c_avail)
2923 use_pred = 0;
2924 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
b761659b
DB
2925 ac_val2 = ac_val;
2926
2927 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2928
50f97219 2929 if (dc_pred_dir) // left
b761659b 2930 ac_val -= 16;
50f97219 2931 else // top
b761659b
DB
2932 ac_val -= 16 * s->block_wrap[n];
2933
657ccb5a 2934 q1 = s->current_picture.f.qscale_table[mb_pos];
50f97219
KS
2935 if ( dc_pred_dir && c_avail && mb_pos)
2936 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2937 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2938 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2939 if ( dc_pred_dir && n == 1)
2940 q2 = q1;
2941 if (!dc_pred_dir && n == 2)
2942 q2 = q1;
2943 if (n == 3)
2944 q2 = q1;
2945
2946 if (coded) {
b761659b 2947 int last = 0, skip, value;
0724a674 2948 const uint8_t *zz_table;
b761659b
DB
2949 int k;
2950
50f97219 2951 if (v->s.ac_pred) {
cad16562
MSS
2952 if (!use_pred && v->fcm == 1) {
2953 zz_table = v->zzi_8x8;
2954 } else {
50f97219 2955 if (!dc_pred_dir) // top
cad16562 2956 zz_table = v->zz_8x8[2];
50f97219 2957 else // left
cad16562
MSS
2958 zz_table = v->zz_8x8[3];
2959 }
2960 } else {
2961 if (v->fcm != 1)
2962 zz_table = v->zz_8x8[1];
b761659b 2963 else
cad16562
MSS
2964 zz_table = v->zzi_8x8;
2965 }
b761659b
DB
2966
2967 while (!last) {
2968 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2969 i += skip;
50f97219 2970 if (i > 63)
b761659b
DB
2971 break;
2972 block[zz_table[i++]] = value;
2973 }
2974
2975 /* apply AC prediction if needed */
50f97219 2976 if (use_pred) {
b761659b 2977 /* scale predictors if needed*/
50f97219 2978 if (q2 && q1 != q2) {
b761659b
DB
2979 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2980 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2981
50f97219
KS
2982 if (dc_pred_dir) { // left
2983 for (k = 1; k < 8; k++)
58bb6b7d 2984 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
50f97219
KS
2985 } else { // top
2986 for (k = 1; k < 8; k++)
58bb6b7d 2987 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
b761659b
DB
2988 }
2989 } else {
50f97219
KS
2990 if (dc_pred_dir) { //left
2991 for (k = 1; k < 8; k++)
58bb6b7d 2992 block[k << v->left_blk_sh] += ac_val[k];
b761659b 2993 } else { //top
50f97219 2994 for (k = 1; k < 8; k++)
58bb6b7d 2995 block[k << v->top_blk_sh] += ac_val[k + 8];
b761659b
DB
2996 }
2997 }
2998 }
2999 /* save AC coeffs for further prediction */
50f97219 3000 for (k = 1; k < 8; k++) {
58bb6b7d
RB
3001 ac_val2[k ] = block[k << v->left_blk_sh];
3002 ac_val2[k + 8] = block[k << v->top_blk_sh];
b761659b
DB
3003 }
3004
3005 /* scale AC coeffs */
50f97219
KS
3006 for (k = 1; k < 64; k++)
3007 if (block[k]) {
b761659b 3008 block[k] *= scale;
50f97219 3009 if (!v->pquantizer)
b761659b
DB
3010 block[k] += (block[k] < 0) ? -mquant : mquant;
3011 }
3012
50f97219 3013 if (use_pred) i = 63;
b761659b
DB
3014 } else { // no AC coeffs
3015 int k;
3016
3017 memset(ac_val2, 0, 16 * 2);
50f97219
KS
3018 if (dc_pred_dir) { // left
3019 if (use_pred) {
b761659b 3020 memcpy(ac_val2, ac_val, 8 * 2);
50f97219 3021 if (q2 && q1 != q2) {
b761659b
DB
3022 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3023 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
50f97219 3024 for (k = 1; k < 8; k++)
b761659b
DB
3025 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3026 }
3027 }
50f97219
KS
3028 } else { // top
3029 if (use_pred) {
b761659b 3030 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
50f97219 3031 if (q2 && q1 != q2) {
b761659b
DB
3032 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3033 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
50f97219 3034 for (k = 1; k < 8; k++)
b761659b
DB
3035 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3036 }
3037 }
3038 }
3039
3040 /* apply AC prediction if needed */
50f97219
KS
3041 if (use_pred) {
3042 if (dc_pred_dir) { // left
3043 for (k = 1; k < 8; k++) {
58bb6b7d 3044 block[k << v->left_blk_sh] = ac_val2[k] * scale;
50f97219 3045 if (!v->pquantizer && block[k << v->left_blk_sh])
58bb6b7d 3046 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
b761659b 3047 }
50f97219
KS
3048 } else { // top
3049 for (k = 1; k < 8; k++) {
58bb6b7d 3050 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
50f97219 3051 if (!v->pquantizer && block[k << v->top_blk_sh])
58bb6b7d 3052 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
b761659b
DB
3053 }
3054 }
3055 i = 63;
3056 }
3057 }
3058 s->block_last_index[n] = i;
3059
3060 return 0;
3061}
3062
3063/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
3064 * @param v VC1Context
3065 * @param block block to decode
3066 * @param[in] n subblock index
3067 * @param coded are AC coeffs present or not
3068 * @param mquant block quantizer
3069 * @param codingset set of VLC to decode data
3070 */
50f97219
KS
3071static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
3072 int coded, int mquant, int codingset)
b761659b
DB
3073{
3074 GetBitContext *gb = &v->s.gb;
3075 MpegEncContext *s = &v->s;
3076 int dc_pred_dir = 0; /* Direction of the DC prediction used */
3077 int i;
3078 int16_t *dc_val;
3079 int16_t *ac_val, *ac_val2;
3080 int dcdiff;
3081 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3082 int a_avail = v->a_avail, c_avail = v->c_avail;
3083 int use_pred = s->ac_pred;
3084 int scale;
3085 int q1, q2 = 0;
3086
010f98f9
JGG
3087 s->dsp.clear_block(block);
3088
b761659b 3089 /* XXX: Guard against dumb values of mquant */
50f97219 3090 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
b761659b
DB
3091
3092 /* Set DC scale - y and c use the same */
3093 s->y_dc_scale = s->y_dc_scale_table[mquant];
3094 s->c_dc_scale = s->c_dc_scale_table[mquant];
3095
3096 /* Get DC differential */
3097 if (n < 4) {
3098 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3099 } else {
3100 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3101 }
50f97219 3102 if (dcdiff < 0) {
b761659b
DB
3103 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3104 return -1;
3105 }
50f97219
KS
3106 if (dcdiff) {
3107 if (dcdiff == 119 /* ESC index value */) {
b761659b 3108 /* TODO: Optimize */
50f97219 3109 if (mquant == 1) dcdiff = get_bits(gb, 10);
b761659b 3110 else if (mquant == 2) dcdiff = get_bits(gb, 9);
50f97219
KS
3111 else dcdiff = get_bits(gb, 8);
3112 } else {
b761659b 3113 if (mquant == 1)
50f97219 3114 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
b761659b 3115 else if (mquant == 2)
50f97219 3116 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
b761659b
DB
3117 }
3118 if (get_bits1(gb))
3119 dcdiff = -dcdiff;
3120 }
3121
3122 /* Prediction */
3123 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3124 *dc_val = dcdiff;
3125
3126 /* Store the quantized DC coeff, used for prediction */
3127
3128 if (n < 4) {
3129 block[0] = dcdiff * s->y_dc_scale;
3130 } else {
3131 block[0] = dcdiff * s->c_dc_scale;
3132 }
3133
3134 //AC Decoding
3135 i = 1;
3136
3137 /* check if AC is needed at all and adjust direction if needed */
50f97219
KS
3138 if (!a_avail) dc_pred_dir = 1;
3139 if (!c_avail) dc_pred_dir = 0;
3140 if (!a_avail && !c_avail) use_pred = 0;
b761659b
DB
3141 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3142 ac_val2 = ac_val;
3143
3144 scale = mquant * 2 + v->halfpq;
3145
50f97219 3146 if (dc_pred_dir) //left
b761659b
DB
3147 ac_val -= 16;
3148 else //top
3149 ac_val -= 16 * s->block_wrap[n];
3150
657ccb5a 3151 q1 = s->current_picture.f.qscale_table[mb_pos];
50f97219
KS
3152 if (dc_pred_dir && c_avail && mb_pos)
3153 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3154 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3155 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3156 if ( dc_pred_dir && n == 1)
3157 q2 = q1;
3158 if (!dc_pred_dir && n == 2)
3159 q2 = q1;
3160 if (n == 3) q2 = q1;
b761659b 3161
50f97219 3162 if (coded) {
b761659b 3163 int last = 0, skip, value;
b761659b
DB
3164 int k;
3165
b761659b
DB
3166 while (!last) {
3167 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3168 i += skip;
50f97219 3169 if (i > 63)
b761659b 3170 break;
cad16562
MSS
3171 if (v->fcm == 0)
3172 block[v->zz_8x8[0][i++]] = value;
3173 else {
50f97219
KS
3174 if (use_pred && (v->fcm == 1)) {
3175 if (!dc_pred_dir) // top
cad16562 3176 block[v->zz_8x8[2][i++]] = value;
50f97219 3177 else // left
cad16562
MSS
3178 block[v->zz_8x8[3][i++]] = value;
3179 } else {
3180 block[v->zzi_8x8[i++]] = value;
3181 }
3182 }
b761659b
DB
3183 }
3184
3185 /* apply AC prediction if needed */
50f97219 3186 if (use_pred) {
b761659b 3187 /* scale predictors if needed*/
50f97219 3188 if (q2 && q1 != q2) {
b761659b
DB
3189 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3190 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3191
50f97219
KS
3192 if (dc_pred_dir) { // left
3193 for (k = 1; k < 8; k++)
58bb6b7d 3194 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
b761659b 3195 } else { //top
50f97219 3196 for (k = 1; k < 8; k++)
58bb6b7d 3197 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
b761659b
DB
3198 }
3199 } else {
50f97219
KS
3200 if (dc_pred_dir) { // left
3201 for (k = 1; k < 8; k++)
58bb6b7d 3202 block[k << v->left_blk_sh] += ac_val[k];
50f97219
KS
3203 } else { // top
3204 for (k = 1; k < 8; k++)
58bb6b7d 3205 block[k << v->top_blk_sh] += ac_val[k + 8];
b761659b
DB
3206 }
3207 }
3208 }
3209 /* save AC coeffs for further prediction */
50f97219 3210 for (k = 1; k < 8; k++) {
58bb6b7d
RB
3211 ac_val2[k ] = block[k << v->left_blk_sh];
3212 ac_val2[k + 8] = block[k << v->top_blk_sh];
b761659b
DB
3213 }
3214
3215 /* scale AC coeffs */
50f97219
KS
3216 for (k = 1; k < 64; k++)
3217 if (block[k]) {
b761659b 3218 block[k] *= scale;
50f97219 3219 if (!v->pquantizer)
b761659b
DB
3220 block[k] += (block[k] < 0) ? -mquant : mquant;
3221 }
3222
50f97219 3223 if (use_pred) i = 63;
b761659b
DB
3224 } else { // no AC coeffs
3225 int k;
3226
3227 memset(ac_val2, 0, 16 * 2);
50f97219
KS
3228 if (dc_pred_dir) { // left
3229 if (use_pred) {
b761659b 3230 memcpy(ac_val2, ac_val, 8 * 2);
50f97219 3231 if (q2 && q1 != q2) {
b761659b
DB
3232 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3233 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
50f97219 3234 for (k = 1; k < 8; k++)
b761659b
DB
3235 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3236 }
3237 }
50f97219
KS
3238 } else { // top
3239 if (use_pred) {
b761659b 3240 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
50f97219 3241 if (q2 && q1 != q2) {
b761659b
DB
3242 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3243 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
50f97219 3244 for (k = 1; k < 8; k++)
b761659b
DB
3245 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3246 }
3247 }
3248 }
3249
3250 /* apply AC prediction if needed */
50f97219
KS
3251 if (use_pred) {
3252 if (dc_pred_dir) { // left
3253 for (k = 1; k < 8; k++) {
58bb6b7d 3254 block[k << v->left_blk_sh] = ac_val2[k] * scale;
50f97219 3255 if (!v->pquantizer && block[k << v->left_blk_sh])
58bb6b7d 3256 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
b761659b 3257 }
50f97219
KS
3258 } else { // top
3259 for (k = 1; k < 8; k++) {
58bb6b7d 3260 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
50f97219 3261 if (!v->pquantizer && block[k << v->top_blk_sh])
58bb6b7d 3262 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
b761659b
DB
3263 }
3264 }
3265 i = 63;
3266 }
3267 }
3268 s->block_last_index[n] = i;
3269
3270 return 0;
3271}
3272
3273/** Decode P block
3274 */
50f97219
KS
3275static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3276 int mquant, int ttmb, int first_block,
3277 uint8_t *dst, int linesize, int skip_block,
3278 int *ttmb_out)
b761659b
DB
3279{
3280 MpegEncContext *s = &v->s;
3281 GetBitContext *gb = &s->gb;
3282 int i, j;
3283 int subblkpat = 0;
3284 int scale, off, idx, last, skip, value;
3285 int ttblk = ttmb & 7;
3286 int pat = 0;
3287
010f98f9
JGG
3288 s->dsp.clear_block(block);
3289
50f97219 3290 if (ttmb == -1) {
b761659b
DB
3291 ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
3292 }
50f97219 3293 if (ttblk == TT_4X4) {
b761659b
DB
3294 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3295 }
50f97219 3296 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
42ff9d7a
KS
3297 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3298 || (!v->res_rtm_flag && !first_block))) {
b761659b 3299 subblkpat = decode012(gb);
50f97219
KS
3300 if (subblkpat)
3301 subblkpat ^= 3; // swap decoded pattern bits
3302 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3303 ttblk = TT_8X4;
3304 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3305 ttblk = TT_4X8;